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. */
39 #include "lwip/icmp.h"
40 #include "lwip/inet.h"
43 #include "lwip/stats.h"
44 #include "lwip/snmp.h"
47 icmp_input(struct pbuf *p, struct netif *inp)
51 struct icmp_echo_hdr *iecho;
53 struct ip_addr tmpaddr;
56 ICMP_STATS_INC(icmp.recv);
57 snmp_inc_icmpinmsgs();
61 hlen = IPH_HL(iphdr) * 4;
62 if (pbuf_header(p, -((s16_t)hlen)) || (p->tot_len < sizeof(u16_t)*2)) {
63 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
65 ICMP_STATS_INC(icmp.lenerr);
66 snmp_inc_icmpinerrors();
70 type = *((u8_t *)p->payload);
71 code = *(((u8_t *)p->payload)+1);
74 /* broadcast or multicast destination address? */
75 if (ip_addr_isbroadcast(&iphdr->dest, inp) || ip_addr_ismulticast(&iphdr->dest)) {
76 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
77 ICMP_STATS_INC(icmp.err);
81 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
82 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
83 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
85 ICMP_STATS_INC(icmp.lenerr);
86 snmp_inc_icmpinerrors();
91 if (inet_chksum_pbuf(p) != 0) {
92 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
94 ICMP_STATS_INC(icmp.chkerr);
95 snmp_inc_icmpinerrors();
98 tmpaddr.addr = iphdr->src.addr;
99 iphdr->src.addr = iphdr->dest.addr;
100 iphdr->dest.addr = tmpaddr.addr;
101 ICMPH_TYPE_SET(iecho, ICMP_ER);
102 /* adjust the checksum */
103 if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
104 iecho->chksum += htons(ICMP_ECHO << 8) + 1;
106 iecho->chksum += htons(ICMP_ECHO << 8);
108 ICMP_STATS_INC(icmp.xmit);
109 /* increase number of messages attempted to send */
110 snmp_inc_icmpoutmsgs();
111 /* increase number of echo replies attempted to send */
112 snmp_inc_icmpoutechoreps();
114 pbuf_header(p, hlen);
115 ip_output_if(p, &(iphdr->src), IP_HDRINCL,
116 IPH_TTL(iphdr), 0, IP_PROTO_ICMP, inp);
119 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code));
120 ICMP_STATS_INC(icmp.proterr);
121 ICMP_STATS_INC(icmp.drop);
127 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
130 struct ip_hdr *iphdr;
131 struct icmp_dur_hdr *idur;
133 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
134 /* ICMP header + IP header + 8 bytes of data */
139 ICMPH_TYPE_SET(idur, ICMP_DUR);
140 ICMPH_CODE_SET(idur, t);
142 memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8);
144 /* calculate checksum */
146 idur->chksum = inet_chksum(idur, q->len);
147 ICMP_STATS_INC(icmp.xmit);
148 /* increase number of messages attempted to send */
149 snmp_inc_icmpoutmsgs();
150 /* increase number of destination unreachable messages attempted to send */
151 snmp_inc_icmpoutdestunreachs();
153 ip_output(q, NULL, &(iphdr->src),
154 ICMP_TTL, 0, IP_PROTO_ICMP);
160 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
163 struct ip_hdr *iphdr;
164 struct icmp_te_hdr *tehdr;
166 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
169 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
170 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
171 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
172 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
173 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
176 ICMPH_TYPE_SET(tehdr, ICMP_TE);
177 ICMPH_CODE_SET(tehdr, t);
179 /* copy fields from original packet */
180 memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8);
182 /* calculate checksum */
184 tehdr->chksum = inet_chksum(tehdr, q->len);
185 ICMP_STATS_INC(icmp.xmit);
186 /* increase number of messages attempted to send */
187 snmp_inc_icmpoutmsgs();
188 /* increase number of destination unreachable messages attempted to send */
189 snmp_inc_icmpouttimeexcds();
190 ip_output(q, NULL, &(iphdr->src),
191 ICMP_TTL, 0, IP_PROTO_ICMP);
195 #endif /* IP_FORWARD */