A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers icmp.c Source File

icmp.c

Go to the documentation of this file.
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_ICMP /* don't build if not configured for use in lwipopts.h */
00045 
00046 #include "lwip/icmp.h"
00047 #include "lwip/inet.h"
00048 #include "lwip/inet_chksum.h"
00049 #include "lwip/ip.h"
00050 #include "lwip/def.h"
00051 #include "lwip/stats.h"
00052 #include "lwip/snmp.h"
00053 
00054 #include <string.h>
00055 
00056 /* The amount of data from the original packet to return in a dest-unreachable */
00057 #define ICMP_DEST_UNREACH_DATASIZE 8
00058 
00059 /**
00060  * Processes ICMP input packets, called from ip_input().
00061  *
00062  * Currently only processes icmp echo requests and sends
00063  * out the echo response.
00064  *
00065  * @param p the icmp echo request packet, p->payload pointing to the ip header
00066  * @param inp the netif on which this packet was received
00067  */
00068 void
00069 icmp_input(struct pbuf *p, struct netif *inp)
00070 {
00071   u8_t type;
00072 #ifdef LWIP_DEBUG
00073   u8_t code;
00074 #endif /* LWIP_DEBUG */
00075   struct icmp_echo_hdr *iecho;
00076   struct ip_hdr *iphdr;
00077   struct ip_addr tmpaddr;
00078   s16_t hlen;
00079 
00080   ICMP_STATS_INC(icmp.recv);
00081   snmp_inc_icmpinmsgs();
00082 
00083 
00084   iphdr = static_cast<struct ip_hdr *>(p->payload);
00085   hlen = IPH_HL(iphdr) * 4;
00086   if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
00087     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
00088     goto lenerr;
00089   }
00090 
00091   type = *((u8_t *)p->payload);
00092 #ifdef LWIP_DEBUG
00093   code = *(((u8_t *)p->payload)+1);
00094 #endif /* LWIP_DEBUG */
00095   switch (type) {
00096   case ICMP_ECHO:
00097     /* broadcast or multicast destination address? */
00098     if (ip_addr_isbroadcast(&(iphdr->dest), inp) || ip_addr_ismulticast(&iphdr->dest)) {
00099       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
00100       ICMP_STATS_INC(icmp.err);
00101       pbuf_free(p);
00102       return;
00103     }
00104     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
00105     if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
00106       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
00107       goto lenerr;
00108     }
00109     if (inet_chksum_pbuf(p) != 0) {
00110       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
00111       pbuf_free(p);
00112       ICMP_STATS_INC(icmp.chkerr);
00113       snmp_inc_icmpinerrors();
00114       return;
00115     }
00116     if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
00117       /* p is not big enough to contain link headers
00118        * allocate a new one and copy p into it
00119        */
00120       struct pbuf *r;
00121       /* switch p->payload to ip header */
00122       if (pbuf_header(p, hlen)) {
00123         LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
00124         goto memerr;
00125       }
00126       /* allocate new packet buffer with space for link headers */
00127       r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
00128       if (r == NULL) {
00129         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
00130         goto memerr;
00131       }
00132       LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
00133                   (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
00134       /* copy the whole packet including ip header */
00135       if (pbuf_copy(r, p) != ERR_OK) {
00136         LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
00137         goto memerr;
00138       }
00139       iphdr = static_cast<struct ip_hdr *>(r->payload);
00140       /* switch r->payload back to icmp header */
00141       if (pbuf_header(r, -hlen)) {
00142         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
00143         goto memerr;
00144       }
00145       /* free the original p */
00146       pbuf_free(p);
00147       /* we now have an identical copy of p that has room for link headers */
00148       p = r;
00149     } else {
00150       /* restore p->payload to point to icmp header */
00151       if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
00152         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
00153         goto memerr;
00154       }
00155     }
00156     /* At this point, all checks are OK. */
00157     /* We generate an answer by switching the dest and src ip addresses,
00158      * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
00159     iecho = static_cast<struct icmp_echo_hdr *>(p->payload);
00160     tmpaddr.addr = iphdr->src.addr;
00161     iphdr->src.addr = iphdr->dest.addr;
00162     iphdr->dest.addr = tmpaddr.addr;
00163     ICMPH_TYPE_SET(iecho, ICMP_ER);
00164     /* adjust the checksum */
00165     if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
00166       iecho->chksum += htons(ICMP_ECHO << 8) + 1;
00167     } else {
00168       iecho->chksum += htons(ICMP_ECHO << 8);
00169     }
00170 
00171     /* Set the correct TTL and recalculate the header checksum. */
00172     IPH_TTL_SET(iphdr, ICMP_TTL);
00173     IPH_CHKSUM_SET(iphdr, 0);
00174 #if CHECKSUM_GEN_IP
00175     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
00176 #endif /* CHECKSUM_GEN_IP */
00177 
00178     ICMP_STATS_INC(icmp.xmit);
00179     /* increase number of messages attempted to send */
00180     snmp_inc_icmpoutmsgs();
00181     /* increase number of echo replies attempted to send */
00182     snmp_inc_icmpoutechoreps();
00183 
00184     if(pbuf_header(p, hlen)) {
00185       LWIP_ASSERT("Can't move over header in packet", 0);
00186     } else {
00187       err_t ret;
00188       struct ip_addr iphdrsrc;                                // XXX: __packed hack
00189       iphdrsrc.addr = iphdr->src.addr;
00190       ret = ip_output_if(p, &(iphdrsrc), IP_HDRINCL,
00191                    ICMP_TTL, 0, IP_PROTO_ICMP, inp);
00192       if (ret != ERR_OK) {
00193         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
00194       }
00195     }
00196     break;
00197   default:
00198     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 
00199                 (s16_t)type, (s16_t)code));
00200     ICMP_STATS_INC(icmp.proterr);
00201     ICMP_STATS_INC(icmp.drop);
00202   }
00203   pbuf_free(p);
00204   return;
00205 lenerr:
00206   pbuf_free(p);
00207   ICMP_STATS_INC(icmp.lenerr);
00208   snmp_inc_icmpinerrors();
00209   return;
00210 memerr:
00211   pbuf_free(p);
00212   ICMP_STATS_INC(icmp.err);
00213   snmp_inc_icmpinerrors();
00214   return;
00215 }
00216 
00217 /**
00218  * Send an icmp 'destination unreachable' packet, called from ip_input() if
00219  * the transport layer protocol is unknown and from udp_input() if the local
00220  * port is not bound.
00221  *
00222  * @param p the input packet for which the 'unreachable' should be sent,
00223  *          p->payload pointing to the IP header
00224  * @param t type of the 'unreachable' packet
00225  */
00226 void
00227 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
00228 {
00229   struct pbuf *q;
00230   struct ip_hdr *iphdr;
00231   struct icmp_dur_hdr *idur;
00232 
00233   /* ICMP header + IP header + 8 bytes of data */
00234   q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
00235                  PBUF_RAM);
00236   if (q == NULL) {
00237     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
00238     return;
00239   }
00240   LWIP_ASSERT("check that first pbuf can hold icmp message",
00241              (q->len >= (sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
00242 
00243   iphdr = static_cast<struct ip_hdr *>(p->payload);
00244 
00245   idur = static_cast<struct icmp_dur_hdr *>(q->payload);
00246   ICMPH_TYPE_SET(idur, ICMP_DUR);
00247   ICMPH_CODE_SET(idur, t);
00248 
00249   SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_dur_hdr), p->payload,
00250           IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
00251 
00252   /* calculate checksum */
00253   idur->chksum = 0;
00254   idur->chksum = inet_chksum(idur, q->len);
00255   ICMP_STATS_INC(icmp.xmit);
00256   /* increase number of messages attempted to send */
00257   snmp_inc_icmpoutmsgs();
00258   /* increase number of destination unreachable messages attempted to send */
00259   snmp_inc_icmpoutdestunreachs();
00260 
00261   struct ip_addr iphdrsrc;
00262   iphdrsrc.addr = iphdr->src.addr;
00263   ip_output(q, NULL, &(iphdrsrc), ICMP_TTL, 0, IP_PROTO_ICMP);
00264   pbuf_free(q);
00265 }
00266 
00267 #if IP_FORWARD || IP_REASSEMBLY
00268 /**
00269  * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
00270  *
00271  * @param p the input packet for which the 'time exceeded' should be sent,
00272  *          p->payload pointing to the IP header
00273  * @param t type of the 'time exceeded' packet
00274  */
00275 void
00276 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
00277 {
00278   struct pbuf *q;
00279   struct ip_hdr *iphdr;
00280   struct icmp_te_hdr *tehdr;
00281 
00282   /* ICMP header + IP header + 8 bytes of data */
00283   q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
00284                  PBUF_RAM);
00285   if (q == NULL) {
00286     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
00287     return;
00288   }
00289   LWIP_ASSERT("check that first pbuf can hold icmp message",
00290              (q->len >= (sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
00291 
00292   iphdr = static_cast<struct ip_hdr *>(p->payload);
00293   LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
00294   ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
00295   LWIP_DEBUGF(ICMP_DEBUG, (" to "));
00296   ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
00297   LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
00298 
00299   tehdr = static_cast<struct icmp_te_hdr *>(q->payload);
00300   ICMPH_TYPE_SET(tehdr, ICMP_TE);
00301   ICMPH_CODE_SET(tehdr, t);
00302 
00303   /* copy fields from original packet */
00304   SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_dur_hdr), (u8_t *)p->payload,
00305           IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
00306 
00307   /* calculate checksum */
00308   tehdr->chksum = 0;
00309   tehdr->chksum = inet_chksum(tehdr, q->len);
00310   ICMP_STATS_INC(icmp.xmit);
00311   /* increase number of messages attempted to send */
00312   snmp_inc_icmpoutmsgs();
00313   /* increase number of destination unreachable messages attempted to send */
00314   snmp_inc_icmpouttimeexcds();
00315   static ip_addr iphdrsrc;
00316   iphdrsrc.addr = iphdr->src.addr;
00317   ip_output(q, NULL, &iphdrsrc, ICMP_TTL, 0, IP_PROTO_ICMP);
00318   pbuf_free(q);
00319 }
00320 
00321 #endif /* IP_FORWARD */
00322 
00323 #endif /* LWIP_ICMP */