1 /**
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
43 #if LWIP_IPV4
44
45 #include "lwip/ip.h"
46 #include "lwip/def.h"
47 #include "lwip/mem.h"
48 #include "lwip/ip4_frag.h"
49 #include "lwip/inet_chksum.h"
50 #include "lwip/netif.h"
51 #include "lwip/icmp.h"
52 #include "lwip/igmp.h"
53 #include "lwip/priv/raw_priv.h"
54 #include "lwip/udp.h"
55 #include "lwip/priv/tcp_priv.h"
56 #include "lwip/autoip.h"
57 #include "lwip/stats.h"
58 #include "lwip/prot/iana.h"
59 #ifdef LWIP_HOOK_FILE_STATIC_ROUTE
60 #include LWIP_HOOK_FILE_STATIC_ROUTE
61 #endif
62
63 #include <string.h>
64
65 #if LWIP_NAT64
66 #include "lwip/nat64.h"
67 #endif
68 #if LWIP_IP6IN4
69 #include "lwip/ip6in4.h"
70 #endif /* LWIP_IP6IN4 */
71
72 #ifdef LWIP_HOOK_FILENAME
73 #include LWIP_HOOK_FILENAME
74 #endif
75
76 #if LWIP_MPL_IPV4
77 #include "mcast6.h"
78 #ifndef LWIP_MPL_IPV4_IN
79 #define LWIP_MPL_IPV4_IN mcast6_ipv4_in
80 #endif /* LWIP_MPL_IPV4_IN */
81 #endif /* LWIP_MPL_IPV4 */
82
83 /** Set this to 0 in the rare case of wanting to call an extra function to
84 * generate the IP checksum (in contrast to calculating it on-the-fly). */
85 #ifndef LWIP_INLINE_IP_CHKSUM
86 #if LWIP_CHECKSUM_CTRL_PER_NETIF
87 #define LWIP_INLINE_IP_CHKSUM 0
88 #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
89 #define LWIP_INLINE_IP_CHKSUM 1
90 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
91 #endif
92
93 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
94 #define CHECKSUM_GEN_IP_INLINE 1
95 #else
96 #define CHECKSUM_GEN_IP_INLINE 0
97 #endif
98
99 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
100 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
101
102 /** Some defines for DHCP to let link-layer-addressed packets through while the
103 * netif is down.
104 * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port)
105 * to return 1 if the port is accepted and 0 if the port is not accepted.
106 */
107 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
108 /* accept DHCP client port and custom port */
109 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \
110 || (LWIP_IP_ACCEPT_UDP_PORT(port)))
111 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
112 /* accept custom port only */
113 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
114 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
115 /* accept DHCP client port only */
116 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT))
117 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
118
119 #else /* LWIP_DHCP */
120 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
121 #endif /* LWIP_DHCP */
122
123 /** Retuning ip identification number */
ip_init(void)124 void ip_init(void)
125 {
126 ip4_set_ip_id(0);
127 }
128
129 #if LWIP_IP_FILTER
130 /* This function is called when an ip packet received. The return value of this function will
131 * decide accept or drop a ip packet when LWIP_IP_FILTER is Enabled and ip_filter function has been set */
132 static ip_filter_fn ip4_filter = NULL;
133 #endif /* LWIP_IP_FILTER */
134
135 /**
136 * Retuning ip identification number
137 *
138 */
get_ipid(void)139 u16_t get_ipid(void)
140 {
141 return (u16_t)(ip_data.ip_id - 1);
142 }
143
144 #if LWIP_MULTICAST_TX_OPTIONS
145 /** The default netif used for multicast */
146 static struct netif *ip4_default_multicast_netif;
147
148 /**
149 * @ingroup ip4
150 * Set a default netif for IPv4 multicast. */
151 void
ip4_set_default_multicast_netif(struct netif * default_multicast_netif)152 ip4_set_default_multicast_netif(struct netif *default_multicast_netif)
153 {
154 ip4_default_multicast_netif = default_multicast_netif;
155 }
156 #endif /* LWIP_MULTICAST_TX_OPTIONS */
157
158 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
159 /**
160 * Source based IPv4 routing must be fully implemented in
161 * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters.
162 */
163 struct netif *
ip4_route_src(const ip4_addr_t * src,const ip4_addr_t * dest)164 ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest)
165 {
166 if (src != NULL) {
167 /* when src==NULL, the hook is called from ip4_route(dest) */
168 struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(src, dest);
169 if (netif != NULL) {
170 return netif;
171 }
172 }
173 return ip4_route(dest);
174 }
175 #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
176
177 /**
178 * Finds the appropriate network interface for a given IP address. It
179 * searches the list of network interfaces linearly. A match is found
180 * if the masked IP address of the network interface equals the masked
181 * IP address given to the function.
182 *
183 * @param dest the destination IP address for which to find the route
184 * @return the netif on which to send to reach dest
185 */
186 struct netif *
ip4_route(const ip4_addr_t * dest)187 ip4_route(const ip4_addr_t *dest)
188 {
189 #if !LWIP_SINGLE_NETIF
190 struct netif *netif;
191
192 LWIP_ASSERT_CORE_LOCKED();
193
194 #if LWIP_MULTICAST_TX_OPTIONS
195 /* Use administratively selected interface for multicast by default */
196 if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
197 NETIF_SET_SCOPE(ip4_default_multicast_netif, RT_SCOPE_LINK);
198 return ip4_default_multicast_netif;
199 }
200 #endif /* LWIP_MULTICAST_TX_OPTIONS */
201
202 #ifdef LWIP_HOOK_IP4_ROUTE_TUNNEL
203 netif = LWIP_HOOK_IP4_ROUTE_TUNNEL(dest);
204 if (netif != NULL) {
205 return netif;
206 }
207 #endif
208
209 /* bug #54569: in case LWIP_SINGLE_NETIF=1 and LWIP_DEBUGF() disabled, the following loop is optimized away */
210 LWIP_UNUSED_ARG(dest);
211
212 /* iterate through netifs */
213 NETIF_FOREACH(netif) {
214 /* is the netif up, does it have a link and a valid address? */
215 if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif)) &&
216 !ip4_addr_isany_val(*netif_ip4_netmask(netif))) {
217 /* network mask matches? */
218 if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {
219 /* return netif on which to forward IP packet */
220 NETIF_SET_SCOPE(netif, RT_SCOPE_LINK);
221 return netif;
222 }
223 /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
224 if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) {
225 /* return netif on which to forward IP packet */
226 NETIF_SET_SCOPE(netif, RT_SCOPE_LINK);
227 return netif;
228 }
229 }
230 }
231
232 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
233 /* loopif is disabled, looopback traffic is passed through any netif */
234 if (ip4_addr_isloopback(dest)) {
235 /* don't check for link on loopback traffic */
236 if (netif_default != NULL && netif_is_up(netif_default)) {
237 return netif_default;
238 }
239 /* default netif is not up, just use any netif for loopback traffic */
240 NETIF_FOREACH(netif) {
241 if (netif_is_up(netif)) {
242 NETIF_SET_SCOPE(netif, RT_SCOPE_HOST);
243 return netif;
244 }
245 }
246 return NULL;
247 }
248 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
249
250 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
251 netif = LWIP_HOOK_IP4_ROUTE_SRC(NULL, dest);
252 if (netif != NULL) {
253 NETIF_SET_SCOPE(netif, RT_SCOPE_UNIVERSAL);
254 return netif;
255 }
256 #elif defined(LWIP_HOOK_IP4_ROUTE)
257 netif = LWIP_HOOK_IP4_ROUTE(dest);
258 if (netif != NULL) {
259 NETIF_SET_SCOPE(netif, RT_SCOPE_UNIVERSAL);
260 return netif;
261 }
262 #endif
263 #endif /* !LWIP_SINGLE_NETIF */
264
265 if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||
266 ip4_addr_isany_val(*netif_ip4_addr(netif_default)) || ip4_addr_isloopback(dest)) {
267 /* No matching netif found and default netif is not usable.
268 If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */
269 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
270 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
271 IP_STATS_INC(ip.rterr);
272 MIB2_STATS_INC(mib2.ipoutnoroutes);
273 return NULL;
274 }
275
276 NETIF_SET_SCOPE(netif_default, RT_SCOPE_UNIVERSAL);
277 return netif_default;
278 }
279
280 #if IP_FORWARD
281 /**
282 * Determine whether an IP address is in a reserved set of addresses
283 * that may not be forwarded, or whether datagrams to that destination
284 * may be forwarded.
285 * @param p the packet to forward
286 * @return 1: can forward 0: discard
287 */
288 static int
ip4_canforward(struct pbuf * p)289 ip4_canforward(struct pbuf *p)
290 {
291 u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));
292
293 #ifdef LWIP_HOOK_IP4_CANFORWARD
294 int ret = LWIP_HOOK_IP4_CANFORWARD(p, addr);
295 if (ret >= 0) {
296 return ret;
297 }
298 #endif /* LWIP_HOOK_IP4_CANFORWARD */
299
300 if (p->flags & PBUF_FLAG_LLBCAST) {
301 /* don't route link-layer broadcasts */
302 return 0;
303 }
304 if ((p->flags & PBUF_FLAG_LLMCAST) || IP_MULTICAST(addr)) {
305 /* don't route link-layer multicasts (use LWIP_HOOK_IP4_CANFORWARD instead) */
306 return 0;
307 }
308 if (IP_EXPERIMENTAL(addr)) {
309 return 0;
310 }
311 if (IP_CLASSA(addr)) {
312 u32_t net = addr & IP_CLASSA_NET;
313 if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
314 /* don't route loopback packets */
315 return 0;
316 }
317 }
318 return 1;
319 }
320
321 /**
322 * Forwards an IP packet. It finds an appropriate route for the
323 * packet, decrements the TTL value of the packet, adjusts the
324 * checksum and outputs the packet on the appropriate interface.
325 *
326 * @param p the packet to forward (p->payload points to IP header)
327 * @param iphdr the IP header of the input packet
328 * @param inp the netif on which this packet was received
329 */
330 static void
ip4_forward(struct pbuf * p,struct ip_hdr * iphdr,struct netif * inp)331 ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
332 {
333 struct netif *netif;
334
335 PERF_START;
336 LWIP_UNUSED_ARG(inp);
337
338 if (!ip4_canforward(p)) {
339 goto return_noroute;
340 }
341
342 /* RFC3927 2.7: do not forward link-local addresses */
343 if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
344 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
345 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
346 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
347 goto return_noroute;
348 }
349
350 /* Find network interface where to forward this IP packet to. */
351 netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr());
352 if (netif == NULL) {
353 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
354 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
355 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
356 /* @todo: send ICMP_DUR_NET? */
357 goto return_noroute;
358 }
359 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
360 /* Do not forward packets onto the same network interface on which
361 * they arrived. */
362 if (netif == inp) {
363 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n"));
364 goto return_noroute;
365 }
366 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
367
368 /* decrement TTL */
369 if (IPH_TTL(iphdr)) {
370 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
371 }
372
373 /* send ICMP if TTL == 0 */
374 if (IPH_TTL(iphdr) == 0) {
375 MIB2_STATS_INC(mib2.ipinhdrerrors);
376 #if LWIP_ICMP
377 /* Don't send ICMP messages in response to ICMP error messages */
378 if (IPH_PROTO(iphdr) == IP_PROTO_ICMP) {
379 (void)pbuf_header(p, -IP_HLEN);
380 if ((((u8_t*)p->payload)[0] == ICMP_ER) || (((u8_t*)p->payload)[0] == ICMP_ECHO) ||
381 (((u8_t*)p->payload)[0] == ICMP_TS) || (((u8_t*)p->payload)[0] == ICMP_TSR)) {
382 (void)pbuf_header(p, IP_HLEN);
383 icmp_time_exceeded(p, ICMP_TE_TTL);
384 } else {
385 (void)pbuf_header(p, IP_HLEN);
386 }
387 } else {
388 icmp_time_exceeded(p, ICMP_TE_TTL);
389 }
390
391 #endif /* LWIP_ICMP */
392 return;
393 }
394
395 /* Incrementally update the IP checksum. */
396 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
397 IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1));
398 } else {
399 IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100)));
400 }
401
402 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
403 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
404 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
405
406 IP_STATS_INC(ip.fw);
407 MIB2_STATS_INC(mib2.ipforwdatagrams);
408 IP_STATS_INC(ip.xmit);
409
410 PERF_STOP("ip4_forward");
411 /* don't fragment if interface has mtu set to 0 [loopif] */
412 if (netif->mtu && (p->tot_len > netif->mtu)) {
413 if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
414 #if IP_FRAG
415 ip4_frag(p, netif, ip4_current_dest_addr());
416 #else /* IP_FRAG */
417 /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
418 #endif /* IP_FRAG */
419 } else {
420 #if LWIP_ICMP
421 /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
422 icmp_dest_unreach(p, ICMP_DUR_FRAG);
423 #endif /* LWIP_ICMP */
424 }
425 return;
426 }
427 /* transmit pbuf on chosen interface */
428 netif->output(netif, p, ip4_current_dest_addr());
429 return;
430 return_noroute:
431 MIB2_STATS_INC(mib2.ipoutnoroutes);
432 }
433 #endif /* IP_FORWARD */
434
435 /** Return true if the current input packet should be accepted on this netif */
436 static int
ip4_input_accept(struct netif * netif)437 ip4_input_accept(struct netif *netif)
438 {
439 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",
440 ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
441 ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
442 ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
443 ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
444
445 /* interface is up and configured? */
446 if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
447 /* unicast to this interface address? */
448 if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
449 /* or broadcast on this interface network address? */
450 ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
451 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
452 || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
453 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
454 ) {
455 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %s\n",
456 netif->name));
457 /* accept on this netif */
458 return 1;
459 }
460 #if LWIP_AUTOIP
461 /* connections to link-local addresses must persist after changing
462 the netif's address (RFC3927 ch. 1.9) */
463 if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
464 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %s\n",
465 netif->name));
466 /* accept on this netif */
467 return 1;
468 }
469 #endif /* LWIP_AUTOIP */
470 }
471 return 0;
472 }
473
474 /**
475 * This function is called by the network interface device driver when
476 * an IP packet is received. The function does the basic checks of the
477 * IP header such as packet size being at least larger than the header
478 * size etc. If the packet was not destined for us, the packet is
479 * forwarded (using ip_forward). The IP checksum is always checked.
480 *
481 * Finally, the packet is sent to the upper layer protocol input function.
482 *
483 * @param p the received IP packet (p->payload points to IP header)
484 * @param inp the netif on which this packet was received
485 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
486 * processed, but currently always returns ERR_OK)
487 */
488 err_t
ip4_input(struct pbuf * p,struct netif * inp)489 ip4_input(struct pbuf *p, struct netif *inp)
490 {
491 struct ip_hdr *iphdr;
492 struct netif *netif;
493 u16_t iphdr_hlen;
494 u16_t iphdr_len;
495 #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
496 int check_ip_src = 1;
497 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
498 #if LWIP_RAW
499 raw_input_state_t raw_status;
500 #endif /* LWIP_RAW */
501
502 LWIP_ASSERT_CORE_LOCKED();
503
504 IP_STATS_INC(ip.recv);
505 MIB2_STATS_INC(mib2.ipinreceives);
506
507 #if LWIP_IP_FILTER
508 if (ip4_filter != NULL && ip4_filter(p, inp) != ERR_OK) {
509 (void)pbuf_free(p);
510 IP_STATS_INC(ip.drop);
511 return ERR_OK;
512 }
513 #endif /* LWIP_IP_FILTER */
514 if (p->tot_len < IP_HLEN) {
515 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP header length should be minimum of 20 bytes\n"));
516 ip4_debug_print(p);
517 pbuf_free(p);
518 IP_STATS_INC(ip.err);
519 IP_STATS_INC(ip.drop);
520 MIB2_STATS_INC(mib2.ipinhdrerrors);
521 return ERR_OK;
522 }
523
524 /* identify the IP header */
525 iphdr = (struct ip_hdr *)p->payload;
526 if (IPH_V(iphdr) != 4) {
527 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
528 ip4_debug_print(p);
529 pbuf_free(p);
530 IP_STATS_INC(ip.err);
531 IP_STATS_INC(ip.drop);
532 MIB2_STATS_INC(mib2.ipinhdrerrors);
533 return ERR_OK;
534 }
535
536 #ifdef LWIP_HOOK_IP4_INPUT
537 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
538 /* the packet has been eaten */
539 return ERR_OK;
540 }
541 #endif
542
543 /* obtain IP header length in bytes */
544 iphdr_hlen = IPH_HL_BYTES(iphdr);
545 /* obtain ip length in bytes */
546 iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
547
548 /* Trim pbuf. This is especially required for packets < 60 bytes. */
549 if (iphdr_len < p->tot_len) {
550 pbuf_realloc(p, iphdr_len);
551 }
552
553 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
554 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
555 if (iphdr_hlen < IP_HLEN) {
556 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
557 ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
558 }
559 if (iphdr_hlen > p->len) {
560 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
561 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
562 iphdr_hlen, p->len));
563 }
564 if (iphdr_len > p->tot_len) {
565 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
566 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
567 iphdr_len, p->tot_len));
568 }
569 /* free (drop) packet pbufs */
570 pbuf_free(p);
571 IP_STATS_INC(ip.lenerr);
572 IP_STATS_INC(ip.drop);
573 MIB2_STATS_INC(mib2.ipindiscards);
574 return ERR_OK;
575 }
576
577 /* verify checksum */
578 #if CHECKSUM_CHECK_IP
579 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
580 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
581
582 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
583 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
584 ip4_debug_print(p);
585 pbuf_free(p);
586 IP_STATS_INC(ip.chkerr);
587 IP_STATS_INC(ip.drop);
588 MIB2_STATS_INC(mib2.ipinhdrerrors);
589 return ERR_OK;
590 }
591 }
592 #endif
593
594 #ifdef LWIP_HOOK_IP4_INPUT_AFTER_CHKSUM
595 if (LWIP_HOOK_IP4_INPUT_AFTER_CHKSUM(p, inp, &iphdr, &iphdr_hlen)) {
596 /* the packet has been eaten */
597 return ERR_OK;
598 }
599 #endif
600
601 /* copy IP addresses to aligned ip_addr_t */
602 ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
603 ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
604
605 /* match packet against an interface, i.e. is this packet for us? */
606 if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
607 #if LWIP_IGMP
608 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
609 /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
610 ip4_addr_t allsystems;
611 IP4_ADDR(&allsystems, 224, 0, 0, 1);
612 if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
613 ip4_addr_isany_val(*ip4_current_src_addr())) {
614 check_ip_src = 0;
615 }
616 netif = inp;
617 } else {
618 netif = NULL;
619 }
620 #else /* LWIP_IGMP */
621 if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
622 netif = inp;
623 } else {
624 netif = NULL;
625 }
626 #endif /* LWIP_IGMP */
627 } else {
628 /* start trying with inp. if that's not acceptable, start walking the
629 list of configured netifs. */
630 if (ip4_input_accept(inp)) {
631 netif = inp;
632 } else {
633 netif = NULL;
634 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
635 /* Packets sent to the loopback address must not be accepted on an
636 * interface that does not have the loopback address assigned to it,
637 * unless a non-loopback interface is used for loopback traffic. */
638 if (!ip4_addr_isloopback(ip4_current_dest_addr()))
639 #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
640 {
641 #if !LWIP_SINGLE_NETIF
642 NETIF_FOREACH(netif) {
643 if (netif == inp) {
644 /* we checked that before already */
645 continue;
646 }
647 if (ip4_input_accept(netif)) {
648 break;
649 }
650 }
651 #endif /* !LWIP_SINGLE_NETIF */
652 }
653 }
654 }
655
656 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
657 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
658 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
659 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
660 *
661 * If you want to accept private broadcast communication while a netif is down,
662 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
663 *
664 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
665 */
666 if (netif == NULL) {
667 /* remote port is DHCP server? */
668 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
669 const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);
670 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
671 lwip_ntohs(udphdr->dest)));
672 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
673 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
674 netif = inp;
675 check_ip_src = 0;
676 }
677 }
678 }
679 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
680
681 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
682 #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
683 if (check_ip_src
684 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
685 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
686 && !ip4_addr_isany_val(*ip4_current_src_addr())
687 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
688 )
689 #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
690 {
691 if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
692 (ip4_addr_ismulticast(ip4_current_src_addr()))) {
693 /* packet source is not valid */
694 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
695 /* free (drop) packet pbufs */
696 pbuf_free(p);
697 IP_STATS_INC(ip.drop);
698 MIB2_STATS_INC(mib2.ipinaddrerrors);
699 MIB2_STATS_INC(mib2.ipindiscards);
700 return ERR_OK;
701 }
702 }
703
704 /* packet not for us? */
705 if (netif == NULL) {
706 /* packet not for us, route or discard */
707 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
708 #if LWIP_NAT64
709 if (nat64_ip4_input(p, iphdr, inp)) {
710 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("nat64_ip4_input: packet handle.\n"));
711 return ERR_OK;
712 }
713 #endif /* LWIP_NAT64 */
714 #if IP_FORWARD
715 /* non-broadcast packet? */
716 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
717 /* try to forward IP packet on (other) interfaces */
718 ip4_forward(p, (struct ip_hdr *)p->payload, inp);
719 } else
720 #endif /* IP_FORWARD */
721 {
722 IP_STATS_INC(ip.drop);
723 MIB2_STATS_INC(mib2.ipinaddrerrors);
724 MIB2_STATS_INC(mib2.ipindiscards);
725 }
726 pbuf_free(p);
727 return ERR_OK;
728 }
729 /* packet consists of multiple fragments? */
730 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
731 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
732 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 ip4_reass()\n",
733 lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8)));
734 /* reassemble the packet*/
735 p = ip4_reass(p);
736 /* packet not fully reassembled yet? */
737 if (p == NULL) {
738 return ERR_OK;
739 }
740 iphdr = (struct ip_hdr *)p->payload;
741 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
742 pbuf_free(p);
743 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
744 lwip_ntohs(IPH_OFFSET(iphdr))));
745 IP_STATS_INC(ip.opterr);
746 IP_STATS_INC(ip.drop);
747 /* unsupported protocol feature */
748 MIB2_STATS_INC(mib2.ipinunknownprotos);
749 return ERR_OK;
750 #endif /* IP_REASSEMBLY */
751 }
752
753 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
754
755 #if LWIP_IGMP
756 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
757 if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP))
758 #else
759 if (iphdr_hlen > IP_HLEN)
760 #endif /* LWIP_IGMP */
761 {
762 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
763 pbuf_free(p);
764 IP_STATS_INC(ip.opterr);
765 IP_STATS_INC(ip.drop);
766 /* unsupported protocol feature */
767 MIB2_STATS_INC(mib2.ipinunknownprotos);
768 return ERR_OK;
769 }
770 #endif /* IP_OPTIONS_ALLOWED == 0 */
771
772 /* send to upper layers */
773 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
774 ip4_debug_print(p);
775 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
776
777 ip_data.current_netif = netif;
778 ip_data.current_input_netif = inp;
779 ip_data.current_ip4_header = iphdr;
780 ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);
781 #if IP_STATS
782 /* Successfully received bytes */
783 STATS_INC_NUM(ip.ip_rx_bytes, p->tot_len);
784 #endif
785
786 #if LWIP_MPL_IPV4
787 LWIP_MPL_IPV4_IN(p, inp);
788 #endif /* LWIP_MPL_IPV4 */
789
790 #if LWIP_RAW
791 /* raw input did not eat the packet? */
792 raw_status = raw_input(p, inp);
793 #endif /* LWIP_RAW */
794 pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */
795
796 switch (IPH_PROTO(iphdr)) {
797 #if LWIP_UDP
798 case IP_PROTO_UDP:
799 #if LWIP_UDPLITE
800 case IP_PROTO_UDPLITE:
801 #endif /* LWIP_UDPLITE */
802 MIB2_STATS_INC(mib2.ipindelivers);
803 udp_input(p, inp);
804 break;
805 #endif /* LWIP_UDP */
806 #if LWIP_TCP
807 case IP_PROTO_TCP:
808 MIB2_STATS_INC(mib2.ipindelivers);
809 tcp_input(p, inp);
810 break;
811 #endif /* LWIP_TCP */
812 #if LWIP_ICMP
813 case IP_PROTO_ICMP:
814 MIB2_STATS_INC(mib2.ipindelivers);
815 icmp_input(p, inp);
816 break;
817 #endif /* LWIP_ICMP */
818 #if LWIP_IGMP
819 case IP_PROTO_IGMP:
820 igmp_input(p, inp, ip4_current_dest_addr());
821 break;
822 #endif /* LWIP_IGMP */
823 #if LWIP_IP6IN4
824 case IP_PROTO_IP6IN4:
825 p->ip6in4_ip4 = lwIP_TRUE;
826 ip6in4_ip4_input(p, inp, ip4_current_src_addr(), ip4_current_dest_addr());
827 break;
828 #endif /* LWIP_IP6IN4 */
829 default:
830 #if LWIP_RAW
831 if (raw_status != RAW_INPUT_NONE) {
832 MIB2_STATS_INC(mib2.ipindelivers);
833 } else
834 #endif /* LWIP_RAW */
835 {
836 #if LWIP_ICMP
837 /* send ICMP destination protocol unreachable unless is was a broadcast */
838 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
839 #if LWIP_RAW
840 !raw_status &&
841 #endif
842 !ip4_addr_ismulticast(ip4_current_dest_addr())) {
843 pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */
844 icmp_dest_unreach(p, ICMP_DUR_PROTO);
845 }
846 #endif /* LWIP_ICMP */
847
848 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
849
850 IP_STATS_INC(ip.proterr);
851 IP_STATS_INC(ip.drop);
852 MIB2_STATS_INC(mib2.ipinunknownprotos);
853 }
854 pbuf_free(p);
855 break;
856 }
857
858 /* @todo: this is not really necessary... */
859 ip_data.current_netif = NULL;
860 ip_data.current_input_netif = NULL;
861 ip_data.current_ip4_header = NULL;
862 ip_data.current_ip_header_tot_len = 0;
863 ip4_addr_set_any(ip4_current_src_addr());
864 ip4_addr_set_any(ip4_current_dest_addr());
865
866 return ERR_OK;
867 }
868
869 /**
870 * Sends an IP packet on a network interface. This function constructs
871 * the IP header and calculates the IP header checksum. If the source
872 * IP address is NULL, the IP address of the outgoing network
873 * interface is filled in as source address.
874 * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
875 * include an IP header and p->payload points to it instead of the data.
876 *
877 * @param p the packet to send (p->payload points to the data, e.g. next
878 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
879 IP header and p->payload points to that IP header)
880 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
881 * IP address of the netif used to send is used as source address)
882 * @param dest the destination IP address to send the packet to
883 * @param ttl the TTL value to be set in the IP header
884 * @param tos the TOS value to be set in the IP header
885 * @param proto the PROTOCOL to be set in the IP header
886 * @param netif the netif on which to send this packet
887 * @return ERR_OK if the packet was sent OK
888 * ERR_BUF if p doesn't have enough space for IP/LINK headers
889 * returns errors returned by netif->output
890 *
891 * @note ip_id: RFC791 "some host may be able to simply use
892 * unique identifiers independent of destination"
893 */
894 err_t
ip4_output_if(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto,struct netif * netif)895 ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
896 u8_t ttl, u8_t tos,
897 u8_t proto, struct netif *netif)
898 {
899 #if IP_OPTIONS_SEND
900 return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
901 }
902
903 /**
904 * Same as ip_output_if() but with the possibility to include IP options:
905 *
906 * @ param ip_options pointer to the IP options, copied into the IP header
907 * @ param optlen length of ip_options
908 */
909 err_t
ip4_output_if_opt(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto,struct netif * netif,void * ip_options,u16_t optlen)910 ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
911 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
912 u16_t optlen)
913 {
914 #endif /* IP_OPTIONS_SEND */
915 const ip4_addr_t *src_used = src;
916 if (dest != LWIP_IP_HDRINCL) {
917 if (ip4_addr_isany(src)) {
918 src_used = netif_ip4_addr(netif);
919 }
920 }
921
922 #if IP_OPTIONS_SEND
923 return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
924 ip_options, optlen);
925 #else /* IP_OPTIONS_SEND */
926 return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
927 #endif /* IP_OPTIONS_SEND */
928 }
929
930 #if LWIP_NAT64
931 err_t
ip4_iphdr_chksum(struct ip_hdr * iphdr,struct netif * netif,u16_t * chksum)932 ip4_iphdr_chksum(struct ip_hdr *iphdr, struct netif *netif, u16_t *chksum)
933 {
934 #if CHECKSUM_GEN_IP_INLINE
935 u32_t chk_sum = 0;
936 #else
937 #if CHECKSUM_GEN_IP
938 u16_t ip_hlen = IP_HLEN;
939 #endif
940 #endif /* CHECKSUM_GEN_IP_INLINE */
941
942 LWIP_UNUSED_ARG(netif);
943 if (chksum == NULL) {
944 return ERR_ARG;
945 }
946 #if CHECKSUM_GEN_IP_INLINE
947 chk_sum += (u32_t)PP_NTOHS(IPH_PROTO(iphdr) | ((IPH_TTL(iphdr)) << 8));
948 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
949 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
950 chk_sum += (u32_t)PP_NTOHS(IPH_TOS(iphdr) | (iphdr->_v_hl << 8));
951 chk_sum += iphdr->_len;
952 chk_sum += iphdr->_id;
953 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
954 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
955 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
956 chk_sum = (chk_sum >> 16) + chk_sum;
957 chk_sum = ~chk_sum;
958 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
959 *chksum = (u16_t)chk_sum;
960 }
961 #if LWIP_CHECKSUM_CTRL_PER_NETIF
962 else {
963 *chksum = 0;
964 }
965 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
966 #else /* CHECKSUM_GEN_IP_INLINE */
967 *chksum = 0;
968 #if CHECKSUM_GEN_IP
969 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
970 *chksum = inet_chksum(iphdr, ip_hlen);
971 }
972 #endif /* CHECKSUM_GEN_IP */
973 #endif /* CHECKSUM_GEN_IP_INLINE */
974 return ERR_OK;
975 }
976 #endif /* LWIP_NAT64 */
977
978 /**
979 * Same as ip_output_if() but 'src' address is not replaced by netif address
980 * when it is 'any'.
981 */
982 err_t
ip4_output_if_src(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto,struct netif * netif)983 ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
984 u8_t ttl, u8_t tos,
985 u8_t proto, struct netif *netif)
986 {
987 #if IP_OPTIONS_SEND
988 return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
989 }
990
991 /**
992 * Same as ip_output_if_opt() but 'src' address is not replaced by netif address
993 * when it is 'any'.
994 */
995 err_t
ip4_output_if_opt_src(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto,struct netif * netif,void * ip_options,u16_t optlen)996 ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
997 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
998 u16_t optlen)
999 {
1000 #endif /* IP_OPTIONS_SEND */
1001 struct ip_hdr *iphdr;
1002 ip4_addr_t dest_addr;
1003 #if CHECKSUM_GEN_IP_INLINE
1004 u32_t chk_sum = 0;
1005 #endif /* CHECKSUM_GEN_IP_INLINE */
1006
1007 LWIP_ASSERT_CORE_LOCKED();
1008 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1009
1010 MIB2_STATS_INC(mib2.ipoutrequests);
1011
1012 /* Should the IP header be generated or is it already included in p? */
1013 if (dest != LWIP_IP_HDRINCL) {
1014 u16_t ip_hlen = IP_HLEN;
1015 #if IP_OPTIONS_SEND
1016 u16_t optlen_aligned = 0;
1017 if (optlen != 0) {
1018 #if CHECKSUM_GEN_IP_INLINE
1019 u16_t i;
1020 #endif /* CHECKSUM_GEN_IP_INLINE */
1021 if (optlen > (IP_HLEN_MAX - IP_HLEN)) {
1022 /* optlen too long */
1023 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));
1024 IP_STATS_INC(ip.err);
1025 MIB2_STATS_INC(mib2.ipoutdiscards);
1026 return ERR_VAL;
1027 }
1028 #if LWIP_RIPPLE
1029 if (proto == IP_PROTO_ICMP) {
1030 /* check ICMP type is neither Echo request nor Echo reply */
1031 if ((*((u8_t *)p->payload) != ICMP_ECHO) && (*((u8_t *)p->payload) != ICMP_ER)) {
1032 p->flags |= PBUF_FLAG_CTRL_PKT;
1033 #if LWIP_SO_PRIORITY
1034 p->priority = LWIP_PKT_PRIORITY_CTRL;
1035 #endif /* LWIP_SO_PRIORITY */
1036 }
1037 }
1038 #endif /* LWIP_RIPPLE */
1039 /* round up to a multiple of 4 */
1040 optlen_aligned = (u16_t)((optlen + 3) & ~3);
1041 ip_hlen = (u16_t)(ip_hlen + optlen_aligned);
1042 /* First write in the IP options */
1043 if (pbuf_add_header(p, optlen_aligned)) {
1044 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
1045 IP_STATS_INC(ip.err);
1046 MIB2_STATS_INC(mib2.ipoutdiscards);
1047 return ERR_BUF;
1048 }
1049 MEMCPY(p->payload, ip_options, optlen);
1050 if (optlen < optlen_aligned) {
1051 /* zero the remaining bytes */
1052 memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));
1053 }
1054 #if CHECKSUM_GEN_IP_INLINE
1055 for (i = 0; i < optlen_aligned / 2; i++) {
1056 chk_sum += ((u16_t *)p->payload)[i];
1057 }
1058 #endif /* CHECKSUM_GEN_IP_INLINE */
1059 }
1060 #endif /* IP_OPTIONS_SEND */
1061 /* generate IP header */
1062 if (pbuf_add_header(p, IP_HLEN)) {
1063 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
1064
1065 IP_STATS_INC(ip.err);
1066 MIB2_STATS_INC(mib2.ipoutdiscards);
1067 return ERR_BUF;
1068 }
1069
1070 iphdr = (struct ip_hdr *)p->payload;
1071 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
1072 (p->len >= sizeof(struct ip_hdr)));
1073
1074 IPH_TTL_SET(iphdr, ttl);
1075 IPH_PROTO_SET(iphdr, proto);
1076 #if CHECKSUM_GEN_IP_INLINE
1077 chk_sum += PP_NTOHS(proto | (ttl << 8));
1078 #endif /* CHECKSUM_GEN_IP_INLINE */
1079
1080 /* dest cannot be NULL here */
1081 ip4_addr_copy(iphdr->dest, *dest);
1082 #if CHECKSUM_GEN_IP_INLINE
1083 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
1084 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
1085 #endif /* CHECKSUM_GEN_IP_INLINE */
1086
1087 IPH_VHL_SET(iphdr, 4, ip_hlen >> 2);
1088 IPH_TOS_SET(iphdr, tos);
1089 #if CHECKSUM_GEN_IP_INLINE
1090 chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
1091 #endif /* CHECKSUM_GEN_IP_INLINE */
1092 IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
1093 #if CHECKSUM_GEN_IP_INLINE
1094 chk_sum += iphdr->_len;
1095 #endif /* CHECKSUM_GEN_IP_INLINE */
1096 IPH_OFFSET_SET(iphdr, 0);
1097 IPH_ID_SET(iphdr, lwip_htons(ip4_get_ip_id()));
1098 #if CHECKSUM_GEN_IP_INLINE
1099 chk_sum += iphdr->_id;
1100 #endif /* CHECKSUM_GEN_IP_INLINE */
1101 ip4_inc_ip_id();
1102
1103 if (src == NULL) {
1104 ip4_addr_copy(iphdr->src, *netif_ip4_addr(netif));
1105 } else {
1106 /* src cannot be NULL here */
1107 ip4_addr_copy(iphdr->src, *src);
1108 }
1109
1110 #ifdef LWIP_HOOK_IP4_OUTPUT_SRC_DECORATOR
1111 LWIP_HOOK_IP4_OUTPUT_SRC_DECORATOR(p, proto, iphdr);
1112 #endif
1113
1114 #if CHECKSUM_GEN_IP_INLINE
1115 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
1116 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
1117 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
1118 chk_sum = (chk_sum >> 16) + chk_sum;
1119 chk_sum = ~chk_sum;
1120 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
1121 iphdr->_chksum = (u16_t)chk_sum; /* network order */
1122 }
1123 #if LWIP_CHECKSUM_CTRL_PER_NETIF
1124 else {
1125 IPH_CHKSUM_SET(iphdr, 0);
1126 }
1127 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
1128 #else /* CHECKSUM_GEN_IP_INLINE */
1129 IPH_CHKSUM_SET(iphdr, 0);
1130 #if CHECKSUM_GEN_IP
1131 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
1132 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
1133 }
1134 #endif /* CHECKSUM_GEN_IP */
1135 #endif /* CHECKSUM_GEN_IP_INLINE */
1136 } else {
1137 u16_t iphdr_hlen = 0;
1138
1139 /* Invalid Read on send buffer */
1140 if (p->len < IP_HLEN) {
1141 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));
1142 IP_STATS_INC(ip.err);
1143 MIB2_STATS_INC(mib2.ipoutdiscards);
1144 return ERR_BUF;
1145 }
1146 iphdr = (struct ip_hdr *)p->payload;
1147 /* obtain IP header length in bytes */
1148 iphdr_hlen = (u16_t)IPH_HL_BYTES(iphdr);
1149 if (p->tot_len < iphdr_hlen) {
1150 LWIP_DEBUGF(RAW_DEBUG, ("ip4_output: Packet length less than IP packet header length\n"));
1151 return ERR_VAL;
1152 }
1153
1154 /* IP Header fields modified on sending by IP_HDRINCL */
1155 /* Filled in when zero */
1156 if (iphdr->src.addr == 0) {
1157 iphdr->src.addr = ip4_addr_get_u32(src);
1158 }
1159
1160 /* Filled in when zero */
1161 if (IPH_ID(iphdr) == 0) {
1162 IPH_ID_SET(iphdr, ip4_get_ip_id());
1163 ip4_inc_ip_id();
1164 }
1165
1166 /* Length & Checksum are always set */
1167 IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
1168
1169 IPH_CHKSUM_SET(iphdr, 0);
1170 #if CHECKSUM_GEN_IP
1171 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, iphdr_hlen));
1172 #endif
1173 ip4_addr_copy(dest_addr, iphdr->dest);
1174 dest = &dest_addr;
1175 }
1176
1177 #ifdef LWIP_HOOK_IP4_OUTPUT_FORCE_REROUTE
1178 err_t err = LWIP_HOOK_IP4_OUTPUT_FORCE_REROUTE(p, iphdr->src.addr, dest, ttl, tos, proto, netif);
1179 if (err != ERR_CLSD) {
1180 /* the packet has been sent */
1181 return err;
1182 }
1183 #endif
1184
1185 IP_STATS_INC(ip.xmit);
1186 #if IP_STATS
1187 /* Successfully transmitted bytes */
1188 STATS_INC_NUM(ip.ip_tx_bytes, p->tot_len);
1189 #endif
1190
1191 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %s%"U16_F"\n", netif->name, (u16_t)netif->num));
1192 ip4_debug_print(p);
1193
1194 #if ENABLE_LOOPBACK
1195 if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
1196 #if !LWIP_HAVE_LOOPIF
1197 || ip4_addr_isloopback(dest)
1198 #endif /* !LWIP_HAVE_LOOPIF */
1199 ) {
1200 /* Packet to self, enqueue it for loopback */
1201 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
1202 return netif_loop_output(netif, p);
1203 }
1204 #if LWIP_MULTICAST_TX_OPTIONS
1205 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
1206 netif_loop_output(netif, p);
1207 }
1208 #endif /* LWIP_MULTICAST_TX_OPTIONS */
1209 #endif /* ENABLE_LOOPBACK */
1210 #if IP_FRAG
1211 /* don't fragment if interface has mtu set to 0 [loopif] */
1212 if (netif->mtu && (p->tot_len > netif->mtu)) {
1213 return ip4_frag(p, netif, dest);
1214 }
1215 #endif /* IP_FRAG */
1216
1217 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
1218 return netif->output(netif, p, dest);
1219 }
1220
1221 /**
1222 * Simple interface to ip_output_if. It finds the outgoing network
1223 * interface and calls upon ip_output_if to do the actual work.
1224 *
1225 * @param p the packet to send (p->payload points to the data, e.g. next
1226 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1227 IP header and p->payload points to that IP header)
1228 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1229 * IP address of the netif used to send is used as source address)
1230 * @param dest the destination IP address to send the packet to
1231 * @param ttl the TTL value to be set in the IP header
1232 * @param tos the TOS value to be set in the IP header
1233 * @param proto the PROTOCOL to be set in the IP header
1234 *
1235 * @return ERR_RTE if no route is found
1236 * see ip_output_if() for more return values
1237 */
1238 err_t
ip4_output(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto)1239 ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1240 u8_t ttl, u8_t tos, u8_t proto)
1241 {
1242 struct netif *netif;
1243
1244 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1245
1246 if ((netif = ip4_route_src(src, dest)) == NULL) {
1247 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1248 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1249 IP_STATS_INC(ip.rterr);
1250 return ERR_RTE;
1251 }
1252
1253 return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1254 }
1255
1256 #if LWIP_NETIF_USE_HINTS
1257 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
1258 * before calling ip_output_if.
1259 *
1260 * @param p the packet to send (p->payload points to the data, e.g. next
1261 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1262 IP header and p->payload points to that IP header)
1263 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1264 * IP address of the netif used to send is used as source address)
1265 * @param dest the destination IP address to send the packet to
1266 * @param ttl the TTL value to be set in the IP header
1267 * @param tos the TOS value to be set in the IP header
1268 * @param proto the PROTOCOL to be set in the IP header
1269 * @param netif_hint netif output hint pointer set to netif->hint before
1270 * calling ip_output_if()
1271 *
1272 * @return ERR_RTE if no route is found
1273 * see ip_output_if() for more return values
1274 */
1275 err_t
ip4_output_hinted(struct pbuf * p,const ip4_addr_t * src,const ip4_addr_t * dest,u8_t ttl,u8_t tos,u8_t proto,struct netif_hint * netif_hint)1276 ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1277 u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
1278 {
1279 struct netif *netif;
1280 err_t err;
1281
1282 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1283
1284 if ((netif = ip4_route_src(src, dest)) == NULL) {
1285 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1286 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1287 IP_STATS_INC(ip.rterr);
1288 return ERR_RTE;
1289 }
1290
1291 NETIF_SET_HINTS(netif, netif_hint);
1292 err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1293 NETIF_RESET_HINTS(netif);
1294
1295 return err;
1296 }
1297 #endif /* LWIP_NETIF_USE_HINTS*/
1298
1299 #if LWIP_IP_FILTER
1300 /*
1301 * Set ip filter for input packet.
1302 */
set_ip4_filter(ip_filter_fn filter_fn)1303 err_t set_ip4_filter(ip_filter_fn filter_fn)
1304 {
1305 ip4_filter = filter_fn;
1306 return ERR_OK;
1307 }
1308 #endif /* LWIP_IP_FILTER */
1309
1310 #if IP_DEBUG
1311 /* Print an IP header by using LWIP_DEBUGF
1312 * @param p an IP packet, p->payload pointing to the IP header
1313 */
1314 void
ip4_debug_print(struct pbuf * p)1315 ip4_debug_print(struct pbuf *p)
1316 {
1317 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
1318
1319 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
1320 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1321 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
1322 (u16_t)IPH_V(iphdr),
1323 (u16_t)IPH_HL(iphdr),
1324 (u16_t)IPH_TOS(iphdr),
1325 lwip_ntohs(IPH_LEN(iphdr))));
1326 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1327 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
1328 lwip_ntohs(IPH_ID(iphdr)),
1329 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
1330 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
1331 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
1332 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
1333 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1334 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
1335 (u16_t)IPH_TTL(iphdr),
1336 (u16_t)IPH_PROTO(iphdr),
1337 lwip_ntohs(IPH_CHKSUM(iphdr))));
1338 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1339 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
1340 ip4_addr1_16_val(iphdr->src),
1341 ip4_addr2_16_val(iphdr->src),
1342 ip4_addr3_16_val(iphdr->src),
1343 ip4_addr4_16_val(iphdr->src)));
1344 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1345 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
1346 ip4_addr1_16_val(iphdr->dest),
1347 ip4_addr2_16_val(iphdr->dest),
1348 ip4_addr3_16_val(iphdr->dest),
1349 ip4_addr4_16_val(iphdr->dest)));
1350 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1351 }
1352 #endif /* IP_DEBUG */
1353
1354 #endif /* LWIP_IPV4 */
1355