51#include "net/routing/rpl-classic/rpl-private.h"
54#include "lib/random.h"
61#define LOG_MODULE "RPL"
62#define LOG_LEVEL LOG_LEVEL_RPL
65#define RPL_DIO_GROUNDED 0x80
66#define RPL_DIO_MOP_SHIFT 3
67#define RPL_DIO_MOP_MASK 0x38
68#define RPL_DIO_PREFERENCE_MASK 0x07
71static void dis_input(
void);
72static void dio_input(
void);
73static void dao_input(
void);
74static void dao_ack_input(
void);
76static void dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
77 uint8_t lifetime, uint8_t seq_no);
80#ifdef RPL_DEBUG_DIO_INPUT
81void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
84#ifdef RPL_DEBUG_DAO_OUTPUT
85void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
88static uint8_t dao_sequence = RPL_LOLLIPOP_INIT;
95UIP_ICMP6_HANDLER(dis_handler,
ICMP6_RPL, RPL_CODE_DIS, dis_input);
96UIP_ICMP6_HANDLER(dio_handler,
ICMP6_RPL, RPL_CODE_DIO, dio_input);
97UIP_ICMP6_HANDLER(dao_handler,
ICMP6_RPL, RPL_CODE_DAO, dao_input);
98UIP_ICMP6_HANDLER(dao_ack_handler,
ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
103find_route_entry_by_dao_ack(uint8_t seq)
107 if(re->state.dao_seqno_out == seq && RPL_ROUTE_IS_DAO_PENDING(re)) {
111 re = uip_ds6_route_next(re);
123 RPL_LOLLIPOP_INCREMENT(dao_sequence);
126 rep->state.dao_seqno_in = sequence;
127 rep->state.dao_seqno_out = dao_sequence;
128 RPL_ROUTE_SET_DAO_PENDING(rep);
134get_global_addr(uip_ipaddr_t *
addr)
138 uip_ipaddr_t *prefix = NULL;
139 uint8_t prefix_length = 0;
142 if(dag != NULL && dag->prefix_info.length != 0) {
143 prefix = &dag->prefix_info.prefix;
144 prefix_length = dag->prefix_info.length;
147 for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
150 state == ADDR_PREFERRED &&
152 (prefix == NULL || uip_ipaddr_prefixcmp(prefix, &
uip_ds6_if.addr_list[i].ipaddr, prefix_length))) {
153 memcpy(
addr, &
uip_ds6_if.addr_list[i].ipaddr,
sizeof(uip_ipaddr_t));
161get32(uint8_t *buffer,
int pos)
163 return (uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
164 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3];
168set32(uint8_t *buffer,
int pos, uint32_t value)
170 buffer[pos++] = value >> 24;
171 buffer[pos++] = (value >> 16) & 0xff;
172 buffer[pos++] = (value >> 8) & 0xff;
173 buffer[pos++] = value & 0xff;
177get16(uint8_t *buffer,
int pos)
179 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
183set16(uint8_t *buffer,
int pos, uint16_t value)
185 buffer[pos++] = value >> 8;
186 buffer[pos++] = value & 0xff;
190rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason,
198 (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER),
199 0, NBR_REACHABLE, reason, data);
201 LOG_INFO(
"Neighbor added to neighbor cache ");
202 LOG_INFO_6ADDR(from);
204 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
219 LOG_INFO(
"Received a DIS from ");
223 for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
224 instance < end; ++instance) {
225 if(instance->used == 1) {
228 LOG_INFO(
"LEAF ONLY Multicast DIS will NOT reset DIO timer\n");
230 LOG_DBG(
"Multicast DIS => reset DIO timer\n");
231 rpl_reset_dio_timer(instance);
235 if(rpl_icmp6_update_nbr_table(&
UIP_IP_BUF->srcipaddr,
236 NBR_TABLE_REASON_RPL_DIS, NULL) == NULL) {
237 LOG_ERR(
"Out of Memory, not sending unicast DIO, DIS from ");
240 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
243 LOG_DBG(
"Unicast DIS, reply to sender\n");
253dis_output(uip_ipaddr_t *
addr)
255 unsigned char *buffer;
256 uip_ipaddr_t tmpaddr;
267 buffer = UIP_ICMP_PAYLOAD;
268 buffer[0] = buffer[1] = 0;
271 uip_create_linklocal_rplnodes_mcast(&tmpaddr);
275 LOG_INFO(
"Sending a DIS to ");
276 LOG_INFO_6ADDR(
addr);
285 unsigned char *buffer;
286 uint8_t buffer_length;
293 memset(&dio, 0,
sizeof(dio));
296 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
297 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
298 dio.dag_redund = RPL_DIO_REDUNDANCY;
299 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
300 dio.dag_max_rankinc = RPL_MAX_RANKINC;
301 dio.ocp = RPL_OF_OCP;
302 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
303 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
308 LOG_INFO(
"Received a DIO from ");
309 LOG_INFO_6ADDR(&from);
312 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
316 buffer = UIP_ICMP_PAYLOAD;
318 dio.instance_id = buffer[i++];
319 dio.version = buffer[i++];
320 dio.rank = get16(buffer, i);
323 LOG_DBG(
"Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
324 (
unsigned)dio.instance_id,
325 (
unsigned)dio.version,
328 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
329 dio.mop = (buffer[i] & RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
330 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
332 dio.dtsn = buffer[i++];
336 memcpy(&dio.dag_id, buffer + i,
sizeof(dio.dag_id));
337 i +=
sizeof(dio.dag_id);
339 LOG_DBG(
"Incoming DIO (dag_id, pref) = (");
340 LOG_DBG_6ADDR(&dio.dag_id);
341 LOG_DBG_(
", %u)\n", dio.preference);
344 for(; i < buffer_length; i += len) {
345 subopt_type = buffer[i];
346 if(subopt_type == RPL_OPTION_PAD1) {
350 len = 2 + buffer[i + 1];
353 if(len + i > buffer_length) {
354 LOG_WARN(
"Invalid DIO packet\n");
355 RPL_STAT(rpl_stats.malformed_msgs++);
359 LOG_DBG(
"Incoming DIO (option, length) = (%u, %u)\n",
362 switch(subopt_type) {
363 case RPL_OPTION_PAD1:
364 case RPL_OPTION_PADN:
365 LOG_DBG(
"PAD %u bytes\n", len);
367 case RPL_OPTION_DAG_METRIC_CONTAINER:
369 LOG_WARN(
"Invalid DAG MC, len = %d\n", len);
370 RPL_STAT(rpl_stats.malformed_msgs++);
373 dio.mc.type = buffer[i + 2];
374 dio.mc.flags = buffer[i + 3] << 1;
375 dio.mc.flags |= buffer[i + 4] >> 7;
376 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
377 dio.mc.prec = buffer[i + 4] & 0xf;
378 dio.mc.length = buffer[i + 5];
380 if(dio.mc.type == RPL_DAG_MC_NONE) {
382 }
else if(dio.mc.type == RPL_DAG_MC_ETX) {
383 dio.mc.obj.etx = get16(buffer, i + 6);
385 LOG_DBG(
"DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
386 (
unsigned)dio.mc.type,
387 (
unsigned)dio.mc.flags,
388 (
unsigned)dio.mc.aggr,
389 (
unsigned)dio.mc.prec,
390 (
unsigned)dio.mc.length,
391 (
unsigned)dio.mc.obj.etx);
392 }
else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
393 dio.mc.obj.energy.flags = buffer[i + 6];
394 dio.mc.obj.energy.energy_est = buffer[i + 7];
396 LOG_WARN(
"Unhandled DAG MC type: %u\n", (
unsigned)dio.mc.type);
400 case RPL_OPTION_ROUTE_INFO:
402 LOG_WARN(
"Invalid destination prefix option, len = %d\n", len);
403 RPL_STAT(rpl_stats.malformed_msgs++);
408 dio.destination_prefix.length = buffer[i + 2];
409 dio.destination_prefix.flags = buffer[i + 3];
410 dio.destination_prefix.lifetime = get32(buffer, i + 4);
412 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
413 dio.destination_prefix.length <= 128) {
414 LOG_INFO(
"Copying destination prefix\n");
415 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
416 (dio.destination_prefix.length + 7) / 8);
418 LOG_WARN(
"Invalid route info option, len = %d\n", len);
419 RPL_STAT(rpl_stats.malformed_msgs++);
424 case RPL_OPTION_DAG_CONF:
426 LOG_WARN(
"Invalid DAG configuration option, len = %d\n", len);
427 RPL_STAT(rpl_stats.malformed_msgs++);
432 dio.dag_intdoubl = buffer[i + 3];
433 dio.dag_intmin = buffer[i + 4];
434 dio.dag_redund = buffer[i + 5];
435 dio.dag_max_rankinc = get16(buffer, i + 6);
436 dio.dag_min_hoprankinc = get16(buffer, i + 8);
437 dio.ocp = get16(buffer, i + 10);
439 dio.default_lifetime = buffer[i + 13];
440 dio.lifetime_unit = get16(buffer, i + 14);
441 LOG_INFO(
"DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
442 dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
443 dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
444 dio.default_lifetime, dio.lifetime_unit);
446 case RPL_OPTION_PREFIX_INFO:
448 LOG_WARN(
"Invalid DAG prefix info, len != 32\n");
449 RPL_STAT(rpl_stats.malformed_msgs++);
452 dio.prefix_info.length = buffer[i + 2];
454 if(dio.prefix_info.length >
sizeof(uip_ipaddr_t) * 8) {
455 LOG_WARN(
"Invalid DAG prefix info, len %u > %u\n",
456 dio.prefix_info.length, (
unsigned)(
sizeof(uip_ipaddr_t) * 8));
457 RPL_STAT(rpl_stats.malformed_msgs++);
461 dio.prefix_info.flags = buffer[i + 3];
464 dio.prefix_info.lifetime = get32(buffer, i + 8);
466 LOG_INFO(
"Copying prefix information\n");
467 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
470 LOG_WARN(
"Unsupported suboption type in DIO: %u\n",
471 (
unsigned)subopt_type);
475#ifdef RPL_DEBUG_DIO_INPUT
476 RPL_DEBUG_DIO_INPUT(&from, &dio);
479 rpl_process_dio(&from, &dio);
488 unsigned char *buffer;
499 if(uc_addr == NULL) {
500 LOG_DBG(
"LEAF ONLY have multicast addr: skip dio_output\n");
508 buffer = UIP_ICMP_PAYLOAD;
509 buffer[pos++] = instance->instance_id;
510 buffer[pos++] = dag->version;
511 is_root = (dag->rank == ROOT_RANK(instance));
514 LOG_DBG(
"LEAF ONLY DIO rank set to RPL_INFINITE_RANK\n");
515 set16(buffer, pos, RPL_INFINITE_RANK);
517 set16(buffer, pos, dag->rank);
523 buffer[pos] |= RPL_DIO_GROUNDED;
526 buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
527 buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
530 buffer[pos++] = instance->dtsn_out;
532 if(RPL_DIO_REFRESH_DAO_ROUTES && is_root && uc_addr == NULL) {
538 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
545 memcpy(buffer + pos, &dag->dag_id,
sizeof(dag->dag_id));
549 if(instance->mc.type != RPL_DAG_MC_NONE) {
550 instance->of->update_metric_container(instance);
552 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
554 buffer[pos++] = instance->mc.type;
555 buffer[pos++] = instance->mc.flags >> 1;
556 buffer[pos] = (instance->mc.flags & 1) << 7;
557 buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
558 if(instance->mc.type == RPL_DAG_MC_ETX) {
560 set16(buffer, pos, instance->mc.obj.etx);
562 }
else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
564 buffer[pos++] = instance->mc.obj.energy.flags;
565 buffer[pos++] = instance->mc.obj.energy.energy_est;
567 LOG_ERR(
"Unable to send DIO because of unhandled DAG MC type %u\n",
568 (
unsigned)instance->mc.type);
575 buffer[pos++] = RPL_OPTION_DAG_CONF;
578 buffer[pos++] = instance->dio_intdoubl;
579 buffer[pos++] = instance->dio_intmin;
580 buffer[pos++] = instance->dio_redundancy;
581 set16(buffer, pos, instance->max_rankinc);
583 set16(buffer, pos, instance->min_hoprankinc);
586 set16(buffer, pos, instance->of->ocp);
589 buffer[pos++] = instance->default_lifetime;
590 set16(buffer, pos, instance->lifetime_unit);
594 if(dag->prefix_info.length > 0) {
595 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
597 buffer[pos++] = dag->prefix_info.length;
598 buffer[pos++] = dag->prefix_info.flags;
599 set32(buffer, pos, dag->prefix_info.lifetime);
601 set32(buffer, pos, dag->prefix_info.lifetime);
603 memset(&buffer[pos], 0, 4);
605 memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
607 LOG_DBG(
"Sending prefix info in DIO for ");
608 LOG_DBG_6ADDR(&dag->prefix_info.prefix);
611 LOG_DBG(
"No prefix to announce (len %d)\n",
612 dag->prefix_info.length);
616 if(LOG_DBG_ENABLED) {
617 if(uc_addr == NULL) {
618 LOG_DBG(
"LEAF ONLY sending unicast-DIO from multicast-DIO\n");
622 LOG_INFO(
"Sending unicast-DIO with rank %u to ", (
unsigned)dag->rank);
623 LOG_INFO_6ADDR(uc_addr);
628 if(uc_addr == NULL) {
629 LOG_INFO(
"Sending a multicast-DIO with rank %u\n",
630 (
unsigned)instance->current_dag->rank);
631 uip_create_linklocal_rplnodes_mcast(&
addr);
634 LOG_INFO(
"Sending unicast-DIO with rank %u to ",
635 (
unsigned)instance->current_dag->rank);
636 LOG_INFO_6ADDR(uc_addr);
644dao_input_storing(
void)
647 uip_ipaddr_t dao_sender_addr;
650 unsigned char *buffer;
663 rpl_parent_t *parent;
669 memset(&prefix, 0,
sizeof(prefix));
673 buffer = UIP_ICMP_PAYLOAD;
674 uint16_t buffer_length =
uip_len - uip_l3_icmp_hdr_len;
675 if(buffer_length < 4) {
676 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
681 uint16_t last_valid_pos = buffer_length - 1;
684 instance_id = buffer[pos++];
685 instance = rpl_get_instance(instance_id);
686 if(instance == NULL) {
687 LOG_ERR(
"Cannot get RPL instance\n");
691 lifetime = instance->default_lifetime;
693 flags = buffer[pos++];
696 sequence = buffer[pos++];
698 dag = instance->current_dag;
699 is_root = (dag->rank == ROOT_RANK(instance));
702 if(flags & RPL_DAO_D_FLAG) {
703 if(last_valid_pos < pos + 16) {
704 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
705 last_valid_pos, pos + 16);
709 if(memcmp(&dag->dag_id, &buffer[pos],
sizeof(dag->dag_id))) {
710 LOG_INFO(
"Ignoring a DAO for a DAG different from ours\n");
717 RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
720 LOG_DBG(
"Received a (%s) DAO with sequence number %u from ",
721 learned_from == RPL_ROUTE_FROM_UNICAST_DAO ?
"unicast" :
"multicast",
723 LOG_DBG_6ADDR(&dao_sender_addr);
726 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
728 parent = rpl_find_parent(dag, &dao_sender_addr);
732 DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
733 LOG_WARN(
"Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
734 DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
735 parent->rank = RPL_INFINITE_RANK;
736 parent->flags |= RPL_PARENT_FLAG_UPDATED;
741 if(parent != NULL && parent == dag->preferred_parent) {
742 LOG_WARN(
"Loop detected when receiving a unicast DAO from our parent\n");
743 parent->rank = RPL_INFINITE_RANK;
744 parent->flags |= RPL_PARENT_FLAG_UPDATED;
750 for(i = pos; i < buffer_length; i += len) {
751 subopt_type = buffer[i];
752 if(subopt_type == RPL_OPTION_PAD1) {
756 if(last_valid_pos < i + 1) {
757 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
758 last_valid_pos, i + 1);
761 len = 2 + buffer[i + 1];
764 switch(subopt_type) {
765 case RPL_OPTION_TARGET:
767 if(last_valid_pos < i + 3) {
768 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
769 last_valid_pos, i + 3);
772 prefixlen = buffer[i + 3];
777 if(prefixlen > 128) {
778 LOG_ERR(
"Too large target prefix length %d\n", prefixlen);
781 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
782 LOG_ERR(
"Incomplete DAO target option with prefix length of %d bits\n",
786 memset(&prefix, 0,
sizeof(prefix));
787 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
789 case RPL_OPTION_TRANSIT:
791 if(last_valid_pos < i + 5) {
792 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
793 last_valid_pos, i + 5);
796 lifetime = buffer[i + 5];
802 LOG_INFO(
"DAO lifetime: %u, prefix length: %u prefix: ",
803 (
unsigned)lifetime, (
unsigned)prefixlen);
804 LOG_INFO_6ADDR(&prefix);
807#if RPL_WITH_MULTICAST
816 mcast_group->
dag = dag;
817 mcast_group->
lifetime = RPL_LIFETIME(instance, lifetime);
823 rep = uip_ds6_route_lookup(&prefix);
825 if(lifetime == RPL_ZERO_LIFETIME) {
826 LOG_INFO(
"No-Path DAO received\n");
829 !RPL_ROUTE_IS_NOPATH_RECEIVED(rep) &&
830 rep->length == prefixlen &&
831 uip_ds6_route_nexthop(rep) != NULL &&
832 uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
833 LOG_DBG(
"Setting expiration timer for prefix ");
834 LOG_DBG_6ADDR(&prefix);
836 RPL_ROUTE_SET_NOPATH_RECEIVED(rep);
837 rep->state.lifetime = RPL_NOPATH_REMOVAL_DELAY;
841 if(dag->preferred_parent != NULL &&
842 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
844 out_seq = prepare_for_dao_fwd(sequence, rep);
846 LOG_DBG(
"Forwarding No-path DAO out_seq:%d to parent ", out_seq);
847 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
850 buffer = UIP_ICMP_PAYLOAD;
857 if(flags & RPL_DAO_K_FLAG) {
860 dao_ack_output(instance, &dao_sender_addr, sequence,
861 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
866 LOG_INFO(
"Adding DAO route\n");
869 nbr = rpl_icmp6_update_nbr_table(&dao_sender_addr,
870 NBR_TABLE_REASON_RPL_DAO, instance);
872 LOG_ERR(
"Out of memory, dropping DAO from ");
873 LOG_ERR_6ADDR(&dao_sender_addr);
875 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
877 if(flags & RPL_DAO_K_FLAG) {
879 dao_ack_output(instance, &dao_sender_addr, sequence,
880 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
881 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
886 rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
888 RPL_STAT(rpl_stats.mem_overflows++);
889 LOG_ERR(
"Could not add a route after receiving a DAO\n");
890 if(flags & RPL_DAO_K_FLAG) {
892 dao_ack_output(instance, &dao_sender_addr, sequence,
893 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
894 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
900 rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
901 RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
903#if RPL_WITH_MULTICAST
907 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
910 if(flags & RPL_DAO_K_FLAG) {
919 if((!RPL_ROUTE_IS_DAO_PENDING(rep) &&
920 rep->state.dao_seqno_in == sequence) ||
921 dag->rank == ROOT_RANK(instance)) {
927 if(dag->preferred_parent != NULL &&
928 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
933 if(RPL_ROUTE_IS_DAO_PENDING(rep) &&
934 rep->state.dao_seqno_in == sequence) {
936 out_seq = rep->state.dao_seqno_out;
938 out_seq = prepare_for_dao_fwd(sequence, rep);
942 LOG_DBG(
"Forwarding DAO to parent ");
943 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
944 LOG_DBG_(
" in seq: %d out seq: %d\n", sequence, out_seq);
946 buffer = UIP_ICMP_PAYLOAD;
952 LOG_DBG(
"Sending DAO ACK\n");
954 dao_ack_output(instance, &dao_sender_addr, sequence,
955 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
962dao_input_nonstoring(
void)
964#if RPL_WITH_NON_STORING
965 uip_ipaddr_t dao_sender_addr;
966 uip_ipaddr_t dao_parent_addr;
969 unsigned char *buffer;
982 LOG_INFO(
"Received a DAO from ");
989 memset(&dao_parent_addr, 0, 16);
991 buffer = UIP_ICMP_PAYLOAD;
992 uint16_t buffer_length =
uip_len - uip_l3_icmp_hdr_len;
993 if(buffer_length < 4) {
994 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
999 uint16_t last_valid_pos = buffer_length - 1;
1002 instance_id = buffer[pos++];
1003 instance = rpl_get_instance(instance_id);
1004 lifetime = instance->default_lifetime;
1006 flags = buffer[pos++];
1009 sequence = buffer[pos++];
1011 dag = instance->current_dag;
1013 if(flags & RPL_DAO_D_FLAG) {
1014 if(pos +
sizeof(dag->dag_id) > buffer_length) {
1015 LOG_WARN(
"Insufficient data to read DAG ID from DAO\n");
1018 if(memcmp(&dag->dag_id, &buffer[pos],
sizeof(dag->dag_id))) {
1019 LOG_INFO(
"Ignoring a DAO for a DAG different from ours\n");
1026 for(i = pos; i < buffer_length; i += len) {
1027 subopt_type = buffer[i];
1028 if(subopt_type == RPL_OPTION_PAD1) {
1032 if(last_valid_pos < i + 1) {
1033 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
1034 last_valid_pos, i + 1);
1037 len = 2 + buffer[i + 1];
1040 switch(subopt_type) {
1041 case RPL_OPTION_TARGET:
1043 if(last_valid_pos < i + 3) {
1044 LOG_WARN(
"Dropping incomplete DAO (%"PRIu16
" < %d)\n",
1045 last_valid_pos, i + 3);
1048 prefixlen = buffer[i + 3];
1049 if(prefixlen == 0) {
1053 if(prefixlen > 128) {
1054 LOG_ERR(
"Too large target prefix length %d\n", prefixlen);
1057 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
1058 LOG_ERR(
"Incomplete DAO target option with prefix length of %d bits\n",
1063 memset(&prefix, 0,
sizeof(prefix));
1064 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
1066 case RPL_OPTION_TRANSIT:
1068 if(i + 6 + 16 > buffer_length) {
1069 LOG_WARN(
"Incomplete DAO transit option (%d > %"PRIu16
")\n",
1070 i + 6 + 16, buffer_length);
1073 lifetime = buffer[i + 5];
1075 memcpy(&dao_parent_addr, buffer + i + 6, 16);
1081 LOG_INFO(
"DAO lifetime: %u, prefix length: %u prefix: ",
1082 (
unsigned)lifetime, (
unsigned)prefixlen);
1083 LOG_INFO_6ADDR(&prefix);
1084 LOG_INFO_(
", parent: ");
1085 LOG_INFO_6ADDR(&dao_parent_addr);
1088 if(lifetime == RPL_ZERO_LIFETIME) {
1089 LOG_DBG(
"No-Path DAO received\n");
1093 RPL_LIFETIME(instance, lifetime)) == NULL) {
1094 LOG_WARN(
"DAO failed to add link prefix: ");
1095 LOG_WARN_6ADDR(&prefix);
1096 LOG_WARN_(
", parent: ");
1097 LOG_WARN_6ADDR(&dao_parent_addr);
1103 if(flags & RPL_DAO_K_FLAG) {
1104 LOG_DBG(
"Sending DAO ACK\n");
1106 dao_ack_output(instance, &dao_sender_addr, sequence,
1107 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
1116 uint8_t instance_id;
1119 LOG_INFO(
"Received a DAO from ");
1123 if(
uip_len <= uip_l3_icmp_hdr_len) {
1124 LOG_WARN(
"Ignoring DAO ICMPv6 message without DAO header\n");
1128 instance_id = UIP_ICMP_PAYLOAD[0];
1129 instance = rpl_get_instance(instance_id);
1130 if(instance == NULL) {
1131 LOG_INFO(
"Ignoring a DAO for an unknown RPL instance(%u)\n",
1136 if(RPL_IS_STORING(instance)) {
1137 dao_input_storing();
1138 }
else if(RPL_IS_NON_STORING(instance)) {
1139 dao_input_nonstoring();
1148handle_dao_retransmission(
void *ptr)
1150 rpl_parent_t *parent;
1151 uip_ipaddr_t prefix;
1155 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1158 instance = parent->dag->instance;
1160 if(instance->my_dao_transmissions >= RPL_DAO_MAX_RETRANSMISSIONS) {
1162 if(instance->lifetime_unit == 0xffff && instance->default_lifetime == 0xff) {
1173 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1175 instance->of->dao_ack_callback(parent, RPL_DAO_ACK_TIMEOUT);
1179 rpl_local_repair(instance);
1183 LOG_INFO(
"will retransmit DAO - seq:%d trans:%d\n", instance->my_dao_seqno,
1184 instance->my_dao_transmissions);
1186 if(get_global_addr(&prefix) == 0) {
1191 RPL_DAO_RETRANSMISSION_TIMEOUT / 2 +
1192 (
random_rand() % (RPL_DAO_RETRANSMISSION_TIMEOUT / 2)),
1193 handle_dao_retransmission, parent);
1195 instance->my_dao_transmissions++;
1196 dao_output_target_seq(parent, &prefix,
1197 instance->default_lifetime, instance->my_dao_seqno);
1202dao_output(rpl_parent_t *parent, uint8_t lifetime)
1205 uip_ipaddr_t prefix;
1207 if(get_global_addr(&prefix) == 0) {
1208 LOG_ERR(
"No global address set for this node - suppressing DAO\n");
1212 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1216 RPL_LOLLIPOP_INCREMENT(dao_sequence);
1224 if(lifetime != RPL_ZERO_LIFETIME) {
1226 instance = parent->dag->instance;
1228 instance->my_dao_seqno = dao_sequence;
1229 instance->my_dao_transmissions = 1;
1230 ctimer_set(&instance->dao_retransmit_timer, RPL_DAO_RETRANSMISSION_TIMEOUT,
1231 handle_dao_retransmission, parent);
1238 parent->dag->instance->has_downward_route = lifetime != RPL_ZERO_LIFETIME;
1242 dao_output_target(parent, &prefix, lifetime);
1246dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
1248 dao_output_target_seq(parent, prefix, lifetime, dao_sequence);
1252dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
1253 uint8_t lifetime, uint8_t seq_no)
1257 unsigned char *buffer;
1260 uip_ipaddr_t *parent_ipaddr = NULL;
1261 uip_ipaddr_t *dest_ipaddr = NULL;
1270 if(parent == NULL) {
1271 LOG_ERR(
"dao_output_target error parent NULL\n");
1275 parent_ipaddr = rpl_parent_get_ipaddr(parent);
1276 if(parent_ipaddr == NULL) {
1277 LOG_ERR(
"dao_output_target error parent IP address NULL\n");
1283 LOG_ERR(
"dao_output_target error dag NULL\n");
1287 instance = dag->instance;
1289 if(instance == NULL) {
1290 LOG_ERR(
"dao_output_target error instance NULL\n");
1293 if(prefix == NULL) {
1294 LOG_ERR(
"dao_output_target error prefix NULL\n");
1297#ifdef RPL_DEBUG_DAO_OUTPUT
1298 RPL_DEBUG_DAO_OUTPUT(parent);
1301 buffer = UIP_ICMP_PAYLOAD;
1304 buffer[pos++] = instance->instance_id;
1306#if RPL_DAO_SPECIFY_DAG
1307 buffer[pos] |= RPL_DAO_D_FLAG;
1310 if(lifetime != RPL_ZERO_LIFETIME) {
1311 buffer[pos] |= RPL_DAO_K_FLAG;
1316 buffer[pos++] = seq_no;
1317#if RPL_DAO_SPECIFY_DAG
1318 memcpy(buffer + pos, &dag->dag_id,
sizeof(dag->dag_id));
1319 pos +=
sizeof(dag->dag_id);
1323 prefixlen =
sizeof(*prefix) * CHAR_BIT;
1324 buffer[pos++] = RPL_OPTION_TARGET;
1325 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
1327 buffer[pos++] = prefixlen;
1328 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
1329 pos += ((prefixlen + 7) / CHAR_BIT);
1332 buffer[pos++] = RPL_OPTION_TRANSIT;
1333 buffer[pos++] = (instance->mop != RPL_MOP_NON_STORING) ? 4 : 20;
1337 buffer[pos++] = lifetime;
1339 if(instance->mop != RPL_MOP_NON_STORING) {
1341 dest_ipaddr = parent_ipaddr;
1344 memcpy(buffer + pos, &parent->dag->dag_id, 8);
1347 memcpy(buffer + pos, ((
const unsigned char *)parent_ipaddr) + 8, 8);
1350 dest_ipaddr = &parent->dag->dag_id;
1353 LOG_INFO(
"Sending a %sDAO with sequence number %u, lifetime %u, prefix ",
1354 lifetime == RPL_ZERO_LIFETIME ?
"No-Path " :
"", seq_no, lifetime);
1356 LOG_INFO_6ADDR(prefix);
1358 LOG_INFO_6ADDR(dest_ipaddr);
1359 LOG_INFO_(
" , parent ");
1360 LOG_INFO_6ADDR(parent_ipaddr);
1363 if(dest_ipaddr != NULL) {
1374 uint8_t instance_id;
1378 rpl_parent_t *parent;
1380 buffer = UIP_ICMP_PAYLOAD;
1382 instance_id = buffer[0];
1383 sequence = buffer[2];
1386 instance = rpl_get_instance(instance_id);
1387 if(instance == NULL) {
1392 if(RPL_IS_STORING(instance)) {
1393 parent = rpl_find_parent(instance->current_dag, &
UIP_IP_BUF->srcipaddr);
1394 if(parent == NULL) {
1403 if(instance->current_dag->rank == ROOT_RANK(instance)) {
1404 LOG_DBG(
"DODAG root received a DAO ACK, ignoring it\n");
1409 LOG_INFO(
"Received a DAO %s with sequence number %u (%u) and status %u from ",
1410 status < 128 ?
"ACK" :
"NACK",
1411 sequence, instance->my_dao_seqno, status);
1415 if(sequence == instance->my_dao_seqno) {
1416 instance->has_downward_route = status < 128;
1422 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1423 instance->of->dao_ack_callback(parent, status);
1426#if RPL_REPAIR_ON_DAO_NACK
1427 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1432 rpl_local_repair(instance);
1435 }
else if(RPL_IS_STORING(instance)) {
1438 const uip_ipaddr_t *nexthop;
1439 if((re = find_route_entry_by_dao_ack(sequence)) != NULL) {
1442 RPL_ROUTE_CLEAR_DAO_PENDING(re);
1444 nexthop = uip_ds6_route_nexthop(re);
1445 if(nexthop == NULL) {
1446 LOG_WARN(
"No next hop to fwd DAO ACK to\n");
1448 LOG_INFO(
"Fwd DAO ACK to:");
1449 LOG_INFO_6ADDR(nexthop);
1451 buffer[2] = re->state.dao_seqno_in;
1455 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1457 uip_ds6_route_rm(re);
1460 LOG_WARN(
"No route entry found to forward DAO ACK (seqno %u)\n",
1469dao_ack_output(
rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence,
1473 unsigned char *buffer;
1475 LOG_INFO(
"Sending a DAO %s with sequence number %u to ",
1476 status < 128 ?
"ACK" :
"NACK", sequence);
1477 LOG_INFO_6ADDR(dest);
1478 LOG_INFO_(
" with status %u\n", status);
1480 buffer = UIP_ICMP_PAYLOAD;
1482 buffer[0] = instance->instance_id;
1484 buffer[2] = sequence;
1492rpl_icmp6_register_handlers(
void)
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
uip_mcast6_route_t * uip_mcast6_route_add(uip_ipaddr_t *group)
Add a multicast route.
void uip_sr_expire_parent(const void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent)
Expires a given child-parent link.
uip_ds6_nbr_t * uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data)
Add a neighbor cache for a specified IPv6 address, which is associated with a specified link-layer ad...
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
enum rpl_mode rpl_get_mode(void)
Get the RPL mode.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
#define uip_is_addr_mcast_global(a)
is address a global multicast address (FFxE::/16), a is of type uip_ip6addr_t*
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
uip_sr_node_t * uip_sr_update_node(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent, uint32_t lifetime)
Updates a child-parent link.
uip_ds6_netif_t uip_ds6_if
The single interface.
#define UIP_IP_BUF
Direct access to IPv6 header.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Header file for the logging system.
Header file for the Packet buffer (packetbuf) management.
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
An entry in the routing table.
An entry in the multicast routing table.
void * dag
Pointer to an rpl_dag_t struct.
uint32_t lifetime
Entry lifetime seconds.
Header for the Contiki/uIP interface.
Header file for IPv6-related data structures.
Header file for ICMPv6 message and error handing (RFC 4443)
This header file contains configuration directives for uIPv6 multicast support.
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Header file for IPv6 Neighbor discovery (RFC 4861)
Header file for the uIP TCP/IP stack.