1
2
3
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
66
67
68
69
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
103
104
105
106
107
108
109
110
111
112
113
114
115
121
122
123
124
125
132
133
134
146
147
182
183
184
185
186
187
188
189
190
191
195
196
197
198
199
200
201
202
203
204
208
209
210
211
212
219
220
221
222
226
227
228
229
230
237
238
239
240
249
250
251
252
253
254
255
256
257
258
259
260
261
262
270
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
290
291
292
293
294
295
296
297
298
312
313
314
315
316
317
318
319
320
321
322
323
326
327
328
331
332
333
334
335
336
337
338
339
340
341
342
343
344
352
353
357
358
359
360
361
362
363
364
368
369
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
418
419
422
423
426
427
430
431
434
435
436
437
438
439
440
441
447
448
449
450
451
452
455
456
459
460
461
462
463
464
465
466
467
468
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
511
512
513
514
522
523
524
525
526
527
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
555
556
557
558
559
562
563
564
565
566
570
571
572
573
574
575
579
580
581
582
583
584
585
586
587
591
592
593
594
595
596
609
610
611
612
616
617
618
619
620
621
622
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
647
648
653
654
655
656
657
658
659
660
661
662
663
666
667
668
669
670
671
672
675
676
677
678
679
680
681
686
687
692
693
709
710
711
715
716
721
722
723
724
725
732
733
740
741
746
747
752
753
754
755
761
762
763
768
769
770
771
772
773
783
784
785
786
787
788
794
795
798
799
800
801
802
810
811
812
813
814
815
818
819
820
821
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
862
866
867
868
869
870
871
872
873
874
875
880
884
885
886
890
891
892
896
897
898
899
900
901
902
903
904
905
909
910
911
915
916
917
918
922
923
924
928
929
930
931
932
933
943
944
945
946
947
948
949
950
951
952
956
957
961
962
963
964
965
966
967
968
969
970
975
976
977
978
979
980
981
982
983
986
990
991
992
993
994
995
996
997
998
999
1000
1001
1007
1013
1014
1015
1016
1017
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1040
1041
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1066
1067
1068
1069
1070
1071
1072
1073
1074
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1101
1102
1103
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1138
1139
1140
1141
1142
1143
1144
1145
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1167
1168
1171
1172
1173
1174
1177
1178
1179
1180
1181
1182
1183
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1233
1234
1235
1236
1237
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1261
1262
1266
1267
1270
1271
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1296
1297
1298
1302
1303
1307
1308
1309
1310
1311
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1334
1335
1336
1337
1340
1341
1344
1345
1346
1347
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1368
1369
1372
1373
1374
1375
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1399
1400
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1432
1433
1434
1435
1436
1437
1442
1459
1460
1461
1462
1463
1464
1465
1466
1467
1470
1471
1472
1473
1479
1480
1481
1482
1486
1487
1488
1489
1490
1491
1492
1498
1499
1500
1501
1502
1503
1504
1505
1506
1510
1511
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1529
1530
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1599
1600
1601
1606
1607
1608
1609
1610
1611
1615
1616
1620
1621
1625
1626
1627
1628
1632
1633
1638
1639
1640
1646
1647
1652
1653
1654
1655
1656
1663
1664
1665
1669
1670
1671
1676
1677
1678
1679
1690
1691
1692
1697
1698
1699
1700
1704
1708
1709
1713
1717
1718
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1780
1781
1784
1785
1786
1787
1788
1789
1793
1794
1799
1800
1804
1805
1806
1807
1808
1809
1810
1811
1816
1817
1818
1819
1820
1821
1822
1823
1824
1828
1829
1830
1831
1836
1837
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1856
1857
1862
1863
1864
1865
1866
1867
1868
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1907
1908
1911
1912
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1976
1977
1980
1981
1982
1983
1984
1985
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2008
2009
2015
2016
2017
2018
2019
2024
2025
2026
2032
2033
2034
2038
2039
2040
2044
2045
2046
2052
2053
2056
2057
2058
2059
2060
2061
2062
2063
2066
2067
2068
2069
2070
2071
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2104
2105
/* ... */
#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 "rpl.h"
#include "friend.h"
#include "transport.h"
#include "access.h"
#include "foundation.h"
#include "beacon.h"
#include "settings.h"
#include "fast_prov.h"
#include "prov_node.h"
#include "test.h"
#include "fast_prov.h"
#include "proxy_client.h"
#include "proxy_server.h"
#include "pvnr_mgmt.h"22 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
/* ... */
#define BLE_MESH_NET_MIN_PDU_LEN (BLE_MESH_NET_HDR_LEN + 1 + 8)
#define IV_UPDATE_SEQ_LIMIT 8000000
#if CONFIG_BLE_MESH_FRIEND
#define FRIEND_CRED_COUNT CONFIG_BLE_MESH_FRIEND_LPN_COUNT
#elif 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,
seq:17;
}{ ... } msg_cache[CONFIG_BLE_MESH_MSG_CACHE_SIZE];
static uint16_t msg_cache_next;
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 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 == BLE_MESH_NET_HDR_SRC(pdu->data) &&
msg_cache[i].seq == (BLE_MESH_NET_HDR_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
struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx)
{
if (bt_mesh_is_provisioned()) {
#if CONFIG_BLE_MESH_NODE
if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
if (net_idx == BLE_MESH_KEY_ANY) {
return &bt_mesh.sub[0];
}{...}
for (int i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
if (bt_mesh.sub[i].net_idx == net_idx) {
return &bt_mesh.sub[i];
}{...}
}{...}
}{...} else {
return bt_mesh_fast_prov_subnet_get(net_idx);
}{...}
/* ... */#endif
}{...} else if (bt_mesh_is_provisioner_en()) {
#if CONFIG_BLE_MESH_PROVISIONER
if (net_idx == BLE_MESH_KEY_ANY) {
return bt_mesh.p_sub[0];
}{...}
for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
if (bt_mesh.p_sub[i] &&
bt_mesh.p_sub[i]->net_idx == net_idx) {
return bt_mesh.p_sub[i];
}{...}
}{...}
#endif/* ... */
}{...}
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 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
err = bt_mesh_secure_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));
#if CONFIG_BLE_MESH_PRIVATE_BEACON
err = bt_mesh_private_beacon_key(key, keys->private_beacon);
if (err) {
BT_ERR("Unable to generate private beacon key");
return err;
}{...}
BT_DBG("PrivateBeaconKey %s", bt_hex(keys->private_beacon, 16));/* ... */
#endif
#if CONFIG_BLE_MESH_DF_SRV
p[0] = 0x02;
err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->direct_enc, keys->direct_privacy);
if (err) {
BT_ERR("Unable to generate directed NID, EncKey & PrivacyKey");
return err;
}{...}
keys->direct_nid = nid;/* ... */
#endif
return 0;
}{ ... }
#if (CONFIG_BLE_MESH_LOW_POWER || 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 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
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_secure_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_secure_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;
#if CONFIG_BLE_MESH_PRB_SRV
sub->private_node_id = BLE_MESH_PRIVATE_NODE_IDENTITY_NOT_SUPPORTED;
#endif
}{...}
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));
/* ... */
bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS;
bt_mesh_net_secure_beacon_update(sub);
#if CONFIG_BLE_MESH_DF_SRV
return bt_mesh_directed_forwarding_sub_init(sub);
#endif
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) {
break;
}{...}
/* ... */
... case BLE_MESH_KR_PHASE_2:
BT_INFO("KR Phase 0x%02x -> Normal", sub->kr_phase);
#if CONFIG_BLE_MESH_PRB_SRV
/* ... */
bt_mesh_private_beacon_update_random(sub);/* ... */
#endif
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;
}{ ... }
#if 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);
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
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
#if CONFIG_BLE_MESH_PRB_SRV
|| bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED
#endif
)) {
bt_mesh_proxy_server_beacon_send(sub);
}{...}
}{ ... }
bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update)
{
int i;
/* ... */
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 (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
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_update) {
BT_DBG("Already in IV Update in Progress state");
return false;
}{...}
}{...} else {
if (iv_index == bt_mesh.iv_index) {
BT_DBG("Same IV Index in normal mode");
return false;
}{...}
/* ... */
if ((iv_index > bt_mesh.iv_index + 1)
#if CONFIG_BLE_MESH_IVU_RECOVERY_IVI
|| (iv_index == bt_mesh.iv_index + 1 && !iv_update)
#endif
) {
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 !CONFIG_BLE_MESH_IVU_RECOVERY_IVI
if (iv_index == bt_mesh.iv_index + 1 && !iv_update) {
BT_WARN("Ignoring new index in normal mode");
return false;
}{...}
#endif/* ... */
if (!iv_update) {
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;
}{...}
}{...}
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_update();
}{...} 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_secure_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 (bt_mesh_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, uint8_t *tx_cred, uint8_t tx_tag,
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);
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
*tx_cred == BLE_MESH_FRIENDSHIP_CRED) {
err = friend_cred_get(sub, BLE_MESH_ADDR_UNASSIGNED, NULL, &enc, &priv);
if (err) {
/* ... */
if (bt_mesh_tag_immutable_cred(tx_tag)) {
BT_ERR("Resend, no friendship credentials found");
return err;
}{...}
BT_WARN("Falling back to managed flooding credentials");
*tx_cred = BLE_MESH_FLOODING_CRED;
enc = sub->keys[new_key].enc;
priv = sub->keys[new_key].privacy;
}{...}
}{...}
#if CONFIG_BLE_MESH_DF_SRV
else if (*tx_cred == BLE_MESH_DIRECTED_CRED) {
enc = sub->keys[new_key].direct_enc;
priv = sub->keys[new_key].direct_privacy;
}{...}
#endif/* ... */
else {
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, false);
if (err) {
BT_ERR("Decrypt failed (err %d)", err);
return err;
}{...}
seq = bt_mesh_next_seq();
sys_put_be24(seq, &buf->data[2]);
dst = BLE_MESH_NET_HDR_DST(buf->data);
err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false, 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, BLE_MESH_ADV(buf)->xmit, cb, cb_data);
return 0;
}{ ... }
static void bt_mesh_net_local(void)
{
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;
}{...}
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);
}{...}
BT_INFO("Use security credentials 0x%02x, proxy %d", tx->ctx->send_cred, proxy);
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
tx->ctx->send_cred == BLE_MESH_FRIENDSHIP_CRED) {
err = friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED,
&nid, &enc, &priv);
if (err) {
/* ... */
if (bt_mesh_tag_immutable_cred(tx->ctx->send_tag)) {
BT_ERR("No friendship credentials found");
return err;
}{...}
BT_WARN("Falling back to managed flooding credentials");
tx->ctx->send_cred = BLE_MESH_FLOODING_CRED;
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;
}{...}
}{...}
#if CONFIG_BLE_MESH_DF_SRV
else if (tx->ctx->send_cred == BLE_MESH_DIRECTED_CRED) {
nid = tx->sub->keys[tx->sub->kr_flag].direct_nid;
enc = tx->sub->keys[tx->sub->kr_flag].direct_enc;
priv = tx->sub->keys[tx->sub->kr_flag].direct_privacy;
}{...}
#endif/* ... */
else {
tx->ctx->send_cred = BLE_MESH_FLOODING_CRED;
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, false);
if (err) {
return err;
}{...}
return bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
}{ ... }
#if !CONFIG_BLE_MESH_V11_SUPPORT
static void bt_mesh_net_adv_xmit_update(struct bt_mesh_net_tx *tx)
{
/* ... */
if (bt_mesh_tag_friendship(tx->ctx->send_tag)) {
tx->xmit = BLE_MESH_TRANSMIT(0, BLE_MESH_TRANSMIT_INT(bt_mesh_net_transmit_get()));
return;
}{...}
if (bt_mesh_tag_relay(tx->ctx->send_tag)) {
tx->xmit = bt_mesh_relay_retransmit_get();
}{...} else {
tx->xmit = bt_mesh_net_transmit_get();
}{...}
return;
}{...}
/* ... */#endif
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)
{
uint8_t bearer = BLE_MESH_ALL_BEARERS;
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();
}{...}
#if 0
/* ... */
if (tx->ctx->send_ttl == 1U &&
!bt_mesh_tag_relay(tx->ctx->send_tag)) {
BT_WARN("Ignore PDU with TTL=1 but not tagged as relay");
err = -EIO;
goto done;
}{...}
#endif/* ... */
/* ... */
#if CONFIG_BLE_MESH_DF_SRV
bt_mesh_update_net_send_cred(tx, &bearer);
#endif
err = bt_mesh_net_encode(tx, &buf->b, false);
if (err) {
goto done;
}{...}
#if CONFIG_BLE_MESH_SELF_TEST
if (net_pdu_test_cb) {
net_pdu_test_cb(buf->data, buf->len);
goto done;
}{...}
#endif/* ... */
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
(bearer & BLE_MESH_GATT_BEARER) &&
(tx->ctx->send_ttl != 1U ||
bt_mesh_tag_relay(tx->ctx->send_tag)) &&
tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) {
if (bt_mesh_proxy_server_relay(&buf->b, tx->ctx->addr) &&
BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
/* ... */
if ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) {
send_cb_finalize(cb, cb_data);
err = 0;
goto done;
}{...}
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) &&
(bearer & BLE_MESH_GATT_BEARER) &&
(tx->ctx->send_ttl != 1U ||
bt_mesh_tag_relay(tx->ctx->send_tag)) &&
tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) {
if (bt_mesh_proxy_client_relay(&buf->b, tx->ctx->addr)) {
/* ... */
send_cb_finalize(cb, cb_data);
err = 0;
goto done;
}{...}
}{...}
if (((IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) ||
(IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en())) &&
(bt_mesh_fixed_group_match(tx->ctx->addr) || bt_mesh_elem_find(tx->ctx->addr))) {
/* ... */
if (BLE_MESH_ADDR_IS_UNICAST(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);
}{...}
bt_mesh_net_local();
err = 0;
goto done;
}{...} else {
net_buf_slist_put(&bt_mesh.local_queue, net_buf_ref(buf));
bt_mesh_net_local();
}{...}
err = 0;
}{...}
if ((bearer & BLE_MESH_ADV_BEARER) &&
(tx->ctx->send_ttl != 1U || bt_mesh_tag_relay(tx->ctx->send_tag))) {
/* ... */
bt_mesh_net_adv_xmit_update(tx);
BT_INFO("Network PDU, count %d, interval %d",
BLE_MESH_TRANSMIT_COUNT(tx->xmit), BLE_MESH_TRANSMIT_INT(tx->xmit));
bt_mesh_adv_send(buf, tx->xmit, cb, cb_data);
err = 0;
goto done;
}{...}
BT_WARN("Not sent, src 0x%04x, dst 0x%04x, ttl %d, cred 0x%02x, tag 0x%02x",
tx->src, tx->ctx->addr, tx->ctx->send_ttl, tx->ctx->send_cred,
tx->ctx->send_tag);
err = -EIO;
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_secure_beacon_auth(keys->beacon, flags, keys->net_id, iv_index,
net_auth);
if (memcmp(auth, net_auth, 8)) {
BT_WARN("Authentication Value %s != %s",
bt_hex(auth, 8), bt_hex(net_auth, 8));
return false;
}{...}
return true;
}{ ... }
struct bt_mesh_subnet *bt_mesh_subnet_find_with_snb(const uint8_t net_id[8], uint8_t flags,
uint32_t iv_index, const uint8_t auth[8],
bool *new_key)
{
size_t subnet_size = 0U;
int i;
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 == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
continue;
}{...}
if (auth_match(&sub->keys[0], net_id, flags, iv_index, auth)) {
*new_key = false;
return sub;
}{...}
if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
continue;
}{...}
if (auth_match(&sub->keys[1], net_id, flags, iv_index, auth)) {
*new_key = true;
return sub;
}{...}
}{...}
return NULL;
}{ ... }
int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc,
const uint8_t *priv, const uint8_t *data,
size_t data_len, struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
BT_DBG("IVI %u net->iv_index 0x%08x", BLE_MESH_NET_HDR_IVI(data), bt_mesh.iv_index);
rx->old_iv = (BLE_MESH_NET_HDR_IVI(data) != (bt_mesh.iv_index & 0x01));
net_buf_simple_reset(buf);
memcpy(net_buf_simple_add(buf, data_len), data, data_len);
if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) {
return -ENOENT;
}{...}
rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data);
if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr)) {
BT_DBG("Ignoring non-unicast src addr 0x%04x", rx->ctx.addr);
return -EINVAL;
}{...}
if (rx->net_if == BLE_MESH_NET_IF_ADV && msg_cache_match(rx, buf)) {
BT_DBG("Duplicate found in Network Message Cache");
return -EALREADY;
}{...}
BT_DBG("src 0x%04x", rx->ctx.addr);
if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) &&
rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) {
return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx),
true, false);
}{...}
return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false, false);
}{ ... }
#if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND)
static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data,
size_t data_len, struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
int i;
BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
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 (BLE_MESH_NET_HDR_NID(data) == cred->cred[0].nid &&
!net_decrypt(sub, cred->cred[0].enc, cred->cred[0].privacy,
data, data_len, rx, buf)) {
return 0;
}{...}
if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
continue;
}{...}
if (BLE_MESH_NET_HDR_NID(data) == cred->cred[1].nid &&
!net_decrypt(sub, cred->cred[1].enc, cred->cred[1].privacy,
data, data_len, rx, buf)) {
rx->new_key = 1U;
return 0;
}{...}
}{...}
return -ENOENT;
}{...}
/* ... */#endif
static int flooding_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data,
size_t data_len, struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
if (BLE_MESH_NET_HDR_NID(data) == sub->keys[0].nid &&
!net_decrypt(sub, sub->keys[0].enc, sub->keys[0].privacy,
data, data_len, rx, buf)) {
return 0;
}{...}
if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
return -ENOENT;
}{...}
if (BLE_MESH_NET_HDR_NID(data) == sub->keys[1].nid &&
!net_decrypt(sub, sub->keys[1].enc, sub->keys[1].privacy,
data, data_len, rx, buf)) {
rx->new_key = 1U;
return 0;
}{...}
return -ENOENT;
}{ ... }
static bool net_find_and_decrypt(const uint8_t *data, size_t data_len,
struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
size_t array_size = 0U;
int i;
array_size = bt_mesh_rx_netkey_size();
for (i = 0; i < array_size; i++) {
sub = bt_mesh_rx_netkey_get(i);
if (!sub) {
BT_DBG("Subnet not found");
continue;
}{...}
if (sub->net_idx == BLE_MESH_KEY_UNUSED) {
continue;
}{...}
#if CONFIG_BLE_MESH_BRC_SRV
sub->sbr_net_idx = BLE_MESH_KEY_UNUSED;
#endif
#if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND)
if (!friend_decrypt(sub, data, data_len, rx, buf)) {
rx->ctx.recv_cred = BLE_MESH_FRIENDSHIP_CRED;
rx->ctx.net_idx = sub->net_idx;
rx->sub = sub;
return true;
}{...}
#endif/* ... */
#if CONFIG_BLE_MESH_DF_SRV
if (!bt_mesh_directed_decrypt(sub, data, data_len, rx, buf)) {
rx->ctx.recv_cred = BLE_MESH_DIRECTED_CRED;
rx->ctx.net_idx = sub->net_idx;
rx->sub = sub;
return true;
}{...}
#endif/* ... */
if (!flooding_decrypt(sub, data, data_len, rx, buf)) {
rx->ctx.recv_cred = BLE_MESH_FLOODING_CRED;
rx->ctx.net_idx = sub->net_idx;
rx->sub = sub;
return true;
}{...}
}{...}
return false;
}{ ... }
/* ... */
static bool relay_to_adv(enum bt_mesh_net_if net_if)
{
switch (net_if) {
case BLE_MESH_NET_IF_LOCAL:
return true;...
case BLE_MESH_NET_IF_ADV:
return (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED);...
case BLE_MESH_NET_IF_PROXY:
return (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED
#if CONFIG_BLE_MESH_PRB_SRV
|| bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED
#endif
);...
default:
return false;...
}{...}
}{ ... }
#if !CONFIG_BLE_MESH_V11_SUPPORT
static uint8_t net_retransmission_adv(struct bt_mesh_net_rx *rx,
uint8_t *cred, uint8_t *tag)
{
if (rx->ctx.recv_cred == BLE_MESH_FLOODING_CRED) {
uint8_t bearer = BLE_MESH_NONE_BEARER;
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_RELAY) &&
bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED) {
bearer |= BLE_MESH_ADV_BEARER;
*cred = BLE_MESH_FLOODING_CRED;
*tag |= BLE_MESH_TAG_RELAY;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) {
bearer |= BLE_MESH_GATT_BEARER;
*cred = BLE_MESH_FLOODING_CRED;
}{...}
return bearer;
}{...}
if (rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED &&
IS_ENABLED(CONFIG_BLE_MESH_FRIEND) &&
bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED) {
*cred = BLE_MESH_FLOODING_CRED;
return BLE_MESH_ALL_BEARERS;
}{...}
return BLE_MESH_NONE_BEARER;
}{...}
static uint8_t net_retransmission_gatt(struct bt_mesh_net_rx *rx,
uint8_t *cred, uint8_t *tag)
{
if (rx->ctx.recv_cred == BLE_MESH_FLOODING_CRED) {
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) {
*cred = BLE_MESH_FLOODING_CRED;
return BLE_MESH_ALL_BEARERS;
}{...}
return BLE_MESH_NONE_BEARER;
}{...}
return BLE_MESH_NONE_BEARER;
}{...}
static uint8_t bt_mesh_net_retrans_match(struct bt_mesh_net_rx *rx,
uint8_t *cred, uint8_t *tag)
{
if (rx->net_if == BLE_MESH_NET_IF_ADV) {
return net_retransmission_adv(rx, cred, tag);
}{...}
if (rx->net_if == BLE_MESH_NET_IF_PROXY) {
return net_retransmission_gatt(rx, cred, tag);
}{...}
return BLE_MESH_NONE_BEARER;
}{...}
/* ... */#endif
static void bt_mesh_net_relay(struct net_buf_simple *sbuf,
struct bt_mesh_net_rx *rx)
{
const uint8_t *enc = NULL, *priv = NULL;
struct net_buf *buf = NULL;
uint8_t bearer = 0;
uint8_t xmit = 0U;
uint8_t cred = 0;
uint8_t nid = 0U;
bool netkey_changed = false;
uint8_t tag = 0;
if (rx->net_if == BLE_MESH_NET_IF_LOCAL) {
/* ... */
/* ... */
if (rx->ctx.recv_ttl == 1U) {
return;
}{...}
}{...} else {
/* ... */
if (rx->ctx.recv_ttl < 1U) {
return;
}{...}
}{...}
#if 0
/* ... */
if (rx->net_if == BLE_MESH_NET_IF_ADV &&
bt_mesh_relay_get() != BLE_MESH_RELAY_ENABLED &&
bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_ENABLED &&
rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) {
return;
}{...}
#endif/* ... */
/* ... */
bearer = bt_mesh_net_retrans_match(rx, &cred, &tag);
if (bearer == BLE_MESH_NONE_BEARER) {
BT_WARN("No outbound bearer found, inbound bearer %d", rx->net_if);
return;
}{...}
#if CONFIG_BLE_MESH_BRC_SRV
if (rx->sbr_rpl) {
BT_ERR("Bridge RPL attack");
goto done;
}{...}
#endif/* ... */
if (cred != BLE_MESH_FLOODING_CRED
#if CONFIG_BLE_MESH_DF_SRV
&& cred != BLE_MESH_DIRECTED_CRED
#endif
) {
BT_WARN("No outbound security cred found, inbound cred %d", rx->ctx.recv_cred);
return;
}{...}
if (rx->ctx.recv_ttl == 0x01) {
BT_DBG("Ignore PDU with TTL = 1");
return;
}{...}
if (rx->ctx.recv_ttl == 0x02 && bt_mesh_tag_relay(tag) == false) {
BT_DBG("Ignore PDU with TTL = 2 but not tagged as relay");
return;
}{...}
BT_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, rx->ctx.recv_dst);
/* ... */
if (rx->net_if == BLE_MESH_NET_IF_ADV &&
rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) {
xmit = bt_mesh_relay_retransmit_get();
}{...} else {
xmit = bt_mesh_net_transmit_get();
}{...}
#if 0
xmit = bt_mesh_net_adv_xmit_update(cred, tag, rx->ctl);
if (xmit == 0) {
return;
}{...}
#endif/* ... */
/* ... */
#if !CONFIG_BLE_MESH_RELAY_ADV_BUF
buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT);
#else
/* ... */
if (bt_mesh_get_stored_relay_count() >= BLE_MESH_MAX_STORED_RELAY_COUNT) {
xmit = BLE_MESH_TRANSMIT(0, 20);
}{...}
buf = bt_mesh_relay_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT);/* ... */
#endif
if (!buf) {
BT_INFO("Out of relay buffers");
return;
}{...}
if (rx->net_if != BLE_MESH_NET_IF_LOCAL) {
sbuf->data[1] &= 0x80;
sbuf->data[1] |= rx->ctx.recv_ttl - 1U;
}{...}
net_buf_add_mem(buf, sbuf->data, sbuf->len);
if (cred == BLE_MESH_FLOODING_CRED) {
enc = rx->sub->keys[rx->sub->kr_flag].enc;
priv = rx->sub->keys[rx->sub->kr_flag].privacy;
nid = rx->sub->keys[rx->sub->kr_flag].nid;
#if CONFIG_BLE_MESH_DF_SRV
}{...} else {
enc = rx->sub->keys[rx->sub->kr_flag].direct_enc;
priv = rx->sub->keys[rx->sub->kr_flag].direct_privacy;
nid = rx->sub->keys[rx->sub->kr_flag].direct_nid;
#endif
}{...}
#if CONFIG_BLE_MESH_BRC_SRV
if (bt_mesh_subnet_bridge_state_get() == BLE_MESH_SUBNET_BRIDGE_ENABLED) {
netkey_changed = bt_mesh_bridge_change_net_key(rx, &enc, &priv, &nid, cred);
}{...}
#endif/* ... */
BT_DBG("Relaying packet. TTL is now %u", BLE_MESH_NET_HDR_TTL(buf->data));
/* ... */
if (netkey_changed || cred != rx->ctx.recv_cred) {
buf->data[0] &= 0x80;
buf->data[0] |= nid;
}{...}
/* ... */
if (bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_RX(rx), false, false)) {
BT_ERR("Re-encrypting failed");
goto done;
}{...}
if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) {
BT_ERR("Re-obfuscating failed");
goto done;
}{...}
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
(bearer & BLE_MESH_GATT_BEARER) &&
((bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED &&
cred != BLE_MESH_FRIENDSHIP_CRED) ||
#if CONFIG_BLE_MESH_PRB_SRV
bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED ||
#endif
rx->net_if == BLE_MESH_NET_IF_LOCAL ||
rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED)) {
if (bt_mesh_proxy_server_relay(&buf->b, rx->ctx.recv_dst) &&
BLE_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) {
if ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) {
goto done;
}{...}
}{...}
}{...}
if (((bearer & BLE_MESH_ADV_BEARER) && relay_to_adv(rx->net_if)) ||
netkey_changed ||
rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) {
#if !CONFIG_BLE_MESH_RELAY_ADV_BUF
bt_mesh_adv_send(buf, xmit, NULL, NULL);
#else
bt_mesh_relay_adv_send(buf, xmit, rx->ctx.addr, rx->ctx.recv_dst, NULL, NULL);
#endif
}{...}
done:
net_buf_unref(buf);
}{ ... }
void bt_mesh_net_header_parse(struct net_buf_simple *buf,
struct bt_mesh_net_rx *rx)
{
rx->old_iv = (BLE_MESH_NET_HDR_IVI(buf->data) != (bt_mesh.iv_index & 0x01));
rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data);
rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data);
rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data);
rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data);
rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data);
}{ ... }
int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if,
struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
{
if (data->len < BLE_MESH_NET_MIN_PDU_LEN) {
BT_WARN("Dropping too short mesh packet (len %u)", data->len);
BT_WARN("%s", bt_hex(data->data, data->len));
return -EINVAL;
}{...}
if (net_if == BLE_MESH_NET_IF_ADV && check_dup(data)) {
return -EINVAL;
}{...}
BT_DBG("%u bytes: %s", data->len, bt_hex(data->data, data->len));
rx->net_if = net_if;
if (!net_find_and_decrypt(data->data, data->len, rx, buf)) {
BT_DBG("Unable to find matching net for packet");
return -ENOENT;
}{...}
/* ... */
if (net_if == BLE_MESH_NET_IF_PROXY &&
rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) {
BT_WARN("Dropping packet from GATT bearer using friendship creds");
return -EBADMSG;
}{...}
rx->ctx.app_idx = BLE_MESH_KEY_UNUSED;
rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data);
if (rx->ctx.recv_ttl == 0U) {
rx->ctx.send_ttl = 0U;
}{...} else {
rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT;
}{...}
rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data);
rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data);
rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data);
BT_DBG("Decryption successful. Payload len %u", buf->len);
if (net_if != BLE_MESH_NET_IF_PROXY_CFG &&
rx->ctx.recv_dst == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("Destination address is unassigned; dropping packet");
return -EBADMSG;
}{...}
#if !CONFIG_BLE_MESH_BQB_TEST
if (BLE_MESH_ADDR_IS_RFU(rx->ctx.recv_dst)) {
BT_ERR("Destination address is RFU; dropping packet 0x%02x", rx->ctx.recv_dst);
return -EBADMSG;
}{...}
#endif/* ... */
if (net_if != BLE_MESH_NET_IF_LOCAL && bt_mesh_elem_find(rx->ctx.addr)) {
BT_DBG("Dropping locally originated packet");
return -EBADMSG;
}{...}
BT_DBG("src 0x%04x dst 0x%04x ttl %u", rx->ctx.addr, rx->ctx.recv_dst,
rx->ctx.recv_ttl);
BT_DBG("PDU: %s", bt_hex(buf->data, buf->len));
msg_cache_add(rx);
return 0;
}{ ... }
static bool ready_to_recv(void)
{
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
return true;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
if (bt_mesh_provisioner_get_node_count()) {
return true;
}{...}
}{...}
return false;
}{ ... }
static bool ignore_net_msg(uint16_t src, uint16_t dst)
{
if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
/* ... */
return false;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
bt_mesh_is_provisioner_en()) {
/* ... */
if (!bt_mesh_provisioner_get_node_with_addr(src) &&
!bt_mesh_elem_find(src)) {
BT_INFO("Not found node address 0x%04x", src);
return true;
}{...}
}{...}
return false;
}{ ... }
void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,
enum bt_mesh_net_if net_if)
{
NET_BUF_SIMPLE_DEFINE(buf, 29);
struct bt_mesh_net_rx rx = { .ctx.recv_rssi = rssi };
struct net_buf_simple_state state = {0};
BT_DBG("rssi %d net_if %u", rssi, net_if);
if (!ready_to_recv()) {
return;
}{...}
if (bt_mesh_net_decode(data, net_if, &rx, &buf)) {
return;
}{...}
if (ignore_net_msg(rx.ctx.addr, rx.ctx.recv_dst)) {
return;
}{...}
net_buf_simple_save(&buf, &state);
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_NET,
"\nNetRecv: ctl: %d, src: %d, dst: %d, ttl: %d, data: 0x%s",
rx.ctl, rx.ctx.addr, rx.ctx.recv_dst, rx.ctx.recv_ttl,
bt_hex(buf.data + BLE_MESH_NET_HDR_LEN, buf.len - BLE_MESH_NET_HDR_LEN));
/* ... */
rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) ||
bt_mesh_fixed_direct_match(rx.sub, rx.ctx.recv_dst) ||
bt_mesh_elem_find(rx.ctx.recv_dst));
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
#if CONFIG_BLE_MESH_PRB_SRV
bt_mesh_private_gatt_proxy_state_get() != BLE_MESH_PRIVATE_GATT_PROXY_ENABLED &&
#endif
net_if == BLE_MESH_NET_IF_PROXY) {
bt_mesh_proxy_server_addr_add(data, rx.ctx.addr);
if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_DISABLED &&
!rx.local_match) {
BT_INFO("Proxy is disabled; ignoring message");
return;
}{...}
/* ... */
#if CONFIG_BLE_MESH_DF_SRV
if (rx.sub->directed_proxy == BLE_MESH_DIRECTED_PROXY_ENABLED &&
rx.sub->use_directed == BLE_MESH_PROXY_USE_DIRECTED_ENABLED &&
!bt_mesh_addr_in_uar(&rx.sub->proxy_client_uar, rx.ctx.addr) &&
!bt_mesh_proxy_server_find_client_by_addr(rx.ctx.addr)) {
rx.ctx.recv_tag |= BLE_MESH_TAG_IMMUTABLE_CRED;
}{...}
#endif/* ... */
}{...}
/* ... */
if (bt_mesh_trans_recv(&buf, &rx) == -EAGAIN) {
BT_WARN("Removing rejected message from Network Message Cache");
msg_cache[rx.msg_cache_idx].src = BLE_MESH_ADDR_UNASSIGNED;
msg_cache_next = rx.msg_cache_idx;
}{...}
/* ... */
if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) ||
(!rx.local_match && !rx.friend_match
#if CONFIG_BLE_MESH_NOT_RELAY_REPLAY_MSG
&& !rx.replay_msg
#endif
)) {
net_buf_simple_restore(&buf, &state);
bt_mesh_net_relay(&buf, &rx);
}{...}
}{ ... }
static void ivu_refresh(struct k_work *work)
{
bt_mesh.ivu_duration += BLE_MESH_IVU_HOURS;
BT_INFO("%s for %u hour%s",
bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) ?
"IVU in Progress" : "IVU Normal mode",
bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1U ? "" : "s");
if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_iv(true);
}{...}
k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
return;
}{...}
if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
bt_mesh_beacon_ivu_initiator(true);
bt_mesh_net_iv_update(bt_mesh.iv_index, false);
}{...} else if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_iv(true);
}{...}
}{ ... }
void bt_mesh_net_start(void)
{
if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
bt_mesh_secure_beacon_enable();
}{...} else {
bt_mesh_secure_beacon_disable();
}{...}
#if CONFIG_BLE_MESH_PRB_SRV
if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
bt_mesh_private_beacon_enable();
}{...} else {
bt_mesh_private_beacon_disable();
}{...}
#endif/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) {
bt_mesh_proxy_server_gatt_enable();
bt_mesh_adv_update();
}{...}
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
NULL);/* ... */
#endif
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_init();
}{...} else {
bt_mesh_scan_enable();
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
uint16_t net_idx = bt_mesh.sub[0].net_idx;
uint16_t addr = bt_mesh_primary_addr();
uint32_t iv_index = bt_mesh.iv_index;
uint8_t flags = (uint8_t)bt_mesh.sub[0].kr_flag;
const uint8_t *net_key = bt_mesh.sub[0].keys[flags].net;
if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
flags |= BLE_MESH_NET_FLAG_IVU;
}{...}
bt_mesh_prov_complete(net_idx, net_key, addr, flags, iv_index);
}{...}
}{ ... }
void bt_mesh_net_init(void)
{
k_delayed_work_init(&bt_mesh.ivu_timer, ivu_refresh);
}{ ... }
void bt_mesh_net_reset(void)
{
k_delayed_work_cancel(&bt_mesh.ivu_timer);
#if FRIEND_CRED_COUNT > 0
memset(friend_cred, 0, sizeof(friend_cred));
#endif
memset(msg_cache, 0, sizeof(msg_cache));
msg_cache_next = 0U;
memset(dup_cache, 0, sizeof(dup_cache));
dup_cache_next = 0U;
bt_mesh.iv_index = 0U;
bt_mesh.seq = 0U;
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
void bt_mesh_net_deinit(void)
{
bt_mesh_net_reset();
k_delayed_work_free(&bt_mesh.ivu_timer);
/* ... */
}{ ... }
/* ... */#endif