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