xref: /third_party/lwip/src/core/ipv4/ip4.c (revision 195972f6)
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 */
107static u16_t ip_id;
108
109#if LWIP_MULTICAST_TX_OPTIONS
110/** The default netif used for multicast */
111static struct netif *ip4_default_multicast_netif;
112
113/**
114 * @ingroup ip4
115 * Set a default netif for IPv4 multicast. */
116void
117ip4_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 */
128struct netif *
129#ifdef LOSCFG_NET_CONTAINER
130ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest, struct net_group *group)
131#else
132ip4_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 */
159struct netif *
160#ifdef LOSCFG_NET_CONTAINER
161ip4_route(const ip4_addr_t *dest, struct net_group *group)
162#else
163ip4_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_netcmp(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_cmp(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, looopback 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 */
271static int
272ip4_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 */
313static void
314ip4_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  LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
373                         ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
374                         ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
375
376  IP_STATS_INC(ip.fw);
377  MIB2_STATS_INC(mib2.ipforwdatagrams);
378  IP_STATS_INC(ip.xmit);
379
380  PERF_STOP("ip4_forward");
381  /* don't fragment if interface has mtu set to 0 [loopif] */
382  if (netif->mtu && (p->tot_len > netif->mtu)) {
383    if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
384#if IP_FRAG
385      ip4_frag(p, netif, ip4_current_dest_addr());
386#else /* IP_FRAG */
387      /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
388#endif /* IP_FRAG */
389    } else {
390#if LWIP_ICMP
391      /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
392      icmp_dest_unreach(p, ICMP_DUR_FRAG);
393#endif /* LWIP_ICMP */
394    }
395    return;
396  }
397  /* transmit pbuf on chosen interface */
398  netif->output(netif, p, ip4_current_dest_addr());
399  return;
400return_noroute:
401  MIB2_STATS_INC(mib2.ipoutnoroutes);
402}
403#endif /* IP_FORWARD */
404
405/** Return true if the current input packet should be accepted on this netif */
406static int
407ip4_input_accept(struct netif *netif)
408{
409  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",
410                         ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
411                         ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
412                         ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
413                         ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
414
415  /* interface is up and configured? */
416  if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
417    /* unicast to this interface address? */
418    if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
419        /* or broadcast on this interface network address? */
420        ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
421#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
422        || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
423#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
424       ) {
425      LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",
426                             netif->name[0], netif->name[1]));
427      /* accept on this netif */
428      return 1;
429    }
430#if LWIP_AUTOIP
431    /* connections to link-local addresses must persist after changing
432        the netif's address (RFC3927 ch. 1.9) */
433    if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
434      LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",
435                             netif->name[0], netif->name[1]));
436      /* accept on this netif */
437      return 1;
438    }
439#endif /* LWIP_AUTOIP */
440  }
441  return 0;
442}
443
444/**
445 * This function is called by the network interface device driver when
446 * an IP packet is received. The function does the basic checks of the
447 * IP header such as packet size being at least larger than the header
448 * size etc. If the packet was not destined for us, the packet is
449 * forwarded (using ip_forward). The IP checksum is always checked.
450 *
451 * Finally, the packet is sent to the upper layer protocol input function.
452 *
453 * @param p the received IP packet (p->payload points to IP header)
454 * @param inp the netif on which this packet was received
455 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
456 *         processed, but currently always returns ERR_OK)
457 */
458err_t
459ip4_input(struct pbuf *p, struct netif *inp)
460{
461  const struct ip_hdr *iphdr;
462  struct netif *netif;
463  u16_t iphdr_hlen;
464  u16_t iphdr_len;
465#if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
466  int check_ip_src = 1;
467#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
468#if LWIP_RAW
469  raw_input_state_t raw_status;
470#endif /* LWIP_RAW */
471
472  LWIP_ASSERT_CORE_LOCKED();
473
474  IP_STATS_INC(ip.recv);
475  MIB2_STATS_INC(mib2.ipinreceives);
476
477#ifdef LOSCFG_NET_CONTAINER
478  struct net_group *group = get_net_group_from_netif(inp);
479#endif
480
481  /* identify the IP header */
482  iphdr = (struct ip_hdr *)p->payload;
483  if (IPH_V(iphdr) != 4) {
484    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
485    ip4_debug_print(p);
486    pbuf_free(p);
487    IP_STATS_INC(ip.err);
488    IP_STATS_INC(ip.drop);
489    MIB2_STATS_INC(mib2.ipinhdrerrors);
490    return ERR_OK;
491  }
492
493#ifdef LWIP_HOOK_IP4_INPUT
494  if (LWIP_HOOK_IP4_INPUT(p, inp)) {
495    /* the packet has been eaten */
496    return ERR_OK;
497  }
498#endif
499
500  /* obtain IP header length in bytes */
501  iphdr_hlen = IPH_HL_BYTES(iphdr);
502  /* obtain ip length in bytes */
503  iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
504
505  /* Trim pbuf. This is especially required for packets < 60 bytes. */
506  if (iphdr_len < p->tot_len) {
507    pbuf_realloc(p, iphdr_len);
508  }
509
510  /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
511  if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
512    if (iphdr_hlen < IP_HLEN) {
513      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
514                  ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
515    }
516    if (iphdr_hlen > p->len) {
517      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
518                  ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
519                   iphdr_hlen, p->len));
520    }
521    if (iphdr_len > p->tot_len) {
522      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
523                  ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
524                   iphdr_len, p->tot_len));
525    }
526    /* free (drop) packet pbufs */
527    pbuf_free(p);
528    IP_STATS_INC(ip.lenerr);
529    IP_STATS_INC(ip.drop);
530    MIB2_STATS_INC(mib2.ipindiscards);
531    return ERR_OK;
532  }
533
534  /* verify checksum */
535#if CHECKSUM_CHECK_IP
536  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
537    if (inet_chksum(iphdr, iphdr_hlen) != 0) {
538
539      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
540                  ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
541      ip4_debug_print(p);
542      pbuf_free(p);
543      IP_STATS_INC(ip.chkerr);
544      IP_STATS_INC(ip.drop);
545      MIB2_STATS_INC(mib2.ipinhdrerrors);
546      return ERR_OK;
547    }
548  }
549#endif
550
551  /* copy IP addresses to aligned ip_addr_t */
552  ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
553  ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
554
555  /* match packet against an interface, i.e. is this packet for us? */
556  if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
557#if LWIP_IGMP
558    if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
559      /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
560      ip4_addr_t allsystems;
561      IP4_ADDR(&allsystems, 224, 0, 0, 1);
562      if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
563          ip4_addr_isany(ip4_current_src_addr())) {
564        check_ip_src = 0;
565      }
566      netif = inp;
567    } else {
568      netif = NULL;
569    }
570#else /* LWIP_IGMP */
571    if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
572      netif = inp;
573    } else {
574      netif = NULL;
575    }
576#endif /* LWIP_IGMP */
577  } else {
578    /* start trying with inp. if that's not acceptable, start walking the
579       list of configured netifs. */
580    if (ip4_input_accept(inp)) {
581      netif = inp;
582    } else {
583      netif = NULL;
584#if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
585      /* Packets sent to the loopback address must not be accepted on an
586       * interface that does not have the loopback address assigned to it,
587       * unless a non-loopback interface is used for loopback traffic. */
588      if (!ip4_addr_isloopback(ip4_current_dest_addr()))
589#endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
590      {
591#if !LWIP_SINGLE_NETIF
592#ifdef LOSCFG_NET_CONTAINER
593        NETIF_FOREACH(netif, group) {
594#else
595        NETIF_FOREACH(netif) {
596#endif
597          if (netif == inp) {
598            /* we checked that before already */
599            continue;
600          }
601          if (ip4_input_accept(netif)) {
602            break;
603          }
604        }
605#endif /* !LWIP_SINGLE_NETIF */
606      }
607    }
608  }
609
610#if IP_ACCEPT_LINK_LAYER_ADDRESSING
611  /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
612   * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
613   * According to RFC 1542 section 3.1.1, referred by RFC 2131).
614   *
615   * If you want to accept private broadcast communication while a netif is down,
616   * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
617   *
618   * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
619   */
620  if (netif == NULL) {
621    /* remote port is DHCP server? */
622    if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
623      const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);
624      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
625                                              lwip_ntohs(udphdr->dest)));
626      if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
627        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
628        netif = inp;
629        check_ip_src = 0;
630      }
631    }
632  }
633#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
634
635  /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
636#if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
637  if (check_ip_src
638#if IP_ACCEPT_LINK_LAYER_ADDRESSING
639      /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
640      && !ip4_addr_isany_val(*ip4_current_src_addr())
641#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
642     )
643#endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
644  {
645    if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
646        (ip4_addr_ismulticast(ip4_current_src_addr()))) {
647      /* packet source is not valid */
648      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
649      /* free (drop) packet pbufs */
650      pbuf_free(p);
651      IP_STATS_INC(ip.drop);
652      MIB2_STATS_INC(mib2.ipinaddrerrors);
653      MIB2_STATS_INC(mib2.ipindiscards);
654      return ERR_OK;
655    }
656  }
657
658  /* packet not for us? */
659  if (netif == NULL) {
660    /* packet not for us, route or discard */
661    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
662#if IP_FORWARD
663    /* non-broadcast packet? */
664    if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
665      /* try to forward IP packet on (other) interfaces */
666      ip4_forward(p, (struct ip_hdr *)p->payload, inp);
667    } else
668#endif /* IP_FORWARD */
669    {
670      IP_STATS_INC(ip.drop);
671      MIB2_STATS_INC(mib2.ipinaddrerrors);
672      MIB2_STATS_INC(mib2.ipindiscards);
673    }
674    pbuf_free(p);
675    return ERR_OK;
676  }
677  /* packet consists of multiple fragments? */
678  if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
679#if IP_REASSEMBLY /* packet fragment reassembly code present? */
680    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",
681                           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)));
682    /* reassemble the packet*/
683    p = ip4_reass(p);
684    /* packet not fully reassembled yet? */
685    if (p == NULL) {
686      return ERR_OK;
687    }
688    iphdr = (const struct ip_hdr *)p->payload;
689#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
690    pbuf_free(p);
691    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
692                lwip_ntohs(IPH_OFFSET(iphdr))));
693    IP_STATS_INC(ip.opterr);
694    IP_STATS_INC(ip.drop);
695    /* unsupported protocol feature */
696    MIB2_STATS_INC(mib2.ipinunknownprotos);
697    return ERR_OK;
698#endif /* IP_REASSEMBLY */
699  }
700
701#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
702
703#if LWIP_IGMP
704  /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
705  if ((iphdr_hlen > IP_HLEN) &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
706#else
707  if (iphdr_hlen > IP_HLEN) {
708#endif /* LWIP_IGMP */
709    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
710    pbuf_free(p);
711    IP_STATS_INC(ip.opterr);
712    IP_STATS_INC(ip.drop);
713    /* unsupported protocol feature */
714    MIB2_STATS_INC(mib2.ipinunknownprotos);
715    return ERR_OK;
716  }
717#endif /* IP_OPTIONS_ALLOWED == 0 */
718
719  /* send to upper layers */
720  LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
721  ip4_debug_print(p);
722  LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
723
724  ip_data.current_netif = netif;
725  ip_data.current_input_netif = inp;
726  ip_data.current_ip4_header = iphdr;
727  ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);
728
729#if LWIP_RAW
730  /* raw input did not eat the packet? */
731  raw_status = raw_input(p, inp);
732  if (raw_status != RAW_INPUT_EATEN)
733#endif /* LWIP_RAW */
734  {
735    pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */
736
737    switch (IPH_PROTO(iphdr)) {
738#if LWIP_UDP
739      case IP_PROTO_UDP:
740#if LWIP_UDPLITE
741      case IP_PROTO_UDPLITE:
742#endif /* LWIP_UDPLITE */
743        MIB2_STATS_INC(mib2.ipindelivers);
744        udp_input(p, inp);
745        break;
746#endif /* LWIP_UDP */
747#if LWIP_TCP
748      case IP_PROTO_TCP:
749        MIB2_STATS_INC(mib2.ipindelivers);
750        tcp_input(p, inp);
751        break;
752#endif /* LWIP_TCP */
753#if LWIP_ICMP
754      case IP_PROTO_ICMP:
755        MIB2_STATS_INC(mib2.ipindelivers);
756        icmp_input(p, inp);
757        break;
758#endif /* LWIP_ICMP */
759#if LWIP_IGMP
760      case IP_PROTO_IGMP:
761        igmp_input(p, inp, ip4_current_dest_addr());
762        break;
763#endif /* LWIP_IGMP */
764      default:
765#if LWIP_RAW
766        if (raw_status == RAW_INPUT_DELIVERED) {
767          MIB2_STATS_INC(mib2.ipindelivers);
768        } else
769#endif /* LWIP_RAW */
770        {
771#if LWIP_ICMP
772          /* send ICMP destination protocol unreachable unless is was a broadcast */
773          if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
774              !ip4_addr_ismulticast(ip4_current_dest_addr())) {
775            pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */
776            icmp_dest_unreach(p, ICMP_DUR_PROTO);
777          }
778#endif /* LWIP_ICMP */
779
780          LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
781
782          IP_STATS_INC(ip.proterr);
783          IP_STATS_INC(ip.drop);
784          MIB2_STATS_INC(mib2.ipinunknownprotos);
785        }
786        pbuf_free(p);
787        break;
788    }
789  }
790
791  /* @todo: this is not really necessary... */
792  ip_data.current_netif = NULL;
793  ip_data.current_input_netif = NULL;
794  ip_data.current_ip4_header = NULL;
795  ip_data.current_ip_header_tot_len = 0;
796  ip4_addr_set_any(ip4_current_src_addr());
797  ip4_addr_set_any(ip4_current_dest_addr());
798
799  return ERR_OK;
800}
801
802/**
803 * Sends an IP packet on a network interface. This function constructs
804 * the IP header and calculates the IP header checksum. If the source
805 * IP address is NULL, the IP address of the outgoing network
806 * interface is filled in as source address.
807 * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
808 * include an IP header and p->payload points to it instead of the data.
809 *
810 * @param p the packet to send (p->payload points to the data, e.g. next
811            protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
812            IP header and p->payload points to that IP header)
813 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
814 *         IP  address of the netif used to send is used as source address)
815 * @param dest the destination IP address to send the packet to
816 * @param ttl the TTL value to be set in the IP header
817 * @param tos the TOS value to be set in the IP header
818 * @param proto the PROTOCOL to be set in the IP header
819 * @param netif the netif on which to send this packet
820 * @return ERR_OK if the packet was sent OK
821 *         ERR_BUF if p doesn't have enough space for IP/LINK headers
822 *         returns errors returned by netif->output
823 *
824 * @note ip_id: RFC791 "some host may be able to simply use
825 *  unique identifiers independent of destination"
826 */
827err_t
828ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
829              u8_t ttl, u8_t tos,
830              u8_t proto, struct netif *netif)
831{
832#if IP_OPTIONS_SEND
833  return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
834}
835
836/**
837 * Same as ip_output_if() but with the possibility to include IP options:
838 *
839 * @ param ip_options pointer to the IP options, copied into the IP header
840 * @ param optlen length of ip_options
841 */
842err_t
843ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
844                  u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
845                  u16_t optlen)
846{
847#endif /* IP_OPTIONS_SEND */
848  const ip4_addr_t *src_used = src;
849  if (dest != LWIP_IP_HDRINCL) {
850    if (ip4_addr_isany(src)) {
851      src_used = netif_ip4_addr(netif);
852    }
853  }
854
855#if IP_OPTIONS_SEND
856  return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
857                               ip_options, optlen);
858#else /* IP_OPTIONS_SEND */
859  return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
860#endif /* IP_OPTIONS_SEND */
861}
862
863/**
864 * Same as ip_output_if() but 'src' address is not replaced by netif address
865 * when it is 'any'.
866 */
867err_t
868ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
869                  u8_t ttl, u8_t tos,
870                  u8_t proto, struct netif *netif)
871{
872#if IP_OPTIONS_SEND
873  return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
874}
875
876/**
877 * Same as ip_output_if_opt() but 'src' address is not replaced by netif address
878 * when it is 'any'.
879 */
880err_t
881ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
882                      u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
883                      u16_t optlen)
884{
885#endif /* IP_OPTIONS_SEND */
886  struct ip_hdr *iphdr;
887  ip4_addr_t dest_addr;
888#if CHECKSUM_GEN_IP_INLINE
889  u32_t chk_sum = 0;
890#endif /* CHECKSUM_GEN_IP_INLINE */
891
892  LWIP_ASSERT_CORE_LOCKED();
893  LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
894
895  MIB2_STATS_INC(mib2.ipoutrequests);
896
897  /* Should the IP header be generated or is it already included in p? */
898  if (dest != LWIP_IP_HDRINCL) {
899    u16_t ip_hlen = IP_HLEN;
900#if IP_OPTIONS_SEND
901    u16_t optlen_aligned = 0;
902    if (optlen != 0) {
903#if CHECKSUM_GEN_IP_INLINE
904      int i;
905#endif /* CHECKSUM_GEN_IP_INLINE */
906      if (optlen > (IP_HLEN_MAX - IP_HLEN)) {
907        /* optlen too long */
908        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));
909        IP_STATS_INC(ip.err);
910        MIB2_STATS_INC(mib2.ipoutdiscards);
911        return ERR_VAL;
912      }
913      /* round up to a multiple of 4 */
914      optlen_aligned = (u16_t)((optlen + 3) & ~3);
915      ip_hlen = (u16_t)(ip_hlen + optlen_aligned);
916      /* First write in the IP options */
917      if (pbuf_add_header(p, optlen_aligned)) {
918        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
919        IP_STATS_INC(ip.err);
920        MIB2_STATS_INC(mib2.ipoutdiscards);
921        return ERR_BUF;
922      }
923      MEMCPY(p->payload, ip_options, optlen);
924      if (optlen < optlen_aligned) {
925        /* zero the remaining bytes */
926        memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));
927      }
928#if CHECKSUM_GEN_IP_INLINE
929      for (i = 0; i < optlen_aligned / 2; i++) {
930        chk_sum += ((u16_t *)p->payload)[i];
931      }
932#endif /* CHECKSUM_GEN_IP_INLINE */
933    }
934#endif /* IP_OPTIONS_SEND */
935    /* generate IP header */
936    if (pbuf_add_header(p, IP_HLEN)) {
937      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
938
939      IP_STATS_INC(ip.err);
940      MIB2_STATS_INC(mib2.ipoutdiscards);
941      return ERR_BUF;
942    }
943
944    iphdr = (struct ip_hdr *)p->payload;
945    LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
946                (p->len >= sizeof(struct ip_hdr)));
947
948    IPH_TTL_SET(iphdr, ttl);
949    IPH_PROTO_SET(iphdr, proto);
950#if CHECKSUM_GEN_IP_INLINE
951    chk_sum += PP_NTOHS(proto | (ttl << 8));
952#endif /* CHECKSUM_GEN_IP_INLINE */
953
954    /* dest cannot be NULL here */
955    ip4_addr_copy(iphdr->dest, *dest);
956#if CHECKSUM_GEN_IP_INLINE
957    chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
958    chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
959#endif /* CHECKSUM_GEN_IP_INLINE */
960
961    IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
962    IPH_TOS_SET(iphdr, tos);
963#if CHECKSUM_GEN_IP_INLINE
964    chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
965#endif /* CHECKSUM_GEN_IP_INLINE */
966    IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
967#if CHECKSUM_GEN_IP_INLINE
968    chk_sum += iphdr->_len;
969#endif /* CHECKSUM_GEN_IP_INLINE */
970    IPH_OFFSET_SET(iphdr, 0);
971    IPH_ID_SET(iphdr, lwip_htons(ip_id));
972#if CHECKSUM_GEN_IP_INLINE
973    chk_sum += iphdr->_id;
974#endif /* CHECKSUM_GEN_IP_INLINE */
975    ++ip_id;
976
977    if (src == NULL) {
978      ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
979    } else {
980      /* src cannot be NULL here */
981      ip4_addr_copy(iphdr->src, *src);
982    }
983
984#if CHECKSUM_GEN_IP_INLINE
985    chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
986    chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
987    chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
988    chk_sum = (chk_sum >> 16) + chk_sum;
989    chk_sum = ~chk_sum;
990    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
991      iphdr->_chksum = (u16_t)chk_sum; /* network order */
992    }
993#if LWIP_CHECKSUM_CTRL_PER_NETIF
994    else {
995      IPH_CHKSUM_SET(iphdr, 0);
996    }
997#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
998#else /* CHECKSUM_GEN_IP_INLINE */
999    IPH_CHKSUM_SET(iphdr, 0);
1000#if CHECKSUM_GEN_IP
1001    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
1002      IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
1003    }
1004#endif /* CHECKSUM_GEN_IP */
1005#endif /* CHECKSUM_GEN_IP_INLINE */
1006  } else {
1007    /* IP header already included in p */
1008    if (p->len < IP_HLEN) {
1009      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));
1010      IP_STATS_INC(ip.err);
1011      MIB2_STATS_INC(mib2.ipoutdiscards);
1012      return ERR_BUF;
1013    }
1014    iphdr = (struct ip_hdr *)p->payload;
1015    ip4_addr_copy(dest_addr, iphdr->dest);
1016    dest = &dest_addr;
1017  }
1018
1019  IP_STATS_INC(ip.xmit);
1020
1021  LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
1022  ip4_debug_print(p);
1023
1024#if ENABLE_LOOPBACK
1025  if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
1026#if !LWIP_HAVE_LOOPIF
1027      || ip4_addr_isloopback(dest)
1028#endif /* !LWIP_HAVE_LOOPIF */
1029     ) {
1030    /* Packet to self, enqueue it for loopback */
1031    LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
1032    return netif_loop_output(netif, p);
1033  }
1034#if LWIP_MULTICAST_TX_OPTIONS
1035  if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
1036    netif_loop_output(netif, p);
1037  }
1038#endif /* LWIP_MULTICAST_TX_OPTIONS */
1039#endif /* ENABLE_LOOPBACK */
1040#if IP_FRAG
1041  /* don't fragment if interface has mtu set to 0 [loopif] */
1042  if (netif->mtu && (p->tot_len > netif->mtu)) {
1043    return ip4_frag(p, netif, dest);
1044  }
1045#endif /* IP_FRAG */
1046
1047  LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
1048  return netif->output(netif, p, dest);
1049}
1050
1051/**
1052 * Simple interface to ip_output_if. It finds the outgoing network
1053 * interface and calls upon ip_output_if to do the actual work.
1054 *
1055 * @param p the packet to send (p->payload points to the data, e.g. next
1056            protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1057            IP header and p->payload points to that IP header)
1058 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1059 *         IP  address of the netif used to send is used as source address)
1060 * @param dest the destination IP address to send the packet to
1061 * @param ttl the TTL value to be set in the IP header
1062 * @param tos the TOS value to be set in the IP header
1063 * @param proto the PROTOCOL to be set in the IP header
1064 *
1065 * @return ERR_RTE if no route is found
1066 *         see ip_output_if() for more return values
1067 */
1068err_t
1069ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1070           u8_t ttl, u8_t tos, u8_t proto)
1071{
1072  struct netif *netif;
1073
1074  LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1075#ifdef LOSCFG_NET_CONTAINER
1076  struct net_group *group = get_curr_process_net_group();
1077
1078  if ((netif = ip4_route_src(src, dest, group)) == NULL) {
1079#else
1080  if ((netif = ip4_route_src(src, dest)) == NULL) {
1081#endif
1082    LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1083                           ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1084    IP_STATS_INC(ip.rterr);
1085    return ERR_RTE;
1086  }
1087
1088  return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1089}
1090
1091#if LWIP_NETIF_USE_HINTS
1092/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
1093 *  before calling ip_output_if.
1094 *
1095 * @param p the packet to send (p->payload points to the data, e.g. next
1096            protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1097            IP header and p->payload points to that IP header)
1098 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1099 *         IP  address of the netif used to send is used as source address)
1100 * @param dest the destination IP address to send the packet to
1101 * @param ttl the TTL value to be set in the IP header
1102 * @param tos the TOS value to be set in the IP header
1103 * @param proto the PROTOCOL to be set in the IP header
1104 * @param netif_hint netif output hint pointer set to netif->hint before
1105 *        calling ip_output_if()
1106 *
1107 * @return ERR_RTE if no route is found
1108 *         see ip_output_if() for more return values
1109 */
1110err_t
1111ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1112                  u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
1113{
1114  struct netif *netif;
1115  err_t err;
1116
1117  LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1118
1119  if ((netif = ip4_route_src(src, dest)) == NULL) {
1120    LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1121                           ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1122    IP_STATS_INC(ip.rterr);
1123    return ERR_RTE;
1124  }
1125
1126  NETIF_SET_HINTS(netif, netif_hint);
1127  err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1128  NETIF_RESET_HINTS(netif);
1129
1130  return err;
1131}
1132#endif /* LWIP_NETIF_USE_HINTS*/
1133
1134#if IP_DEBUG
1135/* Print an IP header by using LWIP_DEBUGF
1136 * @param p an IP packet, p->payload pointing to the IP header
1137 */
1138void
1139ip4_debug_print(struct pbuf *p)
1140{
1141  struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
1142
1143  LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
1144  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1145  LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
1146                         (u16_t)IPH_V(iphdr),
1147                         (u16_t)IPH_HL(iphdr),
1148                         (u16_t)IPH_TOS(iphdr),
1149                         lwip_ntohs(IPH_LEN(iphdr))));
1150  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1151  LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
1152                         lwip_ntohs(IPH_ID(iphdr)),
1153                         (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
1154                         (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
1155                         (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
1156                         (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
1157  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1158  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
1159                         (u16_t)IPH_TTL(iphdr),
1160                         (u16_t)IPH_PROTO(iphdr),
1161                         lwip_ntohs(IPH_CHKSUM(iphdr))));
1162  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1163  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
1164                         ip4_addr1_16_val(iphdr->src),
1165                         ip4_addr2_16_val(iphdr->src),
1166                         ip4_addr3_16_val(iphdr->src),
1167                         ip4_addr4_16_val(iphdr->src)));
1168  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1169  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
1170                         ip4_addr1_16_val(iphdr->dest),
1171                         ip4_addr2_16_val(iphdr->dest),
1172                         ip4_addr3_16_val(iphdr->dest),
1173                         ip4_addr4_16_val(iphdr->dest)));
1174  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1175}
1176#endif /* IP_DEBUG */
1177
1178#endif /* LWIP_IPV4 */
1179