User | Revision | Line number | New contents of line |
mbed714 |
0:d616ece2d859
|
1
|
/**
|
mbed714 |
0:d616ece2d859
|
2
|
* @file
|
mbed714 |
0:d616ece2d859
|
3
|
* ICMP - Internet Control Message Protocol
|
mbed714 |
0:d616ece2d859
|
4
|
*
|
mbed714 |
0:d616ece2d859
|
5
|
*/
|
mbed714 |
0:d616ece2d859
|
6
|
|
mbed714 |
0:d616ece2d859
|
7
|
/*
|
mbed714 |
0:d616ece2d859
|
8
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
mbed714 |
0:d616ece2d859
|
9
|
* All rights reserved.
|
mbed714 |
0:d616ece2d859
|
10
|
*
|
mbed714 |
0:d616ece2d859
|
11
|
* Redistribution and use in source and binary forms, with or without modification,
|
mbed714 |
0:d616ece2d859
|
12
|
* are permitted provided that the following conditions are met:
|
mbed714 |
0:d616ece2d859
|
13
|
*
|
mbed714 |
0:d616ece2d859
|
14
|
* 1. Redistributions of source code must retain the above copyright notice,
|
mbed714 |
0:d616ece2d859
|
15
|
* this list of conditions and the following disclaimer.
|
mbed714 |
0:d616ece2d859
|
16
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
mbed714 |
0:d616ece2d859
|
17
|
* this list of conditions and the following disclaimer in the documentation
|
mbed714 |
0:d616ece2d859
|
18
|
* and/or other materials provided with the distribution.
|
mbed714 |
0:d616ece2d859
|
19
|
* 3. The name of the author may not be used to endorse or promote products
|
mbed714 |
0:d616ece2d859
|
20
|
* derived from this software without specific prior written permission.
|
mbed714 |
0:d616ece2d859
|
21
|
*
|
mbed714 |
0:d616ece2d859
|
22
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
mbed714 |
0:d616ece2d859
|
23
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
mbed714 |
0:d616ece2d859
|
24
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
mbed714 |
0:d616ece2d859
|
25
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mbed714 |
0:d616ece2d859
|
26
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
mbed714 |
0:d616ece2d859
|
27
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
mbed714 |
0:d616ece2d859
|
28
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
mbed714 |
0:d616ece2d859
|
29
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
mbed714 |
0:d616ece2d859
|
30
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
mbed714 |
0:d616ece2d859
|
31
|
* OF SUCH DAMAGE.
|
mbed714 |
0:d616ece2d859
|
32
|
*
|
mbed714 |
0:d616ece2d859
|
33
|
* This file is part of the lwIP TCP/IP stack.
|
mbed714 |
0:d616ece2d859
|
34
|
*
|
mbed714 |
0:d616ece2d859
|
35
|
* Author: Adam Dunkels <adam@sics.se>
|
mbed714 |
0:d616ece2d859
|
36
|
*
|
mbed714 |
0:d616ece2d859
|
37
|
*/
|
mbed714 |
0:d616ece2d859
|
38
|
|
mbed714 |
0:d616ece2d859
|
39
|
/* Some ICMP messages should be passed to the transport protocols. This
|
mbed714 |
0:d616ece2d859
|
40
|
is not implemented. */
|
mbed714 |
0:d616ece2d859
|
41
|
|
mbed714 |
0:d616ece2d859
|
42
|
#include "lwip/opt.h"
|
mbed714 |
0:d616ece2d859
|
43
|
|
mbed714 |
0:d616ece2d859
|
44
|
#if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
|
mbed714 |
0:d616ece2d859
|
45
|
|
mbed714 |
0:d616ece2d859
|
46
|
#include "lwip/icmp.h"
|
mbed714 |
0:d616ece2d859
|
47
|
#include "lwip/inet_chksum.h"
|
mbed714 |
0:d616ece2d859
|
48
|
#include "lwip/ip.h"
|
mbed714 |
0:d616ece2d859
|
49
|
#include "lwip/def.h"
|
mbed714 |
0:d616ece2d859
|
50
|
#include "lwip/stats.h"
|
mbed714 |
0:d616ece2d859
|
51
|
#include "lwip/snmp.h"
|
mbed714 |
0:d616ece2d859
|
52
|
|
mbed714 |
0:d616ece2d859
|
53
|
#include <string.h>
|
mbed714 |
0:d616ece2d859
|
54
|
|
mbed714 |
0:d616ece2d859
|
55
|
/** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
|
mbed714 |
0:d616ece2d859
|
56
|
* used to modify and send a response packet (and to 1 if this is not the case,
|
mbed714 |
0:d616ece2d859
|
57
|
* e.g. when link header is stripped of when receiving) */
|
mbed714 |
0:d616ece2d859
|
58
|
#ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
|
mbed714 |
0:d616ece2d859
|
59
|
#define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
|
mbed714 |
0:d616ece2d859
|
60
|
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
|
mbed714 |
0:d616ece2d859
|
61
|
|
mbed714 |
0:d616ece2d859
|
62
|
/* The amount of data from the original packet to return in a dest-unreachable */
|
mbed714 |
0:d616ece2d859
|
63
|
#define ICMP_DEST_UNREACH_DATASIZE 8
|
mbed714 |
0:d616ece2d859
|
64
|
|
mbed714 |
0:d616ece2d859
|
65
|
static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
|
mbed714 |
0:d616ece2d859
|
66
|
|
mbed714 |
0:d616ece2d859
|
67
|
/**
|
mbed714 |
0:d616ece2d859
|
68
|
* Processes ICMP input packets, called from ip_input().
|
mbed714 |
0:d616ece2d859
|
69
|
*
|
mbed714 |
0:d616ece2d859
|
70
|
* Currently only processes icmp echo requests and sends
|
mbed714 |
0:d616ece2d859
|
71
|
* out the echo response.
|
mbed714 |
0:d616ece2d859
|
72
|
*
|
mbed714 |
0:d616ece2d859
|
73
|
* @param p the icmp echo request packet, p->payload pointing to the ip header
|
mbed714 |
0:d616ece2d859
|
74
|
* @param inp the netif on which this packet was received
|
mbed714 |
0:d616ece2d859
|
75
|
*/
|
mbed714 |
0:d616ece2d859
|
76
|
void
|
mbed714 |
0:d616ece2d859
|
77
|
icmp_input(struct pbuf *p, struct netif *inp)
|
mbed714 |
0:d616ece2d859
|
78
|
{
|
mbed714 |
0:d616ece2d859
|
79
|
u8_t type;
|
mbed714 |
0:d616ece2d859
|
80
|
#ifdef LWIP_DEBUG
|
mbed714 |
0:d616ece2d859
|
81
|
u8_t code;
|
mbed714 |
0:d616ece2d859
|
82
|
#endif /* LWIP_DEBUG */
|
mbed714 |
0:d616ece2d859
|
83
|
struct icmp_echo_hdr *iecho;
|
mbed714 |
0:d616ece2d859
|
84
|
struct ip_hdr *iphdr;
|
mbed714 |
0:d616ece2d859
|
85
|
s16_t hlen;
|
mbed714 |
0:d616ece2d859
|
86
|
|
mbed714 |
0:d616ece2d859
|
87
|
ICMP_STATS_INC(icmp.recv);
|
mbed714 |
0:d616ece2d859
|
88
|
snmp_inc_icmpinmsgs();
|
mbed714 |
0:d616ece2d859
|
89
|
|
mbed714 |
0:d616ece2d859
|
90
|
|
mbed714 |
0:d616ece2d859
|
91
|
iphdr = (struct ip_hdr *)p->payload;
|
mbed714 |
0:d616ece2d859
|
92
|
hlen = IPH_HL(iphdr) * 4;
|
mbed714 |
0:d616ece2d859
|
93
|
if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
|
mbed714 |
0:d616ece2d859
|
94
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
|
mbed714 |
0:d616ece2d859
|
95
|
goto lenerr;
|
mbed714 |
0:d616ece2d859
|
96
|
}
|
mbed714 |
0:d616ece2d859
|
97
|
|
mbed714 |
0:d616ece2d859
|
98
|
type = *((u8_t *)p->payload);
|
mbed714 |
0:d616ece2d859
|
99
|
#ifdef LWIP_DEBUG
|
mbed714 |
0:d616ece2d859
|
100
|
code = *(((u8_t *)p->payload)+1);
|
mbed714 |
0:d616ece2d859
|
101
|
#endif /* LWIP_DEBUG */
|
mbed714 |
0:d616ece2d859
|
102
|
switch (type) {
|
mbed714 |
0:d616ece2d859
|
103
|
case ICMP_ER:
|
mbed714 |
0:d616ece2d859
|
104
|
/* This is OK, echo reply might have been parsed by a raw PCB
|
mbed714 |
0:d616ece2d859
|
105
|
(as obviously, an echo request has been sent, too). */
|
mbed714 |
0:d616ece2d859
|
106
|
break;
|
mbed714 |
0:d616ece2d859
|
107
|
case ICMP_ECHO:
|
mbed714 |
0:d616ece2d859
|
108
|
#if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
|
mbed714 |
0:d616ece2d859
|
109
|
{
|
mbed714 |
0:d616ece2d859
|
110
|
int accepted = 1;
|
mbed714 |
0:d616ece2d859
|
111
|
#if !LWIP_MULTICAST_PING
|
mbed714 |
0:d616ece2d859
|
112
|
/* multicast destination address? */
|
mbed714 |
0:d616ece2d859
|
113
|
if (ip_addr_ismulticast(¤t_iphdr_dest)) {
|
mbed714 |
0:d616ece2d859
|
114
|
accepted = 0;
|
mbed714 |
0:d616ece2d859
|
115
|
}
|
mbed714 |
0:d616ece2d859
|
116
|
#endif /* LWIP_MULTICAST_PING */
|
mbed714 |
0:d616ece2d859
|
117
|
#if !LWIP_BROADCAST_PING
|
mbed714 |
0:d616ece2d859
|
118
|
/* broadcast destination address? */
|
mbed714 |
0:d616ece2d859
|
119
|
if (ip_addr_isbroadcast(¤t_iphdr_dest, inp)) {
|
mbed714 |
0:d616ece2d859
|
120
|
accepted = 0;
|
mbed714 |
0:d616ece2d859
|
121
|
}
|
mbed714 |
0:d616ece2d859
|
122
|
#endif /* LWIP_BROADCAST_PING */
|
mbed714 |
0:d616ece2d859
|
123
|
/* broadcast or multicast destination address not acceptd? */
|
mbed714 |
0:d616ece2d859
|
124
|
if (!accepted) {
|
mbed714 |
0:d616ece2d859
|
125
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
|
mbed714 |
0:d616ece2d859
|
126
|
ICMP_STATS_INC(icmp.err);
|
mbed714 |
0:d616ece2d859
|
127
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
128
|
return;
|
mbed714 |
0:d616ece2d859
|
129
|
}
|
mbed714 |
0:d616ece2d859
|
130
|
}
|
mbed714 |
0:d616ece2d859
|
131
|
#endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
|
mbed714 |
0:d616ece2d859
|
132
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
|
mbed714 |
0:d616ece2d859
|
133
|
if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
|
mbed714 |
0:d616ece2d859
|
134
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
|
mbed714 |
0:d616ece2d859
|
135
|
goto lenerr;
|
mbed714 |
0:d616ece2d859
|
136
|
}
|
mbed714 |
0:d616ece2d859
|
137
|
if (inet_chksum_pbuf(p) != 0) {
|
mbed714 |
0:d616ece2d859
|
138
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
|
mbed714 |
0:d616ece2d859
|
139
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
140
|
ICMP_STATS_INC(icmp.chkerr);
|
mbed714 |
0:d616ece2d859
|
141
|
snmp_inc_icmpinerrors();
|
mbed714 |
0:d616ece2d859
|
142
|
return;
|
mbed714 |
0:d616ece2d859
|
143
|
}
|
mbed714 |
0:d616ece2d859
|
144
|
#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
|
mbed714 |
0:d616ece2d859
|
145
|
if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
|
mbed714 |
0:d616ece2d859
|
146
|
/* p is not big enough to contain link headers
|
mbed714 |
0:d616ece2d859
|
147
|
* allocate a new one and copy p into it
|
mbed714 |
0:d616ece2d859
|
148
|
*/
|
mbed714 |
0:d616ece2d859
|
149
|
struct pbuf *r;
|
mbed714 |
0:d616ece2d859
|
150
|
/* switch p->payload to ip header */
|
mbed714 |
0:d616ece2d859
|
151
|
if (pbuf_header(p, hlen)) {
|
mbed714 |
0:d616ece2d859
|
152
|
LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
|
mbed714 |
0:d616ece2d859
|
153
|
goto memerr;
|
mbed714 |
0:d616ece2d859
|
154
|
}
|
mbed714 |
0:d616ece2d859
|
155
|
/* allocate new packet buffer with space for link headers */
|
mbed714 |
0:d616ece2d859
|
156
|
r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
|
mbed714 |
0:d616ece2d859
|
157
|
if (r == NULL) {
|
mbed714 |
0:d616ece2d859
|
158
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
|
mbed714 |
0:d616ece2d859
|
159
|
goto memerr;
|
mbed714 |
0:d616ece2d859
|
160
|
}
|
mbed714 |
0:d616ece2d859
|
161
|
LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
|
mbed714 |
0:d616ece2d859
|
162
|
(r->len >= hlen + sizeof(struct icmp_echo_hdr)));
|
mbed714 |
0:d616ece2d859
|
163
|
/* copy the whole packet including ip header */
|
mbed714 |
0:d616ece2d859
|
164
|
if (pbuf_copy(r, p) != ERR_OK) {
|
mbed714 |
0:d616ece2d859
|
165
|
LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
|
mbed714 |
0:d616ece2d859
|
166
|
goto memerr;
|
mbed714 |
0:d616ece2d859
|
167
|
}
|
mbed714 |
0:d616ece2d859
|
168
|
iphdr = (struct ip_hdr *)r->payload;
|
mbed714 |
0:d616ece2d859
|
169
|
/* switch r->payload back to icmp header */
|
mbed714 |
0:d616ece2d859
|
170
|
if (pbuf_header(r, -hlen)) {
|
mbed714 |
0:d616ece2d859
|
171
|
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
|
mbed714 |
0:d616ece2d859
|
172
|
goto memerr;
|
mbed714 |
0:d616ece2d859
|
173
|
}
|
mbed714 |
0:d616ece2d859
|
174
|
/* free the original p */
|
mbed714 |
0:d616ece2d859
|
175
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
176
|
/* we now have an identical copy of p that has room for link headers */
|
mbed714 |
0:d616ece2d859
|
177
|
p = r;
|
mbed714 |
0:d616ece2d859
|
178
|
} else {
|
mbed714 |
0:d616ece2d859
|
179
|
/* restore p->payload to point to icmp header */
|
mbed714 |
0:d616ece2d859
|
180
|
if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
|
mbed714 |
0:d616ece2d859
|
181
|
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
|
mbed714 |
0:d616ece2d859
|
182
|
goto memerr;
|
mbed714 |
0:d616ece2d859
|
183
|
}
|
mbed714 |
0:d616ece2d859
|
184
|
}
|
mbed714 |
0:d616ece2d859
|
185
|
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
|
mbed714 |
0:d616ece2d859
|
186
|
/* At this point, all checks are OK. */
|
mbed714 |
0:d616ece2d859
|
187
|
/* We generate an answer by switching the dest and src ip addresses,
|
mbed714 |
0:d616ece2d859
|
188
|
* setting the icmp type to ECHO_RESPONSE and updating the checksum. */
|
mbed714 |
0:d616ece2d859
|
189
|
iecho = (struct icmp_echo_hdr *)p->payload;
|
mbed714 |
0:d616ece2d859
|
190
|
ip_addr_copy(iphdr->src, *ip_current_dest_addr());
|
mbed714 |
0:d616ece2d859
|
191
|
ip_addr_copy(iphdr->dest, *ip_current_src_addr());
|
mbed714 |
0:d616ece2d859
|
192
|
ICMPH_TYPE_SET(iecho, ICMP_ER);
|
mbed714 |
0:d616ece2d859
|
193
|
/* adjust the checksum */
|
mbed714 |
0:d616ece2d859
|
194
|
if (iecho->chksum >= PP_HTONS(0xffff - (ICMP_ECHO << 8))) {
|
mbed714 |
0:d616ece2d859
|
195
|
iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
|
mbed714 |
0:d616ece2d859
|
196
|
} else {
|
mbed714 |
0:d616ece2d859
|
197
|
iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
|
mbed714 |
0:d616ece2d859
|
198
|
}
|
mbed714 |
0:d616ece2d859
|
199
|
|
mbed714 |
0:d616ece2d859
|
200
|
/* Set the correct TTL and recalculate the header checksum. */
|
mbed714 |
0:d616ece2d859
|
201
|
IPH_TTL_SET(iphdr, ICMP_TTL);
|
mbed714 |
0:d616ece2d859
|
202
|
IPH_CHKSUM_SET(iphdr, 0);
|
mbed714 |
0:d616ece2d859
|
203
|
#if CHECKSUM_GEN_IP
|
mbed714 |
0:d616ece2d859
|
204
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
|
mbed714 |
0:d616ece2d859
|
205
|
#endif /* CHECKSUM_GEN_IP */
|
mbed714 |
0:d616ece2d859
|
206
|
|
mbed714 |
0:d616ece2d859
|
207
|
ICMP_STATS_INC(icmp.xmit);
|
mbed714 |
0:d616ece2d859
|
208
|
/* increase number of messages attempted to send */
|
mbed714 |
0:d616ece2d859
|
209
|
snmp_inc_icmpoutmsgs();
|
mbed714 |
0:d616ece2d859
|
210
|
/* increase number of echo replies attempted to send */
|
mbed714 |
0:d616ece2d859
|
211
|
snmp_inc_icmpoutechoreps();
|
mbed714 |
0:d616ece2d859
|
212
|
|
mbed714 |
0:d616ece2d859
|
213
|
if(pbuf_header(p, hlen)) {
|
mbed714 |
0:d616ece2d859
|
214
|
LWIP_ASSERT("Can't move over header in packet", 0);
|
mbed714 |
0:d616ece2d859
|
215
|
} else {
|
mbed714 |
0:d616ece2d859
|
216
|
err_t ret;
|
mbed714 |
0:d616ece2d859
|
217
|
/* send an ICMP packet, src addr is the dest addr of the curren packet */
|
mbed714 |
0:d616ece2d859
|
218
|
ret = ip_output_if(p, ip_current_dest_addr(), IP_HDRINCL,
|
mbed714 |
0:d616ece2d859
|
219
|
ICMP_TTL, 0, IP_PROTO_ICMP, inp);
|
mbed714 |
0:d616ece2d859
|
220
|
if (ret != ERR_OK) {
|
mbed714 |
0:d616ece2d859
|
221
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
|
mbed714 |
0:d616ece2d859
|
222
|
}
|
mbed714 |
0:d616ece2d859
|
223
|
}
|
mbed714 |
0:d616ece2d859
|
224
|
break;
|
mbed714 |
0:d616ece2d859
|
225
|
default:
|
mbed714 |
0:d616ece2d859
|
226
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
|
mbed714 |
0:d616ece2d859
|
227
|
(s16_t)type, (s16_t)code));
|
mbed714 |
0:d616ece2d859
|
228
|
ICMP_STATS_INC(icmp.proterr);
|
mbed714 |
0:d616ece2d859
|
229
|
ICMP_STATS_INC(icmp.drop);
|
mbed714 |
0:d616ece2d859
|
230
|
}
|
mbed714 |
0:d616ece2d859
|
231
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
232
|
return;
|
mbed714 |
0:d616ece2d859
|
233
|
lenerr:
|
mbed714 |
0:d616ece2d859
|
234
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
235
|
ICMP_STATS_INC(icmp.lenerr);
|
mbed714 |
0:d616ece2d859
|
236
|
snmp_inc_icmpinerrors();
|
mbed714 |
0:d616ece2d859
|
237
|
return;
|
mbed714 |
0:d616ece2d859
|
238
|
#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
|
mbed714 |
0:d616ece2d859
|
239
|
memerr:
|
mbed714 |
0:d616ece2d859
|
240
|
pbuf_free(p);
|
mbed714 |
0:d616ece2d859
|
241
|
ICMP_STATS_INC(icmp.err);
|
mbed714 |
0:d616ece2d859
|
242
|
snmp_inc_icmpinerrors();
|
mbed714 |
0:d616ece2d859
|
243
|
return;
|
mbed714 |
0:d616ece2d859
|
244
|
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
|
mbed714 |
0:d616ece2d859
|
245
|
}
|
mbed714 |
0:d616ece2d859
|
246
|
|
mbed714 |
0:d616ece2d859
|
247
|
/**
|
mbed714 |
0:d616ece2d859
|
248
|
* Send an icmp 'destination unreachable' packet, called from ip_input() if
|
mbed714 |
0:d616ece2d859
|
249
|
* the transport layer protocol is unknown and from udp_input() if the local
|
mbed714 |
0:d616ece2d859
|
250
|
* port is not bound.
|
mbed714 |
0:d616ece2d859
|
251
|
*
|
mbed714 |
0:d616ece2d859
|
252
|
* @param p the input packet for which the 'unreachable' should be sent,
|
mbed714 |
0:d616ece2d859
|
253
|
* p->payload pointing to the IP header
|
mbed714 |
0:d616ece2d859
|
254
|
* @param t type of the 'unreachable' packet
|
mbed714 |
0:d616ece2d859
|
255
|
*/
|
mbed714 |
0:d616ece2d859
|
256
|
void
|
mbed714 |
0:d616ece2d859
|
257
|
icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
|
mbed714 |
0:d616ece2d859
|
258
|
{
|
mbed714 |
0:d616ece2d859
|
259
|
icmp_send_response(p, ICMP_DUR, t);
|
mbed714 |
0:d616ece2d859
|
260
|
}
|
mbed714 |
0:d616ece2d859
|
261
|
|
mbed714 |
0:d616ece2d859
|
262
|
#if IP_FORWARD || IP_REASSEMBLY
|
mbed714 |
0:d616ece2d859
|
263
|
/**
|
mbed714 |
0:d616ece2d859
|
264
|
* Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
|
mbed714 |
0:d616ece2d859
|
265
|
*
|
mbed714 |
0:d616ece2d859
|
266
|
* @param p the input packet for which the 'time exceeded' should be sent,
|
mbed714 |
0:d616ece2d859
|
267
|
* p->payload pointing to the IP header
|
mbed714 |
0:d616ece2d859
|
268
|
* @param t type of the 'time exceeded' packet
|
mbed714 |
0:d616ece2d859
|
269
|
*/
|
mbed714 |
0:d616ece2d859
|
270
|
void
|
mbed714 |
0:d616ece2d859
|
271
|
icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
|
mbed714 |
0:d616ece2d859
|
272
|
{
|
mbed714 |
0:d616ece2d859
|
273
|
icmp_send_response(p, ICMP_TE, t);
|
mbed714 |
0:d616ece2d859
|
274
|
}
|
mbed714 |
0:d616ece2d859
|
275
|
|
mbed714 |
0:d616ece2d859
|
276
|
#endif /* IP_FORWARD || IP_REASSEMBLY */
|
mbed714 |
0:d616ece2d859
|
277
|
|
mbed714 |
0:d616ece2d859
|
278
|
/**
|
mbed714 |
0:d616ece2d859
|
279
|
* Send an icmp packet in response to an incoming packet.
|
mbed714 |
0:d616ece2d859
|
280
|
*
|
mbed714 |
0:d616ece2d859
|
281
|
* @param p the input packet for which the 'unreachable' should be sent,
|
mbed714 |
0:d616ece2d859
|
282
|
* p->payload pointing to the IP header
|
mbed714 |
0:d616ece2d859
|
283
|
* @param type Type of the ICMP header
|
mbed714 |
0:d616ece2d859
|
284
|
* @param code Code of the ICMP header
|
mbed714 |
0:d616ece2d859
|
285
|
*/
|
mbed714 |
0:d616ece2d859
|
286
|
static void
|
mbed714 |
0:d616ece2d859
|
287
|
icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
|
mbed714 |
0:d616ece2d859
|
288
|
{
|
mbed714 |
0:d616ece2d859
|
289
|
struct pbuf *q;
|
mbed714 |
0:d616ece2d859
|
290
|
struct ip_hdr *iphdr;
|
mbed714 |
0:d616ece2d859
|
291
|
/* we can use the echo header here */
|
mbed714 |
0:d616ece2d859
|
292
|
struct icmp_echo_hdr *icmphdr;
|
mbed714 |
0:d616ece2d859
|
293
|
ip_addr_t iphdr_src;
|
mbed714 |
0:d616ece2d859
|
294
|
|
mbed714 |
0:d616ece2d859
|
295
|
/* ICMP header + IP header + 8 bytes of data */
|
mbed714 |
0:d616ece2d859
|
296
|
q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
|
mbed714 |
0:d616ece2d859
|
297
|
PBUF_RAM);
|
mbed714 |
0:d616ece2d859
|
298
|
if (q == NULL) {
|
mbed714 |
0:d616ece2d859
|
299
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
|
mbed714 |
0:d616ece2d859
|
300
|
return;
|
mbed714 |
0:d616ece2d859
|
301
|
}
|
mbed714 |
0:d616ece2d859
|
302
|
LWIP_ASSERT("check that first pbuf can hold icmp message",
|
mbed714 |
0:d616ece2d859
|
303
|
(q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
|
mbed714 |
0:d616ece2d859
|
304
|
|
mbed714 |
0:d616ece2d859
|
305
|
iphdr = (struct ip_hdr *)p->payload;
|
mbed714 |
0:d616ece2d859
|
306
|
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
|
mbed714 |
0:d616ece2d859
|
307
|
ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
|
mbed714 |
0:d616ece2d859
|
308
|
LWIP_DEBUGF(ICMP_DEBUG, (" to "));
|
mbed714 |
0:d616ece2d859
|
309
|
ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
|
mbed714 |
0:d616ece2d859
|
310
|
LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
|
mbed714 |
0:d616ece2d859
|
311
|
|
mbed714 |
0:d616ece2d859
|
312
|
icmphdr = (struct icmp_echo_hdr *)q->payload;
|
mbed714 |
0:d616ece2d859
|
313
|
icmphdr->type = type;
|
mbed714 |
0:d616ece2d859
|
314
|
icmphdr->code = code;
|
mbed714 |
0:d616ece2d859
|
315
|
icmphdr->id = 0;
|
mbed714 |
0:d616ece2d859
|
316
|
icmphdr->seqno = 0;
|
mbed714 |
0:d616ece2d859
|
317
|
|
mbed714 |
0:d616ece2d859
|
318
|
/* copy fields from original packet */
|
mbed714 |
0:d616ece2d859
|
319
|
SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
|
mbed714 |
0:d616ece2d859
|
320
|
IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
|
mbed714 |
0:d616ece2d859
|
321
|
|
mbed714 |
0:d616ece2d859
|
322
|
/* calculate checksum */
|
mbed714 |
0:d616ece2d859
|
323
|
icmphdr->chksum = 0;
|
mbed714 |
0:d616ece2d859
|
324
|
icmphdr->chksum = inet_chksum(icmphdr, q->len);
|
mbed714 |
0:d616ece2d859
|
325
|
ICMP_STATS_INC(icmp.xmit);
|
mbed714 |
0:d616ece2d859
|
326
|
/* increase number of messages attempted to send */
|
mbed714 |
0:d616ece2d859
|
327
|
snmp_inc_icmpoutmsgs();
|
mbed714 |
0:d616ece2d859
|
328
|
/* increase number of destination unreachable messages attempted to send */
|
mbed714 |
0:d616ece2d859
|
329
|
snmp_inc_icmpouttimeexcds();
|
mbed714 |
0:d616ece2d859
|
330
|
ip_addr_copy(iphdr_src, iphdr->src);
|
mbed714 |
0:d616ece2d859
|
331
|
ip_output(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP);
|
mbed714 |
0:d616ece2d859
|
332
|
pbuf_free(q);
|
mbed714 |
0:d616ece2d859
|
333
|
}
|
mbed714 |
0:d616ece2d859
|
334
|
|
mbed714 |
0:d616ece2d859
|
335
|
#endif /* LWIP_ICMP */
|