Commit graph

6489 commits

Author SHA1 Message Date
Tjerand Bjornsen
938e0e4966 net: lwm2m: Add default firmware update port
If a firmware update is fetched from a server, and no port number is set
in the URI (e.g. coap://example.com/fw_update), the client will try
to connect on the port specified by CONFIG_LWM2M_PEER_PORT.
If a port different from the peer port is to be used for firmware
update, this has to be set explicitly in the URI:
coap://example.com:5683/fw_update.

This fix adds CONFIG_LWM2M_FIRMWARE_PORT, which will be used when
fetching a firmware update without specifying the port number in the
URI.

Signed-off-by: Tjerand Bjornsen <tjerand.bjornsen@nordicsemi.no>
2021-10-14 16:34:48 -04:00
Andrei Emeltchenko
3f9d980f7c net: tcp: Remove unneeded declaration
Remove unneeded declaration and change include logic.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2021-10-14 16:34:23 -04:00
Andrei Emeltchenko
636c7b1677 net: tcp: Remove redundant TCP option definitions
Use the same TCP option definitions.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2021-10-14 16:34:23 -04:00
Jukka Rissanen
fc5d46048d net: tcp2: Send our MSS to peer
Send our MSS to peer when sending SYN or SYN-ACK.

Fixes #30367

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2021-10-14 16:34:23 -04:00
Krzysztof Chruscinski
eb3375f47c shell: Add __printf_like to shell_fprintf
Add __printf_like modifier to validate strings used by shell.
Fixing warnings triggered by this change.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-10-14 16:32:19 -04:00
Robert Lubos
875c75c4f9 net: lwm2m: Replace float32_value_t with double
Replace the custom float32_value_t LwM2M type with native double, to
facilitate LwM2M API and improve floating point precission.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-10-13 10:13:32 -04:00
Andrei Emeltchenko
513d691dba net: tcp: Use BIT() macros
Use BIT() macro to make code more readable and consistent.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2021-10-13 06:23:59 -04:00
Andrei Emeltchenko
9d2899657a net: tcp: Fix macro tcp_slist()
Fix error in macro parameter.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2021-10-13 06:23:59 -04:00
Andrei Emeltchenko
2faae3c027 net: tcp: Fix macro conn_send_data_dump()
Fixes error in macro parameter.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2021-10-13 06:23:59 -04:00
Robert Lubos
4e9b9a7fdf drivers: net: loopback: Register loopback IP address to the interface
Regsiter loopback IPv4/IPv6 to the loopback interface during
interface initialization.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-10-13 11:17:51 +02:00
Robert Lubos
6f46124d6e net: if: Skip DAD when adding loopback IPv6 address
There's no point in perfoming DAD procedure for loopback address,
hence skip it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-10-13 11:17:51 +02:00
Nicolas Pitre
cf49699b0d net: sockets: socketpair: fix locking
The irq_lock() usage here is incompatible with SMP systems, and one's
first reaction might be to convert it to a spinlock.

But are those irq_lock() instances really necessary?

Commit 6161ea2542 ("net: socket: socketpair: mitigate possible race
condition") doesn't say much:

> There was a possible race condition between sock_is_nonblock()
> and k_sem_take() in spair_read() and spair_write() that was
> mitigated.

A possible race without the irq_lock would be:

thread A                thread B
|                       |
+ spair_write():        |
+   is_nonblock = sock_is_nonblock(spair); [false]
*   [preemption here]   |
|                       + spair_ioctl():
|                       +   res = k_sem_take(&spair->sem, K_FOREVER);
|                       +   [...]
|                       +   spair->flags |= SPAIR_FLAG_NONBLOCK;
|                       *   [preemption here]
+   res = k_sem_take(&spair->sem, K_NO_WAIT); [-1]
+   if (res < 0) {      |
+     if (is_nonblock) { [skipped] }
*     res = k_sem_take(&spair->sem, K_FOREVER); [blocks here]
|                       +   [...]

But the version with irq_lock() isn't much better:

thread A                thread B
|                       |
|                       + spair_ioctl():
|                       +   res = k_sem_take(&spair->sem, K_FOREVER);
|                       +   [...]
|                       *   [preemption here]
+ spair_write():        |
+   irq_lock();         |
+   is_nonblock = sock_is_nonblock(spair); [false]
+   res = k_sem_take(&spair->sem, K_NO_WAIT); [-1]
+   irq_unlock();       |
*   [preemption here]   |
|                       +   spair->flags |= SPAIR_FLAG_NONBLOCK;
|                       +   [...]
|                       +   k_sem_give(&spair->sem);
|                       + spair_read():
|                       +   res = k_sem_take(&spair->sem, K_NO_WAIT);
|                       *   [preemption here]
+   if (res < 0) {      |
+     if (is_nonblock) { [skipped] }
*     res = k_sem_take(&spair->sem, K_FOREVER); [blocks here]

In both cases the last k_sem_take(K_FOREVER) will block despite
SPAIR_FLAG_NONBLOCK being set at that moment. Other race scenarios
exist too, and on SMP they are even more likely.

The only guarantee provided by the irq_lock() is to make sure that
whenever the semaphore is acquired then the is_nonblock value is always
current. A better way to achieve that and be SMP compatible is to simply
move the initial sock_is_nonblock() *after* the k_sem_take() and remove
those irq_locks().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-10-11 21:00:41 -04:00
Christopher Friedt
65b1c207f2 net: mdns + dns_sd: fix regression that breaks ptr queries
While adding support for service type enumeration, a regression was
introduced which prevented mDNS ptr query responses.

1. There was an off-by-one error with label size checking
2. Valid queries were failing to match in `dns_rec_match()` due to
   not checking for either NULL or 0 "wildcard" port

Fixes #39284

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2021-10-11 08:50:45 -04:00
Tjerand Bjornsen
872f560981 net: lwm2m: Fix device obj missing error code ri
From the OMA LwM2M Object and Resource Registry: "When the single Device
Object Instance is initiated, there is only one error code Resource
Instance whose value is equal to 0 that means no error."

This fix creates that initial error code resource instance, and makes
sure that it doesn't get deleted by the Reset Error Code resource.

Signed-off-by: Tjerand Bjornsen <tjerand.bjornsen@nordicsemi.no>
2021-10-07 15:44:46 -04:00
David Brown
fc3f4a627e net: sockets: tls: Use better error code
Mbed TLS 3.0 removes the definition for MBED_ERR_SSL_PEER_VERIFY_FAILED,
since non of its code ever returns that value.  Since there isn't really
a perfect response, instead return a somewhat generic response
indicating this was unexpected.

Signed-off-by: David Brown <david.brown@linaro.org>
2021-10-07 14:02:40 -05:00
Flavio Ceolin
b78b7d5264 net: websocket: s/mbedtls_sha1_ret/mbedtls_sha1/
sha1 function signature changed in mbedTLS 3.0.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-10-07 14:02:40 -05:00
Flavio Ceolin
1cdc5034e1 net: sockets_tls: Fix mbedTLS usage
mbedtls_pk_parse_key signature has changed and requires an entropy
source.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-10-07 14:02:40 -05:00
Flavio Ceolin
b30958e85f net: tcp2: Fix mbedTLS usage
s/mbedtls_md5_ret/mbedtls_md5/

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-10-07 14:02:40 -05:00
Flavio Ceolin
4edcf48e05 sockets: tls: Enable access to mbedtls private fields
Several fields of structures in mbedTLS 3.0 are now private. To access
them directly is necessary to define MBEDTLS_ALLOW_PRIVATE_ACCESS.

That is a temporary fix, the proper solution is not access directly
but using proper API.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-10-07 14:02:40 -05:00
Robert Melchers
59fcf8c031 net: ethernet: ARP addresses being filled with mcast addresses
Fixes #38994, ARP messages were being sent to IPvXmcast MAC addresses
rather than the expected source MAC address or the broadcast address.

Signed-off-by: Robert Melchers <rmelch@hotmail.com>
2021-10-07 11:23:20 +02:00
Jani Hirsimäki
e913fda436 net: l2: wifi_shell: mac address in scanning results
Showing mac address in scanning results.

Signed-off-by: Jani Hirsimäki <jani.hirsimaki@nordicsemi.no>
2021-10-07 10:55:37 +02:00
Mateusz Karlic
3844b79e96 net: sockets: sockets_can: Allow parallel receive/send
Implements mechanism similar to the one available in net/lib/sockets.c
(since the merge of #27054) in sockets_can to enable parallel rx/tx.

Fixes #38698

Signed-off-by: Mateusz Karlic <mkarlic@internships.antmicro.com>
2021-10-06 22:22:43 -04:00
Ryan Erickson
f7208bbb62 net: lwm2m: Add option to force close the connection
Add an option to force close the LwM2M connection
instead of always trying to deregister.
If on a cellular connection and the connection is dropped,
deregistering will never complete and take a long time
before retries fail. This option allows the app to close the
socket and quickly re-establish the connection when the
network connection is available again.

Signed-off-by: Ryan Erickson <ryan.erickson@lairdconnect.com>
2021-10-05 19:23:59 -04:00
Veijo Pesonen
b3bc827366 net: lwm2m: Decouple CoAP message- and block size
If blockwise transfer is not in use it must be possible to send bigger
CoAP messages than what is the block size used with blockwise transfers.

To compensate for the increased memory usage number of in-flight packets
allowed could be decreased.

Signed-off-by: Veijo Pesonen <veijo.pesonen@nordicsemi.no>
2021-10-05 19:22:54 -04:00
Veijo Pesonen
32b4ec44fe net: lwm2m: Fix Kconfig help about allowed...
...range for LWM2M_COAP_BLOCK_SIZE.

The range has been set to start from 64 bytes and now the help text has
been brought up to date.

Signed-off-by: Veijo Pesonen <veijo.pesonen@nordicsemi.no>
2021-10-05 19:22:54 -04:00
Christopher Friedt
fa26dc0988 net: dns_sd, mdns: support service type enumeration
Support DNS-SD Service Type Enumeration in the dns_sd library
and mdns_responder sample application.

For more information, please see Section 9, "Service Type
Enumeration" in RFC 6763.

https://datatracker.ietf.org/doc/html/rfc6763

Fixes #38673

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2021-10-01 20:11:50 -04:00
Eduardo Montoya
b945d7aee4 openthread: align platform code to ARM PSA changes
Align `otPlatRadioSetMacKey` with latest upstream changes.

Signed-off-by: Eduardo Montoya <eduardo.montoya@nordicsemi.no>
2021-10-01 11:37:38 +02:00
Robert Lubos
45e07dbbb3 net: sockets: tls: Ignore empty iovec entries in sendmsg
According to `sendmsg()` man pages, the `struct msghdr` can contain
empty records (iov_len equal to 0). Ignore them in TLS `sendmsg()`
implementation to avoid unnecessary calls to mbed TLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-29 11:08:40 +02:00
Robert Lubos
f0ec61ef70 net: mqtt: Handle incomplete zsock_sendmsg write
In case zsock_sendmsg did not send all of the data requested, update the
`struct msghdr` content and retry.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-29 11:08:40 +02:00
Robert Lubos
6e04a0f59d net: context: Do not overflow net_pkt when using msghdr
If data for `context_sendto()` was provided in a form of
`struct msghdr` (for instance via `sendmsg()`), it was not verified that
the provided data would actually fit into allocated net_pkt. In result,
and error could be returned in case the provided data was larger than
net_pkt allows.

Fix this, by verifying the remaining buffer length when iterating over
`struct msghdr`. Once the buffer is filled up, break the loop. In
result, functions like `sendmsg()` will return the actual length of data
sent instead of an error.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-29 11:08:40 +02:00
Robert Lubos
0dbdcc770d net: sockets: Add socket processing priority
When creating a socket, all of the registered socket implementation are
processed in a sequence, allowing to find appropriate socket
implementation for specified family/type/protocol. So far however,
the order of processing was not clearly defined, leaving ambiguity if
multiple implmentations supported the same set of parameters.

Fix this, by registering socket priority along with implementation. This
makes the processing order of particular socket implementations
explicit, giving more flexibility to the user, for example when it's
neeed to prioritze one implementation over another if they support the
same set of parameters.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-28 20:11:26 -04:00
Robert Lubos
8b851af02a net: sockets: Register native TCP/UDP like any other socket type
This will allow to assing priority to a native TCP/UDP socket
implementation as well.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-28 20:11:26 -04:00
Markus Fuchs
fb082a6923 net: dns: Make mdns and llmnr responders join their multicast groups
As with IPv6, the mdns and llmnr responders should join their multicast
groups for IPv4 instead of just adding the multicast address to the
interface.

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2021-09-28 20:04:49 -04:00
Markus Fuchs
7926657b27 net: if: Add IPv4 support to multicast monitor
Make multicast group join/leave monitor support both IPv6 and IPv4
addresses.

Fixes #26585

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2021-09-28 20:04:49 -04:00
Alex Sergeev
e7778b8584 net: ptp: extract PTP interfaces outside gPTP subsys under NET_L2_PTP
As per #38352, we would like to start building out PTP (IEEE 1588)
support for superset of gPTP functionality in Zephyr. This is the first
step to abstract away some key interfaces from NET_GPTP umbrella to
NET_L2_PTP.

Signed-off-by: Alex Sergeev <asergeev@carbonrobotics.com>
2021-09-28 20:01:16 -04:00
Nicolas Pitre
fd3dd0c2d6 net_buf_slist_{put|get}: make SMP safe
Replace irq_lock() with a spin lock, and rework the `get` version
to be entirely SMP safe.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-09-28 11:37:56 +02:00
Nicolas Pitre
8cd7f1c98a net_buf_alloc_len: make SMP safe
Replace irq_lock() with a spin lock.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-09-28 11:37:50 +02:00
Florian Vaussard
52638a000f net: ipv6_fragment: send ICMPv6 Time Exceeded upon reassembly timeout
RFC 2460 Sec. 5 requires that a ICMPv6 Time Exceeded message is sent
upon reassembly timeout, if we received the first fragment (i.e. the one
with a Fragment Offset of zero).

Implement this requirement.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
c66b4288e0 net: ipv6_fragment: fix shift_packets() algorithm
The purpose of shift_packets() is to make room to insert one fragment in
the list. This is not what it does currently, potentially leading to
-ENOMEM even if there is enough free room.

To see the current behaviour, let's assume that we receive 3 fragments
in reverse order:
- Frag3(offset = 0x40, M=0)
- Frag2(offset = 0x20, M=1)
- Frag1(offset = 0x00, M=1)

After receiving Frag3 and Frag2, pkt[] will look like:

  .-------.-------.-------.
  | Frag2 | Frag3 | NULL  |
  | 0x20  | 0x40  |       |
  '-------'-------'-------'
    pkt[0]  pkt[1]  pkt[2]

When receiving Frag1, shift_packets(pos = 0) is called to make some room
at position 0. It will iterate up to i = 2 where there is a free
element. The current algorithm will try to shift pkt[0] to pkt[2], which
is indeed impossible but also unnecessary. It is only required to shift
pkt[0] and pkt[1] by one element in order to free pkt[0] to insert
Frag1.

Update the algorithm in order to shift the memory only by one element.
As a result, the ENOMEM test is only simpler: as long as we encounter
one free element, we are guaranteed that we can shift by one element.
Also assign a NULL value to the newly freed element since memmove() only
copy bytes.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
a9917d9bd4 net: ipv6_fragment: improve reassembly condition
Currently net_ipv6_handle_fragment_hdr() performs 2 distinct tests: it
checks the M-bit of the most recent fragment to decide if we can proceed
with the reassembly. Then it performs some sanity checks which can lead
to dropping the whole packet if not successful.

The test on the M-bit assumes that fragments arrive in order. But this
will fail if packets arrive out-of-order, since the last fragment can
arrive before some other fragments. In that case, we proceed with the
reassembly but it will fail because not all the fragments have been
received.

We need a more complete check before proceeding with the reassembly:
- We received the first fragment (offset = 0)
- All intermediate fragments are contiguous
- The More bit of the last fragment is 0

Since these conditions can also detect a malformed fragmented packet, we
can replace the existing sanity check that is performed before
reassembly. As a bonus, we can now detect and rejected overlapping
fragments, since this can have some security issues (see RFC 5722).

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
4fe978661a net: ipv6_fragment: store M-bit in addition to the offset
Currently we only store the fragment offset. But in some cases it might
be necessary to also inspect the M-bit (More Fragment) of all received
fragments.

Modify the semantics of the field to store all the flags, rename the
setter to account for this change, and add a getter for the M-bit.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
0fcf87540e net: ipv6_fragment: remove special handling of 1st fragment
The special handling of the 1st fragment in unnecessary, since it will
be correctly handled even without it. Moreover it causes some corner
cases, like a single packet with a fragment header (M=0), to be
incorrectly handled since the reassembly code is skipped.

Remove the special handling of the 1st fragment to fix these problems.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
108ccfe60c net: ipv6_fragment: check the length also for the first fragment
Currently the requirement of the length being a multiple of 8 is not
tested for the first fragment, since the first fragment takes a
different path due to the goto.

Move the test earlier in the process, so that it is performed on all
fragments, including the first one.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
e6a1643add net: ipv6_fragment: fix NULL pointer dereference issues
If we have less fragments than what can be stored in the reassembly
array, some loops will blindly dereference NULL pointers.

Add checks for NULL pointers when necessary and exit the loop.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
5252468c06 net: ip: replace hardcoded NET_IPV6_FRAGMENTS_MAX_PKT with a Kconfig
Currently the stack is limited to a maximum of 2 incoming fragments per
packet. While this can be enough in most cases, it might not be enough
in other cases.

Make this value configurable at build time.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
0b8a884931 net: ipv6: fix the logics of prev_hdr_offset
Currently prev_hdr_offset always equals 6, which is the offset of
the nexthdr field in the IPv6 header. This value is used to overwrite it
when removing an IPv6 Fragment header, so it will work as long as there
is no other Extension header between the IPv6 header and the Fragment
header.

However this does not work in the other cases: the nexthdr field of the
IPv6 header will be overwritten instead of the nexthdr field of the last
Extension header before the Fragment, leading to unwanted results.

Update prev_hdr_offset so that it always point to the nexthdr field of
the previous header, either the IPv6 header or an Extension header.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
4bacedb4f3 net: ipv6: reject invalid nexthdr early
The current validation code waits to process the header before rejecting
it, while some checks can be already enforced when reading the nexthdr
field of the previous header.

The main problem is a wrong pointer field in the resulting ICMPv6 error
message: the pointer should have the offset of the invalid nexthdr
field, while currently it will the offset the invalid header.

To solve that problem, reorganize the loop in two parts: the first
switch validates nexthdr, while the second switch processes the current
header. This allows to reject invalid nexthdr earlier.

The check for duplicated headers is also generalized, so that we can
catch other kind of headers (like the Fragment header).

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
cd1ab54525 net: ipv6: check for NET_IPV6_NEXTHDR_NONE earlier
By definition, NET_IPV6_NEXTHDR_NONE is void. So we must stop processing
before trying to read any data, since we will start reading values that
are outside the Extension Header (likely the payload, if any).

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
e9dff0fd1f net: ipv6: correctly set the offset to the unknown option
When an unknown option is encountered, an ICMPv6 error message must be
sent in some cases. The message contains a pointer field, which must be
the offset to the unknown option. Currently the offset is computed from
the beginning of the option list, while it should be computed with
respect to the beginning of the IPv6 header.

Record the offset when reading the option type and pass it later to
ipv6_drop_on_unknown_option() to correctly set the pointer field. Also
rename the argument in ipv6_drop_on_unknown_option() to make the
purpose more clear.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00
Florian Vaussard
1f402cc5c8 net: ipv6: correctly handle PADN option
Currently PADN data are not skipped, which results in the stack to think
that the next header starts in the middle of the padding. We have to
skip the bytes before going on.

Also clarify the PAD1 does not have any length field.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2021-09-23 13:21:09 -04:00