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