]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/core/ipv4/ip.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / core / ipv4 / ip.c
1 /**
2  * @file
3  * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
4  * 
5  * @see ip_frag.c
6  *
7  */
8
9 /*
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Adam Dunkels <adam@sics.se>
38  *
39  */
40
41 #include "lwip/opt.h"
42 #include "lwip/ip.h"
43 #include "lwip/def.h"
44 #include "lwip/mem.h"
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet_chksum.h"
47 #include "lwip/netif.h"
48 #include "lwip/icmp.h"
49 #include "lwip/igmp.h"
50 #include "lwip/raw.h"
51 #include "lwip/udp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/snmp.h"
54 #include "lwip/dhcp.h"
55 #include "lwip/autoip.h"
56 #include "lwip/stats.h"
57 #include "arch/perf.h"
58
59 #include <string.h>
60
61 /** Set this to 0 in the rare case of wanting to call an extra function to
62  * generate the IP checksum (in contrast to calculating it on-the-fly). */
63 #ifndef LWIP_INLINE_IP_CHKSUM
64 #define LWIP_INLINE_IP_CHKSUM   1
65 #endif
66 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
67 #define CHECKSUM_GEN_IP_INLINE  1
68 #else
69 #define CHECKSUM_GEN_IP_INLINE  0
70 #endif
71
72 /**
73  * The interface that provided the packet for the current callback
74  * invocation.
75  */
76 struct netif *current_netif;
77
78 /**
79  * Header of the input packet currently being processed.
80  */
81 const struct ip_hdr *current_header;
82
83 /**
84  * Finds the appropriate network interface for a given IP address. It
85  * searches the list of network interfaces linearly. A match is found
86  * if the masked IP address of the network interface equals the masked
87  * IP address given to the function.
88  *
89  * @param dest the destination IP address for which to find the route
90  * @return the netif on which to send to reach dest
91  */
92 struct netif *
93 ip_route(ip_addr_t *dest)
94 {
95   struct netif *netif;
96
97   /* iterate through netifs */
98   for(netif = netif_list; netif != NULL; netif = netif->next) {
99     /* network mask matches? */
100     if (netif_is_up(netif)) {
101       if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
102         /* return netif on which to forward IP packet */
103         return netif;
104       }
105     }
106   }
107   if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
108     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
109       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
110     IP_STATS_INC(ip.rterr);
111     snmp_inc_ipoutnoroutes();
112     return NULL;
113   }
114   /* no matching netif found, use default netif */
115   return netif_default;
116 }
117
118 #if IP_FORWARD
119 /**
120  * Forwards an IP packet. It finds an appropriate route for the
121  * packet, decrements the TTL value of the packet, adjusts the
122  * checksum and outputs the packet on the appropriate interface.
123  *
124  * @param p the packet to forward (p->payload points to IP header)
125  * @param iphdr the IP header of the input packet
126  * @param inp the netif on which this packet was received
127  * @return the netif on which the packet was sent (NULL if it wasn't sent)
128  */
129 static struct netif *
130 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
131 {
132   struct netif *netif;
133   ip_addr_t dest;
134
135   PERF_START;
136   dest = iphdr->dest;
137
138   /* RFC3927 2.7: do not forward link-local addresses */
139   if (ip_addr_islinklocal(&dest)) {
140     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
141       ip4_addr1_16(&dest), ip4_addr2_16(&dest), ip4_addr3_16(&dest), ip4_addr4_16(&dest)));
142     snmp_inc_ipoutnoroutes();
143     return (struct netif *)NULL;
144   }
145
146   /* Find network interface where to forward this IP packet to. */
147   netif = ip_route(&dest);
148   if (netif == NULL) {
149     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
150       ip4_addr1_16(&dest), ip4_addr2_16(&dest), ip4_addr3_16(&dest), ip4_addr4_16(&dest)));
151     snmp_inc_ipoutnoroutes();
152     return (struct netif *)NULL;
153   }
154   /* Do not forward packets onto the same network interface on which
155    * they arrived. */
156   if (netif == inp) {
157     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
158     snmp_inc_ipoutnoroutes();
159     return (struct netif *)NULL;
160   }
161
162   /* decrement TTL */
163   IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
164   /* send ICMP if TTL == 0 */
165   if (IPH_TTL(iphdr) == 0) {
166     snmp_inc_ipinhdrerrors();
167 #if LWIP_ICMP
168     /* Don't send ICMP messages in response to ICMP messages */
169     if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
170       icmp_time_exceeded(p, ICMP_TE_TTL);
171     }
172 #endif /* LWIP_ICMP */
173     return (struct netif *)NULL;
174   }
175
176   /* Incrementally update the IP checksum. */
177   if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffff - 0x100)) {
178     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
179   } else {
180     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
181   }
182
183   LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
184     ip4_addr1_16(&dest), ip4_addr2_16(&dest), ip4_addr3_16(&dest), ip4_addr4_16(&dest)));
185
186   IP_STATS_INC(ip.fw);
187   IP_STATS_INC(ip.xmit);
188   snmp_inc_ipforwdatagrams();
189
190   PERF_STOP("ip_forward");
191   /* transmit pbuf on chosen interface */
192   netif->output(netif, p, &dest);
193   return netif;
194 }
195 #endif /* IP_FORWARD */
196
197 /**
198  * This function is called by the network interface device driver when
199  * an IP packet is received. The function does the basic checks of the
200  * IP header such as packet size being at least larger than the header
201  * size etc. If the packet was not destined for us, the packet is
202  * forwarded (using ip_forward). The IP checksum is always checked.
203  *
204  * Finally, the packet is sent to the upper layer protocol input function.
205  * 
206  * @param p the received IP packet (p->payload points to IP header)
207  * @param inp the netif on which this packet was received
208  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
209  *         processed, but currently always returns ERR_OK)
210  */
211 err_t
212 ip_input(struct pbuf *p, struct netif *inp)
213 {
214   struct ip_hdr *iphdr;
215   struct netif *netif;
216   u16_t iphdr_hlen;
217   u16_t iphdr_len;
218 #if LWIP_DHCP
219   int check_ip_src=1;
220 #endif /* LWIP_DHCP */
221
222   IP_STATS_INC(ip.recv);
223   snmp_inc_ipinreceives();
224
225   /* identify the IP header */
226   iphdr = (struct ip_hdr *)p->payload;
227   if (IPH_V(iphdr) != 4) {
228     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
229     ip_debug_print(p);
230     pbuf_free(p);
231     IP_STATS_INC(ip.err);
232     IP_STATS_INC(ip.drop);
233     snmp_inc_ipinhdrerrors();
234     return ERR_OK;
235   }
236
237   /* obtain IP header length in number of 32-bit words */
238   iphdr_hlen = IPH_HL(iphdr);
239   /* calculate IP header length in bytes */
240   iphdr_hlen *= 4;
241   /* obtain ip length in bytes */
242   iphdr_len = ntohs(IPH_LEN(iphdr));
243
244   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
245   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
246     if (iphdr_hlen > p->len) {
247       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
248         ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
249                                iphdr_hlen, p->len));
250     }
251     if (iphdr_len > p->tot_len) {
252       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
253         ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
254                                iphdr_len, p->tot_len));
255     }
256     /* free (drop) packet pbufs */
257     pbuf_free(p);
258     IP_STATS_INC(ip.lenerr);
259     IP_STATS_INC(ip.drop);
260     snmp_inc_ipindiscards();
261     return ERR_OK;
262   }
263
264   /* verify checksum */
265 #if CHECKSUM_CHECK_IP
266   if (inet_chksum(iphdr, iphdr_hlen) != 0) {
267
268     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
269       ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
270     ip_debug_print(p);
271     pbuf_free(p);
272     IP_STATS_INC(ip.chkerr);
273     IP_STATS_INC(ip.drop);
274     snmp_inc_ipinhdrerrors();
275     return ERR_OK;
276   }
277 #endif
278
279   /* Trim pbuf. This should have been done at the netif layer,
280    * but we'll do it anyway just to be sure that its done. */
281   pbuf_realloc(p, iphdr_len);
282
283   /* match packet against an interface, i.e. is this packet for us? */
284 #if LWIP_IGMP
285   if (ip_addr_ismulticast(&(iphdr->dest))) {
286     if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {
287       netif = inp;
288     } else {
289       netif = NULL;
290     }
291   } else
292 #endif /* LWIP_IGMP */
293   {
294     /* start trying with inp. if that's not acceptable, start walking the
295        list of configured netifs.
296        'first' is used as a boolean to mark whether we started walking the list */
297     int first = 1;
298     netif = inp;
299     do {
300       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",
301           ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
302           ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
303           ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
304           ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
305
306       /* interface is up and configured? */
307       if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
308         /* unicast to this interface address? */
309         if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
310             /* or broadcast on this interface network address? */
311             ip_addr_isbroadcast(&(iphdr->dest), netif)) {
312           LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
313               netif->name[0], netif->name[1]));
314           /* break out of for loop */
315           break;
316         }
317 #if LWIP_AUTOIP
318         /* connections to link-local addresses must persist after changing
319            the netif's address (RFC3927 ch. 1.9) */
320         if ((netif->autoip != NULL) &&
321             ip_addr_cmp(&(iphdr->dest), &(netif->autoip->llipaddr))) {
322           LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
323               netif->name[0], netif->name[1]));
324           /* break out of for loop */
325           break;
326         }
327 #endif /* LWIP_AUTOIP */
328       }
329       if (first) {
330         first = 0;
331         netif = netif_list;
332       } else {
333         netif = netif->next;
334       }
335       if (netif == inp) {
336         netif = netif->next;
337       }
338     } while(netif != NULL);
339   }
340
341 #if LWIP_DHCP
342   /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
343    * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
344    * According to RFC 1542 section 3.1.1, referred by RFC 2131).
345    */
346   if (netif == NULL) {
347     /* remote port is DHCP server? */
348     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
349       struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
350       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
351         ntohs(udphdr->dest)));
352       if (udphdr->dest == PP_NTOHS(DHCP_CLIENT_PORT)) {
353         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
354         netif = inp;
355         check_ip_src = 0;
356       }
357     }
358   }
359 #endif /* LWIP_DHCP */
360
361   /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
362 #if LWIP_DHCP
363   /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
364   if (check_ip_src && !ip_addr_isany(&iphdr->src))
365 #endif /* LWIP_DHCP */
366   {  if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
367          (ip_addr_ismulticast(&(iphdr->src)))) {
368       /* packet source is not valid */
369       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
370       /* free (drop) packet pbufs */
371       pbuf_free(p);
372       IP_STATS_INC(ip.drop);
373       snmp_inc_ipinaddrerrors();
374       snmp_inc_ipindiscards();
375       return ERR_OK;
376     }
377   }
378
379   /* packet not for us? */
380   if (netif == NULL) {
381     /* packet not for us, route or discard */
382     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
383 #if IP_FORWARD
384     /* non-broadcast packet? */
385     if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
386       /* try to forward IP packet on (other) interfaces */
387       ip_forward(p, iphdr, inp);
388     } else
389 #endif /* IP_FORWARD */
390     {
391       snmp_inc_ipinaddrerrors();
392       snmp_inc_ipindiscards();
393     }
394     pbuf_free(p);
395     return ERR_OK;
396   }
397   /* packet consists of multiple fragments? */
398   if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
399 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
400     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",
401       ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
402     /* reassemble the packet*/
403     p = ip_reass(p);
404     /* packet not fully reassembled yet? */
405     if (p == NULL) {
406       return ERR_OK;
407     }
408     iphdr = (struct ip_hdr *)p->payload;
409 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
410     pbuf_free(p);
411     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
412       ntohs(IPH_OFFSET(iphdr))));
413     IP_STATS_INC(ip.opterr);
414     IP_STATS_INC(ip.drop);
415     /* unsupported protocol feature */
416     snmp_inc_ipinunknownprotos();
417     return ERR_OK;
418 #endif /* IP_REASSEMBLY */
419   }
420
421 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
422
423 #if LWIP_IGMP
424   /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
425   if((iphdr_hlen > IP_HLEN) &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
426 #else
427   if (iphdr_hlen > IP_HLEN) {
428 #endif /* LWIP_IGMP */
429     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
430     pbuf_free(p);
431     IP_STATS_INC(ip.opterr);
432     IP_STATS_INC(ip.drop);
433     /* unsupported protocol feature */
434     snmp_inc_ipinunknownprotos();
435     return ERR_OK;
436   }
437 #endif /* IP_OPTIONS_ALLOWED == 0 */
438
439   /* send to upper layers */
440   LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
441   ip_debug_print(p);
442   LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
443
444   current_netif = inp;
445   current_header = iphdr;
446
447 #if LWIP_RAW
448   /* raw input did not eat the packet? */
449   if (raw_input(p, inp) == 0)
450 #endif /* LWIP_RAW */
451   {
452
453     switch (IPH_PROTO(iphdr)) {
454 #if LWIP_UDP
455     case IP_PROTO_UDP:
456 #if LWIP_UDPLITE
457     case IP_PROTO_UDPLITE:
458 #endif /* LWIP_UDPLITE */
459       snmp_inc_ipindelivers();
460       udp_input(p, inp);
461       break;
462 #endif /* LWIP_UDP */
463 #if LWIP_TCP
464     case IP_PROTO_TCP:
465       snmp_inc_ipindelivers();
466       tcp_input(p, inp);
467       break;
468 #endif /* LWIP_TCP */
469 #if LWIP_ICMP
470     case IP_PROTO_ICMP:
471       snmp_inc_ipindelivers();
472       icmp_input(p, inp);
473       break;
474 #endif /* LWIP_ICMP */
475 #if LWIP_IGMP
476     case IP_PROTO_IGMP:
477       igmp_input(p,inp,&(iphdr->dest));
478       break;
479 #endif /* LWIP_IGMP */
480     default:
481 #if LWIP_ICMP
482       /* send ICMP destination protocol unreachable unless is was a broadcast */
483       if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
484           !ip_addr_ismulticast(&(iphdr->dest))) {
485         p->payload = iphdr;
486         icmp_dest_unreach(p, ICMP_DUR_PROTO);
487       }
488 #endif /* LWIP_ICMP */
489       pbuf_free(p);
490
491       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
492
493       IP_STATS_INC(ip.proterr);
494       IP_STATS_INC(ip.drop);
495       snmp_inc_ipinunknownprotos();
496     }
497   }
498
499   current_netif = NULL;
500   current_header = NULL;
501
502   return ERR_OK;
503 }
504
505 /**
506  * Sends an IP packet on a network interface. This function constructs
507  * the IP header and calculates the IP header checksum. If the source
508  * IP address is NULL, the IP address of the outgoing network
509  * interface is filled in as source address.
510  * If the destination IP address is IP_HDRINCL, p is assumed to already
511  * include an IP header and p->payload points to it instead of the data.
512  *
513  * @param p the packet to send (p->payload points to the data, e.g. next
514             protocol header; if dest == IP_HDRINCL, p already includes an IP
515             header and p->payload points to that IP header)
516  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
517  *         IP  address of the netif used to send is used as source address)
518  * @param dest the destination IP address to send the packet to
519  * @param ttl the TTL value to be set in the IP header
520  * @param tos the TOS value to be set in the IP header
521  * @param proto the PROTOCOL to be set in the IP header
522  * @param netif the netif on which to send this packet
523  * @return ERR_OK if the packet was sent OK
524  *         ERR_BUF if p doesn't have enough space for IP/LINK headers
525  *         returns errors returned by netif->output
526  *
527  * @note ip_id: RFC791 "some host may be able to simply use
528  *  unique identifiers independent of destination"
529  */
530 err_t
531 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
532              u8_t ttl, u8_t tos,
533              u8_t proto, struct netif *netif)
534 {
535 #if IP_OPTIONS_SEND
536   return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
537 }
538
539 /**
540  * Same as ip_output_if() but with the possibility to include IP options:
541  *
542  * @ param ip_options pointer to the IP options, copied into the IP header
543  * @ param optlen length of ip_options
544  */
545 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
546        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
547        u16_t optlen)
548 {
549 #endif /* IP_OPTIONS_SEND */
550   struct ip_hdr *iphdr;
551   static u16_t ip_id = 0;
552 #if CHECKSUM_GEN_IP_INLINE
553   u32_t chk_sum;
554 #endif /* CHECKSUM_GEN_IP_INLINE */
555
556   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
557      gets altered as the packet is passed down the stack */
558   LWIP_ASSERT("p->ref == 1", p->ref == 1);
559
560   snmp_inc_ipoutrequests();
561
562   /* Should the IP header be generated or is it already included in p? */
563   if (dest != IP_HDRINCL) {
564     u16_t ip_hlen = IP_HLEN;
565 #if IP_OPTIONS_SEND
566     u16_t optlen_aligned = 0;
567     if (optlen != 0) {
568       /* round up to a multiple of 4 */
569       optlen_aligned = ((optlen + 3) & ~3);
570       ip_hlen += optlen_aligned;
571       /* First write in the IP options */
572       if (pbuf_header(p, optlen_aligned)) {
573         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
574         IP_STATS_INC(ip.err);
575         snmp_inc_ipoutdiscards();
576         return ERR_BUF;
577       }
578       MEMCPY(p->payload, ip_options, optlen);
579       if (optlen < optlen_aligned) {
580         /* zero the remaining bytes */
581         memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
582       }
583     }
584 #endif /* IP_OPTIONS_SEND */
585     /* generate IP header */
586     if (pbuf_header(p, IP_HLEN)) {
587       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
588
589       IP_STATS_INC(ip.err);
590       snmp_inc_ipoutdiscards();
591       return ERR_BUF;
592     }
593
594     iphdr = (struct ip_hdr *)p->payload;
595     LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
596                (p->len >= sizeof(struct ip_hdr)));
597
598     IPH_TTL_SET(iphdr, ttl);
599     IPH_PROTO_SET(iphdr, proto);
600 #if CHECKSUM_GEN_IP_INLINE
601     chk_sum = LWIP_MAKE_U16(proto, ttl);
602 #endif /* CHECKSUM_GEN_IP_INLINE */
603
604     /* dest cannot be NULL here */
605     ip_addr_copy(iphdr->dest, *dest);
606 #if CHECKSUM_GEN_IP_INLINE
607     chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
608     chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
609 #endif /* CHECKSUM_GEN_IP_INLINE */
610
611     IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
612 #if CHECKSUM_GEN_IP_INLINE
613     chk_sum += iphdr->_v_hl_tos;
614 #endif /* CHECKSUM_GEN_IP_INLINE */
615     IPH_LEN_SET(iphdr, htons(p->tot_len));
616 #if CHECKSUM_GEN_IP_INLINE
617     chk_sum += iphdr->_len;
618 #endif /* CHECKSUM_GEN_IP_INLINE */
619     IPH_OFFSET_SET(iphdr, 0);
620     IPH_ID_SET(iphdr, htons(ip_id));
621 #if CHECKSUM_GEN_IP_INLINE
622     chk_sum += iphdr->_id;
623 #endif /* CHECKSUM_GEN_IP_INLINE */
624     ++ip_id;
625
626     if (ip_addr_isany(src)) {
627       ip_addr_copy(iphdr->src, netif->ip_addr);
628     } else {
629       /* src cannot be NULL here */
630       ip_addr_copy(iphdr->src, *src);
631     }
632
633 #if CHECKSUM_GEN_IP_INLINE
634     chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
635     chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
636     chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
637     chk_sum = (chk_sum >> 16) + chk_sum;
638     chk_sum = ~chk_sum;
639     iphdr->_chksum = chk_sum; /* network order */
640 #else /* CHECKSUM_GEN_IP_INLINE */
641     IPH_CHKSUM_SET(iphdr, 0);
642 #if CHECKSUM_GEN_IP
643     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
644 #endif
645 #endif /* CHECKSUM_GEN_IP_INLINE */
646   } else {
647     /* IP header already included in p */
648     iphdr = (struct ip_hdr *)p->payload;
649     dest = &(iphdr->dest);
650   }
651
652   IP_STATS_INC(ip.xmit);
653
654   LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
655   ip_debug_print(p);
656
657 #if ENABLE_LOOPBACK
658   if (ip_addr_cmp(dest, &netif->ip_addr)) {
659     /* Packet to self, enqueue it for loopback */
660     LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
661     return netif_loop_output(netif, p, dest);
662   }
663 #endif /* ENABLE_LOOPBACK */
664 #if IP_FRAG
665   /* don't fragment if interface has mtu set to 0 [loopif] */
666   if (netif->mtu && (p->tot_len > netif->mtu)) {
667     return ip_frag(p,netif,dest);
668   }
669 #endif
670
671   LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
672     return netif->output(netif, p, dest);
673 }
674
675 /**
676  * Simple interface to ip_output_if. It finds the outgoing network
677  * interface and calls upon ip_output_if to do the actual work.
678  *
679  * @param p the packet to send (p->payload points to the data, e.g. next
680             protocol header; if dest == IP_HDRINCL, p already includes an IP
681             header and p->payload points to that IP header)
682  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
683  *         IP  address of the netif used to send is used as source address)
684  * @param dest the destination IP address to send the packet to
685  * @param ttl the TTL value to be set in the IP header
686  * @param tos the TOS value to be set in the IP header
687  * @param proto the PROTOCOL to be set in the IP header
688  *
689  * @return ERR_RTE if no route is found
690  *         see ip_output_if() for more return values
691  */
692 err_t
693 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
694           u8_t ttl, u8_t tos, u8_t proto)
695 {
696   struct netif *netif;
697
698   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
699      gets altered as the packet is passed down the stack */
700   LWIP_ASSERT("p->ref == 1", p->ref == 1);
701
702   if ((netif = ip_route(dest)) == NULL) {
703     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
704       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
705     IP_STATS_INC(ip.rterr);
706     return ERR_RTE;
707   }
708
709   return ip_output_if(p, src, dest, ttl, tos, proto, netif);
710 }
711
712 #if LWIP_NETIF_HWADDRHINT
713 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
714  *  before calling ip_output_if.
715  *
716  * @param p the packet to send (p->payload points to the data, e.g. next
717             protocol header; if dest == IP_HDRINCL, p already includes an IP
718             header and p->payload points to that IP header)
719  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
720  *         IP  address of the netif used to send is used as source address)
721  * @param dest the destination IP address to send the packet to
722  * @param ttl the TTL value to be set in the IP header
723  * @param tos the TOS value to be set in the IP header
724  * @param proto the PROTOCOL to be set in the IP header
725  * @param addr_hint address hint pointer set to netif->addr_hint before
726  *        calling ip_output_if()
727  *
728  * @return ERR_RTE if no route is found
729  *         see ip_output_if() for more return values
730  */
731 err_t
732 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
733           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
734 {
735   struct netif *netif;
736   err_t err;
737
738   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
739      gets altered as the packet is passed down the stack */
740   LWIP_ASSERT("p->ref == 1", p->ref == 1);
741
742   if ((netif = ip_route(dest)) == NULL) {
743     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
744       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
745     IP_STATS_INC(ip.rterr);
746     return ERR_RTE;
747   }
748
749   netif->addr_hint = addr_hint;
750   err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
751   netif->addr_hint = NULL;
752
753   return err;
754 }
755 #endif /* LWIP_NETIF_HWADDRHINT*/
756
757 #if IP_DEBUG
758 /* Print an IP header by using LWIP_DEBUGF
759  * @param p an IP packet, p->payload pointing to the IP header
760  */
761 void
762 ip_debug_print(struct pbuf *p)
763 {
764   struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
765   u8_t *payload;
766
767   payload = (u8_t *)iphdr + IP_HLEN;
768
769   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
770   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
771   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
772                     IPH_V(iphdr),
773                     IPH_HL(iphdr),
774                     IPH_TOS(iphdr),
775                     ntohs(IPH_LEN(iphdr))));
776   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
777   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
778                     ntohs(IPH_ID(iphdr)),
779                     ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
780                     ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
781                     ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
782                     ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
783   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
784   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
785                     IPH_TTL(iphdr),
786                     IPH_PROTO(iphdr),
787                     ntohs(IPH_CHKSUM(iphdr))));
788   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
789   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
790                     ip4_addr1_16(&iphdr->src),
791                     ip4_addr2_16(&iphdr->src),
792                     ip4_addr3_16(&iphdr->src),
793                     ip4_addr4_16(&iphdr->src)));
794   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
795   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
796                     ip4_addr1_16(&iphdr->dest),
797                     ip4_addr2_16(&iphdr->dest),
798                     ip4_addr3_16(&iphdr->dest),
799                     ip4_addr4_16(&iphdr->dest)));
800   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
801 }
802 #endif /* IP_DEBUG */