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 76a505fa63e32cd4af40a200110f1770bee229b9
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 19 06:36:45 2026 +0100

    net/tcp: Free a listener's half-open connections without the list lock.
    
    tcp_unlisten() called tcp_remove_syn_backlog() with the TCP connection
    list lock held, and tcp_remove_syn_backlog() freed each connection in
    TCP_SYN_RCVD of the listener with tcp_free(), which takes the device lock
    of the connection.  That is the list lock before a device lock, the
    reverse of the input path, which takes the list lock under the device
    lock (tcp_active(), tcp_alloc_accept()).  The close or the shutdown of a
    listener and the input path could wait for each other.
    
    Now tcp_unlisten() removes the listener from the table with the list
    locked and calls tcp_remove_syn_backlog() after it releases the lock.
    tcp_remove_syn_backlog() finds each connection under the list lock,
    locks its device and then the list, and frees it if it is still there
    in TCP_SYN_RCVD on the same device.
    
    A connection in TCP_SYN_RCVD does not know its listener: it matches by
    the address and the port.  Once the lock is released, a new listener on
    the same port can come (SO_REUSEADDR), and its half-open connections
    match too.  So each connection now has a list id, the order in which it
    entered the list of active connections (tcp_add_active_conn()), and
    tcp_unlisten() takes the last list id together with the listener out of
    the table.  tcp_remove_syn_backlog() frees only the connections up to
    that id: the ones of the old listener, and the loop ends, also with
    SYNs that keep coming for a new listener.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h        |  32 ++++++++++++++-
 net/tcp/tcp_conn.c   | 114 +++++++++++++++++++++++++++++++++++++++++++++------
 net/tcp/tcp_listen.c |  15 ++++++-
 3 files changed, 147 insertions(+), 14 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 5b263fab540..1acf79caf8d 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -125,6 +125,12 @@
 #  define TCP_SEND_BUFSIZE  TCP_DEFAULT_BUFSIZE
 #endif
 
+/* True if the connection with list id a entered the list of active
+ * connections after the one with list id b (the ids wrap).
+ */
+
+#define TCP_LISTID_AFTER(a, b) ((int32_t)((uint32_t)(a) - (uint32_t)(b)) > 0)
+
 /* The error of a send on a TCP socket that is not connected: -ECONNRESET
  * if the peer reset the connection and this is not yet reported, -EPIPE if
  * the connection was closed (POSIX: the socket is no longer connected),
@@ -252,6 +258,8 @@ struct tcp_conn_s
                            * receive next */
   uint8_t  sndseq[4];     /* The sequence number that was last sent by us */
   uint32_t rexmit_seq;    /* The sequence number to be retrasmitted */
+  uint32_t listid;        /* The order of entry in the list of active
+                           * connections (tcp_add_active_conn()) */
   uint8_t  crefs;         /* Reference counts on this instance */
 #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
   uint8_t  domain;        /* IP domain: PF_INET or PF_INET6 */
