Commit Graph

6535 Commits

Author SHA1 Message Date
Robert Lubos
31b79a93af net: pkt: Add explicit casts to uint32_t when bitshifting uint8_t val
Cast uint8_t variable to uint32_t explicitly to avoid implicit cast to
int, and thus potentially undefined behavior, reported by UBSAN:

  net_pkt.c:1946:17: runtime error: left shift of 239 by 24 places
  cannot be represented in type 'int'

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
7949789ce8 net: ip: utils: Avoid casting to in(6)_addr struct in net_addr_ntop()
In order to avoid alignment issues when casting void pointers to
in(6)_addr structures, create a properly aligned copy of the ip(v6)
address on stack.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
bd96b5dd83 net: lib: websocket: Avoid bitshift overflow warning
In order to prevent an overflow warning from UBSAN when bitshifting,
cast to uint64_t first before shifting, and then back to uint32_t.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
62a5260e01 net: Fix alignment error with net_ipaddr_copy()
As struct sockaddr have now alignment of 4 bytes, net_ipaddr_copy()
gives the following error if used for sockaddr:

  error: alignment 1 of ‘struct <anonymous>’ is less than 4
  [-Werror=packed-not-aligned]

Just use memcpy() instead, net_ipaddr_copy() was intended to use with IP
addresses, not socket related structs.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
8124032cbb net: ipv4: Avoid casting unaligned address to struct in_addr
Rework the IPv4-related code to avoid casting. Use raw variants of
IPv4-related functions whenever possible (especially on the critical
data path).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
f549bfcfeb net: ipv4: Add raw variants of various IPv4 functions
To address the misaligned access issues reported by UBSAN, introduce raw
variant of certain IPv4 functions used in the critical data path of the
network stack, operating on plain uint8_t buffers in stead of IPv4
address struct.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
0f6dcb37d7 net: ipv6: Avoid casting unaligned address to struct in6_addr
Rework the rest of the IPv6-related code to avoid casting. Use raw
variants of IPv6-related functions whenever possible (especially on the
critical data path). For the routing case, use a copy of the address to
avoid massive rework of the routing module.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
edf5319ca6 net: ipv6: nbr: Avoid casting unaligned address to struct in6_addr
IPv6 Neighbor Discovery interfaces modules like neighbor or routing
tables - converting them to raw variants seems futile. Therefore, for
IPv6 ND case, copy the raw IP address from the packet into the in6_addr
structure, and then pass it to respective functions. Performance
overhead should not be a big problem in such case as those actions are
only performed if a respective ND packet is received.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
f5455e66e3 net: ipv6: 6lo: Avoid casting unaligned address to struct in6_addr
Refactor local functions to work with byte buffers instead of struct
in6_addr and use switch to use raw variants of functions operating on
IPv6 addresses.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Robert Lubos
74ccd5fdaa net: ipv6: Add raw variants of various IPv6 functions
To address the misaligned access issues reported by UBSAN, introduce raw
variant of certain IPv6 functions used in the critical data path of the
network stack, operating on plain uint8_t buffers in stead of IPv6
address struct.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-04 13:16:32 -05:00
Shrek Wang
6a27fd2255 net: tcp: Add TCP ACK validation
According to RFC 793, ch 3.9 Event Processing,
after the connection is sync-ed with seqnum of both sides then,
        1. drop any received segment if the ACK bit is off.
        2. validate the acknum like this:
        SND.UNA =< SEG.ACK =< SND.NXT

The ACK validation is done before entering the state-machine, so
remove the flags <ACK> check in the state-machine processing.

Signed-off-by: Shrek Wang <inet_eman@outlook.com>
2025-07-04 13:08:57 -05:00
Tim Pambor
e6f179d180 tests: net: dns-sd: Fix undefined behavior reported by UBSAN
htons() takes uint16_t as argument. Add the 'u' suffix to the
TTL constants to ensure the correct unsigned type is used and to avoid
undefined behavior if these functions are implemented as macros using
bit shifts.

