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