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 65 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) 66 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) 67 #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) 70 static void dis_input(
void);
71 static void dio_input(
void);
72 static void dao_input(
void);
76 UIP_ICMP6_HANDLER(dis_handler,
ICMP6_RPL, RPL_CODE_DIS, dis_input);
77 UIP_ICMP6_HANDLER(dio_handler,
ICMP6_RPL, RPL_CODE_DIO, dio_input);
78 UIP_ICMP6_HANDLER(dao_handler,
ICMP6_RPL, RPL_CODE_DAO, dao_input);
81 static void dao_ack_input(
void);
82 UIP_ICMP6_HANDLER(dao_ack_handler,
ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
87 get32(uint8_t *buffer,
int pos)
89 return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
90 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
94 set32(uint8_t *buffer,
int pos, uint32_t value)
96 buffer[pos++] = value >> 24;
97 buffer[pos++] = (value >> 16) & 0xff;
98 buffer[pos++] = (value >> 8) & 0xff;
99 buffer[pos++] = value & 0xff;
103 get16(uint8_t *buffer,
int pos)
105 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
109 set16(uint8_t *buffer,
int pos, uint16_t value)
111 buffer[pos++] = value >> 8;
112 buffer[pos++] = value & 0xff;
120 if((nbr = uip_ds6_nbr_lookup(from)) == NULL) {
122 packetbuf_addr(PACKETBUF_ADDR_SENDER),
123 0, NBR_REACHABLE, reason, data)) == NULL) {
124 LOG_ERR(
"could not add neighbor to cache ");
127 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
138 if(!curr_instance.used) {
139 LOG_WARN(
"dis_input: not in an instance yet, discard\n");
143 LOG_INFO(
"received a DIS from ");
156 unsigned char *buffer;
161 buffer = UIP_ICMP_PAYLOAD;
162 buffer[0] = buffer[1] = 0;
165 addr = &rpl_multicast_addr;
168 LOG_INFO(
"sending a DIS to ");
169 LOG_INFO_6ADDR(addr);
178 unsigned char *buffer;
179 uint8_t buffer_length;
186 memset(&dio, 0,
sizeof(dio));
189 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
190 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
191 dio.dag_redund = RPL_DIO_REDUNDANCY;
192 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
193 dio.dag_max_rankinc = RPL_MAX_RANKINC;
194 dio.ocp = RPL_OF_OCP;
195 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
196 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
200 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
204 buffer = UIP_ICMP_PAYLOAD;
206 dio.instance_id = buffer[i++];
207 dio.version = buffer[i++];
208 dio.rank = get16(buffer, i);
211 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
212 dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
213 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
215 dio.dtsn = buffer[i++];
219 memcpy(&dio.dag_id, buffer + i,
sizeof(dio.dag_id));
220 i +=
sizeof(dio.dag_id);
223 for(; i < buffer_length; i += len) {
224 subopt_type = buffer[i];
225 if(subopt_type == RPL_OPTION_PAD1) {
229 len = 2 + buffer[i + 1];
232 if(len + i > buffer_length) {
233 LOG_ERR(
"dio_input: malformed packet, discard\n");
237 switch(subopt_type) {
238 case RPL_OPTION_DAG_METRIC_CONTAINER:
240 LOG_WARN(
"dio_input: invalid DAG MC, len %u, discard\n", len);
243 dio.mc.type = buffer[i + 2];
244 dio.mc.flags = buffer[i + 3] << 1;
245 dio.mc.flags |= buffer[i + 4] >> 7;
246 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
247 dio.mc.prec = buffer[i + 4] & 0xf;
248 dio.mc.length = buffer[i + 5];
250 if(dio.mc.type == RPL_DAG_MC_NONE) {
252 }
else if(dio.mc.type == RPL_DAG_MC_ETX) {
253 dio.mc.obj.etx = get16(buffer, i + 6);
254 }
else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
255 dio.mc.obj.energy.flags = buffer[i + 6];
256 dio.mc.obj.energy.energy_est = buffer[i + 7];
258 LOG_WARN(
"dio_input: unsupported DAG MC type %u, discard\n", (
unsigned)dio.mc.type);
262 case RPL_OPTION_ROUTE_INFO:
264 LOG_WARN(
"dio_input: invalid destination prefix option, len %u, discard\n", len);
269 dio.destination_prefix.length = buffer[i + 2];
270 dio.destination_prefix.flags = buffer[i + 3];
271 dio.destination_prefix.lifetime = get32(buffer, i + 4);
273 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
274 dio.destination_prefix.length <= 128) {
275 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
276 (dio.destination_prefix.length + 7) / 8);
278 LOG_WARN(
"dio_input: invalid route info option, len %u, discard\n", len);
283 case RPL_OPTION_DAG_CONF:
285 LOG_WARN(
"dio_input: invalid DAG configuration option, len %u, discard\n", len);
290 dio.dag_intdoubl = buffer[i + 3];
291 dio.dag_intmin = buffer[i + 4];
292 dio.dag_redund = buffer[i + 5];
293 dio.dag_max_rankinc = get16(buffer, i + 6);
294 dio.dag_min_hoprankinc = get16(buffer, i + 8);
295 dio.ocp = get16(buffer, i + 10);
297 dio.default_lifetime = buffer[i + 13];
298 dio.lifetime_unit = get16(buffer, i + 14);
300 case RPL_OPTION_PREFIX_INFO:
302 LOG_WARN(
"dio_input: invalid DAG prefix info, len %u, discard\n", len);
305 dio.prefix_info.length = buffer[i + 2];
306 dio.prefix_info.flags = buffer[i + 3];
309 dio.prefix_info.lifetime = get32(buffer, i + 8);
311 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
314 LOG_WARN(
"dio_input: unsupported suboption type in DIO: %u, discard\n", (
unsigned)subopt_type);
319 LOG_INFO(
"received a %s-DIO from ",
321 LOG_INFO_6ADDR(&from);
322 LOG_INFO_(
", instance_id %u, DAG ID ", (
unsigned)dio.instance_id);
323 LOG_INFO_6ADDR(&dio.dag_id);
324 LOG_INFO_(
", version %u, dtsn %u, rank %u\n",
325 (
unsigned)dio.version,
338 unsigned char *buffer;
340 uip_ipaddr_t *
addr = uc_addr;
348 if(uc_addr == NULL) {
357 buffer = UIP_ICMP_PAYLOAD;
358 buffer[pos++] = curr_instance.instance_id;
359 buffer[pos++] = curr_instance.dag.version;
362 set16(buffer, pos, RPL_INFINITE_RANK);
364 set16(buffer, pos, curr_instance.dag.rank);
369 if(curr_instance.dag.grounded) {
370 buffer[pos] |= RPL_DIO_GROUNDED;
373 buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
374 buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
377 buffer[pos++] = curr_instance.dtsn_out;
383 memcpy(buffer + pos, &curr_instance.dag.dag_id,
sizeof(curr_instance.dag.dag_id));
387 if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
388 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
390 buffer[pos++] = curr_instance.mc.type;
391 buffer[pos++] = curr_instance.mc.flags >> 1;
392 buffer[pos] = (curr_instance.mc.flags & 1) << 7;
393 buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
394 if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
396 set16(buffer, pos, curr_instance.mc.obj.etx);
398 }
else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
400 buffer[pos++] = curr_instance.mc.obj.energy.flags;
401 buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
403 LOG_ERR(
"unable to send DIO because of unsupported DAG MC type %u\n",
404 (
unsigned)curr_instance.mc.type);
411 buffer[pos++] = RPL_OPTION_DAG_CONF;
414 buffer[pos++] = curr_instance.dio_intdoubl;
415 buffer[pos++] = curr_instance.dio_intmin;
416 buffer[pos++] = curr_instance.dio_redundancy;
417 set16(buffer, pos, curr_instance.max_rankinc);
419 set16(buffer, pos, curr_instance.min_hoprankinc);
422 set16(buffer, pos, curr_instance.of->ocp);
425 buffer[pos++] = curr_instance.default_lifetime;
426 set16(buffer, pos, curr_instance.lifetime_unit);
430 if(curr_instance.dag.prefix_info.length > 0) {
431 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
433 buffer[pos++] = curr_instance.dag.prefix_info.length;
434 buffer[pos++] = curr_instance.dag.prefix_info.flags;
435 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
437 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
439 memset(&buffer[pos], 0, 4);
441 memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
446 addr = addr != NULL ?
addr : &rpl_multicast_addr;
449 LOG_INFO(
"sending a %s-DIO with rank %u to ",
450 uc_addr != NULL ?
"unicast" :
"multicast",
451 (
unsigned)curr_instance.dag.rank);
452 LOG_INFO_6ADDR(addr);
463 unsigned char *buffer;
464 uint8_t buffer_length;
470 memset(&dao, 0,
sizeof(dao));
472 dao.instance_id = UIP_ICMP_PAYLOAD[0];
473 if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
474 LOG_ERR(
"dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
479 memset(&dao.parent_addr, 0, 16);
481 buffer = UIP_ICMP_PAYLOAD;
482 buffer_length =
uip_len - uip_l3_icmp_hdr_len;
486 dao.lifetime = curr_instance.default_lifetime;
487 dao.flags = buffer[pos++];
489 dao.sequence = buffer[pos++];
492 if(dao.flags & RPL_DAO_D_FLAG) {
493 if(memcmp(&curr_instance.dag.dag_id, &buffer[pos],
sizeof(curr_instance.dag.dag_id))) {
494 LOG_ERR(
"dao_input: different DAG ID ");
495 LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
496 LOG_ERR_(
", discard\n");
503 for(i = pos; i < buffer_length; i += len) {
504 subopt_type = buffer[i];
505 if(subopt_type == RPL_OPTION_PAD1) {
509 len = 2 + buffer[i + 1];
512 switch(subopt_type) {
513 case RPL_OPTION_TARGET:
515 dao.prefixlen = buffer[i + 3];
516 memset(&dao.prefix, 0,
sizeof(dao.prefix));
517 memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
519 case RPL_OPTION_TRANSIT:
523 dao.lifetime = buffer[i + 5];
525 memcpy(&dao.parent_addr, buffer + i + 6, 16);
532 LOG_INFO(
"received a %sDAO from ", dao.lifetime == 0 ?
"No-path " :
"");
534 LOG_INFO_(
", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
535 LOG_INFO_6ADDR(&dao.prefix);
536 LOG_INFO_(
", prefix length %u, parent ", dao.prefixlen);
537 LOG_INFO_6ADDR(&dao.parent_addr);
549 unsigned char *buffer;
558 if(!curr_instance.used) {
559 LOG_WARN(
"rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
563 if(curr_instance.dag.preferred_parent == NULL) {
564 LOG_WARN(
"rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
568 if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
569 LOG_WARN(
"rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
570 prefix, parent_ipaddr, curr_instance.mop);
574 buffer = UIP_ICMP_PAYLOAD;
577 buffer[pos++] = curr_instance.instance_id;
581 buffer[pos] |= RPL_DAO_K_FLAG;
586 buffer[pos++] = curr_instance.dag.dao_curr_seqno;
589 prefixlen =
sizeof(*prefix) * CHAR_BIT;
590 buffer[pos++] = RPL_OPTION_TARGET;
591 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
593 buffer[pos++] = prefixlen;
594 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
595 pos += ((prefixlen + 7) / CHAR_BIT);
598 buffer[pos++] = RPL_OPTION_TRANSIT;
603 buffer[pos++] = lifetime;
606 memcpy(buffer + pos, &curr_instance.dag.dag_id, 8);
608 memcpy(buffer + pos, ((
const unsigned char *)parent_ipaddr) + 8, 8);
611 LOG_INFO(
"sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
612 lifetime == 0 ?
"No-path " :
"",
613 curr_instance.dag.dao_curr_seqno, curr_instance.dag.dao_transmissions, lifetime);
614 LOG_INFO_6ADDR(prefix);
616 LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
617 LOG_INFO_(
", parent ");
618 LOG_INFO_6ADDR(parent_ipaddr);
634 buffer = UIP_ICMP_PAYLOAD;
636 instance_id = buffer[0];
637 sequence = buffer[2];
640 if(!curr_instance.used || curr_instance.instance_id != instance_id) {
641 LOG_ERR(
"dao_ack_input: unknown instance, discard\n");
645 LOG_INFO(
"received a DAO-%s with seqno %d (%d %d) and status %d from ",
646 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence,
647 curr_instance.dag.dao_curr_seqno, curr_instance.dag.dao_curr_seqno, status);
660 unsigned char *buffer;
665 buffer = UIP_ICMP_PAYLOAD;
666 buffer[0] = curr_instance.instance_id;
668 buffer[2] = sequence;
671 LOG_INFO(
"sending a DAO-%s seqno %d to ",
672 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ?
"ACK" :
"NACK", sequence);
673 LOG_INFO_6ADDR(dest);
674 LOG_INFO_(
" with status %d\n", status);
#define UIP_IP_BUF
Pointer to IP 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.
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)
Neighbor Cache basic routines.
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...
An entry in the nbr cache.
void rpl_icmp6_dis_output(uip_ipaddr_t *addr)
Creates an ICMPv6 DIS packet and sends it.