]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/core/ipv6/ip6.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / core / ipv6 / ip6.c
1 /**
2  * @file
3  *
4  * IPv6 layer.
5  */
6
7 /*
8  * Copyright (c) 2010 Inico Technologies Ltd.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Ivan Delamer <delamer@inicotech.com>
36  *
37  *
38  * Please coordinate changes and requests with Ivan Delamer
39  * <delamer@inicotech.com>
40  */
41
42
43 #include "lwip/opt.h"
44
45 #if LWIP_IPV6  /* don't build if not configured for use in lwipopts.h */
46
47 #include "lwip/def.h"
48 #include "lwip/mem.h"
49 #include "lwip/netif.h"
50 #include "lwip/ip6.h"
51 #include "lwip/ip6_addr.h"
52 #include "lwip/ip6_frag.h"
53 #include "lwip/icmp6.h"
54 #include "lwip/raw.h"
55 #include "lwip/udp.h"
56 #include "lwip/tcp_impl.h"
57 #include "lwip/dhcp6.h"
58 #include "lwip/nd6.h"
59 #include "lwip/mld6.h"
60 #include "lwip/debug.h"
61 #include "lwip/stats.h"
62
63
64 /**
65  * Finds the appropriate network interface for a given IPv6 address. It tries to select
66  * a netif following a sequence of heuristics:
67  * 1) if there is only 1 netif, return it
68  * 2) if the destination is a link-local address, try to match the src address to a netif.
69  *    this is a tricky case because with multiple netifs, link-local addresses only have
70  *    meaning within a particular subnet/link.
71  * 3) tries to match the destination subnet to a configured address
72  * 4) tries to find a router
73  * 5) tries to match the source address to the netif
74  * 6) returns the default netif, if configured
75  *
76  * @param src the source IPv6 address, if known
77  * @param dest the destination IPv6 address for which to find the route
78  * @return the netif on which to send to reach dest
79  */
80 struct netif *
81 ip6_route(struct ip6_addr *src, struct ip6_addr *dest)
82 {
83   struct netif *netif;
84   s8_t i;
85
86   /* If single netif configuration, fast return. */
87   if ((netif_list != NULL) && (netif_list->next == NULL)) {
88     return netif_list;
89   }
90
91   /* Special processing for link-local addresses. */
92   if (ip6_addr_islinklocal(dest)) {
93     if (ip6_addr_isany(src)) {
94       /* Use default netif. */
95       return netif_default;
96     }
97
98     /* Try to find the netif for the source address. */
99     for(netif = netif_list; netif != NULL; netif = netif->next) {
100       for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
101         if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
102             ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
103           return netif;
104         }
105       }
106     }
107
108     /* netif not found, use default netif */
109     return netif_default;
110   }
111
112   /* See if the destination subnet matches a configured address. */
113   for(netif = netif_list; netif != NULL; netif = netif->next) {
114     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
115       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
116           ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
117         return netif;
118       }
119     }
120   }
121
122   /* Get the netif for a suitable router. */
123   i = nd6_select_router(dest, NULL);
124   if (i >= 0) {
125     if (default_router_list[i].neighbor_entry != NULL) {
126       if (default_router_list[i].neighbor_entry->netif != NULL) {
127         return default_router_list[i].neighbor_entry->netif;
128       }
129     }
130   }
131
132   /* try with the netif that matches the source address. */
133   if (!ip6_addr_isany(src)) {
134     for(netif = netif_list; netif != NULL; netif = netif->next) {
135       for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
136         if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
137             ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
138           return netif;
139         }
140       }
141     }
142   }
143
144   /* no matching netif found, use default netif */
145   return netif_default;
146 }
147
148 /**
149  * Select the best IPv6 source address for a given destination
150  * IPv6 address. Loosely follows RFC 3484. "Strong host" behavior
151  * is assumed.
152  *
153  * @param netif the netif on which to send a packet
154  * @param dest the destination we are trying to reach
155  * @return the most suitable source address to use, or NULL if no suitable
156  *         source address is found
157  */
158 ip6_addr_t *
159 ip6_select_source_address(struct netif *netif, ip6_addr_t * dest)
160 {
161   ip6_addr_t * src = NULL;
162   u8_t i;
163
164   /* If dest is link-local, choose a link-local source. */
165   if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_linklocal(dest) || ip6_addr_ismulticast_iflocal(dest)) {
166     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
167       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
168           ip6_addr_islinklocal(netif_ip6_addr(netif, i))) {
169         return netif_ip6_addr(netif, i);
170       }
171     }
172   }
173
174   /* Choose a site-local with matching prefix. */
175   if (ip6_addr_issitelocal(dest) || ip6_addr_ismulticast_sitelocal(dest)) {
176     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
177       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
178           ip6_addr_issitelocal(netif_ip6_addr(netif, i)) &&
179           ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
180         return netif_ip6_addr(netif, i);
181       }
182     }
183   }
184
185   /* Choose a unique-local with matching prefix. */
186   if (ip6_addr_isuniquelocal(dest) || ip6_addr_ismulticast_orglocal(dest)) {
187     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
188       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
189           ip6_addr_isuniquelocal(netif_ip6_addr(netif, i)) &&
190           ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
191         return netif_ip6_addr(netif, i);
192       }
193     }
194   }
195
196   /* Choose a global with best matching prefix. */
197   if (ip6_addr_isglobal(dest) || ip6_addr_ismulticast_global(dest)) {
198     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
199       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
200           ip6_addr_isglobal(netif_ip6_addr(netif, i))) {
201         if (src == NULL) {
202           src = netif_ip6_addr(netif, i);
203         }
204         else {
205           /* Replace src only if we find a prefix match. */
206           /* TODO find longest matching prefix. */
207           if ((!(ip6_addr_netcmp(src, dest))) &&
208               ip6_addr_netcmp(netif_ip6_addr(netif, i), dest)) {
209             src = netif_ip6_addr(netif, i);
210           }
211         }
212       }
213     }
214     if (src != NULL) {
215       return src;
216     }
217   }
218
219   /* Last resort: see if arbitrary prefix matches. */
220   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
221     if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
222         ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
223       return netif_ip6_addr(netif, i);
224     }
225   }
226
227   return NULL;
228 }
229
230 #if LWIP_IPV6_FORWARD
231 /**
232  * Forwards an IPv6 packet. It finds an appropriate route for the
233  * packet, decrements the HL value of the packet, and outputs
234  * the packet on the appropriate interface.
235  *
236  * @param p the packet to forward (p->payload points to IP header)
237  * @param iphdr the IPv6 header of the input packet
238  * @param inp the netif on which this packet was received
239  */
240 static void
241 ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
242 {
243   struct netif *netif;
244
245   /* do not forward link-local addresses */
246   if (ip6_addr_islinklocal(ip6_current_dest_addr())) {
247     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n"));
248     IP6_STATS_INC(ip6.rterr);
249     IP6_STATS_INC(ip6.drop);
250     return;
251   }
252
253   /* Find network interface where to forward this IP packet to. */
254   netif = ip6_route(IP6_ADDR_ANY, ip6_current_dest_addr());
255   if (netif == NULL) {
256     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
257         IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
258         IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
259         IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
260         IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
261         IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
262         IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
263         IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
264         IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
265 #if LWIP_ICMP6
266     /* Don't send ICMP messages in response to ICMP messages */
267     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
268       icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
269     }
270 #endif /* LWIP_ICMP6 */
271     IP6_STATS_INC(ip6.rterr);
272     IP6_STATS_INC(ip6.drop);
273     return;
274   }
275   /* Do not forward packets onto the same network interface on which
276    * they arrived. */
277   if (netif == inp) {
278     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not bouncing packets back on incoming interface.\n"));
279     IP6_STATS_INC(ip6.rterr);
280     IP6_STATS_INC(ip6.drop);
281     return;
282   }
283
284   /* decrement HL */
285   IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
286   /* send ICMP6 if HL == 0 */
287   if (IP6H_HOPLIM(iphdr) == 0) {
288 #if LWIP_ICMP6
289     /* Don't send ICMP messages in response to ICMP messages */
290     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
291       icmp6_time_exceeded(p, ICMP6_TE_HL);
292     }
293 #endif /* LWIP_ICMP6 */
294     IP6_STATS_INC(ip6.drop);
295     return;
296   }
297
298   if (netif->mtu && (p->tot_len > netif->mtu)) {
299 #if LWIP_ICMP6
300     /* Don't send ICMP messages in response to ICMP messages */
301     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
302       icmp6_packet_too_big(p, netif->mtu);
303     }
304 #endif /* LWIP_ICMP6 */
305     IP6_STATS_INC(ip6.drop);
306     return;
307   }
308
309   LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: forwarding packet to %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
310       IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
311       IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
312       IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
313       IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
314       IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
315       IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
316       IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
317       IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
318
319   /* transmit pbuf on chosen interface */
320   netif->output_ip6(netif, p, ip6_current_dest_addr());
321   IP6_STATS_INC(ip6.fw);
322   IP6_STATS_INC(ip6.xmit);
323   return;
324 }
325 #endif /* LWIP_IPV6_FORWARD */
326
327
328 /**
329  * This function is called by the network interface device driver when
330  * an IPv6 packet is received. The function does the basic checks of the
331  * IP header such as packet size being at least larger than the header
332  * size etc. If the packet was not destined for us, the packet is
333  * forwarded (using ip6_forward).
334  *
335  * Finally, the packet is sent to the upper layer protocol input function.
336  *
337  * @param p the received IPv6 packet (p->payload points to IPv6 header)
338  * @param inp the netif on which this packet was received
339  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
340  *         processed, but currently always returns ERR_OK)
341  */
342 err_t
343 ip6_input(struct pbuf *p, struct netif *inp)
344 {
345   struct ip6_hdr *ip6hdr;
346   struct netif *netif;
347   u8_t nexth;
348   u16_t hlen; /* the current header length */
349   u8_t i;
350 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
351   int check_ip_src=1;
352 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
353
354   IP6_STATS_INC(ip6.recv);
355
356   /* identify the IP header */
357   ip6hdr = (struct ip6_hdr *)p->payload;
358   if (IP6H_V(ip6hdr) != 6) {
359     LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U16_F"\n",
360         IP6H_V(ip6hdr)));
361     pbuf_free(p);
362     IP6_STATS_INC(ip6.err);
363     IP6_STATS_INC(ip6.drop);
364     return ERR_OK;
365   }
366
367   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
368   if ((IP6_HLEN > p->len) || ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len)) {
369     if (IP6_HLEN > p->len) {
370       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
371         ("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
372             IP6_HLEN, p->len));
373     }
374     if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) {
375       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
376         ("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
377             IP6H_PLEN(ip6hdr) + IP6_HLEN, p->tot_len));
378     }
379     /* free (drop) packet pbufs */
380     pbuf_free(p);
381     IP6_STATS_INC(ip6.lenerr);
382     IP6_STATS_INC(ip6.drop);
383     return ERR_OK;
384   }
385
386   /* Trim pbuf. This should have been done at the netif layer,
387    * but we'll do it anyway just to be sure that its done. */
388   pbuf_realloc(p, IP6_HLEN + IP6H_PLEN(ip6hdr));
389
390   /* copy IP addresses to aligned ip6_addr_t */
391   ip6_addr_copy(ip_data.current_iphdr_dest.ip6, ip6hdr->dest);
392   ip6_addr_copy(ip_data.current_iphdr_src.ip6, ip6hdr->src);
393
394   /* current header pointer. */
395   ip_data.current_ip6_header = ip6hdr;
396
397   /* match packet against an interface, i.e. is this packet for us? */
398   if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
399     /* Always joined to multicast if-local and link-local all-nodes group. */
400     if (ip6_addr_isallnodes_iflocal(ip6_current_dest_addr()) ||
401         ip6_addr_isallnodes_linklocal(ip6_current_dest_addr())) {
402       netif = inp;
403     }
404 #if LWIP_IPV6_MLD
405     else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
406       netif = inp;
407     }
408 #else /* LWIP_IPV6_MLD */
409     else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) {
410       /* Accept all solicited node packets when MLD is not enabled
411        * (for Neighbor discovery). */
412       netif = inp;
413     }
414 #endif /* LWIP_IPV6_MLD */
415     else {
416       netif = NULL;
417     }
418   }
419   else {
420     /* start trying with inp. if that's not acceptable, start walking the
421        list of configured netifs.
422        'first' is used as a boolean to mark whether we started walking the list */
423     int first = 1;
424     netif = inp;
425     do {
426       /* interface is up? */
427       if (netif_is_up(netif)) {
428         /* unicast to this interface address? address configured? */
429         for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
430           if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
431               ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(netif, i))) {
432             /* exit outer loop */
433             goto netif_found;
434           }
435         }
436       }
437       if (first) {
438         first = 0;
439         netif = netif_list;
440       } else {
441         netif = netif->next;
442       }
443       if (netif == inp) {
444         netif = netif->next;
445       }
446     } while(netif != NULL);
447 netif_found:
448     LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
449         netif->name[0], netif->name[1]));
450   }
451
452   /* "::" packet source address? (used in duplicate address detection) */
453   if (ip6_addr_isany(ip6_current_src_addr()) &&
454       (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) {
455     /* packet source is not valid */
456     /* free (drop) packet pbufs */
457     LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n"));
458     pbuf_free(p);
459     IP6_STATS_INC(ip6.drop);
460     goto ip6_input_cleanup;
461   }
462
463   /* packet not for us? */
464   if (netif == NULL) {
465     /* packet not for us, route or discard */
466     LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_TRACE, ("ip6_input: packet not for us.\n"));
467 #if LWIP_IPV6_FORWARD
468     /* non-multicast packet? */
469     if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
470       /* try to forward IP packet on (other) interfaces */
471       ip6_forward(p, ip6hdr, inp);
472     }
473 #endif /* LWIP_IPV6_FORWARD */
474     pbuf_free(p);
475     goto ip6_input_cleanup;
476   }
477
478   /* current netif pointer. */
479   ip_data.current_netif = inp;
480
481   /* Save next header type. */
482   nexth = IP6H_NEXTH(ip6hdr);
483
484   /* Init header length. */
485   hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
486
487   /* Move to payload. */
488   pbuf_header(p, -IP6_HLEN);
489
490   /* Process known option extension headers, if present. */
491   while (nexth != IP6_NEXTH_NONE)
492   {
493     switch (nexth) {
494     case IP6_NEXTH_HOPBYHOP:
495       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n"));
496       /* Get next header type. */
497       nexth = *((u8_t *)p->payload);
498
499       /* Get the header length. */
500       hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
501       ip_data.current_ip_header_tot_len += hlen;
502
503       /* Skip over this header. */
504       if (hlen > p->len) {
505         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
506           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
507               hlen, p->len));
508         /* free (drop) packet pbufs */
509         pbuf_free(p);
510         IP6_STATS_INC(ip6.lenerr);
511         IP6_STATS_INC(ip6.drop);
512         goto ip6_input_cleanup;
513       }
514
515       pbuf_header(p, -hlen);
516       break;
517     case IP6_NEXTH_DESTOPTS:
518       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n"));
519       /* Get next header type. */
520       nexth = *((u8_t *)p->payload);
521
522       /* Get the header length. */
523       hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
524       ip_data.current_ip_header_tot_len += hlen;
525
526       /* Skip over this header. */
527       if (hlen > p->len) {
528         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
529           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
530               hlen, p->len));
531         /* free (drop) packet pbufs */
532         pbuf_free(p);
533         IP6_STATS_INC(ip6.lenerr);
534         IP6_STATS_INC(ip6.drop);
535         goto ip6_input_cleanup;
536       }
537
538       pbuf_header(p, -hlen);
539       break;
540     case IP6_NEXTH_ROUTING:
541       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n"));
542       /* Get next header type. */
543       nexth = *((u8_t *)p->payload);
544
545       /* Get the header length. */
546       hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
547       ip_data.current_ip_header_tot_len += hlen;
548
549       /* Skip over this header. */
550       if (hlen > p->len) {
551         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
552           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
553               hlen, p->len));
554         /* free (drop) packet pbufs */
555         pbuf_free(p);
556         IP6_STATS_INC(ip6.lenerr);
557         IP6_STATS_INC(ip6.drop);
558         goto ip6_input_cleanup;
559       }
560
561       pbuf_header(p, -hlen);
562       break;
563
564     case IP6_NEXTH_FRAGMENT:
565     {
566       struct ip6_frag_hdr * frag_hdr;
567       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
568
569       frag_hdr = (struct ip6_frag_hdr *)p->payload;
570
571       /* Get next header type. */
572       nexth = frag_hdr->_nexth;
573
574       /* Fragment Header length. */
575       hlen = 8;
576       ip_data.current_ip_header_tot_len += hlen;
577
578       /* Make sure this header fits in current pbuf. */
579       if (hlen > p->len) {
580         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
581           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
582               hlen, p->len));
583         /* free (drop) packet pbufs */
584         pbuf_free(p);
585         IP6_FRAG_STATS_INC(ip6_frag.lenerr);
586         IP6_FRAG_STATS_INC(ip6_frag.drop);
587         goto ip6_input_cleanup;
588       }
589
590       /* Offset == 0 and more_fragments == 0? */
591       if (((frag_hdr->_fragment_offset & IP6_FRAG_OFFSET_MASK) == 0) &&
592           ((frag_hdr->_fragment_offset & IP6_FRAG_MORE_FLAG) == 0)) {
593
594         /* This is a 1-fragment packet, usually a packet that we have
595          * already reassembled. Skip this header anc continue. */
596         pbuf_header(p, -hlen);
597       }
598       else {
599 #if LWIP_IPV6_REASS
600
601         /* reassemble the packet */
602         p = ip6_reass(p);
603         /* packet not fully reassembled yet? */
604         if (p == NULL) {
605           goto ip6_input_cleanup;
606         }
607
608         /* Returned p point to IPv6 header.
609          * Update all our variables and pointers and continue. */
610         ip6hdr = (struct ip6_hdr *)p->payload;
611         nexth = IP6H_NEXTH(ip6hdr);
612         hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
613         pbuf_header(p, -IP6_HLEN);
614
615 #else /* LWIP_IPV6_REASS */
616         /* free (drop) packet pbufs */
617         LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n"));
618         pbuf_free(p);
619         IP6_STATS_INC(ip6.opterr);
620         IP6_STATS_INC(ip6.drop);
621         goto ip6_input_cleanup;
622 #endif /* LWIP_IPV6_REASS */
623       }
624       break;
625     }
626     default:
627       goto options_done;
628       break;
629     }
630   }
631 options_done:
632
633   /* p points to IPv6 header again. */
634   pbuf_header(p, ip_data.current_ip_header_tot_len);
635
636   /* send to upper layers */
637   LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
638   ip6_debug_print(p);
639   LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
640
641 #if LWIP_RAW
642   /* raw input did not eat the packet? */
643   if (raw_input(p, inp) == 0)
644 #endif /* LWIP_RAW */
645   {
646     switch (nexth) {
647     case IP6_NEXTH_NONE:
648       pbuf_free(p);
649       break;
650 #if LWIP_UDP
651     case IP6_NEXTH_UDP:
652 #if LWIP_UDPLITE
653     case IP6_NEXTH_UDPLITE:
654 #endif /* LWIP_UDPLITE */
655       /* Point to payload. */
656       pbuf_header(p, -ip_data.current_ip_header_tot_len);
657       udp_input(p, inp);
658       break;
659 #endif /* LWIP_UDP */
660 #if LWIP_TCP
661     case IP6_NEXTH_TCP:
662       /* Point to payload. */
663       pbuf_header(p, -ip_data.current_ip_header_tot_len);
664       tcp_input(p, inp);
665       break;
666 #endif /* LWIP_TCP */
667 #if LWIP_ICMP6
668     case IP6_NEXTH_ICMP6:
669       /* Point to payload. */
670       pbuf_header(p, -ip_data.current_ip_header_tot_len);
671       icmp6_input(p, inp);
672       break;
673 #endif /* LWIP_ICMP */
674     default:
675 #if LWIP_ICMP6
676       /* send ICMP parameter problem unless it was a multicast or ICMPv6 */
677       if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
678           (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
679         icmp6_param_problem(p, ICMP6_PP_HEADER, ip_data.current_ip_header_tot_len - hlen);
680       }
681 #endif /* LWIP_ICMP */
682       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", IP6H_NEXTH(ip6hdr)));
683       pbuf_free(p);
684       IP6_STATS_INC(ip6.proterr);
685       IP6_STATS_INC(ip6.drop);
686       break;
687     }
688   }
689
690 ip6_input_cleanup:
691   ip_data.current_netif = NULL;
692   ip_data.current_ip6_header = NULL;
693   ip_data.current_ip_header_tot_len = 0;
694   ip6_addr_set_any(&ip_data.current_iphdr_src.ip6);
695   ip6_addr_set_any(&ip_data.current_iphdr_dest.ip6);
696
697   return ERR_OK;
698 }
699
700
701 /**
702  * Sends an IPv6 packet on a network interface. This function constructs
703  * the IPv6 header. If the source IPv6 address is NULL, the IPv6 "ANY" address is
704  * used as source (usually during network startup). If the source IPv6 address it
705  * IP6_ADDR_ANY, the most appropriate IPv6 address of the outgoing network
706  * interface is filled in as source address. If the destination IPv6 address is
707  * IP_HDRINCL, p is assumed to already include an IPv6 header and p->payload points
708  * to it instead of the data.
709  *
710  * @param p the packet to send (p->payload points to the data, e.g. next
711             protocol header; if dest == IP_HDRINCL, p already includes an
712             IPv6 header and p->payload points to that IPv6 header)
713  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
714  *         IP address of the netif is selected and used as source address.
715  *         if src == NULL, IP6_ADDR_ANY is used as source)
716  * @param dest the destination IPv6 address to send the packet to
717  * @param hl the Hop Limit value to be set in the IPv6 header
718  * @param tc the Traffic Class value to be set in the IPv6 header
719  * @param nexth the Next Header to be set in the IPv6 header
720  * @param netif the netif on which to send this packet
721  * @return ERR_OK if the packet was sent OK
722  *         ERR_BUF if p doesn't have enough space for IPv6/LINK headers
723  *         returns errors returned by netif->output
724  */
725 err_t
726 ip6_output_if(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
727              u8_t hl, u8_t tc,
728              u8_t nexth, struct netif *netif)
729 {
730   struct ip6_hdr *ip6hdr;
731   ip6_addr_t dest_addr;
732
733   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
734      gets altered as the packet is passed down the stack */
735   LWIP_ASSERT("p->ref == 1", p->ref == 1);
736
737   /* Should the IPv6 header be generated or is it already included in p? */
738   if (dest != IP_HDRINCL) {
739     /* generate IPv6 header */
740     if (pbuf_header(p, IP6_HLEN)) {
741       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
742       IP6_STATS_INC(ip6.err);
743       return ERR_BUF;
744     }
745
746     ip6hdr = (struct ip6_hdr *)p->payload;
747     LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr",
748                (p->len >= sizeof(struct ip6_hdr)));
749
750     IP6H_HOPLIM_SET(ip6hdr, hl);
751     IP6H_NEXTH_SET(ip6hdr, nexth);
752
753     /* dest cannot be NULL here */
754     ip6_addr_copy(ip6hdr->dest, *dest);
755
756     IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
757     IP6H_PLEN_SET(ip6hdr, p->tot_len - IP6_HLEN);
758
759     if (src == NULL) {
760       src = IP6_ADDR_ANY;
761     }
762     else if (ip6_addr_isany(src)) {
763       src = ip6_select_source_address(netif, dest);
764       if ((src == NULL) || ip6_addr_isany(src)) {
765         /* No appropriate source address was found for this packet. */
766         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n"));
767         IP6_STATS_INC(ip6.rterr);
768         return ERR_RTE;
769       }
770     }
771     /* src cannot be NULL here */
772     ip6_addr_copy(ip6hdr->src, *src);
773
774   } else {
775     /* IP header already included in p */
776     ip6hdr = (struct ip6_hdr *)p->payload;
777     ip6_addr_copy(dest_addr, ip6hdr->dest);
778     dest = &dest_addr;
779   }
780
781   IP6_STATS_INC(ip6.xmit);
782
783   LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
784   ip6_debug_print(p);
785
786 #if ENABLE_LOOPBACK
787   /* TODO implement loopback for v6
788   if (ip6_addr_cmp(dest, netif_ip6_addr(0))) {
789     return netif_loop_output(netif, p, dest);
790   }*/
791 #endif /* ENABLE_LOOPBACK */
792 #if LWIP_IPV6_FRAG
793   /* don't fragment if interface has mtu set to 0 [loopif] */
794   if (netif->mtu && (p->tot_len > nd6_get_destination_mtu(dest, netif))) {
795     return ip6_frag(p, netif, dest);
796   }
797 #endif /* LWIP_IPV6_FRAG */
798
799   LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()"));
800   return netif->output_ip6(netif, p, dest);
801 }
802
803 /**
804  * Simple interface to ip6_output_if. It finds the outgoing network
805  * interface and calls upon ip6_output_if to do the actual work.
806  *
807  * @param p the packet to send (p->payload points to the data, e.g. next
808             protocol header; if dest == IP_HDRINCL, p already includes an
809             IPv6 header and p->payload points to that IPv6 header)
810  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
811  *         IP address of the netif is selected and used as source address.
812  *         if src == NULL, IP6_ADDR_ANY is used as source)
813  * @param dest the destination IPv6 address to send the packet to
814  * @param hl the Hop Limit value to be set in the IPv6 header
815  * @param tc the Traffic Class value to be set in the IPv6 header
816  * @param nexth the Next Header to be set in the IPv6 header
817  *
818  * @return ERR_RTE if no route is found
819  *         see ip_output_if() for more return values
820  */
821 err_t
822 ip6_output(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
823           u8_t hl, u8_t tc, u8_t nexth)
824 {
825   struct netif *netif;
826
827   /* pbufs passed to IPv6 must have a ref-count of 1 as their payload pointer
828      gets altered as the packet is passed down the stack */
829   LWIP_ASSERT("p->ref == 1", p->ref == 1);
830
831   if ((netif = ip6_route(src, dest)) == NULL) {
832     LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
833         IP6_ADDR_BLOCK1(dest),
834         IP6_ADDR_BLOCK2(dest),
835         IP6_ADDR_BLOCK3(dest),
836         IP6_ADDR_BLOCK4(dest),
837         IP6_ADDR_BLOCK5(dest),
838         IP6_ADDR_BLOCK6(dest),
839         IP6_ADDR_BLOCK7(dest),
840         IP6_ADDR_BLOCK8(dest)));
841     IP6_STATS_INC(ip6.rterr);
842     return ERR_RTE;
843   }
844
845   return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
846 }
847
848
849 #if LWIP_NETIF_HWADDRHINT
850 /** Like ip6_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
851  *  before calling ip6_output_if.
852  *
853  * @param p the packet to send (p->payload points to the data, e.g. next
854             protocol header; if dest == IP_HDRINCL, p already includes an
855             IPv6 header and p->payload points to that IPv6 header)
856  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
857  *         IP address of the netif is selected and used as source address.
858  *         if src == NULL, IP6_ADDR_ANY is used as source)
859  * @param dest the destination IPv6 address to send the packet to
860  * @param hl the Hop Limit value to be set in the IPv6 header
861  * @param tc the Traffic Class value to be set in the IPv6 header
862  * @param nexth the Next Header to be set in the IPv6 header
863  * @param addr_hint address hint pointer set to netif->addr_hint before
864  *        calling ip_output_if()
865  *
866  * @return ERR_RTE if no route is found
867  *         see ip_output_if() for more return values
868  */
869 err_t
870 ip6_output_hinted(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
871           u8_t hl, u8_t tc, u8_t nexth, u8_t *addr_hint)
872 {
873   struct netif *netif;
874   err_t err;
875
876   /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
877      gets altered as the packet is passed down the stack */
878   LWIP_ASSERT("p->ref == 1", p->ref == 1);
879
880   if ((netif = ip6_route(src, dest)) == NULL) {
881     LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
882         IP6_ADDR_BLOCK1(dest),
883         IP6_ADDR_BLOCK2(dest),
884         IP6_ADDR_BLOCK3(dest),
885         IP6_ADDR_BLOCK4(dest),
886         IP6_ADDR_BLOCK5(dest),
887         IP6_ADDR_BLOCK6(dest),
888         IP6_ADDR_BLOCK7(dest),
889         IP6_ADDR_BLOCK8(dest)));
890     IP6_STATS_INC(ip6.rterr);
891     return ERR_RTE;
892   }
893
894   NETIF_SET_HWADDRHINT(netif, addr_hint);
895   err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
896   NETIF_SET_HWADDRHINT(netif, NULL);
897
898   return err;
899 }
900 #endif /* LWIP_NETIF_HWADDRHINT*/
901
902 #if LWIP_IPV6_MLD
903 /**
904  * Add a hop-by-hop options header with a router alert option and padding.
905  *
906  * Used by MLD when sending a Multicast listener report/done message.
907  *
908  * @param p the packet to which we will prepend the options header
909  * @param nexth the next header protocol number (e.g. IP6_NEXTH_ICMP6)
910  * @param value the value of the router alert option data (e.g. IP6_ROUTER_ALERT_VALUE_MLD)
911  * @return ERR_OK if hop-by-hop header was added, ERR_* otherwise
912  */
913 err_t
914 ip6_options_add_hbh_ra(struct pbuf * p, u8_t nexth, u8_t value)
915 {
916   struct ip6_hbh_hdr * hbh_hdr;
917
918   /* Move pointer to make room for hop-by-hop options header. */
919   if (pbuf_header(p, sizeof(struct ip6_hbh_hdr))) {
920     LWIP_DEBUGF(IP6_DEBUG, ("ip6_options: no space for options header\n"));
921     IP6_STATS_INC(ip6.err);
922     return ERR_BUF;
923   }
924
925   hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
926
927   /* Set fields. */
928   hbh_hdr->_nexth = nexth;
929   hbh_hdr->_hlen = 0;
930   hbh_hdr->_ra_opt_type = IP6_ROUTER_ALERT_OPTION;
931   hbh_hdr->_ra_opt_dlen = 2;
932   hbh_hdr->_ra_opt_data = value;
933   hbh_hdr->_padn_opt_type = IP6_PADN_ALERT_OPTION;
934   hbh_hdr->_padn_opt_dlen = 0;
935
936   return ERR_OK;
937 }
938 #endif /* LWIP_IPV6_MLD */
939
940 #if IP6_DEBUG
941 /* Print an IPv6 header by using LWIP_DEBUGF
942  * @param p an IPv6 packet, p->payload pointing to the IPv6 header
943  */
944 void
945 ip6_debug_print(struct pbuf *p)
946 {
947   struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
948
949   LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n"));
950   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
951   LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" |  %3"U16_F"  |      %7"U32_F"     | (ver, class, flow)\n",
952                     IP6H_V(ip6hdr),
953                     IP6H_TC(ip6hdr),
954                     IP6H_FL(ip6hdr)));
955   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
956   LWIP_DEBUGF(IP6_DEBUG, ("|     %5"U16_F"     |  %3"U16_F"  |  %3"U16_F"  | (plen, nexth, hopl)\n",
957                     ntohs(IP6H_PLEN(ip6hdr)),
958                     ntohs(IP6H_NEXTH(ip6hdr)),
959                     ntohs(IP6H_HOPLIM(ip6hdr))));
960   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
961   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" | (src)\n",
962                     IP6_ADDR_BLOCK1(&(ip6hdr->src)),
963                     IP6_ADDR_BLOCK2(&(ip6hdr->src)),
964                     IP6_ADDR_BLOCK3(&(ip6hdr->src)),
965                     IP6_ADDR_BLOCK4(&(ip6hdr->src))));
966   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |\n",
967                     IP6_ADDR_BLOCK5(&(ip6hdr->src)),
968                     IP6_ADDR_BLOCK6(&(ip6hdr->src)),
969                     IP6_ADDR_BLOCK7(&(ip6hdr->src)),
970                     IP6_ADDR_BLOCK8(&(ip6hdr->src))));
971   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
972   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" | (dest)\n",
973                     IP6_ADDR_BLOCK1(&(ip6hdr->dest)),
974                     IP6_ADDR_BLOCK2(&(ip6hdr->dest)),
975                     IP6_ADDR_BLOCK3(&(ip6hdr->dest)),
976                     IP6_ADDR_BLOCK4(&(ip6hdr->dest))));
977   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |\n",
978                     IP6_ADDR_BLOCK5(&(ip6hdr->dest)),
979                     IP6_ADDR_BLOCK6(&(ip6hdr->dest)),
980                     IP6_ADDR_BLOCK7(&(ip6hdr->dest)),
981                     IP6_ADDR_BLOCK8(&(ip6hdr->dest))));
982   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
983 }
984 #endif /* IP6_DEBUG */
985
986 #endif /* LWIP_IPV6 */