-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathprotocol_6lowpan.c
878 lines (771 loc) · 28 KB
/
protocol_6lowpan.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
* Copyright (c) 2013-2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nsconfig.h"
#include "string.h"
#include "ns_types.h"
#include "eventOS_event.h"
#include "eventOS_scheduler.h"
#include "ns_trace.h"
#include "nsdynmemLIB.h"
#include "Core/include/ns_socket.h"
#include "NWK_INTERFACE/Include/protocol.h"
#include "Common_Protocols/udp.h"
#include "Common_Protocols/ipv6.h"
#include "Common_Protocols/icmpv6.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan_bootstrap.h"
#include "6LoWPAN/ND/nd_router_object.h"
#ifdef HAVE_RPL
#include "RPL/rpl_control.h"
#include "RPL/rpl_data.h"
#include "RPL/rpl_protocol.h"
#endif
#include "6LoWPAN/IPHC_Decode/cipv6.h"
#include "6LoWPAN/ND/nd_router_object.h"
#ifndef NO_MLE
#include "MLE/mle.h"
#endif
#include "6LoWPAN/Mesh/mesh.h"
#include "6LoWPAN/Thread/thread_common.h"
#include "6LoWPAN/Thread/thread_nd.h"
#include "6LoWPAN/MAC/mac_helper.h"
#ifdef ECC
#include "libX509_V3.h"
#include "ecc.h"
#endif
#include "ccmLIB.h"
#include "shalib.h"
#include "Security/TLS/tls_lib.h"
#include "Security/Common/sec_lib.h"
#include "net_nvm_api.h"
#include "Security/PANA/pana.h"
#include "Security/PANA/pana_internal_api.h"
#include "Common_Protocols/tcp.h"
#include "randLIB.h"
#include "6LoWPAN/ND/nd_router_object.h"
#include "common_functions.h"
#include "BorderRouter/border_router.h"
#include "net_rpl.h"
#ifdef HAVE_RPL
#ifndef NO_MLE
#include "NWK_INTERFACE/Include/protocol_stats.h"
#endif
#endif
#include "sw_mac.h"
#include "6LoWPAN/MAC/mac_data_poll.h"
#include "6LoWPAN/MAC/mpx_api.h"
#include "6LoWPAN/lowpan_adaptation_interface.h"
#include "6LoWPAN/Fragmentation/cipv6_fragmenter.h"
#include "Service_Libs/etx/etx.h"
#include "Service_Libs/mac_neighbor_table/mac_neighbor_table.h"
#include "6LoWPAN/ws/ws_common.h"
#include "6LoWPAN/ws/ws_bootstrap.h"
#ifdef HAVE_WS
#include "6LoWPAN/ws/ws_cfg_settings.h"
#endif
#define TRACE_GROUP_LOWPAN "6lo"
#define TRACE_GROUP "6lo"
/* Data rate for application used in Stagger calculation */
#define STAGGER_DATARATE_FOR_APPL(n) ((n)*25/100)
#ifdef HAVE_RPL
rpl_domain_t *protocol_6lowpan_rpl_domain;
/* Having to sidestep old rpl_dodag_t type for the moment */
struct rpl_dodag *protocol_6lowpan_rpl_root_dodag;
#endif
static uint8_t protocol_buffer_valid(buffer_t *b, protocol_interface_info_entry_t *cur)
{
uint8_t valid = 1;
if (cur) {
if ((b->info & B_TO_MAC_MLME_MASK) != B_TO_MAC_FROM_MAC) {
if (cur->lowpan_info & INTERFACE_NWK_ACTIVE) {
//if((power_save_state & (SLEEP_MODE_REQ | ICMP_ACTIVE)) == SLEEP_MODE_REQ)
if (check_power_state(SLEEP_MODE_REQ | ICMP_ACTIVE) == SLEEP_MODE_REQ) {
/* STill active but Sleep Req and ICMP is not ACTIVE */
valid = 0;
}
} else {
/* Set Clean Core Becauce Protocol Is not Active */
valid = 0;
}
}
} else {
valid = 0;
}
return valid;
}
void protocol_init(void)
{
tr_debug("P.Init\n");
#ifdef PANA
sec_libray_init();
#endif
#ifdef HAVE_RPL
protocol_6lowpan_rpl_domain = rpl_control_create_domain();
#endif
#ifdef HAVE_WS
ws_cfg_settings_init();
#endif
}
void protocol_6lowpan_stack(buffer_t *b)
{
protocol_interface_info_entry_t *cur = b->interface;
if (protocol_buffer_valid(b, cur) == 0) {
tr_debug("Drop Packets\n");
buffer_free(b);
return;
}
/* Protocol Buffer Handle until Buffer Go out from Stack */
while (b) {
/* Buffer Direction Select Switch */
if ((b->info & B_DIR_MASK) == B_DIR_DOWN) {
/* Direction DOWN */
switch (b->info & B_TO_MASK) {
case B_TO_ICMP:
/* Build ICMP Header */
b = icmpv6_down(b);
break;
case B_TO_UDP:
/* Build UDP Header */
b = udp_down(b);
break;
case B_TO_IPV6:
/* Build IP header */
b = ipv6_down(b);
break;
case B_TO_IPV6_FWD:
/* IPv6 forwarding */
b = ipv6_forwarding_down(b);
break;
case B_TO_IPV6_TXRX:
/* Resolution, Compress IP header */
b = lowpan_down(b);
break;
#ifdef HAVE_MESH
case B_TO_MESH_ROUTING:
/* Add mesh header */
b = mesh_down(b);
break;
#endif
case B_TO_MAC:
/* no break */
case B_TO_PHY:
b = lowpan_adaptation_data_process_tx_preprocess(cur, b);
if (lowpan_adaptation_interface_tx(cur, b) != 0) {
tr_error("Adaptation Layer Data req fail");
}
b = NULL;
break;
default:
b = buffer_free(b);
break;
}
} else {
/* Direction UP */
switch (b->info & B_TO_MASK) {
case B_TO_APP:
/* Push Socket Interface */
socket_up(b);
b = NULL;
break;
case B_TO_ICMP:
/* Parse ICMP Message */
b = icmpv6_up(b);
break;
case B_TO_FRAGMENTATION:
/* Packet Reasemley */
b = cipv6_frag_reassembly(cur->id, b);
break;
case B_TO_UDP:
/* Parse UDP Message */
b = udp_up(b);
break;
case B_TO_IPV6_FWD:
/* Handle IP Payload */
b = ipv6_forwarding_up(b);
break;
case B_TO_IPV6_TXRX:
/* Handle MAC Payload */
b = lowpan_up(b);
break;
#ifdef HAVE_MESH
case B_TO_MESH_ROUTING:
/* Handle Mesh header */
b = mesh_up(b);
break;
#endif
case B_TO_TCP:
b = tcp_up(b);
break;
default:
tr_debug("LLL\n");
b = buffer_free(b);
break;
}
}
if (b) {
//Check If Stack forward packet to different interface
if (b->interface != cur) {
protocol_push(b);
b = 0;
}
}
}
}
/* Return length of option always, and write option if opt_out != NULL */
static uint8_t protocol_6lowpan_llao_write(protocol_interface_info_entry_t *cur, uint8_t *opt_out, uint8_t opt_type, bool must, const uint8_t *ip_addr)
{
/* Don't bother including optional LLAO if it's a link-local address -
* they should be mapping anyway.
*/
if (!must && addr_is_ipv6_link_local(ip_addr)) {
return 0;
}
uint16_t mac16 = mac_helper_mac16_address_get(cur);
/* Even if we have a short address, use long address if the IP address's IID matches it */
if (mac16 >= 0xfffe || addr_iid_matches_eui64(ip_addr + 8, cur->mac)) {
if (opt_out) {
opt_out[0] = opt_type;
opt_out[1] = 2;
memcpy(opt_out + 2, cur->mac, 8);
memset(opt_out + 10, 0, 6);
}
return 16;
} else {
if (opt_out) {
opt_out[0] = opt_type;
opt_out[1] = 1;
common_write_16_bit(mac16, opt_out + 2);
memset(opt_out + 4, 0, 4);
}
return 8;
}
}
/* Parse, and return actual size, or 0 if error */
static uint8_t protocol_6lowpan_llao_parse(protocol_interface_info_entry_t *cur, const uint8_t *opt_in, sockaddr_t *ll_addr_out)
{
common_write_16_bit(cur->mac_parameters->pan_id, ll_addr_out->address + 0);
switch (opt_in[1]) {
case 1:
ll_addr_out->addr_type = ADDR_802_15_4_SHORT;
memcpy(ll_addr_out->address + 2, opt_in + 2, 2);
return 2 + 2;
case 2:
ll_addr_out->addr_type = ADDR_802_15_4_LONG;
memcpy(ll_addr_out->address + 2, opt_in + 2, 8);
return 2 + 8;
default:
return 0;
}
}
static bool protocol_6lowpan_map_ip_to_link_addr(protocol_interface_info_entry_t *cur, const uint8_t *ip_addr, addrtype_t *ll_type, const uint8_t **ll_addr_out)
{
static uint8_t ll_addr[10];
*ll_type = ADDR_NONE;
/* RFC 6775 says link-local addresses are based on extended MAC (LL64) */
/* ZigBee IP and Thread both also have link-local addresses based on short MAC (LL16) */
/* Our old IP stack assumed all addresses were based on MAC; this is available as an option */
if (cur->iids_map_to_mac || addr_is_ipv6_link_local(ip_addr)) {
if (memcmp(&ip_addr[8], ADDR_SHORT_ADR_SUFFIC, 6) == 0) {
*ll_type = ADDR_802_15_4_SHORT;
memcpy(&ll_addr[2], &ip_addr[14], 2);
} else {
*ll_type = ADDR_802_15_4_LONG;
memcpy(&ll_addr[2], &ip_addr[8], 8);
ll_addr[2] ^= 2;
}
} else if (thread_addr_is_mesh_local(ip_addr, cur)) {
if (memcmp(&ip_addr[8], ADDR_SHORT_ADR_SUFFIC, 6) == 0) {
/* Thread ML16s map directly to MAC (mesh) short */
*ll_type = ADDR_802_15_4_SHORT;
memcpy(&ll_addr[2], &ip_addr[14], 2);
}
}
if (*ll_type != ADDR_NONE) {
common_write_16_bit(cur->mac_parameters->pan_id, &ll_addr[0]);
*ll_addr_out = ll_addr;
return true;
} else {
return false;
}
}
static bool protocol_6lowpan_map_link_addr_to_ip(protocol_interface_info_entry_t *cur, addrtype_t ll_type, const uint8_t *ll_addr, uint8_t *ip_addr_out)
{
(void)cur;
switch (ll_type) {
case ADDR_802_15_4_LONG:
memcpy(ip_addr_out, ADDR_LINK_LOCAL_PREFIX, 8);
memcpy(ip_addr_out + 8, ll_addr + 2, 8);
ip_addr_out[8] ^= 0x02;
return true;
case ADDR_802_15_4_SHORT:
if (common_read_16_bit(ll_addr + 2) > 0xfffd) {
return false;
}
memcpy(ip_addr_out, ADDR_LINK_LOCAL_PREFIX, 8);
memcpy(ip_addr_out + 8, ADDR_SHORT_ADR_SUFFIC, 6);
memcpy(ip_addr_out + 14, ll_addr + 2, 2);
return true;
default:
return false;
}
}
void protocol_6lowpan_host_init(protocol_interface_info_entry_t *cur, bool sleepy_host)
{
if (sleepy_host) {
cur->bootsrap_mode = ARM_NWK_BOOTSRAP_MODE_6LoWPAN_SLEEPY_HOST;
} else {
cur->bootsrap_mode = ARM_NWK_BOOTSRAP_MODE_6LoWPAN_HOST;
}
//Clear always INTERFACE_NWK_ROUTER_DEVICE, INTERFACE_NWK_CONF_MAC_RX_OFF_IDLE
cur->lowpan_info &= ~(INTERFACE_NWK_ROUTER_DEVICE | INTERFACE_NWK_CONF_MAC_RX_OFF_IDLE);
mac_helper_pib_boolean_set(cur, macRxOnWhenIdle, true);
mac_data_poll_init(cur);
arm_nwk_6lowpan_borderrouter_data_free(cur);
}
void protocol_6lowpan_router_init(protocol_interface_info_entry_t *cur)
{
cur->bootsrap_mode = ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER;
cur->lowpan_info |= INTERFACE_NWK_ROUTER_DEVICE;
cur->lowpan_info &= ~INTERFACE_NWK_CONF_MAC_RX_OFF_IDLE;
mac_data_poll_init(cur);
arm_nwk_6lowpan_borderrouter_data_free(cur);
}
void protocol_6lowpan_configure_core(protocol_interface_info_entry_t *cur)
{
cur->dup_addr_detect_transmits = 0;
cur->ipv6_neighbour_cache.max_ll_len = 2 + 8;
cur->ipv6_neighbour_cache.link_mtu = LOWPAN_MTU;
#ifdef HAVE_6LOWPAN_ND
cur->ipv6_neighbour_cache.send_nud_probes = nd_params.send_nud_probes;
cur->ipv6_neighbour_cache.probe_avoided_routers = nd_params.send_nud_probes;
cur->iids_map_to_mac = nd_params.iids_map_to_mac;
#endif
cur->ip_multicast_as_mac_unicast_to_parent = false;
cur->max_link_mtu = LOWPAN_MAX_MTU;
cur->send_mld = false;
#ifdef HAVE_6LOWPAN_ND
nd_6lowpan_set_radv_params(cur);
#endif
}
void protocol_6lowpan_register_handlers(protocol_interface_info_entry_t *cur)
{
cur->if_stack_buffer_handler = protocol_6lowpan_stack;
cur->if_llao_parse = protocol_6lowpan_llao_parse;
cur->if_llao_write = protocol_6lowpan_llao_write;
cur->if_map_ip_to_link_addr = protocol_6lowpan_map_ip_to_link_addr;
cur->if_map_link_addr_to_ip = protocol_6lowpan_map_link_addr_to_ip;
if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER ||
cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER) {
cur->ipv6_neighbour_cache.recv_addr_reg = true;
cur->ipv6_neighbour_cache.recv_ns_aro = true;
}
/* Always send AROs, (compulsory for hosts, and "SHOULD" in RFC 6557 6.5.5
* for routers, as RPL doesn't deal with it) */
cur->ipv6_neighbour_cache.send_addr_reg = true;
cur->ipv6_neighbour_cache.recv_na_aro = true;
cur->ipv6_neighbour_cache.use_eui64_as_slla_in_aro = false;
}
void protocol_6lowpan_release_short_link_address_from_neighcache(protocol_interface_info_entry_t *cur, uint16_t shortAddress)
{
uint8_t temp_ll[4];
uint8_t *ptr = temp_ll;
ptr = common_write_16_bit(cur->mac_parameters->pan_id, ptr);
ptr = common_write_16_bit(shortAddress, ptr);
ipv6_neighbour_invalidate_ll_addr(&cur->ipv6_neighbour_cache,
ADDR_802_15_4_SHORT, temp_ll);
if (thread_info(cur)) {
thread_nd_address_remove(cur, ADDR_802_15_4_SHORT, temp_ll);
} else {
nd_remove_registration(cur, ADDR_802_15_4_SHORT, temp_ll);
}
}
void protocol_6lowpan_release_long_link_address_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *mac64)
{
uint8_t temp_ll[10];
uint8_t *ptr = temp_ll;
ptr = common_write_16_bit(cur->mac_parameters->pan_id, ptr);
memcpy(ptr, mac64, 8);
ipv6_neighbour_invalidate_ll_addr(&cur->ipv6_neighbour_cache,
ADDR_802_15_4_LONG, temp_ll);
if (thread_info(cur)) {
thread_nd_address_remove(cur, ADDR_802_15_4_LONG, temp_ll);
} else {
nd_remove_registration(cur, ADDR_802_15_4_LONG, temp_ll);
}
}
#ifdef HAVE_6LOWPAN_ND
static int8_t mle_set_link_priority(protocol_interface_info_entry_t *cur, const uint8_t *address, bool priority)
{
uint8_t mac64[8];
mac_neighbor_table_entry_t *entry;
if (!memcmp(address, ADDR_SHORT_ADR_SUFFIC, 6)) {
entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), address + 6, ADDR_802_15_4_SHORT);
} else {
memcpy(mac64, address, 8);
mac64[0] ^= 2;
entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), mac64, ADDR_802_15_4_LONG);
}
if (!entry) {
return -1;
}
if (priority) {
entry->link_role = PRIORITY_PARENT_NEIGHBOUR;
} else {
entry->link_role = NORMAL_NEIGHBOUR;
}
return 0;
}
void protocol_6lowpan_neighbor_priority_update(protocol_interface_info_entry_t *cur, uint8_t *removed_priority, uint8_t *updated_priority)
{
if (cur->lowpan_info & INTERFACE_NWK_BOOTSRAP_MLE) {
#ifndef NO_MLE
if (removed_priority) {
mle_set_link_priority(cur, removed_priority, false);
}
if (updated_priority) {
mle_set_link_priority(cur, updated_priority, true);
}
#endif
}
}
#ifdef HAVE_RPL
uint16_t protocol_6lowpan_neighbor_priority_set(int8_t interface_id, addrtype_t addr_type, const uint8_t *addr_ptr)
{
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
if (!cur || !addr_ptr) {
return 0;
}
mac_neighbor_table_entry_t *entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), addr_ptr + PAN_ID_LEN, addr_type);
if (entry) {
bool new_primary = false;
etx_storage_t *etx_entry = etx_storage_entry_get(interface_id, entry->index);
// If primary parent has changed clears priority from previous parent
if (entry->link_role != PRIORITY_PARENT_NEIGHBOUR) {
new_primary = true;
protocol_6lowpan_neighbor_priority_clear_all(interface_id, PRIORITY_1ST);
}
entry->link_role = PRIORITY_PARENT_NEIGHBOUR;
uint8_t temp[2];
common_write_16_bit(entry->mac16, temp);
mac_helper_coordinator_address_set(cur, ADDR_802_15_4_SHORT, temp);
mac_helper_coordinator_address_set(cur, ADDR_802_15_4_LONG, entry->mac64);
if (etx_entry) {
protocol_stats_update(STATS_ETX_1ST_PARENT, etx_entry->etx >> 4);
}
if (new_primary) {
ws_primary_parent_update(cur, entry);
}
return 1;
} else {
return 0;
}
}
uint16_t protocol_6lowpan_neighbor_second_priority_set(int8_t interface_id, addrtype_t addr_type, const uint8_t *addr_ptr)
{
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
if (!cur || !addr_ptr) {
return 0;
}
mac_neighbor_table_entry_t *entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), addr_ptr + PAN_ID_LEN, addr_type);
if (entry) {
bool new_secondary = false;
etx_storage_t *etx_entry = etx_storage_entry_get(interface_id, entry->index);
// If secondary parent has changed clears priority from previous parent
if (entry->link_role != SECONDARY_PARENT_NEIGHBOUR) {
new_secondary = true;
protocol_6lowpan_neighbor_priority_clear_all(interface_id, PRIORITY_2ND);
}
entry->link_role = SECONDARY_PARENT_NEIGHBOUR;
if (etx_entry) {
protocol_stats_update(STATS_ETX_2ND_PARENT, etx_entry->etx >> 4);
}
if (new_secondary) {
ws_secondary_parent_update(cur);
}
return 1;
} else {
return 0;
}
}
void protocol_6lowpan_neighbor_priority_clear_all(int8_t interface_id, neighbor_priority priority)
{
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
if (!cur) {
return;
}
mac_neighbor_table_list_t *mac_table_list = &mac_neighbor_info(cur)->neighbour_list;
ns_list_foreach(mac_neighbor_table_entry_t, entry, mac_table_list) {
if (priority == PRIORITY_1ST && entry->link_role == PRIORITY_PARENT_NEIGHBOUR) {
entry->link_role = NORMAL_NEIGHBOUR;
} else {
if (entry->link_role == SECONDARY_PARENT_NEIGHBOUR) {
protocol_stats_update(STATS_ETX_2ND_PARENT, 0);
entry->link_role = NORMAL_NEIGHBOUR;
}
}
}
}
#endif
#endif
int8_t protocol_6lowpan_neighbor_address_state_synch(protocol_interface_info_entry_t *cur, const uint8_t eui64[8], const uint8_t iid[8])
{
int8_t ret_val = -1;
mac_neighbor_table_entry_t *entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), eui64, ADDR_802_15_4_LONG);
if (entry) {
if (memcmp(iid, ADDR_SHORT_ADR_SUFFIC, 6) == 0) {
iid += 6;
//Set Short Address to MLE
entry->mac16 = common_read_16_bit(iid);
}
if (!entry->ffd_device) {
if (entry->connected_device) {
mac_neighbor_table_neighbor_refresh(mac_neighbor_info(cur), entry, entry->link_lifetime);
}
ret_val = 1;
} else {
ret_val = 0;
}
}
return ret_val;
}
int8_t protocol_6lowpan_neighbor_remove(protocol_interface_info_entry_t *cur, uint8_t *address_ptr, addrtype_t type)
{
mac_neighbor_table_entry_t *entry = mac_neighbor_table_address_discover(mac_neighbor_info(cur), address_ptr, type);
if (entry) {
mac_neighbor_table_neighbor_remove(mac_neighbor_info(cur), entry);
}
return 0;
}
void protocol_6lowpan_allocate_mac16(protocol_interface_info_entry_t *cur)
{
if (cur) {
cur->lowpan_desired_short_address = (randLIB_get_16bit() & 0x7fff);
}
}
void protocol_6lowpan_interface_common_init(protocol_interface_info_entry_t *cur)
{
cur->lowpan_info |= INTERFACE_NWK_ACTIVE;
protocol_6lowpan_register_handlers(cur);
ipv6_route_add(ADDR_LINK_LOCAL_PREFIX, 64, cur->id, NULL, ROUTE_STATIC, 0xFFFFFFFF, 0);
// Putting a multicast route to ff00::/8 makes sure we can always transmit multicast.
// Interface metric will determine which interface is actually used, if we have multiple.
ipv6_route_add(ADDR_LINK_LOCAL_ALL_NODES, 8, cur->id, NULL, ROUTE_STATIC, 0xFFFFFFFF, -1);
}
int8_t protocol_6lowpan_interface_compare_cordinator_netid(protocol_interface_info_entry_t *cur, uint8_t *adr_ptr)
{
int8_t ret_val = -1;
if (cur) {
addrtype_t addrType;
uint8_t tempAddress[8];
addrType = mac_helper_coordinator_address_get(cur, tempAddress);
if (addrType == ADDR_802_15_4_LONG) {
tempAddress[0] ^= 2;
if (memcmp(adr_ptr, tempAddress, 8) == 0) {
ret_val = 0;
}
} else if (addrType == ADDR_802_15_4_SHORT) {
if (adr_ptr[6] == tempAddress[0] && adr_ptr[7] == tempAddress[1]) {
ret_val = 0;
}
}
}
return ret_val;
}
int8_t protocol_6lowpan_interface_get_link_local_cordinator_address(protocol_interface_info_entry_t *cur, uint8_t *adr_ptr)
{
addrtype_t addrType;
uint8_t tempAddress[8];
addrType = mac_helper_coordinator_address_get(cur, tempAddress);
if (addrType == ADDR_NONE) {
return -1;
}
memcpy(adr_ptr, ADDR_LINK_LOCAL_PREFIX, 8);
adr_ptr += 8;
if (addrType == ADDR_802_15_4_LONG) {
tempAddress[0] ^= 2;
memcpy(adr_ptr, tempAddress, 8);
} else {
memcpy(adr_ptr, ADDR_SHORT_ADR_SUFFIC, 6);
adr_ptr += 6;
*adr_ptr++ = tempAddress[0];
*adr_ptr = tempAddress[1];
}
return 0;
}
int8_t protocol_6lowpan_interface_get_mac_coordinator_address(protocol_interface_info_entry_t *cur, sockaddr_t *adr_ptr)
{
common_write_16_bit(cur->mac_parameters->pan_id, adr_ptr->address + 0);
adr_ptr->addr_type = mac_helper_coordinator_address_get(cur, adr_ptr->address + 2);
if (adr_ptr->addr_type == ADDR_NONE) {
return -1;
}
return 0;
}
int16_t protocol_6lowpan_rpl_global_priority_get(void)
{
#ifdef HAVE_RPL
uint8_t instance_id_list[10];
uint8_t rpl_instance_count;
uint8_t instance_id = RPL_INSTANCE_LOCAL;
uint8_t instance_id_new;
uint8_t instance_index;
rpl_instance_count = rpl_instance_list_read(&instance_id_list[0], sizeof(instance_id_list));
/* Find lowest global instance ID (assumption: RPL instance with lowest instance ID has
most generic routing rule and its rank should be indicated in beacon) */
for (instance_index = 0; instance_index < rpl_instance_count; instance_index++) {
instance_id_new = instance_id_list[instance_index];
if ((instance_id_new & RPL_INSTANCE_LOCAL) == RPL_INSTANCE_LOCAL) {
break;
} else {
if (instance_id_new < instance_id) {
instance_id = instance_id_new;
}
}
}
// Get DAG rank
if (instance_id == RPL_INSTANCE_LOCAL) {
return 255;
}
rpl_dodag_info_t rpl_dodag_info;
if (!rpl_read_dodag_info(&rpl_dodag_info, instance_id)) {
return 255;
}
if (rpl_dodag_info.curent_rank == RPL_RANK_INFINITE) {
return 255;
}
// Default implementation is
// 32 * (7 - DODAGPreference) + 3 * (DAGRank - 1)
int16_t priority;
priority = 32 * (7 - RPL_DODAG_PREF(rpl_dodag_info.flags));
priority += 3 * (rpl_dodag_info.curent_rank / rpl_dodag_info.dag_min_hop_rank_inc - 1);
return priority;
#else
return 255;
#endif
}
uint8_t protocol_6lowpan_beacon_join_priority_tx(int8_t interface_id)
{
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
if (!cur) {
return 255;
}
int16_t priority = 0;
#ifdef HAVE_RPL
if (cur->rpl_domain) {
priority = protocol_6lowpan_rpl_global_priority_get();
}
#endif
#ifndef NO_MLE
mle_6lowpan_data_t *mle_6lowpan_data = protocol_6lowpan_mle_data_get();
if (mle_6lowpan_data && mle_6lowpan_data->nbr_of_neigh_max != 0) {
uint16_t mle_neigh_cnt = mle_class_active_neigh_counter(cur);
if (mle_neigh_cnt > mle_6lowpan_data->nbr_of_neigh_lower_threshold) {
uint16_t mle_neigh_limit;
mle_neigh_cnt -= mle_6lowpan_data->nbr_of_neigh_lower_threshold;
mle_neigh_limit = 32 * mle_neigh_cnt / (mle_6lowpan_data->nbr_of_neigh_max - mle_6lowpan_data->nbr_of_neigh_lower_threshold);
priority += mle_neigh_limit;
}
}
#endif
if (priority < 0) {
priority = 0;
} else if (priority > 255) {
priority = 255;
}
return priority;
}
uint8_t protocol_6lowpan_beacon_compare_rx(int8_t interface_id, uint8_t join_priority, uint8_t link_quality)
{
(void)interface_id; // not used, perhaps should be removed completely
uint16_t conn_to_pref;
conn_to_pref = ((256 - join_priority) * (uint16_t) link_quality) >> 8;
return conn_to_pref;
}
bool protocol_6lowpan_latency_estimate_get(int8_t interface_id, uint32_t *latency)
{
protocol_interface_info_entry_t *cur_interface = protocol_stack_interface_info_get_by_id(interface_id);
uint32_t latency_estimate = 0;
if (!cur_interface) {
return false;
}
if (cur_interface->eth_mac_api) {
// either PPP or Ethernet interface.
latency_estimate = 100;
} else if (thread_info(cur_interface)) {
// thread network
latency_estimate = 2000;
} else if (ws_info(cur_interface)) {
latency_estimate = ws_common_latency_estimate_get(cur_interface);
} else {
// 6LoWPAN ND
latency_estimate = 8000;
}
if (latency_estimate != 0) {
*latency = latency_estimate;
return true;
}
return false;
}
bool protocol_6lowpan_stagger_estimate_get(int8_t interface_id, uint32_t data_amount, uint16_t *stagger_min, uint16_t *stagger_max, uint16_t *stagger_rand)
{
size_t network_size;
uint32_t datarate;
uint32_t stagger_value;
protocol_interface_info_entry_t *cur_interface = protocol_stack_interface_info_get_by_id(interface_id);
if (!cur_interface) {
return false;
}
if (cur_interface->eth_mac_api) {
// either PPP or Ethernet interface.
network_size = 1;
datarate = 1000000;
} else if (thread_info(cur_interface)) {
// thread network
network_size = 23;
datarate = 250000;
} else if (ws_info(cur_interface)) {
network_size = ws_common_network_size_estimate_get(cur_interface);
datarate = ws_common_datarate_get(cur_interface);
} else {
// 6LoWPAN ND
network_size = 1000;
datarate = 250000;
}
if (data_amount == 0) {
// If no data amount given, use 1kB
data_amount = 1;
}
/*
* Do not occupy whole bandwidth, leave space for network formation etc...
*/
datarate = STAGGER_DATARATE_FOR_APPL(datarate);
stagger_value = 1 + ((data_amount * 1024 * 8 * network_size) / datarate);
/**
* Example:
* Maximum stagger value to send 1kB to 100 device network using data rate of 50kbs:
* 1 + (1 * 1024 * 8 * 100) / (50000*0.25) = 66s
*/
*stagger_min = stagger_value / 5; // Minimum stagger value is 1/5 of the max
if (stagger_value > 0xFFFF) {
*stagger_max = 0xFFFF;
} else {
*stagger_max = (uint16_t)stagger_value;
}
// Randomize stagger value
*stagger_rand = randLIB_get_random_in_range(*stagger_min, *stagger_max);
return true;
}