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