The Pedigree Project  0.1
nd6.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
28 /*
29  * Copyright (c) 2010 Inico Technologies Ltd.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without modification,
33  * are permitted provided that the following conditions are met:
34  *
35  * 1. Redistributions of source code must retain the above copyright notice,
36  * this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright notice,
38  * this list of conditions and the following disclaimer in the documentation
39  * and/or other materials provided with the distribution.
40  * 3. The name of the author may not be used to endorse or promote products
41  * derived from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
44  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
46  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
48  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
51  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
52  * OF SUCH DAMAGE.
53  *
54  * This file is part of the lwIP TCP/IP stack.
55  *
56  * Author: Ivan Delamer <delamer@inicotech.com>
57  *
58  *
59  * Please coordinate changes and requests with Ivan Delamer
60  * <delamer@inicotech.com>
61  */
62 
63 #include "lwip/opt.h"
64 
65 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
66 
67 #include "lwip/nd6.h"
68 #include "lwip/priv/nd6_priv.h"
69 #include "lwip/prot/nd6.h"
70 #include "lwip/prot/icmp6.h"
71 #include "lwip/pbuf.h"
72 #include "lwip/mem.h"
73 #include "lwip/memp.h"
74 #include "lwip/ip6.h"
75 #include "lwip/ip6_addr.h"
76 #include "lwip/inet_chksum.h"
77 #include "lwip/netif.h"
78 #include "lwip/icmp6.h"
79 #include "lwip/mld6.h"
80 #include "lwip/ip.h"
81 #include "lwip/stats.h"
82 #include "lwip/dns.h"
83 
84 #include <string.h>
85 
86 #ifdef LWIP_HOOK_FILENAME
87 #include LWIP_HOOK_FILENAME
88 #endif
89 
90 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
91 #error LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
92 #endif
93 
94 /* Router tables. */
95 struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS];
96 struct nd6_destination_cache_entry destination_cache[LWIP_ND6_NUM_DESTINATIONS];
97 struct nd6_prefix_list_entry prefix_list[LWIP_ND6_NUM_PREFIXES];
98 struct nd6_router_list_entry default_router_list[LWIP_ND6_NUM_ROUTERS];
99 
100 /* Default values, can be updated by a RA message. */
101 u32_t reachable_time = LWIP_ND6_REACHABLE_TIME;
102 u32_t retrans_timer = LWIP_ND6_RETRANS_TIMER; /* @todo implement this value in timer */
103 
104 /* Index for cache entries. */
105 static u8_t nd6_cached_neighbor_index;
106 static u8_t nd6_cached_destination_index;
107 
108 /* Multicast address holder. */
109 static ip6_addr_t multicast_address;
110 
111 /* Static buffer to parse RA packet options (size of a prefix option, biggest option) */
112 static u8_t nd6_ra_buffer[sizeof(struct prefix_option)];
113 
114 /* Forward declarations. */
115 static s8_t nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr);
116 static s8_t nd6_new_neighbor_cache_entry(void);
117 static void nd6_free_neighbor_cache_entry(s8_t i);
118 static s8_t nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr);
119 static s8_t nd6_new_destination_cache_entry(void);
120 static s8_t nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif);
121 static s8_t nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif);
122 static s8_t nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif);
123 static s8_t nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif);
124 static s8_t nd6_get_onlink_prefix(ip6_addr_t *prefix, struct netif *netif);
125 static s8_t nd6_new_onlink_prefix(ip6_addr_t *prefix, struct netif *netif);
126 static s8_t nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif);
127 static err_t nd6_queue_packet(s8_t neighbor_index, struct pbuf *q);
128 
129 #define ND6_SEND_FLAG_MULTICAST_DEST 0x01
130 #define ND6_SEND_FLAG_ALLNODES_DEST 0x02
131 static void nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
132 static void nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
133 static void nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags);
134 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
135 static err_t nd6_send_rs(struct netif *netif);
136 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
137 
138 #if LWIP_ND6_QUEUEING
139 static void nd6_free_q(struct nd6_q_entry *q);
140 #else /* LWIP_ND6_QUEUEING */
141 #define nd6_free_q(q) pbuf_free(q)
142 #endif /* LWIP_ND6_QUEUEING */
143 static void nd6_send_q(s8_t i);
144 
145 
152 void
153 nd6_input(struct pbuf *p, struct netif *inp)
154 {
155  u8_t msg_type;
156  s8_t i;
157 
158  ND6_STATS_INC(nd6.recv);
159 
160  msg_type = *((u8_t *)p->payload);
161  switch (msg_type) {
162  case ICMP6_TYPE_NA: /* Neighbor Advertisement. */
163  {
164  struct na_header *na_hdr;
165  struct lladdr_option *lladdr_opt;
166 
167  /* Check that na header fits in packet. */
168  if (p->len < (sizeof(struct na_header))) {
169  /* @todo debug message */
170  pbuf_free(p);
171  ND6_STATS_INC(nd6.lenerr);
172  ND6_STATS_INC(nd6.drop);
173  return;
174  }
175 
176  na_hdr = (struct na_header *)p->payload;
177 
178  /* Unsolicited NA?*/
179  if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
180  ip6_addr_t target_address;
181 
182  /* This is an unsolicited NA.
183  * link-layer changed?
184  * part of DAD mechanism? */
185 
186  /* Create an aligned copy. */
187  ip6_addr_set(&target_address, &(na_hdr->target_address));
188 
189 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
190  /* If the target address matches this netif, it is a DAD response. */
191  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
192  if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
193  ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) {
194  /* We are using a duplicate address. */
195  netif_ip6_addr_set_state(inp, i, IP6_ADDR_INVALID);
196 
197 #if LWIP_IPV6_AUTOCONFIG
198  /* Check to see if this address was autoconfigured. */
199  if (!ip6_addr_islinklocal(&target_address)) {
200  i = nd6_get_onlink_prefix(&target_address, inp);
201  if (i >= 0) {
202  /* Mark this prefix as duplicate, so that we don't use it
203  * to generate this address again. */
204  prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_DUPLICATE;
205  }
206  }
207 #endif /* LWIP_IPV6_AUTOCONFIG */
208 
209  pbuf_free(p);
210  return;
211  }
212  }
213 #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
214 
215  /* Check that link-layer address option also fits in packet. */
216  if (p->len < (sizeof(struct na_header) + 2)) {
217  /* @todo debug message */
218  pbuf_free(p);
219  ND6_STATS_INC(nd6.lenerr);
220  ND6_STATS_INC(nd6.drop);
221  return;
222  }
223 
224  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
225 
226  if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
227  /* @todo debug message */
228  pbuf_free(p);
229  ND6_STATS_INC(nd6.lenerr);
230  ND6_STATS_INC(nd6.drop);
231  return;
232  }
233 
234  /* This is an unsolicited NA, most likely there was a LLADDR change. */
235  i = nd6_find_neighbor_cache_entry(&target_address);
236  if (i >= 0) {
237  if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
238  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
239  }
240  }
241  } else {
242  ip6_addr_t target_address;
243 
244  /* This is a solicited NA.
245  * neighbor address resolution response?
246  * neighbor unreachability detection response? */
247 
248  /* Create an aligned copy. */
249  ip6_addr_set(&target_address, &(na_hdr->target_address));
250 
251  /* Find the cache entry corresponding to this na. */
252  i = nd6_find_neighbor_cache_entry(&target_address);
253  if (i < 0) {
254  /* We no longer care about this target address. drop it. */
255  pbuf_free(p);
256  return;
257  }
258 
259  /* Update cache entry. */
260  if ((na_hdr->flags & ND6_FLAG_OVERRIDE) ||
261  (neighbor_cache[i].state == ND6_INCOMPLETE)) {
262  /* Check that link-layer address option also fits in packet. */
263  if (p->len < (sizeof(struct na_header) + 2)) {
264  /* @todo debug message */
265  pbuf_free(p);
266  ND6_STATS_INC(nd6.lenerr);
267  ND6_STATS_INC(nd6.drop);
268  return;
269  }
270 
271  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
272 
273  if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
274  /* @todo debug message */
275  pbuf_free(p);
276  ND6_STATS_INC(nd6.lenerr);
277  ND6_STATS_INC(nd6.drop);
278  return;
279  }
280 
281  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
282  }
283 
284  neighbor_cache[i].netif = inp;
285  neighbor_cache[i].state = ND6_REACHABLE;
286  neighbor_cache[i].counter.reachable_time = reachable_time;
287 
288  /* Send queued packets, if any. */
289  if (neighbor_cache[i].q != NULL) {
290  nd6_send_q(i);
291  }
292  }
293 
294  break; /* ICMP6_TYPE_NA */
295  }
296  case ICMP6_TYPE_NS: /* Neighbor solicitation. */
297  {
298  struct ns_header *ns_hdr;
299  struct lladdr_option *lladdr_opt;
300  u8_t accepted;
301 
302  /* Check that ns header fits in packet. */
303  if (p->len < sizeof(struct ns_header)) {
304  /* @todo debug message */
305  pbuf_free(p);
306  ND6_STATS_INC(nd6.lenerr);
307  ND6_STATS_INC(nd6.drop);
308  return;
309  }
310 
311  ns_hdr = (struct ns_header *)p->payload;
312 
313  /* Check if there is a link-layer address provided. Only point to it if in this buffer. */
314  if (p->len >= (sizeof(struct ns_header) + 2)) {
315  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
316  if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) {
317  lladdr_opt = NULL;
318  }
319  } else {
320  lladdr_opt = NULL;
321  }
322 
323  /* Check if the target address is configured on the receiving netif. */
324  accepted = 0;
325  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
326  if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) ||
327  (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) &&
328  ip6_addr_isany(ip6_current_src_addr()))) &&
329  ip6_addr_cmp(&(ns_hdr->target_address), netif_ip6_addr(inp, i))) {
330  accepted = 1;
331  break;
332  }
333  }
334 
335  /* NS not for us? */
336  if (!accepted) {
337  pbuf_free(p);
338  return;
339  }
340 
341  /* Check for ANY address in src (DAD algorithm). */
342  if (ip6_addr_isany(ip6_current_src_addr())) {
343  /* Sender is validating this address. */
344  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
345  if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
346  ip6_addr_cmp(&(ns_hdr->target_address), netif_ip6_addr(inp, i))) {
347  /* Send a NA back so that the sender does not use this address. */
348  nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST);
349  if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) {
350  /* We shouldn't use this address either. */
351  netif_ip6_addr_set_state(inp, i, IP6_ADDR_INVALID);
352  }
353  }
354  }
355  } else {
356  ip6_addr_t target_address;
357 
358  /* Sender is trying to resolve our address. */
359  /* Verify that they included their own link-layer address. */
360  if (lladdr_opt == NULL) {
361  /* Not a valid message. */
362  pbuf_free(p);
363  ND6_STATS_INC(nd6.proterr);
364  ND6_STATS_INC(nd6.drop);
365  return;
366  }
367 
368  i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
369  if (i>= 0) {
370  /* We already have a record for the solicitor. */
371  if (neighbor_cache[i].state == ND6_INCOMPLETE) {
372  neighbor_cache[i].netif = inp;
373  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
374 
375  /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */
376  neighbor_cache[i].state = ND6_DELAY;
377  neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
378  }
379  } else {
380  /* Add their IPv6 address and link-layer address to neighbor cache.
381  * We will need it at least to send a unicast NA message, but most
382  * likely we will also be communicating with this node soon. */
383  i = nd6_new_neighbor_cache_entry();
384  if (i < 0) {
385  /* We couldn't assign a cache entry for this neighbor.
386  * we won't be able to reply. drop it. */
387  pbuf_free(p);
388  ND6_STATS_INC(nd6.memerr);
389  return;
390  }
391  neighbor_cache[i].netif = inp;
392  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
393  ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr());
394 
395  /* Receiving a message does not prove reachability: only in one direction.
396  * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
397  neighbor_cache[i].state = ND6_DELAY;
398  neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
399  }
400 
401  /* Create an aligned copy. */
402  ip6_addr_set(&target_address, &(ns_hdr->target_address));
403 
404  /* Send back a NA for us. Allocate the reply pbuf. */
405  nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE);
406  }
407 
408  break; /* ICMP6_TYPE_NS */
409  }
410  case ICMP6_TYPE_RA: /* Router Advertisement. */
411  {
412  struct ra_header *ra_hdr;
413  u8_t *buffer; /* Used to copy options. */
414  u16_t offset;
415 #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
416  /* There can by multiple RDNSS options per RA */
417  u8_t rdnss_server_idx = 0;
418 #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */
419 
420  /* Check that RA header fits in packet. */
421  if (p->len < sizeof(struct ra_header)) {
422  /* @todo debug message */
423  pbuf_free(p);
424  ND6_STATS_INC(nd6.lenerr);
425  ND6_STATS_INC(nd6.drop);
426  return;
427  }
428 
429  ra_hdr = (struct ra_header *)p->payload;
430 
431  /* If we are sending RS messages, stop. */
433  /* ensure at least one solicitation is sent */
434  if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) ||
435  (nd6_send_rs(inp) == ERR_OK)) {
436  inp->rs_count = 0;
437  }
438 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
439 
440  /* Get the matching default router entry. */
441  i = nd6_get_router(ip6_current_src_addr(), inp);
442  if (i < 0) {
443  /* Create a new router entry. */
444  i = nd6_new_router(ip6_current_src_addr(), inp);
445  }
446 
447  if (i < 0) {
448  /* Could not create a new router entry. */
449  pbuf_free(p);
450  ND6_STATS_INC(nd6.memerr);
451  return;
452  }
453 
454  /* Re-set invalidation timer. */
455  default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime);
456 
457  /* Re-set default timer values. */
458 #if LWIP_ND6_ALLOW_RA_UPDATES
459  if (ra_hdr->retrans_timer > 0) {
460  retrans_timer = lwip_htonl(ra_hdr->retrans_timer);
461  }
462  if (ra_hdr->reachable_time > 0) {
463  reachable_time = lwip_htonl(ra_hdr->reachable_time);
464  }
465 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
466 
467  /* @todo set default hop limit... */
468  /* ra_hdr->current_hop_limit;*/
469 
470  /* Update flags in local entry (incl. preference). */
471  default_router_list[i].flags = ra_hdr->flags;
472 
473  /* Offset to options. */
474  offset = sizeof(struct ra_header);
475 
476  /* Process each option. */
477  while ((p->tot_len - offset) > 0) {
478  if (p->len == p->tot_len) {
479  /* no need to copy from contiguous pbuf */
480  buffer = &((u8_t*)p->payload)[offset];
481  } else {
482  buffer = nd6_ra_buffer;
483  if (pbuf_copy_partial(p, buffer, sizeof(struct prefix_option), offset) != sizeof(struct prefix_option)) {
484  pbuf_free(p);
485  ND6_STATS_INC(nd6.lenerr);
486  ND6_STATS_INC(nd6.drop);
487  return;
488  }
489  }
490  if (buffer[1] == 0) {
491  /* zero-length extension. drop packet */
492  pbuf_free(p);
493  ND6_STATS_INC(nd6.lenerr);
494  ND6_STATS_INC(nd6.drop);
495  return;
496  }
497  switch (buffer[0]) {
499  {
500  struct lladdr_option *lladdr_opt;
501  lladdr_opt = (struct lladdr_option *)buffer;
502  if ((default_router_list[i].neighbor_entry != NULL) &&
503  (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) {
504  SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len);
505  default_router_list[i].neighbor_entry->state = ND6_REACHABLE;
506  default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time;
507  }
508  break;
509  }
510  case ND6_OPTION_TYPE_MTU:
511  {
512  struct mtu_option *mtu_opt;
513  mtu_opt = (struct mtu_option *)buffer;
514  if (lwip_htonl(mtu_opt->mtu) >= 1280) {
515 #if LWIP_ND6_ALLOW_RA_UPDATES
516  inp->mtu = (u16_t)lwip_htonl(mtu_opt->mtu);
517 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
518  }
519  break;
520  }
522  {
523  struct prefix_option *prefix_opt;
524  prefix_opt = (struct prefix_option *)buffer;
525 
526  if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) &&
527  (prefix_opt->prefix_length == 64) &&
528  !ip6_addr_islinklocal(&(prefix_opt->prefix))) {
529  /* Add to on-link prefix list. */
530  s8_t prefix;
531  ip6_addr_t prefix_addr;
532 
533  /* Get a memory-aligned copy of the prefix. */
534  ip6_addr_set(&prefix_addr, &(prefix_opt->prefix));
535 
536  /* find cache entry for this prefix. */
537  prefix = nd6_get_onlink_prefix(&prefix_addr, inp);
538  if (prefix < 0) {
539  /* Create a new cache entry. */
540  prefix = nd6_new_onlink_prefix(&prefix_addr, inp);
541  }
542  if (prefix >= 0) {
543  prefix_list[prefix].invalidation_timer = lwip_htonl(prefix_opt->valid_lifetime);
544 
545 #if LWIP_IPV6_AUTOCONFIG
546  if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) {
547  /* Mark prefix as autonomous, so that address autoconfiguration can take place.
548  * Only OR flag, so that we don't over-write other flags (such as ADDRESS_DUPLICATE)*/
549  prefix_list[prefix].flags |= ND6_PREFIX_AUTOCONFIG_AUTONOMOUS;
550  }
551 #endif /* LWIP_IPV6_AUTOCONFIG */
552  }
553  }
554 
555  break;
556  }
558  /* @todo implement preferred routes.
559  struct route_option * route_opt;
560  route_opt = (struct route_option *)buffer;*/
561 
562  break;
563 #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
564  case ND6_OPTION_TYPE_RDNSS:
565  {
566  u8_t num, n;
567  struct rdnss_option * rdnss_opt;
568 
569  rdnss_opt = (struct rdnss_option *)buffer;
570  num = (rdnss_opt->length - 1) / 2;
571  for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++) {
572  ip_addr_t rdnss_address;
573 
574  /* Get a memory-aligned copy of the prefix. */
575  ip_addr_copy_from_ip6(rdnss_address, rdnss_opt->rdnss_address[n]);
576 
577  if (htonl(rdnss_opt->lifetime) > 0) {
578  /* TODO implement Lifetime > 0 */
579  dns_setserver(rdnss_server_idx++, &rdnss_address);
580  } else {
581  /* TODO implement DNS removal in dns.c */
582  u8_t s;
583  for (s = 0; s < DNS_MAX_SERVERS; s++) {
584  const ip_addr_t *addr = dns_getserver(s);
585  if(ip_addr_cmp(addr, &rdnss_address)) {
586  dns_setserver(s, NULL);
587  }
588  }
589  }
590  }
591  break;
592  }
593 #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */
594  default:
595  /* Unrecognized option, abort. */
596  ND6_STATS_INC(nd6.proterr);
597  break;
598  }
599  /* option length is checked earlier to be non-zero to make sure loop ends */
600  offset += 8 * ((u16_t)buffer[1]);
601  }
602 
603  break; /* ICMP6_TYPE_RA */
604  }
605  case ICMP6_TYPE_RD: /* Redirect */
606  {
607  struct redirect_header *redir_hdr;
608  struct lladdr_option *lladdr_opt;
609  ip6_addr_t tmp;
610 
611  /* Check that Redir header fits in packet. */
612  if (p->len < sizeof(struct redirect_header)) {
613  /* @todo debug message */
614  pbuf_free(p);
615  ND6_STATS_INC(nd6.lenerr);
616  ND6_STATS_INC(nd6.drop);
617  return;
618  }
619 
620  redir_hdr = (struct redirect_header *)p->payload;
621 
622  if (p->len >= (sizeof(struct redirect_header) + 2)) {
623  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header));
624  if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) {
625  lladdr_opt = NULL;
626  }
627  } else {
628  lladdr_opt = NULL;
629  }
630 
631  /* Copy original destination address to current source address, to have an aligned copy. */
632  ip6_addr_set(&tmp, &(redir_hdr->destination_address));
633 
634  /* Find dest address in cache */
635  i = nd6_find_destination_cache_entry(&tmp);
636  if (i < 0) {
637  /* Destination not in cache, drop packet. */
638  pbuf_free(p);
639  return;
640  }
641 
642  /* Set the new target address. */
643  ip6_addr_set(&(destination_cache[i].next_hop_addr), &(redir_hdr->target_address));
644 
645  /* If Link-layer address of other router is given, try to add to neighbor cache. */
646  if (lladdr_opt != NULL) {
647  if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) {
648  /* Copy target address to current source address, to have an aligned copy. */
649  ip6_addr_set(&tmp, &(redir_hdr->target_address));
650 
651  i = nd6_find_neighbor_cache_entry(&tmp);
652  if (i < 0) {
653  i = nd6_new_neighbor_cache_entry();
654  if (i >= 0) {
655  neighbor_cache[i].netif = inp;
656  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
657  ip6_addr_set(&(neighbor_cache[i].next_hop_address), &tmp);
658 
659  /* Receiving a message does not prove reachability: only in one direction.
660  * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
661  neighbor_cache[i].state = ND6_DELAY;
662  neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
663  }
664  }
665  if (i >= 0) {
666  if (neighbor_cache[i].state == ND6_INCOMPLETE) {
667  MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
668  /* Receiving a message does not prove reachability: only in one direction.
669  * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
670  neighbor_cache[i].state = ND6_DELAY;
671  neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
672  }
673  }
674  }
675  }
676  break; /* ICMP6_TYPE_RD */
677  }
678  case ICMP6_TYPE_PTB: /* Packet too big */
679  {
680  struct icmp6_hdr *icmp6hdr; /* Packet too big message */
681  struct ip6_hdr *ip6hdr; /* IPv6 header of the packet which caused the error */
682  u32_t pmtu;
683  ip6_addr_t tmp;
684 
685  /* Check that ICMPv6 header + IPv6 header fit in payload */
686  if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) {
687  /* drop short packets */
688  pbuf_free(p);
689  ND6_STATS_INC(nd6.lenerr);
690  ND6_STATS_INC(nd6.drop);
691  return;
692  }
693 
694  icmp6hdr = (struct icmp6_hdr *)p->payload;
695  ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr));
696 
697  /* Copy original destination address to current source address, to have an aligned copy. */
698  ip6_addr_set(&tmp, &(ip6hdr->dest));
699 
700  /* Look for entry in destination cache. */
701  i = nd6_find_destination_cache_entry(&tmp);
702  if (i < 0) {
703  /* Destination not in cache, drop packet. */
704  pbuf_free(p);
705  return;
706  }
707 
708  /* Change the Path MTU. */
709  pmtu = lwip_htonl(icmp6hdr->data);
710  destination_cache[i].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF);
711 
712  break; /* ICMP6_TYPE_PTB */
713  }
714 
715  default:
716  ND6_STATS_INC(nd6.proterr);
717  ND6_STATS_INC(nd6.drop);
718  break; /* default */
719  }
720 
721  pbuf_free(p);
722 }
723 
724 
734 void
735 nd6_tmr(void)
736 {
737  s8_t i;
738  struct netif *netif;
739 
740  /* Process neighbor entries. */
741  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
742  switch (neighbor_cache[i].state) {
743  case ND6_INCOMPLETE:
744  if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
745  (!neighbor_cache[i].isrouter)) {
746  /* Retries exceeded. */
747  nd6_free_neighbor_cache_entry(i);
748  } else {
749  /* Send a NS for this entry. */
750  neighbor_cache[i].counter.probes_sent++;
751  nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
752  }
753  break;
754  case ND6_REACHABLE:
755  /* Send queued packets, if any are left. Should have been sent already. */
756  if (neighbor_cache[i].q != NULL) {
757  nd6_send_q(i);
758  }
759  if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) {
760  /* Change to stale state. */
761  neighbor_cache[i].state = ND6_STALE;
762  neighbor_cache[i].counter.stale_time = 0;
763  } else {
764  neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
765  }
766  break;
767  case ND6_STALE:
768  neighbor_cache[i].counter.stale_time++;
769  break;
770  case ND6_DELAY:
771  if (neighbor_cache[i].counter.delay_time <= 1) {
772  /* Change to PROBE state. */
773  neighbor_cache[i].state = ND6_PROBE;
774  neighbor_cache[i].counter.probes_sent = 0;
775  } else {
776  neighbor_cache[i].counter.delay_time--;
777  }
778  break;
779  case ND6_PROBE:
780  if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
781  (!neighbor_cache[i].isrouter)) {
782  /* Retries exceeded. */
783  nd6_free_neighbor_cache_entry(i);
784  } else {
785  /* Send a NS for this entry. */
786  neighbor_cache[i].counter.probes_sent++;
787  nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0);
788  }
789  break;
790  case ND6_NO_ENTRY:
791  default:
792  /* Do nothing. */
793  break;
794  }
795  }
796 
797  /* Process destination entries. */
798  for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
799  destination_cache[i].age++;
800  }
801 
802  /* Process router entries. */
803  for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
804  if (default_router_list[i].neighbor_entry != NULL) {
805  /* Active entry. */
806  if (default_router_list[i].invalidation_timer > 0) {
807  default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
808  }
809  if (default_router_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
810  /* Less than 1 second remaining. Clear this entry. */
811  default_router_list[i].neighbor_entry->isrouter = 0;
812  default_router_list[i].neighbor_entry = NULL;
813  default_router_list[i].invalidation_timer = 0;
814  default_router_list[i].flags = 0;
815  }
816  }
817  }
818 
819  /* Process prefix entries. */
820  for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
821  if (prefix_list[i].netif != NULL) {
822  if (prefix_list[i].invalidation_timer < ND6_TMR_INTERVAL / 1000) {
823  /* Entry timed out, remove it */
824  prefix_list[i].invalidation_timer = 0;
825 
826 #if LWIP_IPV6_AUTOCONFIG
827  /* If any addresses were configured with this prefix, remove them */
828  if (prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED) {
829  s8_t j;
830 
831  for (j = 1; j < LWIP_IPV6_NUM_ADDRESSES; j++) {
832  if ((netif_ip6_addr_state(prefix_list[i].netif, j) != IP6_ADDR_INVALID) &&
833  ip6_addr_netcmp(&prefix_list[i].prefix, netif_ip6_addr(prefix_list[i].netif, j))) {
834  netif_ip6_addr_set_state(prefix_list[i].netif, j, IP6_ADDR_INVALID);
835  prefix_list[i].flags = 0;
836 
837  /* Exit loop. */
838  break;
839  }
840  }
841  }
842 #endif /* LWIP_IPV6_AUTOCONFIG */
843 
844  prefix_list[i].netif = NULL;
845  prefix_list[i].flags = 0;
846  } else {
847  prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
848 
849 #if LWIP_IPV6_AUTOCONFIG
850  /* Initiate address autoconfiguration for this prefix, if conditions are met. */
851  if (prefix_list[i].netif->ip6_autoconfig_enabled &&
852  (prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_AUTONOMOUS) &&
853  !(prefix_list[i].flags & ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED)) {
854  s8_t j;
855  /* Try to get an address on this netif that is invalid.
856  * Skip 0 index (link-local address) */
857  for (j = 1; j < LWIP_IPV6_NUM_ADDRESSES; j++) {
858  if (netif_ip6_addr_state(prefix_list[i].netif, j) == IP6_ADDR_INVALID) {
859  /* Generate an address using this prefix and interface ID from link-local address. */
860  netif_ip6_addr_set_parts(prefix_list[i].netif, j,
861  prefix_list[i].prefix.addr[0], prefix_list[i].prefix.addr[1],
862  netif_ip6_addr(prefix_list[i].netif, 0)->addr[2], netif_ip6_addr(prefix_list[i].netif, 0)->addr[3]);
863 
864  /* Mark it as tentative (DAD will be performed if configured). */
865  netif_ip6_addr_set_state(prefix_list[i].netif, j, IP6_ADDR_TENTATIVE);
866 
867  /* Mark this prefix with ADDRESS_GENERATED, so that we don't try again. */
868  prefix_list[i].flags |= ND6_PREFIX_AUTOCONFIG_ADDRESS_GENERATED;
869 
870  /* Exit loop. */
871  break;
872  }
873  }
874  }
875 #endif /* LWIP_IPV6_AUTOCONFIG */
876  }
877  }
878  }
879 
880 
881  /* Process our own addresses, if DAD configured. */
882  for (netif = netif_list; netif != NULL; netif = netif->next) {
883  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
884  u8_t addr_state = netif_ip6_addr_state(netif, i);
885  if (ip6_addr_istentative(addr_state)) {
886  if ((addr_state & IP6_ADDR_TENTATIVE_COUNT_MASK) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) {
887  /* No NA received in response. Mark address as valid. */
888  netif_ip6_addr_set_state(netif, i, IP6_ADDR_PREFERRED);
889  /* @todo implement preferred and valid lifetimes. */
890  } else if (netif->flags & NETIF_FLAG_UP) {
891  /* Send a NS for this address. */
892  nd6_send_ns(netif, netif_ip6_addr(netif, i), ND6_SEND_FLAG_MULTICAST_DEST);
893  /* tentative: set next state by increasing by one */
894  netif_ip6_addr_set_state(netif, i, addr_state + 1);
895  /* @todo send max 1 NS per tmr call? enable return*/
896  /*return;*/
897  }
898  }
899  }
900  }
901 
902 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
903  /* Send router solicitation messages, if necessary. */
904  for (netif = netif_list; netif != NULL; netif = netif->next) {
905  if ((netif->rs_count > 0) && (netif->flags & NETIF_FLAG_UP) &&
906  (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, 0)))) {
907  if (nd6_send_rs(netif) == ERR_OK) {
908  netif->rs_count--;
909  }
910  }
911  }
912 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
913 
914 }
915 
921 static void
922 nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags)
923 {
924  nd6_send_ns(entry->netif, &entry->next_hop_address, flags);
925 }
926 
934 static void
935 nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
936 {
937  struct ns_header *ns_hdr;
938  struct pbuf *p;
939  const ip6_addr_t *src_addr;
940  u16_t lladdr_opt_len;
941 
942  if (ip6_addr_isvalid(netif_ip6_addr_state(netif,0))) {
943  /* Use link-local address as source address. */
944  src_addr = netif_ip6_addr(netif, 0);
945  /* calculate option length (in 8-byte-blocks) */
946  lladdr_opt_len = ((netif->hwaddr_len + 2) + 7) >> 3;
947  } else {
948  src_addr = IP6_ADDR_ANY6;
949  /* Option "MUST NOT be included when the source IP address is the unspecified address." */
950  lladdr_opt_len = 0;
951  }
952 
953  /* Allocate a packet. */
954  p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + (lladdr_opt_len << 3), PBUF_RAM);
955  if (p == NULL) {
956  ND6_STATS_INC(nd6.memerr);
957  return;
958  }
959 
960  /* Set fields. */
961  ns_hdr = (struct ns_header *)p->payload;
962 
963  ns_hdr->type = ICMP6_TYPE_NS;
964  ns_hdr->code = 0;
965  ns_hdr->chksum = 0;
966  ns_hdr->reserved = 0;
967  ip6_addr_set(&(ns_hdr->target_address), target_addr);
968 
969  if (lladdr_opt_len != 0) {
970  struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
971  lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
972  lladdr_opt->length = (u8_t)lladdr_opt_len;
973  SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
974  }
975 
976  /* Generate the solicited node address for the target address. */
977  if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
978  ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
979  target_addr = &multicast_address;
980  }
981 
982 #if CHECKSUM_GEN_ICMP6
983  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
984  ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
985  target_addr);
986  }
987 #endif /* CHECKSUM_GEN_ICMP6 */
988 
989  /* Send the packet out. */
990  ND6_STATS_INC(nd6.xmit);
991  ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr,
992  LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
993  pbuf_free(p);
994 }
995 
1003 static void
1004 nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
1005 {
1006  struct na_header *na_hdr;
1007  struct lladdr_option *lladdr_opt;
1008  struct pbuf *p;
1009  const ip6_addr_t *src_addr;
1010  const ip6_addr_t *dest_addr;
1011  u16_t lladdr_opt_len;
1012 
1013  /* Use link-local address as source address. */
1014  /* src_addr = netif_ip6_addr(netif, 0); */
1015  /* Use target address as source address. */
1016  src_addr = target_addr;
1017 
1018  /* Allocate a packet. */
1019  lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
1020  p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + (lladdr_opt_len << 3), PBUF_RAM);
1021  if (p == NULL) {
1022  ND6_STATS_INC(nd6.memerr);
1023  return;
1024  }
1025 
1026  /* Set fields. */
1027  na_hdr = (struct na_header *)p->payload;
1028  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
1029 
1030  na_hdr->type = ICMP6_TYPE_NA;
1031  na_hdr->code = 0;
1032  na_hdr->chksum = 0;
1033  na_hdr->flags = flags & 0xf0;
1034  na_hdr->reserved[0] = 0;
1035  na_hdr->reserved[1] = 0;
1036  na_hdr->reserved[2] = 0;
1037  ip6_addr_set(&(na_hdr->target_address), target_addr);
1038 
1039  lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR;
1040  lladdr_opt->length = (u8_t)lladdr_opt_len;
1041  SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
1042 
1043  /* Generate the solicited node address for the target address. */
1044  if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
1045  ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
1046  dest_addr = &multicast_address;
1047  } else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
1048  ip6_addr_set_allnodes_linklocal(&multicast_address);
1049  dest_addr = &multicast_address;
1050  } else {
1051  dest_addr = ip6_current_src_addr();
1052  }
1053 
1054 #if CHECKSUM_GEN_ICMP6
1055  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
1056  na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
1057  dest_addr);
1058  }
1059 #endif /* CHECKSUM_GEN_ICMP6 */
1060 
1061  /* Send the packet out. */
1062  ND6_STATS_INC(nd6.xmit);
1063  ip6_output_if(p, src_addr, dest_addr,
1064  LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
1065  pbuf_free(p);
1066 }
1067 
1068 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
1069 
1074 static err_t
1075 nd6_send_rs(struct netif *netif)
1076 {
1077  struct rs_header *rs_hdr;
1078  struct lladdr_option *lladdr_opt;
1079  struct pbuf *p;
1080  const ip6_addr_t *src_addr;
1081  err_t err;
1082  u16_t lladdr_opt_len = 0;
1083 
1084  /* Link-local source address, or unspecified address? */
1085  if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
1086  src_addr = netif_ip6_addr(netif, 0);
1087  } else {
1088  src_addr = IP6_ADDR_ANY6;
1089  }
1090 
1091  /* Generate the all routers target address. */
1092  ip6_addr_set_allrouters_linklocal(&multicast_address);
1093 
1094  /* Allocate a packet. */
1095  if (src_addr != IP6_ADDR_ANY6) {
1096  lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
1097  }
1098  p = pbuf_alloc(PBUF_IP, sizeof(struct rs_header) + (lladdr_opt_len << 3), PBUF_RAM);
1099  if (p == NULL) {
1100  ND6_STATS_INC(nd6.memerr);
1101  return ERR_BUF;
1102  }
1103 
1104  /* Set fields. */
1105  rs_hdr = (struct rs_header *)p->payload;
1106 
1107  rs_hdr->type = ICMP6_TYPE_RS;
1108  rs_hdr->code = 0;
1109  rs_hdr->chksum = 0;
1110  rs_hdr->reserved = 0;
1111 
1112  if (src_addr != IP6_ADDR_ANY6) {
1113  /* Include our hw address. */
1114  lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
1115  lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
1116  lladdr_opt->length = (u8_t)lladdr_opt_len;
1117  SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
1118  }
1119 
1120 #if CHECKSUM_GEN_ICMP6
1121  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
1122  rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
1123  &multicast_address);
1124  }
1125 #endif /* CHECKSUM_GEN_ICMP6 */
1126 
1127  /* Send the packet out. */
1128  ND6_STATS_INC(nd6.xmit);
1129 
1130  err = ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address,
1131  LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
1132  pbuf_free(p);
1133 
1134  return err;
1135 }
1136 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
1137 
1145 static s8_t
1146 nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr)
1147 {
1148  s8_t i;
1149  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1150  if (ip6_addr_cmp(ip6addr, &(neighbor_cache[i].next_hop_address))) {
1151  return i;
1152  }
1153  }
1154  return -1;
1155 }
1156 
1166 static s8_t
1167 nd6_new_neighbor_cache_entry(void)
1168 {
1169  s8_t i;
1170  s8_t j;
1171  u32_t time;
1172 
1173 
1174  /* First, try to find an empty entry. */
1175  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1176  if (neighbor_cache[i].state == ND6_NO_ENTRY) {
1177  return i;
1178  }
1179  }
1180 
1181  /* We need to recycle an entry. in general, do not recycle if it is a router. */
1182 
1183  /* Next, try to find a Stale entry. */
1184  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1185  if ((neighbor_cache[i].state == ND6_STALE) &&
1186  (!neighbor_cache[i].isrouter)) {
1187  nd6_free_neighbor_cache_entry(i);
1188  return i;
1189  }
1190  }
1191 
1192  /* Next, try to find a Probe entry. */
1193  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1194  if ((neighbor_cache[i].state == ND6_PROBE) &&
1195  (!neighbor_cache[i].isrouter)) {
1196  nd6_free_neighbor_cache_entry(i);
1197  return i;
1198  }
1199  }
1200 
1201  /* Next, try to find a Delayed entry. */
1202  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1203  if ((neighbor_cache[i].state == ND6_DELAY) &&
1204  (!neighbor_cache[i].isrouter)) {
1205  nd6_free_neighbor_cache_entry(i);
1206  return i;
1207  }
1208  }
1209 
1210  /* Next, try to find the oldest reachable entry. */
1211  time = 0xfffffffful;
1212  j = -1;
1213  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1214  if ((neighbor_cache[i].state == ND6_REACHABLE) &&
1215  (!neighbor_cache[i].isrouter)) {
1216  if (neighbor_cache[i].counter.reachable_time < time) {
1217  j = i;
1218  time = neighbor_cache[i].counter.reachable_time;
1219  }
1220  }
1221  }
1222  if (j >= 0) {
1223  nd6_free_neighbor_cache_entry(j);
1224  return j;
1225  }
1226 
1227  /* Next, find oldest incomplete entry without queued packets. */
1228  time = 0;
1229  j = -1;
1230  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1231  if (
1232  (neighbor_cache[i].q == NULL) &&
1233  (neighbor_cache[i].state == ND6_INCOMPLETE) &&
1234  (!neighbor_cache[i].isrouter)) {
1235  if (neighbor_cache[i].counter.probes_sent >= time) {
1236  j = i;
1237  time = neighbor_cache[i].counter.probes_sent;
1238  }
1239  }
1240  }
1241  if (j >= 0) {
1242  nd6_free_neighbor_cache_entry(j);
1243  return j;
1244  }
1245 
1246  /* Next, find oldest incomplete entry with queued packets. */
1247  time = 0;
1248  j = -1;
1249  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
1250  if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
1251  (!neighbor_cache[i].isrouter)) {
1252  if (neighbor_cache[i].counter.probes_sent >= time) {
1253  j = i;
1254  time = neighbor_cache[i].counter.probes_sent;
1255  }
1256  }
1257  }
1258  if (j >= 0) {
1259  nd6_free_neighbor_cache_entry(j);
1260  return j;
1261  }
1262 
1263  /* No more entries to try. */
1264  return -1;
1265 }
1266 
1273 static void
1274 nd6_free_neighbor_cache_entry(s8_t i)
1275 {
1276  if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1277  return;
1278  }
1279  if (neighbor_cache[i].isrouter) {
1280  /* isrouter needs to be cleared before deleting a neighbor cache entry */
1281  return;
1282  }
1283 
1284  /* Free any queued packets. */
1285  if (neighbor_cache[i].q != NULL) {
1286  nd6_free_q(neighbor_cache[i].q);
1287  neighbor_cache[i].q = NULL;
1288  }
1289 
1290  neighbor_cache[i].state = ND6_NO_ENTRY;
1291  neighbor_cache[i].isrouter = 0;
1292  neighbor_cache[i].netif = NULL;
1293  neighbor_cache[i].counter.reachable_time = 0;
1294  ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address));
1295 }
1296 
1304 static s8_t
1305 nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr)
1306 {
1307  s8_t i;
1308  for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1309  if (ip6_addr_cmp(ip6addr, &(destination_cache[i].destination_addr))) {
1310  return i;
1311  }
1312  }
1313  return -1;
1314 }
1315 
1323 static s8_t
1324 nd6_new_destination_cache_entry(void)
1325 {
1326  s8_t i, j;
1327  u32_t age;
1328 
1329  /* Find an empty entry. */
1330  for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1331  if (ip6_addr_isany(&(destination_cache[i].destination_addr))) {
1332  return i;
1333  }
1334  }
1335 
1336  /* Find oldest entry. */
1337  age = 0;
1338  j = LWIP_ND6_NUM_DESTINATIONS - 1;
1339  for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1340  if (destination_cache[i].age > age) {
1341  j = i;
1342  }
1343  }
1344 
1345  return j;
1346 }
1347 
1354 void
1355 nd6_clear_destination_cache(void)
1356 {
1357  int i;
1358 
1359  for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
1360  ip6_addr_set_any(&destination_cache[i].destination_addr);
1361  }
1362 }
1363 
1370 static s8_t
1371 nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif)
1372 {
1373  s8_t i;
1374  for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
1375  if ((prefix_list[i].netif == netif) &&
1376  (prefix_list[i].invalidation_timer > 0) &&
1377  ip6_addr_netcmp(ip6addr, &(prefix_list[i].prefix))) {
1378  return 1;
1379  }
1380  }
1381  /* Check to see if address prefix matches a (manually?) configured address. */
1382  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1383  if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
1384  ip6_addr_netcmp(ip6addr, netif_ip6_addr(netif, i))) {
1385  return 1;
1386  }
1387  }
1388  return 0;
1389 }
1390 
1399 static s8_t
1400 nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif)
1401 {
1402  s8_t i;
1403  /* last_router is used for round-robin router selection (as recommended
1404  * in RFC). This is more robust in case one router is not reachable,
1405  * we are not stuck trying to resolve it. */
1406  static s8_t last_router;
1407  (void)ip6addr; /* @todo match preferred routes!! (must implement ND6_OPTION_TYPE_ROUTE_INFO) */
1408 
1409  /* @todo: implement default router preference */
1410 
1411  /* Look for reachable routers. */
1412  for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1413  if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1414  last_router = 0;
1415  }
1416  if ((default_router_list[i].neighbor_entry != NULL) &&
1417  (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1418  (default_router_list[i].invalidation_timer > 0) &&
1419  (default_router_list[i].neighbor_entry->state == ND6_REACHABLE)) {
1420  return i;
1421  }
1422  }
1423 
1424  /* Look for router in other reachability states, but still valid according to timer. */
1425  for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1426  if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1427  last_router = 0;
1428  }
1429  if ((default_router_list[i].neighbor_entry != NULL) &&
1430  (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1431  (default_router_list[i].invalidation_timer > 0)) {
1432  return i;
1433  }
1434  }
1435 
1436  /* Look for any router for which we have any information at all. */
1437  for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1438  if (++last_router >= LWIP_ND6_NUM_ROUTERS) {
1439  last_router = 0;
1440  }
1441  if (default_router_list[i].neighbor_entry != NULL &&
1442  (netif != NULL ? netif == default_router_list[i].neighbor_entry->netif : 1)) {
1443  return i;
1444  }
1445  }
1446 
1447  /* no suitable router found. */
1448  return -1;
1449 }
1450 
1460 struct netif *
1461 nd6_find_route(const ip6_addr_t *ip6addr)
1462 {
1463  s8_t i;
1464 
1465  i = nd6_select_router(ip6addr, NULL);
1466  if (i >= 0) {
1467  if (default_router_list[i].neighbor_entry != NULL) {
1468  return default_router_list[i].neighbor_entry->netif; /* may be NULL */
1469  }
1470  }
1471 
1472  return NULL;
1473 }
1474 
1482 static s8_t
1483 nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif)
1484 {
1485  s8_t i;
1486 
1487  /* Look for router. */
1488  for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
1489  if ((default_router_list[i].neighbor_entry != NULL) &&
1490  ((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
1491  ip6_addr_cmp(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) {
1492  return i;
1493  }
1494  }
1495 
1496  /* router not found. */
1497  return -1;
1498 }
1499 
1507 static s8_t
1508 nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif)
1509 {
1510  s8_t router_index;
1511  s8_t free_router_index;
1512  s8_t neighbor_index;
1513 
1514  /* Do we have a neighbor entry for this router? */
1515  neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
1516  if (neighbor_index < 0) {
1517  /* Create a neighbor entry for this router. */
1518  neighbor_index = nd6_new_neighbor_cache_entry();
1519  if (neighbor_index < 0) {
1520  /* Could not create neighbor entry for this router. */
1521  return -1;
1522  }
1523  ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
1524  neighbor_cache[neighbor_index].netif = netif;
1525  neighbor_cache[neighbor_index].q = NULL;
1526  neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
1527  neighbor_cache[neighbor_index].counter.probes_sent = 1;
1528  nd6_send_neighbor_cache_probe(&neighbor_cache[neighbor_index], ND6_SEND_FLAG_MULTICAST_DEST);
1529  }
1530 
1531  /* Mark neighbor as router. */
1532  neighbor_cache[neighbor_index].isrouter = 1;
1533 
1534  /* Look for empty entry. */
1535  free_router_index = LWIP_ND6_NUM_ROUTERS;
1536  for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) {
1537  /* check if router already exists (this is a special case for 2 netifs on the same subnet
1538  - e.g. wifi and cable) */
1539  if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){
1540  return router_index;
1541  }
1542  if (default_router_list[router_index].neighbor_entry == NULL) {
1543  /* remember lowest free index to create a new entry */
1544  free_router_index = router_index;
1545  }
1546  }
1547  if (free_router_index < LWIP_ND6_NUM_ROUTERS) {
1548  default_router_list[free_router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
1549  return free_router_index;
1550  }
1551 
1552  /* Could not create a router entry. */
1553 
1554  /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
1555  neighbor_cache[neighbor_index].isrouter = 0;
1556 
1557  /* router not found. */
1558  return -1;
1559 }
1560 
1568 static s8_t
1569 nd6_get_onlink_prefix(ip6_addr_t *prefix, struct netif *netif)
1570 {
1571  s8_t i;
1572 
1573  /* Look for prefix in list. */
1574  for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1575  if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) &&
1576  (prefix_list[i].netif == netif)) {
1577  return i;
1578  }
1579  }
1580 
1581  /* Entry not available. */
1582  return -1;
1583 }
1584 
1592 static s8_t
1593 nd6_new_onlink_prefix(ip6_addr_t *prefix, struct netif *netif)
1594 {
1595  s8_t i;
1596 
1597  /* Create new entry. */
1598  for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
1599  if ((prefix_list[i].netif == NULL) ||
1600  (prefix_list[i].invalidation_timer == 0)) {
1601  /* Found empty prefix entry. */
1602  prefix_list[i].netif = netif;
1603  ip6_addr_set(&(prefix_list[i].prefix), prefix);
1604 #if LWIP_IPV6_AUTOCONFIG
1605  prefix_list[i].flags = 0;
1606 #endif /* LWIP_IPV6_AUTOCONFIG */
1607  return i;
1608  }
1609  }
1610 
1611  /* Entry not available. */
1612  return -1;
1613 }
1614 
1627 static s8_t
1628 nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif)
1629 {
1630 #ifdef LWIP_HOOK_ND6_GET_GW
1631  const ip6_addr_t *next_hop_addr;
1632 #endif /* LWIP_HOOK_ND6_GET_GW */
1633  s8_t i;
1634 
1635 #if LWIP_NETIF_HWADDRHINT
1636  if (netif->addr_hint != NULL) {
1637  /* per-pcb cached entry was given */
1638  u8_t addr_hint = *(netif->addr_hint);
1639  if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
1640  nd6_cached_destination_index = addr_hint;
1641  }
1642  }
1643 #endif /* LWIP_NETIF_HWADDRHINT */
1644 
1645  /* Look for ip6addr in destination cache. */
1646  if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
1647  /* the cached entry index is the right one! */
1648  /* do nothing. */
1649  ND6_STATS_INC(nd6.cachehit);
1650  } else {
1651  /* Search destination cache. */
1652  i = nd6_find_destination_cache_entry(ip6addr);
1653  if (i >= 0) {
1654  /* found destination entry. make it our new cached index. */
1655  nd6_cached_destination_index = i;
1656  } else {
1657  /* Not found. Create a new destination entry. */
1658  i = nd6_new_destination_cache_entry();
1659  if (i >= 0) {
1660  /* got new destination entry. make it our new cached index. */
1661  nd6_cached_destination_index = i;
1662  } else {
1663  /* Could not create a destination cache entry. */
1664  return ERR_MEM;
1665  }
1666 
1667  /* Copy dest address to destination cache. */
1668  ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr);
1669 
1670  /* Now find the next hop. is it a neighbor? */
1671  if (ip6_addr_islinklocal(ip6addr) ||
1672  nd6_is_prefix_in_netif(ip6addr, netif)) {
1673  /* Destination in local link. */
1674  destination_cache[nd6_cached_destination_index].pmtu = netif->mtu;
1675  ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, destination_cache[nd6_cached_destination_index].destination_addr);
1676 #ifdef LWIP_HOOK_ND6_GET_GW
1677  } else if ((next_hop_addr = LWIP_HOOK_ND6_GET_GW(netif, ip6addr)) != NULL) {
1678  /* Next hop for destination provided by hook function. */
1679  destination_cache[nd6_cached_destination_index].pmtu = netif->mtu;
1680  ip6_addr_set(&destination_cache[nd6_cached_destination_index].next_hop_addr, next_hop_addr);
1681 #endif /* LWIP_HOOK_ND6_GET_GW */
1682  } else {
1683  /* We need to select a router. */
1684  i = nd6_select_router(ip6addr, netif);
1685  if (i < 0) {
1686  /* No router found. */
1687  ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr));
1688  return ERR_RTE;
1689  }
1690  destination_cache[nd6_cached_destination_index].pmtu = netif->mtu; /* Start with netif mtu, correct through ICMPv6 if necessary */
1691  ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address);
1692  }
1693  }
1694  }
1695 
1696 #if LWIP_NETIF_HWADDRHINT
1697  if (netif->addr_hint != NULL) {
1698  /* per-pcb cached entry was given */
1699  *(netif->addr_hint) = nd6_cached_destination_index;
1700  }
1701 #endif /* LWIP_NETIF_HWADDRHINT */
1702 
1703  /* Look in neighbor cache for the next-hop address. */
1704  if (ip6_addr_cmp(&(destination_cache[nd6_cached_destination_index].next_hop_addr),
1705  &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
1706  /* Cache hit. */
1707  /* Do nothing. */
1708  ND6_STATS_INC(nd6.cachehit);
1709  } else {
1710  i = nd6_find_neighbor_cache_entry(&(destination_cache[nd6_cached_destination_index].next_hop_addr));
1711  if (i >= 0) {
1712  /* Found a matching record, make it new cached entry. */
1713  nd6_cached_neighbor_index = i;
1714  } else {
1715  /* Neighbor not in cache. Make a new entry. */
1716  i = nd6_new_neighbor_cache_entry();
1717  if (i >= 0) {
1718  /* got new neighbor entry. make it our new cached index. */
1719  nd6_cached_neighbor_index = i;
1720  } else {
1721  /* Could not create a neighbor cache entry. */
1722  return ERR_MEM;
1723  }
1724 
1725  /* Initialize fields. */
1726  ip6_addr_copy(neighbor_cache[i].next_hop_address,
1727  destination_cache[nd6_cached_destination_index].next_hop_addr);
1728  neighbor_cache[i].isrouter = 0;
1729  neighbor_cache[i].netif = netif;
1730  neighbor_cache[i].state = ND6_INCOMPLETE;
1731  neighbor_cache[i].counter.probes_sent = 1;
1732  nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
1733  }
1734  }
1735 
1736  /* Reset this destination's age. */
1737  destination_cache[nd6_cached_destination_index].age = 0;
1738 
1739  return nd6_cached_neighbor_index;
1740 }
1741 
1749 static err_t
1750 nd6_queue_packet(s8_t neighbor_index, struct pbuf *q)
1751 {
1752  err_t result = ERR_MEM;
1753  struct pbuf *p;
1754  int copy_needed = 0;
1755 #if LWIP_ND6_QUEUEING
1756  struct nd6_q_entry *new_entry, *r;
1757 #endif /* LWIP_ND6_QUEUEING */
1758 
1759  if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
1760  return ERR_ARG;
1761  }
1762 
1763  /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
1764  * to copy the whole queue into a new PBUF_RAM (see bug #11400)
1765  * PBUF_ROMs can be left as they are, since ROM must not get changed. */
1766  p = q;
1767  while (p) {
1768  if (p->type != PBUF_ROM) {
1769  copy_needed = 1;
1770  break;
1771  }
1772  p = p->next;
1773  }
1774  if (copy_needed) {
1775  /* copy the whole packet into new pbufs */
1777  while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1778  /* Free oldest packet (as per RFC recommendation) */
1779 #if LWIP_ND6_QUEUEING
1780  r = neighbor_cache[neighbor_index].q;
1781  neighbor_cache[neighbor_index].q = r->next;
1782  r->next = NULL;
1783  nd6_free_q(r);
1784 #else /* LWIP_ND6_QUEUEING */
1785  pbuf_free(neighbor_cache[neighbor_index].q);
1786  neighbor_cache[neighbor_index].q = NULL;
1787 #endif /* LWIP_ND6_QUEUEING */
1789  }
1790  if (p != NULL) {
1791  if (pbuf_copy(p, q) != ERR_OK) {
1792  pbuf_free(p);
1793  p = NULL;
1794  }
1795  }
1796  } else {
1797  /* referencing the old pbuf is enough */
1798  p = q;
1799  pbuf_ref(p);
1800  }
1801  /* packet was copied/ref'd? */
1802  if (p != NULL) {
1803  /* queue packet ... */
1804 #if LWIP_ND6_QUEUEING
1805  /* allocate a new nd6 queue entry */
1806  new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1807  if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
1808  /* Free oldest packet (as per RFC recommendation) */
1809  r = neighbor_cache[neighbor_index].q;
1810  neighbor_cache[neighbor_index].q = r->next;
1811  r->next = NULL;
1812  nd6_free_q(r);
1813  new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
1814  }
1815  if (new_entry != NULL) {
1816  new_entry->next = NULL;
1817  new_entry->p = p;
1818  if (neighbor_cache[neighbor_index].q != NULL) {
1819  /* queue was already existent, append the new entry to the end */
1820  r = neighbor_cache[neighbor_index].q;
1821  while (r->next != NULL) {
1822  r = r->next;
1823  }
1824  r->next = new_entry;
1825  } else {
1826  /* queue did not exist, first item in queue */
1827  neighbor_cache[neighbor_index].q = new_entry;
1828  }
1829  LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
1830  result = ERR_OK;
1831  } else {
1832  /* the pool MEMP_ND6_QUEUE is empty */
1833  pbuf_free(p);
1834  LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p));
1835  /* { result == ERR_MEM } through initialization */
1836  }
1837 #else /* LWIP_ND6_QUEUEING */
1838  /* Queue a single packet. If an older packet is already queued, free it as per RFC. */
1839  if (neighbor_cache[neighbor_index].q != NULL) {
1840  pbuf_free(neighbor_cache[neighbor_index].q);
1841  }
1842  neighbor_cache[neighbor_index].q = p;
1843  LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
1844  result = ERR_OK;
1845 #endif /* LWIP_ND6_QUEUEING */
1846  } else {
1847  LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
1848  /* { result == ERR_MEM } through initialization */
1849  }
1850 
1851  return result;
1852 }
1853 
1854 #if LWIP_ND6_QUEUEING
1855 
1860 static void
1861 nd6_free_q(struct nd6_q_entry *q)
1862 {
1863  struct nd6_q_entry *r;
1864  LWIP_ASSERT("q != NULL", q != NULL);
1865  LWIP_ASSERT("q->p != NULL", q->p != NULL);
1866  while (q) {
1867  r = q;
1868  q = q->next;
1869  LWIP_ASSERT("r->p != NULL", (r->p != NULL));
1870  pbuf_free(r->p);
1871  memp_free(MEMP_ND6_QUEUE, r);
1872  }
1873 }
1874 #endif /* LWIP_ND6_QUEUEING */
1875 
1881 static void
1882 nd6_send_q(s8_t i)
1883 {
1884  struct ip6_hdr *ip6hdr;
1885  ip6_addr_t dest;
1886 #if LWIP_ND6_QUEUEING
1887  struct nd6_q_entry *q;
1888 #endif /* LWIP_ND6_QUEUEING */
1889 
1890  if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
1891  return;
1892  }
1893 
1894 #if LWIP_ND6_QUEUEING
1895  while (neighbor_cache[i].q != NULL) {
1896  /* remember first in queue */
1897  q = neighbor_cache[i].q;
1898  /* pop first item off the queue */
1899  neighbor_cache[i].q = q->next;
1900  /* Get ipv6 header. */
1901  ip6hdr = (struct ip6_hdr *)(q->p->payload);
1902  /* Create an aligned copy. */
1903  ip6_addr_set(&dest, &(ip6hdr->dest));
1904  /* send the queued IPv6 packet */
1905  (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, &dest);
1906  /* free the queued IP packet */
1907  pbuf_free(q->p);
1908  /* now queue entry can be freed */
1909  memp_free(MEMP_ND6_QUEUE, q);
1910  }
1911 #else /* LWIP_ND6_QUEUEING */
1912  if (neighbor_cache[i].q != NULL) {
1913  /* Get ipv6 header. */
1914  ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload);
1915  /* Create an aligned copy. */
1916  ip6_addr_set(&dest, &(ip6hdr->dest));
1917  /* send the queued IPv6 packet */
1918  (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, neighbor_cache[i].q, &dest);
1919  /* free the queued IP packet */
1920  pbuf_free(neighbor_cache[i].q);
1921  neighbor_cache[i].q = NULL;
1922  }
1923 #endif /* LWIP_ND6_QUEUEING */
1924 }
1925 
1948 err_t
1949 nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr, const u8_t **hwaddrp)
1950 {
1951  s8_t i;
1952 
1953  /* Get next hop record. */
1954  i = nd6_get_next_hop_entry(ip6addr, netif);
1955  if (i < 0) {
1956  /* failed to get a next hop neighbor record. */
1957  return i;
1958  }
1959 
1960  /* Now that we have a destination record, send or queue the packet. */
1961  if (neighbor_cache[i].state == ND6_STALE) {
1962  /* Switch to delay state. */
1963  neighbor_cache[i].state = ND6_DELAY;
1964  neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
1965  }
1966  /* @todo should we send or queue if PROBE? send for now, to let unicast NS pass. */
1967  if ((neighbor_cache[i].state == ND6_REACHABLE) ||
1968  (neighbor_cache[i].state == ND6_DELAY) ||
1969  (neighbor_cache[i].state == ND6_PROBE)) {
1970 
1971  /* Tell the caller to send out the packet now. */
1972  *hwaddrp = neighbor_cache[i].lladdr;
1973  return ERR_OK;
1974  }
1975 
1976  /* We should queue packet on this interface. */
1977  *hwaddrp = NULL;
1978  return nd6_queue_packet(i, q);
1979 }
1980 
1981 
1989 u16_t
1990 nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif)
1991 {
1992  s8_t i;
1993 
1994  i = nd6_find_destination_cache_entry(ip6addr);
1995  if (i >= 0) {
1996  if (destination_cache[i].pmtu > 0) {
1997  return destination_cache[i].pmtu;
1998  }
1999  }
2000 
2001  if (netif != NULL) {
2002  return netif->mtu;
2003  }
2004 
2005  return 1280; /* Minimum MTU */
2006 }
2007 
2008 
2009 #if LWIP_ND6_TCP_REACHABILITY_HINTS
2010 
2019 void
2020 nd6_reachability_hint(const ip6_addr_t *ip6addr)
2021 {
2022  s8_t i;
2023 
2024  /* Find destination in cache. */
2025  if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
2026  i = nd6_cached_destination_index;
2027  ND6_STATS_INC(nd6.cachehit);
2028  } else {
2029  i = nd6_find_destination_cache_entry(ip6addr);
2030  }
2031  if (i < 0) {
2032  return;
2033  }
2034 
2035  /* Find next hop neighbor in cache. */
2036  if (ip6_addr_cmp(&(destination_cache[i].next_hop_addr), &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) {
2037  i = nd6_cached_neighbor_index;
2038  ND6_STATS_INC(nd6.cachehit);
2039  } else {
2040  i = nd6_find_neighbor_cache_entry(&(destination_cache[i].next_hop_addr));
2041  }
2042  if (i < 0) {
2043  return;
2044  }
2045 
2046  /* For safety: don't set as reachable if we don't have a LL address yet. Misuse protection. */
2047  if (neighbor_cache[i].state == ND6_INCOMPLETE || neighbor_cache[i].state == ND6_NO_ENTRY) {
2048  return;
2049  }
2050 
2051  /* Set reachability state. */
2052  neighbor_cache[i].state = ND6_REACHABLE;
2053  neighbor_cache[i].counter.reachable_time = reachable_time;
2054 }
2055 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
2056 
2062 void
2063 nd6_cleanup_netif(struct netif *netif)
2064 {
2065  u8_t i;
2066  s8_t router_index;
2067  for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
2068  if (prefix_list[i].netif == netif) {
2069  prefix_list[i].netif = NULL;
2070  prefix_list[i].flags = 0;
2071  }
2072  }
2073  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
2074  if (neighbor_cache[i].netif == netif) {
2075  for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) {
2076  if (default_router_list[router_index].neighbor_entry == &neighbor_cache[i]) {
2077  default_router_list[router_index].neighbor_entry = NULL;
2078  default_router_list[router_index].flags = 0;
2079  }
2080  }
2081  neighbor_cache[i].isrouter = 0;
2082  nd6_free_neighbor_cache_entry(i);
2083  }
2084  }
2085 }
2086 
2087 #if LWIP_IPV6_MLD
2088 
2096 void
2097 nd6_adjust_mld_membership(struct netif *netif, s8_t addr_idx, u8_t new_state)
2098 {
2099  u8_t old_state, old_member, new_member;
2100 
2101  old_state = netif_ip6_addr_state(netif, addr_idx);
2102 
2103  /* Determine whether we were, and should be, a member of the solicited-node
2104  * multicast group for this address. For tentative addresses, the group is
2105  * not joined until the address enters the TENTATIVE_1 (or VALID) state. */
2106  old_member = (old_state != IP6_ADDR_INVALID && old_state != IP6_ADDR_TENTATIVE);
2107  new_member = (new_state != IP6_ADDR_INVALID && new_state != IP6_ADDR_TENTATIVE);
2108 
2109  if (old_member != new_member) {
2110  ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, addr_idx)->addr[3]);
2111 
2112  if (new_member) {
2113  mld6_joingroup_netif(netif, &multicast_address);
2114  } else {
2115  mld6_leavegroup_netif(netif, &multicast_address);
2116  }
2117  }
2118 }
2119 #endif /* LWIP_IPV6_MLD */
2120 
2121 #endif /* LWIP_IPV6 */
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:322
#define NETIF_FLAG_UP
Definition: netif.h:97
u16_t tot_len
Definition: pbuf.h:175
struct netif * netif_list
Definition: netif.c:123
struct pbuf * next
Definition: pbuf.h:163
u16_t len
Definition: pbuf.h:178
Definition: err.h:86
Definition: pbuf.h:131
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:839
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition: pbuf.c:967
#define LWIP_ND6_NUM_PREFIXES
Definition: opt.h:2323
u8_t hwaddr_len
Definition: netif.h:320
Definition: err.h:84
#define DNS_MAX_SERVERS
Definition: opt.h:1048
#define LWIP_ND6_MAX_MULTICAST_SOLICIT
Definition: opt.h:2338
#define ND6_OPTION_TYPE_MTU
Definition: prot/nd6.h:234
#define LWIP_ND6_DELAY_FIRST_PROBE_TIME
Definition: opt.h:2383
#define LWIP_IPV6_DUP_DETECT_ATTEMPTS
Definition: opt.h:2222
struct netif * next
Definition: netif.h:246
Definition: err.h:115
Definition: pbuf.h:161
u8_t flags
Definition: netif.h:324
Definition: netif.h:244
#define ND6_OPTION_TYPE_ROUTE_INFO
Definition: prot/nd6.h:251
s8_t err_t
Definition: err.h:76
u16_t mtu
Definition: netif.h:318
#define LWIP_DEBUGF(debug, message)
#define LWIP_ND6_REACHABLE_TIME
Definition: opt.h:2368
#define LWIP_ND6_NUM_DESTINATIONS
Definition: opt.h:2316
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:267
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1034
#define LWIP_ND6_NUM_NEIGHBORS
Definition: opt.h:2309
Definition: pbuf.h:127
void * state
Definition: netif.h:298
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166
Definition: pbuf.h:99
#define LWIP_ICMP6_HL
Definition: opt.h:2252
#define ND6_OPTION_TYPE_SOURCE_LLADDR
Definition: prot/nd6.h:173
#define ND6_OPTION_TYPE_PREFIX_INFO
Definition: prot/nd6.h:190
Definition: err.h:90
#define LWIP_IPV6_NUM_ADDRESSES
Definition: opt.h:2179
u8_t type
Definition: pbuf.h:181
void * memp_malloc(memp_t type)
Definition: memp.c:404
#define LWIP_ND6_NUM_ROUTERS
Definition: opt.h:2330
#define LWIP_IPV6_SEND_ROUTER_SOLICIT
Definition: opt.h:2208
#define LWIP_ND6_RETRANS_TIMER
Definition: opt.h:2375