junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 1 | /* |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 19 | import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS; |
| 20 | import static android.net.InvalidPacketException.ERROR_INVALID_PORT; |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 21 | |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Chiachang Wang | fd007f0 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
Aaron Huang | 9deb763 | 2019-04-23 22:17:16 +0800 | [diff] [blame] | 26 | import android.os.Parcelable; |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 27 | import android.system.OsConstants; |
| 28 | |
Remi NGUYEN VAN | 7e3efdb | 2020-09-24 18:31:55 +0900 | [diff] [blame] | 29 | import com.android.net.module.util.IpUtils; |
| 30 | |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 31 | import java.net.Inet4Address; |
| 32 | import java.net.InetAddress; |
| 33 | import java.nio.ByteBuffer; |
| 34 | import java.nio.ByteOrder; |
Chiachang Wang | fd007f0 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 35 | import java.util.Objects; |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 36 | |
| 37 | /** @hide */ |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 38 | @SystemApi |
Aaron Huang | 9deb763 | 2019-04-23 22:17:16 +0800 | [diff] [blame] | 39 | public final class NattKeepalivePacketData extends KeepalivePacketData implements Parcelable { |
Aaron Huang | 91caaee | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 40 | private static final int IPV4_HEADER_LENGTH = 20; |
| 41 | private static final int UDP_HEADER_LENGTH = 8; |
| 42 | |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 43 | // This should only be constructed via static factory methods, such as |
| 44 | // nattKeepalivePacket |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 45 | public NattKeepalivePacketData(@NonNull InetAddress srcAddress, int srcPort, |
| 46 | @NonNull InetAddress dstAddress, int dstPort, @NonNull byte[] data) throws |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 47 | InvalidPacketException { |
| 48 | super(srcAddress, srcPort, dstAddress, dstPort, data); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Factory method to create Nat-T keepalive packet structure. |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 53 | * @hide |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 54 | */ |
| 55 | public static NattKeepalivePacketData nattKeepalivePacket( |
| 56 | InetAddress srcAddress, int srcPort, InetAddress dstAddress, int dstPort) |
| 57 | throws InvalidPacketException { |
chiachangwang | df34744 | 2023-06-06 03:12:24 +0000 | [diff] [blame^] | 58 | if (dstPort != NattSocketKeepalive.NATT_PORT) { |
| 59 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 60 | } |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 61 | |
| 62 | if (!(srcAddress instanceof Inet4Address) || !(dstAddress instanceof Inet4Address)) { |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 63 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 64 | } |
| 65 | |
chiachangwang | df34744 | 2023-06-06 03:12:24 +0000 | [diff] [blame^] | 66 | return nattKeepalivePacketv4( |
| 67 | (Inet4Address) srcAddress, srcPort, |
| 68 | (Inet4Address) dstAddress, dstPort); |
| 69 | } |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 70 | |
chiachangwang | df34744 | 2023-06-06 03:12:24 +0000 | [diff] [blame^] | 71 | private static NattKeepalivePacketData nattKeepalivePacketv4( |
| 72 | Inet4Address srcAddress, int srcPort, Inet4Address dstAddress, int dstPort) |
| 73 | throws InvalidPacketException { |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 74 | int length = IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH + 1; |
| 75 | ByteBuffer buf = ByteBuffer.allocate(length); |
| 76 | buf.order(ByteOrder.BIG_ENDIAN); |
| 77 | buf.putShort((short) 0x4500); // IP version and TOS |
| 78 | buf.putShort((short) length); |
| 79 | buf.putInt(0); // ID, flags, offset |
| 80 | buf.put((byte) 64); // TTL |
| 81 | buf.put((byte) OsConstants.IPPROTO_UDP); |
| 82 | int ipChecksumOffset = buf.position(); |
| 83 | buf.putShort((short) 0); // IP checksum |
| 84 | buf.put(srcAddress.getAddress()); |
| 85 | buf.put(dstAddress.getAddress()); |
| 86 | buf.putShort((short) srcPort); |
| 87 | buf.putShort((short) dstPort); |
| 88 | buf.putShort((short) (length - 20)); // UDP length |
| 89 | int udpChecksumOffset = buf.position(); |
| 90 | buf.putShort((short) 0); // UDP checksum |
| 91 | buf.put((byte) 0xff); // NAT-T keepalive |
| 92 | buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0)); |
| 93 | buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_LENGTH)); |
| 94 | |
| 95 | return new NattKeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, buf.array()); |
| 96 | } |
Aaron Huang | 9deb763 | 2019-04-23 22:17:16 +0800 | [diff] [blame] | 97 | |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 98 | /** Parcelable Implementation */ |
| 99 | public int describeContents() { |
| 100 | return 0; |
Aaron Huang | 9deb763 | 2019-04-23 22:17:16 +0800 | [diff] [blame] | 101 | } |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 102 | |
| 103 | /** Write to parcel */ |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 104 | public void writeToParcel(@NonNull Parcel out, int flags) { |
Aaron Huang | 60011ce | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 105 | out.writeString(getSrcAddress().getHostAddress()); |
| 106 | out.writeString(getDstAddress().getHostAddress()); |
| 107 | out.writeInt(getSrcPort()); |
| 108 | out.writeInt(getDstPort()); |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** Parcelable Creator */ |
Aaron Huang | 5dcbfa8 | 2020-01-09 22:04:21 +0800 | [diff] [blame] | 112 | public static final @NonNull Parcelable.Creator<NattKeepalivePacketData> CREATOR = |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 113 | new Parcelable.Creator<NattKeepalivePacketData>() { |
| 114 | public NattKeepalivePacketData createFromParcel(Parcel in) { |
| 115 | final InetAddress srcAddress = |
| 116 | InetAddresses.parseNumericAddress(in.readString()); |
| 117 | final InetAddress dstAddress = |
| 118 | InetAddresses.parseNumericAddress(in.readString()); |
| 119 | final int srcPort = in.readInt(); |
| 120 | final int dstPort = in.readInt(); |
| 121 | try { |
| 122 | return NattKeepalivePacketData.nattKeepalivePacket(srcAddress, srcPort, |
| 123 | dstAddress, dstPort); |
| 124 | } catch (InvalidPacketException e) { |
| 125 | throw new IllegalArgumentException( |
Aaron Huang | 60011ce | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 126 | "Invalid NAT-T keepalive data: " + e.getError()); |
junyulai | 6b7cf0f | 2019-06-04 05:26:38 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | public NattKeepalivePacketData[] newArray(int size) { |
| 131 | return new NattKeepalivePacketData[size]; |
| 132 | } |
| 133 | }; |
Chiachang Wang | fd007f0 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 134 | |
| 135 | @Override |
| 136 | public boolean equals(@Nullable final Object o) { |
| 137 | if (!(o instanceof NattKeepalivePacketData)) return false; |
| 138 | final NattKeepalivePacketData other = (NattKeepalivePacketData) o; |
Aaron Huang | 60011ce | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 139 | final InetAddress srcAddress = getSrcAddress(); |
| 140 | final InetAddress dstAddress = getDstAddress(); |
| 141 | return srcAddress.equals(other.getSrcAddress()) |
| 142 | && dstAddress.equals(other.getDstAddress()) |
| 143 | && getSrcPort() == other.getSrcPort() |
| 144 | && getDstPort() == other.getDstPort(); |
Chiachang Wang | fd007f0 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public int hashCode() { |
Aaron Huang | 60011ce | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 149 | return Objects.hash(getSrcAddress(), getDstAddress(), getSrcPort(), getDstPort()); |
Chiachang Wang | fd007f0 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 150 | } |
junyulai | 3a5caf8 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 151 | } |