diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-04-20 23:23:39 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-04-20 23:23:39 +0000 |
commit | 6498804829a6c0b2847236b8efed3ec0be750abb (patch) | |
tree | 5a14e5d5c20ff687f49563ac8b01f67cb5446cc7 | |
parent | 1c776376e91e31030ffe751a56913e8ce075d173 (diff) | |
parent | a98bc88da3876d5d3294004dc4218b9f5baf95e1 (diff) | |
download | net-6498804829a6c0b2847236b8efed3ec0be750abb.tar.gz |
Snap for 9981223 from a98bc88da3876d5d3294004dc4218b9f5baf95e1 to udc-release
Change-Id: Id30fcd609b87161893c6b1b90796ed340cd38523
-rw-r--r-- | common/framework/com/android/net/module/util/IpUtils.java | 16 | ||||
-rw-r--r-- | common/tests/unit/src/com/android/net/module/util/IpUtilsTest.java | 14 |
2 files changed, 23 insertions, 7 deletions
diff --git a/common/framework/com/android/net/module/util/IpUtils.java b/common/framework/com/android/net/module/util/IpUtils.java index 569733ed..18d96f30 100644 --- a/common/framework/com/android/net/module/util/IpUtils.java +++ b/common/framework/com/android/net/module/util/IpUtils.java @@ -16,6 +16,8 @@ package com.android.net.module.util; +import com.android.internal.annotations.VisibleForTesting; + import static android.system.OsConstants.IPPROTO_ICMPV6; import static android.system.OsConstants.IPPROTO_TCP; import static android.system.OsConstants.IPPROTO_UDP; @@ -42,8 +44,9 @@ public class IpUtils { * payload) or ICMP checksum on the specified portion of a ByteBuffer. The seed * allows the checksum to commence with a specified value. */ - private static int checksum(ByteBuffer buf, int seed, int start, int end) { - int sum = seed; + @VisibleForTesting + public static int checksum(ByteBuffer buf, int seed, int start, int end) { + int sum = seed + 0xFFFF; // to make things work with empty / zero-filled buffer final int bufPosition = buf.position(); // set position of original ByteBuffer, so that the ShortBuffer @@ -69,13 +72,12 @@ public class IpUtils { b += 256; } - sum += b * 256; + sum += b * 256; // assumes bytebuffer is network order (ie. big endian) } - sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF); - sum = ((sum + ((sum >> 16) & 0xFFFF)) & 0xFFFF); - int negated = ~sum; - return intAbs((short) negated); + sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF); // max sum is 0x1FFFE + sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF); // max sum is 0xFFFF + return sum ^ 0xFFFF; // u16 bitwise negation } private static int pseudoChecksumIPv4( diff --git a/common/tests/unit/src/com/android/net/module/util/IpUtilsTest.java b/common/tests/unit/src/com/android/net/module/util/IpUtilsTest.java index 20555b35..d57023ca 100644 --- a/common/tests/unit/src/com/android/net/module/util/IpUtilsTest.java +++ b/common/tests/unit/src/com/android/net/module/util/IpUtilsTest.java @@ -74,6 +74,20 @@ public class IpUtilsTest { // print JavaPacketDefinition(str(packet)) @Test + public void testEmptyAndZeroBufferChecksum() throws Exception { + ByteBuffer packet = ByteBuffer.wrap(new byte[] { (byte) 0x00, (byte) 0x00, }); + // the following should *not* return 0xFFFF + assertEquals(0, IpUtils.checksum(packet, 0, 0, 0)); + assertEquals(0, IpUtils.checksum(packet, 0, 0, 1)); + assertEquals(0, IpUtils.checksum(packet, 0, 0, 2)); + assertEquals(0, IpUtils.checksum(packet, 0, 1, 2)); + assertEquals(0, IpUtils.checksum(packet, 0xFFFF, 0, 0)); + assertEquals(0, IpUtils.checksum(packet, 0xFFFF, 0, 1)); + assertEquals(0, IpUtils.checksum(packet, 0xFFFF, 0, 2)); + assertEquals(0, IpUtils.checksum(packet, 0xFFFF, 1, 2)); + } + + @Test public void testIpv6TcpChecksum() throws Exception { // packet = (scapy.IPv6(src="2001:db8::1", dst="2001:db8::2", tc=0x80) / // scapy.TCP(sport=12345, dport=7, |