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.h"
48 #include "lwip/inet_chksum.h"
51 #include "lwip/stats.h"
52 #include "lwip/snmp.h"
56 /* The amount of data from the original packet to return in a dest-unreachable */
57 #define ICMP_DEST_UNREACH_DATASIZE 8
60 * Processes ICMP input packets, called from ip_input().
62 * Currently only processes icmp echo requests and sends
63 * out the echo response.
65 * @param p the icmp echo request packet, p->payload pointing to the ip header
66 * @param inp the netif on which this packet was received
69 icmp_input(struct pbuf *p, struct netif *inp)
74 #endif /* LWIP_DEBUG */
75 struct icmp_echo_hdr *iecho;
77 struct ip_addr tmpaddr;
80 ICMP_STATS_INC(icmp.recv);
81 snmp_inc_icmpinmsgs();
85 hlen = IPH_HL(iphdr) * 4;
86 if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
87 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
91 type = *((u8_t *)p->payload);
93 code = *(((u8_t *)p->payload)+1);
94 #endif /* LWIP_DEBUG */
97 /* broadcast or multicast destination address? */
98 if (ip_addr_isbroadcast(&iphdr->dest, inp) || ip_addr_ismulticast(&iphdr->dest)) {
99 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
100 ICMP_STATS_INC(icmp.err);
104 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
105 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
106 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
109 if (inet_chksum_pbuf(p) != 0) {
110 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
112 ICMP_STATS_INC(icmp.chkerr);
113 snmp_inc_icmpinerrors();
116 if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
117 /* p is not big enough to contain link headers
118 * allocate a new one and copy p into it
121 /* switch p->payload to ip header */
122 if (pbuf_header(p, hlen)) {
123 LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
126 /* allocate new packet buffer with space for link headers */
127 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
129 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
132 LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
133 (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
134 /* copy the whole packet including ip header */
135 if (pbuf_copy(r, p) != ERR_OK) {
136 LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
140 /* switch r->payload back to icmp header */
141 if (pbuf_header(r, -hlen)) {
142 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
145 /* free the original p */
147 /* we now have an identical copy of p that has room for link headers */
150 /* restore p->payload to point to icmp header */
151 if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
152 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
156 /* At this point, all checks are OK. */
157 /* We generate an answer by switching the dest and src ip addresses,
158 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
160 tmpaddr.addr = iphdr->src.addr;
161 iphdr->src.addr = iphdr->dest.addr;
162 iphdr->dest.addr = tmpaddr.addr;
163 ICMPH_TYPE_SET(iecho, ICMP_ER);
164 /* adjust the checksum */
165 if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
166 iecho->chksum += htons(ICMP_ECHO << 8) + 1;
168 iecho->chksum += htons(ICMP_ECHO << 8);
171 /* Set the correct TTL and recalculate the header checksum. */
172 IPH_TTL_SET(iphdr, ICMP_TTL);
173 IPH_CHKSUM_SET(iphdr, 0);
175 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
176 #endif /* CHECKSUM_GEN_IP */
178 ICMP_STATS_INC(icmp.xmit);
179 /* increase number of messages attempted to send */
180 snmp_inc_icmpoutmsgs();
181 /* increase number of echo replies attempted to send */
182 snmp_inc_icmpoutechoreps();
184 if(pbuf_header(p, hlen)) {
185 LWIP_ASSERT("Can't move over header in packet", 0);
188 ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
189 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
191 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
196 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
197 (s16_t)type, (s16_t)code));
198 ICMP_STATS_INC(icmp.proterr);
199 ICMP_STATS_INC(icmp.drop);
205 ICMP_STATS_INC(icmp.lenerr);
206 snmp_inc_icmpinerrors();
210 ICMP_STATS_INC(icmp.err);
211 snmp_inc_icmpinerrors();
216 * Send an icmp 'destination unreachable' packet, called from ip_input() if
217 * the transport layer protocol is unknown and from udp_input() if the local
220 * @param p the input packet for which the 'unreachable' should be sent,
221 * p->payload pointing to the IP header
222 * @param t type of the 'unreachable' packet
225 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
228 struct ip_hdr *iphdr;
229 struct icmp_dur_hdr *idur;
231 /* ICMP header + IP header + 8 bytes of data */
232 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
235 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
238 LWIP_ASSERT("check that first pbuf can hold icmp message",
239 (q->len >= (sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
244 ICMPH_TYPE_SET(idur, ICMP_DUR);
245 ICMPH_CODE_SET(idur, t);
247 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_dur_hdr), p->payload,
248 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
250 /* calculate checksum */
252 idur->chksum = inet_chksum(idur, q->len);
253 ICMP_STATS_INC(icmp.xmit);
254 /* increase number of messages attempted to send */
255 snmp_inc_icmpoutmsgs();
256 /* increase number of destination unreachable messages attempted to send */
257 snmp_inc_icmpoutdestunreachs();
259 ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP);
263 #if IP_FORWARD || IP_REASSEMBLY
265 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
267 * @param p the input packet for which the 'time exceeded' should be sent,
268 * p->payload pointing to the IP header
269 * @param t type of the 'time exceeded' packet
272 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
275 struct ip_hdr *iphdr;
276 struct icmp_te_hdr *tehdr;
278 /* ICMP header + IP header + 8 bytes of data */
279 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
282 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
285 LWIP_ASSERT("check that first pbuf can hold icmp message",
286 (q->len >= (sizeof(struct icmp_dur_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
289 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
290 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
291 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
292 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
293 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
296 ICMPH_TYPE_SET(tehdr, ICMP_TE);
297 ICMPH_CODE_SET(tehdr, t);
299 /* copy fields from original packet */
300 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_dur_hdr), (u8_t *)p->payload,
301 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
303 /* calculate checksum */
305 tehdr->chksum = inet_chksum(tehdr, q->len);
306 ICMP_STATS_INC(icmp.xmit);
307 /* increase number of messages attempted to send */
308 snmp_inc_icmpoutmsgs();
309 /* increase number of destination unreachable messages attempted to send */
310 snmp_inc_icmpouttimeexcds();
311 ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP);
315 #endif /* IP_FORWARD */
317 #endif /* LWIP_ICMP */