3 * ICMP - Internet Control Message Protocol
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
39 /* Some ICMP messages should be passed to the transport protocols. This
40 is not implemented. */
44 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
46 #include "lwip/icmp.h"
47 #include "lwip/inet_chksum.h"
50 #include "lwip/stats.h"
51 #include "lwip/snmp.h"
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 */
62 /* The amount of data from the original packet to return in a dest-unreachable */
63 #define ICMP_DEST_UNREACH_DATASIZE 8
65 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
68 * Processes ICMP input packets, called from ip_input().
70 * Currently only processes icmp echo requests and sends
71 * out the echo response.
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
77 icmp_input(struct pbuf *p, struct netif *inp)
82 #endif /* LWIP_DEBUG */
83 struct icmp_echo_hdr *iecho;
87 ICMP_STATS_INC(icmp.recv);
88 snmp_inc_icmpinmsgs();
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));
97 type = *((u8_t *)p->payload);
99 code = *(((u8_t *)p->payload)+1);
100 #endif /* LWIP_DEBUG */
103 /* This is OK, echo reply might have been parsed by a raw PCB
104 (as obviously, an echo request has been sent, too). */
107 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
110 #if !LWIP_MULTICAST_PING
111 /* multicast destination address? */
112 if (ip_addr_ismulticast(ip_current_dest_addr())) {
115 #endif /* LWIP_MULTICAST_PING */
116 #if !LWIP_BROADCAST_PING
117 /* broadcast destination address? */
118 if (ip_addr_isbroadcast(ip_current_dest_addr(), inp)) {
121 #endif /* LWIP_BROADCAST_PING */
122 /* broadcast or multicast destination address not acceptd? */
124 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
125 ICMP_STATS_INC(icmp.err);
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"));
136 if (inet_chksum_pbuf(p) != 0) {
137 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
139 ICMP_STATS_INC(icmp.chkerr);
140 snmp_inc_icmpinerrors();
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
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);
154 /* allocate new packet buffer with space for link headers */
155 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
157 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
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);
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);
173 /* free the original p */
175 /* we now have an identical copy of p that has room for link headers */
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);
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;
196 iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
199 /* Set the correct TTL and recalculate the header checksum. */
200 IPH_TTL_SET(iphdr, ICMP_TTL);
201 IPH_CHKSUM_SET(iphdr, 0);
203 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
204 #endif /* CHECKSUM_GEN_IP */
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();
212 if(pbuf_header(p, hlen)) {
213 LWIP_ASSERT("Can't move over header in packet", 0);
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);
220 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
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);
234 ICMP_STATS_INC(icmp.lenerr);
235 snmp_inc_icmpinerrors();
237 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
240 ICMP_STATS_INC(icmp.err);
241 snmp_inc_icmpinerrors();
243 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
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
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
256 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
258 icmp_send_response(p, ICMP_DUR, t);
261 #if IP_FORWARD || IP_REASSEMBLY
263 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
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
270 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
272 icmp_send_response(p, ICMP_TE, t);
275 #endif /* IP_FORWARD || IP_REASSEMBLY */
278 * Send an icmp packet in response to an incoming packet.
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
286 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
289 struct ip_hdr *iphdr;
290 /* we can use the echo header here */
291 struct icmp_echo_hdr *icmphdr;
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,
298 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
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)));
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"));
311 icmphdr = (struct icmp_echo_hdr *)q->payload;
312 icmphdr->type = type;
313 icmphdr->code = code;
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);
321 /* calculate checksum */
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);
334 #endif /* LWIP_ICMP */