Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2] Bluetooth: hci_event: Fix not emitting Device Connected event
Date: Fri,  4 Dec 2026 09:06:27 -0500	[thread overview]
Message-ID: <20261204140627.1150694-1-luiz.dentz@gmail.com> (raw)

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If the remote initiates the connection and requests authentication
early, the Encryption Change event may arrive before Read Remote
Supported Features Complete. hci_encrypt_cfm() then moves the
connection from BT_CONFIG to BT_CONNECTED, and hci_remote_features_evt()
bails out since the state is no longer BT_CONFIG, so the path leading
to mgmt_device_connected() is never taken and MGMT_EV_DEVICE_CONNECTED
is never emitted. Userspace then services the connection without
knowing it exists ("Authorization request for non-connected device!?")
and Device1.Connected stays false.

Fix this by emitting MGMT_EV_DEVICE_CONNECTED from hci_connect_cfm()
once an ACL/LE connection reaches BT_CONNECTED, which covers every
place setting that state. To not lose the remote name in the regular
flow, where BT_CONNECTED is set while the Remote Name Request is still
outstanding, track the pending request with HCI_CONN_NAME_PEND and
leave the event to hci_check_pending_name() in that case.

Since mgmt_device_connected() may now be called from within
hci_conn_enc_key_size_set(), report the security level change before
calling hci_encrypt_cfm() so it isn't reported twice, and don't report it
from mgmt_device_connected() while the key size is still pending
validation as hci_conn_enc_key_size_set() will do it.

Link: https://github.com/bluez/bluez/issues/2485
Fixes: 7967dc8f797f ("Bluetooth: hci_core: Fix calling mgmt_device_connected")
Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
v2:
 - Drop duplicated mgmt_device_connected() prototype (Sashiko)
 - Call mgmt_security_level_changed() before hci_encrypt_cfm() in
   hci_conn_enc_key_size_set() to avoid reporting it twice (Sashiko)
 - Don't report the security level from mgmt_device_connected() while
   HCI_CONN_ENC_KEY_SIZE_PEND is set (Sashiko)
 - Clear HCI_CONN_NAME_PEND before checking HCI_MGMT in
   hci_remote_name_evt() and hci_cs_remote_name_req() so it cannot get
   stuck (Sashiko)

 include/net/bluetooth/hci_core.h | 17 +++++++++++++++--
 net/bluetooth/hci_event.c        | 28 +++++++++++++++++++---------
 net/bluetooth/mgmt.c             |  7 +++++--
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae104..ecc89025e5a6 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -733,6 +733,7 @@ enum {
 	HCI_CONN_PA_SYNC,
 	HCI_CONN_PA_SYNC_FAILED,
 	HCI_CONN_ENC_KEY_SIZE_PEND,
+	HCI_CONN_NAME_PEND,
 
 	__HCI_CONN_NUM_FLAGS,
 };
@@ -2272,10 +2273,24 @@ struct hci_cb {
 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
 };
 
+void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
+			   u8 *name, u8 name_len);
+
 static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
 {
 	struct hci_cb *cb;
 
+	/* Make sure MGMT_EV_DEVICE_CONNECTED is emitted once the connection
+	 * reaches BT_CONNECTED regardless of the order events were received
+	 * (e.g. Encryption Change before Read Remote Features Complete on
+	 * remote initiated connections). If a remote name request is pending
+	 * the event is deferred to hci_check_pending_name().
+	 */
+	if (!status && conn->state == BT_CONNECTED &&
+	    (conn->type == ACL_LINK || conn->type == LE_LINK) &&
+	    !test_bit(HCI_CONN_NAME_PEND, conn->flags))
+		mgmt_device_connected(conn->hdev, conn, NULL, 0);
+
 	mutex_lock(&hci_cb_list_lock);
 	list_for_each_entry(cb, &hci_cb_list, list) {
 		if (cb->connect_cfm)
@@ -2581,8 +2596,6 @@ void __mgmt_power_off(struct hci_dev *hdev);
 void mgmt_mesh_flush(struct hci_dev *hdev);
 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
 		       bool persistent);
-void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
-			   u8 *name, u8 name_len);
 u8 hci_to_mgmt_reason(u8 err);
 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
 			      u8 link_type, u8 addr_type, u8 reason,
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 749963d0ce77..af32e0ae9802 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -780,6 +780,15 @@ static u8 hci_conn_enc_key_size_set(struct hci_dev *hdev, struct hci_conn *conn,
 			*key_enc_size = conn->enc_key_size;
 	}
 