Signed-off-by: Tim Pambor <tim.pambor@codewrights.de>
2025-07-02 03:55:06 -10:00
Robert Lubos
d720971ae2 net: coap: Verify block number before processing
Verify if the block number isn't negative before processing it, to
prevent potentially undefined behavior. This was reported by the
undefined behavior sanitizer.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-07-02 03:53:40 -10:00
Florian La Roche
25e1b88880 net: dns: is_server_name_found(): fix return code check
In is_server_name_found() fix the return code checking
of net_addr_ntop().

Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
2025-07-01 19:03:29 -05:00
Toon Stegen
d1872d7272 net: mdns: make net_buf size configurable
for longer DNS-SD text records and instance names we need a bigger
buffer

Signed-off-by: Toon Stegen <toon@toostsolutions.be>
2025-07-01 10:16:20 -05:00
Alberto Escolar Piedras
a1d916edd0 net: dhcpv6: Avoid directly accessing address of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-01 10:15:55 -05:00
Alberto Escolar Piedras
1deb2e3c68 net: http_server: Avoid directly accessing address of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-01 10:15:55 -05:00
Alberto Escolar Piedras
82658ea6b2 net: igmp: Avoid directly accessing address of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-01 10:15:55 -05:00
Alberto Escolar Piedras
236ccd379f net: ieee802154: Do not access addr of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-01 10:15:55 -05:00
Alberto Escolar Piedras
93fa3b7831 net: tcp: Avoid directly accessing address of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-01 10:15:55 -05:00
Daniel Schultz
2c767cbfc0 net: lib: zperf: Fix 'kbps' in output
zperf should only return 'Kbps'. There are still two left-overs
from converting shell prints to 'Kbps'.

Align all prints to make it easier for scripting and parsing
content from console output.

Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
2025-06-30 15:19:39 -05:00
Juha Ylinen
ed79675199 net: http_client: Fix handling of POLLHUP
POLLHUP event may be returned even if there is still data to read
and POLLIN is set. To ensure all data is consumed, check for
POLLHUP after handling POLLIN.

https://man7.org/linux/man-pages/man2/poll.2.html

Signed-off-by: Juha Ylinen <juha.ylinen@nordicsemi.no>
2025-06-30 15:17:11 -05:00
Hui Bai
12babf9e39 net: l2: wifi: Fix roaming fail issue
The NET_EVENT_WIFI_SIGNAL_CHANGE was not added to net mgmt event queue
so that no scan was triggered, which caused roaming fail. The event
NET_EVENT_WIFI_SIGNAL_CHANGE was dropped because it was not enabled in
WIFI_SHELL_MGMT_EVENTS. After adding NET_EVENT_WIFI_SIGNAL_CHANGE, the
roaming works as expected.
Same issue found on event NET_EVENT_WIFI_NEIGHBOR_REP_COMP for 11k
roaming. Add this event to WIFI_SHELL_MGMT_EVENTS, too.

Signed-off-by: Hui Bai <hui.bai@nxp.com>
2025-06-27 18:27:45 -05:00
Benjamin Cabé
222a601b21 net: lib: ptp: fix memory slab alignment issues
Commit 3c47f91be4 introduced alignment
validation in K_MEM_SLAB_DEFINE macros.
A couple PTP message slabs wer using alignment 8, while the size of
the elements wasn't a multiple of 8, causing a static assertion
failure.

Fix by changing the alignment from 8 to 4 bytes.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-27 17:00:37 -05:00
Benjamin Cabé
1a01318efa net: lib: dns: dns_cache: check arguments in dns_cache_remove
Like other API in DNS cache, `dns_cache_remove` should check arguements
return -EINVAL if any of them is NULL.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-27 10:05:40 -05:00
Tomasz Bursztyka
175da6bdb0 init: Make entry init-function less and introduce service objects
Since the addition of deinit operation in device, init and deinit have
been within each device, rendering their init entry's init function
useless.

In order to save ROM space, let's remove the init function from
init entry altogether, and introduce a new object called "service"
which owns an init function to go along with SYS_INIT/SYS_INIT_NAMED.

