Commit graph

2451 commits

Author SHA1 Message Date
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
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
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
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
Andrey Dodonov
15ead53ad9 net: lib: http: call socket poll for http_client send
If we couldn't send all (or any data) via the socket,
invoke poll instead of blindly retrying and flooding the socket.
Respect timeout through http_client_req

Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
2024-07-09 14:04:29 +02:00
Robert Lubos
4625fa713f net: mqtt: Fix possible socket leak with websocket transport
In case underlying TCP/TLS connection is already down, the
websocket_disconnect() call is expected to fail, as it involves
communication. Therefore, mqtt_client_websocket_disconnect() should not
quit early in such cases, as it could lead to an underlying socket leak.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-07-05 12:29:33 +02:00
Alberto Escolar Piedras
1df86af309 Revert "net: sockets: move poll implementation to zvfs"
This reverts commit 93973e2ead.

PR #73978 introduced a regression.
Unfortunately this PR cannot be reverted without reverting also
Let's revert both PRs to stabilize main again towards the 3.7 release.

For more details on the issue see
https://github.com/zephyrproject-rtos/zephyr/issues/75205

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-07-03 15:03:05 -04:00
Alberto Escolar Piedras
14e4de6415 Revert "net: sockets: move select() implementation to zvfs"
This reverts commit 49ac1912b2.

PR #73978 introduced a regression.
Unfortunately this PR cannot be reverted without reverting also
Let's revert both PRs to stabilize main again towards the 3.7 release.

For more details on the issue see
https://github.com/zephyrproject-rtos/zephyr/issues/75205

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-07-03 15:03:05 -04:00
Jukka Rissanen
9647db8808 net: dns: Avoid superfluous error message
If we have configured the DNS dispatcher to be only as a
responder but receive a query response, or if we are only
as a resolver but receive a query, then the dispatcher just
ignores the packet and returns -ENOENT.

Unfortunately we print an error message in this case

[00:10:18.818,000] <err> net_dns_dispatcher: DNS recv error (-2)

which is totally unnecessary and causes confusion so do not
print an error message in this case.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-07-02 14:11:15 +02:00
Benjamin Cabé
1f0c22eb01 net: ptp: fix incorrect req_timestamp decoding
Removed incorrect use of ntoh to decode Delay_Req timestamp

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2024-07-01 18:13:42 +02:00
Benjamin Cabé
a89a5ee52d net: ptp: fix offset check by adding missing int64_t cast
Add cast to NSEC_PER_SEC macro to correctly check offset.
Prior to this commit, the would eval to true incorrectly.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2024-07-01 18:13:42 +02:00
Chaitanya Tata
fbe7e95359 net: sockets_service: Fix thread failure
In case of thread failure, fix the registrations by properly managing
the synchronization i.e. use conditional wait only if thread is being
initialized or will be initialized, else check for success or failure
without waiting for conditional variables.

Fixes #73523.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2024-06-28 21:52:49 +02:00
Jordan Yates
91f8c1aea9 everywhere: replace #if IS_ENABLED() as per docs
Replace `#if IS_ENABLED()` with `#if defined()` as recommended by the
documentation of `IS_ENABLED`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-28 07:20:32 -04:00
Chris Friedt
49ac1912b2 net: sockets: move select() implementation to zvfs
Move the implementation of zsock_select() to zvfs_select(). This
allows other types of file descriptors to also make use of
select() functionality even when the network subsystem is not
enabled.

Additionally, it partially removes a dependency cycle between
posix and networking by moving functionality into a mutual
dependency.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
93973e2ead net: sockets: move poll implementation to zvfs
Move the implementation of zsock_poll to zvfs_poll. This allows
other types of file descriptors to also make use of poll()
functionality even when the network subsystem is not enabled.

Additionally, it partially removes a dependency cycle between
posix and networking by moving functionality into a mutual
dependency.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Adam Wojasinski
3b78e76554 net: lib: ptp: Fix issues raised by static analyzer
Fix minor issues reported by static analysis tool.

Signed-off-by: Adam Wojasinski <awojasinski@baylibre.com>
2024-06-27 08:48:59 -04:00
Jukka Rissanen
0cac7af4e4 net: dns: Do not fail if cache flush bit is set in class
Mask the DNS_CLASS_FLUSH value when checking if the DNS_CLASS_IN
is set when unpacking a query.

Fixes #74829

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-06-26 09:27:07 -04:00
Robert Lubos
0ca2a3cce0 net: lib: lwm2m: Fix expected block calculation
Coverity reported that a formula used to calculate the next expected
block in case the block size from the request differs our own block size
has a bug. The expression used to calculate the block size diff would
evaluate to an unsigned integer, giving (wrongly) enormous results in
case block size from the request is smaller than the Zephyr's default.

