Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH v1] Bluetooth: hci_event: Fix not emitting Device Connected event
@ 2026-12-03 20:50 Luiz Augusto von Dentz
  2026-12-04  2:10 ` [v1] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-03 20:50 UTC (permalink / raw)
  To: linux-bluetooth

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.

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>
---
 include/net/bluetooth/hci_core.h | 15 +++++++++++++++
 net/bluetooth/hci_event.c        | 11 +++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae104..1d0d4fe2e1ac 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)
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 749963d0ce77..3e340c7cf6d8 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -2567,6 +2567,9 @@ static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
 	 * Only those in BT_CONFIG or BT_CONNECTED states can be
 	 * considered connected.
 	 */
+	if (conn)
+		clear_bit(HCI_CONN_NAME_PEND, conn->flags);
+
 	if (conn && (conn->state == BT_CONFIG || conn->state == BT_CONNECTED))
 		mgmt_device_connected(hdev, conn, name, name_len);
 
@@ -4111,7 +4114,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 +5560,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);
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  2:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 20:50 [PATCH v1] Bluetooth: hci_event: Fix not emitting Device Connected event Luiz Augusto von Dentz
2026-12-04  2:10 ` [v1] " bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox