Skip to content

Commit a8ac422

Browse files
Thalleym-alperen-sener
authored andcommitted
[nrf fromtree] Bluetooth: Host: Add type check for bt_conn API
Added a new function, bt_conn_is_type, that returns whether the provided conn object is of the provided type. This check is then used to ensure that the conn objects supplied to other bt_conn function are of the right type. The right type has also been documented for these functions. This is an initial commit for a larger change in the BT Host, as similar checks should be added to the L2CAP, GATT, ISO, Audio and possibly Mesh APIs. The type check could have been implemented by using the bt_conn_get_info function, but that requires additional function calls as well as memory allocation and copy. Since bt_conn_is_type is designed to be widely used, it was suited for its own function. Signed-off-by: Emil Gydesen <[email protected]> (cherry picked from commit b67d291)
1 parent 2eb4152 commit a8ac422

File tree

10 files changed

+257
-41
lines changed

10 files changed

+257
-41
lines changed

doc/releases/release-notes-4.1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Bluetooth
9999
* The ECDH HCI command/event emulation layer has been removed, meaning the host will now always
100100
do direct calls to PSA to perform these operations.
101101

102+
* :c:func:`bt_conn_is_type`
103+
102104
* HCI Drivers
103105

104106
* Mesh

include/zephyr/bluetooth/conn.h

Lines changed: 73 additions & 27 deletions
Large diffs are not rendered by default.

subsys/bluetooth/host/classic/ssp.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/*
2-
* Copyright (c) 2017 Nordic Semiconductor ASA
2+
* Copyright (c) 2017-2025 Nordic Semiconductor ASA
33
* Copyright (c) 2015 Intel Corporation
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include <errno.h>
89
#include <stdint.h>
910

11+
#include <zephyr/bluetooth/conn.h>
1012
#include <zephyr/sys/byteorder.h>
1113

1214
#include <zephyr/bluetooth/buf.h>
@@ -87,7 +89,8 @@ int bt_conn_auth_pincode_entry(struct bt_conn *conn, const char *pin)
8789
return -EINVAL;
8890
}
8991

90-
if (conn->type != BT_CONN_TYPE_BR) {
92+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_BR)) {
93+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
9194
return -EINVAL;
9295
}
9396

subsys/bluetooth/host/conn.c

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Copyright (c) 2015-2016 Intel Corporation
5+
* Copyright (c) 2025 Nordic Semiconductor ASA
56
*
67
* SPDX-License-Identifier: Apache-2.0
78
*/
@@ -2580,6 +2581,11 @@ int bt_conn_le_start_encryption(struct bt_conn *conn, uint8_t rand[8],
25802581
#if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC)
25812582
uint8_t bt_conn_enc_key_size(const struct bt_conn *conn)
25822583
{
2584+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
2585+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2586+
return 0;
2587+
}
2588+
25832589
if (!conn->encrypt) {
25842590
return 0;
25852591
}
@@ -2695,6 +2701,11 @@ int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec)
26952701
bool force_pair;
26962702
int err;
26972703

2704+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
2705+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2706+
return -EINVAL;
2707+
}
2708+
26982709
if (conn->state != BT_CONN_CONNECTED) {
26992710
return -ENOTCONN;
27002711
}
@@ -2730,6 +2741,11 @@ int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec)
27302741

27312742
bt_security_t bt_conn_get_security(const struct bt_conn *conn)
27322743
{
2744+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
2745+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2746+
return BT_SECURITY_L0;
2747+
}
2748+
27332749
return conn->sec_level;
27342750
}
27352751
#else
@@ -2890,6 +2906,11 @@ struct bt_conn *bt_conn_lookup_state_le(uint8_t id, const bt_addr_le_t *peer,
28902906

28912907
const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn)
28922908
{
2909+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
2910+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2911+
return NULL;
2912+
}
2913+
28932914
return &conn->le.dst;
28942915
}
28952916

@@ -2983,9 +3004,23 @@ int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info)
29833004
return -EINVAL;
29843005
}
29853006