@@ -805,6 +813,20 @@ int tcp_connect(FAR struct tcp_conn_s *conn,
 
 void tcp_add_active_conn(FAR struct tcp_conn_s *conn);
 
+/****************************************************************************
+ * Name: tcp_conn_lastid
+ *
+ * Description:
+ *   Return the list id of the connection that entered the list of active
+ *   TCP connections last.
+ *
+ * Assumptions:
+ *   The TCP connection list is locked.
+ *
+ ****************************************************************************/
+
+uint32_t tcp_conn_lastid(void);
+
 /****************************************************************************
  * Name: tcp_removeconn
  *
@@ -825,9 +847,17 @@ void tcp_removeconn(FAR struct tcp_conn_s *conn);
  *   This function is used to remove the currently SYN_RCVD connection
  *   created by the listener
  *
+ *   Only the connections up to the list id lastid are freed.
+ *
+ * Assumptions:
+ *   The listener is no longer in the table of listeners.  lastid is the
+ *   value of tcp_conn_lastid() when it was taken out.  The caller holds no
+ *   lock of the network.
+ *
  ****************************************************************************/
 
-void tcp_remove_syn_backlog(FAR struct tcp_conn_s *listener);
+void tcp_remove_syn_backlog(FAR struct tcp_conn_s *listener,
+                            uint32_t lastid);
 
 /****************************************************************************
  * Name: psock_tcp_connect
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index a09648a1752..daee3196fb6 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -105,6 +105,10 @@ MEMPOOL_DEFINE(g_tcp_connections, sizeof(struct 
tcp_conn_s),
 
 static dq_queue_t g_active_tcp_connections;
 
+/* The list id of the connection that entered the list last */
+
+static uint32_t g_tcp_listid;
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -1683,7 +1687,8 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
  * Name: tcp_add_active_conn
  *
  * Description:
- *   Add the connection to the list of active TCP connections
+ *   Add the connection to the list of active TCP connections, at its tail,
+ *   with the next list id.  So the list ids grow from the head to the tail.
  *
  * Assumptions:
  *   This function is called from normal user level code.
@@ -1693,10 +1698,29 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
 void tcp_add_active_conn(FAR struct tcp_conn_s *conn)
 {
   tcp_conn_list_lock();
+  conn->listid = ++g_tcp_listid;
   dq_addlast(&conn->sconn.s_node, &g_active_tcp_connections);
   tcp_conn_list_unlock();
 }
 
+/****************************************************************************
+ * Name: tcp_conn_lastid
+ *
+ * Description:
+ *   Return the list id of the connection that entered the list of active
+ *   TCP connections last.  A connection with a later list id
+ *   (TCP_LISTID_AFTER()) entered the list after this call.
+ *
+ * Assumptions:
+ *   The TCP connection list is locked.
+ *
+ ****************************************************************************/
+
+uint32_t tcp_conn_lastid(void)
+{
+  return g_tcp_listid;
+}
+
 /****************************************************************************
  * Name: tcp_removeconn
  *
@@ -1714,29 +1738,28 @@ void tcp_removeconn(FAR struct tcp_conn_s *conn)
 }
 
 /****************************************************************************
- * Name: tcp_remove_syn_backlog
+ * Name: tcp_syn_backlog_conn
  *
  * Description:
- *   This function is used to remove the currently SYN_RCVD connection
- *   created by the listener
+ *   Return the first connection in TCP_SYN_RCVD that the listener created,
+ *   or NULL.  Only connections up to the list id lastid count.
  *
  * Assumptions:
- *   This function is called from network logic with the tcp conn list
- *   locked.
+ *   The TCP connection list is locked.
  *
  ****************************************************************************/
 
-void tcp_remove_syn_backlog(FAR struct tcp_conn_s *listener)
+static FAR struct tcp_conn_s *
+tcp_syn_backlog_conn(FAR struct tcp_conn_s *listener, uint32_t lastid)
 {
   FAR struct tcp_conn_s *conn;
-  FAR struct tcp_conn_s *next;
 
-  next = (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
-  while (next)
+  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)
     {
-      conn = next;
-      next = (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,
@@ -1746,10 +1769,77 @@ void tcp_remove_syn_backlog(FAR struct tcp_conn_s 
*listener)
                        listener->lport, conn)
 #endif
          )
+        {
+          return conn;
+        }
+    }
+
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: tcp_remove_syn_backlog
+ *
+ * Description:
+ *   This function is used to remove the currently SYN_RCVD connection
+ *   created by the listener
+ *
+ *   Only the connections up to the list id lastid are freed: the ones that
+ *   were there when the listener left the table.  A new listener on the
+ *   same address and port can come meanwhile, and its connections in
+ *   TCP_SYN_RCVD match too.
+ *
+ * Assumptions:
+ *   The listener is no longer in the table of listeners.  lastid is the
+ *   value of tcp_conn_lastid() when it was taken out.  The caller holds no
+ *   lock of the network.
+ *
+ ****************************************************************************/
+
+void tcp_remove_syn_backlog(FAR struct tcp_conn_s *listener,
+                            uint32_t lastid)
+{
+  FAR struct net_driver_s *dev;
+  FAR struct tcp_conn_s *conn;
+
+  /* tcp_free() takes the device lock of the connection, and the input path
+   * takes the list lock under the device lock.  So find the connection
+   * with the list locked, then lock its device and the list again, and
+   * free it if it is still there in TCP_SYN_RCVD on the same device.  The
+   * loop ends: each step frees a connection up to lastid, or finds that
+   * one has gone.
+   */
+
+  for (; ; )
+    {
+      tcp_conn_list_lock();
+      conn = tcp_syn_backlog_conn(listener, lastid);
+      dev  = conn != NULL ? conn->dev : NULL;
+      tcp_conn_list_unlock();
+
+      if (conn == NULL)
+        {
+          break;
+        }
+
+      if (dev != NULL)
+        {
+          netdev_lock(dev);
+        }
+
+      tcp_conn_list_lock();
+      if (tcp_syn_backlog_conn(listener, lastid) == conn &&
+          conn->dev == dev)
         {
           conn->crefs = 0;
           tcp_free(conn);
         }
+
+      tcp_conn_list_unlock();
+      if (dev != NULL)
+        {
+          netdev_unlock(dev);
+        }
     }
 }
 
diff --git a/net/tcp/tcp_listen.c b/net/tcp/tcp_listen.c
index bfbaccf09fa..dbd0e688038 100644
--- a/net/tcp/tcp_listen.c
+++ b/net/tcp/tcp_listen.c
@@ -137,6 +137,7 @@ FAR struct tcp_conn_s *tcp_findlistener(FAR union 
ip_binding_u *uaddr,
 
 int tcp_unlisten(FAR struct tcp_conn_s *conn)
 {
+  uint32_t lastid;
   int ndx;
   int ret = -EINVAL;
 
@@ -146,13 +147,25 @@ int tcp_unlisten(FAR struct tcp_conn_s *conn)
       if (tcp_listenports[ndx] == conn)
         {
           tcp_listenports[ndx] = NULL;
-          tcp_remove_syn_backlog(conn);
           ret = OK;
           break;
         }
     }
 
+  lastid = tcp_conn_lastid();
   tcp_conn_list_unlock();
+
+  /* Free its connections in TCP_SYN_RCVD without the list lock: that
+   * takes their device locks, which come before the list lock.  Only the
+   * ones that are there now: a new listener on the same port can come
+   * before the end.
+   */
+
+  if (ret == OK)
+    {
+      tcp_remove_syn_backlog(conn, lastid);
+    }
+
   return ret;
 }
 

Reply via email to