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 #include "lwip/icmp.h"
39 #include "lwip/inet.h"
43 #include "lwip/stats.h"
47 icmp_input(struct pbuf *p, struct netif *inp)
50 struct icmp_echo_hdr *iecho;
52 struct ip_addr tmpaddr;
55 ++lwip_stats.icmp.recv;
56 #endif /* ICMP_STATS */
58 /* TODO: check length before accessing payload! */
60 type = ((char *)p->payload)[0];
64 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
66 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
67 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
71 ++lwip_stats.icmp.lenerr;
72 #endif /* ICMP_STATS */
77 iphdr = (struct ip_hdr *)((char *)p->payload - IP_HLEN);
78 if (inet_chksum_pbuf(p) != 0) {
79 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%x)\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
82 ++lwip_stats.icmp.chkerr;
83 #endif /* ICMP_STATS */
86 LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %d p->tot_len %d\n", p->len, p->tot_len));
87 ip_addr_set(&tmpaddr, &(iphdr->src));
88 ip_addr_set(&(iphdr->src), &(iphdr->dest));
89 ip_addr_set(&(iphdr->dest), &tmpaddr);
90 iecho->type = ICMP6_ER;
91 /* adjust the checksum */
92 if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) {
93 iecho->chksum += htons(ICMP6_ECHO << 8) + 1;
95 iecho->chksum += htons(ICMP6_ECHO << 8);
97 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%x)\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
99 ++lwip_stats.icmp.xmit;
100 #endif /* ICMP_STATS */
102 /* LWIP_DEBUGF("icmp: p->len %u p->tot_len %u\n", p->len, p->tot_len);*/
103 ip_output_if (p, &(iphdr->src), IP_HDRINCL,
104 iphdr->hoplim, IP_PROTO_ICMP, inp);
107 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %d not supported.\n", (int)type));
109 ++lwip_stats.icmp.proterr;
110 ++lwip_stats.icmp.drop;
111 #endif /* ICMP_STATS */
118 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
121 struct ip_hdr *iphdr;
122 struct icmp_dur_hdr *idur;
124 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
125 /* ICMP header + IP header + 8 bytes of data */
130 idur->type = (char)ICMP6_DUR;
131 idur->icode = (char)t;
133 memcpy((char *)q->payload + 8, p->payload, IP_HLEN + 8);
135 /* calculate checksum */
137 idur->chksum = inet_chksum(idur, q->len);
139 ++lwip_stats.icmp.xmit;
140 #endif /* ICMP_STATS */
143 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
148 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
151 struct ip_hdr *iphdr;
152 struct icmp_te_hdr *tehdr;
154 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded\n"));
156 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
161 tehdr->type = (char)ICMP6_TE;
162 tehdr->icode = (char)t;
164 /* copy fields from original packet */
165 memcpy((char *)q->payload + 8, (char *)p->payload, IP_HLEN + 8);
167 /* calculate checksum */
169 tehdr->chksum = inet_chksum(tehdr, q->len);
171 ++lwip_stats.icmp.xmit;
172 #endif /* ICMP_STATS */
174 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);