This commit applies the issues detected in UDP to recv_raw() as
well. Please refer to the previous commit log for details.
Signed-off-by: Takuya Sasaki <takuya.sasaki@spacecubics.com>
When receiving a UDP packet, net_conn_input() searches for a
matching connection within `conn_used`.
However, when receiving UDP packets simultaneously from multiple
clients, we may encounter a situation where the connection that was
supposed to be bound cannot be found within `conn_used`, and raise
the ICMP error.
This is because, within recv_udp(), to avoid the failure of
bind_default(), we temporarily remove it from `conn_used` using
net_conn_unregister().
If the context already has a connection handler, it means it's
already registered. In that case, all we have to do is 1) update
the callback registered in the net_context and 2) update the
user_data and remote address and port using net_conn_update().
Fixes#70020
Signed-off-by: Takuya Sasaki <takuya.sasaki@spacecubics.com>
If we receive a IPv4 packet to v4 mapped address, the relevant
net_context is bound to IPv6. This causes issues if we try
to get the family from the context struct in sendto.
Fix this by checking if the destination address is IPv4 but
the socket is bound to IPv6 and v4 mapping is enabled.
If all these criterias are set, then set the family of the
packet separately and do not get it from net_context.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
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>
If some specific option is not enabled, then add missing
ARG_UNUSED() calls in relevant functions.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Set separate option setters for bool, uint8_t and uint16_t
values. Use those generic setters when storing the desired
option value.
The uint16_t option setter stores the value to uint16_t variable
and expects that user supplies int value.
For uint8_t value, it is expected that uint8_t value is supplied
instead of int.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Set separate option getters for bool, uint8_t and uint16_t
values. Use those generic getters when fetching the desired
option value.
Noticed mixed usage (bool vs int) for txtime option. Changed
the code to use int type like in other options.
The uint16_t option getter gets the value from uint16_t variable
but returns int value to the caller, and also expects that user
supplies int value.
For uint8_t value, it is expected that uint8_t value is supplied
instead of int.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Add low level support for setting IP_PKTINFO or IPV6_RECVPKTINFO
socket options. The support is disabled by default.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Rework how data is queued for the TCP connections:
* net_context no longer allocates net_pkt for TCP connections. This
was not only inefficient (net_context has no knowledge of the TX
window size), but also error-prone in certain configuration (for
example when IP fragmentation was enabled, net_context may attempt
to allocate enormous packet, instead of let the data be fragmented
for the TCP stream.
* Instead, implement already defined `net_tcp_queue()` API, which
takes raw buffer and length. This allows to take TX window into
account and also better manage the allocated net_buf's (like for
example avoid allocation if there's still room in the buffer). In
result, the TCP stack will not only no longer exceed the TX window,
but also prevent empty gaps in allocated net_buf's, which should
lead to less out-of-mem issues with the stack.
* As net_pkt-based `net_tcp_queue_data()` is no longer in use, it was
removed.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Allow user to bind to different network interface. This is
useful if binding a multicast address to a certain network
interface.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
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>
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>
Currently context->local is not set for offloaded interface.
This change move net_offload_bind call after set of context->local.
Signed-off-by: Wojciech Slenska <wsl@trackunit.com>
A little refactoring that simplifies dealing with nanosecond timestamp
values in packets and further decouples calling code from PTP:
Benefits:
- simplifies calling code by removing redundant conversions.
- prepares for removing PTP dependencies from net_pkt.
Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
It is always possible to bind to same port if the sockets
are in different address family.
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
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>
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>
Remove the shadowing ret variable and fix a bug that was related to
its existence - the shadowing ret variable was assigned with -ETIMEDOUT
which was supposed to be retuned by the function, but was not because
the ret variable at the function scope was left intact.
Also remove the unneded goto unlock; jump (assigning the error code is
the last operation before unlocking the mutex anyway).
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
The IEEE 802.15.4 API and networking subsystem were using several
inconsistent timestamp resolutions and types. This change defines all
timestamps with nanosecond resolution and reduces the number of
available types to represent timestamps to two:
* `struct net_ptp_time` for PTP timestamps
* `net_time_t` for all other high resolution timestamps
All timestamps (including PTP timestamps) are now referred to a
"virtual" local network subsystem clock source based on the well-defined
types above. It is the responsibility of network subsystem L2/driver
implementations (notably Ethernet and IEEE 802.15.4 L2 stacks) to ensure
consistency of all timestamps and radio timer values exposed by the
driver API to such a network subsystem uptime reference clock
independent of internal implementation details.
The "virtual" network clock source may be implemented based on arbitrary
hardware peripherals (e.g. a coarse low power RTC counter during sleep
time plus a high resolution/high precision radio timer while receiving
or sending). Such implementation details must be hidden from API
clients, as if the driver used a single high resolution clock source
instead.
For IEEE 802.15.4, whenever timestamps refer to packet send or receive
times, they are measured when the end of the IEEE 802.15.4 SFD (message
timestamp point) is present at the local antenna (reference plane).
Due to its limited range of ~290 years, net_time_t timestamps (and
therefore net_pkt timestamps and times) must not be used to represent
absolute points in time referred to an external epoch independent of
system uptime (e.g. UTC, TAI, PTP, NTP, ...).
Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
Set default offloaded interface during net_context_get() call, so that
net_context_recv() can be called before net_context_connect(). There is
already an assumption about using default network interface, so this should
not be harmful.
Fixes: 2c75070360 ("net: sockets: tcp: Fix possible race between
connect/recv")
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
In previous patch fixing this issue, I've missed the fact that offloaded
drivers would not set the context->local address, which resulted in a
regression, where the previously introduced assert would hit in
offloaded cases. Not setting laddr is not a problem in case of
offloading, as it's only used in net_tcp_connect() which would not be
reached in this case.
Therefore I propose to remove previous patch to get rid of regression.
As an alternative fix, verify the laddr just before use, so that it is
only checked when native net stack is in use.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Coverity reported, that laddr pointer used in net_context_connect()
could be passed as NULL to net_tcp_connect() where it could be
dereferenced. This is because the actual setting of laddr to a valid
address structure was only done after
net_sin/sin6_ptr(&context->local)->sin/sin6_addr verification.
In practice though, the aforementioned pointer verification would always
pass, as the bind_default() guarantee that the context->local address is
set to an unspecified address (if it hasn't been set earlier).
Therefore refactor the code a bit: replace the pointer verification
with NET_ASSERT - only to assure that we can catch regression in case
for any reason the behavior of bind_default() changes. This should also
ensure that Coverity no longer reports that laddr is NULL when reaching
net_tcp_connect().
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
If a socket has DSCP set then the packets from the socket should also be
marked with appropriate priority in case traffic classes are used in
networking stack.
Signed-off-by: Krishna T <krishna.t@nordicsemi.no>
As `net_if_get_link_addr()` returns a pointer to the link layer
structure, some extra protection is needed to prevent its contents from
being changed while accessed. Using the mutex lock associated with an
interface should do the trick.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
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>
The net_context/TCP connection context ref counting needed to be fixed,
to avoid situation where TCP connection context could be released
before net_context is released.
Generally, this commit modifies the ref counting of the above as
follows:
* net_context is referenced both by the application and TCP stack, when
created.
* TCP context is referenced by the application when created. The TCP
stack adds ref to the TCP context only after connection has been
established.
* TCP stack needs to call the final upper layer callback when connection
is closed, but before the TCP context is released (i. e. to give upper
layer chance to dereference the context on its side). For this,
tcp_conn_close() was introduced which is called from within TCP stack
instead of directly dereferencing the TCP context.
* TCP stack dereferences the net_context only after the TCP context has
been released (i. e. upper layer has dereferenced TCP context on it's
side and connection has been tear down, if established).
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Setting a detected packet family (ipv4 or ipv6) in net_context level
instead in lower layers for AF_PACKET/SOCK_RAW/IPPROTO_RAW type sockets
when sending data.
Signed-off-by: Jani Hirsimäki <jani.hirsimaki@nordicsemi.no>
Clean up occurrences of "#if IS_ENABLED(CONFIG_FOO)" an replace
with classical "#if defined(CONFIG_FOO)".
Signed-off-by: Erwan Gouriou <erwan.gouriou@linaro.org>
Add a new net_context option, which allows to set DSCP/ECN values on a
net context. Those values are then encoded into outgoing packet
IPv4/IPv6 header.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
IEEE 802.15.4 short address support is incomplete in several places.
This change improves short address support without claiming to fix
it everywhere. Future iterations will have to continue where this change
leaves off.
The purpose of this change was to:
* use the short address returned by association responses,
* automatically bind IEEE 802.15.4 datagram sockets to the short
address if available,
* use the short address in outgoing packages where applicable,
* improve validation of association/disassociation frames,
* model association more closely to the spec by tying it to the
existence of a short address in the MAC PIB thereby removing
redundancy in the PIB (which makes race conditions less probable),
* keep both, the short and extended addresses, of the coordinator.
Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
Allocated, but undersized packets must not just be logged, but also
unreferenced before returning an error.
Signed-off-by: Reto Schneider <reto.schneider@husqvarnagroup.com>
In the function find_available_port() a port is randomly selected. Because
the random value is always >= 0x8000, it is redundant to check if it is
<= 1023 afterwards.
This commit removes the redundant check.
Signed-off-by: Christian Taedcke <christian.taedcke@lemonbeat.com>
* reduced cyclomatic complexity
* group validation by family to make the validation easier to understand
and extend
* change preprocessor markup where possible to allow for complete code
elimination when features (esp. IP) are disabled
* renamed net_context_get/set_ip_proto() to net_context_get_proto()
While the latter is formally part of the public API and might therefore
have to be deprecated rather than renamed, it is considered internal API
by the net developers, see
https://github.com/zephyrproject-rtos/zephyr/pull/48751#discussion_r942402612
Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
This change makes the packet socket and ieee802154 l2 drivers aware of
AF_PACKET sockets, see https://github.com/linux-wpan/wpan-tools/tree/master/examples
for examples which inspired this change.
Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
Rename the SocketCAN header from socket_can.h to socketcan.h to better
match the naming of the functionality.
Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
Network connection was not unregistered properly for packet socket,
which resulted in dangling connection when net_context was dereferenced
(i. e. when packet socket was closed). This could lead to a crash on a
consecutive incoming packet.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Logging v1 has been removed and log_strdup wrapper function is no
longer needed. Removing the function and its use in the tree.
Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Datagrams should either be fully sent or not sent at all if networking
buffers or network interface MTU does not allow that. So far the behavior
was to truncate outgoing packets, even for datagram sockets.
When there is not enough available payload buffer to fit all requested
data, fail if that happens for datagram socket.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Currently, the packet socket implementation in net_context required that
netowrk interface to transmit the packet to was set on every sendto()
call. This spoils the whole idea of binding a socket. Fix this, by
checking first if the net_conext is already bound to a particular
interface, and if so, do not throw an error in case sll_ifindex field is
not a valid interface.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Introduce set/get SO_SNDBUF option using the setsockopt
function. In addition, for TCP, check the sndbuf value
before queuing data.
Signed-off-by: Mohan Kumar Kumar <mohankm@fb.com>
Log an error when allocating a network packet for transmission fails.
This is a problem which can be solved by increasing
`CONFIG_NET_PKT_TX_COUNT`, but is currently hard to diagnose.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Introduce set/get SO_RCVBUF option using the setsockopt
function. In addition, use the rcvbuf value to set the
tcp recv window.
Signed-off-by: Mohan Kumar Kumar <mohankm@fb.com>
TCP2 is no longer needed as it is the unique implementation since the
legacy one has been removed.
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>