Commit graph

6,767 commits

Author SHA1 Message Date
Jukka Rissanen
04242abb99 net: sockets: Remove inline from zsock_recv_dgram()
As the zsock_recv_dgram() is rather large, remove the inline
keyword from it and let compiler to decide what to do with it.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-02-07 17:46:55 +01:00
Jukka Rissanen
3fb1e11d5b net: mdns_responder: Add more debug prints
Print the socket descriptor and interface after bounding it
successfully in order to help to figure out what sockets are
bind to one specific interface.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-02-07 17:46:55 +01:00
Jukka Rissanen
0f858b24aa net: socket: services: Restore only the socket related to work
Restore only the socket descriptor that we marked as -1 after running
the work related to that socket. Earlier we tried to restore the whole
global array of descriptors which could go wrong and is not needed
as we only support synchronous work.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-02-07 17:46:55 +01:00
Dave Lacerte
d9aab69fbf net: coap: Fix uninitialized variable warning
The variable block_len in function coap_next_block_for_option()
can be used uninitialized in case of empty packet in
coap_packet_get_payload(). Mute the compiler warning with
default value of 0.

Signed-off-by: Dave Lacerte <lacerte.dave@hydroquebec.com>
2025-02-06 17:49:42 +01:00
Chaitanya Tata
4cd6654b21 net: l2: Add a config option to allow mismatched L3/L2 address packets
The RFC1122 section 3.3.6 says we SHOULD drop the packets if L2 address
is brodcast but L3 address is unicast, but we had seen some Wi-Fi access
points in the field not conforming to that, and DHCP offer is dropped
due to this and causes Wi-Fi connectivity issues.

As the RFC saus it's SHOULD and not a MUST, add a config option to allow
such packets, disabled by default.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2025-02-06 17:48:37 +01:00
Seppo Takalo
ac01879330 net: lwm2m: Tickless does not depend on SOCKETPAIR
Tickless mode is refactored to use zvfs_eventfd,
but the Kconfig dependencies were not updated.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2025-02-06 14:46:55 +01:00
Dominic Moffat
8519fa1627 net: socket service resets its restart flag
The socket service restarts whenever a new socket is registered.
This is triggered via zvfs_eventfd_write(ctx.events[0].fd, 1),
but the restart flag (ctx.events[0].revents) is not properly cleared,
causing unintended repeated restarts and skipped socket processing.

Detecting socket restart is done after the socket fd has processed its
data, so the socket doesn't lose its data. Ensure the socket service
clears its restart flag (ctx.events[0].revents = 0) immediately after
detecting a restart event, preventing unnecessary restarts.

Fixes #81813

Signed-off-by: Dominic Moffat <dom@illysky.com>
2025-02-05 23:49:28 +01:00
Noah Olson
37a924be6a net: dns: Fix DNS caching bug
The current DNS caching logic is not aware of the IP address version.
If there is a cached address for a query, the caller of
dns_get_addr_info() will receive that address, even if it is not the
same version as the caller requested. For example:

- dns_get_addr_info() is called to resolve an IPv4 address
- the DNS client caches the IPv4 address that was received
- dns_get_addr_info() is called to resolve an IPv6 address
- the DNS client sees that there is a cached IPv4 address for the
  given query
- the DNS client does not check that the cached address is of the
  requested version (IPv6)
- the cached IPv4 address is returned from dns_get_addr_info()

This changes the DNS client to check IP address version when searching
cached addresses.

Signed-off-by: Noah Olson <noah@wavelynx.com>
2025-02-05 23:49:17 +01:00
Dave Lacerte
31ec42a33c net: lwm2m: Fix offset for post-write callback
The offset in post-write callback is incorrect for TLV format as
the first data block contain TLV header. This fix is replacing
reamaning for offset in opaque context struct and passing it to
post-write and validation callback.

The fix also corrects the offset in pull context as it was pointing
at the end of the block processed data and not on the block context
prior to processing.

