Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/core/ipv4/ip.c need to be fixed.

0001 /**
0002  * @file
0003  * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
0004  * 
0005  * @see ip_frag.c
0006  *
0007  */
0008 
0009 /*
0010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0011  * All rights reserved.
0012  *
0013  * Redistribution and use in source and binary forms, with or without modification,
0014  * are permitted provided that the following conditions are met:
0015  *
0016  * 1. Redistributions of source code must retain the above copyright notice,
0017  *    this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright notice,
0019  *    this list of conditions and the following disclaimer in the documentation
0020  *    and/or other materials provided with the distribution.
0021  * 3. The name of the author may not be used to endorse or promote products
0022  *    derived from this software without specific prior written permission.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
0027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
0032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
0033  * OF SUCH DAMAGE.
0034  *
0035  * This file is part of the lwIP TCP/IP stack.
0036  *
0037  * Author: Adam Dunkels <adam@sics.se>
0038  *
0039  */
0040 
0041 #include "lwip/opt.h"
0042 #include "lwip/ip.h"
0043 #include "lwip/def.h"
0044 #include "lwip/mem.h"
0045 #include "lwip/ip_frag.h"
0046 #include "lwip/inet.h"
0047 #include "lwip/inet_chksum.h"
0048 #include "lwip/netif.h"
0049 #include "lwip/icmp.h"
0050 #include "lwip/igmp.h"
0051 #include "lwip/raw.h"
0052 #include "lwip/udp.h"
0053 #include "lwip/tcp.h"
0054 #include "lwip/snmp.h"
0055 #include "lwip/dhcp.h"
0056 #include "lwip/stats.h"
0057 #include "arch/perf.h"
0058 
0059 #include <string.h>
0060 
0061 /**
0062  * The interface that provided the packet for the current callback
0063  * invocation.
0064  */
0065 struct netif *current_netif;
0066 
0067 /**
0068  * Header of the input packet currently being processed.
0069  */
0070 const struct ip_hdr *current_header;
0071 
0072 /**
0073  * Finds the appropriate network interface for a given IP address. It
0074  * searches the list of network interfaces linearly. A match is found
0075  * if the masked IP address of the network interface equals the masked
0076  * IP address given to the function.
0077  *
0078  * @param dest the destination IP address for which to find the route
0079  * @return the netif on which to send to reach dest
0080  */
0081 struct netif *
0082 ip_route(struct ip_addr *dest)
0083 {
0084   struct netif *netif;
0085 
0086   /* iterate through netifs */
0087   for(netif = netif_list; netif != NULL; netif = netif->next) {
0088     /* network mask matches? */
0089     if (netif_is_up(netif)) {
0090       if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
0091         /* return netif on which to forward IP packet */
0092         return netif;
0093       }
0094     }
0095   }
0096   if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
0097     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));
0098     IP_STATS_INC(ip.rterr);
0099     snmp_inc_ipoutnoroutes();
0100     return NULL;
0101   }
0102   /* no matching netif found, use default netif */
0103   return netif_default;
0104 }
0105 
0106 #if IP_FORWARD
0107 /**
0108  * Forwards an IP packet. It finds an appropriate route for the
0109  * packet, decrements the TTL value of the packet, adjusts the
0110  * checksum and outputs the packet on the appropriate interface.
0111  *
0112  * @param p the packet to forward (p->payload points to IP header)
0113  * @param iphdr the IP header of the input packet
0114  * @param inp the netif on which this packet was received
0115  * @return the netif on which the packet was sent (NULL if it wasn't sent)
0116  */
0117 static struct netif *
0118 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
0119 {
0120   struct netif *netif;
0121 
0122   PERF_START;
0123   /* Find network interface where to forward this IP packet to. */
0124   netif = ip_route((struct ip_addr *)&(iphdr->dest));
0125   if (netif == NULL) {
0126     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
0127                       iphdr->dest.addr));
0128     snmp_inc_ipoutnoroutes();
0129     return (struct netif *)NULL;
0130   }
0131   /* Do not forward packets onto the same network interface on which
0132    * they arrived. */
0133   if (netif == inp) {
0134     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
0135     snmp_inc_ipoutnoroutes();
0136     return (struct netif *)NULL;
0137   }
0138 
0139   /* decrement TTL */
0140   IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
0141   /* send ICMP if TTL == 0 */
0142   if (IPH_TTL(iphdr) == 0) {
0143     snmp_inc_ipinhdrerrors();
0144 #if LWIP_ICMP
0145     /* Don't send ICMP messages in response to ICMP messages */
0146     if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
0147       icmp_time_exceeded(p, ICMP_TE_TTL);
0148     }
0149 #endif /* LWIP_ICMP */
0150     return (struct netif *)NULL;
0151   }
0152 
0153   /* Incrementally update the IP checksum. */
0154   if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
0155     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
0156   } else {
0157     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
0158   }
0159 
0160   LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
0161                     iphdr->dest.addr));
0162 
0163   IP_STATS_INC(ip.fw);
0164   IP_STATS_INC(ip.xmit);
0165   snmp_inc_ipforwdatagrams();
0166 
0167   PERF_STOP("ip_forward");
0168   /* transmit pbuf on chosen interface */
0169   netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
0170   return netif;
0171 }
0172 #endif /* IP_FORWARD */
0173 
0174 /**
0175  * This function is called by the network interface device driver when
0176  * an IP packet is received. The function does the basic checks of the
0177  * IP header such as packet size being at least larger than the header
0178  * size etc. If the packet was not destined for us, the packet is
0179  * forwarded (using ip_forward). The IP checksum is always checked.
0180  *
0181  * Finally, the packet is sent to the upper layer protocol input function.
0182  * 
0183  * @param p the received IP packet (p->payload points to IP header)
0184  * @param inp the netif on which this packet was received
0185  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
0186  *         processed, but currently always returns ERR_OK)
0187  */
0188 err_t
0189 ip_input(struct pbuf *p, struct netif *inp)
0190 {
0191   struct ip_hdr *iphdr;
0192   struct netif *netif;
0193   u16_t iphdr_hlen;
0194   u16_t iphdr_len;
0195 #if LWIP_DHCP
0196   int check_ip_src=1;
0197 #endif /* LWIP_DHCP */
0198 
0199   IP_STATS_INC(ip.recv);
0200   snmp_inc_ipinreceives();
0201 
0202   /* identify the IP header */
0203   iphdr = p->payload;
0204   if (IPH_V(iphdr) != 4) {
0205     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
0206     ip_debug_print(p);
0207     pbuf_free(p);
0208     IP_STATS_INC(ip.err);
0209     IP_STATS_INC(ip.drop);
0210     snmp_inc_ipinhdrerrors();
0211     return ERR_OK;
0212   }
0213 
0214   /* obtain IP header length in number of 32-bit words */
0215   iphdr_hlen = IPH_HL(iphdr);
0216   /* calculate IP header length in bytes */
0217   iphdr_hlen *= 4;
0218   /* obtain ip length in bytes */
0219   iphdr_len = ntohs(IPH_LEN(iphdr));
0220 
0221   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
0222   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
0223     if (iphdr_hlen > p->len) {
0224       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
0225         ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
0226         iphdr_hlen, p->len));
0227     }
0228     if (iphdr_len > p->tot_len) {
0229       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
0230         ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
0231         iphdr_len, p->tot_len));
0232     }
0233     /* free (drop) packet pbufs */
0234     pbuf_free(p);
0235     IP_STATS_INC(ip.lenerr);
0236     IP_STATS_INC(ip.drop);
0237     snmp_inc_ipindiscards();
0238     return ERR_OK;
0239   }
0240 
0241   /* verify checksum */
0242 #if CHECKSUM_CHECK_IP
0243   if (inet_chksum(iphdr, iphdr_hlen) != 0) {
0244 
0245     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
0246       ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
0247     ip_debug_print(p);
0248     pbuf_free(p);
0249     IP_STATS_INC(ip.chkerr);
0250     IP_STATS_INC(ip.drop);
0251     snmp_inc_ipinhdrerrors();
0252     return ERR_OK;
0253   }
0254 #endif
0255 
0256   /* Trim pbuf. This should have been done at the netif layer,
0257    * but we'll do it anyway just to be sure that its done. */
0258   pbuf_realloc(p, iphdr_len);
0259 
0260   /* match packet against an interface, i.e. is this packet for us? */
0261 #if LWIP_IGMP
0262   if (ip_addr_ismulticast(&(iphdr->dest))) {
0263     if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {
0264       netif = inp;
0265     } else {
0266       netif = NULL;
0267     }
0268   } else
0269 #endif /* LWIP_IGMP */
0270   {
0271     /* start trying with inp. if that's not acceptable, start walking the
0272        list of configured netifs.
0273        'first' is used as a boolean to mark whether we started walking the list */
0274     int first = 1;
0275     netif = inp;
0276     do {
0277       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",
0278           iphdr->dest.addr, netif->ip_addr.addr,
0279           iphdr->dest.addr & netif->netmask.addr,
0280           netif->ip_addr.addr & netif->netmask.addr,
0281           iphdr->dest.addr & ~(netif->netmask.addr)));
0282 
0283       /* interface is up and configured? */
0284       if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
0285         /* unicast to this interface address? */
0286         if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
0287             /* or broadcast on this interface network address? */
0288             ip_addr_isbroadcast(&(iphdr->dest), netif)) {
0289           LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
0290               netif->name[0], netif->name[1]));
0291           /* break out of for loop */
0292           break;
0293         }
0294       }
0295       if (first) {
0296         first = 0;
0297         netif = netif_list;
0298       } else {
0299         netif = netif->next;
0300       }
0301       if (netif == inp) {
0302         netif = netif->next;
0303       }
0304     } while(netif != NULL);
0305   }
0306 
0307 #if LWIP_DHCP
0308   /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
0309    * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
0310    * According to RFC 1542 section 3.1.1, referred by RFC 2131).
0311    */
0312   if (netif == NULL) {
0313     /* remote port is DHCP server? */
0314     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
0315       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
0316         ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
0317       if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {
0318         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
0319         netif = inp;
0320         check_ip_src = 0;
0321       }
0322     }
0323   }
0324 #endif /* LWIP_DHCP */
0325 
0326   /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
0327 #if LWIP_DHCP
0328   /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
0329   if (check_ip_src && (iphdr->src.addr != 0))
0330 #endif /* LWIP_DHCP */
0331   {  if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
0332          (ip_addr_ismulticast(&(iphdr->src)))) {
0333       /* packet source is not valid */
0334       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
0335       /* free (drop) packet pbufs */
0336       pbuf_free(p);
0337       IP_STATS_INC(ip.drop);
0338       snmp_inc_ipinaddrerrors();
0339       snmp_inc_ipindiscards();
0340       return ERR_OK;
0341     }
0342   }
0343 
0344   /* packet not for us? */
0345   if (netif == NULL) {
0346     /* packet not for us, route or discard */
0347     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
0348 #if IP_FORWARD
0349     /* non-broadcast packet? */
0350     if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
0351       /* try to forward IP packet on (other) interfaces */
0352       ip_forward(p, iphdr, inp);
0353     } else
0354 #endif /* IP_FORWARD */
0355     {
0356       snmp_inc_ipinaddrerrors();
0357       snmp_inc_ipindiscards();
0358     }
0359     pbuf_free(p);
0360     return ERR_OK;
0361   }
0362   /* packet consists of multiple fragments? */
0363   if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
0364 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
0365     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 ip_reass()\n",
0366       ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
0367     /* reassemble the packet*/
0368     p = ip_reass(p);
0369     /* packet not fully reassembled yet? */
0370     if (p == NULL) {
0371       return ERR_OK;
0372     }
0373     iphdr = p->payload;
0374 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
0375     pbuf_free(p);
0376     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
0377       ntohs(IPH_OFFSET(iphdr))));
0378     IP_STATS_INC(ip.opterr);
0379     IP_STATS_INC(ip.drop);
0380     /* unsupported protocol feature */
0381     snmp_inc_ipinunknownprotos();
0382     return ERR_OK;
0383 #endif /* IP_REASSEMBLY */
0384   }
0385 
0386 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
0387 
0388 #if LWIP_IGMP
0389   /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
0390   if((iphdr_hlen > IP_HLEN &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
0391 #else
0392   if (iphdr_hlen > IP_HLEN) {
0393 #endif /* LWIP_IGMP */
0394     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
0395     pbuf_free(p);
0396     IP_STATS_INC(ip.opterr);
0397     IP_STATS_INC(ip.drop);
0398     /* unsupported protocol feature */
0399     snmp_inc_ipinunknownprotos();
0400     return ERR_OK;
0401   }
0402 #endif /* IP_OPTIONS_ALLOWED == 0 */
0403 
0404   /* send to upper layers */
0405   LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
0406   ip_debug_print(p);
0407   LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
0408 
0409   current_netif = inp;
0410   current_header = iphdr;
0411 
0412 #if LWIP_RAW
0413   /* raw input did not eat the packet? */
0414   if (raw_input(p, inp) == 0)
0415 #endif /* LWIP_RAW */
0416   {
0417 
0418     switch (IPH_PROTO(iphdr)) {
0419 #if LWIP_UDP
0420     case IP_PROTO_UDP:
0421 #if LWIP_UDPLITE
0422     case IP_PROTO_UDPLITE:
0423 #endif /* LWIP_UDPLITE */
0424       snmp_inc_ipindelivers();
0425       udp_input(p, inp);
0426       break;
0427 #endif /* LWIP_UDP */
0428 #if LWIP_TCP
0429     case IP_PROTO_TCP:
0430       snmp_inc_ipindelivers();
0431       tcp_input(p, inp);
0432       break;
0433 #endif /* LWIP_TCP */
0434 #if LWIP_ICMP
0435     case IP_PROTO_ICMP:
0436       snmp_inc_ipindelivers();
0437       icmp_input(p, inp);
0438       break;
0439 #endif /* LWIP_ICMP */
0440 #if LWIP_IGMP
0441     case IP_PROTO_IGMP:
0442       igmp_input(p,inp,&(iphdr->dest));
0443       break;
0444 #endif /* LWIP_IGMP */
0445     default:
0446 #if LWIP_ICMP
0447       /* send ICMP destination protocol unreachable unless is was a broadcast */
0448       if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
0449           !ip_addr_ismulticast(&(iphdr->dest))) {
0450         p->payload = iphdr;
0451         icmp_dest_unreach(p, ICMP_DUR_PROTO);
0452       }
0453 #endif /* LWIP_ICMP */
0454       pbuf_free(p);
0455 
0456       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
0457 
0458       IP_STATS_INC(ip.proterr);
0459       IP_STATS_INC(ip.drop);
0460       snmp_inc_ipinunknownprotos();
0461     }
0462   }
0463 
0464   current_netif = NULL;
0465   current_header = NULL;
0466 
0467   return ERR_OK;
0468 }
0469 
0470 /**
0471  * Sends an IP packet on a network interface. This function constructs
0472  * the IP header and calculates the IP header checksum. If the source
0473  * IP address is NULL, the IP address of the outgoing network
0474  * interface is filled in as source address.
0475  * If the destination IP address is IP_HDRINCL, p is assumed to already
0476  * include an IP header and p->payload points to it instead of the data.
0477  *
0478  * @param p the packet to send (p->payload points to the data, e.g. next
0479             protocol header; if dest == IP_HDRINCL, p already includes an IP
0480             header and p->payload points to that IP header)
0481  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
0482  *         IP  address of the netif used to send is used as source address)
0483  * @param dest the destination IP address to send the packet to
0484  * @param ttl the TTL value to be set in the IP header
0485  * @param tos the TOS value to be set in the IP header
0486  * @param proto the PROTOCOL to be set in the IP header
0487  * @param netif the netif on which to send this packet
0488  * @return ERR_OK if the packet was sent OK
0489  *         ERR_BUF if p doesn't have enough space for IP/LINK headers
0490  *         returns errors returned by netif->output
0491  *
0492  * @note ip_id: RFC791 "some host may be able to simply use
0493  *  unique identifiers independent of destination"
0494  */
0495 err_t
0496 ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
0497              u8_t ttl, u8_t tos,
0498              u8_t proto, struct netif *netif)
0499 {
0500 #if IP_OPTIONS_SEND
0501   return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
0502 }
0503 
0504 /**
0505  * Same as ip_output_if() but with the possibility to include IP options:
0506  *
0507  * @ param ip_options pointer to the IP options, copied into the IP header
0508  * @ param optlen length of ip_options
0509  */
0510 err_t ip_output_if_opt(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
0511        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
0512        u16_t optlen)
0513 {
0514 #endif /* IP_OPTIONS_SEND */
0515   struct ip_hdr *iphdr;
0516   static u16_t ip_id = 0;
0517 
0518   snmp_inc_ipoutrequests();
0519 
0520   /* Should the IP header be generated or is it already included in p? */
0521   if (dest != IP_HDRINCL) {
0522     u16_t ip_hlen = IP_HLEN;
0523 #if IP_OPTIONS_SEND
0524     u16_t optlen_aligned = 0;
0525     if (optlen != 0) {
0526       /* round up to a multiple of 4 */
0527       optlen_aligned = ((optlen + 3) & ~3);
0528       ip_hlen += optlen_aligned;
0529       /* First write in the IP options */
0530       if (pbuf_header(p, optlen_aligned)) {
0531         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
0532         IP_STATS_INC(ip.err);
0533         snmp_inc_ipoutdiscards();
0534         return ERR_BUF;
0535       }
0536       MEMCPY(p->payload, ip_options, optlen);
0537       if (optlen < optlen_aligned) {
0538         /* zero the remaining bytes */
0539         memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
0540       }
0541     }
0542 #endif /* IP_OPTIONS_SEND */
0543     /* generate IP header */
0544     if (pbuf_header(p, IP_HLEN)) {
0545       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
0546 
0547       IP_STATS_INC(ip.err);
0548       snmp_inc_ipoutdiscards();
0549       return ERR_BUF;
0550     }
0551 
0552     iphdr = p->payload;
0553     LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
0554                (p->len >= sizeof(struct ip_hdr)));
0555 
0556     IPH_TTL_SET(iphdr, ttl);
0557     IPH_PROTO_SET(iphdr, proto);
0558 
0559     ip_addr_set(&(iphdr->dest), dest);
0560 
0561     IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
0562     IPH_LEN_SET(iphdr, htons(p->tot_len));
0563     IPH_OFFSET_SET(iphdr, 0);
0564     IPH_ID_SET(iphdr, htons(ip_id));
0565     ++ip_id;
0566 
0567     if (ip_addr_isany(src)) {
0568       ip_addr_set(&(iphdr->src), &(netif->ip_addr));
0569     } else {
0570       ip_addr_set(&(iphdr->src), src);
0571     }
0572 
0573     IPH_CHKSUM_SET(iphdr, 0);
0574 #if CHECKSUM_GEN_IP
0575     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
0576 #endif
0577   } else {
0578     /* IP header already included in p */
0579     iphdr = p->payload;
0580     dest = &(iphdr->dest);
0581   }
0582 
0583   IP_STATS_INC(ip.xmit);
0584 
0585   LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
0586   ip_debug_print(p);
0587 
0588 #if ENABLE_LOOPBACK
0589   if (ip_addr_cmp(dest, &netif->ip_addr)) {
0590     /* Packet to self, enqueue it for loopback */
0591     LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
0592     return netif_loop_output(netif, p, dest);
0593   }
0594 #endif /* ENABLE_LOOPBACK */
0595 #if IP_FRAG
0596   /* don't fragment if interface has mtu set to 0 [loopif] */
0597   if (netif->mtu && (p->tot_len > netif->mtu)) {
0598     return ip_frag(p,netif,dest);
0599   }
0600 #endif
0601 
0602   LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
0603   return netif->output(netif, p, dest);
0604 }
0605 
0606 /**
0607  * Simple interface to ip_output_if. It finds the outgoing network
0608  * interface and calls upon ip_output_if to do the actual work.
0609  *
0610  * @param p the packet to send (p->payload points to the data, e.g. next
0611             protocol header; if dest == IP_HDRINCL, p already includes an IP
0612             header and p->payload points to that IP header)
0613  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
0614  *         IP  address of the netif used to send is used as source address)
0615  * @param dest the destination IP address to send the packet to
0616  * @param ttl the TTL value to be set in the IP header
0617  * @param tos the TOS value to be set in the IP header
0618  * @param proto the PROTOCOL to be set in the IP header
0619  *
0620  * @return ERR_RTE if no route is found
0621  *         see ip_output_if() for more return values
0622  */
0623 err_t
0624 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
0625           u8_t ttl, u8_t tos, u8_t proto)
0626 {
0627   struct netif *netif;
0628 
0629   if ((netif = ip_route(dest)) == NULL) {
0630     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
0631     IP_STATS_INC(ip.rterr);
0632     return ERR_RTE;
0633   }
0634 
0635   return ip_output_if(p, src, dest, ttl, tos, proto, netif);
0636 }
0637 
0638 #if LWIP_NETIF_HWADDRHINT
0639 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
0640  *  before calling ip_output_if.
0641  *
0642  * @param p the packet to send (p->payload points to the data, e.g. next
0643             protocol header; if dest == IP_HDRINCL, p already includes an IP
0644             header and p->payload points to that IP header)
0645  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
0646  *         IP  address of the netif used to send is used as source address)
0647  * @param dest the destination IP address to send the packet to
0648  * @param ttl the TTL value to be set in the IP header
0649  * @param tos the TOS value to be set in the IP header
0650  * @param proto the PROTOCOL to be set in the IP header
0651  * @param addr_hint address hint pointer set to netif->addr_hint before
0652  *        calling ip_output_if()
0653  *
0654  * @return ERR_RTE if no route is found
0655  *         see ip_output_if() for more return values
0656  */
0657 err_t
0658 ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
0659           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
0660 {
0661   struct netif *netif;
0662   err_t err;
0663 
0664   if ((netif = ip_route(dest)) == NULL) {
0665     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
0666     IP_STATS_INC(ip.rterr);
0667     return ERR_RTE;
0668   }
0669 
0670   netif->addr_hint = addr_hint;
0671   err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
0672   netif->addr_hint = NULL;
0673 
0674   return err;
0675 }
0676 #endif /* LWIP_NETIF_HWADDRHINT*/
0677 
0678 #if IP_DEBUG
0679 /* Print an IP header by using LWIP_DEBUGF
0680  * @param p an IP packet, p->payload pointing to the IP header
0681  */
0682 void
0683 ip_debug_print(struct pbuf *p)
0684 {
0685   struct ip_hdr *iphdr = p->payload;
0686   u8_t *payload;
0687 
0688   payload = (u8_t *)iphdr + IP_HLEN;
0689 
0690   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
0691   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0692   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
0693                     IPH_V(iphdr),
0694                     IPH_HL(iphdr),
0695                     IPH_TOS(iphdr),
0696                     ntohs(IPH_LEN(iphdr))));
0697   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0698   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
0699                     ntohs(IPH_ID(iphdr)),
0700                     ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
0701                     ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
0702                     ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
0703                     ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
0704   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0705   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
0706                     IPH_TTL(iphdr),
0707                     IPH_PROTO(iphdr),
0708                     ntohs(IPH_CHKSUM(iphdr))));
0709   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0710   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
0711                     ip4_addr1(&iphdr->src),
0712                     ip4_addr2(&iphdr->src),
0713                     ip4_addr3(&iphdr->src),
0714                     ip4_addr4(&iphdr->src)));
0715   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0716   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
0717                     ip4_addr1(&iphdr->dest),
0718                     ip4_addr2(&iphdr->dest),
0719                     ip4_addr3(&iphdr->dest),
0720                     ip4_addr4(&iphdr->dest)));
0721   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
0722 }
0723 #endif /* IP_DEBUG */