Commit graph

554 commits

Author SHA1 Message Date
Robert Lubos 3bc50871bc net: socket_services: Increase default stack size for DHCPv4 server
Increase socket services thread default stack size when DHCPv4 server is
enabled, as it uses synchronous processing.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-19 10:15:23 +00:00
Markus Lassila 681330aaf0 net: sockets: tls: Fix crashes in get DTLS CID socket options
Get TLS_DTLS_CID_STATUS and TLS_DTLS_PEER_CID_VALUE utilize
mbedtls_ssl_get_peer_cid, which expects that mbedtls_ssl_setup
has been done.

Signed-off-by: Markus Lassila <markus.lassila@nordicsemi.no>
2024-01-19 09:41:34 +01:00
Jukka Rissanen 5a933299bb net: socket: Change the printf modifier to print size_t correctly
The argument is size_t, so change the printf modifier to %zd
to avoid warning prints.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-18 20:13:31 +01:00
Jukka Rissanen 84ff0e8cdf net: socket: Allow user to tweak service dispatcher thread priority
User is able to tweak the socket service dispatcher thread
priority in order to get better performance from the system
if needed. By default the dispatcher thread runs in lowest
application thread priority (K_LOWEST_APPLICATION_THREAD_PRIO).

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-18 20:13:31 +01:00
Jukka Rissanen 16a54f251a net: sockets: Refactor accept() to support objcore better
If user has not supplied address pointer when calling accept(),
then we would not be able to figure out the used socket domain
properly. But as there is now SO_DOMAIN option supported, use
that to get the correct socket domain.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-17 09:55:40 +01:00
Jukka Rissanen 298ab2c95d net: socket: Add support for SO_DOMAIN option
The getsockopt() will return the address domain of the given
socket like AF_INET or AF_INET6.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-17 09:55:40 +01:00
Jukka Rissanen eff5d02872 net: sockets: Create a socket service API
The socket service provides a similar functionality as what
initd provides in Linux. It listens user registered sockets
for any activity and then launches a k_work for it. This way
each application does not need to create a thread to listen
a blocking socket.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-16 10:00:45 +01:00
Mirko Covizzi 998e839d67 net: sockets: prevent null pointer dereference
According to the POSIX specification, null pointer
is a valid value for the `address` argument
of the `accept` function.
This commit adds a check to prevent a null pointer
dereference inside `z_impl_zsock_accept`.

Signed-off-by: Mirko Covizzi <mirko.covizzi@nordicsemi.no>
2024-01-15 15:11:57 +01:00
Robert Lubos 898aa9ed9a net: sockets: tls: Align DTLS connect() behavior with regular TLS
DTLS socket is not really connection-less as UDP, as it required the
DTLS handshake to take place before the socket is usable. Therefore,
align the DTLS connect() behavior with regular TLS.
The change is backward compatible. connect() call is still optional for
DTLS socket (the handshake can still take place from send()/recv()) and
a socket option was provided to disable DTLS handshake on connect().

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-03 19:00:15 +00:00
Robert Lubos c0d5d2fbd5 net: sockets: tls: Allow handshake during poll()
When using DTLS socket, the application may choose to monitor socket
with poll() before handshake has been complete. This could lead to
potential crash (as the TLS context may have been left uninitialized)
and unexpected POLLIN reports (while handshake was still not complete).
This commit fixes the above - POLLIN will only be reported once
handshake is complete and data is available

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-03 19:00:15 +00:00
Robert Lubos 0a1bee48bf net: sockets: tls: Improve POLLERR error reporting
In case a socket error was caused by TLS layer, it was not reported with
POLLERR. This commit fixes this.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-03 19:00:15 +00:00
Robert Lubos 5b3b462eed net: sockets: tls: Add flag indicating that session is closed
In case TLS session is closed at the TLS level (and thus recv() reports
0 to the application) a certain race occurs between consecutive recv()
call, and TCP session teardown. As mbedtls_ssl_read() only reports
session close upon receiving CLOSE alert, consecutive non-blocking
recv() calls would report EAGAIN instead of connection closed, if called
before underlying TCP connection was closed.

