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 static void nd6_send_q(s8_t i);
106 #endif /* LWIP_ND6_QUEUEING */
110 * Process an incoming neighbor discovery message
112 * @param p the nd packet, p->payload pointing to the icmpv6 header
113 * @param inp the netif on which this packet was received
116 nd6_input(struct pbuf *p, struct netif *inp)
121 ND6_STATS_INC(nd6.recv);
123 msg_type = *((u8_t *)p->payload);
125 case ICMP6_TYPE_NA: /* Neighbor Advertisement. */
127 struct na_header * na_hdr;
128 struct lladdr_option * lladdr_opt;
130 /* Check that na header fits in packet. */
131 if (p->len < (sizeof(struct na_header))) {
132 /* TODO debug message */
134 ND6_STATS_INC(nd6.lenerr);
135 ND6_STATS_INC(nd6.drop);
139 na_hdr = (struct na_header *)p->payload;
142 if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
143 /* This is an unsolicited NA.
144 * link-layer changed?
145 * part of DAD mechanism? */
147 /* Check that link-layer address option also fits in packet. */
148 if (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option))) {
149 /* TODO debug message */
151 ND6_STATS_INC(nd6.lenerr);
152 ND6_STATS_INC(nd6.drop);
156 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
158 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
159 ip6_addr_set(ip6_current_dest_addr(), &(na_hdr->target_address));
161 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
162 /* If the target address matches this netif, it is a DAD response. */
163 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
164 if (ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) {
165 /* We are using a duplicate address. */
166 netif_ip6_addr_set_state(inp, i, IP6_ADDR_INVALID);
169 /* Leave solicited node multicast group. */
170 ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(inp, i)->addr[3]);
171 mld6_leavegroup(netif_ip6_addr(inp, i), &multicast_address);
172 #endif /* LWIP_IPV6_MLD */
177 #if LWIP_IPV6_AUTOCONFIG
178 /* Check to see if this address was autoconfigured. */
179 if (!ip6_addr_islinklocal(ip6_current_dest_addr())) {
180 i = nd6_get_onlink_prefix(ip6_current_dest_addr(), inp);
182 /* Mark this prefix as duplicate, so that we don't use it
183 * to generate this address again. */
184 prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_DUPLICATE;
187 #endif /* LWIP_IPV6_AUTOCONFIG */
193 #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
195 /* This is an unsolicited NA, most likely there was a LLADDR change. */
196 i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
198 if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
199 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
204 /* This is a solicited NA.
205 * neighbor address resolution response?
206 * neighbor unreachability detection response? */
208 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
209 ip6_addr_set(ip6_current_dest_addr(), &(na_hdr->target_address));
211 /* Find the cache entry corresponding to this na. */
212 i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
214 /* We no longer care about this target address. drop it. */
219 /* Update cache entry. */
220 neighbor_cache[i].netif = inp;
221 neighbor_cache[i].counter.reachable_time = reachable_time;
222 if ((na_hdr->flags & ND6_FLAG_OVERRIDE) ||
223 (neighbor_cache[i].state == ND6_INCOMPLETE)) {
224 /* Check that link-layer address option also fits in packet. */
225 if (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option))) {
226 /* TODO debug message */
228 ND6_STATS_INC(nd6.lenerr);
229 ND6_STATS_INC(nd6.drop);
233 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
235 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
237 neighbor_cache[i].state = ND6_REACHABLE;
239 #if LWIP_ND6_QUEUEING
240 /* Send queued packets, if any. */
241 if (neighbor_cache[i].q != NULL) {
244 #endif /* LWIP_ND6_QUEUEING */
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 #if LWIP_ND6_QUEUEING
643 /* Send queued packets, if any are left. Should have been sent already. */
644 if (neighbor_cache[i].q != NULL) {
647 #endif /* LWIP_ND6_QUEUEING */
648 if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) {
649 /* Change to stale state. */
650 neighbor_cache[i].state = ND6_STALE;
651 neighbor_cache[i].counter.stale_time = 0;
654 neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
658 neighbor_cache[i].counter.stale_time += ND6_TMR_INTERVAL;
661 if (neighbor_cache[i].counter.delay_time <= ND6_TMR_INTERVAL) {
662 /* Change to PROBE state. */
663 neighbor_cache[i].state = ND6_PROBE;
664 neighbor_cache[i].counter.probes_sent = 0;
667 neighbor_cache[i].counter.delay_time -= ND6_TMR_INTERVAL;
671 if (neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) {
672 /* Retries exceeded. */
673 nd6_free_neighbor_cache_entry(i);
676 /* Send a NS for this entry. */
677 neighbor_cache[i].counter.probes_sent++;
678 nd6_send_ns(neighbor_cache[i].netif, &(neighbor_cache[i].next_hop_address), 0);
688 /* Process destination entries. */
689 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
690 destination_cache[i].age++;
693 /* Process router entries. */
694 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
695 if (default_router_list[i].neighbor_entry != NULL) {
697 if (default_router_list[i].invalidation_timer > 0) {
698 default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
700 if (default_router_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
701 /* Less than 1 second remainig. Clear this entry. */
702 default_router_list[i].neighbor_entry->isrouter = 0;
703 default_router_list[i].neighbor_entry = NULL;
704 default_router_list[i].invalidation_timer = 0;
705 default_router_list[i].flags = 0;
710 /* Process prefix entries. */
711 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
712 if (prefix_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
713 prefix_list[i].invalidation_timer = 0;
715 if ((prefix_list[i].invalidation_timer > 0) &&
716 (prefix_list[i].netif != NULL)) {
717 prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
719 #if LWIP_IPV6_AUTOCONFIG
720 /* Initiate address autoconfiguration for this prefix, if conditions are met. */
721 if (prefix_list[i].netif->ip6_autoconfig_enabled &&
722 (prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_AUTONOMOUS) &&
723 !(prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED)) {
724 /* Try to get an address on this netif that is invalid.
725 * Skip 0 index (link-local address) */
726 for (j = 1; j < LWIP_IPV6_NUM_ADDRESSES; j++) {
727 if (netif_ip6_addr_state(prefix_list[i].netif, j) == IP6_ADDRESS_STATE_INVALID) {
728 /* Generate an address using this prefix and interface ID from link-local address. */
729 prefix_list[i].netif->ip6_addr[j].addr[0] = prefix_list[i].prefix.addr[0];
730 prefix_list[i].netif->ip6_addr[j].addr[1] = prefix_list[i].prefix.addr[1];
731 prefix_list[i].netif->ip6_addr[j].addr[2] = prefix_list[i].netif->ip6_addr[0].addr[2];
732 prefix_list[i].netif->ip6_addr[j].addr[3] = prefix_list[i].netif->ip6_addr[0].addr[3];
734 /* Mark it as tentative (DAD will be performed if configured). */
735 netif_ip6_addr_set_state(prefix_list[i].netif, j, IP6_ADDR_TENTATIVE);
737 /* Mark this prefix with ADDRESS_GENERATED, so that we don't try again. */
738 prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED;
745 #endif /* LWIP_IPV6_AUTOCONFIG */
750 /* Process our own addresses, if DAD configured. */
751 for (netif = netif_list; netif != NULL; netif = netif->next) {
752 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
753 if (ip6_addr_istentative(netif->ip6_addr_state[i])) {
754 if ((netif->ip6_addr_state[i] & 0x07) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) {
755 /* No NA received in response. Mark address as valid. */
756 netif->ip6_addr_state[i] = IP6_ADDR_PREFERRED;
757 /* TODO implement preferred and valid lifetimes. */
759 else if (netif->flags & NETIF_FLAG_UP) {
761 if ((netif->ip6_addr_state[i] & 0x07) == 0) {
762 /* Join solicited node multicast group. */
763 ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, i)->addr[3]);
764 mld6_joingroup(netif_ip6_addr(netif, i), &multicast_address);
766 #endif /* LWIP_IPV6_MLD */
767 /* Send a NS for this address. */
768 nd6_send_ns(netif, netif_ip6_addr(netif, i), ND6_SEND_FLAG_MULTICAST_DEST);
769 (netif->ip6_addr_state[i])++;
770 /* TODO send max 1 NS per tmr call? enable return*/
777 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
778 /* Send router solicitation messages, if necessary. */
779 for (netif = netif_list; netif != NULL; netif = netif->next) {
780 if ((netif->rs_count > 0) && (netif->flags & NETIF_FLAG_UP)) {
785 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
790 * Send a neighbor solicitation message
792 * @param netif the netif on which to send the message
793 * @param target_addr the IPv6 target address for the ND message
794 * @param flags one of ND6_SEND_FLAG_*
797 nd6_send_ns(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
799 struct ns_header * ns_hdr;
800 struct lladdr_option * lladdr_opt;
802 ip6_addr_t * src_addr;
804 if (ip6_addr_isvalid(netif_ip6_addr_state(netif,0))) {
805 /* Use link-local address as source address. */
806 src_addr = netif_ip6_addr(netif, 0);
808 src_addr = IP6_ADDR_ANY;
811 /* Allocate a packet. */
812 p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + sizeof(struct lladdr_option), PBUF_RAM);
813 if ((p == NULL) || (p->len < (sizeof(struct ns_header) + sizeof(struct lladdr_option)))) {
814 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
818 ND6_STATS_INC(nd6.memerr);
823 ns_hdr = (struct ns_header *)p->payload;
824 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
826 ns_hdr->type = ICMP6_TYPE_NS;
829 ns_hdr->reserved = 0;
830 ip6_addr_set(&(ns_hdr->target_address), target_addr);
832 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
833 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
834 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
836 /* Generate the solicited node address for the target address. */
837 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
838 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
839 target_addr = &multicast_address;
842 ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
845 /* Send the packet out. */
846 ND6_STATS_INC(nd6.xmit);
847 ip6_output_if(p, (src_addr == IP6_ADDR_ANY) ? NULL : src_addr, target_addr,
848 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
853 * Send a neighbor advertisement message
855 * @param netif the netif on which to send the message
856 * @param target_addr the IPv6 target address for the ND message
857 * @param flags one of ND6_SEND_FLAG_*
860 nd6_send_na(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
862 struct na_header * na_hdr;
863 struct lladdr_option * lladdr_opt;
865 ip6_addr_t * src_addr;
866 ip6_addr_t * dest_addr;
868 /* Use link-local address as source address. */
869 /* src_addr = &(netif->ip6_addr[0]); */
870 /* Use target address as source address. */
871 src_addr = target_addr;
873 /* Allocate a packet. */
874 p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + sizeof(struct lladdr_option), PBUF_RAM);
875 if ((p == NULL) || (p->len < (sizeof(struct na_header) + sizeof(struct lladdr_option)))) {
876 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
880 ND6_STATS_INC(nd6.memerr);
885 na_hdr = (struct na_header *)p->payload;
886 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
888 na_hdr->type = ICMP6_TYPE_NA;
891 na_hdr->flags = flags & 0xf0;
892 na_hdr->reserved[0] = 0;
893 na_hdr->reserved[1] = 0;
894 na_hdr->reserved[2] = 0;
895 ip6_addr_set(&(na_hdr->target_address), target_addr);
897 lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR;
898 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
899 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
901 /* Generate the solicited node address for the target address. */
902 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
903 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
904 dest_addr = &multicast_address;
906 else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
907 ip6_addr_set_allnodes_linklocal(&multicast_address);
908 dest_addr = &multicast_address;
911 dest_addr = ip6_current_src_addr();
914 na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
917 /* Send the packet out. */
918 ND6_STATS_INC(nd6.xmit);
919 ip6_output_if(p, src_addr, dest_addr,
920 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
924 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
926 * Send a router solicitation message
928 * @param netif the netif on which to send the message
931 nd6_send_rs(struct netif * netif)
933 struct rs_header * rs_hdr;
934 struct lladdr_option * lladdr_opt;
936 ip6_addr_t * src_addr;
939 /* Link-local source address, or unspecified address? */
940 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
941 src_addr = netif_ip6_addr(netif, 0);
944 src_addr = IP6_ADDR_ANY;
947 /* Generate the all routers target address. */
948 ip6_addr_set_allrouters_linklocal(&multicast_address);
950 /* Allocate a packet. */
951 packet_len = sizeof(struct rs_header);
952 if (src_addr != IP6_ADDR_ANY) {
953 packet_len += sizeof(struct lladdr_option);
955 p = pbuf_alloc(PBUF_IP, packet_len, PBUF_RAM);
956 if ((p == NULL) || (p->len < packet_len)) {
957 /* We couldn't allocate a suitable pbuf for the ns. drop it. */
961 ND6_STATS_INC(nd6.memerr);
966 rs_hdr = (struct rs_header *)p->payload;
968 rs_hdr->type = ICMP6_TYPE_RS;
971 rs_hdr->reserved = 0;
973 if (src_addr != IP6_ADDR_ANY) {
974 /* Include our hw address. */
975 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
976 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
977 lladdr_opt->length = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
978 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
981 rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
984 /* Send the packet out. */
985 ND6_STATS_INC(nd6.xmit);
986 ip6_output_if(p, src_addr, &multicast_address,
987 LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
990 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
993 * Search for a neighbor cache entry
995 * @param ip6addr the IPv6 address of the neighbor
996 * @return The neighbor cache entry index that matched, -1 if no
1000 nd6_find_neighbor_cache_entry(ip6_addr_t * ip6addr)
1003 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1004 if (ip6_addr_cmp(ip6addr, &(neighbor_cache[i].next_hop_address))) {
1012 * Create a new neighbor cache entry.
1014 * If no unused entry is found, will try to recycle an old entry
1015 * according to ad-hoc "age" heuristic.
1017 * @return The neighbor cache entry index that was created, -1 if no
1018 * entry could be created
1021 nd6_new_neighbor_cache_entry(void)
1028 /* First, try to find an empty entry. */
1029 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1030 if (neighbor_cache[i].state == ND6_NO_ENTRY) {
1035 /* We need to recycle an entry. in general, do not recycle if it is a router. */
1037 /* Next, try to find a Stale entry. */
1038 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1039 if ((neighbor_cache[i].state == ND6_STALE) &&
1040 (!neighbor_cache[i].isrouter)) {
1041 nd6_free_neighbor_cache_entry(i);
1046 /* Next, try to find a Probe entry. */
1047 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1048 if ((neighbor_cache[i].state == ND6_PROBE) &&
1049 (!neighbor_cache[i].isrouter)) {
1050 nd6_free_neighbor_cache_entry(i);
1055 /* Next, try to find a Delayed entry. */
1056 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1057 if ((neighbor_cache[i].state == ND6_DELAY) &&
1058 (!neighbor_cache[i].isrouter)) {
1059 nd6_free_neighbor_cache_entry(i);
1064 /* Next, try to find the oldest reachable entry. */
1065 time = 0xfffffffful;
1067 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1068 if ((neighbor_cache[i].state == ND6_REACHABLE) &&
1069 (!neighbor_cache[i].isrouter)) {
1070 if (neighbor_cache[i].counter.reachable_time < time) {
1072 time = neighbor_cache[i].counter.reachable_time;
1077 nd6_free_neighbor_cache_entry(j);
1081 /* Next, find oldest incomplete entry without queued packets. */
1084 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1086 #if LWIP_ND6_QUEUEING
1087 (neighbor_cache[i].q == NULL) &&
1088 #endif /* LWIP_ND6_QUEUEING */
1089 (neighbor_cache[i].state == ND6_INCOMPLETE) &&
1090 (!neighbor_cache[i].isrouter)) {
1091 if (neighbor_cache[i].counter.probes_sent >= time) {
1093 time = neighbor_cache[i].counter.probes_sent;
1098 nd6_free_neighbor_cache_entry(j);
1102 /* Next, find oldest incomplete entry with queued packets. */
1103 #if LWIP_ND6_QUEUEING
1106 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1107 if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
1108 (!neighbor_cache[i].isrouter)) {
1109 if (neighbor_cache[i].counter.probes_sent >= time) {
1111 time = neighbor_cache[i].counter.probes_sent;
1116 nd6_free_neighbor_cache_entry(j);
1119 #endif /* LWIP_ND6_QUEUEING */
1121 /* No more entries to try. */
1126 * Will free any resources associated with a neighbor cache
1127 * entry, and will mark it as unused.
1129 * @param i the neighbor cache entry index to free
1132 nd6_free_neighbor_cache_entry(s8_t i)
1134 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1138 #if LWIP_ND6_QUEUEING
1139 /* Free any queued packets. */
1140 if (neighbor_cache[i].q != NULL) {
1141 nd6_free_q(neighbor_cache[i].q);
1142 neighbor_cache[i].q = NULL;
1144 #endif /* LWIP_ND6_QUEUEING */
1146 neighbor_cache[i].state = ND6_NO_ENTRY;
1147 neighbor_cache[i].isrouter = 0;
1148 neighbor_cache[i].netif = NULL;
1149 neighbor_cache[i].counter.reachable_time = 0;
1150 ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address));
1154 * Search for a destination cache entry
1156 * @param ip6addr the IPv6 address of the destination
1157 * @return The destination cache entry index that matched, -1 if no
1161 nd6_find_destination_cache_entry(ip6_addr_t * ip6addr)
1164 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1165 if (ip6_addr_cmp(ip6addr, &(destination_cache[i].destination_addr))) {
1173 * Create a new destination cache entry. If no unused entry is found,
1174 * will recycle oldest entry.
1176 * @return The destination cache entry index that was created, -1 if no
1180 nd6_new_destination_cache_entry(void)
1185 /* Find an empty entry. */
1186 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1187 if (ip6_addr_isany(&(destination_cache[i].destination_addr))) {
1192 /* Find oldest entry. */
1194 j = LWIP_ND6_NUM_DESTINATIONS - 1;
1195 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1196 if (destination_cache[i].age > age) {
1205 * Determine whether an address matches an on-link prefix.
1207 * @param ip6addr the IPv6 address to match
1208 * @return 1 if the address is on-link, 0 otherwise
1211 nd6_is_prefix_in_netif(ip6_addr_t * ip6addr, struct netif * netif)
1214 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
1215 if ((prefix_list[i].netif == netif) &&
1216 (prefix_list[i].invalidation_timer > 0) &&
1217 ip6_addr_netcmp(ip6addr, &(prefix_list[i].prefix))) {
1221 /* Check to see if address prefix matches a (manually?) configured address. */
1222 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1223 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
1224 ip6_addr_netcmp(ip6addr, netif_ip6_addr(netif, i))) {
1232 * Select a default router for a destination.
1234 * @param ip6addr the destination address
1235 * @param netif the netif for the outgoing packet, if known
1236 * @return the default router entry index, or -1 if no suitable
1240 nd6_select_router(ip6_addr_t * ip6addr, struct netif * netif)
1243 /* last_router is used for round-robin router selection (as recommended
1244 * in RFC). This is more robust in case one router is not reachable,
1245 * we are not stuck trying to resolve it. */
1246 static s8_t last_router;
1247 (void)ip6addr; /* TODO match preferred routes!! (must implement ND6_OPTION_TYPE_ROUTE_INFO) */
1249 /* TODO: implement default router preference */
1251 /* Look for reachable routers. */
1252 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1253 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1256 if ((default_router_list[i].neighbor_entry != NULL) &&
1257 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1258 (default_router_list[i].invalidation_timer > 0) &&
1259 (default_router_list[i].neighbor_entry->state == ND6_REACHABLE)) {
1264 /* Look for router in other reachability states, but still valid according to timer. */
1265 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1266 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1269 if ((default_router_list[i].neighbor_entry != NULL) &&
1270 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1271 (default_router_list[i].invalidation_timer > 0)) {
1276 /* Look for any router for which we have any information at all. */
1277 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1278 if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1281 if (default_router_list[i].neighbor_entry != NULL &&
1282 (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1)) {
1287 /* no suitable router found. */
1292 * Find an entry for a default router.
1294 * @param router_addr the IPv6 address of the router
1295 * @param netif the netif on which the router is found, if known
1296 * @return the index of the router entry, or -1 if not found
1299 nd6_get_router(ip6_addr_t * router_addr, struct netif * netif)
1303 /* Look for router. */
1304 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1305 if ((default_router_list[i].neighbor_entry != NULL) &&
1306 ((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1307 ip6_addr_cmp(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) {
1312 /* router not found. */
1317 * Create a new entry for a default router.
1319 * @param router_addr the IPv6 address of the router
1320 * @param netif the netif on which the router is connected, if known
1321 * @return the index on the router table, or -1 if could not be created
1324 nd6_new_router(ip6_addr_t * router_addr, struct netif * netif)
1327 s8_t neighbor_index;
1329 /* Do we have a neighbor entry for this router? */
1330 neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
1331 if (neighbor_index < 0) {
1332 /* Create a neighbor entry for this router. */
1333 neighbor_index = nd6_new_neighbor_cache_entry();
1334 if (neighbor_index < 0) {
1335 /* Could not create neighbor entry for this router. */
1338 ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
1339 neighbor_cache[neighbor_index].netif = netif;
1340 #if LWIP_ND6_QUEUEING
1341 neighbor_cache[neighbor_index].q = NULL;
1342 #endif /* LWIP_ND6_QUEUEING */
1343 neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
1344 neighbor_cache[neighbor_index].counter.probes_sent = 0;
1347 /* Mark neighbor as router. */
1348 neighbor_cache[neighbor_index].isrouter = 1;
1350 /* Look for empty entry. */
1351 for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) {
1352 if (default_router_list[router_index].neighbor_entry == NULL) {
1353 default_router_list[router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
1354 return router_index;
1358 /* Could not create a router entry. */
1360 /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
1361 neighbor_cache[neighbor_index].isrouter = 0;
1363 /* router not found. */
1368 * Find the cached entry for an on-link prefix.
1370 * @param prefix the IPv6 prefix that is on-link
1371 * @param netif the netif on which the prefix is on-link
1372 * @return the index on the prefix table, or -1 if not found
1375 nd6_get_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1379 /* Look for prefix in list. */
1380 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1381 if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) &&
1382 (prefix_list[i].netif == netif)) {
1387 /* Entry not available. */
1392 * Creates a new entry for an on-link prefix.
1394 * @param prefix the IPv6 prefix that is on-link
1395 * @param netif the netif on which the prefix is on-link
1396 * @return the index on the prefix table, or -1 if not created
1399 nd6_new_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1403 /* Create new entry. */
1404 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1405 if ((prefix_list[i].netif == NULL) ||
1406 (prefix_list[i].invalidation_timer == 0)) {
1407 /* Found empty prefix entry. */
1408 prefix_list[i].netif = netif;
1409 ip6_addr_set(&(prefix_list[i].prefix), prefix);
1410 prefix_list[i].flags = 0;
1415 /* Entry not available. */
1420 * Determine the next hop for a destination. Will determine if the
1421 * destination is on-link, else a suitable on-link router is selected.
1423 * The last entry index is cached for fast entry search.
1425 * @param ip6addr the destination address
1426 * @param netif the netif on which the packet will be sent
1427 * @return the neighbor cache entry for the next hop, ERR_RTE if no
1428 * suitable next hop was found, ERR_MEM if no cache entry
1432 nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
1436 #if LWIP_NETIF_HWADDRHINT
1437 if (netif->addr_hint != NULL) {
1438 /* per-pcb cached entry was given */
1439 u8_t addr_hint = *(netif->addr_hint);
1440 if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
1441 nd6_cached_destination_index = addr_hint;
1444 #endif /* LWIP_NETIF_HWADDRHINT */
1446 /* Look for ip6addr in destination cache. */
1447 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
1448 /* the cached entry index is the right one! */
1450 ND6_STATS_INC(nd6.cachehit);
1452 /* Search destination cache. */
1453 i = nd6_find_destination_cache_entry(ip6addr);
1455 /* found destination entry. make it our new cached index. */
1456 nd6_cached_destination_index = i;
1459 /* Not found. Create a new destination entry. */
1460 i = nd6_new_destination_cache_entry();
1462 /* got new destination entry. make it our new cached index. */
1463 nd6_cached_destination_index = i;
1465 /* Could not create a destination cache entry. */
1469 /* Copy dest address to destination cache. */
1470 ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr);
1472 /* Now find the next hop. is it a neighbor? */
1473 if (ip6_addr_islinklocal(ip6addr) ||
1474 nd6_is_prefix_in_netif(ip6addr, netif)) {
1475 /* Destination in local link. */
1476 destination_cache[nd6_cached_destination_index].pmtu = netif->mtu;
1477 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, destination_cache[nd6_cached_destination_index].destination_addr);
1480 /* We need to select a router. */
1481 i = nd6_select_router(ip6addr, netif);
1483 /* No router found. */
1484 ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr));
1487 destination_cache[nd6_cached_destination_index].pmtu = netif->mtu; /* Start with netif mtu, correct through ICMPv6 if necessary */
1488 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address);
1493 #if LWIP_NETIF_HWADDRHINT
1494 if (netif->addr_hint != NULL) {
1495 /* per-pcb cached entry was given */
1496 *(netif->addr_hint) = nd6_cached_destination_index;
1498 #endif /* LWIP_NETIF_HWADDRHINT */
1500 /* Look in neighbor cache for the next-hop address. */
1501 if (ip6_addr_cmp(&(destination_cache[nd6_cached_destination_index].next_hop_addr),
1502 &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
1505 ND6_STATS_INC(nd6.cachehit);
1507 i = nd6_find_neighbor_cache_entry(&(destination_cache[nd6_cached_destination_index].next_hop_addr));
1509 /* Found a matching record, make it new cached entry. */
1510 nd6_cached_neighbor_index = i;
1513 /* Neighbor not in cache. Make a new entry. */
1514 i = nd6_new_neighbor_cache_entry();
1516 /* got new neighbor entry. make it our new cached index. */
1517 nd6_cached_neighbor_index = i;
1519 /* Could not create a neighbor cache entry. */
1523 /* Initialize fields. */
1524 ip6_addr_copy(neighbor_cache[i].next_hop_address,
1525 destination_cache[nd6_cached_destination_index].next_hop_addr);
1526 neighbor_cache[i].isrouter = 0;
1527 neighbor_cache[i].netif = netif;
1528 neighbor_cache[i].state = ND6_INCOMPLETE;
1529 neighbor_cache[i].counter.probes_sent = 0;
1533 /* Reset this destination's age. */
1534 destination_cache[nd6_cached_destination_index].age = 0;
1536 return nd6_cached_neighbor_index;
1539 #if LWIP_ND6_QUEUEING
1542 * Queue a packet for a neighbor.
1544 * @param neighbor_index the index in the neighbor cache table
1545 * @param q packet to be queued
1546 * @return ERR_OK if succeeded, ERR_MEM if out of memory
1549 nd6_queue_packet(s8_t neighbor_index, struct pbuf * q)
1551 err_t result = ERR_MEM;
1553 int copy_needed = 0;
1554 struct nd6_q_entry *new_entry, *r;
1556 if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
1560 /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
1561 * to copy the whole queue into a new PBUF_RAM (see bug #11400)
1562 * PBUF_ROMs can be left as they are, since ROM must not get changed. */
1565 if(p->type != PBUF_ROM) {
1572 /* copy the whole packet into new pbufs */
1573 p = pbuf_alloc(PBUF_LINK, q->tot_len, PBUF_RAM);
1574 while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1575 /* Free oldest packet (as per RFC recommendation) */
1576 r = neighbor_cache[neighbor_index].q;
1577 neighbor_cache[neighbor_index].q = r->next;
1580 p = pbuf_alloc(PBUF_LINK, q->tot_len, PBUF_RAM);
1583 if (pbuf_copy(p, q) != ERR_OK) {
1589 /* referencing the old pbuf is enough */
1593 /* packet was copied/ref'd? */
1595 /* queue packet ... */
1596 /* allocate a new nd6 queue entry */
1597 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1598 if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1599 /* Free oldest packet (as per RFC recommendation) */
1600 r = neighbor_cache[neighbor_index].q;
1601 neighbor_cache[neighbor_index].q = r->next;
1604 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1606 if (new_entry != NULL) {
1607 new_entry->next = NULL;
1609 if(neighbor_cache[neighbor_index].q != NULL) {
1610 /* queue was already existent, append the new entry to the end */
1611 r = neighbor_cache[neighbor_index].q;
1612 while (r->next != NULL) {
1615 r->next = new_entry;
1617 /* queue did not exist, first item in queue */
1618 neighbor_cache[neighbor_index].q = new_entry;
1620 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)q, (s16_t)neighbor_index));
1623 /* the pool MEMP_ND6_QUEUE is empty */
1625 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
1626 /* { result == ERR_MEM } through initialization */
1629 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
1630 /* { result == ERR_MEM } through initialization */
1637 * Free a complete queue of nd6 q entries
1639 * @param q a queue of nd6_q_entry to free
1642 nd6_free_q(struct nd6_q_entry *q)
1644 struct nd6_q_entry *r;
1645 LWIP_ASSERT("q != NULL", q != NULL);
1646 LWIP_ASSERT("q->p != NULL", q->p != NULL);
1650 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
1652 memp_free(MEMP_ND6_QUEUE, r);
1657 * Send queued packets for a neighbor
1659 * @param i the neighbor to send packets to
1664 struct ip6_hdr *ip6hdr;
1665 struct nd6_q_entry *q;
1667 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1671 while (neighbor_cache[i].q != NULL) {
1672 /* remember first in queue */
1673 q = neighbor_cache[i].q;
1674 /* pop first item off the queue */
1675 neighbor_cache[i].q = q->next;
1676 /* Get ipv6 header. */
1677 ip6hdr = (struct ip6_hdr *)(q->p->payload);
1678 /* Override ip6_current_dest_addr() so that we have an aligned copy. */
1679 ip6_addr_set(ip6_current_dest_addr(), &(ip6hdr->dest));
1680 /* send the queued IPv6 packet */
1681 (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, ip6_current_dest_addr());
1682 /* free the queued IP packet */
1684 /* now queue entry can be freed */
1685 memp_free(MEMP_ND6_QUEUE, q);
1689 #endif /* LWIP_ND6_QUEUEING */
1692 * Get the Path MTU for a destination.
1694 * @param ip6addr the destination address
1695 * @param netif the netif on which the packet will be sent
1696 * @return the Path MTU, if known, or the netif default MTU
1699 nd6_get_destination_mtu(ip6_addr_t * ip6addr, struct netif * netif)
1703 i = nd6_find_destination_cache_entry(ip6addr);
1705 if (destination_cache[i].pmtu > 0) {
1706 return destination_cache[i].pmtu;
1710 if (netif != NULL) {
1714 return 1280; /* Minimum MTU */
1718 #if LWIP_ND6_TCP_REACHABILITY_HINTS
1720 * Provide the Neighbor discovery process with a hint that a
1721 * destination is reachable. Called by tcp_receive when ACKs are
1722 * received or sent (as per RFC). This is useful to avoid sending
1723 * NS messages every 30 seconds.
1725 * @param ip6addr the destination address which is know to be reachable
1726 * by an upper layer protocol (TCP)
1729 nd6_reachability_hint(ip6_addr_t * ip6addr)
1733 /* Find destination in cache. */
1734 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
1735 i = nd6_cached_destination_index;
1736 ND6_STATS_INC(nd6.cachehit);
1739 i = nd6_find_destination_cache_entry(ip6addr);
1745 /* Find next hop neighbor in cache. */
1746 if (ip6_addr_cmp(&(destination_cache[i].next_hop_addr), &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
1747 i = nd6_cached_neighbor_index;
1748 ND6_STATS_INC(nd6.cachehit);
1751 i = nd6_find_neighbor_cache_entry(&(destination_cache[i].next_hop_addr));
1757 /* Set reachability state. */
1758 neighbor_cache[i].state = ND6_REACHABLE;
1759 neighbor_cache[i].counter.reachable_time = reachable_time;
1761 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
1763 #endif /* LWIP_IPV6 */