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