48 #include "lib/random.h"
51 #include "net/ip/uip-packetqueue.h"
53 #define DEBUG DEBUG_NONE
59 struct stimer uip_ds6_timer_ra;
61 static uint8_t racount;
62 static uint16_t rand_time;
75 uint8_t uip_ds6_addr_size;
76 uint8_t uip_ds6_netif_addr_list_offset;
81 static uip_ipaddr_t loc_fipaddr;
96 uip_ds6_neighbors_init();
99 PRINTF(
"Init of IPv6 data structures\n");
100 PRINTF(
"%u neighbors\n%u default routers\n%u prefixes\n%u routes\n%u unicast addresses\n%u multicast addresses\n%u anycast addresses\n",
101 NBR_TABLE_MAX_NEIGHBORS, UIP_DS6_DEFRT_NB, UIP_DS6_PREFIX_NB, UIP_DS6_ROUTE_NB,
102 UIP_DS6_ADDR_NB, UIP_DS6_MADDR_NB, UIP_DS6_AADDR_NB);
103 memset(uip_ds6_prefix_list, 0,
sizeof(uip_ds6_prefix_list));
104 memset(&uip_ds6_if, 0,
sizeof(uip_ds6_if));
106 uip_ds6_netif_addr_list_offset = offsetof(
struct uip_ds6_netif, addr_list);
110 uip_ds6_if.cur_hop_limit =
UIP_TTL;
111 uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
113 uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
117 uip_create_linklocal_prefix(&loc_fipaddr);
119 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
121 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
127 uip_ds6_maddr_add(&loc_fipaddr);
130 uip_ds6_maddr_add(&loc_fipaddr);
151 for(locaddr = uip_ds6_if.addr_list;
152 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
153 if(locaddr->isused) {
154 if((!locaddr->isinfinite) && (
stimer_expired(&locaddr->vlifetime))) {
155 uip_ds6_addr_rm(locaddr);
156 #if UIP_ND6_DEF_MAXDADNS > 0
158 && (locaddr->dadnscount <= uip_ds6_if.maxdadns)
168 uip_ds6_defrt_periodic();
179 for(locprefix = uip_ds6_prefix_list;
180 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB;
182 if(locprefix->isused && !locprefix->isinfinite
184 uip_ds6_prefix_rm(locprefix);
190 uip_ds6_neighbor_periodic();
193 #if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
196 uip_ds6_send_ra_periodic();
206 uint16_t elementsize, uip_ipaddr_t *
ipaddr,
217 if(element->isused) {
218 if(uip_ipaddr_prefixcmp(&element->ipaddr, ipaddr, ipaddrlen)) {
219 *out_element = element;
223 *out_element = element;
227 return *out_element !=
NULL ? FREESPACE : NOSPACE;
234 uip_ds6_prefix_add(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen,
235 uint8_t advertise, uint8_t flags,
unsigned long vtime,
242 locprefix->isused = 1;
244 locprefix->length = ipaddrlen;
245 locprefix->advertise = advertise;
246 locprefix->l_a_reserved = flags;
247 locprefix->vlifetime = vtime;
248 locprefix->plifetime = ptime;
249 PRINTF(
"Adding prefix ");
250 PRINT6ADDR(&locprefix->ipaddr);
251 PRINTF(
"length %u, flags %x, Valid lifetime %lx, Preffered lifetime %lx\n",
252 ipaddrlen, flags, vtime, ptime);
255 PRINTF(
"No more space in Prefix list\n");
263 uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen,
264 unsigned long interval)
270 locprefix->isused = 1;
272 locprefix->length = ipaddrlen;
274 stimer_set(&(locprefix->vlifetime), interval);
275 locprefix->isinfinite = 0;
277 locprefix->isinfinite = 1;
279 PRINTF(
"Adding prefix ");
280 PRINT6ADDR(&locprefix->ipaddr);
281 PRINTF(
"length %u, vlifetime %lu\n", ipaddrlen, interval);
299 uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen)
312 uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr)
314 for(locprefix = uip_ds6_prefix_list;
315 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; locprefix++) {
316 if(locprefix->isused &&
317 uip_ipaddr_prefixcmp(&locprefix->ipaddr, ipaddr, locprefix->length)) {
334 locaddr->type = type;
336 locaddr->isinfinite = 1;
338 locaddr->isinfinite = 0;
341 #if UIP_ND6_DEF_MAXDADNS > 0
346 locaddr->dadnscount = 0;
348 locaddr->state = ADDR_PREFERRED;
351 uip_ds6_maddr_add(&loc_fipaddr);
363 if((locmaddr = uip_ds6_maddr_lookup(&loc_fipaddr)) !=
NULL) {
364 uip_ds6_maddr_rm(locmaddr);
373 uip_ds6_addr_lookup(uip_ipaddr_t *ipaddr)
391 uip_ds6_get_link_local(int8_t state)
393 for(locaddr = uip_ds6_if.addr_list;
394 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
395 if(locaddr->isused && (state == -1 || locaddr->state == state)
410 uip_ds6_get_global(int8_t state)
412 for(locaddr = uip_ds6_if.addr_list;
413 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
414 if(locaddr->isused && (state == -1 || locaddr->state == state)
424 uip_ds6_maddr_add(
const uip_ipaddr_t *ipaddr)
430 locmaddr->isused = 1;
449 uip_ds6_maddr_lookup(
const uip_ipaddr_t *ipaddr)
463 uip_ds6_aaddr_add(uip_ipaddr_t *ipaddr)
470 locaaddr->isused = 1;
490 uip_ds6_aaddr_lookup(uip_ipaddr_t *ipaddr)
512 for(locaddr = uip_ds6_if.addr_list;
513 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
515 if(locaddr->isused && locaddr->state == ADDR_PREFERRED &&
524 #if UIP_IPV6_MULTICAST
526 matchaddr = uip_ds6_get_global(ADDR_PREFERRED);
529 matchaddr = uip_ds6_get_link_local(ADDR_PREFERRED);
533 if(matchaddr ==
NULL) {
546 #if (UIP_LLADDR_LEN == 8)
548 ipaddr->u8[8] ^= 0x02;
549 #elif (UIP_LLADDR_LEN == 6)
550 memcpy(ipaddr->u8 + 8, lladdr, 3);
551 ipaddr->u8[11] = 0xff;
552 ipaddr->u8[12] = 0xfe;
553 memcpy(ipaddr->u8 + 13, (uint8_t *)lladdr + 3, 3);
554 ipaddr->u8[8] ^= 0x02;
556 #error uip-ds6.c cannot build interface address when UIP_LLADDR_LEN is not 6 or 8
567 for(j = 0; j < 16; j++) {
568 if(src->u8[j] == dst->u8[j]) {
571 x_or = src->u8[j] ^ dst->u8[j];
572 for(k = 0; k < 8; k++) {
573 if((x_or & 0x80) == 0) {
587 #if UIP_ND6_DEF_MAXDADNS > 0
592 if(addr->dadnscount < uip_ds6_if.maxdadns) {
603 PRINTF(
"DAD succeeded, ipaddr: ");
604 PRINT6ADDR(&addr->ipaddr);
607 addr->state = ADDR_PREFERRED;
620 PRINTF(
"Contiki shutdown, DAD for link local address failed\n");
623 uip_ds6_addr_rm(addr);
632 uip_ds6_send_ra_sollicited(
void)
641 PRINTF(
"Solicited RA, random time %u\n", rand_time);
644 if(
stimer_elapsed(&uip_ds6_timer_ra) < UIP_ND6_MIN_DELAY_BETWEEN_RAS) {
657 uip_ds6_send_ra_periodic(
void)
661 uip_nd6_ra_output(
NULL);
662 PRINTF(
"Sending periodic RA\n");
665 rand_time = UIP_ND6_MIN_RA_INTERVAL +
random_rand() %
666 (uint16_t) (UIP_ND6_MAX_RA_INTERVAL - UIP_ND6_MIN_RA_INTERVAL);
667 PRINTF(
"Random time 1 = %u\n", rand_time);
669 if(racount < UIP_ND6_MAX_INITIAL_RAS) {
670 if(rand_time > UIP_ND6_MAX_INITIAL_RA_INTERVAL) {
671 rand_time = UIP_ND6_MAX_INITIAL_RA_INTERVAL;
672 PRINTF(
"Random time 2 = %u\n", rand_time);
676 PRINTF(
"Random time 3 = %u\n", rand_time);
686 if((uip_ds6_defrt_choose() ==
NULL)
688 PRINTF(
"Sending RS %u\n",
rscount);
694 PRINTF(
"Router found ? (boolean): %u\n",
695 (uip_ds6_defrt_choose() !=
NULL));
706 return (uint32_t) (UIP_ND6_MIN_RANDOM_FACTOR
707 (uip_ds6_if.base_reachable_time)) +
710 (uint32_t) (UIP_ND6_MAX_RANDOM_FACTOR(uip_ds6_if.base_reachable_time) -
711 UIP_ND6_MIN_RANDOM_FACTOR(uip_ds6_if.base_reachable_time));
Header file for IPv6 Neighbor discovery (RFC 4861)
uip_len
The length of the packet in the uip_buf buffer.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Header file for IPv6-related data structures.
void uip_nd6_rs_output(void)
Send a Router Solicitation.
void uip_ds6_send_rs(void)
Send periodic RS to find router.
uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]
Prefix list.
#define uip_create_unspecified(a)
set IP address a to unspecified
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
int stimer_expired(struct stimer *t)
Check if a timer has expired.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define UIP_LLADDR_LEN
Link layer address length.
int uip_ds6_dad_failed(uip_ds6_addr_t *addr)
Callback when DAD failed.
#define UIP_ND6_MAX_RTR_SOLICITATION_DELAY
Maximum router solicitation delay.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
#define uip_is_addr_mcast_routable(a)
is address a routable multicast address.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 3513 a is of type uip_ipaddr_t*
void etimer_stop(struct etimer *et)
Stop a pending event timer.
#define uip_create_linklocal_allnodes_mcast(a)
set IP address a to the link local all-nodes multicast address
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...
struct etimer uip_ds6_timer_periodic
Timer for maintenance of data structures.
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
#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*...
Unicast address structure.
#define UIP_ND6_RTR_SOLICITATION_INTERVAL
Router solicitation interval.
uint32_t uip_ds6_compute_reachable_time(void)
Compute the reachable time based on base reachable time, see RFC 4861.
Interface structure (contains all the interface variables)
#define uip_create_linklocal_allrouters_mcast(a)
set IP address a to the link local all-routers multicast address
static uint8_t rscount
number of rs already sent
#define NULL
The null pointer.
struct etimer uip_ds6_timer_rs
RS timer, to schedule RS sending.
unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
void uip_ds6_periodic(void)
Periodic processing of data structures.
#define UIP_ND6_MAX_RTR_SOLICITATIONS
Maximum router solicitations.
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
#define CLOCK_SECOND
A second, measured in system clock time.
void uip_ds6_dad(uip_ds6_addr_t *addr)
Perform Duplicate Address Selection on one address.
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.
void uip_nd6_ns_output(uip_ipaddr_t *src, uip_ipaddr_t *dest, uip_ipaddr_t *tgt)
Send a neighbor solicitation, send a Neighbor Advertisement.
A set of debugging macros for the IP stack
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
void uip_ds6_init(void)
Initialize data structures.
#define UIP_DS6_PERIOD
General DS6 definitions.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC3513 i.e.
uip_ds6_netif_t uip_ds6_if
The single interface.
Generic type for a DS6, to use a common loop though all DS.
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
uint8_t get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Get the number of matching bits of two addresses.
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
int timer_expired(struct timer *t)
Check if a timer has expired.
#define UIP_ND6_DEF_MAXDADNS
Do not try DAD when using EUI-64 as allowed by draft-ietf-6lowpan-nd-15 section 8.2.