Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH v5 0/4] Bluetooth: find an RPA peer's connection parameters by its identity
@ 2026-11-25 23:36 Radek Podgorny
  2026-11-25 23:36 ` [PATCH v5 1/4] Bluetooth: copy the peer's RPA out of its IRK under RCU Radek Podgorny
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Radek Podgorny @ 2026-11-25 23:36 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Luiz Augusto von Dentz, Johan Hedberg, Jakub Pawlowski,
	linux-bluetooth, linux-kernel, Radek Podgorny

v5 folds the lookup of a peer's connection by its IRK into
hci_conn_hash_lookup_le(), as Luiz asked on v4:
https://lore.kernel.org/all/CABBYNZKCSXsMb++hkgxk+Q8m1W1=+=Z_sgpaQFgWtnSJg8G43w@mail.gmail.com/

Patch 4 is the v3 fix itself, and the first three fix bugs in
bluetooth-next that it exposes or sits next to. By patch:

1. hci_connect_le() keeps pointing into an IRK after leaving RCU.
2. A lookup by a peer's identity misses its connection while
   hci_connect_le() is creating it under an RPA, so a second
   advertising report before LE Create Connection is sent adds a
   second hci_conn.
3. A connect to a peer's RPA misses the connection the peer already
   has under its identity and adds a second hci_conn.
4. The connection is created with the controller-wide defaults instead
   of the peer's own parameters.

Patch 4 uses the helper added in patch 3, and patch 3 relies on patch 2
to find a connection still being created; patches 1 and 2 stand on
their own.

Tested on v7.3.5 (the flags tests need &c->flags there) on a Raspberry
Pi CYW43455 central and a peer on a resolvable private address whose
parameters are loaded under its identity: first with three successive
builds of v4 applied as one patch (17 of 17 connects), then with the v4
series for three days (6 of 6 forced reconnects and 179 connects in
normal use), and then with v5 through its review rounds, each build with
three forced reconnects, Device Flags by RPA and the probes below. Every
connect was one Create Connection to the RPA, including across RPA
rotations between attempts, and those captured on air carried the
identity's parameters. With the peer connected, an L2CAP connect to its
current or a previous RPA now finds the existing connection, as one to
its identity does; without the changes it adds a second hci_conn under
the RPA, which is never started. Not exercised: a controller that
resolves the address itself and a peer with no cached RPA, which this
controller and peer cannot reach, a second advertising report before LE
Create Connection is sent, and an IRK arriving while an explicit connect
to the RPA waits.

Known limitations, raised by local Sashiko runs and left as they are:

- Patch 2: if an explicit connect was made to a peer's RPA before its
  IRK was known, it is never started, and other connects to the peer,
  auto-connect included, get -EBUSY until whoever made it cancels it,
  which for Pair Device is userspace; Disconnect by the identity now
  cancels it too. Reusing that connection hands its
  reference to params that do not own it, and a connection of their
  own would be dialled to the same RPA, where le_conn_complete_evt(),
  which matches by address, completes the older one and leaves the new
  one blocking every LE connect after it, as happens without the
  series. Avoiding all of these needs the explicit connect moved to the
  identity when the IRK is added, which is a separate change.
- Patch 3: Cancel Pair Device by an RPA no longer finds a pairing
  started by that RPA when the IRK was already known. It now behaves as
  a pairing started by the identity, which is how bluetoothd starts one
  for a peer whose IRK it holds.

Per Documentation/process/generated-content.rst: the patches and this
message were done with the Claude coding assistant (model as in the
Assisted-by trailer). The prompts asked it to fix the issues Sashiko
raised on v2 and v3 and answer the questions raised on v3 and v4, then
to review the result with a local run of Sashiko and with Sashiko's
published review prompts run on the GLM model through the oh-my-pi
agent, and fix what that turned up. The author directed the work and
reviewed the result.

---
Changes in v5:
- hci_conn_hash_lookup_le(): fall back to finding the peer's
  connection by its IRK when given its identity, out of line since
  smp_irk_matches() is private to net/bluetooth (Luiz). This replaces
  le_conn_lookup_irk() in hci_connect_le() and hci_connect_le_scan()
- hci_connect_le(): refuse a connection found by IRK by comparing its
  dst with the address given
- hci_connect_le_scan(): refuse with -EBUSY a connection still
  scanning under an RPA with params of its own there, an explicit
  connect made before the IRK was known that is never started, instead
  of waiting on it (local Sashiko)
- hci_explicit_conn_params_set(): look up and add without resolving
  again, through __hci_conn_params_lookup() and a new
  __hci_conn_params_add(), and so does Load Connection Parameters after
  resolving once; Set Device Flags: a single add instead of a lookup
  and an add; hci_conn_params_del() and Unpair Device: match exactly,
  as before. An IRK arriving in between can then not make any
  of them act on the identity's entry (local Sashiko)
- hci_connect_le_scan_cleanup() and le_conn_complete_evt(): back to
  hci_conn_params_del(), which matches exactly; load_conn_param() looks
  the connection up with the type it resolved
- Set Device Flags: send Device Flags Changed for the identity whose
  flags changed when given an RPA (local Sashiko)
- New Connection Parameter: name the identity whose entry was updated
  when the connection is still under an RPA (local Sashiko)
- Patches 2 and 3 reordered: 2 is now the lookup and the refusal,
  3 the resolve in hci_connect_le_scan()
- Link to v4: https://patch.msgid.link/20261124-for-upstream-le-conn-params-rpa-v4-0-f2dd5eb41f45@podgorny.cz

Changes in v4:
- Split into four patches (Sashiko)
- hci_conn_params_add(): resolve once, through a new
  __hci_conn_params_lookup() that does not resolve again (Luiz, Sashiko)
- New patch 2, hci_connect_le_scan(): resolve an RPA to the identity
  first, so a connect to an RPA neither creates its connection under
  the RPA nor misses one the peer already has under its identity
  (Sashiko). Add Device, which Sashiko also named, accepts only
  identity addresses
- New patch 3, hci_connect_le(): when the lookup by address misses,
  find the peer's connection by IRK and refuse it with -EBUSY, also
  while scanning, since a scanning one then belongs to other params. A
  second advertising report before LE Create Connection is sent no
  longer creates a second hci_conn (Sashiko). This replaces v3's reuse
  of a scanning connection found by IRK, which patch 2 makes
  unnecessary
- New patch 1: copy the RPA dialled out of the IRK under RCU
- Rebased on bluetooth-next
- Link to v3: https://patch.msgid.link/20261109-for-upstream-le-conn-params-rpa-v3-1-ad1225bb0b8f@podgorny.cz

Changes in v3:
- Look up the identity instead of the RPA, not after it, so an entry
  left under the peer's pre-bonding RPA cannot shadow it
- Copy the identity out of the IRK under rcu_read_lock(), since SMP can
  free the IRK without hdev->lock
- hci_conn_params_add() stores a new entry under the identity
- load_conn_param() and load_conn_subrate(): look up the connection with
  the entry's address type
- hci_connect_le(): find the scanning connection of a connect made to
  the peer's RPA by its IRK, so it is not duplicated once the identity
  entry is pending, even after the peer has moved to a new RPA, and
  also when the controller resolved the advertisement or no RPA is
  cached for the peer. Kept in this patch, since on its own it would
  leave a reference imbalance until the lookup is fixed
- Rebased on bluetooth-next
- Link to v2: https://patch.msgid.link/20261104120138.1165715-1-radek@podgorny.cz

Changes in v2:
- Resolve the RPA inside hci_conn_params_lookup() rather than at the
  hci_le_create_conn_sync() call site, as Luiz suggested, so no other
  caller can miss the entry
- Rebased on bluetooth-next
- Link to v1: https://patch.msgid.link/20261101-for-upstream-le-conn-params-rpa-v1-1-988629a09bc9@podgorny.cz

---
Radek Podgorny (4):
      Bluetooth: copy the peer's RPA out of its IRK under RCU
      Bluetooth: find a peer's LE connection by its IRK as well
      Bluetooth: connect to an RPA as to the identity behind it
      Bluetooth: find an RPA peer's connection parameters by its identity

 include/net/bluetooth/hci_core.h |  33 +++---------
 net/bluetooth/hci_conn.c         | 110 +++++++++++++++++++++++++++++++++++++--
 net/bluetooth/hci_core.c         |  77 +++++++++++++++++++++++----
 net/bluetooth/hci_event.c        |  10 +++-
 net/bluetooth/mgmt.c             |  44 +++++++++-------
 5 files changed, 214 insertions(+), 60 deletions(-)
---
base-commit: ffb95458a0f70dcab620eee0ecf5ef7750971b2f
change-id: 20261101-for-upstream-le-conn-params-rpa-f2334fb5dda7

-- 
Radek Podgorny <radek@podgorny.cz>


^ permalink raw reply	[flat|nested] 8+ messages in thread
* [PATCH v4 1/4] Bluetooth: copy the peer's RPA out of its IRK under RCU
@ 2026-11-24  9:30 Radek Podgorny
  2026-11-24 18:48 ` Bluetooth: find an RPA peer's connection parameters by its identity bluez.test.bot
  0 siblings, 1 reply; 8+ messages in thread
