47 #include "net/routing/rpl-lite/rpl.h" 53 #define LOG_MODULE "RPL" 54 #define LOG_LEVEL LOG_LEVEL_RPL 60 struct uip_routing_hdr *rh_header;
65 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len, UIP_PROTO_ROUTING);
74 if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) ||
75 (dest_node != NULL && root_node != NULL &&
76 dest_node->parent == root_node)) {
82 uip_create_linklocal_prefix(ipaddr);
86 LOG_DBG(
"no SRH found\n");
93 struct uip_routing_hdr *rh_header;
94 struct uip_rpl_srh_hdr *srh_header;
99 uint8_t segments_left;
100 uip_ipaddr_t current_dest_addr;
103 rh_header = (
struct uip_routing_hdr *)uipbuf_search_header(
uip_buf,
uip_len, UIP_PROTO_ROUTING);
105 if(rh_header == NULL || rh_header->routing_type != RPL_RH_TYPE_SRH) {
106 LOG_INFO(
"SRH not found\n");
111 srh_header = (
struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN);
112 segments_left = rh_header->seg_left;
113 ext_len = rh_header->len * 8 + 8;
114 cmpri = srh_header->cmpr >> 4;
115 cmpre = srh_header->cmpr & 0x0f;
116 padding = srh_header->pad >> 4;
117 path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
120 LOG_INFO(
"read SRH, path len %u, segments left %u, Cmpri %u, Cmpre %u, ext len %u (padding %u)\n",
121 path_len, segments_left, cmpri, cmpre, ext_len, padding);
124 if(segments_left == 0) {
126 }
else if(segments_left > path_len) {
128 LOG_ERR(
"SRH with too many segments left (%u > %u)\n",
129 segments_left, path_len);
132 uint8_t i = path_len - segments_left;
133 uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
134 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
141 memcpy(((uint8_t *)&
UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
143 memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr);
146 rh_header->seg_left--;
148 LOG_INFO(
"SRH next hop ");
159 count_matching_bytes(
const void *p1,
const void *p2,
size_t n)
162 for(i = 0; i < n; i++) {
163 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
175 insert_srh_header(
void)
180 uint8_t cmpri, cmpre;
186 uip_ipaddr_t node_addr;
189 struct uip_routing_hdr *rh_hdr = (
struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
190 struct uip_rpl_srh_hdr *srh_hdr = (
struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
192 LOG_INFO(
"SRH creating source routing header with destination ");
202 LOG_INFO(
"SRH destination not in our DAG, skip SRH insertion\n");
207 if(dest_node == NULL) {
209 LOG_INFO(
"SRH node not found, skip SRH insertion\n");
214 if(root_node == NULL) {
215 LOG_ERR(
"SRH root node not found\n");
220 LOG_ERR(
"SRH no path found to destination\n");
226 node = dest_node->parent;
235 while(node != NULL && node != root_node) {
240 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &
UIP_IP_BUF->destipaddr, 16));
243 LOG_INFO(
"SRH Hop ");
244 LOG_INFO_6ADDR(&node_addr);
251 ext_len = RPL_RH_LEN + RPL_SRH_LEN
252 + (path_len - 1) * (16 - cmpre)
255 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
258 LOG_INFO(
"SRH path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
259 path_len, cmpri, cmpre, ext_len, padding);
263 LOG_ERR(
"packet too long: impossible to add source routing header (%u bytes)\n", ext_len);
277 rh_hdr->len = (ext_len - 8) / 8;
278 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
279 rh_hdr->seg_left = path_len;
282 srh_hdr->cmpr = (cmpri << 4) + cmpre;
283 srh_hdr->pad = padding << 4;
288 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding;
290 while(node != NULL && node->parent != root_node) {
293 hop_ptr -= (16 - cmpri);
294 memcpy(hop_ptr, ((uint8_t*)&node_addr) + cmpri, 16 - cmpri);
304 uipbuf_add_ext_hdr(ext_len);
314 int rank_error_signaled;
316 uint16_t sender_rank;
317 uint8_t sender_closer;
319 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)ext_buf;
320 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset);
322 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
323 || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL
324 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
325 LOG_ERR(
"hop-by-hop extension header has wrong size or type (%u %u %u)\n",
326 hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len);
330 if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) {
331 LOG_ERR(
"unknown instance: %u\n", rpl_opt->instance);
335 if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
336 LOG_ERR(
"forward error!\n");
340 down = (rpl_opt->flags & RPL_HDR_OPT_DOWN) ? 1 : 0;
341 sender_rank =
UIP_HTONS(rpl_opt->senderrank);
342 sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER));
343 rank_error_signaled = (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0;
344 sender_closer = sender_rank < curr_instance.dag.rank;
345 loop_detected = (down && !sender_closer) || (!down && sender_closer);
347 LOG_INFO(
"ext hdr: packet from ");
351 LOG_INFO_(
" going %s, sender closer %d (%d < %d), rank error %u, loop detected %u\n",
352 down == 1 ?
"down" :
"up", sender_closer, sender_rank,
353 curr_instance.dag.rank, rank_error_signaled, loop_detected);
357 rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR;
360 return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled);
367 update_hbh_header(
void)
369 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
370 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
373 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
374 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
376 LOG_ERR(
"hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len);
380 if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) {
381 LOG_ERR(
"unable to add/update hop-by-hop extension header: incorrect instance\n");
386 rpl_opt->senderrank =
UIP_HTONS(curr_instance.dag.rank);
387 rpl_opt->instance = curr_instance.instance_id;
398 insert_hbh_header(
void)
400 struct uip_hbho_hdr *hbh_hdr = (
struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
401 struct uip_ext_hdr_opt_rpl *rpl_opt = (
struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
404 LOG_INFO(
"creating hop-by-hop option\n");
406 LOG_ERR(
"packet too long: impossible to add hop-by-hop option\n");
411 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0),
uip_len - UIP_IPH_LEN);
412 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
419 hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
420 rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
421 rpl_opt->opt_len = RPL_HDR_OPT_LEN;
423 rpl_opt->senderrank =
UIP_HTONS(curr_instance.dag.rank);
424 rpl_opt->instance = curr_instance.instance_id;
426 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
430 return update_hbh_header();
436 if(!curr_instance.used
447 return insert_srh_header();
453 return insert_hbh_header();
456 return update_hbh_header();
464 uint8_t *prev_proto_ptr;
467 uint8_t *next_header;
468 struct uip_ext_hdr *ext_ptr;
469 struct uip_ext_hdr_opt *opt_ptr;
471 next_header = uipbuf_get_next_header(
uip_buf,
uip_len, &protocol,
true);
472 if(next_header == NULL) {
475 ext_ptr = (
struct uip_ext_hdr *)next_header;
478 while(uip_is_proto_ext_hdr(protocol)) {
479 opt_ptr = (
struct uip_ext_hdr_opt *)(next_header + 2);
480 if(protocol == UIP_PROTO_ROUTING ||
481 (protocol ==
UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
483 *prev_proto_ptr = ext_ptr->next;
484 ext_len = ext_ptr->len * 8 + 8;
485 if(uipbuf_add_ext_hdr(-ext_len) ==
false) {
495 memmove(next_header, next_header + ext_len,
499 protocol = *prev_proto_ptr;
502 next_header = uipbuf_get_next_header(next_header,
505 if(next_header == NULL) {
509 ext_ptr = (
struct uip_ext_hdr *)next_header;
510 prev_proto_ptr = &ext_ptr->next;
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
#define UIP_IP_BUF
Direct access to IPv6 header.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
#define UIP_PROTO_HBHO
extension headers types
int rpl_is_addr_in_our_dag(const uip_ipaddr_t *addr)
Tells whether a given global IPv6 address is in our current DAG.
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_dag_root_is_root(void)
Tells whether we are DAG root or not.
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.
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.
All information related to a RPL neighbor.
#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.
uip_ds6_netif_t uip_ds6_if
The single interface.
int rpl_process_hbh(rpl_nbr_t *sender, uint16_t sender_rank, int loop_detected, int rank_error_signaled)
Processes Hop-by-Hop (HBH) Extension Header of a packet currently being forwrded. ...
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.