Commit graph

6489 commits

Author SHA1 Message Date
Andreas Ålgård
a5850b794c net: lib: dhcpv4_server: Add callback to allow user to set address
This functionality is useful to allow for conditional or static leases.

Signed-off-by: Andreas Ålgård <aal@ixys.no>
2024-07-28 07:30:48 +03:00
Marcin Gasiorek
313a92baf9 net: openthread: Set CIDR for NAT64
Implementation of functionality whitch configure CIDR for OT NAT64.

Signed-off-by: Marcin Gasiorek <marcin.gasiorek@nordicsemi.no>
2024-07-28 07:29:50 +03:00
Marcin Gasiorek
ac0a9d79ab net: openthread: Add NAT64 send and receive callbacks.
Packets are routed between OT and Zephyr net stacks.
For IPv4 these packets are managed by NAT64 by default.

Signed-off-by: Marcin Gasiorek <marcin.gasiorek@nordicsemi.no>
2024-07-28 07:29:50 +03:00
Pisit Sawangvonganan
444e135679 net: if: streamline interface name checking logic
This commit reduces `#if / #endif` pairs by leveraging the
`IS_ENABLED` macro:
- Removed `#ifdef / #endif` around `NET_L2_DECLARE_PUBLIC` in `net_l2.h`,
  enabling compilation without affecting link time if the configuration
  is unavailable.

`set_default_name` function:
- Replaced multiple `if` statements with `else if` to use the last `else`
  without indirectly checking `name[0] == '\0'`.
- Since `snprintk` guarantees null-termination if `sizeof(name) > 0`,
  the `-1` subtraction is unnecessary, eliminating the need for
  zero initialization in `char name[CONFIG_NET_INTERFACE_NAME_LEN + 1];`.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-28 07:29:01 +03:00
Vineeta S Narkhede
232c802c05 net: hostname: Add a function to set the hostname postfix as is.
Fixes: #72363

The existing function to set the postfix is converting postfix
string to hexadecimal. Adding a new function to handle a use case
where the provided postfix should be used as is without any conversion.

Signed-off-by: Vineeta S Narkhede <VineetaSNarkhede@Eaton.com>
2024-07-28 07:28:51 +03:00
Bas van Loon
85f6409043 net: if: Add net_if_ipv4_get_gw as helper function.
Most set functions have a get function as well, add the missing
function to get the set gateway IPv4 address.

Signed-off-by: Bas van Loon <basvanloon@betronic.nl>
2024-07-27 20:51:21 +03:00
Andrey Dodonov
0e699eb251 net: lib: dhcpv4: parse multiple DNS servers received from DHCP
If we receive multiple DNS servers via DHCP only the first one is used,
regardless of CONFIG_DNS_RESOLVER_MAX_SERVERS constant.
Parse DHCP option and save addresses that fit

Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
2024-07-27 20:49:57 +03:00
Pieter De Gendt
ad63ca284e kconfig: replace known integer constants with variables
Make the intent of the value clear and avoid invalid ranges with typos.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2024-07-27 20:49:15 +03:00
Pisit Sawangvonganan
b34349eed9 net: utils: improve hex digit character checking in net_bytes_from_str
Several refactors and improvements for `net_bytes_from_str` as follows:
- Replaced manual hex digit checks with `isxdigit()`.
- Changed variable `i` from unsigned int to size_t for consistency with
  the `strlen()` return type.
- Added `src_len` to store the result of `strlen(src)` to avoid
  multiple calls to `strlen` in the `for-loop`.
- Ensured casting to `unsigned char` before passing to `isxdigit()` to
  prevent undefined behavior.
- Explicitly cast the result of `strtol()` to `uint8_t` to match
  the buffer type.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-27 15:16:42 +03:00
Robert Lubos
bfe958a7f1 net: sockets: tls: Fix poll update event check
In case POLLIN is set, and no new application data has been detected,
the ztls_poll_update_ctx() should only return -EAGAIN if no other events
are available for the socket. Otherwise, the function may end up
busy-looping, in case for example POLLOUT is also monitored for the
socket.

