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

   ## Summary
   
   This PR fixes `bind()` with `SO_REUSEADDR` and `listen()` of TCP sockets. It 
has three commits:
   
   - net/tcp: Let SO_REUSEADDR bind a port that has connections left. `bind()` 
refused a TCP port while any connection on it was not CLOSED, whatever 
`SO_REUSEADDR` said. A server that closes its connections leaves them in 
TIME_WAIT for `TCP_TIME_WAIT_TIMEOUT` (120 s by default), so a restarted server 
could not bind its port for two minutes, although it set `SO_REUSEADDR` for 
that. `bind()` now follows the Linux rule: if the new socket and a connection 
on the port both have `SO_REUSEADDR`, they do not conflict; a listener on the 
port always conflicts. The new static `tcp_bindport()` does this check for 
`bind()`; `tcp_selectport()` (also used by `connect()`, `listen()` and NAT, 
which have no socket options) does not change. An accepted connection now gets 
`SO_REUSEADDR` from its listener, as on Linux, so the connections of a server 
that set it do not block its restart. Only this bit is copied: the `SO_LINGER` 
bit without `s_linger` would change `close()` of the accepted socket.
   - net/tcp: Let listen() again change the backlog of a listening socket. A 
second `listen()` on a listening socket first replaced its backlog 
(`tcp_backlogcreate()` freed the old queue and the connections in it), then 
failed with `EADDRINUSE`, because `tcp_listen()` found the socket itself in the 
listener table. Linux returns 0 and sets the new backlog. `tcp_listen()` now 
returns OK when the listener it finds is the socket itself, and 
`tcp_backlogcreate()` moves the waiting connections to the new backlog, as many 
as it holds. `tcp_islistener()` has no caller left and is removed.
   - net/tcp: Fail listen() on a connected socket with EINVAL. `listen()` on a 
connected socket, or on one that connects, succeeded and put the connection in 
the listener table; on an accepted socket it failed with `EADDRINUSE`. POSIX 
and Linux return `EINVAL`. `inet_listen()` now checks the TCP state first (a 
socket that is not connected is in `TCP_ALLOCATED`).
   
   The rules were checked on Linux 7.2 (Fedora 44) with the test program below 
and with a probe for the cases it does not cover:
   
   | Case | Linux | master | this PR |
   |---|---|---|---|
   | bind, port in TIME_WAIT, both `SO_REUSEADDR` | 0 | `EADDRINUSE` | 0 |
   | bind, port in TIME_WAIT, new socket without `SO_REUSEADDR` | `EADDRINUSE` 
| `EADDRINUSE` | `EADDRINUSE` |
   | bind, port of an established connection, both `SO_REUSEADDR` | 0 | 
`EADDRINUSE` | 0 |
   | bind, port of an established connection, old listener without 
`SO_REUSEADDR` | `EADDRINUSE` | `EADDRINUSE` | `EADDRINUSE` |
   | bind, port of a listener, with or without `SO_REUSEADDR` | `EADDRINUSE` | 
`EADDRINUSE` | `EADDRINUSE` |
   | `listen()` again on a listening socket | 0, new backlog | `EADDRINUSE`, 
queue lost | 0, new backlog, queue kept |
   | `listen()` on a connected or accepted socket | `EINVAL` | 0 (client), 
`EADDRINUSE` (accepted) | `EINVAL` |
   
   Differences from Linux that stay:
   
   - A socket that is bound but does not listen or connect does not hold its 
port: two such sockets can bind the same port without `SO_REUSEADDR` (Linux: 
`EADDRINUSE`), and the second `listen()` fails with `EADDRINUSE`. The listener 
table and the list of active connections do not contain bound-only sockets. Not 
changed by this PR.
   - When `listen()` again gives a smaller backlog than the number of 
connections that wait, the connections that do not fit are freed. Linux keeps 
them and accepts no new ones until the queue is below the new size. Before this 
PR all waiting connections were freed.
   - `listen(fd, 0)` gives no backlog in NuttX (Linux queues one connection). 
Not changed by this PR.
   
   ## Impact
   
   Changes that a program sees, in every build with `CONFIG_NET_TCP`:
   
   - `bind()` of a TCP socket with `SO_REUSEADDR` succeeds on a port that has 
connections in TIME_WAIT or other states, when they have `SO_REUSEADDR` too. It 
still fails with `EADDRINUSE` when a socket listens on the port. Only with 
`CONFIG_NET_SOCKOPTS`.
   - `getsockopt(SO_REUSEADDR)` on an accepted socket returns the value of the 
