50#include "net/routing/rpl-classic/rpl-private.h"
55#define LOG_MODULE "RPL"
56#define LOG_LEVEL LOG_LEVEL_RPL
68 uint8_t sender_closer;
71 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)ext_buf;
72 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset);
74 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
75 || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL
76 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
78 LOG_ERR(
"Hop-by-hop extension header has wrong size or type (%u %u %u)\n",
79 hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len);
83 instance = rpl_get_instance(rpl_opt->instance);
84 if(instance == NULL) {
85 LOG_ERR(
"Unknown instance: %u\n", rpl_opt->instance);
89 if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
90 LOG_ERR(
"Forward error!\n");
96 if(RPL_IS_STORING(instance)) {
97 route = uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr);
99 uip_ds6_route_rm(route);
102 RPL_STAT(rpl_stats.forward_errors++);
104 rpl_reset_dio_timer(instance);
109 if(!instance->current_dag->joined) {
110 LOG_ERR(
"No DAG in the instance\n");
114 if(rpl_opt->flags & RPL_HDR_OPT_DOWN) {
118 sender_rank =
UIP_HTONS(rpl_opt->senderrank);
119 sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER));
121 if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) {
124 sender->rank = sender_rank;
125 if(RPL_IS_NON_STORING(instance)) {
132 rpl_select_dag(instance, sender);
136 sender_closer = sender_rank < instance->current_dag->rank;
139 LOG_DBG(
"Packet going %s, sender%s closer (%d < %d)\n",
140 down == 1 ?
"down" :
"up",
141 sender_closer ?
"" :
" not",
143 instance->current_dag->rank);
145 if((down && !sender_closer) || (!down && sender_closer)) {
146 LOG_WARN(
"Loop detected - senderrank: %d my-rank: %d sender_closer: %d\n",
147 sender_rank, instance->current_dag->rank,
152 instance->unicast_dio_target = sender;
153 rpl_schedule_unicast_dio_immediately(instance);
156 if(rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) {
157 RPL_STAT(rpl_stats.loop_errors++);
158 LOG_ERR(
"Rank error signaled in RPL option!\n");
161 rpl_reset_dio_timer(instance);
165 LOG_WARN(
"One inconsistency along path is tolerated\n");
166 RPL_STAT(rpl_stats.loop_warnings++);
167 rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR;
171 LOG_DBG(
"Rank OK\n");
178#if RPL_WITH_NON_STORING
179 struct uip_routing_hdr *rh_header;
185 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len, UIP_PROTO_ROUTING);
191 if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) ||
192 (dest_node != NULL && root_node != NULL &&
193 dest_node->parent == root_node)) {
202 uip_create_linklocal_prefix(
ipaddr);
215#if RPL_WITH_NON_STORING
216 struct uip_routing_hdr *rh_header;
217 struct uip_rpl_srh_hdr *srh_header;
220 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len,
223 if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) {
225 uint8_t cmpri, cmpre;
229 uint8_t segments_left;
230 uip_ipaddr_t current_dest_addr;
232 srh_header = (
struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN);
233 segments_left = rh_header->seg_left;
234 ext_len = rh_header->len * 8 + 8;
235 cmpri = srh_header->cmpr >> 4;
236 cmpre = srh_header->cmpr & 0x0f;
237 padding = srh_header->pad >> 4;
238 path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
241 LOG_DBG(
"read SRH, path len %u, segments left %u, Cmpri %u, Cmpre %u, ext len %u (padding %u)\n",
242 path_len, segments_left, cmpri, cmpre, ext_len, padding);
244 if(segments_left == 0) {
246 }
else if(segments_left > path_len) {
248 LOG_ERR(
"SRH with too many segments left (%u > %u)\n",
249 segments_left, path_len);
253 uint8_t i = path_len - segments_left;
254 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
255 ptrdiff_t rh_offset = (uint8_t *)rh_header -
uip_buf;
256 size_t addr_offset = RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
258 if(rh_offset + addr_offset + 16 - cmpr >
UIP_BUFSIZE) {
259 LOG_ERR(
"Invalid SRH address pointer\n");
263 uint8_t *addr_ptr = ((uint8_t *)rh_header) + addr_offset;
270 memcpy(((uint8_t *)&
UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
272 memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr);
275 rh_header->seg_left--;
277 LOG_INFO(
"SRH next hop ");
291count_matching_bytes(
const void *p1,
const void *p2,
size_t n)
293 for(
size_t i = 0; i < n; i++) {
294 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
302insert_srh_header(
void)
307 uint8_t cmpri, cmpre;
314 uip_ipaddr_t node_addr;
317 struct uip_routing_hdr *rh_hdr = (
struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
318 struct uip_rpl_srh_hdr *srh_hdr = (
struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
320 LOG_INFO(
"SRH creating source routing header with destination ");
330 LOG_ERR(
"SRH DAG not found\n");
335 if(dest_node == NULL) {
341 if(root_node == NULL) {
342 LOG_ERR(
"SRH root node not found\n");
347 LOG_ERR(
"SRH no path found to destination\n");
353 node = dest_node->parent;
358 if(node == root_node) {
359 LOG_DBG(
"SRH no need to insert SRH\n");
363 while(node != NULL && node != root_node) {
368 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &
UIP_IP_BUF->destipaddr, 16));
372 LOG_DBG_6ADDR(&node_addr);
380 ext_len = RPL_RH_LEN + RPL_SRH_LEN
381 + (path_len - 1) * (16 - cmpre)
384 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
387 LOG_DBG(
"SRH Path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
388 path_len, cmpri, cmpre, ext_len, padding);
392 LOG_ERR(
"Too long packet: impossible to add SRH (%u bytes)\n", ext_len);
406 rh_hdr->len = (ext_len - 8) / 8;
407 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
408 rh_hdr->seg_left = path_len;
411 srh_hdr->cmpr = (cmpri << 4) + cmpre;
412 srh_hdr->pad = padding << 4;
419 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding;
421 while(node != NULL && node->parent != root_node) {
424 hop_ptr -= (16 - cmpri);
425 memcpy(hop_ptr, ((uint8_t *)&node_addr) + cmpri, 16 - cmpri);
436 uipbuf_add_ext_hdr(ext_len);
443update_hbh_header(
void)
446 rpl_parent_t *parent;
447 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
448 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2);
451 rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) {
452 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) ||
453 rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
455 LOG_ERR(
"Hop-by-hop extension header has wrong size (%u)\n",
460 instance = rpl_get_instance(rpl_opt->instance);
461 if(instance == NULL || !instance->used || !instance->current_dag->joined) {
462 LOG_ERR(
"Unable to add/update hop-by-hop extension header: incorrect instance\n");
466 LOG_INFO(
"Updating RPL option\n");
468 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
469 rpl_opt->instance = instance->instance_id;
471 if(RPL_IS_STORING(instance)) {
480 if(rpl_opt->flags & RPL_HDR_OPT_DOWN) {
481 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
482 rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR;
483 LOG_WARN(
"RPL forwarding error\n");
486 LOG_WARN(
"RPL generate No-Path DAO\n");
487 parent = rpl_get_parent((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
489 dao_output_target(parent, &
UIP_IP_BUF->destipaddr,
501 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
504 rpl_opt->flags &= ~RPL_HDR_OPT_DOWN;
505 LOG_DBG(
"RPL option going up\n");
508 rpl_opt->flags |= RPL_HDR_OPT_DOWN;
509 LOG_DBG(
"RPL option going down\n");
521 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
522 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
525 LOG_DBG(
"Creating hop-by-hop option\n");
527 LOG_ERR(
"Too long packet: impossible to add hop-by-hop option\n");
532 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0),
534 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
541 hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
542 rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
543 rpl_opt->opt_len = RPL_HDR_OPT_LEN;
545 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
546 rpl_opt->instance = instance->instance_id;
548 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
552 return update_hbh_header();
558 uint8_t *prev_proto_ptr;
561 uint8_t *next_header;
562 struct uip_ext_hdr *ext_ptr;
563 struct uip_ext_hdr_opt *opt_ptr;
565 next_header = uipbuf_get_next_header(
uip_buf,
uip_len, &protocol,
true);
566 if(next_header == NULL) {
570 ext_ptr = (
struct uip_ext_hdr *)next_header;
573 while(uip_is_proto_ext_hdr(protocol)) {
574 opt_ptr = (
struct uip_ext_hdr_opt *)(next_header + 2);
575 if(protocol == UIP_PROTO_ROUTING ||
576 (protocol ==
UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
578 *prev_proto_ptr = ext_ptr->next;
579 ext_len = ext_ptr->len * 8 + 8;
580 if(uipbuf_add_ext_hdr(-ext_len) ==
false) {
590 memmove(next_header, next_header + ext_len,
594 protocol = *prev_proto_ptr;
597 next_header = uipbuf_get_next_header(next_header,
600 if(next_header == NULL) {
604 ext_ptr = (
struct uip_ext_hdr *)next_header;
605 prev_proto_ptr = &ext_ptr->next;
615 if(default_instance == NULL || default_instance->current_dag == NULL ||
621 if(default_instance->current_dag->rank ==
ROOT_RANK(default_instance)) {
625 if(rpl_get_dag(&
UIP_IP_BUF->destipaddr) != NULL) {
627 if(RPL_IS_NON_STORING(default_instance)) {
628 return insert_srh_header();
630 return insert_hbh_header(default_instance);
644 return insert_hbh_header(default_instance);
647 return update_hbh_header();
#define ROOT_RANK
Rank of a root node.
uip_sr_node_t * uip_sr_get_node(const void *graph, const uip_ipaddr_t *addr)
Looks up for a source routing node from its IPv6 global address.
int rpl_ext_header_update(void)
Adds/updates all RPL extension headers to current uIP packet.
int uip_sr_is_addr_reachable(const void *graph, const uip_ipaddr_t *addr)
Telle whether an address is reachable, i.e.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr)
Look for next hop from SRH of current uIP packet.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
int rpl_ext_header_srh_update(void)
Process and update SRH in-place, i.e.
bool rpl_ext_header_remove(void)
Removes all RPL extension headers.
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.
uip_ds6_netif_t uip_ds6_if
The single interface.
#define UIP_PROTO_HBHO
extension headers types
#define UIP_IP_BUF
Direct access to IPv6 header.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
uint16_t uip_ext_len
The length of the extension headers.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Header file for the logging system.
Header file for the Packet buffer (packetbuf) management.
Routing driver header file.
int(* get_sr_node_ipaddr)(uip_ipaddr_t *ipaddr, const uip_sr_node_t *node)
Returns the global IPv6 address of a source routing node.
An entry in the routing table.
A node in a source routing graph, stored at the root and representing all child-parent relationship.
Header for the Contiki/uIP interface.
Header file for IPv6-related data structures.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Header file for the uIP TCP/IP stack.