Current check verifying that was wrong, as it caused to function to
return -EAGAIN even if some other events could've been reported for the
socket.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-27 15:13:17 +03:00
Maochen Wang
bf42164084 net: wifi: Fix the nm type check error
Fix the NM iface type check error, should use (1 << WIFI_TYPE_STA),
instead of WIFI_TYPE_STA. Same for WIFI_TYPE_SAP.

Signed-off-by: Maochen Wang <maochen.wang@nxp.com>
2024-07-27 15:09:20 +03:00
Fin Maaß
1f38d9dfef net: shell: iface: add infos from eth phy
add information about the ethernet
phy and the current link speed to the
net iface command.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-07-27 15:07:26 +03:00
Fin Maaß
2fb983a427 net: l2: ethernet: add way to access phy
add a way to access the ethernet phy
device via the ethernet device.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-07-27 15:07:26 +03:00
Pisit Sawangvonganan
1100393b79 net: lwm2m: optimize socket_loop by using local variables
Changes include:
- Introducing a local `ctx` variable to replace multiple
  `sock_ctx[i]` references.
- Using a local `revents` variable to simplify repeated
  `sock_fds[i].revents` checks.
- Consolidating conditional checks for socket events
  (ZSOCK_POLLERR, ZSOCK_POLLNVAL, and ZSOCK_POLLHUP) as they are
  individual bit definitions, allowing them to be checked simultaneously.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-27 10:47:58 +03:00
Gaofeng Zhang
a5781547d6 net: ipv4: Fix tx_pkts slab leak in send_ipv4_fragment
In the send_ipv4_fragment function, if net_pkt_get_data returns
fail, a tx_pkts slab leak will occur. If leak exceeds the
maximum number CONFIG_NET_PKT_TX_COUNT of tx_pkts slab, tx_ptks
will be used up, and the related modules(dhcp, ping and so on)
of net cannot alloc tx_pkt slab, and will sleep and can not
return successfully until available slab.
dhcp work or ping work cannot be executed beacause can't alloc
memory, and ping command cannot return, console also fails to
input commands, and the console hang problem occurs.

Signed-off-by: Gaofeng Zhang <gaofeng.zhang@nxp.com>
2024-07-27 10:45:14 +03:00
Konrad Derda
c2747acbff net: ipv6: routing: move checking for an own source address
When IPv6 packet is received, there is a check of the packet's source
address to verify that it is not interface's non-tentative address.

This commit moves this check to the later stages of processing as
packets that can be routed are dropped in the early stage otherwise.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
2024-07-27 10:44:48 +03:00
Konrad Derda
b41b7d5da8 net: ipv6: routing: do not loop back already routed packets
If the packet was routed between interfaces by IPv6 module it should
not be looped back but has to be passed to the destination interface
instead.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
2024-07-27 10:44:48 +03:00
Konrad Derda
807daf3788 net: ipv6: routing: do not use an original source address for NS
When a packet's transmission is prepared and Neighbor Solicitation is
sent its souce address is always the same as of a packet awaiting -
also for packets routed from an other interface.

Quote from the RFC:
If the source address of the packet prompting the solicitation is the
same as one of the addresses assigned to the outgoing interface, that
address SHOULD be placed in the IP Source Address of the outgoing
solicitation.  Otherwise, any one of the addresses assigned to the
interface should be used.

