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