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 d7962e4f92c40f11659d09bd90c12ded5a6f8c9f
Author: gaohedong <[email protected]>
AuthorDate: Sun Sep 14 12:26:52 2025 +0800

    net/tcp: drop packet when offset info is wrong
    
    According RFC 793 section 3.1, TCP header length must be longer than 20.
    Meanwhile, the total length of the packet cannot be less than the sum of 
the IP header and TCP header
    
    Signed-off-by: gaohedong <[email protected]>
---
 .codespellrc                |  1 +
 include/nuttx/net/tcp.h     |  1 +
 net/procfs/net_statistics.c | 17 ++++++++++++-
 net/tcp/tcp_input.c         | 62 +++++++++++++++++++++++++++------------------
 4 files changed, 55 insertions(+), 26 deletions(-)

diff --git a/.codespellrc b/.codespellrc
index 96cd556dedf..0de574c29b0 100644
--- a/.codespellrc
+++ b/.codespellrc
@@ -37,6 +37,7 @@ ignore-words-list =
   FRAM,
   FRO,
   hart,
+  headerr,
   hsi,
   iif,
   IST,
diff --git a/include/nuttx/net/tcp.h b/include/nuttx/net/tcp.h
index 5804b19f5a0..5cebec18b7f 100644
--- a/include/nuttx/net/tcp.h
+++ b/include/nuttx/net/tcp.h
@@ -197,6 +197,7 @@ struct tcp_stats_s
   net_stats_t syndrop;    /* Number of dropped SYNs due to too few
                            * available connections */
   net_stats_t synrst;     /* Number of SYNs for closed ports triggering a RST 
*/
+  net_stats_t headerr;    /* Number of dropped SYNs due to bad header info */
 };
 #endif
 
diff --git a/net/procfs/net_statistics.c b/net/procfs/net_statistics.c
index d828c3ddc66..dbf3a9c9957 100644
--- a/net/procfs/net_statistics.c
+++ b/net/procfs/net_statistics.c
@@ -62,6 +62,7 @@ static int netprocfs_checksum(FAR struct netprocfs_file_s 
*netfile);
 #ifdef CONFIG_NET_TCP
 static int netprocfs_tcp_dropped_1(FAR struct netprocfs_file_s *netfile);
 static int netprocfs_tcp_dropped_2(FAR struct netprocfs_file_s *netfile);
+static int netprocfs_tcp_dropped_3(FAR struct netprocfs_file_s *netfile);
 #endif /* CONFIG_NET_TCP */
 static int netprocfs_prototype(FAR struct netprocfs_file_s *netfile);
 static int netprocfs_sent(FAR struct netprocfs_file_s *netfile);
@@ -94,6 +95,7 @@ static const linegen_t g_stat_linegen[] =
 #ifdef CONFIG_NET_TCP
   netprocfs_tcp_dropped_1,
   netprocfs_tcp_dropped_2,
+  netprocfs_tcp_dropped_3,
 #endif /* CONFIG_NET_TCP */
 
   netprocfs_prototype,
@@ -288,7 +290,8 @@ static int netprocfs_checksum(FAR struct netprocfs_file_s 
*netfile)
                   g_netstats.udp.chkerr);
 #endif
 #ifdef CONFIG_NET_ICMP
-  len += snprintf(&netfile->line[len], NET_LINELEN - len, "  ----");
+  len += snprintf(&netfile->line[len], NET_LINELEN - len, "  %04x",
+                  g_netstats.icmp.csumerr);
 #endif
 #ifdef CONFIG_NET_ICMPv6
   len += snprintf(&netfile->line[len], NET_LINELEN - len, "  ----");
@@ -328,6 +331,18 @@ static int netprocfs_tcp_dropped_2(FAR struct 
netprocfs_file_s *netfile)
 }
 #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
 
+/****************************************************************************
+ * Name: netprocfs_tcp_dropped_3
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_TCP)
+static int netprocfs_tcp_dropped_3(FAR struct netprocfs_file_s *netfile)
+{
+  return snprintf(netfile->line, NET_LINELEN,
+                  "              HDR: %04x\n", g_netstats.tcp.headerr);
+}
+#endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
+
 /****************************************************************************
  * Name: netprocfs_prototype
  ****************************************************************************/
diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c
index 0d17218f54e..9d9d2eb69b5 100644
--- a/net/tcp/tcp_input.c
+++ b/net/tcp/tcp_input.c
@@ -740,6 +740,27 @@ static void tcp_input(FAR struct net_driver_s *dev, 
uint8_t domain,
     }
 #endif
 
+  /* Calculated the length of the data, if the application has sent
+   * any data to us.
+   */
+
+  len = (tcp->tcpoffset >> 4) << 2;
+
+  if (len < TCP_HDRLEN || dev->d_len < (iplen + len))
+    {
+      /* According RFC 793 chapter 3.1, the TCP header length must be
+       * greater than 20. Meanwhile, the total length of the packet
+       * cannot be less than the sum of the IP header and TCP header.
+       */
+
+#ifdef CONFIG_NET_STATISTICS
+      g_netstats.tcp.drop++;
+      g_netstats.tcp.headerr++;
+#endif
+      nwarn("WARNING: TCP offset info error\n");
+      goto drop;
+    }
+
   /* Demultiplex this segment. First check any active connections. */
 
   conn = tcp_active(dev, tcp);
@@ -763,6 +784,7 @@ static void tcp_input(FAR struct net_driver_s *dev, uint8_t 
domain,
       if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_SYN_SENT)
         {
           uint32_t ackseq;
+
           if ((tcp->flags & TCP_ACK) != 0)
             {
               ackseq = tcp_getsequence(tcp->ackno);
@@ -1064,21 +1086,9 @@ found:
       goto drop;
     }
 
-  /* Calculated the length of the data, if the application has sent
-   * any data to us.
-   */
-
-  len = (tcp->tcpoffset >> 4) << 2;
-
   /* d_appdata should remove the tcp specific option field. */
 
-  if ((tcp->tcpoffset & 0xf0) > 0x50)
-    {
-      if (dev->d_len >= len)
-        {
-          dev->d_appdata += len - TCP_HDRLEN;
-        }
-    }
+  dev->d_appdata += len - TCP_HDRLEN;
 
   /* d_len will contain the length of the actual TCP data. This is
    * calculated by subtracting the length of the TCP header (in
@@ -1221,6 +1231,7 @@ found:
            */
 
           uint32_t sndseq = tcp_getsequence(conn->sndseq);
+
           if (TCP_SEQ_LTE(sndseq, ackseq))
             {
               ninfo("sndseq: %08" PRIx32 "->%08" PRIx32
@@ -1239,6 +1250,7 @@ found:
       if (conn->nrtx == 0)
         {
           signed char m;
+
           m = conn->rto - conn->timer;
 
           /* This is taken directly from VJs original code in his paper */
@@ -1578,15 +1590,15 @@ skip_rtt:
                 dev->d_urglen = dev->d_len;
               }
 
-             /* The d_len field contains the length of the incoming data.
-              * d_urgdata points to the "urgent" data at the beginning of
-              * the payload; d_appdata field points to the any "normal" data
-              * that may follow the urgent data.
-              *
-              * NOTE: If the urgent data continues in the next packet, then
-              * d_len will be zero and d_appdata will point past the end of
-              * the payload (which is OK).
-              */
+            /* The d_len field contains the length of the incoming data.
+             * d_urgdata points to the "urgent" data at the beginning of
+             * the payload; d_appdata field points to the any "normal" data
+             * that may follow the urgent data.
+             *
+             * NOTE: If the urgent data continues in the next packet, then
+             * d_len will be zero and d_appdata will point past the end of
+             * the payload (which is OK).
+             */
 
             net_incr32(conn->rcvseq, dev->d_urglen);
             dev->d_len     -= dev->d_urglen;
@@ -1762,9 +1774,9 @@ skip_rtt:
       case TCP_CLOSE_WAIT:
 #ifdef CONFIG_NET_TCP_KEEPALIVE
         /* If the established socket receives an ACK or any kind of data
-        * from the remote peer (whether we accept it or not), then reset
-        * the keep alive timer.
-        */
+         * from the remote peer (whether we accept it or not), then reset
+         * the keep alive timer.
+         */
 
         if (conn->keepalive && (tcp->flags & TCP_ACK) != 0)
           {

Reply via email to