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 4d0e5541a9a0e1390b880971cef04a153c03a7df
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 25 19:38:53 2026 +0100

    net/tcp: Limit the connections in SYN_RCVD by the listen() backlog.
    
    A SYN to a listener makes a connection in TCP_SYN_RCVD.  tcp_input()
    checks only that the backlog of the listener has a free container, and
    it takes none: a container is taken when the handshake completes.  So
    nothing limits the connections in TCP_SYN_RCVD.  Each one holds a
    connection structure of the shared pool until the SYN-ACK retries end,
    more than a minute with the defaults.  A peer that sends SYNs with
    spoofed sources and never completes the handshake takes the whole pool
    (8 connections by default) with a few SYNs.  Then all listeners refuse
    connections, and socket() and connect() on the device fail too.
    
    Now 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 in a time shorter
    than the arrival of backlog new SYNs still gets in.
    
    The backlog is for all devices.  The oldest connection on the device of
    the SYN goes first; if there is none, the oldest on another device
    goes, so a flood on one device does not stop the listener on another.
    tcp_free() takes the device lock of the connection, and the input path
    holds the lock of its own device: the lock of the other device is only
    tried, as tcp_reclaim() does.  If it is busy, the SYN is refused and the
    peer sends it again.  A budget per device would let a listener keep a
    full backlog on each device.
    
    accept() and listen() change the backlog with the listener locked.  It
    is read with the listener locked inside the list lock, the order of
    tcp_accept_connection().
    
    The connections of a listener are found as tcp_remove_syn_backlog()
    finds them, with the new helper tcp_is_syn_backlog().  No field is added
    to struct tcp_conn_s.  Without CONFIG_NET_TCPBACKLOG nothing changes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_conn.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 133 insertions(+), 11 deletions(-)

diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index daee3196fb6..5d54b35aa9a 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -564,6 +564,128 @@ static bool tcp_reclaimable(FAR struct tcp_conn_s *conn)
           conn->tcpstateflags == TCP_LAST_ACK);
 }
 
+/****************************************************************************
+ * Name: tcp_is_syn_backlog
+ *
+ * Description:
+ *   Return true if the connection is in TCP_SYN_RCVD on the address and
+ *   port of the listener.
+ *
+ ****************************************************************************/
+
+static bool tcp_is_syn_backlog(FAR struct tcp_conn_s *listener,
+                               FAR struct tcp_conn_s *conn)
+{
+  if (conn->tcpstateflags != TCP_SYN_RCVD)
+    {
+      return false;
+    }
+
+#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
+  return tcp_conn_cmp(listener->domain,
+                      (FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#else
+  return tcp_conn_cmp((FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#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.
+ *
+ * Input Parameters:
+ *   dev      - The device whose lock the caller holds.
+ *   listener - The listening connection.
+ *
+ * Returned Value:
+ *   true if a new connection in TCP_SYN_RCVD can be made.
+ *
+ ****************************************************************************/
+
+static bool tcp_syn_room(FAR struct net_driver_s *dev,
+                         FAR struct tcp_conn_s *listener)
+{
+  FAR struct net_driver_s *other;
+  FAR struct tcp_conn_s *oldest = NULL;
+  FAR struct tcp_conn_s *conn;
+  size_t nsynrcvd = 0;
+  size_t nfree = SIZE_MAX;
+  bool room = true;
+
+  tcp_conn_list_lock();
+#ifdef CONFIG_NET_TCPBACKLOG
+  /* accept() and listen() change the backlog with the listener locked.
+   * Lock it inside the list lock, as tcp_accept_connection() does.
+   */
+
+  conn_lock(&listener->sconn);
+  nfree = 0;
+  if (listener->backlog != NULL)
+    {
+      nfree = sq_count(&listener->backlog->bl_free);
+    }
+
+  conn_unlock(&listener->sconn);
+#endif
+
+  /* The list is in the order of creation: the first match is the oldest.
+   * Take the oldest on this device if there is one.
+   */
+
+  for (conn = (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
+       conn != NULL;
+       conn = (FAR struct tcp_conn_s *)conn->sconn.s_node.flink)
+    {
+      if (tcp_is_syn_backlog(listener, conn))
+        {
+          if (oldest == NULL || (oldest->dev != dev && conn->dev == dev))
+            {
+              oldest = conn;
+            }
+
+          nsynrcvd++;
+        }
+    }
+
+  if (nsynrcvd >= nfree)
+    {
+      room = false;
+    }
+
+  if (!room && oldest != NULL && nsynrcvd <= nfree)
+    {
+      /* tcp_free() takes the device lock of the connection.  The caller
+       * holds the lock of this device: only try the lock of another
+       * device, as tcp_reclaim() does, and refuse the SYN if it is busy.
+       */
+
+      other = oldest->dev != dev ? oldest->dev : NULL;
+      if (other == NULL || netdev_trylock(other) >= 0)
+        {
+          oldest->crefs = 0;
+          tcp_free(oldest);
+          room = true;
+
+          if (other != NULL)
+            {
+              netdev_unlock(other);
+            }
+        }
+    }
+
+  tcp_conn_list_unlock();
+  return room;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -1211,6 +1333,15 @@ FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct 
net_driver_s *dev,
   domain = PF_INET6;
 #endif
 
+  /* Each connection in TCP_SYN_RCVD takes a connection structure: limit
+   * them by the backlog, or a SYN flood takes them all.
+   */
+
+  if (!tcp_syn_room(dev, listener))
+    {
+      return NULL;
+    }
+
   /* Allocate the connection structure */
 
   conn = tcp_alloc_conn(domain, dev);
@@ -1758,17 +1889,8 @@ tcp_syn_backlog_conn(FAR struct tcp_conn_s *listener, 
uint32_t lastid)
        conn != NULL;
        conn = (FAR struct tcp_conn_s *)conn->sconn.s_node.flink)
     {
-      if (conn->tcpstateflags == TCP_SYN_RCVD &&
-          !TCP_LISTID_AFTER(conn->listid, lastid) &&
-#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-          tcp_conn_cmp(listener->domain,
-                       (FAR const union ip_addr_u *)&listener->u,
-                       listener->lport, conn)
-#else
-          tcp_conn_cmp((FAR const union ip_addr_u *)&listener->u,
-                       listener->lport, conn)
-#endif
-         )
+      if (!TCP_LISTID_AFTER(conn->listid, lastid) &&
+          tcp_is_syn_backlog(listener, conn))
         {
           return conn;
         }

Reply via email to