Contiki-NG
Loading...
Searching...
No Matches
rpl-dag.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, 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 * \file
32 * Logic for Directed Acyclic Graphs in RPL.
33 *
34 * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
35 * Contributors: George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
36 */
37
38/**
39 * \addtogroup uip
40 * @{
41 */
42
43#include "contiki.h"
44#include "net/link-stats.h"
45#include "net/routing/rpl-classic/rpl.h"
46#include "net/routing/rpl-classic/rpl-private.h"
47#include "net/routing/rpl-classic/rpl-dag-root.h"
48#include "net/ipv6/uip.h"
49#include "net/ipv6/uip-nd6.h"
51#include "net/nbr-table.h"
53#include "lib/list.h"
54#include "lib/memb.h"
55#include "sys/cc.h"
56#include "sys/ctimer.h"
57#include "sys/log.h"
58
59#include <limits.h>
60#include <string.h>
61
62#define LOG_MODULE "RPL"
63#define LOG_LEVEL LOG_LEVEL_RPL
64
65/* A configurable function called after every RPL parent switch. */
66#ifdef RPL_CALLBACK_PARENT_SWITCH
67void RPL_CALLBACK_PARENT_SWITCH(rpl_parent_t *old, rpl_parent_t *new);
68#endif /* RPL_CALLBACK_PARENT_SWITCH */
69
70/*---------------------------------------------------------------------------*/
71extern rpl_of_t rpl_of0, rpl_mrhof;
72static rpl_of_t *const objective_functions[] = RPL_SUPPORTED_OFS;
73
74/*---------------------------------------------------------------------------*/
75/* RPL definitions. */
76
77#ifndef RPL_CONF_GROUNDED
78#define RPL_GROUNDED 0
79#else
80#define RPL_GROUNDED RPL_CONF_GROUNDED
81#endif /* !RPL_CONF_GROUNDED */
82
83/*---------------------------------------------------------------------------*/
84/* Per-parent RPL information */
85NBR_TABLE_GLOBAL(rpl_parent_t, rpl_parents);
86/*---------------------------------------------------------------------------*/
87/* Allocate instance table. */
88rpl_instance_t instance_table[RPL_MAX_INSTANCES];
89rpl_instance_t *default_instance;
90
91/*---------------------------------------------------------------------------*/
92void
93rpl_print_neighbor_list(void)
94{
95 if(default_instance != NULL && default_instance->current_dag != NULL &&
96 default_instance->of != NULL) {
97 int curr_dio_interval = default_instance->dio_intcurrent;
98 int curr_rank = default_instance->current_dag->rank;
99 rpl_parent_t *p = nbr_table_head(rpl_parents);
100 clock_time_t clock_now = clock_time();
101
102 LOG_DBG("RPL: MOP %u OCP %u rank %u dioint %u, nbr count %u\n",
103 default_instance->mop, default_instance->of->ocp, curr_rank, curr_dio_interval, uip_ds6_nbr_num());
104 while(p != NULL) {
105 const struct link_stats *stats = rpl_get_parent_link_stats(p);
106 uip_ipaddr_t *parent_addr = rpl_parent_get_ipaddr(p);
107 LOG_DBG("RPL: nbr %02x %5u, %5u => %5u -- %2u %c%c (last tx %u min ago)\n",
108 parent_addr != NULL ? parent_addr->u8[15] : 0x0,
109 p->rank,
110 rpl_get_parent_link_metric(p),
111 rpl_rank_via_parent(p),
112 stats != NULL ? stats->freshness : 0,
113 link_stats_is_fresh(stats) ? 'f' : ' ',
114 p == default_instance->current_dag->preferred_parent ? 'p' : ' ',
115 stats != NULL ? (unsigned)((clock_now - stats->last_tx_time) / (60 * CLOCK_SECOND)) : -1u
116 );
117 p = nbr_table_next(rpl_parents, p);
118 }
119 LOG_DBG("RPL: end of list\n");
120 }
121}
122/*---------------------------------------------------------------------------*/
124rpl_get_nbr(rpl_parent_t *parent)
125{
126 const linkaddr_t *lladdr = rpl_get_parent_lladdr(parent);
127 if(lladdr != NULL) {
128 return uip_ds6_nbr_ll_lookup((const uip_lladdr_t *)lladdr);
129 }
130
131 return NULL;
132}
133/*---------------------------------------------------------------------------*/
134static void
135nbr_callback(void *ptr)
136{
137 rpl_remove_parent(ptr);
138}
139/*---------------------------------------------------------------------------*/
140void
142{
143 nbr_table_register(rpl_parents, (nbr_table_callback *)nbr_callback);
144}
145/*---------------------------------------------------------------------------*/
146rpl_parent_t *
147rpl_get_parent(const uip_lladdr_t *addr)
148{
149 return nbr_table_get_from_lladdr(rpl_parents, (const linkaddr_t *)addr);
150}
151/*---------------------------------------------------------------------------*/
152rpl_rank_t
153rpl_get_parent_rank(uip_lladdr_t *addr)
154{
155 rpl_parent_t *p = nbr_table_get_from_lladdr(rpl_parents, (linkaddr_t *)addr);
156 if(p != NULL) {
157 return p->rank;
158 }
159
160 return RPL_INFINITE_RANK;
161}
162/*---------------------------------------------------------------------------*/
163uint16_t
164rpl_get_parent_link_metric(rpl_parent_t *p)
165{
166 if(p != NULL && p->dag != NULL) {
167 rpl_instance_t *instance = p->dag->instance;
168 if(instance != NULL && instance->of != NULL &&
169 instance->of->parent_link_metric != NULL) {
170 return instance->of->parent_link_metric(p);
171 }
172 }
173 return 0xffff;
174}
175/*---------------------------------------------------------------------------*/
176rpl_rank_t
177rpl_rank_via_parent(rpl_parent_t *p)
178{
179 if(p != NULL && p->dag != NULL) {
180 rpl_instance_t *instance = p->dag->instance;
181 if(instance != NULL && instance->of != NULL &&
182 instance->of->rank_via_parent != NULL) {
183 return instance->of->rank_via_parent(p);
184 }
185 }
186 return RPL_INFINITE_RANK;
187}
188/*---------------------------------------------------------------------------*/
189const linkaddr_t *
190rpl_get_parent_lladdr(rpl_parent_t *p)
191{
192 return nbr_table_get_lladdr(rpl_parents, p);
193}
194/*---------------------------------------------------------------------------*/
195uip_ipaddr_t *
196rpl_parent_get_ipaddr(rpl_parent_t *p)
197{
198 const linkaddr_t *lladdr = rpl_get_parent_lladdr(p);
199 if(lladdr == NULL) {
200 return NULL;
201 }
202 return uip_ds6_nbr_ipaddr_from_lladdr((uip_lladdr_t *)lladdr);
203}
204/*---------------------------------------------------------------------------*/
205const struct link_stats *
206rpl_get_parent_link_stats(rpl_parent_t *p)
207{
208 const linkaddr_t *lladdr = rpl_get_parent_lladdr(p);
209 return link_stats_from_lladdr(lladdr);
210}
211/*---------------------------------------------------------------------------*/
212int
213rpl_parent_is_fresh(rpl_parent_t *p)
214{
215 const struct link_stats *stats = rpl_get_parent_link_stats(p);
216 return link_stats_is_fresh(stats);
217}
218/*---------------------------------------------------------------------------*/
219int
220rpl_parent_is_reachable(rpl_parent_t *p)
221{
222 if(p == NULL || p->dag == NULL || p->dag->instance == NULL ||
223 p->dag->instance->of == NULL) {
224 return 0;
225 }
226
227#if UIP_ND6_SEND_NS
228 /* Exclude links to a neighbor that is not reachable at a NUD level */
229 if(rpl_get_nbr(p) == NULL) {
230 return 0;
231 }
232#endif /* UIP_ND6_SEND_NS */
233
234 /* If we don't have fresh link information, assume the parent is reachable. */
235 return !rpl_parent_is_fresh(p) ||
236 p->dag->instance->of->parent_has_usable_link(p);
237}
238/*---------------------------------------------------------------------------*/
239static void
240rpl_set_preferred_parent(rpl_dag_t *dag, rpl_parent_t *p)
241{
242 if(dag == NULL || dag->preferred_parent == p) {
243 return;
244 }
245
246 LOG_INFO("rpl_set_preferred_parent ");
247 if(p != NULL) {
248 LOG_INFO_6ADDR(rpl_parent_get_ipaddr(p));
249 } else {
250 LOG_INFO_("NULL");
251 }
252 LOG_INFO_(" used to be ");
253 if(dag->preferred_parent != NULL) {
254 LOG_INFO_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
255 } else {
256 LOG_INFO_("NULL");
257 }
258 LOG_INFO_("\n");
259
260#ifdef RPL_CALLBACK_PARENT_SWITCH
261 RPL_CALLBACK_PARENT_SWITCH(dag->preferred_parent, p);
262#endif /* RPL_CALLBACK_PARENT_SWITCH */
263
264 /* Always keep the preferred parent locked, so it remains in the
265 * neighbor table. */
266 nbr_table_unlock(rpl_parents, dag->preferred_parent);
267 nbr_table_lock(rpl_parents, p);
268 dag->preferred_parent = p;
269}
270/*---------------------------------------------------------------------------*/
271/* Greater-than function for the lollipop counter. */
272/*---------------------------------------------------------------------------*/
273static int
274lollipop_greater_than(int a, int b)
275{
276 /* Check if we are comparing an initial value with an old value. */
277 if(a > RPL_LOLLIPOP_CIRCULAR_REGION && b <= RPL_LOLLIPOP_CIRCULAR_REGION) {
278 return (RPL_LOLLIPOP_MAX_VALUE + 1 + b - a) > RPL_LOLLIPOP_SEQUENCE_WINDOWS;
279 }
280 /* Otherwise check if a > b and comparable => ok, or
281 if they have wrapped and are still comparable. */
282 return (a > b && (a - b) < RPL_LOLLIPOP_SEQUENCE_WINDOWS) ||
283 (a < b && (b - a) > (RPL_LOLLIPOP_CIRCULAR_REGION + 1 - RPL_LOLLIPOP_SEQUENCE_WINDOWS));
284}
285/*---------------------------------------------------------------------------*/
286/* Remove DAG parents with a rank that is at least the same as minimum_rank. */
287static void
288remove_parents(rpl_dag_t *dag, rpl_rank_t minimum_rank)
289{
290 rpl_parent_t *p;
291
292 LOG_INFO("Removing parents (minimum rank %u)\n", minimum_rank);
293
294 p = nbr_table_head(rpl_parents);
295 while(p != NULL) {
296 if(dag == p->dag && p->rank >= minimum_rank) {
297 rpl_remove_parent(p);
298 }
299 p = nbr_table_next(rpl_parents, p);
300 }
301}
302/*---------------------------------------------------------------------------*/
303static void
304nullify_parents(rpl_dag_t *dag, rpl_rank_t minimum_rank)
305{
306 rpl_parent_t *p;
307
308 LOG_INFO("Nullifying parents (minimum rank %u)\n", minimum_rank);
309
310 p = nbr_table_head(rpl_parents);
311 while(p != NULL) {
312 if(dag == p->dag && p->rank >= minimum_rank) {
313 rpl_nullify_parent(p);
314 }
315 p = nbr_table_next(rpl_parents, p);
316 }
317}
318/*---------------------------------------------------------------------------*/
319static int
320should_refresh_routes(rpl_instance_t *instance, rpl_dio_t *dio, rpl_parent_t *p)
321{
322 /* If MOP is set to no downward routes, then no DAO should be sent. */
323 if(instance->mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
324 return 0;
325 }
326
327 /* Check if the new DTSN is more recent. */
328 return p == instance->current_dag->preferred_parent &&
329 (lollipop_greater_than(dio->dtsn, p->dtsn));
330}
331/*---------------------------------------------------------------------------*/
332static int
333acceptable_rank(rpl_dag_t *dag, rpl_rank_t rank)
334{
335 return rank != RPL_INFINITE_RANK &&
336 ((dag->instance->max_rankinc == 0) ||
337 DAG_RANK(rank, dag->instance) <= DAG_RANK(dag->min_rank + dag->instance->max_rankinc, dag->instance));
338}
339/*---------------------------------------------------------------------------*/
340static rpl_dag_t *
341get_dag(uint8_t instance_id, uip_ipaddr_t *dag_id)
342{
343 rpl_instance_t *instance;
344 rpl_dag_t *dag;
345 int i;
346
347 instance = rpl_get_instance(instance_id);
348 if(instance == NULL) {
349 return NULL;
350 }
351
352 for(i = 0; i < RPL_MAX_DAG_PER_INSTANCE; ++i) {
353 dag = &instance->dag_table[i];
354 if(dag->used && uip_ipaddr_cmp(&dag->dag_id, dag_id)) {
355 return dag;
356 }
357 }
358
359 return NULL;
360}
361/*---------------------------------------------------------------------------*/
362rpl_dag_t *
363rpl_set_root(uint8_t instance_id, uip_ipaddr_t *dag_id)
364{
365 rpl_dag_t *dag;
366 rpl_instance_t *instance;
367 uint8_t version;
368 int i;
369
370 version = RPL_LOLLIPOP_INIT;
371 instance = rpl_get_instance(instance_id);
372 if(instance != NULL) {
373 for(i = 0; i < RPL_MAX_DAG_PER_INSTANCE; ++i) {
374 dag = &instance->dag_table[i];
375 if(dag->used) {
376 if(uip_ipaddr_cmp(&dag->dag_id, dag_id)) {
377 version = dag->version;
378 RPL_LOLLIPOP_INCREMENT(version);
379 } else {
380 if(dag == dag->instance->current_dag) {
381 LOG_INFO("Dropping a joined DAG when setting this node as root\n");
382 rpl_set_default_route(instance, NULL);
383 dag->instance->current_dag = NULL;
384 } else {
385 LOG_INFO("Dropping a DAG when setting this node as root\n");
386 }
387 rpl_free_dag(dag);
388 }
389 }
390 }
391 }
392
393 dag = rpl_alloc_dag(instance_id, dag_id);
394 if(dag == NULL) {
395 LOG_ERR("Failed to allocate a DAG\n");
396 return NULL;
397 }
398
399 instance = dag->instance;
400
401 dag->version = version;
402 dag->joined = 1;
403 dag->grounded = RPL_GROUNDED;
404 dag->preference = RPL_PREFERENCE;
405 instance->mop = RPL_MOP_DEFAULT;
406 instance->of = rpl_find_of(RPL_OF_OCP);
407 if(instance->of == NULL) {
408 LOG_WARN("OF with OCP %u not supported\n", RPL_OF_OCP);
409 return NULL;
410 }
411
412 rpl_set_preferred_parent(dag, NULL);
413
414 memcpy(&dag->dag_id, dag_id, sizeof(dag->dag_id));
415
416 instance->dio_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
417 instance->dio_intmin = RPL_DIO_INTERVAL_MIN;
418 /* The current interval must differ from the minimum interval in order to
419 trigger a DIO timer reset. */
420 instance->dio_intcurrent = RPL_DIO_INTERVAL_MIN +
421 RPL_DIO_INTERVAL_DOUBLINGS;
422 instance->dio_redundancy = RPL_DIO_REDUNDANCY;
423 instance->max_rankinc = RPL_MAX_RANKINC;
424 instance->min_hoprankinc = RPL_MIN_HOPRANKINC;
425 instance->default_lifetime = RPL_DEFAULT_LIFETIME;
426 instance->lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
427
428 dag->rank = ROOT_RANK(instance);
429
430 if(instance->current_dag != dag && instance->current_dag != NULL) {
431 /* Remove routes installed by DAOs. */
432 if(RPL_IS_STORING(instance)) {
433 rpl_remove_routes(instance->current_dag);
434 }
435
436 instance->current_dag->joined = 0;
437 }
438
439 instance->current_dag = dag;
440 instance->dtsn_out = RPL_LOLLIPOP_INIT;
441 instance->of->update_metric_container(instance);
442 default_instance = instance;
443
444 LOG_INFO("Node set to be a DAG root with DAG ID ");
445 LOG_INFO_6ADDR(&dag->dag_id);
446 LOG_INFO_("\n");
447
448 LOG_ANNOTATE("#A root=%u\n", dag->dag_id.u8[sizeof(dag->dag_id) - 1]);
449
450 rpl_reset_dio_timer(instance);
451
452 return dag;
453}
454/*---------------------------------------------------------------------------*/
455int
456rpl_repair_root(uint8_t instance_id)
457{
458 rpl_instance_t *instance;
459
460 instance = rpl_get_instance(instance_id);
461 if(instance == NULL ||
462 instance->current_dag->rank != ROOT_RANK(instance)) {
463 LOG_WARN("rpl_repair_root triggered but not root\n");
464 return 0;
465 }
466 RPL_STAT(rpl_stats.root_repairs++);
467
468 RPL_LOLLIPOP_INCREMENT(instance->current_dag->version);
469 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
470 LOG_INFO("rpl_repair_root initiating global repair with version %d\n",
471 instance->current_dag->version);
472 rpl_reset_dio_timer(instance);
473 return 1;
474}
475/*---------------------------------------------------------------------------*/
476static void
477set_ip_from_prefix(uip_ipaddr_t *ipaddr, rpl_prefix_t *prefix)
478{
479 memset(ipaddr, 0, sizeof(uip_ipaddr_t));
480 memcpy(ipaddr, &prefix->prefix, (prefix->length + 7) / 8);
482}
483/*---------------------------------------------------------------------------*/
484static void
485check_prefix(rpl_prefix_t *last_prefix, rpl_prefix_t *new_prefix)
486{
487 uip_ipaddr_t ipaddr;
488 uip_ds6_addr_t *rep;
489
490 if(last_prefix != NULL && new_prefix != NULL &&
491 last_prefix->length == new_prefix->length &&
492 uip_ipaddr_prefixcmp(&last_prefix->prefix,
493 &new_prefix->prefix, new_prefix->length) &&
494 last_prefix->flags == new_prefix->flags) {
495 /* Nothing has changed. */
496 return;
497 }
498
499 if(last_prefix != NULL) {
500 set_ip_from_prefix(&ipaddr, last_prefix);
501 rep = uip_ds6_addr_lookup(&ipaddr);
502 if(rep != NULL) {
503 LOG_DBG("removing global IP address ");
504 LOG_DBG_6ADDR(&ipaddr);
505 LOG_DBG_("\n");
506 uip_ds6_addr_rm(rep);
507 }
508 }
509
510 if(new_prefix != NULL) {
511 set_ip_from_prefix(&ipaddr, new_prefix);
512 if(uip_ds6_addr_lookup(&ipaddr) == NULL) {
513 LOG_DBG("adding global IP address ");
514 LOG_DBG_6ADDR(&ipaddr);
515 LOG_DBG_("\n");
516 uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
517 }
518 }
519}
520/*---------------------------------------------------------------------------*/
521int
522rpl_set_prefix(rpl_dag_t *dag, uip_ipaddr_t *prefix, unsigned len)
523{
524 rpl_prefix_t last_prefix;
525 uint8_t last_len = dag->prefix_info.length;
526
527 if(len > 128) {
528 return 0;
529 }
530 if(dag->prefix_info.length != 0) {
531 memcpy(&last_prefix, &dag->prefix_info, sizeof(rpl_prefix_t));
532 }
533 memset(&dag->prefix_info.prefix, 0, sizeof(dag->prefix_info.prefix));
534 memcpy(&dag->prefix_info.prefix, prefix, (len + 7) / 8);
535 dag->prefix_info.length = len;
536 dag->prefix_info.flags = UIP_ND6_RA_FLAG_AUTONOMOUS;
537 dag->prefix_info.lifetime = RPL_ROUTE_INFINITE_LIFETIME;
538 LOG_INFO("Prefix set - will announce this in DIOs\n");
539 if(dag->rank != ROOT_RANK(dag->instance)) {
540 /* Autoconfigure an address if this node does not already have an address
541 with this prefix. Otherwise, update the prefix. */
542 if(last_len == 0) {
543 LOG_INFO("rpl_set_prefix - prefix NULL\n");
544 check_prefix(NULL, &dag->prefix_info);
545 } else {
546 LOG_INFO("rpl_set_prefix - prefix NON-NULL\n");
547 check_prefix(&last_prefix, &dag->prefix_info);
548 }
549 }
550 return 1;
551}
552/*---------------------------------------------------------------------------*/
553int
554rpl_set_default_route(rpl_instance_t *instance, uip_ipaddr_t *from)
555{
556 if(instance->def_route != NULL) {
557 LOG_DBG("Removing default route through ");
558 LOG_DBG_6ADDR(&instance->def_route->ipaddr);
559 LOG_DBG_("\n");
560 uip_ds6_defrt_rm(instance->def_route);
561 instance->def_route = NULL;
562 }
563
564 if(from != NULL) {
565 LOG_DBG("Adding default route through ");
566 LOG_DBG_6ADDR(from);
567 LOG_DBG_("\n");
568 instance->def_route = uip_ds6_defrt_add(from,
569 RPL_DEFAULT_ROUTE_INFINITE_LIFETIME ? 0 : RPL_LIFETIME(instance, instance->default_lifetime));
570 if(instance->def_route == NULL) {
571 return 0;
572 }
573 }
574 return 1;
575}
576/*---------------------------------------------------------------------------*/
577rpl_instance_t *
578rpl_alloc_instance(uint8_t instance_id)
579{
580 rpl_instance_t *instance, *end;
581
582 for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
583 instance < end; ++instance) {
584 if(instance->used == 0) {
585 memset(instance, 0, sizeof(*instance));
586 instance->instance_id = instance_id;
587 instance->def_route = NULL;
588 instance->used = 1;
589#if RPL_WITH_PROBING
590 rpl_schedule_probing(instance);
591#endif /* RPL_WITH_PROBING */
592 return instance;
593 }
594 }
595 return NULL;
596}
597/*---------------------------------------------------------------------------*/
598rpl_dag_t *
599rpl_alloc_dag(uint8_t instance_id, uip_ipaddr_t *dag_id)
600{
601 rpl_dag_t *dag, *end;
602 rpl_instance_t *instance;
603
604 instance = rpl_get_instance(instance_id);
605 if(instance == NULL) {
606 instance = rpl_alloc_instance(instance_id);
607 if(instance == NULL) {
608 RPL_STAT(rpl_stats.mem_overflows++);
609 return NULL;
610 }
611 }
612
613 for(dag = &instance->dag_table[0], end = dag + RPL_MAX_DAG_PER_INSTANCE; dag < end; ++dag) {
614 if(!dag->used) {
615 memset(dag, 0, sizeof(*dag));
616 dag->used = 1;
617 dag->rank = RPL_INFINITE_RANK;
618 dag->min_rank = RPL_INFINITE_RANK;
619 dag->instance = instance;
620 return dag;
621 }
622 }
623
624 RPL_STAT(rpl_stats.mem_overflows++);
625 return NULL;
626}
627/*---------------------------------------------------------------------------*/
628void
629rpl_set_default_instance(rpl_instance_t *instance)
630{
631 default_instance = instance;
632}
633/*---------------------------------------------------------------------------*/
634rpl_instance_t *
636{
637 return default_instance;
638}
639/*---------------------------------------------------------------------------*/
640void
641rpl_free_instance(rpl_instance_t *instance)
642{
643 rpl_dag_t *dag;
644 rpl_dag_t *end;
645
646 LOG_INFO("Leaving the instance %u\n", instance->instance_id);
647
648 /* Remove any DAG inside this instance */
649 for(dag = &instance->dag_table[0], end = dag + RPL_MAX_DAG_PER_INSTANCE;
650 dag < end;
651 ++dag) {
652 if(dag->used) {
653 rpl_free_dag(dag);
654 }
655 }
656
657 rpl_set_default_route(instance, NULL);
658
659#if RPL_WITH_PROBING
660 ctimer_stop(&instance->probing_timer);
661#endif /* RPL_WITH_PROBING */
662 ctimer_stop(&instance->dio_timer);
663 ctimer_stop(&instance->dao_timer);
664 ctimer_stop(&instance->dao_lifetime_timer);
665
666 if(default_instance == instance) {
667 default_instance = NULL;
668 }
669
670 instance->used = 0;
671}
672/*---------------------------------------------------------------------------*/
673void
674rpl_free_dag(rpl_dag_t *dag)
675{
676 if(dag->joined) {
677 LOG_INFO("Leaving the DAG ");
678 LOG_INFO_6ADDR(&dag->dag_id);
679 LOG_INFO_("\n");
680 dag->joined = 0;
681
682 /* Remove routes installed by DAOs. */
683 if(RPL_IS_STORING(dag->instance)) {
684 rpl_remove_routes(dag);
685 }
686 /* Stop the DAO retransmit timer. */
687#if RPL_WITH_DAO_ACK
688 ctimer_stop(&dag->instance->dao_retransmit_timer);
689#endif /* RPL_WITH_DAO_ACK */
690
691 /* Remove autoconfigured address. */
692 if((dag->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS)) {
693 check_prefix(&dag->prefix_info, NULL);
694 }
695
696 remove_parents(dag, 0);
697 }
698 dag->used = 0;
699}
700/*---------------------------------------------------------------------------*/
701rpl_parent_t *
702rpl_add_parent(rpl_dag_t *dag, rpl_dio_t *dio, uip_ipaddr_t *addr)
703{
704 rpl_parent_t *p = NULL;
705 /* Is the parent known by DS6? Drop this request if not. Typically,
706 the parent is added upon receiving a DIO. */
707 const uip_lladdr_t *lladdr = uip_ds6_nbr_lladdr_from_ipaddr(addr);
708
709 LOG_DBG("rpl_add_parent lladdr %p ", lladdr);
710 LOG_DBG_6ADDR(addr);
711 LOG_DBG_("\n");
712 if(lladdr != NULL) {
713 /* Add the parent in rpl_parents -- again this is due to DIO. */
714 p = nbr_table_add_lladdr(rpl_parents, (linkaddr_t *)lladdr,
715 NBR_TABLE_REASON_RPL_DIO, dio);
716 if(p == NULL) {
717 LOG_DBG("rpl_add_parent p NULL\n");
718 } else {
719 p->dag = dag;
720 p->rank = dio->rank;
721 p->dtsn = dio->dtsn;
722#if RPL_WITH_MC
723 memcpy(&p->mc, &dio->mc, sizeof(p->mc));
724#endif /* RPL_WITH_MC */
725 }
726 }
727
728 return p;
729}
730/*---------------------------------------------------------------------------*/
731static rpl_parent_t *
732find_parent_any_dag_any_instance(uip_ipaddr_t *addr)
733{
735 const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(ds6_nbr);
736 return nbr_table_get_from_lladdr(rpl_parents, (linkaddr_t *)lladdr);
737}
738/*---------------------------------------------------------------------------*/
739rpl_parent_t *
740rpl_find_parent(rpl_dag_t *dag, uip_ipaddr_t *addr)
741{
742 rpl_parent_t *p = find_parent_any_dag_any_instance(addr);
743 if(p != NULL && p->dag == dag) {
744 return p;
745 }
746
747 return NULL;
748}
749/*---------------------------------------------------------------------------*/
750static rpl_dag_t *
751find_parent_dag(rpl_instance_t *instance, uip_ipaddr_t *addr)
752{
753 rpl_parent_t *p = find_parent_any_dag_any_instance(addr);
754 if(p != NULL) {
755 return p->dag;
756 }
757
758 return NULL;
759}
760/*---------------------------------------------------------------------------*/
761rpl_parent_t *
762rpl_find_parent_any_dag(rpl_instance_t *instance, uip_ipaddr_t *addr)
763{
764 rpl_parent_t *p = find_parent_any_dag_any_instance(addr);
765 if(p && p->dag && p->dag->instance == instance) {
766 return p;
767 }
768
769 return NULL;
770}
771/*---------------------------------------------------------------------------*/
772rpl_dag_t *
773rpl_select_dag(rpl_instance_t *instance, rpl_parent_t *p)
774{
775 rpl_parent_t *last_parent;
776 rpl_dag_t *dag, *end, *best_dag;
777 rpl_rank_t old_rank;
778
779 old_rank = instance->current_dag->rank;
780 last_parent = instance->current_dag->preferred_parent;
781
782 if(instance->current_dag->rank != ROOT_RANK(instance)) {
783 rpl_select_parent(p->dag);
784 }
785
786 best_dag = NULL;
787 for(dag = &instance->dag_table[0], end = dag + RPL_MAX_DAG_PER_INSTANCE;
788 dag < end;
789 ++dag) {
790 if(dag->used && dag->preferred_parent != NULL &&
791 dag->preferred_parent->rank != RPL_INFINITE_RANK) {
792 if(best_dag == NULL) {
793 best_dag = dag;
794 } else {
795 best_dag = instance->of->best_dag(best_dag, dag);
796 }
797 }
798 }
799
800 if(best_dag == NULL) {
801 /* No parent found: the calling function needs to handle this problem. */
802 return NULL;
803 }
804
805 if(instance->current_dag != best_dag) {
806 /* Remove routes installed by DAOs. */
807 if(RPL_IS_STORING(instance)) {
808 rpl_remove_routes(instance->current_dag);
809 }
810
811 LOG_INFO("New preferred DAG: ");
812 LOG_INFO_6ADDR(&best_dag->dag_id);
813 LOG_INFO_("\n");
814
815 if(best_dag->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS) {
816 check_prefix(&instance->current_dag->prefix_info, &best_dag->prefix_info);
817 } else if(instance->current_dag->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS) {
818 check_prefix(&instance->current_dag->prefix_info, NULL);
819 }
820
821 best_dag->joined = 1;
822 instance->current_dag->joined = 0;
823 instance->current_dag = best_dag;
824 }
825
826 instance->of->update_metric_container(instance);
827 /* Update the DAG rank. */
828 best_dag->rank = rpl_rank_via_parent(best_dag->preferred_parent);
829 if(last_parent == NULL || best_dag->rank < best_dag->min_rank) {
830 /*
831 * This is a slight departure from RFC6550: if we had no preferred
832 * parent before, reset min_rank. This helps recovering from
833 * temporary bad link conditions.
834 */
835 best_dag->min_rank = best_dag->rank;
836 }
837
838 if(!acceptable_rank(best_dag, best_dag->rank)) {
839 LOG_WARN("New rank unacceptable!\n");
840 rpl_set_preferred_parent(instance->current_dag, NULL);
841 if(RPL_IS_STORING(instance) && last_parent != NULL) {
842 /* Send a No-Path DAO to the removed preferred parent. */
843 dao_output(last_parent, RPL_ZERO_LIFETIME);
844 }
845 return NULL;
846 }
847
848 if(best_dag->preferred_parent != last_parent) {
849 rpl_set_default_route(instance, rpl_parent_get_ipaddr(best_dag->preferred_parent));
850 LOG_INFO("Changed preferred parent, rank changed from %u to %u\n",
851 (unsigned)old_rank, best_dag->rank);
852 RPL_STAT(rpl_stats.parent_switch++);
853 if(RPL_IS_STORING(instance)) {
854 if(last_parent != NULL) {
855 /* Send a No-Path DAO to the removed preferred parent. */
856 dao_output(last_parent, RPL_ZERO_LIFETIME);
857 }
858 /* Trigger DAO transmission from immediate children.
859 * Only for storing mode, see RFC6550 section 9.6. */
860 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
861 }
862 /* The DAO parent set changed -- schedule a DAO transmission. If
863 MOP = MOP0, we do not want downward routes. */
864 if(instance->mop != RPL_MOP_NO_DOWNWARD_ROUTES) {
865 rpl_schedule_dao(instance);
866 }
867
868 rpl_reset_dio_timer(instance);
869 if(LOG_DBG_ENABLED) {
870 rpl_print_neighbor_list();
871 }
872 } else if(best_dag->rank != old_rank) {
873 LOG_DBG("Preferred parent update, rank changed from %u to %u\n",
874 (unsigned)old_rank, best_dag->rank);
875 }
876 return best_dag;
877}
878/*---------------------------------------------------------------------------*/
879static rpl_parent_t *
880best_parent(rpl_dag_t *dag, int fresh_only)
881{
882 rpl_parent_t *p;
883 rpl_of_t *of;
884 rpl_parent_t *best = NULL;
885
886 if(dag == NULL || dag->instance == NULL || dag->instance->of == NULL) {
887 return NULL;
888 }
889
890 of = dag->instance->of;
891 /* Search for the best parent according to the OF */
892 for(p = nbr_table_head(rpl_parents); p != NULL; p = nbr_table_next(rpl_parents, p)) {
893
894 /* Exclude parents that are from other DAGs or are announcing an
895 infinite rank. */
896 if(p->dag != dag || p->rank == RPL_INFINITE_RANK ||
897 p->rank < ROOT_RANK(dag->instance)) {
898 if(p->rank < ROOT_RANK(dag->instance)) {
899 LOG_WARN("Parent has invalid rank\n");
900 }
901 continue;
902 }
903
904 if(fresh_only && !rpl_parent_is_fresh(p)) {
905 /* Filter out non-fresh parents if fresh_only is set. */
906 continue;
907 }
908
909#if UIP_ND6_SEND_NS
910 /* Exclude links to a neighbor that is not reachable at a NUD level. */
911 if(rpl_get_nbr(p) == NULL) {
912 continue;
913 }
914#endif /* UIP_ND6_SEND_NS */
915
916 /* Now we have an acceptable parent, check if it is the new best. */
917 best = of->best_parent(best, p);
918 }
919
920 return best;
921}
922/*---------------------------------------------------------------------------*/
923rpl_parent_t *
924rpl_select_parent(rpl_dag_t *dag)
925{
926 /* Look for best parent (regardless of freshness) */
927 rpl_parent_t *best = best_parent(dag, 0);
928
929 if(best != NULL) {
930#if RPL_WITH_PROBING
931 if(rpl_parent_is_fresh(best)) {
932 rpl_set_preferred_parent(dag, best);
933 /* Unschedule any already scheduled urgent probing. */
934 dag->instance->urgent_probing_target = NULL;
935 } else {
936 /* The best is not fresh. Look for the best fresh now. */
937 rpl_parent_t *best_fresh = best_parent(dag, 1);
938 if(best_fresh == NULL) {
939 /* No fresh parent around, use best (non-fresh). */
940 rpl_set_preferred_parent(dag, best);
941 } else {
942 /* Use best fresh */
943 rpl_set_preferred_parent(dag, best_fresh);
944 }
945 /* Probe the best parent shortly in order to get a fresh estimate. */
946 dag->instance->urgent_probing_target = best;
947 rpl_schedule_probing_now(dag->instance);
948 }
949#else /* RPL_WITH_PROBING */
950 rpl_set_preferred_parent(dag, best);
951 dag->rank = rpl_rank_via_parent(dag->preferred_parent);
952#endif /* RPL_WITH_PROBING */
953 } else {
954 rpl_set_preferred_parent(dag, NULL);
955 }
956
957 dag->rank = rpl_rank_via_parent(dag->preferred_parent);
958 return dag->preferred_parent;
959}
960/*---------------------------------------------------------------------------*/
961void
962rpl_remove_parent(rpl_parent_t *parent)
963{
964 LOG_INFO("Removing parent ");
965 LOG_INFO_6ADDR(rpl_parent_get_ipaddr(parent));
966 LOG_INFO_("\n");
967
968 rpl_nullify_parent(parent);
969
970 nbr_table_remove(rpl_parents, parent);
971}
972/*---------------------------------------------------------------------------*/
973void
974rpl_nullify_parent(rpl_parent_t *parent)
975{
976 rpl_dag_t *dag = parent->dag;
977 /*
978 * This function can be called when the preferred parent is NULL, so
979 * we need to handle this condition in order to trigger uip_ds6_defrt_rm.
980 */
981 if(parent == dag->preferred_parent || dag->preferred_parent == NULL) {
982 dag->rank = RPL_INFINITE_RANK;
983 if(dag->joined) {
984 if(dag->instance->def_route != NULL) {
985 LOG_DBG("Removing default route ");
986 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(parent));
987 LOG_DBG_("\n");
988 uip_ds6_defrt_rm(dag->instance->def_route);
989 dag->instance->def_route = NULL;
990 }
991 /* Send a No-Path DAO only when nullifying preferred parent. */
992 if(parent == dag->preferred_parent) {
993 if(RPL_IS_STORING(dag->instance)) {
994 dao_output(parent, RPL_ZERO_LIFETIME);
995 }
996 rpl_set_preferred_parent(dag, NULL);
997 }
998 }
999 }
1000
1001 LOG_INFO("Nullifying parent ");
1002 LOG_INFO_6ADDR(rpl_parent_get_ipaddr(parent));
1003 LOG_INFO_("\n");
1004}
1005/*---------------------------------------------------------------------------*/
1006void
1007rpl_move_parent(rpl_dag_t *dag_src, rpl_dag_t *dag_dst, rpl_parent_t *parent)
1008{
1009 if(parent == dag_src->preferred_parent) {
1010 rpl_set_preferred_parent(dag_src, NULL);
1011 dag_src->rank = RPL_INFINITE_RANK;
1012 if(dag_src->joined && dag_src->instance->def_route != NULL) {
1013 LOG_DBG("Removing default route ");
1014 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(parent));
1015 LOG_DBG_("\n");
1016 LOG_DBG("rpl_move_parent\n");
1017 uip_ds6_defrt_rm(dag_src->instance->def_route);
1018 dag_src->instance->def_route = NULL;
1019 }
1020 } else if(dag_src->joined) {
1021 if(RPL_IS_STORING(dag_src->instance)) {
1022 /* Remove uIPv6 routes that have this parent as the next hop. */
1023 rpl_remove_routes_by_nexthop(rpl_parent_get_ipaddr(parent), dag_src);
1024 }
1025 }
1026
1027 LOG_INFO("Moving parent ");
1028 LOG_INFO_6ADDR(rpl_parent_get_ipaddr(parent));
1029 LOG_INFO_("\n");
1030
1031 parent->dag = dag_dst;
1032}
1033/*---------------------------------------------------------------------------*/
1034static rpl_dag_t *
1035rpl_get_any_dag_with_parent(bool requires_parent)
1036{
1037 int i;
1038
1039 for(i = 0; i < RPL_MAX_INSTANCES; ++i) {
1040 if(instance_table[i].used
1041 && instance_table[i].current_dag->joined
1042 && (!requires_parent || instance_table[i].current_dag->preferred_parent != NULL)) {
1043 return instance_table[i].current_dag;
1044 }
1045 }
1046 return NULL;
1047}
1048/*---------------------------------------------------------------------------*/
1049int
1051{
1052 if(rpl_dag_root_is_root()) {
1053 return 1;
1054 }
1055 return rpl_get_any_dag_with_parent(true) != NULL;
1056}
1057/*---------------------------------------------------------------------------*/
1058int
1060{
1061 int i;
1062 if(rpl_dag_root_is_root()) {
1063 return 1; /* We are the root, and know the route to ourself */
1064 }
1065 for(i = 0; i < RPL_MAX_INSTANCES; ++i) {
1066 if(instance_table[i].used && instance_table[i].has_downward_route) {
1067 return 1;
1068 }
1069 }
1070 return 0;
1071}
1072/*---------------------------------------------------------------------------*/
1073rpl_dag_t *
1074rpl_get_dag(const uip_ipaddr_t *addr)
1075{
1076 for(int i = 0; i < RPL_MAX_INSTANCES; ++i) {
1077 if(instance_table[i].used) {
1078 for(int j = 0; j < RPL_MAX_DAG_PER_INSTANCE; ++j) {
1079 if(instance_table[i].dag_table[j].joined
1080 && uip_ipaddr_prefixcmp(&instance_table[i].dag_table[j].dag_id, addr,
1081 instance_table[i].dag_table[j].prefix_info.length)) {
1082 return &instance_table[i].dag_table[j];
1083 }
1084 }
1085 }
1086 }
1087 return NULL;
1088}
1089/*---------------------------------------------------------------------------*/
1090rpl_dag_t *
1092{
1093 return rpl_get_any_dag_with_parent(false);
1094}
1095/*---------------------------------------------------------------------------*/
1096rpl_instance_t *
1097rpl_get_instance(uint8_t instance_id)
1098{
1099 int i;
1100
1101 for(i = 0; i < RPL_MAX_INSTANCES; ++i) {
1102 if(instance_table[i].used && instance_table[i].instance_id == instance_id) {
1103 return &instance_table[i];
1104 }
1105 }
1106 return NULL;
1107}
1108/*---------------------------------------------------------------------------*/
1109rpl_of_t *
1110rpl_find_of(rpl_ocp_t ocp)
1111{
1112 for(unsigned i = 0; i < CC_ARRAY_LENGTH(objective_functions); i++) {
1113 if(objective_functions[i]->ocp == ocp) {
1114 return objective_functions[i];
1115 }
1116 }
1117
1118 return NULL;
1119}
1120/*---------------------------------------------------------------------------*/
1121void
1122rpl_join_instance(uip_ipaddr_t *from, rpl_dio_t *dio)
1123{
1124 rpl_instance_t *instance;
1125 rpl_dag_t *dag;
1126 rpl_parent_t *p;
1127 rpl_of_t *of;
1128
1129 if((!RPL_WITH_NON_STORING && dio->mop == RPL_MOP_NON_STORING)
1130 || (!RPL_WITH_STORING && (dio->mop == RPL_MOP_STORING_NO_MULTICAST
1131 || dio->mop == RPL_MOP_STORING_MULTICAST))) {
1132 LOG_WARN("DIO advertising a non-supported MOP %u\n", dio->mop);
1133 return;
1134 }
1135
1136 /* Determine the objective function by using the objective code
1137 point of the DIO. */
1138 of = rpl_find_of(dio->ocp);
1139 if(of == NULL) {
1140 LOG_WARN("DIO for DAG instance %u does not specify a supported OF: %u\n",
1141 dio->instance_id, dio->ocp);
1142 return;
1143 }
1144
1145 dag = rpl_alloc_dag(dio->instance_id, &dio->dag_id);
1146 if(dag == NULL) {
1147 LOG_ERR("Failed to allocate a DAG object!\n");
1148 return;
1149 }
1150
1151 instance = dag->instance;
1152
1153 p = rpl_add_parent(dag, dio, from);
1154 LOG_DBG("Adding ");
1155 LOG_DBG_6ADDR(from);
1156 LOG_DBG_(" as a parent: ");
1157 if(p == NULL) {
1158 LOG_DBG_("failed\n");
1159 instance->used = 0;
1160 return;
1161 }
1162 p->dtsn = dio->dtsn;
1163 LOG_DBG_("succeeded\n");
1164
1165 /* Autoconfigure an address if this node does not already have an
1166 address with this prefix. */
1167 if(dio->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS) {
1168 check_prefix(NULL, &dio->prefix_info);
1169 }
1170
1171 dag->joined = 1;
1172 dag->preference = dio->preference;
1173 dag->grounded = dio->grounded;
1174 dag->version = dio->version;
1175
1176 instance->of = of;
1177 instance->mop = dio->mop;
1178 instance->mc.type = dio->mc.type;
1179 instance->mc.flags = dio->mc.flags;
1180 instance->mc.aggr = dio->mc.aggr;
1181 instance->mc.prec = dio->mc.prec;
1182 instance->current_dag = dag;
1183 instance->dtsn_out = RPL_LOLLIPOP_INIT;
1184
1185 instance->max_rankinc = dio->dag_max_rankinc;
1186 instance->min_hoprankinc = dio->dag_min_hoprankinc;
1187 instance->dio_intdoubl = dio->dag_intdoubl;
1188 instance->dio_intmin = dio->dag_intmin;
1189 instance->dio_intcurrent = instance->dio_intmin + instance->dio_intdoubl;
1190 instance->dio_redundancy = dio->dag_redund;
1191 instance->default_lifetime = dio->default_lifetime;
1192 instance->lifetime_unit = dio->lifetime_unit;
1193
1194 memcpy(&dag->dag_id, &dio->dag_id, sizeof(dio->dag_id));
1195
1196 /* Copy prefix information from the DIO into the DAG object. */
1197 memcpy(&dag->prefix_info, &dio->prefix_info, sizeof(rpl_prefix_t));
1198
1199 rpl_set_preferred_parent(dag, p);
1200 instance->of->update_metric_container(instance);
1201 dag->rank = rpl_rank_via_parent(p);
1202 /* So far this is the lowest rank we are aware of. */
1203 dag->min_rank = dag->rank;
1204
1205 if(default_instance == NULL) {
1206 default_instance = instance;
1207 }
1208
1209 LOG_INFO("Joined DAG with instance ID %u, rank %hu, DAG ID ",
1210 dio->instance_id, dag->rank);
1211 LOG_INFO_6ADDR(&dag->dag_id);
1212 LOG_INFO_("\n");
1213
1214 LOG_ANNOTATE("#A join=%u\n", dag->dag_id.u8[sizeof(dag->dag_id) - 1]);
1215
1216 rpl_reset_dio_timer(instance);
1217 rpl_set_default_route(instance, from);
1218
1219 if(instance->mop != RPL_MOP_NO_DOWNWARD_ROUTES) {
1220 rpl_schedule_dao(instance);
1221 } else {
1222 LOG_WARN("The DIO does not meet the prerequisites for sending a DAO\n");
1223 }
1224
1225 instance->of->reset(dag);
1226}
1227#if RPL_MAX_DAG_PER_INSTANCE > 1
1228/*---------------------------------------------------------------------------*/
1229rpl_dag_t *
1230rpl_add_dag(uip_ipaddr_t *from, rpl_dio_t *dio)
1231{
1232 rpl_instance_t *instance;
1233 rpl_dag_t *dag, *previous_dag;
1234 rpl_parent_t *p;
1235 rpl_of_t *of;
1236
1237 dag = rpl_alloc_dag(dio->instance_id, &dio->dag_id);
1238 if(dag == NULL) {
1239 LOG_ERR("Failed to allocate a DAG object!\n");
1240 return NULL;
1241 }
1242
1243 instance = dag->instance;
1244
1245 previous_dag = find_parent_dag(instance, from);
1246 if(previous_dag == NULL) {
1247 LOG_DBG("Adding ");
1248 LOG_DBG_6ADDR(from);
1249 LOG_DBG_(" as a parent: ");
1250 p = rpl_add_parent(dag, dio, from);
1251 if(p == NULL) {
1252 LOG_DBG_("failed\n");
1253 dag->used = 0;
1254 return NULL;
1255 }
1256 LOG_DBG_("succeeded\n");
1257 } else {
1258 p = rpl_find_parent(previous_dag, from);
1259 if(p != NULL) {
1260 rpl_move_parent(previous_dag, dag, p);
1261 }
1262 }
1263 p->rank = dio->rank;
1264
1265 /* Determine the objective function by using the objective code
1266 point of the DIO. */
1267 of = rpl_find_of(dio->ocp);
1268 if(of != instance->of ||
1269 instance->mop != dio->mop ||
1270 instance->max_rankinc != dio->dag_max_rankinc ||
1271 instance->min_hoprankinc != dio->dag_min_hoprankinc ||
1272 instance->dio_intdoubl != dio->dag_intdoubl ||
1273 instance->dio_intmin != dio->dag_intmin ||
1274 instance->dio_redundancy != dio->dag_redund ||
1275 instance->default_lifetime != dio->default_lifetime ||
1276 instance->lifetime_unit != dio->lifetime_unit) {
1277 LOG_WARN("DIO for DAG instance %u incompatible with previous DIO\n",
1278 dio->instance_id);
1279 rpl_remove_parent(p);
1280 dag->used = 0;
1281 return NULL;
1282 }
1283
1284 dag->used = 1;
1285 dag->grounded = dio->grounded;
1286 dag->preference = dio->preference;
1287 dag->version = dio->version;
1288
1289 memcpy(&dag->dag_id, &dio->dag_id, sizeof(dio->dag_id));
1290
1291 /* copy prefix information into the DAG. */
1292 memcpy(&dag->prefix_info, &dio->prefix_info, sizeof(rpl_prefix_t));
1293
1294 rpl_set_preferred_parent(dag, p);
1295 dag->rank = rpl_rank_via_parent(p);
1296 dag->min_rank = dag->rank; /* So far this is the lowest rank we know of. */
1297
1298 LOG_INFO("Joined DAG with instance ID %u, rank %hu, DAG ID ",
1299 dio->instance_id, dag->rank);
1300 LOG_INFO_6ADDR(&dag->dag_id);
1301 LOG_INFO_("\n");
1302
1303 LOG_ANNOTATE("#A join=%u\n", dag->dag_id.u8[sizeof(dag->dag_id) - 1]);
1304
1305 rpl_process_parent_event(instance, p);
1306 p->dtsn = dio->dtsn;
1307
1308 return dag;
1309}
1310#endif /* RPL_MAX_DAG_PER_INSTANCE > 1 */
1311
1312/*---------------------------------------------------------------------------*/
1313static void
1314global_repair(uip_ipaddr_t *from, rpl_dag_t *dag, rpl_dio_t *dio)
1315{
1316 rpl_parent_t *p;
1317
1318 remove_parents(dag, 0);
1319 dag->version = dio->version;
1320
1321 /* Copy parts of the configuration so that it propagates in the network. */
1322 dag->instance->dio_intdoubl = dio->dag_intdoubl;
1323 dag->instance->dio_intmin = dio->dag_intmin;
1324 dag->instance->dio_redundancy = dio->dag_redund;
1325 dag->instance->default_lifetime = dio->default_lifetime;
1326 dag->instance->lifetime_unit = dio->lifetime_unit;
1327
1328 dag->instance->of->reset(dag);
1329 dag->min_rank = RPL_INFINITE_RANK;
1330 RPL_LOLLIPOP_INCREMENT(dag->instance->dtsn_out);
1331
1332 p = rpl_add_parent(dag, dio, from);
1333 if(p == NULL) {
1334 LOG_ERR("Failed to add a parent during the global repair\n");
1335 dag->rank = RPL_INFINITE_RANK;
1336 } else {
1337 dag->rank = rpl_rank_via_parent(p);
1338 dag->min_rank = dag->rank;
1339 LOG_DBG("rpl_process_parent_event global repair\n");
1340 rpl_process_parent_event(dag->instance, p);
1341 }
1342
1343 LOG_DBG("Participating in a global repair (version=%u, rank=%hu)\n",
1344 dag->version, dag->rank);
1345
1346 RPL_STAT(rpl_stats.global_repairs++);
1347}
1348/*---------------------------------------------------------------------------*/
1349void
1350rpl_local_repair(rpl_instance_t *instance)
1351{
1352 int i;
1353
1354 if(instance == NULL) {
1355 LOG_WARN("local repair requested for instance NULL\n");
1356 return;
1357 }
1358 LOG_INFO("Starting a local instance repair\n");
1359 for(i = 0; i < RPL_MAX_DAG_PER_INSTANCE; i++) {
1360 if(instance->dag_table[i].used) {
1361 instance->dag_table[i].rank = RPL_INFINITE_RANK;
1362 nullify_parents(&instance->dag_table[i], 0);
1363 }
1364 }
1365
1366 /* No downward route anymore. */
1367 instance->has_downward_route = 0;
1368#if RPL_WITH_DAO_ACK
1369 ctimer_stop(&instance->dao_retransmit_timer);
1370#endif /* RPL_WITH_DAO_ACK */
1371
1372 rpl_reset_dio_timer(instance);
1373 if(RPL_IS_STORING(instance)) {
1374 /*
1375 * Request refresh of DAO registrations next DIO. This is only for
1376 * storing mode. In non-storing mode, non-root nodes increment
1377 * DTSN only on when their parent do, or on global repair (see
1378 * RFC6550 section 9.6.)
1379 */
1380 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
1381 }
1382
1383 RPL_STAT(rpl_stats.local_repairs++);
1384}
1385/*---------------------------------------------------------------------------*/
1386void
1387rpl_recalculate_ranks(void)
1388{
1389 rpl_parent_t *p;
1390
1391 /*
1392 * We recalculate ranks when we receive feedback from the system rather
1393 * than RPL protocol messages. This periodical recalculation is called
1394 * from a timer in order to keep the stack depth reasonably low.
1395 */
1396 p = nbr_table_head(rpl_parents);
1397 while(p != NULL) {
1398 if(p->dag != NULL && p->dag->instance && (p->flags & RPL_PARENT_FLAG_UPDATED)) {
1399 p->flags &= ~RPL_PARENT_FLAG_UPDATED;
1400 LOG_DBG("rpl_process_parent_event recalculate_ranks\n");
1401 if(!rpl_process_parent_event(p->dag->instance, p)) {
1402 LOG_DBG("A parent was dropped\n");
1403 }
1404 }
1405 p = nbr_table_next(rpl_parents, p);
1406 }
1407}
1408/*---------------------------------------------------------------------------*/
1409int
1410rpl_process_parent_event(rpl_instance_t *instance, rpl_parent_t *p)
1411{
1412 int return_value;
1413 rpl_parent_t *last_parent = instance->current_dag->preferred_parent;
1414
1415#if LOG_DBG_ENABLED
1416 rpl_rank_t old_rank;
1417 old_rank = instance->current_dag->rank;
1418#endif /* LOG_DBG_ENABLED */
1419
1420 return_value = 1;
1421
1422 if(RPL_IS_STORING(instance)
1423 && uip_ds6_route_is_nexthop(rpl_parent_get_ipaddr(p))
1424 && !rpl_parent_is_reachable(p) && instance->mop > RPL_MOP_NON_STORING) {
1425 LOG_WARN("Unacceptable link %u, removing routes via: ",
1426 rpl_get_parent_link_metric(p));
1427 LOG_WARN_6ADDR(rpl_parent_get_ipaddr(p));
1428 LOG_WARN_("\n");
1429 rpl_remove_routes_by_nexthop(rpl_parent_get_ipaddr(p), p->dag);
1430 }
1431
1432 if(!acceptable_rank(p->dag, rpl_rank_via_parent(p))) {
1433 /* The candidate parent is no longer valid: the rank increase
1434 resulting from the choice of it as a parent would be too high. */
1435 LOG_WARN("Unacceptable rank %u (Current min %u, MaxRankInc %u)\n",
1436 (unsigned)p->rank,
1437 p->dag->min_rank, p->dag->instance->max_rankinc);
1438 rpl_nullify_parent(p);
1439 if(p != instance->current_dag->preferred_parent) {
1440 return 0;
1441 } else {
1442 return_value = 0;
1443 }
1444 }
1445
1446 if(rpl_select_dag(instance, p) == NULL) {
1447 if(last_parent != NULL) {
1448 /* No suitable parent anymore; trigger a local repair. */
1449 LOG_ERR("No parents found in any DAG\n");
1450 rpl_local_repair(instance);
1451 return 0;
1452 }
1453 }
1454
1455#if LOG_DBG_ENABLED
1456 if(DAG_RANK(old_rank, instance) !=
1457 DAG_RANK(instance->current_dag->rank, instance)) {
1458 LOG_INFO("Moving in the instance from rank %hu to %hu\n",
1459 DAG_RANK(old_rank, instance),
1460 DAG_RANK(instance->current_dag->rank, instance));
1461 if(instance->current_dag->rank != RPL_INFINITE_RANK) {
1462 LOG_DBG("The preferred parent is ");
1463 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(instance->current_dag->preferred_parent));
1464 LOG_DBG_(" (rank %u)\n",
1465 (unsigned)DAG_RANK(instance->current_dag->preferred_parent->rank,
1466 instance));
1467 } else {
1468 LOG_WARN("We don't have any parent");
1469 }
1470 }
1471#endif /* LOG_DBG_ENABLED */
1472
1473 return return_value;
1474}
1475/*---------------------------------------------------------------------------*/
1476static int
1477add_nbr_from_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
1478{
1479 /* Add this to the neighbor cache if not already there. */
1480 if(rpl_icmp6_update_nbr_table(from, NBR_TABLE_REASON_RPL_DIO, dio) == NULL) {
1481 LOG_ERR("Out of memory, dropping DIO from ");
1482 LOG_ERR_6ADDR(from);
1483 LOG_ERR_("\n");
1484 return 0;
1485 }
1486 return 1;
1487}
1488/*---------------------------------------------------------------------------*/
1489void
1490rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
1491{
1492 rpl_instance_t *instance;
1493 rpl_dag_t *dag, *previous_dag;
1494 rpl_parent_t *p;
1495
1496#if RPL_WITH_MULTICAST
1497 /*
1498 * If the root is advertising MOP 2, but we support MOP 3, we can
1499 * still join. In that scenario, we suppress DAOs for multicast
1500 * targets.
1501 */
1502 if(dio->mop < RPL_MOP_STORING_NO_MULTICAST) {
1503#else
1504 if(dio->mop != RPL_MOP_DEFAULT) {
1505#endif
1506 LOG_ERR("Ignoring a DIO with an unsupported MOP: %d\n", dio->mop);
1507 return;
1508 }
1509
1510 dag = get_dag(dio->instance_id, &dio->dag_id);
1511 instance = rpl_get_instance(dio->instance_id);
1512
1513 if(dag != NULL && instance != NULL) {
1514 if(lollipop_greater_than(dio->version, dag->version)) {
1515 if(dag->rank == ROOT_RANK(instance)) {
1516 LOG_WARN("Root received inconsistent DIO version number (current: %u, received: %u)\n",
1517 dag->version, dio->version);
1518 dag->version = dio->version;
1519 RPL_LOLLIPOP_INCREMENT(dag->version);
1520 rpl_reset_dio_timer(instance);
1521 } else {
1522 LOG_DBG("Global repair\n");
1523 if(dio->prefix_info.length != 0) {
1524 if(dio->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS) {
1525 LOG_DBG("Prefix announced in DIO\n");
1526 rpl_set_prefix(dag, &dio->prefix_info.prefix,
1527 dio->prefix_info.length);
1528 }
1529 }
1530 global_repair(from, dag, dio);
1531 }
1532 return;
1533 }
1534
1535 if(lollipop_greater_than(dag->version, dio->version)) {
1536 /* The DIO sender is on an older version of the DAG. */
1537 LOG_WARN("old version received => inconsistency detected\n");
1538 if(dag->joined) {
1539 rpl_reset_dio_timer(instance);
1540 return;
1541 }
1542 }
1543 }
1544
1545 if(instance == NULL) {
1546 LOG_INFO("New instance detected (ID=%u): Joining...\n", dio->instance_id);
1547 if(add_nbr_from_dio(from, dio)) {
1548 rpl_join_instance(from, dio);
1549 } else {
1550 LOG_WARN("Not joining since we could not add a parent\n");
1551 }
1552 return;
1553 }
1554
1555 if(instance->current_dag->rank == ROOT_RANK(instance) &&
1556 instance->current_dag != dag) {
1557 LOG_WARN("Root ignored DIO for different DAG\n");
1558 return;
1559 }
1560
1561 if(dag == NULL) {
1562#if RPL_MAX_DAG_PER_INSTANCE > 1
1563 LOG_INFO("Adding new DAG to known instance.\n");
1564 if(!add_nbr_from_dio(from, dio)) {
1565 LOG_WARN("Could not add new DAG, could not add parent\n");
1566 return;
1567 }
1568 dag = rpl_add_dag(from, dio);
1569 if(dag == NULL) {
1570 LOG_WARN("Failed to add DAG.\n");
1571 return;
1572 }
1573#else /* RPL_MAX_DAG_PER_INSTANCE > 1 */
1574 LOG_WARN("Only one instance supported.\n");
1575 return;
1576#endif /* RPL_MAX_DAG_PER_INSTANCE > 1 */
1577 }
1578
1579 if(dio->rank < ROOT_RANK(instance)) {
1580 LOG_INFO("Ignoring DIO with too low rank: %u\n",
1581 (unsigned)dio->rank);
1582 return;
1583 }
1584
1585 /* Prefix Information Option included to add a new prefix. */
1586 if(dio->prefix_info.length != 0) {
1587 if(dio->prefix_info.flags & UIP_ND6_RA_FLAG_AUTONOMOUS) {
1588 LOG_DBG("Prefix announced in DIO\n");
1589 rpl_set_prefix(dag, &dio->prefix_info.prefix, dio->prefix_info.length);
1590 }
1591 }
1592
1593 if(!add_nbr_from_dio(from, dio)) {
1594 LOG_WARN("Could not add parent based on DIO\n");
1595 return;
1596 }
1597
1598 if(dag->rank == ROOT_RANK(instance)) {
1599 if(dio->rank != RPL_INFINITE_RANK) {
1600 instance->dio_counter++;
1601 }
1602 return;
1603 }
1604
1605 /* The DIO comes from a valid DAG, so we can refresh its lifetime. */
1606 dag->lifetime = (1UL << (instance->dio_intmin + instance->dio_intdoubl)) *
1607 RPL_DAG_LIFETIME / 1000;
1608 LOG_DBG("Set DAG ");
1609 LOG_DBG_6ADDR(&dag->dag_id);
1610 LOG_DBG_(" lifetime to %ld\n", (long int)dag->lifetime);
1611
1612 /*
1613 * At this point, we know that this DIO pertains to a DAG that we
1614 * are already part of. We consider the sender of the DIO to be a
1615 * candidate parent, and let rpl_process_parent_event decide whether
1616 * to keep it in the set.
1617 */
1618
1619 p = rpl_find_parent(dag, from);
1620 if(p == NULL) {
1621 previous_dag = find_parent_dag(instance, from);
1622 if(previous_dag == NULL) {
1623 /* Add the DIO sender as a candidate parent. */
1624 p = rpl_add_parent(dag, dio, from);
1625 if(p == NULL) {
1626 LOG_WARN("Failed to add a new parent (");
1627 LOG_WARN_6ADDR(from);
1628 LOG_WARN_(")\n");
1629 return;
1630 }
1631 LOG_INFO("New candidate parent with rank %u: ", (unsigned)p->rank);
1632 LOG_INFO_6ADDR(from);
1633 LOG_INFO_("\n");
1634 } else {
1635 p = rpl_find_parent(previous_dag, from);
1636 if(p != NULL) {
1637 rpl_move_parent(previous_dag, dag, p);
1638 }
1639 }
1640 } else {
1641 if(p->rank == dio->rank) {
1642 LOG_INFO("Received consistent DIO\n");
1643 if(dag->joined) {
1644 instance->dio_counter++;
1645 }
1646 }
1647 }
1648 p->rank = dio->rank;
1649
1650 if(dio->rank == RPL_INFINITE_RANK && p == dag->preferred_parent) {
1651 /* Our preferred parent advertised an infinite rank, reset DIO timer. */
1652 rpl_reset_dio_timer(instance);
1653 }
1654
1655 /* Parent info has been updated, trigger rank recalculation. */
1656 p->flags |= RPL_PARENT_FLAG_UPDATED;
1657
1658 LOG_INFO("Preferred DAG ");
1659 LOG_INFO_6ADDR(&instance->current_dag->dag_id);
1660 LOG_INFO_(", rank %u, min_rank %u, ",
1661 instance->current_dag->rank, instance->current_dag->min_rank);
1662 LOG_INFO_("parent rank %u, link metric %u\n",
1663 p->rank, rpl_get_parent_link_metric(p));
1664
1665 /* We have allocated a candidate parent; process the DIO further. */
1666
1667#if RPL_WITH_MC
1668 memcpy(&p->mc, &dio->mc, sizeof(p->mc));
1669#endif /* RPL_WITH_MC */
1670 if(rpl_process_parent_event(instance, p) == 0) {
1671 LOG_WARN("The candidate parent is rejected\n");
1672 return;
1673 }
1674
1675 /* We don't use route control, so we can have only one official parent. */
1676 if(dag->joined && p == dag->preferred_parent) {
1677 if(should_refresh_routes(instance, dio, p)) {
1678 /* Our parent is requesting a new DAO. Increment DTSN in turn,
1679 in both storing and non-storing mode (see RFC6550 section 9.6.) */
1680 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
1681 rpl_schedule_dao(instance);
1682 }
1683 /*
1684 * We received a new DIO from our preferred parent. Call
1685 * uip_ds6_defrt_add to set a fresh value for the lifetime
1686 * counter.
1687 */
1688 uip_ds6_defrt_add(from, RPL_DEFAULT_ROUTE_INFINITE_LIFETIME ? 0 : RPL_LIFETIME(instance, instance->default_lifetime));
1689 }
1690 p->dtsn = dio->dtsn;
1691}
1692/*---------------------------------------------------------------------------*/
1693/** @} */
Default definitions of C compiler quirk work-arounds.
#define CC_ARRAY_LENGTH(array)
Counts the number of elements of an array.
Definition cc.h:122
Header file for the callback timer.
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition ctimer.c:150
void rpl_schedule_probing(void)
Schedule probing with delay RPL_PROBING_DELAY_FUNC().
void rpl_schedule_probing_now(void)
Schedule probing within a few seconds.
#define RPL_LIFETIME(lifetime)
Compute lifetime, accounting for the lifetime unit.
Definition rpl-types.h:72
#define DAG_RANK(fixpt_rank)
Return DAG RANK as per RFC 6550 (rank divided by min_hoprankinc).
Definition rpl-types.h:81
#define ROOT_RANK
Rank of a root node.
Definition rpl-types.h:78
int rpl_has_downward_route(void)
Get the RPL's best guess on if we have downward route or not.
Definition rpl-dag.c:1059
const uip_lladdr_t * uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr)
Get the link-layer address associated with a specified nbr cache.
int rpl_has_joined(void)
Tells whether the node has joined a network or not.
Definition rpl-dag.c:1050
rpl_dag_t * rpl_get_any_dag(void)
Returns pointer to any DAG (for compatibility with legagy RPL code).
Definition rpl-dag.c:1091
int rpl_dag_root_is_root(void)
Tells whether we are DAG root or not.
uip_ds6_nbr_t * uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr)
Get the neighbor cache associated with a specified link-layer address.
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
uip_ds6_nbr_t * rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
Updates IPv6 neighbor cache on incoming link-local RPL ICMPv6 messages.
Definition rpl-icmp6.c:190
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:351
struct uip_ds6_addr uip_ds6_addr_t
Unicast address structure.
void rpl_dag_init(void)
Initializes rpl-dag module.
Definition rpl-dag.c:141
int uip_ds6_nbr_num(void)
Return the number of neighbor caches.
void rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
Processes incoming DIO.
Definition rpl-dag.c:1490
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:567
struct uip_ds6_nbr uip_ds6_nbr_t
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
rpl_instance_t * rpl_get_default_instance(void)
Returns pointer to the default instance (for compatibility with legagy RPL code).
Definition rpl-dag.c:635
const uip_lladdr_t * uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr)
Get the link-layer address associated with a specified IPv6 address.
uip_ipaddr_t * uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr)
Get an IPv6 address associated with a specified link-layer address.
Linked list manipulation routines.
Header file for the logging system.
Memory block allocation routines.
IPv6 Neighbor cache (link-layer/IPv6 address mapping).
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).
Header file for the uIP TCP/IP stack.