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 <gaohedong@xiaomi.com>
This commit is contained in:
gaohedong
2025-09-09 19:06:24 +08:00
committed by Xiang Xiao
parent 51681a6a8b
commit 267df3a120
2 changed files with 20 additions and 3 deletions
+3 -1
View File
@@ -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 <gnutt@nuttx.org>
* SPDX-FileContributor: Adam Dunkels <adam@dunkels.com>
@@ -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
+17 -2
View File
@@ -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++;