3007+
bool bt_conn_is_type(const struct bt_conn *conn, enum bt_conn_type type)
3008+
{
3009+
if (conn == NULL) {
3010+
return false;
3011+
}
3012+
3013+
return (conn->type & type) != 0;
3014+
}
3015+
29863016
int bt_conn_get_remote_info(struct bt_conn *conn,
29873017
struct bt_conn_remote_info *remote_info)
29883018
{
3019+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
3020+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3021+
return -EINVAL;
3022+
}
3023+
29893024
if (!atomic_test_bit(conn->flags, BT_CONN_LE_FEATURES_EXCHANGED) ||
29903025
(IS_ENABLED(CONFIG_BT_REMOTE_VERSION) &&
29913026
!atomic_test_bit(conn->flags, BT_CONN_AUTO_VERSION_INFO))) {
@@ -3079,6 +3114,11 @@ int bt_conn_le_enhanced_get_tx_power_level(struct bt_conn *conn,
30793114
struct bt_hci_cp_le_read_tx_power_level *cp;
30803115
struct net_buf *buf;
30813116

3117+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3118+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3119+
return -EINVAL;
3120+
}
3121+
30823122
if (!tx_power->phy) {
30833123
return -EINVAL;
30843124
}
@@ -3112,6 +3152,11 @@ int bt_conn_le_get_remote_tx_power_level(struct bt_conn *conn,
31123152
struct bt_hci_cp_le_read_tx_power_level *cp;
31133153
struct net_buf *buf;
31143154

3155+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3156+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3157+
return -EINVAL;
3158+
}
3159+
31153160
if (!phy) {
31163161
return -EINVAL;
31173162
}
@@ -3135,6 +3180,11 @@ int bt_conn_le_set_tx_power_report_enable(struct bt_conn *conn,
31353180
struct bt_hci_cp_le_set_tx_power_report_enable *cp;
31363181
struct net_buf *buf;
31373182

3183+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3184+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3185+
return -EINVAL;
3186+
}
3187+
31383188
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_TX_POWER_REPORT_ENABLE, sizeof(*cp));
31393189
if (!buf) {
31403190
return -ENOBUFS;
@@ -3156,6 +3206,11 @@ int bt_conn_le_get_tx_power_level(struct bt_conn *conn,
31563206
{
31573207
int err;
31583208

3209+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3210+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3211+
return -EINVAL;
3212+
}
3213+
31593214
if (tx_power_level->phy != 0) {
31603215
if (IS_ENABLED(CONFIG_BT_TRANSMIT_POWER_CONTROL)) {
31613216
return bt_conn_le_enhanced_get_tx_power_level(conn, tx_power_level);
@@ -3201,6 +3256,11 @@ int bt_conn_le_set_path_loss_mon_param(struct bt_conn *conn,
32013256
struct bt_hci_cp_le_set_path_loss_reporting_parameters *cp;
32023257
struct net_buf *buf;
32033258

3259+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3260+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3261+
return -EINVAL;
3262+
}
3263+
32043264
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PATH_LOSS_REPORTING_PARAMETERS, sizeof(*cp));
32053265
if (!buf) {
32063266
return -ENOBUFS;
@@ -3222,6 +3282,11 @@ int bt_conn_le_set_path_loss_mon_enable(struct bt_conn *conn, bool reporting_ena
32223282
struct bt_hci_cp_le_set_path_loss_reporting_enable *cp;
32233283
struct net_buf *buf;
32243284

3285+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3286+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3287+
return -EINVAL;
3288+
}
3289+
32253290
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PATH_LOSS_REPORTING_ENABLE, sizeof(*cp));
32263291
if (!buf) {
32273292
return -ENOBUFS;
@@ -3318,6 +3383,11 @@ int bt_conn_le_subrate_request(struct bt_conn *conn,
33183383
struct bt_hci_cp_le_subrate_request *cp;
33193384
struct net_buf *buf;
33203385

3386+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3387+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3388+
return -EINVAL;
3389+
}
3390+
33213391
if (!le_subrate_common_params_valid(params)) {
33223392
return -EINVAL;
33233393
}
@@ -3464,6 +3534,11 @@ void notify_cs_subevent_result(struct bt_conn *conn, struct bt_conn_le_cs_subeve
34643534
int bt_conn_le_param_update(struct bt_conn *conn,
34653535
const struct bt_le_conn_param *param)
34663536
{
3537+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3538+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3539+
return -EINVAL;
3540+
}
3541+
34673542
LOG_DBG("conn %p features 0x%02x params (%d-%d %d %d)", conn, conn->le.features[0],
34683543
param->interval_min, param->interval_max, param->latency, param->timeout);
34693544

@@ -3493,6 +3568,11 @@ int bt_conn_le_param_update(struct bt_conn *conn,
34933568
int bt_conn_le_data_len_update(struct bt_conn *conn,
34943569
const struct bt_conn_le_data_len_param *param)
34953570
{
3571+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3572+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3573+
return -EINVAL;
3574+
}
3575+
34963576
if (conn->le.data_len.tx_max_len == param->tx_max_len &&
34973577
conn->le.data_len.tx_max_time == param->tx_max_time) {
34983578
return -EALREADY;
@@ -3508,6 +3588,11 @@ int bt_conn_le_phy_update(struct bt_conn *conn,
35083588
{
35093589
uint8_t phy_opts, all_phys;
35103590

3591+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
3592+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
3593+
return -EINVAL;
3594+
}
3595+
35113596
if ((param->options & BT_CONN_LE_PHY_OPT_CODED_S2) &&
35123597
(param->options & BT_CONN_LE_PHY_OPT_CODED_S8)) {
35133598
phy_opts = BT_HCI_LE_PHY_CODED_ANY;
@@ -3982,7 +4067,8 @@ int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb)
39824067
#if defined(CONFIG_BT_SMP)
39834068
int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb)
39844069
{
3985-
CHECKIF(conn == NULL) {
4070+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
4071+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
39864072
return -EINVAL;
39874073
}
39884074

@@ -4033,6 +4119,11 @@ int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb)
40334119

40344120
int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey)
40354121
{
4122+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
4123+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
4124+
return -EINVAL;
4125+
}
4126+
40364127
if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
40374128
return bt_smp_auth_passkey_entry(conn, passkey);
40384129
}
@@ -4052,7 +4143,12 @@ int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey)
40524143
int bt_conn_auth_keypress_notify(struct bt_conn *conn,
40534144
enum bt_conn_auth_keypress type)
40544145
{
4055-
if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
4146+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
4147+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
4148+
return -EINVAL;
4149+
}
4150+
4151+
if (IS_ENABLED(CONFIG_BT_SMP)) {
40564152
return bt_smp_auth_keypress_notify(conn, type);
40574153
}
40584154

@@ -4063,6 +4159,11 @@ int bt_conn_auth_keypress_notify(struct bt_conn *conn,
40634159

40644160
int bt_conn_auth_passkey_confirm(struct bt_conn *conn)
40654161
{
4162+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
4163+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
4164+
return -EINVAL;
4165+
}
4166+
40664167
if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
40674168
return bt_smp_auth_passkey_confirm(conn);
40684169
}
@@ -4080,6 +4181,11 @@ int bt_conn_auth_passkey_confirm(struct bt_conn *conn)
40804181

40814182
int bt_conn_auth_cancel(struct bt_conn *conn)
40824183
{
4184+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
4185+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
4186+
return -EINVAL;
4187+
}
4188+
40834189
if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
40844190
return bt_smp_auth_cancel(conn);
40854191
}
@@ -4097,6 +4203,11 @@ int bt_conn_auth_cancel(struct bt_conn *conn)
40974203

40984204
int bt_conn_auth_pairing_confirm(struct bt_conn *conn)
40994205
{
4206+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
4207+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
4208+
return -EINVAL;
4209+
}
4210+
41004211
if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
41014212
return bt_smp_auth_pairing_confirm(conn);
41024213
}

subsys/bluetooth/host/id.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
2-
* Copyright (c) 2017-2024 Nordic Semiconductor ASA
2+
* Copyright (c) 2017-2025 Nordic Semiconductor ASA
33
* Copyright (c) 2015-2016 Intel Corporation
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include <errno.h>
89
#include <stdbool.h>
910
#include <stdint.h>
1011

12+
#include <zephyr/bluetooth/conn.h>
1113
#include <zephyr/bluetooth/hci_types.h>
1214
#include <zephyr/settings/settings.h>
1315
#include <zephyr/sys/byteorder.h>
@@ -2133,7 +2135,12 @@ int bt_le_ext_adv_oob_get_local(struct bt_le_ext_adv *adv,
21332135
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
21342136
int bt_le_oob_set_legacy_tk(struct bt_conn *conn, const uint8_t *tk)
21352137
{
2136-
CHECKIF(conn == NULL || tk == NULL) {
2138+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
2139+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2140+
return -EINVAL;
2141+
}
2142+
2143+
CHECKIF(tk == NULL) {
21372144
return -EINVAL;
21382145
}
21392146

@@ -2146,7 +2153,8 @@ int bt_le_oob_set_sc_data(struct bt_conn *conn,
21462153
const struct bt_le_oob_sc_data *oobd_local,
21472154
const struct bt_le_oob_sc_data *oobd_remote)
21482155
{
2149-
CHECKIF(conn == NULL) {
2156+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
2157+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
21502158
return -EINVAL;
21512159
}
21522160

@@ -2161,7 +2169,9 @@ int bt_le_oob_get_sc_data(struct bt_conn *conn,
21612169
const struct bt_le_oob_sc_data **oobd_local,
21622170
const struct bt_le_oob_sc_data **oobd_remote)
21632171
{
2164-
CHECKIF(conn == NULL) {
2172+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) {
2173+
LOG_ERR("Invalid connection: %p", conn);
2174+
LOG_ERR("Invalid connection type: %u for %p", conn->handle, conn);
21652175
return -EINVAL;
21662176
}
21672177

subsys/bluetooth/host/smp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/*
7-
* Copyright (c) 2017 Nordic Semiconductor ASA
7+
* Copyright (c) 2017-2025 Nordic Semiconductor ASA
88
* Copyright (c) 2015-2016 Intel Corporation
99
*
1010
* SPDX-License-Identifier: Apache-2.0
@@ -5441,6 +5441,11 @@ int bt_conn_set_bondable(struct bt_conn *conn, bool enable)
54415441
{
54425442
struct bt_smp *smp;
54435443

5444+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) {
5445+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
5446+
return -EINVAL;
5447+
}
5448+
54445449
if (IS_ENABLED(CONFIG_BT_CLASSIC) && (conn->type == BT_CONN_TYPE_BR)) {
54455450
if (enable && atomic_test_and_set_bit(conn->flags, BT_CONN_BR_BONDABLE)) {
54465451
return -EALREADY;

tests/bluetooth/host/conn/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/conn/mocks mocks)
1616

1717
target_link_libraries(testbinary PRIVATE mocks host_mocks)
1818

19+
target_include_directories(mocks PUBLIC
20+
${ZEPHYR_BASE}/subsys/bluetooth/host
21+
)
22+
1923
target_sources(testbinary
2024
PRIVATE
2125
src/main.c

0 commit comments

Comments
 (0)