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 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \addtogroup rpl-lite
35 * @{
36 *
37 * \file
38 * ICMP6 I/O for RPL control messages.
39 *
40 * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>,
41 * Simon Duquennoy <simon.duquennoy@inria.fr>
42 * Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
43 * Mathieu Pouillot <m.pouillot@watteco.com>,
44 * George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
45 */
46
47#include "net/routing/rpl-lite/rpl.h"
48#include "net/ipv6/uip-icmp6.h"
49#include "net/packetbuf.h"
50#include "lib/random.h"
51
52#include <inttypes.h>
53#include <limits.h>
54
55/* Log configuration */
56#include "sys/log.h"
57#define LOG_MODULE "RPL"
58#define LOG_LEVEL LOG_LEVEL_RPL
59
60/*---------------------------------------------------------------------------*/
61#define RPL_DIO_GROUNDED 0x80
62#define RPL_DIO_MOP_SHIFT 3
63#define RPL_DIO_MOP_MASK 0x38
64#define RPL_DIO_PREFERENCE_MASK 0x07
65
66/*---------------------------------------------------------------------------*/
67static void dis_input(void);
68static void dio_input(void);
69static void dao_input(void);
70
71/*---------------------------------------------------------------------------*/
72/* Initialize RPL ICMPv6 message handlers */
73UIP_ICMP6_HANDLER(dis_handler, ICMP6_RPL, RPL_CODE_DIS, dis_input);
74UIP_ICMP6_HANDLER(dio_handler, ICMP6_RPL, RPL_CODE_DIO, dio_input);
75UIP_ICMP6_HANDLER(dao_handler, ICMP6_RPL, RPL_CODE_DAO, dao_input);
76
77#if RPL_WITH_DAO_ACK
78static void dao_ack_input(void);
79UIP_ICMP6_HANDLER(dao_ack_handler, ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
80#endif /* RPL_WITH_DAO_ACK */
81
82/*---------------------------------------------------------------------------*/
83static uint32_t
84get32(uint8_t *buffer, int pos)
85{
86 return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
87 (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
88}
89/*---------------------------------------------------------------------------*/
90static void
91set32(uint8_t *buffer, int pos, uint32_t value)
92{
93 buffer[pos++] = value >> 24;
94 buffer[pos++] = (value >> 16) & 0xff;
95 buffer[pos++] = (value >> 8) & 0xff;
96 buffer[pos++] = value & 0xff;
97}
98/*---------------------------------------------------------------------------*/
99static uint16_t
100get16(uint8_t *buffer, int pos)
101{
102 return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
103}
104/*---------------------------------------------------------------------------*/
105static void
106set16(uint8_t *buffer, int pos, uint16_t value)
107{
108 buffer[pos++] = value >> 8;
109 buffer[pos++] = value & 0xff;
110}
111/*---------------------------------------------------------------------------*/
113rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
114{
116
117 if((nbr = uip_ds6_nbr_lookup(from)) == NULL) {
118 if((nbr = uip_ds6_nbr_add(from, (uip_lladdr_t *)
119 packetbuf_addr(PACKETBUF_ADDR_SENDER),
120 0, NBR_REACHABLE, reason, data)) == NULL) {
121 LOG_ERR("could not add neighbor to cache ");
122 LOG_ERR_6ADDR(from);
123 LOG_ERR_(", ");
124 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
125 LOG_ERR_("\n");
126 }
127 }
128
129 return nbr;
130}
131/*---------------------------------------------------------------------------*/
132static void
133dis_input(void)
134{
135 if(!curr_instance.used) {
136 LOG_WARN("dis_input: not in an instance yet, discard\n");
137 goto discard;
138 }
139
140 LOG_INFO("received a DIS from ");
141 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
142 LOG_INFO_("\n");
143
144 rpl_process_dis(&UIP_IP_BUF->srcipaddr, uip_is_addr_mcast(&UIP_IP_BUF->destipaddr));
145
146 discard:
147 uipbuf_clear();
148}
149/*---------------------------------------------------------------------------*/
150void
152{
153 unsigned char *buffer;
154
155 /* Make sure we're up-to-date before sending data out */
157
158 buffer = UIP_ICMP_PAYLOAD;
159 buffer[0] = buffer[1] = 0;
160
161 if(addr == NULL) {
162 addr = &rpl_multicast_addr;
163 }
164
165 LOG_INFO("sending a DIS to ");
166 LOG_INFO_6ADDR(addr);
167 LOG_INFO_("\n");
168
169 uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
170}
171/*---------------------------------------------------------------------------*/
172static void
173dio_input(void)
174{
175 unsigned char *buffer;
176 uint16_t buffer_length;
177 rpl_dio_t dio;
178 uint8_t subopt_type;
179 int i;
180 int len;
181 uip_ipaddr_t from;
182
183 memset(&dio, 0, sizeof(dio));
184
185 /* Set default values in case the DIO configuration option is missing. */
186 dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
187 dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
188 dio.dag_redund = RPL_DIO_REDUNDANCY;
189 dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
190 dio.dag_max_rankinc = RPL_MAX_RANKINC;
191 dio.ocp = RPL_OF_OCP;
192 dio.default_lifetime = RPL_DEFAULT_LIFETIME;
193 dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
194
195 uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
196
197 buffer_length = uip_len - uip_l3_icmp_hdr_len;
198
199 if(buffer_length < 8 + sizeof(dio.dag_id)) {
200 LOG_WARN("dio_input: invalid DIO header, len %"PRIu16", discard\n",
201 buffer_length);
202 goto discard;
203 }
204
205 /* Process the DIO base option. */
206 i = 0;
207 buffer = UIP_ICMP_PAYLOAD;
208
209 dio.instance_id = buffer[i++];
210 dio.version = buffer[i++];
211 dio.rank = get16(buffer, i);
212 i += 2;
213
214 dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
215 dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
216 dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
217
218 dio.dtsn = buffer[i++];
219 /* two reserved bytes */
220 i += 2;
221
222 memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
223 i += sizeof(dio.dag_id);
224
225 /* Check if there are any DIO suboptions. */
226 for(; i < buffer_length; i += len) {
227 subopt_type = buffer[i];
228 if(subopt_type == RPL_OPTION_PAD1) {
229 len = 1;
230 } else {
231 /* Suboption with a two-byte header + payload */
232 if(i + 1 >= buffer_length) {
233 LOG_ERR("dio_input: malformed packet, discard\n");
234 goto discard;
235 }
236 len = 2 + buffer[i + 1];
237 }
238
239 if(len + i > buffer_length) {
240 LOG_ERR("dio_input: malformed packet, discard\n");
241 goto discard;
242 }
243
244 switch(subopt_type) {
245 case RPL_OPTION_DAG_METRIC_CONTAINER:
246 if(len < 6) {
247 LOG_WARN("dio_input: invalid DAG MC, len %u, discard\n", len);
248 goto discard;
249 }
250 dio.mc.type = buffer[i + 2];
251 dio.mc.flags = buffer[i + 3] << 1;
252 dio.mc.flags |= buffer[i + 4] >> 7;
253 dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
254 dio.mc.prec = buffer[i + 4] & 0xf;
255 dio.mc.length = buffer[i + 5];
256
257 if(dio.mc.type == RPL_DAG_MC_NONE) {
258 /* No metric container: do nothing */
259 } else if(dio.mc.type == RPL_DAG_MC_ETX) {
260 if(len < 8) {
261 LOG_WARN("dio_input: invalid DAG MC, len %u, discard\n", len);
262 goto discard;
263 }
264 dio.mc.obj.etx = get16(buffer, i + 6);
265 } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
266 if(len < 8) {
267 LOG_WARN("dio_input: invalid DAG MC, len %u, discard\n", len);
268 goto discard;
269 }
270 dio.mc.obj.energy.flags = buffer[i + 6];
271 dio.mc.obj.energy.energy_est = buffer[i + 7];
272 } else {
273 LOG_WARN("dio_input: unsupported DAG MC type %u, discard\n", (unsigned)dio.mc.type);
274 goto discard;
275 }
276 break;
277 case RPL_OPTION_ROUTE_INFO:
278 if(len < 8) {
279 LOG_WARN("dio_input: invalid route info option, len %u, discard\n",
280 len);
281 goto discard;
282 }
283
284 /* The flags field includes the preference value. */
285 dio.destination_prefix.length = buffer[i + 2];
286 dio.destination_prefix.flags = buffer[i + 3];
287 dio.destination_prefix.lifetime = get32(buffer, i + 4);
288
289 if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
290 dio.destination_prefix.length <= 128) {
291 memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
292 (dio.destination_prefix.length + 7) / 8);
293 } else {
294 LOG_WARN("dio_input: invalid route info option, len %u, discard\n", len);
295 goto discard;
296 }
297
298 break;
299 case RPL_OPTION_DAG_CONF:
300 if(len != 16) {
301 LOG_WARN("dio_input: invalid DAG configuration option, len %u, discard\n", len);
302 goto discard;
303 }
304
305 /* Path control field not yet implemented - at i + 2 */
306 dio.dag_intdoubl = buffer[i + 3];
307 dio.dag_intmin = buffer[i + 4];
308 dio.dag_redund = buffer[i + 5];
309 dio.dag_max_rankinc = get16(buffer, i + 6);
310 dio.dag_min_hoprankinc = get16(buffer, i + 8);
311 dio.ocp = get16(buffer, i + 10);
312 /* buffer + 12 is reserved */
313 dio.default_lifetime = buffer[i + 13];
314 dio.lifetime_unit = get16(buffer, i + 14);
315 break;
316 case RPL_OPTION_PREFIX_INFO:
317 if(len != 32) {
318 LOG_WARN("dio_input: invalid DAG prefix info, len %u, discard\n", len);
319 goto discard;
320 }
321 dio.prefix_info.length = buffer[i + 2];
322 if(dio.prefix_info.length > sizeof(uip_ipaddr_t) * 8) {
323 LOG_WARN("dio_input: invalid DAG prefix info, len %u > %zu\n",
324 dio.prefix_info.length, sizeof(uip_ipaddr_t) * 8);
325 goto discard;
326 }
327
328 dio.prefix_info.flags = buffer[i + 3];
329 /* valid lifetime is ingnored for now - at i + 4 */
330 /* preferred lifetime stored in lifetime */
331 dio.prefix_info.lifetime = get32(buffer, i + 8);
332 /* 32-bit reserved at i + 12 */
333 memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
334 break;
335 default:
336 LOG_WARN("dio_input: unsupported suboption type in DIO: %u, discard\n", (unsigned)subopt_type);
337 goto discard;
338 }
339 }
340
341 LOG_INFO("received a %s-DIO from ",
342 uip_is_addr_mcast(&UIP_IP_BUF->destipaddr) ? "multicast" : "unicast");
343 LOG_INFO_6ADDR(&from);
344 LOG_INFO_(", instance_id %u, DAG ID ", (unsigned)dio.instance_id);
345 LOG_INFO_6ADDR(&dio.dag_id);
346 LOG_INFO_(", version %u, dtsn %u, rank %u\n",
347 (unsigned)dio.version,
348 (unsigned)dio.dtsn,
349 (unsigned)dio.rank);
350
351 rpl_process_dio(&from, &dio);
352
353discard:
354 uipbuf_clear();
355}
356/*---------------------------------------------------------------------------*/
357void
358rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
359{
360 unsigned char *buffer;
361 int pos;
362 uip_ipaddr_t *addr = uc_addr;
363
364 /* Make sure we're up-to-date before sending data out */
366
367 if(rpl_get_leaf_only()) {
368 /* In leaf mode, we only send DIO messages as unicasts in response to
369 unicast DIS messages. */
370 if(uc_addr == NULL) {
371 /* Do not send multicast DIO in leaf mode */
372 return;
373 }
374 }
375
376 /* DAG Information Object */
377 pos = 0;
378
379 buffer = UIP_ICMP_PAYLOAD;
380 buffer[pos++] = curr_instance.instance_id;
381 buffer[pos++] = curr_instance.dag.version;
382
383 set16(buffer, pos,
384 rpl_get_leaf_only() ? RPL_INFINITE_RANK : curr_instance.dag.rank);
385 pos += 2;
386
387 buffer[pos] = curr_instance.dag.grounded ? RPL_DIO_GROUNDED : 0;
388 buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
389 buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
390 pos++;
391
392 buffer[pos++] = curr_instance.dtsn_out;
393
394 /* reserved 2 bytes */
395 buffer[pos++] = 0; /* flags */
396 buffer[pos++] = 0; /* reserved */
397
398 memcpy(buffer + pos, &curr_instance.dag.dag_id, sizeof(curr_instance.dag.dag_id));
399 pos += 16;
400
401 if(!rpl_get_leaf_only()) {
402 if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
403 buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
404 buffer[pos++] = 6;
405 buffer[pos++] = curr_instance.mc.type;
406 buffer[pos++] = curr_instance.mc.flags >> 1;
407 buffer[pos] = (curr_instance.mc.flags & 1) << 7;
408 buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
409 if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
410 buffer[pos++] = 2;
411 set16(buffer, pos, curr_instance.mc.obj.etx);
412 pos += 2;
413 } else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
414 buffer[pos++] = 2;
415 buffer[pos++] = curr_instance.mc.obj.energy.flags;
416 buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
417 } else {
418 LOG_ERR("unable to send DIO because of unsupported DAG MC type %u\n",
419 (unsigned)curr_instance.mc.type);
420 return;
421 }
422 }
423 }
424
425 /* Always add a DAG configuration option. */
426 buffer[pos++] = RPL_OPTION_DAG_CONF;
427 buffer[pos++] = 14;
428 buffer[pos++] = 0; /* No Auth, PCS = 0 */
429 buffer[pos++] = curr_instance.dio_intdoubl;
430 buffer[pos++] = curr_instance.dio_intmin;
431 buffer[pos++] = curr_instance.dio_redundancy;
432 set16(buffer, pos, curr_instance.max_rankinc);
433 pos += 2;
434 set16(buffer, pos, curr_instance.min_hoprankinc);
435 pos += 2;
436 /* OCP is in the DAG_CONF option */
437 set16(buffer, pos, curr_instance.of->ocp);
438 pos += 2;
439 buffer[pos++] = 0; /* reserved */
440 buffer[pos++] = curr_instance.default_lifetime;
441 set16(buffer, pos, curr_instance.lifetime_unit);
442 pos += 2;
443
444 /* Check if we have a prefix to send also. */
445 if(curr_instance.dag.prefix_info.length > 0) {
446 buffer[pos++] = RPL_OPTION_PREFIX_INFO;
447 buffer[pos++] = 30; /* always 30 bytes + 2 long */
448 buffer[pos++] = curr_instance.dag.prefix_info.length;
449 buffer[pos++] = curr_instance.dag.prefix_info.flags;
450 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
451 pos += 4;
452 set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
453 pos += 4;
454 memset(&buffer[pos], 0, 4);
455 pos += 4;
456 memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
457 pos += 16;
458 }
459
460 if(!rpl_get_leaf_only()) {
461 addr = addr != NULL ? addr : &rpl_multicast_addr;
462 }
463
464 LOG_INFO("sending a %s-DIO with rank %u to ",
465 uc_addr != NULL ? "unicast" : "multicast",
466 (unsigned)curr_instance.dag.rank);
467 LOG_INFO_6ADDR(addr);
468 LOG_INFO_("\n");
469
470 uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIO, pos);
471}
472/*---------------------------------------------------------------------------*/
473static void
474dao_input(void)
475{
476 struct rpl_dao dao;
477 uint8_t subopt_type;
478 unsigned char *buffer;
479 uint16_t buffer_length;
480 int pos;
481 int len;
482 int i;
483 uip_ipaddr_t from;
484
485 memset(&dao, 0, sizeof(dao));
486
487 dao.instance_id = UIP_ICMP_PAYLOAD[0];
488 if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
489 LOG_ERR("dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
490 goto discard;
491 }
492
493 uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
494 memset(&dao.parent_addr, 0, 16);
495
496 buffer = UIP_ICMP_PAYLOAD;
497 buffer_length = uip_len - uip_l3_icmp_hdr_len;
498
499 if(buffer_length < 4) {
500 LOG_WARN("dao_input: invalid DAO header, len %"PRIu16", discard\n",
501 buffer_length);
502 goto discard;
503 }
504
505 pos = 0;
506 pos++; /* instance ID */
507 dao.lifetime = curr_instance.default_lifetime;
508 dao.flags = buffer[pos++];
509 pos++; /* reserved */
510 dao.sequence = buffer[pos++];
511
512 /* Is the DAG ID present? */
513 if(dao.flags & RPL_DAO_D_FLAG) {
514 if(buffer_length < 4 + sizeof(curr_instance.dag.dag_id)) {
515 LOG_WARN("dao_input: missing full DAG ID, len %"PRIu16", discard\n",
516 buffer_length);
517 goto discard;
518 }
519 if(memcmp(&curr_instance.dag.dag_id, &buffer[pos], sizeof(curr_instance.dag.dag_id))) {
520 LOG_ERR("dao_input: different DAG ID ");
521 LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
522 LOG_ERR_(", discard\n");
523 goto discard;
524 }
525 pos += 16;
526 }
527
528 /* Check if there are any RPL options present. */
529 for(i = pos; i < buffer_length; i += len) {
530 subopt_type = buffer[i];
531 if(subopt_type == RPL_OPTION_PAD1) {
532 len = 1;
533 } else {
534 /* The option consists of a two-byte header and a payload. */
535 if(i + 1 >= buffer_length) {
536 LOG_ERR("dao_input: malformed packet, discard\n");
537 goto discard;
538 }
539 len = 2 + buffer[i + 1];
540 }
541
542 if(i + len > buffer_length) {
543 LOG_ERR("dao_input: malformed packet, discard\n");
544 goto discard;
545 }
546
547 switch(subopt_type) {
548 case RPL_OPTION_TARGET:
549 /* Handle the target option. */
550 if(len < 4) {
551 LOG_WARN("dao_input: invalid target option, len %u, discard\n", len);
552 goto discard;
553 }
554 dao.prefixlen = buffer[i + 3];
555 if(4 + (dao.prefixlen + 7) / CHAR_BIT != len) {
556 LOG_WARN("dao_input: invalid target option, len %u != %u, discard\n",
557 len, 4 + (dao.prefixlen + 7) / CHAR_BIT);
558 goto discard;
559 }
560 memset(&dao.prefix, 0, sizeof(dao.prefix));
561 memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
562 break;
563 case RPL_OPTION_TRANSIT:
564 /* The path sequence and control are ignored. */
565 if(len < 6) {
566 LOG_WARN("dao_input: invalid transit option, len %"PRIu16", discard\n",
567 buffer_length);
568 goto discard;
569 }
570 dao.lifetime = buffer[i + 5];
571 if(len >= 20) {
572 memcpy(&dao.parent_addr, buffer + i + 6, 16);
573 }
574 break;
575 }
576 }
577
578 /* Destination Advertisement Object */
579 LOG_INFO("received a %sDAO from ", dao.lifetime == 0 ? "No-path " : "");
580 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
581 LOG_INFO_(", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
582 LOG_INFO_6ADDR(&dao.prefix);
583 LOG_INFO_(", prefix length %u, parent ", dao.prefixlen);
584 LOG_INFO_6ADDR(&dao.parent_addr);
585 LOG_INFO_(" \n");
586
587 rpl_process_dao(&from, &dao);
588
589 discard:
590 uipbuf_clear();
591}
592/*---------------------------------------------------------------------------*/
593void
594rpl_icmp6_dao_output(uint8_t lifetime)
595{
596 unsigned char *buffer;
597 uint8_t prefixlen;
598 int pos;
599 const uip_ipaddr_t *prefix = rpl_get_global_address();
600 uip_ipaddr_t *parent_ipaddr = rpl_neighbor_get_ipaddr(curr_instance.dag.preferred_parent);
601
602 /* Make sure we're up-to-date before sending data out */
604
605 if(!curr_instance.used) {
606 LOG_WARN("rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
607 return;
608 }
609
610 if(curr_instance.dag.preferred_parent == NULL) {
611 LOG_WARN("rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
612 return;
613 }
614
615 if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
616 LOG_WARN("rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
617 prefix, parent_ipaddr, curr_instance.mop);
618 return;
619 }
620
621 buffer = UIP_ICMP_PAYLOAD;
622 pos = 0;
623
624 buffer[pos++] = curr_instance.instance_id;
625 buffer[pos] = 0;
626#if RPL_WITH_DAO_ACK
627 if(lifetime != 0) {
628 buffer[pos] |= RPL_DAO_K_FLAG;
629 }
630#endif /* RPL_WITH_DAO_ACK */
631 ++pos;
632 buffer[pos++] = 0; /* reserved */
633 buffer[pos++] = curr_instance.dag.dao_last_seqno;
634
635 /* create target subopt */
636 prefixlen = sizeof(*prefix) * CHAR_BIT;
637 buffer[pos++] = RPL_OPTION_TARGET;
638 buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
639 buffer[pos++] = 0; /* reserved */
640 buffer[pos++] = prefixlen;
641 memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
642 pos += ((prefixlen + 7) / CHAR_BIT);
643
644 /* Create a transit information sub-option. */
645 buffer[pos++] = RPL_OPTION_TRANSIT;
646 buffer[pos++] = 20;
647 buffer[pos++] = 0; /* flags - ignored */
648 buffer[pos++] = 0; /* path control - ignored */
649 buffer[pos++] = 0; /* path seq - ignored */
650 buffer[pos++] = lifetime;
651
652 /* Include parent global IP address */
653 memcpy(buffer + pos, &curr_instance.dag.dag_id, 8); /* Prefix */
654 pos += 8;
655 memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8); /* Interface identifier */
656 pos += 8;
657
658 LOG_INFO("sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
659 lifetime == 0 ? "No-path " : "",
660 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_transmissions, lifetime);
661 LOG_INFO_6ADDR(prefix);
662 LOG_INFO_(" to ");
663 LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
664 LOG_INFO_(", parent ");
665 LOG_INFO_6ADDR(parent_ipaddr);
666 LOG_INFO_("\n");
667
668 /* Send DAO to root (IPv6 address is DAG ID) */
669 uip_icmp6_send(&curr_instance.dag.dag_id, ICMP6_RPL, RPL_CODE_DAO, pos);
670}
671#if RPL_WITH_DAO_ACK
672/*---------------------------------------------------------------------------*/
673static void
674dao_ack_input(void)
675{
676 uint8_t *buffer;
677 uint8_t instance_id;
678 uint8_t sequence;
679 uint8_t status;
680
681 buffer = UIP_ICMP_PAYLOAD;
682
683 instance_id = buffer[0];
684 sequence = buffer[2];
685 status = buffer[3];
686
687 if(!curr_instance.used || curr_instance.instance_id != instance_id) {
688 LOG_ERR("dao_ack_input: unknown instance, discard\n");
689 goto discard;
690 }
691
692 LOG_INFO("received a DAO-%s with seqno %d (%d %d) and status %d from ",
693 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ? "ACK" : "NACK", sequence,
694 curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_last_seqno, status);
695 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
696 LOG_INFO_("\n");
697
698 rpl_process_dao_ack(sequence, status);
699
700 discard:
701 uipbuf_clear();
702}
703/*---------------------------------------------------------------------------*/
704void
705rpl_icmp6_dao_ack_output(uip_ipaddr_t *dest, uint8_t sequence, uint8_t status)
706{
707 unsigned char *buffer;
708
709 /* Make sure we're up-to-date before sending data out */
711
712 buffer = UIP_ICMP_PAYLOAD;
713 buffer[0] = curr_instance.instance_id;
714 buffer[1] = 0;
715 buffer[2] = sequence;
716 buffer[3] = status;
717
718 LOG_INFO("sending a DAO-%s seqno %d to ",
719 status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ? "ACK" : "NACK", sequence);
720 LOG_INFO_6ADDR(dest);
721 LOG_INFO_(" with status %d\n", status);
722
723 uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
724}
725#endif /* RPL_WITH_DAO_ACK */
726/*---------------------------------------------------------------------------*/
727void
729{
733#if RPL_WITH_DAO_ACK
734 uip_icmp6_register_input_handler(&dao_ack_handler);
735#endif /* RPL_WITH_DAO_ACK */
736}
737/*---------------------------------------------------------------------------*/
738
739/** @}*/
void rpl_process_dao_ack(uint8_t sequence, uint8_t status)
Processes incoming DAO-ACK.
void rpl_icmp6_dis_output(uip_ipaddr_t *addr)
Creates an ICMPv6 DIS packet and sends it.
Definition rpl-icmp6.c:151
uint8_t rpl_get_leaf_only(void)
Get the value of the rpl_leaf_only flag.
Definition rpl.c:242
void rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
Creates an ICMPv6 DIO packet and sends it.
Definition rpl-icmp6.c:358
void rpl_dag_update_state(void)
Updates RPL internal state: selects preferred parent, updates rank & metreic container,...
Definition rpl-dag.c:266
void rpl_process_dis(uip_ipaddr_t *from, int is_multicast)
Processes incoming DIS.
Definition rpl-dag.c:619
void rpl_icmp6_dao_output(uint8_t lifetime)
Creates an ICMPv6 DAO packet and sends it to the root, advertising the current preferred parent,...
Definition rpl-icmp6.c:594
void rpl_icmp6_init()
Initializes rpl-icmp6 module, registers ICMPv6 handlers for all RPL ICMPv6 messages: DIO,...
Definition rpl-icmp6.c:728
void rpl_process_dao(uip_ipaddr_t *from, rpl_dao_t *dao)
Processes incoming DAO.
Definition rpl-dag.c:633
const uip_ipaddr_t * rpl_get_global_address(void)
Get one of the node's global addresses.
Definition rpl.c:71
void rpl_icmp6_dao_ack_output(uip_ipaddr_t *dest, uint8_t sequence, uint8_t status)
Creates an ICMPv6 DAO-ACK packet and sends it to the originator of the ACK.
uip_ipaddr_t * rpl_neighbor_get_ipaddr(rpl_nbr_t *nbr)
Returns a neighbor's (link-local) IPv6 address.
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
#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
#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.
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
Header file for ICMPv6 message and error handing (RFC 4443)
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