Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 com.android.server.connectivity; |
| 18 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 19 | import static android.net.util.NetworkConstants.IPV4_HEADER_MIN_LEN; |
| 20 | import static android.net.util.NetworkConstants.UDP_HEADER_LEN; |
| 21 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 22 | import android.system.OsConstants; |
| 23 | import android.net.ConnectivityManager; |
| 24 | import android.net.NetworkUtils; |
| 25 | import android.net.util.IpUtils; |
| 26 | |
| 27 | import java.net.Inet4Address; |
| 28 | import java.net.Inet6Address; |
| 29 | import java.net.InetAddress; |
| 30 | import java.nio.ByteBuffer; |
| 31 | import java.nio.ByteOrder; |
| 32 | |
| 33 | import static android.net.ConnectivityManager.PacketKeepalive.*; |
| 34 | |
| 35 | /** |
| 36 | * Represents the actual packets that are sent by the |
| 37 | * {@link android.net.ConnectivityManager.PacketKeepalive} API. |
| 38 | * |
| 39 | * @hide |
| 40 | */ |
| 41 | public class KeepalivePacketData { |
| 42 | /** Protocol of the packet to send; one of the OsConstants.ETH_P_* values. */ |
| 43 | public final int protocol; |
| 44 | |
| 45 | /** Source IP address */ |
| 46 | public final InetAddress srcAddress; |
| 47 | |
| 48 | /** Destination IP address */ |
| 49 | public final InetAddress dstAddress; |
| 50 | |
| 51 | /** Source port */ |
| 52 | public final int srcPort; |
| 53 | |
| 54 | /** Destination port */ |
| 55 | public final int dstPort; |
| 56 | |
| 57 | /** Destination MAC address. Can change if routing changes. */ |
| 58 | public byte[] dstMac; |
| 59 | |
| 60 | /** Packet data. A raw byte string of packet data, not including the link-layer header. */ |
| 61 | public final byte[] data; |
| 62 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 63 | protected KeepalivePacketData(InetAddress srcAddress, int srcPort, |
| 64 | InetAddress dstAddress, int dstPort, byte[] data) throws InvalidPacketException { |
| 65 | this.srcAddress = srcAddress; |
| 66 | this.dstAddress = dstAddress; |
| 67 | this.srcPort = srcPort; |
| 68 | this.dstPort = dstPort; |
| 69 | this.data = data; |
| 70 | |
| 71 | // Check we have two IP addresses of the same family. |
| 72 | if (srcAddress == null || dstAddress == null || |
| 73 | !srcAddress.getClass().getName().equals(dstAddress.getClass().getName())) { |
Lorenzo Colitti | 9acca09 | 2015-09-08 13:44:23 +0900 | [diff] [blame] | 74 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Set the protocol. |
| 78 | if (this.dstAddress instanceof Inet4Address) { |
| 79 | this.protocol = OsConstants.ETH_P_IP; |
| 80 | } else if (this.dstAddress instanceof Inet6Address) { |
| 81 | this.protocol = OsConstants.ETH_P_IPV6; |
| 82 | } else { |
| 83 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 84 | } |
| 85 | |
| 86 | // Check the ports. |
| 87 | if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { |
| 88 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public static class InvalidPacketException extends Exception { |
| 93 | final public int error; |
| 94 | public InvalidPacketException(int error) { |
| 95 | this.error = error; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates an IPsec NAT-T keepalive packet with the specified parameters. |
| 101 | */ |
| 102 | public static KeepalivePacketData nattKeepalivePacket( |
| 103 | InetAddress srcAddress, int srcPort, |
| 104 | InetAddress dstAddress, int dstPort) throws InvalidPacketException { |
| 105 | |
Lorenzo Colitti | 9acca09 | 2015-09-08 13:44:23 +0900 | [diff] [blame] | 106 | if (!(srcAddress instanceof Inet4Address) || !(dstAddress instanceof Inet4Address)) { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 107 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 108 | } |
| 109 | |
| 110 | if (dstPort != NATT_PORT) { |
| 111 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 112 | } |
| 113 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 114 | int length = IPV4_HEADER_MIN_LEN + UDP_HEADER_LEN + 1; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 115 | ByteBuffer buf = ByteBuffer.allocate(length); |
| 116 | buf.order(ByteOrder.BIG_ENDIAN); |
| 117 | buf.putShort((short) 0x4500); // IP version and TOS |
| 118 | buf.putShort((short) length); |
| 119 | buf.putInt(0); // ID, flags, offset |
| 120 | buf.put((byte) 64); // TTL |
| 121 | buf.put((byte) OsConstants.IPPROTO_UDP); |
| 122 | int ipChecksumOffset = buf.position(); |
| 123 | buf.putShort((short) 0); // IP checksum |
| 124 | buf.put(srcAddress.getAddress()); |
| 125 | buf.put(dstAddress.getAddress()); |
| 126 | buf.putShort((short) srcPort); |
| 127 | buf.putShort((short) dstPort); |
| 128 | buf.putShort((short) (length - 20)); // UDP length |
| 129 | int udpChecksumOffset = buf.position(); |
| 130 | buf.putShort((short) 0); // UDP checksum |
| 131 | buf.put((byte) 0xff); // NAT-T keepalive |
| 132 | buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0)); |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 133 | buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_MIN_LEN)); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 134 | |
| 135 | return new KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, buf.array()); |
| 136 | } |
| 137 | } |