-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathws_bootstrap.c
2895 lines (2529 loc) · 115 KB
/
ws_bootstrap.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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018-2021, Pelion 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 <string.h>
#include "nsconfig.h"
#ifdef HAVE_WS
#include "ns_types.h"
#include "ns_trace.h"
#include "nsdynmemLIB.h"
#include "net_interface.h"
#include "eventOS_event.h"
#include "randLIB.h"
#include "common_functions.h"
#include "mac_common_defines.h"
#include "sw_mac.h"
#include "ccmLIB.h"
#include "Core/include/ns_monitor.h"
#include "NWK_INTERFACE/Include/protocol.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan_interface.h"
#include "ipv6_stack/protocol_ipv6.h"
#include "ipv6_stack/ipv6_routing_table.h"
#include "6LoWPAN/MAC/mac_helper.h"
#include "6LoWPAN/MAC/mac_data_poll.h"
#include "6LoWPAN/MAC/mpx_api.h"
#include "6LoWPAN/MAC/mac_ie_lib.h"
#include "MPL/mpl.h"
#include "RPL/rpl_protocol.h"
#include "RPL/rpl_control.h"
#include "RPL/rpl_data.h"
#include "RPL/rpl_policy.h"
#include "Common_Protocols/icmpv6.h"
#include "Common_Protocols/icmpv6_radv.h"
#include "Common_Protocols/ipv6_constants.h"
#include "Common_Protocols/ip.h"
#include "Service_Libs/Trickle/trickle.h"
#include "Service_Libs/fhss/channel_list.h"
#include "Service_Libs/utils/ns_time.h"
#include "6LoWPAN/ws/ws_common_defines.h"
#include "6LoWPAN/ws/ws_common_defines.h"
#include "6LoWPAN/ws/ws_config.h"
#include "6LoWPAN/ws/ws_common.h"
#include "6LoWPAN/ws/ws_bootstrap.h"
#include "6LoWPAN/ws/ws_bbr_api_internal.h"
#include "6LoWPAN/ws/ws_common_defines.h"
#include "6LoWPAN/ws/ws_llc.h"
#include "6LoWPAN/ws/ws_neighbor_class.h"
#include "6LoWPAN/ws/ws_ie_lib.h"
#include "6LoWPAN/ws/ws_stats.h"
#include "6LoWPAN/ws/ws_cfg_settings.h"
#include "6LoWPAN/ws/ws_bootstrap_6lbr.h"
#include "6LoWPAN/ws/ws_bootstrap_6lr.h"
#include "6LoWPAN/ws/ws_bootstrap_ffn.h"
#include "6LoWPAN/ws/ws_bootstrap_6ln.h"
#include "6LoWPAN/ws/ws_phy.h"
#include "6LoWPAN/lowpan_adaptation_interface.h"
#include "Service_Libs/etx/etx.h"
#include "Service_Libs/mac_neighbor_table/mac_neighbor_table.h"
#include "Service_Libs/nd_proxy/nd_proxy.h"
#include "Service_Libs/blacklist/blacklist.h"
#include "platform/topo_trace.h"
#include "dhcp_service_api.h"
#include "libDHCPv6/libDHCPv6.h"
#include "libDHCPv6/libDHCPv6_vendordata.h"
#include "DHCPv6_client/dhcpv6_client_api.h"
#include "ws_management_api.h"
#include "net_rpl.h"
#include "mac_api.h"
#include "6LoWPAN/ws/ws_pae_controller.h"
#include "6LoWPAN/ws/ws_eapol_pdu.h"
#include "6LoWPAN/ws/ws_eapol_auth_relay.h"
#include "6LoWPAN/ws/ws_eapol_relay.h"
#include "libNET/src/net_dns_internal.h"
#include "Service_Libs/random_early_detection/random_early_detection_api.h"
#define TRACE_GROUP "wsbs"
static void ws_bootstrap_event_handler(arm_event_s *event);
static int8_t ws_bootsrap_event_trig(ws_bootsrap_event_type_e event_type, int8_t interface_id, arm_library_event_priority_e priority, void *event_data);
static uint16_t ws_bootstrap_routing_cost_calculate(protocol_interface_info_entry_t *cur);
static void ws_bootstrap_mac_security_enable(protocol_interface_info_entry_t *cur);
static void ws_bootstrap_nw_key_set(protocol_interface_info_entry_t *cur, uint8_t operation, uint8_t index, uint8_t *key);
static void ws_bootstrap_nw_key_clear(protocol_interface_info_entry_t *cur, uint8_t slot);
static void ws_bootstrap_nw_key_index_set(protocol_interface_info_entry_t *cur, uint8_t index);
static void ws_bootstrap_nw_frame_counter_set(protocol_interface_info_entry_t *cur, uint32_t counter, uint8_t slot);
static void ws_bootstrap_nw_frame_counter_read(protocol_interface_info_entry_t *cur, uint32_t *counter, uint8_t slot);
static void ws_bootstrap_authentication_completed(protocol_interface_info_entry_t *cur, auth_result_e result, uint8_t *target_eui_64);
static const uint8_t *ws_bootstrap_authentication_next_target(protocol_interface_info_entry_t *cur, const uint8_t *previous_eui_64, uint16_t *pan_id);
static ws_nud_table_entry_t *ws_nud_entry_discover(protocol_interface_info_entry_t *cur, void *neighbor);
static void ws_nud_entry_remove(protocol_interface_info_entry_t *cur, mac_neighbor_table_entry_t *entry_ptr);
static bool ws_neighbor_entry_nud_notify(mac_neighbor_table_entry_t *entry_ptr, void *user_data);
static void ws_bootstrap_test_procedure_trigger_timer(protocol_interface_info_entry_t *cur, uint32_t seconds);
uint16_t test_pan_version = 1;
static mac_neighbor_table_entry_t *ws_bootstrap_mac_neighbor_allocate(struct protocol_interface_info_entry *interface, const uint8_t *src64)
{
mac_neighbor_table_entry_t *neighbor = mac_neighbor_table_entry_allocate(mac_neighbor_info(interface), src64);
if (!neighbor) {
return NULL;
}
// TODO only call these for new neighbour
mlme_device_descriptor_t device_desc;
neighbor->lifetime = ws_cfg_neighbour_temporary_lifetime_get();
neighbor->link_lifetime = ws_cfg_neighbour_temporary_lifetime_get();
mac_helper_device_description_write(interface, &device_desc, neighbor->mac64, neighbor->mac16, 0, false);
mac_helper_devicetable_set(&device_desc, interface, neighbor->index, interface->mac_parameters->mac_default_key_index, true);
return neighbor;
}
mac_neighbor_table_entry_t *ws_bootstrap_mac_neighbor_add(struct protocol_interface_info_entry *interface, const uint8_t *src64)
{
mac_neighbor_table_entry_t *neighbor = mac_neighbor_table_address_discover(mac_neighbor_info(interface), src64, MAC_ADDR_MODE_64_BIT);
if (neighbor) {
return neighbor;
}
return ws_bootstrap_mac_neighbor_allocate(interface, src64);
}
void ws_bootstrap_neighbor_set_stable(struct protocol_interface_info_entry *interface, const uint8_t *src64)
{
mac_neighbor_table_entry_t *neighbor = mac_neighbor_table_address_discover(mac_neighbor_info(interface), src64, MAC_ADDR_MODE_64_BIT);
if (neighbor && neighbor->link_lifetime != WS_NEIGHBOR_LINK_TIMEOUT) {
neighbor->lifetime = WS_NEIGHBOR_LINK_TIMEOUT;
neighbor->link_lifetime = WS_NEIGHBOR_LINK_TIMEOUT;
tr_info("Added new neighbor %s : index:%u", trace_array(src64, 8), neighbor->index);
}
}
void ws_bootstrap_mac_neighbor_short_time_set(struct protocol_interface_info_entry *interface, const uint8_t *src64, uint32_t valid_time)
{
mac_neighbor_table_entry_t *neighbor = mac_neighbor_table_address_discover(mac_neighbor_info(interface), src64, MAC_ADDR_MODE_64_BIT);
if (neighbor && neighbor->link_lifetime <= valid_time) {
//mlme_device_descriptor_t device_desc;
neighbor->lifetime = valid_time;
neighbor->link_lifetime = valid_time;
tr_debug("Set short response neighbor %s : index:%u", trace_array(src64, 8), neighbor->index);
}
}
static void ws_bootstrap_neighbor_delete(struct protocol_interface_info_entry *interface, mac_neighbor_table_entry_t *entry_ptr)
{
mac_helper_devicetable_remove(interface->mac_api, entry_ptr->index, entry_ptr->mac64);
etx_neighbor_remove(interface->id, entry_ptr->index, entry_ptr->mac64);
ws_neighbor_class_entry_remove(&interface->ws_info->neighbor_storage, entry_ptr->index);
}
void ws_bootstrap_neighbor_list_clean(struct protocol_interface_info_entry *interface)
{
mac_neighbor_table_neighbor_list_clean(mac_neighbor_info(interface));
}
void ws_address_reregister_trig(struct protocol_interface_info_entry *interface)
{
if (interface->ws_info->aro_registration_timer == 0) {
interface->ws_info->aro_registration_timer = WS_NEIGHBOR_NUD_TIMEOUT;
}
}
static void ws_bootstrap_address_notification_cb(struct protocol_interface_info_entry *interface, const struct if_address_entry *addr, if_address_callback_t reason)
{
/* No need for LL address registration */
if (addr->source == ADDR_SOURCE_UNKNOWN || !interface->ws_info) {
return;
}
if (reason == ADDR_CALLBACK_DAD_COMPLETE) {
//If address is generated manually we need to force registration
if (addr->source != ADDR_SOURCE_DHCP) {
//Trigger Address Registration only when Bootstrap is ready
if (interface->nwk_bootstrap_state == ER_BOOTSRAP_DONE) {
tr_debug("Address registration %s", trace_ipv6(addr->address));
ws_bootstrap_6lr_address_registration_update(interface, addr->address);
}
ws_address_reregister_trig(interface);
}
if (addr_ipv6_scope(addr->address, interface) > IPV6_SCOPE_LINK_LOCAL) {
// at least ula address available inside mesh.
interface->global_address_available = true;
}
} else if (reason == ADDR_CALLBACK_DELETED) {
// What to do?
// Go through address list and check if there is global address still available
if (addr->source == ADDR_SOURCE_DHCP) {
//Deprecate dhcpv address
uint8_t address[16];
memcpy(address, addr->address, 16);
dhcp_client_global_address_delete(interface->id, NULL, address);
}
//Discover prefix policy
addr_policy_remove_by_label(WS_NON_PREFFRED_LABEL);
interface->global_address_available = false;
ns_list_foreach(if_address_entry_t, addr_str, &interface->ip_addresses) {
if (addr_ipv6_scope(addr_str->address, interface) > IPV6_SCOPE_LINK_LOCAL) {
// at least ula address available inside mesh.
interface->global_address_available = true;
break;
}
}
}
// Addressing in Wi-SUN interface was changed for Border router send new event so Application can update the state
if (interface->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER &&
interface->nwk_bootstrap_state == ER_BOOTSRAP_DONE) {
if (interface->bootsrap_state_machine_cnt == 0) {
interface->bootsrap_state_machine_cnt = 10; //Re trigger state check
}
}
}
#ifdef HAVE_WS_VERSION_1_1
static ws_pcap_ie_t ws_neighbour_phy_cap_list_compare(ws_phy_cap_info_t *prefered_mode, ws_phy_cap_info_t *neighbour_cap_list)
{
ws_pcap_ie_t pref_setup;
ws_pcap_ie_t *prefered_setup = prefered_mode->pcap;
int length_of_list = prefered_mode->length_of_list;
while (length_of_list) {
for (int i = 0; i < neighbour_cap_list->length_of_list; i++) {
//Check first phy type is matching
if (neighbour_cap_list->pcap[i].phy_type != prefered_setup->phy_type) {
continue;
}
//Validate supported
if (neighbour_cap_list->pcap[i].operating_mode & prefered_setup->operating_mode) {
//Take only matched opeating modes
pref_setup.operating_mode = neighbour_cap_list->pcap[i].operating_mode & prefered_setup->operating_mode;
pref_setup.phy_type = prefered_setup->phy_type;
return pref_setup;
}
break;
}
prefered_setup++;
length_of_list--;
}
//Mark zero operating modes
pref_setup.operating_mode = 0;
return pref_setup;
}
static void ws_neighbour_mdr_mode_analyze(struct protocol_interface_info_entry *interface)
{
if (!ws_version_1_1(interface)) {
return;
}
if (!interface->ws_info->uptime || (interface->ws_info->uptime % 10)) {
return;
}
if (!interface->ws_info->phy_cap_info.length_of_list) {
//No Preferred Cap modes
return;
}
ns_list_foreach_safe(mac_neighbor_table_entry_t, cur, &mac_neighbor_info(interface)->neighbour_list) {
ws_neighbor_class_entry_t *ws_neighbor = ws_neighbor_class_entry_get(&interface->ws_info->neighbor_storage, cur->index);
if (!ws_neighbor || ws_neighbor->phy_mode_id || !ws_neighbor->pcap_info.length_of_list) {
continue;
}
ws_pcap_ie_t preferred = ws_neighbour_phy_cap_list_compare(&interface->ws_info->phy_cap_info, ws_neighbour_cap_pointer(ws_neighbor));
uint8_t phy_mode_id = ws_ie_lib_phy_mode_id_get_from_phy_cap(&preferred);
if (ws_neighbor->phy_mode_id != phy_mode_id) {
tr_debug("Updated Neigh %u MDR phy mode id %u -> %u", cur->index, ws_neighbor->phy_mode_id, phy_mode_id);
ws_neighbor->phy_mode_id = phy_mode_id;
}
}
}
#else
#define ws_neighbour_mdr_mode_analyze(interface) ((void)0)
#endif
void ws_bootstrap_configure_max_retries(protocol_interface_info_entry_t *cur, uint8_t max_mac_retries)
{
mac_helper_mac_mlme_max_retry_set(cur->id, max_mac_retries);
}
void ws_bootstrap_configure_csma_ca_backoffs(protocol_interface_info_entry_t *cur, uint8_t max_backoffs, uint8_t min_be, uint8_t max_be)
{
mac_helper_mac_mlme_max_csma_backoffs_set(cur->id, max_backoffs);
mac_helper_mac_mlme_be_set(cur->id, min_be, max_be);
}
void ws_bootstrap_configure_data_request_restart(protocol_interface_info_entry_t *cur, uint8_t cca_failure_restart_max, uint8_t tx_failure_restart_max, uint16_t blacklist_min_ms, uint16_t blacklist_max_ms)
{
mlme_request_restart_config_t request_restart_config;
request_restart_config.cca_failure_restart_max = cca_failure_restart_max;
request_restart_config.tx_failure_restart_max = tx_failure_restart_max;
request_restart_config.blacklist_min_ms = blacklist_min_ms;
request_restart_config.blacklist_max_ms = blacklist_max_ms;
mac_helper_mac_mlme_data_request_restart_set(cur->id, &request_restart_config);
}
static int ws_bootstrap_tasklet_init(protocol_interface_info_entry_t *cur)
{
if (cur->bootStrapId < 0) {
cur->bootStrapId = eventOS_event_handler_create(&ws_bootstrap_event_handler, WS_INIT_EVENT);
tr_info("WS tasklet init");
}
if (cur->bootStrapId < 0) {
tr_error("tasklet init failed");
return -1;
}
return 0;
}
static void ws_nwk_event_post(protocol_interface_info_entry_t *cur, arm_nwk_interface_status_type_e posted_event)
{
arm_event_s event = {
.receiver = cur->net_start_tasklet,
.sender = protocol_read_tasklet_id(), /**< Event sender Tasklet ID */
.event_type = ARM_LIB_NWK_INTERFACE_EVENT,
.event_data = posted_event,
.event_id = (int8_t) cur->id,
.data_ptr = NULL,
.priority = ARM_LIB_LOW_PRIORITY_EVENT,
};
if (eventOS_event_send(&event) != 0) {
tr_error("nwk_net_event_post(): event send failed");
}
}
static int8_t ws_bootsrap_event_trig(ws_bootsrap_event_type_e event_type, int8_t interface_id, arm_library_event_priority_e priority, void *event_data)
{
arm_event_s event = {
.receiver = interface_id,
.sender = 0,
.event_type = event_type,
.priority = priority,
.data_ptr = event_data,
};
return eventOS_event_send(&event);
}
void ws_nud_table_reset(protocol_interface_info_entry_t *cur)
{
//Empty active list
ns_list_foreach_safe(ws_nud_table_entry_t, entry, &cur->ws_info->active_nud_process) {
ns_list_remove(&cur->ws_info->active_nud_process, entry);
}
//Empty free list
ns_list_foreach_safe(ws_nud_table_entry_t, entry, &cur->ws_info->free_nud_entries) {
ns_list_remove(&cur->ws_info->free_nud_entries, entry);
}
//Add to free list to full
for (int i = 0; i < ACTIVE_NUD_PROCESS_MAX; i++) {
ns_list_add_to_end(&cur->ws_info->free_nud_entries, &cur->ws_info->nud_table_entrys[i]);
}
}
static ws_nud_table_entry_t *ws_nud_entry_get_free(protocol_interface_info_entry_t *cur)
{
ws_nud_table_entry_t *entry = ns_list_get_first(&cur->ws_info->free_nud_entries);
if (entry) {
entry->wait_response = false;
entry->retry_count = 0;
entry->nud_process = false;
entry->timer = randLIB_get_random_in_range(1, 900);
entry->neighbor_info = NULL;
ns_list_remove(&cur->ws_info->free_nud_entries, entry);
ns_list_add_to_end(&cur->ws_info->active_nud_process, entry);
}
return entry;
}
void ws_nud_entry_remove_active(protocol_interface_info_entry_t *cur, void *neighbor)
{
ws_nud_table_entry_t *entry = ws_nud_entry_discover(cur, neighbor);
if (entry) {
mac_neighbor_table_entry_t *mac_neighbor = neighbor;
ns_list_remove(&cur->ws_info->active_nud_process, entry);
ns_list_add_to_end(&cur->ws_info->free_nud_entries, entry);
if (mac_neighbor->nud_active) {
mac_neighbor_table_neighbor_refresh(mac_neighbor_info(cur), mac_neighbor, mac_neighbor->link_lifetime);
}
mac_neighbor_table_neighbor_connected(mac_neighbor_info(cur), mac_neighbor);
}
}
static ws_nud_table_entry_t *ws_nud_entry_discover(protocol_interface_info_entry_t *cur, void *neighbor)
{
ns_list_foreach(ws_nud_table_entry_t, entry, &cur->ws_info->active_nud_process) {
if (entry->neighbor_info == neighbor) {
return entry;
}
}
return NULL;
}
static void ws_nud_state_clean(protocol_interface_info_entry_t *cur, ws_nud_table_entry_t *entry)
{
mac_neighbor_table_entry_t *neighbor = entry->neighbor_info;
ns_list_remove(&cur->ws_info->active_nud_process, entry);
ns_list_add_to_end(&cur->ws_info->free_nud_entries, entry);
if (neighbor->nud_active) {
neighbor->nud_active = false;
mac_neighbor_info(cur)->active_nud_process--;
}
}
static void ws_nud_entry_remove(protocol_interface_info_entry_t *cur, mac_neighbor_table_entry_t *entry_ptr)
{
ws_nud_table_entry_t *nud_entry = ws_nud_entry_discover(cur, entry_ptr);
if (nud_entry) {
ws_nud_state_clean(cur, nud_entry);
}
}
if_address_entry_t *ws_probe_aro_address(protocol_interface_info_entry_t *interface)
{
if (interface->global_address_available) {
ns_list_foreach(if_address_entry_t, address, &interface->ip_addresses) {
if (addr_ipv6_scope(address->address, interface) > IPV6_SCOPE_LINK_LOCAL) {
return address;
}
}
}
return NULL;
}
static bool ws_nud_message_build(protocol_interface_info_entry_t *cur, mac_neighbor_table_entry_t *neighbor, bool nud_process)
{
//Send NS
uint8_t ll_target[16];
aro_t aro_temp;
//SET ARO and src address pointer to NULL by default
aro_t *aro_ptr = NULL;
uint8_t *src_address_ptr = NULL;
ws_common_create_ll_address(ll_target, neighbor->mac64);
if (nud_process) {
tr_info("NUD generate NS %u", neighbor->index);
} else {
tr_info("Probe generate NS %u", neighbor->index);
if_address_entry_t *gp_address = ws_probe_aro_address(cur);
if (gp_address) {
src_address_ptr = gp_address->address;
aro_temp.status = ARO_SUCCESS;
aro_temp.present = true;
memcpy(aro_temp.eui64, cur->mac, 8);
//Just Short Test
aro_temp.lifetime = 1;
aro_ptr = &aro_temp;
}
}
buffer_t *buffer = icmpv6_build_ns(cur, ll_target, src_address_ptr, true, false, aro_ptr);
if (buffer) {
buffer->options.traffic_class = IP_DSCP_CS6 << IP_TCLASS_DSCP_SHIFT;
protocol_push(buffer);
return true;
}
return false;
}
void ws_nud_active_timer(protocol_interface_info_entry_t *cur, uint16_t ticks)
{
//Convert TICKS to real milliseconds
if (ticks > 0xffff / 100) {
ticks = 0xffff;
} else if (ticks == 0) {
ticks = 1;
} else {
ticks *= 100;
}
ns_list_foreach_safe(ws_nud_table_entry_t, entry, &cur->ws_info->active_nud_process) {
if (entry->timer <= ticks) {
//TX Process or timeout
if (entry->wait_response) {
//Timeout for NUD or Probe
if (entry->nud_process) {
tr_debug("NUD NA timeout");
if (entry->retry_count < 2) {
entry->timer = randLIB_get_random_in_range(1, 900);
entry->wait_response = false;
} else {
//Clear entry from active list
ws_nud_state_clean(cur, entry);
//Remove whole entry
mac_neighbor_table_neighbor_remove(mac_neighbor_info(cur), entry->neighbor_info);
}
} else {
ws_nud_state_clean(cur, entry);
}
} else {
//Random TX wait period is over
entry->wait_response = ws_nud_message_build(cur, entry->neighbor_info, entry->nud_process);
if (!entry->wait_response) {
if (entry->nud_process && entry->retry_count < 2) {
entry->timer = randLIB_get_random_in_range(1, 900);
} else {
//Clear entry from active list
//Remove and try again later on
ws_nud_state_clean(cur, entry);
}
} else {
entry->retry_count++;
entry->timer = 5001;
}
}
} else {
entry->timer -= ticks;
}
}
}
static fhss_ws_neighbor_timing_info_t *ws_bootstrap_get_neighbor_info(const fhss_api_t *api, uint8_t eui64[8])
{
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_fhss_api(api);
if (!cur || !cur->mac_parameters || !mac_neighbor_info(cur)) {
return NULL;
}
mac_neighbor_table_entry_t *mac_neighbor = mac_neighbor_table_address_discover(mac_neighbor_info(cur), eui64, MAC_ADDR_MODE_64_BIT);
if (mac_neighbor) {
ws_neighbor_class_entry_t *ws_neighbor = ws_neighbor_class_entry_get(&cur->ws_info->neighbor_storage, mac_neighbor->index);
if (!ws_neighbor) {
return NULL;
}
return &ws_neighbor->fhss_data;
}
//Discover temporary entry
ws_neighbor_temp_class_t *temp_entry = ws_llc_get_eapol_temp_entry(cur, eui64);
if (!temp_entry) {
return NULL;
}
return &temp_entry->neigh_info_list.fhss_data;
}
void ws_bootstrap_llc_hopping_update(struct protocol_interface_info_entry *cur, const fhss_ws_configuration_t *fhss_configuration)
{
cur->ws_info->hopping_schdule.uc_fixed_channel = fhss_configuration->unicast_fixed_channel;
cur->ws_info->hopping_schdule.bc_fixed_channel = fhss_configuration->broadcast_fixed_channel;
// Read UC channel function from WS info because FHSS might be temporarily configured to fixed channel during discovery.
cur->ws_info->hopping_schdule.uc_channel_function = cur->ws_info->cfg->fhss.fhss_uc_channel_function;
cur->ws_info->hopping_schdule.bc_channel_function = fhss_configuration->ws_bc_channel_function;
cur->ws_info->hopping_schdule.fhss_bc_dwell_interval = fhss_configuration->fhss_bc_dwell_interval;
cur->ws_info->hopping_schdule.fhss_broadcast_interval = fhss_configuration->fhss_broadcast_interval;
cur->ws_info->hopping_schdule.fhss_uc_dwell_interval = fhss_configuration->fhss_uc_dwell_interval;
cur->ws_info->hopping_schdule.fhss_bsi = fhss_configuration->bsi;
}
static uint8_t ws_bootstrap_generate_exluded_channel_list_from_active_channels(ws_excluded_channel_data_t *excluded_data, const uint32_t *selected_channel_mask, const uint32_t *global_channel_mask, uint16_t number_of_channels)
{
bool active_range = false;
//Clear Old Data
memset(excluded_data, 0, sizeof(ws_excluded_channel_data_t));
for (uint8_t i = 0; i < number_of_channels; i++) {
if (!(global_channel_mask[i / 32] & (1U << (i % 32)))) {
//Global exluded channel
if (active_range) {
//Mark range stop here
active_range = false;
}
continue;
}
if (selected_channel_mask[i / 32] & (1U << (i % 32))) {
if (active_range) {
//Mark range stop here
active_range = false;
}
} else {
//Mark excluded channel
//Swap Order already here
excluded_data->channel_mask[i / 32] |= 1U << (31 - (i % 32));
excluded_data->excluded_channel_count++;
if (excluded_data->excluded_range_length < WS_EXCLUDED_MAX_RANGE_TO_SEND) {
if (!active_range) {
excluded_data->excluded_range_length++;
active_range = true;
//Set start channel
excluded_data->exluded_range[excluded_data->excluded_range_length - 1].range_start = i;
} else {
excluded_data->exluded_range[excluded_data->excluded_range_length - 1].range_end = i;
}
}
}
}
excluded_data->channel_mask_bytes_inline = ((number_of_channels + 7) / 8);
uint8_t channel_plan = 0;
if (excluded_data->excluded_range_length == 0) {
excluded_data->excuded_channel_ctrl = WS_EXC_CHAN_CTRL_NONE;
} else if (excluded_data->excluded_range_length <= WS_EXCLUDED_MAX_RANGE_TO_SEND) {
uint8_t range_length = (excluded_data->excluded_range_length * 4) + 3;
if (range_length <= ((number_of_channels + 7) / 8) + 6) {
excluded_data->excuded_channel_ctrl = WS_EXC_CHAN_CTRL_RANGE;
} else {
excluded_data->excuded_channel_ctrl = WS_EXC_CHAN_CTRL_BITMASK;
channel_plan = 1;
}
} else {
excluded_data->excuded_channel_ctrl = WS_EXC_CHAN_CTRL_BITMASK;
channel_plan = 1;
}
tr_debug("Excluded ctrl %u, exluded channel count %u, total domain channels %u", excluded_data->excuded_channel_ctrl, excluded_data->excluded_channel_count, number_of_channels);
return channel_plan;
}
void ws_bootstrap_fhss_configure_channel_masks(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration)
{
fhss_configuration->channel_mask_size = cur->ws_info->hopping_schdule.number_of_channels;
ws_common_generate_channel_list(fhss_configuration->channel_mask, cur->ws_info->hopping_schdule.number_of_channels, cur->ws_info->hopping_schdule.regulatory_domain, cur->ws_info->hopping_schdule.operating_class, cur->ws_info->hopping_schdule.channel_plan_id);
ws_common_generate_channel_list(fhss_configuration->unicast_channel_mask, cur->ws_info->hopping_schdule.number_of_channels, cur->ws_info->hopping_schdule.regulatory_domain, cur->ws_info->hopping_schdule.operating_class, cur->ws_info->hopping_schdule.channel_plan_id);
// using bitwise AND operation for user set channel mask to remove channels not allowed in this device
for (uint8_t n = 0; n < 8; n++) {
fhss_configuration->unicast_channel_mask[n] &= cur->ws_info->cfg->fhss.fhss_channel_mask[n];
}
//Update Exluded channels
cur->ws_info->hopping_schdule.channel_plan = ws_bootstrap_generate_exluded_channel_list_from_active_channels(&cur->ws_info->hopping_schdule.excluded_channels, fhss_configuration->unicast_channel_mask, fhss_configuration->channel_mask, cur->ws_info->hopping_schdule.number_of_channels);
}
int8_t ws_bootstrap_fhss_initialize(protocol_interface_info_entry_t *cur)
{
fhss_api_t *fhss_api = ns_sw_mac_get_fhss_api(cur->mac_api);
fhss_ws_configuration_t fhss_configuration;
memset(&fhss_configuration, 0, sizeof(fhss_ws_configuration_t));
if (!fhss_api) {
// When FHSS doesn't exist yet, create one
ws_bootstrap_fhss_configure_channel_masks(cur, &fhss_configuration);
ws_bootstrap_fhss_set_defaults(cur, &fhss_configuration);
fhss_api = ns_fhss_ws_create(&fhss_configuration, cur->ws_info->fhss_timer_ptr);
if (!fhss_api) {
return -1;
}
ns_sw_mac_fhss_register(cur->mac_api, fhss_api);
// Allow transmitting unicast frames only on TX slots in normal and expedited forwarding mode
ns_fhss_ws_set_tx_allowance_level(fhss_api, WS_TX_SLOT, WS_TX_SLOT);
} else {
return -1;
}
return 0;
}
int8_t ws_bootstrap_fhss_set_defaults(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration)
{
fhss_configuration->fhss_uc_dwell_interval = cur->ws_info->cfg->fhss.fhss_uc_dwell_interval;
fhss_configuration->ws_uc_channel_function = (fhss_ws_channel_functions)cur->ws_info->cfg->fhss.fhss_uc_channel_function;
fhss_configuration->ws_bc_channel_function = (fhss_ws_channel_functions)cur->ws_info->cfg->fhss.fhss_bc_channel_function;
fhss_configuration->fhss_bc_dwell_interval = cur->ws_info->cfg->fhss.fhss_bc_dwell_interval;
fhss_configuration->fhss_broadcast_interval = cur->ws_info->cfg->fhss.fhss_bc_interval;
if (cur->ws_info->cfg->fhss.fhss_uc_fixed_channel != 0xffff) {
fhss_configuration->unicast_fixed_channel = cur->ws_info->cfg->fhss.fhss_uc_fixed_channel;
}
fhss_configuration->broadcast_fixed_channel = cur->ws_info->cfg->fhss.fhss_bc_fixed_channel;
return 0;
}
static bool ws_bootstrap_channel_allowed(uint8_t channel, uint32_t *channel_mask)
{
if ((1U << (channel % 32)) & (channel_mask[channel / 32])) {
return true;
}
return false;
}
uint16_t ws_bootstrap_randomize_fixed_channel(uint16_t configured_fixed_channel, uint8_t number_of_channels, uint32_t *channel_mask)
{
if (configured_fixed_channel == 0xFFFF) {
uint16_t random_channel = randLIB_get_random_in_range(0, number_of_channels - 1);
while (ws_bootstrap_channel_allowed(random_channel, channel_mask) == false) {
random_channel = randLIB_get_random_in_range(0, number_of_channels - 1);
}
return random_channel;
} else {
return configured_fixed_channel;
}
}
static int8_t ws_bootstrap_fhss_enable(protocol_interface_info_entry_t *cur)
{
fhss_ws_configuration_t fhss_configuration = ws_common_get_current_fhss_configuration(cur);
// Set the LLC information to follow the actual fhss settings
ws_bootstrap_llc_hopping_update(cur, &fhss_configuration);
// Set neighbor info callback
if (ns_fhss_set_neighbor_info_fp(cur->ws_info->fhss_api, &ws_bootstrap_get_neighbor_info)) {
return -1;
}
return 0;
}
/* Sets the parent and broadcast schedule we are following
*
*/
void ws_bootstrap_primary_parent_set(struct protocol_interface_info_entry *cur, llc_neighbour_req_t *neighbor_info, ws_parent_synch_e synch_req)
{
if (!neighbor_info->ws_neighbor->broadcast_timing_info_stored) {
tr_error("No BC timing info for set new parent");
return;
}
fhss_ws_configuration_t fhss_configuration = ws_common_get_current_fhss_configuration(cur);
// Learning broadcast network configuration
if (neighbor_info->ws_neighbor->broadcast_shedule_info_stored) {
if (synch_req != WS_EAPOL_PARENT_SYNCH) {
ws_bootstrap_fhss_set_defaults(cur, &fhss_configuration);
}
fhss_configuration.ws_bc_channel_function = (fhss_ws_channel_functions)neighbor_info->ws_neighbor->fhss_data.bc_timing_info.broadcast_channel_function;
if (fhss_configuration.ws_bc_channel_function == WS_FIXED_CHANNEL) {
cur->ws_info->hopping_schdule.bc_fixed_channel = neighbor_info->ws_neighbor->fhss_data.bc_timing_info.fixed_channel;
cur->ws_info->cfg->fhss.fhss_bc_fixed_channel = neighbor_info->ws_neighbor->fhss_data.bc_timing_info.fixed_channel;
}
fhss_configuration.bsi = neighbor_info->ws_neighbor->fhss_data.bc_timing_info.broadcast_schedule_id;
fhss_configuration.fhss_bc_dwell_interval = neighbor_info->ws_neighbor->fhss_data.bc_timing_info.broadcast_dwell_interval;
fhss_configuration.fhss_broadcast_interval = neighbor_info->ws_neighbor->fhss_data.bc_timing_info.broadcast_interval;
fhss_configuration.broadcast_fixed_channel = cur->ws_info->cfg->fhss.fhss_bc_fixed_channel;
neighbor_info->ws_neighbor->synch_done = true;
}
ns_fhss_ws_configuration_set(cur->ws_info->fhss_api, &fhss_configuration);
// We have broadcast schedule set up set the broadcast parent schedule
ns_fhss_ws_set_parent(cur->ws_info->fhss_api, neighbor_info->neighbor->mac64, &neighbor_info->ws_neighbor->fhss_data.bc_timing_info, synch_req != WS_PARENT_SOFT_SYNCH);
// Update LLC to follow updated fhss settings
ws_bootstrap_llc_hopping_update(cur, &fhss_configuration);
}
void ws_bootstrap_eapol_parent_synch(struct protocol_interface_info_entry *cur, llc_neighbour_req_t *neighbor_info)
{
if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER || cur->ws_info->configuration_learned || !neighbor_info->ws_neighbor->broadcast_shedule_info_stored || !neighbor_info->ws_neighbor->broadcast_timing_info_stored) {
return;
}
if (ws_bootstrap_candidate_parent_get(cur, neighbor_info->neighbor->mac64, false) == NULL) {
return;
}
//Store Brodacst Shedule
if (!neighbor_info->ws_neighbor->synch_done) {
ws_bootstrap_primary_parent_set(cur, neighbor_info, WS_EAPOL_PARENT_SYNCH);
} else {
ns_fhss_ws_set_parent(cur->ws_info->fhss_api, neighbor_info->neighbor->mac64, &neighbor_info->ws_neighbor->fhss_data.bc_timing_info, false);
}
}
void ws_bootstrap_ll_address_validate(struct protocol_interface_info_entry *cur)
{
// Configure EUI64 for MAC if missing
uint8_t mac64[8];
if (!cur->mac_api) {
return;
}
cur->mac_api->mac64_get(cur->mac_api, MAC_EXTENDED_DYNAMIC, mac64);
if (memcmp(mac64, ADDR_UNSPECIFIED, 8) == 0) {
cur->mac_api->mac64_get(cur->mac_api, MAC_EXTENDED_READ_ONLY, mac64);
}
if (memcmp(mac64, ADDR_UNSPECIFIED, 8) == 0) {
// Generate random mac because it was not available
randLIB_get_n_bytes_random(mac64, 8);
mac64[0] |= 2; //Set Local Bit
mac64[0] &= ~1; //Clear multicast bit
tr_info("Generated random MAC address");
}
tr_info("MAC address: %s", trace_array(mac64, 8));
mac_helper_mac64_set(cur, mac64);
memcpy(cur->iid_eui64, mac64, 8);
/* Invert U/L Bit */
cur->iid_eui64[0] ^= 2;
memcpy(cur->iid_slaac, cur->iid_eui64, 8);
}
/* \return 0x0100 to 0xFFFF ETX value (8 bit fraction)
* \return 0xFFFF address not associated
* \return 0x0000 address unknown or other error
* \return 0x0001 no ETX statistics on this interface
*/
uint16_t ws_local_etx_read(protocol_interface_info_entry_t *interface, addrtype_t addr_type, const uint8_t *mac_adddress)
{
uint16_t etx;
if (!mac_adddress || !interface) {
return 0;
}
uint8_t attribute_index;
mac_neighbor_table_entry_t *mac_neighbor = mac_neighbor_table_address_discover(mac_neighbor_info(interface), mac_adddress, addr_type);
if (!mac_neighbor) {
return 0xffff;
}
attribute_index = mac_neighbor->index;
ws_neighbor_class_entry_t *ws_neighbour = ws_neighbor_class_entry_get(&interface->ws_info->neighbor_storage, attribute_index);
etx_storage_t *etx_entry = etx_storage_entry_get(interface->id, attribute_index);
if (!ws_neighbour || !etx_entry) {
return 0xffff;
}
etx = etx_local_etx_read(interface->id, attribute_index);
// if we have a measurement ready then we will check the RSL validity
if (etx != 0xffff && !ws_neighbour->candidate_parent) {
// RSL value measured is lower than acceptable ETX will be given as MAX
return WS_ETX_MAX << 1; // We use 8 bit fraction and ETX is usually 7 bit fraction
}
// If we dont have valid ETX for children we assume good ETX.
// After enough packets is sent to children real calculated ETX is given.
// This might result in ICMP source route errors returned to Border router causing secondary route uses
if (etx == 0xffff && ipv6_neighbour_has_registered_by_eui64(&interface->ipv6_neighbour_cache, mac_neighbor->mac64)) {
return 0x100;
}
return etx;
}
uint16_t ws_etx_read(protocol_interface_info_entry_t *interface, addrtype_t addr_type, const uint8_t *addr_ptr)
{
if (!addr_ptr || !interface) {
return 0;
}
return ws_local_etx_read(interface, addr_type, addr_ptr + PAN_ID_LEN);
}
bool ws_bootstrap_nd_ns_transmit(protocol_interface_info_entry_t *cur, ipv6_neighbour_t *entry, bool unicast, uint8_t seq)
{
(void)cur;
(void)seq;
if (unicast) {
// Unicast NS is OK
return false;
}
// Fail the resolution
tr_warn("Link address lost for %s", trace_ipv6(entry->ip_address));
ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, entry);
// True means we skip the message sending
return true;
}
void ws_bootstrap_memory_configuration()
{
/* Configure memory limits for garbage collection based on total memory size
* Starting from these values
* 5% for High mark
* 2% for critical mark
* 1% for Routing limit
* Memory High Critical Drop routing
* 32K RAM 3200 bytes 1280 Bytes 1024 bytes
* 64K RAM 3200 bytes 1280 Bytes 1024 bytes
* 128K RAM 6400 bytes 2560 Bytes 1280 bytes
* 320K RAM 16000 byte 6400 Bytes 3200 bytes
* 640K RAM 32000 byte 12800 Bytes 6400 bytes
* 1000K RAM 50000 bytes 20000 Bytes 10000 bytes
* 4000K RAM 120000 bytes 40000 Bytes 10000 bytes
* */
// In small memory devices there needs to lower limit so that there some change to be usable
// and there is no use for having very large values on high memory devices
ns_monitor_packet_ingress_rate_limit_by_memory(1024, 10000, 1);
ns_monitor_heap_gc_threshold_set(3200, 120000, 95, 1280, 40000, 98);
return;
}
void ws_bootstrap_configuration_reset(protocol_interface_info_entry_t *cur)
{
// Configure IP stack to operate as Wi-SUN node
// Do not process beacons
cur->mac_parameters->beacon_ind = NULL;
cur->mac_parameters->mac_security_level = 0;
// Set default parameters to interface
cur->configure_flags = INTERFACE_BOOTSTRAP_DEFINED;
cur->configure_flags |= INTERFACE_SECURITY_DEFINED;
cur->lowpan_info = 0;
switch (cur->bootsrap_mode) {
// case NET_6LOWPAN_SLEEPY_HOST:
case ARM_NWK_BOOTSRAP_MODE_6LoWPAN_HOST:
break;
case ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER:
case ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER:
cur->lowpan_info |= INTERFACE_NWK_ROUTER_DEVICE;
break;
default:
tr_err("Invalid bootstrap_mode");
}
cur->nwk_bootstrap_state = ER_ACTIVE_SCAN;
cur->ws_info->network_pan_id = 0xffff;
ws_bootstrap_asynch_trickle_stop(cur);
//cur->mac_security_key_usage_update_cb = ws_management_mac_security_key_update_cb;
return;
}
bool ws_bootstrap_network_name_matches(const struct mcps_data_ie_list *ie_ext, const char *network_name_ptr)
{
ws_wp_network_name_t network_name;
if (!network_name_ptr || !ie_ext) {
return false;
}
if (!ws_wp_nested_network_name_read(ie_ext->payloadIeList, ie_ext->payloadIeListLength, &network_name)) {
tr_warn("No network name IE");
return false;
}
if (network_name.network_name_length != strlen(network_name_ptr)) {
return false;
}
if (strncmp(network_name_ptr, (char *)network_name.network_name, network_name.network_name_length) != 0) {
return false;
}
// names have equal length and same characters
return true;
}
static void ws_bootstrap_decode_exclude_range_to_mask_by_range(void *mask_buffer, ws_excluded_channel_range_t *range_info, uint16_t number_of_channels)
{
uint16_t range_start, range_stop;
uint8_t mask_index = 0;
//uint8_t channel_index = 0;
uint8_t *range_ptr = range_info->range_start;
uint32_t *mask_ptr = mask_buffer;
while (range_info->number_of_range) {
range_start = common_read_16_bit_inverse(range_ptr);
range_ptr += 2;
range_stop = common_read_16_bit_inverse(range_ptr);
range_ptr += 2;
range_info->number_of_range--;
for (uint16_t channel = 0; channel < number_of_channels; channel++) {
if (channel && (channel % 32 == 0)) {
mask_index++;
//channel_index = 0;
}
if (channel >= range_start && channel <= range_stop) {
//mask_ptr[mask_index] |= 1U << (31 - channel_index);
mask_ptr[channel / 32] |= 1U << (31 - (channel % 32));
} else if (channel > range_stop) {
break;
}
}
}
}
void ws_bootstrap_candidate_parent_store(parent_info_t *parent, const struct mcps_data_ind_s *data, ws_utt_ie_t *ws_utt, ws_us_ie_t *ws_us, ws_pan_information_t *pan_information)
{
parent->ws_utt = *ws_utt;
// Saved from unicast IE
parent->ws_us = *ws_us;
//copy excluded channel here if it is inline