listener (it returned 0). Only with `CONFIG_NET_SOCKOPTS`.
   - `listen()` on a listening socket returns 0 instead of `EADDRINUSE`, keeps 
the connections that wait and applies the new backlog.
   - `listen()` on a connected socket, on an accepted socket, or during 
`connect()` fails with `EINVAL`. Before, it returned 0 (or `EADDRINUSE` for an 
accepted socket) and put the connection in the listener table.
   
   Size (text, master vs this PR): qemu-armv8a:netnsh with `NET_SOCKOPTS`, 
`NET_IPv6`, `NET_NAT` 557570 -> 557830 (+260); lm3s6965-ek:qemu-protected 
(`NET_SOCKOPTS`) 127652 -> 127772 (+120); lm3s6965-ek:nsh (no `NET_SOCKOPTS`) 
145444 -> 145560 (+116).
   
   No documentation change: Documentation/ does not describe `bind()`, 
`SO_REUSEADDR` or the `listen()` backlog; 
Documentation/os/networking/tcp_state_machine.rst (listeners in 
`tcp_listenports[]`, `TCP_ALLOCATED` before connect or accept) stays true.
   
   ## Testing
   
   Test program `cmocka_net_tcp`: apache/nuttx-apps#NNNN adds 5 cases to 
`testing/nettest/tcp`. Rig: qemu-armv8a, HVF, 1 CPU; options `NET_IPv4`, 
`NET_IPv6`, `NET_LOOPBACK`, `NET_TCP`, `NET_TCPBACKLOG`, `NET_SOCKOPTS`, 
`NET_UDP`, `DEBUG_ASSERTIONS`; cmocka with `ALLOW_MIT_COMPONENTS`, 
`LIBC_REGEX`, `ARCH_SETJMP_H`. knsh: kernel build, the program from 
`/system/bin`. nsh: flat build, the program as a builtin.
   
   | Configuration | master 03aea786390 | this PR |
   |---|---|---|
   | qemu-armv8a:knsh | 7 run, 3 passed, 4 failed | 7 run, 7 passed |
   | qemu-armv8a:nsh | 7 run, 3 passed, 4 failed | 7 run, 7 passed |
   
   The 3 cases that pass on master are the existing `test_tcp_connect_ipv4` and 
`test_tcp_connect_ipv6`, and `test_tcp_bind_reuseaddr_listener` (a listener 
still holds its port). The 4 that fail on master: 
`test_tcp_bind_reuseaddr_timewait` and `test_tcp_bind_reuseaddr_connected` 
(`bind()` with `SO_REUSEADDR`: `EADDRINUSE`), `test_tcp_listen_again` 
(`listen()` again: `EADDRINUSE`), `test_tcp_listen_connected` (`listen()` on a 
connected socket: 0).
   
   On Linux (Fedora 44, kernel 7.2, gcc, cmocka 1.1.5, the same sources): the 5 
new cases pass. The existing `test_tcp_connect_ipv4` fails there because its 
loopback server calls `select()` with `tv_sec = -1` (`EINVAL` on Linux).
   
   ostest with this PR (same options): status 0 on qemu-armv8a:knsh and on 
qemu-armv8a:nsh (also on master).
   
   Build with `EXTRAFLAGS=-Werror`, master and this PR, 0 warnings: 
qemu-armv8a:netnsh with `NET_SOCKOPTS`, `NET_IPv6`, `NET_LOOPBACK`, 
`NET_IPFORWARD` and `NET_NAT` (gcc 14.2, arm64, the NAT and dual-stack paths of 
`tcp_bindport()`); lm3s6965-ek:qemu-protected (gcc 15.3 arm-none-eabi, 32-bit, 
protected build, `NET_SOCKOPTS`); lm3s6965-ek:nsh (32-bit, flat, without 
`NET_SOCKOPTS`). CMake (Ninja): qemu-armv8a:nsh with `NET_IPv4`, `NET_TCP`, 
`NET_SOCKOPTS` and `cmocka_net_tcp`: OK, no compiler warning.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   
   Not run: `listen()` during a `connect()` that is in progress 
(`TCP_SYN_SENT`, the same check), other architectures at run time, sim, a real 
Ethernet device, NAT at run time (built only), other compilers than gcc.
   


-- 
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