casaroli opened a new pull request, #21612:
URL: https://github.com/apache/nuttx/pull/21612

   ## Summary
   
   A UDP socket of `AF_INET6` in NuttX is dual-stack: it gets the IPv4 
datagrams of its port with an IPv4-mapped source address (`udp_ipv4_active()`, 
`udp_callback()`), and `udp_send()` sends a datagram to an IPv4-mapped address 
as IPv4. Five things around it are wrong:
   
   - `udp_ipv6_active()` does not check the domain. An unconnected IPv4 socket 
bound to `INADDR_ANY` matches every IPv6 datagram to its port 
(`conn->u.ipv6.laddr` of a `PF_INET` connection holds the IPv4 addresses, 
zeros), and gets it with an `AF_INET6` source address.
   - `udp_ipv4_active()` compares the IPv4 destination with 
`conn->u.ipv4.laddr` also for an `AF_INET6` socket, where that field is the 
first 4 bytes of its IPv6 address. A socket bound to `::1` has zeros there and 
takes every IPv4 datagram of its port.
   - `udp_connect()` maps `::` to `::1` (as `0.0.0.0` to `127.0.0.1`), but 
compares the start of the `struct sockaddr` with `::`, not `sin6_addr`, so the 
rule never applies: after `connect()` to `[::]:port`, `send()` fails with 
`ENETUNREACH`. `sendto()` to `[::]` or `0.0.0.0` also fails, because the write 
buffer (`CONFIG_NET_UDP_WRITE_BUFFERS`) and the ARP or neighbor check use the 
destination before `udp_connect()` maps it.
   - `setsockopt(IPV6_V6ONLY)` fails with `ENOSYS` for every value. A server 
that listens on both `[::]` and `0.0.0.0` sets it to 1, so that the IPv6 socket 
does not take the IPv4 traffic.
   - `udp_find_conn()` compares only sockets of the same domain. A dual-stack 
IPv6 socket and an IPv4 socket can bind the same port, and an IPv4 datagram 
goes to the socket that comes first in the list.
   
   This PR has six commits:
   
   - net/udp: Fix style errors in udp_sendto_unbuffered.c. A blank line and the 
indent of two lines that nxstyle asks for. The errors are older than this PR, 
and checkpatch checks the whole file.
   - net/udp: Do not give an IPv6 datagram to an IPv4 socket. 
`udp_ipv6_active()` skips `PF_INET` connections.
   - net/udp: Match an IPv4 datagram to an IPv6 socket by its IPv6 address. For 
an `AF_INET6` socket, the local address must be `[::]` or the IPv4-mapped 
destination, and in connected mode the remote address must be `[::]` or the 
IPv4-mapped source.
   - net/udp: Send a datagram for :: to ::1. `udp_connect()` compares 
`sin6_addr` and copies `g_ipv6_loopback`. The destination of a write buffer is 
mapped to the loopback address in the same way, and the ARP or neighbor check 
is skipped for the unspecified address (the loopback device needs none). The 
buffered and the unbuffered send paths now behave the same.
   - net/inet: Implement IPV6_V6ONLY. UDP: with the option set, the socket 
takes no IPv4 datagram, and `sendto()` or `connect()` to an IPv4-mapped address 
fails with `ENETUNREACH`. The default stays 0. TCP: a socket of `AF_INET6` is 
always IPv6-only (`tcp_conn_cmp()` matches only its own domain), so setting 1 
is accepted, setting 0 fails with `ENOPROTOOPT`, and `getsockopt()` reports 1. 
As on Linux (`net/ipv6/ipv6_sockglue.c`: `if (optlen < sizeof(int) || 
inet_sk(sk)->inet_num) goto e_inval;`), the value must be an `int` and the 
option can change only before `bind()`: after it, `setsockopt()` fails with 
`EINVAL`. One difference: Linux checks the local port, so it also refuses the 
option after an implicit bind by `connect()` or `sendto()`; this PR checks 
`bind()` only, because `ipv6_setsockopt()` serves TCP, UDP, ICMPv6 and raw 
sockets and only `_SF_BOUND` is common to them.
   - net/udp: Do not let a dual-stack socket share a port with an IPv4 one. 
