coap_get_block1_option() and coap_get_block2_option() take their value
from coap_get_option_int(), which returns the whole four byte option as
an int. A Block1 of ff ff ff fe therefore arrives as -2, and both
functions hand it straight back from their own `ret < 0` guard: a peer
choosing the option bytes chooses the errno a public API function
reports, and -ENOENT there claims the option is absent while it is
present.
Take the value through get_option_uint() instead, as
update_descriptive_block() and the four control block functions already
do, and apply MAX_BLOCK_NUM before the shift for the reason it exists.
RFC 7959 gives the option value three bytes at most, four bits of which
carry the more flag and the size exponent, so a block number wider than
twenty bits is not something the encoding can express. No option value
that fits those three bytes changes its answer.
Both functions keep returning the block size on success and a negative
errno on failure.
The new testcases pass ff ff ff fe as Block1 and as Block2 and check
that each is refused with -EINVAL and the block number left alone.
Without this commit both return -ENOENT.
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
The four control block functions shift the block number into a byte
offset with no upper bound on the result, the same way
update_descriptive_block() did. A request carrying Block2 7f ff ff 06
gives NUM = 0x07FFFFF0 and shifts it ten places: update_control_block2()
accepts it, because the more flag is clear and the size exponent matches
the context, and leaves 137438937088 in ctx->current on a 64 bit target,
or 4294950912 there on a 32 bit one. The TCP twin behaves the same.
Apply MAX_BLOCK_NUM before the shift, for the reason it was introduced:
RFC 7959 gives the option value three bytes at most, four bits of which
carry the more flag and the size exponent, so a block number wider than
twenty bits is not something the encoding can express.
On the Block1 side the guard changes no outcome a peer can reach today -
an out of range number does not match ctx->current, so the offset
comparison already refuses it - but the shift it removes is the same one,
and the four functions are worth reading alike.
The new testcases send 7f ff ff 06 as Block2 over both transports and
check that it is refused and the context left alone. Without this commit
both are accepted.
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
update_control_block1(), update_control_block2() and their two TCP twins
take the block and size options as ints produced by
coap_get_option_int(), which reports an absent option as -ENOENT. The two
are the same value whenever the peer sends the bit pattern of a small
negative errno, and the collision the descriptive path had is here as
well.
A request carrying Block2 ff ff ff fe reads back as -ENOENT, so
update_control_block2() returns 0 and the control option is ignored
rather than refused. A response carrying Size1 ff ff ff fe reads back the
same way, and Size1 ff ff ff ff arrives as -1, so the `size >= 0` test
discards both and a four gigabyte transfer records no total size at all.
Neither is reported to the caller.
Let the four functions read their own options through get_option_uint(),
the helper the descriptive path already uses, and work on a uint32_t
throughout, which is the width the option was read at. A control block or
size can no longer arrive negative, so the `block < 0` rejection goes
with the sentinel it was defending against, and the four options the two
update_from_block() entry points read up front are read where they are
used instead.
The new testcases send ff ff ff fe as Block2 in a request and as Size1 in
a response, over both transports. Without this commit the block option is
silently ignored and the size option records nothing.
The TCP half of the file had no coverage at all, so the test enables
CONFIG_COAP_OVER_RELIABLE_TRANSPORT to reach it.
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
update_descriptive_block() shifts the block number into a byte offset
with no upper bound on the result. The whole four byte option value is
read, so a Block2 option of 7f ff ff 06 carries NUM = 0x07FFFFF0 and
shifts it ten places: 137438937088 on a 64 bit target, and on a 32 bit
one the same shift wraps a size_t to 4294950912. Either way the function
returns 0 and leaves that offset in ctx->current, which the block
transfer then treats as its position in the response.
RFC 7959 gives the option value three bytes at most, four bits of which
carry the more flag and the size exponent, so a block number wider than
twenty bits is not something the encoding can express. Refuse it before
the shift. The largest offset that survives is 0xFFFFF << 10, which fits
a 32 bit size_t.
The new testcases send 7f ff ff 06 and ff ff ff fe as Block2 and check
that both are refused and that the context is left alone. Without this
commit both are accepted, on either width of size_t.
Reported-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
coap_get_option_int() returns the option value through an int and uses
-ENOENT for an option that is not there, so the two are the same value
whenever the peer sends the bit pattern of a small negative errno. A
Size2 of ff ff ff fe reads back as -ENOENT and coap_update_from_block()
maps it to zero, so a four gigabyte transfer records no total size at
all. A Block2 of ff ff ff fe reads back the same way and
update_descriptive_block() returns 0, so the option is ignored rather
than acted on. Neither is reported to the caller.
Size1 and Size2 also carry an unsigned option value, so a peer sending
ff ff ff ff arrives as -1 and sign extends into the size_t
ctx->total_size on a 64 bit target: the packet asks for a four gigabyte
transfer and the context records 18446744073709551615.
Read the two descriptive options with a helper that answers presence
separately from value, and let update_descriptive_block() do the reading
rather than take the sentinel through its arguments. The value it works
on is a uint32_t throughout, which is the width the option was read at,
so both word sizes record what the peer sent and a descriptive block or
size can no longer arrive negative at all.
The new testcases send ff ff ff ff and ff ff ff fe as Size2. Without this
commit the first records 18446744073709551615 on a 64 bit target and the
second records nothing on either.
Reported-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
GET_NUM() yields the block number as an int, and the five functions that
turn a block option into a byte offset shift it left by up to eleven
places. A block option carrying a large number therefore overflows the
shift, which is undefined for a signed type. A four byte Block2 option
reaches a block number of a hundred million without difficulty, and the
option is read from the wire.
Give the macro an unsigned result. Every caller either stores it in a
size_t, which is what the block context uses for its offsets, or in the
uint32_t block number an application reads back, so this changes the type
the arithmetic happens in rather than the value for any block number the
encoding can express.
What it removes is the undefined behaviour, not the large offset. An
unsigned shift past the width of a size_t wraps quietly, which on a 32
bit target still leaves an attacker-chosen ctx->current behind; bounding
the block number is a later commit in this series.
Found by fuzzing the parser with the sanitizers enabled: a three minute
run reported "left shift of 75501601 by 7 places cannot be represented in
type int" at the descriptive block path, and the same run over this
commit completes 34943525 executions with no report.
Signed-off-by: Arkadiusz Grzelka <devitwise@gmail.com>
In preparation for deprecating net_buf_max_len(), size the linearize
destination with net_buf_tailroom(). The buffer is freshly allocated and
empty, so its tailroom is its whole capacity.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
In preparation for deprecating net_buf_max_len(), print the tailroom of
each buffer in the packet chain instead of its capacity.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
In preparation for deprecating net_buf_simple_max_len(), check the
message size against net_buf_simple_tailroom(). The TX buffer is reset
after every send, so the tailroom equals the capacity here, and it is
the right check even if that ever changes.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
In preparation for deprecating net_buf_max_len(), replace it with
net_buf_tailroom() where the buffer is freshly allocated or empty, which
covers the receive buffers, the temporary name buffers and the "does the
answer fit" checks, where the difference between the capacity and the
current length is exactly the tailroom.
The LLMNR responder now clears the result length before zeroing the
buffer, so the memset covers the same bytes as before.
The two mDNS DNS-SD loops encode every response from the start of a
buffer that still carries the previous content, so they compute the
usable size once, as the current length plus the tailroom, before
entering the loop.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
In preparation for deprecating net_buf_max_len(), size the linearize
destination with net_buf_tailroom(). The buffer comes straight from
net_pkt_get_frag() and holds no data yet, so its tailroom is its whole
capacity.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
In preparation for deprecating net_buf_max_len(), express what the
cursor code needs in terms of the payload length and the tailroom. The
cursor compares offsets from buf->data against the point past which
nothing can be appended; that is the payload length plus the tailroom,
provided by the local helper pkt_buf_write_limit(). The two "fragment
still has room" checks become plain tailroom checks.
net_pkt_available_buffer() sums the tailroom of the chain directly
instead of subtracting the packet length from the summed capacities,
which gives the same number.
net_pkt_print_frags() printed the buffer size under the "max len" label
and the capacity under "size". It now prints the tailroom and the size
in the right slots. Its summary line adds up the storage of every
fragment instead of multiplying the fragment count by the capacity of
the last one, which was only right for pools of equal-sized buffers.
net_pkt_print_buffer_info() shows the tailroom instead of the capacity.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
An address being checked for duplicates has an entry for itself in the
neighbour cache, left there by the solicitation, and removing the
address has to take that entry with it. The removal decides whether
there is one to take by looking for the address on the DAD timer list,
which is not what says so: dad_timeout() takes the entry off that list
before it works on it, and works on it with the lock released, so an
address removed just then is on no list at all and keeps its entry.
Nothing is seen to leak today only because the timeout goes on to drop
the entry itself, as the last thing it does for that address. That
holds just as long as it keeps working on a slot that is no longer in
use, and it reads the address out of the slot to do it, so once the
slot has been taken by another address the entry that gets dropped is
the new address's and the old one stays for good, an incomplete entry
being one that nothing evicts.
Ask the address state instead. An address stays tentative until the
check finishes, so a removal that finds it tentative is one that has an
entry to reclaim, whichever list the address is on at the time. Drop
the entry outside the timer lock as well, the way the timeout does,
rather than taking the neighbour lock underneath it.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
socks5_tcp_connect() decided how to interpret the destination address
from the address family of the proxy. A SOCKS5 request carries the
destination independently of the transport used to reach the proxy, so
the two are unrelated: an IPv4 destination reached through an IPv6 proxy,
or the other way round, is a normal thing to ask for. Nothing rejects it
either. set_context_proxy() only requires the proxy family to match the
socket, and zsock_connect_ctx() enters the SOCKS path before any
destination is validated.
With an IPv6 proxy and an IPv4 destination the code cast a
struct net_sockaddr_in to a struct net_sockaddr_in6 and copied the 16
byte sin6_addr out of an 8 byte object, reading 12 bytes past its end and
sending them to the proxy. Under CONFIG_USERSPACE the syscall copies the
address into a net_sockaddr_storage first, so the read stays in bounds
but discloses uninitialised kernel stack instead.
The opposite pairing is quieter but no better: an IPv6 destination
reached through an IPv4 proxy was encoded as SOCKS5_ATYP_IPV4 built from
the first four bytes of the IPv6 address, so the proxy was asked to
connect somewhere else entirely.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
socks5_tcp_connect() hands net_context_recv() pointers to two of its own
stack variables, method_rsp and cmd_rsp, as the user data for the
negotiation and CONNECT response callbacks. For a TCP context
net_context_recv() only stores the callback and the pointer, it never
removes them, so they stay installed on the context after the function
has returned.
On the success path that is harmless, because zsock_connect_ctx()
installs zsock_received_cb() in their place right away. On every error
path nothing does, and the context is left pointing into a stack frame
that no longer exists. When the socket is closed later, tcp_conn_close()
invokes context->recv_cb() with a non zero status and the callback
memsets through the stale pointer.
A proxy that answers the negotiation with NO ACCEPTABLE METHODS is
enough to reach this. Built with CONFIG_ASAN it shows up as a SEGV in
tcp_conn_close().
Route the error returns through a common exit that uninstalls the
callbacks before the frame goes away.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Add the utils helpers. These are optional, reusable utilities built on
top of the subsys to simplify application development.
The utils currently includes:
- mpipe_player, a small pipeline controller that drives a pipeline
through its states and exposes simple play/pause/stop/replay/quit
controls (usable from a shell).
- mpipe_dump, which renders a pipeline topology, element states and the
negotiated caps on each link as a Graphviz graph, on demand from the
shell or automatically on every state change and error through the
player.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Phi Bang Nguyen <phibang.nguyen@nxp.com>
Use ticks instead of milliseconds to avoid 64 bit division which
is needed by k_uptime_get.
Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
Use BT_BIP_APPL_PARAM_TAG_ID_SERVICE_ID instead of
BT_BIP_APPL_PARAM_TAG_ID_STORE_FLAG when setting the service ID
application parameter in cmd_bip_client_start_print() and
cmd_bip_client_start_archive().
The service ID parameter should be used to identify the printing
and archiving services respectively, not the store flag parameter.
Signed-off-by: Cheng Chang <cheng.chang@nxp.com>
bt_l2cap_br_recv_seg() reassembles an SDU from the segments of an
I-frame stream, for channels that supply an alloc_buf callback. It
takes the declared SDU length from a SAR_START segment and never
compares it against the channel's negotiated receive MTU, so the peer
can announce an SDU of up to 65535 bytes on a channel configured for
far less, and reassembly is not bounded by anything the two sides
agreed on. The seg_recv path in bt_l2cap_br_recv_seg_direct() already
rejects such an SDU; apply the same check here so the two paths agree.
Every in-tree profile using this path sets rx.mtu at or above the size
of the buffers its alloc_buf callback hands out, so none of them needs
to receive an SDU larger than the MTU it advertised.
The running total the function compares against that declared length is
also wrong. Segments are appended with net_buf_append_bytes(), which
chains a new fragment whenever the current buffer fills up, so once
reassembly spills past the head buffer the data lands in the fragment
chain and the head buffer's len stops growing. Every test written in
terms of _sdu->len then measures the head buffer alone rather than how
much has actually arrived: the overflow guard stops rejecting oversized
input, and the SAR_END completeness check rejects a complete SDU as
short. Use net_buf_frags_len() for all three reads of the accumulated
length, the debug log included, so they describe the whole chain.
Assisted-By: Claude:opus-5
Signed-off-by: David Brown <david.brown@linaro.org>
The buffer callback of a GOEP v2 L2CAP channel allocates with
K_FOREVER. L2CAP calls it from the Bluetooth RX workqueue, both for a
new SDU and for every further fragment appended to it, and it is that
same workqueue that releases the buffers once the SDU has been handed
to OBEX. The pool is shared by all GOEP v2 channels and one SDU can
take several buffers, so once it runs dry the wait can never be
satisfied and it stops all HCI receive processing instead.
The callback also has no timeout parameter, so a caller that asks for
a non-blocking append has no way to say so; the callback has to
decide, and here the only workable answer is not to wait.
Allocate without waiting. L2CAP already turns a failed allocation on
this channel into -ENOBUFS and reports the channel as locally busy to
the peer, which is the back pressure the peer expects, unlike a
blocked host.
Compile-tested only; there is no GOEP v2 test coverage in the tree.
Assisted-By: Claude:opus-5
Signed-off-by: David Brown <david.brown@linaro.org>
The shell declared one option bitmap for all advertising sets instead
of one per set. Creating a second advertising set then wrote its
options past the end of that bitmap, corrupting adjacent state such
as adv_sets[0]: after "bt adv-create" twice, "bt adv-info" no longer
showed the first advertiser correctly.
Declare the set dimension inside ATOMIC_DEFINE so every advertising
set owns a complete option bitmap, and add a compile-time check that
the array size matches CONFIG_BT_EXT_ADV_MAX_ADV_SET.
Signed-off-by: tallplay lin <tlin@atmosic.com>
Add hci_lockstep.h, a helper on top of the HCI packet helpers for
sending HCI commands one at a time and waiting for their responses
through a caller-provided transport send function. The header lives
with the HCI driver API under include/zephyr/drivers/bluetooth, since
HCI drivers are its users, and the implementation in
subsys/bluetooth/common is part of every CONFIG_BT build, like the
packet helpers.
The helper is for HCI drivers that need to exchange HCI commands with
the controller over their own transport, typically for vendor-specific
controller initialization, without the Host's command machinery and
regardless of whether a Host is present in the build. It relies on the
HCI traffic being in lockstep during such initialization windows, so
it needs no opcode correlation state machine. Command flow control
still applies: the helper sends a command only once the controller
allows one, which every command response fed to it announces through
Num_HCI_Command_Packets, the NOP Command Complete included, so a
controller that allows no further command holds the next one until it
does.
The helper is shaped after what its users have: the transport send
function receives the HCI device, so a driver's own raw send function
serves as-is; received packets are fed as the bytes the driver holds
them in; a NULL command buffer stands for a command without parameters;
the command timeout, which covers both the wait for the controller to
allow the command and the wait for its response, defaults to the Host's
own 10 seconds; and every failure is logged with the opcode by the
helper, so that drivers need no error reporting of their own.
The helper's state is one byte under a short spinlock, with a single
semaphore that only signals a change of it, so an instance costs 40
bytes on Cortex-M, or 48 with CONFIG_POLL widening the semaphore.
Add a test suite for the lockstep behavior to the HCI packet helper
test, including feeding the helper from ISR context, the command flow
control and transport failures, and the documentation page and release
notes.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
Nothing in asserts infra is related to spin validation, spin validation
should not be automatically enable when someone wants to enable assert
and suffer the cost in flash and performance.
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Use intc_aurix_ir_irq_raise() to software-trigger interrupts
via SRC SETR bit for TriCore architecture.
Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
Add a TriCore timestamp_serialize stub to the inline-assembly test
helper so architecture tests build for TriCore.
Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
Declare the mcumgr os_mgmt processor name for TriCore so the OS
management group reports the correct architecture.
Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
TriCore does not store the stack pointer at a fixed offset in
the thread struct. Mark as unimplemented to suppress the build
warning promoted to error by -Werror.
Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
bt_addr_le_is_resolved() tests bit 1 of the HCI address type. That bit
is set for the two Controller-resolved identity types (0x02 and 0x03),
but also for the sentinels the LE Extended Advertising Report event
uses for an anonymous advertiser (0xff) and for a directed address the
Controller could not resolve (0xfe).
le_adv_recv() checks for a resolved address before it checks for an
anonymous advertiser, so since commit baad0c300f ("BluetootH: Host:
add helper functions for resolved addresses") replaced the explicit
identity-type comparison with the helper, the anonymous branch has been
unreachable: reports from anonymous advertisers were passed through
bt_addr_le_copy_resolved() and delivered to scan callbacks with an
address of type 0xfd instead of BT_ADDR_LE_ANY, contrary to the
bt_le_scan_recv_info documentation.
Compare against the two identity types explicitly and document that in
the helper's contract.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
Subscribing and unsubscribing to a notification are asynchronous,
callback-driven procedures. Each is a CCC write, i.e. one ATT request,
and a single bt_gatt_subscribe_params backs one such write at a time
(one value, one list node, one flags word). bt_gatt_subscribe() sends
the enable CCC write to the peer as an ATT transaction with the
30-second ATT timeout running; an unsubscribe reuses the same params
for the disable write.
With the subscribe CCC write still in flight (WRITE_PENDING set),
bt_gatt_unsubscribe() called bt_gatt_cancel() to abort it.
bt_gatt_cancel() runs the aborted request's completion synchronously,
so gatt_write_ccc_rsp() ran with BT_ATT_ERR_UNLIKELY: the subscribe
callback saw a spurious failure and the subscription was torn down
(NULL notification, node removed). bt_gatt_unsubscribe() then queued
the disable write on the same params, whose completion delivered the
subscribe and NULL callbacks a second time. One unsubscribe thus
produced a spurious subscribe failure and duplicate completions, with
the params and its list node reused while the first completion was
still unwinding.
Cancel the in-flight write silently instead. Factor the body of
bt_gatt_cancel() into a helper taking whether to run the completion,
and call it from bt_gatt_unsubscribe() without the completion, clearing
WRITE_PENDING in its place. The superseded write's response is then
ignored, the disable write is queued behind it exactly as before, and
the application sees one NULL notification, as for any other
unsubscribe. No new flag, and no locking beyond what bt_gatt_cancel()
already takes.
Assisted-by: Claude:claude-opus-4.8
Signed-off-by: Zhijie Zhong <zhongzhijie1@xiaomi.com>
bt_att_find_req_by_user_data() walks every ATT channel on the
connection and dereferences chan->req unconditionally. A channel with
no request in flight has chan->req set to NULL, so the lookup faults as
soon as it visits such a channel. With EATT connected this is easy to
hit: the pending request sits on one channel while the others are idle.
The lookup is already reachable on this path today. bt_gatt_unsubscribe(),
called while a subscribe CCC write is still pending, uses bt_gatt_cancel()
to find that in-flight request; the traversal then dereferences an idle
channel's NULL req and crashes before any callback runs.
Guard the dereference with an explicit chan->req != NULL check. Also
reject a NULL user_data up front, which would otherwise match the first
request that happens to carry one. These are pre-existing defects with
no behaviour change, so they can be applied on their own.
Assisted-by: Claude:claude-opus-4.8
Signed-off-by: Zhijie Zhong <zhongzhijie1@xiaomi.com>
806c344146 changed the name from ITS_UID
to ITS_CALLER_ID, but the #undef was forgotten.
Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
Just don't treat the case specially, the usual way handles it well and
allows checking whether an entry exists and is valid without getting
its data.
Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
secure_storage_its_set() and secure_storage_its_remove() read an entry
back before deciding what to write or remove, and nothing guarded them
against concurrent access.
Take a mutex in those two functions only. They become thin wrappers so
that their implementations keep their early returns. Retrieving an entry
doesn't need it: it makes a single call to the store module and then
works on its own copy of the data, so it is atomic as long as the store
module's own operations are. Document that requirement, which the
settings and ZMS implementations already meet.
Remove the separate mutex from the nonce provider that was introduced
in dc66397309 as it's now guarded by
this new mutex.
Add a mutex to the custom store test to respect the explicit contract.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
An entry that cannot be read back is overwritten or removed on purpose,
so as not to be stuck with it forever, but that silently drops data that
may have been corrupted or tampered with. Log it so that it is
diagnosable in the field, and stay quiet when the entry simply doesn't
exist, which is the ordinary path of a first write.
The UID format string moves into a macro now that it has three users.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
The transform module tells an entry that fails authentication apart from
one that is malformed, but the ITS flattened both into
PSA_ERROR_GENERIC_ERROR. Callers could therefore not tell a tampered-with
entry from an internal failure, and a failing storage medium looked
exactly like an attack.
At the same time, be more precise about the expected return values of
secure_storage_its_store_get().
Assisted-by: Claude:claude-opus-5
Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
Add a --ext-filter-policy flag to the bt scan command, so the shell can
start a scanner that uses the extended scanner filter policy. Like
--coded, the flag is always accepted by the parser and the stack rejects
it when the required Kconfig or Controller support is missing.
Print the target address of a directed advertisement on its own line,
marking it as unresolved when the Controller could not resolve it. The
existing report line is left untouched, as it is reproduced verbatim in
the shell documentation.
Tested by building tests/bluetooth/shell on native_sim with and without
CONFIG_BT_SCAN_EXT_FILTER_POLICY. The shell command itself was not
exercised on a target.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
le_ext_adv_legacy_report() reports the target address of a directed
advertisement with the address type taken verbatim from the PDU, without
looking at node_rx->rx_ftr.direct. A directed advertisement that was
accepted only because the scanner uses an extended filter policy, and
whose target address the Controller was therefore unable to resolve, is
reported as an ordinary random address. The host cannot tell it apart
from a directed advertisement addressed to the local identity.
Report BT_ADDR_LE_UNRESOLVED for that case, as le_ext_adv_report()
already does through ext_adv_direct_addr_type(). This is the path taken
for legacy advertising PDUs received while scanning with the extended
scanning commands, so without it the extended scanner filter policies
only produce usable reports when the host uses the legacy scanning
commands.
The rx_ftr.direct field only exists when CONFIG_BT_CTLR_EXT_SCAN_FP is
enabled, so the new branch is compiled out otherwise and the reported
address type is unchanged there.
Tested by building tests/bluetooth/init on nrf52840dk/nrf52840 with
CONFIG_BT_CTLR_EXT_SCAN_FP both enabled and disabled. Not run on
hardware.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
The host had no support for the LE Directed Advertising Report event.
Its event code, structures and event mask bit were defined in
hci_types.h, but le_set_event_mask() never enabled the event and
meta_events[] had no handler for it, so a Controller was neither allowed
to generate it nor able to get it processed.
That event is how a Controller reports a directed advertisement whose
target address is a resolvable private address it was unable to resolve,
while scanning with one of the extended scanner filter policies. Without
it such an advertisement is invisible to the application, even though it
is the case the extended filter policies exist to surface.
Enable the event mask bit and add bt_hci_le_direct_adv_report(), which
turns each report into a bt_le_scan_recv_info and passes it to the
existing scan callbacks. The event mask bit is only set when the
Controller supports the Extended Scanner Filter Policies, as a Controller
may reject Set Event Mask with a bit it does not know.
Report the target address through the new bt_le_scan_recv_info.direct_addr
member. The extended advertising report carries the same address and it
was being discarded, so populate it from there as well. This gives one
representation for both scanning paths, which matters because the same
on-air advertisement is reported as an LE Directed Advertising Report
with the legacy scanning commands and as an LE Extended Advertising
Report with the extended ones. The address type is BT_ADDR_LE_UNRESOLVED
when the Controller could not resolve the target address. The legacy
event only reports an unresolved target address, and Core 6.3 Vol 4 Part
E 7.7.65.11 defines only BT_ADDR_LE_RANDOM for its Direct_Address_Type,
so normalise that address type to give the application one representation
across both report types.
le_adv_recv() drops directed reports when scanning explicitly without
privacy and without CONFIG_BT_SCAN_WITH_IDENTITY, to avoid reports for
the local identity while scanning with a non-resolvable private address.
An unresolved target address is by construction not the local identity,
so exclude that case from the rule. Without this the feature would be a
no-op in the default configuration. This cannot affect existing users:
per the specification a Controller only reports an unresolved target
address while using a filter policy that the host requests exclusively
for BT_LE_SCAN_OPT_EXT_FILTER_POLICY.
Tested by building tests/bluetooth/init with
CONFIG_BT_SCAN_EXT_FILTER_POLICY=y on qemu_cortex_m3 and
nrf52840dk/nrf52840, with CONFIG_BT_EXT_ADV both enabled and disabled,
and by confirming the existing configurations that set
CONFIG_BT_CTLR_EXT_SCAN_FP=n still build with the new symbol unavailable.
Fixes#114236
Assisted-by: Claude:claude-opus-5
Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
The host has always requested one of the basic scanning filter policies,
BT_HCI_LE_SCAN_FP_BASIC_NO_FILTER or BT_HCI_LE_SCAN_FP_BASIC_FILTER, in
both start_le_scan_ext() and start_le_scan_legacy(). The extended
policies BT_HCI_LE_SCAN_FP_EXT_NO_FILTER and BT_HCI_LE_SCAN_FP_EXT_FILTER
were defined in hci_types.h but had no users, so there was no way for an
application to ask the Controller to report directed advertisements whose
target address is a resolvable private address it could not resolve.
Those advertisements were silently discarded by the Controller.
Add BT_LE_SCAN_OPT_EXT_FILTER_POLICY, which selects the extended variant
of whichever policy would otherwise be picked, so that all four policies
are now reachable. The option is gated by the new Kconfig symbol
BT_SCAN_EXT_FILTER_POLICY, which depends on the Controller advertising
support for the feature through BT_CTLR_EXT_SCAN_FP when the Controller
is built in tree.
Not every Link Layer supports the extended scanner filter policies, and a
Controller that does not shall reject the scan parameters with Invalid
HCI Command Parameters. Check LE feature bit 7 in bt_le_scan_start() and
return -ENOTSUP instead, so the application gets an unambiguous error
before any HCI command is sent. Bit 7 had no BT_FEAT_LE_*() accessor, so
add BT_FEAT_LE_EXT_SCAN() next to the other feature test macros.
Consuming the resulting reports is handled separately. Applications that
do not set the new option are unaffected: the option bit is rejected by
valid_le_scan_param() unless the Kconfig symbol is enabled, and the
filter policy sent to the Controller is unchanged otherwise.
Both scan start paths now select the filter policy through one helper,
which gates the filter accept list on BT_FILTER_ACCEPT_LIST. The legacy
path already did so, the extended one did not. This only changes the
extended path when BT_FILTER_ACCEPT_LIST is disabled and the application
sets BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST anyway: it now asks for no
filtering instead of filtering against the accept list. Nothing can add
entries to that list in this configuration, as
bt_le_filter_accept_list_add() is compiled out, so the scanner used to
report nothing at all.
Tested by building tests/bluetooth/init with the new
prj_scan_ext_fp.conf on qemu_cortex_m3 and nrf52840dk/nrf52840, and by
checking that CONFIG_BT_SCAN_EXT_FILTER_POLICY resolves against
CONFIG_BT_CTLR_EXT_SCAN_FP on the latter.
Fixes#114103
Assisted-by: Claude:claude-opus-5
Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
The handshake was accepted when the server's Sec-WebSocket-Accept value
merely matched a prefix of the expected one:
if (strncmp(at, str, length)) {
length is the length of the received value, so a server that returns a
few correct characters, or an empty value, passed the check. With an
empty value strncmp() is called with a count of zero and returns 0, so
the handshake was accepted without any validation at all.
Compare against the expected value at the position reached so far, and
accept only once every byte of it, and no more, has been seen. Doing it
incrementally matters because the HTTP parser hands a header value over
in as many pieces as the receive buffer boundaries dictate. A value that
was split that way used to be accepted for the wrong reason, by matching
a prefix, and a plain length check would have started rejecting it.
on_header_field() now also clears the flag for any other header, so the
value cannot be continued across the header that follows it.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
The Sec-WebSocket-Accept value reaches on_header_value() as a pointer and
a length from the HTTP parser, and is not NUL terminated. On a mismatch
it was logged with a %s conversion, which reads on past the end of the
value looking for a terminator.
This one is worse than the same mistake elsewhere in the HTTP code,
because there is no bounded destination: whatever follows the value in
memory is printed as well, on a path the peer decides to take. With the
default CONFIG_LOG_MODE_DEFERRED the read happens while the log message
is packaged rather than while it is formatted, so the choice of formatter
implementation makes no difference.
Copy the part of the value that fits into a bounded buffer and log that.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Log formats were using %ld, assuming that off_t was equivalent to
long. Place an explicit cast in the code so that when off_t is wider, the
value is still displayed correctly.
Signed-off-by: Keith Packard <keithp@keithp.com>
The backend is enabled in shell_start(), so with SHELL_AUTOSTART=y
early log/printk output is buffered (deferred mode) or lost (immediate
mode) until the shell thread runs. Document this and the
SHELL_AUTOSTART=n + shell_start() alternative.
Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@verkada.com>
log_backend_enable() was called before the backend state was set to
SHELL_LOG_BACKEND_ENABLED. The core may call process() as soon as the
backend is active, so a message arriving in that window hit the
DISABLED case and was dropped. Activate the backend last.
Fixes#118912
Co-Authored-By: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@verkada.com>
put_sync_locked() released a claimed power domain by calling itself on the
domain device. The domain's usage count, state and flags are updated there
while only the child's spinlock is held, never the domain's own, so on SMP
two children suspending at the same time race on the domain they share.
get_sync_locked() claims the domain with pm_device_runtime_get(), which
does take that lock. Do the same on the put side and call
pm_device_runtime_put(). The domain still has to be ISR-safe, otherwise the
semaphore back end would be entered with a spinlock held, so the nesting
stays spinlock over spinlock, as on the get side.
put_sync_locked() is now only reached from the two public put functions,
which already test PM_DEVICE_FLAG_RUNTIME_ENABLED, so drop its own check.
Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
pm_device_runtime_get() tests PM_DEVICE_FLAG_RUNTIME_ENABLED in the public
function; the put side tested it in both back ends instead, in
runtime_suspend() and in put_sync_locked().
Succeeding when runtime PM is not enabled is documented behaviour of
pm_device_runtime_put() and pm_device_runtime_put_async(), so test the flag
there, next to the tracing entry point, like the get side does.
runtime_suspend() is only reached from those two functions, so its check is
now redundant and is removed. put_sync_locked() also recurses into itself
to release the power domain, which does not go through the public API, so
it keeps its check.
Behaviour is unchanged; the ISR-safe path stops taking a spinlock just to
find out it has nothing to do.
Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
cleanup_fragmentation_context() was forward-declared although nothing
forces its definition below avrcp_disconnected(): move it above its
first caller and drop the declaration. No functional change.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
If a device has multiple configurations, choose the first one that is
used by any driver.
Other attempts to chosse a configuraton, such as matching filter rules,
only narrow candidates by class, subclass, protocol, or VID/PID. The
only reliable way to determine whether a driver supports a function is
through the driver's probe() callback. This requires the device to be in
the configured state for the driver's own descriptor parsing and setup.
Therefore, there is no way to test a configuration without setting it.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>