This commit fixes the behavior.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
2024-07-27 10:44:48 +03:00
Konrad Derda
79f7b400cb net: ipv6: routing: match destination address to a given interface
Before packet is routed there is a check verifying if the destination
address is not the device's one. However, the check should be limited
to the packet's original interface. Otherwise, packet cannot be routed
if it is destined for an other interface of the device.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
2024-07-27 10:44:48 +03:00
Seppo Takalo
6addba5698 net: lwm2m: Verify X509 hostname when URI contains valid name
Enable hostname verification in DTLS handshake when server URI contains
valid hostname.
When URI is given just as IP address, don't fill up the
ctx->desthosname or enable hostname verification.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-07-27 10:44:27 +03:00
Bjarki Arge Andreasen
280fbec58e net: lib: sockets: socketpairs: Update socketpair mem alloc
The memory allocation for socketpairs is not conformant to the new
MEM_POOL_ADD_SIZE_ mechanism for allocating heap memory.

Specifically CONFIG_NET_SOCKETPAIR_HEAP can not be selected unless
the user has specified CONFIG_HEAP_MEM_POOL_SIZE. We should be
using MEM_POOL_ADD_SIZE_ to add to the heap if the user wants to
use it for socketpair allocation.

Additionally increase the size of pre-allocated sockets to 8 from 1
to support larger devices by default, taking into consideration the
required socketpairs and buffer sizes when using
WIFI_NM_WPA_SUPPLICANT.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2024-07-27 10:43:32 +03:00
Robert Lubos
35fe353ad6 net: lib: coap_client: Replace send_mutex with more generic lock
Introduce a more generic mutex for protecting coap_client structure.

This allows to avoid a certain race condition when sending consecutive
CoAP requests. The case was, that when a CoAP receive thread notified
the application that a complete response was received, and the
application wanted to send another request from the application thread,
the consecutive call to coap_client_req() might've failed if the
application thread has higher priority than the CoAP receive thread
because of the request context cleanup is done after calling the
application callback.
Having a mutex, which is locked while processing the response, and when
attempting to send a new request allows to synchronize threads as
expected.

As this new mutex seemed redundant with the more specialized send_mutex
already present in the coap_client, the latter was removed (i. e.
transmission is also protected with the new mutex).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-27 10:43:15 +03:00
Robert Lubos
2555612265 net: lib: coap_client: Add static keyword where missing
Make sure internal functions are static. Remove unused function.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-27 10:43:15 +03:00
Fabian Pflug
ca53d2bf80 net: l2: ieee802154: mgmt: allow beacon payload
The standard does allow for a optional beacon payload, which gets lost
during scan, that could be interesting for the application to access
in the NET_EVENT_IEEE802154_SCAN_RESULT callback.

See section 7.3.1.6 in IEEE Std 802.15.4 for more information about
the beacon payload field. And section 7.3.1 and figure 7-5 about general
beacon frame format.

Signed-off-by: Fabian Pflug <fabian.pflug@grandcentrix.net>
2024-07-27 10:42:45 +03:00
Muhammad Munir
04011dafe0 net: lib: dhcpv4: added router id in response options
When running dhcp, it was not assigning gateway/router
address to the client devices. It is fixed in this PR.

Signed-off-by: Muhammad Munir <muhammad.munir@zintechnologies.com>
2024-07-27 10:40:17 +03:00
Jukka Rissanen
cf552905f4 net: context: Check null pointer in V6ONLY getsockopt
Make sure we are not accessing NULL pointer when checking
if the IPv4 mapping to IPv6 is enabled.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-07-27 10:39:40 +03:00
Pisit Sawangvonganan
10f495e0fd net: lib: fix typo
Utilize a code spell-checking tool to scan for and correct spelling errors
in all files within the `subsys/net/lib` directory.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-12 09:33:20 -04:00
Pisit Sawangvonganan
d2af60687e net: l2: fix typo
Utilize a code spell-checking tool to scan for and correct spelling errors
in all files within the `subsys/net/l2` directory.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-12 09:33:20 -04:00
Pisit Sawangvonganan
d1781da2ba net: lwm2m: fix typo
Utilize a code spell-checking tool to scan for and correct spelling errors
in all files within the `subsys/net/lib/lwm2m` directory.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-12 09:33:20 -04:00
Robert Lubos
5251533f97 net: lib: http_server: Respect Connection close header
In case no "Connection: close" header is present in the request, the
server should keep the connection open for the client. Hence, after
serving a request, we need to check if the header was present (the
parser sets a flag for it), and only close the connection immediately,
if the client requested it. In case the client remains silent, the
connection will be closed anyway after the inactivity timer kicks in.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-11 13:11:04 -04:00
Robert Lubos
94b1f443ac net: sockets: Allocate FD after receiving connection in accept()
Allocating FD before pulling the new connection from the fifo can lead
to busy looping in certain cases. If the application keeps calling
accept() on a listening socket after failing to allocate new FD for the
incoming connection, it'll start busy looping, as will report POLLIN in
such case (as the new connection is still on the queue), but it'll
consistently fail with ENFILE.

