2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without modification,
\r
6 * are permitted provided that the following conditions are met:
\r
8 * 1. Redistributions of source code must retain the above copyright notice,
\r
9 * this list of conditions and the following disclaimer.
\r
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
11 * this list of conditions and the following disclaimer in the documentation
\r
12 * and/or other materials provided with the distribution.
\r
13 * 3. The name of the author may not be used to endorse or promote products
\r
14 * derived from this software without specific prior written permission.
\r
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
27 * This file is part of the lwIP TCP/IP stack.
\r
29 * Author: Adam Dunkels <adam@sics.se>
\r
33 /* Some ICMP messages should be passed to the transport protocols. This
\r
34 is not implemented. */
\r
36 #include "lwip/opt.h"
\r
38 #include "lwip/icmp.h"
\r
39 #include "lwip/inet.h"
\r
40 #include "lwip/ip.h"
\r
41 #include "lwip/def.h"
\r
43 #include "lwip/stats.h"
\r
47 icmp_input(struct pbuf *p, struct netif *inp)
\r
50 struct icmp_echo_hdr *iecho;
\r
51 struct ip_hdr *iphdr;
\r
52 struct ip_addr tmpaddr;
\r
55 ++lwip_stats.icmp.recv;
\r
56 #endif /* ICMP_STATS */
\r
58 /* TODO: check length before accessing payload! */
\r
60 type = ((u8_t *)p->payload)[0];
\r
64 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
\r
66 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
\r
67 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
\r
71 ++lwip_stats.icmp.lenerr;
\r
72 #endif /* ICMP_STATS */
\r
77 iphdr = (struct ip_hdr *)((u8_t *)p->payload - IP_HLEN);
\r
78 if (inet_chksum_pbuf(p) != 0) {
\r
79 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)));
\r
82 ++lwip_stats.icmp.chkerr;
\r
83 #endif /* ICMP_STATS */
\r
86 LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %"S16_F" p->tot_len %"S16_F"\n", p->len, p->tot_len));
\r
87 ip_addr_set(&tmpaddr, &(iphdr->src));
\r
88 ip_addr_set(&(iphdr->src), &(iphdr->dest));
\r
89 ip_addr_set(&(iphdr->dest), &tmpaddr);
\r
90 iecho->type = ICMP6_ER;
\r
91 /* adjust the checksum */
\r
92 if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) {
\r
93 iecho->chksum += htons(ICMP6_ECHO << 8) + 1;
\r
95 iecho->chksum += htons(ICMP6_ECHO << 8);
\r
97 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)));
\r
99 ++lwip_stats.icmp.xmit;
\r
100 #endif /* ICMP_STATS */
\r
102 /* LWIP_DEBUGF("icmp: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
\r
103 ip_output_if (p, &(iphdr->src), IP_HDRINCL,
\r
104 iphdr->hoplim, IP_PROTO_ICMP, inp);
\r
107 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" not supported.\n", (s16_t)type));
\r
109 ++lwip_stats.icmp.proterr;
\r
110 ++lwip_stats.icmp.drop;
\r
111 #endif /* ICMP_STATS */
\r
118 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
\r
121 struct ip_hdr *iphdr;
\r
122 struct icmp_dur_hdr *idur;
\r
124 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
\r
125 /* ICMP header + IP header + 8 bytes of data */
\r
127 iphdr = p->payload;
\r
130 idur->type = (u8_t)ICMP6_DUR;
\r
131 idur->icode = (u8_t)t;
\r
133 memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8);
\r
135 /* calculate checksum */
\r
137 idur->chksum = inet_chksum(idur, q->len);
\r
139 ++lwip_stats.icmp.xmit;
\r
140 #endif /* ICMP_STATS */
\r
143 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
\r
148 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
\r
151 struct ip_hdr *iphdr;
\r
152 struct icmp_te_hdr *tehdr;
\r
154 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded\n"));
\r
156 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
\r
158 iphdr = p->payload;
\r
160 tehdr = q->payload;
\r
161 tehdr->type = (u8_t)ICMP6_TE;
\r
162 tehdr->icode = (u8_t)t;
\r
164 /* copy fields from original packet */
\r
165 memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8);
\r
167 /* calculate checksum */
\r
169 tehdr->chksum = inet_chksum(tehdr, q->len);
\r
171 ++lwip_stats.icmp.xmit;
\r
172 #endif /* ICMP_STATS */
\r
174 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
\r