From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2 12/16] profiles/ranging: Defer CS Security Enable until the link is encrypted
Date: Wed, 18 Nov 2026 14:58:40 -0500 [thread overview]
Message-ID: <20261118195846.393710-13-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261118195846.393710-1-luiz.dentz@gmail.com>
From: Cheng Jiang <cheng.jiang@oss.qualcomm.com>
LE CS Security Enable is only valid on an encrypted link (Core v6.2
Vol 4 Part E 7.8.133, which fails it with Insufficient Security
otherwise). On the Central, the Config Complete handler checked the ATT
security level once and put the state machine on hold if the link was
not yet encrypted, so a configuration completing before encryption
never got its Security Enable.
Defer the command instead and send it when the HCI Encryption Change
event reports the link as encrypted. Track the encryption in the
connection mapping, so the check no longer depends only on an ATT
instance being attached, and keep the ATT security level as a fallback
for a link that was encrypted before the state machine was attached.
Assisted-by: Claude:claude-opus-5-5
---
profiles/ranging/rap_hci.c | 78 +++++++++++++++++++++++++++++++-------
1 file changed, 65 insertions(+), 13 deletions(-)
diff --git a/profiles/ranging/rap_hci.c b/profiles/ranging/rap_hci.c
index 1d59eaa6efb1..ad9c4c0feb9b 100644
--- a/profiles/ranging/rap_hci.c
+++ b/profiles/ranging/rap_hci.c
@@ -75,6 +75,8 @@ struct cs_state_machine {
bool procedure_active;
struct bt_rap_hci_cs_options cs_opt; /* Per-instance CS options */
uint8_t role_enable; /* Role value for HCI commands (1, 2, or 3) */
+ bool sec_enable_pending; /* Security Enable deferred until encrypted */
+ unsigned int encrypt_evt_id; /* Encryption Change registration */
/* Negotiated CS role for this config (CS_INITIATOR/CS_REFLECTOR), taken
* from LE CS Config Complete. Unknown until that event arrives, which
* is why it is kept apart from role_enable: the latter is the bitmask
@@ -99,6 +101,7 @@ struct rap_conn_mapping {
uint8_t bdaddr[6];
uint8_t bdaddr_type;
bool is_central; /* true if local device is BLE Central on this link */
+ bool encrypted; /* set from the HCI Encryption Change event */
struct bt_att *att;
struct bt_rap *rap;
};
@@ -902,7 +905,8 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
* the ones for other links: acting on them would overwrite this
* link's negotiated role and advance its state.
*/
- if (!find_mapping_by_handle(sm, evt->handle)) {
+ mapping = find_mapping_by_handle(sm, evt->handle);
+ if (!mapping) {
DBG("Not for this link, ignoring");
return;
}
@@ -1010,26 +1014,62 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
/* Success - proceed to Security enable complete */
cs_set_state(sm, CS_STATE_WAIT_SEC_CMPLT);
- /* CS Security Enable may only be issued by the BLE Central */
- mapping = find_mapping_by_handle(sm, evt->handle);
- if (mapping && mapping->is_central) {
- if (bt_att_get_security(mapping->att, NULL) <
- BT_ATT_SECURITY_MEDIUM) {
- error("CS Security Enable skipped: not encrypted");
- cs_set_state(sm, CS_STATE_HOLD);
- return;
- }
-
+ /* CS Security Enable may only be issued by the BLE Central, and only on
+ * an encrypted link. The LE role comes from the connection mapping
+ * (set from the LE connection event), not from the CS role in rap_ev.
+ * If the link is not encrypted yet, defer: the Encryption Change event
+ * retries it (rap_encrypt_change_evt).
+ */
+ if (!mapping->is_central) {
+ /* Peripheral link: the Central's Link Layer starts CS Security
+ * Start (Core v6.2 Vol 6 Part B 5.1.23). The local controller
+ * reports completion with LE CS Security Enable Complete when
+ * it responds to the peer's request (Vol 4 Part E 7.7.65.41),
+ * and that event continues the procedure. Do not stop here.
+ */
+ DBG("Peripheral link: waiting for peer CS Security Start");
+ sm->sec_enable_pending = false;
+ } else if (mapping->encrypted ||
+ (mapping->att && bt_att_get_security(mapping->att,
+ NULL) >= BT_ATT_SECURITY_MEDIUM)) {
DBG("Central,encrypted: Sending CS Security Enable command");
rap_send_hci_cs_security_enable_command(sm, evt->handle);
} else {
- DBG("Peripheral: Waiting for security enable event...");
+ DBG("Central, not yet encrypted: deferring CS Security Enable");
+ sm->sec_enable_pending = true;
}
/* Send callback to RAP Profile */
bt_rap_hci_cs_config_complete_callback(size, &rap_ev, sm->rap);
}
+static void rap_encrypt_change_evt(const void *data, uint8_t size,
+ void *user_data)
+{
+ struct cs_state_machine *sm = user_data;
+ const struct bt_hci_evt_encrypt_change *evt = data;
+ struct rap_conn_mapping *mapping;
+ uint16_t handle;
+
+ if (!sm || size < sizeof(*evt) || evt->status != 0)
+ return;
+
+ handle = le16_to_cpu(evt->handle);
+ mapping = find_mapping_by_handle(sm, handle);
+ if (!mapping)
+ return;
+
+ mapping->encrypted = (evt->encr_mode != 0x00);
+
+ if (!sm->sec_enable_pending || !mapping->is_central ||
+ !mapping->encrypted)
+ return;
+
+ DBG("Encrypted: sending deferred CS Security Enable");
+ sm->sec_enable_pending = false;
+ rap_send_hci_cs_security_enable_command(sm, handle);
+}
+
static void rap_cs_sec_enable_cmplt_evt(const void *data, uint8_t size,
void *user_data)
{
@@ -1799,6 +1839,14 @@ void *bt_rap_attach_hci(struct bt_rap *rap, struct bt_hci *hci,
queue_push_tail(sm->event_ids, UINT_TO_PTR(id));
}
+ /* Encryption Change is a plain HCI event; it lets a deferred CS
+ * Security Enable be sent once the link is encrypted.
+ */
+ sm->encrypt_evt_id = bt_hci_register(hci, BT_HCI_EVT_ENCRYPT_CHANGE,
+ rap_encrypt_change_evt, sm, NULL);
+ if (!sm->encrypt_evt_id)
+ error("Failed to register Encryption Change handler");
+
DBG("HCI state machine attached (role=%u, ant=0x%02x, power=%d)",
sm->role_enable, sm->cs_opt.cs_sync_ant_sel,
sm->cs_opt.max_tx_power);
@@ -1943,6 +1991,7 @@ void bt_rap_clear_conn_handle(void *hci_sm, uint16_t handle)
remove_conn_mapping(sm, handle);
sm->procedure_active = false;
sm->cs_role_known = false;
+ sm->sec_enable_pending = false;
sm->current_state = CS_STATE_UNSPECIFIED;
}
@@ -1991,9 +2040,12 @@ void bt_rap_detach_hci(struct bt_rap *rap, void *hci_sm)
/* Cleanup the per-instance state machine */
if (sm) {
/* Unregister HCI events */
- if (sm->hci)
+ if (sm->hci) {
queue_foreach(sm->event_ids, unregister_event_id,
sm->hci);
+ if (sm->encrypt_evt_id)
+ bt_hci_unregister(sm->hci, sm->encrypt_evt_id);
+ }
if (sm->measurement_timeout_id) {
timeout_remove(sm->measurement_timeout_id);
--
2.56.0
next prev parent reply other threads:[~2026-11-18 19:58 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 19:58 [PATCH BlueZ v2 00/16] Channel Sounding and RAP fixes Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 01/16] gatt-database: Drop stale device state for removed bonds Luiz Augusto von Dentz
2026-11-19 1:57 ` Channel Sounding and RAP fixes bluez.test.bot
2026-11-18 19:58 ` [PATCH BlueZ v2 02/16] shared/rap: Reset the readable Ranging Counters per connection Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 03/16] shared/rap: Subscribe on-demand data with the requested transport Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 04/16] shared/rap: Subscribe to Ranging Data when a measurement starts Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 05/16] client/cs: Fix use-after-free in stop_measurement_reply Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 06/16] shared/hci: Look up the connection handle on the right link Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 07/16] profiles/ranging: Let the Reflector run the CS setup phase Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 08/16] profiles/ranging: Let StartMeasurement() pick the Initiator for Both Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 09/16] profiles/ranging: Take the CS role from Config Complete Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 10/16] profiles/ranging: Act on a CS configuration created by the peer Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 11/16] profiles/ranging: Keep a peer-driven setup from being rewound Luiz Augusto von Dentz
2026-11-18 19:58 ` Luiz Augusto von Dentz [this message]
2026-11-18 19:58 ` [PATCH BlueZ v2 13/16] profiles/ranging: Choose the CS antenna configuration from both ends Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 14/16] profiles/ranging: Do not start a procedure after StopMeasurement() Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 15/16] shared/rap: Start the Real-time timeout when the procedure starts Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 16/16] unit/test-rap: Add tests for Ranging Data subscription and counters Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118195846.393710-13-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox