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 )
25812582uint8_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
27312742bt_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
28912907const 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+
29863016int 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
34643534int 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,
34933568int 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 )
39834068int 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
40344120int 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)
40524143int 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
40644160int 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
40814182int 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
40984204int 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 }
0 commit comments