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 ip 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();
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));
98 type = *((u8_t *)p->payload);
100 code = *(((u8_t *)p->payload)+1);
101 #endif /* LWIP_DEBUG */
104 /* This is OK, echo reply might have been parsed by a raw PCB
105 (as obviously, an echo request has been sent, too). */
108 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
111 #if !LWIP_MULTICAST_PING
112 /* multicast destination address? */
113 if (ip_addr_ismulticast(¤t_iphdr_dest)) {
116 #endif /* LWIP_MULTICAST_PING */
117 #if !LWIP_BROADCAST_PING
118 /* broadcast destination address? */
119 if (ip_addr_isbroadcast(¤t_iphdr_dest, inp)) {
122 #endif /* LWIP_BROADCAST_PING */
123 /* broadcast or multicast destination address not acceptd? */
125 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
126 ICMP_STATS_INC(icmp.err);
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"));
137 if (inet_chksum_pbuf(p) != 0) {
138 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
140 ICMP_STATS_INC(icmp.chkerr);
141 snmp_inc_icmpinerrors();
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
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);
155 /* allocate new packet buffer with space for link headers */
156 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
158 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
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);
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);
174 /* free the original p */
176 /* we now have an identical copy of p that has room for link headers */
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);
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());
191 ip_addr_copy(iphdr->dest, *ip_current_src_addr());
192 ICMPH_TYPE_SET(iecho, ICMP_ER);
193 /* adjust the checksum */
194 if (iecho->chksum >= PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
195 iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
197 iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
200 /* Set the correct TTL and recalculate the header checksum. */
201 IPH_TTL_SET(iphdr, ICMP_TTL);
202 IPH_CHKSUM_SET(iphdr, 0);
204 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
205 #endif /* CHECKSUM_GEN_IP */
207 ICMP_STATS_INC(icmp.xmit);
208 /* increase number of messages attempted to send */
209 snmp_inc_icmpoutmsgs();
210 /* increase number of echo replies attempted to send */
211 snmp_inc_icmpoutechoreps();
213 if(pbuf_header(p, hlen)) {
214 LWIP_ASSERT("Can't move over header in packet", 0);
217 /* send an ICMP packet, src addr is the dest addr of the curren packet */
218 ret = ip_output_if(p, ip_current_dest_addr(), IP_HDRINCL,
219 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
221 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
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);
235 ICMP_STATS_INC(icmp.lenerr);
236 snmp_inc_icmpinerrors();
238 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
241 ICMP_STATS_INC(icmp.err);
242 snmp_inc_icmpinerrors();
244 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
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
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
257 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
259 icmp_send_response(p, ICMP_DUR, t);
262 #if IP_FORWARD || IP_REASSEMBLY
264 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
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
271 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
273 icmp_send_response(p, ICMP_TE, t);
276 #endif /* IP_FORWARD || IP_REASSEMBLY */
279 * Send an icmp packet in response to an incoming packet.
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
287 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
290 struct ip_hdr *iphdr;
291 /* we can use the echo header here */
292 struct icmp_echo_hdr *icmphdr;
295 /* ICMP header + IP header + 8 bytes of data */
296 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
299 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
302 LWIP_ASSERT("check that first pbuf can hold icmp message",
303 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
305 iphdr = (struct ip_hdr *)p->payload;
306 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
307 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
308 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
309 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
310 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
312 icmphdr = (struct icmp_echo_hdr *)q->payload;
313 icmphdr->type = type;
314 icmphdr->code = code;
318 /* copy fields from original packet */
319 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
320 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
322 /* calculate checksum */
324 icmphdr->chksum = inet_chksum(icmphdr, q->len);
325 ICMP_STATS_INC(icmp.xmit);
326 /* increase number of messages attempted to send */
327 snmp_inc_icmpoutmsgs();
328 /* increase number of destination unreachable messages attempted to send */
329 snmp_inc_icmpouttimeexcds();
330 ip_addr_copy(iphdr_src, iphdr->src);
331 ip_output(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP);
335 #endif /* LWIP_ICMP */