Commit graph zephyr/subsys/bluetooth
Author SHA1 Message Date
Johan Hedberg
2315640795 Bluetooth: GATT: Take the host lock for the notification batches
A connection's pending notification batch is appended to and sent by
the notifying thread, sent by the flush work on the Bluetooth workqueue
and dropped by the disconnection cleanup, with no serialization. A
flush that preempts a notifying thread between its check of the batch
and the append leaves that thread appending through a NULL pointer or
to a buffer that is being sent. A batch whose allocation raced with the
disconnection cleanup is stored after it and never dropped, and the
flush work passes it to bt_att_send() with a NULL connection once the
connection object has been released.

Hold the host lock for every access. The ATT buffer allocation can
block and stays outside of it, so the connection state and a batch
started meanwhile by another thread are checked again before the new
batch is stored.

The races need preemption at specific points, which bsim does not
reproduce; the notify_multiple bsim scenarios pass.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 18:16:23 +02:00
Johan Hedberg
5a3453053c Bluetooth: GATT: Keep notification batches within the ATT_MTU
gatt_notify_mult() appends a notification to the pending batch as long
as the ATT buffer has room, but the buffers are sized for the local
ATT_MTU and the negotiated one can be smaller, for example when the
peer has not exchanged the ATT_MTU. The batch then exceeds the ATT_MTU:
an EATT bearer rejects such a PDU, so a batch restricted to EATT is
never sent. Flush the batch also when the next notification would take
it past the ATT_MTU.

The batch MTU scenario added to the GATT notify_multiple bsim test in
the next commit fails without this change.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 18:16:23 +02:00
Johan Hedberg
2afb7444c0 Bluetooth: GATT: Count every value header in bt_gatt_notify_multiple()
bt_gatt_notify_multiple() compares the ATT_MTU with the total value
length plus a single handle and length header, leaving out the headers
of all other values. A PDU that exceeds the ATT_MTU by up to
4 * (num_params - 1) octets is accepted, and when the ATT_MTU is close
to the local maximum, appending the values overflows the ATT buffer.
Count the opcode and every header, so that the call fails with -ERANGE
as documented, and check the 16-bit limit after every value, as the
headers could otherwise make the sum wrap on 32-bit targets.

The MTU scenario added to the GATT notify_multiple bsim test in the
next commit fails without this change.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 18:16:23 +02:00
Johan Hedberg
d0dd3f6c24 Bluetooth: GATT: Free a failed notification batch only once
bt_att_send() consumes the buffer also when it fails: it unrefs it when
the connection has no connected ATT bearer. gatt_notify_mult_send()
unrefs it again, so a notification batch flushed while the connection
is disconnecting frees its buffer twice, which the net_buf double free
assertion catches. Return the bt_att_send() result as its other callers
do.

The disconnect scenario added to the GATT notify_multiple bsim test in
the next commit fails without this change.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 18:16:23 +02:00
Johan Hedberg
c85c20c6d5 Bluetooth: Controller: Check the extended header length before parsing
The extended advertising parsers followed the fields that an extended
header's flags announce before confirming the header was long enough
to hold them, so a PDU whose flags announced more than its extended
header length held, or an extended header length larger than the PDU,
was parsed past its contents.

Confirm that the header fits the PDU and holds every field its flags
announce before walking it, and take nothing from the PDU otherwise,
in lll_scan_aux_setup(), both ull_scan_aux_setup() implementations and
the HCI report builders. An AUX_CHAIN_IND reaches these parsers
without going through the AUX_ADV_IND checks, so the check is in the
parsers rather than only in lll_scan_ext_tgta_check().

The periodic advertising report of such a PDU ends with the Incomplete
data status, as on a failed chain reception: the PDU can follow a
Partial report of its chain, which the Host would otherwise keep.

Valid PDUs are unaffected: a complete extended header, and a PDU
without one, pass the check.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 12:29:19 +02:00
Johan Hedberg
4e47e711d6 Bluetooth: Controller: Send no Incomplete periodic report while disabled
The Controller ends a periodic advertising report with the Incomplete
data status and no data when it fails to receive the rest of a chain,
or when the chain holds more data than CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX.
It sent that report also after the Host had disabled the reports of the
train (Core Spec Vol 4, Part E, Section 7.8.88), as the LLL receives
and follows the chains whether reporting is enabled or not, and only
the other reports checked it.

Send it only while reporting is enabled. The node that reports a failed
chain reception does not record whether reporting was enabled when the
PDUs were received, so the current state of the sync is checked.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 12:29:19 +02:00
Johan Hedberg
6872e86aa9 Bluetooth: Controller: Require the AdvA in scannable AUX_ADV_INDs
Connectable and scannable advertising has no AdvA in its ADV_EXT_IND
(Core Spec Vol 6, Part B, Table 2.4), so its AUX_ADV_IND has to have
one (Table 2.5). lll_scan_ext_tgta_check() did not require it in an
auxiliary PDU, so an active scanner sent an AUX_SCAN_REQ in answer to
an AUX_ADV_IND without AdvA, with other octets of the PDU as the AdvA.