Fix this, by storing the information that TLS session has ended at TLS
socket level. The new flag will be checked before attempting further
mbed TLS actions, so that connection status is reported correctly.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-03 19:00:15 +00:00
Robert Lubos 1dc9028316 net: sockets: tls: Add function to obtain underlying ssl context
For test purposes only. Should not be used in regular applications.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-01-03 19:00:15 +00:00
Jukka Rissanen 8f97c1c2ee net: lib: sockets: Initialize iovec to 0 at start of func
Make sure iovec is initialized to a value so that there
is no possibility that it is accessed uninitialized.

Fixes: #66838
Coverity-CID: 334911

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-01-03 10:19:50 +01:00
Robert Lubos 9aba4e8f3d net: sockets: tls: Read the actual error on interrupted wait
In case a waiting TLS socket reports an error in the underlying poll
call, try to read the actual error from the socket, instead of blindly
returning -EIO in all cases.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-12-20 11:10:57 +01:00
Robert Lubos a150380d65 net: tcp: Implement Keep-alive support
When a TCP connection is established, if there is no data exchange
between the two parties within the set time, the side that enables
TCP Keep-alive will send a TCP probe packet with the same sequence
number as the previous TCP packet. This TCP probe packet is an empty
ACK packet (the specification recommends that it should not contain
any data, but can also contain 1 nonsense byte, such as 0x00.). If
there is no response from the other side after several consecutive
probe packets are sent, it is determined that the tcp connection has
failed, and the connection is closed.

The keep-alive default parameters are aligned with Linux defaults.

Signed-off-by: Horse Ma <mawei@coltsmart.com>
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-12-11 10:11:10 +01:00
Jukka Rissanen bed63764d6 net: socket: Add IPv6 multicast join/leave via socket
Zephyr has its own multicast join/leave API but for
interoperability, it is possible to use the multicast
socket API and IPV6_ADD_MEMBERSHIP and IPV6_DROP_MEMBERSHIP
socket options.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 16:38:49 +00:00
Jukka Rissanen b58bddb85c net: socket: Add IPv4 multicast join/leave via socket
Zephyr has its own multicast join/leave API but for
interoperability, it is possible to use the multicast
socket API and IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP
socket options.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 16:38:49 +00:00
Jukka Rissanen b4a8e3ffff net: socket: Add support for adjusting IPv4 TTL
The IPv4 TTL could only manipulated via net_context interface.
It makes sense to allow the same from socket interface via
the setsockopt/getsockopt calls.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 15:07:43 +01:00
Jukka Rissanen 96ac91d1c9 net: Add support for adjusting IPv6 unicast hop limit
Add option support for adjusting the IPv6 unicast
hop limit value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 15:07:43 +01:00
Jukka Rissanen 1c684bc360 net: Add support for adjusting IPv6 multicast hop limit
Add option support for adjusting the IPv6 multicast
multicast hop limit value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 15:07:43 +01:00
Jukka Rissanen de0268def0 net: context: Add support for adjusting IPv4 multicast ttl
Add option support for adjusting the IPv4 multicast
time-to-live value.

Fixes #60299

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-04 15:07:43 +01:00
Jukka Rissanen 5d915398a4 net: sockets: Add additional checks to recvmsg()
Add extra checks that make sure that msg_iov is set
as we cannot receive anything if receive buffers are
not set.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-12-01 10:56:34 +00:00
Jukka Rissanen 1961adfb96 net: socket: Return ENOTSUP for unknown socket type in recvfrom()
If we for some reason are supplied unsupported socket type in
recvfrom(), then return ENOTSUP error to the caller instead of
silently accept it by returning 0.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-27 19:58:47 +01:00
Jukka Rissanen 5488e76bb2 net: socket: Add support for filling receive pktinfo data
If user has set either IP_PKTINFO (for IPv4) or
IPV6_RECVPKTINFO (for IPv6) socket options, then the system
will return relevant information in recvmsg() ancillary data.

Fixes #36415

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-27 19:58:47 +01:00
Jukka Rissanen 80704bb361 net: socket: Add support for setting pktinfo options
Add IP_PKTINFO or IPV6_RECVPKTINFO BSD socket options that
can be used to get extra information of received data in
the ancillary data in recvmsg() call.

