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;
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 dio.prefix_info.flags = buffer[i + 3];
325 dio.prefix_info.lifetime = get32(buffer, i + 8);
327 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
330 LOG_WARN(
"dio_input: unsupported suboption type in DIO: %u, discard\n", (
unsigned)subopt_type);
335 LOG_INFO(
"received a %s-DIO from ",
337 LOG_INFO_6ADDR(&from);
338 LOG_INFO_(
", instance_id %u, DAG ID ", (
unsigned)dio.instance_id);
339 LOG_INFO_6ADDR(&dio.dag_id);
340 LOG_INFO_(
", version %u, dtsn %u, rank %u\n",
341 (
unsigned)dio.version,
354 unsigned char *buffer;
356 uip_ipaddr_t *
addr = uc_addr;
364 if(uc_addr == NULL) {
373 buffer = UIP_ICMP_PAYLOAD;
374 buffer[pos++] = curr_instance.instance_id;
375 buffer[pos++] = curr_instance.dag.version;
378 set16(buffer, pos, RPL_INFINITE_RANK);
380 set16(buffer, pos, curr_instance.dag.rank);
385 if(curr_instance.dag.grounded) {
386 buffer[pos] |= RPL_DIO_GROUNDED;
389 buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
390 buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
393 buffer[pos++] = curr_instance.dtsn_out;
399 memcpy(buffer + pos, &curr_instance.dag.dag_id,
sizeof(curr_instance.dag.dag_id));
403 if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
404 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
406 buffer[pos++] = curr_instance.mc.type;
407 buffer[pos++] = curr_instance.mc.flags >> 1;
408 buffer[pos] = (curr_instance.mc.flags & 1) << 7;
409 buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
410 if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
412 set16(buffer, pos, curr_instance.mc.obj.etx);
414 }
else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
416 buffer[pos++] = curr_instance.mc.obj.energy.flags;
417 buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
419 LOG_ERR(
"unable to send DIO because of unsupported DAG MC type %u\n",
420 (
unsigned)curr_instance.mc.type);
427 buffer[pos++] = RPL_OPTION_DAG_CONF;
430 buffer[pos++] = curr_instance.dio_intdoubl;
431 buffer[pos++] = curr_instance.dio_intmin;
432 buffer[pos++] = curr_instance.dio_redundancy;
433 set16(buffer, pos, curr_instance.max_rankinc);
435 set16(buffer, pos, curr_instance.min_hoprankinc);
438 set16(buffer, pos, curr_instance.of->ocp);
441 buffer[pos++] = curr_instance.default_lifetime;
442 set16(buffer, pos, curr_instance.lifetime_unit);
446 if(curr_instance.dag.prefix_info.length > 0) {
447 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
449 buffer[pos++] = curr_instance.dag.prefix_info.length;
450 buffer[pos++] = curr_instance.dag.prefix_info.flags;
451 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
453 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
455 memset(&buffer[pos], 0, 4);
457 memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
465 LOG_INFO(
"sending a %s-DIO with rank %u to ",
466 uc_addr != NULL ?
"unicast" :
"multicast",
467 (
unsigned)curr_instance.dag.rank);
468 LOG_INFO_6ADDR(
addr);
479 unsigned char *buffer;
480 uint16_t buffer_length;
486 memset(&dao, 0,
sizeof(dao));
488 dao.instance_id = UIP_ICMP_PAYLOAD[0];
489 if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
490 LOG_ERR(
"dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
495 memset(&dao.parent_addr, 0, 16);
497 buffer = UIP_ICMP_PAYLOAD;
498 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
500 if(buffer_length < 4) {
501 LOG_WARN(
"dao_input: invalid DAO header, len %"PRIu16
", discard\n",
508 dao.lifetime = curr_instance.default_lifetime;
509 dao.flags = buffer[pos++];
511 dao.sequence = buffer[pos++];
514 if(dao.flags & RPL_DAO_D_FLAG) {
515 if(buffer_length < 4 +
sizeof(curr_instance.dag.dag_id)) {
516 LOG_WARN(
"dao_input: missing full DAG ID, len %"PRIu16
", discard\n",
520 if(memcmp(&curr_instance.dag.dag_id, &buffer[pos],
sizeof(curr_instance.dag.dag_id))) {
521 LOG_ERR(
"dao_input: different DAG ID ");
522 LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
523 LOG_ERR_(
", discard\n");
530 for(i = pos; i < buffer_length; i += len) {
531 subopt_type = buffer[i];
532 if(subopt_type == RPL_OPTION_PAD1) {
536 if(i + 1 >= buffer_length) {
537 LOG_ERR(
"dao_input: malformed packet, discard\n");
540 len = 2 + buffer[i + 1];
543 if(i + len > buffer_length) {
544 LOG_ERR(
"dao_input: malformed packet, discard\n");
548 switch(subopt_type) {
549 case RPL_OPTION_TARGET:
552 LOG_WARN(
"dao_input: invalid target option, len %u, discard\n", len);
555 dao.prefixlen = buffer[i + 3];
556 if(4 + (dao.prefixlen + 7) / CHAR_BIT != len) {
557 LOG_WARN(
"dao_input: invalid target option, len %u != %u, discard\n",
558 len, 4 + (dao.prefixlen + 7) / CHAR_BIT);
561 memset(&dao.prefix, 0,
sizeof(dao.prefix));
562 memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
564 case RPL_OPTION_TRANSIT:
569 LOG_WARN(
"dao_input: invalid transit option, len %"PRIu16
", discard\n",
573 dao.lifetime = buffer[i + 5];
575 memcpy(&dao.parent_addr, buffer + i + 6, 16);
582 LOG_INFO(
"received a %sDAO from ", dao.lifetime == 0 ?
"No-path " :
"");
584 LOG_INFO_(
", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
585 LOG_INFO_6ADDR(&dao.prefix);
586 LOG_INFO_(
", prefix length %u, parent ", dao.prefixlen);
587 LOG_INFO_6ADDR(&dao.parent_addr);
599 unsigned char *buffer;
608 if(!curr_instance.used) {
609 LOG_WARN(
"rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
613 if(curr_instance.dag.preferred_parent == NULL) {
614 LOG_WARN(
"rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
618 if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
619 LOG_WARN(
"rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
620 prefix, parent_ipaddr, curr_instance.mop);
624 buffer = UIP_ICMP_PAYLOAD;
627 buffer[pos++] = curr_instance.instance_id;
631 buffer[pos] |= RPL_DAO_K_FLAG;
636 buffer[pos++] = curr_instance.dag.dao_last_seqno;
639 prefixlen =
sizeof(*prefix) * CHAR_BIT;
640 buffer[pos++] = RPL_OPTION_TARGET;
641 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
643 buffer[pos++] = prefixlen;
644 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
645 pos += ((prefixlen + 7) / CHAR_BIT);
648 buffer[pos++] = RPL_OPTION_TRANSIT;
653 buffer[pos++] = lifetime;
656 memcpy(buffer + pos, &curr_instance.dag.dag_id, 8);
658 memcpy(buffer + pos, ((
const unsigned char *)parent_ipaddr) + 8, 8);
661 LOG_INFO(
"sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
662 lifetime == 0 ?
"No-path " :
"",
663 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_transmissions, lifetime);
664 LOG_INFO_6ADDR(prefix);
666 LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
667 LOG_INFO_(
", parent ");
668 LOG_INFO_6ADDR(parent_ipaddr);
684 buffer = UIP_ICMP_PAYLOAD;
686 instance_id = buffer[0];
687 sequence = buffer[2];
690 if(!curr_instance.used || curr_instance.instance_id != instance_id) {
691 LOG_ERR(
"dao_ack_input: unknown instance, discard\n");
695 LOG_INFO(
"received a DAO-%s with seqno %d (%d %d) and status %d from ",
696 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence,
697 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_last_seqno, status);
710 unsigned char *buffer;
715 buffer = UIP_ICMP_PAYLOAD;
716 buffer[0] = curr_instance.instance_id;
718 buffer[2] = sequence;
721 LOG_INFO(
"sending a DAO-%s seqno %d to ",
722 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence);
723 LOG_INFO_6ADDR(dest);
724 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*
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.
void rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
Processes incoming DIO.
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.