1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <stdbool.h>
11 #include <zephyr/sys/atomic.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/sys/byteorder.h>
14
15 #include <zephyr/net_buf.h>
16 #include <zephyr/bluetooth/bluetooth.h>
17 #include <zephyr/bluetooth/conn.h>
18 #include <zephyr/bluetooth/mesh.h>
19
20 #include "common/bt_str.h"
21
22 #include "crypto.h"
23 #include "mesh.h"
24 #include "net.h"
25 #include "rpl.h"
26 #include "lpn.h"
27 #include "friend.h"
28 #include "proxy.h"
29 #include "proxy_cli.h"
30 #include "transport.h"
31 #include "access.h"
32 #include "foundation.h"
33 #include "beacon.h"
34 #include "settings.h"
35 #include "prov.h"
36 #include "cfg.h"
37 #include "statistic.h"
38 #include "sar_cfg_internal.h"
39 #include "brg_cfg.h"
40
41 #define LOG_LEVEL CONFIG_BT_MESH_NET_LOG_LEVEL
42 #include <zephyr/logging/log.h>
43 LOG_MODULE_REGISTER(bt_mesh_net);
44
45 #define LOOPBACK_MAX_PDU_LEN (BT_MESH_NET_HDR_LEN + 16)
46
47 /* Seq limit after IV Update is triggered */
48 #define IV_UPDATE_SEQ_LIMIT CONFIG_BT_MESH_IV_UPDATE_SEQ_LIMIT
49
50 #define IVI(pdu) ((pdu)[0] >> 7)
51 #define NID(pdu) ((pdu)[0] & 0x7f)
52 #define CTL(pdu) ((pdu)[1] >> 7)
53 #define TTL(pdu) ((pdu)[1] & 0x7f)
54 #define SEQ(pdu) (sys_get_be24(&pdu[2]))
55 #define SRC(pdu) (sys_get_be16(&(pdu)[5]))
56 #define DST(pdu) (sys_get_be16(&(pdu)[7]))
57
58 /* Information needed for bridging the network PDUs */
59 struct pdu_ctx {
60 struct net_buf_simple *sbuf;
61 struct net_buf_simple_state *state;
62 struct bt_mesh_net_rx *rx;
63 };
64
65 /* Mesh network information for persistent storage. */
66 struct net_val {
67 uint16_t primary_addr;
68 struct bt_mesh_key dev_key;
69 } __packed;
70
71 /* Sequence number information for persistent storage. */
72 struct seq_val {
73 uint8_t val[3];
74 } __packed;
75
76 /* IV Index & IV Update information for persistent storage. */
77 struct iv_val {
78 uint32_t iv_index;
79 uint8_t iv_update:1,
80 iv_duration:7;
81 } __packed;
82
83 static struct {
84 uint32_t src : 15, /* MSb of source is always 0 */
85 seq : 17;
86 } msg_cache[CONFIG_BT_MESH_MSG_CACHE_SIZE];
87 static uint16_t msg_cache_next;
88
89 /* Singleton network context (the implementation only supports one) */
90 struct bt_mesh_net bt_mesh = {
91 .local_queue = SYS_SLIST_STATIC_INIT(&bt_mesh.local_queue),
92 .sar_tx = BT_MESH_SAR_TX_INIT,
93 .sar_rx = BT_MESH_SAR_RX_INIT,
94
95 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
96 .priv_beacon_int = 0x3c,
97 #endif
98 };
99
100 /* MshPRTv1.1: 3.11.5:
101 * "A node shall not start an IV Update procedure more often than once every 192 hours."
102 *
103 * Mark that the IV Index Recovery has been done to prevent two recoveries to be
104 * done before a normal IV Index update has been completed within 96h+96h.
105 */
106 static bool ivi_was_recovered;
107
108 struct loopback_buf {
109 sys_snode_t node;
110 struct bt_mesh_subnet *sub;
111 uint8_t len;
112 uint8_t data[LOOPBACK_MAX_PDU_LEN];
113 };
114
115 K_MEM_SLAB_DEFINE(loopback_buf_pool,
116 sizeof(struct loopback_buf),
117 CONFIG_BT_MESH_LOOPBACK_BUFS, __alignof__(struct loopback_buf));
118
119 static uint32_t dup_cache[CONFIG_BT_MESH_MSG_CACHE_SIZE];
120 static int dup_cache_next;
121
check_dup(struct net_buf_simple * data)122 static bool check_dup(struct net_buf_simple *data)
123 {
124 const uint8_t *tail = net_buf_simple_tail(data);
125 uint32_t val;
126 int i;
127
128 val = sys_get_be32(tail - 4) ^ sys_get_be32(tail - 8);
129
130 for (i = dup_cache_next; i > 0;) {
131 if (dup_cache[--i] == val) {
132 return true;
133 }
134 }
135
136 for (i = ARRAY_SIZE(dup_cache); i > dup_cache_next;) {
137 if (dup_cache[--i] == val) {
138 return true;
139 }
140 }
141
142 dup_cache_next %= ARRAY_SIZE(dup_cache);
143 dup_cache[dup_cache_next++] = val;
144
145 return false;
146 }
147
msg_cache_match(struct net_buf_simple * pdu)148 static bool msg_cache_match(struct net_buf_simple *pdu)
149 {
150 uint16_t i;
151
152 for (i = msg_cache_next; i > 0U;) {
153 if (msg_cache[--i].src == SRC(pdu->data) &&
154 msg_cache[i].seq == (SEQ(pdu->data) & BIT_MASK(17))) {
155 return true;
156 }
157 }
158
159 for (i = ARRAY_SIZE(msg_cache); i > msg_cache_next;) {
160 if (msg_cache[--i].src == SRC(pdu->data) &&
161 msg_cache[i].seq == (SEQ(pdu->data) & BIT_MASK(17))) {
162 return true;
163 }
164 }
165
166 return false;
167 }
168
msg_cache_add(struct bt_mesh_net_rx * rx)169 static void msg_cache_add(struct bt_mesh_net_rx *rx)
170 {
171 msg_cache_next %= ARRAY_SIZE(msg_cache);
172 msg_cache[msg_cache_next].src = rx->ctx.addr;
173 msg_cache[msg_cache_next].seq = rx->seq;
174 msg_cache_next++;
175 }
176
store_iv(bool only_duration)177 static void store_iv(bool only_duration)
178 {
179 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_IV_PENDING);
180
181 if (!only_duration) {
182 /* Always update Seq whenever IV changes */
183 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_SEQ_PENDING);
184 }
185 }
186
bt_mesh_net_seq_store(bool force)187 void bt_mesh_net_seq_store(bool force)
188 {
189 if (!force &&
190 CONFIG_BT_MESH_SEQ_STORE_RATE > 1 &&
191 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE)) {
192 return;
193 }
194
195 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_SEQ_PENDING);
196 }
197
bt_mesh_net_create(uint16_t idx,uint8_t flags,const struct bt_mesh_key * key,uint32_t iv_index)198 int bt_mesh_net_create(uint16_t idx, uint8_t flags, const struct bt_mesh_key *key,
199 uint32_t iv_index)
200 {
201 int err;
202
203 LOG_DBG("idx %u flags 0x%02x iv_index %u", idx, flags, iv_index);
204
205 LOG_DBG("NetKey %s", bt_hex(key, sizeof(struct bt_mesh_key)));
206
207 if (BT_MESH_KEY_REFRESH(flags)) {
208 err = bt_mesh_subnet_set(idx, BT_MESH_KR_PHASE_2, NULL, key);
209 } else {
210 err = bt_mesh_subnet_set(idx, BT_MESH_KR_NORMAL, key, NULL);
211 }
212
213 if (err) {
214 LOG_ERR("Failed creating subnet");
215 return err;
216 }
217
218 (void)memset(msg_cache, 0, sizeof(msg_cache));
219 msg_cache_next = 0U;
220
221 bt_mesh.iv_index = iv_index;
222 atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS,
223 BT_MESH_IV_UPDATE(flags));
224
225 /* If the node is added to a network when the network is in Normal
226 * operation, then it shall operate in Normal operation for at least
227 * 96 hours. If a node is added to a network while the network is
228 * in the IV Update in Progress state, then the node shall be given
229 * the new IV Index value and operate in IV Update in Progress
230 * operation without the restriction of being in this state for at
231 * least 96 hours.
232 */
233 if (BT_MESH_IV_UPDATE(flags)) {
234 bt_mesh.ivu_duration = BT_MESH_IVU_MIN_HOURS;
235 } else {
236 bt_mesh.ivu_duration = 0U;
237 }
238
239 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
240 LOG_DBG("Storing network information persistently");
241 bt_mesh_subnet_store(idx);
242 store_iv(false);
243 }
244
245 return 0;
246 }
247
248 #if defined(CONFIG_BT_MESH_IV_UPDATE_TEST)
bt_mesh_iv_update_test(bool enable)249 void bt_mesh_iv_update_test(bool enable)
250 {
251 atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_TEST, enable);
252 /* Reset the duration variable - needed for some PTS tests */
253 bt_mesh.ivu_duration = 0U;
254 }
255
bt_mesh_iv_update(void)256 bool bt_mesh_iv_update(void)
257 {
258 if (!bt_mesh_is_provisioned()) {
259 LOG_ERR("Not yet provisioned");
260 return false;
261 }
262
263 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
264 bt_mesh_net_iv_update(bt_mesh.iv_index, false);
265 } else {
266 bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
267 }
268
269 return atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
270 }
271 #endif /* CONFIG_BT_MESH_IV_UPDATE_TEST */
272
bt_mesh_net_iv_update(uint32_t iv_index,bool iv_update)273 bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update)
274 {
275 /* Check if IV index should to be recovered. */
276 if (iv_index < bt_mesh.iv_index ||
277 iv_index > bt_mesh.iv_index + 42) {
278 LOG_ERR("IV Index out of sync: 0x%08x != 0x%08x", iv_index, bt_mesh.iv_index);
279 return false;
280 }
281
282 /* Discard [iv, false] --> [iv, true] */
283 if (iv_index == bt_mesh.iv_index && iv_update) {
284 LOG_DBG("Ignore previous IV update procedure");
285 return false;
286 }
287
288 if ((iv_index > bt_mesh.iv_index + 1) ||
289 (iv_index == bt_mesh.iv_index + 1 &&
290 (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) || !iv_update))) {
291 if (ivi_was_recovered &&
292 (bt_mesh.ivu_duration < (2 * BT_MESH_IVU_MIN_HOURS))) {
293 LOG_ERR("IV Index Recovery before minimum delay");
294 return false;
295 }
296
297 /* MshPRTv1.1 allows to initiate an
298 * IV Index Recovery procedure if previous IV update has
299 * been missed. This allows the node to remain
300 * functional.
301 *
302 * Upon receiving and successfully authenticating a
303 * Secure Network beacon for a primary subnet whose
304 * IV Index is 1 or more higher than the current known IV
305 * Index, the node shall set its current IV Index and its
306 * current IV Update procedure state from the values in
307 * this Secure Network beacon.
308 */
309 LOG_WRN("Performing IV Index Recovery");
310 ivi_was_recovered = true;
311 bt_mesh_rpl_clear();
312 bt_mesh.iv_index = iv_index;
313 bt_mesh.seq = 0U;
314
315 goto do_update;
316 }
317
318 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) == iv_update) {
319 LOG_DBG("No change for IV Update procedure");
320 return false;
321 }
322
323 if (!(IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST) &&
324 atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST))) {
325 if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
326 LOG_WRN("IV Update before minimum duration");
327 return false;
328 }
329 }
330
331 /* Defer change to Normal Operation if there are pending acks */
332 if (!iv_update && bt_mesh_tx_in_progress()) {
333 LOG_WRN("IV Update deferred because of pending transfer");
334 atomic_set_bit(bt_mesh.flags, BT_MESH_IVU_PENDING);
335 return false;
336 }
337
338 if (iv_update) {
339 bt_mesh.iv_index = iv_index;
340 LOG_DBG("IV Update state entered. New index 0x%08x", bt_mesh.iv_index);
341
342 bt_mesh_rpl_reset();
343 ivi_was_recovered = false;
344 } else {
345 LOG_DBG("Normal mode entered");
346 bt_mesh.seq = 0U;
347 }
348
349 do_update:
350 atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv_update);
351 bt_mesh.ivu_duration = 0U;
352
353 k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
354
355 /* Notify other modules */
356 if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
357 bt_mesh_friend_sec_update(BT_MESH_KEY_ANY);
358 }
359
360 bt_mesh_subnet_foreach(bt_mesh_beacon_update);
361
362 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) &&
363 (bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED ||
364 bt_mesh_priv_gatt_proxy_get() == BT_MESH_PRIV_GATT_PROXY_ENABLED)) {
365 bt_mesh_proxy_beacon_send(NULL);
366 }
367
368 if (IS_ENABLED(CONFIG_BT_MESH_CDB)) {
369 bt_mesh_cdb_iv_update(iv_index, iv_update);
370 }
371
372 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
373 store_iv(false);
374 }
375
376 return true;
377 }
378
bt_mesh_next_seq(void)379 uint32_t bt_mesh_next_seq(void)
380 {
381 uint32_t seq = bt_mesh.seq++;
382
383 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
384 bt_mesh_net_seq_store(false);
385 }
386
387 if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) &&
388 bt_mesh.seq > IV_UPDATE_SEQ_LIMIT &&
389 bt_mesh_subnet_get(BT_MESH_KEY_PRIMARY)) {
390 bt_mesh_beacon_ivu_initiator(true);
391 bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
392 }
393
394 return seq;
395 }
396
bt_mesh_net_local(struct k_work * work)397 static void bt_mesh_net_local(struct k_work *work)
398 {
399 struct net_buf_simple sbuf;
400 sys_snode_t *node;
401
402 while ((node = sys_slist_get(&bt_mesh.local_queue))) {
403 struct loopback_buf *buf = CONTAINER_OF(node, struct loopback_buf, node);
404 struct bt_mesh_net_rx rx = {
405 .ctx = {
406 .net_idx = buf->sub->net_idx,
407 /* Initialize AppIdx to a sane value */
408 .app_idx = BT_MESH_KEY_UNUSED,
409 .recv_ttl = TTL(buf->data),
410 /* TTL=1 only goes to local IF */
411 .send_ttl = 1U,
412 .addr = SRC(buf->data),
413 .recv_dst = DST(buf->data),
414 .recv_rssi = 0,
415 },
416 .net_if = BT_MESH_NET_IF_LOCAL,
417 .sub = buf->sub,
418 .old_iv = (IVI(buf->data) != (bt_mesh.iv_index & 0x01)),
419 .ctl = CTL(buf->data),
420 .seq = SEQ(buf->data),
421 .new_key = SUBNET_KEY_TX_IDX(buf->sub),
422 .local_match = 1U,
423 .friend_match = 0U,
424 };
425
426 LOG_DBG("src: 0x%04x dst: 0x%04x seq 0x%06x sub %p", rx.ctx.addr, rx.ctx.addr,
427 rx.seq, buf->sub);
428
429 net_buf_simple_init_with_data(&sbuf, buf->data, buf->len);
430 (void)bt_mesh_trans_recv(&sbuf, &rx);
431 k_mem_slab_free(&loopback_buf_pool, (void *)buf);
432 }
433 }
434
net_tx_cred_get(struct bt_mesh_net_tx * tx)435 static const struct bt_mesh_net_cred *net_tx_cred_get(struct bt_mesh_net_tx *tx)
436 {
437 #if defined(CONFIG_BT_MESH_LOW_POWER)
438 if (tx->friend_cred && bt_mesh.lpn.frnd) {
439 return &bt_mesh.lpn.cred[SUBNET_KEY_TX_IDX(tx->sub)];
440 }
441 #endif
442
443 tx->friend_cred = 0U;
444 return &tx->sub->keys[SUBNET_KEY_TX_IDX(tx->sub)].msg;
445 }
446
net_header_encode(struct bt_mesh_net_tx * tx,uint8_t nid,struct net_buf_simple * buf)447 static int net_header_encode(struct bt_mesh_net_tx *tx, uint8_t nid,
448 struct net_buf_simple *buf)
449 {
450 const bool ctl = (tx->ctx->app_idx == BT_MESH_KEY_UNUSED);
451
452 if (ctl && net_buf_simple_tailroom(buf) < 8) {
453 LOG_ERR("Insufficient MIC space for CTL PDU");
454 return -EINVAL;
455 } else if (net_buf_simple_tailroom(buf) < 4) {
456 LOG_ERR("Insufficient MIC space for PDU");
457 return -EINVAL;
458 }
459
460 LOG_DBG("src 0x%04x dst 0x%04x ctl %u seq 0x%06x", tx->src, tx->ctx->addr, ctl,
461 bt_mesh.seq);
462
463 net_buf_simple_push_be16(buf, tx->ctx->addr);
464 net_buf_simple_push_be16(buf, tx->src);
465 net_buf_simple_push_be24(buf, bt_mesh_next_seq());
466
467 if (ctl) {
468 net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80);
469 } else {
470 net_buf_simple_push_u8(buf, tx->ctx->send_ttl);
471 }
472
473 net_buf_simple_push_u8(buf, (nid | (BT_MESH_NET_IVI_TX & 1) << 7));
474
475 return 0;
476 }
477
net_encrypt(struct net_buf_simple * buf,const struct bt_mesh_net_cred * cred,uint32_t iv_index,enum bt_mesh_nonce_type proxy)478 static int net_encrypt(struct net_buf_simple *buf,
479 const struct bt_mesh_net_cred *cred, uint32_t iv_index,
480 enum bt_mesh_nonce_type proxy)
481 {
482 int err;
483
484 err = bt_mesh_net_encrypt(&cred->enc, buf, iv_index, proxy);
485 if (err) {
486 return err;
487 }
488
489 return bt_mesh_net_obfuscate(buf->data, iv_index, &cred->privacy);
490 }
491
bt_mesh_net_encode(struct bt_mesh_net_tx * tx,struct net_buf_simple * buf,enum bt_mesh_nonce_type type)492 int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
493 enum bt_mesh_nonce_type type)
494 {
495 const struct bt_mesh_net_cred *cred;
496 int err;
497
498 cred = net_tx_cred_get(tx);
499 err = net_header_encode(tx, cred->nid, buf);
500 if (err) {
501 return err;
502 }
503
504 return net_encrypt(buf, cred, BT_MESH_NET_IVI_TX, type);
505 }
506
net_loopback(const struct bt_mesh_net_tx * tx,const uint8_t * data,size_t len)507 static int net_loopback(const struct bt_mesh_net_tx *tx, const uint8_t *data,
508 size_t len)
509 {
510 int err;
511 struct loopback_buf *buf;
512
513 err = k_mem_slab_alloc(&loopback_buf_pool, (void **)&buf, K_NO_WAIT);
514 if (err) {
515 LOG_WRN("Unable to allocate loopback");
516 return -ENOMEM;
517 }
518
519 buf->sub = tx->sub;
520
521 (void)memcpy(buf->data, data, len);
522 buf->len = len;
523
524 sys_slist_append(&bt_mesh.local_queue, &buf->node);
525
526 k_work_submit(&bt_mesh.local_work);
527
528 return 0;
529 }
530
bt_mesh_net_send(struct bt_mesh_net_tx * tx,struct bt_mesh_adv * adv,const struct bt_mesh_send_cb * cb,void * cb_data)531 int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct bt_mesh_adv *adv,
532 const struct bt_mesh_send_cb *cb, void *cb_data)
533 {
534 const struct bt_mesh_net_cred *cred;
535 int err;
536
537 LOG_DBG("src 0x%04x dst 0x%04x len %u headroom %zu tailroom %zu", tx->src, tx->ctx->addr,
538 adv->b.len, net_buf_simple_headroom(&adv->b), net_buf_simple_tailroom(&adv->b));
539 LOG_DBG("Payload len %u: %s", adv->b.len, bt_hex(adv->b.data, adv->b.len));
540 LOG_DBG("Seq 0x%06x", bt_mesh.seq);
541
542 cred = net_tx_cred_get(tx);
543 err = net_header_encode(tx, cred->nid, &adv->b);
544 if (err) {
545 goto done;
546 }
547
548 /* Deliver to local network interface if necessary */
549 if (bt_mesh_fixed_group_match(tx->ctx->addr) ||
550 bt_mesh_has_addr(tx->ctx->addr)) {
551 err = net_loopback(tx, adv->b.data, adv->b.len);
552
553 /* Local unicast messages should not go out to network */
554 if (BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr) ||
555 tx->ctx->send_ttl == 1U) {
556 if (!err) {
557 send_cb_finalize(cb, cb_data);
558 }
559
560 goto done;
561 }
562 }
563
564 /* MshPRTv1.1: 3.4.5.2: "The output filter of the interface connected to
565 * advertising or GATT bearers shall drop all messages with TTL value
566 * set to 1." If a TTL=1 packet wasn't for a local interface, it is
567 * invalid.
568 */
569 if (tx->ctx->send_ttl == 1U) {
570 err = -EINVAL;
571 goto done;
572 }
573
574 err = net_encrypt(&adv->b, cred, BT_MESH_NET_IVI_TX, BT_MESH_NONCE_NETWORK);
575 if (err) {
576 goto done;
577 }
578
579 adv->ctx.cb = cb;
580 adv->ctx.cb_data = cb_data;
581
582 /* Deliver to GATT Proxy Clients if necessary. */
583 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
584 (void)bt_mesh_proxy_relay(adv, tx->ctx->addr);
585 }
586
587 /* Deliver to GATT Proxy Servers if necessary. */
588 if (IS_ENABLED(CONFIG_BT_MESH_PROXY_CLIENT)) {
589 (void)bt_mesh_proxy_cli_relay(adv);
590 }
591
592 bt_mesh_adv_send(adv, cb, cb_data);
593
594 done:
595 bt_mesh_adv_unref(adv);
596 return err;
597 }
598
bt_mesh_net_loopback_clear(uint16_t net_idx)599 void bt_mesh_net_loopback_clear(uint16_t net_idx)
600 {
601 sys_slist_t new_list;
602 sys_snode_t *node;
603
604 LOG_DBG("0x%04x", net_idx);
605
606 sys_slist_init(&new_list);
607
608 while ((node = sys_slist_get(&bt_mesh.local_queue))) {
609 struct loopback_buf *buf = CONTAINER_OF(node, struct loopback_buf, node);
610
611 if (net_idx == BT_MESH_KEY_ANY || net_idx == buf->sub->net_idx) {
612 LOG_DBG("Dropped 0x%06x", SEQ(buf->data));
613 k_mem_slab_free(&loopback_buf_pool, (void *)buf);
614 } else {
615 sys_slist_append(&new_list, &buf->node);
616 }
617 }
618
619 bt_mesh.local_queue = new_list;
620 }
621
net_decrypt(struct bt_mesh_net_rx * rx,struct net_buf_simple * in,struct net_buf_simple * out,const struct bt_mesh_net_cred * cred)622 static bool net_decrypt(struct bt_mesh_net_rx *rx, struct net_buf_simple *in,
623 struct net_buf_simple *out,
624 const struct bt_mesh_net_cred *cred)
625 {
626 bool proxy = (rx->net_if == BT_MESH_NET_IF_PROXY_CFG);
627
628 if (NID(in->data) != cred->nid) {
629 return false;
630 }
631
632 LOG_DBG("NID 0x%02x", NID(in->data));
633 LOG_DBG("IVI %u net->iv_index 0x%08x", IVI(in->data), bt_mesh.iv_index);
634
635 rx->old_iv = (IVI(in->data) != (bt_mesh.iv_index & 0x01));
636
637 net_buf_simple_reset(out);
638 net_buf_simple_add_mem(out, in->data, in->len);
639
640 if (bt_mesh_net_obfuscate(out->data, BT_MESH_NET_IVI_RX(rx),
641 &cred->privacy)) {
642 return false;
643 }
644
645 rx->ctx.addr = SRC(out->data);
646 if (!BT_MESH_ADDR_IS_UNICAST(rx->ctx.addr)) {
647 LOG_DBG("Ignoring non-unicast src addr 0x%04x", rx->ctx.addr);
648 return false;
649 }
650
651 if (bt_mesh_has_addr(rx->ctx.addr)) {
652 LOG_DBG("Dropping locally originated packet");
653 return false;
654 }
655
656 if (rx->net_if == BT_MESH_NET_IF_ADV && msg_cache_match(out)) {
657 LOG_DBG("Duplicate found in Network Message Cache");
658 return false;
659 }
660
661 LOG_DBG("src 0x%04x", rx->ctx.addr);
662
663 return bt_mesh_net_decrypt(&cred->enc, out, BT_MESH_NET_IVI_RX(rx),
664 proxy) == 0;
665 }
666
667 /* Relaying from advertising to the advertising bearer should only happen
668 * if the Relay state is set to enabled. Locally originated packets always
669 * get sent to the advertising bearer. If the packet came in through GATT,
670 * then we should only relay it if the GATT Proxy state is enabled.
671 */
relay_to_adv(enum bt_mesh_net_if net_if)672 static bool relay_to_adv(enum bt_mesh_net_if net_if)
673 {
674 switch (net_if) {
675 case BT_MESH_NET_IF_ADV:
676 return (bt_mesh_relay_get() == BT_MESH_RELAY_ENABLED);
677 case BT_MESH_NET_IF_PROXY:
678 return (bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED) ||
679 (bt_mesh_priv_gatt_proxy_get() == BT_MESH_PRIV_GATT_PROXY_ENABLED);
680 default:
681 return false;
682 }
683 }
684
bt_mesh_net_relay(struct net_buf_simple * sbuf,struct bt_mesh_net_rx * rx,bool bridge)685 static void bt_mesh_net_relay(struct net_buf_simple *sbuf, struct bt_mesh_net_rx *rx, bool bridge)
686 {
687 const struct bt_mesh_net_cred *cred;
688 struct bt_mesh_adv *adv;
689 uint8_t transmit;
690
691 if (rx->ctx.recv_ttl <= 1U) {
692 return;
693 }
694
695 if (rx->net_if == BT_MESH_NET_IF_ADV && !rx->friend_cred && !bridge &&
696 bt_mesh_relay_get() != BT_MESH_RELAY_ENABLED &&
697 bt_mesh_gatt_proxy_get() != BT_MESH_GATT_PROXY_ENABLED &&
698 bt_mesh_priv_gatt_proxy_get() != BT_MESH_PRIV_GATT_PROXY_ENABLED) {
699 return;
700 }
701
702 LOG_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, rx->ctx.recv_dst);
703
704 /* The Relay Retransmit state is only applied to adv-adv relaying.
705 * Anything else (like GATT to adv, or locally originated packets)
706 * use the Network Transmit state.
707 */
708 if (rx->net_if == BT_MESH_NET_IF_ADV && !rx->friend_cred && !bridge) {
709 transmit = bt_mesh_relay_retransmit_get();
710 } else {
711 transmit = bt_mesh_net_transmit_get();
712 }
713
714 adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_RELAY,
715 transmit, K_NO_WAIT);
716 if (!adv) {
717 LOG_DBG("Out of relay advs");
718 return;
719 }
720
721 /* Leave CTL bit intact */
722 sbuf->data[1] &= 0x80;
723 sbuf->data[1] |= rx->ctx.recv_ttl - 1U;
724
725 net_buf_simple_add_mem(&adv->b, sbuf->data, sbuf->len);
726
727 cred = &rx->sub->keys[SUBNET_KEY_TX_IDX(rx->sub)].msg;
728
729 LOG_DBG("Relaying packet. TTL is now %u", TTL(adv->b.data));
730
731 /* Update NID if RX, RX was with friend credentials or when bridging the message */
732 if (rx->friend_cred || bridge) {
733 adv->b.data[0] &= 0x80; /* Clear everything except IVI */
734 adv->b.data[0] |= cred->nid;
735 }
736
737 /* We re-encrypt and obfuscate using the received IVI rather than
738 * the normal TX IVI (which may be different) since the transport
739 * layer nonce includes the IVI.
740 */
741 if (net_encrypt(&adv->b, cred, BT_MESH_NET_IVI_RX(rx), BT_MESH_NONCE_NETWORK)) {
742 LOG_ERR("Re-encrypting failed");
743 goto done;
744 }
745
746 /* When the Friend node relays message for lpn, the message will be
747 * retransmitted using the managed flooding security credentials and
748 * the Network PDU shall be retransmitted to all network interfaces.
749 */
750 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) &&
751 (rx->friend_cred ||
752 bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED ||
753 bt_mesh_priv_gatt_proxy_get() == BT_MESH_PRIV_GATT_PROXY_ENABLED)) {
754 bt_mesh_proxy_relay(adv, rx->ctx.recv_dst);
755 }
756
757 if (relay_to_adv(rx->net_if) || rx->friend_cred || bridge) {
758 bt_mesh_adv_send(adv, NULL, NULL);
759 }
760
761 done:
762 bt_mesh_adv_unref(adv);
763 }
764
765 #if IS_ENABLED(CONFIG_BT_MESH_BRG_CFG_SRV)
find_subnet_cb(struct bt_mesh_subnet * sub,void * cb_data)766 static bool find_subnet_cb(struct bt_mesh_subnet *sub, void *cb_data)
767 {
768 uint16_t *net_idx = cb_data;
769
770 return sub->net_idx == *net_idx;
771 }
772
bt_mesh_sbr_check_cb(uint16_t new_net_idx,void * user_data)773 static void bt_mesh_sbr_check_cb(uint16_t new_net_idx, void *user_data)
774 {
775 struct pdu_ctx *ctx = (struct pdu_ctx *)user_data;
776
777 if (new_net_idx < BT_MESH_BRG_CFG_NETIDX_NOMATCH) {
778 struct bt_mesh_subnet *subnet = bt_mesh_subnet_find(find_subnet_cb, &new_net_idx);
779
780 if (!subnet) {
781 LOG_ERR("Failed to find subnet 0x%04x", new_net_idx);
782 return;
783 }
784
785 ctx->rx->sub = subnet;
786 ctx->rx->ctx.net_idx = new_net_idx;
787
788 net_buf_simple_restore(ctx->sbuf, ctx->state);
789 bt_mesh_net_relay(ctx->sbuf, ctx->rx, true);
790 }
791 }
792 #endif
793
bt_mesh_net_header_parse(struct net_buf_simple * buf,struct bt_mesh_net_rx * rx)794 void bt_mesh_net_header_parse(struct net_buf_simple *buf,
795 struct bt_mesh_net_rx *rx)
796 {
797 rx->old_iv = (IVI(buf->data) != (bt_mesh.iv_index & 0x01));
798 rx->ctl = CTL(buf->data);
799 rx->ctx.recv_ttl = TTL(buf->data);
800 rx->seq = SEQ(buf->data);
801 rx->ctx.addr = SRC(buf->data);
802 rx->ctx.recv_dst = DST(buf->data);
803 }
804
bt_mesh_net_decode(struct net_buf_simple * in,enum bt_mesh_net_if net_if,struct bt_mesh_net_rx * rx,struct net_buf_simple * out)805 int bt_mesh_net_decode(struct net_buf_simple *in, enum bt_mesh_net_if net_if,
806 struct bt_mesh_net_rx *rx, struct net_buf_simple *out)
807 {
808 if (in->len < BT_MESH_NET_MIN_PDU_LEN) {
809 LOG_WRN("Dropping too short mesh packet (len %u)", in->len);
810 LOG_WRN("%s", bt_hex(in->data, in->len));
811 return -EINVAL;
812 }
813
814 if (in->len > BT_MESH_NET_MAX_PDU_LEN) {
815 LOG_WRN("Dropping too long mesh packet (len %u)", in->len);
816 return -EINVAL;
817 }
818
819 if (net_if == BT_MESH_NET_IF_ADV && check_dup(in)) {
820 return -EINVAL;
821 }
822
823 LOG_DBG("%u bytes: %s", in->len, bt_hex(in->data, in->len));
824
825 rx->net_if = net_if;
826
827 if (!bt_mesh_net_cred_find(rx, in, out, net_decrypt)) {
828 LOG_DBG("Unable to find matching net for packet");
829 return -ENOENT;
830 }
831
832 /* Initialize AppIdx to a sane value */
833 rx->ctx.app_idx = BT_MESH_KEY_UNUSED;
834
835 rx->ctx.recv_ttl = TTL(out->data);
836
837 /* Default to responding with TTL 0 for non-routed messages */
838 if (rx->ctx.recv_ttl == 0U) {
839 rx->ctx.send_ttl = 0U;
840 } else {
841 rx->ctx.send_ttl = BT_MESH_TTL_DEFAULT;
842 }
843
844 rx->ctl = CTL(out->data);
845 rx->seq = SEQ(out->data);
846 rx->ctx.recv_dst = DST(out->data);
847
848 LOG_DBG("Decryption successful. Payload len %u", out->len);
849
850 if (net_if != BT_MESH_NET_IF_PROXY_CFG &&
851 rx->ctx.recv_dst == BT_MESH_ADDR_UNASSIGNED) {
852 LOG_ERR("Destination address is unassigned; dropping packet");
853 return -EBADMSG;
854 }
855
856 LOG_DBG("src 0x%04x dst 0x%04x ttl %u", rx->ctx.addr, rx->ctx.recv_dst, rx->ctx.recv_ttl);
857 LOG_DBG("PDU: %s", bt_hex(out->data, out->len));
858
859 msg_cache_add(rx);
860
861 return 0;
862 }
863
bt_mesh_net_recv(struct net_buf_simple * data,int8_t rssi,enum bt_mesh_net_if net_if)864 void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,
865 enum bt_mesh_net_if net_if)
866 {
867 NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_NET_MAX_PDU_LEN);
868 struct bt_mesh_net_rx rx = { .ctx.recv_rssi = rssi };
869 struct net_buf_simple_state state;
870 int err;
871
872 LOG_DBG("rssi %d net_if %u", rssi, net_if);
873
874 if (!bt_mesh_is_provisioned()) {
875 return;
876 }
877
878 if (bt_mesh_net_decode(data, net_if, &rx, &buf)) {
879 return;
880 }
881
882 if (IS_ENABLED(CONFIG_BT_MESH_STATISTIC)) {
883 bt_mesh_stat_rx(net_if);
884 }
885
886 /* Save the state so the buffer can later be relayed */
887 net_buf_simple_save(&buf, &state);
888
889 rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) ||
890 bt_mesh_has_addr(rx.ctx.recv_dst));
891
892 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) &&
893 net_if == BT_MESH_NET_IF_PROXY) {
894 bt_mesh_proxy_addr_add(data, rx.ctx.addr);
895
896 if (bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_DISABLED &&
897 bt_mesh_priv_gatt_proxy_get() == BT_MESH_PRIV_GATT_PROXY_DISABLED &&
898 !rx.local_match) {
899 LOG_INF("Proxy is disabled; ignoring message");
900 return;
901 }
902 }
903
904 err = bt_mesh_trans_recv(&buf, &rx);
905 if (err == -EAGAIN) {
906 /* The transport layer has indicated that it has rejected the message,
907 * but would like to see it again if it is received in the future.
908 * This can happen if a message is received when the device is in
909 * Low Power mode, but the message was not encrypted with the friend
910 * credentials. Remove it from the message cache so that we accept
911 * it again in the future.
912 */
913 LOG_WRN("Removing rejected message from Network Message Cache");
914 /* Rewind the next index now that we're not using this entry */
915 msg_cache[--msg_cache_next].src = BT_MESH_ADDR_UNASSIGNED;
916 dup_cache[--dup_cache_next] = 0;
917 return;
918 } else if (err == -EBADMSG) {
919 LOG_DBG("Not relaying message rejected by the Transport layer");
920 return;
921 }
922
923 /* Relay if this was a group/virtual address, or if the destination
924 * was neither a local element nor an LPN we're Friends for.
925 */
926 if (!BT_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) ||
927 (!rx.local_match && !rx.friend_match)) {
928 net_buf_simple_restore(&buf, &state);
929 bt_mesh_net_relay(&buf, &rx, false);
930 }
931
932 #if IS_ENABLED(CONFIG_BT_MESH_BRG_CFG_SRV)
933 struct pdu_ctx tx_ctx = {
934 .sbuf = &buf,
935 .state = &state,
936 .rx = &rx,
937 };
938
939 /* Bridge the traffic if enabled */
940 if (!bt_mesh_brg_cfg_enable_get()) {
941 return;
942 }
943
944 if (bt_mesh_rpl_check(&rx, NULL, true)) {
945 return;
946 }
947
948 bt_mesh_brg_cfg_tbl_foreach_subnet(rx.ctx.addr, rx.ctx.recv_dst, rx.ctx.net_idx,
949 bt_mesh_sbr_check_cb, &tx_ctx);
950 #endif
951 }
952
ivu_refresh(struct k_work * work)953 static void ivu_refresh(struct k_work *work)
954 {
955 if (!bt_mesh_is_provisioned()) {
956 return;
957 }
958
959 bt_mesh.ivu_duration = MIN(UINT8_MAX,
960 bt_mesh.ivu_duration + BT_MESH_IVU_HOURS);
961
962 LOG_DBG("%s for %u hour%s",
963 atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) ? "IVU in Progress"
964 : "IVU Normal mode",
965 bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1U ? "" : "s");
966
967 if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
968 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
969 store_iv(true);
970 }
971
972 goto end;
973 }
974
975 /* Because the beacon may be cached, iv update or iv recovery
976 * cannot be performed after 96 hours or 192 hours.
977 * So we need clear beacon cache.
978 */
979 if (!(bt_mesh.ivu_duration % BT_MESH_IVU_MIN_HOURS)) {
980 bt_mesh_subnet_foreach(bt_mesh_beacon_cache_clear);
981 }
982
983 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
984 bt_mesh_beacon_ivu_initiator(true);
985 bt_mesh_net_iv_update(bt_mesh.iv_index, false);
986 } else if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
987 store_iv(true);
988 }
989
990 end:
991 k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
992 }
993
bt_mesh_net_init(void)994 void bt_mesh_net_init(void)
995 {
996 k_work_init_delayable(&bt_mesh.ivu_timer, ivu_refresh);
997
998 k_work_init(&bt_mesh.local_work, bt_mesh_net_local);
999 }
1000
net_set(const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)1001 static int net_set(const char *name, size_t len_rd, settings_read_cb read_cb,
1002 void *cb_arg)
1003 {
1004 struct net_val net;
1005 struct bt_mesh_key key;
1006 int err;
1007
1008 if (len_rd == 0) {
1009 LOG_DBG("val (null)");
1010
1011 bt_mesh_comp_unprovision();
1012 bt_mesh_key_destroy(&bt_mesh.dev_key);
1013 memset(&bt_mesh.dev_key, 0, sizeof(struct bt_mesh_key));
1014 return 0;
1015 }
1016
1017 err = bt_mesh_settings_set(read_cb, cb_arg, &net, sizeof(net));
1018 if (err) {
1019 LOG_ERR("Failed to set \'net\'");
1020 return err;
1021 }
1022
1023 /* One extra copying since net.dev_key is from packed structure
1024 * and might be unaligned.
1025 */
1026 memcpy(&key, &net.dev_key, sizeof(struct bt_mesh_key));
1027
1028 bt_mesh_key_assign(&bt_mesh.dev_key, &key);
1029 bt_mesh_comp_provision(net.primary_addr);
1030
1031 LOG_DBG("Provisioned with primary address 0x%04x", net.primary_addr);
1032 LOG_DBG("Recovered DevKey %s", bt_hex(&bt_mesh.dev_key, sizeof(struct bt_mesh_key)));
1033
1034 return 0;
1035 }
1036
1037 BT_MESH_SETTINGS_DEFINE(net, "Net", net_set);
1038
iv_set(const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)1039 static int iv_set(const char *name, size_t len_rd, settings_read_cb read_cb,
1040 void *cb_arg)
1041 {
1042 struct iv_val iv;
1043 int err;
1044
1045 if (len_rd == 0) {
1046 LOG_DBG("IV deleted");
1047
1048 bt_mesh.iv_index = 0U;
1049 atomic_clear_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
1050 return 0;
1051 }
1052
1053 err = bt_mesh_settings_set(read_cb, cb_arg, &iv, sizeof(iv));
1054 if (err) {
1055 LOG_ERR("Failed to set \'iv\'");
1056 return err;
1057 }
1058
1059 bt_mesh.iv_index = iv.iv_index;
1060 atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv.iv_update);
1061 bt_mesh.ivu_duration = iv.iv_duration;
1062
1063 LOG_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours", iv.iv_index, iv.iv_update,
1064 iv.iv_duration);
1065
1066 return 0;
1067 }
1068
1069 BT_MESH_SETTINGS_DEFINE(iv, "IV", iv_set);
1070
seq_set(const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)1071 static int seq_set(const char *name, size_t len_rd, settings_read_cb read_cb,
1072 void *cb_arg)
1073 {
1074 struct seq_val seq;
1075 int err;
1076
1077 if (len_rd == 0) {
1078 LOG_DBG("val (null)");
1079
1080 bt_mesh.seq = 0U;
1081 return 0;
1082 }
1083
1084 err = bt_mesh_settings_set(read_cb, cb_arg, &seq, sizeof(seq));
1085 if (err) {
1086 LOG_ERR("Failed to set \'seq\'");
1087 return err;
1088 }
1089
1090 bt_mesh.seq = sys_get_le24(seq.val);
1091
1092 if (CONFIG_BT_MESH_SEQ_STORE_RATE > 0) {
1093 /* Make sure we have a large enough sequence number. We
1094 * subtract 1 so that the first transmission causes a write
1095 * to the settings storage.
1096 */
1097 bt_mesh.seq += (CONFIG_BT_MESH_SEQ_STORE_RATE -
1098 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE));
1099 bt_mesh.seq--;
1100 }
1101
1102 LOG_DBG("Sequence Number 0x%06x", bt_mesh.seq);
1103
1104 return 0;
1105 }
1106
1107 BT_MESH_SETTINGS_DEFINE(seq, "Seq", seq_set);
1108
1109 #if defined(CONFIG_BT_MESH_RPR_SRV)
dev_key_cand_set(const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)1110 static int dev_key_cand_set(const char *name, size_t len_rd, settings_read_cb read_cb,
1111 void *cb_arg)
1112 { int err;
1113
1114 if (len_rd < 16) {
1115 return -EINVAL;
1116 }
1117
1118 err = bt_mesh_settings_set(read_cb, cb_arg, &bt_mesh.dev_key_cand,
1119 sizeof(struct bt_mesh_key));
1120 if (!err) {
1121 LOG_DBG("DevKey candidate recovered from storage");
1122 atomic_set_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND);
1123 }
1124
1125 return err;
1126 }
1127
1128 BT_MESH_SETTINGS_DEFINE(dev_key, "DevKeyC", dev_key_cand_set);
1129 #endif
1130
bt_mesh_net_pending_dev_key_cand_store(void)1131 void bt_mesh_net_pending_dev_key_cand_store(void)
1132 {
1133 #if defined(CONFIG_BT_MESH_RPR_SRV)
1134 int err;
1135
1136 if (atomic_test_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND)) {
1137 err = settings_save_one("bt/mesh/DevKeyC", &bt_mesh.dev_key_cand,
1138 sizeof(struct bt_mesh_key));
1139 } else {
1140 err = settings_delete("bt/mesh/DevKeyC");
1141 }
1142
1143 if (err) {
1144 LOG_ERR("Failed to update DevKey candidate value");
1145 } else {
1146 LOG_DBG("Stored DevKey candidate value");
1147 }
1148 #endif
1149 }
1150
bt_mesh_net_dev_key_cand_store(void)1151 void bt_mesh_net_dev_key_cand_store(void)
1152 {
1153 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_DEV_KEY_CAND_PENDING);
1154 }
1155
clear_iv(void)1156 static void clear_iv(void)
1157 {
1158 int err;
1159
1160 err = settings_delete("bt/mesh/IV");
1161 if (err) {
1162 LOG_ERR("Failed to clear IV");
1163 } else {
1164 LOG_DBG("Cleared IV");
1165 }
1166 }
1167
store_pending_iv(void)1168 static void store_pending_iv(void)
1169 {
1170 struct iv_val iv;
1171 int err;
1172
1173 iv.iv_index = bt_mesh.iv_index;
1174 iv.iv_update = atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
1175 iv.iv_duration = bt_mesh.ivu_duration;
1176
1177 err = settings_save_one("bt/mesh/IV", &iv, sizeof(iv));
1178 if (err) {
1179 LOG_ERR("Failed to store IV value");
1180 } else {
1181 LOG_DBG("Stored IV value");
1182 }
1183 }
1184
bt_mesh_net_pending_iv_store(void)1185 void bt_mesh_net_pending_iv_store(void)
1186 {
1187 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1188 store_pending_iv();
1189 } else {
1190 clear_iv();
1191 }
1192 }
1193
clear_net(void)1194 static void clear_net(void)
1195 {
1196 int err;
1197
1198 err = settings_delete("bt/mesh/Net");
1199 if (err) {
1200 LOG_ERR("Failed to clear Network");
1201 } else {
1202 LOG_DBG("Cleared Network");
1203 }
1204 }
1205
store_pending_net(void)1206 static void store_pending_net(void)
1207 {
1208 struct net_val net;
1209 int err;
1210
1211 LOG_DBG("addr 0x%04x DevKey %s", bt_mesh_primary_addr(),
1212 bt_hex(&bt_mesh.dev_key, sizeof(struct bt_mesh_key)));
1213
1214 net.primary_addr = bt_mesh_primary_addr();
1215 memcpy(&net.dev_key, &bt_mesh.dev_key, sizeof(struct bt_mesh_key));
1216
1217 err = settings_save_one("bt/mesh/Net", &net, sizeof(net));
1218 if (err) {
1219 LOG_ERR("Failed to store Network value");
1220 } else {
1221 LOG_DBG("Stored Network value");
1222 }
1223 }
1224
bt_mesh_net_pending_net_store(void)1225 void bt_mesh_net_pending_net_store(void)
1226 {
1227 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1228 store_pending_net();
1229 } else {
1230 clear_net();
1231 }
1232 }
1233
bt_mesh_net_pending_seq_store(void)1234 void bt_mesh_net_pending_seq_store(void)
1235 {
1236 struct seq_val seq;
1237 int err;
1238
1239 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1240 sys_put_le24(bt_mesh.seq, seq.val);
1241
1242 err = settings_save_one("bt/mesh/Seq", &seq, sizeof(seq));
1243 if (err) {
1244 LOG_ERR("Failed to stor Seq value");
1245 } else {
1246 LOG_DBG("Stored Seq value");
1247 }
1248 } else {
1249 err = settings_delete("bt/mesh/Seq");
1250 if (err) {
1251 LOG_ERR("Failed to clear Seq value");
1252 } else {
1253 LOG_DBG("Cleared Seq value");
1254 }
1255 }
1256 }
1257
bt_mesh_net_store(void)1258 void bt_mesh_net_store(void)
1259 {
1260 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_NET_PENDING);
1261 }
1262
bt_mesh_net_clear(void)1263 void bt_mesh_net_clear(void)
1264 {
1265 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_NET_PENDING);
1266 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_IV_PENDING);
1267 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
1268 bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_SEQ_PENDING);
1269 }
1270
bt_mesh_net_settings_commit(void)1271 void bt_mesh_net_settings_commit(void)
1272 {
1273 if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
1274 k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
1275 }
1276 }
1277