For IPV6_RECVPKTINFO see RFC 3542 for details.
For IP_PKTINFO see Linux ip(7) manual page for details.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-27 19:58:47 +01:00
Jukka Rissanen 760c2f2949 net: sockets: Remove extra check from sendmsg()
There was double "if (status < 0)" check in sendmsg(),
remove the extra check.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-27 19:58:47 +01:00
Jukka Rissanen 4b365fab45 net: sockets: Add recvmsg() implementation
Add support for recvmsg() function which can return data
in msghdr struct (iovec).

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-27 19:58:47 +01:00
Declan Snyder cf42b8b2fb net: sockets: fix shadowing warning
Fix compiler local variable shadowing warning

Rename ret to bytes_sent in offending funciton

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-11-21 08:48:04 +00:00
Declan Snyder 7c72d4a2d6 net: Fix CMakeLists
Fix the CMakeLists of the tls_credentials and sockets folders
to link/interface to the net library instead of the zephyr library.
This fixes issues where some files are not found in the link interface
when compiling the sources in this folder.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-11-21 08:48:04 +00:00
Robert Lubos e6d90b409b net: sockets: tls: Set errno on TX waiting error
In case underlying socket reported error while waiting for TX, the
errno value was not set accordingly. This commit fixes this.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-11-21 08:46:45 +00:00
Jukka Rissanen 8157b48734 net: context: Add function to bound to a network interface
Helper function that marks the net_context to bound to a
network interface.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-11-17 12:40:33 +01:00
Anas Nashif a08bfeb49c syscall: rename Z_OOPS -> K_OOPS
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif ee9f278323 syscall: rename Z_SYSCALL_VERIFY -> K_SYSCALL_VERIFY
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 9c4d881183 syscall: rename Z_SYSCALL_ to K_SYSCALL_
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 9c1aeb5fd3 syscall: rename z_user_ to k_usermode_
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 56fddd805a syscall: rename z_user_from_copy -> k_usermode_from_copy
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 6ba8176e33 syscall: rename z_user_alloc_from_copy -> k_usermode_alloc_from_copy
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif df9428991a syscall: Z_SYSCALL_MEMORY_ARRAY -> K_SYSCALL_MEMORY_ARRAY
Rename macros and do not use Z_ for internal APIs.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 4d5d04169d syscall: rename z_is_in_user_syscall
Rename z_is_in_user_syscall -> k_is_in_user_syscall

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 4e396174ce kernel: move syscall_handler.h to internal include directory
Move the syscall_handler.h header, used internally only to a dedicated
internal folder that should not be used outside of Zephyr.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif a6b490073e kernel: object: rename z_object -> k_object
Do not use z_ for internal structures and rename to k_object instead.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif d2c025dd78 kernel: objects: rename z_dynamic_object_create -> k_object_create_dynamic
Do not use z_ for internal APIs and rename z_dynamic_object_create.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Jukka Rissanen 83c875adab hostap: Move the relevant config options away from hostap
Moving the Zephyr specific config options from
modules/hostap/Kconfig to corresponding Kconfig where the
option is specified.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-26 09:48:47 +02:00
Jukka Rissanen 7d9f2ad2ca net: sockets: Add object core support to sockets
Use the generic object core support to track network sockets
and their statistics.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-24 11:11:30 +02:00
Jukka Rissanen 5bf18e39ad net: sockets: Set writefds in case of error in select()
The writefds is typically set if there is an error while
waiting for example the connect() to finish. So check if
the user supplied the writefds and update it accordingly.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-24 00:48:03 -07:00
Jukka Rissanen b864880000 net: sockets: Add SO_ERROR socket option to SOL_SOCKET level
Return the last socket error to user.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-24 00:48:03 -07:00
Jukka Rissanen 4f37d63ed1 net: Add support for v4-mapping-to-v6 sockets
This allows IPv4 and IPv6 share the same port space.
User can still control the behavior of the v4-mapping-to-v6
by using the IPV6_V6ONLY socket option at runtime.
Currently the IPv4 mapping to IPv6 is turned off by
default, and also the IPV6_V6ONLY is true by default which
means that IPv4 and IPv6 do not share the port space.
Only way to use v4-mapping-to-v6 is to enable the Kconfig
option and turn off the v6only socket option.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-23 09:57:22 -05:00
Jukka Rissanen 0a16d5c7c3 net: socket: mgmt: Check buf size in recvfrom()
Return EMSGSIZE if trying to copy too much data into
user supplied buffer.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-12 10:33:36 +03:00
Flavio Ceolin e7bd10ae71 random: Rename random header
rand32.h does not make much sense, since the random subsystem
provides more APIs than just getting a random 32 bits value.

