git: cad0d5772516 - stable/15 - tests: fix checksum computation

Michael Tuexen <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src,gmane.os.freebsd.devel.stable.scm
Message-ID <[email protected]>
The branch stable/15 has been updated by tuexen:

URL: https://cgit.FreeBSD.org/src/commit/?id=cad0d577251632bc9c7f808121ea9b0220474dbd

commit cad0d577251632bc9c7f808121ea9b0220474dbd
Author:     Michael Tuexen <[email protected]>
AuthorDate: 2026-07-01 16:07:04 +0000
Commit:     Michael Tuexen <[email protected]>
CommitDate: 2026-08-06 09:05:08 +0000

    tests: fix checksum computation
    
    This fixes an endianness bug in sys/netinet/ip_reass_test.
    Just use the code from RFC 1071.
    
    Reported by:            glebius
    Reviewed by:            glebius, Timo Völker
    Sponsored by:           Netflix, Inc.
    Differential Revision:  https://reviews.freebsd.org/D57988
    
    (cherry picked from commit fbc039e512c3bb1635ad20cc8f70ad608ea818b7)
---
 tests/sys/netinet/ip_reass_test.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/tests/sys/netinet/ip_reass_test.c b/tests/sys/netinet/ip_reass_test.c
index 538815bd7a2c..e32fb63f34da 100644
--- a/tests/sys/netinet/ip_reass_test.c
+++ b/tests/sys/netinet/ip_reass_test.c
@@ -55,24 +55,28 @@ struct lopacket {
 	char		payload[];
 };
 
-static void
-update_cksum(struct ip *ip)
+static uint16_t
+in_cksum(void *data, size_t len)
 {
+	uint16_t *cksump;
 	size_t i;
 	uint32_t cksum;
-	uint8_t  *cksump;
-	uint16_t tmp;
 
+	ATF_REQUIRE(len % 2 == 0);
+	cksump = (uint16_t *)data;
+	cksum = 0;
+	for (i = 0; i < len / sizeof(uint16_t); i++)
+		cksum += *cksump++;
+	while ((cksum >> 16) != 0)
+		cksum = (cksum & 0xffff) + (cksum >> 16);
+	return ((uint16_t)~cksum);
+}
+
+static void
+update_cksum(struct ip *ip)
+{
 	ip->ip_sum = 0;
-	cksump = (char *)ip;
-	for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(uint16_t); i++) {
-		tmp = *cksump++;
-		tmp = tmp << 8 | *cksump++;
-		cksum += ntohs(tmp);
-	}
-	cksum = (cksum >> 16) + (cksum & 0xffff);
-	cksum = ~(cksum + (cksum >> 16));
-	ip->ip_sum = htons((uint16_t)cksum);
+	ip->ip_sum = in_cksum(ip, sizeof(struct ip));
 }
 
 static struct lopacket *
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.