This can be avoided by trying to allocate new file descriptor only after
new connection has been pulled from the fifo. That way, if we fail to
allocate the file descriptor, the incoming connection will be dropped,
which seems correct given we don't have enough resources to service it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-11 13:11:04 -04:00
Robert Lubos
0e601d4103 net: lib: http_server: Add restart delay
In case there were active connections when restarting the server, it
can't be re-initialized immediately, as binding to the server port will
fail. We need to wait for the TCP connection teardown, as even with
REUSEADDR socket option set, binding will fail if the sockets are not in
TCP TIMED_WAIT state (i. e. connections are active).

Because of this, add a configurable delay when restarting the server.
Additionally, make server initialization failures non-fatal, i. e. try
to restart the server again after the delay if the initialization fails.
It's been observed with Chromium, that it tends to keep connections open
even after closing them on the server side (socket lingers in FIN_WAIT_2
state), so the server re-initialization may fail even with delay, so
it's beneficial in  such case to keep retrying the server
re-initialization.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-11 13:11:04 -04:00
Robert Lubos
fc10a94683 net: lib: http_server: Close sockets on server core errors
In case of fatal errors (during poll() or when handling listening
socket), the server operation is restarted. It was missed however, that
sockets opened for the server should be closed in such case.

Additionally, in case there were active client connections, it's needed
to cleanup related resources, otherwise running timers may trigger a
crash.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-11 13:11:04 -04:00
Robert Lubos
be7175351a net: lib: http_server: Fix HTTP1 POST request handling
In previous batch of fixes it was overlooked that streams are
HTTP2-specific concept. While for HTTP2 we need to track headers reply
state for each individual stream, at HTTP1 level we need to track this
at the client level. Hence, reintroduce respective flags to track
headers reply state, but only for HTTP1.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-11 13:10:09 -04:00
Jukka Rissanen
794d7cf3c5 net: sockets: Remove async service support
As found in PR #75525, we should not modify the polled fd array
in multiple places. Because of this fix, the async version of
the socket service could start to trigger while it is being handled
by the async handler. This basically means that the async version
cannot work as intended so remove its support.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-07-10 11:36:59 +02:00
Fin Maaß
f82249c932 net: ethernet: check vlan iface existence
Drop a packet, if it has a VLAN tag, for
that we don't have a VLAN interface.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-07-10 11:35:46 +02:00
Robert Lubos
9db2dc4aa2 net: lib: http_server: Move stream-specific flags to stream context
The information about replied headers or END_OF_STREAM flag are
stream-specific and not general for a client. Hence, need to move them
to the stream context.

For the upgrade case, we need to allocate a new stream now when HTTP1
request /w upgrade field is received. The stream ID in such case is
assumed to be 1 according to RFC.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
0e1c0a7b6b net: lib: http_server: Fix trailing headers frame processing
In case client decides to send a trailing headers frame, the last data
frame will not carry END_STREAM flag. In result, with current logic
server would not include END_STREAM flag either, causing the connection
to stall. This commit fixes this logic, so that the server replies
accordingly in case END_STREAM flag is present in the trailing headers
frame.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
b4cfee090d net: lib: http_server: Implement proper CONTINUATION frame processing
CONTINUATION frames are tricky, because individual header fields can be
split between HEADERS frame and CONTINUATION frame, or two CONTINUATION
frames. Therefore, some extra logic is needed when header parsing
returns -EAGAIN, as we may need to remove the CONTINUATION frame header
from the stream before proceeding with headers parsing.