`udp_find_conn()` also counts a socket of the other domain when the IPv6 side 
takes IPv4 traffic (no `IPV6_V6ONLY`, bound to `[::]` or to an IPv4-mapped 
address) and the IPv4 addresses overlap. `bind()` then fails with `EADDRINUSE` 
in both orders, as on Linux. With `IPV6_V6ONLY` on the IPv6 socket, or 
`SO_REUSEADDR` on both, both binds succeed. `udp_select_port()` uses the same 
check, so an ephemeral port also skips such ports. TCP needs no change: an IPv4 
and an IPv6 TCP listener can share a port, as on Linux with `IPV6_V6ONLY` set.
   
   ## Impact
   
   Changes that a program sees, in builds with both `CONFIG_NET_IPv4` and 
`CONFIG_NET_IPv6` (`IPV6_V6ONLY` also with IPv6 only, the `::` and `0.0.0.0` 
mapping in every build):
   
   - `setsockopt(IPPROTO_IPV6, IPV6_V6ONLY)` and `getsockopt()` work instead of 
`ENOSYS` and `ENOPROTOOPT`. `setsockopt()` after `bind()`, or with a value 
shorter than an `int`, fails with `EINVAL`.
   - `bind()` of a UDP socket fails with `EADDRINUSE` where it succeeded 
before: an IPv4 socket on a port that a dual-stack IPv6 socket holds, or a 
dual-stack IPv6 socket on a port that an IPv4 socket holds. A program that 
binds both needs `IPV6_V6ONLY` on the IPv6 socket, as on Linux.
   - An IPv4 UDP socket no longer gets IPv6 datagrams, and an IPv6 UDP socket 
bound to a real IPv6 address no longer gets IPv4 datagrams.
   - `connect()` to `[::]`, and `sendto()` to `[::]` or `0.0.0.0`, send to the 
loopback address instead of failing with `ENETUNREACH`.
   
   Size: +620 bytes of text on qemu-armv8a:netnsh (arm64, the build below). No 
documentation change: Documentation/ does not describe `IPV6_V6ONLY` or 
dual-stack sockets.
   
   ## Testing
   
   Test program `cmocka_net_udp`: the nuttx-apps PR of branch 
apps-udp-dualstack-test-up adds it as `testing/nettest/udp`, with 23 cases 
(IPV6_V6ONLY, delivery, the `::` and `0.0.0.0` mapping, bind). Rig: 
qemu-armv8a, HVF, 1 CPU; options `NET_IPv4`, `NET_IPv6`, `NET_LOOPBACK`, 
`NET_TCP`, `NET_UDP`, `NET_SOCKOPTS`, `NET_ARP_SEND` (from the defconfig), 
`DEBUG_ASSERTIONS` and others of a shared network rig; cmocka with 
`ALLOW_MIT_COMPONENTS`, `LIBC_REGEX`, `ARCH_SETJMP_H`. knsh: kernel build, 
`NET_UDP_WRITE_BUFFERS`, the program from `/system/bin`. nsh: flat build, no 
UDP write buffers, the program as a builtin.
   
   | Configuration | master c8c0661c997 | this PR |
   |---|---|---|
   | qemu-armv8a:knsh | 23 run, 4 passed, 19 failed | 23 run, 23 passed |
   | qemu-armv8a:nsh | 23 run, 4 passed, 19 failed | 23 run, 23 passed |
   
   The 4 cases that pass on master check behaviour that does not change: 
`test_udp_v6only_mapped` (an IPv6 socket on `[::]` gets an IPv4 datagram), 
`test_udp_v6only_tcp_ipv4` (an IPv6 TCP listener gets no IPv4 connection), 
`test_udp_bind_reuse` (`SO_REUSEADDR` on both sockets) and 
`test_udp_bind_v6addr` (an IPv6 socket on `::1` next to an IPv4 socket on the 
same port). `test_udp_family_sendto6` and `test_udp_family_sendto4` (`sendto()` 
to `[::]` and `0.0.0.0` without `connect()`) fail on master with `ENETUNREACH` 
in both configurations and pass with this PR.
   
   ostest with this PR (same options): status 0 on qemu-armv8a:knsh and on 
qemu-armv8a:nsh.
   
   Build with `EXTRAFLAGS=-Werror`, master and this PR: qemu-armv8a:netnsh with 
`NET_IPv4`, `NET_IPv6`, `NET_ICMPv6`, `NET_ICMPv6_SOCKET`, 
`NET_ICMPv6_NEIGHBOR`, `NET_ARP_SEND`, `NET_UDP`, `NET_TCP`, `NET_LOOPBACK`, 
`NET_SOCKOPTS` and `cmocka_net_udp`: OK.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   
   Not run: the protected build, sim, a real Ethernet device (the ARP and 
neighbor checks for a real destination do not change), other compilers than 
gcc, CMake after the last change (an earlier version of this PR with the same 
build files built with CMake).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to