]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/core/ipv4/icmp.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / core / ipv4 / icmp.c
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_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 #include "lwip/snmp.h"
52
53 #include <string.h>
54
55 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
56  * used to modify and send a response packet (and to 1 if this is not the case,
57  * e.g. when link header is stripped of when receiving) */
58 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
59 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
60 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
61
62 /* The amount of data from the original packet to return in a dest-unreachable */
63 #define ICMP_DEST_UNREACH_DATASIZE 8
64
65 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
66
67 /**
68  * Processes ICMP input packets, called from ip_input().
69  *
70  * Currently only processes icmp echo requests and sends
71  * out the echo response.
72  *
73  * @param p the icmp echo request packet, p->payload pointing to the icmp header
74  * @param inp the netif on which this packet was received
75  */
76 void
77 icmp_input(struct pbuf *p, struct netif *inp)
78 {
79   u8_t type;
80 #ifdef LWIP_DEBUG
81   u8_t code;
82 #endif /* LWIP_DEBUG */
83   struct icmp_echo_hdr *iecho;
84   struct ip_hdr *iphdr;
85   s16_t hlen;
86
87   ICMP_STATS_INC(icmp.recv);
88   snmp_inc_icmpinmsgs();
89
90   iphdr = (struct ip_hdr *)ip_current_header();
91   hlen = IPH_HL(iphdr) * 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 #ifdef 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     break; 
106   case ICMP_ECHO:
107 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
108     {
109       int accepted = 1;
110 #if !LWIP_MULTICAST_PING
111       /* multicast destination address? */
112       if (ip_addr_ismulticast(ip_current_dest_addr())) {
113         accepted = 0;
114       }
115 #endif /* LWIP_MULTICAST_PING */
116 #if !LWIP_BROADCAST_PING
117       /* broadcast destination address? */
118       if (ip_addr_isbroadcast(ip_current_dest_addr(), inp)) {
119         accepted = 0;
120       }
121 #endif /* LWIP_BROADCAST_PING */
122       /* broadcast or multicast destination address not acceptd? */
123       if (!accepted) {
124         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
125         ICMP_STATS_INC(icmp.err);
126         pbuf_free(p);
127         return;
128       }
129     }
130 #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
131     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
132     if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
133       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
134       goto lenerr;
135     }
136     if (inet_chksum_pbuf(p) != 0) {
137       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
138       pbuf_free(p);
139       ICMP_STATS_INC(icmp.chkerr);
140       snmp_inc_icmpinerrors();
141       return;
142     }
143 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
144     if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
145       /* p is not big enough to contain link headers
146        * allocate a new one and copy p into it
147        */
148       struct pbuf *r;
149       /* switch p->payload to ip header */
150       if (pbuf_header(p, hlen)) {
151         LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
152         goto memerr;
153       }
154       /* allocate new packet buffer with space for link headers */
155       r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
156       if (r == NULL) {
157         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
158         goto memerr;
159       }
160       LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
161                   (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
162       /* copy the whole packet including ip header */
163       if (pbuf_copy(r, p) != ERR_OK) {
164         LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
165         goto memerr;
166       }
167       iphdr = (struct ip_hdr *)r->payload;
168       /* switch r->payload back to icmp header */
169       if (pbuf_header(r, -hlen)) {
170         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
171         goto memerr;
172       }
173       /* free the original p */
174       pbuf_free(p);
175       /* we now have an identical copy of p that has room for link headers */
176       p = r;
177     } else {
178       /* restore p->payload to point to icmp header */
179       if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
180         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
181         goto memerr;
182       }
183     }
184 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
185     /* At this point, all checks are OK. */
186     /* We generate an answer by switching the dest and src ip addresses,
187      * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
188     iecho = (struct icmp_echo_hdr *)p->payload;
189     ip_addr_copy(iphdr->src, *ip_current_dest_addr());
190     ip_addr_copy(iphdr->dest, *ip_current_src_addr());
191     ICMPH_TYPE_SET(iecho, ICMP_ER);
192     /* adjust the checksum */
193     if (iecho->chksum >= PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
194       iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
195     } else {
196       iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
197     }
198
199     /* Set the correct TTL and recalculate the header checksum. */
200     IPH_TTL_SET(iphdr, ICMP_TTL);
201     IPH_CHKSUM_SET(iphdr, 0);
202 #if CHECKSUM_GEN_IP
203     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
204 #endif /* CHECKSUM_GEN_IP */
205
206     ICMP_STATS_INC(icmp.xmit);
207     /* increase number of messages attempted to send */
208     snmp_inc_icmpoutmsgs();
209     /* increase number of echo replies attempted to send */
210     snmp_inc_icmpoutechoreps();
211
212     if(pbuf_header(p, hlen)) {
213       LWIP_ASSERT("Can't move over header in packet", 0);
214     } else {
215       err_t ret;
216       /* send an ICMP packet, src addr is the dest addr of the curren packet */
217       ret = ip_output_if(p, ip_current_dest_addr(), IP_HDRINCL,
218                    ICMP_TTL, 0, IP_PROTO_ICMP, inp);
219       if (ret != ERR_OK) {
220         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
221       }
222     }
223     break;
224   default:
225     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 
226                 (s16_t)type, (s16_t)code));
227     ICMP_STATS_INC(icmp.proterr);
228     ICMP_STATS_INC(icmp.drop);
229   }
230   pbuf_free(p);
231   return;
232 lenerr:
233   pbuf_free(p);
234   ICMP_STATS_INC(icmp.lenerr);
235   snmp_inc_icmpinerrors();
236   return;
237 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
238 memerr:
239   pbuf_free(p);
240   ICMP_STATS_INC(icmp.err);
241   snmp_inc_icmpinerrors();
242   return;
243 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
244 }
245
246 /**
247  * Send an icmp 'destination unreachable' packet, called from ip_input() if
248  * the transport layer protocol is unknown and from udp_input() if the local
249  * port is not bound.
250  *
251  * @param p the input packet for which the 'unreachable' should be sent,
252  *          p->payload pointing to the IP header
253  * @param t type of the 'unreachable' packet
254  */
255 void
256 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
257 {
258   icmp_send_response(p, ICMP_DUR, t);
259 }
260
261 #if IP_FORWARD || IP_REASSEMBLY
262 /**
263  * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
264  *
265  * @param p the input packet for which the 'time exceeded' should be sent,
266  *          p->payload pointing to the IP header
267  * @param t type of the 'time exceeded' packet
268  */
269 void
270 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
271 {
272   icmp_send_response(p, ICMP_TE, t);
273 }
274
275 #endif /* IP_FORWARD || IP_REASSEMBLY */
276
277 /**
278  * Send an icmp packet in response to an incoming packet.
279  *
280  * @param p the input packet for which the 'unreachable' should be sent,
281  *          p->payload pointing to the IP header
282  * @param type Type of the ICMP header
283  * @param code Code of the ICMP header
284  */
285 static void
286 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
287 {
288   struct pbuf *q;
289   struct ip_hdr *iphdr;
290   /* we can use the echo header here */
291   struct icmp_echo_hdr *icmphdr;
292   ip_addr_t iphdr_src;
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 = (struct ip_hdr *)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 = (struct icmp_echo_hdr *)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_addr_copy(iphdr_src, iphdr->src);
330   ip_output(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP);
331   pbuf_free(q);
332 }
333
334 #endif /* LWIP_ICMP */