From ef6667aacde2711dfaf88542ebc03c5657379f5e Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Fri, 12 Jan 2024 11:30:23 +0100 Subject: [PATCH 1/2] [nrf fromtree] Bluetooth: Mesh: Fix dereferencing before null pointer check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't dereference pointer until it is checked on NULL. Fixes: #66805 Coverity-CID: 338098 Signed-off-by: Pavel Vasilyev (cherry picked from commit 27b1f4eb7f9d887d8fda81cbdc52f651223e75e9) Signed-off-by: Håvard Reierstad --- subsys/bluetooth/mesh/delayable_msg.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/subsys/bluetooth/mesh/delayable_msg.c b/subsys/bluetooth/mesh/delayable_msg.c index 5fa43205d63..a1a247bb9c9 100644 --- a/subsys/bluetooth/mesh/delayable_msg.c +++ b/subsys/bluetooth/mesh/delayable_msg.c @@ -167,13 +167,15 @@ static bool push_msg_from_delayable_msgs(void) sys_snode_t *node; struct delayable_msg_chunk *chunk; struct delayable_msg_ctx *msg = peek_pending_msg(); - uint16_t len = msg->len; + uint16_t len; int err; if (!msg) { return false; } + len = msg->len; + NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_TX_SDU_MAX); SYS_SLIST_FOR_EACH_NODE(&msg->chunks, node) { From 48d3ecd4f36940c157b08e2249bd2518b3cf26cd Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Tue, 9 Jan 2024 15:22:29 +0100 Subject: [PATCH 2/2] [nrf fromtree] Bluetooth: Mesh: enable access responses randomization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable by default the access layer responses random delays. Commit also adapts all mesh models, samples and babblesim tests to use random delay functionality correctly. Signed-off-by: Aleksandr Khromykh (cherry picked from commit 4e7d64b1b45247de1e1d71a4402f5637b0fd0afe) Signed-off-by: Håvard Reierstad --- subsys/bluetooth/mesh/Kconfig | 15 +++++++++++++-- subsys/bluetooth/mesh/access.c | 10 ++++++++-- tests/bsim/bluetooth/mesh/src/test_access.c | 11 ++++++++--- tests/bsim/bluetooth/mesh/src/test_persistence.c | 6 ------ tests/bsim/bluetooth/mesh/src/test_provision.c | 10 ---------- .../access/access_transmit_delayable.sh | 2 +- .../priv_beacon/proxy_adv_multi_subnet_coex.sh | 4 ++++ 7 files changed, 34 insertions(+), 24 deletions(-) diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index af6af8c44dc..555aac45bc0 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -639,6 +639,7 @@ config BT_MESH_LABEL_NO_RECOVER menuconfig BT_MESH_ACCESS_DELAYABLE_MSG bool "Access layer tx delayable message" + default y help Enable following of the message transmitting recommendations, the Access layer specification. The recommendations are optional. @@ -647,6 +648,16 @@ menuconfig BT_MESH_ACCESS_DELAYABLE_MSG if BT_MESH_ACCESS_DELAYABLE_MSG +config BT_MESH_ACCESS_DELAYABLE_MSG_CTX_ENABLED + bool "The delayable message in the notification message context" + default y + help + Controls whether the delayable message feature is enabled by default in + the message context of the opcode notifications. This allows the server part of any + model to not bother about additional context configuration to enable the delayable message. + Note that if this is disabled then all foundation models stop using the delayable message + functionality. + config BT_MESH_ACCESS_DELAYABLE_MSG_COUNT int "Number of simultaneously delayed messages" default 4 @@ -657,14 +668,14 @@ config BT_MESH_ACCESS_DELAYABLE_MSG_COUNT config BT_MESH_ACCESS_DELAYABLE_MSG_CHUNK_SIZE int "Maximum delayable message storage chunk" - default 20 + default 10 help Size of memory that Access layer uses to split model message to. It allocates a sufficient number of these chunks from the pool to store the full model payload. config BT_MESH_ACCESS_DELAYABLE_MSG_CHUNK_COUNT int "Maximum number of available chunks" - default 20 + default 40 help The maximum number of available chunks the Access layer allocates to store model payload. It is recommended to keep chunk size equal to the reasonable small value to prevent diff --git a/subsys/bluetooth/mesh/access.c b/subsys/bluetooth/mesh/access.c index c3ef6a40554..23fbb8e82c2 100644 --- a/subsys/bluetooth/mesh/access.c +++ b/subsys/bluetooth/mesh/access.c @@ -922,7 +922,7 @@ static void mod_publish(struct k_work *work) return; } - LOG_DBG("%u", k_uptime_get_32()); + LOG_DBG("timestamp: %u", k_uptime_get_32()); if (pub->count) { pub->count--; @@ -1504,6 +1504,10 @@ static int element_model_recv(struct bt_mesh_msg_ctx *ctx, struct net_buf_simple return ACCESS_STATUS_MESSAGE_NOT_UNDERSTOOD; } + if (IS_ENABLED(CONFIG_BT_MESH_ACCESS_DELAYABLE_MSG_CTX_ENABLED)) { + ctx->rnd_delay = true; + } + net_buf_simple_save(buf, &state); err = op->func(model, ctx, buf); net_buf_simple_restore(buf, &state); @@ -1578,7 +1582,9 @@ int bt_mesh_model_send(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx } #if defined CONFIG_BT_MESH_ACCESS_DELAYABLE_MSG - if (ctx->rnd_delay) { + /* No sense to use delayable message for unicast loopback. */ + if (ctx->rnd_delay && + !(bt_mesh_has_addr(ctx->addr) && BT_MESH_ADDR_IS_UNICAST(ctx->addr))) { return bt_mesh_delayable_msg_manage(ctx, msg, bt_mesh_model_elem(model)->rt->addr, cb, cb_data); } diff --git a/tests/bsim/bluetooth/mesh/src/test_access.c b/tests/bsim/bluetooth/mesh/src/test_access.c index 5cb7d2e28e1..6967aa6d71c 100644 --- a/tests/bsim/bluetooth/mesh/src/test_access.c +++ b/tests/bsim/bluetooth/mesh/src/test_access.c @@ -102,11 +102,15 @@ static bool publish_allow; static int model1_update(const struct bt_mesh_model *model) { - model->pub->msg->data[1]++; + if (!publish_allow) { + return -1; + } + model->pub->msg->data[1]++; LOG_DBG("New pub: n: %d t: %d", model->pub->msg->data[1], k_uptime_get_32()); + k_sem_give(&publish_sem); - return publish_allow ? k_sem_give(&publish_sem), 0 : -1; + return 0; } static int test_msgf_handler(const struct bt_mesh_model *model, @@ -604,7 +608,8 @@ static void recv_delayable_check(int32_t interval, uint8_t count) LOG_DBG("Recv time: %d delta: %d boundaries: %d/%d", (int32_t)timestamp, time_delta, lower_boundary, upper_boundary); - ASSERT_IN_RANGE(time_delta, lower_boundary, upper_boundary + RX_JITTER_MAX); + ASSERT_IN_RANGE(time_delta, lower_boundary - RX_JITTER_MAX, + upper_boundary + RX_JITTER_MAX); } } diff --git a/tests/bsim/bluetooth/mesh/src/test_persistence.c b/tests/bsim/bluetooth/mesh/src/test_persistence.c index ec7d838d26a..4bf1f9faa93 100644 --- a/tests/bsim/bluetooth/mesh/src/test_persistence.c +++ b/tests/bsim/bluetooth/mesh/src/test_persistence.c @@ -455,12 +455,6 @@ static void provisioner_setup(void) FAIL("Failed to add test_netkey (err: %d, status: %d)", err, status); } - err = bt_mesh_cfg_cli_net_transmit_set(test_netkey_idx, TEST_PROV_ADDR, - BT_MESH_TRANSMIT(3, 50), &status); - if (err || status != BT_MESH_TRANSMIT(3, 50)) { - FAIL("Net transmit set failed (err %d, transmit %x)", err, status); - } - provisioner_ready = true; } diff --git a/tests/bsim/bluetooth/mesh/src/test_provision.c b/tests/bsim/bluetooth/mesh/src/test_provision.c index 2bff871b187..3d0e8010c75 100644 --- a/tests/bsim/bluetooth/mesh/src/test_provision.c +++ b/tests/bsim/bluetooth/mesh/src/test_provision.c @@ -1205,7 +1205,6 @@ static void test_provisioner_pb_remote_client_nppi_robustness(void) uint16_t pb_remote_server_addr; uint8_t status; struct bt_mesh_cdb_node *node; - int err; provisioner_pb_remote_client_setup(); @@ -1219,15 +1218,6 @@ static void test_provisioner_pb_remote_client_nppi_robustness(void) .ttl = 3, }; - /* Set Network Transmit Count state on the remote client greater than on the remote server - * to increase probability of reception responses. - */ - err = bt_mesh_cfg_cli_net_transmit_set(0, current_dev_addr, BT_MESH_TRANSMIT(3, 50), - &status); - if (err || status != BT_MESH_TRANSMIT(3, 50)) { - FAIL("Net transmit set failed (err %d, transmit %x)", err, status); - } - ASSERT_OK(provision_remote(&srv, 2, &srv.addr)); /* Check device key by adding appkey. */ diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/access/access_transmit_delayable.sh b/tests/bsim/bluetooth/mesh/tests_scripts/access/access_transmit_delayable.sh index 1622ac49f06..0e966288db0 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/access/access_transmit_delayable.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/access/access_transmit_delayable.sh @@ -14,4 +14,4 @@ RunTest mesh_access_pub_transmit_delayable_retr_1d1 \ conf=prj_mesh1d1_conf overlay=overlay_psa_conf RunTest mesh_access_pub_transmit_delayable_retr_psa \ - access_tx_period_delayable access_rx_period_delayable + access_tx_transmit_delayable access_rx_transmit_delayable diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/priv_beacon/proxy_adv_multi_subnet_coex.sh b/tests/bsim/bluetooth/mesh/tests_scripts/priv_beacon/proxy_adv_multi_subnet_coex.sh index aa96ee325d8..42863a13458 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/priv_beacon/proxy_adv_multi_subnet_coex.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/priv_beacon/proxy_adv_multi_subnet_coex.sh @@ -22,6 +22,10 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh # window. The Mesh Protocol specification does not specify exactly the # timing for Proxy ADV messages. +# Note 3: The proxy transmitting device mandates emitting of the secure +# network beacons. This allows to check that proxy goes back to normal +# behavior after the device advertises the secure network beacons. + # Test procedure: # 1. (0-20 seconds) A single subnet is active on the TX device with GATT # Proxy enabled. RX device verifies that the single subnet has exclusive