Bluetooth: Mesh: Prevent duplicate cdb appkeys

Fixes issue where it is possible to store the same appkey
multiple times in CDB implementation.

Signed-off-by: Anders Storrø <anders.storro@nordicsemi.no>
This commit is contained in:
Anders Storrø 2024-02-28 14:38:12 +01:00 committed by Fabio Baltieri
commit 6af7ad5a52
2 changed files with 20 additions and 10 deletions

View file

@ -249,7 +249,8 @@ void bt_mesh_cdb_node_foreach(bt_mesh_cdb_node_func_t func, void *user_data);
* *
* @param net_idx NetIdx of the subnet. * @param net_idx NetIdx of the subnet.
* *
* @return The new subnet or NULL if it cannot be allocated. * @return The new subnet or NULL if it cannot be allocated due to
* lack of resources or the subnet has been already allocated.
*/ */
struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_alloc(uint16_t net_idx); struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_alloc(uint16_t net_idx);
@ -328,7 +329,8 @@ int bt_mesh_cdb_subnet_key_export(const struct bt_mesh_cdb_subnet *sub, int key_
* @param net_idx NetIdx of NetKey that the application key is bound to. * @param net_idx NetIdx of NetKey that the application key is bound to.
* @param app_idx AppIdx of the application key. * @param app_idx AppIdx of the application key.
* *
* @return The new application key or NULL if it cannot be allocated. * @return The new application key or NULL if it cannot be allocated due to
* lack of resources or the key has been already allocated.
*/ */
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx, struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx,
uint16_t app_idx); uint16_t app_idx);

View file

@ -94,6 +94,7 @@ struct bt_mesh_cdb bt_mesh_cdb = {
}, },
.app_keys = { .app_keys = {
[0 ... (CONFIG_BT_MESH_CDB_APP_KEY_COUNT - 1)] = { [0 ... (CONFIG_BT_MESH_CDB_APP_KEY_COUNT - 1)] = {
.app_idx = BT_MESH_KEY_UNUSED,
.net_idx = BT_MESH_KEY_UNUSED, .net_idx = BT_MESH_KEY_UNUSED,
} }
}, },
@ -1024,26 +1025,32 @@ int bt_mesh_cdb_node_key_export(const struct bt_mesh_cdb_node *node, uint8_t out
return bt_mesh_key_export(out, &node->dev_key); return bt_mesh_key_export(out, &node->dev_key);
} }
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx, struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx, uint16_t app_idx)
uint16_t app_idx)
{ {
struct bt_mesh_cdb_app_key *key; struct bt_mesh_cdb_app_key *key;
struct bt_mesh_cdb_app_key *vacant_key = NULL;
int i; int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh_cdb.app_keys); ++i) { for (i = 0; i < ARRAY_SIZE(bt_mesh_cdb.app_keys); ++i) {
key = &bt_mesh_cdb.app_keys[i]; key = &bt_mesh_cdb.app_keys[i];
if (key->net_idx != BT_MESH_KEY_UNUSED) { if (key->app_idx == app_idx) {
return NULL;
}
if (key->net_idx != BT_MESH_KEY_UNUSED || vacant_key) {
continue; continue;
} }
key->net_idx = net_idx; vacant_key = key;
key->app_idx = app_idx;
return key;
} }
return NULL; if (vacant_key) {
vacant_key->net_idx = net_idx;
vacant_key->app_idx = app_idx;
}
return vacant_key;
} }
void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store) void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
@ -1055,6 +1062,7 @@ void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
} }
key->net_idx = BT_MESH_KEY_UNUSED; key->net_idx = BT_MESH_KEY_UNUSED;
key->app_idx = BT_MESH_KEY_UNUSED;
bt_mesh_key_destroy(&key->keys[0].app_key); bt_mesh_key_destroy(&key->keys[0].app_key);
bt_mesh_key_destroy(&key->keys[1].app_key); bt_mesh_key_destroy(&key->keys[1].app_key);
memset(key->keys, 0, sizeof(key->keys)); memset(key->keys, 0, sizeof(key->keys));