Signed-off-by: Dave Lacerte <lacerte.dave@hydroquebec.com>
2025-02-05 23:48:38 +01:00
Robert Lubos
b7cfa54c18 net: tc: Deprecate CONFIG_NET_TC_SKIP_FOR_HIGH_PRIO
Since a separate CONFIG_NET_TC_RX_SKIP_FOR_HIGH_PRIO was introduced for
RX TC queues, CONFIG_NET_TC_SKIP_FOR_HIGH_PRIO became ambiguous,
rename it to CONFIG_NET_TC_TX_SKIP_FOR_HIGH_PRIO and deprecate the old
config.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-02-05 12:37:01 +01:00
Robert Lubos
e430dfb00f net: arp: Set family and proto type for all ARP packets
The family and proto type was not set for the IPv4 autoconf ARP packets.
Since those should be set for all ARP packets, just move the code
outside of the if block.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-02-05 12:36:25 +01:00
Cla Mattia Galliard
2153e34e14 net: ethernet: cleanup net-stats calls
There is no need to if-def away the net-stats function since there is a
shim available now.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
2025-02-03 16:59:53 +01:00
Robert Lubos
57189b410e net: lwm2m: gateway: Prevent underflow when processing URI options
lwm2m_gw_handle_req() function logic expects that at least one URI
option was found, therefore add an extra check to return the function
early if that isn't the case.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-31 19:51:17 +01:00
Jukka Rissanen
9c24578cc4 net: vlan: Allow VLAN count to be set to 0
If VLAN count is set to 0, then only priority tagged VLAN frames
that have tag value 0 can be received. Also this means that VLAN
interfaces are not created which can save memory if you do not need
to receive any other VLAN frames than those tagged with value 0.

Fixes #84023

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-31 16:12:50 +01:00
Cla Mattia Galliard
586bec760d net: ethernet: Ensure packet is not touched after consumed by l3
Ensure that all packets are no longer used by ethernet_recv after it was
consumed by the l3-handler.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
2025-01-31 16:09:47 +01:00
Marc Lasch
dd16bc5153 net: lwm2m: Check result when sending messages
`lwm2m_send_message_async` returns critical errors which are not handled in
many cases. The function can return -ENOMEM, -EPERM or other error codes
from functions called.

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
2025-01-31 16:09:33 +01:00
Cla Mattia Galliard
bb3ac84bce net: tc: Add a skip for rx-queues
Add a configuration option to skip the rx-queues for high priority packets
analogously to the tx-side.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
2025-01-31 11:49:50 +01:00
Cla Mattia Galliard
ed1ebb337d net: tc: Add statistics about dropped packets
Add statistics about packets dropped in net_tc.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
2025-01-30 20:27:33 +01:00
Cla Mattia Galliard
84f6e7a313 net: tc: Limit the max amount of packets in the traffic-class-fifo
To avoid starvation of a traffic class by another, limit the maximum amount
of packets, that can sit in a single traffic-class-fifo to a fraction of
the maximum amount of available packets. In the tx-case also reserve
packets for direct sending, in the case, where
CONFIG_NET_TC_SKIP_FOR_HIGH_PRIO is enabled.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
2025-01-30 20:27:33 +01:00
Jukka Rissanen
4195130214 net: shell: events: Print more details for L4 events
Print IP address information for L4 DNS server add and remove
events.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Jukka Rissanen
6edcf445ab net: shell: events: IPv4 mcast join and leave were missing
The event monitor did not handled IPv4 multicast join/leave
event information.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Jukka Rissanen
ef9b3d4dbf net: shell: events: IPv4 mcast addr add and del were missing
The event monitor did not handled IPv4 multicast address
addition or deletion information.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Jukka Rissanen
b83f86a1d2 net: shell: events: Add admin up and down events
The "admin up/down" event was not being decoded.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Jukka Rissanen
e109a27c99 net: shell: events: Print event id for unknown events
To simplify debugging what event code is missing from the
events shell module, print also event id for the for unknown
events. This way it is a bit easier to figure out what the
unknown event is.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Jukka Rissanen
b2dcaa8f9e net: shell: events: Add Ethernet L2 support
Print Ethernet carrier on/off and VLAN tag enabled/disabled
status when monitoring events.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-30 18:29:45 +01:00
Robert Lubos
6677db21ce net: sockets: Make sure recvmsg() doesn't modify buffer configuration
The recvmsg() function should not modify the buffer configuration
provided in struct msghdr, such behavior is not specified in the POSIX
function description, nor is it consistent with well-known behavior of
this function in Linux.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-30 14:09:35 +01:00
Seppo Takalo
4d6372be93 net: lwm2m: Fix Notify response handling
As the CoAP library was refactored to handle response
messages as stated by RFC, the LwM2M message handling was
untouched.
LwM2M Notify handling is relying on response callback
to be called, but as per CoAP RFC an empty Ack is not
actually a response and the callback was not called anymore.

