47#include "net/routing/rpl-lite/rpl.h"
50#include "lib/random.h"
57#define LOG_MODULE "RPL"
58#define LOG_LEVEL LOG_LEVEL_RPL
61#define RPL_DIO_GROUNDED 0x80
62#define RPL_DIO_MOP_SHIFT 3
63#define RPL_DIO_MOP_MASK 0x38
64#define RPL_DIO_PREFERENCE_MASK 0x07
67static void dis_input(
void);
68static void dio_input(
void);
69static void dao_input(
void);
73UIP_ICMP6_HANDLER(dis_handler,
ICMP6_RPL, RPL_CODE_DIS, dis_input);
74UIP_ICMP6_HANDLER(dio_handler,
ICMP6_RPL, RPL_CODE_DIO, dio_input);
75UIP_ICMP6_HANDLER(dao_handler,
ICMP6_RPL, RPL_CODE_DAO, dao_input);
78static void dao_ack_input(
void);
79UIP_ICMP6_HANDLER(dao_ack_handler,
ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
84get32(uint8_t *buffer,
int pos)
86 return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
87 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
91set32(uint8_t *buffer,
int pos, uint32_t value)
93 buffer[pos++] = value >> 24;
94 buffer[pos++] = (value >> 16) & 0xff;
95 buffer[pos++] = (value >> 8) & 0xff;
96 buffer[pos++] = value & 0xff;
100get16(uint8_t *buffer,
int pos)
102 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
106set16(uint8_t *buffer,
int pos, uint16_t value)
108 buffer[pos++] = value >> 8;
109 buffer[pos++] = value & 0xff;
113rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason,
void *data)
119 packetbuf_addr(PACKETBUF_ADDR_SENDER),
120 0, NBR_REACHABLE, reason, data)) == NULL) {
121 LOG_ERR(
"could not add neighbor to cache ");
124 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
135 if(!curr_instance.used) {
136 LOG_WARN(
"dis_input: not in an instance yet, discard\n");
140 LOG_INFO(
"received a DIS from ");
153 unsigned char *buffer;
158 buffer = UIP_ICMP_PAYLOAD;
159 buffer[0] = buffer[1] = 0;
162 addr = &rpl_multicast_addr;
165 LOG_INFO(
"sending a DIS to ");
166 LOG_INFO_6ADDR(
addr);
175 unsigned char *buffer;
176 uint16_t buffer_length;
183 memset(&dio, 0,
sizeof(dio));
186 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
187 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
188 dio.dag_redund = RPL_DIO_REDUNDANCY;
189 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
190 dio.dag_max_rankinc = RPL_MAX_RANKINC;
191 dio.ocp = RPL_OF_OCP;
192 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
193 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
197 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
199 if(buffer_length < 8 +
sizeof(dio.dag_id)) {
200 LOG_WARN(
"dio_input: invalid DIO header, len %"PRIu16
", discard\n",
207 buffer = UIP_ICMP_PAYLOAD;
209 dio.instance_id = buffer[i++];
210 dio.version = buffer[i++];
211 dio.rank = get16(buffer, i);
214 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
215 dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
216 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
218 dio.dtsn = buffer[i++];
222 memcpy(&dio.dag_id, buffer + i,
sizeof(dio.dag_id));
223 i +=
sizeof(dio.dag_id);
226 for(; i < buffer_length; i += len) {
227 subopt_type = buffer[i];
228 if(subopt_type == RPL_OPTION_PAD1) {
232 if(i + 1 >= buffer_length) {
233 LOG_ERR(
"dio_input: malformed packet, discard\n");
236 len = 2 + buffer[i + 1];
239 if(len + i > buffer_length) {
240 LOG_ERR(
"dio_input: malformed packet, discard\n");
244 switch(subopt_type) {
245 case RPL_OPTION_DAG_METRIC_CONTAINER:
247 LOG_WARN(
"dio_input: invalid DAG MC, len %u, discard\n", len);
250 dio.mc.type = buffer[i + 2];
251 dio.mc.flags = buffer[i + 3] << 1;
252 dio.mc.flags |= buffer[i + 4] >> 7;
253 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
254 dio.mc.prec = buffer[i + 4] & 0xf;
255 dio.mc.length = buffer[i + 5];
257 if(dio.mc.type == RPL_DAG_MC_NONE) {
259 }
else if(dio.mc.type == RPL_DAG_MC_ETX) {
261 LOG_WARN(
"dio_input: invalid DAG MC, len %u, discard\n", len);
264 dio.mc.obj.etx = get16(buffer, i + 6);
265 }
else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
267 LOG_WARN(
"dio_input: invalid DAG MC, len %u, discard\n", len);
270 dio.mc.obj.energy.flags = buffer[i + 6];
271 dio.mc.obj.energy.energy_est = buffer[i + 7];
273 LOG_WARN(
"dio_input: unsupported DAG MC type %u, discard\n", (
unsigned)dio.mc.type);
277 case RPL_OPTION_ROUTE_INFO:
279 LOG_WARN(
"dio_input: invalid route info option, len %u, discard\n",
285 dio.destination_prefix.length = buffer[i + 2];
286 dio.destination_prefix.flags = buffer[i + 3];
287 dio.destination_prefix.lifetime = get32(buffer, i + 4);
289 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
290 dio.destination_prefix.length <= 128) {
291 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
292 (dio.destination_prefix.length + 7) / 8);
294 LOG_WARN(
"dio_input: invalid route info option, len %u, discard\n", len);
299 case RPL_OPTION_DAG_CONF:
301 LOG_WARN(
"dio_input: invalid DAG configuration option, len %u, discard\n", len);
306 dio.dag_intdoubl = buffer[i + 3];
307 dio.dag_intmin = buffer[i + 4];
308 dio.dag_redund = buffer[i + 5];
309 dio.dag_max_rankinc = get16(buffer, i + 6);
310 dio.dag_min_hoprankinc = get16(buffer, i + 8);
311 dio.ocp = get16(buffer, i + 10);
313 dio.default_lifetime = buffer[i + 13];
314 dio.lifetime_unit = get16(buffer, i + 14);
316 case RPL_OPTION_PREFIX_INFO:
318 LOG_WARN(
"dio_input: invalid DAG prefix info, len %u, discard\n", len);
321 dio.prefix_info.length = buffer[i + 2];
322 if(dio.prefix_info.length >
sizeof(uip_ipaddr_t) * 8) {
323 LOG_WARN(
"dio_input: invalid DAG prefix info, len %u > %zu\n",
324 dio.prefix_info.length,
sizeof(uip_ipaddr_t) * 8);
328 dio.prefix_info.flags = buffer[i + 3];
331 dio.prefix_info.lifetime = get32(buffer, i + 8);
333 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
336 LOG_WARN(
"dio_input: unsupported suboption type in DIO: %u, discard\n", (
unsigned)subopt_type);
341 LOG_INFO(
"received a %s-DIO from ",
343 LOG_INFO_6ADDR(&from);
344 LOG_INFO_(
", instance_id %u, DAG ID ", (
unsigned)dio.instance_id);
345 LOG_INFO_6ADDR(&dio.dag_id);
346 LOG_INFO_(
", version %u, dtsn %u, rank %u\n",
347 (
unsigned)dio.version,
351 rpl_process_dio(&from, &dio);
360 unsigned char *buffer;
362 uip_ipaddr_t *
addr = uc_addr;
370 if(uc_addr == NULL) {
379 buffer = UIP_ICMP_PAYLOAD;
380 buffer[pos++] = curr_instance.instance_id;
381 buffer[pos++] = curr_instance.dag.version;
387 buffer[pos] = curr_instance.dag.grounded ? RPL_DIO_GROUNDED : 0;
388 buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
389 buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
392 buffer[pos++] = curr_instance.dtsn_out;
398 memcpy(buffer + pos, &curr_instance.dag.dag_id,
sizeof(curr_instance.dag.dag_id));
402 if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
403 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
405 buffer[pos++] = curr_instance.mc.type;
406 buffer[pos++] = curr_instance.mc.flags >> 1;
407 buffer[pos] = (curr_instance.mc.flags & 1) << 7;
408 buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
409 if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
411 set16(buffer, pos, curr_instance.mc.obj.etx);
413 }
else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
415 buffer[pos++] = curr_instance.mc.obj.energy.flags;
416 buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
418 LOG_ERR(
"unable to send DIO because of unsupported DAG MC type %u\n",
419 (
unsigned)curr_instance.mc.type);
426 buffer[pos++] = RPL_OPTION_DAG_CONF;
429 buffer[pos++] = curr_instance.dio_intdoubl;
430 buffer[pos++] = curr_instance.dio_intmin;
431 buffer[pos++] = curr_instance.dio_redundancy;
432 set16(buffer, pos, curr_instance.max_rankinc);
434 set16(buffer, pos, curr_instance.min_hoprankinc);
437 set16(buffer, pos, curr_instance.of->ocp);
440 buffer[pos++] = curr_instance.default_lifetime;
441 set16(buffer, pos, curr_instance.lifetime_unit);
445 if(curr_instance.dag.prefix_info.length > 0) {
446 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
448 buffer[pos++] = curr_instance.dag.prefix_info.length;
449 buffer[pos++] = curr_instance.dag.prefix_info.flags;
450 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
452 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
454 memset(&buffer[pos], 0, 4);
456 memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
464 LOG_INFO(
"sending a %s-DIO with rank %u to ",
465 uc_addr != NULL ?
"unicast" :
"multicast",
466 (unsigned)curr_instance.dag.rank);
467 LOG_INFO_6ADDR(
addr);
478 unsigned char *buffer;
479 uint16_t buffer_length;
485 memset(&dao, 0,
sizeof(dao));
487 dao.instance_id = UIP_ICMP_PAYLOAD[0];
488 if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
489 LOG_ERR(
"dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
494 memset(&dao.parent_addr, 0, 16);
496 buffer = UIP_ICMP_PAYLOAD;
497 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
499 if(buffer_length < 4) {
500 LOG_WARN(
"dao_input: invalid DAO header, len %"PRIu16
", discard\n",
507 dao.lifetime = curr_instance.default_lifetime;
508 dao.flags = buffer[pos++];
510 dao.sequence = buffer[pos++];
513 if(dao.flags & RPL_DAO_D_FLAG) {
514 if(buffer_length < 4 +
sizeof(curr_instance.dag.dag_id)) {
515 LOG_WARN(
"dao_input: missing full DAG ID, len %"PRIu16
", discard\n",
519 if(memcmp(&curr_instance.dag.dag_id, &buffer[pos],
sizeof(curr_instance.dag.dag_id))) {
520 LOG_ERR(
"dao_input: different DAG ID ");
521 LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
522 LOG_ERR_(
", discard\n");
529 for(i = pos; i < buffer_length; i += len) {
530 subopt_type = buffer[i];
531 if(subopt_type == RPL_OPTION_PAD1) {
535 if(i + 1 >= buffer_length) {
536 LOG_ERR(
"dao_input: malformed packet, discard\n");
539 len = 2 + buffer[i + 1];
542 if(i + len > buffer_length) {
543 LOG_ERR(
"dao_input: malformed packet, discard\n");
547 switch(subopt_type) {
548 case RPL_OPTION_TARGET:
551 LOG_WARN(
"dao_input: invalid target option, len %u, discard\n", len);
554 dao.prefixlen = buffer[i + 3];
555 if(4 + (dao.prefixlen + 7) / CHAR_BIT != len) {
556 LOG_WARN(
"dao_input: invalid target option, len %u != %u, discard\n",
557 len, 4 + (dao.prefixlen + 7) / CHAR_BIT);
560 memset(&dao.prefix, 0,
sizeof(dao.prefix));
561 memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
563 case RPL_OPTION_TRANSIT:
566 LOG_WARN(
"dao_input: invalid transit option, len %"PRIu16
", discard\n",
570 dao.lifetime = buffer[i + 5];
572 memcpy(&dao.parent_addr, buffer + i + 6, 16);
579 LOG_INFO(
"received a %sDAO from ", dao.lifetime == 0 ?
"No-path " :
"");
581 LOG_INFO_(
", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
582 LOG_INFO_6ADDR(&dao.prefix);
583 LOG_INFO_(
", prefix length %u, parent ", dao.prefixlen);
584 LOG_INFO_6ADDR(&dao.parent_addr);
596 unsigned char *buffer;
605 if(!curr_instance.used) {
606 LOG_WARN(
"rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
610 if(curr_instance.dag.preferred_parent == NULL) {
611 LOG_WARN(
"rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
615 if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
616 LOG_WARN(
"rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
617 prefix, parent_ipaddr, curr_instance.mop);
621 buffer = UIP_ICMP_PAYLOAD;
624 buffer[pos++] = curr_instance.instance_id;
628 buffer[pos] |= RPL_DAO_K_FLAG;
633 buffer[pos++] = curr_instance.dag.dao_last_seqno;
636 prefixlen =
sizeof(*prefix) * CHAR_BIT;
637 buffer[pos++] = RPL_OPTION_TARGET;
638 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
640 buffer[pos++] = prefixlen;
641 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
642 pos += ((prefixlen + 7) / CHAR_BIT);
645 buffer[pos++] = RPL_OPTION_TRANSIT;
650 buffer[pos++] = lifetime;
653 memcpy(buffer + pos, &curr_instance.dag.dag_id, 8);
655 memcpy(buffer + pos, ((
const unsigned char *)parent_ipaddr) + 8, 8);
658 LOG_INFO(
"sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
659 lifetime == 0 ?
"No-path " :
"",
660 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_transmissions, lifetime);
661 LOG_INFO_6ADDR(prefix);
663 LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
664 LOG_INFO_(
", parent ");
665 LOG_INFO_6ADDR(parent_ipaddr);
681 buffer = UIP_ICMP_PAYLOAD;
683 instance_id = buffer[0];
684 sequence = buffer[2];
687 if(!curr_instance.used || curr_instance.instance_id != instance_id) {
688 LOG_ERR(
"dao_ack_input: unknown instance, discard\n");
692 LOG_INFO(
"received a DAO-%s with seqno %d (%d %d) and status %d from ",
693 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence,
694 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_last_seqno, status);
707 unsigned char *buffer;
712 buffer = UIP_ICMP_PAYLOAD;
713 buffer[0] = curr_instance.instance_id;
715 buffer[2] = sequence;
718 LOG_INFO(
"sending a DAO-%s seqno %d to ",
719 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence);
720 LOG_INFO_6ADDR(dest);
721 LOG_INFO_(
" with status %d\n", status);
void rpl_process_dao_ack(uint8_t sequence, uint8_t status)
Processes incoming DAO-ACK.
void rpl_icmp6_dis_output(uip_ipaddr_t *addr)
Creates an ICMPv6 DIS packet and sends it.
uint8_t rpl_get_leaf_only(void)
Get the value of the rpl_leaf_only flag.
void rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
Creates an ICMPv6 DIO packet and sends it.
void rpl_dag_update_state(void)
Updates RPL internal state: selects preferred parent, updates rank & metreic container,...
void rpl_process_dis(uip_ipaddr_t *from, int is_multicast)
Processes incoming DIS.
void rpl_icmp6_dao_output(uint8_t lifetime)
Creates an ICMPv6 DAO packet and sends it to the root, advertising the current preferred parent,...
void rpl_icmp6_init()
Initializes rpl-icmp6 module, registers ICMPv6 handlers for all RPL ICMPv6 messages: DIO,...
void rpl_process_dao(uip_ipaddr_t *from, rpl_dao_t *dao)
Processes incoming DAO.
const uip_ipaddr_t * rpl_get_global_address(void)
Get one of the node's global addresses.
void rpl_icmp6_dao_ack_output(uip_ipaddr_t *dest, uint8_t sequence, uint8_t status)
Creates an ICMPv6 DAO-ACK packet and sends it to the originator of the ACK.
uip_ipaddr_t * rpl_neighbor_get_ipaddr(rpl_nbr_t *nbr)
Returns a neighbor's (link-local) IPv6 address.
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*
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
#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...
Header file for ICMPv6 message and error handing (RFC 4443)
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.