From: Radek Podgorny @ 2026-11-24  9:30 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Luiz Augusto von Dentz, Johan Hedberg, Jakub Pawlowski,
	linux-bluetooth, linux-kernel, Radek Podgorny

hci_connect_le() looks up the peer's IRK with hci_find_irk_by_addr()
and points dst at irk->rpa, which it later copies into the connection.
The IRK list is RCU protected, and SMP removes and frees an IRK with
kfree_rcu() without hdev->lock, which is all the caller holds, so the
IRK can be freed while dst still points into it.

Copy the RPA out under rcu_read_lock() instead.

Fixes: 759331d7cc66 ("Bluetooth: Fix clearing SMP keys if pairing fails")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Radek Podgorny <radek@podgorny.cz>
---
 net/bluetooth/hci_conn.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 0b76df1bc5fd..8638b7f8dee3 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -1526,6 +1526,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 {
 	struct hci_conn *conn;
 	struct smp_irk *irk;
+	bdaddr_t rpa;
 	int err;
 
 	/* Let's make sure that le is enabled.*/
@@ -1564,11 +1565,14 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 		 * be resolved back into the original identity address
 		 * from the connect request.
 		 */
+		rcu_read_lock();
 		irk = hci_find_irk_by_addr(hdev, dst, dst_type);
 		if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
-			dst = &irk->rpa;
+			bacpy(&rpa, &irk->rpa);
+			dst = &rpa;
 			dst_type = ADDR_LE_DEV_RANDOM;
 		}