Signed-off-by: Tomasz Bursztyka <tobu@bang-olufsen.dk>
2025-06-27 14:13:58 +02:00
Jukka Rissanen
96818f45a9 hostap: Replace wifi event mechanism by k_fifo
Earlier we had socketpair to pass wifi event information from
wpa_supplicant side to zephyr adaption layer. This is now replaced
by k_fifo to save some RAM.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-27 10:56:49 +02:00
Robert Lubos
3e704256e3 net: ppp: stats: Fix net_mgmt request handler format
This has been missed in net_mgmt rework in commit
5a9a39caf3.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-27 09:43:13 +02:00
Robert Lubos
8aa842c2c4 net: lwm2m: Prevent busy looping when active notify is set
In case a new notification is triggered while the previous one is still
active (i.e. not acknowledged), the LwM2M engine will start busy
looping, as the new notification is still valid (and supposed to be sent
after the previous one is done), but it's not sent in the current
iteration. The busy looping might prevent the system from receiving the
acknowledgment for the previous one, resulting in unresponsive system.

Fix this, by delaying the new notification timestamp a bit in such case,
so that the new notification will be scheduled for later instead of
trying to send it again right away.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-26 11:00:42 -05:00
Robert Lubos
a9c30d73f8 net: lwm2m: Implement gt/lt/st attributes handling
While it was possible to specify Greater Than / Less Than / Step
observe attributes for a resource, they were not taken into
consideration when evaluating notification criteria. This commit adds
support for checking if the resource value meets the value criteria
specified by gt/lt/st attributes.

Note that gt/lt attributes are thresholds - notification should only
sent if the resource value crosses them.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-26 11:00:42 -05:00
Robert Lubos
1bfd2ab4fd net: lwm2m: Update value of resources in notification registry
Update the value stored in the notification registry whenever
notification is sent (for those resources that have gt/lt/st attributes
assigned).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-26 11:00:42 -05:00
Robert Lubos
1ee6943cb5 net: lwm2m: Implement notified value registry for gt/lt/st attributes
Greater than, Less than and Step attributes require to track the last
notified value of a resource/resource instance in order to be able to
apply the specific thershold/step rules that the attributes define.

Therefore, implement a simple registry of the last notified values. When
one of the gt/lt/st attributes is configured on a resource/resource
instance, a registry entry is allocated for respective resource.
Whenever a notification is sent (either as a reply to Observe message,
or proactively) the registry is updated for the notified resources.

The stored resource values have been unified as "double" variables, to
avoid implementation complexity of having to support different
integer/floating point resource types.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-26 11:00:42 -05:00
Robert Lubos
46aaf287da net: lwm2m: Do not apply gt/lt/st attributes to incompatible resources
gt/lt/st attributes can only be applied to numerical resources according
to LwM2M specification (and common sense), hence verify that when
attributes are assigned.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-06-26 11:00:42 -05:00
Jukka Rissanen
f1a9ff97cb net: shell: dns: Print info about DHCP added servers
Print information which DNS servers were added by DHCP when
listing DNS servers in "net dns" command. This helps debugging
DNS server issues.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-26 14:08:19 +02:00
Jukka Rissanen
25084203c2 net: dhcp: Remove only added DNS servers when stopping
Make sure that we remove only the added DNS servers when
the DHCP is stopped.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-26 14:08:19 +02:00
Jukka Rissanen
cdc6c324d7 net: dns: Save info about source when configuring DNS servers
Remember which DNS server was added by a source like DHCPv4 or v6
message. This will allow system to remove DNS servers that were added by
that source. Then when stopping for example DHCP, we can remove those
specific DNS servers and not leaving DNS servers hanging in the system.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-26 14:08:19 +02:00
Ravi Dondaputi
d289bed4af net: lib: wifi_credentials: Add support for Enterprise security
Add support for configuring enterprise mode security.

