From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v1] Bluetooth: hci_event: Fix not emitting Device Connected event
Date: Thu, 3 Dec 2026 15:50:42 -0500 [thread overview]
Message-ID: <20261203205042.947891-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.
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
next reply other threads:[~2026-12-03 20:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 20:50 Luiz Augusto von Dentz [this message]
2026-12-04 2:10 ` [v1] Bluetooth: hci_event: Fix not emitting Device Connected event bluez.test.bot
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=20261203205042.947891-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