Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_ip4.c Source File

lwip_ip4.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
00004  *
00005  * @see ip_frag.c
00006  *
00007  */
00008 
00009 /*
00010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  * 2. Redistributions in binary form must reproduce the above copyright notice,
00019  *    this list of conditions and the following disclaimer in the documentation
00020  *    and/or other materials provided with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote products
00022  *    derived from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00033  * OF SUCH DAMAGE.
00034  *
00035  * This file is part of the lwIP TCP/IP stack.
00036  *
00037  * Author: Adam Dunkels <adam@sics.se>
00038  *
00039  */
00040 
00041 #include "lwip/opt.h"
00042 
00043 #if LWIP_IPV4
00044 
00045 #include "lwip/ip.h"
00046 #include "lwip/def.h"
00047 #include "lwip/mem.h"
00048 #include "lwip/ip4_frag.h"
00049 #include "lwip/inet_chksum.h"
00050 #include "lwip/netif.h"
00051 #include "lwip/icmp.h"
00052 #include "lwip/igmp.h"
00053 #include "lwip/priv/raw_priv.h"
00054 #include "lwip/udp.h"
00055 #include "lwip/priv/tcp_priv.h"
00056 #include "lwip/autoip.h"
00057 #include "lwip/stats.h"
00058 #include "lwip/prot/iana.h"
00059 
00060 #include <string.h>
00061 
00062 #ifdef LWIP_HOOK_FILENAME
00063 #include LWIP_HOOK_FILENAME
00064 #endif
00065 
00066 /** Set this to 0 in the rare case of wanting to call an extra function to
00067  * generate the IP checksum (in contrast to calculating it on-the-fly). */
00068 #ifndef LWIP_INLINE_IP_CHKSUM
00069 #if LWIP_CHECKSUM_CTRL_PER_NETIF
00070 #define LWIP_INLINE_IP_CHKSUM   0
00071 #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
00072 #define LWIP_INLINE_IP_CHKSUM   1
00073 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
00074 #endif
00075 
00076 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
00077 #define CHECKSUM_GEN_IP_INLINE  1
00078 #else
00079 #define CHECKSUM_GEN_IP_INLINE  0
00080 #endif
00081 
00082 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
00083 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
00084 
00085 /** Some defines for DHCP to let link-layer-addressed packets through while the
00086  * netif is down.
00087  * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port)
00088  * to return 1 if the port is accepted and 0 if the port is not accepted.
00089  */
00090 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
00091 /* accept DHCP client port and custom port */
00092 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \
00093          || (LWIP_IP_ACCEPT_UDP_PORT(port)))
00094 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
00095 /* accept custom port only */
00096 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
00097 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
00098 /* accept DHCP client port only */
00099 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT))
00100 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
00101 
00102 #else /* LWIP_DHCP */
00103 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
00104 #endif /* LWIP_DHCP */
00105 
00106 /** The IP header ID of the next outgoing IP packet */
00107 static u16_t ip_id;
00108 
00109 #if LWIP_MULTICAST_TX_OPTIONS
00110 /** The default netif used for multicast */
00111 static struct netif *ip4_default_multicast_netif;
00112 
00113 /**
00114  * @ingroup ip4
00115  * Set a default netif for IPv4 multicast. */
00116 void
00117 ip4_set_default_multicast_netif(struct netif *default_multicast_netif)
00118 {
00119   ip4_default_multicast_netif = default_multicast_netif;
00120 }
00121 #endif /* LWIP_MULTICAST_TX_OPTIONS */
00122 
00123 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
00124 /**
00125  * Source based IPv4 routing must be fully implemented in
00126  * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters.
00127  */
00128 struct netif *
00129 ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest)
00130 {
00131   if (src != NULL) {
00132     /* when src==NULL, the hook is called from ip4_route(dest) */
00133     struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(src, dest);
00134     if (netif != NULL) {
00135       return netif;
00136     }
00137   }
00138   return ip4_route(dest);
00139 }
00140 #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
00141 
00142 /**
00143  * Finds the appropriate network interface for a given IP address. It
00144  * searches the list of network interfaces linearly. A match is found
00145  * if the masked IP address of the network interface equals the masked
00146  * IP address given to the function.
00147  *
00148  * @param dest the destination IP address for which to find the route
00149  * @return the netif on which to send to reach dest
00150  */
00151 struct netif *
00152 ip4_route(const ip4_addr_t *dest)
00153 {
00154 #if !LWIP_SINGLE_NETIF
00155   struct netif *netif;
00156 
00157   LWIP_ASSERT_CORE_LOCKED();
00158 
00159 #if LWIP_MULTICAST_TX_OPTIONS
00160   /* Use administratively selected interface for multicast by default */
00161   if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
00162     return ip4_default_multicast_netif;
00163   }
00164 #endif /* LWIP_MULTICAST_TX_OPTIONS */
00165 
00166   /* bug #54569: in case LWIP_SINGLE_NETIF=1 and LWIP_DEBUGF() disabled, the following loop is optimized away */
00167   LWIP_UNUSED_ARG(dest);
00168 
00169   /* iterate through netifs */
00170   NETIF_FOREACH(netif) {
00171     /* is the netif up, does it have a link and a valid address? */
00172     if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
00173       /* network mask matches? */
00174       if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {
00175         /* return netif on which to forward IP packet */
00176         return netif;
00177       }
00178       /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
00179       if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) {
00180         /* return netif on which to forward IP packet */
00181         return netif;
00182       }
00183     }
00184   }
00185 
00186 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
00187   /* loopif is disabled, looopback traffic is passed through any netif */
00188   if (ip4_addr_isloopback(dest)) {
00189     /* don't check for link on loopback traffic */
00190     if (netif_default != NULL && netif_is_up(netif_default)) {
00191       return netif_default;
00192     }
00193     /* default netif is not up, just use any netif for loopback traffic */
00194     NETIF_FOREACH(netif) {
00195       if (netif_is_up(netif)) {
00196         return netif;
00197       }
00198     }
00199     return NULL;
00200   }
00201 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
00202 
00203 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
00204   netif = LWIP_HOOK_IP4_ROUTE_SRC(NULL, dest);
00205   if (netif != NULL) {
00206     return netif;
00207   }
00208 #elif defined(LWIP_HOOK_IP4_ROUTE)
00209   netif = LWIP_HOOK_IP4_ROUTE(dest);
00210   if (netif != NULL) {
00211     return netif;
00212   }
00213 #endif
00214 #endif /* !LWIP_SINGLE_NETIF */
00215 
00216   if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||
00217       ip4_addr_isany_val(*netif_ip4_addr(netif_default)) || ip4_addr_isloopback(dest)) {
00218     /* No matching netif found and default netif is not usable.
00219        If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */
00220     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
00221                 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
00222     IP_STATS_INC(ip.rterr);
00223     MIB2_STATS_INC(mib2.ipoutnoroutes);
00224     return NULL;
00225   }
00226 
00227   return netif_default;
00228 }
00229 
00230 #if IP_FORWARD
00231 /**
00232  * Determine whether an IP address is in a reserved set of addresses
00233  * that may not be forwarded, or whether datagrams to that destination
00234  * may be forwarded.
00235  * @param p the packet to forward
00236  * @return 1: can forward 0: discard
00237  */
00238 static int
00239 ip4_canforward(struct pbuf *p)
00240 {
00241   u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));
00242 
00243 #ifdef LWIP_HOOK_IP4_CANFORWARD
00244   int ret = LWIP_HOOK_IP4_CANFORWARD(p, addr);
00245   if (ret >= 0) {
00246     return ret;
00247   }
00248 #endif /* LWIP_HOOK_IP4_CANFORWARD */
00249 
00250   if (p->flags & PBUF_FLAG_LLBCAST) {
00251     /* don't route link-layer broadcasts */
00252     return 0;
00253   }
00254   if ((p->flags & PBUF_FLAG_LLMCAST) || IP_MULTICAST(addr)) {
00255     /* don't route link-layer multicasts (use LWIP_HOOK_IP4_CANFORWARD instead) */
00256     return 0;
00257   }
00258   if (IP_EXPERIMENTAL(addr)) {
00259     return 0;
00260   }
00261   if (IP_CLASSA(addr)) {
00262     u32_t net = addr & IP_CLASSA_NET;
00263     if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
00264       /* don't route loopback packets */
00265       return 0;
00266     }
00267   }
00268   return 1;
00269 }
00270 
00271 /**
00272  * Forwards an IP packet. It finds an appropriate route for the
00273  * packet, decrements the TTL value of the packet, adjusts the
00274  * checksum and outputs the packet on the appropriate interface.
00275  *
00276  * @param p the packet to forward (p->payload points to IP header)
00277  * @param iphdr the IP header of the input packet
00278  * @param inp the netif on which this packet was received
00279  */
00280 static void
00281 ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
00282 {
00283   struct netif *netif;
00284 
00285   PERF_START;
00286   LWIP_UNUSED_ARG(inp);
00287 
00288   if (!ip4_canforward(p)) {
00289     goto return_noroute;
00290   }
00291 
00292   /* RFC3927 2.7: do not forward link-local addresses */
00293   if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
00294     LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
00295                            ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
00296                            ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
00297     goto return_noroute;
00298   }
00299 
00300   /* Find network interface where to forward this IP packet to. */
00301   netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr());
00302   if (netif == NULL) {
00303     LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
00304                            ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
00305                            ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
00306     /* @todo: send ICMP_DUR_NET? */
00307     goto return_noroute;
00308   }
00309 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
00310   /* Do not forward packets onto the same network interface on which
00311    * they arrived. */
00312   if (netif == inp) {
00313     LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n"));
00314     goto return_noroute;
00315   }
00316 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
00317 
00318   /* decrement TTL */
00319   IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
00320   /* send ICMP if TTL == 0 */
00321   if (IPH_TTL(iphdr) == 0) {
00322     MIB2_STATS_INC(mib2.ipinhdrerrors);
00323 #if LWIP_ICMP
00324     /* Don't send ICMP messages in response to ICMP messages */
00325     if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
00326       icmp_time_exceeded(p, ICMP_TE_TTL);
00327     }
00328 #endif /* LWIP_ICMP */
00329     return;
00330   }
00331 
00332   /* Incrementally update the IP checksum. */
00333   if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
00334     IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1));
00335   } else {
00336     IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100)));
00337   }
00338 
00339   LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
00340                          ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
00341                          ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
00342 
00343   IP_STATS_INC(ip.fw);
00344   MIB2_STATS_INC(mib2.ipforwdatagrams);
00345   IP_STATS_INC(ip.xmit);
00346 
00347   PERF_STOP("ip4_forward");
00348   /* don't fragment if interface has mtu set to 0 [loopif] */
00349   if (netif->mtu && (p->tot_len > netif->mtu)) {
00350     if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
00351 #if IP_FRAG
00352       ip4_frag(p, netif, ip4_current_dest_addr());
00353 #else /* IP_FRAG */
00354       /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
00355 #endif /* IP_FRAG */
00356     } else {
00357 #if LWIP_ICMP
00358       /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
00359       icmp_dest_unreach(p, ICMP_DUR_FRAG);
00360 #endif /* LWIP_ICMP */
00361     }
00362     return;
00363   }
00364   /* transmit pbuf on chosen interface */
00365   netif->output(netif, p, ip4_current_dest_addr());
00366   return;
00367 return_noroute:
00368   MIB2_STATS_INC(mib2.ipoutnoroutes);
00369 }
00370 #endif /* IP_FORWARD */
00371 
00372 /** Return true if the current input packet should be accepted on this netif */
00373 static int
00374 ip4_input_accept(struct netif *netif)
00375 {
00376   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",
00377                          ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
00378                          ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
00379                          ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
00380                          ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
00381 
00382   /* interface is up and configured? */
00383   if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
00384     /* unicast to this interface address? */
00385     if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
00386         /* or broadcast on this interface network address? */
00387         ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
00388 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
00389         || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
00390 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
00391        ) {
00392       LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",
00393                              netif->name[0], netif->name[1]));
00394       /* accept on this netif */
00395       return 1;
00396     }
00397 #if LWIP_AUTOIP
00398     /* connections to link-local addresses must persist after changing
00399         the netif's address (RFC3927 ch. 1.9) */
00400     if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
00401       LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",
00402                              netif->name[0], netif->name[1]));
00403       /* accept on this netif */
00404       return 1;
00405     }
00406 #endif /* LWIP_AUTOIP */
00407   }
00408   return 0;
00409 }
00410 
00411 /**
00412  * This function is called by the network interface device driver when
00413  * an IP packet is received. The function does the basic checks of the
00414  * IP header such as packet size being at least larger than the header
00415  * size etc. If the packet was not destined for us, the packet is
00416  * forwarded (using ip_forward). The IP checksum is always checked.
00417  *
00418  * Finally, the packet is sent to the upper layer protocol input function.
00419  *
00420  * @param p the received IP packet (p->payload points to IP header)
00421  * @param inp the netif on which this packet was received
00422  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
00423  *         processed, but currently always returns ERR_OK)
00424  */
00425 err_t
00426 ip4_input(struct pbuf *p, struct netif *inp)
00427 {
00428   const struct ip_hdr *iphdr;
00429   struct netif *netif;
00430   u16_t iphdr_hlen;
00431   u16_t iphdr_len;
00432 #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
00433   int check_ip_src = 1;
00434 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
00435 #if LWIP_RAW
00436   raw_input_state_t raw_status;
00437 #endif /* LWIP_RAW */
00438 
00439   LWIP_ASSERT_CORE_LOCKED();
00440 
00441   IP_STATS_INC(ip.recv);
00442   MIB2_STATS_INC(mib2.ipinreceives);
00443 
00444   /* identify the IP header */
00445   iphdr = (struct ip_hdr *)p->payload;
00446   if (IPH_V(iphdr) != 4) {
00447     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
00448     ip4_debug_print(p);
00449     pbuf_free(p);
00450     IP_STATS_INC(ip.err);
00451     IP_STATS_INC(ip.drop);
00452     MIB2_STATS_INC(mib2.ipinhdrerrors);
00453     return ERR_OK;
00454   }
00455 
00456 #ifdef LWIP_HOOK_IP4_INPUT
00457   if (LWIP_HOOK_IP4_INPUT(p, inp)) {
00458     /* the packet has been eaten */
00459     return ERR_OK;
00460   }
00461 #endif
00462 
00463   /* obtain IP header length in bytes */
00464   iphdr_hlen = IPH_HL_BYTES(iphdr);
00465   /* obtain ip length in bytes */
00466   iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
00467 
00468   /* Trim pbuf. This is especially required for packets < 60 bytes. */
00469   if (iphdr_len < p->tot_len) {
00470     pbuf_realloc(p, iphdr_len);
00471   }
00472 
00473   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
00474   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
00475     if (iphdr_hlen < IP_HLEN) {
00476       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00477                   ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
00478     }
00479     if (iphdr_hlen > p->len) {
00480       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00481                   ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
00482                    iphdr_hlen, p->len));
00483     }
00484     if (iphdr_len > p->tot_len) {
00485       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00486                   ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
00487                    iphdr_len, p->tot_len));
00488     }
00489     /* free (drop) packet pbufs */
00490     pbuf_free(p);
00491     IP_STATS_INC(ip.lenerr);
00492     IP_STATS_INC(ip.drop);
00493     MIB2_STATS_INC(mib2.ipindiscards);
00494     return ERR_OK;
00495   }
00496 
00497   /* verify checksum */
00498 #if CHECKSUM_CHECK_IP
00499   IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
00500     if (inet_chksum(iphdr, iphdr_hlen) != 0) {
00501 
00502       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00503                   ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
00504       ip4_debug_print(p);
00505       pbuf_free(p);
00506       IP_STATS_INC(ip.chkerr);
00507       IP_STATS_INC(ip.drop);
00508       MIB2_STATS_INC(mib2.ipinhdrerrors);
00509       return ERR_OK;
00510     }
00511   }
00512 #endif
00513 
00514   /* copy IP addresses to aligned ip_addr_t */
00515   ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
00516   ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
00517 
00518   /* match packet against an interface, i.e. is this packet for us? */
00519   if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
00520 #if LWIP_IGMP
00521     if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
00522       /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
00523       ip4_addr_t allsystems;
00524       IP4_ADDR(&allsystems, 224, 0, 0, 1);
00525       if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
00526           ip4_addr_isany(ip4_current_src_addr())) {
00527         check_ip_src = 0;
00528       }
00529       netif = inp;
00530     } else {
00531       netif = NULL;
00532     }
00533 #else /* LWIP_IGMP */
00534     if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
00535       netif = inp;
00536     } else {
00537       netif = NULL;
00538     }
00539 #endif /* LWIP_IGMP */
00540   } else {
00541     /* start trying with inp. if that's not acceptable, start walking the
00542        list of configured netifs. */
00543     if (ip4_input_accept(inp)) {
00544       netif = inp;
00545     } else {
00546       netif = NULL;
00547 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
00548       /* Packets sent to the loopback address must not be accepted on an
00549        * interface that does not have the loopback address assigned to it,
00550        * unless a non-loopback interface is used for loopback traffic. */
00551       if (!ip4_addr_isloopback(ip4_current_dest_addr()))
00552 #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
00553       {
00554 #if !LWIP_SINGLE_NETIF
00555         NETIF_FOREACH(netif) {
00556           if (netif == inp) {
00557             /* we checked that before already */
00558             continue;
00559           }
00560           if (ip4_input_accept(netif)) {
00561             break;
00562           }
00563         }
00564 #endif /* !LWIP_SINGLE_NETIF */
00565       }
00566     }
00567   }
00568 
00569 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
00570   /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
00571    * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
00572    * According to RFC 1542 section 3.1.1, referred by RFC 2131).
00573    *
00574    * If you want to accept private broadcast communication while a netif is down,
00575    * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
00576    *
00577    * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
00578    */
00579   if (netif == NULL) {
00580     /* remote port is DHCP server? */
00581     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
00582       const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);
00583       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
00584                                               lwip_ntohs(udphdr->dest)));
00585       if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
00586         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
00587         netif = inp;
00588         check_ip_src = 0;
00589       }
00590     }
00591   }
00592 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
00593 
00594   /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
00595 #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
00596   if (check_ip_src
00597 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
00598       /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
00599       && !ip4_addr_isany_val(*ip4_current_src_addr())
00600 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
00601      )
00602 #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
00603   {
00604     if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
00605         (ip4_addr_ismulticast(ip4_current_src_addr()))) {
00606       /* packet source is not valid */
00607       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
00608       /* free (drop) packet pbufs */
00609       pbuf_free(p);
00610       IP_STATS_INC(ip.drop);
00611       MIB2_STATS_INC(mib2.ipinaddrerrors);
00612       MIB2_STATS_INC(mib2.ipindiscards);
00613       return ERR_OK;
00614     }
00615   }
00616 
00617   /* packet not for us? */
00618   if (netif == NULL) {
00619     /* packet not for us, route or discard */
00620     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
00621 #if IP_FORWARD
00622     /* non-broadcast packet? */
00623     if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
00624       /* try to forward IP packet on (other) interfaces */
00625       ip4_forward(p, (struct ip_hdr *)p->payload, inp);
00626     } else
00627 #endif /* IP_FORWARD */
00628     {
00629       IP_STATS_INC(ip.drop);
00630       MIB2_STATS_INC(mib2.ipinaddrerrors);
00631       MIB2_STATS_INC(mib2.ipindiscards);
00632     }
00633     pbuf_free(p);
00634     return ERR_OK;
00635   }
00636   /* packet consists of multiple fragments? */
00637   if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
00638 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
00639     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",
00640                            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)));
00641     /* reassemble the packet*/
00642     p = ip4_reass(p);
00643     /* packet not fully reassembled yet? */
00644     if (p == NULL) {
00645       return ERR_OK;
00646     }
00647     iphdr = (const struct ip_hdr *)p->payload;
00648 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
00649     pbuf_free(p);
00650     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
00651                 lwip_ntohs(IPH_OFFSET(iphdr))));
00652     IP_STATS_INC(ip.opterr);
00653     IP_STATS_INC(ip.drop);
00654     /* unsupported protocol feature */
00655     MIB2_STATS_INC(mib2.ipinunknownprotos);
00656     return ERR_OK;
00657 #endif /* IP_REASSEMBLY */
00658   }
00659 
00660 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
00661 
00662 #if LWIP_IGMP
00663   /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
00664   if ((iphdr_hlen > IP_HLEN) &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
00665 #else
00666   if (iphdr_hlen > IP_HLEN) {
00667 #endif /* LWIP_IGMP */
00668     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
00669     pbuf_free(p);
00670     IP_STATS_INC(ip.opterr);
00671     IP_STATS_INC(ip.drop);
00672     /* unsupported protocol feature */
00673     MIB2_STATS_INC(mib2.ipinunknownprotos);
00674     return ERR_OK;
00675   }
00676 #endif /* IP_OPTIONS_ALLOWED == 0 */
00677 
00678   /* send to upper layers */
00679   LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
00680   ip4_debug_print(p);
00681   LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
00682 
00683   ip_data.current_netif = netif;
00684   ip_data.current_input_netif = inp;
00685   ip_data.current_ip4_header = iphdr;
00686   ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);
00687 
00688 #if LWIP_RAW
00689   /* raw input did not eat the packet? */
00690   raw_status = raw_input(p, inp);
00691   if (raw_status != RAW_INPUT_EATEN)
00692 #endif /* LWIP_RAW */
00693   {
00694     pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */
00695 
00696     switch (IPH_PROTO(iphdr)) {
00697 #if LWIP_UDP
00698       case IP_PROTO_UDP:
00699 #if LWIP_UDPLITE
00700       case IP_PROTO_UDPLITE:
00701 #endif /* LWIP_UDPLITE */
00702         MIB2_STATS_INC(mib2.ipindelivers);
00703         udp_input(p, inp);
00704         break;
00705 #endif /* LWIP_UDP */
00706 #if LWIP_TCP
00707       case IP_PROTO_TCP:
00708         MIB2_STATS_INC(mib2.ipindelivers);
00709         tcp_input(p, inp);
00710         break;
00711 #endif /* LWIP_TCP */
00712 #if LWIP_ICMP
00713       case IP_PROTO_ICMP:
00714         MIB2_STATS_INC(mib2.ipindelivers);
00715         icmp_input(p, inp);
00716         break;
00717 #endif /* LWIP_ICMP */
00718 #if LWIP_IGMP
00719       case IP_PROTO_IGMP:
00720         igmp_input(p, inp, ip4_current_dest_addr());
00721         break;
00722 #endif /* LWIP_IGMP */
00723       default:
00724 #if LWIP_RAW
00725         if (raw_status == RAW_INPUT_DELIVERED) {
00726           MIB2_STATS_INC(mib2.ipindelivers);
00727         } else
00728 #endif /* LWIP_RAW */
00729         {
00730 #if LWIP_ICMP
00731           /* send ICMP destination protocol unreachable unless is was a broadcast */
00732           if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
00733               !ip4_addr_ismulticast(ip4_current_dest_addr())) {
00734             pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */
00735             icmp_dest_unreach(p, ICMP_DUR_PROTO);
00736           }
00737 #endif /* LWIP_ICMP */
00738 
00739           LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
00740 
00741           IP_STATS_INC(ip.proterr);
00742           IP_STATS_INC(ip.drop);
00743           MIB2_STATS_INC(mib2.ipinunknownprotos);
00744         }
00745         pbuf_free(p);
00746         break;
00747     }
00748   }
00749 
00750   /* @todo: this is not really necessary... */
00751   ip_data.current_netif = NULL;
00752   ip_data.current_input_netif = NULL;
00753   ip_data.current_ip4_header = NULL;
00754   ip_data.current_ip_header_tot_len = 0;
00755   ip4_addr_set_any(ip4_current_src_addr());
00756   ip4_addr_set_any(ip4_current_dest_addr());
00757 
00758   return ERR_OK;
00759 }
00760 
00761 /**
00762  * Sends an IP packet on a network interface. This function constructs
00763  * the IP header and calculates the IP header checksum. If the source
00764  * IP address is NULL, the IP address of the outgoing network
00765  * interface is filled in as source address.
00766  * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
00767  * include an IP header and p->payload points to it instead of the data.
00768  *
00769  * @param p the packet to send (p->payload points to the data, e.g. next
00770             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
00771             IP header and p->payload points to that IP header)
00772  * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
00773  *         IP  address of the netif used to send is used as source address)
00774  * @param dest the destination IP address to send the packet to
00775  * @param ttl the TTL value to be set in the IP header
00776  * @param tos the TOS value to be set in the IP header
00777  * @param proto the PROTOCOL to be set in the IP header
00778  * @param netif the netif on which to send this packet
00779  * @return ERR_OK if the packet was sent OK
00780  *         ERR_BUF if p doesn't have enough space for IP/LINK headers
00781  *         returns errors returned by netif->output
00782  *
00783  * @note ip_id: RFC791 "some host may be able to simply use
00784  *  unique identifiers independent of destination"
00785  */
00786 err_t
00787 ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
00788               u8_t ttl, u8_t tos,
00789               u8_t proto, struct netif *netif)
00790 {
00791 #if IP_OPTIONS_SEND
00792   return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
00793 }
00794 
00795 /**
00796  * Same as ip_output_if() but with the possibility to include IP options:
00797  *
00798  * @ param ip_options pointer to the IP options, copied into the IP header
00799  * @ param optlen length of ip_options
00800  */
00801 err_t
00802 ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
00803                   u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
00804                   u16_t optlen)
00805 {
00806 #endif /* IP_OPTIONS_SEND */
00807   const ip4_addr_t *src_used = src;
00808   if (dest != LWIP_IP_HDRINCL) {
00809     if (ip4_addr_isany(src)) {
00810       src_used = netif_ip4_addr(netif);
00811     }
00812   }
00813 
00814 #if IP_OPTIONS_SEND
00815   return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
00816                                ip_options, optlen);
00817 #else /* IP_OPTIONS_SEND */
00818   return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
00819 #endif /* IP_OPTIONS_SEND */
00820 }
00821 
00822 /**
00823  * Same as ip_output_if() but 'src' address is not replaced by netif address
00824  * when it is 'any'.
00825  */
00826 err_t
00827 ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
00828                   u8_t ttl, u8_t tos,
00829                   u8_t proto, struct netif *netif)
00830 {
00831 #if IP_OPTIONS_SEND
00832   return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
00833 }
00834 
00835 /**
00836  * Same as ip_output_if_opt() but 'src' address is not replaced by netif address
00837  * when it is 'any'.
00838  */
00839 err_t
00840 ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
00841                       u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
00842                       u16_t optlen)
00843 {
00844 #endif /* IP_OPTIONS_SEND */
00845   struct ip_hdr *iphdr;
00846   ip4_addr_t dest_addr;
00847 #if CHECKSUM_GEN_IP_INLINE
00848   u32_t chk_sum = 0;
00849 #endif /* CHECKSUM_GEN_IP_INLINE */
00850 
00851   LWIP_ASSERT_CORE_LOCKED();
00852   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
00853 
00854   MIB2_STATS_INC(mib2.ipoutrequests);
00855 
00856   /* Should the IP header be generated or is it already included in p? */
00857   if (dest != LWIP_IP_HDRINCL) {
00858     u16_t ip_hlen = IP_HLEN;
00859 #if IP_OPTIONS_SEND
00860     u16_t optlen_aligned = 0;
00861     if (optlen != 0) {
00862 #if CHECKSUM_GEN_IP_INLINE
00863       int i;
00864 #endif /* CHECKSUM_GEN_IP_INLINE */
00865       if (optlen > (IP_HLEN_MAX - IP_HLEN)) {
00866         /* optlen too long */
00867         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));
00868         IP_STATS_INC(ip.err);
00869         MIB2_STATS_INC(mib2.ipoutdiscards);
00870         return ERR_VAL;
00871       }
00872       /* round up to a multiple of 4 */
00873       optlen_aligned = (u16_t)((optlen + 3) & ~3);
00874       ip_hlen = (u16_t)(ip_hlen + optlen_aligned);
00875       /* First write in the IP options */
00876       if (pbuf_add_header(p, optlen_aligned)) {
00877         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
00878         IP_STATS_INC(ip.err);
00879         MIB2_STATS_INC(mib2.ipoutdiscards);
00880         return ERR_BUF;
00881       }
00882       MEMCPY(p->payload, ip_options, optlen);
00883       if (optlen < optlen_aligned) {
00884         /* zero the remaining bytes */
00885         memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));
00886       }
00887 #if CHECKSUM_GEN_IP_INLINE
00888       for (i = 0; i < optlen_aligned / 2; i++) {
00889         chk_sum += ((u16_t *)p->payload)[i];
00890       }
00891 #endif /* CHECKSUM_GEN_IP_INLINE */
00892     }
00893 #endif /* IP_OPTIONS_SEND */
00894     /* generate IP header */
00895     if (pbuf_add_header(p, IP_HLEN)) {
00896       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
00897 
00898       IP_STATS_INC(ip.err);
00899       MIB2_STATS_INC(mib2.ipoutdiscards);
00900       return ERR_BUF;
00901     }
00902 
00903     iphdr = (struct ip_hdr *)p->payload;
00904     LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
00905                 (p->len >= sizeof(struct ip_hdr)));
00906 
00907     IPH_TTL_SET(iphdr, ttl);
00908     IPH_PROTO_SET(iphdr, proto);
00909 #if CHECKSUM_GEN_IP_INLINE
00910     chk_sum += PP_NTOHS(proto | (ttl << 8));
00911 #endif /* CHECKSUM_GEN_IP_INLINE */
00912 
00913     /* dest cannot be NULL here */
00914     ip4_addr_copy(iphdr->dest, *dest);
00915 #if CHECKSUM_GEN_IP_INLINE
00916     chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
00917     chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
00918 #endif /* CHECKSUM_GEN_IP_INLINE */
00919 
00920     IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
00921     IPH_TOS_SET(iphdr, tos);
00922 #if CHECKSUM_GEN_IP_INLINE
00923     chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
00924 #endif /* CHECKSUM_GEN_IP_INLINE */
00925     IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
00926 #if CHECKSUM_GEN_IP_INLINE
00927     chk_sum += iphdr->_len;
00928 #endif /* CHECKSUM_GEN_IP_INLINE */
00929     IPH_OFFSET_SET(iphdr, 0);
00930     IPH_ID_SET(iphdr, lwip_htons(ip_id));
00931 #if CHECKSUM_GEN_IP_INLINE
00932     chk_sum += iphdr->_id;
00933 #endif /* CHECKSUM_GEN_IP_INLINE */
00934     ++ip_id;
00935 
00936     if (src == NULL) {
00937       ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
00938     } else {
00939       /* src cannot be NULL here */
00940       ip4_addr_copy(iphdr->src, *src);
00941     }
00942 
00943 #if CHECKSUM_GEN_IP_INLINE
00944     chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
00945     chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
00946     chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
00947     chk_sum = (chk_sum >> 16) + chk_sum;
00948     chk_sum = ~chk_sum;
00949     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
00950       iphdr->_chksum = (u16_t)chk_sum; /* network order */
00951     }
00952 #if LWIP_CHECKSUM_CTRL_PER_NETIF
00953     else {
00954       IPH_CHKSUM_SET(iphdr, 0);
00955     }
00956 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
00957 #else /* CHECKSUM_GEN_IP_INLINE */
00958     IPH_CHKSUM_SET(iphdr, 0);
00959 #if CHECKSUM_GEN_IP
00960     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
00961       IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
00962     }
00963 #endif /* CHECKSUM_GEN_IP */
00964 #endif /* CHECKSUM_GEN_IP_INLINE */
00965   } else {
00966     /* IP header already included in p */
00967     if (p->len < IP_HLEN) {
00968       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));
00969       IP_STATS_INC(ip.err);
00970       MIB2_STATS_INC(mib2.ipoutdiscards);
00971       return ERR_BUF;
00972     }
00973     iphdr = (struct ip_hdr *)p->payload;
00974     ip4_addr_copy(dest_addr, iphdr->dest);
00975     dest = &dest_addr;
00976   }
00977 
00978   IP_STATS_INC(ip.xmit);
00979 
00980   LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
00981   ip4_debug_print(p);
00982 
00983 #if ENABLE_LOOPBACK
00984   if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
00985 #if !LWIP_HAVE_LOOPIF
00986       || ip4_addr_isloopback(dest)
00987 #endif /* !LWIP_HAVE_LOOPIF */
00988      ) {
00989     /* Packet to self, enqueue it for loopback */
00990     LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
00991     return netif_loop_output(netif, p);
00992   }
00993 #if LWIP_MULTICAST_TX_OPTIONS
00994   if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
00995     netif_loop_output(netif, p);
00996   }
00997 #endif /* LWIP_MULTICAST_TX_OPTIONS */
00998 #endif /* ENABLE_LOOPBACK */
00999 #if IP_FRAG
01000   /* don't fragment if interface has mtu set to 0 [loopif] */
01001   if (netif->mtu && (p->tot_len > netif->mtu)) {
01002     return ip4_frag(p, netif, dest);
01003   }
01004 #endif /* IP_FRAG */
01005 
01006   LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
01007   return netif->output(netif, p, dest);
01008 }
01009 
01010 /**
01011  * Simple interface to ip_output_if. It finds the outgoing network
01012  * interface and calls upon ip_output_if to do the actual work.
01013  *
01014  * @param p the packet to send (p->payload points to the data, e.g. next
01015             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
01016             IP header and p->payload points to that IP header)
01017  * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
01018  *         IP  address of the netif used to send is used as source address)
01019  * @param dest the destination IP address to send the packet to
01020  * @param ttl the TTL value to be set in the IP header
01021  * @param tos the TOS value to be set in the IP header
01022  * @param proto the PROTOCOL to be set in the IP header
01023  *
01024  * @return ERR_RTE if no route is found
01025  *         see ip_output_if() for more return values
01026  */
01027 err_t
01028 ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
01029            u8_t ttl, u8_t tos, u8_t proto)
01030 {
01031   struct netif *netif;
01032 
01033   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
01034 
01035   if ((netif = ip4_route_src(src, dest)) == NULL) {
01036     LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
01037                            ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
01038     IP_STATS_INC(ip.rterr);
01039     return ERR_RTE;
01040   }
01041 
01042   return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
01043 }
01044 
01045 #if LWIP_NETIF_USE_HINTS
01046 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
01047  *  before calling ip_output_if.
01048  *
01049  * @param p the packet to send (p->payload points to the data, e.g. next
01050             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
01051             IP header and p->payload points to that IP header)
01052  * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
01053  *         IP  address of the netif used to send is used as source address)
01054  * @param dest the destination IP address to send the packet to
01055  * @param ttl the TTL value to be set in the IP header
01056  * @param tos the TOS value to be set in the IP header
01057  * @param proto the PROTOCOL to be set in the IP header
01058  * @param netif_hint netif output hint pointer set to netif->hint before
01059  *        calling ip_output_if()
01060  *
01061  * @return ERR_RTE if no route is found
01062  *         see ip_output_if() for more return values
01063  */
01064 err_t
01065 ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
01066                   u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
01067 {
01068   struct netif *netif;
01069   err_t err;
01070 
01071   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
01072 
01073   if ((netif = ip4_route_src(src, dest)) == NULL) {
01074     LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
01075                            ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
01076     IP_STATS_INC(ip.rterr);
01077     return ERR_RTE;
01078   }
01079 
01080   NETIF_SET_HINTS(netif, netif_hint);
01081   err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
01082   NETIF_RESET_HINTS(netif);
01083 
01084   return err;
01085 }
01086 #endif /* LWIP_NETIF_USE_HINTS*/
01087 
01088 #if IP_DEBUG
01089 /* Print an IP header by using LWIP_DEBUGF
01090  * @param p an IP packet, p->payload pointing to the IP header
01091  */
01092 void
01093 ip4_debug_print(struct pbuf *p)
01094 {
01095   struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
01096 
01097   TRACE_TO_ASCII_HEX_DUMPF("IP>", lwip_ntohs(IPH_LEN(iphdr)), (char *) iphdr);
01098 
01099   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
01100   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01101   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
01102                          (u16_t)IPH_V(iphdr),
01103                          (u16_t)IPH_HL(iphdr),
01104                          (u16_t)IPH_TOS(iphdr),
01105                          lwip_ntohs(IPH_LEN(iphdr))));
01106   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01107   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
01108                          lwip_ntohs(IPH_ID(iphdr)),
01109                          (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
01110                          (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
01111                          (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
01112                          (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
01113   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01114   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
01115                          (u16_t)IPH_TTL(iphdr),
01116                          (u16_t)IPH_PROTO(iphdr),
01117                          lwip_ntohs(IPH_CHKSUM(iphdr))));
01118   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01119   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
01120                          ip4_addr1_16_val(iphdr->src),
01121                          ip4_addr2_16_val(iphdr->src),
01122                          ip4_addr3_16_val(iphdr->src),
01123                          ip4_addr4_16_val(iphdr->src)));
01124   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01125   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
01126                          ip4_addr1_16_val(iphdr->dest),
01127                          ip4_addr2_16_val(iphdr->dest),
01128                          ip4_addr3_16_val(iphdr->dest),
01129                          ip4_addr4_16_val(iphdr->dest)));
01130   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
01131 }
01132 #endif /* IP_DEBUG */
01133 
01134 #endif /* LWIP_IPV4 */