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

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

commit 267df3a120aa264fa0ef75e78fe6190ef2474842
Author: gaohedong <[email protected]>
AuthorDate: Tue Sep 9 19:06:24 2025 +0800

    net/icmp: check the checksum field when recieve icmp message
    
    According to RFC 792 page 4, do the icmp checksum if 
CONFIG_NET_ICMP_CHECKSUMS is set.
    
    Signed-off-by: gaohedong <[email protected]>
---
 include/nuttx/net/icmp.h |  4 +++-
 net/icmp/icmp_input.c    | 19 +++++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/net/icmp.h b/include/nuttx/net/icmp.h
index 364581805d5..2a61800d5df 100644
--- a/include/nuttx/net/icmp.h
+++ b/include/nuttx/net/icmp.h
@@ -2,7 +2,8 @@
  * include/nuttx/net/icmp.h
  *
  * SPDX-License-Identifier: BSD-3-Clause
- * SPDX-FileCopyrightText: 2007-2009, 2012, 2014 Gregory Nutt. All rights 
reserved.
+ * SPDX-FileCopyrightText: 2007-2009, 2012, 2014 Gregory Nutt. All rights
+ *                         reserved.
  * SPDX-FileCopyrightText: 2001-2003, Adam Dunkels. All rights reserved.
  * SPDX-FileContributor: Gregory Nutt <[email protected]>
  * SPDX-FileContributor: Adam Dunkels <[email protected]>
@@ -159,6 +160,7 @@ struct icmp_stats_s
   net_stats_t recv;       /* Number of received ICMP packets */
   net_stats_t sent;       /* Number of sent ICMP packets */
   net_stats_t typeerr;    /* Number of ICMP packets with a wrong type */
+  net_stats_t csumerr;    /* Number of ICMP packets with a wrong checksum */
 };
 #endif
 
diff --git a/net/icmp/icmp_input.c b/net/icmp/icmp_input.c
index c8aa572c2c0..1d4abef7894 100644
--- a/net/icmp/icmp_input.c
+++ b/net/icmp/icmp_input.c
@@ -272,7 +272,9 @@ void icmp_input(FAR struct net_driver_s *dev)
 {
   FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
   FAR struct icmp_hdr_s *icmp;
-
+#ifdef CONFIG_NET_ICMP_CHECKSUMS
+  uint16_t csum;
+#endif
   /* Get the IP header length (accounting for possible options). */
 
   uint16_t iphdrlen = (ipv4->vhl & IPv4_HLMASK) << 2;
@@ -288,6 +290,19 @@ void icmp_input(FAR struct net_driver_s *dev)
 
   icmp = IPBUF(iphdrlen);
 
+#ifdef CONFIG_NET_ICMP_CHECKSUMS
+  csum = icmp_chksum(dev,
+                     ((ipv4->len[0] << 8) | ipv4->len[1]) - iphdrlen);
+  if (csum != 0xffff)
+    {
+      ninfo("ICMP checksum error\n");
+#ifdef CONFIG_NET_STATISTICS
+      g_netstats.icmp.csumerr++;
+#endif
+      goto drop;
+    }
+#endif
+
   /* ICMP echo (i.e., ping) processing. This is simple, we only change the
    * ICMP type from ECHO to ECHO_REPLY and adjust the ICMP checksum before
    * we return the packet.
@@ -408,7 +423,7 @@ typeerr:
   g_netstats.icmp.typeerr++;
 #endif
 
-#ifdef CONFIG_NET_ICMP_SOCKET
+#if defined(CONFIG_NET_ICMP_SOCKET) || defined(CONFIG_NET_ICMP_CHECKSUMS)
 drop:
 #ifdef CONFIG_NET_STATISTICS
   g_netstats.icmp.drop++;

Reply via email to