Rename it to random.h and get consistently with other
subsystems.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-10 14:23:50 +03:00
Daniel Leung d47b1c05f3 kernel: userspace: add k_object_is_valid()
This adds a function k_object_is_valid() to check if a kernel
object exists, of certain type, and has been initialized.
This replaces the same (or very similar) code that has been
copied from kernel into the network subsystem.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-09-28 17:28:43 -04:00
Anas Nashif 6d23a960db lib: os: build fdtable conditionally
Stop building fdtable by default, make it conditional and build it only
when needed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-28 06:25:16 -04:00
Flavio Ceolin 5d505c7b28 random: Fix feature dependency usage
Code using sys_csrand_get should depend on CONFIG_CSPRNG_ENABLED symbol
and not in ENTROPY_HAS_DRIVER since they are not using the entropy
device directly.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-09-27 11:55:10 -05:00
Tobias Frauenschläger dcc63120cf net: sockets: add support for SO_REUSEPORT
This commits adds support for the SO_REUSEPORT socket option.

The implementation follows the behavior of BSD and tries to also follow
the specific additional features of linux with the following
limitations:
* SO_REUSEADDR and SO_REUSEPORT are not "the same" for client sockets,
  as we do not have a trivial way so identify a socket as "client"
  during binding. To get the Linux behavior, one has to use SO_REUSEPORT
  with Zephyr
* No prevention of "port hijacking"
* No support for the load balancing stuff for incoming
  packets/connections

There is also a new Kconfig option to control this feature, which is
enabled by default if TCP or UDP is enabled.

Signed-off-by: Tobias Frauenschläger <t.frauenschlaeger@me.com>
2023-09-20 08:56:31 +02:00
Tobias Frauenschläger 3d3a221b1e net: sockets: add support for SO_REUSEADDR
This commit adds support for the SO_REUSEADDR option to be enabled for
a socket using setsockopt(). With this option, it is possible to bind
multiple sockets to the same local IP address / port combination, when
one of the IP address is unspecified (ANY_ADDR).

The implementation strictly follows the BSD implementation and tries to
follow the Linux implementation as close as possible. However, there is
one limitation: for client sockets, the Linux implementation of
SO_REUSEADDR behaves exactly like the one for SO_REUSEPORT and enables
multiple sockets to have exactly the same specific IP address / port
combination. This behavior is not possible with this implementation, as
there is no trivial way to identify a socket to be a client socket
during the bind() call. For this behavior, one has to use the
SO_REUSEPORT option in Zephyr.

There is also a new Kconfig to control this feature similar to other
socket options: CONFIG_NET_CONTEXT_REUSEADDR. This option is enabled by
default if TCP or UDP are enabled. However, it can still be disabled
explicitly.

Signed-off-by: Tobias Frauenschläger <t.frauenschlaeger@me.com>
2023-09-20 08:56:31 +02:00
Ambroise Vincent bb450eb26f net: sockets: Keep lock when notifying condvar
Releasing the lock before notifying condvar led to a race condition
between a thread calling k_condvar_wait to wait for a condition variable
and another thread signalling for this same condition variable. This
resulted in the waiting thread to stay pending and the handle to it
getting removed from the notifyq, meaning it couldn't get woken up
again.

Signed-off-by: Ambroise Vincent <ambroise.vincent@arm.com>
2023-09-18 15:41:23 -04:00
Chaitanya Tata 40ee8791f2 net: socketpair: Fix use after free
In low memory conditions, its possible for socketpair memory allocation
to fail and then the socketpair is freed but after that the remote
semaphore is released causing a crash.

Fix this by freeing the socketpair after releasing the semaphore. Add a
test case to induce low memory conditions (low HEAP and high socketpair
buffer size), with the fix issue is not seen.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2023-09-18 20:34:12 +02:00
Martin Jäger eae44a55d8 net: lib: sockets: sockets_tls: prefix mbedtls error with 0x
The errors are printed in hex, but no prefix was used. This could be
confused with usual errno return values. The 0x prefix makes clear
that it's a hex value.

