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