[PATCH v4 2/4] Bluetooth: connect to an RPA as to the identity behind it

From: Radek Podgorny

Date: Tue Nov 24 2026 - 04:31:34 EST


A connect made to a peer's RPA through hci_connect_le_scan(), from Pair
Device, an L2CAP socket or a CIS, looks for an existing connection by
that RPA only. A bonded peer's established connection is kept under its
identity, so the lookup misses it and a second hci_conn is created
under the RPA, which waits until the connect times out. An L2CAP
connect to the RPA of a connected peer does exactly that on v7.3.5.
The connect also marks params under the RPA for itself, while the
peer's advertising reports are resolved to its identity before
check_pending_le_conn() looks params up, so it is never started either.

Resolve an RPA to the identity first, through a new
hci_rpa_to_identity(), so the connect proceeds as one made to the
identity: it finds the peer's connection and marks the identity's
params. hci_connect_le() still puts the RPA on air when the controller
cannot resolve it. SMP can free the IRK without hdev->lock, so the
identity is copied out under rcu_read_lock().

A connection hci_connect_le() is already creating for the peer is kept
under the RPA it dials, which the lookup by the identity now misses.
Fall back to finding the peer's connection by its IRK, which also finds
it after the peer has moved to a new RPA.

Fixes: f75113a26008 ("Bluetooth: add hci_connect_le_scan")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Radek Podgorny <radek@xxxxxxxxxxx>
---
include/net/bluetooth/hci_core.h | 2 ++
net/bluetooth/hci_conn.c | 57 ++++++++++++++++++++++++++++++++++++++++
net/bluetooth/hci_core.c | 20 ++++++++++++++
3 files changed, 79 insertions(+)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae104..28eb79dd51f0 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -1927,6 +1927,8 @@ int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
u8 type);
void hci_bdaddr_list_clear(struct list_head *list);

+void hci_rpa_to_identity(struct hci_dev *hdev, bdaddr_t **addr,
+ u8 *addr_type, bdaddr_t *id_addr);
struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
bdaddr_t *addr, u8 addr_type);
struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 8638b7f8dee3..c1583cb6c28a 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -1520,6 +1520,54 @@ u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
return 0;
}

+/* A connection to a peer the controller cannot resolve is kept under the
+ * peer's RPA until it completes, and the peer may have moved to a new RPA
+ * since. Match it by the peer's IRK instead of by address.
+ *
+ * This function requires the caller holds hdev->lock.
+ */
+static struct hci_conn *le_conn_by_irk(struct hci_dev *hdev,
+ const u8 irk_val[16])
+{
+ struct hci_conn_hash *h = &hdev->conn_hash;
+ struct hci_conn *c;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(c, &h->list, list) {
+ if (c->type != LE_LINK ||
+ !hci_bdaddr_is_rpa(&c->dst, c->dst_type))
+ continue;
+
+ if (smp_irk_matches(hdev, irk_val, &c->dst)) {
+ rcu_read_unlock();
+ return c;
+ }
+ }
+ rcu_read_unlock();
+
+ return NULL;
+}
+
+/* Find the LE connection of the peer at addr by the peer's IRK, for when it
+ * is kept under one of the peer's RPAs instead of addr.
+ *
+ * This function requires the caller holds hdev->lock.
+ */
+static struct hci_conn *le_conn_lookup_irk(struct hci_dev *hdev,
+ bdaddr_t *addr, u8 addr_type)
+{
+ struct hci_conn *conn = NULL;
+ struct smp_irk *irk;
+
+ rcu_read_lock();
+ irk = hci_find_irk_by_addr(hdev, addr, addr_type);
+ if (irk)
+ conn = le_conn_by_irk(hdev, irk->val);
+ rcu_read_unlock();
+
+ return conn;
+}
+
struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
u8 dst_type, bool dst_resolved, u8 sec_level,
u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
@@ -1787,6 +1835,7 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
enum conn_reasons conn_reason)
{
struct hci_conn *conn;
+ bdaddr_t id_addr;

/* Let's make sure that le is enabled.*/
if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
@@ -1796,6 +1845,12 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
return ERR_PTR(-EOPNOTSUPP);
}

+ /* Connect to the peer as if by its identity, under which its
+ * connection and parameters are kept. hci_connect_le() puts its RPA
+ * on air when the controller cannot resolve it.
+ */
+ hci_rpa_to_identity(hdev, &dst, &dst_type, &id_addr);
+
/* Some devices send ATT messages as soon as the physical link is
* established. To be able to handle these ATT messages, the user-
* space first establishes the connection and then starts the pairing
@@ -1806,6 +1861,8 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
* and return the object found.
*/
conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
+ if (!conn)
+ conn = le_conn_lookup_irk(hdev, dst, dst_type);
if (conn) {
if (conn->pending_sec_level < sec_level)
conn->pending_sec_level = sec_level;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 024960e9af66..a87c92dccd4f 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -2359,6 +2359,26 @@ int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
return 0;
}

+/* A peer we hold the IRK of is tracked under its identity address, so
+ * point *addr and *addr_type at the identity behind an RPA we can resolve.
+ * SMP can free the IRK without hdev->lock, so the identity is copied to
+ * @id_addr before leaving the RCU read-side section.
+ */
+void hci_rpa_to_identity(struct hci_dev *hdev, bdaddr_t **addr,
+ u8 *addr_type, bdaddr_t *id_addr)
+{
+ struct smp_irk *irk;
+
+ rcu_read_lock();
+ irk = hci_get_irk(hdev, *addr, *addr_type);
+ if (irk) {
+ bacpy(id_addr, &irk->bdaddr);
+ *addr = id_addr;
+ *addr_type = irk->addr_type;
+ }
+ rcu_read_unlock();
+}
+
/* This function requires the caller holds hdev->lock */
struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
bdaddr_t *addr, u8 addr_type)

--
2.56.1