Contiki-NG
Loading...
Searching...
No Matches
rpl-ext-header.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 */
31
32/**
33 * \addtogroup rpl-lite
34 * @{
35 *
36 * \file
37 * Management of extension headers for ContikiRPL.
38 *
39 * \author Vincent Brillault <vincent.brillault@imag.fr>,
40 * Joakim Eriksson <joakime@sics.se>,
41 * Niclas Finne <nfi@sics.se>,
42 * Nicolas Tsiftes <nvt@sics.se>,
43 * Simon Duquennoy <simon.duquennoy@inria.fr>
44 */
45
46#include "net/routing/routing.h"
47#include "net/routing/rpl-lite/rpl.h"
48#include "net/ipv6/uip-sr.h"
49#include "net/packetbuf.h"
50
51/* Log configuration */
52#include "sys/log.h"
53#define LOG_MODULE "RPL"
54#define LOG_LEVEL LOG_LEVEL_RPL
55
56/*---------------------------------------------------------------------------*/
57int
58rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr)
59{
60 struct uip_routing_hdr *rh_header;
61 uip_sr_node_t *dest_node;
62 uip_sr_node_t *root_node;
63
64 /* Look for routing ext header */
65 rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING);
66
67 if(!rpl_is_addr_in_our_dag(&UIP_IP_BUF->destipaddr)) {
68 return 0;
69 }
70
71 root_node = uip_sr_get_node(NULL, &curr_instance.dag.dag_id);
72 dest_node = uip_sr_get_node(NULL, &UIP_IP_BUF->destipaddr);
73
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)) {
77 /* Routing header found or the packet destined for a direct child of the root.
78 * The next hop should be already copied as the IPv6 destination
79 * address, via rpl_ext_header_srh_update. We turn this address into a link-local to enable
80 * forwarding to next hop */
81 uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr);
82 uip_create_linklocal_prefix(ipaddr);
83 return 1;
84 }
85
86 LOG_DBG("no SRH found\n");
87 return 0;
88}
89/*---------------------------------------------------------------------------*/
90static bool
91srh_is_valid(struct uip_routing_hdr *rh_header,
92 struct uip_rpl_srh_hdr *srh_header)
93{
94 uip_ipaddr_t hop_addr;
95 uip_ipaddr_copy(&hop_addr, &UIP_IP_BUF->destipaddr);
96
97 uint8_t segments_left = rh_header->seg_left;
98 uint8_t ext_len = rh_header->len * 8 + 8;
99 uint8_t cmpri = srh_header->cmpr >> 4;
100 uint8_t cmpre = srh_header->cmpr & 0x0f;
101 uint8_t padding = srh_header->pad >> 4;
102 uint8_t path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
103
104 bool prev_hop_is_my_addr = false;
105 uint8_t my_addr_count = 0;
106 for(uint8_t i = path_len - segments_left; i < path_len; i++) {
107 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
108 ptrdiff_t rh_offset = (uint8_t *)rh_header - uip_buf;
109 size_t addr_offset = RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
110
111 if(rh_offset + addr_offset + 16 - cmpr > UIP_BUFSIZE) {
112 return false;
113 }
114
115 uint8_t *addr_ptr = (uint8_t *)rh_header + addr_offset;
116 memcpy((uint8_t *)&hop_addr + cmpr, addr_ptr, 16 - cmpr);
117
118 LOG_DBG("Processing SRH hop %u, IP addr ", i);
119 LOG_DBG_6ADDR(&hop_addr);
120 LOG_DBG_("\n");
121
122 /*
123 * RFC 6554 states that "To detect loops in the SRH, a router MUST
124 * determine if the SRH includes multiple addresses assigned to
125 * any interface on that router. If such addresses appear more
126 * than once and are separated by at least one address not
127 * assigned to that router, the router MUST drop the packet."
128 *
129 * We add further checks of unacceptable next hop addresses.
130 */
131 if(uip_ds6_is_my_addr(&hop_addr) ||
132 uip_ds6_is_my_maddr(&hop_addr)) {
133 my_addr_count++;
134 if(!prev_hop_is_my_addr && my_addr_count > 1) {
135 LOG_WARN("SRH contains a loop\n");
136 return false;
137 }
138 prev_hop_is_my_addr = true;
139 } else {
140 prev_hop_is_my_addr = false;
141 }
142
143 if(uip_is_addr_mcast(&hop_addr) ||
144 uip_is_addr_unspecified(&hop_addr) ||
145 uip_is_addr_loopback(&hop_addr)) {
146 LOG_WARN("SRH contains an invalid next hop address\n");
147 return false;
148 }
149 }
150
151 return true;
152}
153/*---------------------------------------------------------------------------*/
154int
155rpl_ext_header_srh_update(void)
156{
157 struct uip_routing_hdr *rh_header;
158 struct uip_rpl_srh_hdr *srh_header;
159 uint8_t cmpri, cmpre;
160 uint8_t ext_len;
161 uint8_t padding;
162 uint8_t path_len;
163 uint8_t segments_left;
164 uip_ipaddr_t current_dest_addr;
165
166 /* Look for routing ext header */
167 rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING);
168
169 if(rh_header == NULL || rh_header->routing_type != RPL_RH_TYPE_SRH) {
170 LOG_INFO("SRH not found\n");
171 return 0;
172 }
173
174 /* Parse SRH */
175 srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN);
176 segments_left = rh_header->seg_left;
177 ext_len = rh_header->len * 8 + 8;
178 cmpri = srh_header->cmpr >> 4;
179 cmpre = srh_header->cmpr & 0x0f;
180 padding = srh_header->pad >> 4;
181 path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
182 (void)path_len;
183
184 LOG_INFO("read SRH, path len %u, segments left %u, Cmpri %u, Cmpre %u, ext len %u (padding %u)\n",
185 path_len, segments_left, cmpri, cmpre, ext_len, padding);
186
187 /* Update SRH in-place */
188 if(segments_left == 0) {
189 /* We are the final destination, do nothing */
190 } else if(segments_left > path_len) {
191 /* Discard the packet because of a parameter problem. */
192 LOG_ERR("SRH with too many segments left (%u > %u)\n",
193 segments_left, path_len);
194 return 0;
195 } else {
196 if(!srh_is_valid(rh_header, srh_header)) {
197 LOG_ERR("Invalid SRH hop sequence\n");
198 return 0;
199 }
200
201 uint8_t i = path_len - segments_left; /* The index of the next address to be visited */
202 uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
203 ptrdiff_t rh_offset = (uint8_t *)rh_header - uip_buf;
204 size_t addr_offset = RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
205
206 if(rh_offset + addr_offset + 16 - cmpr > UIP_BUFSIZE) {
207 LOG_ERR("Invalid SRH address pointer\n");
208 return 0;
209 }
210
211 uint8_t *addr_ptr = ((uint8_t *)rh_header) + addr_offset;
212
213 /* As per RFC6554: swap the IPv6 destination address with address[i] */
214
215 /* First, copy the current IPv6 destination address */
216 uip_ipaddr_copy(&current_dest_addr, &UIP_IP_BUF->destipaddr);
217 /* Second, update the IPv6 destination address with addresses[i] */
218 memcpy(((uint8_t *)&UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
219 /* Third, write current_dest_addr to addresses[i] */
220 memcpy(addr_ptr, ((uint8_t *)&current_dest_addr) + cmpr, 16 - cmpr);
221
222 /* Update segments left field */
223 rh_header->seg_left--;
224
225 LOG_INFO("SRH next hop ");
226 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
227 LOG_INFO_("\n");
228 }
229
230 return 1;
231}
232/*---------------------------------------------------------------------------*/
233/* Utility function for SRH. Counts the number of bytes in common between
234 * two addresses at p1 and p2. */
235static int
236count_matching_bytes(const void *p1, const void *p2, size_t n)
237{
238 for(size_t i = 0; i < n; i++) {
239 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
240 return i;
241 }
242 }
243 return n;
244}
245/*---------------------------------------------------------------------------*/
246/* Used by rpl_ext_header_update to insert a RPL SRH extension header. This
247 * is used at the root, to initiate downward routing. Returns 1 on success,
248 * 0 on failure.
249*/
250static int
251insert_srh_header(void)
252{
253 /* Implementation of RFC6554 */
254 uint8_t path_len;
255 uint8_t ext_len;
256 uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */
257 uint8_t *hop_ptr;
258 uint8_t padding;
259 uip_sr_node_t *dest_node;
260 uip_sr_node_t *root_node;
261 uip_sr_node_t *node;
262 uip_ipaddr_t node_addr;
263
264 /* Always insest SRH as first extension header */
265 struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
266 struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
267
268 LOG_INFO("SRH creating source routing header with destination ");
269 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
270 LOG_INFO_(" \n");
271
272 /* Construct source route. We do not do this recursively to keep the runtime stack usage constant. */
273
274 /* Get link of the destination and root */
275
276 if(!rpl_is_addr_in_our_dag(&UIP_IP_BUF->destipaddr)) {
277 /* The destination is not in our DAG, skip SRH insertion */
278 LOG_INFO("SRH destination not in our DAG, skip SRH insertion\n");
279 return 1;
280 }
281
282 dest_node = uip_sr_get_node(NULL, &UIP_IP_BUF->destipaddr);
283 if(dest_node == NULL) {
284 /* The destination is not found, skip SRH insertion */
285 LOG_INFO("SRH node not found, skip SRH insertion\n");
286 return 1;
287 }
288
289 root_node = uip_sr_get_node(NULL, &curr_instance.dag.dag_id);
290 if(root_node == NULL) {
291 LOG_ERR("SRH root node not found\n");
292 return 0;
293 }
294
295 if(!uip_sr_is_addr_reachable(NULL, &UIP_IP_BUF->destipaddr)) {
296 LOG_ERR("SRH no path found to destination\n");
297 return 0;
298 }
299
300 /* Compute path length and compression factors (we use cmpri == cmpre) */
301 path_len = 0;
302 node = dest_node->parent;
303 /* For simplicity, we use cmpri = cmpre */
304 cmpri = 15;
305 cmpre = 15;
306
307 /* Note that in case of a direct child (node == root_node), we insert
308 SRH anyway, as RFC 6553 mandates that routed datagrams must include
309 SRH or the RPL option (or both) */
310
311 while(node != NULL && node != root_node) {
312
313 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
314
315 /* How many bytes in common between all nodes in the path? */
316 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &UIP_IP_BUF->destipaddr, 16));
317 cmpre = cmpri;
318
319 LOG_INFO("SRH Hop ");
320 LOG_INFO_6ADDR(&node_addr);
321 LOG_INFO_("\n");
322 node = node->parent;
323 path_len++;
324 }
325
326 /* Extension header length: fixed headers + (n-1) * (16-ComprI) + (16-ComprE)*/
327 ext_len = RPL_RH_LEN + RPL_SRH_LEN
328 + (path_len - 1) * (16 - cmpre)
329 + (16 - cmpri);
330
331 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
332 ext_len += padding;
333
334 LOG_INFO("SRH path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
335 path_len, cmpri, cmpre, ext_len, padding);
336
337 /* Check if there is enough space to store the extension header */
338 if(uip_len + ext_len > UIP_LINK_MTU) {
339 LOG_ERR("packet too long: impossible to add source routing header (%u bytes)\n", ext_len);
340 return 0;
341 }
342
343 /* Move existing ext headers and payload ext_len further */
344 memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len,
345 uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN);
346 memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len);
347
348 /* Insert source routing header (as first ext header) */
349 rh_hdr->next = UIP_IP_BUF->proto;
350 UIP_IP_BUF->proto = UIP_PROTO_ROUTING;
351
352 /* Initialize IPv6 Routing Header */
353 rh_hdr->len = (ext_len - 8) / 8;
354 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
355 rh_hdr->seg_left = path_len;
356
357 /* Initialize RPL Source Routing Header */
358 srh_hdr->cmpr = (cmpri << 4) + cmpre;
359 srh_hdr->pad = padding << 4;
360
361 /* Initialize addresses field (the actual source route).
362 * From last to first. */
363 node = dest_node;
364 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */
365
366 while(node != NULL && node->parent != root_node) {
367 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
368
369 hop_ptr -= (16 - cmpri);
370 memcpy(hop_ptr, ((uint8_t*)&node_addr) + cmpri, 16 - cmpri);
371
372 node = node->parent;
373 }
374
375 /* The next hop (i.e. node whose parent is the root) is placed as the current IPv6 destination */
376 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
377 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr);
378
379 /* Update the IPv6 length field */
380 uipbuf_add_ext_hdr(ext_len);
381 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
382
383 return 1;
384}
385/*---------------------------------------------------------------------------*/
386int
387rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset)
388{
389 int down;
390 int rank_error_signaled;
391 int loop_detected;
392 uint16_t sender_rank;
393 uint8_t sender_closer;
394 rpl_nbr_t *sender;
395
396 /* RFC 6553: "This option has an alignment requirement of 2n." */
397 if(opt_offset < 0 || opt_offset & 1) {
398 LOG_ERR("Invalid RPL option offset: %d\n", opt_offset);
399 return 0;
400 }
401
402 struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf;
403 struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset);
404
405 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
406 || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL
407 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
408 LOG_ERR("hop-by-hop extension header has wrong size or type (%u %u %u)\n",
409 hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len);
410 return 0; /* Drop */
411 }
412
413 if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) {
414 LOG_ERR("unknown instance: %u\n", rpl_opt->instance);
415 return 0; /* Drop */
416 }
417
418 if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
419 LOG_ERR("forward error!\n");
420 return 0; /* Drop */
421 }
422
423 down = (rpl_opt->flags & RPL_HDR_OPT_DOWN) ? 1 : 0;
424 sender_rank = UIP_HTONS(rpl_opt->senderrank);
425 sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER));
426 rank_error_signaled = (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0;
427 sender_closer = sender_rank < curr_instance.dag.rank;
428 loop_detected = down != sender_closer;
429
430 LOG_INFO("ext hdr: packet from ");
431 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
432 LOG_INFO_(" to ");
433 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
434 LOG_INFO_(" going %s, sender closer %d (%d < %d), rank error %u, loop detected %u\n",
435 down == 1 ? "down" : "up", sender_closer, sender_rank,
436 curr_instance.dag.rank, rank_error_signaled, loop_detected);
437
438 if(loop_detected) {
439 /* Set forward error flag */
440 rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR;
441 }
442
443 return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled);
444}
445/*---------------------------------------------------------------------------*/
446/* In-place update of the RPL HBH extension header, when already present
447 * in the uIP packet. Used by insert_hbh_header and rpl_ext_header_update.
448 * Returns 1 on success, 0 on failure. */
449static int
450update_hbh_header(void)
451{
452 struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
453 struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
454
455 if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) {
456 if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
457 || rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
458
459 LOG_ERR("hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len);
460 return 0; /* Drop */
461 }
462
463 if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) {
464 LOG_ERR("unable to add/update hop-by-hop extension header: incorrect instance\n");
465 return 0; /* Drop */
466 }
467
468 /* Update sender rank and instance, will update flags next */
469 rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank);
470 rpl_opt->instance = curr_instance.instance_id;
471 }
472
473 return 1;
474}
475/*---------------------------------------------------------------------------*/
476/* Used by rpl_ext_header_update on packets without an HBH extension header,
477 * for packets initated by non-root nodes.
478 * Inserts and initalizes (via update_hbh_header) a RPL HBH ext header.
479 * Returns 1 on success, 0 on failure. */
480static int
481insert_hbh_header(void)
482{
483 struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
484 struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
485
486 /* Insert hop-by-hop header */
487 LOG_INFO("creating hop-by-hop option\n");
488 if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) {
489 LOG_ERR("packet too long: impossible to add hop-by-hop option\n");
490 return 0;
491 }
492
493 /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */
494 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN);
495 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
496
497 /* Insert HBH header (as first ext header) */
498 hbh_hdr->next = UIP_IP_BUF->proto;
499 UIP_IP_BUF->proto = UIP_PROTO_HBHO;
500
501 /* Initialize HBH option */
502 hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
503 rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
504 rpl_opt->opt_len = RPL_HDR_OPT_LEN;
505 rpl_opt->flags = 0;
506 rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank);
507 rpl_opt->instance = curr_instance.instance_id;
508
509 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
510 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
511
512 /* Update header before returning */
513 return update_hbh_header();
514}
515/*---------------------------------------------------------------------------*/
516int
517rpl_ext_header_update(void)
518{
519 if(!curr_instance.used
520 || uip_is_addr_linklocal(&UIP_IP_BUF->destipaddr)
521 || uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
522 return 1;
523 }
524
525 if(rpl_dag_root_is_root()) {
526 /* At the root, remove headers if any, and insert SRH or HBH
527 * (SRH is inserted only if the destination is down the DODAG) */
528 rpl_ext_header_remove();
529 /* Insert SRH (if needed) */
530 return insert_srh_header();
531 } else {
532 if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)
533 && UIP_IP_BUF->ttl == uip_ds6_if.cur_hop_limit) {
534 /* Insert HBH option at source. Checking the address is not sufficient because
535 * in non-storing mode, a packet may go up and then down the same path again */
536 return insert_hbh_header();
537 } else {
538 /* Update HBH option at forwarders */
539 return update_hbh_header();
540 }
541 }
542}
543/*---------------------------------------------------------------------------*/
544bool
545rpl_ext_header_remove(void)
546{
547 uint8_t *prev_proto_ptr;
548 uint8_t protocol;
549 uint16_t ext_len;
550 uint8_t *next_header;
551 struct uip_ext_hdr *ext_ptr;
552 struct uip_ext_hdr_opt *opt_ptr;
553
554 next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true);
555 if(next_header == NULL) {
556 return true;
557 }
558 ext_ptr = (struct uip_ext_hdr *)next_header;
559 prev_proto_ptr = &UIP_IP_BUF->proto;
560
561 while(uip_is_proto_ext_hdr(protocol)) {
562 opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2);
563 if(protocol == UIP_PROTO_ROUTING ||
564 (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
565 /* Remove ext header */
566 *prev_proto_ptr = ext_ptr->next;
567 ext_len = ext_ptr->len * 8 + 8;
568 if(uipbuf_add_ext_hdr(-ext_len) == false) {
569 return false;
570 }
571
572 /* Update length field and move rest of packet to the "left" */
573 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
574 if(uip_len <= next_header - uip_buf) {
575 /* No more data to move. */
576 return false;
577 }
578 memmove(next_header, next_header + ext_len,
579 uip_len - (next_header - uip_buf));
580
581 /* Update loop variables */
582 protocol = *prev_proto_ptr;
583 } else {
584 /* move to the ext hdr */
585 next_header = uipbuf_get_next_header(next_header,
586 uip_len - (next_header - uip_buf),
587 &protocol, false);
588 if(next_header == NULL) {
589 /* Processing finished. */
590 break;
591 }
592 ext_ptr = (struct uip_ext_hdr *)next_header;
593 prev_proto_ptr = &ext_ptr->next;
594 }
595 }
596
597 return true;
598}
599/** @}*/
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.
Definition rpl-dag.c:680
int rpl_is_addr_in_our_dag(const uip_ipaddr_t *addr)
Tells whether a given global IPv6 address is in our current DAG.
Definition rpl-dag.c:158
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.
Definition uip-sr.c:82
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition uip.h:1725
int uip_sr_is_addr_reachable(const void *graph, const uip_ipaddr_t *addr)
Telle whether an address is reachable, i.e.
Definition uip-sr.c:95
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1860
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition uip.h:1766
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition uip-ds6.c:75
#define uip_is_addr_loopback(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition uip.h:1711
#define UIP_PROTO_HBHO
extension headers types
Definition uip.h:1663
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition uip.h:71
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition uip.h:1157
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition uip.h:969
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition uip.h:465
uint16_t uip_ext_len
The length of the extension headers.
Definition uip6.c:122
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition uip6.c:159
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition uipopt.h:92
#define UIP_LINK_MTU
The maximum transmission unit at the IP layer.
Definition uipopt.h:145
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.
Definition routing.h:97
All information related to a RPL neighbor.
Definition rpl-types.h:136
A node in a source routing graph, stored at the root and representing all child-parent relationship.
Definition uip-sr.h:92
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116
Source routing support.