Contiki-NG
Loading...
Searching...
No Matches
rpl-dag-root.c
1/*
2 * Copyright (c) 2012-2014, Thingsquare, http://www.thingsquare.com/.
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32/**
33 * \addtogroup uip
34 * @{
35 */
36
37#include "contiki.h"
38#include "contiki-net.h"
39
40#include "net/routing/rpl-classic/rpl.h"
41#include "net/routing/rpl-classic/rpl-private.h"
43
44#include "sys/log.h"
45
46#include <string.h>
47
48#define LOG_MODULE "RPL"
49#define LOG_LEVEL LOG_LEVEL_RPL
50
51/*---------------------------------------------------------------------------*/
52static void
53set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
54{
55 const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix();
56 static uip_ipaddr_t root_ipaddr;
57
58 /* Assign a unique local address (RFC4193,
59 http://tools.ietf.org/html/rfc4193). */
60 if(prefix == NULL) {
61 uip_ip6addr_copy(&root_ipaddr, default_prefix);
62 } else {
63 memcpy(&root_ipaddr, prefix, 8);
64 }
65
66 if(iid == NULL) {
67 uip_ds6_set_addr_iid(&root_ipaddr, &uip_lladdr);
68 } else {
69 memcpy((uint8_t *)&root_ipaddr + 8, (uint8_t *)iid + 8, 8);
70 }
71
72 uip_ds6_addr_add(&root_ipaddr, 0, ADDR_AUTOCONF);
73
74 if(LOG_DBG_ENABLED) {
75 LOG_DBG("IPv6 addresses: \n");
76 for(size_t i = 0; i < UIP_DS6_ADDR_NB; i++) {
77 uint8_t state = uip_ds6_if.addr_list[i].state;
78 if(uip_ds6_if.addr_list[i].isused &&
79 (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
80 LOG_DBG(" - ");
81 LOG_DBG_6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
82 LOG_DBG_("\n");
83 }
84 }
85 }
86}
87/*---------------------------------------------------------------------------*/
88void
89rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
90{
91 static uint8_t initialized = 0;
92
93 if(!initialized) {
94 set_global_address(prefix, iid);
95 initialized = 1;
96 }
97}
98/*---------------------------------------------------------------------------*/
99int
100rpl_dag_root_start(void)
101{
102 uip_ipaddr_t *ipaddr = NULL;
103
104 rpl_dag_root_set_prefix(NULL, NULL);
105
106 for(size_t i = 0; i < UIP_DS6_ADDR_NB; i++) {
107 uint8_t state = uip_ds6_if.addr_list[i].state;
108 if(uip_ds6_if.addr_list[i].isused &&
109 state == ADDR_PREFERRED &&
110 !uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr)) {
111 ipaddr = &uip_ds6_if.addr_list[i].ipaddr;
112 }
113 }
114
115 if(ipaddr == NULL) {
116 LOG_ERR("failed to create a DAG: no preferred IP address found\n");
117 return -2;
118 }
119
120 struct uip_ds6_addr *root_if = uip_ds6_addr_lookup(ipaddr);
121 if(root_if == NULL) {
122 LOG_ERR("failed to create a DAG: no root interface found\n");
123 return -1;
124 }
125
126 rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr);
127 rpl_dag_t *dag = rpl_get_any_dag();
128 if(dag == NULL) {
129 LOG_ERR("failed to create a DAG: cannot get any DAG\n");
130 return -3;
131 }
132
133 /* If there are routes in this DAG, we remove them all as we are
134 from now on the new dag root and the old routes are wrong. */
135 if(RPL_IS_STORING(dag->instance)) {
136 rpl_remove_routes(dag);
137 }
138 if(dag->instance != NULL && dag->instance->def_route != NULL) {
139 uip_ds6_defrt_rm(dag->instance->def_route);
140 dag->instance->def_route = NULL;
141 }
142
143 uip_ipaddr_t prefix;
144 uip_ip6addr_copy(&prefix, ipaddr);
145 rpl_set_prefix(dag, &prefix, 64);
146
147 LOG_INFO("created a new RPL dag\n");
148 return 0;
149}
150/*---------------------------------------------------------------------------*/
151int
152rpl_dag_root_is_root(void)
153{
154 rpl_instance_t *instance = rpl_get_default_instance();
155
156 return instance && instance->current_dag &&
157 instance->current_dag->rank == ROOT_RANK(instance);
158}
159/*---------------------------------------------------------------------------*/
160
161/** @}*/
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
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
#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_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
#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
Header file for the logging system.
RPL DAG structure.
Definition rpl.h:138
RPL instance structure.
Definition rpl.h:222
Unicast address structure.
Definition uip-ds6.h:205
Header file for routing table manipulation.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116