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
30/**
31 * \file
32 * Management of extension headers for ContikiRPL.
33 *
34 * \author Vincent Brillault <vincent.brillault@imag.fr>,
35 * Joakim Eriksson <joakime@sics.se>,
36 * Niclas Finne <nfi@sics.se>,
37 * Nicolas Tsiftes <nvt@sics.se>.
38 */
39
40/**
41 * \addtogroup uip
42 * @{
43 */
44
45#include "net/routing/routing.h"
46#include "net/ipv6/uip.h"
47#include "net/ipv6/tcpip.h"
48#include "net/ipv6/uip-ds6.h"
49#include "net/ipv6/uip-sr.h"
50#include "net/routing/rpl-classic/rpl-private.h"
51#include "net/packetbuf.h"
52
53#include "sys/log.h"
54
55#define LOG_MODULE "RPL"
56#define LOG_LEVEL LOG_LEVEL_RPL
57
58#include <limits.h>
59#include <string.h>
60
61/*---------------------------------------------------------------------------*/
62int
63rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset)
64{
65 rpl_instance_t *instance;
66 int down;
67 uint16_t sender_rank;
68 uint8_t sender_closer;
69 uip_ds6_route_t *route;
70 rpl_parent_t *sender;
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);
73
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) {
77
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);
80 return 0; /* Drop */
81 }
82
83 instance = rpl_get_instance(rpl_opt->instance);
84 if(instance == NULL) {
85 LOG_ERR("Unknown instance: %u\n", rpl_opt->instance);
86 return 0;
87 }
88
89 if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
90 LOG_ERR("Forward error!\n");
91 /*
92 * We should try to repair it by removing the neighbor that caused
93 * the packet to be forwarded in the first place. We drop any
94 * routes that go through the neighbor that sent the packet to us.
95 */
96 if(RPL_IS_STORING(instance)) {
97 route = uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr);
98 if(route != NULL) {
99 uip_ds6_route_rm(route);
100 }
101 }
102 RPL_STAT(rpl_stats.forward_errors++);
103 /* Trigger DAO retransmission. */
104 rpl_reset_dio_timer(instance);
105 /* Drop the packet because it is not routable. */
106 return 0;
107 }
108
109 if(!instance->current_dag->joined) {
110 LOG_ERR("No DAG in the instance\n");
111 return 0;
112 }
113 down = 0;
114 if(rpl_opt->flags & RPL_HDR_OPT_DOWN) {
115 down = 1;
116 }
117
118 sender_rank = UIP_HTONS(rpl_opt->senderrank);
119 sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER));
120
121 if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) {
122 /* A rank error was signaled -- attempt to repair it by updating
123 the sender's rank from the ext header. */
124 sender->rank = sender_rank;
125 if(RPL_IS_NON_STORING(instance)) {
126 /*
127 * Select DAG and preferred parent only in non-storing mode. In
128 * storing mode, a parent switch would result in an immediate
129 * no-path DAO transmission, dropping the current incoming
130 * packet.
131 */
132 rpl_select_dag(instance, sender);
133 }
134 }
135
136 sender_closer = sender_rank < instance->current_dag->rank;
137
138
139 LOG_DBG("Packet going %s, sender%s closer (%d < %d)\n",
140 down == 1 ? "down" : "up",
141 sender_closer ? "" : " not",
142 sender_rank,
143 instance->current_dag->rank);
144
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,
148 sender_closer);
149 /* Attempt to repair the loop by sending a unicast DIO back to the
150 sender so that it gets a fresh update of our rank. */
151 if(sender != NULL) {
152 instance->unicast_dio_target = sender;
153 rpl_schedule_unicast_dio_immediately(instance);
154 }
155
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");
159 /* Packet must be dropped and dio trickle timer reset, see
160 RFC6550 - 11.2.2.2 */
161 rpl_reset_dio_timer(instance);
162 return 0;
163 }
164
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;
168 return 1;
169 }
170
171 LOG_DBG("Rank OK\n");
172 return 1;
173}
174/*---------------------------------------------------------------------------*/
175int
176rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr)
177{
178#if RPL_WITH_NON_STORING
179 struct uip_routing_hdr *rh_header;
180 rpl_dag_t *dag;
181 uip_sr_node_t *dest_node;
182 uip_sr_node_t *root_node;
183
184 /* Look for the routing ext header. */
185 rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING);
186
187 dag = rpl_get_dag(&UIP_IP_BUF->destipaddr);
188 root_node = uip_sr_get_node(dag, &dag->dag_id);
189 dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr);
190
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)) {
194 /*
195 * A routing header was found or the packet was destined for a
196 * direct child of the root. The next hop should already be copied
197 * as the IPv6 destination address, via
198 * rpl_ext_header_srh_update. We turn this address into a
199 * link-local address to enable forwarding to next hop.
200 */
201 uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr);
202 uip_create_linklocal_prefix(ipaddr);
203 return 1;
204 }
205
206 return 0;
207#else /* RPL_WITH_NON_STORING */
208 return 0; /* SRH not found */
209#endif /* RPL_WITH_NON_STORING */
210}
211/*---------------------------------------------------------------------------*/
212int
213rpl_ext_header_srh_update(void)
214{
215#if RPL_WITH_NON_STORING
216 struct uip_routing_hdr *rh_header;
217 struct uip_rpl_srh_hdr *srh_header;
218
219 /* Look for routing ext header */
220 rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len,
221 UIP_PROTO_ROUTING);
222
223 if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) {
224 /* SRH found, now look for next hop */
225 uint8_t cmpri, cmpre;
226 uint8_t ext_len;
227 uint8_t padding;
228 uint8_t path_len;
229 uint8_t segments_left;
230 uip_ipaddr_t current_dest_addr;
231
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;
239 (void)path_len;
240
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);
243
244 if(segments_left == 0) {
245 /* We are the final destination, do nothing. */
246 } else if(segments_left > path_len) {
247 /* Discard the packet because of a parameter problem. */
248 LOG_ERR("SRH with too many segments left (%u > %u)\n",
249 segments_left, path_len);
250 return 0;
251 } else {
252 /* The index of the next address to be visited. */
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));
257
258 if(rh_offset + addr_offset + 16 - cmpr > UIP_BUFSIZE) {
259 LOG_ERR("Invalid SRH address pointer\n");
260 return 0;
261 }
262
263 uint8_t *addr_ptr = ((uint8_t *)rh_header) + addr_offset;
264
265 /* As per RFC6554: swap the IPv6 destination address and address[i]. */
266
267 /* First, copy the current IPv6 destination address. */
268 uip_ipaddr_copy(&current_dest_addr, &UIP_IP_BUF->destipaddr);
269 /* Second, update the IPv6 destination address with addresses[i]. */
270 memcpy(((uint8_t *)&UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
271 /* Third, write current_dest_addr to addresses[i]. */
272 memcpy(addr_ptr, ((uint8_t *)&current_dest_addr) + cmpr, 16 - cmpr);
273
274 /* Update segments left field */
275 rh_header->seg_left--;
276
277 LOG_INFO("SRH next hop ");
278 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
279 LOG_INFO_("\n");
280 }
281 return 1;
282 }
283
284 return 0;
285#else /* RPL_WITH_NON_STORING */
286 return 0; /* SRH not found */
287#endif /* RPL_WITH_NON_STORING */
288}
289/*---------------------------------------------------------------------------*/
290static int
291count_matching_bytes(const void *p1, const void *p2, size_t n)
292{
293 for(size_t i = 0; i < n; i++) {
294 if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
295 return i;
296 }
297 }
298 return n;
299}
300/*---------------------------------------------------------------------------*/
301static int
302insert_srh_header(void)
303{
304 /* Implementation of RFC6554. */
305 uint8_t path_len;
306 uint8_t ext_len;
307 uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header. */
308 uint8_t *hop_ptr;
309 uint8_t padding;
310 uip_sr_node_t *dest_node;
311 uip_sr_node_t *root_node;
312 uip_sr_node_t *node;
313 rpl_dag_t *dag;
314 uip_ipaddr_t node_addr;
315
316 /* Always insert the SRH as the first extension header. */
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);
319
320 LOG_INFO("SRH creating source routing header with destination ");
321 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
322 LOG_INFO_("\n");
323
324 /* Construct source route. We do not do this recursively to keep the
325 runtime stack usage constant. */
326
327 /* Get link of the destination and root. */
328 dag = rpl_get_dag(&UIP_IP_BUF->destipaddr);
329 if(dag == NULL) {
330 LOG_ERR("SRH DAG not found\n");
331 return 0;
332 }
333
334 dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr);
335 if(dest_node == NULL) {
336 /* The destination was not found, skip SRH insertion. */
337 return 1;
338 }
339
340 root_node = uip_sr_get_node(dag, &dag->dag_id);
341 if(root_node == NULL) {
342 LOG_ERR("SRH root node not found\n");
343 return 0;
344 }
345
346 if(!uip_sr_is_addr_reachable(dag, &UIP_IP_BUF->destipaddr)) {
347 LOG_ERR("SRH no path found to destination\n");
348 return 0;
349 }
350
351 /* Compute path length and compression factors. (We use cmpri == cmpre.) */
352 path_len = 0;
353 node = dest_node->parent;
354 /* For simplicity, we use cmpri = cmpre. */
355 cmpri = 15;
356 cmpre = 15;
357
358 if(node == root_node) {
359 LOG_DBG("SRH no need to insert SRH\n");
360 return 1;
361 }
362
363 while(node != NULL && node != root_node) {
364
365 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
366
367 /* How many bytes in common between all nodes in the path? */
368 cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &UIP_IP_BUF->destipaddr, 16));
369 cmpre = cmpri;
370
371 LOG_DBG("SRH Hop ");
372 LOG_DBG_6ADDR(&node_addr);
373 LOG_DBG_("\n");
374 node = node->parent;
375 path_len++;
376 }
377
378 /* Extension header length:
379 fixed headers + (n - 1) * (16 - ComprI) + (16 - ComprE). */
380 ext_len = RPL_RH_LEN + RPL_SRH_LEN
381 + (path_len - 1) * (16 - cmpre)
382 + (16 - cmpri);
383
384 padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
385 ext_len += padding;
386
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);
389
390 /* Check if there is enough space to store the extension header. */
391 if(uip_len + ext_len > UIP_LINK_MTU) {
392 LOG_ERR("Too long packet: impossible to add SRH (%u bytes)\n", ext_len);
393 return 0;
394 }
395
396 /* Move existing ext headers and payload ext_len further. */
397 memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len,
398 uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN);
399 memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len);
400
401 /* Insert source routing header (as first ext header). */
402 rh_hdr->next = UIP_IP_BUF->proto;
403 UIP_IP_BUF->proto = UIP_PROTO_ROUTING;
404
405 /* Initialize IPv6 Routing Header. */
406 rh_hdr->len = (ext_len - 8) / 8;
407 rh_hdr->routing_type = RPL_RH_TYPE_SRH;
408 rh_hdr->seg_left = path_len;
409
410 /* Initialize RPL Source Routing Header. */
411 srh_hdr->cmpr = (cmpri << 4) + cmpre;
412 srh_hdr->pad = padding << 4;
413
414 /* Initialize the addresses field (the actual source route).
415 From last to first. */
416 node = dest_node;
417
418 /* Pointer where to write the next hop compressed address. */
419 hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding;
420
421 while(node != NULL && node->parent != root_node) {
422 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
423
424 hop_ptr -= (16 - cmpri);
425 memcpy(hop_ptr, ((uint8_t *)&node_addr) + cmpri, 16 - cmpri);
426
427 node = node->parent;
428 }
429
430 /* The next hop (i.e. node whose parent is the root) is placed as
431 the current IPv6 destination. */
432 NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
433 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr);
434
435 /* Update the IPv6 length field. */
436 uipbuf_add_ext_hdr(ext_len);
437 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
438
439 return 1;
440}
441/*---------------------------------------------------------------------------*/
442static int
443update_hbh_header(void)
444{
445 rpl_instance_t *instance;
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);
449
450 if(UIP_IP_BUF->proto == UIP_PROTO_HBHO &&
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) {
454
455 LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n",
456 rpl_opt->opt_len);
457 return 0; /* Drop */
458 }
459
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");
463 return 0; /* Drop */
464 }
465
466 LOG_INFO("Updating RPL option\n");
467 /* Update sender rank and instance, will update flags next. */
468 rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank);
469 rpl_opt->instance = instance->instance_id;
470
471 if(RPL_IS_STORING(instance)) {
472 /* In non-storing mode, downwards traffic does not have the HBH option. */
473
474 /*
475 * Check the direction of the down flag, as per Section
476 * 11.2.2.3, which states that if a packet is going down it
477 * should in general not go back up again. If this happens, a
478 * RPL_HDR_OPT_FWD_ERR should be flagged.
479 */
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");
484 /* We should send back the packet to the originating parent,
485 but it is not feasible yet, so we send a No-Path DAO instead. */
486 LOG_WARN("RPL generate No-Path DAO\n");
487 parent = rpl_get_parent((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
488 if(parent != NULL) {
489 dao_output_target(parent, &UIP_IP_BUF->destipaddr,
490 RPL_ZERO_LIFETIME);
491 }
492 /* Drop packet. */
493 return 0;
494 }
495 } else {
496 /*
497 * Set the down extension flag correctly as described in
498 * Section 11.2 of RFC6550. If the packet progresses along a
499 * DAO route, the down flag should be set.
500 */
501 if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) {
502 /* No route was found, so this packet will go towards the
503 RPL root. If so, we should not set the down flag. */
504 rpl_opt->flags &= ~RPL_HDR_OPT_DOWN;
505 LOG_DBG("RPL option going up\n");
506 } else {
507 /* A DAO route was found so we set the down flag. */
508 rpl_opt->flags |= RPL_HDR_OPT_DOWN;
509 LOG_DBG("RPL option going down\n");
510 }
511 }
512 }
513 }
514
515 return 1;
516}
517/*---------------------------------------------------------------------------*/
518static int
519insert_hbh_header(const rpl_instance_t *instance)
520{
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));
523
524 /* Insert hop-by-hop header. */
525 LOG_DBG("Creating hop-by-hop option\n");
526 if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) {
527 LOG_ERR("Too long packet: impossible to add hop-by-hop option\n");
528 return 0;
529 }
530
531 /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further. */
532 memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0),
533 uip_len - UIP_IPH_LEN);
534 memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
535
536 /* Insert the HBH header as the first ext header. */
537 hbh_hdr->next = UIP_IP_BUF->proto;
538 UIP_IP_BUF->proto = UIP_PROTO_HBHO;
539
540 /* Initialize the HBH option. */
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;
544 rpl_opt->flags = 0;
545 rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank);
546 rpl_opt->instance = instance->instance_id;
547
548 uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
549 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
550
551 /* Update the header before returning. */
552 return update_hbh_header();
553}
554/*---------------------------------------------------------------------------*/
555bool
556rpl_ext_header_remove(void)
557{
558 uint8_t *prev_proto_ptr;
559 uint8_t protocol;
560 uint16_t ext_len;
561 uint8_t *next_header;
562 struct uip_ext_hdr *ext_ptr;
563 struct uip_ext_hdr_opt *opt_ptr;
564
565 next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true);
566 if(next_header == NULL) {
567 return true;
568 }
569
570 ext_ptr = (struct uip_ext_hdr *)next_header;
571 prev_proto_ptr = &UIP_IP_BUF->proto;
572
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)) {
577 /* Remove ext header */
578 *prev_proto_ptr = ext_ptr->next;
579 ext_len = ext_ptr->len * 8 + 8;
580 if(uipbuf_add_ext_hdr(-ext_len) == false) {
581 return false;
582 }
583
584 /* Update length field and rest of packet to the "left". */
585 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
586 if(uip_len <= next_header - uip_buf) {
587 /* No more data to move. */
588 return false;
589 }
590 memmove(next_header, next_header + ext_len,
591 uip_len - (next_header - uip_buf));
592
593 /* Update loop variables. */
594 protocol = *prev_proto_ptr;
595 } else {
596 /* Move to the ext hdr. */
597 next_header = uipbuf_get_next_header(next_header,
598 uip_len - (next_header - uip_buf),
599 &protocol, false);
600 if(next_header == NULL) {
601 /* Processing finished. */
602 break;
603 }
604 ext_ptr = (struct uip_ext_hdr *)next_header;
605 prev_proto_ptr = &ext_ptr->next;
606 }
607 }
608
609 return true;
610}
611/*---------------------------------------------------------------------------*/
612int
613rpl_ext_header_update(void)
614{
615 if(default_instance == NULL || default_instance->current_dag == NULL ||
616 uip_is_addr_linklocal(&UIP_IP_BUF->destipaddr) ||
617 uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
618 return 1;
619 }
620
621 if(default_instance->current_dag->rank == ROOT_RANK(default_instance)) {
622 /* At the root, remove headers if any, and insert SRH or HBH.
623 (SRH is inserted only if the destination is in the DODAG.) */
624 rpl_ext_header_remove();
625 if(rpl_get_dag(&UIP_IP_BUF->destipaddr) != NULL) {
626 /* dest is in a DODAG; the packet is going down. */
627 if(RPL_IS_NON_STORING(default_instance)) {
628 return insert_srh_header();
629 } else {
630 return insert_hbh_header(default_instance);
631 }
632 } else {
633 /* dest is outside of DODAGs; no ext header is needed. */
634 return 1;
635 }
636 } else {
637 if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)
638 && UIP_IP_BUF->ttl == uip_ds6_if.cur_hop_limit) {
639 /*
640 * Insert a HBH option at the source. Checking the address is
641 * insufficient because in non-storing mode, a packet may go up
642 * and then down the same path again.
643 */
644 return insert_hbh_header(default_instance);
645 } else {
646 /* Update HBH option at forwarders. */
647 return update_hbh_header();
648 }
649 }
650}
651/** @}*/
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
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_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:93
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Definition uipopt.h:154
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
RPL DAG structure.
Definition rpl.h:138
RPL instance structure.
Definition rpl.h:222
An entry in the routing table.
A node in a source routing graph, stored at the root and representing all child-parent relationship.
Definition uip-sr.h:92
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.
Definition uip-nd6.c:116
Source routing support.
Header file for the uIP TCP/IP stack.