Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
lwip_icmp.c
00001 /** 00002 * @file 00003 * ICMP - Internet Control Message Protocol 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 /* Some ICMP messages should be passed to the transport protocols. This 00040 is not implemented. */ 00041 00042 #include "lwip/opt.h" 00043 00044 #if LWIP_IPV4 && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */ 00045 00046 #include "lwip/icmp.h" 00047 #include "lwip/inet_chksum.h" 00048 #include "lwip/ip.h" 00049 #include "lwip/def.h" 00050 #include "lwip/stats.h" 00051 00052 #include <string.h> 00053 00054 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be 00055 * used to modify and send a response packet (and to 1 if this is not the case, 00056 * e.g. when link header is stripped of when receiving) */ 00057 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 00058 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1 00059 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ 00060 00061 /* The amount of data from the original packet to return in a dest-unreachable */ 00062 #define ICMP_DEST_UNREACH_DATASIZE 8 00063 00064 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code); 00065 00066 /** 00067 * Processes ICMP input packets, called from ip_input(). 00068 * 00069 * Currently only processes icmp echo requests and sends 00070 * out the echo response. 00071 * 00072 * @param p the icmp echo request packet, p->payload pointing to the icmp header 00073 * @param inp the netif on which this packet was received 00074 */ 00075 void 00076 icmp_input(struct pbuf *p, struct netif *inp) 00077 { 00078 u8_t type; 00079 #ifdef LWIP_DEBUG 00080 u8_t code; 00081 #endif /* LWIP_DEBUG */ 00082 struct icmp_echo_hdr *iecho; 00083 const struct ip_hdr *iphdr_in; 00084 s16_t hlen; 00085 const ip4_addr_t* src; 00086 00087 ICMP_STATS_INC(icmp.recv); 00088 MIB2_STATS_INC(mib2.icmpinmsgs); 00089 00090 iphdr_in = ip4_current_header(); 00091 hlen = IPH_HL(iphdr_in) * 4; 00092 if (hlen < IP_HLEN) { 00093 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen)); 00094 goto lenerr; 00095 } 00096 if (p->len < sizeof(u16_t)*2) { 00097 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); 00098 goto lenerr; 00099 } 00100 00101 type = *((u8_t *)p->payload); 00102 #ifdef LWIP_DEBUG 00103 code = *(((u8_t *)p->payload)+1); 00104 #endif /* LWIP_DEBUG */ 00105 switch (type) { 00106 case ICMP_ER: 00107 /* This is OK, echo reply might have been parsed by a raw PCB 00108 (as obviously, an echo request has been sent, too). */ 00109 MIB2_STATS_INC(mib2.icmpinechoreps); 00110 break; 00111 case ICMP_ECHO: 00112 MIB2_STATS_INC(mib2.icmpinechos); 00113 src = ip4_current_dest_addr(); 00114 /* multicast destination address? */ 00115 if (ip4_addr_ismulticast(ip4_current_dest_addr())) { 00116 #if LWIP_MULTICAST_PING 00117 /* For multicast, use address of receiving interface as source address */ 00118 src = netif_ip4_addr(inp); 00119 #else /* LWIP_MULTICAST_PING */ 00120 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n")); 00121 goto icmperr; 00122 #endif /* LWIP_MULTICAST_PING */ 00123 } 00124 /* broadcast destination address? */ 00125 if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) { 00126 #if LWIP_BROADCAST_PING 00127 /* For broadcast, use address of receiving interface as source address */ 00128 src = netif_ip4_addr(inp); 00129 #else /* LWIP_BROADCAST_PING */ 00130 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n")); 00131 goto icmperr; 00132 #endif /* LWIP_BROADCAST_PING */ 00133 } 00134 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); 00135 if (p->tot_len < sizeof(struct icmp_echo_hdr)) { 00136 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); 00137 goto lenerr; 00138 } 00139 #if CHECKSUM_CHECK_ICMP 00140 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) { 00141 if (inet_chksum_pbuf(p) != 0) { 00142 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); 00143 pbuf_free(p); 00144 ICMP_STATS_INC(icmp.chkerr); 00145 MIB2_STATS_INC(mib2.icmpinerrors); 00146 return; 00147 } 00148 } 00149 #endif 00150 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 00151 if (pbuf_header(p, (hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { 00152 /* p is not big enough to contain link headers 00153 * allocate a new one and copy p into it 00154 */ 00155 struct pbuf *r; 00156 /* allocate new packet buffer with space for link headers */ 00157 r = pbuf_alloc(PBUF_LINK, p->tot_len + hlen, PBUF_RAM); 00158 if (r == NULL) { 00159 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); 00160 goto icmperr; 00161 } 00162 if (r->len < hlen + sizeof(struct icmp_echo_hdr)) { 00163 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header")); 00164 pbuf_free(r); 00165 goto icmperr; 00166 } 00167 /* copy the ip header */ 00168 MEMCPY(r->payload, iphdr_in, hlen); 00169 /* switch r->payload back to icmp header (cannot fail) */ 00170 if (pbuf_header(r, -hlen)) { 00171 LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0); 00172 pbuf_free(r); 00173 goto icmperr; 00174 } 00175 /* copy the rest of the packet without ip header */ 00176 if (pbuf_copy(r, p) != ERR_OK) { 00177 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed")); 00178 pbuf_free(r); 00179 goto icmperr; 00180 } 00181 /* free the original p */ 00182 pbuf_free(p); 00183 /* we now have an identical copy of p that has room for link headers */ 00184 p = r; 00185 } else { 00186 /* restore p->payload to point to icmp header (cannot fail) */ 00187 if (pbuf_header(p, -(s16_t)(hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { 00188 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); 00189 goto icmperr; 00190 } 00191 } 00192 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ 00193 /* At this point, all checks are OK. */ 00194 /* We generate an answer by switching the dest and src ip addresses, 00195 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ 00196 iecho = (struct icmp_echo_hdr *)p->payload; 00197 if (pbuf_header(p, hlen)) { 00198 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet")); 00199 } else { 00200 err_t ret; 00201 struct ip_hdr *iphdr = (struct ip_hdr*)p->payload; 00202 ip4_addr_copy(iphdr->src, *src); 00203 ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); 00204 ICMPH_TYPE_SET(iecho, ICMP_ER); 00205 #if CHECKSUM_GEN_ICMP 00206 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) { 00207 /* adjust the checksum */ 00208 if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { 00209 iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; 00210 } else { 00211 iecho->chksum += PP_HTONS(ICMP_ECHO << 8); 00212 } 00213 } 00214 #if LWIP_CHECKSUM_CTRL_PER_NETIF 00215 else { 00216 iecho->chksum = 0; 00217 } 00218 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ 00219 #else /* CHECKSUM_GEN_ICMP */ 00220 iecho->chksum = 0; 00221 #endif /* CHECKSUM_GEN_ICMP */ 00222 00223 /* Set the correct TTL and recalculate the header checksum. */ 00224 IPH_TTL_SET(iphdr, ICMP_TTL); 00225 IPH_CHKSUM_SET(iphdr, 0); 00226 #if CHECKSUM_GEN_IP 00227 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) { 00228 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen)); 00229 } 00230 #endif /* CHECKSUM_GEN_IP */ 00231 00232 ICMP_STATS_INC(icmp.xmit); 00233 /* increase number of messages attempted to send */ 00234 MIB2_STATS_INC(mib2.icmpoutmsgs); 00235 /* increase number of echo replies attempted to send */ 00236 MIB2_STATS_INC(mib2.icmpoutechoreps); 00237 00238 /* send an ICMP packet */ 00239 ret = ip4_output_if(p, src, IP_HDRINCL, 00240 ICMP_TTL, 0, IP_PROTO_ICMP, inp); 00241 if (ret != ERR_OK) { 00242 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret))); 00243 } 00244 } 00245 break; 00246 default: 00247 if (type == ICMP_DUR) { 00248 MIB2_STATS_INC(mib2.icmpindestunreachs); 00249 } else if (type == ICMP_TE) { 00250 MIB2_STATS_INC(mib2.icmpindestunreachs); 00251 } else if (type == ICMP_PP) { 00252 MIB2_STATS_INC(mib2.icmpinparmprobs); 00253 } else if (type == ICMP_SQ) { 00254 MIB2_STATS_INC(mib2.icmpinsrcquenchs); 00255 } else if (type == ICMP_RD) { 00256 MIB2_STATS_INC(mib2.icmpinredirects); 00257 } else if (type == ICMP_TS) { 00258 MIB2_STATS_INC(mib2.icmpintimestamps); 00259 } else if (type == ICMP_TSR) { 00260 MIB2_STATS_INC(mib2.icmpintimestampreps); 00261 } else if (type == ICMP_AM) { 00262 MIB2_STATS_INC(mib2.icmpinaddrmasks); 00263 } else if (type == ICMP_AMR) { 00264 MIB2_STATS_INC(mib2.icmpinaddrmaskreps); 00265 } 00266 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 00267 (s16_t)type, (s16_t)code)); 00268 ICMP_STATS_INC(icmp.proterr); 00269 ICMP_STATS_INC(icmp.drop); 00270 } 00271 pbuf_free(p); 00272 return; 00273 lenerr: 00274 pbuf_free(p); 00275 ICMP_STATS_INC(icmp.lenerr); 00276 MIB2_STATS_INC(mib2.icmpinerrors); 00277 return; 00278 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING 00279 icmperr: 00280 pbuf_free(p); 00281 ICMP_STATS_INC(icmp.err); 00282 MIB2_STATS_INC(mib2.icmpinerrors); 00283 return; 00284 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */ 00285 } 00286 00287 /** 00288 * Send an icmp 'destination unreachable' packet, called from ip_input() if 00289 * the transport layer protocol is unknown and from udp_input() if the local 00290 * port is not bound. 00291 * 00292 * @param p the input packet for which the 'unreachable' should be sent, 00293 * p->payload pointing to the IP header 00294 * @param t type of the 'unreachable' packet 00295 */ 00296 void 00297 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t) 00298 { 00299 MIB2_STATS_INC(mib2.icmpoutdestunreachs); 00300 icmp_send_response(p, ICMP_DUR, t); 00301 } 00302 00303 #if IP_FORWARD || IP_REASSEMBLY 00304 /** 00305 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0. 00306 * 00307 * @param p the input packet for which the 'time exceeded' should be sent, 00308 * p->payload pointing to the IP header 00309 * @param t type of the 'time exceeded' packet 00310 */ 00311 void 00312 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t) 00313 { 00314 MIB2_STATS_INC(mib2.icmpouttimeexcds); 00315 icmp_send_response(p, ICMP_TE, t); 00316 } 00317 00318 #endif /* IP_FORWARD || IP_REASSEMBLY */ 00319 00320 /** 00321 * Send an icmp packet in response to an incoming packet. 00322 * 00323 * @param p the input packet for which the 'unreachable' should be sent, 00324 * p->payload pointing to the IP header 00325 * @param type Type of the ICMP header 00326 * @param code Code of the ICMP header 00327 */ 00328 static void 00329 icmp_send_response(struct pbuf *p, u8_t type, u8_t code) 00330 { 00331 struct pbuf *q; 00332 struct ip_hdr *iphdr; 00333 /* we can use the echo header here */ 00334 struct icmp_echo_hdr *icmphdr; 00335 ip4_addr_t iphdr_src; 00336 struct netif *netif; 00337 00338 /* increase number of messages attempted to send */ 00339 MIB2_STATS_INC(mib2.icmpoutmsgs); 00340 00341 /* ICMP header + IP header + 8 bytes of data */ 00342 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE, 00343 PBUF_RAM); 00344 if (q == NULL) { 00345 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n")); 00346 MIB2_STATS_INC(mib2.icmpouterrors); 00347 return; 00348 } 00349 LWIP_ASSERT("check that first pbuf can hold icmp message", 00350 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE))); 00351 00352 iphdr = (struct ip_hdr *)p->payload; 00353 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from ")); 00354 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src); 00355 LWIP_DEBUGF(ICMP_DEBUG, (" to ")); 00356 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest); 00357 LWIP_DEBUGF(ICMP_DEBUG, ("\n")); 00358 00359 icmphdr = (struct icmp_echo_hdr *)q->payload; 00360 icmphdr->type = type; 00361 icmphdr->code = code; 00362 icmphdr->id = 0; 00363 icmphdr->seqno = 0; 00364 00365 /* copy fields from original packet */ 00366 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload, 00367 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE); 00368 00369 ip4_addr_copy(iphdr_src, iphdr->src); 00370 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 00371 { 00372 ip4_addr_t iphdr_dst; 00373 ip4_addr_copy(iphdr_dst, iphdr->dest); 00374 netif = ip4_route_src(&iphdr_src, &iphdr_dst); 00375 } 00376 #else 00377 netif = ip4_route(&iphdr_src); 00378 #endif 00379 if (netif != NULL) { 00380 /* calculate checksum */ 00381 icmphdr->chksum = 0; 00382 #if CHECKSUM_GEN_ICMP 00383 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) { 00384 icmphdr->chksum = inet_chksum(icmphdr, q->len); 00385 } 00386 #endif 00387 ICMP_STATS_INC(icmp.xmit); 00388 ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif); 00389 } 00390 pbuf_free(q); 00391 } 00392 00393 #endif /* LWIP_IPV4 && LWIP_ICMP */
Generated on Tue Jul 12 2022 11:02:41 by