Fix the issue by calling the response callback when
we receive an empty ack for any confirmable response
messages.
This should not affect any handling of CoAP requests.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2025-01-30 14:08:39 +01:00
Jukka Rissanen
d243bc1fdb net: Fix inet_pton IPv4 implementation
The conversion from IPv4 string presentation to numeric value
did not check if the individual address value was between 0 and 255
inclusive.

Add also test case for this.

Fixes #84593

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-29 17:56:09 +01:00
Eric Holmberg
b952f613e7 net: wifi: shell: only process scan events during requested scan
The scan events are always enabled which means if another software
component requests a scan, then the WiFi shell scan printouts will
also be sent to the shell.

Only enable the network management scan events when a user has
requested a scan.

Signed-off-by: Eric Holmberg <eric.holmberg@northriversystems.co.nz>
2025-01-29 12:51:16 +01:00
Fabio Baltieri
e4752e5e11 net: dhcpv4_server: skip the DNS option if not configured
Check if NET_DHCPV4_SERVER_OPTION_DNS_ADDRESS is set before using it to
set the DNS option in DHCP responses. This avoids sending a client a DNS
server of 0.0.0.0.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-01-29 04:17:36 +01:00
Fabio Baltieri
68b63d023b net: dhcpv4_server: allow skipping the router option
Add a Kconfig option to skip the router DHCP server option. This can be
useful to avoid having the client trying to forward all its traffic to
the embedded device.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-01-29 04:17:36 +01:00
Chaitanya Tata
2019b7ba86 net: l2: ethernet: Fix interface state check
Modifying MAC address is allowed only when the interface is not
administratively UP, as it typically involves conveying the MAC address
to the chip firmware, and accepting the MAC address when interface is
administratively UP will not reflect in the actual usage.

So, modify the check to reject MAC address change if the interface is
administratively UP.

Fixes #81486.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2025-01-29 04:17:21 +01:00
Jukka Rissanen
535e70a298 net: socket: Release packets in accepted socket in close
If we have received data to the accepted socket, then release
those before removing the accepted socket. This is a rare event
as it requires that we get multiple simultaneous connections
and there is a failure before the socket accept is called by
the application.
For example one such scenario is when HTTP server receives multiple
connection attempts at the same time, and the server poll fails
before socket accept is called. This leads to buffer leak as the
socket close is not called for the accepted socket because the
accepted is not yet created from application point of view.
The solution is to flush the received queue of the accepted socket
before removing the actual accepted socket.

Fixes #84538

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-28 18:24:39 +01:00
Matt Rodgers
ea6ca4e8b7 net: http_server: allow specifying a fallback resource
A _res_fallback parameter to HTTP_SERVICE_DEFINE is added to optionally
specify a fallback resource detail, which will be served if no other
resource matches the URL.