It turns out however, that this formula is no longer needed at all.
Since commit d3081e2f30, Zephyr's LwM2M
implementation will no longer negotiate the block size in case of write
operation, but simply comply with the block size included in the
request. This means that calculating the diff makes no longer sense and
can be safely removed - the next expected block number should be simply
increased by 1.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-26 08:59:56 -04:00
Robert Lubos
0528df9a4e net: shell: udp: Check net_pkt_read_u8 result
In case packet read fails for any reason, there's no point proceeding or
printing the byte, just break the loop in such case.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-26 08:59:41 -04:00
Robert Lubos
ee451e5cf0 net: sockets: tls: Fix iov_len comparison in sendmsg()
vec->iov_len is of type size_t, so the comparison was always true.
Additionally, doing the memcpy() when iov_len was 0 did not really make
sense, so do it only when the actual length is larger than 0.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-26 12:39:51 +02:00
Chris Friedt
9ada52f060 fdtable: replace z_ prefix with zvfs_ for fdtable.h functions
For each of the fdtable.h functions listed below, convert the
z_ prefixed semi-private functions to use the zvfs_ prefix.

ZVFS stands for Zephyr Virtual File System and
is intended to be a common library used by the C library,
POSIX API, Networking, Filesystem, and other areas.

There are already a few functions in fdtable.h that use the
zvfs_ prefix, so this change is mostly about unifying them in
a way that uses a suitable prefix ("namespace") so that it can
be considered a public API.

- z_alloc_fd
- z_fdtable_call_ioctl
- z_finalize_fd
- z_finalize_typed_fd
- z_free_fd
- z_get_fd_obj
- z_get_fd_obj_and_vtable
- z_get_obj_lock_and_cond
- z_reserve_fd

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-26 12:33:02 +02:00
Andreas Ålgård
eb4c76a6ec net: lib: dhcpv4_server: Fix client ID bug
First byte of client ID should be htype, followed by the client address.
See: https://datatracker.ietf.org/doc/html/rfc1533#section-9.12

Signed-off-by: Andreas Ålgård <aal@ixys.no>
2024-06-26 05:58:49 -04:00
Robert Lubos
ddf9e67169 net: lib: sockets: net_mgmt: Add note about thread priorities
Add a note about the thread priority requirements in the help string of
the Kconfig option enabling net_mgmt sockets.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-25 10:25:37 -04:00
Jukka Rissanen
8c1834a25a net: dns: mdns_responder: Interface name might miss terminating null
The network interface name that is copied to if_req struct might
be missing terminating null.

Fixes #74795
Coverity-CID: 368797

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-06-24 14:48:03 -04:00
Jukka Rissanen
b1a1a534ff net: websocket: Remove dead code
The zsock_close() is not needed as the fd is always <0 so
the close is never called.

Fixes #74791
Coverity-CID: 366273

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-06-24 14:47:52 -04:00
Robert Lubos
b854d8b466 net: sockets_service: Increase stack size for mDNS responder
Tests showed that mDNS responder needs slightly more stack on certain
platforms, hence increase the default for such case.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-24 12:42:16 -04:00
Robert Lubos
027a19a565 net: lib: dns_sd: Fix buffer sizes on query processing
A few issues were identified with DNS_SD segment buffers, making it work
"by chance" (depending on memory layout):

- size check in dns_sd_query_extract() did not take NULL termination
  into account, and in result could overflow provided buffer
- the proto_buf in send_sd_response() can either be used to parse
  protocol or domain, depending on number of segments in the query.
  It should therefore be large enough to hold either.
- Similarily, instance_buf should be able to hold
  DNS_SD_INSTANCE_MAX_SIZE, not DNS_SD_SERVICE_MAX_SIZE.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-24 12:42:16 -04:00
Robert Lubos
d9a979f2dc net: lib: dhcpv4: Fix typo in logs
DHCPv4 was misspelled in logs.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-24 12:41:38 -04:00
Robert Lubos
cd5e1cd0a5 net: lib: http_server: Fix possible NULL pointer dereference
Fix possible NULL pointer dereference in http_hpack_decode_header().

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-24 12:41:22 -04:00
Jukka Rissanen
a0b56dab1a net: dns: llmnr_responder: Fix uinit variable
The family variable was not initialized and could potentially
be left like that. This could only affect error printout.

Fixes #74796
Coverity-CID: 368799

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-06-24 12:41:06 -04:00
Alberto Escolar Piedras
f13196429e subsys/net/lib/http: Set feature macro as required
This file uses strnlen() but the C library
is not require to expose its prototype unless
_POSIX_C_SOURCE is defined.
So let's define it to avoid an implicit function
declaration warning.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-06-24 13:01:38 +02:00
Jordan Yates
07870934e3 everywhere: replace double words
Treewide search and replace on a range of double word combinations:
    * `the the`
    * `to to`
    * `if if`
    * `that that`
    * `on on`
    * `is is`
    * `from from`

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-22 05:40:22 -04:00
Robert Lubos
abc91227b2 net: lib: zperf: Fix socket leak during asynchronous TCP upload
In case of TCP upload error, zperf would leak a socket when running in
asynchronous mode. The upload work have to release the socket it
allocated in any case, regardless of the session result.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-21 11:38:11 -04:00