+		rcu_read_unlock();
 	}
 
 	if (conn) {

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [PATCH] Bluetooth: find an RPA peer's connection parameters by its identity
@ 2026-11-01 17:20 Radek Podgorny
  2026-11-01 19:57 ` bluez.test.bot
  0 siblings, 1 reply; 8+ messages in thread
From: Radek Podgorny @ 2026-11-01 17:20 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Luiz Augusto von Dentz, linux-bluetooth, linux-kernel,
	Radek Podgorny

hci_le_create_conn_sync() looks up the peer's connection parameters by
conn->dst. Those parameters are stored under the identity address, but
hci_connect_le() puts the peer's cached RPA in conn->dst, and
__hci_conn_add() no longer converts it back when the controller cannot
translate an identity address.

The lookup then misses, and the connection is created with the
controller-wide defaults in hdev instead of the peer's interval, latency
and supervision timeout.

Resolve an RPA to its identity before the lookup, as
hci_connect_le_scan_cleanup() does. The other lookups by conn->dst run
after le_conn_complete_evt() has resolved it, so they are not affected.

Measured with btmon on a BCM4345C0 against a bonded peer that rotates
RPAs, with stored parameters of interval 12, latency 0 and timeout 600,
over six forced reconnects each way on v7.3.1:

                       before          after
  requested interval   30.00-50.00 ms  15.00-15.00 ms
  requested timeout    420 ms          6000 ms
  link interval        48.75 ms        15.00 ms
  link timeout         420 ms          6000 ms

Fixes: 4914c4998961 ("Bluetooth: put the peer's on-air address on air when we cannot resolve")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Radek Podgorny <radek@podgorny.cz>
---
This is the follow-up to the on-air address patch that Luiz said could
be done on top:
https://lore.kernel.org/linux-bluetooth/CABBYNZ+5zV8xzhHZKTUQYT4tLHaZAO7iiKOEsKuT8aA+YNLjPQ@mail.gmail.com/

What this does not establish:
 - tested on v7.3.1, not on bluetooth-next; the patch applies to both
   and bluetooth-next was only build-tested
 - it shows the stored parameters now reach the controller. It does not
   show that links are more reliable: all twelve reconnects succeeded,
   with or without the patch

Per Documentation/process/generated-content.rst: the patch, this message
and the btmon test runs were done with the Claude coding assistant
(model as in the Assisted-by trailer). The prompts asked it to fix the
lookup miss named in the on-air address review, then to measure the
result on the author's van, with and without the patch. The author
directed the work and reviewed the result.
---
 net/bluetooth/hci_sync.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 9d829c20c31604..810d6f5143342a 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -7018,6 +7018,7 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 {
 	struct hci_cp_le_create_conn cp;
 	struct hci_conn_params *params;
+	struct smp_irk *irk;
 	u8 own_addr_type;
 	int err;
 	struct hci_conn *conn = data;
@@ -7057,7 +7058,16 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 
 	hci_dev_lock(hdev);
 
-	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
+	/* conn->dst may hold the peer's RPA, but connection parameters are
+	 * stored under its identity address.
+	 */
+	irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
+	if (irk)
+		params = hci_conn_params_lookup(hdev, &irk->bdaddr,
+						irk->addr_type);
+	else
+		params = hci_conn_params_lookup(hdev, &conn->dst,
+						conn->dst_type);
 	if (params) {
 		conn->le_conn_min_interval = params->conn_min_interval;
 		conn->le_conn_max_interval = params->conn_max_interval;

---
base-commit: 42b8c2d729268a65a2d98064e8dc59ff53646b14
change-id: 20261101-for-upstream-le-conn-params-rpa-f2334fb5dda7

-- 
Radek Podgorny <radek@podgorny.cz>


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

end of thread, other threads:[~2026-11-26  3:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 23:36 [PATCH v5 0/4] Bluetooth: find an RPA peer's connection parameters by its identity Radek Podgorny
2026-11-25 23:36 ` [PATCH v5 1/4] Bluetooth: copy the peer's RPA out of its IRK under RCU Radek Podgorny
2026-11-26  3:51   ` Bluetooth: find an RPA peer's connection parameters by its identity bluez.test.bot
2026-11-25 23:36 ` [PATCH v5 2/4] Bluetooth: find a peer's LE connection by its IRK as well Radek Podgorny
2026-11-25 23:36 ` [PATCH v5 3/4] Bluetooth: connect to an RPA as to the identity behind it Radek Podgorny
2026-11-25 23:36 ` [PATCH v5 4/4] Bluetooth: find an RPA peer's connection parameters by its identity Radek Podgorny
  -- strict thread matches above, loose matches on Subject: below --
2026-11-24  9:30 [PATCH v4 1/4] Bluetooth: copy the peer's RPA out of its IRK under RCU Radek Podgorny
2026-11-24 18:48 ` Bluetooth: find an RPA peer's connection parameters by its identity bluez.test.bot
2026-11-01 17:20 [PATCH] " Radek Podgorny
2026-11-01 19:57 ` 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