Signed-off-by: Matt Rodgers <mrodgers@witekio.com>
2025-01-28 18:14:36 +01:00
Maciej Baczmanski
341359568f openthread: fix logging configuration
Currently, `CONFIG_OPENTHREAD_LOG_LEVEL` is used to set log level
in OT and register log modules in Zephyr. OpenThread allows 5
levels, causing issues when `OPENTHREAD_LOG_LEVEL_DEBG` is
selected ad we are trying to register modules with unknown lvl.

This commit adds `CONFIG_OPENTHREAD_MODULE_LOG_LEVEL` which is
aligned for Zephyr's log levels.

Signed-off-by: Maciej Baczmanski <maciej.baczmanski@nordicsemi.no>
2025-01-28 14:13:31 +01:00
Joakim Andersson
8457db52c7 net: tls_credentials: Do not free slot when cred buf is NULL
Do not free the slot when cred buf is NULL.
If the TLS credential storage backend does not have a buffer pointer
to the TLS credential, then the buffer pointer can be NULL.
This may happen with an out-of-tree TLS credential storage backend.

In this case where credential buffer is NULL find_ref_slot retrieves
a new slot, and then tries to free it, causing a crash.

Signed-off-by: Joakim Andersson <joerchan@gmail.com>
2025-01-28 09:51:43 +01:00
Kamil Kasperczyk
2534dc14b9 net: Added configuring child timeouts on openthread start
Some time ago three Kconfigs dedicated for the child timeouts
configuration were added, but changing them does not apply when
using OpenThread libraries. Added setting these values using
openthread API on openthread start.

Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
2025-01-24 08:39:05 +01:00
Robert Lubos
21b71224ac net: ethernet: Remove L2 header stripping after TX
It seems that this change was solely added to address issues with old
TCP stack, which blindly queued packets intended for TX for potential
further retransmission, expecting that the packet would remain intact
during transmission.

I think this assumption was wrong, as it's natural that lower layers
append respective headers to the packet, and this "header stripping"
behavior was specific for Ethernet L2 only. If an upper layer expects
that the packet would need to be retransmitted at some point, it should
clone it instead.

Therefore, remove the L2 header stripping from the Ethernet L2 to avoid
any potential issues in zero-copy case.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-23 16:31:41 +01:00
Robert Lubos
78c3996b59 net: ethernet: Allow drivers to reserve net_pkt headroom
Add new Ethernet driver config option,
ETHERNET_CONFIG_TYPE_EXTRA_TX_PKT_HEADROOM, which allows Ethernet
drivers to inform L2 about the extra net_pkt headroom they need to be
allocated.
This is only supported when CONFIG_NET_L2_ETHERNET_RESERVE_HEADER is
enabled, so that it's possible to fit entire packet into a single
net_buf, which is needed for zero-copy transmission.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-23 16:31:41 +01:00
Robert Lubos
ea191bddaf net: pkt: Fix fixed buffer allocation with headroom bug
The size calculation for the first buffer, in case extra headroom is
requested, had a bug which could result in a size variable underflow
followed by net_buf exhaustion.

In case the net_buf size was larger than requested size, but smaller
than requested size + headroom, the whole buffer size was subtracted
from the requested size. This however did not take the extra headroom
into account and in effect could result in underflow.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-23 16:31:03 +01:00
Matt Rodgers
d1d85fa40b net: http_server: fix URL matching with '?' character in resource
Fixes #84198.

If a '?' character is used as part of a wildcard resource, do not treat
this as the end of the string when comparing with a path from the HTTP
request. Only the path from the HTTP request may be terminated by '?'
(in the case of a request with query parameters).

Signed-off-by: Matt Rodgers <mrodgers@witekio.com>
2025-01-22 13:44:01 +01:00
Jukka Rissanen
594fa24199 net: ethernet: Properly handle VLAN tag 0
Packets are forwarded to the native interface or in other words,
the vlan header is simply stripped and ignored. This feature is called
'priority tagging'.

