1 |
606 |
jeremybenn |
/**
|
2 |
|
|
* @file
|
3 |
|
|
* This is the IPv4 layer implementation for incoming and outgoing IP traffic.
|
4 |
|
|
*
|
5 |
|
|
* @see ip_frag.c
|
6 |
|
|
*
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/*
|
10 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
11 |
|
|
* All rights reserved.
|
12 |
|
|
*
|
13 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
14 |
|
|
* are permitted provided that the following conditions are met:
|
15 |
|
|
*
|
16 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
* this list of conditions and the following disclaimer.
|
18 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
19 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
20 |
|
|
* and/or other materials provided with the distribution.
|
21 |
|
|
* 3. The name of the author may not be used to endorse or promote products
|
22 |
|
|
* derived from this software without specific prior written permission.
|
23 |
|
|
*
|
24 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
25 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
26 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
27 |
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
29 |
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
31 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32 |
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
33 |
|
|
* OF SUCH DAMAGE.
|
34 |
|
|
*
|
35 |
|
|
* This file is part of the lwIP TCP/IP stack.
|
36 |
|
|
*
|
37 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
38 |
|
|
*
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
|
|
#include "lwip/opt.h"
|
42 |
|
|
#include "lwip/ip.h"
|
43 |
|
|
#include "lwip/def.h"
|
44 |
|
|
#include "lwip/mem.h"
|
45 |
|
|
#include "lwip/ip_frag.h"
|
46 |
|
|
#include "lwip/inet.h"
|
47 |
|
|
#include "lwip/inet_chksum.h"
|
48 |
|
|
#include "lwip/netif.h"
|
49 |
|
|
#include "lwip/icmp.h"
|
50 |
|
|
#include "lwip/igmp.h"
|
51 |
|
|
#include "lwip/raw.h"
|
52 |
|
|
#include "lwip/udp.h"
|
53 |
|
|
#include "lwip/tcp.h"
|
54 |
|
|
#include "lwip/snmp.h"
|
55 |
|
|
#include "lwip/dhcp.h"
|
56 |
|
|
#include "lwip/stats.h"
|
57 |
|
|
#include "arch/perf.h"
|
58 |
|
|
|
59 |
|
|
#include <string.h>
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* The interface that provided the packet for the current callback
|
63 |
|
|
* invocation.
|
64 |
|
|
*/
|
65 |
|
|
struct netif *current_netif;
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Header of the input packet currently being processed.
|
69 |
|
|
*/
|
70 |
|
|
const struct ip_hdr *current_header;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* Finds the appropriate network interface for a given IP address. It
|
74 |
|
|
* searches the list of network interfaces linearly. A match is found
|
75 |
|
|
* if the masked IP address of the network interface equals the masked
|
76 |
|
|
* IP address given to the function.
|
77 |
|
|
*
|
78 |
|
|
* @param dest the destination IP address for which to find the route
|
79 |
|
|
* @return the netif on which to send to reach dest
|
80 |
|
|
*/
|
81 |
|
|
struct netif *
|
82 |
|
|
ip_route(struct ip_addr *dest)
|
83 |
|
|
{
|
84 |
|
|
struct netif *netif;
|
85 |
|
|
|
86 |
|
|
/* iterate through netifs */
|
87 |
|
|
for(netif = netif_list; netif != NULL; netif = netif->next) {
|
88 |
|
|
/* network mask matches? */
|
89 |
|
|
if (netif_is_up(netif)) {
|
90 |
|
|
if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
|
91 |
|
|
/* return netif on which to forward IP packet */
|
92 |
|
|
return netif;
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
|
97 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));
|
98 |
|
|
IP_STATS_INC(ip.rterr);
|
99 |
|
|
snmp_inc_ipoutnoroutes();
|
100 |
|
|
return NULL;
|
101 |
|
|
}
|
102 |
|
|
/* no matching netif found, use default netif */
|
103 |
|
|
return netif_default;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
#if IP_FORWARD
|
107 |
|
|
/**
|
108 |
|
|
* Forwards an IP packet. It finds an appropriate route for the
|
109 |
|
|
* packet, decrements the TTL value of the packet, adjusts the
|
110 |
|
|
* checksum and outputs the packet on the appropriate interface.
|
111 |
|
|
*
|
112 |
|
|
* @param p the packet to forward (p->payload points to IP header)
|
113 |
|
|
* @param iphdr the IP header of the input packet
|
114 |
|
|
* @param inp the netif on which this packet was received
|
115 |
|
|
* @return the netif on which the packet was sent (NULL if it wasn't sent)
|
116 |
|
|
*/
|
117 |
|
|
static struct netif *
|
118 |
|
|
ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
|
119 |
|
|
{
|
120 |
|
|
struct netif *netif;
|
121 |
|
|
|
122 |
|
|
PERF_START;
|
123 |
|
|
/* Find network interface where to forward this IP packet to. */
|
124 |
|
|
netif = ip_route((struct ip_addr *)&(iphdr->dest));
|
125 |
|
|
if (netif == NULL) {
|
126 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
|
127 |
|
|
iphdr->dest.addr));
|
128 |
|
|
snmp_inc_ipoutnoroutes();
|
129 |
|
|
return (struct netif *)NULL;
|
130 |
|
|
}
|
131 |
|
|
/* Do not forward packets onto the same network interface on which
|
132 |
|
|
* they arrived. */
|
133 |
|
|
if (netif == inp) {
|
134 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
|
135 |
|
|
snmp_inc_ipoutnoroutes();
|
136 |
|
|
return (struct netif *)NULL;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/* decrement TTL */
|
140 |
|
|
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
|
141 |
|
|
/* send ICMP if TTL == 0 */
|
142 |
|
|
if (IPH_TTL(iphdr) == 0) {
|
143 |
|
|
snmp_inc_ipinhdrerrors();
|
144 |
|
|
#if LWIP_ICMP
|
145 |
|
|
/* Don't send ICMP messages in response to ICMP messages */
|
146 |
|
|
if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
|
147 |
|
|
icmp_time_exceeded(p, ICMP_TE_TTL);
|
148 |
|
|
}
|
149 |
|
|
#endif /* LWIP_ICMP */
|
150 |
|
|
return (struct netif *)NULL;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
/* Incrementally update the IP checksum. */
|
154 |
|
|
if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
|
155 |
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
|
156 |
|
|
} else {
|
157 |
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
|
161 |
|
|
iphdr->dest.addr));
|
162 |
|
|
|
163 |
|
|
IP_STATS_INC(ip.fw);
|
164 |
|
|
IP_STATS_INC(ip.xmit);
|
165 |
|
|
snmp_inc_ipforwdatagrams();
|
166 |
|
|
|
167 |
|
|
PERF_STOP("ip_forward");
|
168 |
|
|
/* transmit pbuf on chosen interface */
|
169 |
|
|
netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
|
170 |
|
|
return netif;
|
171 |
|
|
}
|
172 |
|
|
#endif /* IP_FORWARD */
|
173 |
|
|
|
174 |
|
|
/**
|
175 |
|
|
* This function is called by the network interface device driver when
|
176 |
|
|
* an IP packet is received. The function does the basic checks of the
|
177 |
|
|
* IP header such as packet size being at least larger than the header
|
178 |
|
|
* size etc. If the packet was not destined for us, the packet is
|
179 |
|
|
* forwarded (using ip_forward). The IP checksum is always checked.
|
180 |
|
|
*
|
181 |
|
|
* Finally, the packet is sent to the upper layer protocol input function.
|
182 |
|
|
*
|
183 |
|
|
* @param p the received IP packet (p->payload points to IP header)
|
184 |
|
|
* @param inp the netif on which this packet was received
|
185 |
|
|
* @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
|
186 |
|
|
* processed, but currently always returns ERR_OK)
|
187 |
|
|
*/
|
188 |
|
|
err_t
|
189 |
|
|
ip_input(struct pbuf *p, struct netif *inp)
|
190 |
|
|
{
|
191 |
|
|
struct ip_hdr *iphdr;
|
192 |
|
|
struct netif *netif;
|
193 |
|
|
u16_t iphdr_hlen;
|
194 |
|
|
u16_t iphdr_len;
|
195 |
|
|
#if LWIP_DHCP
|
196 |
|
|
int check_ip_src=1;
|
197 |
|
|
#endif /* LWIP_DHCP */
|
198 |
|
|
|
199 |
|
|
IP_STATS_INC(ip.recv);
|
200 |
|
|
snmp_inc_ipinreceives();
|
201 |
|
|
|
202 |
|
|
/* identify the IP header */
|
203 |
|
|
iphdr = p->payload;
|
204 |
|
|
if (IPH_V(iphdr) != 4) {
|
205 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
|
206 |
|
|
ip_debug_print(p);
|
207 |
|
|
pbuf_free(p);
|
208 |
|
|
IP_STATS_INC(ip.err);
|
209 |
|
|
IP_STATS_INC(ip.drop);
|
210 |
|
|
snmp_inc_ipinhdrerrors();
|
211 |
|
|
return ERR_OK;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
/* obtain IP header length in number of 32-bit words */
|
215 |
|
|
iphdr_hlen = IPH_HL(iphdr);
|
216 |
|
|
/* calculate IP header length in bytes */
|
217 |
|
|
iphdr_hlen *= 4;
|
218 |
|
|
/* obtain ip length in bytes */
|
219 |
|
|
iphdr_len = ntohs(IPH_LEN(iphdr));
|
220 |
|
|
|
221 |
|
|
/* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
|
222 |
|
|
if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
|
223 |
|
|
if (iphdr_hlen > p->len) {
|
224 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
225 |
|
|
("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
|
226 |
|
|
iphdr_hlen, p->len));
|
227 |
|
|
}
|
228 |
|
|
if (iphdr_len > p->tot_len) {
|
229 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
230 |
|
|
("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
|
231 |
|
|
iphdr_len, p->tot_len));
|
232 |
|
|
}
|
233 |
|
|
/* free (drop) packet pbufs */
|
234 |
|
|
pbuf_free(p);
|
235 |
|
|
IP_STATS_INC(ip.lenerr);
|
236 |
|
|
IP_STATS_INC(ip.drop);
|
237 |
|
|
snmp_inc_ipindiscards();
|
238 |
|
|
return ERR_OK;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/* verify checksum */
|
242 |
|
|
#if CHECKSUM_CHECK_IP
|
243 |
|
|
if (inet_chksum(iphdr, iphdr_hlen) != 0) {
|
244 |
|
|
|
245 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
246 |
|
|
("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
|
247 |
|
|
ip_debug_print(p);
|
248 |
|
|
pbuf_free(p);
|
249 |
|
|
IP_STATS_INC(ip.chkerr);
|
250 |
|
|
IP_STATS_INC(ip.drop);
|
251 |
|
|
snmp_inc_ipinhdrerrors();
|
252 |
|
|
return ERR_OK;
|
253 |
|
|
}
|
254 |
|
|
#endif
|
255 |
|
|
|
256 |
|
|
/* Trim pbuf. This should have been done at the netif layer,
|
257 |
|
|
* but we'll do it anyway just to be sure that its done. */
|
258 |
|
|
pbuf_realloc(p, iphdr_len);
|
259 |
|
|
|
260 |
|
|
/* match packet against an interface, i.e. is this packet for us? */
|
261 |
|
|
#if LWIP_IGMP
|
262 |
|
|
if (ip_addr_ismulticast(&(iphdr->dest))) {
|
263 |
|
|
if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {
|
264 |
|
|
netif = inp;
|
265 |
|
|
} else {
|
266 |
|
|
netif = NULL;
|
267 |
|
|
}
|
268 |
|
|
} else
|
269 |
|
|
#endif /* LWIP_IGMP */
|
270 |
|
|
{
|
271 |
|
|
/* start trying with inp. if that's not acceptable, start walking the
|
272 |
|
|
list of configured netifs.
|
273 |
|
|
'first' is used as a boolean to mark whether we started walking the list */
|
274 |
|
|
int first = 1;
|
275 |
|
|
netif = inp;
|
276 |
|
|
do {
|
277 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n",
|
278 |
|
|
iphdr->dest.addr, netif->ip_addr.addr,
|
279 |
|
|
iphdr->dest.addr & netif->netmask.addr,
|
280 |
|
|
netif->ip_addr.addr & netif->netmask.addr,
|
281 |
|
|
iphdr->dest.addr & ~(netif->netmask.addr)));
|
282 |
|
|
|
283 |
|
|
/* interface is up and configured? */
|
284 |
|
|
if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
|
285 |
|
|
/* unicast to this interface address? */
|
286 |
|
|
if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
|
287 |
|
|
/* or broadcast on this interface network address? */
|
288 |
|
|
ip_addr_isbroadcast(&(iphdr->dest), netif)) {
|
289 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
|
290 |
|
|
netif->name[0], netif->name[1]));
|
291 |
|
|
/* break out of for loop */
|
292 |
|
|
break;
|
293 |
|
|
}
|
294 |
|
|
}
|
295 |
|
|
if (first) {
|
296 |
|
|
first = 0;
|
297 |
|
|
netif = netif_list;
|
298 |
|
|
} else {
|
299 |
|
|
netif = netif->next;
|
300 |
|
|
}
|
301 |
|
|
if (netif == inp) {
|
302 |
|
|
netif = netif->next;
|
303 |
|
|
}
|
304 |
|
|
} while(netif != NULL);
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
#if LWIP_DHCP
|
308 |
|
|
/* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
|
309 |
|
|
* using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
|
310 |
|
|
* According to RFC 1542 section 3.1.1, referred by RFC 2131).
|
311 |
|
|
*/
|
312 |
|
|
if (netif == NULL) {
|
313 |
|
|
/* remote port is DHCP server? */
|
314 |
|
|
if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
|
315 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
|
316 |
|
|
ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
|
317 |
|
|
if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {
|
318 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
|
319 |
|
|
netif = inp;
|
320 |
|
|
check_ip_src = 0;
|
321 |
|
|
}
|
322 |
|
|
}
|
323 |
|
|
}
|
324 |
|
|
#endif /* LWIP_DHCP */
|
325 |
|
|
|
326 |
|
|
/* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
|
327 |
|
|
#if LWIP_DHCP
|
328 |
|
|
/* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
|
329 |
|
|
if (check_ip_src && (iphdr->src.addr != 0))
|
330 |
|
|
#endif /* LWIP_DHCP */
|
331 |
|
|
{ if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
|
332 |
|
|
(ip_addr_ismulticast(&(iphdr->src)))) {
|
333 |
|
|
/* packet source is not valid */
|
334 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
|
335 |
|
|
/* free (drop) packet pbufs */
|
336 |
|
|
pbuf_free(p);
|
337 |
|
|
IP_STATS_INC(ip.drop);
|
338 |
|
|
snmp_inc_ipinaddrerrors();
|
339 |
|
|
snmp_inc_ipindiscards();
|
340 |
|
|
return ERR_OK;
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
/* packet not for us? */
|
345 |
|
|
if (netif == NULL) {
|
346 |
|
|
/* packet not for us, route or discard */
|
347 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
|
348 |
|
|
#if IP_FORWARD
|
349 |
|
|
/* non-broadcast packet? */
|
350 |
|
|
if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
|
351 |
|
|
/* try to forward IP packet on (other) interfaces */
|
352 |
|
|
ip_forward(p, iphdr, inp);
|
353 |
|
|
} else
|
354 |
|
|
#endif /* IP_FORWARD */
|
355 |
|
|
{
|
356 |
|
|
snmp_inc_ipinaddrerrors();
|
357 |
|
|
snmp_inc_ipindiscards();
|
358 |
|
|
}
|
359 |
|
|
pbuf_free(p);
|
360 |
|
|
return ERR_OK;
|
361 |
|
|
}
|
362 |
|
|
/* packet consists of multiple fragments? */
|
363 |
|
|
if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
|
364 |
|
|
#if IP_REASSEMBLY /* packet fragment reassembly code present? */
|
365 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip_reass()\n",
|
366 |
|
|
ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
|
367 |
|
|
/* reassemble the packet*/
|
368 |
|
|
p = ip_reass(p);
|
369 |
|
|
/* packet not fully reassembled yet? */
|
370 |
|
|
if (p == NULL) {
|
371 |
|
|
return ERR_OK;
|
372 |
|
|
}
|
373 |
|
|
iphdr = p->payload;
|
374 |
|
|
#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
|
375 |
|
|
pbuf_free(p);
|
376 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
|
377 |
|
|
ntohs(IPH_OFFSET(iphdr))));
|
378 |
|
|
IP_STATS_INC(ip.opterr);
|
379 |
|
|
IP_STATS_INC(ip.drop);
|
380 |
|
|
/* unsupported protocol feature */
|
381 |
|
|
snmp_inc_ipinunknownprotos();
|
382 |
|
|
return ERR_OK;
|
383 |
|
|
#endif /* IP_REASSEMBLY */
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
|
387 |
|
|
|
388 |
|
|
#if LWIP_IGMP
|
389 |
|
|
/* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
|
390 |
|
|
if((iphdr_hlen > IP_HLEN && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
|
391 |
|
|
#else
|
392 |
|
|
if (iphdr_hlen > IP_HLEN) {
|
393 |
|
|
#endif /* LWIP_IGMP */
|
394 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
|
395 |
|
|
pbuf_free(p);
|
396 |
|
|
IP_STATS_INC(ip.opterr);
|
397 |
|
|
IP_STATS_INC(ip.drop);
|
398 |
|
|
/* unsupported protocol feature */
|
399 |
|
|
snmp_inc_ipinunknownprotos();
|
400 |
|
|
return ERR_OK;
|
401 |
|
|
}
|
402 |
|
|
#endif /* IP_OPTIONS_ALLOWED == 0 */
|
403 |
|
|
|
404 |
|
|
/* send to upper layers */
|
405 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
|
406 |
|
|
ip_debug_print(p);
|
407 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
|
408 |
|
|
|
409 |
|
|
current_netif = inp;
|
410 |
|
|
current_header = iphdr;
|
411 |
|
|
|
412 |
|
|
#if LWIP_RAW
|
413 |
|
|
/* raw input did not eat the packet? */
|
414 |
|
|
if (raw_input(p, inp) == 0)
|
415 |
|
|
#endif /* LWIP_RAW */
|
416 |
|
|
{
|
417 |
|
|
|
418 |
|
|
switch (IPH_PROTO(iphdr)) {
|
419 |
|
|
#if LWIP_UDP
|
420 |
|
|
case IP_PROTO_UDP:
|
421 |
|
|
#if LWIP_UDPLITE
|
422 |
|
|
case IP_PROTO_UDPLITE:
|
423 |
|
|
#endif /* LWIP_UDPLITE */
|
424 |
|
|
snmp_inc_ipindelivers();
|
425 |
|
|
udp_input(p, inp);
|
426 |
|
|
break;
|
427 |
|
|
#endif /* LWIP_UDP */
|
428 |
|
|
#if LWIP_TCP
|
429 |
|
|
case IP_PROTO_TCP:
|
430 |
|
|
snmp_inc_ipindelivers();
|
431 |
|
|
tcp_input(p, inp);
|
432 |
|
|
break;
|
433 |
|
|
#endif /* LWIP_TCP */
|
434 |
|
|
#if LWIP_ICMP
|
435 |
|
|
case IP_PROTO_ICMP:
|
436 |
|
|
snmp_inc_ipindelivers();
|
437 |
|
|
icmp_input(p, inp);
|
438 |
|
|
break;
|
439 |
|
|
#endif /* LWIP_ICMP */
|
440 |
|
|
#if LWIP_IGMP
|
441 |
|
|
case IP_PROTO_IGMP:
|
442 |
|
|
igmp_input(p,inp,&(iphdr->dest));
|
443 |
|
|
break;
|
444 |
|
|
#endif /* LWIP_IGMP */
|
445 |
|
|
default:
|
446 |
|
|
#if LWIP_ICMP
|
447 |
|
|
/* send ICMP destination protocol unreachable unless is was a broadcast */
|
448 |
|
|
if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
|
449 |
|
|
!ip_addr_ismulticast(&(iphdr->dest))) {
|
450 |
|
|
p->payload = iphdr;
|
451 |
|
|
icmp_dest_unreach(p, ICMP_DUR_PROTO);
|
452 |
|
|
}
|
453 |
|
|
#endif /* LWIP_ICMP */
|
454 |
|
|
pbuf_free(p);
|
455 |
|
|
|
456 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
|
457 |
|
|
|
458 |
|
|
IP_STATS_INC(ip.proterr);
|
459 |
|
|
IP_STATS_INC(ip.drop);
|
460 |
|
|
snmp_inc_ipinunknownprotos();
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
|
464 |
|
|
current_netif = NULL;
|
465 |
|
|
current_header = NULL;
|
466 |
|
|
|
467 |
|
|
return ERR_OK;
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
/**
|
471 |
|
|
* Sends an IP packet on a network interface. This function constructs
|
472 |
|
|
* the IP header and calculates the IP header checksum. If the source
|
473 |
|
|
* IP address is NULL, the IP address of the outgoing network
|
474 |
|
|
* interface is filled in as source address.
|
475 |
|
|
* If the destination IP address is IP_HDRINCL, p is assumed to already
|
476 |
|
|
* include an IP header and p->payload points to it instead of the data.
|
477 |
|
|
*
|
478 |
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
479 |
|
|
protocol header; if dest == IP_HDRINCL, p already includes an IP
|
480 |
|
|
header and p->payload points to that IP header)
|
481 |
|
|
* @param src the source IP address to send from (if src == IP_ADDR_ANY, the
|
482 |
|
|
* IP address of the netif used to send is used as source address)
|
483 |
|
|
* @param dest the destination IP address to send the packet to
|
484 |
|
|
* @param ttl the TTL value to be set in the IP header
|
485 |
|
|
* @param tos the TOS value to be set in the IP header
|
486 |
|
|
* @param proto the PROTOCOL to be set in the IP header
|
487 |
|
|
* @param netif the netif on which to send this packet
|
488 |
|
|
* @return ERR_OK if the packet was sent OK
|
489 |
|
|
* ERR_BUF if p doesn't have enough space for IP/LINK headers
|
490 |
|
|
* returns errors returned by netif->output
|
491 |
|
|
*
|
492 |
|
|
* @note ip_id: RFC791 "some host may be able to simply use
|
493 |
|
|
* unique identifiers independent of destination"
|
494 |
|
|
*/
|
495 |
|
|
err_t
|
496 |
|
|
ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
497 |
|
|
u8_t ttl, u8_t tos,
|
498 |
|
|
u8_t proto, struct netif *netif)
|
499 |
|
|
{
|
500 |
|
|
#if IP_OPTIONS_SEND
|
501 |
|
|
return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
/**
|
505 |
|
|
* Same as ip_output_if() but with the possibility to include IP options:
|
506 |
|
|
*
|
507 |
|
|
* @ param ip_options pointer to the IP options, copied into the IP header
|
508 |
|
|
* @ param optlen length of ip_options
|
509 |
|
|
*/
|
510 |
|
|
err_t ip_output_if_opt(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
511 |
|
|
u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
|
512 |
|
|
u16_t optlen)
|
513 |
|
|
{
|
514 |
|
|
#endif /* IP_OPTIONS_SEND */
|
515 |
|
|
struct ip_hdr *iphdr;
|
516 |
|
|
static u16_t ip_id = 0;
|
517 |
|
|
|
518 |
|
|
snmp_inc_ipoutrequests();
|
519 |
|
|
|
520 |
|
|
/* Should the IP header be generated or is it already included in p? */
|
521 |
|
|
if (dest != IP_HDRINCL) {
|
522 |
|
|
u16_t ip_hlen = IP_HLEN;
|
523 |
|
|
#if IP_OPTIONS_SEND
|
524 |
|
|
u16_t optlen_aligned = 0;
|
525 |
|
|
if (optlen != 0) {
|
526 |
|
|
/* round up to a multiple of 4 */
|
527 |
|
|
optlen_aligned = ((optlen + 3) & ~3);
|
528 |
|
|
ip_hlen += optlen_aligned;
|
529 |
|
|
/* First write in the IP options */
|
530 |
|
|
if (pbuf_header(p, optlen_aligned)) {
|
531 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
|
532 |
|
|
IP_STATS_INC(ip.err);
|
533 |
|
|
snmp_inc_ipoutdiscards();
|
534 |
|
|
return ERR_BUF;
|
535 |
|
|
}
|
536 |
|
|
MEMCPY(p->payload, ip_options, optlen);
|
537 |
|
|
if (optlen < optlen_aligned) {
|
538 |
|
|
/* zero the remaining bytes */
|
539 |
|
|
memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
|
540 |
|
|
}
|
541 |
|
|
}
|
542 |
|
|
#endif /* IP_OPTIONS_SEND */
|
543 |
|
|
/* generate IP header */
|
544 |
|
|
if (pbuf_header(p, IP_HLEN)) {
|
545 |
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
|
546 |
|
|
|
547 |
|
|
IP_STATS_INC(ip.err);
|
548 |
|
|
snmp_inc_ipoutdiscards();
|
549 |
|
|
return ERR_BUF;
|
550 |
|
|
}
|
551 |
|
|
|
552 |
|
|
iphdr = p->payload;
|
553 |
|
|
LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
|
554 |
|
|
(p->len >= sizeof(struct ip_hdr)));
|
555 |
|
|
|
556 |
|
|
IPH_TTL_SET(iphdr, ttl);
|
557 |
|
|
IPH_PROTO_SET(iphdr, proto);
|
558 |
|
|
|
559 |
|
|
ip_addr_set(&(iphdr->dest), dest);
|
560 |
|
|
|
561 |
|
|
IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
|
562 |
|
|
IPH_LEN_SET(iphdr, htons(p->tot_len));
|
563 |
|
|
IPH_OFFSET_SET(iphdr, 0);
|
564 |
|
|
IPH_ID_SET(iphdr, htons(ip_id));
|
565 |
|
|
++ip_id;
|
566 |
|
|
|
567 |
|
|
if (ip_addr_isany(src)) {
|
568 |
|
|
ip_addr_set(&(iphdr->src), &(netif->ip_addr));
|
569 |
|
|
} else {
|
570 |
|
|
ip_addr_set(&(iphdr->src), src);
|
571 |
|
|
}
|
572 |
|
|
|
573 |
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
574 |
|
|
#if CHECKSUM_GEN_IP
|
575 |
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
|
576 |
|
|
#endif
|
577 |
|
|
} else {
|
578 |
|
|
/* IP header already included in p */
|
579 |
|
|
iphdr = p->payload;
|
580 |
|
|
dest = &(iphdr->dest);
|
581 |
|
|
}
|
582 |
|
|
|
583 |
|
|
IP_STATS_INC(ip.xmit);
|
584 |
|
|
|
585 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
|
586 |
|
|
ip_debug_print(p);
|
587 |
|
|
|
588 |
|
|
#if ENABLE_LOOPBACK
|
589 |
|
|
if (ip_addr_cmp(dest, &netif->ip_addr)) {
|
590 |
|
|
/* Packet to self, enqueue it for loopback */
|
591 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
|
592 |
|
|
return netif_loop_output(netif, p, dest);
|
593 |
|
|
}
|
594 |
|
|
#endif /* ENABLE_LOOPBACK */
|
595 |
|
|
#if IP_FRAG
|
596 |
|
|
/* don't fragment if interface has mtu set to 0 [loopif] */
|
597 |
|
|
if (netif->mtu && (p->tot_len > netif->mtu)) {
|
598 |
|
|
return ip_frag(p,netif,dest);
|
599 |
|
|
}
|
600 |
|
|
#endif
|
601 |
|
|
|
602 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
|
603 |
|
|
return netif->output(netif, p, dest);
|
604 |
|
|
}
|
605 |
|
|
|
606 |
|
|
/**
|
607 |
|
|
* Simple interface to ip_output_if. It finds the outgoing network
|
608 |
|
|
* interface and calls upon ip_output_if to do the actual work.
|
609 |
|
|
*
|
610 |
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
611 |
|
|
protocol header; if dest == IP_HDRINCL, p already includes an IP
|
612 |
|
|
header and p->payload points to that IP header)
|
613 |
|
|
* @param src the source IP address to send from (if src == IP_ADDR_ANY, the
|
614 |
|
|
* IP address of the netif used to send is used as source address)
|
615 |
|
|
* @param dest the destination IP address to send the packet to
|
616 |
|
|
* @param ttl the TTL value to be set in the IP header
|
617 |
|
|
* @param tos the TOS value to be set in the IP header
|
618 |
|
|
* @param proto the PROTOCOL to be set in the IP header
|
619 |
|
|
*
|
620 |
|
|
* @return ERR_RTE if no route is found
|
621 |
|
|
* see ip_output_if() for more return values
|
622 |
|
|
*/
|
623 |
|
|
err_t
|
624 |
|
|
ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
625 |
|
|
u8_t ttl, u8_t tos, u8_t proto)
|
626 |
|
|
{
|
627 |
|
|
struct netif *netif;
|
628 |
|
|
|
629 |
|
|
if ((netif = ip_route(dest)) == NULL) {
|
630 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
|
631 |
|
|
IP_STATS_INC(ip.rterr);
|
632 |
|
|
return ERR_RTE;
|
633 |
|
|
}
|
634 |
|
|
|
635 |
|
|
return ip_output_if(p, src, dest, ttl, tos, proto, netif);
|
636 |
|
|
}
|
637 |
|
|
|
638 |
|
|
#if LWIP_NETIF_HWADDRHINT
|
639 |
|
|
/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
|
640 |
|
|
* before calling ip_output_if.
|
641 |
|
|
*
|
642 |
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
643 |
|
|
protocol header; if dest == IP_HDRINCL, p already includes an IP
|
644 |
|
|
header and p->payload points to that IP header)
|
645 |
|
|
* @param src the source IP address to send from (if src == IP_ADDR_ANY, the
|
646 |
|
|
* IP address of the netif used to send is used as source address)
|
647 |
|
|
* @param dest the destination IP address to send the packet to
|
648 |
|
|
* @param ttl the TTL value to be set in the IP header
|
649 |
|
|
* @param tos the TOS value to be set in the IP header
|
650 |
|
|
* @param proto the PROTOCOL to be set in the IP header
|
651 |
|
|
* @param addr_hint address hint pointer set to netif->addr_hint before
|
652 |
|
|
* calling ip_output_if()
|
653 |
|
|
*
|
654 |
|
|
* @return ERR_RTE if no route is found
|
655 |
|
|
* see ip_output_if() for more return values
|
656 |
|
|
*/
|
657 |
|
|
err_t
|
658 |
|
|
ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
659 |
|
|
u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
|
660 |
|
|
{
|
661 |
|
|
struct netif *netif;
|
662 |
|
|
err_t err;
|
663 |
|
|
|
664 |
|
|
if ((netif = ip_route(dest)) == NULL) {
|
665 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
|
666 |
|
|
IP_STATS_INC(ip.rterr);
|
667 |
|
|
return ERR_RTE;
|
668 |
|
|
}
|
669 |
|
|
|
670 |
|
|
netif->addr_hint = addr_hint;
|
671 |
|
|
err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
|
672 |
|
|
netif->addr_hint = NULL;
|
673 |
|
|
|
674 |
|
|
return err;
|
675 |
|
|
}
|
676 |
|
|
#endif /* LWIP_NETIF_HWADDRHINT*/
|
677 |
|
|
|
678 |
|
|
#if IP_DEBUG
|
679 |
|
|
/* Print an IP header by using LWIP_DEBUGF
|
680 |
|
|
* @param p an IP packet, p->payload pointing to the IP header
|
681 |
|
|
*/
|
682 |
|
|
void
|
683 |
|
|
ip_debug_print(struct pbuf *p)
|
684 |
|
|
{
|
685 |
|
|
struct ip_hdr *iphdr = p->payload;
|
686 |
|
|
u8_t *payload;
|
687 |
|
|
|
688 |
|
|
payload = (u8_t *)iphdr + IP_HLEN;
|
689 |
|
|
|
690 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
|
691 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
692 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
|
693 |
|
|
IPH_V(iphdr),
|
694 |
|
|
IPH_HL(iphdr),
|
695 |
|
|
IPH_TOS(iphdr),
|
696 |
|
|
ntohs(IPH_LEN(iphdr))));
|
697 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
698 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
|
699 |
|
|
ntohs(IPH_ID(iphdr)),
|
700 |
|
|
ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
|
701 |
|
|
ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
|
702 |
|
|
ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
|
703 |
|
|
ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
|
704 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
705 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
|
706 |
|
|
IPH_TTL(iphdr),
|
707 |
|
|
IPH_PROTO(iphdr),
|
708 |
|
|
ntohs(IPH_CHKSUM(iphdr))));
|
709 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
710 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
|
711 |
|
|
ip4_addr1(&iphdr->src),
|
712 |
|
|
ip4_addr2(&iphdr->src),
|
713 |
|
|
ip4_addr3(&iphdr->src),
|
714 |
|
|
ip4_addr4(&iphdr->src)));
|
715 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
716 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
|
717 |
|
|
ip4_addr1(&iphdr->dest),
|
718 |
|
|
ip4_addr2(&iphdr->dest),
|
719 |
|
|
ip4_addr3(&iphdr->dest),
|
720 |
|
|
ip4_addr4(&iphdr->dest)));
|
721 |
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
722 |
|
|
}
|
723 |
|
|
#endif /* IP_DEBUG */
|