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"
65 * Finds the appropriate network interface for a given IPv6 address. It tries to select
66 * a netif following a sequence of heuristics:
67 * 1) if there is only 1 netif, return it
68 * 2) if the destination is a link-local address, try to match the src address to a netif.
69 * this is a tricky case because with multiple netifs, link-local addresses only have
70 * meaning within a particular subnet/link.
71 * 3) tries to match the destination subnet to a configured address
72 * 4) tries to find a router
73 * 5) tries to match the source address to the netif
74 * 6) returns the default netif, if configured
76 * @param src the source IPv6 address, if known
77 * @param dest the destination IPv6 address for which to find the route
78 * @return the netif on which to send to reach dest
81 ip6_route(struct ip6_addr *src, struct ip6_addr *dest)
86 /* If single netif configuration, fast return. */
87 if ((netif_list != NULL) && (netif_list->next == NULL)) {
91 /* Special processing for link-local addresses. */
92 if (ip6_addr_islinklocal(dest)) {
93 if (ip6_addr_isany(src)) {
94 /* Use default netif. */
98 /* Try to find the netif for the source address. */
99 for(netif = netif_list; netif != NULL; netif = netif->next) {
100 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
101 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
102 ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
108 /* netif not found, use default netif */
109 return netif_default;
112 /* See if the destination subnet matches a configured address. */
113 for(netif = netif_list; netif != NULL; netif = netif->next) {
114 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
115 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
116 ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
122 /* Get the netif for a suitable router. */
123 i = nd6_select_router(dest, NULL);
125 if (default_router_list[i].neighbor_entry != NULL) {
126 if (default_router_list[i].neighbor_entry->netif != NULL) {
127 return default_router_list[i].neighbor_entry->netif;
132 /* try with the netif that matches the source address. */
133 if (!ip6_addr_isany(src)) {
134 for(netif = netif_list; netif != NULL; netif = netif->next) {
135 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
136 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
137 ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
144 /* no matching netif found, use default netif */
145 return netif_default;
149 * Select the best IPv6 source address for a given destination
150 * IPv6 address. Loosely follows RFC 3484. "Strong host" behavior
153 * @param netif the netif on which to send a packet
154 * @param dest the destination we are trying to reach
155 * @return the most suitable source address to use, or NULL if no suitable
156 * source address is found
159 ip6_select_source_address(struct netif *netif, ip6_addr_t * dest)
161 ip6_addr_t * src = NULL;
164 /* If dest is link-local, choose a link-local source. */
165 if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_linklocal(dest) || ip6_addr_ismulticast_iflocal(dest)) {
166 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
167 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
168 ip6_addr_islinklocal(netif_ip6_addr(netif, i))) {
169 return netif_ip6_addr(netif, i);
174 /* Choose a site-local with matching prefix. */
175 if (ip6_addr_issitelocal(dest) || ip6_addr_ismulticast_sitelocal(dest)) {
176 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
177 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
178 ip6_addr_issitelocal(netif_ip6_addr(netif, i)) &&
179 ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
180 return netif_ip6_addr(netif, i);
185 /* Choose a unique-local with matching prefix. */
186 if (ip6_addr_isuniquelocal(dest) || ip6_addr_ismulticast_orglocal(dest)) {
187 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
188 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
189 ip6_addr_isuniquelocal(netif_ip6_addr(netif, i)) &&
190 ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
191 return netif_ip6_addr(netif, i);
196 /* Choose a global with best matching prefix. */
197 if (ip6_addr_isglobal(dest) || ip6_addr_ismulticast_global(dest)) {
198 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
199 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
200 ip6_addr_isglobal(netif_ip6_addr(netif, i))) {
202 src = netif_ip6_addr(netif, i);
205 /* Replace src only if we find a prefix match. */
206 /* TODO find longest matching prefix. */
207 if ((!(ip6_addr_netcmp(src, dest))) &&
208 ip6_addr_netcmp(netif_ip6_addr(netif, i), dest)) {
209 src = netif_ip6_addr(netif, i);
219 /* Last resort: see if arbitrary prefix matches. */
220 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
221 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
222 ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) {
223 return netif_ip6_addr(netif, i);
230 #if LWIP_IPV6_FORWARD
232 * Forwards an IPv6 packet. It finds an appropriate route for the
233 * packet, decrements the HL value of the packet, and outputs
234 * the packet on the appropriate interface.
236 * @param p the packet to forward (p->payload points to IP header)
237 * @param iphdr the IPv6 header of the input packet
238 * @param inp the netif on which this packet was received
241 ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
245 /* do not forward link-local addresses */
246 if (ip6_addr_islinklocal(ip6_current_dest_addr())) {
247 LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n"));
248 IP6_STATS_INC(ip6.rterr);
249 IP6_STATS_INC(ip6.drop);
253 /* Find network interface where to forward this IP packet to. */
254 netif = ip6_route(IP6_ADDR_ANY, ip6_current_dest_addr());
256 LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
257 IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
258 IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
259 IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
260 IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
261 IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
262 IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
263 IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
264 IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
266 /* Don't send ICMP messages in response to ICMP messages */
267 if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
268 icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
270 #endif /* LWIP_ICMP6 */
271 IP6_STATS_INC(ip6.rterr);
272 IP6_STATS_INC(ip6.drop);
275 /* Do not forward packets onto the same network interface on which
278 LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not bouncing packets back on incoming interface.\n"));
279 IP6_STATS_INC(ip6.rterr);
280 IP6_STATS_INC(ip6.drop);
285 IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
286 /* send ICMP6 if HL == 0 */
287 if (IP6H_HOPLIM(iphdr) == 0) {
289 /* Don't send ICMP messages in response to ICMP messages */
290 if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
291 icmp6_time_exceeded(p, ICMP6_TE_HL);
293 #endif /* LWIP_ICMP6 */
294 IP6_STATS_INC(ip6.drop);
298 if (netif->mtu && (p->tot_len > netif->mtu)) {
300 /* Don't send ICMP messages in response to ICMP messages */
301 if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
302 icmp6_packet_too_big(p, netif->mtu);
304 #endif /* LWIP_ICMP6 */
305 IP6_STATS_INC(ip6.drop);
309 LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: forwarding packet to %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
310 IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
311 IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
312 IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
313 IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
314 IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
315 IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
316 IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
317 IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
319 /* transmit pbuf on chosen interface */
320 netif->output_ip6(netif, p, ip6_current_dest_addr());
321 IP6_STATS_INC(ip6.fw);
322 IP6_STATS_INC(ip6.xmit);
325 #endif /* LWIP_IPV6_FORWARD */
329 * This function is called by the network interface device driver when
330 * an IPv6 packet is received. The function does the basic checks of the
331 * IP header such as packet size being at least larger than the header
332 * size etc. If the packet was not destined for us, the packet is
333 * forwarded (using ip6_forward).
335 * Finally, the packet is sent to the upper layer protocol input function.
337 * @param p the received IPv6 packet (p->payload points to IPv6 header)
338 * @param inp the netif on which this packet was received
339 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
340 * processed, but currently always returns ERR_OK)
343 ip6_input(struct pbuf *p, struct netif *inp)
345 struct ip6_hdr *ip6hdr;
348 u16_t hlen; /* the current header length */
350 #if 0 /*IP_ACCEPT_LINK_LAYER_ADDRESSING*/
353 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
355 IP6_STATS_INC(ip6.recv);
357 /* identify the IP header */
358 ip6hdr = (struct ip6_hdr *)p->payload;
359 if (IP6H_V(ip6hdr) != 6) {
360 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U16_F"\n",
363 IP6_STATS_INC(ip6.err);
364 IP6_STATS_INC(ip6.drop);
368 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
369 if ((IP6_HLEN > p->len) || ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len)) {
370 if (IP6_HLEN > p->len) {
371 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
372 ("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
375 if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) {
376 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
377 ("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
378 IP6H_PLEN(ip6hdr) + IP6_HLEN, p->tot_len));
380 /* free (drop) packet pbufs */
382 IP6_STATS_INC(ip6.lenerr);
383 IP6_STATS_INC(ip6.drop);
387 /* Trim pbuf. This should have been done at the netif layer,
388 * but we'll do it anyway just to be sure that its done. */
389 pbuf_realloc(p, IP6_HLEN + IP6H_PLEN(ip6hdr));
391 /* copy IP addresses to aligned ip6_addr_t */
392 ip6_addr_copy(ip_data.current_iphdr_dest.ip6, ip6hdr->dest);
393 ip6_addr_copy(ip_data.current_iphdr_src.ip6, ip6hdr->src);
395 /* current header pointer. */
396 ip_data.current_ip6_header = ip6hdr;
398 /* match packet against an interface, i.e. is this packet for us? */
399 if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
400 /* Always joined to multicast if-local and link-local all-nodes group. */
401 if (ip6_addr_isallnodes_iflocal(ip6_current_dest_addr()) ||
402 ip6_addr_isallnodes_linklocal(ip6_current_dest_addr())) {
406 else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
409 #else /* LWIP_IPV6_MLD */
410 else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) {
411 /* Accept all solicited node packets when MLD is not enabled
412 * (for Neighbor discovery). */
415 #endif /* LWIP_IPV6_MLD */
421 /* start trying with inp. if that's not acceptable, start walking the
422 list of configured netifs.
423 'first' is used as a boolean to mark whether we started walking the list */
427 /* interface is up? */
428 if (netif_is_up(netif)) {
429 /* unicast to this interface address? address configured? */
430 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
431 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
432 ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(netif, i))) {
433 /* exit outer loop */
447 } while(netif != NULL);
449 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
450 netif->name[0], netif->name[1]));
453 /* "::" packet source address? (used in duplicate address detection) */
454 if (ip6_addr_isany(ip6_current_src_addr()) &&
455 (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) {
456 /* packet source is not valid */
457 /* free (drop) packet pbufs */
458 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n"));
460 IP6_STATS_INC(ip6.drop);
461 goto ip6_input_cleanup;
464 /* packet not for us? */
466 /* packet not for us, route or discard */
467 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_TRACE, ("ip6_input: packet not for us.\n"));
468 #if LWIP_IPV6_FORWARD
469 /* non-multicast packet? */
470 if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
471 /* try to forward IP packet on (other) interfaces */
472 ip6_forward(p, ip6hdr, inp);
474 #endif /* LWIP_IPV6_FORWARD */
476 goto ip6_input_cleanup;
479 /* current netif pointer. */
480 ip_data.current_netif = inp;
482 /* Save next header type. */
483 nexth = IP6H_NEXTH(ip6hdr);
485 /* Init header length. */
486 hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
488 /* Move to payload. */
489 pbuf_header(p, -IP6_HLEN);
491 /* Process known option extension headers, if present. */
492 while (nexth != IP6_NEXTH_NONE)
495 case IP6_NEXTH_HOPBYHOP:
496 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n"));
497 /* Get next header type. */
498 nexth = *((u8_t *)p->payload);
500 /* Get the header length. */
501 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
502 ip_data.current_ip_header_tot_len += hlen;
504 /* Skip over this header. */
506 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
507 ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
509 /* free (drop) packet pbufs */
511 IP6_STATS_INC(ip6.lenerr);
512 IP6_STATS_INC(ip6.drop);
513 goto ip6_input_cleanup;
516 pbuf_header(p, -hlen);
518 case IP6_NEXTH_DESTOPTS:
519 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n"));
520 /* Get next header type. */
521 nexth = *((u8_t *)p->payload);
523 /* Get the header length. */
524 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
525 ip_data.current_ip_header_tot_len += hlen;
527 /* Skip over this header. */
529 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
530 ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
532 /* free (drop) packet pbufs */
534 IP6_STATS_INC(ip6.lenerr);
535 IP6_STATS_INC(ip6.drop);
536 goto ip6_input_cleanup;
539 pbuf_header(p, -hlen);
541 case IP6_NEXTH_ROUTING:
542 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n"));
543 /* Get next header type. */
544 nexth = *((u8_t *)p->payload);
546 /* Get the header length. */
547 hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
548 ip_data.current_ip_header_tot_len += hlen;
550 /* Skip over this header. */
552 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
553 ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
555 /* free (drop) packet pbufs */
557 IP6_STATS_INC(ip6.lenerr);
558 IP6_STATS_INC(ip6.drop);
559 goto ip6_input_cleanup;
562 pbuf_header(p, -hlen);
565 case IP6_NEXTH_FRAGMENT:
567 struct ip6_frag_hdr * frag_hdr;
568 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
570 frag_hdr = (struct ip6_frag_hdr *)p->payload;
572 /* Get next header type. */
573 nexth = frag_hdr->_nexth;
575 /* Fragment Header length. */
577 ip_data.current_ip_header_tot_len += hlen;
579 /* Make sure this header fits in current pbuf. */
581 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
582 ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
584 /* free (drop) packet pbufs */
586 IP6_FRAG_STATS_INC(ip6_frag.lenerr);
587 IP6_FRAG_STATS_INC(ip6_frag.drop);
588 goto ip6_input_cleanup;
591 /* Offset == 0 and more_fragments == 0? */
592 if (((frag_hdr->_fragment_offset & IP6_FRAG_OFFSET_MASK) == 0) &&
593 ((frag_hdr->_fragment_offset & IP6_FRAG_MORE_FLAG) == 0)) {
595 /* This is a 1-fragment packet, usually a packet that we have
596 * already reassembled. Skip this header anc continue. */
597 pbuf_header(p, -hlen);
602 /* reassemble the packet */
604 /* packet not fully reassembled yet? */
606 goto ip6_input_cleanup;
609 /* Returned p point to IPv6 header.
610 * Update all our variables and pointers and continue. */
611 ip6hdr = (struct ip6_hdr *)p->payload;
612 nexth = IP6H_NEXTH(ip6hdr);
613 hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
614 pbuf_header(p, -IP6_HLEN);
616 #else /* LWIP_IPV6_REASS */
617 /* free (drop) packet pbufs */
618 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n"));
620 IP6_STATS_INC(ip6.opterr);
621 IP6_STATS_INC(ip6.drop);
622 goto ip6_input_cleanup;
623 #endif /* LWIP_IPV6_REASS */
634 /* p points to IPv6 header again. */
635 pbuf_header(p, ip_data.current_ip_header_tot_len);
637 /* send to upper layers */
638 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
640 LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
643 /* raw input did not eat the packet? */
644 if (raw_input(p, inp) == 0)
645 #endif /* LWIP_RAW */
654 case IP6_NEXTH_UDPLITE:
655 #endif /* LWIP_UDPLITE */
656 /* Point to payload. */
657 pbuf_header(p, -ip_data.current_ip_header_tot_len);
660 #endif /* LWIP_UDP */
663 /* Point to payload. */
664 pbuf_header(p, -ip_data.current_ip_header_tot_len);
667 #endif /* LWIP_TCP */
669 case IP6_NEXTH_ICMP6:
670 /* Point to payload. */
671 pbuf_header(p, -ip_data.current_ip_header_tot_len);
674 #endif /* LWIP_ICMP */
677 /* send ICMP parameter problem unless it was a multicast or ICMPv6 */
678 if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
679 (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
680 icmp6_param_problem(p, ICMP6_PP_HEADER, ip_data.current_ip_header_tot_len - hlen);
682 #endif /* LWIP_ICMP */
683 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", IP6H_NEXTH(ip6hdr)));
685 IP6_STATS_INC(ip6.proterr);
686 IP6_STATS_INC(ip6.drop);
692 ip_data.current_netif = NULL;
693 ip_data.current_ip6_header = NULL;
694 ip_data.current_ip_header_tot_len = 0;
695 ip6_addr_set_any(&ip_data.current_iphdr_src.ip6);
696 ip6_addr_set_any(&ip_data.current_iphdr_dest.ip6);
703 * Sends an IPv6 packet on a network interface. This function constructs
704 * the IPv6 header. If the source IPv6 address is NULL, the IPv6 "ANY" address is
705 * used as source (usually during network startup). If the source IPv6 address it
706 * IP6_ADDR_ANY, the most appropriate IPv6 address of the outgoing network
707 * interface is filled in as source address. If the destination IPv6 address is
708 * IP_HDRINCL, p is assumed to already include an IPv6 header and p->payload points
709 * to it instead of the data.
711 * @param p the packet to send (p->payload points to the data, e.g. next
712 protocol header; if dest == IP_HDRINCL, p already includes an
713 IPv6 header and p->payload points to that IPv6 header)
714 * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
715 * IP address of the netif is selected and used as source address.
716 * if src == NULL, IP6_ADDR_ANY is used as source)
717 * @param dest the destination IPv6 address to send the packet to
718 * @param hl the Hop Limit value to be set in the IPv6 header
719 * @param tc the Traffic Class value to be set in the IPv6 header
720 * @param nexth the Next Header to be set in the IPv6 header
721 * @param netif the netif on which to send this packet
722 * @return ERR_OK if the packet was sent OK
723 * ERR_BUF if p doesn't have enough space for IPv6/LINK headers
724 * returns errors returned by netif->output
727 ip6_output_if(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
729 u8_t nexth, struct netif *netif)
731 struct ip6_hdr *ip6hdr;
732 ip6_addr_t dest_addr;
734 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
735 gets altered as the packet is passed down the stack */
736 LWIP_ASSERT("p->ref == 1", p->ref == 1);
738 /* Should the IPv6 header be generated or is it already included in p? */
739 if (dest != IP_HDRINCL) {
740 /* generate IPv6 header */
741 if (pbuf_header(p, IP6_HLEN)) {
742 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
743 IP6_STATS_INC(ip6.err);
747 ip6hdr = (struct ip6_hdr *)p->payload;
748 LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr",
749 (p->len >= sizeof(struct ip6_hdr)));
751 IP6H_HOPLIM_SET(ip6hdr, hl);
752 IP6H_NEXTH_SET(ip6hdr, nexth);
754 /* dest cannot be NULL here */
755 ip6_addr_copy(ip6hdr->dest, *dest);
757 IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
758 IP6H_PLEN_SET(ip6hdr, p->tot_len - IP6_HLEN);
763 else if (ip6_addr_isany(src)) {
764 src = ip6_select_source_address(netif, dest);
765 if ((src == NULL) || ip6_addr_isany(src)) {
766 /* No appropriate source address was found for this packet. */
767 LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n"));
768 IP6_STATS_INC(ip6.rterr);
772 /* src cannot be NULL here */
773 ip6_addr_copy(ip6hdr->src, *src);
776 /* IP header already included in p */
777 ip6hdr = (struct ip6_hdr *)p->payload;
778 ip6_addr_copy(dest_addr, ip6hdr->dest);
782 IP6_STATS_INC(ip6.xmit);
784 LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
788 /* TODO implement loopback for v6
789 if (ip6_addr_cmp(dest, netif_ip6_addr(0))) {
790 return netif_loop_output(netif, p, dest);
792 #endif /* ENABLE_LOOPBACK */
794 /* don't fragment if interface has mtu set to 0 [loopif] */
795 if (netif->mtu && (p->tot_len > nd6_get_destination_mtu(dest, netif))) {
796 return ip6_frag(p, netif, dest);
798 #endif /* LWIP_IPV6_FRAG */
800 LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()"));
801 return netif->output_ip6(netif, p, dest);
805 * Simple interface to ip6_output_if. It finds the outgoing network
806 * interface and calls upon ip6_output_if to do the actual work.
808 * @param p the packet to send (p->payload points to the data, e.g. next
809 protocol header; if dest == IP_HDRINCL, p already includes an
810 IPv6 header and p->payload points to that IPv6 header)
811 * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
812 * IP address of the netif is selected and used as source address.
813 * if src == NULL, IP6_ADDR_ANY is used as source)
814 * @param dest the destination IPv6 address to send the packet to
815 * @param hl the Hop Limit value to be set in the IPv6 header
816 * @param tc the Traffic Class value to be set in the IPv6 header
817 * @param nexth the Next Header to be set in the IPv6 header
819 * @return ERR_RTE if no route is found
820 * see ip_output_if() for more return values
823 ip6_output(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
824 u8_t hl, u8_t tc, u8_t nexth)
828 /* pbufs passed to IPv6 must have a ref-count of 1 as their payload pointer
829 gets altered as the packet is passed down the stack */
830 LWIP_ASSERT("p->ref == 1", p->ref == 1);
832 if ((netif = ip6_route(src, dest)) == NULL) {
833 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",
834 IP6_ADDR_BLOCK1(dest),
835 IP6_ADDR_BLOCK2(dest),
836 IP6_ADDR_BLOCK3(dest),
837 IP6_ADDR_BLOCK4(dest),
838 IP6_ADDR_BLOCK5(dest),
839 IP6_ADDR_BLOCK6(dest),
840 IP6_ADDR_BLOCK7(dest),
841 IP6_ADDR_BLOCK8(dest)));
842 IP6_STATS_INC(ip6.rterr);
846 return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
850 #if LWIP_NETIF_HWADDRHINT
851 /** Like ip6_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
852 * before calling ip6_output_if.
854 * @param p the packet to send (p->payload points to the data, e.g. next
855 protocol header; if dest == IP_HDRINCL, p already includes an
856 IPv6 header and p->payload points to that IPv6 header)
857 * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
858 * IP address of the netif is selected and used as source address.
859 * if src == NULL, IP6_ADDR_ANY is used as source)
860 * @param dest the destination IPv6 address to send the packet to
861 * @param hl the Hop Limit value to be set in the IPv6 header
862 * @param tc the Traffic Class value to be set in the IPv6 header
863 * @param nexth the Next Header to be set in the IPv6 header
864 * @param addr_hint address hint pointer set to netif->addr_hint before
865 * calling ip_output_if()
867 * @return ERR_RTE if no route is found
868 * see ip_output_if() for more return values
871 ip6_output_hinted(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
872 u8_t hl, u8_t tc, u8_t nexth, u8_t *addr_hint)
877 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
878 gets altered as the packet is passed down the stack */
879 LWIP_ASSERT("p->ref == 1", p->ref == 1);
881 if ((netif = ip6_route(src, dest)) == NULL) {
882 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",
883 IP6_ADDR_BLOCK1(dest),
884 IP6_ADDR_BLOCK2(dest),
885 IP6_ADDR_BLOCK3(dest),
886 IP6_ADDR_BLOCK4(dest),
887 IP6_ADDR_BLOCK5(dest),
888 IP6_ADDR_BLOCK6(dest),
889 IP6_ADDR_BLOCK7(dest),
890 IP6_ADDR_BLOCK8(dest)));
891 IP6_STATS_INC(ip6.rterr);
895 NETIF_SET_HWADDRHINT(netif, addr_hint);
896 err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
897 NETIF_SET_HWADDRHINT(netif, NULL);
901 #endif /* LWIP_NETIF_HWADDRHINT*/
905 * Add a hop-by-hop options header with a router alert option and padding.
907 * Used by MLD when sending a Multicast listener report/done message.
909 * @param p the packet to which we will prepend the options header
910 * @param nexth the next header protocol number (e.g. IP6_NEXTH_ICMP6)
911 * @param value the value of the router alert option data (e.g. IP6_ROUTER_ALERT_VALUE_MLD)
912 * @return ERR_OK if hop-by-hop header was added, ERR_* otherwise
915 ip6_options_add_hbh_ra(struct pbuf * p, u8_t nexth, u8_t value)
917 struct ip6_hbh_hdr * hbh_hdr;
919 /* Move pointer to make room for hop-by-hop options header. */
920 if (pbuf_header(p, sizeof(struct ip6_hbh_hdr))) {
921 LWIP_DEBUGF(IP6_DEBUG, ("ip6_options: no space for options header\n"));
922 IP6_STATS_INC(ip6.err);
926 hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
929 hbh_hdr->_nexth = nexth;
931 hbh_hdr->_ra_opt_type = IP6_ROUTER_ALERT_OPTION;
932 hbh_hdr->_ra_opt_dlen = 2;
933 hbh_hdr->_ra_opt_data = value;
934 hbh_hdr->_padn_opt_type = IP6_PADN_ALERT_OPTION;
935 hbh_hdr->_padn_opt_dlen = 0;
939 #endif /* LWIP_IPV6_MLD */
942 /* Print an IPv6 header by using LWIP_DEBUGF
943 * @param p an IPv6 packet, p->payload pointing to the IPv6 header
946 ip6_debug_print(struct pbuf *p)
948 struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
950 LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n"));
951 LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
952 LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" | %3"U16_F" | %7"U32_F" | (ver, class, flow)\n",
956 LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
957 LWIP_DEBUGF(IP6_DEBUG, ("| %5"U16_F" | %3"U16_F" | %3"U16_F" | (plen, nexth, hopl)\n",
958 ntohs(IP6H_PLEN(ip6hdr)),
959 ntohs(IP6H_NEXTH(ip6hdr)),
960 ntohs(IP6H_HOPLIM(ip6hdr))));
961 LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
962 LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (src)\n",
963 IP6_ADDR_BLOCK1(&(ip6hdr->src)),
964 IP6_ADDR_BLOCK2(&(ip6hdr->src)),
965 IP6_ADDR_BLOCK3(&(ip6hdr->src)),
966 IP6_ADDR_BLOCK4(&(ip6hdr->src))));
967 LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
968 IP6_ADDR_BLOCK5(&(ip6hdr->src)),
969 IP6_ADDR_BLOCK6(&(ip6hdr->src)),
970 IP6_ADDR_BLOCK7(&(ip6hdr->src)),
971 IP6_ADDR_BLOCK8(&(ip6hdr->src))));
972 LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
973 LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (dest)\n",
974 IP6_ADDR_BLOCK1(&(ip6hdr->dest)),
975 IP6_ADDR_BLOCK2(&(ip6hdr->dest)),
976 IP6_ADDR_BLOCK3(&(ip6hdr->dest)),
977 IP6_ADDR_BLOCK4(&(ip6hdr->dest))));
978 LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
979 IP6_ADDR_BLOCK5(&(ip6hdr->dest)),
980 IP6_ADDR_BLOCK6(&(ip6hdr->dest)),
981 IP6_ADDR_BLOCK7(&(ip6hdr->dest)),
982 IP6_ADDR_BLOCK8(&(ip6hdr->dest))));
983 LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
985 #endif /* IP6_DEBUG */
987 #endif /* LWIP_IPV6 */