47 #include "net/routing/rpl-lite/rpl.h" 50 #include "lib/random.h" 56 #define LOG_MODULE "RPL" 57 #define LOG_LEVEL LOG_LEVEL_RPL 60 #define RPL_DIO_GROUNDED 0x80 61 #define RPL_DIO_MOP_SHIFT 3 62 #define RPL_DIO_MOP_MASK 0x38 63 #define RPL_DIO_PREFERENCE_MASK 0x07 66 static void dis_input(
void);
67 static void dio_input(
void);
68 static void dao_input(
void);
72 UIP_ICMP6_HANDLER(dis_handler,
ICMP6_RPL, RPL_CODE_DIS, dis_input);
73 UIP_ICMP6_HANDLER(dio_handler,
ICMP6_RPL, RPL_CODE_DIO, dio_input);
74 UIP_ICMP6_HANDLER(dao_handler,
ICMP6_RPL, RPL_CODE_DAO, dao_input);
77 static void dao_ack_input(
void);
78 UIP_ICMP6_HANDLER(dao_ack_handler,
ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
83 get32(uint8_t *buffer,
int pos)
85 return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
86 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
90 set32(uint8_t *buffer,
int pos, uint32_t value)
92 buffer[pos++] = value >> 24;
93 buffer[pos++] = (value >> 16) & 0xff;
94 buffer[pos++] = (value >> 8) & 0xff;
95 buffer[pos++] = value & 0xff;
99 get16(uint8_t *buffer,
int pos)
101 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
105 set16(uint8_t *buffer,
int pos, uint16_t value)
107 buffer[pos++] = value >> 8;
108 buffer[pos++] = value & 0xff;
118 packetbuf_addr(PACKETBUF_ADDR_SENDER),
119 0, NBR_REACHABLE, reason, data)) == NULL) {
120 LOG_ERR(
"could not add neighbor to cache ");
123 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
134 if(!curr_instance.used) {
135 LOG_WARN(
"dis_input: not in an instance yet, discard\n");
139 LOG_INFO(
"received a DIS from ");
152 unsigned char *buffer;
157 buffer = UIP_ICMP_PAYLOAD;
158 buffer[0] = buffer[1] = 0;
161 addr = &rpl_multicast_addr;
164 LOG_INFO(
"sending a DIS to ");
165 LOG_INFO_6ADDR(addr);
174 unsigned char *buffer;
175 uint8_t buffer_length;
182 memset(&dio, 0,
sizeof(dio));
185 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
186 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
187 dio.dag_redund = RPL_DIO_REDUNDANCY;
188 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
189 dio.dag_max_rankinc = RPL_MAX_RANKINC;
190 dio.ocp = RPL_OF_OCP;
191 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
192 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
196 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
200 buffer = UIP_ICMP_PAYLOAD;
202 dio.instance_id = buffer[i++];
203 dio.version = buffer[i++];
204 dio.rank = get16(buffer, i);
207 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
208 dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
209 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
211 dio.dtsn = buffer[i++];
215 memcpy(&dio.dag_id, buffer + i,
sizeof(dio.dag_id));
216 i +=
sizeof(dio.dag_id);
219 for(; i < buffer_length; i += len) {
220 subopt_type = buffer[i];
221 if(subopt_type == RPL_OPTION_PAD1) {
225 len = 2 + buffer[i + 1];
228 if(len + i > buffer_length) {
229 LOG_ERR(
"dio_input: malformed packet, discard\n");
233 switch(subopt_type) {
234 case RPL_OPTION_DAG_METRIC_CONTAINER:
236 LOG_WARN(
"dio_input: invalid DAG MC, len %u, discard\n", len);
239 dio.mc.type = buffer[i + 2];
240 dio.mc.flags = buffer[i + 3] << 1;
241 dio.mc.flags |= buffer[i + 4] >> 7;
242 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
243 dio.mc.prec = buffer[i + 4] & 0xf;
244 dio.mc.length = buffer[i + 5];
246 if(dio.mc.type == RPL_DAG_MC_NONE) {
248 }
else if(dio.mc.type == RPL_DAG_MC_ETX) {
249 dio.mc.obj.etx = get16(buffer, i + 6);
250 }
else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
251 dio.mc.obj.energy.flags = buffer[i + 6];
252 dio.mc.obj.energy.energy_est = buffer[i + 7];
254 LOG_WARN(
"dio_input: unsupported DAG MC type %u, discard\n", (
unsigned)dio.mc.type);
258 case RPL_OPTION_ROUTE_INFO:
260 LOG_WARN(
"dio_input: invalid destination prefix option, len %u, discard\n", len);
265 dio.destination_prefix.length = buffer[i + 2];
266 dio.destination_prefix.flags = buffer[i + 3];
267 dio.destination_prefix.lifetime = get32(buffer, i + 4);
269 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
270 dio.destination_prefix.length <= 128) {
271 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
272 (dio.destination_prefix.length + 7) / 8);
274 LOG_WARN(
"dio_input: invalid route info option, len %u, discard\n", len);
279 case RPL_OPTION_DAG_CONF:
281 LOG_WARN(
"dio_input: invalid DAG configuration option, len %u, discard\n", len);
286 dio.dag_intdoubl = buffer[i + 3];
287 dio.dag_intmin = buffer[i + 4];
288 dio.dag_redund = buffer[i + 5];
289 dio.dag_max_rankinc = get16(buffer, i + 6);
290 dio.dag_min_hoprankinc = get16(buffer, i + 8);
291 dio.ocp = get16(buffer, i + 10);
293 dio.default_lifetime = buffer[i + 13];
294 dio.lifetime_unit = get16(buffer, i + 14);
296 case RPL_OPTION_PREFIX_INFO:
298 LOG_WARN(
"dio_input: invalid DAG prefix info, len %u, discard\n", len);
301 dio.prefix_info.length = buffer[i + 2];
302 dio.prefix_info.flags = buffer[i + 3];
305 dio.prefix_info.lifetime = get32(buffer, i + 8);
307 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
310 LOG_WARN(
"dio_input: unsupported suboption type in DIO: %u, discard\n", (
unsigned)subopt_type);
315 LOG_INFO(
"received a %s-DIO from ",
317 LOG_INFO_6ADDR(&from);
318 LOG_INFO_(
", instance_id %u, DAG ID ", (
unsigned)dio.instance_id);
319 LOG_INFO_6ADDR(&dio.dag_id);
320 LOG_INFO_(
", version %u, dtsn %u, rank %u\n",
321 (
unsigned)dio.version,
334 unsigned char *buffer;
336 uip_ipaddr_t *
addr = uc_addr;
344 if(uc_addr == NULL) {
353 buffer = UIP_ICMP_PAYLOAD;
354 buffer[pos++] = curr_instance.instance_id;
355 buffer[pos++] = curr_instance.dag.version;
358 set16(buffer, pos, RPL_INFINITE_RANK);
360 set16(buffer, pos, curr_instance.dag.rank);
365 if(curr_instance.dag.grounded) {
366 buffer[pos] |= RPL_DIO_GROUNDED;
369 buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
370 buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
373 buffer[pos++] = curr_instance.dtsn_out;
379 memcpy(buffer + pos, &curr_instance.dag.dag_id,
sizeof(curr_instance.dag.dag_id));
383 if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
384 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
386 buffer[pos++] = curr_instance.mc.type;
387 buffer[pos++] = curr_instance.mc.flags >> 1;
388 buffer[pos] = (curr_instance.mc.flags & 1) << 7;
389 buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
390 if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
392 set16(buffer, pos, curr_instance.mc.obj.etx);
394 }
else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
396 buffer[pos++] = curr_instance.mc.obj.energy.flags;
397 buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
399 LOG_ERR(
"unable to send DIO because of unsupported DAG MC type %u\n",
400 (
unsigned)curr_instance.mc.type);
407 buffer[pos++] = RPL_OPTION_DAG_CONF;
410 buffer[pos++] = curr_instance.dio_intdoubl;
411 buffer[pos++] = curr_instance.dio_intmin;
412 buffer[pos++] = curr_instance.dio_redundancy;
413 set16(buffer, pos, curr_instance.max_rankinc);
415 set16(buffer, pos, curr_instance.min_hoprankinc);
418 set16(buffer, pos, curr_instance.of->ocp);
421 buffer[pos++] = curr_instance.default_lifetime;
422 set16(buffer, pos, curr_instance.lifetime_unit);
426 if(curr_instance.dag.prefix_info.length > 0) {
427 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
429 buffer[pos++] = curr_instance.dag.prefix_info.length;
430 buffer[pos++] = curr_instance.dag.prefix_info.flags;
431 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
433 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
435 memset(&buffer[pos], 0, 4);
437 memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
442 addr = addr != NULL ?
addr : &rpl_multicast_addr;
445 LOG_INFO(
"sending a %s-DIO with rank %u to ",
446 uc_addr != NULL ?
"unicast" :
"multicast",
447 (
unsigned)curr_instance.dag.rank);
448 LOG_INFO_6ADDR(addr);
459 unsigned char *buffer;
460 uint8_t buffer_length;
466 memset(&dao, 0,
sizeof(dao));
468 dao.instance_id = UIP_ICMP_PAYLOAD[0];
469 if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
470 LOG_ERR(
"dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
475 memset(&dao.parent_addr, 0, 16);
477 buffer = UIP_ICMP_PAYLOAD;
478 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
482 dao.lifetime = curr_instance.default_lifetime;
483 dao.flags = buffer[pos++];
485 dao.sequence = buffer[pos++];
488 if(dao.flags & RPL_DAO_D_FLAG) {
489 if(memcmp(&curr_instance.dag.dag_id, &buffer[pos],
sizeof(curr_instance.dag.dag_id))) {
490 LOG_ERR(
"dao_input: different DAG ID ");
491 LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
492 LOG_ERR_(
", discard\n");
499 for(i = pos; i < buffer_length; i += len) {
500 subopt_type = buffer[i];
501 if(subopt_type == RPL_OPTION_PAD1) {
505 len = 2 + buffer[i + 1];
508 switch(subopt_type) {
509 case RPL_OPTION_TARGET:
511 dao.prefixlen = buffer[i + 3];
512 memset(&dao.prefix, 0,
sizeof(dao.prefix));
513 memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
515 case RPL_OPTION_TRANSIT:
519 dao.lifetime = buffer[i + 5];
521 memcpy(&dao.parent_addr, buffer + i + 6, 16);
528 LOG_INFO(
"received a %sDAO from ", dao.lifetime == 0 ?
"No-path " :
"");
530 LOG_INFO_(
", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
531 LOG_INFO_6ADDR(&dao.prefix);
532 LOG_INFO_(
", prefix length %u, parent ", dao.prefixlen);
533 LOG_INFO_6ADDR(&dao.parent_addr);
545 unsigned char *buffer;
554 if(!curr_instance.used) {
555 LOG_WARN(
"rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
559 if(curr_instance.dag.preferred_parent == NULL) {
560 LOG_WARN(
"rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
564 if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
565 LOG_WARN(
"rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
566 prefix, parent_ipaddr, curr_instance.mop);
570 buffer = UIP_ICMP_PAYLOAD;
573 buffer[pos++] = curr_instance.instance_id;
577 buffer[pos] |= RPL_DAO_K_FLAG;
582 buffer[pos++] = curr_instance.dag.dao_last_seqno;
585 prefixlen =
sizeof(*prefix) * CHAR_BIT;
586 buffer[pos++] = RPL_OPTION_TARGET;
587 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
589 buffer[pos++] = prefixlen;
590 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
591 pos += ((prefixlen + 7) / CHAR_BIT);
594 buffer[pos++] = RPL_OPTION_TRANSIT;
599 buffer[pos++] = lifetime;
602 memcpy(buffer + pos, &curr_instance.dag.dag_id, 8);
604 memcpy(buffer + pos, ((
const unsigned char *)parent_ipaddr) + 8, 8);
607 LOG_INFO(
"sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
608 lifetime == 0 ?
"No-path " :
"",
609 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_transmissions, lifetime);
610 LOG_INFO_6ADDR(prefix);
612 LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
613 LOG_INFO_(
", parent ");
614 LOG_INFO_6ADDR(parent_ipaddr);
630 buffer = UIP_ICMP_PAYLOAD;
632 instance_id = buffer[0];
633 sequence = buffer[2];
636 if(!curr_instance.used || curr_instance.instance_id != instance_id) {
637 LOG_ERR(
"dao_ack_input: unknown instance, discard\n");
641 LOG_INFO(
"received a DAO-%s with seqno %d (%d %d) and status %d from ",
642 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence,
643 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_last_seqno, status);
656 unsigned char *buffer;
661 buffer = UIP_ICMP_PAYLOAD;
662 buffer[0] = curr_instance.instance_id;
664 buffer[2] = sequence;
667 LOG_INFO(
"sending a DAO-%s seqno %d to ",
668 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence);
669 LOG_INFO_6ADDR(dest);
670 LOG_INFO_(
" with status %d\n", status);
#define UIP_IP_BUF
Direct access to IPv6 header.
Header file for ICMPv6 message and error handing (RFC 4443)
uip_ipaddr_t * rpl_neighbor_get_ipaddr(rpl_nbr_t *nbr)
Returns a neighbor's (link-local) IPv6 address.
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
void rpl_process_dao(uip_ipaddr_t *from, rpl_dao_t *dao)
Processes incoming DAO.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
uint8_t rpl_get_leaf_only(void)
Get the value of the rpl_leaf_only flag.
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_process_dao_ack(uint8_t sequence, uint8_t status)
Processes incoming DAO-ACK.
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
void rpl_process_dis(uip_ipaddr_t *from, int is_multicast)
Processes incoming DIS.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
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.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
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_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 rpl_dag_update_state(void)
Updates RPL internal state: selects preferred parent, updates rank & metreic container, triggers control traffic accordingly and updates uIP6 internal state.
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.
Header file for the Packet buffer (packetbuf) management
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
void rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
Creates an ICMPv6 DIO packet and sends it.
Header file for the logging system
void rpl_icmp6_init()
Initializes rpl-icmp6 module, registers ICMPv6 handlers for all RPL ICMPv6 messages: DIO...
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
void rpl_icmp6_dis_output(uip_ipaddr_t *addr)
Creates an ICMPv6 DIS packet and sends it.