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) {
236 }
else if(segments_left > path_len) {
238 LOG_ERR(
"SRH with too many segments left (%u > %u)\n",
239 segments_left, path_len);
242 uint8_t i = path_len - segments_left;
243 uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
244 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
251 memcpy(((uint8_t *)&
UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
253 memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr);
256 rh_header->seg_left--;
258 LOG_INFO(
"SRH next hop ");
272 count_matching_bytes(
const void *p1,
const void *p2,
size_t n)
275 for(i = 0; i < n; i++) {
276 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
284 insert_srh_header(
void)
289 uint8_t cmpri, cmpre;
296 uip_ipaddr_t node_addr;
299 struct uip_routing_hdr *rh_hdr = (
struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
300 struct uip_rpl_srh_hdr *srh_hdr = (
struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
302 LOG_INFO(
"SRH creating source routing header with destination ");
312 LOG_ERR(
"SRH DAG not found\n");
317 if(dest_node == NULL) {
323 if(root_node == NULL) {
324 LOG_ERR(
"SRH root node not found\n");
329 LOG_ERR(
"SRH no path found to destination\n");
335 node = dest_node->parent;
340 if(node == root_node) {
341 LOG_DBG(
"SRH no need to insert SRH\n");
345 while(node != NULL && node != root_node) {
350 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &
UIP_IP_BUF->destipaddr, 16));
354 LOG_DBG_6ADDR(&node_addr);
361 ext_len = RPL_RH_LEN + RPL_SRH_LEN
362 + (path_len - 1) * (16 - cmpre)
365 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
368 LOG_DBG(
"SRH Path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
369 path_len, cmpri, cmpre, ext_len, padding);
373 LOG_ERR(
"Packet too long: impossible to add source routing header (%u bytes)\n", ext_len);
387 rh_hdr->len = (ext_len - 8) / 8;
388 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
389 rh_hdr->seg_left = path_len;
392 srh_hdr->cmpr = (cmpri << 4) + cmpre;
393 srh_hdr->pad = padding << 4;
398 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding;
400 while(node != NULL && node->parent != root_node) {
403 hop_ptr -= (16 - cmpri);
404 memcpy(hop_ptr, ((uint8_t*)&node_addr) + cmpri, 16 - cmpri);
414 uipbuf_add_ext_hdr(ext_len);
421 update_hbh_header(
void)
424 rpl_parent_t *parent;
425 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
426 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2);
429 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
430 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
432 LOG_ERR(
"Hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len);
436 instance = rpl_get_instance(rpl_opt->instance);
437 if(instance == NULL || !instance->used || !instance->current_dag->joined) {
438 LOG_ERR(
"Unable to add/update hop-by-hop extension header: incorrect instance\n");
442 LOG_INFO(
"Updating RPL option\n");
444 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
445 rpl_opt->instance = instance->instance_id;
447 if(RPL_IS_STORING(instance)) {
452 if((rpl_opt->flags & RPL_HDR_OPT_DOWN)) {
453 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
454 rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR;
455 LOG_WARN(
"RPL forwarding error\n");
458 LOG_WARN(
"RPL generate No-Path DAO\n");
459 parent = rpl_get_parent((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
461 dao_output_target(parent, &
UIP_IP_BUF->destipaddr, RPL_ZERO_LIFETIME);
470 if(uip_ds6_route_lookup(&
UIP_IP_BUF->destipaddr) == NULL) {
473 rpl_opt->flags &= ~RPL_HDR_OPT_DOWN;
474 LOG_DBG(
"RPL option going up\n");
477 rpl_opt->flags |= RPL_HDR_OPT_DOWN;
478 LOG_DBG(
"RPL option going down\n");
490 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
491 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
494 LOG_DBG(
"Creating hop-by-hop option\n");
496 LOG_ERR(
"Packet too long: impossible to add hop-by-hop option\n");
501 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0),
uip_len - UIP_IPH_LEN);
502 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
509 hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
510 rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
511 rpl_opt->opt_len = RPL_HDR_OPT_LEN;
513 rpl_opt->senderrank =
UIP_HTONS(instance->current_dag->rank);
514 rpl_opt->instance = instance->instance_id;
516 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
520 return update_hbh_header();
526 uint8_t *prev_proto_ptr;
529 uint8_t *next_header;
530 struct uip_ext_hdr *ext_ptr;
531 struct uip_ext_hdr_opt *opt_ptr;
533 next_header = uipbuf_get_next_header(
uip_buf,
uip_len, &protocol,
true);
534 if(next_header == NULL) {
537 ext_ptr = (
struct uip_ext_hdr *)next_header;
540 while(uip_is_proto_ext_hdr(protocol)) {
541 opt_ptr = (
struct uip_ext_hdr_opt *)(next_header + 2);
542 if(protocol == UIP_PROTO_ROUTING ||
543 (protocol ==
UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
545 *prev_proto_ptr = ext_ptr->next;
546 ext_len = ext_ptr->len * 8 + 8;
547 if(uipbuf_add_ext_hdr(-ext_len) ==
false) {
557 memmove(next_header, next_header + ext_len,
561 protocol = *prev_proto_ptr;
564 next_header = uipbuf_get_next_header(next_header,
567 if(next_header == NULL) {
571 ext_ptr = (
struct uip_ext_hdr *)next_header;
572 prev_proto_ptr = &ext_ptr->next;
582 if(default_instance == NULL || default_instance->current_dag == NULL
587 if(default_instance->current_dag->rank ==
ROOT_RANK(default_instance)) {
591 if(rpl_get_dag(&
UIP_IP_BUF->destipaddr) != NULL) {
593 if(RPL_IS_NON_STORING(default_instance)) {
594 return insert_srh_header();
596 return insert_hbh_header(default_instance);
607 return insert_hbh_header(default_instance);
610 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.