-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
net.c
1611 lines (1309 loc) · 45.6 KB
/
net.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
/* Bluetooth Mesh */
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "crypto.h"
#include "adv.h"
#include "scan.h"
#include "mesh.h"
#include "lpn.h"
#include "friend.h"
#include "transport.h"
#include "access.h"
#include "foundation.h"
#include "beacon.h"
#include "settings.h"
#include "prov.h"
#include "proxy_client.h"
#include "proxy_server.h"
#include "provisioner_main.h"
/* Minimum valid Mesh Network PDU length. The Network headers
* themselves take up 9 bytes. After that there is a minumum of 1 byte
* payload for both CTL=1 and CTL=0 PDUs (smallest OpCode is 1 byte). CTL=1
* PDUs must use a 64-bit (8 byte) NetMIC, whereas CTL=0 PDUs have at least
* a 32-bit (4 byte) NetMIC and AppMIC giving again a total of 8 bytes.
*/
#define BLE_MESH_NET_MIN_PDU_LEN (BLE_MESH_NET_HDR_LEN + 1 + 8)
/* Seq limit after IV Update is triggered */
#define IV_UPDATE_SEQ_LIMIT 8000000
#define IVI(pdu) ((pdu)[0] >> 7)
#define NID(pdu) ((pdu)[0] & 0x7f)
#define CTL(pdu) ((pdu)[1] >> 7)
#define TTL(pdu) ((pdu)[1] & 0x7f)
#define SEQ(pdu) (sys_get_be24(&(pdu)[2]))
#define SRC(pdu) (sys_get_be16(&(pdu)[5]))
#define DST(pdu) (sys_get_be16(&(pdu)[7]))
/* Determine how many friendship credentials we need */
#if defined(CONFIG_BLE_MESH_FRIEND)
#define FRIEND_CRED_COUNT CONFIG_BLE_MESH_FRIEND_LPN_COUNT
#elif defined(CONFIG_BLE_MESH_LOW_POWER)
#define FRIEND_CRED_COUNT CONFIG_BLE_MESH_SUBNET_COUNT
#else
#define FRIEND_CRED_COUNT 0
#endif
#if FRIEND_CRED_COUNT > 0
static struct friend_cred friend_cred[FRIEND_CRED_COUNT];
#endif
static struct {
uint32_t src:15, /* MSB of source address is always 0 */
seq:17;
} msg_cache[CONFIG_BLE_MESH_MSG_CACHE_SIZE];
static uint16_t msg_cache_next;
/* Singleton network context (the implementation only supports one) */
struct bt_mesh_net bt_mesh = {
.local_queue = SYS_SLIST_STATIC_INIT(&bt_mesh.local_queue),
.sub = {
[0 ... (CONFIG_BLE_MESH_SUBNET_COUNT - 1)] = {
.net_idx = BLE_MESH_KEY_UNUSED,
}
},
.app_keys = {
[0 ... (CONFIG_BLE_MESH_APP_KEY_COUNT - 1)] = {
.net_idx = BLE_MESH_KEY_UNUSED,
}
},
};
static uint32_t dup_cache[4];
static int dup_cache_next;
#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF)
#define BLE_MESH_MAX_STORED_RELAY_COUNT (CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT / 2)
#endif
static bool check_dup(struct net_buf_simple *data)
{
const uint8_t *tail = net_buf_simple_tail(data);
uint32_t val = 0U;
int i;
val = sys_get_be32(tail - 4) ^ sys_get_be32(tail - 8);
for (i = 0; i < ARRAY_SIZE(dup_cache); i++) {
if (dup_cache[i] == val) {
return true;
}
}
dup_cache[dup_cache_next++] = val;
dup_cache_next %= ARRAY_SIZE(dup_cache);
return false;
}
static bool msg_cache_match(struct bt_mesh_net_rx *rx,
struct net_buf_simple *pdu)
{
int i;
for (i = 0; i < ARRAY_SIZE(msg_cache); i++) {
if (msg_cache[i].src == SRC(pdu->data) &&
msg_cache[i].seq == (SEQ(pdu->data) & BIT_MASK(17))) {
return true;
}
}
return false;
}
static void msg_cache_add(struct bt_mesh_net_rx *rx)
{
rx->msg_cache_idx = msg_cache_next++;
msg_cache[rx->msg_cache_idx].src = rx->ctx.addr;
msg_cache[rx->msg_cache_idx].seq = rx->seq;
msg_cache_next %= ARRAY_SIZE(msg_cache);
}
#if CONFIG_BLE_MESH_PROVISIONER
void bt_mesh_msg_cache_clear(uint16_t unicast_addr, uint8_t elem_num)
{
int i;
for (i = 0; i < ARRAY_SIZE(msg_cache); i++) {
if (msg_cache[i].src >= unicast_addr &&
msg_cache[i].src < unicast_addr + elem_num) {
memset(&msg_cache[i], 0, sizeof(msg_cache[i]));
}
}
}
#endif /* CONFIG_BLE_MESH_PROVISIONER */
struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx)
{
int i;
if (net_idx == BLE_MESH_KEY_ANY) {
return &bt_mesh.sub[0];
}
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
if (bt_mesh.sub[i].net_idx == net_idx) {
return &bt_mesh.sub[i];
}
}
return NULL;
}
int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys,
const uint8_t key[16])
{
uint8_t p[] = { 0 };
uint8_t nid = 0U;
int err = 0;
err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->enc, keys->privacy);
if (err) {
BT_ERR("Unable to generate NID, EncKey & PrivacyKey");
return err;
}
memcpy(keys->net, key, 16);
keys->nid = nid;
BT_DBG("NID 0x%02x EncKey %s", keys->nid, bt_hex(keys->enc, 16));
BT_DBG("PrivacyKey %s", bt_hex(keys->privacy, 16));
err = bt_mesh_k3(key, keys->net_id);
if (err) {
BT_ERR("Unable to generate Net ID");
return err;
}
BT_DBG("NetID %s", bt_hex(keys->net_id, 8));
#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER)
err = bt_mesh_identity_key(key, keys->identity);
if (err) {
BT_ERR("Unable to generate IdentityKey");
return err;
}
BT_DBG("IdentityKey %s", bt_hex(keys->identity, 16));
#endif /* GATT_PROXY */
err = bt_mesh_beacon_key(key, keys->beacon);
if (err) {
BT_ERR("Unable to generate beacon key");
return err;
}
BT_DBG("BeaconKey %s", bt_hex(keys->beacon, 16));
return 0;
}
#if (defined(CONFIG_BLE_MESH_LOW_POWER) || \
defined(CONFIG_BLE_MESH_FRIEND))
int friend_cred_set(struct friend_cred *cred, uint8_t idx, const uint8_t net_key[16])
{
uint16_t lpn_addr = 0U, frnd_addr = 0U;
uint8_t p[9] = {0};
int err = 0;
#if defined(CONFIG_BLE_MESH_LOW_POWER)
if (cred->addr == bt_mesh.lpn.frnd) {
lpn_addr = bt_mesh_primary_addr();
frnd_addr = cred->addr;
} else {
lpn_addr = cred->addr;
frnd_addr = bt_mesh_primary_addr();
}
#else
lpn_addr = cred->addr;
frnd_addr = bt_mesh_primary_addr();
#endif
BT_DBG("LPNAddress 0x%04x FriendAddress 0x%04x", lpn_addr, frnd_addr);
BT_DBG("LPNCounter 0x%04x FriendCounter 0x%04x", cred->lpn_counter,
cred->frnd_counter);
p[0] = 0x01;
sys_put_be16(lpn_addr, p + 1);
sys_put_be16(frnd_addr, p + 3);
sys_put_be16(cred->lpn_counter, p + 5);
sys_put_be16(cred->frnd_counter, p + 7);
err = bt_mesh_k2(net_key, p, sizeof(p), &cred->cred[idx].nid,
cred->cred[idx].enc, cred->cred[idx].privacy);
if (err) {
BT_ERR("Unable to generate NID, EncKey & PrivacyKey");
return err;
}
BT_DBG("Friend NID 0x%02x EncKey %s", cred->cred[idx].nid,
bt_hex(cred->cred[idx].enc, 16));
BT_DBG("Friend PrivacyKey %s", bt_hex(cred->cred[idx].privacy, 16));
return 0;
}
void friend_cred_refresh(uint16_t net_idx)
{
int i;
for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
struct friend_cred *cred = &friend_cred[i];
if (cred->addr != BLE_MESH_ADDR_UNASSIGNED &&
cred->net_idx == net_idx) {
memcpy(&cred->cred[0], &cred->cred[1],
sizeof(cred->cred[0]));
}
}
}
int friend_cred_update(struct bt_mesh_subnet *sub)
{
int err = 0, i;
BT_DBG("net_idx 0x%04x", sub->net_idx);
for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
struct friend_cred *cred = &friend_cred[i];
if (cred->addr == BLE_MESH_ADDR_UNASSIGNED ||
cred->net_idx != sub->net_idx) {
continue;
}
err = friend_cred_set(cred, 1, sub->keys[1].net);
if (err) {
return err;
}
}
return 0;
}
struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, uint16_t addr,
uint16_t lpn_counter, uint16_t frnd_counter)
{
struct friend_cred *cred = NULL;
int i, err = 0;
BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr);
for (cred = NULL, i = 0; i < ARRAY_SIZE(friend_cred); i++) {
if ((friend_cred[i].addr == BLE_MESH_ADDR_UNASSIGNED) ||
(friend_cred[i].addr == addr &&
friend_cred[i].net_idx == sub->net_idx)) {
cred = &friend_cred[i];
break;
}
}
if (!cred) {
BT_WARN("No free friend credential slots");
return NULL;
}
cred->net_idx = sub->net_idx;
cred->addr = addr;
cred->lpn_counter = lpn_counter;
cred->frnd_counter = frnd_counter;
err = friend_cred_set(cred, 0, sub->keys[0].net);
if (err) {
friend_cred_clear(cred);
return NULL;
}
if (sub->kr_flag) {
err = friend_cred_set(cred, 1, sub->keys[1].net);
if (err) {
friend_cred_clear(cred);
return NULL;
}
}
return cred;
}
void friend_cred_clear(struct friend_cred *cred)
{
cred->net_idx = BLE_MESH_KEY_UNUSED;
cred->addr = BLE_MESH_ADDR_UNASSIGNED;
cred->lpn_counter = 0U;
cred->frnd_counter = 0U;
(void)memset(cred->cred, 0, sizeof(cred->cred));
}
int friend_cred_del(uint16_t net_idx, uint16_t addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
struct friend_cred *cred = &friend_cred[i];
if (cred->addr == addr && cred->net_idx == net_idx) {
friend_cred_clear(cred);
return 0;
}
}
return -ENOENT;
}
int friend_cred_get(struct bt_mesh_subnet *sub, uint16_t addr, uint8_t *nid,
const uint8_t **enc, const uint8_t **priv)
{
int i;
BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr);
for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
struct friend_cred *cred = &friend_cred[i];
if (cred->net_idx != sub->net_idx) {
continue;
}
if (addr != BLE_MESH_ADDR_UNASSIGNED && cred->addr != addr) {
continue;
}
if (nid) {
*nid = cred->cred[sub->kr_flag].nid;
}
if (enc) {
*enc = cred->cred[sub->kr_flag].enc;
}
if (priv) {
*priv = cred->cred[sub->kr_flag].privacy;
}
return 0;
}
return -ENOENT;
}
#else
int friend_cred_get(struct bt_mesh_subnet *sub, uint16_t addr, uint8_t *nid,
const uint8_t **enc, const uint8_t **priv)
{
return -ENOENT;
}
#endif /* FRIEND || LOW_POWER */
uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub)
{
uint8_t flags = 0x00;
if (sub && sub->kr_flag) {
flags |= BLE_MESH_NET_FLAG_KR;
}
if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
flags |= BLE_MESH_NET_FLAG_IVU;
}
return flags;
}
int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub)
{
uint8_t flags = bt_mesh_net_flags(sub);
struct bt_mesh_subnet_keys *keys = NULL;
if (sub->kr_flag) {
BT_DBG("NetIndex %u Using new key", sub->net_idx);
keys = &sub->keys[1];
} else {
BT_DBG("NetIndex %u Using current key", sub->net_idx);
keys = &sub->keys[0];
}
BT_DBG("flags 0x%02x, IVI 0x%08x", flags, bt_mesh.iv_index);
return bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id,
bt_mesh.iv_index, sub->auth);
}
int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16],
uint32_t iv_index)
{
struct bt_mesh_subnet *sub = NULL;
int err = 0;
BT_DBG("idx %u flags 0x%02x iv_index %u", idx, flags, iv_index);
BT_DBG("NetKey %s", bt_hex(key, 16));
(void)memset(msg_cache, 0, sizeof(msg_cache));
msg_cache_next = 0U;
sub = &bt_mesh.sub[0];
sub->kr_flag = BLE_MESH_KEY_REFRESH(flags);
if (sub->kr_flag) {
err = bt_mesh_net_keys_create(&sub->keys[1], key);
if (err) {
return -EIO;
}
sub->kr_phase = BLE_MESH_KR_PHASE_2;
} else {
err = bt_mesh_net_keys_create(&sub->keys[0], key);
if (err) {
return -EIO;
}
}
sub->net_idx = idx;
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED;
} else {
sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED;
}
bt_mesh.iv_index = iv_index;
bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS,
BLE_MESH_IV_UPDATE(flags));
/* Set minimum required hours, since the 96-hour minimum requirement
* doesn't apply straight after provisioning (since we can't know how
* long has actually passed since the network changed its state).
*/
bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS;
/* Make sure we have valid beacon data to be sent */
bt_mesh_net_beacon_update(sub);
return 0;
}
void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub)
{
int i;
BT_DBG("idx 0x%04x", sub->net_idx);
memcpy(&sub->keys[0], &sub->keys[1], sizeof(sub->keys[0]));
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Store updated NetKey persistently");
bt_mesh_store_subnet(sub);
}
for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
struct bt_mesh_app_key *key = &bt_mesh.app_keys[i];
if (key->net_idx != sub->net_idx || !key->updated) {
continue;
}
memcpy(&key->keys[0], &key->keys[1], sizeof(key->keys[0]));
key->updated = false;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Store updated AppKey persistently");
bt_mesh_store_app_key(key);
}
}
}
bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key)
{
if (new_kr != sub->kr_flag && sub->kr_phase == BLE_MESH_KR_NORMAL) {
BT_WARN("KR change in normal operation. Are we blacklisted?");
return false;
}
sub->kr_flag = new_kr;
if (sub->kr_flag) {
if (sub->kr_phase == BLE_MESH_KR_PHASE_1) {
BT_INFO("Phase 1 -> Phase 2");
sub->kr_phase = BLE_MESH_KR_PHASE_2;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Storing kr phase persistently");
bt_mesh_store_subnet(sub);
}
return true;
}
} else {
switch (sub->kr_phase) {
case BLE_MESH_KR_PHASE_1:
if (!new_key) {
/* Ignore */
break;
}
/* Upon receiving a Secure Network beacon with the KR flag set
* to 0 using the new NetKey in Phase 1, the node shall
* immediately transition to Phase 3, which effectively skips
* Phase 2.
*
* Intentional fall-through.
*/
case BLE_MESH_KR_PHASE_2:
BT_INFO("KR Phase 0x%02x -> Normal", sub->kr_phase);
sub->kr_phase = BLE_MESH_KR_NORMAL;
bt_mesh_net_revoke_keys(sub);
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) ||
IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
friend_cred_refresh(sub->net_idx);
}
return true;
}
}
return false;
}
void bt_mesh_rpl_reset(void)
{
int i;
/* Discard "old old" IV Index entries from RPL and flag
* any other ones (which are valid) as old.
*/
for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
if (rpl->src) {
if (rpl->old_iv) {
(void)memset(rpl, 0, sizeof(*rpl));
} else {
rpl->old_iv = true;
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_rpl(rpl);
}
}
}
}
#if defined(CONFIG_BLE_MESH_IV_UPDATE_TEST)
void bt_mesh_iv_update_test(bool enable)
{
bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_TEST, enable);
/* Reset the duration variable - needed for some PTS tests */
bt_mesh.ivu_duration = 0U;
}
bool bt_mesh_iv_update(void)
{
if (!bt_mesh_is_provisioned()) {
BT_ERR("Not yet provisioned");
return false;
}
if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
bt_mesh_net_iv_update(bt_mesh.iv_index, false);
} else {
bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
}
bt_mesh_net_sec_update(NULL);
return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS);
}
#endif /* CONFIG_BLE_MESH_IV_UPDATE_TEST */
/* Used for sending immediate beacons to Friend queues and GATT clients */
void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub)
{
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_sec_update(sub ? sub->net_idx : BLE_MESH_KEY_ANY);
}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) {
bt_mesh_proxy_server_beacon_send(sub);
}
}
bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update)
{
int i;
if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
/* We're currently in IV Update mode */
if (iv_index != bt_mesh.iv_index) {
BT_WARN("IV Index mismatch: 0x%08x != 0x%08x",
iv_index, bt_mesh.iv_index);
return false;
}
if (iv_update) {
/* Nothing to do */
BT_DBG("Already in IV Update in Progress state");
return false;
}
} else {
/* We're currently in Normal mode */
if (iv_index == bt_mesh.iv_index) {
BT_DBG("Same IV Index in normal mode");
return false;
}
if (iv_index < bt_mesh.iv_index ||
iv_index > bt_mesh.iv_index + 42) {
BT_ERR("IV Index out of sync: 0x%08x != 0x%08x",
iv_index, bt_mesh.iv_index);
return false;
}
if (iv_index > bt_mesh.iv_index + 1) {
BT_WARN("Performing IV Index Recovery");
(void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
bt_mesh.iv_index = iv_index;
bt_mesh.seq = 0U;
goto do_update;
}
if (iv_index == bt_mesh.iv_index + 1 && !iv_update) {
BT_WARN("Ignoring new index in normal mode");
return false;
}
if (!iv_update) {
/* Nothing to do */
BT_DBG("Already in Normal state");
return false;
}
}
if (!(IS_ENABLED(CONFIG_BLE_MESH_IV_UPDATE_TEST) &&
bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_TEST))) {
if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
BT_WARN("IV Update before minimum duration");
return false;
}
}
/* Defer change to Normal Operation if there are pending acks */
if (!iv_update && bt_mesh_tx_in_progress()) {
BT_WARN("IV Update deferred because of pending transfer");
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING);
return false;
}
do_update:
bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv_update);
bt_mesh.ivu_duration = 0U;
if (iv_update) {
bt_mesh.iv_index = iv_index;
BT_INFO("IV Update state entered. New index 0x%08x",
bt_mesh.iv_index);
bt_mesh_rpl_reset();
} else {
BT_INFO("Normal mode entered");
bt_mesh.seq = 0U;
}
k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
size_t subnet_size = bt_mesh_rx_netkey_size();
for (i = 0; i < subnet_size; i++) {
struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
if (sub && sub->net_idx != BLE_MESH_KEY_UNUSED) {
bt_mesh_net_beacon_update(sub);
}
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_iv(false);
}
return true;
}
bool bt_mesh_primary_subnet_exist(void)
{
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) {
return true;
}
} else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
if (bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY)) {
return true;
}
}
return false;
}
uint32_t bt_mesh_next_seq(void)
{
uint32_t seq = bt_mesh.seq++;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_seq();
}
if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) &&
bt_mesh.seq > IV_UPDATE_SEQ_LIMIT &&
bt_mesh_primary_subnet_exist()) {
bt_mesh_beacon_ivu_initiator(true);
bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
bt_mesh_net_sec_update(NULL);
}
return seq;
}
int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
bool new_key, const struct bt_mesh_send_cb *cb,
void *cb_data)
{
const uint8_t *enc = NULL, *priv = NULL;
uint32_t seq = 0U;
uint16_t dst = 0U;
int err = 0;
BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key,
buf->len);
enc = sub->keys[new_key].enc;
priv = sub->keys[new_key].privacy;
err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
if (err) {
BT_ERR("De-obfuscate failed (err %d)", err);
return err;
}
err = bt_mesh_net_decrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false);
if (err) {
BT_ERR("Decrypt failed (err %d)", err);
return err;
}
/* Update with a new sequence number */
seq = bt_mesh_next_seq();
sys_put_be24(seq, &buf->data[2]);
/* Get destination, in case it's a proxy client */
dst = DST(buf->data);
err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false);
if (err) {
BT_ERR("Encrypt failed (err %d)", err);
return err;
}
err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
if (err) {
BT_ERR("Obfuscate failed (err %d)", err);
return err;
}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_proxy_server_relay(&buf->b, dst) &&
BLE_MESH_ADDR_IS_UNICAST(dst)) {
send_cb_finalize(cb, cb_data);
return 0;
}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) &&
bt_mesh_proxy_client_relay(&buf->b, dst)) {
send_cb_finalize(cb, cb_data);
return 0;
}
bt_mesh_adv_send(buf, cb, cb_data);
return 0;
}
static void bt_mesh_net_local(struct k_work *work)
{
struct net_buf *buf = NULL;
while ((buf = net_buf_slist_get(&bt_mesh.local_queue))) {
BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
bt_mesh_net_recv(&buf->b, 0, BLE_MESH_NET_IF_LOCAL);
net_buf_unref(buf);
}
}
int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
bool proxy)
{
const bool ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED);
const uint8_t *enc = NULL, *priv = NULL;
uint8_t nid = 0U;
int err = 0;
if (ctl && net_buf_simple_tailroom(buf) < BLE_MESH_MIC_LONG) {
BT_ERR("Insufficient MIC space for CTL PDU");
return -EINVAL;
} else if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) {
BT_ERR("Insufficient MIC space for PDU");
return -EINVAL;
}
BT_DBG("src 0x%04x dst 0x%04x ctl %u seq 0x%06x",
tx->src, tx->ctx->addr, ctl, bt_mesh.seq);
net_buf_simple_push_be16(buf, tx->ctx->addr);
net_buf_simple_push_be16(buf, tx->src);
net_buf_simple_push_be24(buf, bt_mesh_next_seq());
if (ctl) {
net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80);
} else {
net_buf_simple_push_u8(buf, tx->ctx->send_ttl);
}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && tx->friend_cred) {
if (friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED,
&nid, &enc, &priv)) {
BT_WARN("Falling back to master credentials");
tx->friend_cred = 0U;
nid = tx->sub->keys[tx->sub->kr_flag].nid;
enc = tx->sub->keys[tx->sub->kr_flag].enc;
priv = tx->sub->keys[tx->sub->kr_flag].privacy;
}
} else {
tx->friend_cred = 0U;
nid = tx->sub->keys[tx->sub->kr_flag].nid;
enc = tx->sub->keys[tx->sub->kr_flag].enc;
priv = tx->sub->keys[tx->sub->kr_flag].privacy;
}
net_buf_simple_push_u8(buf, (nid | (BLE_MESH_NET_IVI_TX & 1) << 7));
err = bt_mesh_net_encrypt(enc, buf, BLE_MESH_NET_IVI_TX, proxy);
if (err) {
return err;
}
return bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
}
int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf,
const struct bt_mesh_send_cb *cb, void *cb_data)
{
int err = 0;
BT_DBG("src 0x%04x dst 0x%04x len %u headroom %u tailroom %u",
tx->src, tx->ctx->addr, buf->len, net_buf_headroom(buf),
net_buf_tailroom(buf));
BT_DBG("Payload len %u: %s", buf->len, bt_hex(buf->data, buf->len));
BT_DBG("Seq 0x%06x", bt_mesh.seq);
if (tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) {
tx->ctx->send_ttl = bt_mesh_default_ttl_get();
}
err = bt_mesh_net_encode(tx, &buf->b, false);
if (err) {
goto done;
}
/* Deliver to GATT Proxy Clients if necessary. Mesh spec 3.4.5.2:
* "The output filter of the interface connected to advertising or
* GATT bearers shall drop all messages with TTL value set to 1."
*/
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
tx->ctx->send_ttl != 1U) {
if (bt_mesh_proxy_server_relay(&buf->b, tx->ctx->addr) &&
BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
/* Notify completion if this only went
* through the Mesh Proxy.
*/
send_cb_finalize(cb, cb_data);
err = 0;
goto done;
}
}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) &&
tx->ctx->send_ttl != 1U) {
if (bt_mesh_proxy_client_relay(&buf->b, tx->ctx->addr)) {
/* If Proxy Client succeeds to send messages with GATT bearer,
* we can directly finish here. And if not, which means no
* connection has been created with Proxy Client, here we will
* use advertising bearer for the messages.
*/
send_cb_finalize(cb, cb_data);
err = 0;
goto done;
}
}
/* Deliver to local network interface if necessary */
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() &&
(bt_mesh_fixed_group_match(tx->ctx->addr) ||
bt_mesh_elem_find(tx->ctx->addr))) {
if (cb && cb->start) {
cb->start(0, 0, cb_data);
}
net_buf_slist_put(&bt_mesh.local_queue, net_buf_ref(buf));
if (cb && cb->end) {
cb->end(0, cb_data);
}
k_work_submit(&bt_mesh.local_work);
} else if (tx->ctx->send_ttl != 1U) {
/* Deliver to the advertising network interface. Mesh spec
* 3.4.5.2: "The output filter of the interface connected to
* advertising or GATT bearers shall drop all messages with
* TTL value set to 1."
*/
bt_mesh_adv_send(buf, cb, cb_data);
}
done:
net_buf_unref(buf);
return err;
}
static bool auth_match(struct bt_mesh_subnet_keys *keys,
const uint8_t net_id[8], uint8_t flags,
uint32_t iv_index, const uint8_t auth[8])
{
uint8_t net_auth[8] = {0};
if (memcmp(net_id, keys->net_id, 8)) {
return false;
}
bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id, iv_index,
net_auth);