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