diff options
| author | Michael Tuexen <tuexen@FreeBSD.org> | 2026-07-01 16:07:04 +0000 |
|---|---|---|
| committer | Michael Tuexen <tuexen@FreeBSD.org> | 2026-07-01 16:07:04 +0000 |
| commit | fbc039e512c3bb1635ad20cc8f70ad608ea818b7 (patch) | |
| tree | 1211608aea818e35cc09f8cd2288ef3d1f64af70 | |
| parent | c146f5ae308ee47f2832fb3df1e14f864be70015 (diff) | |
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
| -rw-r--r-- | tests/sys/netinet/ip_reass_test.c | 30 |
1 files 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 * |
