]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/core/ipv4/ip4.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / core / ipv4 / ip4.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 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
73 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
74
75 /** Some defines for DHCP to let link-layer-addressed packets through while the
76  * netif is down.
77  * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT
78  * to return 1 if the port is accepted and 0 if the port is not accepted.
79  */
80 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
81 /* accept DHCP client port and custom port */
82 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
83          || (LWIP_IP_ACCEPT_UDP_PORT(port)))
84 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
85 /* accept custom port only */
86 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(dst_port))
87 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
88 /* accept DHCP client port only */
89 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
90 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
91
92 #else /* LWIP_DHCP */
93 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
94 #endif /* LWIP_DHCP */
95
96 /** Global data for both IPv4 and IPv6 */
97 struct ip_globals ip_data;
98
99 /** The IP header ID of the next outgoing IP packet */
100 static u16_t ip_id;
101
102 /**
103  * Finds the appropriate network interface for a given IP address. It
104  * searches the list of network interfaces linearly. A match is found
105  * if the masked IP address of the network interface equals the masked
106  * IP address given to the function.
107  *
108  * @param dest the destination IP address for which to find the route
109  * @return the netif on which to send to reach dest
110  */
111 struct netif *
112 ip_route(ip_addr_t *dest)
113 {
114   struct netif *netif;
115
116 #ifdef LWIP_HOOK_IP4_ROUTE
117   netif = LWIP_HOOK_IP4_ROUTE(dest);
118   if (netif != NULL) {
119     return netif;
120   }
121 #endif
122
123   /* iterate through netifs */
124   for (netif = netif_list; netif != NULL; netif = netif->next) {
125     /* network mask matches? */
126     if (netif_is_up(netif)) {
127       if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
128         /* return netif on which to forward IP packet */
129         return netif;
130       }
131     }
132   }
133   if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
134     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
135       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
136     IP_STATS_INC(ip.rterr);
137     snmp_inc_ipoutnoroutes();
138     return NULL;
139   }
140   /* no matching netif found, use default netif */
141   return netif_default;
142 }
143
144 #if IP_FORWARD
145 /**
146  * Determine whether an IP address is in a reserved set of addresses
147  * that may not be forwarded, or whether datagrams to that destination
148  * may be forwarded.
149  * @param p the packet to forward
150  * @param dest the destination IP address
151  * @return 1: can forward 0: discard
152  */
153 static int
154 ip_canforward(struct pbuf *p)
155 {
156   u32_t addr = ip4_addr_get_u32(ip_current_dest_addr());
157
158   if (p->flags & PBUF_FLAG_LLBCAST) {
159     /* don't route link-layer broadcasts */
160     return 0;
161   }
162   if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) {
163     /* don't route link-layer multicasts unless the destination address is an IP
164        multicast address */
165     return 0;
166   }
167   if (IP_EXPERIMENTAL(addr)) {
168     return 0;
169   }
170   if (IP_CLASSA(addr)) {
171     u32_t net = addr & IP_CLASSA_NET;
172     if ((net == 0) || (net == (IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
173       /* don't route loopback packets */
174       return 0;
175     }
176   }
177   return 1;
178 }
179
180 /**
181  * Forwards an IP packet. It finds an appropriate route for the
182  * packet, decrements the TTL value of the packet, adjusts the
183  * checksum and outputs the packet on the appropriate interface.
184  *
185  * @param p the packet to forward (p->payload points to IP header)
186  * @param iphdr the IP header of the input packet
187  * @param inp the netif on which this packet was received
188  */
189 static void
190 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
191 {
192   struct netif *netif;
193
194   PERF_START;
195
196   if (!ip_canforward(p)) {
197     goto return_noroute;
198   }
199
200   /* RFC3927 2.7: do not forward link-local addresses */
201   if (ip_addr_islinklocal(ip_current_dest_addr())) {
202     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
203       ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
204       ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
205     goto return_noroute;
206   }
207
208   /* Find network interface where to forward this IP packet to. */
209   netif = ip_route(ip_current_dest_addr());
210   if (netif == NULL) {
211     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
212       ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
213       ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
214     goto return_noroute;
215   }
216 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
217   /* Do not forward packets onto the same network interface on which
218    * they arrived. */
219   if (netif == inp) {
220     LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
221     goto return_noroute;
222   }
223 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
224
225   /* decrement TTL */
226   IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
227   /* send ICMP if TTL == 0 */
228   if (IPH_TTL(iphdr) == 0) {
229     snmp_inc_ipinhdrerrors();
230 #if LWIP_ICMP
231     /* Don't send ICMP messages in response to ICMP messages */
232     if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
233       icmp_time_exceeded(p, ICMP_TE_TTL);
234     }
235 #endif /* LWIP_ICMP */
236     return;
237   }
238
239   /* Incrementally update the IP checksum. */
240   if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
241     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
242   } else {
243     IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
244   }
245
246   LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
247     ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
248     ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
249
250   IP_STATS_INC(ip.fw);
251   IP_STATS_INC(ip.xmit);
252   snmp_inc_ipforwdatagrams();
253
254   PERF_STOP("ip_forward");
255   /* transmit pbuf on chosen interface */
256   netif->output(netif, p, ip_current_dest_addr());
257   return;
258 return_noroute:
259   snmp_inc_ipoutnoroutes();
260 }
261 #endif /* IP_FORWARD */
262
263 /**
264  * This function is called by the network interface device driver when
265  * an IP packet is received. The function does the basic checks of the
266  * IP header such as packet size being at least larger than the header
267  * size etc. If the packet was not destined for us, the packet is
268  * forwarded (using ip_forward). The IP checksum is always checked.
269  *
270  * Finally, the packet is sent to the upper layer protocol input function.
271  * 
272  * @param p the received IP packet (p->payload points to IP header)
273  * @param inp the netif on which this packet was received
274  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
275  *         processed, but currently always returns ERR_OK)
276  */
277 err_t
278 ip_input(struct pbuf *p, struct netif *inp)
279 {
280   struct ip_hdr *iphdr;
281   struct netif *netif;
282   u16_t iphdr_hlen;
283   u16_t iphdr_len;
284 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
285   int check_ip_src=1;
286 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
287
288   IP_STATS_INC(ip.recv);
289   snmp_inc_ipinreceives();
290
291   /* identify the IP header */
292   iphdr = (struct ip_hdr *)p->payload;
293   if (IPH_V(iphdr) != 4) {
294     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
295     ip_debug_print(p);
296     pbuf_free(p);
297     IP_STATS_INC(ip.err);
298     IP_STATS_INC(ip.drop);
299     snmp_inc_ipinhdrerrors();
300     return ERR_OK;
301   }
302
303 #ifdef LWIP_HOOK_IP4_INPUT
304   if (LWIP_HOOK_IP4_INPUT(p, inp)) {
305     /* the packet has been eaten */
306     return ERR_OK;
307   }
308 #endif
309
310   /* obtain IP header length in number of 32-bit words */
311   iphdr_hlen = IPH_HL(iphdr);
312   /* calculate IP header length in bytes */
313   iphdr_hlen *= 4;
314   /* obtain ip length in bytes */
315   iphdr_len = ntohs(IPH_LEN(iphdr));
316
317   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
318   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
319     if (iphdr_hlen > p->len) {
320       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
321         ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
322         iphdr_hlen, p->len));
323     }
324     if (iphdr_len > p->tot_len) {
325       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
326         ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
327         iphdr_len, p->tot_len));
328     }
329     /* free (drop) packet pbufs */
330     pbuf_free(p);
331     IP_STATS_INC(ip.lenerr);
332     IP_STATS_INC(ip.drop);
333     snmp_inc_ipindiscards();
334     return ERR_OK;
335   }
336
337   /* verify checksum */
338 #if CHECKSUM_CHECK_IP
339   if (inet_chksum(iphdr, iphdr_hlen) != 0) {
340
341     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
342       ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
343     ip_debug_print(p);
344     pbuf_free(p);
345     IP_STATS_INC(ip.chkerr);
346     IP_STATS_INC(ip.drop);
347     snmp_inc_ipinhdrerrors();
348     return ERR_OK;
349   }
350 #endif
351
352   /* Trim pbuf. This should have been done at the netif layer,
353    * but we'll do it anyway just to be sure that its done. */
354   pbuf_realloc(p, iphdr_len);
355
356   /* copy IP addresses to aligned ip_addr_t */
357   ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_dest), iphdr->dest);
358   ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_src), iphdr->src);
359
360   /* match packet against an interface, i.e. is this packet for us? */
361 #if LWIP_IGMP
362   if (ip_addr_ismulticast(ip_current_dest_addr())) {
363     if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip_current_dest_addr()))) {
364       netif = inp;
365     } else {
366       netif = NULL;
367     }
368   } else
369 #endif /* LWIP_IGMP */
370   {
371     /* start trying with inp. if that's not acceptable, start walking the
372        list of configured netifs.
373        'first' is used as a boolean to mark whether we started walking the list */
374     int first = 1;
375     netif = inp;
376     do {
377       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",
378           ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
379           ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
380           ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
381           ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
382
383       /* interface is up and configured? */
384       if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
385         /* unicast to this interface address? */
386         if (ip_addr_cmp(ip_current_dest_addr(), &(netif->ip_addr)) ||
387             /* or broadcast on this interface network address? */
388             ip_addr_isbroadcast(ip_current_dest_addr(), netif)) {
389           LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
390               netif->name[0], netif->name[1]));
391           /* break out of for loop */
392           break;
393         }
394 #if LWIP_AUTOIP
395         /* connections to link-local addresses must persist after changing
396            the netif's address (RFC3927 ch. 1.9) */
397         if ((netif->autoip != NULL) &&
398             ip_addr_cmp(ip_current_dest_addr(), &(netif->autoip->llipaddr))) {
399           LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
400               netif->name[0], netif->name[1]));
401           /* break out of for loop */
402           break;
403         }
404 #endif /* LWIP_AUTOIP */
405       }
406       if (first) {
407         first = 0;
408         netif = netif_list;
409       } else {
410         netif = netif->next;
411       }
412       if (netif == inp) {
413         netif = netif->next;
414       }
415     } while(netif != NULL);
416   }
417
418 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
419   /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
420    * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
421    * According to RFC 1542 section 3.1.1, referred by RFC 2131).
422    *
423    * If you want to accept private broadcast communication while a netif is down,
424    * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
425    *
426    * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
427    */
428   if (netif == NULL) {
429     /* remote port is DHCP server? */
430     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
431       struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
432       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
433         ntohs(udphdr->dest)));
434       if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
435         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
436         netif = inp;
437         check_ip_src = 0;
438       }
439     }
440   }
441 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
442
443   /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
444 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
445   /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
446   if (check_ip_src && !ip_addr_isany(ip_current_src_addr()))
447 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
448   {  if ((ip_addr_isbroadcast(ip_current_src_addr(), inp)) ||
449          (ip_addr_ismulticast(ip_current_src_addr()))) {
450       /* packet source is not valid */
451       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
452       /* free (drop) packet pbufs */
453       pbuf_free(p);
454       IP_STATS_INC(ip.drop);
455       snmp_inc_ipinaddrerrors();
456       snmp_inc_ipindiscards();
457       return ERR_OK;
458     }
459   }
460
461   /* packet not for us? */
462   if (netif == NULL) {
463     /* packet not for us, route or discard */
464     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
465 #if IP_FORWARD
466     /* non-broadcast packet? */
467     if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp)) {
468       /* try to forward IP packet on (other) interfaces */
469       ip_forward(p, iphdr, inp);
470     } else
471 #endif /* IP_FORWARD */
472     {
473       snmp_inc_ipinaddrerrors();
474       snmp_inc_ipindiscards();
475     }
476     pbuf_free(p);
477     return ERR_OK;
478   }
479   /* packet consists of multiple fragments? */
480   if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
481 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
482     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",
483       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));
484     /* reassemble the packet*/
485     p = ip_reass(p);
486     /* packet not fully reassembled yet? */
487     if (p == NULL) {
488       return ERR_OK;
489     }
490     iphdr = (struct ip_hdr *)p->payload;
491 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
492     pbuf_free(p);
493     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
494       ntohs(IPH_OFFSET(iphdr))));
495     IP_STATS_INC(ip.opterr);
496     IP_STATS_INC(ip.drop);
497     /* unsupported protocol feature */
498     snmp_inc_ipinunknownprotos();
499     return ERR_OK;
500 #endif /* IP_REASSEMBLY */
501   }
502
503 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
504
505 #if LWIP_IGMP
506   /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
507   if((iphdr_hlen > IP_HLEN) &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
508 #else
509   if (iphdr_hlen > IP_HLEN) {
510 #endif /* LWIP_IGMP */
511     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
512     pbuf_free(p);
513     IP_STATS_INC(ip.opterr);
514     IP_STATS_INC(ip.drop);
515     /* unsupported protocol feature */
516     snmp_inc_ipinunknownprotos();
517     return ERR_OK;
518   }
519 #endif /* IP_OPTIONS_ALLOWED == 0 */
520
521   /* send to upper layers */
522   LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
523   ip_debug_print(p);
524   LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
525
526   ip_data.current_netif = inp;
527   ip_data.current_ip4_header = iphdr;
528   ip_data.current_ip_header_tot_len = IPH_HL(iphdr) * 4;
529
530 #if LWIP_RAW
531   /* raw input did not eat the packet? */
532   if (raw_input(p, inp) == 0)
533 #endif /* LWIP_RAW */
534   {
535     pbuf_header(p, -iphdr_hlen); /* Move to payload, no check necessary. */
536
537     switch (IPH_PROTO(iphdr)) {
538 #if LWIP_UDP
539     case IP_PROTO_UDP:
540 #if LWIP_UDPLITE
541     case IP_PROTO_UDPLITE:
542 #endif /* LWIP_UDPLITE */
543       snmp_inc_ipindelivers();
544       udp_input(p, inp);
545       break;
546 #endif /* LWIP_UDP */
547 #if LWIP_TCP
548     case IP_PROTO_TCP:
549       snmp_inc_ipindelivers();
550       tcp_input(p, inp);
551       break;
552 #endif /* LWIP_TCP */
553 #if LWIP_ICMP
554     case IP_PROTO_ICMP:
555       snmp_inc_ipindelivers();
556       icmp_input(p, inp);
557       break;
558 #endif /* LWIP_ICMP */
559 #if LWIP_IGMP
560     case IP_PROTO_IGMP:
561       igmp_input(p, inp, ip_current_dest_addr());
562       break;
563 #endif /* LWIP_IGMP */
564     default:
565 #if LWIP_ICMP
566       /* send ICMP destination protocol unreachable unless is was a broadcast */
567       if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp) &&
568           !ip_addr_ismulticast(ip_current_dest_addr())) {
569         pbuf_header(p, iphdr_hlen); /* Move to ip header, no check necessary. */
570         p->payload = iphdr;
571         icmp_dest_unreach(p, ICMP_DUR_PROTO);
572       }
573 #endif /* LWIP_ICMP */
574       pbuf_free(p);
575
576       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
577
578       IP_STATS_INC(ip.proterr);
579       IP_STATS_INC(ip.drop);
580       snmp_inc_ipinunknownprotos();
581     }
582   }
583
584   /* @todo: this is not really necessary... */
585   ip_data.current_netif = NULL;
586   ip_data.current_ip4_header = NULL;
587   ip_data.current_ip_header_tot_len = 0;
588   ip_addr_set_any(ip_current_src_addr());
589   ip_addr_set_any(ip_current_dest_addr());
590
591   return ERR_OK;
592 }
593
594 /**
595  * Sends an IP packet on a network interface. This function constructs
596  * the IP header and calculates the IP header checksum. If the source
597  * IP address is NULL, the IP address of the outgoing network
598  * interface is filled in as source address.
599  * If the destination IP address is IP_HDRINCL, p is assumed to already
600  * include an IP header and p->payload points to it instead of the data.
601  *
602  * @param p the packet to send (p->payload points to the data, e.g. next
603             protocol header; if dest == IP_HDRINCL, p already includes an IP
604             header and p->payload points to that IP header)
605  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
606  *         IP  address of the netif used to send is used as source address)
607  * @param dest the destination IP address to send the packet to
608  * @param ttl the TTL value to be set in the IP header
609  * @param tos the TOS value to be set in the IP header
610  * @param proto the PROTOCOL to be set in the IP header
611  * @param netif the netif on which to send this packet
612  * @return ERR_OK if the packet was sent OK
613  *         ERR_BUF if p doesn't have enough space for IP/LINK headers
614  *         returns errors returned by netif->output
615  *
616  * @note ip_id: RFC791 "some host may be able to simply use
617  *  unique identifiers independent of destination"
618  */
619 err_t
620 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
621              u8_t ttl, u8_t tos,
622              u8_t proto, struct netif *netif)
623 {
624 #if IP_OPTIONS_SEND
625   return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
626 }
627
628 /**
629  * Same as ip_output_if() but with the possibility to include IP options:
630  *
631  * @ param ip_options pointer to the IP options, copied into the IP header
632  * @ param optlen length of ip_options
633  */
634 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
635        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
636        u16_t optlen)
637 {
638 #endif /* IP_OPTIONS_SEND */
639   struct ip_hdr *iphdr;
640   ip_addr_t dest_addr;
641 #if CHECKSUM_GEN_IP_INLINE
642   u32_t chk_sum = 0;
643 #endif /* CHECKSUM_GEN_IP_INLINE */
644
645   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
646      gets altered as the packet is passed down the stack */
647   LWIP_ASSERT("p->ref == 1", p->ref == 1);
648
649   snmp_inc_ipoutrequests();
650
651   /* Should the IP header be generated or is it already included in p? */
652   if (dest != IP_HDRINCL) {
653     u16_t ip_hlen = IP_HLEN;
654 #if IP_OPTIONS_SEND
655     u16_t optlen_aligned = 0;
656     if (optlen != 0) {
657 #if CHECKSUM_GEN_IP_INLINE
658       int i;
659 #endif /* CHECKSUM_GEN_IP_INLINE */
660       /* round up to a multiple of 4 */
661       optlen_aligned = ((optlen + 3) & ~3);
662       ip_hlen += optlen_aligned;
663       /* First write in the IP options */
664       if (pbuf_header(p, optlen_aligned)) {
665         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
666         IP_STATS_INC(ip.err);
667         snmp_inc_ipoutdiscards();
668         return ERR_BUF;
669       }
670       MEMCPY(p->payload, ip_options, optlen);
671       if (optlen < optlen_aligned) {
672         /* zero the remaining bytes */
673         memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
674       }
675 #if CHECKSUM_GEN_IP_INLINE
676       for (i = 0; i < optlen_aligned/2; i++) {
677         chk_sum += ((u16_t*)p->payload)[i];
678       }
679 #endif /* CHECKSUM_GEN_IP_INLINE */
680     }
681 #endif /* IP_OPTIONS_SEND */
682     /* generate IP header */
683     if (pbuf_header(p, IP_HLEN)) {
684       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
685
686       IP_STATS_INC(ip.err);
687       snmp_inc_ipoutdiscards();
688       return ERR_BUF;
689     }
690
691     iphdr = (struct ip_hdr *)p->payload;
692     LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
693                (p->len >= sizeof(struct ip_hdr)));
694
695     IPH_TTL_SET(iphdr, ttl);
696     IPH_PROTO_SET(iphdr, proto);
697 #if CHECKSUM_GEN_IP_INLINE
698     chk_sum += LWIP_MAKE_U16(proto, ttl);
699 #endif /* CHECKSUM_GEN_IP_INLINE */
700
701     /* dest cannot be NULL here */
702     ip_addr_copy(iphdr->dest, *dest);
703 #if CHECKSUM_GEN_IP_INLINE
704     chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
705     chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
706 #endif /* CHECKSUM_GEN_IP_INLINE */
707
708     IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
709     IPH_TOS_SET(iphdr, tos);
710 #if CHECKSUM_GEN_IP_INLINE
711     chk_sum += LWIP_MAKE_U16(tos, iphdr->_v_hl);
712 #endif /* CHECKSUM_GEN_IP_INLINE */
713     IPH_LEN_SET(iphdr, htons(p->tot_len));
714 #if CHECKSUM_GEN_IP_INLINE
715     chk_sum += iphdr->_len;
716 #endif /* CHECKSUM_GEN_IP_INLINE */
717     IPH_OFFSET_SET(iphdr, 0);
718     IPH_ID_SET(iphdr, htons(ip_id));
719 #if CHECKSUM_GEN_IP_INLINE
720     chk_sum += iphdr->_id;
721 #endif /* CHECKSUM_GEN_IP_INLINE */
722     ++ip_id;
723
724     if (ip_addr_isany(src)) {
725       ip_addr_copy(iphdr->src, netif->ip_addr);
726     } else {
727       /* src cannot be NULL here */
728       ip_addr_copy(iphdr->src, *src);
729     }
730
731 #if CHECKSUM_GEN_IP_INLINE
732     chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
733     chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
734     chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
735     chk_sum = (chk_sum >> 16) + chk_sum;
736     chk_sum = ~chk_sum;
737     iphdr->_chksum = chk_sum; /* network order */
738 #else /* CHECKSUM_GEN_IP_INLINE */
739     IPH_CHKSUM_SET(iphdr, 0);
740 #if CHECKSUM_GEN_IP
741     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
742 #endif
743 #endif /* CHECKSUM_GEN_IP_INLINE */
744   } else {
745     /* IP header already included in p */
746     iphdr = (struct ip_hdr *)p->payload;
747     ip_addr_copy(dest_addr, iphdr->dest);
748     dest = &dest_addr;
749   }
750
751   IP_STATS_INC(ip.xmit);
752
753   LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
754   ip_debug_print(p);
755
756 #if ENABLE_LOOPBACK
757   if (ip_addr_cmp(dest, &netif->ip_addr)) {
758     /* Packet to self, enqueue it for loopback */
759     LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
760     return netif_loop_output(netif, p, dest);
761   }
762 #if LWIP_IGMP
763   if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
764     netif_loop_output(netif, p, dest);
765   }
766 #endif /* LWIP_IGMP */
767 #endif /* ENABLE_LOOPBACK */
768 #if IP_FRAG
769   /* don't fragment if interface has mtu set to 0 [loopif] */
770   if (netif->mtu && (p->tot_len > netif->mtu)) {
771     return ip_frag(p, netif, dest);
772   }
773 #endif /* IP_FRAG */
774
775   LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
776   return netif->output(netif, p, dest);
777 }
778
779 /**
780  * Simple interface to ip_output_if. It finds the outgoing network
781  * interface and calls upon ip_output_if to do the actual work.
782  *
783  * @param p the packet to send (p->payload points to the data, e.g. next
784             protocol header; if dest == IP_HDRINCL, p already includes an IP
785             header and p->payload points to that IP header)
786  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
787  *         IP  address of the netif used to send is used as source address)
788  * @param dest the destination IP address to send the packet to
789  * @param ttl the TTL value to be set in the IP header
790  * @param tos the TOS value to be set in the IP header
791  * @param proto the PROTOCOL to be set in the IP header
792  *
793  * @return ERR_RTE if no route is found
794  *         see ip_output_if() for more return values
795  */
796 err_t
797 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
798           u8_t ttl, u8_t tos, u8_t proto)
799 {
800   struct netif *netif;
801
802   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
803      gets altered as the packet is passed down the stack */
804   LWIP_ASSERT("p->ref == 1", p->ref == 1);
805
806   if ((netif = ip_route(dest)) == NULL) {
807     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
808       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
809     IP_STATS_INC(ip.rterr);
810     return ERR_RTE;
811   }
812
813   return ip_output_if(p, src, dest, ttl, tos, proto, netif);
814 }
815
816 #if LWIP_NETIF_HWADDRHINT
817 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
818  *  before calling ip_output_if.
819  *
820  * @param p the packet to send (p->payload points to the data, e.g. next
821             protocol header; if dest == IP_HDRINCL, p already includes an IP
822             header and p->payload points to that IP header)
823  * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
824  *         IP  address of the netif used to send is used as source address)
825  * @param dest the destination IP address to send the packet to
826  * @param ttl the TTL value to be set in the IP header
827  * @param tos the TOS value to be set in the IP header
828  * @param proto the PROTOCOL to be set in the IP header
829  * @param addr_hint address hint pointer set to netif->addr_hint before
830  *        calling ip_output_if()
831  *
832  * @return ERR_RTE if no route is found
833  *         see ip_output_if() for more return values
834  */
835 err_t
836 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
837           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
838 {
839   struct netif *netif;
840   err_t err;
841
842   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
843      gets altered as the packet is passed down the stack */
844   LWIP_ASSERT("p->ref == 1", p->ref == 1);
845
846   if ((netif = ip_route(dest)) == NULL) {
847     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
848       ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
849     IP_STATS_INC(ip.rterr);
850     return ERR_RTE;
851   }
852
853   NETIF_SET_HWADDRHINT(netif, addr_hint);
854   err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
855   NETIF_SET_HWADDRHINT(netif, NULL);
856
857   return err;
858 }
859 #endif /* LWIP_NETIF_HWADDRHINT*/
860
861 #if IP_DEBUG
862 /* Print an IP header by using LWIP_DEBUGF
863  * @param p an IP packet, p->payload pointing to the IP header
864  */
865 void
866 ip_debug_print(struct pbuf *p)
867 {
868   struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
869   u8_t *payload;
870
871   payload = (u8_t *)iphdr + IP_HLEN;
872
873   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
874   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
875   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
876                     IPH_V(iphdr),
877                     IPH_HL(iphdr),
878                     IPH_TOS(iphdr),
879                     ntohs(IPH_LEN(iphdr))));
880   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
881   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
882                     ntohs(IPH_ID(iphdr)),
883                     ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
884                     ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
885                     ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
886                     ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
887   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
888   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
889                     IPH_TTL(iphdr),
890                     IPH_PROTO(iphdr),
891                     ntohs(IPH_CHKSUM(iphdr))));
892   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
893   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
894                     ip4_addr1_16(&iphdr->src),
895                     ip4_addr2_16(&iphdr->src),
896                     ip4_addr3_16(&iphdr->src),
897                     ip4_addr4_16(&iphdr->src)));
898   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
899   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
900                     ip4_addr1_16(&iphdr->dest),
901                     ip4_addr2_16(&iphdr->dest),
902                     ip4_addr3_16(&iphdr->dest),
903                     ip4_addr4_16(&iphdr->dest)));
904   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
905 }
906 #endif /* IP_DEBUG */