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