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

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

commit e7c6b54dffb44bfccc3f92c35722c6bba1280682
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 14:16:26 2026 +0100

    net/tcp: Do not count a segment with data as a duplicate ACK.
    
    RFC 5681 (section 2) counts an ACK as a duplicate only when it carries
    no data.  A peer that sends data while our segment is in flight repeats
    the ACK number in each of its segments, because it has not received our
    segment yet.  psock_send_eventhandler() counted those segments as
    duplicate ACKs, and so did tcp_cc_recv_ack() with NewReno: every third
    segment started a fast retransmission.  During a flood of incoming data
    a small segment was sent again and again within a millisecond, and the
    timer path of each retransmission raised conn->nrtx, up to TCP_MAXRTX,
    which closes the connection with a reset.
    
    The fast retransmission for a segment with data also corrupted the
    stream.  It notified the driver while the device held the received
    segment, and the driver can poll the network at once in
    netdev_txnotify_dev().  After that poll it put back the buffer and the
    lengths of the received segment, but not d_sndlen.  When the poll
    stopped right after a sent segment, the reply to the received segment
    sent d_sndlen bytes of the received packet as new data, and then the
    real data under the same sequence numbers.  The peer kept the first
    copy: an SFTP upload over SSH failed with a bad MAC.
    
    Now only a segment without data is a duplicate ACK.  tcp_cc_recv_ack()
    gets the number of data bytes of the segment for this.  The fast
    retransmission of psock_send_eventhandler() for a segment with data
    cannot occur now: remove it.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h               |  8 +++++---
 net/tcp/tcp_cc.c            | 11 +++++++----
 net/tcp/tcp_input.c         |  2 +-
 net/tcp/tcp_send_buffered.c | 41 ++++++-----------------------------------
 4 files changed, 19 insertions(+), 43 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index cdc1958b580..05c2b59a18c 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -2401,8 +2401,9 @@ void tcp_cc_update(FAR struct tcp_conn_s *conn, FAR 
struct tcp_hdr_s *tcp);
  *   Update congestion control variables
  *
  * Input Parameters:
- *   conn   - The TCP connection of interest
- *   tcp    - The TCP header.
+ *   conn    - The TCP connection of interest
+ *   tcp     - The TCP header.
+ *   datalen - The number of data bytes in the segment
  *
  * Returned Value:
  *   None
@@ -2412,7 +2413,8 @@ void tcp_cc_update(FAR struct tcp_conn_s *conn, FAR 
struct tcp_hdr_s *tcp);
  *
  ****************************************************************************/
 
-void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp);
+void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp,
+                     uint16_t datalen);
 #endif
 
 #ifdef __cplusplus
diff --git a/net/tcp/tcp_cc.c b/net/tcp/tcp_cc.c
index 2a0b2bc1a91..d735bf12292 100644
--- a/net/tcp/tcp_cc.c
+++ b/net/tcp/tcp_cc.c
@@ -175,8 +175,9 @@ void tcp_cc_update(FAR struct tcp_conn_s *conn, FAR struct 
tcp_hdr_s *tcp)
  *   Update congestion control variables
  *
  * Input Parameters:
- *   conn   - The TCP connection of interest
- *   tcp    - The TCP header.
+ *   conn    - The TCP connection of interest
+ *   tcp     - The TCP header.
+ *   datalen - The number of data bytes in the segment
  *
  * Returned Value:
  *   None
@@ -186,11 +187,13 @@ void tcp_cc_update(FAR struct tcp_conn_s *conn, FAR 
struct tcp_hdr_s *tcp)
  *
  ****************************************************************************/
 
-void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp)
+void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp,
+                     uint16_t datalen)
 {
   uint32_t ackno = tcp_getsequence(tcp->ackno);
 
   /* Its only a duplicate ack if:
+   * 0) It carries no data (RFC 5681, section 2)
    * 1) It doesn't ACK new data
    * 2) There is outstanding unacknowledged data (retransmission
    *    timer running)
@@ -210,7 +213,7 @@ void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR 
struct tcp_hdr_s *tcp)
     {
       /* Clause 2 and Clause 3 */
 
-      if (conn->timer >= 0 &&
+      if (datalen == 0 && conn->timer >= 0 &&
           conn->last_ackno == ackno)
         {
           if (++conn->dupacks > TCP_FAST_RETRANSMISSION_THRESH)
diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c
index 07fc1fdd216..eac4414138b 100644
--- a/net/tcp/tcp_input.c
+++ b/net/tcp/tcp_input.c
@@ -1489,7 +1489,7 @@ skip_rtt:
 #ifdef CONFIG_NET_TCP_CC_NEWRENO
       /* If the packet is ack, update the cc var. */
 
-      tcp_cc_recv_ack(conn, tcp);
+      tcp_cc_recv_ack(conn, tcp, dev->d_len);
 #endif
       if (tcp_snd_wnd_update(conn, tcp))
         {
diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index a8f43163751..2b88be23384 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -581,8 +581,13 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
                         wrb, TCP_WBSEQNO(wrb), TCP_WBPKTLEN(wrb));
                 }
             }
-          else if (ackno == TCP_WBSEQNO(wrb))
+          else if (ackno == TCP_WBSEQNO(wrb) && (flags & TCP_NEWDATA) == 0)
             {
+              /* A duplicate ACK carries no data (RFC 5681, section 2): a
+               * segment with data that repeats the ACK number was sent
+               * before the peer received the segment.
+               */
+
 #ifdef CONFIG_NET_TCP_CC_NEWRENO
               if (conn->dupacks >= TCP_FAST_RETRANSMISSION_THRESH)
 #elif defined(CONFIG_NET_TCP_FAST_RETRANSMIT)
@@ -599,40 +604,6 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
                   TCP_WBNACK(wrb) = 0;
 #endif
 
-#ifdef CONFIG_NET_TCP_FAST_RETRANSMIT
-                  if ((flags & TCP_NEWDATA) != 0)
-                    {
-                      FAR uint8_t *buf = dev->d_buf;
-                      FAR uint8_t *appdata = dev->d_appdata;
-                      uint16_t len = dev->d_len;
-#  ifdef CONFIG_NET_TCPURGDATA
-                      FAR uint8_t *urgdata = dev->d_urgdata;
-                      uint16_t urglen = dev->d_urglen;
-#  endif
-                      FAR struct iob_s *iob = dev->d_iob;
-
-                      dev->d_buf = NULL;
-                      dev->d_iob = NULL;
-
-                      /* The current receive data needs to be handled by
-                       * following tcp_recvhandler or tcp_data_event. Notify
-                       * driver to send the message and marked as rexmit
-                       */
-
-                      conn->timeout = true;
-                      netdev_txnotify_dev(conn->dev, TCP_POLL);
-                      netdev_iob_replace(dev, iob);
-                      dev->d_buf = buf;
-                      dev->d_appdata = appdata;
-                      dev->d_len = len;
-#  ifdef CONFIG_NET_TCPURGDATA
-                      dev->d_urgdata = urgdata;
-                      dev->d_urglen = urglen;
-#  endif
-                      return flags;
-                    }
-#endif
-
 #ifdef CONFIG_NET_TCP_SELECTIVE_ACK
                   if ((conn->flags & TCP_SACK) &&
                       (tcp->tcpoffset & 0xf0) > 0x50)

Reply via email to