Also a missing minus sign is added to one log message.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-18 10:38:44 +01:00
Carles Cufi 8c748fd005 kernel: Modify the signature of k_mem_slab_free()
Modify the signature of the k_mem_slab_free() function with a new one,
replacing the old void **mem with void *mem as a parameter.

The following function:
void k_mem_slab_free(struct k_mem_slab *slab, void **mem);

has the wrong signature. mem is only used as a regular pointer, so there
is no need to use a double-pointer. The correct signature should be:
void k_mem_slab_free(struct k_mem_slab *slab, void *mem);

The issue with the current signature, although functional, is that it is
extremely confusing. I myself, a veteran Zephyr developer, was confused
by this parameter when looking at it recently.

All in-tree uses of the function have been adapted.

Fixes #61888.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2023-09-03 18:20:59 -04:00
Jukka Rissanen 7aa4904b5a net: socket: Change SO_BINDTODEVICE to use interface name
Make sure we use the network interface name (if configured)
instead of device name when binding to certain network
interface.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-08-31 14:43:36 +02:00
Robert Lubos d8a96b1be0 net: sockets: tls: Implement TLS_DTLS_CID option
Add TLS_DTLS_CID socket option, which enables to use the Connection ID
extension for the DTLS session.

The option provides control of the use of CID with the `setsockopt()`
function. The value provided can disable, enable, and control whether to
provide a CID to the peer. It uses a random self CID (if told to provide
one to the peer) unless TLS_DTLS_CID_VALUE set previously.

Add TLS_DTLS_CID_VALUE to get or set the CID sent to the peer, if any.

Add TLS_DTLS_PEER_CID_VALUE to get the CID value provided by the peer,
if any.

Add TLS_DTLS_CID_STATUS to determine if CID used, and whether
bidirectional or one way.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Signed-off-by: Pete Skeggs <peter.skeggs@nordicsemi.no>
2023-08-30 11:36:51 +02:00
Robert Lubos d5252cb5de net: sockets: Fix getsockname()
getsockname() did not work properly on bound sockets, as it verified
whether the socket has an active connection before retuning result. This
is not correct, as socket after bound may not have a connection yet.

Fix this, by verifying that local_addr on an underlying net_context is
set, to determine whether socket has a local address assigned, before
returning result.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-08-27 07:35:34 -04:00
Robert Lubos 6e1a205819 net: sockets: Fix connected datagram socket packet filtering
The previous patch to address race condition on STREAM sockets had a
side effect on DGRAM socket, where net_context_recv() is not only
installing recv callback, but also registering a connection at net_conn
level. Doing so before setting remote address first (which is done in
net_context_connect()) had an impact on the connected DGRAM socket
operation, which now accepted packets from any remote peer, and not only
the one socket was connected to.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-08-25 10:31:19 +02:00
Christopher Friedt 4a095bb34b net: sockets: support fionbio and fionread
The `ioctl()` interface is already supported by the network
subsystem but there was no `zsock_` interface available for it.

Add the `zsock_ioctl()` syscall.

Implement two somewhat commont ioctl requests for socket
file descriptors; namely

- `FIONBIO` set non-blocking I/O mode
- `FIONREAD` get the number of available bytes

In the process, added `net_pkt_ip_proto_hdr_len()`

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-22 09:59:44 +02:00
Christopher Friedt 501c56cce7 net: socketpair: support for fionbio
Support for setting non-blocking mode via `ZFD_IOCTL_FIONBIO`.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-22 09:59:44 +02:00
Christopher Friedt e0ac4eb5cd net: sockets: socketpair: support querying bytes available
In order to get a semi-accurate assessment of how many
bytes are available on a socket prior to performing a read,
BSD and POSIX systems have typically used

`ioctl(fd, FIONREAD, &avail)`

We can support this in Zephyr as well with little effort, so
add support for `socketpair()` sockets as an example.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-22 09:59:44 +02:00
Christopher Friedt 1fa2ea1c82 net: sockets: tcp: split recv_stream into immediate and timed
Previously, if a net_context had multiple packets already in
the receive queue, and a call to zsock_recvfrom() was made with
a buffer large enough to receive content from multiple packets,
only the content from a single receive buffer would be received.

