source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/core/ipv4/icmp.c @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 10.9 KB
Line 
1/* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2
3/**
4 * @file
5 * ICMP - Internet Control Message Protocol
6 *
7 */
8
9/*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 *    this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 *    this list of conditions and the following disclaimer in the documentation
20 *    and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <adam@sics.se>
38 *
39 */
40
41/* Some ICMP messages should be passed to the transport protocols. This
42   is not implemented. */
43
44#include "lwip/opt.h"
45
46#if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
47
48#include "lwip/icmp.h"
49#include "lwip/inet.h"
50#include "lwip/inet_chksum.h"
51#include "lwip/ip.h"
52#include "lwip/def.h"
53#include "lwip/stats.h"
54#include "lwip/snmp.h"
55
56#include <string.h>
57
58/** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
59 * used to modify and send a response packet (and to 1 if this is not the case,
60 * e.g. when link header is stripped of when receiving) */
61#ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
62#define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
63#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
64
65/* The amount of data from the original packet to return in a dest-unreachable */
66#define ICMP_DEST_UNREACH_DATASIZE 8
67
68static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
69
70/**
71 * Processes ICMP input packets, called from ip_input().
72 *
73 * Currently only processes icmp echo requests and sends
74 * out the echo response.
75 *
76 * @param p the icmp echo request packet, p->payload pointing to the ip header
77 * @param inp the netif on which this packet was received
78 */
79void
80icmp_input(struct pbuf *p, struct netif *inp)
81{
82  u8_t type;
83#ifdef LWIP_DEBUG
84  u8_t code;
85#endif /* LWIP_DEBUG */
86  struct icmp_echo_hdr *iecho;
87  struct ip_hdr *iphdr;
88  struct ip_addr tmpaddr;
89  s16_t hlen;
90
91  ICMP_STATS_INC(icmp.recv);
92  snmp_inc_icmpinmsgs();
93
94
95  iphdr = p->payload;
96  hlen = IPH_HL(iphdr) * 4;
97  if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
98    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
99    goto lenerr;
100  }
101
102  type = *((u8_t *)p->payload);
103#ifdef LWIP_DEBUG
104  code = *(((u8_t *)p->payload)+1);
105#endif /* LWIP_DEBUG */
106  switch (type) {
107  case ICMP_ECHO:
108#if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
109    {
110      int accepted = 1;
111#if !LWIP_MULTICAST_PING
112      /* multicast destination address? */
113      if (ip_addr_ismulticast(&iphdr->dest)) {
114        accepted = 0;
115      }
116#endif /* LWIP_MULTICAST_PING */
117#if !LWIP_BROADCAST_PING
118      /* broadcast destination address? */
119      if (ip_addr_isbroadcast(&iphdr->dest, inp)) {
120        accepted = 0;
121      }
122#endif /* LWIP_BROADCAST_PING */
123      /* broadcast or multicast destination address not acceptd? */
124      if (!accepted) {
125        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
126        ICMP_STATS_INC(icmp.err);
127        pbuf_free(p);
128        return;
129      }
130    }
131#endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
132    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
133    if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
134      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
135      goto lenerr;
136    }
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      snmp_inc_icmpinerrors();
142      return;
143    }
144#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
145    if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
146      /* p is not big enough to contain link headers
147       * allocate a new one and copy p into it
148       */
149      struct pbuf *r;
150      /* switch p->payload to ip header */
151      if (pbuf_header(p, hlen)) {
152        LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
153        goto memerr;
154      }
155      /* allocate new packet buffer with space for link headers */
156      r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
157      if (r == NULL) {
158        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
159        goto memerr;
160      }
161      LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
162                  (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
163      /* copy the whole packet including ip header */
164      if (pbuf_copy(r, p) != ERR_OK) {
165        LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
166        goto memerr;
167      }
168      iphdr = r->payload;
169      /* switch r->payload back to icmp header */
170      if (pbuf_header(r, -hlen)) {
171        LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
172        goto memerr;
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 */
180      if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
181        LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
182        goto memerr;
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 = p->payload;
190    tmpaddr.addr = iphdr->src.addr;
191    iphdr->src.addr = iphdr->dest.addr;
192    iphdr->dest.addr = tmpaddr.addr;
193    ICMPH_TYPE_SET(iecho, ICMP_ER);
194    /* adjust the checksum */
195    if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
196      iecho->chksum += htons(ICMP_ECHO << 8) + 1;
197    } else {
198      iecho->chksum += htons(ICMP_ECHO << 8);
199    }
200
201    /* Set the correct TTL and recalculate the header checksum. */
202    IPH_TTL_SET(iphdr, ICMP_TTL);
203    IPH_CHKSUM_SET(iphdr, 0);
204#if CHECKSUM_GEN_IP
205    IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
206#endif /* CHECKSUM_GEN_IP */
207
208    ICMP_STATS_INC(icmp.xmit);
209    /* increase number of messages attempted to send */
210    snmp_inc_icmpoutmsgs();
211    /* increase number of echo replies attempted to send */
212    snmp_inc_icmpoutechoreps();
213
214    if(pbuf_header(p, hlen)) {
215      LWIP_ASSERT("Can't move over header in packet", 0);
216    } else {
217      err_t ret;
218      ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
219                   ICMP_TTL, 0, IP_PROTO_ICMP, inp);
220      if (ret != ERR_OK) {
221        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
222      }
223    }
224    break;
225  default:
226    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 
227                (s16_t)type, (s16_t)code));
228    ICMP_STATS_INC(icmp.proterr);
229    ICMP_STATS_INC(icmp.drop);
230  }
231  pbuf_free(p);
232  return;
233lenerr:
234  pbuf_free(p);
235  ICMP_STATS_INC(icmp.lenerr);
236  snmp_inc_icmpinerrors();
237  return;
238#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
239memerr:
240  pbuf_free(p);
241  ICMP_STATS_INC(icmp.err);
242  snmp_inc_icmpinerrors();
243  return;
244#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
245}
246
247/**
248 * Send an icmp 'destination unreachable' packet, called from ip_input() if
249 * the transport layer protocol is unknown and from udp_input() if the local
250 * port is not bound.
251 *
252 * @param p the input packet for which the 'unreachable' should be sent,
253 *          p->payload pointing to the IP header
254 * @param t type of the 'unreachable' packet
255 */
256void
257icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
258{
259  icmp_send_response(p, ICMP_DUR, t);
260}
261
262#if IP_FORWARD || IP_REASSEMBLY
263/**
264 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
265 *
266 * @param p the input packet for which the 'time exceeded' should be sent,
267 *          p->payload pointing to the IP header
268 * @param t type of the 'time exceeded' packet
269 */
270void
271icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
272{
273  icmp_send_response(p, ICMP_TE, t);
274}
275
276#endif /* IP_FORWARD || IP_REASSEMBLY */
277
278/**
279 * Send an icmp packet in response to an incoming packet.
280 *
281 * @param p the input packet for which the 'unreachable' should be sent,
282 *          p->payload pointing to the IP header
283 * @param type Type of the ICMP header
284 * @param code Code of the ICMP header
285 */
286static void
287icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
288{
289  struct pbuf *q;
290  struct ip_hdr *iphdr;
291  /* we can use the echo header here */
292  struct icmp_echo_hdr *icmphdr;
293
294  /* ICMP header + IP header + 8 bytes of data */
295  q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
296                 PBUF_RAM);
297  if (q == NULL) {
298    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
299    return;
300  }
301  LWIP_ASSERT("check that first pbuf can hold icmp message",
302             (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
303
304  iphdr = p->payload;
305  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
306  ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
307  LWIP_DEBUGF(ICMP_DEBUG, (" to "));
308  ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
309  LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
310
311  icmphdr = q->payload;
312  icmphdr->type = type;
313  icmphdr->code = code;
314  icmphdr->id = 0;
315  icmphdr->seqno = 0;
316
317  /* copy fields from original packet */
318  SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
319          IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
320
321  /* calculate checksum */
322  icmphdr->chksum = 0;
323  icmphdr->chksum = inet_chksum(icmphdr, q->len);
324  ICMP_STATS_INC(icmp.xmit);
325  /* increase number of messages attempted to send */
326  snmp_inc_icmpoutmsgs();
327  /* increase number of destination unreachable messages attempted to send */
328  snmp_inc_icmpouttimeexcds();
329  ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP);
330  pbuf_free(q);
331}
332
333#endif /* LWIP_ICMP */
Note: See TracBrowser for help on using the repository browser.