1 /**
2  * @file
3  * ICMP - Internet Control Message Protocol
4  *
5  */
6 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38 
39 /* Some ICMP messages should be passed to the transport protocols. This
40    is not implemented. */
41 
42 #include "lwip/opt.h"
43 
44 #if LWIP_IPV4 && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
45 
46 #include "lwip/icmp.h"
47 #include "lwip/inet_chksum.h"
48 #include "lwip/ip.h"
49 #include "lwip/def.h"
50 #include "lwip/stats.h"
51 
52 #include <string.h>
53 
54 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
55  * used to modify and send a response packet (and to 1 if this is not the case,
56  * e.g. when link header is stripped of when receiving) */
57 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
58 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
59 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
60 
61 /* The amount of data from the original packet to return in a dest-unreachable */
62 #define ICMP_DEST_UNREACH_DATASIZE 8
63 
64 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
65 
66 /**
67  * Processes ICMP input packets, called from ip_input().
68  *
69  * Currently only processes icmp echo requests and sends
70  * out the echo response.
71  *
72  * @param p the icmp echo request packet, p->payload pointing to the icmp header
73  * @param inp the netif on which this packet was received
74  */
75 void
icmp_input(struct pbuf * p,struct netif * inp)76 icmp_input(struct pbuf *p, struct netif *inp)
77 {
78   u8_t type;
79 #ifdef LWIP_DEBUG
80   u8_t code;
81 #endif /* LWIP_DEBUG */
82   struct icmp_echo_hdr *iecho;
83   const struct ip_hdr *iphdr_in;
84   s16_t hlen;
85   const ip4_addr_t* src;
86 
87   ICMP_STATS_INC(icmp.recv);
88   MIB2_STATS_INC(mib2.icmpinmsgs);
89 
90   iphdr_in = ip4_current_header();
91   hlen = IPH_HL(iphdr_in) * 4;
92   if (hlen < IP_HLEN) {
93     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen));
94     goto lenerr;
95   }
96   if (p->len < sizeof(u16_t)*2) {
97     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
98     goto lenerr;
99   }
100 
101   type = *((u8_t *)p->payload);
102 #ifdef LWIP_DEBUG
103   code = *(((u8_t *)p->payload)+1);
104 #endif /* LWIP_DEBUG */
105   switch (type) {
106   case ICMP_ER:
107     /* This is OK, echo reply might have been parsed by a raw PCB
108        (as obviously, an echo request has been sent, too). */
109     MIB2_STATS_INC(mib2.icmpinechoreps);
110     break;
111   case ICMP_ECHO:
112     MIB2_STATS_INC(mib2.icmpinechos);
113     src = ip4_current_dest_addr();
114     /* multicast destination address? */
115     if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
116 #if LWIP_MULTICAST_PING
117       /* For multicast, use address of receiving interface as source address */
118       src = netif_ip4_addr(inp);
119 #else /* LWIP_MULTICAST_PING */
120       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n"));
121       goto icmperr;
122 #endif /* LWIP_MULTICAST_PING */
123     }
124     /* broadcast destination address? */
125     if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) {
126 #if LWIP_BROADCAST_PING
127       /* For broadcast, use address of receiving interface as source address */
128       src = netif_ip4_addr(inp);
129 #else /* LWIP_BROADCAST_PING */
130       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n"));
131       goto icmperr;
132 #endif /* LWIP_BROADCAST_PING */
133     }
134     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
135     if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
136       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
137       goto lenerr;
138     }
139 #if CHECKSUM_CHECK_ICMP
140     IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) {
141       if (inet_chksum_pbuf(p) != 0) {
142         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
143         pbuf_free(p);
144         ICMP_STATS_INC(icmp.chkerr);
145         MIB2_STATS_INC(mib2.icmpinerrors);
146         return;
147       }
148     }
149 #endif
150 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
151     if (pbuf_header(p, (hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) {
152       /* p is not big enough to contain link headers
153        * allocate a new one and copy p into it
154        */
155       struct pbuf *r;
156       /* allocate new packet buffer with space for link headers */
157       r = pbuf_alloc(PBUF_LINK, p->tot_len + hlen, PBUF_RAM);
158       if (r == NULL) {
159         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
160         goto icmperr;
161       }
162       if (r->len < hlen + sizeof(struct icmp_echo_hdr)) {
163         LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header"));
164         pbuf_free(r);
165         goto icmperr;
166       }
167       /* copy the ip header */
168       MEMCPY(r->payload, iphdr_in, hlen);
169       /* switch r->payload back to icmp header (cannot fail) */
170       if (pbuf_header(r, -hlen)) {
171         LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0);
172         pbuf_free(r);
173         goto icmperr;
174       }
175       /* copy the rest of the packet without ip header */
176       if (pbuf_copy(r, p) != ERR_OK) {
177         LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed"));
178         pbuf_free(r);
179         goto icmperr;
180       }
181       /* free the original p */
182       pbuf_free(p);
183       /* we now have an identical copy of p that has room for link headers */
184       p = r;
185     } else {
186       /* restore p->payload to point to icmp header (cannot fail) */
187       if (pbuf_header(p, -(s16_t)(hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) {
188         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
189         goto icmperr;
190       }
191     }
192 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
193     /* At this point, all checks are OK. */
194     /* We generate an answer by switching the dest and src ip addresses,
195      * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
196     iecho = (struct icmp_echo_hdr *)p->payload;
197     if (pbuf_header(p, hlen)) {
198       LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet"));
199     } else {
200       err_t ret;
201       struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
202       ip4_addr_copy(iphdr->src, *src);
203       ip4_addr_copy(iphdr->dest, *ip4_current_src_addr());
204       ICMPH_TYPE_SET(iecho, ICMP_ER);
205 #if CHECKSUM_GEN_ICMP
206       IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) {
207         /* adjust the checksum */
208         if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
209           iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
210         } else {
211           iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
212         }
213       }
214 #if LWIP_CHECKSUM_CTRL_PER_NETIF
215       else {
216         iecho->chksum = 0;
217       }
218 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
219 #else /* CHECKSUM_GEN_ICMP */
220       iecho->chksum = 0;
221 #endif /* CHECKSUM_GEN_ICMP */
222 
223       /* Set the correct TTL and recalculate the header checksum. */
224       IPH_TTL_SET(iphdr, ICMP_TTL);
225       IPH_CHKSUM_SET(iphdr, 0);
226 #if CHECKSUM_GEN_IP
227       IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) {
228         IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen));
229       }
230 #endif /* CHECKSUM_GEN_IP */
231 
232       ICMP_STATS_INC(icmp.xmit);
233       /* increase number of messages attempted to send */
234       MIB2_STATS_INC(mib2.icmpoutmsgs);
235       /* increase number of echo replies attempted to send */
236       MIB2_STATS_INC(mib2.icmpoutechoreps);
237 
238       /* send an ICMP packet */
239       ret = ip4_output_if(p, src, LWIP_IP_HDRINCL,
240                    ICMP_TTL, 0, IP_PROTO_ICMP, inp);
241       if (ret != ERR_OK) {
242         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret)));
243       }
244     }
245     break;
246   default:
247     if (type == ICMP_DUR) {
248       MIB2_STATS_INC(mib2.icmpindestunreachs);
249     } else if (type == ICMP_TE) {
250       MIB2_STATS_INC(mib2.icmpindestunreachs);
251     } else if (type == ICMP_PP) {
252       MIB2_STATS_INC(mib2.icmpinparmprobs);
253     } else if (type == ICMP_SQ) {
254       MIB2_STATS_INC(mib2.icmpinsrcquenchs);
255     } else if (type == ICMP_RD) {
256       MIB2_STATS_INC(mib2.icmpinredirects);
257     } else if (type == ICMP_TS) {
258       MIB2_STATS_INC(mib2.icmpintimestamps);
259     } else if (type == ICMP_TSR) {
260       MIB2_STATS_INC(mib2.icmpintimestampreps);
261     } else if (type == ICMP_AM) {
262       MIB2_STATS_INC(mib2.icmpinaddrmasks);
263     } else if (type == ICMP_AMR) {
264       MIB2_STATS_INC(mib2.icmpinaddrmaskreps);
265     }
266     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
267                 (s16_t)type, (s16_t)code));
268     ICMP_STATS_INC(icmp.proterr);
269     ICMP_STATS_INC(icmp.drop);
270   }
271   pbuf_free(p);
272   return;
273 lenerr:
274   pbuf_free(p);
275   ICMP_STATS_INC(icmp.lenerr);
276   MIB2_STATS_INC(mib2.icmpinerrors);
277   return;
278 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
279 icmperr:
280   pbuf_free(p);
281   ICMP_STATS_INC(icmp.err);
282   MIB2_STATS_INC(mib2.icmpinerrors);
283   return;
284 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
285 }
286 
287 /**
288  * Send an icmp 'destination unreachable' packet, called from ip_input() if
289  * the transport layer protocol is unknown and from udp_input() if the local
290  * port is not bound.
291  *
292  * @param p the input packet for which the 'unreachable' should be sent,
293  *          p->payload pointing to the IP header
294  * @param t type of the 'unreachable' packet
295  */
296 void
icmp_dest_unreach(struct pbuf * p,enum icmp_dur_type t)297 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
298 {
299   MIB2_STATS_INC(mib2.icmpoutdestunreachs);
300   icmp_send_response(p, ICMP_DUR, t);
301 }
302 
303 #if IP_FORWARD || IP_REASSEMBLY
304 /**
305  * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
306  *
307  * @param p the input packet for which the 'time exceeded' should be sent,
308  *          p->payload pointing to the IP header
309  * @param t type of the 'time exceeded' packet
310  */
311 void
icmp_time_exceeded(struct pbuf * p,enum icmp_te_type t)312 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
313 {
314   MIB2_STATS_INC(mib2.icmpouttimeexcds);
315   icmp_send_response(p, ICMP_TE, t);
316 }
317 
318 #endif /* IP_FORWARD || IP_REASSEMBLY */
319 
320 /**
321  * Send an icmp packet in response to an incoming packet.
322  *
323  * @param p the input packet for which the 'unreachable' should be sent,
324  *          p->payload pointing to the IP header
325  * @param type Type of the ICMP header
326  * @param code Code of the ICMP header
327  */
328 static void
icmp_send_response(struct pbuf * p,u8_t type,u8_t code)329 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
330 {
331   struct pbuf *q;
332   struct ip_hdr *iphdr;
333   /* we can use the echo header here */
334   struct icmp_echo_hdr *icmphdr;
335   ip4_addr_t iphdr_src;
336   struct netif *netif;
337 
338   /* increase number of messages attempted to send */
339   MIB2_STATS_INC(mib2.icmpoutmsgs);
340 
341   /* ICMP header + IP header + 8 bytes of data */
342   q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
343                  PBUF_RAM);
344   if (q == NULL) {
345     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
346     MIB2_STATS_INC(mib2.icmpouterrors);
347     return;
348   }
349   LWIP_ASSERT("check that first pbuf can hold icmp message",
350              (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
351 
352   iphdr = (struct ip_hdr *)p->payload;
353   LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
354   ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src);
355   LWIP_DEBUGF(ICMP_DEBUG, (" to "));
356   ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest);
357   LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
358 
359   icmphdr = (struct icmp_echo_hdr *)q->payload;
360   icmphdr->type = type;
361   icmphdr->code = code;
362   icmphdr->id = 0;
363   icmphdr->seqno = 0;
364 
365   /* copy fields from original packet */
366   SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
367           IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
368 
369   ip4_addr_copy(iphdr_src, iphdr->src);
370 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
371   {
372     ip4_addr_t iphdr_dst;
373     ip4_addr_copy(iphdr_dst, iphdr->dest);
374     netif = ip4_route_src(&iphdr_src, &iphdr_dst);
375   }
376 #else
377   netif = ip4_route(&iphdr_src);
378 #endif
379   if (netif != NULL) {
380     /* calculate checksum */
381     icmphdr->chksum = 0;
382 #if CHECKSUM_GEN_ICMP
383     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) {
384       icmphdr->chksum = inet_chksum(icmphdr, q->len);
385     }
386 #endif
387     ICMP_STATS_INC(icmp.xmit);
388     ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif);
389   }
390   pbuf_free(q);
391 }
392 
393 #endif /* LWIP_IPV4 && LWIP_ICMP */
394