This is an automated email from the ASF dual-hosted git repository.

casaroli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new d80bd921ff8 net/tcp: Add a connection to the activation queue requires 
lock protection
d80bd921ff8 is described below

commit d80bd921ff8a10fe7b057340a6c34af9fb3ec48b
Author: zhangshuai39 <[email protected]>
AuthorDate: Thu Nov 27 10:19:39 2025 +0800

    net/tcp: Add a connection to the activation queue requires lock protection
    
    If the `conn` is added to the activation queue without lock protection,
    other threads might trigger `netdev_txnotify_dev`, causing this `conn`
    to also trigger the three-way handshake.
    
    Signed-off-by: zhangshuai39 <[email protected]>
---
 net/tcp/tcp.h         | 13 +++++++++++++
 net/tcp/tcp_conn.c    | 29 +++++++++++++++++++----------
 net/tcp/tcp_connect.c |  2 ++
 3 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 5eb1c6a8ff9..2bdb0f50665 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -698,6 +698,19 @@ int tcp_bind(FAR struct tcp_conn_s *conn, FAR const struct 
sockaddr *addr);
 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
+ *
+ * Assumptions:
+ *   This function is called from normal user level code.
+ *
+ ****************************************************************************/
+
+void tcp_add_active_conn(FAR struct tcp_conn_s *conn);
+
 /****************************************************************************
  * Name: tcp_removeconn
  *
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index ae05dafbb50..c3bc8dc4a42 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -1175,9 +1175,7 @@ FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct 
net_driver_s *dev,
        * Interrupts should already be disabled in this context.
        */
 
-      tcp_conn_list_lock();
-      dq_addlast(&conn->sconn.node, &g_active_tcp_connections);
-      tcp_conn_list_unlock();
+      tcp_add_active_conn(conn);
 
       tcp_update_retrantimer(conn, TCP_RTO);
     }
@@ -1482,13 +1480,6 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
   sq_init(&conn->write_q);
   sq_init(&conn->unacked_q);
 #endif
-
-  /* And, finally, put the connection structure into the active list. */
-
-  tcp_conn_list_lock();
-  dq_addlast(&conn->sconn.node, &g_active_tcp_connections);
-  tcp_conn_list_unlock();
-
   return OK;
 }
 
@@ -1518,6 +1509,24 @@ void tcp_conn_list_unlock(void)
   NET_BUFPOOL_UNLOCK(g_tcp_connections);
 }
 
+/****************************************************************************
+ * Name: tcp_add_active_conn
+ *
+ * Description:
+ *   Add the connection to the list of active TCP connections
+ *
+ * Assumptions:
+ *   This function is called from normal user level code.
+ *
+ ****************************************************************************/
+
+void tcp_add_active_conn(FAR struct tcp_conn_s *conn)
+{
+  tcp_conn_list_lock();
+  dq_addlast(&conn->sconn.node, &g_active_tcp_connections);
+  tcp_conn_list_unlock();
+}
+
 /****************************************************************************
  * Name: tcp_removeconn
  *
diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c
index 5fdeb964b67..ffe3e3116b3 100644
--- a/net/tcp/tcp_connect.c
+++ b/net/tcp/tcp_connect.c
@@ -359,6 +359,7 @@ int psock_tcp_connect(FAR struct socket *psock,
 
       if (_SS_ISNONBLOCK(conn->sconn.s_flags))
         {
+          tcp_add_active_conn(conn);
           ret = -EINPROGRESS;
         }
       else
@@ -366,6 +367,7 @@ int psock_tcp_connect(FAR struct socket *psock,
           /* Set up the callbacks in the connection */
 
           conn_dev_lock(&conn->sconn, conn->dev);
+          tcp_add_active_conn(conn);
           ret = psock_setup_callbacks(psock, &state);
           if (ret >= 0)
             {

Reply via email to