casaroli opened a new pull request, #22143: URL: https://github.com/apache/nuttx/pull/22143
## Summary This PR fixes six defects of `recv()`, `readv()`, `connect()` and `getpeername()` on TCP and IPv6 sockets, one per commit. Each one gives a wrong result or stops the kernel from a user program. 1. **`net/tcp: Do not compute the receive window of a socket with no device.`** After `recv()` on a TCP socket that never connected returns `ENOTCONN`, `tcp_recvfrom_one()` calls `tcp_should_send_recvwindow()`. The connection has no device: when fewer than half of the I/O buffers are free, `tcp_rx_mss(NULL)` reads through a NULL pointer (data abort at address 0x40). Now there is no window update for a connection with no device. 2. **`net/tcp: Fix readv() and recvmsg() with more than one buffer.`** `psock_tcp_recvfrom()` stops at the first buffer that gives 0 or less. An empty first buffer gives end of file (or `EAGAIN`) while data waits, and data that fill the first buffer exactly make the receive for the second buffer wait for new data. Now an empty buffer is skipped, and after data the next buffers take only the data that is there, unless `MSG_WAITALL` is set, as `local_recvmsg()` does. 3. **`net/neighbor: Drop the IPv6 packet when no link address is known.`** With `NET_IPv6` and no `NET_ICMPv6`, a `sendto()` or `connect()` to an IPv6 address that is not in the Neighbor Table calls `DEBUGPANIC()`. Now the packet is dropped, as when no router is known. 4. **`net/tcp: Refuse an IPv4-mapped address in connect() of an IPv6 socket.`** A TCP socket of `AF_INET6` is IPv6-only, but `connect()` to `::ffff:a.b.c.d` sends IPv6 SYNs to the mapped address. Now it gives `ENETUNREACH`, as Linux does for an IPv6-only socket and `udp_connect()` already does. 5. **`net/tcp: Connect an IPv6 socket to ::1 for the address ::.`** `tcp_connect()` gives `net_ipv6addr_cmp()` the sockaddr pointer instead of `sin6_addr`, so `::` never maps to `::1`, and `connect([::]:port)` fails with `ECONNREFUSED`. Now it compares `sin6_addr`, as `udp_connect()` does since #21612. 6. **`net/inet: Return the remote port in getpeername() of an IPv6 socket.`** `ipv6_getpeername()` gives the local port with the remote address, for TCP and UDP. Now it gives `rport`, as `ipv4_getpeername()` does. ## Impact Common network code (`net/tcp`, `net/neighbor`, `net/inet`): the same in flat, protected and kernel builds and on every architecture. No API, ABI or Kconfig change. Each fix changes only the case that was wrong: - Commit 1: only connections with no device; a connected socket always has one. - Commit 2: only `readv()` and `recvmsg()` of TCP sockets with more than one buffer; one buffer and `MSG_WAITALL` do not change. - Commit 3: only IPv6 on Ethernet without ICMPv6; the packet goes nowhere as before, the system does not stop. - Commits 4 and 5: only `connect()` of an `AF_INET6` TCP socket to `::ffff:0:0/96` or to `::`; neither ever connected. - Commit 6: `getpeername()` of IPv6 sockets gives the peer port, as for IPv4. ## Testing qemu-armv8a:knsh (kernel build, 1 and 4 CPUs) and qemu-armv8a:nsh (flat build) under HVF (Apple M-series), `NET_IPv6`; for commits 1 and 2 96 I/O buffers, `IOB_THROTTLE` 8; for commits 3 and 4 virtio-net and no `NET_ICMPv6`. The cases of apache/nuttx-apps#APPS: | Commit | Test | master | this PR | |---|---|---|---| | 1 | `test_tcp_listen_recv`: `recv()` on a new socket with the free buffers at the throttle and at 38 of 96 | data abort, FAR 0x40, `tcp_rx_mss()` | `ENOTCONN` | | 2 | `test_tcp_recv_iov_empty`: `readv()` 0+16 with 4 bytes waiting | 0 | 4 | | 2 | `test_tcp_recv_iov_nowait`: `readv()` 4+16 with 4 bytes waiting, `SO_RCVTIMEO` 2 s | 4 after 2000 ms | 4 after 0 ms | | 3 | UDP `sendto()` to `2001:db8::1` (knsh, virtio-net) | panic at `neighbor_ethernet_out.c:178` | returns 1 | | 4 | `test_udp_v6only_tcp_mapped`: `connect()` to `::ffff:127.0.0.1` | panic at `neighbor_ethernet_out.c:178` (without commit 3: the SYN goes to eth0) | `ENETUNREACH` | | 5 | `connect()` to `[::]:port`, listener on `[::1]:port` | `ECONNREFUSED` | connected | | 6 | `test_tcp_peername_ipv6`, `test_udp_family_connect` | the own port | the peer port | All of the above again on this branch, on master 5814b404c95, knsh with 4 CPUs and flat nsh: every case fails, crashes or stops on master and passes with this PR. With this PR `cmocka_net_tcp` runs to its end after `test_tcp_listen_recv` (on master the kernel stops there). No other case changes. In flat nsh `test_tcp_reset_send` fails on master and with this PR alike: the `SIGPIPE` goes to another thread of the process. Commit 3 shows only on knsh: the flat nsh has no Ethernet device. Builds: qemu-armv8a:knsh and nsh, sabre-6quad:netknsh (ARMv7-A kernel build) and rv-virt:netnsh (RV32 flat), the last two also with IPv6, backlog, write buffers and procfs: 0 warnings in `net/`. Commit 3 has no case in nuttx-apps: it needs an Ethernet device and a build without ICMPv6, and the flat qemu-armv8a:nsh has no Ethernet device. `tools/checkpatch.sh -c -u -m -g` passes. Documentation: no change. -- 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]
