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 849d4fd873b8c8494cc64ccf3fe141a88be90826
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 15:55:33 2026 +0100

    net/tcp: Restart the retransmission timer only as RFC 6298 says.
    
    Two places restarted the retransmission timer while data was
    unacknowledged:
    - tcp_fill_header(), for every outgoing segment, also for an ACK
      without data;
    - tcp_input(), for every incoming ACK that was not an old one, also
      for one that acknowledges no new data (a duplicate ACK).  Such an
      ACK also gave an RTT sample, and when sndseq had come to ackseq it
      reset the count of retransmissions (conn->nrtx).
    While the peer sends data, each of its segments carries such an ACK
    and gets an ACK from us, so the timer did not expire until the peer
    stopped.  Only a fast retransmission sent a lost segment of ours
    again, and that fast retransmission came from segments with data
    that were counted as duplicate ACKs, which RFC 5681 (section 2) does
    not allow.
    
    RFC 6298 (section 5) starts the timer when a segment with data is sent
    and restarts it when an ACK acknowledges new data.  Now only a segment
    that takes sequence space (data, SYN or FIN) restarts the timer when it
    is sent; tcp_fill_header() gets this from its callers.  An incoming ACK
    that is not beyond snd_wl2 (the highest ACK so far) acknowledges no new
    data: it still sets TCP_ACKDATA, but gives no RTT sample and leaves the
    timer, sndseq and the count of retransmissions alone.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/sixlowpan/sixlowpan_tcpsend.c |  2 +-
 net/tcp/tcp.h                     | 13 ++++++++-----
 net/tcp/tcp_input.c               | 15 +++++++++++++++
 net/tcp/tcp_send.c                | 31 +++++++++++++++++++++----------
 4 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/net/sixlowpan/sixlowpan_tcpsend.c 
