]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/ipv6/nd6.c
44629dbe5ce3674ade984f13df3215d7fef3a751
[pes-rpp/rpp-lwip.git] / src / core / ipv6 / nd6.c
1 /**
2  * @file
3  *
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).
7  */
8
9 /*
10  * Copyright (c) 2010 Inico Technologies Ltd.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
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.
23  *
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
33  * OF SUCH DAMAGE.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Ivan Delamer <delamer@inicotech.com>
38  *
39  *
40  * Please coordinate changes and requests with Ivan Delamer
41  * <delamer@inicotech.com>
42  */
43
44 #include "lwip/opt.h"
45
46 #if LWIP_IPV6  /* don't build if not configured for use in lwipopts.h */
47
48 #include "lwip/nd6.h"
49 #include "lwip/pbuf.h"
50 #include "lwip/mem.h"
51 #include "lwip/memp.h"
52 #include "lwip/ip6.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"
59
60 #include <string.h>
61
62
63 /* Router tables. */
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];
68
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 */
72
73 /* Index for cache entries. */
74 static u8_t nd6_cached_neighbor_index;
75 static u8_t nd6_cached_destination_index;
76
77 /* Multicast address holder. */
78 static ip6_addr_t multicast_address;
79
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)];
82
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);
94
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 */
102
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);
109
110
111 /**
112  * Process an incoming neighbor discovery message
113  *
114  * @param p the nd packet, p->payload pointing to the icmpv6 header
115  * @param inp the netif on which this packet was received
116  */
117 void
118 nd6_input(struct pbuf *p, struct netif *inp)
119 {
120   u8_t msg_type;
121   s8_t i;
122
123   ND6_STATS_INC(nd6.recv);
124
125   msg_type = *((u8_t *)p->payload);
126   switch (msg_type) {
127   case ICMP6_TYPE_NA: /* Neighbor Advertisement. */
128   {
129     struct na_header * na_hdr;
130     struct lladdr_option * lladdr_opt;
131
132     /* Check that na header fits in packet. */
133     if (p->len < (sizeof(struct na_header))) {
134       /* TODO debug message */
135       pbuf_free(p);
136       ND6_STATS_INC(nd6.lenerr);
137       ND6_STATS_INC(nd6.drop);
138       return;
139     }
140
141     na_hdr = (struct na_header *)p->payload;
142
143     /* Unsolicited NA?*/
144     if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
145       /* This is an unsolicited NA.
146        * link-layer changed?
147        * part of DAD mechanism? */
148
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 */
152         pbuf_free(p);
153         ND6_STATS_INC(nd6.lenerr);
154         ND6_STATS_INC(nd6.drop);
155         return;
156       }
157
158       lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
159
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));
162
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);
169
170 #if LWIP_IPV6_MLD
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 */
175
176
177
178
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);
183             if (i >= 0) {
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;
187             }
188           }
189 #endif /* LWIP_IPV6_AUTOCONFIG */
190
191           pbuf_free(p);
192           return;
193         }
194       }
195 #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
196
197       /* This is an unsolicited NA, most likely there was a LLADDR change. */
198       i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
199       if (i >= 0) {
200         if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
201           MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
202         }
203       }
204     }
205     else {
206       /* This is a solicited NA.
207        * neighbor address resolution response?
208        * neighbor unreachability detection response? */
209
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));
212
213       /* Find the cache entry corresponding to this na. */
214       i = nd6_find_neighbor_cache_entry(ip6_current_dest_addr());
215       if (i < 0) {
216         /* We no longer care about this target address. drop it. */
217         pbuf_free(p);
218         return;
219       }
220
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 */
229           pbuf_free(p);
230           ND6_STATS_INC(nd6.lenerr);
231           ND6_STATS_INC(nd6.drop);
232           return;
233         }
234
235         lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
236
237         MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
238       }
239       neighbor_cache[i].state = ND6_REACHABLE;
240
241       /* Send queued packets, if any. */
242       if (neighbor_cache[i].q != NULL) {
243         nd6_send_q(i);
244       }
245     }
246
247     break; /* ICMP6_TYPE_NA */
248   }
249   case ICMP6_TYPE_NS: /* Neighbor solicitation. */
250   {
251     struct ns_header * ns_hdr;
252     struct lladdr_option * lladdr_opt;
253     u8_t accepted;
254
255     /* Check that ns header fits in packet. */
256     if (p->len < sizeof(struct ns_header)) {
257       /* TODO debug message */
258       pbuf_free(p);
259       ND6_STATS_INC(nd6.lenerr);
260       ND6_STATS_INC(nd6.drop);
261       return;
262     }
263
264     ns_hdr = (struct ns_header *)p->payload;
265
266     /* Check if there is a link-layer address provided. Only point to it if in this buffer. */
267     lladdr_opt = NULL;
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));
270     }
271
272     /* Check if the target address is configured on the receiving netif. */
273     accepted = 0;
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))) {
279         accepted = 1;
280         break;
281       }
282     }
283
284     /* NS not for us? */
285     if (!accepted) {
286       pbuf_free(p);
287       return;
288     }
289
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);
300           }
301         }
302       }
303     }
304     else {
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. */
309         pbuf_free(p);
310         ND6_STATS_INC(nd6.proterr);
311         ND6_STATS_INC(nd6.drop);
312         return;
313       }
314
315       i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
316       if ( i>= 0) {
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);
321
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;
325         }
326       }
327       else
328       {
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();
333         if (i < 0) {
334           /* We couldn't assign a cache entry for this neighbor.
335            * we won't be able to reply. drop it. */
336           pbuf_free(p);
337           ND6_STATS_INC(nd6.memerr);
338           return;
339         }
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());
343
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;
348       }
349
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));
352
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);
355     }
356
357     break; /* ICMP6_TYPE_NS */
358   }
359   case ICMP6_TYPE_RA: /* Router Advertisement. */
360   {
361     struct ra_header * ra_hdr;
362     u8_t * buffer; /* Used to copy options. */
363     u16_t offset;
364
365     /* Check that RA header fits in packet. */
366     if (p->len < sizeof(struct ra_header)) {
367       /* TODO debug message */
368       pbuf_free(p);
369       ND6_STATS_INC(nd6.lenerr);
370       ND6_STATS_INC(nd6.drop);
371       return;
372     }
373
374     ra_hdr = (struct ra_header *)p->payload;
375
376     /* If we are sending RS messages, stop. */
377 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
378     inp->rs_count = 0;
379 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
380
381     /* Get the matching default router entry. */
382     i = nd6_get_router(ip6_current_src_addr(), inp);
383     if (i < 0) {
384       /* Create a new router entry. */
385       i = nd6_new_router(ip6_current_src_addr(), inp);
386     }
387
388     if (i < 0) {
389       /* Could not create a new router entry. */
390       pbuf_free(p);
391       ND6_STATS_INC(nd6.memerr);
392       return;
393     }
394
395     /* Re-set invalidation timer. */
396     default_router_list[i].invalidation_timer = ra_hdr->router_lifetime;
397
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;
402     }
403     if (ra_hdr->reachable_time > 0) {
404       reachable_time = ra_hdr->reachable_time;
405     }
406 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
407
408     /* TODO set default hop limit... */
409     /* ra_hdr->current_hop_limit;*/
410
411     /* Update flags in local entry (incl. preference). */
412     default_router_list[i].flags = ra_hdr->flags;
413
414     /* Offset to options. */
415     offset = sizeof(struct ra_header);
416
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];
422       } else {
423         buffer = nd6_ra_buffer;
424         pbuf_copy_partial(p, buffer, sizeof(struct prefix_option), offset);
425       }
426       switch (buffer[0]) {
427       case ND6_OPTION_TYPE_SOURCE_LLADDR:
428       {
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;
436         }
437         break;
438       }
439       case ND6_OPTION_TYPE_MTU:
440       {
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 */
447         }
448         break;
449       }
450       case ND6_OPTION_TYPE_PREFIX_INFO:
451       {
452         struct prefix_option * prefix_opt;
453         prefix_opt = (struct prefix_option *)buffer;
454
455         if (prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) {
456           /* Add to on-link prefix list. */
457
458           /* Get a memory-aligned copy of the prefix. */
459           ip6_addr_set(ip6_current_dest_addr(), &(prefix_opt->prefix));
460
461           /* find cache entry for this prefix. */
462           i = nd6_get_onlink_prefix(ip6_current_dest_addr(), inp);
463           if (i < 0) {
464             /* Create a new cache entry. */
465             i = nd6_new_onlink_prefix(ip6_current_dest_addr(), inp);
466           }
467           if (i >= 0) {
468             prefix_list[i].invalidation_timer = prefix_opt->valid_lifetime;
469
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;
475             }
476 #endif /* LWIP_IPV6_AUTOCONFIG */
477           }
478         }
479
480         break;
481       }
482       case ND6_OPTION_TYPE_ROUTE_INFO:
483       {
484         /* TODO implement preferred routes.
485         struct route_option * route_opt;
486         route_opt = (struct route_option *)buffer;*/
487
488         break;
489       }
490       default:
491         /* Unrecognized option, abort. */
492         ND6_STATS_INC(nd6.proterr);
493         break;
494       }
495       offset += 8 * ((u16_t)buffer[1]);
496     }
497
498     break; /* ICMP6_TYPE_RA */
499   }
500   case ICMP6_TYPE_RD: /* Redirect */
501   {
502     struct redirect_header * redir_hdr;
503     struct lladdr_option * lladdr_opt;
504
505     /* Check that Redir header fits in packet. */
506     if (p->len < sizeof(struct redirect_header)) {
507       /* TODO debug message */
508       pbuf_free(p);
509       ND6_STATS_INC(nd6.lenerr);
510       ND6_STATS_INC(nd6.drop);
511       return;
512     }
513
514     redir_hdr = (struct redirect_header *)p->payload;
515
516     lladdr_opt = NULL;
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));
519     }
520
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));
523
524     /* Find dest address in cache */
525     i = nd6_find_destination_cache_entry(ip6_current_src_addr());
526     if (i < 0) {
527       /* Destination not in cache, drop packet. */
528       pbuf_free(p);
529       return;
530     }
531
532     /* Set the new target address. */
533     ip6_addr_set(&(destination_cache[i].next_hop_addr), &(redir_hdr->target_address));
534
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));
540
541         i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
542         if (i < 0) {
543           i = nd6_new_neighbor_cache_entry();
544           if (i >= 0) {
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());
548
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;
553           }
554         }
555         if (i >= 0) {
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;
562           }
563         }
564       }
565     }
566     break; /* ICMP6_TYPE_RD */
567   }
568   case ICMP6_TYPE_PTB: /* Packet too big */
569   {
570     struct icmp6_hdr *icmp6hdr; /* Packet too big message */
571     struct ip6_hdr * ip6hdr; /* IPv6 header of the packet which caused the error */
572
573     /* Check that ICMPv6 header + IPv6 header fit in payload */
574     if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) {
575       /* drop short packets */
576       pbuf_free(p);
577       ND6_STATS_INC(nd6.lenerr);
578       ND6_STATS_INC(nd6.drop);
579       return;
580     }
581
582     icmp6hdr = (struct icmp6_hdr *)p->payload;
583     ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr));
584
585     /* Copy original destination address to current source address, to have an aligned copy. */
586     ip6_addr_set(ip6_current_src_addr(), &(ip6hdr->dest));
587
588     /* Look for entry in destination cache. */
589     i = nd6_find_destination_cache_entry(ip6_current_src_addr());
590     if (i < 0) {
591       /* Destination not in cache, drop packet. */
592       pbuf_free(p);
593       return;
594     }
595
596     /* Change the Path MTU. */
597     destination_cache[i].pmtu = icmp6hdr->data;
598
599     break; /* ICMP6_TYPE_PTB */
600   }
601
602   default:
603     ND6_STATS_INC(nd6.proterr);
604     ND6_STATS_INC(nd6.drop);
605     break; /* default */
606   }
607
608   pbuf_free(p);
609 }
610
611
612 /**
613  * Periodic timer for Neighbor discovery functions:
614  *
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
620  */
621 void
622 nd6_tmr(void)
623 {
624   s8_t i, j;
625   struct netif * netif;
626
627   /* Process neighbor entries. */
628   for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
629     switch (neighbor_cache[i].state) {
630     case ND6_INCOMPLETE:
631       if (neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) {
632         /* Retries exceeded. */
633         nd6_free_neighbor_cache_entry(i);
634       }
635       else {
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);
639       }
640       break;
641     case ND6_REACHABLE:
642       /* Send queued packets, if any are left. Should have been sent already. */
643       if (neighbor_cache[i].q != NULL) {
644         nd6_send_q(i);
645       }
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;
650       }
651       else {
652         neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
653       }
654       break;
655     case ND6_STALE:
656       neighbor_cache[i].counter.stale_time += ND6_TMR_INTERVAL;
657       break;
658     case ND6_DELAY:
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;
663       }
664       else {
665         neighbor_cache[i].counter.delay_time -= ND6_TMR_INTERVAL;
666       }
667       break;
668     case ND6_PROBE:
669       if (neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) {
670         /* Retries exceeded. */
671         nd6_free_neighbor_cache_entry(i);
672       }
673       else {
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);
677       }
678       break;
679     case ND6_NO_ENTRY:
680     default:
681       /* Do nothing. */
682       break;
683     }
684   }
685
686   /* Process destination entries. */
687   for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
688     destination_cache[i].age++;
689   }
690
691   /* Process router entries. */
692   for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
693     if (default_router_list[i].neighbor_entry != NULL) {
694       /* Active entry. */
695       if (default_router_list[i].invalidation_timer > 0) {
696         default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
697       }
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;
704       }
705     }
706   }
707
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;
712     }
713     if ((prefix_list[i].invalidation_timer > 0) &&
714         (prefix_list[i].netif != NULL)) {
715       prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
716
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];
731
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);
734
735             /* Mark this prefix with ADDRESS_GENERATED, so that we don't try again. */
736             prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED;
737
738             /* Exit loop. */
739             break;
740           }
741         }
742       }
743 #endif /* LWIP_IPV6_AUTOCONFIG */
744     }
745   }
746
747
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. */
756         }
757         else if (netif->flags & NETIF_FLAG_UP) {
758 #if LWIP_IPV6_MLD
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);
763           }
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*/
769           /*return;*/
770         }
771       }
772     }
773   }
774
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)) {
779       nd6_send_rs(netif);
780       netif->rs_count--;
781     }
782   }
783 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
784
785 }
786
787 /**
788  * Send a neighbor solicitation message
789  *
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_*
793  */
794 static void
795 nd6_send_ns(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
796 {
797   struct ns_header * ns_hdr;
798   struct lladdr_option * lladdr_opt;
799   struct pbuf * p;
800   ip6_addr_t * src_addr;
801
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);
805   } else {
806     src_addr = IP6_ADDR_ANY;
807   }
808
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. */
813     if (p != NULL) {
814       pbuf_free(p);
815     }
816     ND6_STATS_INC(nd6.memerr);
817     return;
818   }
819
820   /* Set fields. */
821   ns_hdr = (struct ns_header *)p->payload;
822   lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
823
824   ns_hdr->type = ICMP6_TYPE_NS;
825   ns_hdr->code = 0;
826   ns_hdr->chksum = 0;
827   ns_hdr->reserved = 0;
828   ip6_addr_set(&(ns_hdr->target_address), target_addr);
829
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);
833
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;
838   }
839
840   ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
841     target_addr);
842
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);
847   pbuf_free(p);
848 }
849
850 /**
851  * Send a neighbor advertisement message
852  *
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_*
856  */
857 static void
858 nd6_send_na(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
859 {
860   struct na_header * na_hdr;
861   struct lladdr_option * lladdr_opt;
862   struct pbuf * p;
863   ip6_addr_t * src_addr;
864   ip6_addr_t * dest_addr;
865
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;
870
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. */
875     if (p != NULL) {
876       pbuf_free(p);
877     }
878     ND6_STATS_INC(nd6.memerr);
879     return;
880   }
881
882   /* Set fields. */
883   na_hdr = (struct na_header *)p->payload;
884   lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
885
886   na_hdr->type = ICMP6_TYPE_NA;
887   na_hdr->code = 0;
888   na_hdr->chksum = 0;
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);
894
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);
898
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;
903   }
904   else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
905     ip6_addr_set_allnodes_linklocal(&multicast_address);
906     dest_addr = &multicast_address;
907   }
908   else {
909     dest_addr = ip6_current_src_addr();
910   }
911
912   na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
913     dest_addr);
914
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);
919   pbuf_free(p);
920 }
921
922 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
923 /**
924  * Send a router solicitation message
925  *
926  * @param netif the netif on which to send the message
927  */
928 static void
929 nd6_send_rs(struct netif * netif)
930 {
931   struct rs_header * rs_hdr;
932   struct lladdr_option * lladdr_opt;
933   struct pbuf * p;
934   ip6_addr_t * src_addr;
935   u16_t packet_len;
936
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);
940   }
941   else {
942     src_addr = IP6_ADDR_ANY;
943   }
944
945   /* Generate the all routers target address. */
946   ip6_addr_set_allrouters_linklocal(&multicast_address);
947
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);
952   }
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. */
956     if (p != NULL) {
957       pbuf_free(p);
958     }
959     ND6_STATS_INC(nd6.memerr);
960     return;
961   }
962
963   /* Set fields. */
964   rs_hdr = (struct rs_header *)p->payload;
965
966   rs_hdr->type = ICMP6_TYPE_RS;
967   rs_hdr->code = 0;
968   rs_hdr->chksum = 0;
969   rs_hdr->reserved = 0;
970
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);
977   }
978
979   rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
980     &multicast_address);
981
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);
986   pbuf_free(p);
987 }
988 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
989
990 /**
991  * Search for a neighbor cache entry
992  *
993  * @param ip6addr the IPv6 address of the neighbor
994  * @return The neighbor cache entry index that matched, -1 if no
995  * entry is found
996  */
997 static s8_t
998 nd6_find_neighbor_cache_entry(ip6_addr_t * ip6addr)
999 {
1000   s8_t i;
1001   for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1002     if (ip6_addr_cmp(ip6addr, &(neighbor_cache[i].next_hop_address))) {
1003       return i;
1004     }
1005   }
1006   return -1;
1007 }
1008
1009 /**
1010  * Create a new neighbor cache entry.
1011  *
1012  * If no unused entry is found, will try to recycle an old entry
1013  * according to ad-hoc "age" heuristic.
1014  *
1015  * @return The neighbor cache entry index that was created, -1 if no
1016  * entry could be created
1017  */
1018 static s8_t
1019 nd6_new_neighbor_cache_entry(void)
1020 {
1021   s8_t i;
1022   s8_t j;
1023   u32_t time;
1024
1025
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) {
1029       return i;
1030     }
1031   }
1032
1033   /* We need to recycle an entry. in general, do not recycle if it is a router. */
1034
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);
1040       return i;
1041     }
1042   }
1043
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);
1049       return i;
1050     }
1051   }
1052
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);
1058       return i;
1059     }
1060   }
1061
1062   /* Next, try to find the oldest reachable entry. */
1063   time = 0xfffffffful;
1064   j = -1;
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) {
1069         j = i;
1070         time = neighbor_cache[i].counter.reachable_time;
1071       }
1072     }
1073   }
1074   if (j >= 0) {
1075     nd6_free_neighbor_cache_entry(j);
1076     return j;
1077   }
1078
1079   /* Next, find oldest incomplete entry without queued packets. */
1080   time = 0;
1081   j = -1;
1082   for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1083     if (
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) {
1088         j = i;
1089         time = neighbor_cache[i].counter.probes_sent;
1090       }
1091     }
1092   }
1093   if (j >= 0) {
1094     nd6_free_neighbor_cache_entry(j);
1095     return j;
1096   }
1097
1098   /* Next, find oldest incomplete entry with queued packets. */
1099   time = 0;
1100   j = -1;
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) {
1105         j = i;
1106         time = neighbor_cache[i].counter.probes_sent;
1107       }
1108     }
1109   }
1110   if (j >= 0) {
1111     nd6_free_neighbor_cache_entry(j);
1112     return j;
1113   }
1114
1115   /* No more entries to try. */
1116   return -1;
1117 }
1118
1119 /**
1120  * Will free any resources associated with a neighbor cache
1121  * entry, and will mark it as unused.
1122  *
1123  * @param i the neighbor cache entry index to free
1124  */
1125 static void
1126 nd6_free_neighbor_cache_entry(s8_t i)
1127 {
1128   if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1129     return;
1130   }
1131
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;
1136   }
1137
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));
1143 }
1144
1145 /**
1146  * Search for a destination cache entry
1147  *
1148  * @param ip6addr the IPv6 address of the destination
1149  * @return The destination cache entry index that matched, -1 if no
1150  * entry is found
1151  */
1152 static s8_t
1153 nd6_find_destination_cache_entry(ip6_addr_t * ip6addr)
1154 {
1155   s8_t i;
1156   for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1157     if (ip6_addr_cmp(ip6addr, &(destination_cache[i].destination_addr))) {
1158       return i;
1159     }
1160   }
1161   return -1;
1162 }
1163
1164 /**
1165  * Create a new destination cache entry. If no unused entry is found,
1166  * will recycle oldest entry.
1167  *
1168  * @return The destination cache entry index that was created, -1 if no
1169  * entry was created
1170  */
1171 static s8_t
1172 nd6_new_destination_cache_entry(void)
1173 {
1174   s8_t i, j;
1175   u32_t age;
1176
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))) {
1180       return i;
1181     }
1182   }
1183
1184   /* Find oldest entry. */
1185   age = 0;
1186   j = LWIP_ND6_NUM_DESTINATIONS - 1;
1187   for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1188     if (destination_cache[i].age > age) {
1189       j = i;
1190     }
1191   }
1192
1193   return j;
1194 }
1195
1196 /**
1197  * Determine whether an address matches an on-link prefix.
1198  *
1199  * @param ip6addr the IPv6 address to match
1200  * @return 1 if the address is on-link, 0 otherwise
1201  */
1202 static s8_t
1203 nd6_is_prefix_in_netif(ip6_addr_t * ip6addr, struct netif * netif)
1204 {
1205   s8_t i;
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))) {
1210       return 1;
1211     }
1212   }
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))) {
1217       return 1;
1218     }
1219   }
1220   return 0;
1221 }
1222
1223 /**
1224  * Select a default router for a destination.
1225  *
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
1229  *         router is found
1230  */
1231 s8_t
1232 nd6_select_router(ip6_addr_t * ip6addr, struct netif * netif)
1233 {
1234   s8_t i;
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) */
1240
1241   /* TODO: implement default router preference */
1242
1243   /* Look for reachable routers. */
1244   for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1245     if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1246       last_router = 0;
1247     }
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)) {
1252       return i;
1253     }
1254   }
1255
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) {
1259       last_router = 0;
1260     }
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)) {
1264       return i;
1265     }
1266   }
1267
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) {
1271       last_router = 0;
1272     }
1273     if (default_router_list[i].neighbor_entry != NULL &&
1274         (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1)) {
1275       return i;
1276     }
1277   }
1278
1279   /* no suitable router found. */
1280   return -1;
1281 }
1282
1283 /**
1284  * Find an entry for a default router.
1285  *
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
1289  */
1290 static s8_t
1291 nd6_get_router(ip6_addr_t * router_addr, struct netif * netif)
1292 {
1293   s8_t i;
1294
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))) {
1300       return i;
1301     }
1302   }
1303
1304   /* router not found. */
1305   return -1;
1306 }
1307
1308 /**
1309  * Create a new entry for a default router.
1310  *
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
1314  */
1315 static s8_t
1316 nd6_new_router(ip6_addr_t * router_addr, struct netif * netif)
1317 {
1318   s8_t router_index;
1319   s8_t neighbor_index;
1320
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. */
1328       return -1;
1329     }
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;
1335   }
1336
1337   /* Mark neighbor as router. */
1338   neighbor_cache[neighbor_index].isrouter = 1;
1339
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;
1345     }
1346   }
1347
1348   /* Could not create a router entry. */
1349
1350   /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
1351   neighbor_cache[neighbor_index].isrouter = 0;
1352
1353   /* router not found. */
1354   return -1;
1355 }
1356
1357 /**
1358  * Find the cached entry for an on-link prefix.
1359  *
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
1363  */
1364 static s8_t
1365 nd6_get_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1366 {
1367   s8_t i;
1368
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)) {
1373       return i;
1374     }
1375   }
1376
1377   /* Entry not available. */
1378   return -1;
1379 }
1380
1381 /**
1382  * Creates a new entry for an on-link prefix.
1383  *
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
1387  */
1388 static s8_t
1389 nd6_new_onlink_prefix(ip6_addr_t * prefix, struct netif * netif)
1390 {
1391   s8_t i;
1392
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;
1401       return i;
1402     }
1403   }
1404
1405   /* Entry not available. */
1406   return -1;
1407 }
1408
1409 /**
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.
1412  *
1413  * The last entry index is cached for fast entry search.
1414  *
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
1419  *         could be created
1420  */
1421 s8_t
1422 nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
1423 {
1424   s8_t i;
1425
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;
1432     }
1433   }
1434 #endif /* LWIP_NETIF_HWADDRHINT */
1435
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! */
1439     /* do nothing. */
1440     ND6_STATS_INC(nd6.cachehit);
1441   } else {
1442     /* Search destination cache. */
1443     i = nd6_find_destination_cache_entry(ip6addr);
1444     if (i >= 0) {
1445       /* found destination entry. make it our new cached index. */
1446       nd6_cached_destination_index = i;
1447     }
1448     else {
1449       /* Not found. Create a new destination entry. */
1450       i = nd6_new_destination_cache_entry();
1451       if (i >= 0) {
1452         /* got new destination entry. make it our new cached index. */
1453         nd6_cached_destination_index = i;
1454       } else {
1455         /* Could not create a destination cache entry. */
1456         return ERR_MEM;
1457       }
1458
1459       /* Copy dest address to destination cache. */
1460       ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr);
1461
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);
1468       }
1469       else {
1470         /* We need to select a router. */
1471         i = nd6_select_router(ip6addr, netif);
1472         if (i < 0) {
1473           /* No router found. */
1474           ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr));
1475           return ERR_RTE;
1476         }
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);
1479       }
1480     }
1481   }
1482
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;
1487   }
1488 #endif /* LWIP_NETIF_HWADDRHINT */
1489
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))) {
1493     /* Cache hit. */
1494     /* Do nothing. */
1495     ND6_STATS_INC(nd6.cachehit);
1496   } else {
1497     i = nd6_find_neighbor_cache_entry(&(destination_cache[nd6_cached_destination_index].next_hop_addr));
1498     if (i >= 0) {
1499       /* Found a matching record, make it new cached entry. */
1500       nd6_cached_neighbor_index = i;
1501     }
1502     else {
1503       /* Neighbor not in cache. Make a new entry. */
1504       i = nd6_new_neighbor_cache_entry();
1505       if (i >= 0) {
1506         /* got new neighbor entry. make it our new cached index. */
1507         nd6_cached_neighbor_index = i;
1508       } else {
1509         /* Could not create a neighbor cache entry. */
1510         return ERR_MEM;
1511       }
1512
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;
1520     }
1521   }
1522
1523   /* Reset this destination's age. */
1524   destination_cache[nd6_cached_destination_index].age = 0;
1525
1526   return nd6_cached_neighbor_index;
1527 }
1528
1529 /**
1530  * Queue a packet for a neighbor.
1531  *
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
1535  */
1536 err_t
1537 nd6_queue_packet(s8_t neighbor_index, struct pbuf * q)
1538 {
1539   err_t result = ERR_MEM;
1540   struct pbuf *p;
1541   int copy_needed = 0;
1542 #if LWIP_ND6_QUEUEING
1543   struct nd6_q_entry *new_entry, *r;
1544 #endif /* LWIP_ND6_QUEUEING */
1545
1546   if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
1547     return ERR_ARG;
1548   }
1549
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. */
1553   p = q;
1554   while (p) {
1555     if(p->type != PBUF_ROM) {
1556       copy_needed = 1;
1557       break;
1558     }
1559     p = p->next;
1560   }
1561   if(copy_needed) {
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;
1569       r->next = NULL;
1570       nd6_free_q(r);
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);
1576     }
1577     if(p != NULL) {
1578       if (pbuf_copy(p, q) != ERR_OK) {
1579         pbuf_free(p);
1580         p = NULL;
1581       }
1582     }
1583   } else {
1584     /* referencing the old pbuf is enough */
1585     p = q;
1586     pbuf_ref(p);
1587   }
1588   /* packet was copied/ref'd? */
1589   if (p != NULL) {
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;
1598       r->next = NULL;
1599       nd6_free_q(r);
1600       new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1601     }
1602     if (new_entry != NULL) {
1603       new_entry->next = NULL;
1604       new_entry->p = p;
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) {
1609           r = r->next;
1610         }
1611         r->next = new_entry;
1612       } else {
1613         /* queue did not exist, first item in queue */
1614         neighbor_cache[neighbor_index].q = new_entry;
1615       }
1616       LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
1617       result = ERR_OK;
1618     } else {
1619       /* the pool MEMP_ND6_QUEUE is empty */
1620       pbuf_free(p);
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 */
1623     }
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);
1628     }
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));
1631     result = ERR_OK;
1632 #endif /* LWIP_ND6_QUEUEING */
1633   } else {
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 */
1636   }
1637
1638   return result;
1639 }
1640
1641 #if LWIP_ND6_QUEUEING
1642 /**
1643  * Free a complete queue of nd6 q entries
1644  *
1645  * @param q a queue of nd6_q_entry to free
1646  */
1647 static void
1648 nd6_free_q(struct nd6_q_entry *q)
1649 {
1650   struct nd6_q_entry *r;
1651   LWIP_ASSERT("q != NULL", q != NULL);
1652   LWIP_ASSERT("q->p != NULL", q->p != NULL);
1653   while (q) {
1654     r = q;
1655     q = q->next;
1656     LWIP_ASSERT("r->p != NULL", (r->p != NULL));
1657     pbuf_free(r->p);
1658     memp_free(MEMP_ND6_QUEUE, r);
1659   }
1660 }
1661 #endif /* LWIP_ND6_QUEUEING */
1662
1663 /**
1664  * Send queued packets for a neighbor
1665  *
1666  * @param i the neighbor to send packets to
1667  */
1668 static void
1669 nd6_send_q(s8_t i)
1670 {
1671   struct ip6_hdr *ip6hdr;
1672 #if LWIP_ND6_QUEUEING
1673   struct nd6_q_entry *q;
1674 #endif /* LWIP_ND6_QUEUEING */
1675
1676   if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1677     return;
1678   }
1679
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 */
1693     pbuf_free(q->p);
1694     /* now queue entry can be freed */
1695     memp_free(MEMP_ND6_QUEUE, q);
1696   }
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;
1708   }
1709 #endif /* LWIP_ND6_QUEUEING */
1710 }
1711
1712
1713 /**
1714  * Get the Path MTU for a destination.
1715  *
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
1719  */
1720 u16_t
1721 nd6_get_destination_mtu(ip6_addr_t * ip6addr, struct netif * netif)
1722 {
1723   s8_t i;
1724
1725   i = nd6_find_destination_cache_entry(ip6addr);
1726   if (i >= 0) {
1727     if (destination_cache[i].pmtu > 0) {
1728       return destination_cache[i].pmtu;
1729     }
1730   }
1731
1732   if (netif != NULL) {
1733     return netif->mtu;
1734   }
1735
1736   return 1280; /* Minimum MTU */
1737 }
1738
1739
1740 #if LWIP_ND6_TCP_REACHABILITY_HINTS
1741 /**
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.
1746  *
1747  * @param ip6addr the destination address which is know to be reachable
1748  *                by an upper layer protocol (TCP)
1749  */
1750 void
1751 nd6_reachability_hint(ip6_addr_t * ip6addr)
1752 {
1753   s8_t i;
1754
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);
1759   }
1760   else {
1761     i = nd6_find_destination_cache_entry(ip6addr);
1762   }
1763   if (i < 0) {
1764     return;
1765   }
1766
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);
1771   }
1772   else {
1773     i = nd6_find_neighbor_cache_entry(&(destination_cache[i].next_hop_addr));
1774   }
1775   if (i < 0) {
1776     return;
1777   }
1778
1779   /* Set reachability state. */
1780   neighbor_cache[i].state = ND6_REACHABLE;
1781   neighbor_cache[i].counter.reachable_time = reachable_time;
1782 }
1783 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
1784
1785 #endif /* LWIP_IPV6 */