Contiki-NG
Loading...
Searching...
No Matches
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. */
235 if(rpl_icmp6_update_nbr_table(&UIP_IP_BUF->srcipaddr,
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 uipbuf_clear();
250}
251/*---------------------------------------------------------------------------*/
252void
253dis_output(uip_ipaddr_t *addr)
254{
255 unsigned char *buffer;
256 uip_ipaddr_t tmpaddr;
257
258 /*
259 * DAG Information Solicitation - 2 bytes reserved
260 * 0 1 2
261 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
262 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
263 * | Flags | Reserved | Option(s)...
264 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265 */
266
267 buffer = UIP_ICMP_PAYLOAD;
268 buffer[0] = buffer[1] = 0;
269
270 if(addr == NULL) {
271 uip_create_linklocal_rplnodes_mcast(&tmpaddr);
272 addr = &tmpaddr;
273 }
274
275 LOG_INFO("Sending a DIS to ");
276 LOG_INFO_6ADDR(addr);
277 LOG_INFO_("\n");
278
279 uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
280}
281/*---------------------------------------------------------------------------*/
282static void
283dio_input(void)
284{
285 unsigned char *buffer;
286 uint8_t buffer_length;
287 rpl_dio_t dio;
288 uint8_t subopt_type;
289 int i;
290 int len;
291 uip_ipaddr_t from;
292
293 memset(&dio, 0, sizeof(dio));
294
295 /* Set default values in case the DIO configuration option is missing. */
296 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
297 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
298 dio.dag_redund = RPL_DIO_REDUNDANCY;
299 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
300 dio.dag_max_rankinc = RPL_MAX_RANKINC;
301 dio.ocp = RPL_OF_OCP;
302 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
303 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
304
305 uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
306
307 /* DAG Information Object */
308 LOG_INFO("Received a DIO from ");
309 LOG_INFO_6ADDR(&from);
310 LOG_INFO_("\n");
311
312 buffer_length = uip_len - uip_l3_icmp_hdr_len;
313
314 /* Process the DIO base option. */
315 i = 0;
316 buffer = UIP_ICMP_PAYLOAD;
317
318 dio.instance_id = buffer[i++];
319 dio.version = buffer[i++];
320 dio.rank = get16(buffer, i);
321 i += 2;
322
323 LOG_DBG("Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
324 (unsigned)dio.instance_id,
325 (unsigned)dio.version,
326 (unsigned)dio.rank);
327
328 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
329 dio.mop = (buffer[i] & RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
330 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
331
332 dio.dtsn = buffer[i++];
333 /* two reserved bytes */
334 i += 2;
335
336 memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
337 i += sizeof(dio.dag_id);
338
339 LOG_DBG("Incoming DIO (dag_id, pref) = (");
340 LOG_DBG_6ADDR(&dio.dag_id);
341 LOG_DBG_(", %u)\n", dio.preference);
342
343 /* Check if there are any DIO suboptions. */
344 for(; i < buffer_length; i += len) {
345 subopt_type = buffer[i];
346 if(subopt_type == RPL_OPTION_PAD1) {
347 len = 1;
348 } else {
349 /* Suboption with a two-byte header + payload. */
350 len = 2 + buffer[i + 1];
351 }
352
353 if(len + i > buffer_length) {
354 LOG_WARN("Invalid DIO packet\n");
355 RPL_STAT(rpl_stats.malformed_msgs++);
356 goto discard;
357 }
358
359 LOG_DBG("Incoming DIO (option, length) = (%u, %u)\n",
360 subopt_type, len);
361
362 switch(subopt_type) {
363 case RPL_OPTION_PAD1:
364 case RPL_OPTION_PADN:
365 LOG_DBG("PAD %u bytes\n", len);
366 break;
367 case RPL_OPTION_DAG_METRIC_CONTAINER:
368 if(len < 6) {
369 LOG_WARN("Invalid DAG MC, len = %d\n", len);
370 RPL_STAT(rpl_stats.malformed_msgs++);
371 goto discard;
372 }
373 dio.mc.type = buffer[i + 2];
374 dio.mc.flags = buffer[i + 3] << 1;
375 dio.mc.flags |= buffer[i + 4] >> 7;
376 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
377 dio.mc.prec = buffer[i + 4] & 0xf;
378 dio.mc.length = buffer[i + 5];
379
380 if(dio.mc.type == RPL_DAG_MC_NONE) {
381 /* No metric container: do nothing. */
382 } else if(dio.mc.type == RPL_DAG_MC_ETX) {
383 dio.mc.obj.etx = get16(buffer, i + 6);
384
385 LOG_DBG("DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
386 (unsigned)dio.mc.type,
387 (unsigned)dio.mc.flags,
388 (unsigned)dio.mc.aggr,
389 (unsigned)dio.mc.prec,
390 (unsigned)dio.mc.length,
391 (unsigned)dio.mc.obj.etx);
392 } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
393 dio.mc.obj.energy.flags = buffer[i + 6];
394 dio.mc.obj.energy.energy_est = buffer[i + 7];
395 } else {
396 LOG_WARN("Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
397 goto discard;
398 }
399 break;
400 case RPL_OPTION_ROUTE_INFO:
401 if(len < 9) {
402 LOG_WARN("Invalid destination prefix option, len = %d\n", len);
403 RPL_STAT(rpl_stats.malformed_msgs++);
404 goto discard;
405 }
406
407 /* The flags field includes the preference value. */
408 dio.destination_prefix.length = buffer[i + 2];
409 dio.destination_prefix.flags = buffer[i + 3];
410 dio.destination_prefix.lifetime = get32(buffer, i + 4);
411
412 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
413 dio.destination_prefix.length <= 128) {
414 LOG_INFO("Copying destination prefix\n");
415 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
416 (dio.destination_prefix.length + 7) / 8);
417 } else {
418 LOG_WARN("Invalid route info option, len = %d\n", len);
419 RPL_STAT(rpl_stats.malformed_msgs++);
420 goto discard;
421 }
422
423 break;
424 case RPL_OPTION_DAG_CONF:
425 if(len != 16) {
426 LOG_WARN("Invalid DAG configuration option, len = %d\n", len);
427 RPL_STAT(rpl_stats.malformed_msgs++);
428 goto discard;
429 }
430
431 /* Path control field not yet implemented - at i + 2 */
432 dio.dag_intdoubl = buffer[i + 3];
433 dio.dag_intmin = buffer[i + 4];
434 dio.dag_redund = buffer[i + 5];
435 dio.dag_max_rankinc = get16(buffer, i + 6);
436 dio.dag_min_hoprankinc = get16(buffer, i + 8);
437 dio.ocp = get16(buffer, i + 10);
438 /* buffer + 12 is reserved */
439 dio.default_lifetime = buffer[i + 13];
440 dio.lifetime_unit = get16(buffer, i + 14);
441 LOG_INFO("DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
442 dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
443 dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
444 dio.default_lifetime, dio.lifetime_unit);
445 break;
446 case RPL_OPTION_PREFIX_INFO:
447 if(len != 32) {
448 LOG_WARN("Invalid DAG prefix info, len != 32\n");
449 RPL_STAT(rpl_stats.malformed_msgs++);
450 goto discard;
451 }
452 dio.prefix_info.length = buffer[i + 2];
453
454 if(dio.prefix_info.length > sizeof(uip_ipaddr_t) * 8) {
455 LOG_WARN("Invalid DAG prefix info, len %u > %u\n",
456 dio.prefix_info.length, (unsigned)(sizeof(uip_ipaddr_t) * 8));
457 RPL_STAT(rpl_stats.malformed_msgs++);
458 goto discard;
459 }
460
461 dio.prefix_info.flags = buffer[i + 3];
462 /* valid lifetime is ingnored for now - at i + 4 */
463 /* preferred lifetime stored in lifetime */
464 dio.prefix_info.lifetime = get32(buffer, i + 8);
465 /* 32-bit reserved at i + 12 */
466 LOG_INFO("Copying prefix information\n");
467 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
468 break;
469 default:
470 LOG_WARN("Unsupported suboption type in DIO: %u\n",
471 (unsigned)subopt_type);
472 }
473 }
474
475#ifdef RPL_DEBUG_DIO_INPUT
476 RPL_DEBUG_DIO_INPUT(&from, &dio);
477#endif
478
479 rpl_process_dio(&from, &dio);
480
481discard:
482 uipbuf_clear();
483}
484/*---------------------------------------------------------------------------*/
485void
486dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
487{
488 unsigned char *buffer;
489 int pos;
490 int is_root;
491 rpl_dag_t *dag = instance->current_dag;
492#if !RPL_LEAF_ONLY
493 uip_ipaddr_t addr;
494#endif /* !RPL_LEAF_ONLY */
495
496#if RPL_LEAF_ONLY
497 /* In leaf mode, we only send DIO messages as unicasts in response to
498 unicast DIS messages. */
499 if(uc_addr == NULL) {
500 LOG_DBG("LEAF ONLY have multicast addr: skip dio_output\n");
501 return;
502 }
503#endif /* RPL_LEAF_ONLY */
504
505 /* DAG Information Object */
506 pos = 0;
507
508 buffer = UIP_ICMP_PAYLOAD;
509 buffer[pos++] = instance->instance_id;
510 buffer[pos++] = dag->version;
511 is_root = (dag->rank == ROOT_RANK(instance));
512
513#if RPL_LEAF_ONLY
514 LOG_DBG("LEAF ONLY DIO rank set to RPL_INFINITE_RANK\n");
515 set16(buffer, pos, RPL_INFINITE_RANK);
516#else /* RPL_LEAF_ONLY */
517 set16(buffer, pos, dag->rank);
518#endif /* RPL_LEAF_ONLY */
519 pos += 2;
520
521 buffer[pos] = 0;
522 if(dag->grounded) {
523 buffer[pos] |= RPL_DIO_GROUNDED;
524 }
525
526 buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
527 buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
528 pos++;
529
530 buffer[pos++] = instance->dtsn_out;
531
532 if(RPL_DIO_REFRESH_DAO_ROUTES && is_root && uc_addr == NULL) {
533 /*
534 * Request new DAO to refresh route. We do not do this for unicast
535 * DIO in order to avoid DAO messages after a DIS-DIO update, or
536 * upon unicast DIO probing.
537 */
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 ", (unsigned)dag->rank);
623 LOG_INFO_6ADDR(uc_addr);
624 LOG_INFO_("\n");
625 uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
626#else /* RPL_LEAF_ONLY */
627 /* Unicast requests get unicast replies! */
628 if(uc_addr == NULL) {
629 LOG_INFO("Sending a multicast-DIO with rank %u\n",
630 (unsigned)instance->current_dag->rank);
631 uip_create_linklocal_rplnodes_mcast(&addr);
632 uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
633 } else {
634 LOG_INFO("Sending unicast-DIO with rank %u to ",
635 (unsigned)instance->current_dag->rank);
636 LOG_INFO_6ADDR(uc_addr);
637 LOG_INFO_("\n");
638 uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
639 }
640#endif /* RPL_LEAF_ONLY */
641}
642/*---------------------------------------------------------------------------*/
643static void
644dao_input_storing(void)
645{
646#if RPL_WITH_STORING
647 uip_ipaddr_t dao_sender_addr;
648 rpl_dag_t *dag;
649 rpl_instance_t *instance;
650 unsigned char *buffer;
651 uint16_t sequence;
652 uint8_t instance_id;
653 uint8_t lifetime;
654 uint8_t prefixlen;
655 uint8_t flags;
656 uint8_t subopt_type;
657 uip_ipaddr_t prefix;
658 uip_ds6_route_t *rep;
659 int pos;
660 int len;
661 int i;
662 int learned_from;
663 rpl_parent_t *parent;
665 int is_root;
666
667 prefixlen = 0;
668 parent = NULL;
669 memset(&prefix, 0, sizeof(prefix));
670
671 uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
672
673 buffer = UIP_ICMP_PAYLOAD;
674 uint16_t buffer_length = uip_len - uip_l3_icmp_hdr_len;
675 if(buffer_length < 4) {
676 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
677 buffer_length, 4);
678 return;
679 }
680
681 uint16_t last_valid_pos = buffer_length - 1;
682
683 pos = 0;
684 instance_id = buffer[pos++];
685 instance = rpl_get_instance(instance_id);
686 if(instance == NULL) {
687 LOG_ERR("Cannot get RPL instance\n");
688 return;
689 }
690
691 lifetime = instance->default_lifetime;
692
693 flags = buffer[pos++];
694 /* reserved */
695 pos++;
696 sequence = buffer[pos++];
697
698 dag = instance->current_dag;
699 is_root = (dag->rank == ROOT_RANK(instance));
700
701 /* Is the DAG ID present? */
702 if(flags & RPL_DAO_D_FLAG) {
703 if(last_valid_pos < pos + 16) {
704 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
705 last_valid_pos, pos + 16);
706 return;
707 }
708
709 if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
710 LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
711 return;
712 }
713 pos += 16;
714 }
715
716 learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
717 RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
718
719 /* Destination Advertisement Object */
720 LOG_DBG("Received a (%s) DAO with sequence number %u from ",
721 learned_from == RPL_ROUTE_FROM_UNICAST_DAO ? "unicast" : "multicast",
722 sequence);
723 LOG_DBG_6ADDR(&dao_sender_addr);
724 LOG_DBG_("\n");
725
726 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
727 /* Check whether this is a DAO forwarding loop. */
728 parent = rpl_find_parent(dag, &dao_sender_addr);
729 /* Check if this is a new DAO registration with an "illegal" rank.
730 If we already route to this node, then it is likely. */
731 if(parent != NULL &&
732 DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
733 LOG_WARN("Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
734 DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
735 parent->rank = RPL_INFINITE_RANK;
736 parent->flags |= RPL_PARENT_FLAG_UPDATED;
737 return;
738 }
739
740 /* If we get the DAO from our parent, we also have a loop. */
741 if(parent != NULL && parent == dag->preferred_parent) {
742 LOG_WARN("Loop detected when receiving a unicast DAO from our parent\n");
743 parent->rank = RPL_INFINITE_RANK;
744 parent->flags |= RPL_PARENT_FLAG_UPDATED;
745 return;
746 }
747 }
748
749 /* Check if there are any RPL options present. */
750 for(i = pos; i < buffer_length; i += len) {
751 subopt_type = buffer[i];
752 if(subopt_type == RPL_OPTION_PAD1) {
753 len = 1;
754 } else {
755 /* The option consists of a two-byte header and a payload. */
756 if(last_valid_pos < i + 1) {
757 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
758 last_valid_pos, i + 1);
759 return;
760 }
761 len = 2 + buffer[i + 1];
762 }
763
764 switch(subopt_type) {
765 case RPL_OPTION_TARGET:
766 /* Handle the target option. */
767 if(last_valid_pos < i + 3) {
768 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
769 last_valid_pos, i + 3);
770 return;
771 }
772 prefixlen = buffer[i + 3];
773 if(prefixlen == 0) {
774 /* Ignore option targets with a prefix length of 0. */
775 break;
776 }
777 if(prefixlen > 128) {
778 LOG_ERR("Too large target prefix length %d\n", prefixlen);
779 return;
780 }
781 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
782 LOG_ERR("Incomplete DAO target option with prefix length of %d bits\n",
783 prefixlen);
784 return;
785 }
786 memset(&prefix, 0, sizeof(prefix));
787 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
788 break;
789 case RPL_OPTION_TRANSIT:
790 /* The path sequence and control are ignored. */
791 if(last_valid_pos < i + 5) {
792 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
793 last_valid_pos, i + 5);
794 return;
795 }
796 lifetime = buffer[i + 5];
797 /* The parent address is also ignored. */
798 break;
799 }
800 }
801
802 LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
803 (unsigned)lifetime, (unsigned)prefixlen);
804 LOG_INFO_6ADDR(&prefix);
805 LOG_INFO_("\n");
806
807#if RPL_WITH_MULTICAST
808 if(uip_is_addr_mcast_global(&prefix)) {
809 /*
810 * "rep" is used for a unicast route which we don't need now; so
811 * set NULL so that operations on "rep" will be skipped.
812 */
813 rep = NULL;
814 mcast_group = uip_mcast6_route_add(&prefix);
815 if(mcast_group) {
816 mcast_group->dag = dag;
817 mcast_group->lifetime = RPL_LIFETIME(instance, lifetime);
818 }
819 goto fwd_dao;
820 }
821#endif
822
823 rep = uip_ds6_route_lookup(&prefix);
824
825 if(lifetime == RPL_ZERO_LIFETIME) {
826 LOG_INFO("No-Path DAO received\n");
827 /* No-Path DAO received; invoke the route purging routine. */
828 if(rep != NULL &&
829 !RPL_ROUTE_IS_NOPATH_RECEIVED(rep) &&
830 rep->length == prefixlen &&
831 uip_ds6_route_nexthop(rep) != NULL &&
832 uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
833 LOG_DBG("Setting expiration timer for prefix ");
834 LOG_DBG_6ADDR(&prefix);
835 LOG_DBG_("\n");
836 RPL_ROUTE_SET_NOPATH_RECEIVED(rep);
837 rep->state.lifetime = RPL_NOPATH_REMOVAL_DELAY;
838
839 /* We forward the incoming No-Path DAO to our parent, if we have
840 one. */
841 if(dag->preferred_parent != NULL &&
842 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
843 uint8_t out_seq;
844 out_seq = prepare_for_dao_fwd(sequence, rep);
845
846 LOG_DBG("Forwarding No-path DAO out_seq:%d to parent ", out_seq);
847 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
848 LOG_DBG_("\n");
849
850 buffer = UIP_ICMP_PAYLOAD;
851 buffer[3] = out_seq; /* add an outgoing seq no before fwd */
852 uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
853 ICMP6_RPL, RPL_CODE_DAO, buffer_length);
854 }
855 }
856 /* Regardless of whether we remove it or not -- ACK the request. */
857 if(flags & RPL_DAO_K_FLAG) {
858 /* Indicate that we accepted the no-path DAO. */
859 uipbuf_clear();
860 dao_ack_output(instance, &dao_sender_addr, sequence,
861 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
862 }
863 return;
864 }
865
866 LOG_INFO("Adding DAO route\n");
867
868 /* Update and add neighbor, and fail if there is no room. */
869 nbr = rpl_icmp6_update_nbr_table(&dao_sender_addr,
870 NBR_TABLE_REASON_RPL_DAO, instance);
871 if(nbr == NULL) {
872 LOG_ERR("Out of memory, dropping DAO from ");
873 LOG_ERR_6ADDR(&dao_sender_addr);
874 LOG_ERR_(", ");
875 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
876 LOG_ERR_("\n");
877 if(flags & RPL_DAO_K_FLAG) {
878 /* Signal the failure to add the node. */
879 dao_ack_output(instance, &dao_sender_addr, sequence,
880 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
881 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
882 }
883 return;
884 }
885
886 rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
887 if(rep == NULL) {
888 RPL_STAT(rpl_stats.mem_overflows++);
889 LOG_ERR("Could not add a route after receiving a DAO\n");
890 if(flags & RPL_DAO_K_FLAG) {
891 /* Signal the failure to add the node. */
892 dao_ack_output(instance, &dao_sender_addr, sequence,
893 is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
894 RPL_DAO_ACK_UNABLE_TO_ACCEPT);
895 }
896 return;
897 }
898
899 /* Set the lifetime and clear the NOPATH bit. */
900 rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
901 RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
902
903#if RPL_WITH_MULTICAST
904fwd_dao:
905#endif
906
907 if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
908 int should_ack = 0;
909
910 if(flags & RPL_DAO_K_FLAG) {
911 if(rep != NULL) {
912 /*
913 * Check if this route is already installed and that we can
914 * acknowledge it now! Not pending and same sequence number
915 * means that we can acknowledge it. E.g., the route is
916 * installed already, so it will not take any more room that
917 * it already takes. Hence, it should be OK.
918 */
919 if((!RPL_ROUTE_IS_DAO_PENDING(rep) &&
920 rep->state.dao_seqno_in == sequence) ||
921 dag->rank == ROOT_RANK(instance)) {
922 should_ack = 1;
923 }
924 }
925 }
926
927 if(dag->preferred_parent != NULL &&
928 rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
929 uint8_t out_seq = 0;
930 if(rep != NULL) {
931 /* If this is pending and we get the same sequence number,
932 then it is a retransmission. */
933 if(RPL_ROUTE_IS_DAO_PENDING(rep) &&
934 rep->state.dao_seqno_in == sequence) {
935 /* Keep the same sequence number as before for parent also. */
936 out_seq = rep->state.dao_seqno_out;
937 } else {
938 out_seq = prepare_for_dao_fwd(sequence, rep);
939 }
940 }
941
942 LOG_DBG("Forwarding DAO to parent ");
943 LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
944 LOG_DBG_(" in seq: %d out seq: %d\n", sequence, out_seq);
945
946 buffer = UIP_ICMP_PAYLOAD;
947 buffer[3] = out_seq; /* add an outgoing seq no before fwd */
948 uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
949 ICMP6_RPL, RPL_CODE_DAO, buffer_length);
950 }
951 if(should_ack) {
952 LOG_DBG("Sending DAO ACK\n");
953 uipbuf_clear();
954 dao_ack_output(instance, &dao_sender_addr, sequence,
955 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
956 }
957 }
958#endif /* RPL_WITH_STORING */
959}
960/*---------------------------------------------------------------------------*/
961static void
962dao_input_nonstoring(void)
963{
964#if RPL_WITH_NON_STORING
965 uip_ipaddr_t dao_sender_addr;
966 uip_ipaddr_t dao_parent_addr;
967 rpl_dag_t *dag;
968 rpl_instance_t *instance;
969 unsigned char *buffer;
970 uint16_t sequence;
971 uint8_t instance_id;
972 uint8_t lifetime;
973 uint8_t prefixlen;
974 uint8_t flags;
975 uint8_t subopt_type;
976 uip_ipaddr_t prefix;
977 int pos;
978 int len;
979 int i;
980
981 /* Destination Advertisement Object */
982 LOG_INFO("Received a DAO from ");
983 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
984 LOG_INFO_("\n");
985
986 prefixlen = 0;
987
988 uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
989 memset(&dao_parent_addr, 0, 16);
990
991 buffer = UIP_ICMP_PAYLOAD;
992 uint16_t buffer_length = uip_len - uip_l3_icmp_hdr_len;
993 if(buffer_length < 4) {
994 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
995 buffer_length, 4);
996 return;
997 }
998
999 uint16_t last_valid_pos = buffer_length - 1;
1000
1001 pos = 0;
1002 instance_id = buffer[pos++];
1003 instance = rpl_get_instance(instance_id);
1004 lifetime = instance->default_lifetime;
1005
1006 flags = buffer[pos++];
1007 /* reserved */
1008 pos++;
1009 sequence = buffer[pos++];
1010
1011 dag = instance->current_dag;
1012 /* Is the DAG ID present? */
1013 if(flags & RPL_DAO_D_FLAG) {
1014 if(pos + sizeof(dag->dag_id) > buffer_length) {
1015 LOG_WARN("Insufficient data to read DAG ID from DAO\n");
1016 return;
1017 }
1018 if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
1019 LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
1020 return;
1021 }
1022 pos += 16;
1023 }
1024
1025 /* Check if there are any RPL options present. */
1026 for(i = pos; i < buffer_length; i += len) {
1027 subopt_type = buffer[i];
1028 if(subopt_type == RPL_OPTION_PAD1) {
1029 len = 1;
1030 } else {
1031 /* The option consists of a two-byte header and a payload. */
1032 if(last_valid_pos < i + 1) {
1033 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
1034 last_valid_pos, i + 1);
1035 return;
1036 }
1037 len = 2 + buffer[i + 1];
1038 }
1039
1040 switch(subopt_type) {
1041 case RPL_OPTION_TARGET:
1042 /* Handle the target option. */
1043 if(last_valid_pos < i + 3) {
1044 LOG_WARN("Dropping incomplete DAO (%"PRIu16" < %d)\n",
1045 last_valid_pos, i + 3);
1046 return;
1047 }
1048 prefixlen = buffer[i + 3];
1049 if(prefixlen == 0) {
1050 /* Ignore option targets with a prefix length of 0. */
1051 break;
1052 }
1053 if(prefixlen > 128) {
1054 LOG_ERR("Too large target prefix length %d\n", prefixlen);
1055 return;
1056 }
1057 if(i + 4 + ((prefixlen + 7) / CHAR_BIT) > buffer_length) {
1058 LOG_ERR("Incomplete DAO target option with prefix length of %d bits\n",
1059 prefixlen);
1060 return;
1061 }
1062
1063 memset(&prefix, 0, sizeof(prefix));
1064 memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
1065 break;
1066 case RPL_OPTION_TRANSIT:
1067 /* The path sequence and control are ignored. */
1068 if(i + 6 + 16 > buffer_length) {
1069 LOG_WARN("Incomplete DAO transit option (%d > %"PRIu16")\n",
1070 i + 6 + 16, buffer_length);
1071 return;
1072 }
1073 lifetime = buffer[i + 5];
1074 if(len >= 20) {
1075 memcpy(&dao_parent_addr, buffer + i + 6, 16);
1076 }
1077 break;
1078 }
1079 }
1080
1081 LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
1082 (unsigned)lifetime, (unsigned)prefixlen);
1083 LOG_INFO_6ADDR(&prefix);
1084 LOG_INFO_(", parent: ");
1085 LOG_INFO_6ADDR(&dao_parent_addr);
1086 LOG_INFO_("\n");
1087
1088 if(lifetime == RPL_ZERO_LIFETIME) {
1089 LOG_DBG("No-Path DAO received\n");
1090 uip_sr_expire_parent(dag, &prefix, &dao_parent_addr);
1091 } else {
1092 if(uip_sr_update_node(dag, &prefix, &dao_parent_addr,
1093 RPL_LIFETIME(instance, lifetime)) == NULL) {
1094 LOG_WARN("DAO failed to add link prefix: ");
1095 LOG_WARN_6ADDR(&prefix);
1096 LOG_WARN_(", parent: ");
1097 LOG_WARN_6ADDR(&dao_parent_addr);
1098 LOG_WARN_("\n");
1099 return;
1100 }
1101 }
1102
1103 if(flags & RPL_DAO_K_FLAG) {
1104 LOG_DBG("Sending DAO ACK\n");
1105 uipbuf_clear();
1106 dao_ack_output(instance, &dao_sender_addr, sequence,
1107 RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
1108 }
1109#endif /* RPL_WITH_NON_STORING */
1110}
1111/*---------------------------------------------------------------------------*/
1112static void
1113dao_input(void)
1114{
1115 rpl_instance_t *instance;
1116 uint8_t instance_id;
1117
1118 /* Destination Advertisement Object */
1119 LOG_INFO("Received a DAO from ");
1120 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1121 LOG_INFO_("\n");
1122
1123 if(uip_len <= uip_l3_icmp_hdr_len) {
1124 LOG_WARN("Ignoring DAO ICMPv6 message without DAO header\n");
1125 goto discard;
1126 }
1127
1128 instance_id = UIP_ICMP_PAYLOAD[0];
1129 instance = rpl_get_instance(instance_id);
1130 if(instance == NULL) {
1131 LOG_INFO("Ignoring a DAO for an unknown RPL instance(%u)\n",
1132 instance_id);
1133 goto discard;
1134 }
1135
1136 if(RPL_IS_STORING(instance)) {
1137 dao_input_storing();
1138 } else if(RPL_IS_NON_STORING(instance)) {
1139 dao_input_nonstoring();
1140 }
1141
1142discard:
1143 uipbuf_clear();
1144}
1145/*---------------------------------------------------------------------------*/
1146#if RPL_WITH_DAO_ACK
1147static void
1148handle_dao_retransmission(void *ptr)
1149{
1150 rpl_parent_t *parent;
1151 uip_ipaddr_t prefix;
1152 rpl_instance_t *instance;
1153
1154 parent = ptr;
1155 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1156 return;
1157 }
1158 instance = parent->dag->instance;
1159
1160 if(instance->my_dao_transmissions >= RPL_DAO_MAX_RETRANSMISSIONS) {
1161 /* No more retransmissions - give up. */
1162 if(instance->lifetime_unit == 0xffff && instance->default_lifetime == 0xff) {
1163 /*
1164 * ContikiRPL was previously using infinite lifetime for routes
1165 * and no DAO_ACK configured. This probably means that the root
1166 * and possibly other nodes might be running an old version that
1167 * does not support DAO ACKs. Assume that everything is ok for
1168 * now and let the normal repair mechanisms detect any problems.
1169 */
1170 return;
1171 }
1172
1173 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1174 /* Inform the objective function about the timeout. */
1175 instance->of->dao_ack_callback(parent, RPL_DAO_ACK_TIMEOUT);
1176 }
1177
1178 /* Perform a local repair and hope to find another parent. */
1179 rpl_local_repair(instance);
1180 return;
1181 }
1182
1183 LOG_INFO("will retransmit DAO - seq:%d trans:%d\n", instance->my_dao_seqno,
1184 instance->my_dao_transmissions);
1185
1186 if(get_global_addr(&prefix) == 0) {
1187 return;
1188 }
1189
1190 ctimer_set(&instance->dao_retransmit_timer,
1191 RPL_DAO_RETRANSMISSION_TIMEOUT / 2 +
1192 (random_rand() % (RPL_DAO_RETRANSMISSION_TIMEOUT / 2)),
1193 handle_dao_retransmission, parent);
1194
1195 instance->my_dao_transmissions++;
1196 dao_output_target_seq(parent, &prefix,
1197 instance->default_lifetime, instance->my_dao_seqno);
1198}
1199#endif /* RPL_WITH_DAO_ACK */
1200/*---------------------------------------------------------------------------*/
1201void
1202dao_output(rpl_parent_t *parent, uint8_t lifetime)
1203{
1204 /* Destination Advertisement Object */
1205 uip_ipaddr_t prefix;
1206
1207 if(get_global_addr(&prefix) == 0) {
1208 LOG_ERR("No global address set for this node - suppressing DAO\n");
1209 return;
1210 }
1211
1212 if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1213 return;
1214 }
1215
1216 RPL_LOLLIPOP_INCREMENT(dao_sequence);
1217#if RPL_WITH_DAO_ACK
1218 /*
1219 * Set up the state since this will be the first transmission of
1220 * DAO. Retransmissions will call directly to dao_output_target_seq.
1221 * Also keep track of my own sending of DAO for handling ack and
1222 * loss of ack.
1223 */
1224 if(lifetime != RPL_ZERO_LIFETIME) {
1225 rpl_instance_t *instance;
1226 instance = parent->dag->instance;
1227
1228 instance->my_dao_seqno = dao_sequence;
1229 instance->my_dao_transmissions = 1;
1230 ctimer_set(&instance->dao_retransmit_timer, RPL_DAO_RETRANSMISSION_TIMEOUT,
1231 handle_dao_retransmission, parent);
1232 }
1233#else
1234 /*
1235 * We know that we have tried to register, so now we are assuming
1236 * that we have a down-link -- unless this is a zero lifetime one.
1237 */
1238 parent->dag->instance->has_downward_route = lifetime != RPL_ZERO_LIFETIME;
1239#endif /* RPL_WITH_DAO_ACK */
1240
1241 /* Sending a DAO with own prefix as target. */
1242 dao_output_target(parent, &prefix, lifetime);
1243}
1244/*---------------------------------------------------------------------------*/
1245void
1246dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
1247{
1248 dao_output_target_seq(parent, prefix, lifetime, dao_sequence);
1249}
1250/*---------------------------------------------------------------------------*/
1251static void
1252dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
1253 uint8_t lifetime, uint8_t seq_no)
1254{
1255 rpl_dag_t *dag;
1256 rpl_instance_t *instance;
1257 unsigned char *buffer;
1258 uint8_t prefixlen;
1259 int pos;
1260 uip_ipaddr_t *parent_ipaddr = NULL;
1261 uip_ipaddr_t *dest_ipaddr = NULL;
1262
1263 /* Destination Advertisement Object */
1264
1265 /* If we are in feather mode, we should not send any DAOs. */
1266 if(rpl_get_mode() == RPL_MODE_FEATHER) {
1267 return;
1268 }
1269
1270 if(parent == NULL) {
1271 LOG_ERR("dao_output_target error parent NULL\n");
1272 return;
1273 }
1274
1275 parent_ipaddr = rpl_parent_get_ipaddr(parent);
1276 if(parent_ipaddr == NULL) {
1277 LOG_ERR("dao_output_target error parent IP address NULL\n");
1278 return;
1279 }
1280
1281 dag = parent->dag;
1282 if(dag == NULL) {
1283 LOG_ERR("dao_output_target error dag NULL\n");
1284 return;
1285 }
1286
1287 instance = dag->instance;
1288
1289 if(instance == NULL) {
1290 LOG_ERR("dao_output_target error instance NULL\n");
1291 return;
1292 }
1293 if(prefix == NULL) {
1294 LOG_ERR("dao_output_target error prefix NULL\n");
1295 return;
1296 }
1297#ifdef RPL_DEBUG_DAO_OUTPUT
1298 RPL_DEBUG_DAO_OUTPUT(parent);
1299#endif
1300
1301 buffer = UIP_ICMP_PAYLOAD;
1302 pos = 0;
1303
1304 buffer[pos++] = instance->instance_id;
1305 buffer[pos] = 0;
1306#if RPL_DAO_SPECIFY_DAG
1307 buffer[pos] |= RPL_DAO_D_FLAG;
1308#endif /* RPL_DAO_SPECIFY_DAG */
1309#if RPL_WITH_DAO_ACK
1310 if(lifetime != RPL_ZERO_LIFETIME) {
1311 buffer[pos] |= RPL_DAO_K_FLAG;
1312 }
1313#endif /* RPL_WITH_DAO_ACK */
1314 ++pos;
1315 buffer[pos++] = 0; /* reserved */
1316 buffer[pos++] = seq_no;
1317#if RPL_DAO_SPECIFY_DAG
1318 memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
1319 pos += sizeof(dag->dag_id);
1320#endif /* RPL_DAO_SPECIFY_DAG */
1321
1322 /* Create a target suboption. */
1323 prefixlen = sizeof(*prefix) * CHAR_BIT;
1324 buffer[pos++] = RPL_OPTION_TARGET;
1325 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
1326 buffer[pos++] = 0; /* reserved */
1327 buffer[pos++] = prefixlen;
1328 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
1329 pos += ((prefixlen + 7) / CHAR_BIT);
1330
1331 /* Create a transit information sub-option. */
1332 buffer[pos++] = RPL_OPTION_TRANSIT;
1333 buffer[pos++] = (instance->mop != RPL_MOP_NON_STORING) ? 4 : 20;
1334 buffer[pos++] = 0; /* flags - ignored */
1335 buffer[pos++] = 0; /* path control - ignored */
1336 buffer[pos++] = 0; /* path seq - ignored */
1337 buffer[pos++] = lifetime;
1338
1339 if(instance->mop != RPL_MOP_NON_STORING) {
1340 /* Send DAO to the parent. */
1341 dest_ipaddr = parent_ipaddr;
1342 } else {
1343 /* Include the parent's global IP address. */
1344 memcpy(buffer + pos, &parent->dag->dag_id, 8); /* Prefix */
1345 pos += 8;
1346 /* Interface identifier. */
1347 memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8);
1348 pos += 8;
1349 /* Send DAO to root */
1350 dest_ipaddr = &parent->dag->dag_id;
1351 }
1352
1353 LOG_INFO("Sending a %sDAO with sequence number %u, lifetime %u, prefix ",
1354 lifetime == RPL_ZERO_LIFETIME ? "No-Path " : "", seq_no, lifetime);
1355
1356 LOG_INFO_6ADDR(prefix);
1357 LOG_INFO_(" to ");
1358 LOG_INFO_6ADDR(dest_ipaddr);
1359 LOG_INFO_(" , parent ");
1360 LOG_INFO_6ADDR(parent_ipaddr);
1361 LOG_INFO_("\n");
1362
1363 if(dest_ipaddr != NULL) {
1364 uip_icmp6_send(dest_ipaddr, ICMP6_RPL, RPL_CODE_DAO, pos);
1365 }
1366}
1367/*---------------------------------------------------------------------------*/
1368static void
1369dao_ack_input(void)
1370{
1371#if RPL_WITH_DAO_ACK
1372
1373 uint8_t *buffer;
1374 uint8_t instance_id;
1375 uint8_t sequence;
1376 uint8_t status;
1377 rpl_instance_t *instance;
1378 rpl_parent_t *parent;
1379
1380 buffer = UIP_ICMP_PAYLOAD;
1381
1382 instance_id = buffer[0];
1383 sequence = buffer[2];
1384 status = buffer[3];
1385
1386 instance = rpl_get_instance(instance_id);
1387 if(instance == NULL) {
1388 uipbuf_clear();
1389 return;
1390 }
1391
1392 if(RPL_IS_STORING(instance)) {
1393 parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr);
1394 if(parent == NULL) {
1395 /* Unknown instance -- drop the packet and ignore. */
1396 uipbuf_clear();
1397 return;
1398 }
1399 } else {
1400 parent = NULL;
1401 }
1402
1403 if(instance->current_dag->rank == ROOT_RANK(instance)) {
1404 LOG_DBG("DODAG root received a DAO ACK, ignoring it\n");
1405 uipbuf_clear();
1406 return;
1407 }
1408
1409 LOG_INFO("Received a DAO %s with sequence number %u (%u) and status %u from ",
1410 status < 128 ? "ACK" : "NACK",
1411 sequence, instance->my_dao_seqno, status);
1412 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1413 LOG_INFO_("\n");
1414
1415 if(sequence == instance->my_dao_seqno) {
1416 instance->has_downward_route = status < 128;
1417
1418 /* Always stop the retransmit timer when the ACK arrived. */
1419 ctimer_stop(&instance->dao_retransmit_timer);
1420
1421 /* Inform the objective function on the status of the DAO ACK. */
1422 if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1423 instance->of->dao_ack_callback(parent, status);
1424 }
1425
1426#if RPL_REPAIR_ON_DAO_NACK
1427 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1428 /*
1429 * Failed the DAO transmission -- we need to remove the default route.
1430 * Trigger a local repair since we can not get our DAO in.
1431 */
1432 rpl_local_repair(instance);
1433 }
1434#endif
1435 } else if(RPL_IS_STORING(instance)) {
1436 /* This DAO ACK should be forwarded to another recently registered route. */
1437 uip_ds6_route_t *re;
1438 const uip_ipaddr_t *nexthop;
1439 if((re = find_route_entry_by_dao_ack(sequence)) != NULL) {
1440 /* Pick the recorded seq no from that node and forward the DAO ACK.
1441 Also clear the pending flag. */
1442 RPL_ROUTE_CLEAR_DAO_PENDING(re);
1443
1444 nexthop = uip_ds6_route_nexthop(re);
1445 if(nexthop == NULL) {
1446 LOG_WARN("No next hop to fwd DAO ACK to\n");
1447 } else {
1448 LOG_INFO("Fwd DAO ACK to:");
1449 LOG_INFO_6ADDR(nexthop);
1450 LOG_INFO_("\n");
1451 buffer[2] = re->state.dao_seqno_in;
1452 uip_icmp6_send(nexthop, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1453 }
1454
1455 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1456 /* This node did not get in to the routing tables above -- remove. */
1457 uip_ds6_route_rm(re);
1458 }
1459 } else {
1460 LOG_WARN("No route entry found to forward DAO ACK (seqno %u)\n",
1461 sequence);
1462 }
1463 }
1464#endif /* RPL_WITH_DAO_ACK */
1465 uipbuf_clear();
1466}
1467/*---------------------------------------------------------------------------*/
1468void
1469dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence,
1470 uint8_t status)
1471{
1472#if RPL_WITH_DAO_ACK
1473 unsigned char *buffer;
1474
1475 LOG_INFO("Sending a DAO %s with sequence number %u to ",
1476 status < 128 ? "ACK" : "NACK", sequence);
1477 LOG_INFO_6ADDR(dest);
1478 LOG_INFO_(" with status %u\n", status);
1479
1480 buffer = UIP_ICMP_PAYLOAD;
1481
1482 buffer[0] = instance->instance_id;
1483 buffer[1] = 0;
1484 buffer[2] = sequence;
1485 buffer[3] = status;
1486
1487 uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1488#endif /* RPL_WITH_DAO_ACK */
1489}
1490/*---------------------------------------------------------------------------*/
1491void
1492rpl_icmp6_register_handlers(void)
1493{
1497 uip_icmp6_register_input_handler(&dao_ack_handler);
1498}
1499/*---------------------------------------------------------------------------*/
1500
1501/** @}*/
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:138
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition ctimer.h:137
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...
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
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1860
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:1766
#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:1867
#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...
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.