b/net/sixlowpan/sixlowpan_tcpsend.c
index 2570ddb1383..3b91b9c8d35 100644
--- a/net/sixlowpan/sixlowpan_tcpsend.c
+++ b/net/sixlowpan/sixlowpan_tcpsend.c
@@ -230,7 +230,7 @@ static int sixlowpan_tcp_header(FAR struct tcp_conn_s *conn,
 
   ipv6tcp->tcp.tcpoffset = (TCP_HDRLEN / 4) << 4; /* No optdata */
   ipv6tcp->tcp.flags     = TCP_ACK | TCP_PSH;     /* No urgent data */
-  tcp_fill_header(dev, conn, &ipv6tcp->tcp);
+  tcp_fill_header(dev, conn, &ipv6tcp->tcp, buflen > 0);
 
   /* Calculate TCP checksum. */
 
diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 18dcac7804c..cdc1958b580 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1222,12 +1222,15 @@ int tcp_accept_connection(FAR struct net_driver_s *dev,
  * Description:
  *   Fill in the sequence and acknowledgment numbers, the ports, the receive
  *   window and the urgent pointer of an outgoing TCP header, and start the
- *   retransmission timer if there is unacknowledged data.
+ *   retransmission timer if there is unacknowledged data and the segment
+ *   takes sequence space.
  *
  * Input Parameters:
- *   dev  - The device driver structure to use in the send operation
- *   conn - The TCP connection structure holding connection information
- *   tcp  - The TCP header to fill in
+ *   dev   - The device driver structure to use in the send operation
+ *   conn  - The TCP connection structure holding connection information
+ *   tcp   - The TCP header to fill in
+ *   timer - True if the segment carries data, SYN or FIN.  A segment
+ *           without them (an ACK) does not restart the timer.
  *
  * Returned Value:
  *   None
@@ -1239,7 +1242,7 @@ int tcp_accept_connection(FAR struct net_driver_s *dev,
 
 void tcp_fill_header(FAR struct net_driver_s *dev,
                      FAR struct tcp_conn_s *conn,
-                     FAR struct tcp_hdr_s *tcp);
+                     FAR struct tcp_hdr_s *tcp, bool timer);
 
 /****************************************************************************
  * Name: tcp_send
diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c
index 3b7c0a7118e..07fc1fdd216 100644
--- a/net/tcp/tcp_input.c
+++ b/net/tcp/tcp_input.c
@@ -1318,6 +1318,21 @@ found:
           goto skip_rtt;
         }
 
+      if ((conn->tcpstateflags & TCP_STATE_MASK) >= TCP_ESTABLISHED &&
+          !TCP_SEQ_GT(ackseq, conn->snd_wl2))
+        {
+          /* The segment acknowledges no new data (a duplicate ACK: not
+           * beyond snd_wl2, the highest ACK so far): it gives no RTT
+           * sample, does not restart the retransmission timer (RFC 6298,
+           * section 5.3; else a peer that sends data keeps the timer from
+           * expiring) and does not reset the count of retransmissions
+           * (the backoff of section 5.5).
+           */
+
+          flags |= TCP_ACKDATA;
+          goto skip_rtt;
+        }
+
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
 #ifdef CONFIG_NET_SENDFILE
       if (!conn->sendfile)
diff --git a/net/tcp/tcp_send.c b/net/tcp/tcp_send.c
index e5f1f90d907..b6e86080275 100644
--- a/net/tcp/tcp_send.c
+++ b/net/tcp/tcp_send.c
@@ -110,12 +110,15 @@ static inline FAR struct tcp_hdr_s *tcp_header(FAR struct 
net_driver_s *dev)
  * Description:
  *   Fill in the sequence and acknowledgment numbers, the ports, the receive
  *   window and the urgent pointer of an outgoing TCP header, and start the
- *   retransmission timer if there is unacknowledged data.
+ *   retransmission timer if there is unacknowledged data and the segment
+ *   takes sequence space.
  *
  * Input Parameters:
- *   dev  - The device driver structure to use in the send operation
- *   conn - The TCP connection structure holding connection information
- *   tcp  - The TCP header to fill in
+ *   dev   - The device driver structure to use in the send operation
+ *   conn  - The TCP connection structure holding connection information
+ *   tcp   - The TCP header to fill in
+ *   timer - True if the segment carries data, SYN or FIN.  A segment
+ *           without them (an ACK) does not restart the timer.
  *
  * Returned Value:
  *   None
@@ -127,7 +130,7 @@ static inline FAR struct tcp_hdr_s *tcp_header(FAR struct 
net_driver_s *dev)
 
 void tcp_fill_header(FAR struct net_driver_s *dev,
                      FAR struct tcp_conn_s *conn,
-                     FAR struct tcp_hdr_s *tcp)
+                     FAR struct tcp_hdr_s *tcp, bool timer)
 {
   /* Set TCP sequence numbers and port numbers */
 
@@ -150,7 +153,13 @@ void tcp_fill_header(FAR struct net_driver_s *dev,
     }
   else
     {
-      if (conn->tx_unacked != 0)
+      /* Restart the retransmission timer with a segment that takes
+       * sequence space.  An ACK does not: while the peer sends data, each
+       * of its segments gets an ACK, and a lost segment of ours would never
+       * be sent again.
+       */
+
+      if (conn->tx_unacked != 0 && timer)
         {
           conn->timeout = false;
           tcp_update_retrantimer(conn, conn->rto);
@@ -208,9 +217,9 @@ void tcp_fill_header(FAR struct net_driver_s *dev,
 
 static void tcp_sendcommon(FAR struct net_driver_s *dev,
                            FAR struct tcp_conn_s *conn,
-                           FAR struct tcp_hdr_s *tcp)
+                           FAR struct tcp_hdr_s *tcp, bool timer)
 {
-  tcp_fill_header(dev, conn, tcp);
+  tcp_fill_header(dev, conn, tcp, timer);
 
   /* Update device buffer length before setup the IP header */
 
@@ -368,7 +377,9 @@ void tcp_send(FAR struct net_driver_s *dev, FAR struct 
tcp_conn_s *conn,
       tcp->tcpoffset = (TCP_HDRLEN / 4) << 4;
     }
 
-  tcp_sendcommon(dev, conn, tcp);
+  tcp_sendcommon(dev, conn, tcp,
+                 len > tcpip_hdrsize(conn) ||
+                 (flags & (TCP_SYN | TCP_FIN)) != 0);
 
 #if defined(CONFIG_NET_STATISTICS) && \
     defined(CONFIG_NET_TCP_DEBUG_DROP_SEND)
@@ -714,7 +725,7 @@ void tcp_synack(FAR struct net_driver_s *dev, FAR struct 
tcp_conn_s *conn,
 
   /* Complete the common portions of the TCP message */
 
-  tcp_sendcommon(dev, conn, tcp);
+  tcp_sendcommon(dev, conn, tcp, (ack & (TCP_SYN | TCP_FIN)) != 0);
 }
 
 /****************************************************************************

Reply via email to