+	/* mgmt_security_level_changed() only reports the change for
+	 * connections already announced to userspace (it checks
+	 * HCI_CONN_MGMT_CONNECTED itself); otherwise it is a no-op here and
+	 * reporting is deferred to mgmt_device_connected(), which may be
+	 * called by hci_encrypt_cfm() below, so this must be done first to
+	 * not report the change twice.
+	 */
+	mgmt_security_level_changed(conn);
+
 	hci_encrypt_cfm(conn, status);
 
 	/* The link was reported as encrypted but it can't be used if the key
@@ -790,13 +799,6 @@ static u8 hci_conn_enc_key_size_set(struct hci_dev *hdev, struct hci_conn *conn,
 	if (status && conn->state == BT_CONNECTED)
 		hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
 
-	/* mgmt_security_level_changed() only reports the change for
-	 * connections already announced to userspace (it checks
-	 * HCI_CONN_MGMT_CONNECTED itself); otherwise it is a no-op here and
-	 * reporting is deferred to mgmt_device_connected().
-	 */
-	mgmt_security_level_changed(conn);
-
 	return status;
 }
 
@@ -2621,6 +2623,8 @@ static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
 	hci_dev_lock(hdev);
 
 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
+	if (conn)
+		clear_bit(HCI_CONN_NAME_PEND, conn->flags);
 
 	if (hci_dev_test_flag(hdev, HCI_MGMT))
 		hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
@@ -3764,6 +3768,8 @@ static void hci_remote_name_evt(struct hci_dev *hdev, void *data,
 	hci_dev_lock(hdev);
 
 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
+	if (conn)
+		clear_bit(HCI_CONN_NAME_PEND, conn->flags);
 
 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
 		goto check_auth;
@@ -4111,7 +4117,9 @@ static void hci_remote_features_evt(struct hci_dev *hdev, void *data,
 		memset(&cp, 0, sizeof(cp));
 		bacpy(&cp.bdaddr, &conn->dst);
 		cp.pscan_rep_mode = 0x02;
-		hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
+		if (!hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp),
+				  &cp))
+			set_bit(HCI_CONN_NAME_PEND, conn->flags);
 	} else {
 		mgmt_device_connected(hdev, conn, NULL, 0);
 	}
@@ -5555,7 +5563,9 @@ static void hci_remote_ext_features_evt(struct hci_dev *hdev, void *data,
 		memset(&cp, 0, sizeof(cp));
 		bacpy(&cp.bdaddr, &conn->dst);
 		cp.pscan_rep_mode = 0x02;
-		hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
+		if (!hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp),
+				  &cp))
+			set_bit(HCI_CONN_NAME_PEND, conn->flags);
 	} else {
 		mgmt_device_connected(hdev, conn, NULL, 0);
 	}
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 01969ab57339..24bab42e28ef 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -10407,9 +10407,12 @@ void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
 	/* Connections that negotiated encryption as part of setup (e.g. via
 	 * an existing link key) had their security level change suppressed
 	 * since userspace did not know about them yet, so report it now.
-	 * Unencrypted connections have no change to report.
+	 * Unencrypted connections have no change to report, and those still
+	 * waiting for the encryption key size to be validated have it
+	 * reported by hci_conn_enc_key_size_set() instead.
 	 */
-	if (test_bit(HCI_CONN_ENCRYPT, conn->flags))
+	if (test_bit(HCI_CONN_ENCRYPT, conn->flags) &&
+	    !test_bit(HCI_CONN_ENC_KEY_SIZE_PEND, conn->flags))
 		mgmt_security_level_changed(conn);
 }
 
-- 
2.56.1


             reply	other threads:[~2026-12-04 14:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 14:06 Luiz Augusto von Dentz [this message]
2026-12-04 15:40 ` [PATCH v2] Bluetooth: hci_event: Fix not emitting Device Connected event patchwork-bot+bluetooth

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=20261204140627.1150694-1-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