Signed-off-by: Cla Mattia Galliard <cla-mattia.galliard@zuehlke.com>
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-22 10:47:52 +01:00
Jukka Rissanen
18cd2d83be net: pkt: Alloc headroom also for variable size data buffers
The headroom was not taken into account for variable size data
buffers when CONFIG_NET_L2_ETHERNET_RESERVE_HEADER was enabled.

Add a test case for it to make sure the reserve allocation works
properly.

Fixes #84053

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-22 10:39:23 +01:00
Robert Lubos
64e5a31b61 net: coap: Fix coap_packet_is_request() check for empty code
Empty code was incorrectly matched as a request, fix that.

Align coap_handle_request_len() function to behave as documented in the
API documentation - in case of invalid request code (which is also the
case for empty code) -ENOTSUP Should be returned.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-21 19:30:06 +01:00
Robert Lubos
13cd48a431 net: coap: Fix response matching algorithm
The algorithm for matching request with response was incorrect, which
could lead to false matches (for example if request had a token, and
piggybacked reply had no token but matching message ID only, that would
still be counted as a match).

This commit fixes it. The request/reply matching is implemented based on
RFC now, with separate conditions for piggybacked/separate responses.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-21 19:30:06 +01:00
Jukka Rissanen
ab9b85b199 net: Build assert issue with llvm
Remove the build assert from NET_L3_REGISTER() macro as that
is causing an issue with llvm. Add runtime check of the handler
pointer value.

subsys/net/l2/ethernet/arp.c:1044:1: error: static_assert expression
is not an integral constant expression

ETH_NET_L3_REGISTER(ARP, NET_ETH_PTYPE_ARP, arp_recv);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/zephyr/net/ethernet.h:1272:2: note: expanded from
macro 'ETH_NET_L3_REGISTER'
        NET_L3_REGISTER(&NET_L2_GET_NAME(ETHERNET), name, ptype, handler)
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/zephyr/net/net_core.h:190:2: note: expanded from
macro 'NET_L3_REGISTER'
        BUILD_ASSERT((_handler) != NULL, "Handler is not defined")
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/zephyr/toolchain/gcc.h:87:51: note: expanded from
macro 'BUILD_ASSERT'
define BUILD_ASSERT(EXPR, MSG...) _Static_assert((EXPR), "" MSG)
                                                  ^~~~~~
subsys/net/l2/ethernet/arp.c:1044:1: note: cast from 'void *' is not
allowed in a constant expression
include/zephyr/net/ethernet.h:1272:2: note: expanded from
macro 'ETH_NET_L3_REGISTER'
        NET_L3_REGISTER(&NET_L2_GET_NAME(ETHERNET), name, ptype, handler)
        ^
include/zephyr/net/net_core.h:190:29: note: expanded from
macro 'NET_L3_REGISTER'
        BUILD_ASSERT((_handler) != NULL, "Handler is not defined")
                                   ^
/usr/lib/llvm-14/lib/clang/14.0.0/include/stddef.h:89:16: note: expanded
from macro 'NULL'
  define NULL ((void*)0)

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-21 19:29:55 +01:00
Jukka Rissanen
04205ae24f net: Set the protocol type of fragmented packet
The fragmented packet should inherit the protocol type of the
original packet.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-21 19:29:55 +01:00
Jukka Rissanen
3e680551b6 net: ipv6: Do not set ptype when preparing for sending
Trust that the protocol type is set correctly by functions
called before this one. We should not set the protocol type
blindly in this generic function.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-21 19:29:55 +01:00
Jukka Rissanen
30ad29c2a3 net: ipv4: Do not change the protocol type when sending
The ARP code has set the protocol type of the packet to
0x806, so do not change it when preparing to send to 0x800
which is the IP protocol type. Lets trust the previously
called functions to set the ptype correctly and do not set
it here.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-21 19:29:55 +01:00
Jukka Rissanen
4a796913cc net: arp: Enhance debug messages
Print more data / debug information for ARP messages.
Also remove unnecessary "&" when printing IPv4 address.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-01-21 19:29:55 +01:00