Reject an auxiliary PDU of connectable or scannable advertising that
has no AdvA.

Tested with samples/bluetooth/observer (prj_extended.conf) on
nrf52_bsim against a BabbleSim playback device that sends such PDUs,
and with tests/bsim/bluetooth/ll/advx and
tests/bsim/bluetooth/host/adv on nrf52_bsim.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-22 12:29:19 +02:00
Vinayak Kariappa Chettimada
4cd3fbf608 Bluetooth: Controller: Fix scan stop assertion
Fix assertion on scan stop waiting for semaphore that was
not given under race conditions wherein a scan event present
in the prepare pipeline did not abort, but remained in
continuous scanning.

ASSERTION FAIL [err == 0 || err == -114] @
  WEST_TOPDIR/zephyr/subsys/bluetooth/controller/ll_sw/ull_scan.c:657
	param1: 0 param2: 4294967285
@ WEST_TOPDIR/zephyr/subsys/bluetooth/controller/ll_sw/ull_scan.c:657

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
2026-10-22 09:41:47 +02:00
Johan Hedberg
49502d57b1 Bluetooth: Controller: nordic: Fix preempted ECB use with EasyVDMA
A do_ecb() call preempted by an ISR that runs do_ecb() itself, such as
the radio ISR resolving a TargetA while a thread encrypts with
bt_encrypt_le(), relies on the preempting call clearing ECBDATAPTR to
start over. On SoCs with EasyVDMA (nRF54L, nRF54H20, nRF7120), the key
is in separate registers and ECBDATAPTR is IN.PTR, which is set after
the key:

- Preempted between its key writes, the call keeps the words that the
  preempting call wrote in place of its own earlier ones, then sets
  IN.PTR, and returns a result computed with a mix of both keys.
- Preempted after setting IN.PTR but before starting, it starts the
  ECB with IN.PTR cleared, which the nrf54l15bsim model reports as a
  hard fault on hardware.

Count the attempts, so that a call starts over whenever another one ran
in between, and have each call restore the pointers it found instead of
clearing them, so that a preempted call always starts with its own.

Tested with a scratch build that runs a nested ecb_encrypt() at each
point of do_ecb(), as a preempting ISR would: on nrf54l15bsim,
preemption during the key writes gave a wrong result and preemption
between the IN.PTR write and the start hit the model's NULL IN.PTR
error, and now every point gives the right result; on nrf52_bsim every
point gives the right result before and after. The bsim ll/conn,
ll/bis, ll/edtt, ll/multiple_id and ll/advx tests pass on both, and
host/security and host/privacy on nrf52_bsim. Not run on hardware.

Fixes #122777

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 15:02:24 -05:00
Johan Hedberg
f15830d16e Bluetooth: Controller: nordic: Don't let threads interleave in do_ecb()
do_ecb() relies on a call that preempts it completing before it
continues, as an ISR's does: the preempting call clears ECBDATAPTR when
done, and the preempted one then starts over. Threads of equal priority
can interleave instead, under time slicing, which is enabled by
default. One thread can then reprogram the ECB and clear ECBDATAPTR
while another one is still programming it, so that the other one starts
the ECB with ECBDATAPTR cleared, or on SoCs with EasyVDMA encrypts with
a mix of both keys.

Lock the scheduler while a thread runs do_ecb(), so that only ISRs can
preempt it, and their calls nest.

Fixes #122777

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 15:02:24 -05:00
Johan Hedberg
588ab4db8d Bluetooth: Controller: Fix RX thread stack sizes with NO_OPTIMIZATIONS
With CONFIG_NO_OPTIMIZATIONS=y, a combined Host and Controller build
overflows the Controller's priority RX thread stack (448 bytes) during
bt_enable(), and its RX thread needs more than its 896 bytes as well,
already before a connection. Both symbols are hidden, so they cannot
be set from a configuration file such as prj.conf.

Unoptimized peaks in samples/bluetooth/peripheral_hr on
nrf52dk/nrf52832 with pairing and notifications, optimized in
parentheses: priority RX thread 956 (312), RX thread 972 (304) bytes.
Default both to 2048 bytes with NO_OPTIMIZATIONS.

Tested on nrf52dk, unoptimized, MPU stack guard only, with the Host's
thread stacks and the main and system workqueue stacks raised:
peripheral_hr faults in the priority RX thread at boot without this
change and runs with it. Optimized .config files are unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 15:01:20 -05:00
Aleksandr Khromykh
378b6c2a30 bluetooth: mesh: reject BLOB Block Status with zero chunk size
handle_block_status() divides the current block size by the Chunk Size
field of the received BLOB Block Status without checking it. A Block
Status with Chunk Size 0 from a transfer target causes an integer
division by zero, which faults on cores that trap on it.

