STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
icmp.c
Go to the documentation of this file.
1 
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 
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 
75 void
76 icmp_input(struct pbuf *p, struct netif *inp)
77 {
78  u8_t type;
79 #if 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 (p->len < sizeof(u16_t)*2) {
93  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
94  goto lenerr;
95  }
96 
97  type = *((u8_t *)p->payload);
98 #if LWIP_DEBUG
99  code = *(((u8_t *)p->payload)+1);
100 #endif /* LWIP_DEBUG */
101  switch (type) {
102  case ICMP_ER:
103  /* This is OK, echo reply might have been parsed by a raw PCB
104  (as obviously, an echo request has been sent, too). */
105  MIB2_STATS_INC(mib2.icmpinechoreps);
106  break;
107  case ICMP_ECHO:
108  MIB2_STATS_INC(mib2.icmpinechos);
109  src = ip4_current_dest_addr();
110  /* multicast destination address? */
112 #if LWIP_MULTICAST_PING
113  /* For multicast, use address of receiving interface as source address */
114  src = netif_ip4_addr(inp);
115 #else /* LWIP_MULTICAST_PING */
116  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n"));
117  goto icmperr;
118 #endif /* LWIP_MULTICAST_PING */
119  }
120  /* broadcast destination address? */
122 #if LWIP_BROADCAST_PING
123  /* For broadcast, use address of receiving interface as source address */
124  src = netif_ip4_addr(inp);
125 #else /* LWIP_BROADCAST_PING */
126  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n"));
127  goto icmperr;
128 #endif /* LWIP_BROADCAST_PING */
129  }
130  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
131  if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
132  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
133  goto lenerr;
134  }
135 #if CHECKSUM_CHECK_ICMP
136  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) {
137  if (inet_chksum_pbuf(p) != 0) {
138  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
139  pbuf_free(p);
140  ICMP_STATS_INC(icmp.chkerr);
141  MIB2_STATS_INC(mib2.icmpinerrors);
142  return;
143  }
144  }
145 #endif
146 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
148  /* p is not big enough to contain link headers
149  * allocate a new one and copy p into it
150  */
151  struct pbuf *r;
152  /* allocate new packet buffer with space for link headers */
153  r = pbuf_alloc(PBUF_LINK, p->tot_len + hlen, PBUF_RAM);
154  if (r == NULL) {
155  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
156  goto icmperr;
157  }
158  LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
159  (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
160  /* copy the ip header */
161  MEMCPY(r->payload, iphdr_in, hlen);
162  /* switch r->payload back to icmp header */
163  if (pbuf_header(r, -hlen)) {
164  LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0);
165  pbuf_free(r);
166  goto icmperr;
167  }
168  /* copy the rest of the packet without ip header */
169  if (pbuf_copy(r, p) != ERR_OK) {
170  LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
171  pbuf_free(r);
172  goto icmperr;
173  }
174  /* free the original p */
175  pbuf_free(p);
176  /* we now have an identical copy of p that has room for link headers */
177  p = r;
178  } else {
179  /* restore p->payload to point to icmp header */
181  LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
182  goto icmperr;
183  }
184  }
185 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
186  /* At this point, all checks are OK. */
187  /* We generate an answer by switching the dest and src ip addresses,
188  * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
189  iecho = (struct icmp_echo_hdr *)p->payload;
190  if (pbuf_header(p, hlen)) {
191  LWIP_ASSERT("Can't move over header in packet", 0);
192  } else {
193  err_t ret;
194  struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
195  ip4_addr_copy(iphdr->src, *src);
196  ip4_addr_copy(iphdr->dest, *ip4_current_src_addr());
197  ICMPH_TYPE_SET(iecho, ICMP_ER);
198 #if CHECKSUM_GEN_ICMP
199  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) {
200  /* adjust the checksum */
201  if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
202  iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
203  } else {
204  iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
205  }
206  }
207 #if LWIP_CHECKSUM_CTRL_PER_NETIF
208  else {
209  iecho->chksum = 0;
210  }
211 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
212 #else /* CHECKSUM_GEN_ICMP */
213  iecho->chksum = 0;
214 #endif /* CHECKSUM_GEN_ICMP */
215 
216  /* Set the correct TTL and recalculate the header checksum. */
217  IPH_TTL_SET(iphdr, ICMP_TTL);
218  IPH_CHKSUM_SET(iphdr, 0);
219 #if CHECKSUM_GEN_IP
220  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) {
221  IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
222  }
223 #endif /* CHECKSUM_GEN_IP */
224 
225  ICMP_STATS_INC(icmp.xmit);
226  /* increase number of messages attempted to send */
227  MIB2_STATS_INC(mib2.icmpoutmsgs);
228  /* increase number of echo replies attempted to send */
229  MIB2_STATS_INC(mib2.icmpoutechoreps);
230 
231  /* send an ICMP packet */
232  ret = ip4_output_if(p, src, IP_HDRINCL,
233  ICMP_TTL, 0, IP_PROTO_ICMP, inp);
234  if (ret != ERR_OK) {
235  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret)));
236  }
237  }
238  break;
239  default:
240  if (type == ICMP_DUR) {
241  MIB2_STATS_INC(mib2.icmpindestunreachs);
242  } else if (type == ICMP_TE) {
243  MIB2_STATS_INC(mib2.icmpindestunreachs);
244  } else if (type == ICMP_PP) {
245  MIB2_STATS_INC(mib2.icmpinparmprobs);
246  } else if (type == ICMP_SQ) {
247  MIB2_STATS_INC(mib2.icmpinsrcquenchs);
248  } else if (type == ICMP_RD) {
249  MIB2_STATS_INC(mib2.icmpinredirects);
250  } else if (type == ICMP_TS) {
251  MIB2_STATS_INC(mib2.icmpintimestamps);
252  } else if (type == ICMP_TSR) {
253  MIB2_STATS_INC(mib2.icmpintimestampreps);
254  } else if (type == ICMP_AM) {
255  MIB2_STATS_INC(mib2.icmpinaddrmasks);
256  } else if (type == ICMP_AMR) {
257  MIB2_STATS_INC(mib2.icmpinaddrmaskreps);
258  }
259  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
260  (s16_t)type, (s16_t)code));
261  ICMP_STATS_INC(icmp.proterr);
262  ICMP_STATS_INC(icmp.drop);
263  }
264  pbuf_free(p);
265  return;
266 lenerr:
267  pbuf_free(p);
268  ICMP_STATS_INC(icmp.lenerr);
269  MIB2_STATS_INC(mib2.icmpinerrors);
270  return;
271 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
272 icmperr:
273  pbuf_free(p);
274  ICMP_STATS_INC(icmp.err);
275  MIB2_STATS_INC(mib2.icmpinerrors);
276  return;
277 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
278 }
279 
289 void
290 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
291 {
292  MIB2_STATS_INC(mib2.icmpoutdestunreachs);
293  icmp_send_response(p, ICMP_DUR, t);
294 }
295 
296 #if IP_FORWARD || IP_REASSEMBLY
297 
304 void
305 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
306 {
307  MIB2_STATS_INC(mib2.icmpouttimeexcds);
308  icmp_send_response(p, ICMP_TE, t);
309 }
310 
311 #endif /* IP_FORWARD || IP_REASSEMBLY */
312 
321 static void
322 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
323 {
324  struct pbuf *q;
325  struct ip_hdr *iphdr;
326  /* we can use the echo header here */
327  struct icmp_echo_hdr *icmphdr;
328  ip4_addr_t iphdr_src;
329  struct netif *netif;
330 
331  /* increase number of messages attempted to send */
332  MIB2_STATS_INC(mib2.icmpoutmsgs);
333 
334  /* ICMP header + IP header + 8 bytes of data */
335  q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
336  PBUF_RAM);
337  if (q == NULL) {
338  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
339  MIB2_STATS_INC(mib2.icmpouterrors);
340  return;
341  }
342  LWIP_ASSERT("check that first pbuf can hold icmp message",
343  (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
344 
345  iphdr = (struct ip_hdr *)p->payload;
346  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
347  ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src);
348  LWIP_DEBUGF(ICMP_DEBUG, (" to "));
349  ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest);
350  LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
351 
352  icmphdr = (struct icmp_echo_hdr *)q->payload;
353  icmphdr->type = type;
354  icmphdr->code = code;
355  icmphdr->id = 0;
356  icmphdr->seqno = 0;
357 
358  /* copy fields from original packet */
359  SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
360  IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
361 
362  ip4_addr_copy(iphdr_src, iphdr->src);
363  netif = ip4_route(&iphdr_src);
364  if (netif != NULL) {
365  /* calculate checksum */
366  icmphdr->chksum = 0;
367 #if CHECKSUM_GEN_ICMP
368  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) {
369  icmphdr->chksum = inet_chksum(icmphdr, q->len);
370  }
371 #endif
372  ICMP_STATS_INC(icmp.xmit);
373  ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif);
374  }
375  pbuf_free(q);
376 }
377 
378 #endif /* LWIP_IPV4 && LWIP_ICMP */
379 
#define MIB2_STATS_INC(x)
Definition: stats.h:407
#define IP_PROTO_ICMP
Definition: ip.h:49
#define ICMP_ECHO
Definition: icmp.h:52
#define ip_current_netif()
Definition: ip.h:154
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
#define ICMP_ER
Definition: icmp.h:48
#define ICMP_TTL
Definition: opt.h:697
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define PBUF_IP_HLEN
Definition: pbuf.h:60
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:313
#define MEMCPY(dst, src, len)
Definition: opt.h:84
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
#define ICMP_STATS_INC(x)
Definition: stats.h:278
#define ICMP_TS
Definition: icmp.h:55
#define ip_addr_isbroadcast(addr, netif)
Definition: ip_addr.h:219
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
#define ip_current_dest_addr()
Definition: ip.h:238
#define IP_HDRINCL
Definition: ip.h:64
Definition: pbuf.h:77
#define ip_addr_ismulticast(ipaddr)
Definition: ip_addr.h:220
u16_t inet_chksum(const void *dataptr, u16_t len)
Definition: inet_chksum.c:558
#define lwip_strerr(x)
Definition: err.h:79
Definition: pbuf.h:66
#define ICMP_SQ
Definition: icmp.h:50
#define ICMP_TSR
Definition: icmp.h:56
u16_t inet_chksum_pbuf(struct pbuf *p)
Definition: inet_chksum.c:571
#define ICMP_TE
Definition: icmp.h:53
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
Definition: pbuf.c:886
#define PP_HTONS(x)
Definition: def.h:97
#define ICMP_RD
Definition: icmp.h:51
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1193
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
icmp_te_type
Definition: icmp.h:71
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
#define ICMP_DUR
Definition: icmp.h:49
icmp_dur_type
Definition: icmp.h:62
#define S16_F
Definition: cc.h:49
unsigned char u8_t
Definition: cc.h:38
#define ICMP_AM
Definition: icmp.h:59
Definition: pbuf.h:65
#define ICMP_DEBUG
Definition: opt.h:2816
#define PBUF_LINK_HLEN
Definition: opt.h:1184
#define ICMP_PP
Definition: icmp.h:54
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ICMP_AMR
Definition: icmp.h:60
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define ICMPH_TYPE_SET(hdr, t)
Definition: icmp.h:101