This commit implements the above logic and additionally adds more checks
to detect when CONTINUATION frame is expected. Not receiving a
CONTINUATION frame when expect should be treated as a protocol error.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
7e22dd45a0 net: lib: http_server: Fix frame printouts
Frame printouts should not be done from the state handlers, but rather
during state transition, otherwise a single frame can be printed several
times as new data arrive. This also simplifies code a bit, as we just
print the frame in a single place, instead of duplicating code.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
78c2f48091 net: lib: http_server: Remove upper bounds on buffer sizes
There's really no good reason to have an upper bound on the buffer sizes
and this limits testing in some cases, so just remove them.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
ccf2e9b025 net: lib: http_server: Implement proper RST_STREAM frame processing
In case RST_STREAM frame is received it should not be ignored, but the
corresponding stream should be closed.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
b98edfd951 net: lib: http_server: Fix parsing of HTTP2 header frames with priority
In case priority flag is present in the HTTP2 headers frame header, we
should expect additional priority fields before the actual frame
content.

The stream priority signalling has been deprecated by RFC 9113, however
we should still be able to handle this in case some implementation
(nghttp for instance) sends them.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
2e288aed08 net: lib: http_server: Fix handling of HTTP2 frames with padding
Data and header frames can contain padding - we need to take this into
account when parsing them, otherwise the stream is broken.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
245b8351f4 net: lib: http_server: Unify HTTP2 header flags checking
Instead of multiplying function to check header flags, just have a
single one, with flag mask as parameter.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
adfdc54434 net: lib: http_server: Update HTTP2-specific structs/enums naming
For HTTP2-specific structures and enums, use "http2_" prefix to clearly
indicate the distinction from the generic HTTP stuff.

Additionally, some structures/enums describing HTTP2 protocol details
had "server" in the name, while in reality they describe nothing
server-specific. Hence, drop the "server" part where applicable.

Remove unused macros.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Robert Lubos
e1e50bdda9 net: lib: http_server: Simplify HTTP2 frame header parsing
* Remove unneeded variable.
* Use system utilities to read big endian numbers instead of parsing
  manually.
* Remove `payload` member from the http_frame structure. It's not used
  for anything useful, and could actually be misleading, as in case of
  large frames, where not entire frame is parsed at once it will point
  to incorrect location.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 15:20:57 -04:00
Chaitanya Tata
a63a96428e wifi_nm: Fix Wi-Fi interface backward compatibility
If newly introduced interface type is unset then return the first Wi-Fi
interface as a fallback, this fixes backward compatibility.

Also, add NM APIs and use them for type checks, rather than directly
using the type enumeration.

Fixes #75332.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2024-07-09 13:07:11 -04:00
Robert Lubos
e5d67adf80 net: sockets: services: Don't modify pollfd array from other threads
pollfd array used with zsock_poll() should not be modified while inside
zsock_poll() function as this could lead to unexpected results. For
instance, k_poll already monitoring some kernel primitive could report
an event, but it will not be processed if the monitored socket file
descriptor in the pollfd array was set to -1. In result,
zsock_poll() may unexpectedly quit prematurely, returning 0 events, even
if it was requested to wait infinitely.

The pollfd arrays used by zsock_poll() (ctx.events) is reinitialized
when the service thread is restarted so modifying it directly when
registering/unregistering service is not really needed. It's enough if
those functions notify the eventfd socket used to restart the services
thread.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-09 17:19:12 +02:00
Andrey Dodonov
a4123a8ea2 net: lib: websocket: call socket poll for websocket
If we couldn't send all (or any data) via the socket,
invoke poll instead of blindly retrying and flooding the socket

Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
2024-07-09 14:04:29 +02:00