Contiki-NG
Loading...
Searching...
No Matches
uip-ds6.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31/**
32 * \addtogroup uip
33 * @{
34 */
35
36/**
37 * \file
38 * IPv6 data structure manipulation.
39 * Comprises part of the Neighbor discovery (RFC 4861)
40 * and auto configuration (RFC 4862) state machines.
41 * \author Mathilde Durvy <mdurvy@cisco.com>
42 * \author Julien Abeille <jabeille@cisco.com>
43 */
44
45#include <string.h>
46#include <stdlib.h>
47#include <stddef.h>
48#include "lib/random.h"
49#include "net/ipv6/uip-nd6.h"
51#include "net/ipv6/uip-ds6.h"
53#include "net/ipv6/uip-packetqueue.h"
54
55/* Log configuration */
56#include "sys/log.h"
57#define LOG_MODULE "IPv6 DS"
58#define LOG_LEVEL LOG_LEVEL_IPV6
59
60struct etimer uip_ds6_timer_periodic; /**< Timer for maintenance of data structures */
61
62#if UIP_CONF_ROUTER
63struct stimer uip_ds6_timer_ra; /**< RA timer, to schedule RA sending */
64#if UIP_ND6_SEND_RA
65static uint8_t racount; /**< number of RA already sent */
66static uint16_t rand_time; /**< random time value for timers */
67#endif
68#else /* UIP_CONF_ROUTER */
69struct etimer uip_ds6_timer_rs; /**< RS timer, to schedule RS sending */
70static uint8_t rscount; /**< number of rs already sent */
71#endif /* UIP_CONF_ROUTER */
72
73/** \name "DS6" Data structures */
74/** @{ */
75uip_ds6_netif_t uip_ds6_if; /**< The single interface */
76uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]; /**< Prefix list */
77
78/* Used by Cooja to enable extraction of addresses from memory.*/
79uint8_t uip_ds6_addr_size;
80uint8_t uip_ds6_netif_addr_list_offset;
81
82/** @} */
83
84/* "full" (as opposed to pointer) ip address used in this file, */
85static uip_ipaddr_t loc_fipaddr;
86
87/* Pointers used in this file */
88static uip_ds6_addr_t *locaddr;
89static uip_ds6_maddr_t *locmaddr;
90#if UIP_DS6_AADDR_NB
91static uip_ds6_aaddr_t *locaaddr;
92#endif /* UIP_DS6_AADDR_NB */
93static uip_ds6_prefix_t *locprefix;
94#if (UIP_LLADDR_LEN == 2)
95static const uint8_t iid_prefix[] = { 0x00, 0x00 , 0x00 , 0xff , 0xfe , 0x00 };
96#endif /* (UIP_LLADDR_LEN == 2) */
97
98/* The default prefix */
99static uip_ip6addr_t default_prefix = {
100 .u16 = { 0, 0, 0, 0, 0, 0, 0, 0 }
101};
102/*---------------------------------------------------------------------------*/
103const uip_ip6addr_t *
105{
106 return &default_prefix;
107}
108/*---------------------------------------------------------------------------*/
109void
110uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix)
111{
112 uip_ip6addr_copy(&default_prefix, prefix);
113}
114/*---------------------------------------------------------------------------*/
115void
117{
118 if(uip_is_addr_unspecified(&default_prefix)) {
119 uip_ip6addr(&default_prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
120 }
121
122 uip_ds6_neighbors_init();
123 uip_ds6_route_init();
124
125 LOG_INFO("Init: %u neighbors\n", NBR_TABLE_MAX_NEIGHBORS);
126 LOG_INFO("%u default routers\n", UIP_DS6_DEFRT_NB);
127 LOG_INFO("%u prefixes\n", UIP_DS6_PREFIX_NB);
128 LOG_INFO("%u routes\n", UIP_DS6_ROUTE_NB);
129 LOG_INFO("%u unicast addresses\n", UIP_DS6_ADDR_NB);
130 LOG_INFO("%u multicast addresses\n", UIP_DS6_MADDR_NB);
131 LOG_INFO("%u anycast addresses\n", UIP_DS6_AADDR_NB);
132
133 memset(uip_ds6_prefix_list, 0, sizeof(uip_ds6_prefix_list));
134 memset(&uip_ds6_if, 0, sizeof(uip_ds6_if));
135 uip_ds6_addr_size = sizeof(struct uip_ds6_addr);
136 uip_ds6_netif_addr_list_offset = offsetof(struct uip_ds6_netif, addr_list);
137
138 /* Set interface parameters */
139 uip_ds6_if.link_mtu = UIP_LINK_MTU;
140 uip_ds6_if.cur_hop_limit = UIP_TTL;
141 uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
143 uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
145
146 /* Create link local address, prefix, multicast addresses, anycast addresses */
147 uip_create_linklocal_prefix(&loc_fipaddr);
148#if UIP_CONF_ROUTER
149 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
150#else /* UIP_CONF_ROUTER */
151 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
152#endif /* UIP_CONF_ROUTER */
153 uip_ds6_set_addr_iid(&loc_fipaddr, &uip_lladdr);
154 uip_ds6_addr_add(&loc_fipaddr, 0, ADDR_AUTOCONF);
155
157 uip_ds6_maddr_add(&loc_fipaddr);
158#if UIP_CONF_ROUTER
160 uip_ds6_maddr_add(&loc_fipaddr);
161#if UIP_ND6_SEND_RA
162 stimer_set(&uip_ds6_timer_ra, 2); /* wait to have a link local IP address */
163#endif /* UIP_ND6_SEND_RA */
164#else /* UIP_CONF_ROUTER */
167 CLOCK_SECOND));
168#endif /* UIP_CONF_ROUTER */
170
171 return;
172}
173
174
175/*---------------------------------------------------------------------------*/
176void
178{
179
180 /* Periodic processing on unicast addresses */
181 for(locaddr = uip_ds6_if.addr_list;
182 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
183 if(locaddr->isused) {
184 if((!locaddr->isinfinite) && (stimer_expired(&locaddr->vlifetime))) {
185 uip_ds6_addr_rm(locaddr);
186#if UIP_ND6_DEF_MAXDADNS > 0
187 } else if((locaddr->state == ADDR_TENTATIVE)
188 && (locaddr->dadnscount <= uip_ds6_if.maxdadns)
189 && (timer_expired(&locaddr->dadtimer))
190 && (uip_len == 0)) {
191 uip_ds6_dad(locaddr);
192#endif /* UIP_ND6_DEF_MAXDADNS > 0 */
193 }
194 }
195 }
196
197 /* Periodic processing on default routers */
198 uip_ds6_defrt_periodic();
199#if !UIP_CONF_ROUTER
200 /* Periodic processing on prefixes */
201 for(locprefix = uip_ds6_prefix_list;
202 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB;
203 locprefix++) {
204 if(locprefix->isused && !locprefix->isinfinite
205 && stimer_expired(&(locprefix->vlifetime))) {
206 uip_ds6_prefix_rm(locprefix);
207 }
208 }
209#endif /* !UIP_CONF_ROUTER */
210
211#if UIP_ND6_SEND_NS
213#endif /* UIP_ND6_SEND_NS */
214
215#if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
216 /* Periodic RA sending */
217 if(stimer_expired(&uip_ds6_timer_ra) && (uip_len == 0)) {
218 uip_ds6_send_ra_periodic();
219 }
220#endif /* UIP_CONF_ROUTER && UIP_ND6_SEND_RA */
222 return;
223}
224
225/*---------------------------------------------------------------------------*/
226uint8_t
228 uint16_t elementsize, uip_ipaddr_t *ipaddr,
229 uint8_t ipaddrlen, uip_ds6_element_t **out_element)
230{
231 uip_ds6_element_t *element;
232
233 if(list == NULL || ipaddr == NULL || out_element == NULL) {
234 return NOSPACE;
235 }
236
237 *out_element = NULL;
238
239 for(element = list;
240 element <
241 (uip_ds6_element_t *)((uint8_t *)list + (size * elementsize));
242 element = (uip_ds6_element_t *)((uint8_t *)element + elementsize)) {
243 if(element->isused) {
244 if(uip_ipaddr_prefixcmp(&element->ipaddr, ipaddr, ipaddrlen)) {
245 *out_element = element;
246 return FOUND;
247 }
248 } else {
249 *out_element = element;
250 }
251 }
252
253 return *out_element != NULL ? FREESPACE : NOSPACE;
254}
255
256/*---------------------------------------------------------------------------*/
257#if UIP_CONF_ROUTER
258/*---------------------------------------------------------------------------*/
260uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen,
261 uint8_t advertise, uint8_t flags, unsigned long vtime,
262 unsigned long ptime)
263{
265 ((uip_ds6_element_t *)uip_ds6_prefix_list, UIP_DS6_PREFIX_NB,
266 sizeof(uip_ds6_prefix_t), ipaddr, ipaddrlen,
267 (uip_ds6_element_t **)&locprefix) == FREESPACE) {
268 locprefix->isused = 1;
269 uip_ipaddr_copy(&locprefix->ipaddr, ipaddr);
270 locprefix->length = ipaddrlen;
271 locprefix->advertise = advertise;
272 locprefix->l_a_reserved = flags;
273 locprefix->vlifetime = vtime;
274 locprefix->plifetime = ptime;
275 LOG_INFO("Adding prefix ");
276 LOG_INFO_6ADDR(&locprefix->ipaddr);
277 LOG_INFO_(" len %u, flags %x, valid/preferred lifetime %lx/%lx\n",
278 ipaddrlen, flags, vtime, ptime);
279 return locprefix;
280 } else {
281 LOG_INFO("No more space in Prefix list\n");
282 }
283 return NULL;
284}
285
286
287#else /* UIP_CONF_ROUTER */
289uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen,
290 unsigned long interval)
291{
293 ((uip_ds6_element_t *)uip_ds6_prefix_list, UIP_DS6_PREFIX_NB,
294 sizeof(uip_ds6_prefix_t), ipaddr, ipaddrlen,
295 (uip_ds6_element_t **)&locprefix) == FREESPACE) {
296 locprefix->isused = 1;
297 uip_ipaddr_copy(&locprefix->ipaddr, ipaddr);
298 locprefix->length = ipaddrlen;
299 if(interval != 0) {
300 stimer_set(&(locprefix->vlifetime), interval);
301 locprefix->isinfinite = 0;
302 } else {
303 locprefix->isinfinite = 1;
304 }
305 LOG_INFO("Adding prefix ");
306 LOG_INFO_6ADDR(&locprefix->ipaddr);
307 LOG_INFO_(" len %u, valid lifetime %lu\n", ipaddrlen, interval);
308 return locprefix;
309 }
310 return NULL;
311}
312#endif /* UIP_CONF_ROUTER */
313
314/*---------------------------------------------------------------------------*/
315void
316uip_ds6_prefix_rm(uip_ds6_prefix_t *prefix)
317{
318 if(prefix != NULL) {
319 prefix->isused = 0;
320 }
321 return;
322}
323/*---------------------------------------------------------------------------*/
325uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen)
326{
328 UIP_DS6_PREFIX_NB, sizeof(uip_ds6_prefix_t),
329 ipaddr, ipaddrlen,
330 (uip_ds6_element_t **)&locprefix) == FOUND) {
331 return locprefix;
332 }
333 return NULL;
334}
335
336/*---------------------------------------------------------------------------*/
337uint8_t
338uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr)
339{
340 for(locprefix = uip_ds6_prefix_list;
341 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; locprefix++) {
342 if(locprefix->isused &&
343 uip_ipaddr_prefixcmp(&locprefix->ipaddr, ipaddr, locprefix->length)) {
344 return 1;
345 }
346 }
347 return 0;
348}
349
350/*---------------------------------------------------------------------------*/
352uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
353{
355 ((uip_ds6_element_t *)uip_ds6_if.addr_list, UIP_DS6_ADDR_NB,
356 sizeof(uip_ds6_addr_t), ipaddr, 128,
357 (uip_ds6_element_t **)&locaddr) == FREESPACE) {
358 locaddr->isused = 1;
359 uip_ipaddr_copy(&locaddr->ipaddr, ipaddr);
360 locaddr->type = type;
361 if(vlifetime == 0) {
362 locaddr->isinfinite = 1;
363 } else {
364 locaddr->isinfinite = 0;
365 stimer_set(&(locaddr->vlifetime), vlifetime);
366 }
367#if UIP_ND6_DEF_MAXDADNS > 0
368 locaddr->state = ADDR_TENTATIVE;
369 timer_set(&locaddr->dadtimer,
371 CLOCK_SECOND));
372 locaddr->dadnscount = 0;
373#else /* UIP_ND6_DEF_MAXDADNS > 0 */
374 locaddr->state = ADDR_PREFERRED;
375#endif /* UIP_ND6_DEF_MAXDADNS > 0 */
376 uip_create_solicited_node(ipaddr, &loc_fipaddr);
377 uip_ds6_maddr_add(&loc_fipaddr);
378 return locaddr;
379 }
380 return NULL;
381}
382
383/*---------------------------------------------------------------------------*/
384void
385uip_ds6_addr_rm(uip_ds6_addr_t *addr)
386{
387 if(addr != NULL) {
388 uip_create_solicited_node(&addr->ipaddr, &loc_fipaddr);
389 if((locmaddr = uip_ds6_maddr_lookup(&loc_fipaddr)) != NULL) {
390 uip_ds6_maddr_rm(locmaddr);
391 }
392 addr->isused = 0;
393 }
394 return;
395}
396
397/*---------------------------------------------------------------------------*/
399uip_ds6_addr_lookup(uip_ipaddr_t *ipaddr)
400{
402 ((uip_ds6_element_t *)uip_ds6_if.addr_list, UIP_DS6_ADDR_NB,
403 sizeof(uip_ds6_addr_t), ipaddr, 128,
404 (uip_ds6_element_t **)&locaddr) == FOUND) {
405 return locaddr;
406 }
407 return NULL;
408}
409
410/*---------------------------------------------------------------------------*/
411/*
412 * get a link local address -
413 * state = -1 => any address is ok. Otherwise state = desired state of addr.
414 * (TENTATIVE, PREFERRED, DEPRECATED)
415 */
417uip_ds6_get_link_local(int8_t state)
418{
419 for(locaddr = uip_ds6_if.addr_list;
420 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
421 if(locaddr->isused && (state == -1 || locaddr->state == state)
422 && (uip_is_addr_linklocal(&locaddr->ipaddr))) {
423 return locaddr;
424 }
425 }
426 return NULL;
427}
428
429/*---------------------------------------------------------------------------*/
430/*
431 * get a global address -
432 * state = -1 => any address is ok. Otherwise state = desired state of addr.
433 * (TENTATIVE, PREFERRED, DEPRECATED)
434 */
436uip_ds6_get_global(int8_t state)
437{
438 for(locaddr = uip_ds6_if.addr_list;
439 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
440 if(locaddr->isused && (state == -1 || locaddr->state == state)
441 && !(uip_is_addr_linklocal(&locaddr->ipaddr))) {
442 return locaddr;
443 }
444 }
445 return NULL;
446}
447
448/*---------------------------------------------------------------------------*/
450uip_ds6_maddr_add(const uip_ipaddr_t *ipaddr)
451{
453 ((uip_ds6_element_t *)uip_ds6_if.maddr_list, UIP_DS6_MADDR_NB,
454 sizeof(uip_ds6_maddr_t), (void*)ipaddr, 128,
455 (uip_ds6_element_t **)&locmaddr) == FREESPACE) {
456 locmaddr->isused = 1;
457 uip_ipaddr_copy(&locmaddr->ipaddr, ipaddr);
458 return locmaddr;
459 }
460 return NULL;
461}
462
463/*---------------------------------------------------------------------------*/
464void
465uip_ds6_maddr_rm(uip_ds6_maddr_t *maddr)
466{
467 if(maddr != NULL) {
468 maddr->isused = 0;
469 }
470 return;
471}
472
473/*---------------------------------------------------------------------------*/
475uip_ds6_maddr_lookup(const uip_ipaddr_t *ipaddr)
476{
478 ((uip_ds6_element_t *)uip_ds6_if.maddr_list, UIP_DS6_MADDR_NB,
479 sizeof(uip_ds6_maddr_t), (void*)ipaddr, 128,
480 (uip_ds6_element_t **)&locmaddr) == FOUND) {
481 return locmaddr;
482 }
483 return NULL;
484}
485
486
487/*---------------------------------------------------------------------------*/
489uip_ds6_aaddr_add(uip_ipaddr_t *ipaddr)
490{
491#if UIP_DS6_AADDR_NB
493 ((uip_ds6_element_t *)uip_ds6_if.aaddr_list, UIP_DS6_AADDR_NB,
494 sizeof(uip_ds6_aaddr_t), ipaddr, 128,
495 (uip_ds6_element_t **)&locaaddr) == FREESPACE) {
496 locaaddr->isused = 1;
497 uip_ipaddr_copy(&locaaddr->ipaddr, ipaddr);
498 return locaaddr;
499 }
500#endif /* UIP_DS6_AADDR_NB */
501 return NULL;
502}
503
504/*---------------------------------------------------------------------------*/
505void
506uip_ds6_aaddr_rm(uip_ds6_aaddr_t *aaddr)
507{
508 if(aaddr != NULL) {
509 aaddr->isused = 0;
510 }
511 return;
512}
513
514/*---------------------------------------------------------------------------*/
516uip_ds6_aaddr_lookup(uip_ipaddr_t *ipaddr)
517{
518#if UIP_DS6_AADDR_NB
520 UIP_DS6_AADDR_NB, sizeof(uip_ds6_aaddr_t), ipaddr, 128,
521 (uip_ds6_element_t **)&locaaddr) == FOUND) {
522 return locaaddr;
523 }
524#endif /* UIP_DS6_AADDR_NB */
525 return NULL;
526}
527
528/*---------------------------------------------------------------------------*/
529void
530uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
531{
532 uint8_t best = 0; /* number of bit in common with best match */
533 uint8_t n = 0;
534 uip_ds6_addr_t *matchaddr = NULL;
535
536 if(!uip_is_addr_linklocal(dst) && !uip_is_addr_mcast(dst)) {
537 /* find longest match */
538 for(locaddr = uip_ds6_if.addr_list;
539 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
540 /* Only preferred global (not link-local) addresses */
541 if(locaddr->isused && locaddr->state == ADDR_PREFERRED &&
542 !uip_is_addr_linklocal(&locaddr->ipaddr)) {
543 n = get_match_length(dst, &locaddr->ipaddr);
544 if(n >= best) {
545 best = n;
546 matchaddr = locaddr;
547 }
548 }
549 }
550#if UIP_IPV6_MULTICAST
551 } else if(uip_is_addr_mcast_routable(dst)) {
552 matchaddr = uip_ds6_get_global(ADDR_PREFERRED);
553#endif
554 } else {
555 matchaddr = uip_ds6_get_link_local(ADDR_PREFERRED);
556 }
557
558 /* use the :: (unspecified address) as source if no match found */
559 if(matchaddr == NULL) {
561 } else {
562 uip_ipaddr_copy(src, &matchaddr->ipaddr);
563 }
564}
565
566/*---------------------------------------------------------------------------*/
567void
568uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr)
569{
570#if (UIP_LLADDR_LEN == 8)
571 memcpy(ipaddr->u8 + 8, lladdr, UIP_LLADDR_LEN);
572 ipaddr->u8[8] ^= 0x02;
573#elif (UIP_LLADDR_LEN == 6)
574 memcpy(ipaddr->u8 + 8, lladdr, 3);
575 ipaddr->u8[11] = 0xff;
576 ipaddr->u8[12] = 0xfe;
577 memcpy(ipaddr->u8 + 13, (uint8_t *)lladdr + 3, 3);
578 ipaddr->u8[8] ^= 0x02;
579#elif (UIP_LLADDR_LEN == 2)
580 /* derive IID as per RFC 6282 */
581 memcpy(ipaddr->u8 + 8, iid_prefix, 6);
582 memcpy(ipaddr->u8 + 8 + 6, lladdr, UIP_LLADDR_LEN);
583#else
584#error uip-ds6.c cannot build interface address when UIP_LLADDR_LEN is not 6, 8, or 2
585#endif
586}
587/*---------------------------------------------------------------------------*/
588void
589uip_ds6_set_lladdr_from_iid(uip_lladdr_t *lladdr, const uip_ipaddr_t *ipaddr)
590{
591#if (UIP_LLADDR_LEN == 8)
592 memcpy(lladdr, ipaddr->u8 + 8, UIP_LLADDR_LEN);
593 lladdr->addr[0] ^= 0x02;
594#elif (UIP_LLADDR_LEN == 2)
595 memcpy(lladdr, ipaddr->u8 + 8 + 6, UIP_LLADDR_LEN);
596#else
597#error uip-ds6.c cannot build lladdr address when UIP_LLADDR_LEN is not 8 or 2
598#endif
599}
600
601/*---------------------------------------------------------------------------*/
602uint8_t
603get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst)
604{
605 uint8_t j, k, x_or;
606 uint8_t len = 0;
607
608 for(j = 0; j < 16; j++) {
609 if(src->u8[j] == dst->u8[j]) {
610 len += 8;
611 } else {
612 x_or = src->u8[j] ^ dst->u8[j];
613 for(k = 0; k < 8; k++) {
614 if((x_or & 0x80) == 0) {
615 len++;
616 x_or <<= 1;
617 } else {
618 break;
619 }
620 }
621 break;
622 }
623 }
624 return len;
625}
626
627/*---------------------------------------------------------------------------*/
628#if UIP_ND6_DEF_MAXDADNS > 0
629void
631{
632 /* send maxdadns NS for DAD */
633 if(addr->dadnscount < uip_ds6_if.maxdadns) {
634 uip_nd6_ns_output(NULL, NULL, &addr->ipaddr);
635 addr->dadnscount++;
636 timer_set(&addr->dadtimer,
637 uip_ds6_if.retrans_timer / 1000 * CLOCK_SECOND);
638 return;
639 }
640 /*
641 * If we arrive here it means DAD succeeded, otherwise the dad process
642 * would have been interrupted in ds6_dad_ns/na_input
643 */
644 LOG_INFO("DAD succeeded, ipaddr: ");
645 LOG_INFO_6ADDR(&addr->ipaddr);
646 LOG_INFO_("\n");
647
648 addr->state = ADDR_PREFERRED;
649 return;
650}
651
652/*---------------------------------------------------------------------------*/
653/*
654 * Calling code must handle when this returns 0 (e.g. link local
655 * address can not be used).
656 */
657int
659{
660 if(uip_is_addr_linklocal(&addr->ipaddr)) {
661 LOG_ERR("Contiki shutdown, DAD for link local address failed\n");
662 return 0;
663 }
664 uip_ds6_addr_rm(addr);
665 return 1;
666}
667#endif /*UIP_ND6_DEF_MAXDADNS > 0 */
668
669/*---------------------------------------------------------------------------*/
670#if UIP_CONF_ROUTER
671#if UIP_ND6_SEND_RA
672void
673uip_ds6_send_ra_sollicited(void)
674{
675 /* We have a pb here: RA timer max possible value is 1800s,
676 * hence we have to use stimers. However, when receiving a RS, we
677 * should delay the reply by a random value between 0 and 500ms timers.
678 * stimers are in seconds, hence we cannot do this. Therefore we just send
679 * the RA (setting the timer to 0 below). We keep the code logic for
680 * the days contiki will support appropriate timers */
681 rand_time = 0;
682 LOG_INFO("Solicited RA, random time %u\n", rand_time);
683
684 if(stimer_remaining(&uip_ds6_timer_ra) > rand_time) {
685 if(stimer_elapsed(&uip_ds6_timer_ra) < UIP_ND6_MIN_DELAY_BETWEEN_RAS) {
686 /* Ensure that the RAs are rate limited */
687/* stimer_set(&uip_ds6_timer_ra, rand_time +
688 UIP_ND6_MIN_DELAY_BETWEEN_RAS -
689 stimer_elapsed(&uip_ds6_timer_ra));
690 */ } else {
691 stimer_set(&uip_ds6_timer_ra, rand_time);
692 }
693 }
694}
695
696/*---------------------------------------------------------------------------*/
697void
698uip_ds6_send_ra_periodic(void)
699{
700 if(racount > 0) {
701 /* send previously scheduled RA */
702 uip_nd6_ra_output(NULL);
703 LOG_INFO("Sending periodic RA\n");
704 }
705
706 rand_time = UIP_ND6_MIN_RA_INTERVAL + random_rand() %
707 (uint16_t) (UIP_ND6_MAX_RA_INTERVAL - UIP_ND6_MIN_RA_INTERVAL);
708 LOG_DBG("Random time 1 = %u\n", rand_time);
709
710 if(racount < UIP_ND6_MAX_INITIAL_RAS) {
711 if(rand_time > UIP_ND6_MAX_INITIAL_RA_INTERVAL) {
712 rand_time = UIP_ND6_MAX_INITIAL_RA_INTERVAL;
713 LOG_DBG("Random time 2 = %u\n", rand_time);
714 }
715 racount++;
716 }
717 LOG_DBG("Random time 3 = %u\n", rand_time);
718 stimer_set(&uip_ds6_timer_ra, rand_time);
719}
720
721#endif /* UIP_ND6_SEND_RA */
722#else /* UIP_CONF_ROUTER */
723/*---------------------------------------------------------------------------*/
724void
726{
727 if((uip_ds6_defrt_choose() == NULL)
729 LOG_INFO("Sending RS %u\n", rscount);
731 rscount++;
734 } else {
735 LOG_INFO("Router found ? (boolean): %u\n",
736 (uip_ds6_defrt_choose() != NULL));
738 }
739 return;
740}
741
742#endif /* UIP_CONF_ROUTER */
743/*---------------------------------------------------------------------------*/
744uint32_t
746{
747 return (uint32_t) (UIP_ND6_MIN_RANDOM_FACTOR
748 (uip_ds6_if.base_reachable_time)) +
749 ((uint16_t) (random_rand() << 8) +
750 (uint16_t) random_rand()) %
751 (uint32_t) (UIP_ND6_MAX_RANDOM_FACTOR(uip_ds6_if.base_reachable_time) -
752 UIP_ND6_MIN_RANDOM_FACTOR(uip_ds6_if.base_reachable_time));
753}
754/*---------------------------------------------------------------------------*/
755
756/** @}*/
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition random.c:58
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:103
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition etimer.c:192
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition etimer.c:237
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
static void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
Definition stimer.h:100
static unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
Definition stimer.h:121
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
Definition stimer.c:125
bool stimer_expired(struct stimer *t)
Check if a timer has expired.
Definition stimer.c:109
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition timer.c:64
bool timer_expired(struct timer *t)
Check if a timer has expired.
Definition timer.c:123
void uip_nd6_ns_output(const uip_ipaddr_t *src, const uip_ipaddr_t *dest, uip_ipaddr_t *tgt)
Send a neighbor solicitation, send a Neighbor Advertisement.
#define uip_create_linklocal_allrouters_mcast(a)
set IP address a to the link local all-routers multicast address
Definition uip.h:1777
#define uip_create_unspecified(a)
set IP address a to unspecified
Definition uip.h:1771
uint8_t uip_ds6_list_loop(uip_ds6_element_t *list, uint8_t size, uint16_t elementsize, uip_ipaddr_t *ipaddr, uint8_t ipaddrlen, uip_ds6_element_t **out_element)
Generic loop routine on an abstract data structure, which generalizes all data structures used in DS6...
Definition uip-ds6.c:227
uint8_t get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Get the number of matching bits of two addresses.
Definition uip-ds6.c:603
#define UIP_ND6_MAX_RTR_SOLICITATION_DELAY
Maximum router solicitation delay.
Definition uip-nd6.h:64
void uip_ds6_periodic(void)
Periodic processing of data structures.
Definition uip-ds6.c:177
const uip_ip6addr_t * uip_ds6_default_prefix()
Retrieve the Default IPv6 prefix.
Definition uip-ds6.c:104
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
#define uip_is_addr_mcast_routable(a)
is address a routable multicast address.
Definition uip.h:1888
void uip_ds6_dad(uip_ds6_addr_t *addr)
Perform Duplicate Address Selection on one address.
Definition uip-ds6.c:630
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition uip.h:1725
void uip_ds6_init(void)
Initialize data structures.
Definition uip-ds6.c:116
void uip_ds6_send_rs(void)
Send periodic RS to find router.
Definition uip-ds6.c:725
#define UIP_LLADDR_LEN
802.15.4 address
Definition uip.h:145
#define UIP_DS6_PERIOD
General DS6 definitions.
Definition uip-ds6.h:169
struct etimer uip_ds6_timer_periodic
Timer for maintenance of data structures.
Definition uip-ds6.c:60
void uip_nd6_rs_output(void)
Neighbor Solicitation Processing.
Definition uip-nd6.c:810
#define UIP_ND6_RTR_SOLICITATION_INTERVAL
Router solicitation interval.
Definition uip-nd6.h:70
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1860
#define UIP_ND6_DEF_MAXDADNS
Do not try DAD when using EUI-64 as allowed by draft-ietf-6lowpan-nd-15 section 8....
Definition uip-nd6.h:144
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition uip-ds6.c:352
void uip_ds6_neighbor_periodic(void)
The housekeeping function called periodically.
void uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix)
Set the Default IPv6 prefix.
Definition uip-ds6.c:110
int uip_ds6_dad_failed(uip_ds6_addr_t *addr)
Callback when DAD failed.
Definition uip-ds6.c:658
static uint8_t rscount
number of rs already sent
Definition uip-ds6.c:70
#define uip_create_solicited_node(a, b)
put in b the solicited node address corresponding to address a both a and b are of type uip_ipaddr_t*
Definition uip.h:1804
struct etimer uip_ds6_timer_rs
RS timer, to schedule RS sending.
Definition uip-ds6.c:69
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition uip.h:1766
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
Definition uip-ds6.c:530
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition uip-ds6.c:568
void uip_ds6_set_lladdr_from_iid(uip_lladdr_t *lladdr, const uip_ipaddr_t *ipaddr)
Build a link-layer address from an IPv6 address based on its UUID64.
Definition uip-ds6.c:589
uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]
Prefix list.
Definition uip-ds6.c:76
#define UIP_ND6_MAX_RTR_SOLICITATIONS
Maximum router solicitations.
Definition uip-nd6.h:76
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition uip-ds6.h:156
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition uip-ds6.c:75
#define uip_create_linklocal_allnodes_mcast(a)
set IP address a to the link local all-nodes multicast address
Definition uip.h:1774
uint32_t uip_ds6_compute_reachable_time(void)
Compute the reachable time based on base reachable time, see RFC 4861.
Definition uip-ds6.c:745
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition uip.h:912
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition uip.h:969
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition uip6.c:159
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
Definition uipopt.h:134
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Definition uipopt.h:154
Header file for the logging system.
A timer.
Definition etimer.h:79
A timer.
Definition stimer.h:83
Anycast address
Definition uip-ds6.h:219
Unicast address structure.
Definition uip-ds6.h:205
Generic type for a DS6, to use a common loop though all DS.
Definition uip-ds6.h:250
A multicast address.
Definition uip-ds6.h:225
Interface structure (contains all the interface variables)
Definition uip-ds6.h:231
A prefix list entry.
Definition uip-ds6.h:195
IPv6 Neighbor cache (link-layer/IPv6 address mapping)
Header file for IPv6-related data structures.
This header file contains configuration directives for uIPv6 multicast support.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107
Header file for IPv6 Neighbor discovery (RFC 4861)