Signed-off-by: Ravi Dondaputi <ravi.dondaputi@nordicsemi.no>
2025-06-25 15:31:29 -10:00
Pieter De Gendt
0b8c00b5dd net: lib: dhcpv4: Support INIT-REBOOT
Add the init-reboot state for DHCPv4 to request an already assigned IP
address.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-06-25 14:09:46 +02:00
Pieter De Gendt
99a2893713 net: lib: dhcpv4: Skip leftover hostname or domain name bytes
When reading and printing hosname or domain name from DHCP options, we need
to skip the leftover bytes that do not fit into the provided buffers.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-06-25 14:09:46 +02:00
Pan Gao
e53ceeb86b net: l2: wifi: fix ap rts_threshold cmd parameter count
fix the number of mandatory arguments for command Wi-Fi ap RTS threshold,
3 to 2.

Signed-off-by: Pan Gao <pan.gao@nxp.com>
2025-06-25 10:53:40 +02:00
Marc Lasch
86dbcd2cf2 net: lwm2m: Make Bootstrap on Registration Failure configurable
The fallback to Bootstrap on a Registration Failure might not be desirable
for some applications. This change makes the default behavior configure
through Kconfig.

An alternative to configure "Bootstrap on Registration Failure" would be to
set the server object resource id 16 at runtime. However, this is tedious
and error-prone, as it will be reset when the server object is deleted and
might have to be set at different locations in the client code.

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
2025-06-24 15:38:27 -05:00
Marc Lasch
4c46b0bc8b net: lwm2m: Fix typo in bootstrap_on_fail
Bootstrap vs. Boostrap.

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
2025-06-24 15:38:27 -05:00
Chris Friedt
03db50b79d net: remove dependency on posix for iso c time() function
The ISO C function time() should not depend on POSIX and this was
corrected recently via the common libc time() implementation.

Remove this dependency from the network subsystem where it has
been unduly needed for some time.

Similarly, XSI_SINGLE_PROCESS was a dependency for time() via
picolibc, because the time() implementation in picolibc relies
on the POSIX gettimeofday() call.

However, since Zephyr's common libc time() implementation no longer
depends on that, it can be removed as a dependency in the network
subsystem as well.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-06-24 14:18:52 +02:00
Nick Ward
ab99a53ae7 net: lwm2m: ipso: accelerometer: remove buffers for optional resources
Allows application code to control allocation of buffers of optional
resources.

Y value, Z value, min range value, max range value resources are
optional.

Signed-off-by: Nick Ward <nix.ward@gmail.com>
2025-06-23 15:46:18 +01:00
Yusuke Omae
d9d6f07c27 net: gptp: Add TLV type, OUI check for signaling messages
Add validation to ensure gPTP signaling messages
contain the correct TLV type, IEEE 802.1 Committee OUI
and sub-type before processing it as interval requests.

signed-off-by: Yusuke Omae <s1413091@gmail.com>
2025-06-23 09:47:03 +02:00
Benjamin Cabé
92d4669752 net: lib: http: remove offensive language
Remove use of offensive language in code comment

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-22 18:45:42 -07:00
Jukka Rissanen
9a0e9be8c3 net: shell: dns: Add service discovery command
Add "net dns service <dns-service-name>" which can resolve
service names like "_http._tcp.dns-sd.org" described in DNS-SD
RFC 6763.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-18 09:08:41 -04:00
Jukka Rissanen
7799f0c994 net: dns: Allow user to specify max answer length via Kconfig
If the default max. answer length of 512 bytes is not enough,
user can specify it by setting CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE
to suitable value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-18 09:08:41 -04:00
Jukka Rissanen
cf8b7d715d net: dns: Add service discovery support
Allow PTR queries with dns_resolve_service() API.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-18 09:08:41 -04:00
Jukka Rissanen
2ed2232f17 net: socket: mgmt: Create proper socket options for net_mgmt sockets
We cannot use the network management event number directly as
a socket option value because the management value is uint64_t
and that cannot be mapped directly to 32 bit integer.
So have an intermediate socket option that is mapped to actual
network management request number in getsockopt() and setsockopt().

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-18 10:54:44 +02:00