52 #include "net/routing/rpl-classic/rpl-private.h" 57 #define LOG_MODULE "RPL" 58 #define LOG_LEVEL LOG_LEVEL_RPL 70 uint8_t sender_closer;
73 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)ext_buf;
74 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset);
76 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
77 || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL
78 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
80 LOG_ERR(
"Hop-by-hop extension header has wrong size or type (%u %u %u)\n",
81 hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len);
85 instance = rpl_get_instance(rpl_opt->instance);
86 if(instance == NULL) {
87 LOG_ERR(
"Unknown instance: %u\n", rpl_opt->instance);
91 if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
92 LOG_ERR(
"Forward error!\n");
97 if(RPL_IS_STORING(instance)) {
98 route = uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr);
100 uip_ds6_route_rm(route);
103 RPL_STAT(rpl_stats.forward_errors++);
105 rpl_reset_dio_timer(instance);
110 if(!instance->current_dag->joined) {
111 LOG_ERR(
"No DAG in the instance\n");
115 if(rpl_opt->flags & RPL_HDR_OPT_DOWN) {
119 sender_rank =
UIP_HTONS(rpl_opt->senderrank);
120 sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER));
122 if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) {
125 sender->rank = sender_rank;
126 if(RPL_IS_NON_STORING(instance)) {
130 rpl_select_dag(instance, sender);
134 sender_closer = sender_rank < instance->current_dag->rank;
136 LOG_DBG(
"Packet going %s, sender closer %d (%d < %d)\n", down == 1 ?
"down" :
"up",
139 instance->current_dag->rank
142 if((down && !sender_closer) || (!down && sender_closer)) {
143 LOG_WARN(
"Loop detected - senderrank: %d my-rank: %d sender_closer: %d\n",
144 sender_rank, instance->current_dag->rank,
149 instance->unicast_dio_target = sender;
150 rpl_schedule_unicast_dio_immediately(instance);
152 if(rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) {
153 RPL_STAT(rpl_stats.loop_errors++);
154 LOG_ERR(
" Rank error signalled in RPL option!\n");
156 rpl_reset_dio_timer(instance);
159 LOG_WARN(
"Single error tolerated\n");
160 RPL_STAT(rpl_stats.loop_warnings++);
161 rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR;
165 LOG_DBG(
"Rank OK\n");
172 #if RPL_WITH_NON_STORING 173 struct uip_routing_hdr *rh_header;
179 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len, UIP_PROTO_ROUTING);
185 if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) ||
186 (dest_node != NULL && root_node != NULL &&
187 dest_node->parent == root_node)) {
193 uip_create_linklocal_prefix(ipaddr);
206 #if RPL_WITH_NON_STORING 207 struct uip_routing_hdr *rh_header;
208 struct uip_rpl_srh_hdr *srh_header;
211 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len, UIP_PROTO_ROUTING);
213 if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) {
215 uint8_t cmpri, cmpre;
219 uint8_t segments_left;
220 uip_ipaddr_t current_dest_addr;
222 srh_header = (
struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN);
223 segments_left = rh_header->seg_left;
224 ext_len = rh_header->len * 8 + 8;
225 cmpri = srh_header->cmpr >> 4;
226 cmpre = srh_header->cmpr & 0x0f;
227 padding = srh_header->pad >> 4;
228 path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
231 LOG_DBG(
"read SRH, path len %u, segments left %u, Cmpri %u, Cmpre %u, ext len %u (padding %u)\n",
232 path_len, segments_left, cmpri, cmpre, ext_len, padding);
234 if(segments_left == 0) {
237 uint8_t i = path_len - segments_left;
238 uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
239 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
246 memcpy(((uint8_t *)&
UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
248 memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr);
251 rh_header->seg_left--;
253 LOG_INFO(
"SRH next hop ");
267 count_matching_bytes(
const void *p1,
const void *p2,
size_t n)
270 for(i = 0; i < n; i++) {
271 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
279 insert_srh_header(
void)
284 uint8_t cmpri, cmpre;
291 uip_ipaddr_t node_addr;
294 struct uip_routing_hdr *rh_hdr = (
struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
295 struct uip_rpl_srh_hdr *srh_hdr = (
struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
297 LOG_INFO(
"SRH creating source routing header with destination ");
307 LOG_ERR(
"SRH DAG not found\n");
312 if(dest_node == NULL) {
318 if(root_node == NULL) {
319 LOG_ERR(
"SRH root node not found\n");
324 LOG_ERR(
"SRH no path found to destination\n");
330 node = dest_node->parent;
335 if(node == root_node) {
336 LOG_DBG(
"SRH no need to insert SRH\n");
340 while(node != NULL && node != root_node) {
345 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &
UIP_IP_BUF->destipaddr, 16));
349 LOG_DBG_6ADDR(&node_addr);
356 ext_len = RPL_RH_LEN + RPL_SRH_LEN
357 + (path_len - 1) * (16 - cmpre)
360 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
363 LOG_DBG(
"SRH Path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
364 path_len, cmpri, cmpre, ext_len, padding);
368 LOG_ERR(
"Packet too long: impossible to add source routing header (%u bytes)\n", ext_len);
382 rh_hdr->len = (ext_len - 8) / 8;
383 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
384 rh_hdr->seg_left = path_len;
387 srh_hdr->cmpr = (cmpri << 4) + cmpre;
388 srh_hdr->pad = padding << 4;
393 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding;
395 while(node != NULL && node->parent != root_node) {
398 hop_ptr -= (16 - cmpri);
399 memcpy(hop_ptr, ((uint8_t*)&node_addr) + cmpri, 16 - cmpri);
409 uipbuf_add_ext_hdr(ext_len);
416 update_hbh_header(
void)
419 rpl_parent_t *parent;
420 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
421 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2);
424 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
425 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
427 LOG_ERR(
"Hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len);
431 instance = rpl_get_instance(rpl_opt->instance);
432 if(instance == NULL || !instance->used || !instance->current_dag->joined) {
433 LOG_ERR(
"Unable to add/update hop-by-hop extension header: incorrect instance\n");
437 LOG_INFO(
"Updating RPL option\n");
439 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
440 rpl_opt->instance = instance->instance_id;
442 if(RPL_IS_STORING(instance)) {
447 if((rpl_opt->flags & RPL_HDR_OPT_DOWN)) {
448 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
449 rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR;
450 LOG_WARN(
"RPL forwarding error\n");
453 LOG_WARN(
"RPL generate No-Path DAO\n");
454 parent = rpl_get_parent((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
456 dao_output_target(parent, &
UIP_IP_BUF->destipaddr, RPL_ZERO_LIFETIME);
465 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
468 rpl_opt->flags &= ~RPL_HDR_OPT_DOWN;
469 LOG_DBG(
"RPL option going up\n");
472 rpl_opt->flags |= RPL_HDR_OPT_DOWN;
473 LOG_DBG(
"RPL option going down\n");
485 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
486 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
489 LOG_DBG(
"Creating hop-by-hop option\n");
491 LOG_ERR(
"Packet too long: impossible to add hop-by-hop option\n");
496 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0),
uip_len - UIP_IPH_LEN);
497 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
504 hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
505 rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
506 rpl_opt->opt_len = RPL_HDR_OPT_LEN;
508 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
509 rpl_opt->instance = instance->instance_id;
511 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
515 return update_hbh_header();
521 uint8_t *prev_proto_ptr;
524 uint8_t *next_header;
525 struct uip_ext_hdr *ext_ptr;
526 struct uip_ext_hdr_opt *opt_ptr;
528 next_header = uipbuf_get_next_header(
uip_buf,
uip_len, &protocol,
true);
529 if(next_header == NULL) {
532 ext_ptr = (
struct uip_ext_hdr *)next_header;
535 while(uip_is_proto_ext_hdr(protocol)) {
536 opt_ptr = (
struct uip_ext_hdr_opt *)(next_header + 2);
537 if(protocol == UIP_PROTO_ROUTING ||
538 (protocol ==
UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
540 *prev_proto_ptr = ext_ptr->next;
541 ext_len = ext_ptr->len * 8 + 8;
542 if(uipbuf_add_ext_hdr(-ext_len) ==
false) {
552 memmove(next_header, next_header + ext_len,
556 protocol = *prev_proto_ptr;
559 next_header = uipbuf_get_next_header(next_header,
562 if(next_header == NULL) {
566 ext_ptr = (
struct uip_ext_hdr *)next_header;
567 prev_proto_ptr = &ext_ptr->next;
577 if(default_instance == NULL || default_instance->current_dag == NULL
582 if(default_instance->current_dag->rank ==
ROOT_RANK(default_instance)) {
586 if(rpl_get_dag(&
UIP_IP_BUF->destipaddr) != NULL) {
588 if(RPL_IS_NON_STORING(default_instance)) {
589 return insert_srh_header();
591 return insert_hbh_header(default_instance);
602 return insert_hbh_header(default_instance);
605 return update_hbh_header();
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
#define UIP_IP_BUF
Direct access to IPv6 header.
Header for the Contiki/uIP interface.
#define ROOT_RANK
Rank of a root node.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
#define UIP_PROTO_HBHO
extension headers types
int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr)
Look for next hop from SRH of current uIP packet.
int rpl_ext_header_update(void)
Adds/updates all RPL extension headers to current uIP packet.
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
int rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset)
Process and update the RPL hop-by-hop extension headers of the current uIP packet.
Header file for IPv6-related data structures.
An entry in the routing table.
uint16_t uip_ext_len
The length of the extension headers.
Routing driver header file
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
uip_sr_node_t * uip_sr_get_node(void *graph, const uip_ipaddr_t *addr)
Looks up for a source routing node from its IPv6 global address.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
bool rpl_ext_header_remove(void)
Removes all RPL extension headers.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Header file for the uIP TCP/IP stack.
uip_ds6_netif_t uip_ds6_if
The single interface.
int(* get_sr_node_ipaddr)(uip_ipaddr_t *addr, const uip_sr_node_t *node)
Returns the global IPv6 address of a source routing node.
Header file for the Packet buffer (packetbuf) management
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Header file for the logging system
int uip_sr_is_addr_reachable(void *graph, const uip_ipaddr_t *addr)
Telle whether an address is reachable, i.e.
A node in a source routing graph, stored at the root and representing all child-parent relationship...
int rpl_ext_header_srh_update(void)
Process and update SRH in-place, i.e.