4 * Neighbor discovery and stateless address autoconfiguration for IPv6.
5 * Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862
6 * (Address autoconfiguration).
10 * Copyright (c) 2010 Inico Technologies Ltd.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Ivan Delamer <delamer@inicotech.com>
40 * Please coordinate changes and requests with Ivan Delamer
41 * <delamer@inicotech.com>
46 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
49 #include "lwip/pbuf.h"
51 #include "lwip/memp.h"
53 #include "lwip/ip6_addr.h"
54 #include "lwip/inet_chksum.h"
55 #include "lwip/netif.h"
56 #include "lwip/icmp6.h"
57 #include "lwip/mld6.h"
58 #include "lwip/stats.h"
64 struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS];
65 struct nd6_destination_cache_entry destination_cache[LWIP_ND6_NUM_DESTINATIONS];
66 struct nd6_prefix_list_entry prefix_list[LWIP_ND6_NUM_PREFIXES];
67 struct nd6_router_list_entry default_router_list[LWIP_ND6_NUM_ROUTERS];
69 /* Default values, can be updated by a RA message. */
70 u32_t reachable_time = LWIP_ND6_REACHABLE_TIME;
71 u32_t retrans_timer = LWIP_ND6_RETRANS_TIMER; /* TODO implement this value in timer */
73 /* Index for cache entries. */
74 static u8_t nd6_cached_neighbor_index;
75 static u8_t nd6_cached_destination_index;
77 /* Multicast address holder. */
78 static ip6_addr_t multicast_address;
80 /* Static buffer to parse RA packet options (size of a prefix option, biggest option) */
81 static u8_t nd6_ra_buffer[sizeof(struct prefix_option)];
83 /* Forward declarations. */
84 static s8_t nd6_find_neighbor_cache_entry(ip6_addr_t * ip6addr);
85 static s8_t nd6_new_neighbor_cache_entry(void);
86 static void nd6_free_neighbor_cache_entry(s8_t i);
87 static s8_t nd6_find_destination_cache_entry(ip6_addr_t * ip6addr);
88 static s8_t nd6_new_destination_cache_entry(void);
89 static s8_t nd6_is_prefix_in_netif(ip6_addr_t * ip6addr, struct netif * netif);
90 static s8_t nd6_get_router(ip6_addr_t * router_addr, struct netif * netif);
91 static s8_t nd6_new_router(ip6_addr_t * router_addr, struct netif * netif);
92 static s8_t nd6_get_onlink_prefix(ip6_addr_t * prefix, struct netif * netif);
93 static s8_t nd6_new_onlink_prefix(ip6_addr_t * prefix, struct netif * netif);
95 #define ND6_SEND_FLAG_MULTICAST_DEST 0x01
96 #define ND6_SEND_FLAG_ALLNODES_DEST 0x02
97 static void nd6_send_ns(struct netif * netif, ip6_addr_t * target_addr, u8_t flags);
98 static void nd6_send_na(struct netif * netif, ip6_addr_t * target_addr, u8_t flags);
99 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
100 static void nd6_send_rs(struct netif * netif);
101 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
103 #if LWIP_ND6_QUEUEING
104 static void nd6_free_q(struct nd6_q_entry *q);
105 #else /* LWIP_ND6_QUEUEING */
106 #define nd6_free_q(q) pbuf_free(q)
107 #endif /* LWIP_ND6_QUEUEING */
108 static void nd6_send_q(s8_t i);
112 * Process an incoming neighbor discovery message
114 * @param p the nd packet, p->payload pointing to the icmpv6 header
115 * @param inp the netif on which this packet was received
118 nd6_input(struct pbuf *p, struct netif *inp)
123 ND6_STATS_INC(nd6.recv);
125 msg_type = *((u8_t *)p->payload);
127 case ICMP6_TYPE_NA: /* Neighbor Advertisement. */
129 struct na_header * na_hdr;
130 struct lladdr_option * lladdr_opt;
132 /* Check that na header fits in packet. */
133 if (p->len < (sizeof(struct na_header))) {
134 /* TODO debug message */
136 ND6_STATS_INC(nd6.lenerr);
137 ND6_STATS_INC(nd6.drop);
141 na_hdr = (struct na_header *)p->payload;
144 if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
145 /* This is an unsolicited NA.
146 * link-layer changed?
147 * part of DAD mechanism? */
149 /* Check that link-layer address option also fits in packet. */
150 if (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option))) {
151 /* TODO debug message */
153 ND6_STATS_INC(nd6.lenerr);
154 ND6_STATS_INC(nd6.drop);
158 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
160 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
161 ip6_addr_set(ip6_current_dest_addr(), &(na_hdr->target_address));
163 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
164 /* If the target address matches this netif, it is a DAD response. */
165 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
166 if (ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) {
167 /* We are using a duplicate address. */
168 netif_ip6_addr_set_state(inp, i, IP6_ADDR_INVALID);
171 /* Leave solicited node multicast group. */
172 ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(inp, i)->addr[3]);
173 mld6_leavegroup(netif_ip6_addr(inp, i), &multicast_address);
174 #endif /* LWIP_IPV6_MLD */
179 #if LWIP_IPV6_AUTOCONFIG
180 /* Check to see if this address was autoconfigured. */
181 if (!ip6_addr_islinklocal(ip6_current_dest_addr())) {
182 i = nd6_get_onlink_prefix(ip6_current_dest_addr(), inp);
184 /* Mark this prefix as duplicate, so that we don't use it
185 * to generate this address again. */
186 prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_DUPLICATE;
189 #endif /* LWIP_IPV6_AUTOCONFIG */
195 #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
197 /* This is an unsolicited NA, most likely there was a LLADDR change. */
198 i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
200 if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
201 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
206 /* This is a solicited NA.
207 * neighbor address resolution response?
208 * neighbor unreachability detection response? */
210 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
211 ip6_addr_set(ip6_current_dest_addr(), &(na_hdr->target_address));
213 /* Find the cache entry corresponding to this na. */
214 i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
216 /* We no longer care about this target address. drop it. */
221 /* Update cache entry. */
222 neighbor_cache[i].netif = inp;
223 neighbor_cache[i].counter.reachable_time = reachable_time;
224 if ((na_hdr->flags & ND6_FLAG_OVERRIDE) ||
225 (neighbor_cache[i].state == ND6_INCOMPLETE)) {
226 /* Check that link-layer address option also fits in packet. */
227 if (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option))) {
228 /* TODO debug message */
230 ND6_STATS_INC(nd6.lenerr);
231 ND6_STATS_INC(nd6.drop);
235 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
237 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
239 neighbor_cache[i].state = ND6_REACHABLE;
241 /* Send queued packets, if any. */
242 if (neighbor_cache[i].q != NULL) {
247 break; /* ICMP6_TYPE_NA */
249 case ICMP6_TYPE_NS: /* Neighbor solicitation. */
251 struct ns_header * ns_hdr;
252 struct lladdr_option * lladdr_opt;
255 /* Check that ns header fits in packet. */
256 if (p->len < sizeof(struct ns_header)) {
257 /* TODO debug message */
259 ND6_STATS_INC(nd6.lenerr);
260 ND6_STATS_INC(nd6.drop);
264 ns_hdr = (struct ns_header *)p->payload;
266 /* Check if there is a link-layer address provided. Only point to it if in this buffer. */
268 if (p->len >= (sizeof(struct ns_header) + sizeof(struct lladdr_option))) {
269 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
272 /* Check if the target address is configured on the receiving netif. */
274 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
275 if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) ||
276 (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) &&
277 ip6_addr_isany(ip6_current_src_addr()))) &&
278 ip6_addr_cmp(&(ns_hdr->target_address), netif_ip6_addr(inp, i))) {
290 /* Check for ANY address in src (DAD algorithm). */
291 if (ip6_addr_isany(ip6_current_src_addr())) {
292 /* Sender is validating this address. */
293 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
294 if (ip6_addr_cmp(&(ns_hdr->target_address), netif_ip6_addr(inp, i))) {
295 /* Send a NA back so that the sender does not use this address. */
296 nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST);
297 if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) {
298 /* We shouldn't use this address either. */
299 netif_ip6_addr_set_state(inp, i, IP6_ADDR_INVALID);
305 /* Sender is trying to resolve our address. */
306 /* Verify that they included their own link-layer address. */
307 if (lladdr_opt == NULL) {
308 /* Not a valid message. */
310 ND6_STATS_INC(nd6.proterr);
311 ND6_STATS_INC(nd6.drop);
315 i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
317 /* We already have a record for the solicitor. */
318 if (neighbor_cache[i].state == ND6_INCOMPLETE) {
319 neighbor_cache[i].netif = inp;
320 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
322 /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */
323 neighbor_cache[i].state = ND6_DELAY;
324 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
329 /* Add their IPv6 address and link-layer address to neighbor cache.
330 * We will need it at least to send a unicast NA message, but most
331 * likely we will also be communicating with this node soon. */
332 i = nd6_new_neighbor_cache_entry();
334 /* We couldn't assign a cache entry for this neighbor.
335 * we won't be able to reply. drop it. */
337 ND6_STATS_INC(nd6.memerr);
340 neighbor_cache[i].netif = inp;
341 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
342 ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr());
344 /* Receiving a message does not prove reachability: only in one direction.
345 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
346 neighbor_cache[i].state = ND6_DELAY;
347 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
350 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
351 ip6_addr_set(ip6_current_dest_addr(), &(ns_hdr->target_address));
353 /* Send back a NA for us. Allocate the reply pbuf. */
354 nd6_send_na(inp, ip6_current_dest_addr(), ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE);
357 break; /* ICMP6_TYPE_NS */
359 case ICMP6_TYPE_RA: /* Router Advertisement. */
361 struct ra_header * ra_hdr;
362 u8_t * buffer; /* Used to copy options. */
365 /* Check that RA header fits in packet. */
366 if (p->len < sizeof(struct ra_header)) {
367 /* TODO debug message */
369 ND6_STATS_INC(nd6.lenerr);
370 ND6_STATS_INC(nd6.drop);
374 ra_hdr = (struct ra_header *)p->payload;
376 /* If we are sending RS messages, stop. */
377 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
379 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
381 /* Get the matching default router entry. */
382 i = nd6_get_router(ip6_current_src_addr(), inp);
384 /* Create a new router entry. */
385 i = nd6_new_router(ip6_current_src_addr(), inp);
389 /* Could not create a new router entry. */
391 ND6_STATS_INC(nd6.memerr);
395 /* Re-set invalidation timer. */
396 default_router_list[i].invalidation_timer = ra_hdr->router_lifetime;
398 /* Re-set default timer values. */
399 #if LWIP_ND6_ALLOW_RA_UPDATES
400 if (ra_hdr->retrans_timer > 0) {
401 retrans_timer = ra_hdr->retrans_timer;
403 if (ra_hdr->reachable_time > 0) {
404 reachable_time = ra_hdr->reachable_time;
406 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
408 /* TODO set default hop limit... */
409 /* ra_hdr->current_hop_limit;*/
411 /* Update flags in local entry (incl. preference). */
412 default_router_list[i].flags = ra_hdr->flags;
414 /* Offset to options. */
415 offset = sizeof(struct ra_header);
417 /* Process each option. */
418 while ((p->tot_len - offset) > 0) {
419 if (p->len == p->tot_len) {
420 /* no need to copy from contiguous pbuf */
421 buffer = &((u8_t*)p->payload)[offset];
423 buffer = nd6_ra_buffer;
424 pbuf_copy_partial(p, buffer, sizeof(struct prefix_option), offset);
427 case ND6_OPTION_TYPE_SOURCE_LLADDR:
429 struct lladdr_option * lladdr_opt;
430 lladdr_opt = (struct lladdr_option *)buffer;
431 if ((default_router_list[i].neighbor_entry != NULL) &&
432 (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) {
433 SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len);
434 default_router_list[i].neighbor_entry->state = ND6_REACHABLE;
435 default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time;
439 case ND6_OPTION_TYPE_MTU:
441 struct mtu_option * mtu_opt;
442 mtu_opt = (struct mtu_option *)buffer;
443 if (mtu_opt->mtu >= 1280) {
444 #if LWIP_ND6_ALLOW_RA_UPDATES
445 inp->mtu = mtu_opt->mtu;
446 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
450 case ND6_OPTION_TYPE_PREFIX_INFO:
452 struct prefix_option * prefix_opt;
453 prefix_opt = (struct prefix_option *)buffer;
455 if (prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) {
456 /* Add to on-link prefix list. */
458 /* Get a memory-aligned copy of the prefix. */
459 ip6_addr_set(ip6_current_dest_addr(), &(prefix_opt->prefix));
461 /* find cache entry for this prefix. */
462 i = nd6_get_onlink_prefix(ip6_current_dest_addr(), inp);
464 /* Create a new cache entry. */
465 i = nd6_new_onlink_prefix(ip6_current_dest_addr(), inp);
468 prefix_list[i].invalidation_timer = prefix_opt->valid_lifetime;
470 #if LWIP_IPV6_AUTOCONFIG
471 if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) {
472 /* Mark prefix as autonomous, so that address autoconfiguration can take place.
473 * Only OR flag, so that we don't over-write other flags (such as ADDRESS_DUPLICATE)*/
474 prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_AUTONOMOUS;
476 #endif /* LWIP_IPV6_AUTOCONFIG */
482 case ND6_OPTION_TYPE_ROUTE_INFO:
484 /* TODO implement preferred routes.
485 struct route_option * route_opt;
486 route_opt = (struct route_option *)buffer;*/
491 /* Unrecognized option, abort. */
492 ND6_STATS_INC(nd6.proterr);
495 offset += 8 * ((u16_t)buffer[1]);
498 break; /* ICMP6_TYPE_RA */
500 case ICMP6_TYPE_RD: /* Redirect */
502 struct redirect_header * redir_hdr;
503 struct lladdr_option * lladdr_opt;
505 /* Check that Redir header fits in packet. */
506 if (p->len < sizeof(struct redirect_header)) {
507 /* TODO debug message */
509 ND6_STATS_INC(nd6.lenerr);
510 ND6_STATS_INC(nd6.drop);
514 redir_hdr = (struct redirect_header *)p->payload;
517 if (p->len >= (sizeof(struct redirect_header) + sizeof(struct lladdr_option))) {
518 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header));
521 /* Copy original destination address to current source address, to have an aligned copy. */
522 ip6_addr_set(ip6_current_src_addr(), &(redir_hdr->destination_address));
524 /* Find dest address in cache */
525 i = nd6_find_destination_cache_entry(ip6_current_src_addr());
527 /* Destination not in cache, drop packet. */
532 /* Set the new target address. */
533 ip6_addr_set(&(destination_cache[i].next_hop_addr), &(redir_hdr->target_address));
535 /* If Link-layer address of other router is given, try to add to neighbor cache. */
536 if (lladdr_opt != NULL) {
537 if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) {
538 /* Copy target address to current source address, to have an aligned copy. */
539 ip6_addr_set(ip6_current_src_addr(), &(redir_hdr->target_address));
541 i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
543 i = nd6_new_neighbor_cache_entry();
545 neighbor_cache[i].netif = inp;
546 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
547 ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr());
549 /* Receiving a message does not prove reachability: only in one direction.
550 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
551 neighbor_cache[i].state = ND6_DELAY;
552 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
556 if (neighbor_cache[i].state == ND6_INCOMPLETE) {
557 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
558 /* Receiving a message does not prove reachability: only in one direction.
559 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
560 neighbor_cache[i].state = ND6_DELAY;
561 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
566 break; /* ICMP6_TYPE_RD */
568 case ICMP6_TYPE_PTB: /* Packet too big */
570 struct icmp6_hdr *icmp6hdr; /* Packet too big message */
571 struct ip6_hdr * ip6hdr; /* IPv6 header of the packet which caused the error */
573 /* Check that ICMPv6 header + IPv6 header fit in payload */
574 if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) {
575 /* drop short packets */
577 ND6_STATS_INC(nd6.lenerr);
578 ND6_STATS_INC(nd6.drop);
582 icmp6hdr = (struct icmp6_hdr *)p->payload;
583 ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr));
585 /* Copy original destination address to current source address, to have an aligned copy. */
586 ip6_addr_set(ip6_current_src_addr(), &(ip6hdr->dest));
588 /* Look for entry in destination cache. */
589 i = nd6_find_destination_cache_entry(ip6_current_src_addr());
591 /* Destination not in cache, drop packet. */
596 /* Change the Path MTU. */
597 destination_cache[i].pmtu = icmp6hdr->data;
599 break; /* ICMP6_TYPE_PTB */
603 ND6_STATS_INC(nd6.proterr);
604 ND6_STATS_INC(nd6.drop);
613 * Periodic timer for Neighbor discovery functions:
615 * - Update neighbor reachability states
616 * - Update destination cache entries age
617 * - Update invalidation timers of default routers and on-link prefixes
618 * - Perform duplicate address detection (DAD) for our addresses
619 * - Send router solicitations
625 struct netif * netif;
627 /* Process neighbor entries. */
628 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
629 switch (neighbor_cache[i].state) {
631 if (neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) {
632 /* Retries exceeded. */
633 nd6_free_neighbor_cache_entry(i);
636 /* Send a NS for this entry. */
637 neighbor_cache[i].counter.probes_sent++;
638 nd6_send_ns(neighbor_cache[i].netif, &(neighbor_cache[i].next_hop_address), ND6_SEND_FLAG_MULTICAST_DEST);
642 /* Send queued packets, if any are left. Should have been sent already. */
643 if (neighbor_cache[i].q != NULL) {
646 if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) {
647 /* Change to stale state. */
648 neighbor_cache[i].state = ND6_STALE;
649 neighbor_cache[i].counter.stale_time = 0;
652 neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
656 neighbor_cache[i].counter.stale_time += ND6_TMR_INTERVAL;
659 if (neighbor_cache[i].counter.delay_time <= ND6_TMR_INTERVAL) {
660 /* Change to PROBE state. */
661 neighbor_cache[i].state = ND6_PROBE;
662 neighbor_cache[i].counter.probes_sent = 0;
665 neighbor_cache[i].counter.delay_time -= ND6_TMR_INTERVAL;
669 if (neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) {
670 /* Retries exceeded. */
671 nd6_free_neighbor_cache_entry(i);
674 /* Send a NS for this entry. */
675 neighbor_cache[i].counter.probes_sent++;
676 nd6_send_ns(neighbor_cache[i].netif, &(neighbor_cache[i].next_hop_address), 0);
686 /* Process destination entries. */
687 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
688 destination_cache[i].age++;
691 /* Process router entries. */
692 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
693 if (default_router_list[i].neighbor_entry != NULL) {
695 if (default_router_list[i].invalidation_timer > 0) {
696 default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
698 if (default_router_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
699 /* Less than 1 second remainig. Clear this entry. */
700 default_router_list[i].neighbor_entry->isrouter = 0;
701 default_router_list[i].neighbor_entry = NULL;
702 default_router_list[i].invalidation_timer = 0;
703 default_router_list[i].flags = 0;
708 /* Process prefix entries. */
709 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
710 if (prefix_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
711 prefix_list[i].invalidation_timer = 0;
713 if ((prefix_list[i].invalidation_timer > 0) &&
714 (prefix_list[i].netif != NULL)) {
715 prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
717 #if LWIP_IPV6_AUTOCONFIG
718 /* Initiate address autoconfiguration for this prefix, if conditions are met. */
719 if (prefix_list[i].netif->ip6_autoconfig_enabled &&
720 (prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_AUTONOMOUS) &&
721 !(prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED)) {
722 /* Try to get an address on this netif that is invalid.
723 * Skip 0 index (link-local address) */
724 for (j = 1; j < LWIP_IPV6_NUM_ADDRESSES; j++) {
725 if (netif_ip6_addr_state(prefix_list[i].netif, j) == IP6_ADDRESS_STATE_INVALID) {
726 /* Generate an address using this prefix and interface ID from link-local address. */
727 prefix_list[i].netif->ip6_addr[j].addr[0] = prefix_list[i].prefix.addr[0];
728 prefix_list[i].netif->ip6_addr[j].addr[1] = prefix_list[i].prefix.addr[1];
729 prefix_list[i].netif->ip6_addr[j].addr[2] = prefix_list[i].netif->ip6_addr[0].addr[2];
730 prefix_list[i].netif->ip6_addr[j].addr[3] = prefix_list[i].netif->ip6_addr[0].addr[3];
732 /* Mark it as tentative (DAD will be performed if configured). */
733 netif_ip6_addr_set_state(prefix_list[i].netif, j, IP6_ADDR_TENTATIVE);
735 /* Mark this prefix with ADDRESS_GENERATED, so that we don't try again. */
736 prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED;
743 #endif /* LWIP_IPV6_AUTOCONFIG */
748 /* Process our own addresses, if DAD configured. */
749 for (netif = netif_list; netif != NULL; netif = netif->next) {
750 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
751 if (ip6_addr_istentative(netif->ip6_addr_state[i])) {
752 if ((netif->ip6_addr_state[i] & 0x07) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) {
753 /* No NA received in response. Mark address as valid. */
754 netif->ip6_addr_state[i] = IP6_ADDR_PREFERRED;
755 /* TODO implement preferred and valid lifetimes. */
757 else if (netif->flags & NETIF_FLAG_UP) {
759 if ((netif->ip6_addr_state[i] & 0x07) == 0) {
760 /* Join solicited node multicast group. */
761 ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, i)->addr[3]);
762 mld6_joingroup(netif_ip6_addr(netif, i), &multicast_address);
764 #endif /* LWIP_IPV6_MLD */
765 /* Send a NS for this address. */
766 nd6_send_ns(netif, netif_ip6_addr(netif, i), ND6_SEND_FLAG_MULTICAST_DEST);
767 (netif->ip6_addr_state[i])++;
768 /* TODO send max 1 NS per tmr call? enable return*/
775 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
776 /* Send router solicitation messages, if necessary. */
777 for (netif = netif_list; netif != NULL; netif = netif->next) {
778 if ((netif->rs_count > 0) && (netif->flags & NETIF_FLAG_UP)) {
783 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
788 * Send a neighbor solicitation message
790 * @param netif the netif on which to send the message
791 * @param target_addr the IPv6 target address for the ND message
792 * @param flags one of ND6_SEND_FLAG_*
795 nd6_send_ns(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
797 struct ns_header * ns_hdr;
798 struct lladdr_option * lladdr_opt;
800 ip6_addr_t * src_addr;
802 if (ip6_addr_isvalid(netif_ip6_addr_state(netif,0))) {
803 /* Use link-local address as source address. */
804 src_addr = netif_ip6_addr(netif, 0);
806 src_addr = IP6_ADDR_ANY;
809 /* Allocate a packet. */
810 p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + sizeof(struct lladdr_option), PBUF_RAM);
811 if ((p == NULL) || (p->len < (sizeof(struct ns_header) + sizeof(struct lladdr_option)))) {
812 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
816 ND6_STATS_INC(nd6.memerr);
821 ns_hdr = (struct ns_header *)p->payload;
822 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
824 ns_hdr->type = ICMP6_TYPE_NS;
827 ns_hdr->reserved = 0;
828 ip6_addr_set(&(ns_hdr->target_address), target_addr);
830 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
831 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
832 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
834 /* Generate the solicited node address for the target address. */
835 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
836 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
837 target_addr = &multicast_address;
840 ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
843 /* Send the packet out. */
844 ND6_STATS_INC(nd6.xmit);
845 ip6_output_if(p, (src_addr == IP6_ADDR_ANY) ? NULL : src_addr, target_addr,
846 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
851 * Send a neighbor advertisement message
853 * @param netif the netif on which to send the message
854 * @param target_addr the IPv6 target address for the ND message
855 * @param flags one of ND6_SEND_FLAG_*
858 nd6_send_na(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
860 struct na_header * na_hdr;
861 struct lladdr_option * lladdr_opt;
863 ip6_addr_t * src_addr;
864 ip6_addr_t * dest_addr;
866 /* Use link-local address as source address. */
867 /* src_addr = &(netif->ip6_addr[0]); */
868 /* Use target address as source address. */
869 src_addr = target_addr;
871 /* Allocate a packet. */
872 p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + sizeof(struct lladdr_option), PBUF_RAM);
873 if ((p == NULL) || (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option)))) {
874 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
878 ND6_STATS_INC(nd6.memerr);
883 na_hdr = (struct na_header *)p->payload;
884 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
886 na_hdr->type = ICMP6_TYPE_NA;
889 na_hdr->flags = flags & 0xf0;
890 na_hdr->reserved[0] = 0;
891 na_hdr->reserved[1] = 0;
892 na_hdr->reserved[2] = 0;
893 ip6_addr_set(&(na_hdr->target_address), target_addr);
895 lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR;
896 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
897 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
899 /* Generate the solicited node address for the target address. */
900 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
901 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
902 dest_addr = &multicast_address;
904 else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
905 ip6_addr_set_allnodes_linklocal(&multicast_address);
906 dest_addr = &multicast_address;
909 dest_addr = ip6_current_src_addr();
912 na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
915 /* Send the packet out. */
916 ND6_STATS_INC(nd6.xmit);
917 ip6_output_if(p, src_addr, dest_addr,
918 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
922 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
924 * Send a router solicitation message
926 * @param netif the netif on which to send the message
929 nd6_send_rs(struct netif * netif)
931 struct rs_header * rs_hdr;
932 struct lladdr_option * lladdr_opt;
934 ip6_addr_t * src_addr;
937 /* Link-local source address, or unspecified address? */
938 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
939 src_addr = netif_ip6_addr(netif, 0);
942 src_addr = IP6_ADDR_ANY;
945 /* Generate the all routers target address. */
946 ip6_addr_set_allrouters_linklocal(&multicast_address);
948 /* Allocate a packet. */
949 packet_len = sizeof(struct rs_header);
950 if (src_addr != IP6_ADDR_ANY) {
951 packet_len += sizeof(struct lladdr_option);
953 p = pbuf_alloc(PBUF_IP, packet_len, PBUF_RAM);
954 if ((p == NULL) || (p->len < packet_len)) {
955 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
959 ND6_STATS_INC(nd6.memerr);
964 rs_hdr = (struct rs_header *)p->payload;
966 rs_hdr->type = ICMP6_TYPE_RS;
969 rs_hdr->reserved = 0;
971 if (src_addr != IP6_ADDR_ANY) {
972 /* Include our hw address. */
973 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
974 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
975 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
976 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
979 rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
982 /* Send the packet out. */
983 ND6_STATS_INC(nd6.xmit);
984 ip6_output_if(p, src_addr, &multicast_address,
985 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
988 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
991 * Search for a neighbor cache entry
993 * @param ip6addr the IPv6 address of the neighbor
994 * @return The neighbor cache entry index that matched, -1 if no
998 nd6_find_neighbor_cache_entry(ip6_addr_t * ip6addr)
1001 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1002 if (ip6_addr_cmp(ip6addr, &(neighbor_cache[i].next_hop_address))) {
1010 * Create a new neighbor cache entry.
1012 * If no unused entry is found, will try to recycle an old entry
1013 * according to ad-hoc "age" heuristic.
1015 * @return The neighbor cache entry index that was created, -1 if no
1016 * entry could be created
1019 nd6_new_neighbor_cache_entry(void)
1026 /* First, try to find an empty entry. */
1027 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1028 if (neighbor_cache[i].state == ND6_NO_ENTRY) {
1033 /* We need to recycle an entry. in general, do not recycle if it is a router. */
1035 /* Next, try to find a Stale entry. */
1036 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1037 if ((neighbor_cache[i].state == ND6_STALE) &&
1038 (!neighbor_cache[i].isrouter)) {
1039 nd6_free_neighbor_cache_entry(i);
1044 /* Next, try to find a Probe entry. */
1045 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1046 if ((neighbor_cache[i].state == ND6_PROBE) &&
1047 (!neighbor_cache[i].isrouter)) {
1048 nd6_free_neighbor_cache_entry(i);
1053 /* Next, try to find a Delayed entry. */
1054 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1055 if ((neighbor_cache[i].state == ND6_DELAY) &&
1056 (!neighbor_cache[i].isrouter)) {
1057 nd6_free_neighbor_cache_entry(i);
1062 /* Next, try to find the oldest reachable entry. */
1063 time = 0xfffffffful;
1065 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1066 if ((neighbor_cache[i].state == ND6_REACHABLE) &&
1067 (!neighbor_cache[i].isrouter)) {
1068 if (neighbor_cache[i].counter.reachable_time < time) {
1070 time = neighbor_cache[i].counter.reachable_time;
1075 nd6_free_neighbor_cache_entry(j);
1079 /* Next, find oldest incomplete entry without queued packets. */
1082 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1084 (neighbor_cache[i].q == NULL) &&
1085 (neighbor_cache[i].state == ND6_INCOMPLETE) &&
1086 (!neighbor_cache[i].isrouter)) {
1087 if (neighbor_cache[i].counter.probes_sent >= time) {
1089 time = neighbor_cache[i].counter.probes_sent;
1094 nd6_free_neighbor_cache_entry(j);
1098 /* Next, find oldest incomplete entry with queued packets. */
1101 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1102 if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
1103 (!neighbor_cache[i].isrouter)) {
1104 if (neighbor_cache[i].counter.probes_sent >= time) {
1106 time = neighbor_cache[i].counter.probes_sent;
1111 nd6_free_neighbor_cache_entry(j);
1115 /* No more entries to try. */
1120 * Will free any resources associated with a neighbor cache
1121 * entry, and will mark it as unused.
1123 * @param i the neighbor cache entry index to free
1126 nd6_free_neighbor_cache_entry(s8_t i)
1128 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1132 /* Free any queued packets. */
1133 if (neighbor_cache[i].q != NULL) {
1134 nd6_free_q(neighbor_cache[i].q);
1135 neighbor_cache[i].q = NULL;
1138 neighbor_cache[i].state = ND6_NO_ENTRY;
1139 neighbor_cache[i].isrouter = 0;
1140 neighbor_cache[i].netif = NULL;
1141 neighbor_cache[i].counter.reachable_time = 0;
1142 ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address));
1146 * Search for a destination cache entry
1148 * @param ip6addr the IPv6 address of the destination
1149 * @return The destination cache entry index that matched, -1 if no
1153 nd6_find_destination_cache_entry(ip6_addr_t * ip6addr)
1156 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1157 if (ip6_addr_cmp(ip6addr, &(destination_cache[i].destination_addr))) {
1165 * Create a new destination cache entry. If no unused entry is found,
1166 * will recycle oldest entry.
1168 * @return The destination cache entry index that was created, -1 if no
1172 nd6_new_destination_cache_entry(void)
1177 /* Find an empty entry. */
1178 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1179 if (ip6_addr_isany(&(destination_cache[i].destination_addr))) {
1184 /* Find oldest entry. */
1186 j = LWIP_ND6_NUM_DESTINATIONS - 1;
1187 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1188 if (destination_cache[i].age > age) {
1197 * Determine whether an address matches an on-link prefix.
1199 * @param ip6addr the IPv6 address to match
1200 * @return 1 if the address is on-link, 0 otherwise
1203 nd6_is_prefix_in_netif(ip6_addr_t * ip6addr, struct netif * netif)
1206 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
1207 if ((prefix_list[i].netif == netif) &&
1208 (prefix_list[i].invalidation_timer > 0) &&
1209 ip6_addr_netcmp(ip6addr, &(prefix_list[i].prefix))) {
1213 /* Check to see if address prefix matches a (manually?) configured address. */
1214 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1215 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
1216 ip6_addr_netcmp(ip6addr, netif_ip6_addr(netif, i))) {
1224 * Select a default router for a destination.
1226 * @param ip6addr the destination address
1227 * @param netif the netif for the outgoing packet, if known
1228 * @return the default router entry index, or -1 if no suitable
1232 nd6_select_router(ip6_addr_t * ip6addr, struct netif * netif)
1235 /* last_router is used for round-robin router selection (as recommended
1236 * in RFC). This is more robust in case one router is not reachable,
1237 * we are not stuck trying to resolve it. */
1238 static s8_t last_router;
1239 (void)ip6addr; /* TODO match preferred routes!! (must implement ND6_OPTION_TYPE_ROUTE_INFO) */
1241 /* TODO: implement default router preference */
1243 /* Look for reachable routers. */
1244 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1245 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1248 if ((default_router_list[i].neighbor_entry != NULL) &&
1249 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1250 (default_router_list[i].invalidation_timer > 0) &&
1251 (default_router_list[i].neighbor_entry->state == ND6_REACHABLE)) {
1256 /* Look for router in other reachability states, but still valid according to timer. */
1257 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1258 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1261 if ((default_router_list[i].neighbor_entry != NULL) &&
1262 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1263 (default_router_list[i].invalidation_timer > 0)) {
1268 /* Look for any router for which we have any information at all. */
1269 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1270 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1273 if (default_router_list[i].neighbor_entry != NULL &&
1274 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1)) {
1279 /* no suitable router found. */
1284 * Find an entry for a default router.
1286 * @param router_addr the IPv6 address of the router
1287 * @param netif the netif on which the router is found, if known
1288 * @return the index of the router entry, or -1 if not found
1291 nd6_get_router(ip6_addr_t * router_addr, struct netif * netif)
1295 /* Look for router. */
1296 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1297 if ((default_router_list[i].neighbor_entry != NULL) &&
1298 ((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1299 ip6_addr_cmp(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) {
1304 /* router not found. */
1309 * Create a new entry for a default router.
1311 * @param router_addr the IPv6 address of the router
1312 * @param netif the netif on which the router is connected, if known
1313 * @return the index on the router table, or -1 if could not be created
1316 nd6_new_router(ip6_addr_t * router_addr, struct netif * netif)
1319 s8_t neighbor_index;
1321 /* Do we have a neighbor entry for this router? */
1322 neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
1323 if (neighbor_index < 0) {
1324 /* Create a neighbor entry for this router. */
1325 neighbor_index = nd6_new_neighbor_cache_entry();
1326 if (neighbor_index < 0) {
1327 /* Could not create neighbor entry for this router. */
1330 ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
1331 neighbor_cache[neighbor_index].netif = netif;
1332 neighbor_cache[neighbor_index].q = NULL;
1333 neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
1334 neighbor_cache[neighbor_index].counter.probes_sent = 0;
1337 /* Mark neighbor as router. */
1338 neighbor_cache[neighbor_index].isrouter = 1;
1340 /* Look for empty entry. */
1341 for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) {
1342 if (default_router_list[router_index].neighbor_entry == NULL) {
1343 default_router_list[router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
1344 return router_index;
1348 /* Could not create a router entry. */
1350 /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
1351 neighbor_cache[neighbor_index].isrouter = 0;
1353 /* router not found. */
1358 * Find the cached entry for an on-link prefix.
1360 * @param prefix the IPv6 prefix that is on-link
1361 * @param netif the netif on which the prefix is on-link
1362 * @return the index on the prefix table, or -1 if not found
1365 nd6_get_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1369 /* Look for prefix in list. */
1370 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1371 if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) &&
1372 (prefix_list[i].netif == netif)) {
1377 /* Entry not available. */
1382 * Creates a new entry for an on-link prefix.
1384 * @param prefix the IPv6 prefix that is on-link
1385 * @param netif the netif on which the prefix is on-link
1386 * @return the index on the prefix table, or -1 if not created
1389 nd6_new_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1393 /* Create new entry. */
1394 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1395 if ((prefix_list[i].netif == NULL) ||
1396 (prefix_list[i].invalidation_timer == 0)) {
1397 /* Found empty prefix entry. */
1398 prefix_list[i].netif = netif;
1399 ip6_addr_set(&(prefix_list[i].prefix), prefix);
1400 prefix_list[i].flags = 0;
1405 /* Entry not available. */
1410 * Determine the next hop for a destination. Will determine if the
1411 * destination is on-link, else a suitable on-link router is selected.
1413 * The last entry index is cached for fast entry search.
1415 * @param ip6addr the destination address
1416 * @param netif the netif on which the packet will be sent
1417 * @return the neighbor cache entry for the next hop, ERR_RTE if no
1418 * suitable next hop was found, ERR_MEM if no cache entry
1422 nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
1426 #if LWIP_NETIF_HWADDRHINT
1427 if (netif->addr_hint != NULL) {
1428 /* per-pcb cached entry was given */
1429 u8_t addr_hint = *(netif->addr_hint);
1430 if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
1431 nd6_cached_destination_index = addr_hint;
1434 #endif /* LWIP_NETIF_HWADDRHINT */
1436 /* Look for ip6addr in destination cache. */
1437 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
1438 /* the cached entry index is the right one! */
1440 ND6_STATS_INC(nd6.cachehit);
1442 /* Search destination cache. */
1443 i = nd6_find_destination_cache_entry(ip6addr);
1445 /* found destination entry. make it our new cached index. */
1446 nd6_cached_destination_index = i;
1449 /* Not found. Create a new destination entry. */
1450 i = nd6_new_destination_cache_entry();
1452 /* got new destination entry. make it our new cached index. */
1453 nd6_cached_destination_index = i;
1455 /* Could not create a destination cache entry. */
1459 /* Copy dest address to destination cache. */
1460 ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr);
1462 /* Now find the next hop. is it a neighbor? */
1463 if (ip6_addr_islinklocal(ip6addr) ||
1464 nd6_is_prefix_in_netif(ip6addr, netif)) {
1465 /* Destination in local link. */
1466 destination_cache[nd6_cached_destination_index].pmtu = netif->mtu;
1467 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, destination_cache[nd6_cached_destination_index].destination_addr);
1470 /* We need to select a router. */
1471 i = nd6_select_router(ip6addr, netif);
1473 /* No router found. */
1474 ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr));
1477 destination_cache[nd6_cached_destination_index].pmtu = netif->mtu; /* Start with netif mtu, correct through ICMPv6 if necessary */
1478 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address);
1483 #if LWIP_NETIF_HWADDRHINT
1484 if (netif->addr_hint != NULL) {
1485 /* per-pcb cached entry was given */
1486 *(netif->addr_hint) = nd6_cached_destination_index;
1488 #endif /* LWIP_NETIF_HWADDRHINT */
1490 /* Look in neighbor cache for the next-hop address. */
1491 if (ip6_addr_cmp(&(destination_cache[nd6_cached_destination_index].next_hop_addr),
1492 &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
1495 ND6_STATS_INC(nd6.cachehit);
1497 i = nd6_find_neighbor_cache_entry(&(destination_cache[nd6_cached_destination_index].next_hop_addr));
1499 /* Found a matching record, make it new cached entry. */
1500 nd6_cached_neighbor_index = i;
1503 /* Neighbor not in cache. Make a new entry. */
1504 i = nd6_new_neighbor_cache_entry();
1506 /* got new neighbor entry. make it our new cached index. */
1507 nd6_cached_neighbor_index = i;
1509 /* Could not create a neighbor cache entry. */
1513 /* Initialize fields. */
1514 ip6_addr_copy(neighbor_cache[i].next_hop_address,
1515 destination_cache[nd6_cached_destination_index].next_hop_addr);
1516 neighbor_cache[i].isrouter = 0;
1517 neighbor_cache[i].netif = netif;
1518 neighbor_cache[i].state = ND6_INCOMPLETE;
1519 neighbor_cache[i].counter.probes_sent = 0;
1523 /* Reset this destination's age. */
1524 destination_cache[nd6_cached_destination_index].age = 0;
1526 return nd6_cached_neighbor_index;
1530 * Queue a packet for a neighbor.
1532 * @param neighbor_index the index in the neighbor cache table
1533 * @param q packet to be queued
1534 * @return ERR_OK if succeeded, ERR_MEM if out of memory
1537 nd6_queue_packet(s8_t neighbor_index, struct pbuf * q)
1539 err_t result = ERR_MEM;
1541 int copy_needed = 0;
1542 #if LWIP_ND6_QUEUEING
1543 struct nd6_q_entry *new_entry, *r;
1544 #endif /* LWIP_ND6_QUEUEING */
1546 if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
1550 /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
1551 * to copy the whole queue into a new PBUF_RAM (see bug #11400)
1552 * PBUF_ROMs can be left as they are, since ROM must not get changed. */
1555 if(p->type != PBUF_ROM) {
1562 /* copy the whole packet into new pbufs */
1563 p = pbuf_alloc(PBUF_LINK, q->tot_len, PBUF_RAM);
1564 while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1565 /* Free oldest packet (as per RFC recommendation) */
1566 #if LWIP_ND6_QUEUEING
1567 r = neighbor_cache[neighbor_index].q;
1568 neighbor_cache[neighbor_index].q = r->next;
1571 #else /* LWIP_ND6_QUEUEING */
1572 pbuf_free(neighbor_cache[neighbor_index].q);
1573 neighbor_cache[neighbor_index].q = NULL;
1574 #endif /* LWIP_ND6_QUEUEING */
1575 p = pbuf_alloc(PBUF_LINK, q->tot_len, PBUF_RAM);
1578 if (pbuf_copy(p, q) != ERR_OK) {
1584 /* referencing the old pbuf is enough */
1588 /* packet was copied/ref'd? */
1590 /* queue packet ... */
1591 #if LWIP_ND6_QUEUEING
1592 /* allocate a new nd6 queue entry */
1593 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1594 if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1595 /* Free oldest packet (as per RFC recommendation) */
1596 r = neighbor_cache[neighbor_index].q;
1597 neighbor_cache[neighbor_index].q = r->next;
1600 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1602 if (new_entry != NULL) {
1603 new_entry->next = NULL;
1605 if(neighbor_cache[neighbor_index].q != NULL) {
1606 /* queue was already existent, append the new entry to the end */
1607 r = neighbor_cache[neighbor_index].q;
1608 while (r->next != NULL) {
1611 r->next = new_entry;
1613 /* queue did not exist, first item in queue */
1614 neighbor_cache[neighbor_index].q = new_entry;
1616 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
1619 /* the pool MEMP_ND6_QUEUE is empty */
1621 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p));
1622 /* { result == ERR_MEM } through initialization */
1624 #else /* LWIP_ND6_QUEUEING */
1625 /* Queue a single packet. If an older packet is already queued, free it as per RFC. */
1626 if (neighbor_cache[neighbor_index].q != NULL) {
1627 pbuf_free(neighbor_cache[neighbor_index].q);
1629 neighbor_cache[neighbor_index].q = p;
1630 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
1632 #endif /* LWIP_ND6_QUEUEING */
1634 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
1635 /* { result == ERR_MEM } through initialization */
1641 #if LWIP_ND6_QUEUEING
1643 * Free a complete queue of nd6 q entries
1645 * @param q a queue of nd6_q_entry to free
1648 nd6_free_q(struct nd6_q_entry *q)
1650 struct nd6_q_entry *r;
1651 LWIP_ASSERT("q != NULL", q != NULL);
1652 LWIP_ASSERT("q->p != NULL", q->p != NULL);
1656 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
1658 memp_free(MEMP_ND6_QUEUE, r);
1661 #endif /* LWIP_ND6_QUEUEING */
1664 * Send queued packets for a neighbor
1666 * @param i the neighbor to send packets to
1671 struct ip6_hdr *ip6hdr;
1672 #if LWIP_ND6_QUEUEING
1673 struct nd6_q_entry *q;
1674 #endif /* LWIP_ND6_QUEUEING */
1676 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1680 #if LWIP_ND6_QUEUEING
1681 while (neighbor_cache[i].q != NULL) {
1682 /* remember first in queue */
1683 q = neighbor_cache[i].q;
1684 /* pop first item off the queue */
1685 neighbor_cache[i].q = q->next;
1686 /* Get ipv6 header. */
1687 ip6hdr = (struct ip6_hdr *)(q->p->payload);
1688 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
1689 ip6_addr_set(ip6_current_dest_addr(), &(ip6hdr->dest));
1690 /* send the queued IPv6 packet */
1691 (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, ip6_current_dest_addr());
1692 /* free the queued IP packet */
1694 /* now queue entry can be freed */
1695 memp_free(MEMP_ND6_QUEUE, q);
1697 #else /* LWIP_ND6_QUEUEING */
1698 if (neighbor_cache[i].q != NULL) {
1699 /* Get ipv6 header. */
1700 ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload);
1701 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
1702 ip6_addr_set(ip6_current_dest_addr(), &(ip6hdr->dest));
1703 /* send the queued IPv6 packet */
1704 (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, neighbor_cache[i].q, ip6_current_dest_addr());
1705 /* free the queued IP packet */
1706 pbuf_free(neighbor_cache[i].q);
1707 neighbor_cache[i].q = NULL;
1709 #endif /* LWIP_ND6_QUEUEING */
1714 * Get the Path MTU for a destination.
1716 * @param ip6addr the destination address
1717 * @param netif the netif on which the packet will be sent
1718 * @return the Path MTU, if known, or the netif default MTU
1721 nd6_get_destination_mtu(ip6_addr_t * ip6addr, struct netif * netif)
1725 i = nd6_find_destination_cache_entry(ip6addr);
1727 if (destination_cache[i].pmtu > 0) {
1728 return destination_cache[i].pmtu;
1732 if (netif != NULL) {
1736 return 1280; /* Minimum MTU */
1740 #if LWIP_ND6_TCP_REACHABILITY_HINTS
1742 * Provide the Neighbor discovery process with a hint that a
1743 * destination is reachable. Called by tcp_receive when ACKs are
1744 * received or sent (as per RFC). This is useful to avoid sending
1745 * NS messages every 30 seconds.
1747 * @param ip6addr the destination address which is know to be reachable
1748 * by an upper layer protocol (TCP)
1751 nd6_reachability_hint(ip6_addr_t * ip6addr)
1755 /* Find destination in cache. */
1756 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
1757 i = nd6_cached_destination_index;
1758 ND6_STATS_INC(nd6.cachehit);
1761 i = nd6_find_destination_cache_entry(ip6addr);
1767 /* Find next hop neighbor in cache. */
1768 if (ip6_addr_cmp(&(destination_cache[i].next_hop_addr), &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
1769 i = nd6_cached_neighbor_index;
1770 ND6_STATS_INC(nd6.cachehit);
1773 i = nd6_find_neighbor_cache_entry(&(destination_cache[i].next_hop_addr));
1779 /* Set reachability state. */
1780 neighbor_cache[i].state = ND6_REACHABLE;
1781 neighbor_cache[i].counter.reachable_time = reachable_time;
1783 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
1785 #endif /* LWIP_IPV6 */