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 63 struct stimer uip_ds6_timer_ra;
65 static uint8_t racount;
66 static uint16_t rand_time;
79 uint8_t uip_ds6_addr_size;
80 uint8_t uip_ds6_netif_addr_list_offset;
85 static uip_ipaddr_t loc_fipaddr;
94 #if (UIP_LLADDR_LEN == 2) 95 static const uint8_t iid_prefix[] = { 0x00, 0x00 , 0x00 , 0xff , 0xfe , 0x00 };
103 uip_ds6_neighbors_init();
104 uip_ds6_route_init();
106 LOG_INFO(
"Init: %u neighbors\n", NBR_TABLE_MAX_NEIGHBORS);
107 LOG_INFO(
"%u default routers\n", UIP_DS6_DEFRT_NB);
108 LOG_INFO(
"%u prefixes\n", UIP_DS6_PREFIX_NB);
109 LOG_INFO(
"%u routes\n", UIP_DS6_ROUTE_NB);
110 LOG_INFO(
"%u unicast addresses\n", UIP_DS6_ADDR_NB);
111 LOG_INFO(
"%u multicast addresses\n", UIP_DS6_MADDR_NB);
112 LOG_INFO(
"%u anycast addresses\n", UIP_DS6_AADDR_NB);
114 memset(uip_ds6_prefix_list, 0,
sizeof(uip_ds6_prefix_list));
115 memset(&uip_ds6_if, 0,
sizeof(uip_ds6_if));
117 uip_ds6_netif_addr_list_offset = offsetof(
struct uip_ds6_netif, addr_list);
121 uip_ds6_if.cur_hop_limit =
UIP_TTL;
122 uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
124 uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
128 uip_create_linklocal_prefix(&loc_fipaddr);
130 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
132 uip_ds6_prefix_add(&loc_fipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
138 uip_ds6_maddr_add(&loc_fipaddr);
141 uip_ds6_maddr_add(&loc_fipaddr);
162 for(locaddr = uip_ds6_if.addr_list;
163 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
164 if(locaddr->isused) {
165 if((!locaddr->isinfinite) && (
stimer_expired(&locaddr->vlifetime))) {
166 uip_ds6_addr_rm(locaddr);
167 #if UIP_ND6_DEF_MAXDADNS > 0 169 && (locaddr->dadnscount <= uip_ds6_if.maxdadns)
179 uip_ds6_defrt_periodic();
190 for(locprefix = uip_ds6_prefix_list;
191 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB;
193 if(locprefix->isused && !locprefix->isinfinite
195 uip_ds6_prefix_rm(locprefix);
201 uip_ds6_neighbor_periodic();
204 #if UIP_CONF_ROUTER && UIP_ND6_SEND_RA 207 uip_ds6_send_ra_periodic();
217 uint16_t elementsize, uip_ipaddr_t *
ipaddr,
222 if(list == NULL || ipaddr == NULL || out_element == NULL) {
232 if(element->isused) {
233 if(uip_ipaddr_prefixcmp(&element->ipaddr, ipaddr, ipaddrlen)) {
234 *out_element = element;
238 *out_element = element;
242 return *out_element != NULL ? FREESPACE : NOSPACE;
249 uip_ds6_prefix_add(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen,
250 uint8_t advertise, uint8_t flags,
unsigned long vtime,
257 locprefix->isused = 1;
259 locprefix->length = ipaddrlen;
260 locprefix->advertise = advertise;
261 locprefix->l_a_reserved = flags;
262 locprefix->vlifetime = vtime;
263 locprefix->plifetime = ptime;
264 LOG_INFO(
"Adding prefix ");
265 LOG_INFO_6ADDR(&locprefix->ipaddr);
266 LOG_INFO_(
"length %u, flags %x, Valid lifetime %lx, Preffered lifetime %lx\n",
267 ipaddrlen, flags, vtime, ptime);
270 LOG_INFO(
"No more space in Prefix list\n");
278 uip_ds6_prefix_add(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen,
279 unsigned long interval)
285 locprefix->isused = 1;
287 locprefix->length = ipaddrlen;
289 stimer_set(&(locprefix->vlifetime), interval);
290 locprefix->isinfinite = 0;
292 locprefix->isinfinite = 1;
294 LOG_INFO(
"Adding prefix ");
295 LOG_INFO_6ADDR(&locprefix->ipaddr);
296 LOG_INFO_(
"length %u, vlifetime %lu\n", ipaddrlen, interval);
314 uip_ds6_prefix_lookup(uip_ipaddr_t *
ipaddr, uint8_t ipaddrlen)
327 uip_ds6_is_addr_onlink(uip_ipaddr_t *
ipaddr)
329 for(locprefix = uip_ds6_prefix_list;
330 locprefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; locprefix++) {
331 if(locprefix->isused &&
332 uip_ipaddr_prefixcmp(&locprefix->ipaddr, ipaddr, locprefix->length)) {
349 locaddr->type = type;
351 locaddr->isinfinite = 1;
353 locaddr->isinfinite = 0;
356 #if UIP_ND6_DEF_MAXDADNS > 0 361 locaddr->dadnscount = 0;
363 locaddr->state = ADDR_PREFERRED;
366 uip_ds6_maddr_add(&loc_fipaddr);
378 if((locmaddr = uip_ds6_maddr_lookup(&loc_fipaddr)) != NULL) {
379 uip_ds6_maddr_rm(locmaddr);
388 uip_ds6_addr_lookup(uip_ipaddr_t *
ipaddr)
406 uip_ds6_get_link_local(int8_t state)
408 for(locaddr = uip_ds6_if.addr_list;
409 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
410 if(locaddr->isused && (state == -1 || locaddr->state == state)
425 uip_ds6_get_global(int8_t state)
427 for(locaddr = uip_ds6_if.addr_list;
428 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
429 if(locaddr->isused && (state == -1 || locaddr->state == state)
439 uip_ds6_maddr_add(
const uip_ipaddr_t *
ipaddr)
445 locmaddr->isused = 1;
464 uip_ds6_maddr_lookup(
const uip_ipaddr_t *
ipaddr)
478 uip_ds6_aaddr_add(uip_ipaddr_t *
ipaddr)
485 locaaddr->isused = 1;
505 uip_ds6_aaddr_lookup(uip_ipaddr_t *
ipaddr)
527 for(locaddr = uip_ds6_if.addr_list;
528 locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) {
530 if(locaddr->isused && locaddr->state == ADDR_PREFERRED &&
539 #if UIP_IPV6_MULTICAST 541 matchaddr = uip_ds6_get_global(ADDR_PREFERRED);
544 matchaddr = uip_ds6_get_link_local(ADDR_PREFERRED);
548 if(matchaddr == NULL) {
559 #if (UIP_LLADDR_LEN == 8) 561 ipaddr->u8[8] ^= 0x02;
562 #elif (UIP_LLADDR_LEN == 6) 563 memcpy(ipaddr->u8 + 8, lladdr, 3);
564 ipaddr->u8[11] = 0xff;
565 ipaddr->u8[12] = 0xfe;
566 memcpy(ipaddr->u8 + 13, (uint8_t *)lladdr + 3, 3);
567 ipaddr->u8[8] ^= 0x02;
568 #elif (UIP_LLADDR_LEN == 2) 570 memcpy(ipaddr->u8 + 8, iid_prefix, 6);
573 #error uip-ds6.c cannot build interface address when UIP_LLADDR_LEN is not 6, 8, or 2 580 #if (UIP_LLADDR_LEN == 8) 582 lladdr->addr[0] ^= 0x02;
583 #elif (UIP_LLADDR_LEN == 2) 586 #error uip-ds6.c cannot build lladdr address when UIP_LLADDR_LEN is not 8 or 2 597 for(j = 0; j < 16; j++) {
598 if(src->u8[j] == dst->u8[j]) {
601 x_or = src->u8[j] ^ dst->u8[j];
602 for(k = 0; k < 8; k++) {
603 if((x_or & 0x80) == 0) {
617 #if UIP_ND6_DEF_MAXDADNS > 0 622 if(addr->dadnscount < uip_ds6_if.maxdadns) {
633 LOG_INFO(
"DAD succeeded, ipaddr: ");
634 LOG_INFO_6ADDR(&addr->ipaddr);
637 addr->state = ADDR_PREFERRED;
650 LOG_ERR(
"Contiki shutdown, DAD for link local address failed\n");
653 uip_ds6_addr_rm(addr);
662 uip_ds6_send_ra_sollicited(
void)
671 LOG_INFO(
"Solicited RA, random time %u\n", rand_time);
674 if(
stimer_elapsed(&uip_ds6_timer_ra) < UIP_ND6_MIN_DELAY_BETWEEN_RAS) {
687 uip_ds6_send_ra_periodic(
void)
691 uip_nd6_ra_output(NULL);
692 LOG_INFO(
"Sending periodic RA\n");
695 rand_time = UIP_ND6_MIN_RA_INTERVAL +
random_rand() %
696 (uint16_t) (UIP_ND6_MAX_RA_INTERVAL - UIP_ND6_MIN_RA_INTERVAL);
697 LOG_DBG(
"Random time 1 = %u\n", rand_time);
699 if(racount < UIP_ND6_MAX_INITIAL_RAS) {
700 if(rand_time > UIP_ND6_MAX_INITIAL_RA_INTERVAL) {
701 rand_time = UIP_ND6_MAX_INITIAL_RA_INTERVAL;
702 LOG_DBG(
"Random time 2 = %u\n", rand_time);
706 LOG_DBG(
"Random time 3 = %u\n", rand_time);
716 if((uip_ds6_defrt_choose() == NULL)
718 LOG_INFO(
"Sending RS %u\n",
rscount);
724 LOG_INFO(
"Router found ? (boolean): %u\n",
725 (uip_ds6_defrt_choose() != NULL));
736 return (uint32_t) (UIP_ND6_MIN_RANDOM_FACTOR
737 (uip_ds6_if.base_reachable_time)) +
740 (uint32_t) (UIP_ND6_MAX_RANDOM_FACTOR(uip_ds6_if.base_reachable_time) -
741 UIP_ND6_MIN_RANDOM_FACTOR(uip_ds6_if.base_reachable_time));
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
uip_lladdr_t uip_lladdr
Host L2 address.
void etimer_stop(struct etimer *et)
Stop a pending event timer.
#define uip_is_addr_mcast_routable(a)
is address a routable multicast address.
void uip_ds6_send_rs(void)
Send periodic RS to find router.
unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
void uip_ds6_init(void)
Initialize data structures.
#define UIP_ND6_RTR_SOLICITATION_INTERVAL
Router solicitation interval.
Generic type for a DS6, to use a common loop though all DS.
IPv6 Neighbor cache (link-layer/IPv6 address mapping)
int stimer_expired(struct stimer *t)
Check if a timer has expired.
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.
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 uip_ds6_dad(uip_ds6_addr_t *addr)
Perform Duplicate Address Selection on one address.
uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]
Prefix list.
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Header file for IPv6-related data structures.
int uip_ds6_dad_failed(uip_ds6_addr_t *addr)
Callback when DAD failed.
#define UIP_LLADDR_LEN
802.15.4 address
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.
This header file contains configuration directives for uIPv6 multicast support.
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
#define CLOCK_SECOND
A second, measured in system clock time.
void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
static uint8_t rscount
number of rs already sent
struct etimer uip_ds6_timer_periodic
Timer for maintenance of data structures.
#define UIP_ND6_MAX_RTR_SOLICITATIONS
Maximum router solicitations.
#define UIP_ND6_DEF_MAXDADNS
Do not try DAD when using EUI-64 as allowed by draft-ietf-6lowpan-nd-15 section 8.2.
#define uip_create_linklocal_allnodes_mcast(a)
set IP address a to the link local all-nodes multicast address
Unicast address structure.
int timer_expired(struct timer *t)
Check if a timer has expired.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
#define UIP_ND6_MAX_RTR_SOLICITATION_DELAY
Maximum router solicitation delay.
#define uip_create_linklocal_allrouters_mcast(a)
set IP address a to the link local all-routers multicast address
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
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.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
void uip_ds6_periodic(void)
Periodic processing of data structures.
struct etimer uip_ds6_timer_rs
RS timer, to schedule RS sending.
#define uip_create_unspecified(a)
set IP address a to unspecified
Header file for IPv6 Neighbor discovery (RFC 4861)
uip_ds6_netif_t uip_ds6_if
The single interface.
Interface structure (contains all the interface variables)
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...
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Header file for the logging system
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
void uip_nd6_rs_output(void)
Neighbor Solicitation Processing.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define UIP_DS6_PERIOD
General DS6 definitions.
#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*...
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
uint32_t uip_ds6_compute_reachable_time(void)
Compute the reachable time based on base reachable time, see RFC 4861.