xref: /minix/minix/lib/liblwip/dist/src/core/ipv6/ip6.c (revision ef8d499e)
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 #include "lwip/opt.h"
43 
44 #if LWIP_IPV6  /* don't build if not configured for use in lwipopts.h */
45 
46 #include "lwip/def.h"
47 #include "lwip/mem.h"
48 #include "lwip/netif.h"
49 #include "lwip/ip.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/priv/tcp_priv.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 #ifdef LWIP_HOOK_FILENAME
64 #include LWIP_HOOK_FILENAME
65 #endif
66 
67 /**
68  * Finds the appropriate network interface for a given IPv6 address. It tries to select
69  * a netif following a sequence of heuristics:
70  * 1) if there is only 1 netif, return it
71  * 2) if the destination is a zoned address, match its zone to a netif
72  * 3) if the either the source or destination address is a scoped address,
73  *    match the source address's zone (if set) or address (if not) to a netif
74  * 4) tries to match the destination subnet to a configured address
75  * 5) tries to find a router-announced route
76  * 6) tries to match the (unscoped) source address to the netif
77  * 7) returns the default netif, if configured
78  *
79  * Note that each of the two given addresses may or may not be properly zoned.
80  *
81  * @param src the source IPv6 address, if known
82  * @param dest the destination IPv6 address for which to find the route
83  * @return the netif on which to send to reach dest
84  */
85 struct netif *
86 __weak
87 ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest)
88 {
89 #if LWIP_SINGLE_NETIF
90   LWIP_UNUSED_ARG(src);
91   LWIP_UNUSED_ARG(dest);
92 #else /* LWIP_SINGLE_NETIF */
93   struct netif *netif;
94   s8_t i;
95 
96   /* If single netif configuration, fast return. */
97   if ((netif_list != NULL) && (netif_list->next == NULL)) {
98     if (!netif_is_up(netif_list) || !netif_is_link_up(netif_list) ||
99         (ip6_addr_has_zone(dest) && !ip6_addr_test_zone(dest, netif_list))) {
100       return NULL;
101     }
102     return netif_list;
103   }
104 
105 #if LWIP_IPV6_SCOPES
106   /* Special processing for zoned destination addresses. This includes link-
107    * local unicast addresses and interface/link-local multicast addresses. Use
108    * the zone to find a matching netif. If the address is not zoned, then there
109    * is technically no "wrong" netif to choose, and we leave routing to other
110    * rules; in most cases this should be the scoped-source rule below. */
111   if (ip6_addr_has_zone(dest)) {
112     IP6_ADDR_ZONECHECK(dest);
113     /* Find a netif based on the zone. For custom mappings, one zone may map
114      * to multiple netifs, so find one that can actually send a packet. */
115     for (netif = netif_list; netif != NULL; netif = netif->next) {
116       if (ip6_addr_test_zone(dest, netif) &&
117           netif_is_up(netif) && netif_is_link_up(netif)) {
118         return netif;
119       }
120     }
121     /* No matching netif found. Do no try to route to a different netif,
122      * as that would be a zone violation, resulting in any packets sent to
123      * that netif being dropped on output. */
124     return NULL;
125   }
126 #endif /* LWIP_IPV6_SCOPES */
127 
128   /* Special processing for scoped source and destination addresses. If we get
129    * here, the destination address does not have a zone, so either way we need
130    * to look at the source address, which may or may not have a zone. If it
131    * does, the zone is restrictive: there is (typically) only one matching
132    * netif for it, and we should avoid routing to any other netif as that would
133    * result in guaranteed zone violations. For scoped source addresses that do
134    * not have a zone, use (only) a netif that has that source address locally
135    * assigned. This case also applies to the loopback source address, which has
136    * an implied link-local scope. If only the destination address is scoped
137    * (but, again, not zoned), we still want to use only the source address to
138    * determine its zone because that's most likely what the user/application
139    * wants, regardless of whether the source address is scoped. Finally, some
140    * of this story also applies if scoping is disabled altogether. */
141 #if LWIP_IPV6_SCOPES
142   if (ip6_addr_has_scope(dest, IP6_UNKNOWN) ||
143       ip6_addr_has_scope(src, IP6_UNICAST) ||
144 #else /* LWIP_IPV6_SCOPES */
145   if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_iflocal(dest) ||
146       ip6_addr_ismulticast_linklocal(dest) || ip6_addr_islinklocal(src) ||
147 #endif /* LWIP_IPV6_SCOPES */
148       ip6_addr_isloopback(src)) {
149 #if LWIP_IPV6_SCOPES
150     if (ip6_addr_has_zone(src)) {
151       /* Find a netif matching the source zone (relatively cheap). */
152       for (netif = netif_list; netif != NULL; netif = netif->next) {
153         if (netif_is_up(netif) && netif_is_link_up(netif) &&
154             ip6_addr_test_zone(src, netif)) {
155           return netif;
156         }
157       }
158     } else
159 #endif /* LWIP_IPV6_SCOPES */
160     {
161       /* Find a netif matching the source address (relatively expensive). */
162       for (netif = netif_list; netif != NULL; netif = netif->next) {
163         if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
164           continue;
165         }
166         for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
167           if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
168               ip6_addr_cmp_zoneless(src, netif_ip6_addr(netif, i))) {
169             return netif;
170           }
171         }
172       }
173     }
174     /* Again, do not use any other netif in this case, as that could result in
175      * zone boundary violations. */
176     return NULL;
177   }
178 
179   /* We come here only if neither source nor destination is scoped. */
180   IP6_ADDR_ZONECHECK(src);
181 
182 #ifdef LWIP_HOOK_IP6_ROUTE
183   netif = LWIP_HOOK_IP6_ROUTE(src, dest);
184   if (netif != NULL) {
185     return netif;
186   }
187 #endif
188 
189   /* See if the destination subnet matches a configured address. In accordance
190    * with RFC 5942, dynamically configured addresses do not have an implied
191    * local subnet, and thus should be considered /128 assignments. However, as
192    * such, the destination address may still match a local address, and so we
193    * still need to check for exact matches here. By (lwIP) policy, statically
194    * configured addresses do always have an implied local /64 subnet. */
195   for (netif = netif_list; netif != NULL; netif = netif->next) {
196     if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
197       continue;
198     }
199     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
200       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
201           ip6_addr_netcmp(dest, netif_ip6_addr(netif, i)) &&
202           (netif_ip6_addr_isstatic(netif, i) ||
203           ip6_addr_nethostcmp(dest, netif_ip6_addr(netif, i)))) {
204         return netif;
205       }
206     }
207   }
208 
209   /* Get the netif for a suitable router-announced route. */
210   netif = nd6_find_route(dest);
211   if (netif != NULL) {
212     return netif;
213   }
214 
215   /* Try with the netif that matches the source address. Given the earlier rule
216    * for scoped source addresses, this applies to unscoped addresses only. */
217   if (!ip6_addr_isany(src)) {
218     for (netif = netif_list; netif != NULL; netif = netif->next) {
219       if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
220         continue;
221       }
222       for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
223         if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
224             ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
225           return netif;
226         }
227       }
228     }
229   }
230 
231 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
232   /* loopif is disabled, loopback traffic is passed through any netif */
233   if (ip6_addr_isloopback(dest)) {
234     /* don't check for link on loopback traffic */
235     if (netif_default != NULL && netif_is_up(netif_default)) {
236       return netif_default;
237     }
238     /* default netif is not up, just use any netif for loopback traffic */
239     for (netif = netif_list; netif != NULL; netif = netif->next) {
240       if (netif_is_up(netif)) {
241         return netif;
242       }
243     }
244     return NULL;
245   }
246 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
247 #endif /* !LWIP_SINGLE_NETIF */
248 
249   /* no matching netif found, use default netif, if up */
250   if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default)) {
251     return NULL;
252   }
253   return netif_default;
254 }
255 
256 /**
257  * @ingroup ip6
258  * Select the best IPv6 source address for a given destination IPv6 address.
259  *
260  * This implementation follows RFC 6724 Sec. 5 to the following extent:
261  * - Rules 1, 2, 3: fully implemented
262  * - Rules 4, 5, 5.5: not applicable
263  * - Rule 6: not implemented
264  * - Rule 7: not applicable
265  * - Rule 8: limited to "prefer /64 subnet match over non-match"
266  *
267  * For Rule 2, we deliberately deviate from RFC 6724 Sec. 3.1 by considering
268  * ULAs to be of smaller scope than global addresses, to avoid that a preferred
269  * ULA is picked over a deprecated global address when given a global address
270  * as destination, as that would likely result in broken two-way communication.
271  *
272  * As long as temporary addresses are not supported (as used in Rule 7), a
273  * proper implementation of Rule 8 would obviate the need to implement Rule 6.
274  *
275  * @param netif the netif on which to send a packet
276  * @param dest the destination we are trying to reach (possibly not properly
277  *             zoned)
278  * @return the most suitable source address to use, or NULL if no suitable
279  *         source address is found
280  */
281 const ip_addr_t *
282 __weak
283 ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
284 {
285   const ip_addr_t *best_addr;
286   const ip6_addr_t *cand_addr;
287   s8_t dest_scope, cand_scope;
288   s8_t best_scope = IP6_MULTICAST_SCOPE_RESERVED;
289   u8_t i, cand_pref, cand_bits;
290   u8_t best_pref = 0;
291   u8_t best_bits = 0;
292 
293   /* Start by determining the scope of the given destination address. These
294    * tests are hopefully (roughly) in order of likeliness to match. */
295   if (ip6_addr_isglobal(dest)) {
296     dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
297   } else if (ip6_addr_islinklocal(dest) || ip6_addr_isloopback(dest)) {
298     dest_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
299   } else if (ip6_addr_isuniquelocal(dest)) {
300     dest_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
301   } else if (ip6_addr_ismulticast(dest)) {
302     dest_scope = ip6_addr_multicast_scope(dest);
303   } else if (ip6_addr_issitelocal(dest)) {
304     dest_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
305   } else {
306     /* no match, consider scope global */
307     dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
308   }
309 
310   best_addr = NULL;
311 
312   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
313     /* Consider only valid (= preferred and deprecated) addresses. */
314     if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
315       continue;
316     }
317     /* Determine the scope of this candidate address. Same ordering idea. */
318     cand_addr = netif_ip6_addr(netif, i);
319     if (ip6_addr_isglobal(cand_addr)) {
320       cand_scope = IP6_MULTICAST_SCOPE_GLOBAL;
321     } else if (ip6_addr_islinklocal(cand_addr)) {
322       cand_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
323     } else if (ip6_addr_isuniquelocal(cand_addr)) {
324       cand_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
325     } else if (ip6_addr_issitelocal(cand_addr)) {
326       cand_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
327     } else {
328       /* no match, treat as low-priority global scope */
329       cand_scope = IP6_MULTICAST_SCOPE_RESERVEDF;
330     }
331     cand_pref = ip6_addr_ispreferred(netif_ip6_addr_state(netif, i));
332     /* @todo compute the actual common bits, for longest matching prefix. */
333     /* We cannot count on the destination address having a proper zone
334      * assignment, so do not compare zones in this case. */
335     cand_bits = ip6_addr_netcmp_zoneless(cand_addr, dest); /* just 1 or 0 for now */
336     if (cand_bits && ip6_addr_nethostcmp(cand_addr, dest)) {
337       return netif_ip_addr6(netif, i); /* Rule 1 */
338     }
339     if ((best_addr == NULL) || /* no alternative yet */
340         ((cand_scope < best_scope) && (cand_scope >= dest_scope)) ||
341         ((cand_scope > best_scope) && (best_scope < dest_scope)) || /* Rule 2 */
342         ((cand_scope == best_scope) && ((cand_pref > best_pref) || /* Rule 3 */
343         ((cand_pref == best_pref) && (cand_bits > best_bits))))) { /* Rule 8 */
344       /* We found a new "winning" candidate. */
345       best_addr = netif_ip_addr6(netif, i);
346       best_scope = cand_scope;
347       best_pref = cand_pref;
348       best_bits = cand_bits;
349     }
350   }
351 
352   return best_addr; /* may be NULL */
353 }
354 
355 #if LWIP_IPV6_FORWARD
356 /**
357  * Forwards an IPv6 packet. It finds an appropriate route for the
358  * packet, decrements the HL value of the packet, and outputs
359  * the packet on the appropriate interface.
360  *
361  * @param p the packet to forward (p->payload points to IP header)
362  * @param iphdr the IPv6 header of the input packet
363  * @param inp the netif on which this packet was received
364  */
365 static void
366 ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
367 {
368   struct netif *netif;
369 
370 #if defined(__minix)
371   /* MINIX 3 only: forward packets only when enabled through configuration. */
372   if (!lwip_ip6_forward) {
373     return;
374   }
375 #endif /* defined(__minix) */
376 
377   /* do not forward link-local or loopback addresses */
378   if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
379       ip6_addr_isloopback(ip6_current_dest_addr())) {
380     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n"));
381     IP6_STATS_INC(ip6.rterr);
382     IP6_STATS_INC(ip6.drop);
383     return;
384   }
385 
386   /* Find network interface where to forward this IP packet to. */
387   netif = ip6_route(IP6_ADDR_ANY6, ip6_current_dest_addr());
388   if (netif == NULL) {
389     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",
390         IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
391         IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
392         IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
393         IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
394         IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
395         IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
396         IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
397         IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
398 #if LWIP_ICMP6
399     /* Don't send ICMP messages in response to ICMP messages */
400     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
401       icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
402     }
403 #endif /* LWIP_ICMP6 */
404     IP6_STATS_INC(ip6.rterr);
405     IP6_STATS_INC(ip6.drop);
406     return;
407   }
408 #if LWIP_IPV6_SCOPES
409   /* Do not forward packets with a zoned (e.g., link-local) source address
410    * outside of their zone. We determined the zone a bit earlier, so we know
411    * that the address is properly zoned here, so we can safely use has_zone.
412    * Also skip packets with a loopback source address (link-local implied). */
413   if ((ip6_addr_has_zone(ip6_current_src_addr()) &&
414       !ip6_addr_test_zone(ip6_current_src_addr(), netif)) ||
415       ip6_addr_isloopback(ip6_current_src_addr())) {
416     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding packet beyond its source address zone.\n"));
417     IP6_STATS_INC(ip6.rterr);
418     IP6_STATS_INC(ip6.drop);
419     return;
420   }
421 #endif /* LWIP_IPV6_SCOPES */
422   /* Do not forward packets onto the same network interface on which
423    * they arrived. */
424   if (netif == inp) {
425     LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not bouncing packets back on incoming interface.\n"));
426     IP6_STATS_INC(ip6.rterr);
427     IP6_STATS_INC(ip6.drop);
428     return;
429   }
430 
431   /* decrement HL */
432   IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
433   /* send ICMP6 if HL == 0 */
434   if (IP6H_HOPLIM(iphdr) == 0) {
435 #if LWIP_ICMP6
436     /* Don't send ICMP messages in response to ICMP messages */
437     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
438       icmp6_time_exceeded(p, ICMP6_TE_HL);
439     }
440 #endif /* LWIP_ICMP6 */
441     IP6_STATS_INC(ip6.drop);
442     return;
443   }
444 
445   if (netif->mtu && (p->tot_len > netif->mtu)) {
446 #if LWIP_ICMP6
447     /* Don't send ICMP messages in response to ICMP messages */
448     if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
449       icmp6_packet_too_big(p, netif->mtu);
450     }
451 #endif /* LWIP_ICMP6 */
452     IP6_STATS_INC(ip6.drop);
453     return;
454   }
455 
456   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",
457       IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
458       IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
459       IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
460       IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
461       IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
462       IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
463       IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
464       IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
465 
466   /* transmit pbuf on chosen interface */
467   netif->output_ip6(netif, p, ip6_current_dest_addr());
468   IP6_STATS_INC(ip6.fw);
469   IP6_STATS_INC(ip6.xmit);
470   return;
471 }
472 #endif /* LWIP_IPV6_FORWARD */
473 
474 /** Return true if the current input packet should be accepted on this netif */
475 static int
476 ip6_input_accept(struct netif *netif)
477 {
478   /* interface is up? */
479   if (netif_is_up(netif)) {
480     u8_t i;
481     /* unicast to this interface address? address configured? */
482     /* If custom scopes are used, the destination zone will be tested as
483       * part of the local-address comparison, but we need to test the source
484       * scope as well (e.g., is this interface on the same link?). */
485     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
486       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
487           ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(netif, i))
488 #if IPV6_CUSTOM_SCOPES
489           && (!ip6_addr_has_zone(ip6_current_src_addr()) ||
490               ip6_addr_test_zone(ip6_current_src_addr(), netif))
491 #endif /* IPV6_CUSTOM_SCOPES */
492       ) {
493         /* accept on this netif */
494         return 1;
495       }
496     }
497   }
498   return 0;
499 }
500 
501 /**
502  * This function is called by the network interface device driver when
503  * an IPv6 packet is received. The function does the basic checks of the
504  * IP header such as packet size being at least larger than the header
505  * size etc. If the packet was not destined for us, the packet is
506  * forwarded (using ip6_forward).
507  *
508  * Finally, the packet is sent to the upper layer protocol input function.
509  *
510  * @param p the received IPv6 packet (p->payload points to IPv6 header)
511  * @param inp the netif on which this packet was received
512  * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
513  *         processed, but currently always returns ERR_OK)
514  */
515 err_t
516 ip6_input(struct pbuf *p, struct netif *inp)
517 {
518   struct ip6_hdr *ip6hdr;
519   struct netif *netif;
520   u8_t nexth;
521   u16_t hlen; /* the current header length */
522 #if 0 /*IP_ACCEPT_LINK_LAYER_ADDRESSING*/
523   @todo
524   int check_ip_src=1;
525 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
526 
527   IP6_STATS_INC(ip6.recv);
528 
529   /* identify the IP header */
530   ip6hdr = (struct ip6_hdr *)p->payload;
531   if (IP6H_V(ip6hdr) != 6) {
532     LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U32_F"\n",
533         IP6H_V(ip6hdr)));
534     pbuf_free(p);
535     IP6_STATS_INC(ip6.err);
536     IP6_STATS_INC(ip6.drop);
537     return ERR_OK;
538   }
539 
540 #ifdef LWIP_HOOK_IP6_INPUT
541   if (LWIP_HOOK_IP6_INPUT(p, inp)) {
542     /* the packet has been eaten */
543     return ERR_OK;
544   }
545 #endif
546 
547   /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
548   if ((IP6_HLEN > p->len) || ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len)) {
549     if (IP6_HLEN > p->len) {
550       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
551         ("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
552             (u16_t)IP6_HLEN, p->len));
553     }
554     if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) {
555       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
556         ("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
557             (u16_t)(IP6H_PLEN(ip6hdr) + IP6_HLEN), p->tot_len));
558     }
559     /* free (drop) packet pbufs */
560     pbuf_free(p);
561     IP6_STATS_INC(ip6.lenerr);
562     IP6_STATS_INC(ip6.drop);
563     return ERR_OK;
564   }
565 
566   /* Trim pbuf. This should have been done at the netif layer,
567    * but we'll do it anyway just to be sure that its done. */
568   pbuf_realloc(p, IP6_HLEN + IP6H_PLEN(ip6hdr));
569 
570   /* copy IP addresses to aligned ip6_addr_t */
571   ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_dest, ip6hdr->dest);
572   ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_src, ip6hdr->src);
573 
574   /* Don't accept virtual IPv4 mapped IPv6 addresses.
575    * Don't accept multicast source addresses. */
576   if (ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_dest)) ||
577      ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_src)) ||
578      ip6_addr_ismulticast(ip_2_ip6(&ip_data.current_iphdr_src))) {
579     IP6_STATS_INC(ip6.err);
580     IP6_STATS_INC(ip6.drop);
581     return ERR_OK;
582   }
583 
584   /* Set the appropriate zone identifier on the addresses. */
585   ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_dest), IP6_UNKNOWN, inp);
586   ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_src), IP6_UNICAST, inp);
587 
588   /* current header pointer. */
589   ip_data.current_ip6_header = ip6hdr;
590 
591   /* In netif, used in case we need to send ICMPv6 packets back. */
592   ip_data.current_netif = inp;
593   ip_data.current_input_netif = inp;
594 
595   /* match packet against an interface, i.e. is this packet for us? */
596   if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
597     /* Always joined to multicast if-local and link-local all-nodes group. */
598     if (ip6_addr_isallnodes_iflocal(ip6_current_dest_addr()) ||
599         ip6_addr_isallnodes_linklocal(ip6_current_dest_addr())) {
600       netif = inp;
601     }
602 #if LWIP_IPV6_MLD
603     else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
604       netif = inp;
605     }
606 #else /* LWIP_IPV6_MLD */
607     else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) {
608       u8_t i;
609       /* Filter solicited node packets when MLD is not enabled
610        * (for Neighbor discovery). */
611       netif = NULL;
612       for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
613         if (ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) &&
614             ip6_addr_cmp_solicitednode(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) {
615           netif = inp;
616           LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: solicited node packet accepted on interface %c%c\n",
617               netif->name[0], netif->name[1]));
618           break;
619         }
620       }
621     }
622 #endif /* LWIP_IPV6_MLD */
623     else {
624       netif = NULL;
625     }
626   } else {
627     /* start trying with inp. if that's not acceptable, start walking the
628        list of configured netifs. */
629     if (ip6_input_accept(inp)) {
630       netif = inp;
631     } else {
632       netif = NULL;
633 #if !IPV6_CUSTOM_SCOPES
634       /* Shortcut: stop looking for other interfaces if either the source or
635         * the destination has a scope constrained to this interface. Custom
636         * scopes may break the 1:1 link/interface mapping, however. */
637       if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
638           ip6_addr_islinklocal(ip6_current_src_addr())) {
639         goto netif_found;
640       }
641 #endif /* !IPV6_CUSTOM_SCOPES */
642 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
643       /* The loopback address is to be considered link-local. Packets to it
644         * should be dropped on other interfaces, as per RFC 4291 Sec. 2.5.3.
645         * Its implied scope means packets *from* the loopback address should
646         * not be accepted on other interfaces, either. These requirements
647         * cannot be implemented in the case that loopback traffic is sent
648         * across a non-loopback interface, however. */
649       if (ip6_addr_isloopback(ip6_current_dest_addr()) ||
650           ip6_addr_isloopback(ip6_current_src_addr())) {
651         goto netif_found;
652       }
653 #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
654 #if !LWIP_SINGLE_NETIF
655       NETIF_FOREACH(netif) {
656         if (netif == inp) {
657           /* we checked that before already */
658           continue;
659         }
660         if (ip6_input_accept(netif)) {
661           break;
662         }
663       }
664 #endif /* !LWIP_SINGLE_NETIF */
665     }
666 netif_found:
667     LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
668         netif ? netif->name[0] : 'X', netif? netif->name[1] : 'X'));
669   }
670 
671   /* "::" packet source address? (used in duplicate address detection) */
672   if (ip6_addr_isany(ip6_current_src_addr()) &&
673       (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) {
674     /* packet source is not valid */
675     /* free (drop) packet pbufs */
676     LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n"));
677     pbuf_free(p);
678     IP6_STATS_INC(ip6.drop);
679     goto ip6_input_cleanup;
680   }
681 
682   /* packet not for us? */
683   if (netif == NULL) {
684     /* packet not for us, route or discard */
685     LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_TRACE, ("ip6_input: packet not for us.\n"));
686 #if LWIP_IPV6_FORWARD
687     /* non-multicast packet? */
688     if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
689       /* try to forward IP packet on (other) interfaces */
690       ip6_forward(p, ip6hdr, inp);
691     }
692 #endif /* LWIP_IPV6_FORWARD */
693     pbuf_free(p);
694     goto ip6_input_cleanup;
695   }
696 
697   /* current netif pointer. */
698   ip_data.current_netif = netif;
699 
700   /* Save next header type. */
701   nexth = IP6H_NEXTH(ip6hdr);
702 
703   /* Init header length. */
704   hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
705 
706   /* Move to payload. */
707   pbuf_header(p, -IP6_HLEN);
708 
709   /* Process known option extension headers, if present. */
710   while (nexth != IP6_NEXTH_NONE)
711   {
712     switch (nexth) {
713     case IP6_NEXTH_HOPBYHOP:
714       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n"));
715       /* Get and check the header length, while staying in packet bounds. */
716       if ((p->len < 8) ||
717           ((hlen = 8 * (1 + *((u8_t *)p->payload + 1))) > p->len)) {
718         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
719           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
720               hlen, p->len));
721         /* free (drop) packet pbufs */
722         pbuf_free(p);
723         IP6_STATS_INC(ip6.lenerr);
724         IP6_STATS_INC(ip6.drop);
725         goto ip6_input_cleanup;
726       }
727 
728       ip_data.current_ip_header_tot_len += hlen;
729 
730       /* Get next header type. */
731       nexth = *((u8_t *)p->payload);
732 
733       /* Skip over this header. */
734       pbuf_header(p, -(s16_t)hlen);
735       break;
736     case IP6_NEXTH_DESTOPTS:
737       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n"));
738 
739       /* Get and check the header length, while staying in packet bounds. */
740       if ((p->len < 8) ||
741           (hlen = 8 * (1 + *((u8_t *)p->payload + 1))) > p->len) {
742         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
743           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
744               hlen, p->len));
745         /* free (drop) packet pbufs */
746         pbuf_free(p);
747         IP6_STATS_INC(ip6.lenerr);
748         IP6_STATS_INC(ip6.drop);
749         goto ip6_input_cleanup;
750       }
751 
752       ip_data.current_ip_header_tot_len += hlen;
753 
754       /* Get next header type. */
755       nexth = *((u8_t *)p->payload);
756 
757       /* Skip over this header. */
758       pbuf_header(p, -(s16_t)hlen);
759       break;
760     case IP6_NEXTH_ROUTING:
761       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n"));
762 
763       /* Get and check the header length, while staying in packet bounds. */
764       if ((p->len < 8) ||
765           (hlen = 8 * (1 + *((u8_t *)p->payload + 1))) > p->len) {
766         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
767           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
768               hlen, p->len));
769         /* free (drop) packet pbufs */
770         pbuf_free(p);
771         IP6_STATS_INC(ip6.lenerr);
772         IP6_STATS_INC(ip6.drop);
773         goto ip6_input_cleanup;
774       }
775 
776       /* Get next header type. */
777       nexth = *((u8_t *)p->payload);
778 
779       /* Skip over this header. */
780       ip_data.current_ip_header_tot_len += hlen;
781 
782       pbuf_header(p, -(s16_t)hlen);
783       break;
784 
785     case IP6_NEXTH_FRAGMENT:
786     {
787       struct ip6_frag_hdr *frag_hdr;
788       LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
789 
790       /* Fragment Header length. */
791       hlen = 8;
792 
793       /* Make sure this header fits in current pbuf. */
794       if (hlen > p->len) {
795         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
796           ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
797               hlen, p->len));
798         /* free (drop) packet pbufs */
799         pbuf_free(p);
800         IP6_FRAG_STATS_INC(ip6_frag.lenerr);
801         IP6_FRAG_STATS_INC(ip6_frag.drop);
802         goto ip6_input_cleanup;
803       }
804 
805       ip_data.current_ip_header_tot_len += hlen;
806 
807       frag_hdr = (struct ip6_frag_hdr *)p->payload;
808 
809       /* Get next header type. */
810       nexth = frag_hdr->_nexth;
811 
812       /* Offset == 0 and more_fragments == 0? */
813       if ((frag_hdr->_fragment_offset &
814            PP_HTONS(IP6_FRAG_OFFSET_MASK | IP6_FRAG_MORE_FLAG)) == 0) {
815         /* This is a 1-fragment packet. Skip this header and continue. */
816         pbuf_header(p, -(s16_t)hlen);
817       } else {
818 #if LWIP_IPV6_REASS
819 
820         /* reassemble the packet */
821         p = ip6_reass(p);
822         /* packet not fully reassembled yet? */
823         if (p == NULL) {
824           goto ip6_input_cleanup;
825         }
826 
827         /* Returned p point to IPv6 header.
828          * Update all our variables and pointers and continue. */
829         ip6hdr = (struct ip6_hdr *)p->payload;
830         nexth = IP6H_NEXTH(ip6hdr);
831         hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
832         pbuf_header(p, -IP6_HLEN);
833 
834 #else /* LWIP_IPV6_REASS */
835         /* free (drop) packet pbufs */
836         LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n"));
837         pbuf_free(p);
838         IP6_STATS_INC(ip6.opterr);
839         IP6_STATS_INC(ip6.drop);
840         goto ip6_input_cleanup;
841 #endif /* LWIP_IPV6_REASS */
842       }
843       break;
844     }
845     default:
846       goto options_done;
847       break;
848     }
849   }
850 options_done:
851 
852   /* p points to IPv6 header again. */
853   pbuf_header_force(p, (s16_t)ip_data.current_ip_header_tot_len);
854 
855   /* send to upper layers */
856   LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
857   ip6_debug_print(p);
858   LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
859 
860 #if LWIP_RAW
861   /* raw input did not eat the packet? */
862   if (raw_input(p, inp) == 0)
863 #endif /* LWIP_RAW */
864   {
865     switch (nexth) {
866     case IP6_NEXTH_NONE:
867       pbuf_free(p);
868       break;
869 #if LWIP_UDP
870     case IP6_NEXTH_UDP:
871 #if LWIP_UDPLITE
872     case IP6_NEXTH_UDPLITE:
873 #endif /* LWIP_UDPLITE */
874       /* Point to payload. */
875       pbuf_header(p, -(s16_t)ip_data.current_ip_header_tot_len);
876       udp_input(p, inp);
877       break;
878 #endif /* LWIP_UDP */
879 #if LWIP_TCP
880     case IP6_NEXTH_TCP:
881       /* Point to payload. */
882       pbuf_header(p, -(s16_t)ip_data.current_ip_header_tot_len);
883       tcp_input(p, inp);
884       break;
885 #endif /* LWIP_TCP */
886 #if LWIP_ICMP6
887     case IP6_NEXTH_ICMP6:
888       /* Point to payload. */
889       pbuf_header(p, -(s16_t)ip_data.current_ip_header_tot_len);
890       icmp6_input(p, inp);
891       break;
892 #endif /* LWIP_ICMP */
893     default:
894 #if LWIP_ICMP6
895       /* send ICMP parameter problem unless it was a multicast or ICMPv6 */
896       if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
897           (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
898         icmp6_param_problem(p, ICMP6_PP_HEADER, ip_data.current_ip_header_tot_len - hlen);
899       }
900 #endif /* LWIP_ICMP */
901       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", (u16_t)IP6H_NEXTH(ip6hdr)));
902       pbuf_free(p);
903       IP6_STATS_INC(ip6.proterr);
904       IP6_STATS_INC(ip6.drop);
905       break;
906     }
907   }
908 
909 ip6_input_cleanup:
910   ip_data.current_netif = NULL;
911   ip_data.current_input_netif = NULL;
912   ip_data.current_ip6_header = NULL;
913   ip_data.current_ip_header_tot_len = 0;
914   ip6_addr_set_zero(ip6_current_src_addr());
915   ip6_addr_set_zero(ip6_current_dest_addr());
916 
917   return ERR_OK;
918 }
919 
920 
921 /**
922  * Sends an IPv6 packet on a network interface. This function constructs
923  * the IPv6 header. If the source IPv6 address is NULL, the IPv6 "ANY" address is
924  * used as source (usually during network startup). If the source IPv6 address it
925  * IP6_ADDR_ANY, the most appropriate IPv6 address of the outgoing network
926  * interface is filled in as source address. If the destination IPv6 address is
927  * LWIP_IP_HDRINCL, p is assumed to already include an IPv6 header and
928  * p->payload points to it instead of the data.
929  *
930  * @param p the packet to send (p->payload points to the data, e.g. next
931             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
932             IPv6 header and p->payload points to that IPv6 header)
933  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
934  *         IP address of the netif is selected and used as source address.
935  *         if src == NULL, IP6_ADDR_ANY is used as source) (src is possibly not
936  *         properly zoned)
937  * @param dest the destination IPv6 address to send the packet to (possibly not
938  *             properly zoned)
939  * @param hl the Hop Limit value to be set in the IPv6 header
940  * @param tc the Traffic Class value to be set in the IPv6 header
941  * @param nexth the Next Header to be set in the IPv6 header
942  * @param netif the netif on which to send this packet
943  * @return ERR_OK if the packet was sent OK
944  *         ERR_BUF if p doesn't have enough space for IPv6/LINK headers
945  *         returns errors returned by netif->output
946  */
947 err_t
948 ip6_output_if(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
949              u8_t hl, u8_t tc,
950              u8_t nexth, struct netif *netif)
951 {
952   const ip6_addr_t *src_used = src;
953   if (dest != LWIP_IP_HDRINCL) {
954     if (src != NULL && ip6_addr_isany(src)) {
955       src_used = ip_2_ip6(ip6_select_source_address(netif, dest));
956       if ((src_used == NULL) || ip6_addr_isany(src_used)) {
957         /* No appropriate source address was found for this packet. */
958         LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n"));
959         IP6_STATS_INC(ip6.rterr);
960         return ERR_RTE;
961       }
962     }
963   }
964   return ip6_output_if_src(p, src_used, dest, hl, tc, nexth, netif);
965 }
966 
967 /**
968  * Same as ip6_output_if() but 'src' address is not replaced by netif address
969  * when it is 'any'.
970  */
971 err_t
972 ip6_output_if_src(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
973              u8_t hl, u8_t tc,
974              u8_t nexth, struct netif *netif)
975 {
976   struct ip6_hdr *ip6hdr;
977   ip6_addr_t dest_addr;
978 
979   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
980 
981   /* Should the IPv6 header be generated or is it already included in p? */
982   if (dest != LWIP_IP_HDRINCL) {
983 #if LWIP_IPV6_SCOPES
984     /* If the destination address is scoped but lacks a zone, add a zone now,
985      * based on the outgoing interface. The lower layers (e.g., nd6) absolutely
986      * require addresses to be properly zoned for correctness. In some cases,
987      * earlier attempts will have been made to add a zone to the destination,
988      * but this function is the only one that is called in all (other) cases,
989      * so we must do this here. */
990     if (ip6_addr_lacks_zone(dest, IP6_UNKNOWN)) {
991       ip6_addr_copy(dest_addr, *dest);
992       ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
993       dest = &dest_addr;
994     }
995 #endif /* LWIP_IPV6_SCOPES */
996 
997     /* generate IPv6 header */
998     if (pbuf_header(p, IP6_HLEN)) {
999       LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
1000       IP6_STATS_INC(ip6.err);
1001       return ERR_BUF;
1002     }
1003 
1004     ip6hdr = (struct ip6_hdr *)p->payload;
1005     LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr",
1006                (p->len >= sizeof(struct ip6_hdr)));
1007 
1008     IP6H_HOPLIM_SET(ip6hdr, hl);
1009     IP6H_NEXTH_SET(ip6hdr, nexth);
1010 
1011     /* dest cannot be NULL here */
1012     ip6_addr_copy_to_packed(ip6hdr->dest, *dest);
1013 
1014     IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
1015     IP6H_PLEN_SET(ip6hdr, p->tot_len - IP6_HLEN);
1016 
1017     if (src == NULL) {
1018       src = IP6_ADDR_ANY6;
1019     }
1020     /* src cannot be NULL here */
1021     ip6_addr_copy_to_packed(ip6hdr->src, *src);
1022 
1023   } else {
1024     /* IP header already included in p */
1025     ip6hdr = (struct ip6_hdr *)p->payload;
1026     ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
1027     ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
1028     dest = &dest_addr;
1029   }
1030 
1031   IP6_STATS_INC(ip6.xmit);
1032 
1033   LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
1034   ip6_debug_print(p);
1035 
1036 #if ENABLE_LOOPBACK
1037   {
1038     int i;
1039 #if !LWIP_HAVE_LOOPIF
1040     if (ip6_addr_isloopback(dest)) {
1041       return netif_loop_output(netif, p);
1042     }
1043 #endif /* !LWIP_HAVE_LOOPIF */
1044     for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1045       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
1046           ip6_addr_cmp(dest, netif_ip6_addr(netif, i))) {
1047         /* Packet to self, enqueue it for loopback */
1048         LWIP_DEBUGF(IP6_DEBUG, ("netif_loop_output()\n"));
1049         return netif_loop_output(netif, p);
1050       }
1051     }
1052   }
1053 #if LWIP_MULTICAST_TX_OPTIONS
1054   if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
1055     netif_loop_output(netif, p);
1056   }
1057 #endif /* LWIP_MULTICAST_TX_OPTIONS */
1058 #endif /* ENABLE_LOOPBACK */
1059 #if LWIP_IPV6_FRAG
1060   /* don't fragment if interface has mtu set to 0 [loopif] */
1061   if (netif->mtu && (p->tot_len > nd6_get_destination_mtu(dest, netif))) {
1062     return ip6_frag(p, netif, dest);
1063   }
1064 #endif /* LWIP_IPV6_FRAG */
1065 
1066   LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()\n"));
1067   return netif->output_ip6(netif, p, dest);
1068 }
1069 
1070 /**
1071  * Simple interface to ip6_output_if. It finds the outgoing network
1072  * interface and calls upon ip6_output_if to do the actual work.
1073  *
1074  * @param p the packet to send (p->payload points to the data, e.g. next
1075             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1076             IPv6 header and p->payload points to that IPv6 header)
1077  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
1078  *         IP address of the netif is selected and used as source address.
1079  *         if src == NULL, IP6_ADDR_ANY is used as source)
1080  * @param dest the destination IPv6 address to send the packet to
1081  * @param hl the Hop Limit value to be set in the IPv6 header
1082  * @param tc the Traffic Class value to be set in the IPv6 header
1083  * @param nexth the Next Header to be set in the IPv6 header
1084  *
1085  * @return ERR_RTE if no route is found
1086  *         see ip_output_if() for more return values
1087  */
1088 err_t
1089 ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
1090           u8_t hl, u8_t tc, u8_t nexth)
1091 {
1092   struct netif *netif;
1093   struct ip6_hdr *ip6hdr;
1094   ip6_addr_t src_addr, dest_addr;
1095 
1096   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1097 
1098   if (dest != LWIP_IP_HDRINCL) {
1099     netif = ip6_route(src, dest);
1100   } else {
1101     /* IP header included in p, read addresses. */
1102     ip6hdr = (struct ip6_hdr *)p->payload;
1103     ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
1104     ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
1105     netif = ip6_route(&src_addr, &dest_addr);
1106   }
1107 
1108   if (netif == NULL) {
1109     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",
1110         IP6_ADDR_BLOCK1(dest),
1111         IP6_ADDR_BLOCK2(dest),
1112         IP6_ADDR_BLOCK3(dest),
1113         IP6_ADDR_BLOCK4(dest),
1114         IP6_ADDR_BLOCK5(dest),
1115         IP6_ADDR_BLOCK6(dest),
1116         IP6_ADDR_BLOCK7(dest),
1117         IP6_ADDR_BLOCK8(dest)));
1118     IP6_STATS_INC(ip6.rterr);
1119     return ERR_RTE;
1120   }
1121 
1122   return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
1123 }
1124 
1125 
1126 #if LWIP_NETIF_HWADDRHINT
1127 /** Like ip6_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
1128  *  before calling ip6_output_if.
1129  *
1130  * @param p the packet to send (p->payload points to the data, e.g. next
1131             protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1132             IPv6 header and p->payload points to that IPv6 header)
1133  * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
1134  *         IP address of the netif is selected and used as source address.
1135  *         if src == NULL, IP6_ADDR_ANY is used as source)
1136  * @param dest the destination IPv6 address to send the packet to
1137  * @param hl the Hop Limit value to be set in the IPv6 header
1138  * @param tc the Traffic Class value to be set in the IPv6 header
1139  * @param nexth the Next Header to be set in the IPv6 header
1140  * @param addr_hint address hint pointer set to netif->addr_hint before
1141  *        calling ip_output_if()
1142  *
1143  * @return ERR_RTE if no route is found
1144  *         see ip_output_if() for more return values
1145  */
1146 err_t
1147 ip6_output_hinted(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
1148           u8_t hl, u8_t tc, u8_t nexth, u8_t *addr_hint)
1149 {
1150   struct netif *netif;
1151   struct ip6_hdr *ip6hdr;
1152   ip6_addr_t src_addr, dest_addr;
1153   err_t err;
1154 
1155   LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1156 
1157   if (dest != LWIP_IP_HDRINCL) {
1158     netif = ip6_route(src, dest);
1159   } else {
1160     /* IP header included in p, read addresses. */
1161     ip6hdr = (struct ip6_hdr *)p->payload;
1162     ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
1163     ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
1164     netif = ip6_route(&src_addr, &dest_addr);
1165   }
1166 
1167   if (netif == NULL) {
1168     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",
1169         IP6_ADDR_BLOCK1(dest),
1170         IP6_ADDR_BLOCK2(dest),
1171         IP6_ADDR_BLOCK3(dest),
1172         IP6_ADDR_BLOCK4(dest),
1173         IP6_ADDR_BLOCK5(dest),
1174         IP6_ADDR_BLOCK6(dest),
1175         IP6_ADDR_BLOCK7(dest),
1176         IP6_ADDR_BLOCK8(dest)));
1177     IP6_STATS_INC(ip6.rterr);
1178     return ERR_RTE;
1179   }
1180 
1181   NETIF_SET_HWADDRHINT(netif, addr_hint);
1182   err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
1183   NETIF_SET_HWADDRHINT(netif, NULL);
1184 
1185   return err;
1186 }
1187 #endif /* LWIP_NETIF_HWADDRHINT*/
1188 
1189 #if LWIP_IPV6_MLD
1190 /**
1191  * Add a hop-by-hop options header with a router alert option and padding.
1192  *
1193  * Used by MLD when sending a Multicast listener report/done message.
1194  *
1195  * @param p the packet to which we will prepend the options header
1196  * @param nexth the next header protocol number (e.g. IP6_NEXTH_ICMP6)
1197  * @param value the value of the router alert option data (e.g. IP6_ROUTER_ALERT_VALUE_MLD)
1198  * @return ERR_OK if hop-by-hop header was added, ERR_* otherwise
1199  */
1200 err_t
1201 ip6_options_add_hbh_ra(struct pbuf *p, u8_t nexth, u8_t value)
1202 {
1203   struct ip6_hbh_hdr *hbh_hdr;
1204 
1205   /* Move pointer to make room for hop-by-hop options header. */
1206   if (pbuf_header(p, sizeof(struct ip6_hbh_hdr))) {
1207     LWIP_DEBUGF(IP6_DEBUG, ("ip6_options: no space for options header\n"));
1208     IP6_STATS_INC(ip6.err);
1209     return ERR_BUF;
1210   }
1211 
1212   hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
1213 
1214   /* Set fields. */
1215   hbh_hdr->_nexth = nexth;
1216   hbh_hdr->_hlen = 0;
1217   hbh_hdr->_ra_opt_type = IP6_ROUTER_ALERT_OPTION;
1218   hbh_hdr->_ra_opt_dlen = 2;
1219   hbh_hdr->_ra_opt_data = value;
1220   hbh_hdr->_padn_opt_type = IP6_PADN_ALERT_OPTION;
1221   hbh_hdr->_padn_opt_dlen = 0;
1222 
1223   return ERR_OK;
1224 }
1225 #endif /* LWIP_IPV6_MLD */
1226 
1227 #if IP6_DEBUG
1228 /* Print an IPv6 header by using LWIP_DEBUGF
1229  * @param p an IPv6 packet, p->payload pointing to the IPv6 header
1230  */
1231 void
1232 ip6_debug_print(struct pbuf *p)
1233 {
1234   struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
1235 
1236   LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n"));
1237   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
1238   LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" |  %3"U16_F"  |      %7"U32_F"     | (ver, class, flow)\n",
1239                     IP6H_V(ip6hdr),
1240                     IP6H_TC(ip6hdr),
1241                     IP6H_FL(ip6hdr)));
1242   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
1243   LWIP_DEBUGF(IP6_DEBUG, ("|     %5"U16_F"     |  %3"U16_F"  |  %3"U16_F"  | (plen, nexth, hopl)\n",
1244                     IP6H_PLEN(ip6hdr),
1245                     IP6H_NEXTH(ip6hdr),
1246                     IP6H_HOPLIM(ip6hdr)));
1247   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
1248   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" | (src)\n",
1249                     IP6_ADDR_BLOCK1(&(ip6hdr->src)),
1250                     IP6_ADDR_BLOCK2(&(ip6hdr->src)),
1251                     IP6_ADDR_BLOCK3(&(ip6hdr->src)),
1252                     IP6_ADDR_BLOCK4(&(ip6hdr->src))));
1253   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |\n",
1254                     IP6_ADDR_BLOCK5(&(ip6hdr->src)),
1255                     IP6_ADDR_BLOCK6(&(ip6hdr->src)),
1256                     IP6_ADDR_BLOCK7(&(ip6hdr->src)),
1257                     IP6_ADDR_BLOCK8(&(ip6hdr->src))));
1258   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
1259   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" | (dest)\n",
1260                     IP6_ADDR_BLOCK1(&(ip6hdr->dest)),
1261                     IP6_ADDR_BLOCK2(&(ip6hdr->dest)),
1262                     IP6_ADDR_BLOCK3(&(ip6hdr->dest)),
1263                     IP6_ADDR_BLOCK4(&(ip6hdr->dest))));
1264   LWIP_DEBUGF(IP6_DEBUG, ("|  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |  %4"X32_F" |\n",
1265                     IP6_ADDR_BLOCK5(&(ip6hdr->dest)),
1266                     IP6_ADDR_BLOCK6(&(ip6hdr->dest)),
1267                     IP6_ADDR_BLOCK7(&(ip6hdr->dest)),
1268                     IP6_ADDR_BLOCK8(&(ip6hdr->dest))));
1269   LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
1270 }
1271 #endif /* IP6_DEBUG */
1272 
1273 #endif /* LWIP_IPV6 */
1274