2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
33 /* Some ICMP messages should be passed to the transport protocols. This
34 is not implemented. */
38 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
40 #include "lwip/icmp.h"
41 #include "lwip/inet.h"
44 #include "lwip/stats.h"
47 icmp_input(struct pbuf *p, struct netif *inp)
50 struct icmp_echo_hdr *iecho;
52 struct ip_addr tmpaddr;
54 ICMP_STATS_INC(icmp.recv);
56 /* TODO: check length before accessing payload! */
58 type = ((u8_t *)p->payload)[0];
62 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
64 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
65 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
68 ICMP_STATS_INC(icmp.lenerr);
72 iphdr = (struct ip_hdr *)((u8_t *)p->payload - IP_HLEN);
73 if (inet_chksum_pbuf(p) != 0) {
74 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
75 ICMP_STATS_INC(icmp.chkerr);
78 LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %"S16_F" p->tot_len %"S16_F"\n", p->len, p->tot_len));
79 ip_addr_set(&tmpaddr, &(iphdr->src));
80 ip_addr_set(&(iphdr->src), &(iphdr->dest));
81 ip_addr_set(&(iphdr->dest), &tmpaddr);
82 iecho->type = ICMP6_ER;
83 /* adjust the checksum */
84 if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) {
85 iecho->chksum += htons(ICMP6_ECHO << 8) + 1;
87 iecho->chksum += htons(ICMP6_ECHO << 8);
89 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
90 ICMP_STATS_INC(icmp.xmit);
92 /* LWIP_DEBUGF("icmp: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
93 ip_output_if (p, &(iphdr->src), IP_HDRINCL,
94 iphdr->hoplim, IP_PROTO_ICMP, inp);
97 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" not supported.\n", (s16_t)type));
98 ICMP_STATS_INC(icmp.proterr);
99 ICMP_STATS_INC(icmp.drop);
106 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
109 struct ip_hdr *iphdr;
110 struct icmp_dur_hdr *idur;
112 /* @todo: can this be PBUF_LINK instead of PBUF_IP? */
113 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
114 /* ICMP header + IP header + 8 bytes of data */
116 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
120 LWIP_ASSERT("check that first pbuf can hold icmp message",
121 (q->len >= (8 + IP_HLEN + 8)));
126 idur->type = (u8_t)ICMP6_DUR;
127 idur->icode = (u8_t)t;
129 SMEMCPY((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8);
131 /* calculate checksum */
133 idur->chksum = inet_chksum(idur, q->len);
134 ICMP_STATS_INC(icmp.xmit);
137 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
142 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
145 struct ip_hdr *iphdr;
146 struct icmp_te_hdr *tehdr;
148 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded\n"));
150 /* @todo: can this be PBUF_LINK instead of PBUF_IP? */
151 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
152 /* ICMP header + IP header + 8 bytes of data */
154 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
158 LWIP_ASSERT("check that first pbuf can hold icmp message",
159 (q->len >= (8 + IP_HLEN + 8)));
164 tehdr->type = (u8_t)ICMP6_TE;
165 tehdr->icode = (u8_t)t;
167 /* copy fields from original packet */
168 SMEMCPY((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8);
170 /* calculate checksum */
172 tehdr->chksum = inet_chksum(tehdr, q->len);
173 ICMP_STATS_INC(icmp.xmit);
175 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
179 #endif /* LWIP_ICMP */