Set DF flag on NAT-T keepalive packet am: c6a2f6faf5

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2616795

Change-Id: I90864e1c4693bc30e4dcb5e6e1b39a8e933f3591
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
chiachangwang
2023-06-07 02:52:30 +00:00
committed by Automerger Merge Worker

View File

@@ -72,23 +72,26 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
Inet4Address srcAddress, int srcPort, Inet4Address dstAddress, int dstPort) Inet4Address srcAddress, int srcPort, Inet4Address dstAddress, int dstPort)
throws InvalidPacketException { throws InvalidPacketException {
int length = IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH + 1; int length = IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH + 1;
ByteBuffer buf = ByteBuffer.allocate(length); final ByteBuffer buf = ByteBuffer.allocate(length);
buf.order(ByteOrder.BIG_ENDIAN); buf.order(ByteOrder.BIG_ENDIAN);
buf.putShort((short) 0x4500); // IP version and TOS buf.putShort((short) 0x4500); // IP version and TOS
buf.putShort((short) length); buf.putShort((short) length);
buf.putInt(0); // ID, flags, offset buf.putShort((short) 0); // ID
buf.put((byte) 64); // TTL buf.putShort((short) 0x4000); // Flags(DF), offset
// Technically speaking, this should be reading/using the v4 sysctl
// /proc/sys/net/ipv4/ip_default_ttl. Use hard-coded 64 for simplicity.
buf.put((byte) 64); // TTL
buf.put((byte) OsConstants.IPPROTO_UDP); buf.put((byte) OsConstants.IPPROTO_UDP);
int ipChecksumOffset = buf.position(); int ipChecksumOffset = buf.position();
buf.putShort((short) 0); // IP checksum buf.putShort((short) 0); // IP checksum
buf.put(srcAddress.getAddress()); buf.put(srcAddress.getAddress());
buf.put(dstAddress.getAddress()); buf.put(dstAddress.getAddress());
buf.putShort((short) srcPort); buf.putShort((short) srcPort);
buf.putShort((short) dstPort); buf.putShort((short) dstPort);
buf.putShort((short) (length - 20)); // UDP length buf.putShort((short) (UDP_HEADER_LENGTH + 1)); // UDP length
int udpChecksumOffset = buf.position(); int udpChecksumOffset = buf.position();
buf.putShort((short) 0); // UDP checksum buf.putShort((short) 0); // UDP checksum
buf.put((byte) 0xff); // NAT-T keepalive buf.put((byte) 0xff); // NAT-T keepalive
buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0)); buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0));
buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_LENGTH)); buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_LENGTH));