48#include "lib/random.h"
53#include "net/ipv6/uip-packetqueue.h"
57#define LOG_MODULE "IPv6 DS"
58#define LOG_LEVEL LOG_LEVEL_IPV6
63struct stimer uip_ds6_timer_ra;
65static uint8_t racount;
66static uint16_t rand_time;
79uint8_t uip_ds6_addr_size;
80uint8_t uip_ds6_netif_addr_list_offset;
85static uip_ipaddr_t loc_fipaddr;
94#if (UIP_LLADDR_LEN == 2)
95static const uint8_t iid_prefix[] = { 0x00, 0x00 , 0x00 , 0xff , 0xfe , 0x00 };
99static uip_ip6addr_t default_prefix = {
100 .u16 = { 0, 0, 0, 0, 0, 0, 0, 0 }
106 return &default_prefix;
112 uip_ip6addr_copy(&default_prefix, prefix);
119 uip_ip6addr(&default_prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
122 uip_ds6_neighbors_init();
123 uip_ds6_route_init();
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);
136 uip_ds6_netif_addr_list_offset = offsetof(
struct uip_ds6_netif, addr_list);
141 uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
143 uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
147 uip_create_linklocal_prefix(&loc_fipaddr);
149 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
151 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
157 uip_ds6_maddr_add(&loc_fipaddr);
160 uip_ds6_maddr_add(&loc_fipaddr);
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
188 && (locaddr->dadnscount <=
uip_ds6_if.maxdadns)
198 uip_ds6_defrt_periodic();
204 if(locprefix->isused && !locprefix->isinfinite
206 uip_ds6_prefix_rm(locprefix);
215#if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
218 uip_ds6_send_ra_periodic();
228 uint16_t elementsize, uip_ipaddr_t *
ipaddr,
233 if(list == NULL ||
ipaddr == NULL || out_element == NULL) {
243 if(element->isused) {
244 if(uip_ipaddr_prefixcmp(&element->ipaddr,
ipaddr, ipaddrlen)) {
245 *out_element = element;
249 *out_element = element;
253 return *out_element != NULL ? FREESPACE : NOSPACE;
260uip_ds6_prefix_add(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen,
261 uint8_t advertise, uint8_t flags,
unsigned long vtime,
268 locprefix->isused = 1;
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);
281 LOG_INFO(
"No more space in Prefix list\n");
289uip_ds6_prefix_add(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen,
290 unsigned long interval)
296 locprefix->isused = 1;
298 locprefix->length = ipaddrlen;
300 stimer_set(&(locprefix->vlifetime), interval);
301 locprefix->isinfinite = 0;
303 locprefix->isinfinite = 1;
305 LOG_INFO(
"Adding prefix ");
306 LOG_INFO_6ADDR(&locprefix->ipaddr);
307 LOG_INFO_(
" len %u, valid lifetime %lu\n", ipaddrlen, interval);
325uip_ds6_prefix_lookup(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen)
338uip_ds6_is_addr_onlink(uip_ipaddr_t *
ipaddr)
342 if(locprefix->isused &&
343 uip_ipaddr_prefixcmp(&locprefix->ipaddr,
ipaddr, locprefix->length)) {
360 locaddr->type = type;
362 locaddr->isinfinite = 1;
364 locaddr->isinfinite = 0;
367#if UIP_ND6_DEF_MAXDADNS > 0
372 locaddr->dadnscount = 0;
374 locaddr->state = ADDR_PREFERRED;
377 uip_ds6_maddr_add(&loc_fipaddr);
389 if((locmaddr = uip_ds6_maddr_lookup(&loc_fipaddr)) != NULL) {
390 uip_ds6_maddr_rm(locmaddr);
399uip_ds6_addr_lookup(uip_ipaddr_t *
ipaddr)
417uip_ds6_get_link_local(int8_t state)
420 locaddr <
uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
421 if(locaddr->isused && (state == -1 || locaddr->state == state)
436uip_ds6_get_global(int8_t state)
439 locaddr <
uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
440 if(locaddr->isused && (state == -1 || locaddr->state == state)
450uip_ds6_maddr_add(
const uip_ipaddr_t *
ipaddr)
456 locmaddr->isused = 1;
475uip_ds6_maddr_lookup(
const uip_ipaddr_t *
ipaddr)
489uip_ds6_aaddr_add(uip_ipaddr_t *
ipaddr)
496 locaaddr->isused = 1;
516uip_ds6_aaddr_lookup(uip_ipaddr_t *
ipaddr)
539 locaddr <
uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
541 if(locaddr->isused && locaddr->state == ADDR_PREFERRED &&
550#if UIP_IPV6_MULTICAST
552 matchaddr = uip_ds6_get_global(ADDR_PREFERRED);
555 matchaddr = uip_ds6_get_link_local(ADDR_PREFERRED);
559 if(matchaddr == NULL) {
570#if (UIP_LLADDR_LEN == 8)
573#elif (UIP_LLADDR_LEN == 6)
574 memcpy(
ipaddr->u8 + 8, lladdr, 3);
577 memcpy(
ipaddr->u8 + 13, (uint8_t *)lladdr + 3, 3);
579#elif (UIP_LLADDR_LEN == 2)
581 memcpy(
ipaddr->u8 + 8, iid_prefix, 6);
584#error uip-ds6.c cannot build interface address when UIP_LLADDR_LEN is not 6, 8, or 2
591#if (UIP_LLADDR_LEN == 8)
593 lladdr->addr[0] ^= 0x02;
594#elif (UIP_LLADDR_LEN == 2)
597#error uip-ds6.c cannot build lladdr address when UIP_LLADDR_LEN is not 8 or 2
608 for(j = 0; j < 16; j++) {
609 if(src->u8[j] == dst->u8[j]) {
612 x_or = src->u8[j] ^ dst->u8[j];
613 for(k = 0; k < 8; k++) {
614 if((x_or & 0x80) == 0) {
628#if UIP_ND6_DEF_MAXDADNS > 0
644 LOG_INFO(
"DAD succeeded, ipaddr: ");
645 LOG_INFO_6ADDR(&
addr->ipaddr);
648 addr->state = ADDR_PREFERRED;
661 LOG_ERR(
"Contiki shutdown, DAD for link local address failed\n");
664 uip_ds6_addr_rm(
addr);
673uip_ds6_send_ra_sollicited(
void)
682 LOG_INFO(
"Solicited RA, random time %u\n", rand_time);
685 if(
stimer_elapsed(&uip_ds6_timer_ra) < UIP_ND6_MIN_DELAY_BETWEEN_RAS) {
698uip_ds6_send_ra_periodic(
void)
702 uip_nd6_ra_output(NULL);
703 LOG_INFO(
"Sending periodic RA\n");
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);
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);
717 LOG_DBG(
"Random time 3 = %u\n", rand_time);
727 if((uip_ds6_defrt_choose() == NULL)
729 LOG_INFO(
"Sending RS %u\n",
rscount);
735 LOG_INFO(
"Router found ? (boolean): %u\n",
736 (uip_ds6_defrt_choose() != NULL));
747 return (uint32_t) (UIP_ND6_MIN_RANDOM_FACTOR
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));
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
#define CLOCK_SECOND
A second, measured in system clock time.
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
void etimer_stop(struct etimer *et)
Stop a pending event timer.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
static void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
static unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
bool stimer_expired(struct stimer *t)
Check if a timer has expired.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
bool timer_expired(struct timer *t)
Check if a timer has expired.
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
#define uip_create_unspecified(a)
set IP address a to unspecified
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...
uint8_t get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Get the number of matching bits of two addresses.
#define UIP_ND6_MAX_RTR_SOLICITATION_DELAY
Maximum router solicitation delay.
void uip_ds6_periodic(void)
Periodic processing of data structures.
const uip_ip6addr_t * uip_ds6_default_prefix()
Retrieve the Default IPv6 prefix.
uip_lladdr_t uip_lladdr
Host L2 address.
#define uip_is_addr_mcast_routable(a)
is address a routable multicast address.
void uip_ds6_dad(uip_ds6_addr_t *addr)
Perform Duplicate Address Selection on one address.
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
void uip_ds6_init(void)
Initialize data structures.
void uip_ds6_send_rs(void)
Send periodic RS to find router.
#define UIP_LLADDR_LEN
802.15.4 address
#define UIP_DS6_PERIOD
General DS6 definitions.
struct etimer uip_ds6_timer_periodic
Timer for maintenance of data structures.
void uip_nd6_rs_output(void)
Neighbor Solicitation Processing.
#define UIP_ND6_RTR_SOLICITATION_INTERVAL
Router solicitation interval.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
#define UIP_ND6_DEF_MAXDADNS
Do not try DAD when using EUI-64 as allowed by draft-ietf-6lowpan-nd-15 section 8....
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_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.
int uip_ds6_dad_failed(uip_ds6_addr_t *addr)
Callback when DAD failed.
static uint8_t rscount
number of rs already sent
#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*
struct etimer uip_ds6_timer_rs
RS timer, to schedule RS sending.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
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
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.
uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]
Prefix list.
#define UIP_ND6_MAX_RTR_SOLICITATIONS
Maximum router solicitations.
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
uip_ds6_netif_t uip_ds6_if
The single interface.
#define uip_create_linklocal_allnodes_mcast(a)
set IP address a to the link local all-nodes multicast address
uint32_t uip_ds6_compute_reachable_time(void)
Compute the reachable time based on base reachable time, see RFC 4861.
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Header file for the logging system.
Unicast address structure.
Generic type for a DS6, to use a common loop though all DS.
Interface structure (contains all the interface variables)
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.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Header file for IPv6 Neighbor discovery (RFC 4861)