ReactOS 0.4.16-dev-311-g9382aa2
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
54#ifdef LWIP_HOOK_FILENAME
55#include LWIP_HOOK_FILENAME
56#endif
57
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 maximum 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
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 const struct ip_hdr *iphdr_in;
88 u16_t hlen;
89 const ip4_addr_t *src;
90
91 ICMP_STATS_INC(icmp.recv);
92 MIB2_STATS_INC(mib2.icmpinmsgs);
93
94 iphdr_in = ip4_current_header();
95 hlen = IPH_HL_BYTES(iphdr_in);
96 if (hlen < IP_HLEN) {
97 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen));
98 goto lenerr;
99 }
100 if (p->len < sizeof(u16_t) * 2) {
101 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
102 goto lenerr;
103 }
104
105 type = *((u8_t *)p->payload);
106#ifdef LWIP_DEBUG
107 code = *(((u8_t *)p->payload) + 1);
108 /* if debug is enabled but debug statement below is somehow disabled: */
110#endif /* LWIP_DEBUG */
111 switch (type) {
112 case ICMP_ER:
113 /* This is OK, echo reply might have been parsed by a raw PCB
114 (as obviously, an echo request has been sent, too). */
115 MIB2_STATS_INC(mib2.icmpinechoreps);
116 break;
117 case ICMP_ECHO:
118 MIB2_STATS_INC(mib2.icmpinechos);
119 src = ip4_current_dest_addr();
120 /* multicast destination address? */
121 if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
122#if LWIP_MULTICAST_PING
123 /* For multicast, use address of receiving interface as source address */
124 src = netif_ip4_addr(inp);
125#else /* LWIP_MULTICAST_PING */
126 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n"));
127 goto icmperr;
128#endif /* LWIP_MULTICAST_PING */
129 }
130 /* broadcast destination address? */
131 if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) {
132#if LWIP_BROADCAST_PING
133 /* For broadcast, use address of receiving interface as source address */
134 src = netif_ip4_addr(inp);
135#else /* LWIP_BROADCAST_PING */
136 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n"));
137 goto icmperr;
138#endif /* LWIP_BROADCAST_PING */
139 }
140 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
141 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
142 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
143 goto lenerr;
144 }
145#if CHECKSUM_CHECK_ICMP
146 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) {
147 if (inet_chksum_pbuf(p) != 0) {
148 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
149 pbuf_free(p);
150 ICMP_STATS_INC(icmp.chkerr);
151 MIB2_STATS_INC(mib2.icmpinerrors);
152 return;
153 }
154 }
155#endif
156#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
158 /* p is not big enough to contain link headers
159 * allocate a new one and copy p into it
160 */
161 struct pbuf *r;
162 u16_t alloc_len = (u16_t)(p->tot_len + hlen);
163 if (alloc_len < p->tot_len) {
164 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed (tot_len overflow)\n"));
165 goto icmperr;
166 }
167 /* allocate new packet buffer with space for link headers */
168 r = pbuf_alloc(PBUF_LINK, alloc_len, PBUF_RAM);
169 if (r == NULL) {
170 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
171 goto icmperr;
172 }
173 if (r->len < hlen + sizeof(struct icmp_echo_hdr)) {
174 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header\n"));
175 pbuf_free(r);
176 goto icmperr;
177 }
178 /* copy the ip header */
179 MEMCPY(r->payload, iphdr_in, hlen);
180 /* switch r->payload back to icmp header (cannot fail) */
181 if (pbuf_remove_header(r, hlen)) {
182 LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed", 0);
183 pbuf_free(r);
184 goto icmperr;
185 }
186 /* copy the rest of the packet without ip header */
187 if (pbuf_copy(r, p) != ERR_OK) {
188 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed\n"));
189 pbuf_free(r);
190 goto icmperr;
191 }
192 /* free the original p */
193 pbuf_free(p);
194 /* we now have an identical copy of p that has room for link headers */
195 p = r;
196 } else {
197 /* restore p->payload to point to icmp header (cannot fail) */
199 LWIP_ASSERT("icmp_input: restoring original p->payload failed", 0);
200 goto icmperr;
201 }
202 }
203#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
204 /* At this point, all checks are OK. */
205 /* We generate an answer by switching the dest and src ip addresses,
206 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
207 iecho = (struct icmp_echo_hdr *)p->payload;
208 if (pbuf_add_header(p, hlen)) {
209 LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet\n"));
210 } else {
211 err_t ret;
212 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
213 ip4_addr_copy(iphdr->src, *src);
214 ip4_addr_copy(iphdr->dest, *ip4_current_src_addr());
215 ICMPH_TYPE_SET(iecho, ICMP_ER);
216#if CHECKSUM_GEN_ICMP
217 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) {
218 /* adjust the checksum */
219 if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
220 iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS((u16_t)(ICMP_ECHO << 8)) + 1);
221 } else {
222 iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS(ICMP_ECHO << 8));
223 }
224 }
225#if LWIP_CHECKSUM_CTRL_PER_NETIF
226 else {
227 iecho->chksum = 0;
228 }
229#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
230#else /* CHECKSUM_GEN_ICMP */
231 iecho->chksum = 0;
232#endif /* CHECKSUM_GEN_ICMP */
233
234 /* Set the correct TTL and recalculate the header checksum. */
235 IPH_TTL_SET(iphdr, ICMP_TTL);
236 IPH_CHKSUM_SET(iphdr, 0);
237#if CHECKSUM_GEN_IP
238 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) {
239 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen));
240 }
241#endif /* CHECKSUM_GEN_IP */
242
243 ICMP_STATS_INC(icmp.xmit);
244 /* increase number of messages attempted to send */
245 MIB2_STATS_INC(mib2.icmpoutmsgs);
246 /* increase number of echo replies attempted to send */
247 MIB2_STATS_INC(mib2.icmpoutechoreps);
248
249 /* send an ICMP packet */
250 ret = ip4_output_if(p, src, LWIP_IP_HDRINCL,
251 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
252 if (ret != ERR_OK) {
253 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret)));
254 }
255 }
256 break;
257 default:
258 if (type == ICMP_DUR) {
259 MIB2_STATS_INC(mib2.icmpindestunreachs);
260 } else if (type == ICMP_TE) {
261 MIB2_STATS_INC(mib2.icmpintimeexcds);
262 } else if (type == ICMP_PP) {
263 MIB2_STATS_INC(mib2.icmpinparmprobs);
264 } else if (type == ICMP_SQ) {
265 MIB2_STATS_INC(mib2.icmpinsrcquenchs);
266 } else if (type == ICMP_RD) {
267 MIB2_STATS_INC(mib2.icmpinredirects);
268 } else if (type == ICMP_TS) {
269 MIB2_STATS_INC(mib2.icmpintimestamps);
270 } else if (type == ICMP_TSR) {
271 MIB2_STATS_INC(mib2.icmpintimestampreps);
272 } else if (type == ICMP_AM) {
273 MIB2_STATS_INC(mib2.icmpinaddrmasks);
274 } else if (type == ICMP_AMR) {
275 MIB2_STATS_INC(mib2.icmpinaddrmaskreps);
276 }
277 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
278 (s16_t)type, (s16_t)code));
279 ICMP_STATS_INC(icmp.proterr);
280 ICMP_STATS_INC(icmp.drop);
281 }
282 pbuf_free(p);
283 return;
284lenerr:
285 pbuf_free(p);
286 ICMP_STATS_INC(icmp.lenerr);
287 MIB2_STATS_INC(mib2.icmpinerrors);
288 return;
289#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
290icmperr:
291 pbuf_free(p);
292 ICMP_STATS_INC(icmp.err);
293 MIB2_STATS_INC(mib2.icmpinerrors);
294 return;
295#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
296}
297
307void
308icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
309{
310 MIB2_STATS_INC(mib2.icmpoutdestunreachs);
311 icmp_send_response(p, ICMP_DUR, t);
312}
313
314#if IP_FORWARD || IP_REASSEMBLY
322void
323icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
324{
325 MIB2_STATS_INC(mib2.icmpouttimeexcds);
326 icmp_send_response(p, ICMP_TE, t);
327}
328
329#endif /* IP_FORWARD || IP_REASSEMBLY */
330
339static void
340icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
341{
342 struct pbuf *q;
343 struct ip_hdr *iphdr;
344 struct icmp_hdr *icmphdr;
345 ip4_addr_t iphdr_src;
346 struct netif *netif;
347 u16_t response_pkt_len;
348
349 /* increase number of messages attempted to send */
350 MIB2_STATS_INC(mib2.icmpoutmsgs);
351
352 /* Keep IP header + up to 8 bytes */
353 response_pkt_len = IP_HLEN + ICMP_DEST_UNREACH_DATASIZE;
354 if (p->tot_len < response_pkt_len) {
355 response_pkt_len = p->tot_len;
356 }
357
358 /* ICMP header + part of original packet */
359 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_hdr) + response_pkt_len, PBUF_RAM);
360 if (q == NULL) {
361 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_send_response: failed to allocate pbuf for ICMP packet.\n"));
362 MIB2_STATS_INC(mib2.icmpouterrors);
363 return;
364 }
365 LWIP_ASSERT("check that first pbuf can hold icmp message",
366 (q->len >= (sizeof(struct icmp_hdr) + response_pkt_len)));
367
368 iphdr = (struct ip_hdr *)p->payload;
369 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_send_response: Sending ICMP type %02X for packet from ", type));
370 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src);
371 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
372 ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest);
373 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
374
375 icmphdr = (struct icmp_hdr *)q->payload;
376 icmphdr->type = type;
377 icmphdr->code = code;
378 icmphdr->data = 0;
379
380 /* copy fields from original packet */
381 pbuf_copy_partial_pbuf(q, p, response_pkt_len, sizeof(struct icmp_hdr));
382
383 ip4_addr_copy(iphdr_src, iphdr->src);
384#ifdef LWIP_HOOK_IP4_ROUTE_SRC
385 {
386 ip4_addr_t iphdr_dst;
387 ip4_addr_copy(iphdr_dst, iphdr->dest);
388 netif = ip4_route_src(&iphdr_dst, &iphdr_src);
389 }
390#else
391 netif = ip4_route(&iphdr_src);
392#endif
393 if (netif != NULL) {
394 /* calculate checksum */
395 icmphdr->chksum = 0;
396#if CHECKSUM_GEN_ICMP
397 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) {
398 icmphdr->chksum = inet_chksum(icmphdr, q->len);
399 }
400#endif
401 ICMP_STATS_INC(icmp.xmit);
402 ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif);
403 }
404 pbuf_free(q);
405}
406
407#endif /* LWIP_IPV4 && LWIP_ICMP */
#define PP_HTONS(x)
Definition: def.h:90
#define NULL
Definition: types.h:112
static UINT mib2[]
Definition: main.c:83
#define U16_F
Definition: cc.h:19
#define S16_F
Definition: cc.h:20
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:158
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
#define lwip_strerr(x)
Definition: err.h:106
#define LWIP_IP_HDRINCL
Definition: ip.h:58
#define ip_current_netif()
Definition: ip.h:146
#define IP_PROTO_ICMP
Definition: ip.h:46
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLenum src
Definition: glext.h:6340
GLfloat GLfloat p
Definition: glext.h:8902
uint8_t u8_t
Definition: arch.h:125
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
uint16_t u16_t
Definition: arch.h:127
int16_t s16_t
Definition: arch.h:128
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:57
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
#define ICMP_DEBUG
Definition: opt.h:3371
#define ICMP_TTL
Definition: opt.h:864
#define PBUF_LINK_HLEN
Definition: opt.h:1581
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1590
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition: pbuf.c:959
err_t pbuf_copy_partial_pbuf(struct pbuf *p_to, const struct pbuf *p_from, u16_t copy_len, u16_t offset)
Definition: pbuf.c:986
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_LINK
Definition: pbuf.h:102
@ PBUF_IP
Definition: pbuf.h:97
u16_t inet_chksum(const void *dataptr, u16_t len)
Definition: inet_chksum.c:555
u16_t inet_chksum_pbuf(struct pbuf *p)
Definition: inet_chksum.c:568
#define ICMP_ECHO
Definition: ip_icmp.h:157
if(dx< 0)
Definition: linetemp.h:194
icmp_te_type
Definition: icmp.h:71
icmp_dur_type
Definition: icmp.h:55
#define ICMP_TSR
Definition: icmp.h:54
#define ICMP_PP
Definition: icmp.h:52
#define ICMP_TE
Definition: icmp.h:51
#define ICMP_TS
Definition: icmp.h:53
#define ICMP_DUR
Definition: icmp.h:47
#define ICMP_AM
Definition: icmp.h:57
#define ICMPH_TYPE_SET(hdr, t)
Definition: icmp.h:79
#define ICMP_SQ
Definition: icmp.h:48
#define ICMP_ER
Definition: icmp.h:46
#define ICMP_AMR
Definition: icmp.h:58
#define ICMP_RD
Definition: icmp.h:49
#define MEMCPY(DST, SRC, BYTES)
Definition: macros.h:231
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:416
u8_t pbuf_add_header(struct pbuf *p, size_t header_size_increment)
Definition: pbuf.c:554
u8_t pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
Definition: pbuf.c:585
#define IPH_HL_BYTES(hdr)
Definition: ip4.h:106
#define IPH_TTL_SET(hdr, ttl)
Definition: ip4.h:122
#define IP_HLEN
Definition: ip4.h:64
#define IPH_CHKSUM_SET(hdr, chksum)
Definition: ip4.h:124
#define MIB2_STATS_INC(x)
Definition: stats.h:467
#define ICMP_STATS_INC(x)
Definition: stats.h:344
Definition: inflate.c:139
Definition: icmp.h:65
Definition: ip_icmp.h:52
Definition: ip4.h:73
Definition: netif.h:269
Definition: pbuf.h:186
u16_t tot_len
Definition: pbuf.h:200
int ret