-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
transport.c
1981 lines (1607 loc) · 55.1 KB
/
transport.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 "crypto.h"
#include "adv.h"
#include "mesh.h"
#include "lpn.h"
#include "friend.h"
#include "access.h"
#include "foundation.h"
#include "settings.h"
#include "transport.h"
#include "mesh_main.h"
#include "mesh_common.h"
#include "cfg_srv.h"
/* The transport layer needs at least three buffers for itself to avoid
* deadlocks. Ensure that there are a sufficient number of advertising
* buffers available compared to the maximum supported outgoing segment
* count.
*/
_Static_assert(CONFIG_BLE_MESH_ADV_BUF_COUNT >= (CONFIG_BLE_MESH_TX_SEG_MAX + 3),
"Too small BLE Mesh adv buffer count");
#define AID_MASK ((uint8_t)(BIT_MASK(6)))
#define SEG(data) ((data)[0] >> 7)
#define AKF(data) (((data)[0] >> 6) & 0x01)
#define AID(data) ((data)[0] & AID_MASK)
#define ASZMIC(data) (((data)[1] >> 7) & 1)
#define APP_MIC_LEN(aszmic) ((aszmic) ? BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT)
#define UNSEG_HDR(akf, aid) ((akf << 6) | (aid & AID_MASK))
#define SEG_HDR(akf, aid) (UNSEG_HDR(akf, aid) | 0x80)
#define BLOCK_COMPLETE(seg_n) (uint32_t)(((uint64_t)1 << (seg_n + 1)) - 1)
#define SEQ_AUTH(iv_index, seq) (((uint64_t)iv_index) << 24 | (uint64_t)seq)
/* Number of retransmit attempts (after the initial transmit) per segment */
#define SEG_RETRANSMIT_ATTEMPTS 4
/* "This timer shall be set to a minimum of 200 + 50 * TTL milliseconds.".
* We use 400 since 300 is a common send duration for standard HCI, and we
* need to have a timeout that's bigger than that.
*/
#define SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) (K_MSEC(400) + 50 * (tx)->ttl)
/* When sending to a group, the messages are not acknowledged, and there's no
* reason to delay the repetitions significantly. Delaying by more than 0 ms
* to avoid flooding the network.
*/
#define SEG_RETRANSMIT_TIMEOUT_GROUP K_MSEC(50)
#define SEG_RETRANSMIT_TIMEOUT(tx) \
(BLE_MESH_ADDR_IS_UNICAST((tx)->dst) ? \
SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) : \
SEG_RETRANSMIT_TIMEOUT_GROUP)
/* How long to wait for available buffers before giving up */
#define BUF_TIMEOUT K_NO_WAIT
static struct seg_tx {
struct bt_mesh_subnet *sub;
struct net_buf *seg[CONFIG_BLE_MESH_TX_SEG_MAX];
uint64_t seq_auth;
uint16_t dst;
uint8_t seg_n:5, /* Last segment index */
new_key:1; /* New/old key */
uint8_t nack_count; /* Number of unacked segs */
uint8_t ttl;
uint8_t seg_pending; /* Number of segments pending */
uint8_t attempts; /* Transmit attempts */
const struct bt_mesh_send_cb *cb;
void *cb_data;
struct k_delayed_work retransmit; /* Retransmit timer */
} seg_tx[CONFIG_BLE_MESH_TX_SEG_MSG_COUNT];
static struct seg_rx {
struct bt_mesh_subnet *sub;
uint64_t seq_auth;
uint8_t seg_n:5,
ctl:1,
in_use:1,
obo:1;
uint8_t hdr;
uint8_t ttl;
uint16_t src;
uint16_t dst;
uint32_t block;
uint32_t last;
struct k_delayed_work ack;
struct net_buf_simple buf;
} seg_rx[CONFIG_BLE_MESH_RX_SEG_MSG_COUNT] = {
[0 ... (CONFIG_BLE_MESH_RX_SEG_MSG_COUNT - 1)] = {
.buf.size = CONFIG_BLE_MESH_RX_SDU_MAX,
},
};
static uint8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT *
CONFIG_BLE_MESH_RX_SDU_MAX)];
static uint16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED;
static bt_mesh_mutex_t tx_seg_lock;
static bt_mesh_mutex_t rx_seg_lock;
static inline void bt_mesh_tx_seg_mutex_new(void)
{
if (!tx_seg_lock.mutex) {
bt_mesh_mutex_create(&tx_seg_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_tx_seg_mutex_free(void)
{
bt_mesh_mutex_free(&tx_seg_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_tx_seg_lock(void)
{
bt_mesh_mutex_lock(&tx_seg_lock);
}
static inline void bt_mesh_tx_seg_unlock(void)
{
bt_mesh_mutex_unlock(&tx_seg_lock);
}
static inline void bt_mesh_rx_seg_mutex_new(void)
{
if (!rx_seg_lock.mutex) {
bt_mesh_mutex_create(&rx_seg_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_rx_seg_mutex_free(void)
{
bt_mesh_mutex_free(&rx_seg_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_rx_seg_lock(void)
{
bt_mesh_mutex_lock(&rx_seg_lock);
}
static inline void bt_mesh_rx_seg_unlock(void)
{
bt_mesh_mutex_unlock(&rx_seg_lock);
}
uint8_t bt_mesh_get_seg_retrans_num(void)
{
return SEG_RETRANSMIT_ATTEMPTS;
}
int32_t bt_mesh_get_seg_retrans_timeout(uint8_t ttl)
{
/* This function will be used when a client model sending an
* acknowledged message. And if the dst of a message is not
* a unicast address, the function will not be invoked.
* So we can directly use the SEG_RETRANSMIT_TIMEOUT_UNICAST
* macro here.
*/
struct seg_tx tx = {
.ttl = ttl,
};
return SEG_RETRANSMIT_TIMEOUT_UNICAST(&tx);
}
void bt_mesh_set_hb_sub_dst(uint16_t addr)
{
hb_sub_dst = addr;
}
static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu,
const struct bt_mesh_send_cb *cb, void *cb_data)
{
struct net_buf *buf = NULL;
BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x sdu_len %u",
tx->src, tx->ctx->addr, tx->ctx->app_idx, sdu->len);
buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, BUF_TIMEOUT);
if (!buf) {
BT_ERR("Out of network buffers");
return -ENOBUFS;
}
net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN);
if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) {
net_buf_add_u8(buf, UNSEG_HDR(0, 0));
} else {
net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid));
}
net_buf_add_mem(buf, sdu->data, sdu->len);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx,
tx->src, tx->ctx->addr,
NULL, 1)) {
if (BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
BT_ERR("Not enough space in Friend Queue");
net_buf_unref(buf);
return -ENOBUFS;
} else {
BT_WARN("No space in Friend Queue");
goto send;
}
}
if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE,
NULL, 1, &buf->b) &&
BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
/* PDUs for a specific Friend should only go
* out through the Friend Queue.
*/
net_buf_unref(buf);
send_cb_finalize(cb, cb_data);
return 0;
}
}
send:
return bt_mesh_net_send(tx, buf, cb, cb_data);
}
bool bt_mesh_tx_in_progress(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
if (seg_tx[i].nack_count) {
return true;
}
}
return false;
}
static void seg_tx_done(struct seg_tx *tx, uint8_t seg_idx)
{
bt_mesh_adv_buf_ref_debug(__func__, tx->seg[seg_idx], 3U, BLE_MESH_BUF_REF_SMALL);
BLE_MESH_ADV(tx->seg[seg_idx])->busy = 0U;
net_buf_unref(tx->seg[seg_idx]);
tx->seg[seg_idx] = NULL;
tx->nack_count--;
}
static void seg_tx_reset(struct seg_tx *tx)
{
int i;
bt_mesh_tx_seg_lock();
k_delayed_work_cancel(&tx->retransmit);
tx->cb = NULL;
tx->cb_data = NULL;
tx->seq_auth = 0U;
tx->sub = NULL;
tx->dst = BLE_MESH_ADDR_UNASSIGNED;
for (i = 0; i <= tx->seg_n && tx->nack_count; i++) {
if (!tx->seg[i]) {
continue;
}
seg_tx_done(tx, i);
}
tx->nack_count = 0U;
bt_mesh_tx_seg_unlock();
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) {
BT_DBG("Proceding with pending IV Update");
/* bt_mesh_net_iv_update() will re-enable the flag if this
* wasn't the only transfer.
*/
if (bt_mesh_net_iv_update(bt_mesh.iv_index, false)) {
bt_mesh_net_sec_update(NULL);
}
}
}
static inline void seg_tx_complete(struct seg_tx *tx, int err)
{
const struct bt_mesh_send_cb *cb = tx->cb;
void *cb_data = tx->cb_data;
seg_tx_reset(tx);
if (cb && cb->end) {
cb->end(err, cb_data);
}
}
static void schedule_retransmit(struct seg_tx *tx)
{
if (--tx->seg_pending) {
return;
}
if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst) && !tx->attempts) {
BT_INFO("Complete tx sdu to group");
seg_tx_complete(tx, 0);
return;
}
k_delayed_work_submit(&tx->retransmit, SEG_RETRANSMIT_TIMEOUT(tx));
}
static void seg_first_send_start(uint16_t duration, int err, void *user_data)
{
struct seg_tx *tx = user_data;
if (tx->cb && tx->cb->start) {
tx->cb->start(duration, err, tx->cb_data);
}
}
static void seg_send_start(uint16_t duration, int err, void *user_data)
{
struct seg_tx *tx = user_data;
/* If there's an error in transmitting the 'sent' callback will never
* be called. Make sure that we kick the retransmit timer also in this
* case since otherwise we risk the transmission of becoming stale.
*/
if (err) {
schedule_retransmit(tx);
}
}
static void seg_sent(int err, void *user_data)
{
struct seg_tx *tx = user_data;
schedule_retransmit(tx);
}
static const struct bt_mesh_send_cb first_sent_cb = {
.start = seg_first_send_start,
.end = seg_sent,
};
static const struct bt_mesh_send_cb seg_sent_cb = {
.start = seg_send_start,
.end = seg_sent,
};
static void seg_tx_send_unacked(struct seg_tx *tx)
{
int i, err = 0;
bt_mesh_tx_seg_lock();
if (!(tx->attempts--)) {
BT_WARN("Ran out of retransmit attempts");
bt_mesh_tx_seg_unlock();
seg_tx_complete(tx, -ETIMEDOUT);
return;
}
BT_INFO("Attempts: %u", tx->attempts);
for (i = 0; i <= tx->seg_n; i++) {
struct net_buf *seg = tx->seg[i];
if (!seg) {
continue;
}
if (BLE_MESH_ADV(seg)->busy) {
BT_DBG("Skipping segment that's still advertising");
continue;
}
tx->seg_pending++;
BT_DBG("resending %u/%u", i, tx->seg_n);
err = bt_mesh_net_resend(tx->sub, seg, tx->new_key,
&seg_sent_cb, tx);
if (err) {
BT_ERR("Sending segment failed");
bt_mesh_tx_seg_unlock();
seg_tx_complete(tx, -EIO);
return;
}
}
bt_mesh_tx_seg_unlock();
}
static void seg_retransmit(struct k_work *work)
{
struct seg_tx *tx = CONTAINER_OF(work, struct seg_tx, retransmit);
seg_tx_send_unacked(tx);
}
static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
const struct bt_mesh_send_cb *cb, void *cb_data)
{
uint8_t seg_hdr = 0U, seg_o = 0U;
uint16_t seq_zero = 0U;
struct seg_tx *tx = NULL;
int i;
BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u",
net_tx->src, net_tx->ctx->addr, net_tx->ctx->app_idx,
net_tx->aszmic, sdu->len);
if (sdu->len < 1) {
BT_ERR("Zero-length SDU not allowed");
return -EINVAL;
}
if (sdu->len > BLE_MESH_TX_SDU_MAX) {
BT_ERR("Not enough segment buffers for length %u", sdu->len);
return -EMSGSIZE;
}
for (tx = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) {
if (!seg_tx[i].nack_count) {
tx = &seg_tx[i];
break;
}
}
if (!tx) {
BT_ERR("No multi-segment message contexts available");
return -EBUSY;
}
if (net_tx->ctx->app_idx == BLE_MESH_KEY_DEV) {
seg_hdr = SEG_HDR(0, 0);
} else {
seg_hdr = SEG_HDR(1, net_tx->aid);
}
seg_o = 0U;
tx->dst = net_tx->ctx->addr;
tx->seg_n = (sdu->len - 1) / BLE_MESH_APP_SEG_SDU_MAX;
tx->nack_count = tx->seg_n + 1;
tx->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq);
tx->sub = net_tx->sub;
tx->new_key = net_tx->sub->kr_flag;
tx->attempts = SEG_RETRANSMIT_ATTEMPTS;
tx->seg_pending = 0;
tx->cb = cb;
tx->cb_data = cb_data;
if (net_tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) {
tx->ttl = bt_mesh_default_ttl_get();
} else {
tx->ttl = net_tx->ctx->send_ttl;
}
seq_zero = tx->seq_auth & TRANS_SEQ_ZERO_MASK;
BT_DBG("SeqZero 0x%04x", seq_zero);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) &&
!bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src,
tx->dst, &tx->seq_auth,
tx->seg_n + 1) &&
BLE_MESH_ADDR_IS_UNICAST(tx->dst)) {
BT_ERR("Not enough space in Friend Queue for %u segments",
tx->seg_n + 1);
seg_tx_reset(tx);
return -ENOBUFS;
}
for (seg_o = 0U; sdu->len; seg_o++) {
struct net_buf *seg = NULL;
uint16_t len = 0U;
int err = 0;
seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, net_tx->xmit,
BUF_TIMEOUT);
if (!seg) {
BT_ERR("Out of segment buffers");
seg_tx_reset(tx);
return -ENOBUFS;
}
net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN);
net_buf_add_u8(seg, seg_hdr);
net_buf_add_u8(seg, (net_tx->aszmic << 7) | seq_zero >> 6);
net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) |
(seg_o >> 3)));
net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx->seg_n);
len = MIN(sdu->len, BLE_MESH_APP_SEG_SDU_MAX);
net_buf_add_mem(seg, sdu->data, len);
net_buf_simple_pull(sdu, len);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
enum bt_mesh_friend_pdu_type type = BLE_MESH_FRIEND_PDU_PARTIAL;
if (seg_o == tx->seg_n) {
type = BLE_MESH_FRIEND_PDU_COMPLETE;
} else {
type = BLE_MESH_FRIEND_PDU_PARTIAL;
}
if (bt_mesh_friend_enqueue_tx(net_tx, type,
&tx->seq_auth,
tx->seg_n + 1,
&seg->b) &&
BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) {
/* PDUs for a specific Friend should only go
* out through the Friend Queue.
*/
net_buf_unref(seg);
continue;
}
}
tx->seg[seg_o] = net_buf_ref(seg);
BT_DBG("Sending %u/%u", seg_o, tx->seg_n);
tx->seg_pending++;
err = bt_mesh_net_send(net_tx, seg,
seg_o ? &seg_sent_cb : &first_sent_cb,
tx);
if (err) {
BT_ERR("Sending segment failed");
seg_tx_reset(tx);
return err;
}
}
/* This can happen if segments only went into the Friend Queue */
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) {
seg_tx_reset(tx);
/* If there was a callback notify sending immediately since
* there's no other way to track this (at least currently)
* with the Friend Queue.
*/
send_cb_finalize(cb, cb_data);
}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
bt_mesh_lpn_established()) {
bt_mesh_lpn_poll();
}
return 0;
}
struct bt_mesh_app_key *bt_mesh_app_key_find(uint16_t app_idx)
{
int i;
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 != BLE_MESH_KEY_UNUSED &&
key->app_idx == app_idx) {
return key;
}
}
return NULL;
}
int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg,
const struct bt_mesh_send_cb *cb, void *cb_data)
{
const uint8_t *key = NULL;
uint8_t *ad = NULL, role = 0U;
uint8_t aid = 0U;
int err = 0;
if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) {
BT_ERR("Insufficient tailroom for Transport MIC");
return -EINVAL;
}
if (msg->len > BLE_MESH_SDU_UNSEG_MAX) {
tx->ctx->send_rel = 1U;
}
BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->sub->net_idx,
tx->ctx->app_idx, tx->ctx->addr);
BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len));
role = bt_mesh_get_device_role(tx->ctx->model, tx->ctx->srv_send);
if (role == ROLE_NVAL) {
BT_ERR("Failed to get model role");
return -EINVAL;
}
err = bt_mesh_app_key_get(tx->sub, tx->ctx->app_idx, &key,
&aid, role, tx->ctx->addr);
if (err) {
return err;
}
tx->aid = aid;
if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) {
tx->aszmic = 0U;
} else {
tx->aszmic = 1U;
}
if (BLE_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) {
ad = bt_mesh_label_uuid_get(tx->ctx->addr);
} else {
ad = NULL;
}
err = bt_mesh_app_encrypt(key, tx->ctx->app_idx == BLE_MESH_KEY_DEV,
tx->aszmic, msg, ad, tx->src,
tx->ctx->addr, bt_mesh.seq,
BLE_MESH_NET_IVI_TX);
if (err) {
BT_ERR("Encrypt failed (err %d)", err);
return err;
}
if (tx->ctx->send_rel) {
err = send_seg(tx, msg, cb, cb_data);
} else {
err = send_unseg(tx, msg, cb, cb_data);
}
return err;
}
static void update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx)
{
rpl->src = rx->ctx.addr;
rpl->seq = rx->seq;
rpl->old_iv = rx->old_iv;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_rpl(rpl);
}
}
/* Check the Replay Protection List for a replay attempt. If non-NULL match
* parameter is given the RPL slot is returned but it is not immediately
* updated (needed for segmented messages), whereas if a NULL match is given
* the RPL is immediately updated (used for unsegmented messages).
*/
bool bt_mesh_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match)
{
int i;
/* Don't bother checking messages from ourselves */
if (rx->net_if == BLE_MESH_NET_IF_LOCAL) {
return false;
}
/* The RPL is used only for the local node */
if (!rx->local_match) {
return false;
}
for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
/* Empty slot */
if (!rpl->src) {
if (match) {
*match = rpl;
} else {
update_rpl(rpl, rx);
}
return false;
}
/* Existing slot for given address */
if (rpl->src == rx->ctx.addr) {
if (rx->old_iv && !rpl->old_iv) {
return true;
}
if ((!rx->old_iv && rpl->old_iv) ||
rpl->seq < rx->seq) {
if (match) {
*match = rpl;
} else {
update_rpl(rpl, rx);
}
return false;
} else {
return true;
}
}
}
BT_ERR("RPL is full!");
return true;
}
static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr,
uint8_t aszmic, struct net_buf_simple *buf)
{
struct net_buf_simple *sdu = NULL;
size_t array_size = 0U;
uint8_t *ad = NULL;
size_t i = 0U;
int err = 0;
BT_DBG("ASZMIC %u AKF %u AID 0x%02x", aszmic, AKF(&hdr), AID(&hdr));
BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
if (buf->len < 1 + APP_MIC_LEN(aszmic)) {
BT_ERR("Too short SDU + MIC (len %u)", buf->len);
return -EINVAL;
}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !rx->local_match) {
BT_DBG("Ignoring PDU for LPN 0x%04x of this Friend",
rx->ctx.recv_dst);
return 0;
}
if (BLE_MESH_ADDR_IS_VIRTUAL(rx->ctx.recv_dst)) {
ad = bt_mesh_label_uuid_get(rx->ctx.recv_dst);
} else {
ad = NULL;
}
/* Adjust the length to not contain the MIC at the end */
buf->len -= APP_MIC_LEN(aszmic);
/* Use bt_mesh_alloc_buf() instead of NET_BUF_SIMPLE_DEFINE to avoid
* causing btu task stackoverflow.
*/
sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - BLE_MESH_MIC_SHORT);
if (!sdu) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}
if (!AKF(&hdr)) {
array_size = bt_mesh_rx_devkey_size();
for (i = 0U; i < array_size; i++) {
const uint8_t *dev_key = NULL;
dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr);
if (!dev_key) {
BT_DBG("DevKey not found");
continue;
}
net_buf_simple_reset(sdu);
err = bt_mesh_app_decrypt(dev_key, true, aszmic, buf,
sdu, ad, rx->ctx.addr,
rx->ctx.recv_dst, seq,
BLE_MESH_NET_IVI_RX(rx));
if (err) {
continue;
}
rx->ctx.app_idx = BLE_MESH_KEY_DEV;
bt_mesh_model_recv(rx, sdu);
bt_mesh_free_buf(sdu);
return 0;
}
BT_WARN("Unable to decrypt with DevKey");
bt_mesh_free_buf(sdu);
return -ENODEV;
}
array_size = bt_mesh_rx_appkey_size();
for (i = 0U; i < array_size; i++) {
struct bt_mesh_app_keys *keys = NULL;
struct bt_mesh_app_key *key = NULL;
key = bt_mesh_rx_appkey_get(i);
if (!key) {
BT_DBG("AppKey not found");
continue;
}
/* Make sure that this AppKey matches received net_idx */
if (key->net_idx != rx->sub->net_idx) {
continue;
}
if (rx->new_key && key->updated) {
keys = &key->keys[1];
} else {
keys = &key->keys[0];
}
/* Check that the AppKey ID matches */
if (AID(&hdr) != keys->id) {
continue;
}
net_buf_simple_reset(sdu);
err = bt_mesh_app_decrypt(keys->val, false, aszmic, buf,
sdu, ad, rx->ctx.addr,
rx->ctx.recv_dst, seq,
BLE_MESH_NET_IVI_RX(rx));
if (err) {
BT_DBG("Unable to decrypt with AppKey 0x%03x",
key->app_idx);
continue;
}
rx->ctx.app_idx = key->app_idx;
bt_mesh_model_recv(rx, sdu);
bt_mesh_free_buf(sdu);
return 0;
}
if (rx->local_match) {
BT_WARN("No matching AppKey");
}
bt_mesh_free_buf(sdu);
return 0;
}
static struct seg_tx *seg_tx_lookup(uint16_t seq_zero, uint8_t obo, uint16_t addr)
{
struct seg_tx *tx = NULL;
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
tx = &seg_tx[i];
if ((tx->seq_auth & TRANS_SEQ_ZERO_MASK) != seq_zero) {
continue;
}
if (tx->dst == addr) {
return tx;
}
/* If the expected remote address doesn't match,
* but the OBO flag is set and this is the first
* acknowledgement, assume it's a Friend that's
* responding and therefore accept the message.
*/
if (obo && tx->nack_count == tx->seg_n + 1) {
tx->dst = addr;
return tx;
}
}
return NULL;
}
static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr,
struct net_buf_simple *buf, uint64_t *seq_auth)
{
struct seg_tx *tx = NULL;
unsigned int bit = 0;
uint32_t ack = 0U;
uint16_t seq_zero = 0U;
uint8_t obo = 0U;
if (buf->len < 6) {
BT_ERR("Too short ack message (len %u)", buf->len);
return -EINVAL;
}
seq_zero = net_buf_simple_pull_be16(buf);
obo = seq_zero >> 15;
seq_zero = (seq_zero >> 2) & TRANS_SEQ_ZERO_MASK;
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match) {
BT_DBG("Ack for LPN 0x%04x of this Friend", rx->ctx.recv_dst);
/* Best effort - we don't have enough info for true SeqAuth */
*seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(rx), seq_zero);
return 0;
}
ack = net_buf_simple_pull_be32(buf);
BT_DBG("OBO %u seq_zero 0x%04x ack 0x%08x", obo, seq_zero, ack);
tx = seg_tx_lookup(seq_zero, obo, rx->ctx.addr);
if (!tx) {
BT_WARN("No matching TX context for ack");
return -EINVAL;
}
if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst)) {
BT_WARN("Received ack for segments to group");
return -EINVAL;
}
*seq_auth = tx->seq_auth;
if (!ack) {
BT_WARN("SDU canceled");
seg_tx_complete(tx, -ECANCELED);
return 0;
}
if (find_msb_set(ack) - 1 > tx->seg_n) {
BT_ERR("Too large segment number in ack");
return -EINVAL;
}
k_delayed_work_cancel(&tx->retransmit);
while ((bit = find_lsb_set(ack))) {
if (tx->seg[bit - 1]) {
BT_DBG("seg %u/%u acked", bit - 1, tx->seg_n);
bt_mesh_tx_seg_lock();
seg_tx_done(tx, bit - 1);
bt_mesh_tx_seg_unlock();
}
ack &= ~BIT(bit - 1);
}
if (tx->nack_count) {
seg_tx_send_unacked(tx);
} else {
BT_DBG("SDU TX complete");
seg_tx_complete(tx, 0);
}
return 0;
}
static int trans_heartbeat(struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
uint8_t init_ttl = 0U, hops = 0U;
uint16_t feat = 0U;
if (buf->len < 3) {
BT_ERR("Too short heartbeat message (len %u)", buf->len);
return -EINVAL;
}
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() &&
rx->ctx.recv_dst != hb_sub_dst) {
BT_WARN("Ignoring heartbeat to non-subscribed destination");
return 0;
}
init_ttl = (net_buf_simple_pull_u8(buf) & 0x7f);
feat = net_buf_simple_pull_be16(buf);
hops = (init_ttl - rx->ctx.recv_ttl + 1);
BT_INFO("src 0x%04x TTL %u InitTTL %u (%u hop%s) feat 0x%04x",
rx->ctx.addr, rx->ctx.recv_ttl, init_ttl, hops,
(hops == 1U) ? "" : "s", feat);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
bt_mesh_heartbeat(rx->ctx.addr, rx->ctx.recv_dst, hops, feat);
} else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER_RECV_HB) && bt_mesh_is_provisioner_en()) {
bt_mesh_provisioner_heartbeat(rx->ctx.addr, rx->ctx.recv_dst,
init_ttl, rx->ctx.recv_ttl,
hops, feat, rx->ctx.recv_rssi);
}
return 0;
}
static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr,
struct net_buf_simple *buf, uint64_t *seq_auth)
{
uint8_t ctl_op = TRANS_CTL_OP(&hdr);
BT_DBG("OpCode 0x%02x len %u", ctl_op, buf->len);
switch (ctl_op) {