Since zsock_recvfrom() is a system call, which has a
non-negligible overhead, it makes sense to receive as many bytes
as possible per system call.

Add zsock_recv_stream_immediate() as a shorthand for
"fill this receive buffer with as many bytes as possible without
blocking". Allow nullable buffer parameters so that we can also
have a shorthand for "count how many bytes are immediately
available".

With minor refactoring, zsock_recv_stream_timed() is a simple
wrapper around zsock_recv_stream_immediate() that handles timing
and error conditions.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-22 09:59:44 +02:00
Sjors Hettinga 81650746f7 net: socket: Make the send timeout configurable
When the protocol layer like TCP is blocking transmission, the socket
layer will attempt and wait for a maximum amount of time before returning
with an ENOBUFS error.
This change allows to set the maximum waiting time from the configuration
file instead of using a fixed 10 second value.

Signed-off-by: Sjors Hettinga <s.a.hettinga@gmail.com>
2023-08-16 10:30:33 +02:00
Daniel Leung 5bc08ae3c6 net: rename shadow variables
Renames shadow variables found by -Wshadow.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-08-10 08:14:43 +00:00
Daniel Mangum 775a8e8c8d net: sockets: use DTLS in NET_SOCKETS_TLS_MAX_APP_PROTOCOLS
Updates NET_SOCKETS_TLS_MAX_APP_PROTOCOLS Kconfig option description to use
DTLS instead of DTL.

Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
2023-08-07 11:27:33 +02:00
Seppo Takalo c8ac3070cc net: sockets: socketpair: Allow statically allocated socketpairs
When the target board does not have heap by default, allows
statically reserving the space for required socketpairs.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2023-07-31 14:49:05 +02:00
Nicolas Pitre 603cdaa032 subsys/net/lib/socket: move to timepoint API
Remove sys_clock_timeout_end_calc() usage and custom timeout_recalc().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2023-07-25 09:12:26 +02:00
Daniel Leung 1e1ab38bf0 net: syscalls: use zephyr_syscall_header
This adds a few line use zephyr_syscall_header() to include
headers containing syscall function prototypes.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-06-17 07:57:45 -04:00
Robert Lubos e6fc53b399 net: sockets: tls: Allow to interrupt blocking accept() call
In order to allow the TLS accept() call to be interrupted, it should
release the top-level TLS socket mutex before blocking. As the
underlying TCP accept() makes no use of TLS resources, and has its own
mutex protection, it should be safe to do so.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-05-29 14:34:03 -04:00
Robert Lubos 76b74f007f net: sockets: Fix accept() not being interrupted on close()
The accept() so far would block with mutex held, making it impossible to
interrupt it from another thread when the socket was closed.

Fix this, by reusing the condvar mechanism used for receiving. It's OK
to use the same routine, as underneath accept() is monitoring the same
FIFO as recv().

Additionally, simplify k_fifo_get() handling in accept() - as the
waiting now takes place on condvar, it can be used in a non-blocking
manner. Blocking accept() call should not reach this place if there's no
new incoming connection waiting on the FIFO.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-05-29 14:34:03 -04:00
Robert Lubos 2c75070360 net: sockets: tcp: Fix possible race between connect/recv
Installing recv callback with net_context_recv() after
net_context_connect() left an opening for a possible race - in case the
server send some data immediately after establishing TCP connection, and
Zephyr did not manage to install the callback on time, the data would be
lost, corrupting the stream.