Chunk Size 0 is Prohibited (MshMBTv1.0 section 3.1.2.3), and a message
carrying a Prohibited value is not understood and shall be ignored
(MshPRT section 3.7.3.4). Reject such a message before computing the
chunk count. 0xFFFF (Chunk Size not set) is still accepted, as servers
send it in error responses.

Assisted-by: Copilot:claude-opus-5.5
Signed-off-by: Aleksandr Khromykh <aleksandr.khromykh@nordicsemi.no>
2026-10-21 14:58:56 -05:00
Johan Hedberg
f6525ff3f7 Bluetooth: Controller: Report LE Encrypt only with BT_CTLR_CRYPTO
Since commit a66baa1101 ("Bluetooth: Controller: Introduce
BT_CTLR_CRYPTO_SUPPORT") the HCI LE Encrypt command is handled only
with BT_CTLR_CRYPTO, but Read Local Supported Commands reports it in
every build. With BT_CTLR_CRYPTO=n, a Host that relies on the report
gets Unknown HCI Command.

Set the LE Encrypt bit only with BT_CTLR_CRYPTO.

Tested: on nrf52_bsim, Read Local Supported Commands reports LE Encrypt
with the default configuration and not with BT_CTLR_CRYPTO=n, and LE
Rand with both.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 13:46:41 +02:00
Johan Hedberg
2e62ddf65e Bluetooth: Controller: Fix builds with BT_CTLR_CRYPTO disabled
BT_CTLR_CRYPTO selects whether the Controller provides bt_rand(),
bt_encrypt_le() and HCI LE Encrypt, and with them the ECB driver that
the Zephyr Link Layer also needs for LE encryption and privacy. Its
LLLs indicate support for both features whenever the Controller has
crypto support at all, so a build with BT_CTLR_CRYPTO=n still enables
them:

- LE encryption derives the session key with ecb_encrypt(), which the
  Nordic LLL builds only with BT_CTLR_CRYPTO since commit a66baa1101
  ("Bluetooth: Controller: Introduce BT_CTLR_CRYPTO_SUPPORT"), so every
  such build with BT_CTLR_LE_ENC fails to link. On rv32m1_vega, the
  board enables the CAU3 driver that it uses only with BT_CTLR_CRYPTO.
- Privacy calls bt_rand() and bt_encrypt_le() through rpa.c, which are
  then the Host's: from the radio ISR, and from HCI command handling,
  where with BT_HOST_CRYPTO_PRNG=n bt_rand() sends HCI LE Rand from
  within the command being handled and deadlocks. HCI-only builds have
  no bt_rand() at all and stop at configuration time, as
  BT_CTLR_PRIVACY selects BT_RPA, which depends on BT_HOST_CRYPTO or
  BT_CTLR_CRYPTO.

Have the Nordic and OpenISA LLLs indicate support for LE encryption and
privacy only with BT_CTLR_CRYPTO, and say so in its help text.

Tested with BT_CTLR_CRYPTO=n: samples/bluetooth/peripheral builds on
nrf52840dk/nrf52840, nrf51dk/nrf51822, nrf54l15dk/nrf54l15/cpuapp and
nrf52_bsim, samples/bluetooth/central on nrf52840dk/nrf52840, hci_ipc on
nrf5340dk/nrf5340/cpunet, hci_uart on nrf52840dk/nrf52840, and
tests/bluetooth/init with prj_ctlr_central_priv.conf on
rv32m1_vega/openisa_rv32m1/ri5cy. The default configurations of these
builds are unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 13:46:41 +02:00
Johan Hedberg
c79ea22c31 Bluetooth: Remove unreachable Controller crypto calls from rpa.c
rpa.c calls lll_csrand_get() and ecb_encrypt() directly when both
BT_CTLR_CRYPTO and BT_HOST_CRYPTO are enabled. That combination cannot
link, as both crypto implementations define bt_rand() and
bt_encrypt_le(), and Kconfig now rejects it, so the branch is dead.
With privacy enabled it does not compile either, as rpa.c does not
include lll.h.

Call bt_rand() and bt_encrypt_le() unconditionally, which is what
every configuration that links has done since commit a66baa1101
("Bluetooth: Controller: Introduce BT_CTLR_CRYPTO_SUPPORT").

Tested: the EAD samples on nrf52840dk/nrf52840 and
samples/bluetooth/peripheral with BT_CTLR_CRYPTO=n and BT_CTLR_LE_ENC=n
build; the bsim host/privacy and host/adv/encrypted tests pass.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 12:02:48 +02:00
Johan Hedberg
817f941024 Bluetooth: Host: Make BT_HOST_CRYPTO depend on !BT_CTLR_CRYPTO
In a combined build, host/crypto_psa.c and controller/crypto/crypto.c
both define bt_rand(), bt_encrypt_le() and bt_encrypt_be(), so
BT_HOST_CRYPTO=y together with BT_CTLR_CRYPTO=y fails to link with
multiple definitions. The option only had a conditional default, which
left the combination open to be set by hand.

Make the dependency explicit, so that Kconfig rejects the combination
with a warning. No configuration that links changes value.

Tested: samples/bluetooth/beacon on nrf52840dk/nrf52840 with
-DCONFIG_BT_HOST_CRYPTO=y gives a Kconfig warning and links.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 12:02:48 +02:00
Vinayak Kariappa Chettimada
1cfd5c0952 Bluetooth: Controller: Fix incorrect aux_offset used when scanning
Fix incorrect aux_offset used when Extended Scanning using
the single timer implementation of tIFS software switching.

The Radio END timestamp was not captured and hence the
packet timing was not accumulated. Passive scanning on the
primary channel reception for single timer implementation
did not config the DPPI that was used for Timer Clear and
Radio END capture.

The bug was present when LLL scheduling was used for
auxiliary PDU reception, where the auxiliary PDU followed
closely after the primary channel PDU.

Relates to commit ebc023b0ce ("Bluetooth: Controller: Fix
incorrect aux offset using single timer").

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
2026-10-21 12:01:00 +02:00
Johan Hedberg
f3fb71df0b Bluetooth: Fix default thread stack sizes on arm64
The default stack sizes of the Bluetooth threads are too small on arm64.
With CONFIG_STACK_SENTINEL=y, tests/bluetooth/host_disable overflows the
stacks of bt_tx_processor (1188 bytes used, default 1024) and BT LW WQ
(1540 used, default 1400 with BT_ECC) on qemu_cortex_a53, and BT RX WQ
uses 1028 of its 1200.

arch/arm64/core/Kconfig raises the kernel's stack defaults to 4096 on
arm64, the system workqueue's among them. Give the TX processor, the
Bluetooth workqueue and the long workqueue the same default. It comes
first in each list (for the long workqueue after NO_OPTIMIZATIONS, also
4096), so on arm64 it takes precedence over the feature-dependent values
that follow, and the other architectures keep theirs.

Tested on qemu_cortex_a53 with the stack sentinel and the thread
analyzer: no Bluetooth thread overflows in any scenario of the test.

Fixes #122573

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:59:59 +02:00
Johan Hedberg
8cab85f0d4 Bluetooth: Fix the copyright holder of files from Silicon Labs
These files were written for Silicon Laboratories, which holds their
copyright, but were added with the line for The Zephyr Project
Contributors. They came with #119615, #120846, #121257 and #122021, and
nobody has changed them since. The build and test scaffolding that came
with them, and a file mostly copied from another test, keep that line.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:56:53 +02:00
Johan Hedberg
04b3be4813 Bluetooth: Host: Fix build for SoCs without soc.h
hci_core.c includes <soc.h>, so the Host does not build for a SoC that
has none, such as qemu_cortex_a53. Nothing in the file uses the header
since commit aaeff3c165
("Bluetooth: Add basic host-side support for HCI vendor extensions")
replaced the nRF5x FICR read it came with. Remove the include.

Built for qemu_cortex_a53, which fails without this change, and for
ESP32, Silabs, NXP, STM32WB, nRF and qemu_x86 targets.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:55:10 +02:00
Johan Hedberg
40275ef7af Bluetooth: Classic: AVRCP: Fix TG vendor response queue corruption
bt_avrcp_tg_vendor_tx_work() hands a single-packet response to
bt_avctp_send() while the buffer is still the head of
tg->vd_rsp_tx_pending, and removes it from that list only afterwards.
By then bt_avctp_send() has linked the same buf->node into the AVCTP TX
list, so with another response pending the removal leaves the list
with a tail but no head: the responses queued behind it and every
vendor dependent response the TG sends later are never sent, and their
buffers are lost.

Remove the buffer from the pending list before handing it to AVCTP.

Tested with a Bumble CT against the BR shell on native_sim over the
simulated BR/EDR controllers: with two CHANGED notifications queued
behind a fragmented GetElementAttributes response, only the first one
arrived and no later response did; with the fix all of them arrive.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:53:14 +02:00
Johan Hedberg
407aad9483 Bluetooth: L2CAP: Fix TX stall behind a channel without credits
When the channel that the TX processor pulls from has no credits for a
new K-frame, the channel is taken off the ready list and the pull
returns nothing, without triggering the processor again as the other
empty paths of the pull do. Another channel of the connection with data
to send then waits for a completion, credits or new data, which may
never come, for instance when the peer waits for that data before it
gives credits.

Trigger the TX processor again, so that it pulls from the next channel.

Tested with a unit test that queues a signalling response behind an SDU
on a channel without credits: without this change the response is not
sent.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:52:48 +02:00
Johan Hedberg
68a2e85099 Bluetooth: Host: Fix advertising sent callback on connection
bt_le_ext_adv_cb.sent is documented to report that an advertising set
reached its timeout or event limit. It was also called, right after
the connected callback, when a connection ended a set that had been
started with a limit, which an application could only take for a
timeout.

Call it only when the status of the LE Advertising Set Terminated
event is not success. Core Specification 6.3, Vol 4, Part E, 7.7.65.18
defines success as a connection having been created and every other
status as advertising having ended for another reason. Bluetooth Mesh,
the one in-tree user acting on the callback, passes its PB-GATT bsim
tests, which end limited sets with a connection.

Fixes #121951

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-21 11:44:08 +02:00
Flavio Ceolin
fb6b996d28 Bluetooth: Mesh: Check state before handling BLOB Block Status
handle_block_status() looks up the sender in the transfer target list
before checking the client state. The target list is reached through
cli->inputs, which is only set once a procedure is started, so a Block
Status received by a client that never started one dereferences a NULL
pointer.

Ignore Block Status messages unless the client is in one of the block
states, as rx_block_status() already requires, and make target_get()
return NULL when no inputs are set.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Flavio Ceolin <flavio@hubble.com>
2026-10-20 17:37:08 +02:00
Johan Hedberg
7d3ec35414 Bluetooth: Host: Fix NULL write when limited advertising ends
With CONFIG_BT_RPA_SHARING, adv_rpa_expired() records in an array that
an advertising set wants to keep its RPA, so that le_rpa_invalidate()
rotates the RPA of an identity for all of its sets or for none. When
limited advertising is stopped or times out, bt_id_adv_limited_stopped()
passes no array, and an rpa_expired callback that returns false made
the Host write through a NULL pointer.

The end of limited advertising rotates no shared RPA, so there is
nothing to record then.

Introduced by commit 6ce38c1aa7 ("bluetooth: id: fix adv sets with
same id use different RPA"), first released in v3.7.0.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-20 17:30:34 +02:00
Johan Hedberg
5a8cae9c71 Bluetooth: Host: Add quirk for adv timeout without terminated event
The timeout of high duty cycle connectable directed advertising that was
enabled with the extended advertising commands may be reported with a
connection complete event instead of, or in addition to, the LE
Advertising Set Terminated event (Core Specification 6.3, Vol 4, Part E,
7.8.56). The Host acts on the terminated event only. With a Controller
that sends the connection complete alone the advertising set stays
enabled, the application gets no connected callback with
BT_HCI_ERR_ADV_TIMEOUT, and every further start returns -EALREADY.

Acting on the connection complete in general is not safe. With a
Controller that sends both events, the terminated event would end
whatever the application started in between, and the connection complete
does not name the advertising set. Add
BT_HCI_QUIRK_NO_ADV_TERM_ON_DIR_TIMEOUT for Controllers known to send
the connection complete alone. With it the Host ends the enabled high
duty cycle directed advertising set at that event, through the code that
handles the terminated event, and refuses to start a second such set
while one is enabled. A terminated event for such a set shows that the
quirk is wrong for the Controller, and the Host warns about it. The
quirk is read from devicetree at build time, so builds without it are
unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-20 17:29:34 +02:00
Arkadiusz Grzelka
9bb3553b74 Bluetooth: BAP: BSNK: Report decrypting state after BIG resync
When the BIG sync is lost or terminated for any reason other than a MIC
failure, the BIG_Encryption field of the receive state is set to
Not_Encrypted, but the sink keeps its BIG encrypted flag. The BIGInfo
handler only updates the receive state when that flag changes, and the
BIG synced handler only changed Broadcast_Code_Required and Bad_Code to
Decrypting. A sink that synced to the same encrypted BIG again thus
reported Not_Encrypted while decrypting the BIG.

Set the BIG_Encryption field to Decrypting on BIG sync whenever the BIG
was synced with encryption.

Extend the broadcast_sink_encrypted bsim test to stop and sync the sink
again, and verify that the receive state reports Decrypting after both
syncs.

Assisted-by: Claude:claude-opus-5.5
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
2026-10-20 17:25:27 +02:00
chao an
3161e877c8 Bluetooth: BAP: Reset client buffer on send failures
Reset the unicast client ATT buffer when control point PDU preparation
or write submission fails before the GATT layer can consume it.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
ae5d31703d Bluetooth: AVRCP: Free buffers on send failure
Release locally allocated vendor PDUs when sending them fails so the
failed AVRCP command path does not leak buffers. Avoid an extra unref in
send_single_vendor_rsp() so the response path does not release the
buffer twice after send failure cleanup.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
2a4c0f32d4 Bluetooth: Classic: HFP: Free command buffer on format failure
Release the RFCOMM command buffer when formatting the HFP HF command
fails so the allocated buffer is not leaked on the error path.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
b6de45bbdb Bluetooth: AVRCP: Unregister SDP records on failure
Roll back AVRCP controller and target SDP records if the later AVCTP
server registration fails so failed role registration leaves no record.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
b4072a8dce Bluetooth: RFCOMM: Use safe iteration when dropping DLCs
Use safe list iteration when starting pending DLCs because the failure
path drops the current DLC and removes it from the session list.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
294c91ff95 Bluetooth: PBAP: Unregister SDP record on PSE failure
Roll back the PBAP PSE SDP record when OBEX server registration fails
so that a failed registration does not leave a stale service record.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
52dcc3764f Bluetooth: CSIP: Reset instance on register failure
Clear the generated service state if GATT service registration fails so
that the instance can be selected again by a later registration attempt.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
a63dc1070e Bluetooth: Classic: L2CAP: Remove channels before delete
Remove BR/EDR L2CAP channels from the connection list before deleting
them during disconnection so stale list entries are not left behind.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
5bd3a24034 Bluetooth: L2CAP: Remove channels from connection list
Remove each L2CAP channel from the connection list before deleting it
during disconnection so that stale list entries are not left behind.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
chao an
eab427e94d Bluetooth: ATT: Free context on channel allocation failure
Release the ATT context when the fixed channel cannot be allocated so
that the slab entry is not leaked on the error path.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2026-10-19 16:33:21 +02:00
Arkadiusz Grzelka
7377b46fa3 Bluetooth: Controller: Release held PDUs on BIG sync terminate
An ISO Synchronized Receiver stores PDUs received ahead of their event
in the lll->payload[][] sliding window until isr_rx_done() enqueues them
towards the Host. PDUs still held there when the BIG sync is terminated,
locally or by the remote, were never released: lll_sync_iso_flush() was
empty and ull_sync_iso_setup() clears the pointers on the next
synchronization. Each termination could leak ISO Rx buffers until too
few remained to receive, and reception was then skipped without
reporting lost SDUs.

Release the held PDUs in lll_sync_iso_flush(), which runs in LLL context
once the BIG sync event has been disabled, by enqueuing them marked as
NODE_RX_TYPE_RELEASE.

Tested with the BIS re-sync bsim test added in the next commit, which
fails without this change on nrf52_bsim and nrf5340bsim cpunet.

Tested on two nRF5340 Audio DKs, a broadcast sink following a source
that re-creates its BIG. Without this change both BISes lose most of
their SDUs after each re-sync. With it they are received at full rate.

Assisted-by: Claude:claude-opus-5.5
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
2026-10-19 16:27:43 +02:00
Aric Radzin
ccdc53e302 Bluetooth: Host: Imply MBEDTLS_AES_ROM_TABLES for Host crypto
CONFIG_BT_CRYPTO, CONFIG_BT_GATT_CACHING and the Mesh crypto imply
CONFIG_MBEDTLS_AES_ROM_TABLES, but CONFIG_BT_HOST_CRYPTO, which
provides bt_encrypt_le(), does not. A Host without SMP has none of the
others, so its first AES operation generates the AES tables in RAM.
The generation also needs 512 bytes of stack in the calling thread.

Imply the ROM tables for the Host crypto as well when Mbed TLS is the
PSA Crypto provider, as the Mesh crypto does. For an image that uses
AES this trades 2096 bytes of flash for 8748 bytes of RAM on
qemu_cortex_m3. Images that do not use AES are unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
2026-10-19 10:27:45 +02:00
Johan Hedberg
cb79d6e62a Bluetooth: Classic: L2CAP: Fix echo request left pending on send failure
When sending an Echo Request failed, bt_l2cap_br_echo_req() kept its
identifier as the pending request, so every later Echo Request on that
connection returned -EBUSY. It also returned the buffer with the
signaling header, and possibly the L2CAP header, still pushed, although
the caller keeps the buffer on error. bt_l2cap_br_echo_rsp() had the
same buffer problem.

Clear the pending identifier and restore the buffer when sending fails,
and document that the caller gets the buffer back unchanged.

Tested with tests/bluetooth/host_l2cap_br_echo on native_sim.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-19 10:18:42 +02:00
Johan Hedberg
74b892fb56 Bluetooth: Classic: L2CAP: Fix data seen by later echo/connless callbacks
Several Echo and connectionless callbacks can be registered, and all of
them receive the same buffer, so a callback that pulled data left the
ones after it with less or none. Restore the buffer after each
callback.

The next callback and the signaling loop then move the buffer's cursor
again, so the callback documentation now says the buffer is only valid
until the callback returns.

Tested with tests/bluetooth/host_l2cap_br_sig on native_sim.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-19 10:18:27 +02:00
Johan Hedberg
e19af035b6 Bluetooth: Classic: L2CAP: Fix echo data including following commands
A signaling packet may carry several commands, but
l2cap_br_sig_handle() handed each command's handler the buffer up to
the end of the packet. Echo Request and Echo Response callbacks
therefore got any commands after the Echo as part of its data.

Limit the buffer to the command's own length while it is handled.

Tested with tests/bluetooth/host_l2cap_br_sig on native_sim.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-19 10:18:27 +02:00
Johan Hedberg
11228e4aff Bluetooth: Classic: L2CAP: Fix buffer freed while receiver owns it
A recv() callback returning -EINPROGRESS takes over the reference to the
buffer and releases it with bt_l2cap_chan_recv_complete(). On basic mode
channels, and on retransmission, flow control and streaming mode
channels without alloc_buf(), bt_l2cap_br_recv() released that same
reference after the callback, so the buffer was freed while the
application still used it and released a second time on completion.

Give the receiver a reference of its own and drop it unless recv()
returns -EINPROGRESS, as RFCOMM does for its receivers.

Tested with tests/bluetooth/host_l2cap_br_recv on native_sim, with and
without CONFIG_BT_L2CAP_RET_FC.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-19 10:18:11 +02:00
Lyle Zhu
ae04e49c9d Bluetooth: Classic: HID: Update comments for close_work handler
Update comments in HID host implementation to reflect current behavior
after L2CAP signaling buffer allocation improvements.

Remove outdated comment explaining that `hid_close_handler()` runs on
the `bt_workq` to avoid deadlock when calling
`bt_l2cap_br_chan_disconnect()` from L2CAP callback. This workaround
is no longer necessary since L2CAP BR/EDR signaling now uses retry
mechanism instead of `K_FOREVER` for buffer allocation.

Clarify that `close_work` is used for:
- INTR channel connect timeout when CTRL is up but device never opens
  INTR
- Closing the peer channel when one channel disconnects (normal
  teardown path)
- Closing INTR channel when it establishes while CTRL is not connected

Update both the function comment in hid_host.c and the struct field
comment in hid_internal.h for consistency.

Signed-off-by: Lyle Zhu <lyle.zhu@nxp.com>
2026-10-19 10:15:37 +02:00
Lyle Zhu
a12c268c8b Bluetooth: Classic: L2CAP: Add configurable signaling TX buffer count
Add a new Kconfig option `BT_L2CAP_SIG_TX_BUF_COUNT` to allow
independent configuration of L2CAP BR/EDR signaling channel TX buffer
pool size, separate from the connection count.

Previously, the signaling buffer pool size was fixed to
`CONFIG_BT_MAX_CONN`, which could lead to buffer exhaustion under
certain conditions. This change allows tuning the signaling buffer
pool independently to better handle memory pressure scenarios.

The new option defaults to `BT_L2CAP_TX_BUF_COUNT` and valid range
is 1 to 255 buffers.

Update `br_sig_pool` definition to use the new configuration option
instead of `CONFIG_BT_MAX_CONN`.

Signed-off-by: Lyle Zhu <lyle.zhu@nxp.com>
2026-10-19 10:15:37 +02:00
Lyle Zhu
935efcb731 Bluetooth: Classic: L2CAP: Remove K_FOREVER from sig chan buf alloc
Previously, a thread running on the `bt_workq` can deadlock while
waiting for a `br_sig_pool` buffer using `K_FOREVER`, because the
allocated buffer can only be released when it has been sent. For
instance, when `bt_l2cap_br_chan_del()` executes a channel's
`disconnected()` callback, `l2cap_br_disconn_req()` is already holding
the buffer for the `DISCONN_REQ` if the callback invokes
`bt_l2cap_br_chan_disconnect()` for another channel, the nested call
waits indefinitely for a buffer that cannot be released until the
outer call completes sending its response.

Add a retry mechanism to handle buffer allocation failures in L2CAP
BR/EDR signaling channel. When buffer allocation fails, the operation
is deferred and retried after a random interval to prevent unbounded
blocking of `bt_workq` and improve reliability.

Add `retry_work` delayed work to handle deferred operations. Implement
`l2cap_br_retry_handler()` to process pending operations.

Introduce `allocated_buf` to pre-allocate buffer for the response or
request.

Add flags to track pending retry operations, including `CONN_RSP`,
`CONFIG_REQ`, `CONFIG_REQ_RETRY`, `DISCONNECT_REQ`, `INFO_REQ`. Add
`BUILD_ASSERT` to verify L2CAP flags fit within available bitmap.

Modify signaling functions calling by the function
`l2cap_br_sig_handle()` to use pre-allocated buffer when the function
`l2cap_br_is_tx_buf_required` returns `true` value. Add `recv_pending`
buffer to handle incomplete L2CAP packets and schedule retries on
allocation failure in function `l2cap_br_recv()`.

Modify `l2cap_br_recv()` to queue incoming packets when a previous
packet is still being processed. Allocate from the memory slabs
`br_recv_pending_pool` and append to the pending queue.

Update `l2cap_br_retry_signaling_handler()` to process the current
pending buffer and automatically load the next queued buffer when
processing completes via `l2cap_br_recv_pending_done()`.

Add `l2cap_br_drop_recv_pending_bufs()` to properly clean up all
queued pending buffers. Ensure proper cleanup of allocated buffer,
free allocated memory slabs, and retry work on channel disconnect.

Also update the doxygen comments of the functions
`bt_l2cap_br_chan_connect()` and `bt_l2cap_br_chan_disconnect()`.

The changes are intended to shift the allocation strategy from a
blocking approach to a non-blocking one. When no buffer is available,
the request is deferred and retried later (e.g., after a short random
delay or when a buffer is returned to the pool). Consequently, a
return value of 0 from `bt_l2cap_br_chan_connect()` or
`bt_l2cap_br_chan_disconnect()` now indicates that the connection or
disconnection process has been initiated, rather than that the request
PDU has been sent. The trade-off-choosing between a deadlock and
dropping the request is intentional.

Signed-off-by: Lyle Zhu <lyle.zhu@nxp.com>
2026-10-19 10:15:37 +02:00
Zhijie Zhong
a637bb04c7 Bluetooth: Classic: fix discovery stop while resolving names
BT_DEV_INQUIRY stays set while remote names are being resolved, but the
controller has already reported Inquiry Complete by then, and the Host
is only told to issue Inquiry Cancel before that event (Core
Specification 6.3, Vol 4, Part E, Section 7.1.2). If the controller
fails the cancel then, bt_br_discovery_stop() returns that error with
the flag still set and the name resolution still running, and
bt_br_discovery_start() keeps returning -EALREADY until the last name
request completes.

Only send Inquiry Cancel when no remote name request is outstanding. A
name is requested from report_discovery_results(), which runs on the
Inquiry Complete event, so a result in RESOLVE_REMOTE_NAME_RESOLVING is
itself the record that the inquiry has finished.

The inquiry can also complete while the cancel is in flight. When that
completion starts no name request it clears the flag, and a failure
with the flag already clear is the stop that has just happened, not an
error to report. When it starts one the flag stays set until the last
name request completes, so the failure is returned and the caller can
stop again, which then skips the cancel and tears down.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Zhijie Zhong <zhongzhijie1@xiaomi.com>
2026-10-19 10:11:15 +02:00
Zhijie Zhong
957401942a Bluetooth: Classic: RFCOMM: stop the RTX timer when DLC connect fails
bt_rfcomm_dlc_connect() arms the RTX timer in rfcomm_dlc_init(), but its
failure path clears dlc->session without cancelling the timer. When
bt_l2cap_chan_connect() or rfcomm_dlc_start() fails, the timeout handler
therefore fires RFCOMM_CONN_TIMEOUT later on a DLC that was already torn
down, with no interleaving needed.

Cancel the timer on that path.

Signed-off-by: Zhijie Zhong <zhongzhijie1@xiaomi.com>
Assisted-by: Kiro:claude-opus-5
2026-10-19 10:10:52 +02:00
Zhijie Zhong
8bad9a85ea Bluetooth: Classic: RFCOMM: guard against NULL session in RTX timeout
rfcomm_dlc_rtx_timeout() dereferences dlc->session, which is cleared in
rfcomm_dlc_destroy(). Destroy normally runs on bt_workq, where the cancel
there wins over the RTX work, but bt_rfcomm_dlc_disconnect() reaches
destroy from the caller's thread while the DLC is in INIT or
SECURITY_PENDING, and rfcomm.c takes no lock. The handler can therefore
still observe a torn down DLC and fault on the first dereference.

Return early in that case. This narrows the window rather than closing
it: a synchronous cancel would close it, but it is undefined here because
destroy is reachable from the handler itself.

Signed-off-by: Zhijie Zhong <zhongzhijie1@xiaomi.com>
Assisted-by: Kiro:claude-opus-5
2026-10-19 10:10:52 +02:00
Johan Hedberg
b9df71da80 Bluetooth: Host: Remove declarations of deleted conn functions
conn_internal.h still declares functions whose definitions are gone,
and nothing in the tree refers to them. Remove the declarations, the
bt_conn_create_frag() macros and their comments.

bt_conn_disconnect_all() was removed by
commit 6eef6cd946 ("Bluetooth: host: Rethink how unpair all works"),
bt_conn_send_cb() and bt_conn_send_iso_cb() by
commit 28535fe2f2 ("Bluetooth: host: Change TX pattern (push -> pull)")
and bt_conn_create_frag_timeout() and its debug variant by
commit 1c8cae30a8 ("Bluetooth: host: Introduce "view" buffer concept").

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
2026-10-16 15:50:55 -05:00