git: fbc039e512c3 - main - tests: fix checksum computation

From: Michael Tuexen <tuexen_at_FreeBSD.org>
Date: Thu, 02 Jul 2026 09:34:53 UTC
The branch main has been updated by tuexen:

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

commit fbc039e512c3bb1635ad20cc8f70ad608ea818b7
Author:     Michael Tuexen <tuexen@FreeBSD.org>
AuthorDate: 2026-07-01 16:07:04 +0000
Commit:     Michael Tuexen <tuexen@FreeBSD.org>
CommitDate: 2026-07-01 16:07:04 +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
    MFC after:              1 week
    Sponsored by:           Netflix, Inc.
    Differential Revision:  https://reviews.freebsd.org/D57988
---
 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 *