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