This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 4b72f4990e3ab900ca195b1c09f62f8fab01b53f Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 25 19:38:53 2026 +0100 net/tcp: Keep free connections that a SYN from the network cannot take. All TCP connections come from one pool. The backlog limits the connections in TCP_SYN_RCVD of one listener, but several listeners, or one listener with a backlog as large as the pool, can still take all of it. With the defaults the backlog (SOMAXCONN, 8) and the pool (NET_TCP_PREALLOC_CONNS, 8) are the same. Then a SYN flood stops the device from opening connections of its own: socket() and connect() fail until the SYN-ACK retries of the flood end. The new option NET_TCP_SYN_RESERVE (default 2) keeps connections from incoming SYNs. While this number of connections or fewer are free, tcp_alloc_accept() first frees the connections that only wait for the end of their close, as tcp_alloc_conn() does when the pool is empty. If that is not enough, a SYN frees the oldest connection in TCP_SYN_RCVD of its listener and takes its place, as for a full backlog; a listener without such a connection refuses the SYN. So a flood still cannot take the reserve, and the flooded listener still accepts a peer that completes its handshake quickly, also on another device. socket() and connect() do not check the reserve. It applies only when the pool has a limit: NET_TCP_ALLOC_CONNS is 0, or NET_TCP_MAX_CONNS is set. On a small pool the reserve also limits the connections that the listeners accept: with the defaults a SYN takes no connection when 6 of the 8 are in use. Set it to 0 to get the old behavior. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/Kconfig | 13 ++++++++++++ net/tcp/tcp_conn.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/net/tcp/Kconfig b/net/tcp/Kconfig index ed1c82adaf7..6ad55756650 100644 --- a/net/tcp/Kconfig +++ b/net/tcp/Kconfig @@ -140,6 +140,19 @@ config NET_TCP_MAX_CONNS This is useful in case the system is under very heavy load (or under attack), ensuring that the heap will not be exhausted. +config NET_TCP_SYN_RESERVE + int "TCP/IP connections kept from incoming SYNs" + default 2 + ---help--- + A SYN to a listener is refused while this number of TCP/IP + connections or fewer are free. Then SYNs from the network cannot + take the last connections, and socket() and connect() on this + device still work. This applies only when the number of + connections has a limit: NET_TCP_ALLOC_CONNS is 0, or + NET_TCP_MAX_CONNS is set. + + Set to 0 to disable. + config NET_TCP_NPOLLWAITERS int "Number of TCP poll waiters" default 2 diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c index 5d54b35aa9a..9d39a65e232 100644 --- a/net/tcp/tcp_conn.c +++ b/net/tcp/tcp_conn.c @@ -591,16 +591,67 @@ static bool tcp_is_syn_backlog(FAR struct tcp_conn_s *listener, #endif } +/**************************************************************************** + * Name: tcp_syn_reserved + * + * Description: + * Return true if only CONFIG_NET_TCP_SYN_RESERVE free connections or + * fewer are left. Free the connections that only wait for the end of + * their close first, as tcp_alloc_conn() does when the pool is empty. + * + * Input Parameters: + * dev - The device whose lock the caller holds. + * + * Returned Value: + * true if the free connections are kept from incoming SYNs. + * + ****************************************************************************/ + +#if CONFIG_NET_TCP_SYN_RESERVE > 0 && \ + (CONFIG_NET_TCP_ALLOC_CONNS == 0 || CONFIG_NET_TCP_MAX_CONNS > 0) +static bool tcp_syn_reserved(FAR struct net_driver_s *dev) +{ + struct mempoolinfo_s info; + size_t navail; + + for (; ; ) + { + /* Not mempool_navail(): it returns SIZE_MAX for a pool without + * maxalloc, also for the preallocated pool that cannot grow. + */ + + mempool_info(&g_tcp_connections, &info); +#if CONFIG_NET_TCP_ALLOC_CONNS == 0 + navail = info.ordblks; +#else + navail = info.maxalloc - info.aordblks; +#endif + if (navail > CONFIG_NET_TCP_SYN_RESERVE) + { + return false; + } + + if (!tcp_reclaim(dev)) + { + return true; + } + } +} +#else +# define tcp_syn_reserved(d) (false) +#endif + /**************************************************************************** * Name: tcp_syn_room * * Description: * Return true if the listener can take one more connection in * TCP_SYN_RCVD. Its connections in TCP_SYN_RCVD and the ones that wait - * for accept() together take at most the backlog of listen(). If there - * is no room, free the oldest connection in TCP_SYN_RCVD of the listener - * and take its place (RFC 4987, section 3.4): one on this device first, - * else one on another device. + * for accept() together take at most the backlog of listen(). The last + * CONFIG_NET_TCP_SYN_RESERVE free connections are kept for the sockets + * of this device. If there is no room, free the oldest connection in + * TCP_SYN_RCVD of the listener and take its place (RFC 4987, section + * 3.4): one on this device first, else one on another device. * * Input Parameters: * dev - The device whose lock the caller holds. @@ -619,7 +670,7 @@ static bool tcp_syn_room(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn; size_t nsynrcvd = 0; size_t nfree = SIZE_MAX; - bool room = true; + bool room = !tcp_syn_reserved(dev); tcp_conn_list_lock(); #ifdef CONFIG_NET_TCPBACKLOG