This can be avoided, by installing the recv callback before the
connection is triggered. As net_context_recv() called w/o timeout only
registers the callback function, it should have no negative impact. The
only change on the TCP side is when the connection is closed - in case
TCP is in connect stage, do not call the recv callback (before this
change it'd be NULL at that point).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-05-26 09:55:13 -04:00
Robert Lubos 966eff642f net: sockets: Fix recv() not being interrupted on close()
In case recv() call was waiting for data, and the socket was closed from
another thread, the recv() call would not be interrupted, causing the
receiving thread to be blocked indefinitely.

Fix this, by signalling the condvar the recv() call is waiting on
close(). Additionally, close will now set the socket into error mode,
with EINTR as the error condition, allowing the blocked calls to
recognise that the call was interrupted, and return a proper error code
on the event.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-05-26 09:05:12 -04:00
Gerard Marull-Paretas dacb3dbfeb iterable_sections: move to specific header
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-22 10:42:30 +02:00
Wojciech Slenska c3575fbd2e net: sockets: fix POLLOUT for offloaded iface
For offloaded iface net_tcp_get is never called, so context->tcp
is always NULL. In that case net_tcp_tx_sem_get will return wrong pointer.
For pollout k_poll will be called with NULL semph,
which cause HardFault.

Signed-off-by: Wojciech Slenska <wsl@trackunit.com>
2023-04-19 17:15:12 +02:00
Daniel Nejezchleb 663b684fea net: socket: fix hanging net contexts
Calls put instead of unref on net contexts
in the socket accept function.
Mere unref didn't subtract the reference
count of net context which leaves
it in used state. This situation happens
in case of accepting already
closed connection.

Signed-off-by: Daniel Nejezchleb <dnejezchleb@hwg.cz>
2023-04-17 15:12:41 +02:00
Daniel Nejezchleb ee720b5412 net: socket: asynchronous connect
Added a feature of socket connect
being asynchronous. If socket is set
to nonblock with O_NONBLOCK flag,
then connect() is non-blocking aswell.
App can normally poll the socket to
test when the connection is established.

Signed-off-by: Daniel Nejezchleb <dnejezchleb@hwg.cz>
2023-04-17 11:35:20 +02:00
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Robert Lubos afaf4cddd2 net: sockets: tls: Implement handshake timeout
Currently, the handshake operation could only be fully blocking or
non-blocking. This did not play well if SO_RCVTIMEO was set for DTLS
server, as the recv() call where the blocking handshake was used, could
block indefinitely, ignoring the timeout parameter. Fix this, by
allowing for the handshake operation to timeout.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-04-12 11:09:58 +02:00
Robert Lubos 9082d4b98e net: sockets: tls: Implement TLS/DTLS socket TX/RX timeout
As the underlying socket operations for TLS/DTLS are now non-blocking,
it's no longer possible to rely on the underlying socket timeout
handling. Instead, implement SO_RCVTIMEO/SO_SNDTIMEO at the TLS socket
layer.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-04-12 11:09:58 +02:00
Robert Lubos 81be0f6d73 net: sockets: tls: Switch DTLS to use non-blocking socket operations
As for TLS, switch to use non-blocking operations on underlying socket.
This is a bit tricker for DTLS, as there were not truly blocking bio
(binary input/output) function for DTLS, as timeout had to been
implemented. It is possible though to implement non-blocking mbedTLS bio
function instead, and handle timeout outside of mbedTLS context, which
has been done in this commit.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-04-12 11:09:58 +02:00
Robert Lubos ee48ddc205 net: sockets: tls: Switch TLS to use non-blocking socket operations
Switch TLS sockets to use non-blocking socket operations underneath.
This allows to implement the socket blocking outside of the mbedTLS
context (using poll()), and therefore release the mutex for the time the
underlying socket is waiting for data. In result, it's now possible to
do blocking TLS RX/TX operations simultaneously from separate threads.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-04-12 11:09:58 +02:00
Robert Lubos 96e14ba91f net: sockets: tls: Implement ZFD_IOCTL_SET_LOCK handling
Implement ZFD_IOCTL_SET_LOCK so that TLS socket layer gets access to the
mutex protecting socket calls.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-04-12 11:09:58 +02:00
Chris Friedt ff2efd7ae5 net: socket: socketpair: remove experimental status
Socketpair functionality has matured enough to be used in a
consistent way now regardless of architecture or platform,
even on `native_posix`.

Remove the experimental status to reflect that.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-25 07:05:53 -04:00
Robert Lubos 66ae9153a6 net: sockets: Fix SO_SNDTIMEO handling
The TX timeout configured with SO_SNDTIMEO on a socket did not work
properly. If the timeout was set on a socket, the TX would work as if
the socket was put into non-blocking mode. This commit fixes this.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-03-20 16:54:41 +01:00
Robert Lubos 616797c429 net: sockets: Add helper function for recalculating remaining timeout
The timeout recalculation logic was duplicated across several routines,
therefore it makes sense to make a helper function out of it,
especially, that the same functionality would be needed for the send
routines.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2023-03-20 16:54:41 +01:00
Vidar Lillebø ca3d0c8ee9 mbedtls: Remove dependency on MBEDTLS_BUILTIN for MBEDTLS_DEBUG
Allows using MBEDTLS_DEBUG functionality when not using MBEDTLS_BUILTIN.

Signed-off-by: Vidar Lillebø <vidar.lillebo@nordicsemi.no>
2023-03-10 09:30:32 +01:00
Stig Bjørlykke 0f71a130ef net: sockets: getaddrinfo: Minor refactoring
Minor refactoring in getaddrinfo() to make the code easier to
read and to make handling IPv4 and IPv6 support more equal.

- Move common wait and error handling code to exec_query()
- Use the same check for CONFIG_NET_IPV4 and CONFIG_NET_IPV6
- Add extra sanity check for family before exec_query()
- Do not set errno when return DNS_EAI_ADDRFAMILY

Fix issue with setting port number for all DNS servers.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2023-02-21 15:02:35 +01:00
Chris Friedt c093678784 net: sockets: fix fcntl.h usage
If we are using `CONFIG_ARCH_POSIX`, then include
`<fcntl.h>`. Otherwise, include `<zephyr/posix/fcntl.h>`
since there are no requirements to use `CONFIG_POSIX_API`
internally.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-02-08 19:04:25 +09:00
Chris Friedt ac3efe70cd net: sockets: socketpair: header fixups
* include `<zephyr/posix/fcntl.h>` instead of `<fcntl.h>`
* drop unused logging header and module declaration
* reorder headers alphabetically

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-02-08 19:04:25 +09:00
Jared Baumann 5a62f2592f net: lib: Fix build warning for sockets_tls
Fixes issue where a build warning would be emmited for sockets_tls.c due
to usage of the deprecated fcntl.h header file.

Signed-off-by: Jared Baumann <jared.baumann8@t-mobile.com>
2023-01-28 08:01:03 -05:00
Chris Friedt c2a62f4ad7 net: sockets: conditionally include zephyr/posix/fcntl.h
Only include `<fcntl.h>` for `CONFIG_ARCH_POSIX`. Otherwise,
include `<zephyr/posix/fcntl.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt 6ac402bb3a net: socket: additional POSIX constants
The POSIX spec requires that `SO_LINGER`, `SO_RCVLOWAT`,
and `SO_SNDLOWAT`, and `SOMAXCONN` are defined in
`<sys/socket.h>`. However, most of the existing socket
options and related constants are defined in
`<zephyr/net/socket.h>`.

For now, we'll co-locate them. It would be
good to properly namespace things.

Additionally, a no-op for setsockopt for `SO_LINGER` to
make things Just Work (TM) for now.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Marco Argiolas d51182d57d net: lib: sockets: support IPv6-only use case with AF_UNSPEC
Setting `hints.ai_family` to `AF_UNSPEC` was causing
`net_getaddrinfo_addr_str()` and in turn `getaddrinfo()` to resolve the
literal SNTP SERVER first into IPv4 and then (if supported) IPv6 addresses.
 This was causing useless waste of time and memory in case IPv4 was not
supported. In addition, in case IPv4 addresses were not supported, other
system components (eg. SNTP) could fail due to the DNS returning IP
addresses with unsupported family type (ie. IPv4).
Now, if address family is not explicitly set to `AF_INET` (ie. IPv4), then
 no attempt is made to resolve SNTP server address into an IPv4 address.

Signed-off-by: Marco Argiolas <marco.argiolas@ftpsolutions.com.au>
2023-01-03 11:03:25 +01:00
Markus Fuchs ea17d5152d net: sockets_tls: Fix memory leak in socket
Fix file descriptor leak on unsupported socket protocols.

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2022-11-09 10:44:44 +01:00
Christoph Schnetzler c364721796 net: sockets: Prevent compiler error if warnings being treated as errors
If gcc compiler option -Werror is used the warning,

declared inside parameter list will not be visible outside of this
definition or declaration [-Werror]

is treated as error, for

sockets_internal.h:18:28: ‘struct net_context’
sockets_internal.h:19:32: ‘struct zsock_pollfd’
fdtable.h:108:17: ‘struct k_mutex’

Signed-off-by: Christoph Schnetzler <christoph.schnetzler@husqvarnagroup.com>
2022-11-09 09:15:32 +00:00