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: TYBLE16_simple_data_logger TYBLE16_MP3_Air
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 #ifdef LWIP_HOOK_FILENAME 00055 #include LWIP_HOOK_FILENAME 00056 #endif 00057 00058 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be 00059 * used to modify and send a response packet (and to 1 if this is not the case, 00060 * e.g. when link header is stripped off when receiving) */ 00061 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 00062 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1 00063 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ 00064 00065 /* The amount of data from the original packet to return in a dest-unreachable */ 00066 #define ICMP_DEST_UNREACH_DATASIZE 8 00067 00068 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code); 00069 00070 /** 00071 * Processes ICMP input packets, called from ip_input(). 00072 * 00073 * Currently only processes icmp echo requests and sends 00074 * out the echo response. 00075 * 00076 * @param p the icmp echo request packet, p->payload pointing to the icmp header 00077 * @param inp the netif on which this packet was received 00078 */ 00079 void 00080 icmp_input(struct pbuf *p, struct netif *inp) 00081 { 00082 u8_t type; 00083 #ifdef LWIP_DEBUG 00084 u8_t code; 00085 #endif /* LWIP_DEBUG */ 00086 struct icmp_echo_hdr *iecho; 00087 const struct ip_hdr *iphdr_in; 00088 u16_t hlen; 00089 const ip4_addr_t *src; 00090 00091 ICMP_STATS_INC(icmp.recv); 00092 MIB2_STATS_INC(mib2.icmpinmsgs); 00093 00094 iphdr_in = ip4_current_header(); 00095 hlen = IPH_HL_BYTES(iphdr_in); 00096 if (hlen < IP_HLEN) { 00097 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen)); 00098 goto lenerr; 00099 } 00100 if (p->len < sizeof(u16_t) * 2) { 00101 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); 00102 goto lenerr; 00103 } 00104 00105 type = *((u8_t *)p->payload); 00106 #ifdef LWIP_DEBUG 00107 code = *(((u8_t *)p->payload) + 1); 00108 /* if debug is enabled but debug statement below is somehow disabled: */ 00109 LWIP_UNUSED_ARG(code); 00110 #endif /* LWIP_DEBUG */ 00111 switch (type) { 00112 case ICMP_ER: 00113 /* This is OK, echo reply might have been parsed by a raw PCB 00114 (as obviously, an echo request has been sent, too). */ 00115 MIB2_STATS_INC(mib2.icmpinechoreps); 00116 break; 00117 case ICMP_ECHO: 00118 MIB2_STATS_INC(mib2.icmpinechos); 00119 src = ip4_current_dest_addr(); 00120 /* multicast destination address? */ 00121 if (ip4_addr_ismulticast(ip4_current_dest_addr())) { 00122 #if LWIP_MULTICAST_PING 00123 /* For multicast, use address of receiving interface as source address */ 00124 src = netif_ip4_addr(inp); 00125 #else /* LWIP_MULTICAST_PING */ 00126 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n")); 00127 goto icmperr; 00128 #endif /* LWIP_MULTICAST_PING */ 00129 } 00130 /* broadcast destination address? */ 00131 if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) { 00132 #if LWIP_BROADCAST_PING 00133 /* For broadcast, use address of receiving interface as source address */ 00134 src = netif_ip4_addr(inp); 00135 #else /* LWIP_BROADCAST_PING */ 00136 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n")); 00137 goto icmperr; 00138 #endif /* LWIP_BROADCAST_PING */ 00139 } 00140 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); 00141 if (p->tot_len < sizeof(struct icmp_echo_hdr)) { 00142 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); 00143 goto lenerr; 00144 } 00145 #if CHECKSUM_CHECK_ICMP 00146 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) { 00147 if (inet_chksum_pbuf(p) != 0) { 00148 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); 00149 pbuf_free(p); 00150 ICMP_STATS_INC(icmp.chkerr); 00151 MIB2_STATS_INC(mib2.icmpinerrors); 00152 return; 00153 } 00154 } 00155 #endif 00156 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 00157 if (pbuf_add_header(p, hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN)) { 00158 /* p is not big enough to contain link headers 00159 * allocate a new one and copy p into it 00160 */ 00161 struct pbuf *r; 00162 u16_t alloc_len = (u16_t)(p->tot_len + hlen); 00163 if (alloc_len < p->tot_len) { 00164 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed (tot_len overflow)\n")); 00165 goto icmperr; 00166 } 00167 /* allocate new packet buffer with space for link headers */ 00168 r = pbuf_alloc(PBUF_LINK, alloc_len, PBUF_RAM); 00169 if (r == NULL) { 00170 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); 00171 goto icmperr; 00172 } 00173 if (r->len < hlen + sizeof(struct icmp_echo_hdr)) { 00174 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header")); 00175 pbuf_free(r); 00176 goto icmperr; 00177 } 00178 /* copy the ip header */ 00179 MEMCPY(r->payload, iphdr_in, hlen); 00180 /* switch r->payload back to icmp header (cannot fail) */ 00181 if (pbuf_remove_header(r, hlen)) { 00182 LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0); 00183 pbuf_free(r); 00184 goto icmperr; 00185 } 00186 /* copy the rest of the packet without ip header */ 00187 if (pbuf_copy(r, p) != ERR_OK) { 00188 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed")); 00189 pbuf_free(r); 00190 goto icmperr; 00191 } 00192 /* free the original p */ 00193 pbuf_free(p); 00194 /* we now have an identical copy of p that has room for link headers */ 00195 p = r; 00196 } else { 00197 /* restore p->payload to point to icmp header (cannot fail) */ 00198 if (pbuf_remove_header(p, hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN)) { 00199 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); 00200 goto icmperr; 00201 } 00202 } 00203 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ 00204 /* At this point, all checks are OK. */ 00205 /* We generate an answer by switching the dest and src ip addresses, 00206 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ 00207 iecho = (struct icmp_echo_hdr *)p->payload; 00208 if (pbuf_add_header(p, hlen)) { 00209 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet")); 00210 } else { 00211 err_t ret; 00212 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; 00213 ip4_addr_copy(iphdr->src, *src); 00214 ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); 00215 ICMPH_TYPE_SET(iecho, ICMP_ER); 00216 #if CHECKSUM_GEN_ICMP 00217 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) { 00218 /* adjust the checksum */ 00219 if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { 00220 iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS((u16_t)(ICMP_ECHO << 8)) + 1); 00221 } else { 00222 iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS(ICMP_ECHO << 8)); 00223 } 00224 } 00225 #if LWIP_CHECKSUM_CTRL_PER_NETIF 00226 else { 00227 iecho->chksum = 0; 00228 } 00229 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ 00230 #else /* CHECKSUM_GEN_ICMP */ 00231 iecho->chksum = 0; 00232 #endif /* CHECKSUM_GEN_ICMP */ 00233 00234 /* Set the correct TTL and recalculate the header checksum. */ 00235 IPH_TTL_SET(iphdr, ICMP_TTL); 00236 IPH_CHKSUM_SET(iphdr, 0); 00237 #if CHECKSUM_GEN_IP 00238 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) { 00239 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen)); 00240 } 00241 #endif /* CHECKSUM_GEN_IP */ 00242 00243 ICMP_STATS_INC(icmp.xmit); 00244 /* increase number of messages attempted to send */ 00245 MIB2_STATS_INC(mib2.icmpoutmsgs); 00246 /* increase number of echo replies attempted to send */ 00247 MIB2_STATS_INC(mib2.icmpoutechoreps); 00248 00249 /* send an ICMP packet */ 00250 ret = ip4_output_if(p, src, LWIP_IP_HDRINCL, 00251 ICMP_TTL, 0, IP_PROTO_ICMP, inp); 00252 if (ret != ERR_OK) { 00253 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret))); 00254 } 00255 } 00256 break; 00257 default: 00258 if (type == ICMP_DUR) { 00259 MIB2_STATS_INC(mib2.icmpindestunreachs); 00260 } else if (type == ICMP_TE) { 00261 MIB2_STATS_INC(mib2.icmpintimeexcds); 00262 } else if (type == ICMP_PP) { 00263 MIB2_STATS_INC(mib2.icmpinparmprobs); 00264 } else if (type == ICMP_SQ) { 00265 MIB2_STATS_INC(mib2.icmpinsrcquenchs); 00266 } else if (type == ICMP_RD) { 00267 MIB2_STATS_INC(mib2.icmpinredirects); 00268 } else if (type == ICMP_TS) { 00269 MIB2_STATS_INC(mib2.icmpintimestamps); 00270 } else if (type == ICMP_TSR) { 00271 MIB2_STATS_INC(mib2.icmpintimestampreps); 00272 } else if (type == ICMP_AM) { 00273 MIB2_STATS_INC(mib2.icmpinaddrmasks); 00274 } else if (type == ICMP_AMR) { 00275 MIB2_STATS_INC(mib2.icmpinaddrmaskreps); 00276 } 00277 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 00278 (s16_t)type, (s16_t)code)); 00279 ICMP_STATS_INC(icmp.proterr); 00280 ICMP_STATS_INC(icmp.drop); 00281 } 00282 pbuf_free(p); 00283 return; 00284 lenerr: 00285 pbuf_free(p); 00286 ICMP_STATS_INC(icmp.lenerr); 00287 MIB2_STATS_INC(mib2.icmpinerrors); 00288 return; 00289 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING 00290 icmperr: 00291 pbuf_free(p); 00292 ICMP_STATS_INC(icmp.err); 00293 MIB2_STATS_INC(mib2.icmpinerrors); 00294 return; 00295 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */ 00296 } 00297 00298 /** 00299 * Send an icmp 'destination unreachable' packet, called from ip_input() if 00300 * the transport layer protocol is unknown and from udp_input() if the local 00301 * port is not bound. 00302 * 00303 * @param p the input packet for which the 'unreachable' should be sent, 00304 * p->payload pointing to the IP header 00305 * @param t type of the 'unreachable' packet 00306 */ 00307 void 00308 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t) 00309 { 00310 MIB2_STATS_INC(mib2.icmpoutdestunreachs); 00311 icmp_send_response(p, ICMP_DUR, t); 00312 } 00313 00314 #if IP_FORWARD || IP_REASSEMBLY 00315 /** 00316 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0. 00317 * 00318 * @param p the input packet for which the 'time exceeded' should be sent, 00319 * p->payload pointing to the IP header 00320 * @param t type of the 'time exceeded' packet 00321 */ 00322 void 00323 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t) 00324 { 00325 MIB2_STATS_INC(mib2.icmpouttimeexcds); 00326 icmp_send_response(p, ICMP_TE, t); 00327 } 00328 00329 #endif /* IP_FORWARD || IP_REASSEMBLY */ 00330 00331 /** 00332 * Send an icmp packet in response to an incoming packet. 00333 * 00334 * @param p the input packet for which the 'unreachable' should be sent, 00335 * p->payload pointing to the IP header 00336 * @param type Type of the ICMP header 00337 * @param code Code of the ICMP header 00338 */ 00339 static void 00340 icmp_send_response(struct pbuf *p, u8_t type, u8_t code) 00341 { 00342 struct pbuf *q; 00343 struct ip_hdr *iphdr; 00344 /* we can use the echo header here */ 00345 struct icmp_echo_hdr *icmphdr; 00346 ip4_addr_t iphdr_src; 00347 struct netif *netif; 00348 00349 /* increase number of messages attempted to send */ 00350 MIB2_STATS_INC(mib2.icmpoutmsgs); 00351 00352 /* ICMP header + IP header + 8 bytes of data */ 00353 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE, 00354 PBUF_RAM); 00355 if (q == NULL) { 00356 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n")); 00357 MIB2_STATS_INC(mib2.icmpouterrors); 00358 return; 00359 } 00360 LWIP_ASSERT("check that first pbuf can hold icmp message", 00361 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE))); 00362 00363 iphdr = (struct ip_hdr *)p->payload; 00364 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from ")); 00365 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src); 00366 LWIP_DEBUGF(ICMP_DEBUG, (" to ")); 00367 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest); 00368 LWIP_DEBUGF(ICMP_DEBUG, ("\n")); 00369 00370 icmphdr = (struct icmp_echo_hdr *)q->payload; 00371 icmphdr->type = type; 00372 icmphdr->code = code; 00373 icmphdr->id = 0; 00374 icmphdr->seqno = 0; 00375 00376 /* copy fields from original packet */ 00377 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload, 00378 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE); 00379 00380 ip4_addr_copy(iphdr_src, iphdr->src); 00381 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 00382 { 00383 ip4_addr_t iphdr_dst; 00384 ip4_addr_copy(iphdr_dst, iphdr->dest); 00385 netif = ip4_route_src(&iphdr_dst, &iphdr_src); 00386 } 00387 #else 00388 netif = ip4_route(&iphdr_src); 00389 #endif 00390 if (netif != NULL) { 00391 /* calculate checksum */ 00392 icmphdr->chksum = 0; 00393 #if CHECKSUM_GEN_ICMP 00394 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) { 00395 icmphdr->chksum = inet_chksum(icmphdr, q->len); 00396 } 00397 #endif 00398 ICMP_STATS_INC(icmp.xmit); 00399 ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif); 00400 } 00401 pbuf_free(q); 00402 } 00403 00404 #endif /* LWIP_IPV4 && LWIP_ICMP */
Generated on Tue Jul 12 2022 13:54:28 by
