SAMV71 Xplained Ultra Software Package 1.3

ip.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 #include "lwip/ip.h"
00043 #include "lwip/def.h"
00044 #include "lwip/mem.h"
00045 #include "lwip/ip_frag.h"
00046 #include "lwip/inet.h"
00047 #include "lwip/inet_chksum.h"
00048 #include "lwip/netif.h"
00049 #include "lwip/icmp.h"
00050 #include "lwip/igmp.h"
00051 #include "lwip/raw.h"
00052 #include "lwip/udp.h"
00053 #include "lwip/tcp.h"
00054 #include "lwip/snmp.h"
00055 #include "lwip/dhcp.h"
00056 #include "lwip/stats.h"
00057 #include "arch/perf.h"
00058 
00059 #include <string.h>
00060 
00061 /**
00062  * The interface that provided the packet for the current callback
00063  * invocation.
00064  */
00065 struct netif *current_netif;
00066 
00067 /**
00068  * Header of the input packet currently being processed.
00069  */
00070 const struct ip_hdr *current_header;
00071 
00072 /**
00073  * Finds the appropriate network interface for a given IP address. It
00074  * searches the list of network interfaces linearly. A match is found
00075  * if the masked IP address of the network interface equals the masked
00076  * IP address given to the function.
00077  *
00078  * @param dest the destination IP address for which to find the route
00079  * @return the netif on which to send to reach dest
00080  */
00081 struct netif *
00082 ip_route(struct ip_addr *dest)
00083 {
00084   struct netif *netif;
00085 
00086   /* iterate through netifs */
00087   for(netif = netif_list; netif != NULL; netif = netif->next) {
00088     /* network mask matches? */
00089     if (netif_is_up(netif)) {
00090       if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
00091         /* return netif on which to forward IP packet */
00092         return netif;
00093       }
00094     }
00095   }
00096   if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
00097     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));
00098     IP_STATS_INC(ip.rterr);
00099     snmp_inc_ipoutnoroutes();
00100     return NULL;
00101   }
00102   /* no matching netif found, use default netif */
00103   return netif_default;
00104 }
00105 
00106 #if IP_FORWARD
00107 /**
00108  * Forwards an IP packet. It finds an appropriate route for the
00109  * packet, decrements the TTL value of the packet, adjusts the
00110  * checksum and outputs the packet on the appropriate interface.
00111  *
00112  * @param p the packet to forward (p->payload points to IP header)
00113  * @param iphdr the IP header of the input packet
00114  * @param inp the netif on which this packet was received
00115  * @return the netif on which the packet was sent (NULL if it wasn't sent)
00116  */
00117 static struct netif *
00118 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
00119 {
00120   struct netif *netif;
00121 
00122   PERF_START;
00123   /* Find network interface where to forward this IP packet to. */
00124   netif = ip_route((struct ip_addr *)&(iphdr->dest));
00125   if (netif == NULL) {
00126     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
00127                       iphdr->dest.addr));
00128     snmp_inc_ipoutnoroutes();
00129     return (struct netif *)NULL;
00130   }
00131   /* Do not forward packets onto the same network interface on which
00132    * they arrived. */
00133   if (netif == inp) {
00134     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
00135     snmp_inc_ipoutnoroutes();
00136     return (struct netif *)NULL;
00137   }
00138 
00139   /* decrement TTL */
00140   IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
00141   /* send ICMP if TTL == 0 */
00142   if (IPH_TTL(iphdr) == 0) {
00143     snmp_inc_ipinhdrerrors();
00144 #if LWIP_ICMP
00145     /* Don't send ICMP messages in response to ICMP messages */
00146     if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
00147       icmp_time_exceeded(p, ICMP_TE_TTL);
00148     }
00149 #endif /* LWIP_ICMP */
00150     return (struct netif *)NULL;
00151   }
00152 
00153   /* Incrementally update the IP checksum. */
00154   if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
00155     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
00156   } else {
00157     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
00158   }
00159 
00160   LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
00161                     iphdr->dest.addr));
00162 
00163   IP_STATS_INC(ip.fw);
00164   IP_STATS_INC(ip.xmit);
00165   snmp_inc_ipforwdatagrams();
00166 
00167   PERF_STOP("ip_forward");
00168   /* transmit pbuf on chosen interface */
00169   netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
00170   return netif;
00171 }
00172 #endif /* IP_FORWARD */
00173 
00174 /**
00175  * This function is called by the network interface device driver when
00176  * an IP packet is received. The function does the basic checks of the
00177  * IP header such as packet size being at least larger than the header
00178  * size etc. If the packet was not destined for us, the packet is
00179  * forwarded (using ip_forward). The IP checksum is always checked.
00180  *
00181  * Finally, the packet is sent to the upper layer protocol input function.
00182  * 
00183  * @param p the received IP packet (p->payload points to IP header)
00184  * @param inp the netif on which this packet was received
00185  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
00186  *         processed, but currently always returns ERR_OK)
00187  */
00188 err_t
00189 ip_input(struct pbuf *p, struct netif *inp)
00190 {
00191   struct ip_hdr *iphdr;
00192   struct netif *netif;
00193   u16_t iphdr_hlen;
00194   u16_t iphdr_len;
00195 #if LWIP_DHCP
00196   int check_ip_src=1;
00197 #endif /* LWIP_DHCP */
00198 
00199   IP_STATS_INC(ip.recv);
00200   snmp_inc_ipinreceives();
00201 
00202   /* identify the IP header */
00203   iphdr = p->payload;
00204   if (IPH_V(iphdr) != 4) {
00205     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
00206     ip_debug_print(p);
00207     pbuf_free(p);
00208     IP_STATS_INC(ip.err);
00209     IP_STATS_INC(ip.drop);
00210     snmp_inc_ipinhdrerrors();
00211     return ERR_OK;
00212   }
00213 
00214   /* obtain IP header length in number of 32-bit words */
00215   iphdr_hlen = IPH_HL(iphdr);
00216   /* calculate IP header length in bytes */
00217   iphdr_hlen *= 4;
00218   /* obtain ip length in bytes */
00219   iphdr_len = ntohs(IPH_LEN(iphdr));
00220 
00221   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
00222   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
00223     if (iphdr_hlen > p->len) {
00224       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00225         ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
00226         iphdr_hlen, p->len));
00227     }
00228     if (iphdr_len > p->tot_len) {
00229       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00230         ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
00231         iphdr_len, p->tot_len));
00232     }
00233     /* free (drop) packet pbufs */
00234     pbuf_free(p);
00235     IP_STATS_INC(ip.lenerr);
00236     IP_STATS_INC(ip.drop);
00237     snmp_inc_ipindiscards();
00238     return ERR_OK;
00239   }
00240 
00241   /* verify checksum */
00242 #if CHECKSUM_CHECK_IP
00243   if (inet_chksum(iphdr, iphdr_hlen) != 0) {
00244 
00245     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00246       ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
00247     ip_debug_print(p);
00248     pbuf_free(p);
00249     IP_STATS_INC(ip.chkerr);
00250     IP_STATS_INC(ip.drop);
00251     snmp_inc_ipinhdrerrors();
00252     return ERR_OK;
00253   }
00254 #endif
00255 
00256   /* Trim pbuf. This should have been done at the netif layer,
00257    * but we'll do it anyway just to be sure that its done. */
00258   pbuf_realloc(p, iphdr_len);
00259 
00260   /* match packet against an interface, i.e. is this packet for us? */
00261 #if LWIP_IGMP
00262   if (ip_addr_ismulticast(&(iphdr->dest))) {
00263     if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {
00264       netif = inp;
00265     } else {
00266       netif = NULL;
00267     }
00268   } else
00269 #endif /* LWIP_IGMP */
00270   {
00271     /* start trying with inp. if that's not acceptable, start walking the
00272        list of configured netifs.
00273        'first' is used as a boolean to mark whether we started walking the list */
00274     int first = 1;
00275     netif = inp;
00276     do {
00277       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",
00278           iphdr->dest.addr, netif->ip_addr.addr,
00279           iphdr->dest.addr & netif->netmask.addr,
00280           netif->ip_addr.addr & netif->netmask.addr,
00281           iphdr->dest.addr & ~(netif->netmask.addr)));
00282 
00283       /* interface is up and configured? */
00284       if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
00285         /* unicast to this interface address? */
00286         if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
00287             /* or broadcast on this interface network address? */
00288             ip_addr_isbroadcast(&(iphdr->dest), netif)) {
00289           LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
00290               netif->name[0], netif->name[1]));
00291           /* break out of for loop */
00292           break;
00293         }
00294       }
00295       if (first) {
00296         first = 0;
00297         netif = netif_list;
00298       } else {
00299         netif = netif->next;
00300       }
00301       if (netif == inp) {
00302         netif = netif->next;
00303       }
00304     } while(netif != NULL);
00305   }
00306 
00307 #if LWIP_DHCP
00308   /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
00309    * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
00310    * According to RFC 1542 section 3.1.1, referred by RFC 2131).
00311    */
00312   if (netif == NULL) {
00313     /* remote port is DHCP server? */
00314     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
00315       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
00316         ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
00317       if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {
00318         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
00319         netif = inp;
00320         check_ip_src = 0;
00321       }
00322     }
00323   }
00324 #endif /* LWIP_DHCP */
00325 
00326   /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
00327 #if LWIP_DHCP
00328   /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
00329   if (check_ip_src && (iphdr->src.addr != 0))
00330 #endif /* LWIP_DHCP */
00331   {  if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
00332          (ip_addr_ismulticast(&(iphdr->src)))) {
00333       /* packet source is not valid */
00334       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
00335       /* free (drop) packet pbufs */
00336       pbuf_free(p);
00337       IP_STATS_INC(ip.drop);
00338       snmp_inc_ipinaddrerrors();
00339       snmp_inc_ipindiscards();
00340       return ERR_OK;
00341     }
00342   }
00343 
00344   /* packet not for us? */
00345   if (netif == NULL) {
00346     /* packet not for us, route or discard */
00347     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
00348 #if IP_FORWARD
00349     /* non-broadcast packet? */
00350     if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
00351       /* try to forward IP packet on (other) interfaces */
00352       ip_forward(p, iphdr, inp);
00353     } else
00354 #endif /* IP_FORWARD */
00355     {
00356       snmp_inc_ipinaddrerrors();
00357       snmp_inc_ipindiscards();
00358     }
00359     pbuf_free(p);
00360     return ERR_OK;
00361   }
00362   /* packet consists of multiple fragments? */
00363   if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
00364 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
00365     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",
00366       ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
00367     /* reassemble the packet*/
00368     p = ip_reass(p);
00369     /* packet not fully reassembled yet? */
00370     if (p == NULL) {
00371       return ERR_OK;
00372     }
00373     iphdr = p->payload;
00374 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
00375     pbuf_free(p);
00376     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
00377       ntohs(IPH_OFFSET(iphdr))));
00378     IP_STATS_INC(ip.opterr);
00379     IP_STATS_INC(ip.drop);
00380     /* unsupported protocol feature */
00381     snmp_inc_ipinunknownprotos();
00382     return ERR_OK;
00383 #endif /* IP_REASSEMBLY */
00384   }
00385 
00386 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
00387 
00388 #if LWIP_IGMP
00389   /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
00390   if((iphdr_hlen > IP_HLEN &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
00391 #else
00392   if (iphdr_hlen > IP_HLEN) {
00393 #endif /* LWIP_IGMP */
00394     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
00395     pbuf_free(p);
00396     IP_STATS_INC(ip.opterr);
00397     IP_STATS_INC(ip.drop);
00398     /* unsupported protocol feature */
00399     snmp_inc_ipinunknownprotos();
00400     return ERR_OK;
00401   }
00402 #endif /* IP_OPTIONS_ALLOWED == 0 */
00403 
00404   /* send to upper layers */
00405   LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
00406   ip_debug_print(p);
00407   LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
00408 
00409   current_netif = inp;
00410   current_header = iphdr;
00411 
00412 #if LWIP_RAW
00413   /* raw input did not eat the packet? */
00414   if (raw_input(p, inp) == 0)
00415 #endif /* LWIP_RAW */
00416   {
00417 
00418     switch (IPH_PROTO(iphdr)) {
00419 #if LWIP_UDP
00420     case IP_PROTO_UDP:
00421 #if LWIP_UDPLITE
00422     case IP_PROTO_UDPLITE:
00423 #endif /* LWIP_UDPLITE */
00424       snmp_inc_ipindelivers();
00425       udp_input(p, inp);
00426       break;
00427 #endif /* LWIP_UDP */
00428 #if LWIP_TCP
00429     case IP_PROTO_TCP:
00430       snmp_inc_ipindelivers();
00431       tcp_input(p, inp);
00432       break;
00433 #endif /* LWIP_TCP */
00434 #if LWIP_ICMP
00435     case IP_PROTO_ICMP:
00436       snmp_inc_ipindelivers();
00437       icmp_input(p, inp);
00438       break;
00439 #endif /* LWIP_ICMP */
00440 #if LWIP_IGMP
00441     case IP_PROTO_IGMP:
00442       igmp_input(p,inp,&(iphdr->dest));
00443       break;
00444 #endif /* LWIP_IGMP */
00445     default:
00446 #if LWIP_ICMP
00447       /* send ICMP destination protocol unreachable unless is was a broadcast */
00448       if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
00449           !ip_addr_ismulticast(&(iphdr->dest))) {
00450         p->payload = iphdr;
00451         icmp_dest_unreach(p, ICMP_DUR_PROTO);
00452       }
00453 #endif /* LWIP_ICMP */
00454       pbuf_free(p);
00455 
00456       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
00457 
00458       IP_STATS_INC(ip.proterr);
00459       IP_STATS_INC(ip.drop);
00460       snmp_inc_ipinunknownprotos();
00461     }
00462   }
00463 
00464   current_netif = NULL;
00465   current_header = NULL;
00466 
00467   return ERR_OK;
00468 }
00469 
00470 /**
00471  * Sends an IP packet on a network interface. This function constructs
00472  * the IP header and calculates the IP header checksum. If the source
00473  * IP address is NULL, the IP address of the outgoing network
00474  * interface is filled in as source address.
00475  * If the destination IP address is IP_HDRINCL, p is assumed to already
00476  * include an IP header and p->payload points to it instead of the data.
00477  *
00478  * @param p the packet to send (p->payload points to the data, e.g. next
00479             protocol header; if dest == IP_HDRINCL, p already includes an IP
00480             header and p->payload points to that IP header)
00481  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
00482  *         IP  address of the netif used to send is used as source address)
00483  * @param dest the destination IP address to send the packet to
00484  * @param ttl the TTL value to be set in the IP header
00485  * @param tos the TOS value to be set in the IP header
00486  * @param proto the PROTOCOL to be set in the IP header
00487  * @param netif the netif on which to send this packet
00488  * @return ERR_OK if the packet was sent OK
00489  *         ERR_BUF if p doesn't have enough space for IP/LINK headers
00490  *         returns errors returned by netif->output
00491  *
00492  * @note ip_id: RFC791 "some host may be able to simply use
00493  *  unique identifiers independent of destination"
00494  */
00495 err_t
00496 ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00497              u8_t ttl, u8_t tos,
00498              u8_t proto, struct netif *netif)
00499 {
00500 #if IP_OPTIONS_SEND
00501   return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
00502 }
00503 
00504 /**
00505  * Same as ip_output_if() but with the possibility to include IP options:
00506  *
00507  * @ param ip_options pointer to the IP options, copied into the IP header
00508  * @ param optlen length of ip_options
00509  */
00510 err_t ip_output_if_opt(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00511        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
00512        u16_t optlen)
00513 {
00514 #endif /* IP_OPTIONS_SEND */
00515   struct ip_hdr *iphdr;
00516   static u16_t ip_id = 0;
00517 
00518   snmp_inc_ipoutrequests();
00519 
00520   /* Should the IP header be generated or is it already included in p? */
00521   if (dest != IP_HDRINCL) {
00522     u16_t ip_hlen = IP_HLEN;
00523 #if IP_OPTIONS_SEND
00524     u16_t optlen_aligned = 0;
00525     if (optlen != 0) {
00526       /* round up to a multiple of 4 */
00527       optlen_aligned = ((optlen + 3) & ~3);
00528       ip_hlen += optlen_aligned;
00529       /* First write in the IP options */
00530       if (pbuf_header(p, optlen_aligned)) {
00531         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
00532         IP_STATS_INC(ip.err);
00533         snmp_inc_ipoutdiscards();
00534         return ERR_BUF;
00535       }
00536       MEMCPY(p->payload, ip_options, optlen);
00537       if (optlen < optlen_aligned) {
00538         /* zero the remaining bytes */
00539         memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
00540       }
00541     }
00542 #endif /* IP_OPTIONS_SEND */
00543     /* generate IP header */
00544     if (pbuf_header(p, IP_HLEN)) {
00545       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
00546 
00547       IP_STATS_INC(ip.err);
00548       snmp_inc_ipoutdiscards();
00549       return ERR_BUF;
00550     }
00551 
00552     iphdr = p->payload;
00553     LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
00554                (p->len >= sizeof(struct ip_hdr)));
00555 
00556     IPH_TTL_SET(iphdr, ttl);
00557     IPH_PROTO_SET(iphdr, proto);
00558 
00559     ip_addr_set(&(iphdr->dest), dest);
00560 
00561     IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
00562     IPH_LEN_SET(iphdr, htons(p->tot_len));
00563     IPH_OFFSET_SET(iphdr, 0);
00564     IPH_ID_SET(iphdr, htons(ip_id));
00565     ++ip_id;
00566 
00567     if (ip_addr_isany(src)) {
00568       ip_addr_set(&(iphdr->src), &(netif->ip_addr));
00569     } else {
00570       ip_addr_set(&(iphdr->src), src);
00571     }
00572 
00573     IPH_CHKSUM_SET(iphdr, 0);
00574 #if CHECKSUM_GEN_IP
00575     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
00576 #endif
00577   } else {
00578     /* IP header already included in p */
00579     iphdr = p->payload;
00580     dest = &(iphdr->dest);
00581   }
00582 
00583   IP_STATS_INC(ip.xmit);
00584 
00585   LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
00586   ip_debug_print(p);
00587 
00588 #if ENABLE_LOOPBACK
00589   if (ip_addr_cmp(dest, &netif->ip_addr)) {
00590     /* Packet to self, enqueue it for loopback */
00591     LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
00592     return netif_loop_output(netif, p, dest);
00593   }
00594 #endif /* ENABLE_LOOPBACK */
00595 #if IP_FRAG
00596   /* don't fragment if interface has mtu set to 0 [loopif] */
00597   if (netif->mtu && (p->tot_len > netif->mtu)) {
00598     return ip_frag(p,netif,dest);
00599   }
00600 #endif
00601 
00602   LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
00603   return netif->output(netif, p, dest);
00604 }
00605 
00606 /**
00607  * Simple interface to ip_output_if. It finds the outgoing network
00608  * interface and calls upon ip_output_if to do the actual work.
00609  *
00610  * @param p the packet to send (p->payload points to the data, e.g. next
00611             protocol header; if dest == IP_HDRINCL, p already includes an IP
00612             header and p->payload points to that IP header)
00613  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
00614  *         IP  address of the netif used to send is used as source address)
00615  * @param dest the destination IP address to send the packet to
00616  * @param ttl the TTL value to be set in the IP header
00617  * @param tos the TOS value to be set in the IP header
00618  * @param proto the PROTOCOL to be set in the IP header
00619  *
00620  * @return ERR_RTE if no route is found
00621  *         see ip_output_if() for more return values
00622  */
00623 err_t
00624 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00625           u8_t ttl, u8_t tos, u8_t proto)
00626 {
00627   struct netif *netif;
00628 
00629   if ((netif = ip_route(dest)) == NULL) {
00630     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00631     IP_STATS_INC(ip.rterr);
00632     return ERR_RTE;
00633   }
00634 
00635   return ip_output_if(p, src, dest, ttl, tos, proto, netif);
00636 }
00637 
00638 #if LWIP_NETIF_HWADDRHINT
00639 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
00640  *  before calling ip_output_if.
00641  *
00642  * @param p the packet to send (p->payload points to the data, e.g. next
00643             protocol header; if dest == IP_HDRINCL, p already includes an IP
00644             header and p->payload points to that IP header)
00645  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
00646  *         IP  address of the netif used to send is used as source address)
00647  * @param dest the destination IP address to send the packet to
00648  * @param ttl the TTL value to be set in the IP header
00649  * @param tos the TOS value to be set in the IP header
00650  * @param proto the PROTOCOL to be set in the IP header
00651  * @param addr_hint address hint pointer set to netif->addr_hint before
00652  *        calling ip_output_if()
00653  *
00654  * @return ERR_RTE if no route is found
00655  *         see ip_output_if() for more return values
00656  */
00657 err_t
00658 ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00659           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
00660 {
00661   struct netif *netif;
00662   err_t err;
00663 
00664   if ((netif = ip_route(dest)) == NULL) {
00665     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00666     IP_STATS_INC(ip.rterr);
00667     return ERR_RTE;
00668   }
00669 
00670   netif->addr_hint = addr_hint;
00671   err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
00672   netif->addr_hint = NULL;
00673 
00674   return err;
00675 }
00676 #endif /* LWIP_NETIF_HWADDRHINT*/
00677 
00678 #if IP_DEBUG
00679 /* Print an IP header by using LWIP_DEBUGF
00680  * @param p an IP packet, p->payload pointing to the IP header
00681  */
00682 void
00683 ip_debug_print(struct pbuf *p)
00684 {
00685   struct ip_hdr *iphdr = p->payload;
00686   u8_t *payload;
00687 
00688   payload = (u8_t *)iphdr + IP_HLEN;
00689 
00690   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
00691   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00692   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
00693                     IPH_V(iphdr),
00694                     IPH_HL(iphdr),
00695                     IPH_TOS(iphdr),
00696                     ntohs(IPH_LEN(iphdr))));
00697   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00698   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
00699                     ntohs(IPH_ID(iphdr)),
00700                     ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
00701                     ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
00702                     ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
00703                     ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
00704   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00705   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
00706                     IPH_TTL(iphdr),
00707                     IPH_PROTO(iphdr),
00708                     ntohs(IPH_CHKSUM(iphdr))));
00709   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00710   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
00711                     ip4_addr1(&iphdr->src),
00712                     ip4_addr2(&iphdr->src),
00713                     ip4_addr3(&iphdr->src),
00714                     ip4_addr4(&iphdr->src)));
00715   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00716   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
00717                     ip4_addr1(&iphdr->dest),
00718                     ip4_addr2(&iphdr->dest),
00719                     ip4_addr3(&iphdr->dest),
00720                     ip4_addr4(&iphdr->dest)));
00721   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00722 }
00723 #endif /* IP_DEBUG */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines