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

   ## Summary
   
   A SYN to a listener makes a connection in `TCP_SYN_RCVD`. Nothing limits 
these connections: `tcp_input()` only checks that the backlog has a free 
container, and the container is taken only when the handshake completes. Each 
one holds a connection of the shared pool until its SYN-ACK retries end, more 
than a minute with the defaults. So a peer that sends SYNs and never completes 
the handshake fills the pool (8 connections by default) with a few SYNs. Then 
the listeners refuse legitimate connections, and `socket()` on the device fails 
too.
   
   This PR limits the connections in `TCP_SYN_RCVD`:
   
   - net/tcp: The connections in `TCP_SYN_RCVD` of a listener and the 
connections that wait for `accept()` together take at most the backlog of 
`listen()`, as in Linux. A SYN to a listener whose backlog is full of 
connections in `TCP_SYN_RCVD` frees the oldest of them and takes its place (RFC 
4987, section 3.4). A peer that completes its handshake quickly still gets in.
   - net/tcp: The new option `CONFIG_NET_TCP_SYN_RESERVE` (default 2) keeps the 
last free connections from incoming SYNs, for `socket()` and `connect()`. With 
the defaults the backlog (8) and the pool (8) have the same size, so the 
backlog limit alone does not protect the pool. At the reserve, a SYN first 
frees the connections that only wait for the end of their close 
(`tcp_reclaim()`), then the oldest connection in `TCP_SYN_RCVD` of its 
listener; else it is refused.
   - Documentation: The TCP state machine page describes the limits.
   
   No field is added to `struct tcp_conn_s`. The connections of a listener are 
found with the same match that `tcp_remove_syn_backlog()` uses (new helper 
`tcp_is_syn_backlog()`, used by both).
   
   ## Impact
   
   - The `listen()` backlog now also limits the connections in `TCP_SYN_RCVD`, 
as in Linux. No API change.
   - With a bounded pool (`NET_TCP_ALLOC_CONNS` 0, or `NET_TCP_MAX_CONNS` set), 
incoming SYNs cannot take the last `CONFIG_NET_TCP_SYN_RESERVE` connections. 
With the defaults a SYN takes no connection when 6 of the 8 are in use, unless 
it replaces an older connection in `TCP_SYN_RCVD` of its listener. Set the 
option to 0 to get the old behavior. An unbounded pool (`NET_TCP_ALLOC_CONNS` > 
0 without `NET_TCP_MAX_CONNS`) does not change.
   - Without `CONFIG_NET_TCPBACKLOG` only the reserve applies.
   - Common network code: the same in flat, protected and kernel builds and on 
every architecture.
   - Size (text): +300 bytes on qemu-armv8a:nsh with TCP, +288 bytes on 
qemu-armv8a:knsh with TCP. No change without `CONFIG_NET_TCP`.
   - Cost per SYN to a listener: one walk of the list of active connections, as 
`tcp_remove_syn_backlog()` does.
   
   ## Testing
   
   Host: Fedora x86_64. Target: qemu-armv8a:nsh (flat build) under QEMU TCG, 
cortex-a53, 1 CPU, default pool (`NET_TCP_PREALLOC_CONNS` 8), `NET_TCPBACKLOG`, 
a TUN device. Master is 4f8706c4b35, the base of this PR.
   
   A test program (not part of this PR) listens on the TUN address with 
`listen(fd, 8)` and accepts each connection. It writes SYNs from spoofed 
sources to the TUN device and never answers them. Each second it makes one real 
handshake from another address. Each two seconds the target opens one outbound 
`socket()` + `connect()`, closed one second later. Phases: 6 s before, the 
flood, 60 s wait, 6 s after.
   
   | Flood | Boots | master: handshakes | master: outbound | this PR: 
handshakes | this PR: outbound |
   |---|---|---|---|---|---|
   | 20 SYN/s for 60 s | 3 | 1 of 16 | 1 of 30 (`socket()` failed 29 times) | 
60 of 60 | 30 of 30 |
   | 200 SYN/s for 30 s | 2 | 1 of 9 | 1 of 15 (`socket()` failed 14 times) | 
30 of 30 | 15 of 15 |
   
   The runs of each row gave the same counts. Before and after the flood all 
handshakes and outbound connections work on both. On master the flood holds 7 
connections in `TCP_SYN_RCVD` and the target answers 42 of the 1200 SYNs; with 
this PR it answers every SYN, and the oldest half-open connection gives its 
place.
   
   - `cmocka_net_tcp` and `cmocka_net_tun` (nuttx-apps `testing/nettest`), same 
image options. Pool 16: the same 34 of 36 TCP cases pass on master and with 
this PR (`test_tcp_close_timewait` and `test_tcp_reset_send` fail on both). 
Pool 8: master passes 32 of 36 (also `test_tcp_backlog_relisten_race` and 
`test_tcp_backlog_close_race` fail); this PR passes 34 of 36. TUN: 1 of 1 on 
both.
   - Builds with `-Werror` (CI image, aarch64-none-elf-gcc 13.2.1, 
riscv-none-elf-gcc 14.3.0): qemu-armv8a:netnsh and qemu-armv8a:knsh (with TCP), 
`NET_TCPBACKLOG` on and off; rv-virt:netnsh64 with IPv4 + IPv6, and with 
`NET_TCPBACKLOG` off and an unbounded pool; rv-virt:knetnsh64 with a bounded 
dynamic pool (`NET_TCP_ALLOC_CONNS` 2, `NET_TCP_MAX_CONNS` 16), and with 
`NET_TCPBACKLOG` off and `NET_TCP_SYN_RESERVE` 0.
   - `tools/checkpatch.sh -c -u -m -g`: pass.
   


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