8 * Copyright (c) 2010 Inico Technologies Ltd.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Ivan Delamer <delamer@inicotech.com>
38 * Please coordinate changes and requests with Ivan Delamer
39 * <delamer@inicotech.com>
45 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
49 #include "lwip/netif.h"
51 #include "lwip/ip6_addr.h"
52 #include "lwip/ip6_frag.h"
53 #include "lwip/icmp6.h"
56 #include "lwip/tcp_impl.h"
57 #include "lwip/dhcp6.h"
59 #include "lwip/mld6.h"
60 #include "lwip/debug.h"
61 #include "lwip/stats.h"
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;
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
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
92 ip6_route(struct ip6_addr *src, struct ip6_addr *dest)
97 /* If single netif configuration, fast return. */
98 if ((netif_list != NULL) && (netif_list->next == NULL)) {
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;
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))) {
119 /* netif not found, use default netif */
120 return netif_default;
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))) {
133 /* Get the netif for a suitable router. */
134 i = nd6_select_router(dest, NULL);
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;
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))) {
155 /* no matching netif found, use default netif */
156 return netif_default;
160 * Select the best IPv6 source address for a given destination
161 * IPv6 address. Loosely follows RFC 3484. "Strong host" behavior
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
170 ip6_select_source_address(struct netif *netif, ip6_addr_t * dest)
172 ip6_addr_t * src = NULL;
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);
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);
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);
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))) {
213 src = netif_ip6_addr(netif, i);
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);
233 #if LWIP_IPV6_FORWARD
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.
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
244 ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
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);
256 /* Find network interface where to forward this IP packet to. */
257 netif = ip6_route(IP6_ADDR_ANY, ip6_current_dest_addr());
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())));
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);
273 #endif /* LWIP_ICMP6 */
274 IP6_STATS_INC(ip6.rterr);
275 IP6_STATS_INC(ip6.drop);
278 /* Do not forward packets onto the same network interface on which
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);
288 IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
289 /* send ICMP6 if HL == 0 */
290 if (IP6H_HOPLIM(iphdr) == 0) {
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);
296 #endif /* LWIP_ICMP6 */
297 IP6_STATS_INC(ip6.drop);
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())));
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);
317 #endif /* LWIP_IPV6_FORWARD */
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).
327 * Finally, the packet is sent to the upper layer protocol input function.
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)
335 ip6_input(struct pbuf *p, struct netif *inp)
337 struct ip6_hdr *ip6hdr;
340 u16_t hlen; /* the current header length */
342 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
344 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
346 IP6_STATS_INC(ip6.recv);
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",
354 IP6_STATS_INC(ip6.err);
355 IP6_STATS_INC(ip6.drop);
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",
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));
371 /* free (drop) packet pbufs */
373 IP6_STATS_INC(ip6.lenerr);
374 IP6_STATS_INC(ip6.drop);
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));
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);
386 /* current header pointer. */
387 current_ip6_header = ip6hdr;
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())) {
397 else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
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). */
406 #endif /* LWIP_IPV6_MLD */
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 */
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 */
438 } while(netif != NULL);
440 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
441 netif->name[0], netif->name[1]));
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"));
451 IP6_STATS_INC(ip6.drop);
452 goto ip6_input_cleanup;
455 /* packet not for us? */
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);
465 #endif /* LWIP_IPV6_FORWARD */
467 goto ip6_input_cleanup;
470 /* current netif pointer. */
473 /* Save next header type. */
474 nexth = IP6H_NEXTH(ip6hdr);
476 /* Init header length. */
477 hlen = current_ip6_header_tot_len = IP6_HLEN;
479 /* Move to payload. */
480 pbuf_header(p, -IP6_HLEN);
482 /* Process known option extension headers, if present. */
483 while (nexth != IP6_NEXTH_NONE)
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);
491 /* Get the header length. */
492 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
493 current_ip6_header_tot_len += hlen;
495 /* Skip over this header. */
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",
500 /* free (drop) packet pbufs */
502 IP6_STATS_INC(ip6.lenerr);
503 IP6_STATS_INC(ip6.drop);
504 goto ip6_input_cleanup;
507 pbuf_header(p, -hlen);
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);
514 /* Get the header length. */
515 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
516 current_ip6_header_tot_len += hlen;
518 /* Skip over this header. */
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",
523 /* free (drop) packet pbufs */
525 IP6_STATS_INC(ip6.lenerr);
526 IP6_STATS_INC(ip6.drop);
527 goto ip6_input_cleanup;
530 pbuf_header(p, -hlen);
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);
537 /* Get the header length. */
538 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
539 current_ip6_header_tot_len += hlen;
541 /* Skip over this header. */
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",
546 /* free (drop) packet pbufs */
548 IP6_STATS_INC(ip6.lenerr);
549 IP6_STATS_INC(ip6.drop);
550 goto ip6_input_cleanup;
553 pbuf_header(p, -hlen);
556 case IP6_NEXTH_FRAGMENT:
558 struct ip6_frag_hdr * frag_hdr;
559 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
561 frag_hdr = (struct ip6_frag_hdr *)p->payload;
563 /* Get next header type. */
564 nexth = frag_hdr->_nexth;
566 /* Fragment Header length. */
568 current_ip6_header_tot_len += hlen;
570 /* Make sure this header fits in current pbuf. */
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",
575 /* free (drop) packet pbufs */
577 IP6_FRAG_STATS_INC(ip6_frag.lenerr);
578 IP6_FRAG_STATS_INC(ip6_frag.drop);
579 goto ip6_input_cleanup;
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)) {
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);
593 /* reassemble the packet */
595 /* packet not fully reassembled yet? */
597 goto ip6_input_cleanup;
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);
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"));
611 IP6_STATS_INC(ip6.opterr);
612 IP6_STATS_INC(ip6.drop);
613 goto ip6_input_cleanup;
614 #endif /* LWIP_IPV6_REASS */
625 /* p points to IPv6 header again. */
626 pbuf_header(p, current_ip6_header_tot_len);
628 /* send to upper layers */
629 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
631 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
634 /* raw input did not eat the packet? */
635 if (raw_input(p, inp) == 0)
636 #endif /* LWIP_RAW */
645 case IP6_NEXTH_UDPLITE:
646 #endif /* LWIP_UDPLITE */
647 /* Point to payload. */
648 pbuf_header(p, -current_ip6_header_tot_len);
651 #endif /* LWIP_UDP */
654 /* Point to payload. */
655 pbuf_header(p, -current_ip6_header_tot_len);
658 #endif /* LWIP_TCP */
660 case IP6_NEXTH_ICMP6:
661 /* Point to payload. */
662 pbuf_header(p, -current_ip6_header_tot_len);
665 #endif /* LWIP_ICMP */
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);
673 #endif /* LWIP_ICMP */
674 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", IP6H_NEXTH(ip6hdr)));
676 IP6_STATS_INC(ip6.proterr);
677 IP6_STATS_INC(ip6.drop);
683 current_netif = NULL;
684 current_ip6_header = NULL;
685 current_ip6_header_tot_len = 0;
686 ip6_addr_set_any(¤t_ip6hdr_src);
687 ip6_addr_set_any(¤t_ip6hdr_dest);
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.
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
718 ip6_output_if(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
720 u8_t nexth, struct netif *netif)
722 struct ip6_hdr *ip6hdr;
723 ip6_addr_t dest_addr;
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);
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);
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)));
742 IP6H_HOPLIM_SET(ip6hdr, hl);
743 IP6H_NEXTH_SET(ip6hdr, nexth);
745 /* dest cannot be NULL here */
746 ip6_addr_copy(ip6hdr->dest, *dest);
748 IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
749 IP6H_PLEN_SET(ip6hdr, p->tot_len - IP6_HLEN);
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);
763 /* src cannot be NULL here */
764 ip6_addr_copy(ip6hdr->src, *src);
767 /* IP header already included in p */
768 ip6hdr = (struct ip6_hdr *)p->payload;
769 ip6_addr_copy(dest_addr, ip6hdr->dest);
773 IP6_STATS_INC(ip6.xmit);
775 LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
779 /* TODO implement loopback for v6
780 if (ip6_addr_cmp(dest, netif_ip6_addr(0))) {
781 return netif_loop_output(netif, p, dest);
783 #endif /* ENABLE_LOOPBACK */
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);
789 #endif /* LWIP_IPV6_FRAG */
791 LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()"));
792 return netif->output_ip6(netif, p, dest);
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.
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
810 * @return ERR_RTE if no route is found
811 * see ip_output_if() for more return values
814 ip6_output(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
815 u8_t hl, u8_t tc, u8_t nexth)
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);
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);
837 return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
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.
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()
858 * @return ERR_RTE if no route is found
859 * see ip_output_if() for more return values
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)
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);
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);
886 netif->addr_hint = addr_hint;
887 err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
888 netif->addr_hint = NULL;
892 #endif /* LWIP_NETIF_HWADDRHINT*/
896 * Add a hop-by-hop options header with a router alert option and padding.
898 * Used by MLD when sending a Multicast listener report/done message.
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
906 ip6_options_add_hbh_ra(struct pbuf * p, u8_t nexth, u8_t value)
908 struct ip6_hbh_hdr * hbh_hdr;
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);
917 hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
920 hbh_hdr->_nexth = nexth;
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;
930 #endif /* LWIP_IPV6_MLD */
933 /* Print an IPv6 header by using LWIP_DEBUGF
934 * @param p an IPv6 packet, p->payload pointing to the IPv6 header
937 ip6_debug_print(struct pbuf *p)
939 struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
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",
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"));
976 #endif /* IP6_DEBUG */
978 #endif /* LWIP_IPV6 */