From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796393198; b=Ar0+uV8R6O3it1JkKHfrfYb5kX179r9AkwWwRnksyynmOAzF6V7hqiTnr8s/Ed27Hw18 PDTCXzUdvz+P2x7dTecdyHDg4T2TnElxdmR/BgKgcBFlAy1hdn9UC1Tfucpfayx4CpkZg HYU1WS0FOSi0GyHOd4tVoK2AvSmijzCM6hKxmYiUhLMEGFxlz4RdYLUhaHPztlh71xLNr azYMf0KL7R0hUjg9zgzJ6xACm1eE1SGvrBw7gdiLlFcQnVyDeYJN8YEV+4JBiNM9tBa28 xRyErbl1RvZsw87xoWTlrYxb57Es1nPKzrJgXNPiTvHCwPouB39pAaSLAUDbJM624dA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796393198; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=zx1GnD+wJvmtwytz16OeFHhqelfnMTtWxnKyoh1d4UU=; b=FbOfIAmOPpChln+Mm4YRHg0+Ktuf6v+udCqyaFmWCfW9VDrGSGzXCGWzwNYSyBcC0I8V +TE1YaTsNim4WWj38bo9ZRt/j5wN4FXKiCtVrNfNQXhaNCMg8tggziHknJJcK2fkqoHqA mA2uZqXOg7p2qlaSpyJS8jXCqkxa36zuIlRW5VG4iFTgEfJnRcGgYcC11iWLdMsBacJFs +vAiQUrna5SWWCeLghdzMlWftoa0wGkU5rN02odqcosJCLYpICTAVQ5dkCL1pckbiKXKV MTRp7M5Gnj+5+7Zdy01xlJzlmO2LvrDdrVFb1R0c50GZjniJir4rkYoZrJaskuzQFow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=iFTzu8M5; arc=none smtp.remote-ip=209.85.222.49 Received: from mail-ua1-f49.google.com (mail-ua1-f49.google.com [209.85.222.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE20B606A330 for ; Fri, 4 Dec 2026 14:06:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BE20B606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=iFTzu8M5 Received: by mail-ua1-f49.google.com with SMTP id a1e0cc1a2514c-99b49d6a5caso217761241.2 for ; Fri, 04 Dec 2026 06:06:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796393197; x=1796997997; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=zx1GnD+wJvmtwytz16OeFHhqelfnMTtWxnKyoh1d4UU=; b=iFTzu8M5v8XUjJkA3+UHHoGy/idP8NfJwQzv7yKBLBvadYAOC41F9oxiWCXPtbtAb9 ekE9Q0oPY83QteT/ak0unsaERX3TeVAIplJPNC8kFE2Oy4u3maGkXHGi05MGDIL++vYK u1hwdhJvZgWI1bltccF/FphdpNMRdp0+TO3J0yySfhM4eEG9SbXeN91991tIoI1KuqrP kG2sQkMI6ZvmwDTa2o3VNxQ6TMFHf91oUexH3mUNZ9o0ZudrpIDgWCAIwmxsDnsZKlA3 04ZLE888JWIa1FGP1Qu9q86yeBbvQTTHeJY5/jhrz4xabPNwiwgafRewT9+jA2+gFORf NQzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796393197; x=1796997997; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=zx1GnD+wJvmtwytz16OeFHhqelfnMTtWxnKyoh1d4UU=; b=VY7ZUpmXKmTwkehjq5lfbaIx/bHiEXhvtrsT1Djqthspl6IjwF0ICn4fcZ0rAEzWZX 0WlNInRu4rwMP9QQV6NC2l2oTbuzvhnGFmLUEf5DS2MEI2CMQsi12JpqFt/taZS/Vn8g I+ZC3xQVXBzlt1krDmn0mKEU+Q8vBI7DsebRID1OTdAmSsdKj5OogF4Tio537PNMb0Ei 9RweMYyEaIbzTl9QkzinCRyO4pPyUvbbMuAq19PmSXWgkLsnOtCEERVMdjK9KUJxAtfh aC3Pfh/cP3XgMiKlMFrkPOf/BZxt7FgcGaL1UDc8eEh0ALQQkUf2RIruiweSNiNLsppM pkSg== X-Gm-Message-State: APuU4Z9/cguwFKHHPDd2PWkBYVBJWTU0+KNXtI4boDsI35Aq3x1CmQJm I2FNQYoynuKSAwyCnWyGMCM05tcbzsTp5fifj0eO266cdpGqVGMPeDMviGB4GjLY X-Gm-Gg: AZFdTZe1GFaXLnsPG2Z74E1I3n6fOTdXyIrJy5bTsck3Q7mTzN6ELe51+uYcnLF9/u5 S54rEN6zT/cwCy9o4SgiaaZ8ust87Cy4H8MF1+246Flu1z/euXse8/VmMDe4grgLBUsg0hTi7Ul zcmIQqZ99YBydifPTin9bv/UvsAQC5Um0rBKURSE2Ost2QmcCtf9QkLbmT2F6BleaIzZEigjRlv 2y+4Wzq0NRuAOIgGZ0PYVUoOZUro4zDwYh6U/w5gzSf9lJOP+n8mDMO3WdOFhAv3yjy/hzuZOIt hTQdIXZ262V0NoY8qyNxIaifeYPtKGKVqYONkMVr2/yUzVKQw50qXYSESJqkGoQH5Hl4qMdEQsG FmqQGl8vigyE0rJDqIKJ5Sg+ikqqYOrm0ek45wvzDtM7zJ6x/6oe3t5Yh7PE7Jq7qSh0+rGDt3H rH60lZ1Ru6pBSgPBdSMBMqe7c+XbGnD+6wtqHVHJ/9XfqPFzJMe9eJzjb3ZarnjB3xACyB9tjsD 61U9TOArXKWb/yDBEeg74FtB/TvkLiNPZPf2iYb+ItDzyUycPFO4En1BTHc+54drj7KzA6dA0Uo lcGy5HlVWh/wanbuBUM1kZA+1hFTtZGILzJrXWjHrK3lSx4RGgG2kbwKZCuvSPFaiZPMFPeb/MY BNUylpFWhNQofySg+zTg/l4PNnCkM0+dwJtCVe8ZvRZ66VMb4Sfs9/kY= X-Received: by 2002:a05:6122:3121:b0:5da:a74:d6a4 with SMTP id 71dfb90a1353d-5f01d0c5558mr1861777e0c.15.1796393196572; Fri, 04 Dec 2026 06:06:36 -0800 (PST) Received: from lvondent-mobl5 ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5f01d3bf678sm1062267e0c.9.2026.12.04.06.06.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 06:06:35 -0800 (PST) From: Luiz Augusto von Dentz 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 Message-ID: <20261204140627.1150694-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Luiz Augusto von Dentz 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 --- 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