Contiki-NG
rpl-icmp6.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, 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/**
34 * \file
35 * ICMP6 I/O for RPL control messages.
36 *
37 * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
38 * Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
39 * Mathieu Pouillot <m.pouillot@watteco.com>
40 * George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
41 */
42
43/**
44 * \addtogroup uip
45 * @{
46 */
47
48#include "net/ipv6/tcpip.h"
49#include "net/ipv6/uip.h"
50#include "net/ipv6/uip-ds6.h"
51#include "net/ipv6/uip-nd6.h"
52#include "net/ipv6/uip-sr.h"
53#include "net/ipv6/uip-icmp6.h"
54#include "net/routing/rpl-classic/rpl-private.h"
55#include "net/packetbuf.h"
57#include "random.h"
58
59#include "sys/log.h"
60
61#include <limits.h>
62#include <string.h>
63
64#define LOG_MODULE "RPL"
65#define LOG_LEVEL LOG_LEVEL_RPL
66
67/*---------------------------------------------------------------------------*/
68#define RPL_DIO_GROUNDED 0x80
69#define RPL_DIO_MOP_SHIFT 3
70#define RPL_DIO_MOP_MASK 0x38
71#define RPL_DIO_PREFERENCE_MASK 0x07
72
73/*---------------------------------------------------------------------------*/
74static void dis_input(void);
75static void dio_input(void);
76static void dao_input(void);
77static void dao_ack_input(void);
78
79static void dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
80 uint8_t lifetime, uint8_t seq_no);
81
82/* some debug callbacks useful when debugging RPL networks */
83#ifdef RPL_DEBUG_DIO_INPUT
84void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
85#endif
86
87#ifdef RPL_DEBUG_DAO_OUTPUT
88void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
89#endif
90
91static uint8_t dao_sequence = RPL_LOLLIPOP_INIT;
92
93#if RPL_WITH_MULTICAST
94static uip_mcast6_route_t *mcast_group;
95#endif
96/*---------------------------------------------------------------------------*/
97/* Initialise RPL ICMPv6 message handlers */
98UIP_ICMP6_HANDLER(dis_handler, ICMP6_RPL, RPL_CODE_DIS, dis_input);
99UIP_ICMP6_HANDLER(dio_handler, ICMP6_RPL, RPL_CODE_DIO, dio_input);
100UIP_ICMP6_HANDLER(dao_handler, ICMP6_RPL, RPL_CODE_DAO, dao_input);
101UIP_ICMP6_HANDLER(dao_ack_handler, ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
102/*---------------------------------------------------------------------------*/
103
104#if RPL_WITH_DAO_ACK
105static uip_ds6_route_t *
106find_route_entry_by_dao_ack(uint8_t seq)
107{
108 uip_ds6_route_t *re;
109 re = uip_ds6_route_head();
110 while(re != NULL) {
111 if(re->state.dao_seqno_out == seq && RPL_ROUTE_IS_DAO_PENDING(re)) {
112 /* found it! */
113 return re;
114 }
115 re = uip_ds6_route_next(re);
116 }
117 return NULL;
118}
119#endif /* RPL_WITH_DAO_ACK */
120
121#if RPL_WITH_STORING
122/* prepare for forwarding of DAO */
123static uint8_t
124prepare_for_dao_fwd(uint8_t sequence, uip_ds6_route_t *rep)
125{
126 /* not pending - or pending but not a retransmission */
127 RPL_LOLLIPOP_INCREMENT(dao_sequence);
128
129 /* set DAO pending and sequence numbers */
130 rep->state.dao_seqno_in = sequence;
131 rep->state.dao_seqno_out = dao_sequence;
132 RPL_ROUTE_SET_DAO_PENDING(rep);
133 return dao_sequence;
134}
135#endif /* RPL_WITH_STORING */
136/*---------------------------------------------------------------------------*/
137static int
138get_global_addr(uip_ipaddr_t *addr)
139{
140 int i;
141 int state;
142 uip_ipaddr_t *prefix = NULL;
143 uint8_t prefix_length = 0;
144 rpl_dag_t *dag = rpl_get_any_dag();
145
146 if(dag != NULL && dag->prefix_info.length != 0) {
147 prefix = &dag->prefix_info.prefix;
148 prefix_length = dag->prefix_info.length;
149 }
150
151 for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
152 state = uip_ds6_if.addr_list[i].state;
153 if(uip_ds6_if.addr_list[i].isused &&
154 state == ADDR_PREFERRED &&
155 !uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr) &&
156 (prefix == NULL || uip_ipaddr_prefixcmp(prefix, &uip_ds6_if.addr_list[i].ipaddr, prefix_length))) {
157 memcpy(addr, &uip_ds6_if.addr_list[i].ipaddr, sizeof(uip_ipaddr_t));
158 return 1;
159 }
160 }
161 return 0;
162}
163/*---------------------------------------------------------------------------*/
164static uint32_t
165get32(uint8_t *buffer, int pos)
166{
167 return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
168 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
169}
170/*---------------------------------------------------------------------------*/
171static void
172set32(uint8_t *buffer, int pos, uint32_t value)
173{
174 buffer[pos++] = value >> 24;
175 buffer[pos++] = (value >> 16) & 0xff;
176 buffer[pos++] = (value >> 8) & 0xff;
177 buffer[pos++] = value & 0xff;
178}
179/*---------------------------------------------------------------------------*/
180static uint16_t
181get16(uint8_t *buffer, int pos)
182{
183 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
184}
185/*---------------------------------------------------------------------------*/
186static void
187set16(uint8_t *buffer, int pos, uint16_t value)
188{
189 buffer[pos++] = value >> 8;
190 buffer[pos++] = value & 0xff;
191}
192/*---------------------------------------------------------------------------*/
194rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
195{
197
198 if((nbr = uip_ds6_nbr_lookup(from)) == NULL) {
199 if((nbr = uip_ds6_nbr_add(from, (uip_lladdr_t *)
200 packetbuf_addr(PACKETBUF_ADDR_SENDER),
201 0, NBR_REACHABLE, reason, data)) != NULL) {
202 LOG_INFO("Neighbor added to neighbor cache ");
203 LOG_INFO_6ADDR(from);
204 LOG_INFO_(", ");
205 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
206 LOG_INFO_("\n");
207 }
208 }
209
210 return nbr;
211}
212/*---------------------------------------------------------------------------*/
213static void
214dis_input(void)
215{
216 rpl_instance_t *instance;
217 rpl_instance_t *end;
218
219 /* DAG Information Solicitation */
220 LOG_INFO("Received a DIS from ");
221 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
222 LOG_INFO_("\n");
223
224 for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
225 instance < end; ++instance) {
226 if(instance->used == 1) {
227 if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
228#if RPL_LEAF_ONLY
229 LOG_INFO("LEAF ONLY Multicast DIS will NOT reset DIO timer\n");
230#else /* !RPL_LEAF_ONLY */
231 LOG_DBG("Multicast DIS => reset DIO timer\n");
232 rpl_reset_dio_timer(instance);
233#endif /* !RPL_LEAF_ONLY */
234 } else {
235 /* Check if this neighbor should be added according to the policy. */
237 NBR_TABLE_REASON_RPL_DIS, NULL) == NULL) {
238 LOG_ERR("Out of Memory, not sending unicast DIO, DIS from ");
239 LOG_ERR_6ADDR(&UIP_IP_BUF->srcipaddr);
240 LOG_ERR_(", ");
241 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
242 LOG_ERR_("\n");
243 } else {
244 LOG_DBG("Unicast DIS, reply to sender\n");
245 dio_output(instance, &UIP_IP_BUF->srcipaddr);
246 }
247 /* } */
248 }
249 }
250 }
251 uipbuf_clear();
252}
253/*---------------------------------------------------------------------------*/
254void
255dis_output(uip_ipaddr_t *addr)
256{
257 unsigned char *buffer;
258 uip_ipaddr_t tmpaddr;
259
260 /*
261 * DAG Information Solicitation - 2 bytes reserved
262 * 0 1 2
263 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
264 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265 * | Flags | Reserved | Option(s)...
266 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267 */
268
269 buffer = UIP_ICMP_PAYLOAD;
270 buffer[0] = buffer[1] = 0;
271
272 if(addr == NULL) {
274 addr = &tmpaddr;
275 }
276
277 LOG_INFO("Sending a DIS to ");
278 LOG_INFO_6ADDR(addr);
279 LOG_INFO_("\n");
280
281 uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
282}
283/*---------------------------------------------------------------------------*/
284static void
285dio_input(void)
286{
287 unsigned char *buffer;
288 uint8_t buffer_length;
289 rpl_dio_t dio;
290 uint8_t subopt_type;
291 int i;
292 int len;
293 uip_ipaddr_t from;
294
295 memset(&dio, 0, sizeof(dio));
296
297 /* Set default values in case the DIO configuration option is missing. */
298 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
299 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
300 dio.dag_redund = RPL_DIO_REDUNDANCY;
301 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
302 dio.dag_max_rankinc = RPL_MAX_RANKINC;
303 dio.ocp = RPL_OF_OCP;
304 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
305 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
306
307 uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
308
309 /* DAG Information Object */
310 LOG_INFO("Received a DIO from ");
311 LOG_INFO_6ADDR(&from);
312 LOG_INFO_("\n");
313
314 buffer_length = uip_len - uip_l3_icmp_hdr_len;
315
316 /* Process the DIO base option. */
317 i = 0;
318 buffer = UIP_ICMP_PAYLOAD;
319
320 dio.instance_id = buffer[i++];
321 dio.version = buffer[i++];
322 dio.rank = get16(buffer, i);
323 i += 2;
324
325 LOG_DBG("Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
326 (unsigned)dio.instance_id,
327 (unsigned)dio.version,
328 (unsigned)dio.rank);
329
330 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
331 dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
332 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
333
334 dio.dtsn = buffer[i++];
335 /* two reserved bytes */
336 i += 2;
337
338 memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
339 i += sizeof(dio.dag_id);
340
341 LOG_DBG("Incoming DIO (dag_id, pref) = (");
342 LOG_DBG_6ADDR(&dio.dag_id);
343 LOG_DBG_(", %u)\n", dio.preference);
344
345 /* Check if there are any DIO suboptions. */
346 for(; i < buffer_length; i += len) {
347 subopt_type = buffer[i];
348 if(subopt_type == RPL_OPTION_PAD1) {
349 len = 1;
350 } else {
351 /* Suboption with a two-byte header + payload */
352 len = 2 + buffer[i + 1];
353 }
354
355 if(len + i > buffer_length) {
356 LOG_WARN("Invalid DIO packet\n");
357 RPL_STAT(rpl_stats.malformed_msgs++);
358 goto discard;
359 }
360
361 LOG_DBG("Incoming DIO (option, length) = (%u, %u)\n",
362 subopt_type, len);
363
364 switch(subopt_type) {
365 case RPL_OPTION_PAD1:
366 case RPL_OPTION_PADN:
367 LOG_DBG("PAD %u bytes\n", len);
368 break;
369 case RPL_OPTION_DAG_METRIC_CONTAINER:
370 if(len < 6) {
371 LOG_WARN("Invalid DAG MC, len = %d\n", len);
372 RPL_STAT(rpl_stats.malformed_msgs++);
373 goto discard;
374 }
375 dio.mc.type = buffer[i + 2];
376 dio.mc.flags = buffer[i + 3] << 1;
377 dio.mc.flags |= buffer[i + 4] >> 7;
378 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
379 dio.mc.prec = buffer[i + 4] & 0xf;
380 dio.mc.length = buffer[i + 5];
381
382 if(dio.mc.type == RPL_DAG_MC_NONE) {
383 /* No metric container: do nothing */
384 } else if(dio.mc.type == RPL_DAG_MC_ETX) {
385 dio.mc.obj.etx = get16(buffer, i + 6);
386
387 LOG_DBG("DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
388 (unsigned)dio.mc.type,
389 (unsigned)dio.mc.flags,
390 (unsigned)dio.mc.aggr,
391 (unsigned)dio.mc.prec,
392 (unsigned)dio.mc.length,
393 (unsigned)dio.mc.obj.etx);
394 } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
395 dio.mc.obj.energy.flags = buffer[i + 6];
396 dio.mc.obj.energy.energy_est = buffer[i + 7];
397 } else {
398 LOG_WARN("Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
399 goto discard;
400 }
401 break;
402 case RPL_OPTION_ROUTE_INFO:
403 if(len < 9) {
404 LOG_WARN("Invalid destination prefix option, len = %d\n", len);
405 RPL_STAT(rpl_stats.malformed_msgs++);
406 goto discard;
407 }
408
409 /* The flags field includes the preference value. */
410 dio.destination_prefix.length = buffer[i + 2];
411 dio.destination_prefix.flags = buffer[i + 3];
412 dio.destination_prefix.lifetime = get32(buffer, i + 4);
413
414 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
415 dio.destination_prefix.length <= 128) {
416 LOG_INFO("Copying destination prefix\n");
417 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
418 (dio.destination_prefix.length + 7) / 8);
419 } else {
420 LOG_WARN("Invalid route info option, len = %d\n", len);
421 RPL_STAT(rpl_stats.malformed_msgs++);
422 goto discard;
423 }
424
425 break;
426 case RPL_OPTION_DAG_CONF:
427 if(len != 16) {
428 LOG_WARN("Invalid DAG configuration option, len = %d\n", len);
429 RPL_STAT(rpl_stats.malformed_msgs++);
430 goto discard;
431 }
432
433 /* Path control field not yet implemented - at i + 2 */
434 dio.dag_intdoubl = buffer[i + 3];
435 dio.dag_intmin = buffer[i + 4];
436 dio.dag_redund = buffer[i + 5];
437 dio.dag_max_rankinc = get16(buffer, i + 6);
438 dio.dag_min_hoprankinc = get16(buffer, i + 8);
439 dio.ocp = get16(buffer, i + 10);
440 /* buffer + 12 is reserved */
441 dio.default_lifetime = buffer[i + 13];
442 dio.lifetime_unit = get16(buffer, i + 14);
443 LOG_INFO("DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
444 dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
445 dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
446 dio.default_lifetime, dio.lifetime_unit);
447 break;
448 case RPL_OPTION_PREFIX_INFO:
449 if(len != 32) {
450 LOG_WARN("Invalid DAG prefix info, len != 32\n");
451 RPL_STAT(rpl_stats.malformed_msgs++);
452 goto discard;
453 }
454 dio.prefix_info.length = buffer[i + 2];
455
456 if(dio.prefix_info.length > sizeof(uip_ipaddr_t) * 8) {
457 LOG_WARN("Invalid DAG prefix info, len %u > %u\n",
458 dio.prefix_info.length, (unsigned)(sizeof(uip_ipaddr_t) * 8));
459 RPL_STAT(rpl_stats.malformed_msgs++);
460 goto discard;
461 }
462
463 dio.prefix_info.flags = buffer[i + 3];
464 /* valid lifetime is ingnored for now - at i + 4 */
465 /* preferred lifetime stored in lifetime */
466 dio.prefix_info.lifetime = get32(buffer, i + 8);
467 /* 32-bit reserved at i + 12 */
468 LOG_INFO("Copying prefix information\n");
469 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
470 break;
471 default:
472 LOG_WARN("Unsupported suboption type in DIO: %u\n",
473 (unsigned)subopt_type);
474 }
475 }
476
477#ifdef RPL_DEBUG_DIO_INPUT
478 RPL_DEBUG_DIO_INPUT(&from, &dio);
479#endif
480
481 rpl_process_dio(&from, &dio);
482
483discard:
484 uipbuf_clear();
485}
486/*---------------------------------------------------------------------------*/
487void
488dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
489{
490 unsigned char *buffer;
491 int pos;
492 int is_root;
493 rpl_dag_t *dag = instance->current_dag;
494#if !RPL_LEAF_ONLY
495 uip_ipaddr_t addr;
496#endif /* !RPL_LEAF_ONLY */
497
498#if RPL_LEAF_ONLY
499 /* In leaf mode, we only send DIO messages as unicasts in response to
500 unicast DIS messages. */
501 if(uc_addr == NULL) {
502 LOG_DBG("LEAF ONLY have multicast addr: skip dio_output\n");
503 return;
504 }
505#endif /* RPL_LEAF_ONLY */
506
507 /* DAG Information Object */
508 pos = 0;
509
510 buffer = UIP_ICMP_PAYLOAD;
511 buffer[pos++] = instance->instance_id;
512 buffer[pos++] = dag->version;
513 is_root = (dag->rank == ROOT_RANK(instance));
514
515#if RPL_LEAF_ONLY
516 LOG_DBG("LEAF ONLY DIO rank set to RPL_INFINITE_RANK\n");
517 set16(buffer, pos, RPL_INFINITE_RANK);
518#else /* RPL_LEAF_ONLY */
519 set16(buffer, pos, dag->rank);
520#endif /* RPL_LEAF_ONLY */
521 pos += 2;
522
523 buffer[pos] = 0;
524 if(dag->grounded) {
525 buffer[pos] |= RPL_DIO_GROUNDED;
526 }
527
528 buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
529 buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
530 pos++;
531
532 buffer[pos++] = instance->dtsn_out;
533
534 if(RPL_DIO_REFRESH_DAO_ROUTES && is_root && uc_addr == NULL) {
535 /* Request new DAO to refresh route. We do not do this for unicast DIO
536 * in order to avoid DAO messages after a DIS-DIO update,
537 * or upon unicast DIO probing. */
538 RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
539 }
540
541 /* reserved 2 bytes */
542 buffer[pos++] = 0; /* flags */
543 buffer[pos++] = 0; /* reserved */
544
545 memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
546 pos += 16;
547
548#if !RPL_LEAF_ONLY
549 if(instance->mc.type != RPL_DAG_MC_NONE) {
550 instance->of->update_metric_container(instance);
551
552 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
553 buffer[pos++] = 6;
554 buffer[pos++] = instance->mc.type;
555 buffer[pos++] = instance->mc.flags >> 1;
556 buffer[pos] = (instance->mc.flags & 1) << 7;
557 buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
558 if(instance->mc.type == RPL_DAG_MC_ETX) {
559 buffer[pos++] = 2;
560 set16(buffer, pos, instance->mc.obj.etx);
561 pos += 2;
562 } else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
563 buffer[pos++] = 2;
564 buffer[pos++] = instance->mc.obj.energy.flags;
565 buffer[pos++] = instance->mc.obj.energy.energy_est;
566 } else {
567 LOG_ERR("Unable to send DIO because of unhandled DAG MC type %u\n",
568 (unsigned)instance->mc.type);
569 return;
570 }
571 }
572#endif /* !RPL_LEAF_ONLY */
573
574 /* Always add a DAG configuration option. */
575 buffer[pos++] = RPL_OPTION_DAG_CONF;
576 buffer[pos++] = 14;
577 buffer[pos++] = 0; /* No Auth, PCS = 0 */
578 buffer[pos++] = instance->dio_intdoubl;
579 buffer[pos++] = instance->dio_intmin;
580 buffer[pos++] = instance->dio_redundancy;
581 set16(buffer, pos, instance->max_rankinc);
582 pos += 2;
583 set16(buffer, pos, instance->min_hoprankinc);
584 pos += 2;
585 /* OCP is in the DAG_CONF option */
586 set16(buffer, pos, instance->of->ocp);
587 pos += 2;
588 buffer[pos++] = 0; /* reserved */
589 buffer[pos++] = instance->default_lifetime;
590 set16(buffer, pos, instance->lifetime_unit);
591 pos += 2;
592
593 /* Check if we have a prefix to send also. */
594 if(dag->prefix_info.length > 0) {
595 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
596 buffer[pos++] = 30; /* always 30 bytes + 2 long */
597 buffer[pos++] = dag->prefix_info.length;
598 buffer[pos++] = dag->prefix_info.flags;
599 set32(buffer, pos, dag->prefix_info.lifetime);
600 pos += 4;
601 set32(buffer, pos, dag->prefix_info.lifetime);
602 pos += 4;
603 memset(&buffer[pos], 0, 4);
604 pos += 4;
605 memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
606 pos += 16;
607 LOG_DBG("Sending prefix info in DIO for ");
608 LOG_DBG_6ADDR(&dag->prefix_info.prefix);
609 LOG_DBG_("\n");
610 } else {
611 LOG_DBG("No prefix to announce (len %d)\n",
612 dag->prefix_info.length);
613 }
614
615#if RPL_LEAF_ONLY
616 if(LOG_DBG_ENABLED) {
617 if(uc_addr == NULL) {
618 LOG_DBG("LEAF ONLY sending unicast-DIO from multicast-DIO\n");
619 }
620 }
621
622 LOG_INFO("Sending unicast-DIO with rank %u to ",
623 (unsigned)dag->rank);
624 LOG_INFO_6ADDR(uc_addr);
625 LOG_INFO_("\n");
626 uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
627#else /* RPL_LEAF_ONLY */
628 /* Unicast requests get unicast replies! */
629 if(uc_addr == NULL) {
630 LOG_INFO("Sending a multicast-DIO with rank %u\n",
631 (unsigned)instance->current_dag->rank);
633 uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
634 } else {
635 LOG_INFO("Sending unicast-DIO with rank %u to ",
636 (unsigned)instance->current_dag->rank);
637 LOG_INFO_6ADDR(uc_addr);
638 LOG_INFO_("\n");
639 uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
640 }
641#endif /* RPL_LEAF_ONLY */
642}
643/*---------------------------------------------------------------------------*/
644static void
645dao_input_storing(void)
646{
647#if RPL_WITH_STORING
648 uip_ipaddr_t dao_sender_addr;
649 rpl_dag_t *dag;
650 rpl_instance_t *instance;
651 unsigned char *buffer;
652 uint16_t sequence;
653 uint8_t instance_id;
654 uint8_t lifetime;
655 uint8_t prefixlen;
656 uint8_t flags;
657 uint8_t subopt_type;
658 /*
659 uint8_t pathcontrol;
660 uint8_t pathsequence;
661 */
662 uip_ipaddr_t prefix;
663 uip_ds6_route_t *rep;
664 uint8_t buffer_length;
665 int pos;
666 int len;
667 int i;
668 int learned_from;
669 rpl_parent_t *parent;
671 int is_root;
672
673 prefixlen = 0;
674 parent = NULL;
675 memset(&prefix, 0, sizeof(prefix));
676
677 uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
678
679 buffer = UIP_ICMP_PAYLOAD;
680 buffer_length = uip_len - uip_l3_icmp_hdr_len;
681
682 pos = 0;
683 instance_id = buffer[pos++];
684
685 instance = rpl_get_instance(instance_id);
686
687 lifetime = instance->default_lifetime;
688
689 flags = buffer[pos++];
690 /* reserved */
691 pos++;
692 sequence = buffer[pos++];
693
694 dag = instance->current_dag;
695 is_root = (dag->rank == ROOT_RANK(instance));
696
697 /* Is the DAG ID present? */
698 if(flags & RPL_DAO_D_FLAG) {
699 if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
700 LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
701 return;
702 }
703 pos += 16;
704 }
705
706 learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
707 RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
708
709 /* Destination Advertisement Object */
710 LOG_DBG("Received a (%s) DAO with sequence number %u from ",
711 learned_from == RPL_ROUTE_FROM_UNICAST_DAO? "unicast": "multicast", sequence);
712 LOG_DBG_6ADDR(&dao_sender_addr);
713 LOG_DBG_("\n");
714
715 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
716 /* Check whether this is a DAO forwarding loop. */
717 parent = rpl_find_parent(dag, &dao_sender_addr);
718 /* check if this is a new DAO registration with an "illegal" rank */
719 /* if we already route to this node it is likely */
720 if(parent != NULL &&
721 DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
722 LOG_WARN("Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
723 DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
724 parent->rank = RPL_INFINITE_RANK;
725 parent->flags |= RPL_PARENT_FLAG_UPDATED;
726 return;
727 }
728
729 /* If we get the DAO from our parent, we also have a loop. */
730 if(parent != NULL && parent == dag->preferred_parent) {
731 LOG_WARN("Loop detected when receiving a unicast DAO from our parent\n");
732 parent->rank = RPL_INFINITE_RANK;
733 parent->flags |= RPL_PARENT_FLAG_UPDATED;
734 return;
735 }
736 }
737
738 /* Check if there are any RPL options present. */
739 for(i = pos; i < buffer_length; i += len) {
740 subopt_type = buffer[i];
741 if(subopt_type == RPL_OPTION_PAD1) {
742 len = 1;
743 } else {
744 /* The option consists of a two-byte header and a payload. */
745 len = 2 + buffer[i + 1];
746 }
747
748 switch(subopt_type) {
749 case RPL_OPTION_TARGET:
750 /* Handle the target option. */
751 prefixlen = buffer[i + 3];
752 if(prefixlen == 0) {
753 /* Ignore option targets with a prefix length of 0. */
754 break;
755 }
756 if(prefixlen > 128) {
757 LOG_ERR("Too large target prefix length %d\n", prefixlen);
758 return;
759 }
760 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
761 LOG_ERR("Insufficient space to copy RPL Target of %d bits\n",
762 prefixlen);
763 return;
764 }
765 memset(&prefix, 0, sizeof(prefix));
766 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
767 break;
768 case RPL_OPTION_TRANSIT:
769 /* The path sequence and control are ignored. */
770 /* pathcontrol = buffer[i + 3];
771 pathsequence = buffer[i + 4];*/
772 lifetime = buffer[i + 5];
773 /* The parent address is also ignored. */
774 break;
775 }
776 }
777
778 LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
779 (unsigned)lifetime, (unsigned)prefixlen);
780 LOG_INFO_6ADDR(&prefix);
781 LOG_INFO_("\n");
782
783#if RPL_WITH_MULTICAST
784 if(uip_is_addr_mcast_global(&prefix)) {
785 /*
786 * "rep" is used for a unicast route which we don't need now; so set NULL so
787 * that operations on "rep" will be skipped.
788 */
789 rep = NULL;
790 mcast_group = uip_mcast6_route_add(&prefix);
791 if(mcast_group) {
792 mcast_group->dag = dag;
793 mcast_group->lifetime = RPL_LIFETIME(instance, lifetime);
794 }
795 goto fwd_dao;
796 }
797#endif
798
799 rep = uip_ds6_route_lookup(&prefix);
800
801 if(lifetime == RPL_ZERO_LIFETIME) {
802 LOG_INFO("No-Path DAO received\n");
803 /* No-Path DAO received; invoke the route purging routine. */
804 if(rep != NULL &&
805 !RPL_ROUTE_IS_NOPATH_RECEIVED(rep) &&
806 rep->length == prefixlen &&
807 uip_ds6_route_nexthop(rep) != NULL &&
808 uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
809 LOG_DBG("Setting expiration timer for prefix ");
810 LOG_DBG_6ADDR(&prefix);
811 LOG_DBG_("\n");
812 RPL_ROUTE_SET_NOPATH_RECEIVED(rep);
813 rep->state.lifetime = RPL_NOPATH_REMOVAL_DELAY;
814
815 /* We forward the incoming No-Path DAO to our parent, if we have
816 one. */
817 if(dag->preferred_parent != NULL &&
818 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
819 uint8_t out_seq;
820 out_seq = prepare_for_dao_fwd(sequence, rep);
821
822 LOG_DBG("Forwarding No-path DAO to parent - out_seq:%d",
823 out_seq);
824 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
825 LOG_DBG_("\n");
826
827 buffer = UIP_ICMP_PAYLOAD;
828 buffer[3] = out_seq; /* add an outgoing seq no before fwd */
829 uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
830 ICMP6_RPL, RPL_CODE_DAO, buffer_length);
831 }
832 }
833 /* independent if we remove or not - ACK the request */
834 if(flags & RPL_DAO_K_FLAG) {
835 /* indicate that we accepted the no-path DAO */
836 uipbuf_clear();
837 dao_ack_output(instance, &dao_sender_addr, sequence,
838 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
839 }
840 return;
841 }
842
843 LOG_INFO("Adding DAO route\n");
844
845 /* Update and add neighbor - if no room - fail. */
846 if((nbr = rpl_icmp6_update_nbr_table(&dao_sender_addr, NBR_TABLE_REASON_RPL_DAO, instance)) == NULL) {
847 LOG_ERR("Out of Memory, dropping DAO from ");
848 LOG_ERR_6ADDR(&dao_sender_addr);
849 LOG_ERR_(", ");
850 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
851 LOG_ERR_("\n");
852 if(flags & RPL_DAO_K_FLAG) {
853 /* signal the failure to add the node */
854 dao_ack_output(instance, &dao_sender_addr, sequence,
855 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
856 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
857 }
858 return;
859 }
860
861 rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
862 if(rep == NULL) {
863 RPL_STAT(rpl_stats.mem_overflows++);
864 LOG_ERR("Could not add a route after receiving a DAO\n");
865 if(flags & RPL_DAO_K_FLAG) {
866 /* signal the failure to add the node */
867 dao_ack_output(instance, &dao_sender_addr, sequence,
868 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
869 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
870 }
871 return;
872 }
873
874 /* set lifetime and clear NOPATH bit */
875 rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
876 RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
877
878#if RPL_WITH_MULTICAST
879fwd_dao:
880#endif
881
882 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
883 int should_ack = 0;
884
885 if(flags & RPL_DAO_K_FLAG) {
886 if(rep != NULL) {
887 /*
888 * check if this route is already installed and we can ack now!
889 * not pending - and same seq-no means that we can ack.
890 * (e.g. the route is installed already so it will not take any
891 * more room that it already takes - so should be ok!)
892 */
893 if((!RPL_ROUTE_IS_DAO_PENDING(rep) &&
894 rep->state.dao_seqno_in == sequence) ||
895 dag->rank == ROOT_RANK(instance)) {
896 should_ack = 1;
897 }
898 }
899 }
900
901 if(dag->preferred_parent != NULL &&
902 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
903 uint8_t out_seq = 0;
904 if(rep != NULL) {
905 /* if this is pending and we get the same seq no it is a retrans */
906 if(RPL_ROUTE_IS_DAO_PENDING(rep) &&
907 rep->state.dao_seqno_in == sequence) {
908 /* keep the same seq-no as before for parent also */
909 out_seq = rep->state.dao_seqno_out;
910 } else {
911 out_seq = prepare_for_dao_fwd(sequence, rep);
912 }
913 }
914
915 LOG_DBG("Forwarding DAO to parent ");
916 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
917 LOG_DBG_(" in seq: %d out seq: %d\n", sequence, out_seq);
918
919 buffer = UIP_ICMP_PAYLOAD;
920 buffer[3] = out_seq; /* add an outgoing seq no before fwd */
921 uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
922 ICMP6_RPL, RPL_CODE_DAO, buffer_length);
923 }
924 if(should_ack) {
925 LOG_DBG("Sending DAO ACK\n");
926 uipbuf_clear();
927 dao_ack_output(instance, &dao_sender_addr, sequence,
928 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
929 }
930 }
931#endif /* RPL_WITH_STORING */
932}
933/*---------------------------------------------------------------------------*/
934static void
935dao_input_nonstoring(void)
936{
937#if RPL_WITH_NON_STORING
938 uip_ipaddr_t dao_sender_addr;
939 uip_ipaddr_t dao_parent_addr;
940 rpl_dag_t *dag;
941 rpl_instance_t *instance;
942 unsigned char *buffer;
943 uint16_t sequence;
944 uint8_t instance_id;
945 uint8_t lifetime;
946 uint8_t prefixlen;
947 uint8_t flags;
948 uint8_t subopt_type;
949 uip_ipaddr_t prefix;
950 uint8_t buffer_length;
951 int pos;
952 int len;
953 int i;
954
955 /* Destination Advertisement Object */
956 LOG_INFO("Received a DAO from ");
957 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
958 LOG_INFO_("\n");
959
960 prefixlen = 0;
961
962 uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
963 memset(&dao_parent_addr, 0, 16);
964
965 buffer = UIP_ICMP_PAYLOAD;
966 buffer_length = uip_len - uip_l3_icmp_hdr_len;
967
968 pos = 0;
969 instance_id = buffer[pos++];
970 instance = rpl_get_instance(instance_id);
971 lifetime = instance->default_lifetime;
972
973 flags = buffer[pos++];
974 /* reserved */
975 pos++;
976 sequence = buffer[pos++];
977
978 dag = instance->current_dag;
979 /* Is the DAG ID present? */
980 if(flags & RPL_DAO_D_FLAG) {
981 if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
982 LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
983 return;
984 }
985 pos += 16;
986 }
987
988 /* Check if there are any RPL options present. */
989 for(i = pos; i < buffer_length; i += len) {
990 subopt_type = buffer[i];
991 if(subopt_type == RPL_OPTION_PAD1) {
992 len = 1;
993 } else {
994 /* The option consists of a two-byte header and a payload. */
995 len = 2 + buffer[i + 1];
996 }
997
998 switch(subopt_type) {
999 case RPL_OPTION_TARGET:
1000 /* Handle the target option. */
1001 prefixlen = buffer[i + 3];
1002 if(prefixlen == 0) {
1003 /* Ignore option targets with a prefix length of 0. */
1004 break;
1005 }
1006 if(prefixlen > 128) {
1007 LOG_ERR("Too large target prefix length %d\n", prefixlen);
1008 return;
1009 }
1010 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
1011 LOG_ERR("Insufficient space to copy RPL Target of %d bits\n",
1012 prefixlen);
1013 return;
1014 }
1015 memset(&prefix, 0, sizeof(prefix));
1016 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
1017 break;
1018 case RPL_OPTION_TRANSIT:
1019 /* The path sequence and control are ignored. */
1020 /* pathcontrol = buffer[i + 3];
1021 pathsequence = buffer[i + 4];*/
1022 lifetime = buffer[i + 5];
1023 if(len >= 20) {
1024 memcpy(&dao_parent_addr, buffer + i + 6, 16);
1025 }
1026 break;
1027 }
1028 }
1029
1030 LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
1031 (unsigned)lifetime, (unsigned)prefixlen);
1032 LOG_INFO_6ADDR(&prefix);
1033 LOG_INFO_(", parent: ");
1034 LOG_INFO_6ADDR(&dao_parent_addr);
1035 LOG_INFO_("\n");
1036
1037 if(lifetime == RPL_ZERO_LIFETIME) {
1038 LOG_DBG("No-Path DAO received\n");
1039 uip_sr_expire_parent(dag, &prefix, &dao_parent_addr);
1040 } else {
1041 if(uip_sr_update_node(dag, &prefix, &dao_parent_addr, RPL_LIFETIME(instance, lifetime)) == NULL) {
1042 LOG_WARN("DAO failed to add link prefix: ");
1043 LOG_WARN_6ADDR(&prefix);
1044 LOG_WARN_(", parent: ");
1045 LOG_WARN_6ADDR(&dao_parent_addr);
1046 LOG_WARN_("\n");
1047 return;
1048 }
1049 }
1050
1051 if(flags & RPL_DAO_K_FLAG) {
1052 LOG_DBG("Sending DAO ACK\n");
1053 uipbuf_clear();
1054 dao_ack_output(instance, &dao_sender_addr, sequence,
1055 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
1056 }
1057#endif /* RPL_WITH_NON_STORING */
1058}
1059/*---------------------------------------------------------------------------*/
1060static void
1061dao_input(void)
1062{
1063 rpl_instance_t *instance;
1064 uint8_t instance_id;
1065
1066 /* Destination Advertisement Object */
1067 LOG_INFO("Received a DAO from ");
1068 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1069 LOG_INFO_("\n");
1070
1071 instance_id = UIP_ICMP_PAYLOAD[0];
1072 instance = rpl_get_instance(instance_id);
1073 if(instance == NULL) {
1074 LOG_INFO("Ignoring a DAO for an unknown RPL instance(%u)\n",
1075 instance_id);
1076 goto discard;
1077 }
1078
1079 if(RPL_IS_STORING(instance)) {
1080 dao_input_storing();
1081 } else if(RPL_IS_NON_STORING(instance)) {
1082 dao_input_nonstoring();
1083 }
1084
1085discard:
1086 uipbuf_clear();
1087}
1088/*---------------------------------------------------------------------------*/
1089#if RPL_WITH_DAO_ACK
1090static void
1091handle_dao_retransmission(void *ptr)
1092{
1093 rpl_parent_t *parent;
1094 uip_ipaddr_t prefix;
1095 rpl_instance_t *instance;
1096
1097 parent = ptr;
1098 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1099 return;
1100 }
1101 instance = parent->dag->instance;
1102
1103 if(instance->my_dao_transmissions >= RPL_DAO_MAX_RETRANSMISSIONS) {
1104 /* No more retransmissions - give up. */
1105 if(instance->lifetime_unit == 0xffff && instance->default_lifetime == 0xff) {
1106 /*
1107 * ContikiRPL was previously using infinite lifetime for routes
1108 * and no DAO_ACK configured. This probably means that the root
1109 * and possibly other nodes might be running an old version that
1110 * does not support DAO ack. Assume that everything is ok for
1111 * now and let the normal repair mechanisms detect any problems.
1112 */
1113 return;
1114 }
1115
1116 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1117 /* Inform the objective function about the timeout. */
1118 instance->of->dao_ack_callback(parent, RPL_DAO_ACK_TIMEOUT);
1119 }
1120
1121 /* Perform local repair and hope to find another parent. */
1122 rpl_local_repair(instance);
1123 return;
1124 }
1125
1126 LOG_INFO("will retransmit DAO - seq:%d trans:%d\n", instance->my_dao_seqno,
1127 instance->my_dao_transmissions);
1128
1129 if(get_global_addr(&prefix) == 0) {
1130 return;
1131 }
1132
1133 ctimer_set(&instance->dao_retransmit_timer,
1134 RPL_DAO_RETRANSMISSION_TIMEOUT / 2 +
1135 (random_rand() % (RPL_DAO_RETRANSMISSION_TIMEOUT / 2)),
1136 handle_dao_retransmission, parent);
1137
1138 instance->my_dao_transmissions++;
1139 dao_output_target_seq(parent, &prefix,
1140 instance->default_lifetime, instance->my_dao_seqno);
1141}
1142#endif /* RPL_WITH_DAO_ACK */
1143/*---------------------------------------------------------------------------*/
1144void
1145dao_output(rpl_parent_t *parent, uint8_t lifetime)
1146{
1147 /* Destination Advertisement Object */
1148 uip_ipaddr_t prefix;
1149
1150 if(get_global_addr(&prefix) == 0) {
1151 LOG_ERR("No global address set for this node - suppressing DAO\n");
1152 return;
1153 }
1154
1155 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1156 return;
1157 }
1158
1159 RPL_LOLLIPOP_INCREMENT(dao_sequence);
1160#if RPL_WITH_DAO_ACK
1161 /* set up the state since this will be the first transmission of DAO */
1162 /* retransmissions will call directly to dao_output_target_seq */
1163 /* keep track of my own sending of DAO for handling ack and loss of ack */
1164 if(lifetime != RPL_ZERO_LIFETIME) {
1165 rpl_instance_t *instance;
1166 instance = parent->dag->instance;
1167
1168 instance->my_dao_seqno = dao_sequence;
1169 instance->my_dao_transmissions = 1;
1170 ctimer_set(&instance->dao_retransmit_timer, RPL_DAO_RETRANSMISSION_TIMEOUT,
1171 handle_dao_retransmission, parent);
1172 }
1173#else
1174 /* We know that we have tried to register so now we are assuming
1175 that we have a down-link - unless this is a zero lifetime one */
1176 parent->dag->instance->has_downward_route = lifetime != RPL_ZERO_LIFETIME;
1177#endif /* RPL_WITH_DAO_ACK */
1178
1179 /* Sending a DAO with own prefix as target */
1180 dao_output_target(parent, &prefix, lifetime);
1181}
1182/*---------------------------------------------------------------------------*/
1183void
1184dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
1185{
1186 dao_output_target_seq(parent, prefix, lifetime, dao_sequence);
1187}
1188/*---------------------------------------------------------------------------*/
1189static void
1190dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
1191 uint8_t lifetime, uint8_t seq_no)
1192{
1193 rpl_dag_t *dag;
1194 rpl_instance_t *instance;
1195 unsigned char *buffer;
1196 uint8_t prefixlen;
1197 int pos;
1198 uip_ipaddr_t *parent_ipaddr = NULL;
1199 uip_ipaddr_t *dest_ipaddr = NULL;
1200
1201 /* Destination Advertisement Object */
1202
1203 /* If we are in feather mode, we should not send any DAOs */
1204 if(rpl_get_mode() == RPL_MODE_FEATHER) {
1205 return;
1206 }
1207
1208 if(parent == NULL) {
1209 LOG_ERR("dao_output_target error parent NULL\n");
1210 return;
1211 }
1212
1213 parent_ipaddr = rpl_parent_get_ipaddr(parent);
1214 if(parent_ipaddr == NULL) {
1215 LOG_ERR("dao_output_target error parent IP address NULL\n");
1216 return;
1217 }
1218
1219 dag = parent->dag;
1220 if(dag == NULL) {
1221 LOG_ERR("dao_output_target error dag NULL\n");
1222 return;
1223 }
1224
1225 instance = dag->instance;
1226
1227 if(instance == NULL) {
1228 LOG_ERR("dao_output_target error instance NULL\n");
1229 return;
1230 }
1231 if(prefix == NULL) {
1232 LOG_ERR("dao_output_target error prefix NULL\n");
1233 return;
1234 }
1235#ifdef RPL_DEBUG_DAO_OUTPUT
1236 RPL_DEBUG_DAO_OUTPUT(parent);
1237#endif
1238
1239 buffer = UIP_ICMP_PAYLOAD;
1240 pos = 0;
1241
1242 buffer[pos++] = instance->instance_id;
1243 buffer[pos] = 0;
1244#if RPL_DAO_SPECIFY_DAG
1245 buffer[pos] |= RPL_DAO_D_FLAG;
1246#endif /* RPL_DAO_SPECIFY_DAG */
1247#if RPL_WITH_DAO_ACK
1248 if(lifetime != RPL_ZERO_LIFETIME) {
1249 buffer[pos] |= RPL_DAO_K_FLAG;
1250 }
1251#endif /* RPL_WITH_DAO_ACK */
1252 ++pos;
1253 buffer[pos++] = 0; /* reserved */
1254 buffer[pos++] = seq_no;
1255#if RPL_DAO_SPECIFY_DAG
1256 memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
1257 pos+=sizeof(dag->dag_id);
1258#endif /* RPL_DAO_SPECIFY_DAG */
1259
1260 /* create target subopt */
1261 prefixlen = sizeof(*prefix) * CHAR_BIT;
1262 buffer[pos++] = RPL_OPTION_TARGET;
1263 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
1264 buffer[pos++] = 0; /* reserved */
1265 buffer[pos++] = prefixlen;
1266 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
1267 pos += ((prefixlen + 7) / CHAR_BIT);
1268
1269 /* Create a transit information sub-option. */
1270 buffer[pos++] = RPL_OPTION_TRANSIT;
1271 buffer[pos++] = (instance->mop != RPL_MOP_NON_STORING) ? 4 : 20;
1272 buffer[pos++] = 0; /* flags - ignored */
1273 buffer[pos++] = 0; /* path control - ignored */
1274 buffer[pos++] = 0; /* path seq - ignored */
1275 buffer[pos++] = lifetime;
1276
1277 if(instance->mop != RPL_MOP_NON_STORING) {
1278 /* Send DAO to parent */
1279 dest_ipaddr = parent_ipaddr;
1280 } else {
1281 /* Include parent global IP address */
1282 memcpy(buffer + pos, &parent->dag->dag_id, 8); /* Prefix */
1283 pos += 8;
1284 memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8); /* Interface identifier */
1285 pos += 8;
1286 /* Send DAO to root */
1287 dest_ipaddr = &parent->dag->dag_id;
1288 }
1289
1290 LOG_INFO("Sending a %sDAO with sequence number %u, lifetime %u, prefix ",
1291 lifetime == RPL_ZERO_LIFETIME ? "No-Path " : "", seq_no, lifetime);
1292
1293 LOG_INFO_6ADDR(prefix);
1294 LOG_INFO_(" to ");
1295 LOG_INFO_6ADDR(dest_ipaddr);
1296 LOG_INFO_(" , parent ");
1297 LOG_INFO_6ADDR(parent_ipaddr);
1298 LOG_INFO_("\n");
1299
1300 if(dest_ipaddr != NULL) {
1301 uip_icmp6_send(dest_ipaddr, ICMP6_RPL, RPL_CODE_DAO, pos);
1302 }
1303}
1304/*---------------------------------------------------------------------------*/
1305static void
1306dao_ack_input(void)
1307{
1308#if RPL_WITH_DAO_ACK
1309
1310 uint8_t *buffer;
1311 uint8_t instance_id;
1312 uint8_t sequence;
1313 uint8_t status;
1314 rpl_instance_t *instance;
1315 rpl_parent_t *parent;
1316
1317 buffer = UIP_ICMP_PAYLOAD;
1318
1319 instance_id = buffer[0];
1320 sequence = buffer[2];
1321 status = buffer[3];
1322
1323 instance = rpl_get_instance(instance_id);
1324 if(instance == NULL) {
1325 uipbuf_clear();
1326 return;
1327 }
1328
1329 if(RPL_IS_STORING(instance)) {
1330 parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr);
1331 if(parent == NULL) {
1332 /* not a known instance - drop the packet and ignore */
1333 uipbuf_clear();
1334 return;
1335 }
1336 } else {
1337 parent = NULL;
1338 }
1339
1340 if(instance->current_dag->rank == ROOT_RANK(instance)) {
1341 LOG_DBG("DODAG root received a DAO ACK, ignoring it\n");
1342 uipbuf_clear();
1343 return;
1344 }
1345
1346 LOG_INFO("Received a DAO %s with sequence number %d (%d) and status %d from ",
1347 status < 128 ? "ACK" : "NACK",
1348 sequence, instance->my_dao_seqno, status);
1349 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1350 LOG_INFO_("\n");
1351
1352 if(sequence == instance->my_dao_seqno) {
1353 instance->has_downward_route = status < 128;
1354
1355 /* always stop the retransmit timer when the ACK arrived */
1356 ctimer_stop(&instance->dao_retransmit_timer);
1357
1358 /* Inform objective function on status of the DAO ACK */
1359 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1360 instance->of->dao_ack_callback(parent, status);
1361 }
1362
1363#if RPL_REPAIR_ON_DAO_NACK
1364 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1365 /*
1366 * Failed the DAO transmission - need to remove the default route.
1367 * Trigger a local repair since we can not get our DAO in.
1368 */
1369 rpl_local_repair(instance);
1370 }
1371#endif
1372
1373 } else if(RPL_IS_STORING(instance)) {
1374 /* this DAO ACK should be forwarded to another recently registered route */
1375 uip_ds6_route_t *re;
1376 const uip_ipaddr_t *nexthop;
1377 if((re = find_route_entry_by_dao_ack(sequence)) != NULL) {
1378 /* pick the recorded seq no from that node and forward DAO ACK - and
1379 clear the pending flag*/
1380 RPL_ROUTE_CLEAR_DAO_PENDING(re);
1381
1382 nexthop = uip_ds6_route_nexthop(re);
1383 if(nexthop == NULL) {
1384 LOG_WARN("No next hop to fwd DAO ACK to\n");
1385 } else {
1386 LOG_INFO("Fwd DAO ACK to:");
1387 LOG_INFO_6ADDR(nexthop);
1388 LOG_INFO_("\n");
1389 buffer[2] = re->state.dao_seqno_in;
1390 uip_icmp6_send(nexthop, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1391 }
1392
1393 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1394 /* this node did not get in to the routing tables above... - remove */
1395 uip_ds6_route_rm(re);
1396 }
1397 } else {
1398 LOG_WARN("No route entry found to forward DAO ACK (seqno %u)\n", sequence);
1399 }
1400 }
1401#endif /* RPL_WITH_DAO_ACK */
1402 uipbuf_clear();
1403}
1404/*---------------------------------------------------------------------------*/
1405void
1406dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence,
1407 uint8_t status)
1408{
1409#if RPL_WITH_DAO_ACK
1410 unsigned char *buffer;
1411
1412 LOG_INFO("Sending a DAO %s with sequence number %d to ", status < 128 ? "ACK" : "NACK", sequence);
1413 LOG_INFO_6ADDR(dest);
1414 LOG_INFO_(" with status %d\n", status);
1415
1416 buffer = UIP_ICMP_PAYLOAD;
1417
1418 buffer[0] = instance->instance_id;
1419 buffer[1] = 0;
1420 buffer[2] = sequence;
1421 buffer[3] = status;
1422
1423 uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1424#endif /* RPL_WITH_DAO_ACK */
1425}
1426/*---------------------------------------------------------------------------*/
1427void
1428rpl_icmp6_register_handlers()
1429{
1433 uip_icmp6_register_input_handler(&dao_ack_handler);
1434}
1435/*---------------------------------------------------------------------------*/
1436
1437/** @}*/
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
#define uip_create_linklocal_rplnodes_mcast(addr)
Set IP address addr to the link-local, all-rpl-nodes multicast address.
Definition: rpl-types.h:54
#define RPL_LIFETIME(lifetime)
Compute lifetime, accounting for the lifetime unit.
Definition: rpl-types.h:72
#define DAG_RANK(fixpt_rank)
Return DAG RANK as per RFC 6550 (rank divided by min_hoprankinc)
Definition: rpl-types.h:81
void rpl_local_repair(const char *str)
Triggers a RPL local repair.
Definition: rpl-dag.c:240
#define ROOT_RANK
Rank of a root node.
Definition: rpl-types.h:78
uip_mcast6_route_t * uip_mcast6_route_add(uip_ipaddr_t *group)
Add a multicast route.
void uip_sr_expire_parent(const void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent)
Expires a given child-parent link.
Definition: uip-sr.c:114
uip_ds6_nbr_t * uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data)
Add a neighbor cache for a specified IPv6 address, which is associated with a specified link-layer ad...
Definition: uip-ds6-nbr.c:122
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:230
rpl_dag_t * rpl_get_any_dag(void)
Returns pointer to any DAG (for compatibility with legagy RPL code)
Definition: rpl-dag.c:1069
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
Definition: uip-ds6-nbr.c:467
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition: uip.h:1859
uip_ds6_nbr_t * rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
Updates IPv6 neighbor cache on incoming link-local RPL ICMPv6 messages.
Definition: rpl-icmp6.c:194
enum rpl_mode rpl_get_mode(void)
Get the RPL mode.
Definition: rpl.c:70
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition: uip.h:1775
void rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
Processes incoming DIO.
Definition: rpl-dag.c:1466
#define uip_is_addr_mcast_global(a)
is address a global multicast address (FFxE::/16), a is of type uip_ip6addr_t*
Definition: uip.h:1866
#define ICMP6_RPL
RPL.
Definition: uip-icmp6.h:66
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
Definition: uip-icmp6.c:102
uip_sr_node_t * uip_sr_update_node(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent, uint32_t lifetime)
Updates a child-parent link.
Definition: uip-sr.c:127
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition: uip-ds6.c:75
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:969
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:159
Header file for the logging system.
Header file for the Packet buffer (packetbuf) management.
RPL DAG structure.
Definition: rpl.h:135
RPL instance structure.
Definition: rpl.h:219
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
Definition: uip-ds6-nbr.h:105
An entry in the routing table.
An entry in the multicast routing table.
void * dag
Pointer to an rpl_dag_t struct.
uint32_t lifetime
Entry lifetime seconds.
Header for the Contiki/uIP interface.
Header file for IPv6-related data structures.
Header file for ICMPv6 message and error handing (RFC 4443)
This header file contains configuration directives for uIPv6 multicast support.
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition: uip-nd6.c:106
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
Header file for IPv6 Neighbor discovery (RFC 4861)
Source routing support.
Header file for the uIP TCP/IP stack.