ReactOS 0.4.15-dev-7712-gbbbcd8e
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_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
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
65static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
66
76void
77icmp_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);
89
90
91 iphdr = (struct ip_hdr *)p->payload;
92 hlen = IPH_HL(iphdr) * 4;
93 if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
94 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
95 goto lenerr;
96 }
97
98 type = *((u8_t *)p->payload);
99#ifdef LWIP_DEBUG
100 code = *(((u8_t *)p->payload)+1);
101#endif /* LWIP_DEBUG */
102 switch (type) {
103 case ICMP_ER:
104 /* This is OK, echo reply might have been parsed by a raw PCB
105 (as obviously, an echo request has been sent, too). */
106 break;
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? */
114 accepted = 0;
115 }
116#endif /* LWIP_MULTICAST_PING */
117#if !LWIP_BROADCAST_PING
118 /* broadcast destination address? */
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);
142 return;
143 }
144#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
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 = (struct ip_hdr *)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 */
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 = (struct icmp_echo_hdr *)p->payload;
190 ip_addr_copy(iphdr->src, *ip_current_dest_addr());
192 ICMPH_TYPE_SET(iecho, ICMP_ER);
193#if CHECKSUM_GEN_ICMP
194 /* adjust the checksum */
195 if (iecho->chksum >= PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
196 iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
197 } else {
198 iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
199 }
200#else /* CHECKSUM_GEN_ICMP */
201 iecho->chksum = 0;
202#endif /* CHECKSUM_GEN_ICMP */
203
204 /* Set the correct TTL and recalculate the header checksum. */
205 IPH_TTL_SET(iphdr, ICMP_TTL);
206 IPH_CHKSUM_SET(iphdr, 0);
207#if CHECKSUM_GEN_IP
208 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
209#endif /* CHECKSUM_GEN_IP */
210
211 ICMP_STATS_INC(icmp.xmit);
212 /* increase number of messages attempted to send */
214 /* increase number of echo replies attempted to send */
216
217 if(pbuf_header(p, hlen)) {
218 LWIP_ASSERT("Can't move over header in packet", 0);
219 } else {
220 err_t ret;
221 /* send an ICMP packet, src addr is the dest addr of the curren packet */
223 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
224 if (ret != ERR_OK) {
225 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
226 }
227 }
228 break;
229 default:
230 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
231 (s16_t)type, (s16_t)code));
232 ICMP_STATS_INC(icmp.proterr);
233 ICMP_STATS_INC(icmp.drop);
234 }
235 pbuf_free(p);
236 return;
237lenerr:
238 pbuf_free(p);
239 ICMP_STATS_INC(icmp.lenerr);
241 return;
242#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
243memerr:
244 pbuf_free(p);
245 ICMP_STATS_INC(icmp.err);
247 return;
248#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
249}
250
260void
261icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
262{
263 icmp_send_response(p, ICMP_DUR, t);
264}
265
266#if IP_FORWARD || IP_REASSEMBLY
274void
275icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
276{
277 icmp_send_response(p, ICMP_TE, t);
278}
279
280#endif /* IP_FORWARD || IP_REASSEMBLY */
281
290static void
291icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
292{
293 struct pbuf *q;
294 struct ip_hdr *iphdr;
295 /* we can use the echo header here */
296 struct icmp_echo_hdr *icmphdr;
297 ip_addr_t iphdr_src;
298
299 /* ICMP header + IP header + 8 bytes of data */
300 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
301 PBUF_RAM);
302 if (q == NULL) {
303 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
304 return;
305 }
306 LWIP_ASSERT("check that first pbuf can hold icmp message",
307 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
308
309 iphdr = (struct ip_hdr *)p->payload;
310 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
311 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
312 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
314 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
315
316 icmphdr = (struct icmp_echo_hdr *)q->payload;
317 icmphdr->type = type;
318 icmphdr->code = code;
319 icmphdr->id = 0;
320 icmphdr->seqno = 0;
321
322 /* copy fields from original packet */
323 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
324 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
325
326 /* calculate checksum */
327 icmphdr->chksum = 0;
328 icmphdr->chksum = inet_chksum(icmphdr, q->len);
329 ICMP_STATS_INC(icmp.xmit);
330 /* increase number of messages attempted to send */
332 /* increase number of destination unreachable messages attempted to send */
334 ip_addr_copy(iphdr_src, iphdr->src);
335 ip_output(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP);
336 pbuf_free(q);
337}
338
339#endif /* LWIP_ICMP */
#define PP_HTONS(x)
Definition: def.h:88
#define NULL
Definition: types.h:112
signed short s16_t
Definition: cc.h:29
#define U16_F
Definition: cc.h:36
#define S16_F
Definition: cc.h:37
unsigned char u8_t
Definition: cc.h:23
unsigned short u16_t
Definition: cc.h:24
#define IP_HDRINCL
Definition: ip.h:64
#define ip_current_src_addr()
Definition: ip.h:200
#define IPH_HL(hdr)
Definition: ip.h:147
#define ip_current_dest_addr()
Definition: ip.h:202
#define IPH_TTL_SET(hdr, ttl)
Definition: ip.h:161
#define IP_PROTO_ICMP
Definition: ip.h:52
#define IP_HLEN
Definition: ip.h:50
#define IPH_CHKSUM_SET(hdr, chksum)
Definition: ip.h:163
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
#define ERR_OK
Definition: err.h:52
s8_t err_t
Definition: err.h:47
#define snmp_inc_icmpoutechoreps()
Definition: snmp.h:305
#define snmp_inc_icmpinerrors()
Definition: snmp.h:285
#define snmp_inc_icmpinmsgs()
Definition: snmp.h:284
#define snmp_inc_icmpouttimeexcds()
Definition: snmp.h:300
#define snmp_inc_icmpoutmsgs()
Definition: snmp.h:297
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
GLfloat GLfloat p
Definition: glext.h:8902
u16_t inet_chksum(void *dataptr, u16_t len)
Definition: inet_chksum.c:396
u16_t inet_chksum_pbuf(struct pbuf *p)
Definition: inet_chksum.c:409
#define ICMP_ECHO
Definition: ip_icmp.h:157
#define ip_addr_ismulticast(addr1)
Definition: ip_addr.h:208
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:162
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define ip_addr_isbroadcast(ipaddr, netif)
Definition: ip_addr.h:202
#define ip_addr_debug_print(debug, ipaddr)
Definition: ip_addr.h:212
if(dx< 0)
Definition: linetemp.h:194
err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, u8_t ttl, u8_t tos, u8_t proto, struct netif *netif)
Definition: ip.c:641
ip_addr_t current_iphdr_dest
Definition: ip.c:109
err_t ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, u8_t ttl, u8_t tos, u8_t proto)
Definition: ip.c:818
icmp_te_type
Definition: icmp.h:65
icmp_dur_type
Definition: icmp.h:56
#define ICMP_TE
Definition: icmp.h:49
#define ICMP_DUR
Definition: icmp.h:45
#define ICMPH_TYPE_SET(hdr, t)
Definition: icmp.h:95
#define ICMP_ER
Definition: icmp.h:44
#define PBUF_LINK_HLEN
Definition: opt.h:1095
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define ICMP_DEBUG
Definition: opt.h:1940
#define ICMP_TTL
Definition: opt.h:637
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:511
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
Definition: pbuf.c:852
#define PBUF_IP_HLEN
Definition: pbuf.h:48
@ PBUF_RAM
Definition: pbuf.h:58
@ PBUF_LINK
Definition: pbuf.h:53
@ PBUF_IP
Definition: pbuf.h:52
static void accepted(enum accept_stat, struct rpc_err *)
Definition: rpc_prot.c:280
#define ICMP_STATS_INC(x)
Definition: stats.h:187
Definition: inflate.c:139
Definition: ip_icmp.h:52
Definition: ip.h:116
struct ip_addr src dest
Definition: ip.h:96
Definition: netif.h:136
Definition: pbuf.h:79
int ret