Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.net; |
| 17 | |
Chiachang Wang | a034331 | 2021-01-12 18:48:13 +0800 | [diff] [blame] | 18 | import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 19 | |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 21 | import android.annotation.RequiresFeature; |
Nathan Harold | 025aae1 | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
Nathan Harold | 3a287d5 | 2018-04-25 12:01:34 -0700 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
Jeff Sharkey | ad1cebe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 24 | import android.annotation.SystemService; |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Jeff Sharkey | ad1cebe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 26 | import android.content.Context; |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
evitayan | 30878b0 | 2019-12-30 16:05:38 -0800 | [diff] [blame] | 28 | import android.net.annotations.PolicyDirection; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 29 | import android.os.Binder; |
Aaron Huang | 80a73fd | 2021-12-28 02:33:53 +0800 | [diff] [blame^] | 30 | import android.os.IBinder; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 32 | import android.os.RemoteException; |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 33 | import android.os.ServiceSpecificException; |
| 34 | import android.system.ErrnoException; |
| 35 | import android.system.OsConstants; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 36 | import android.util.AndroidException; |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Nathan Harold | 81f5463 | 2017-09-11 19:50:19 -0700 | [diff] [blame] | 38 | |
Nathan Harold | 19b99d9 | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
| 40 | |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 41 | import dalvik.system.CloseGuard; |
Nathan Harold | 81f5463 | 2017-09-11 19:50:19 -0700 | [diff] [blame] | 42 | |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 43 | import java.io.FileDescriptor; |
| 44 | import java.io.IOException; |
| 45 | import java.net.DatagramSocket; |
| 46 | import java.net.InetAddress; |
| 47 | import java.net.Socket; |
Aaron Huang | 49eef6f | 2021-12-22 16:35:13 +0800 | [diff] [blame] | 48 | import java.util.Objects; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 51 | * This class contains methods for managing IPsec sessions. Once configured, the kernel will apply |
| 52 | * confidentiality (encryption) and integrity (authentication) to IP traffic. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 53 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 54 | * <p>Note that not all aspects of IPsec are permitted by this API. Applications may create |
| 55 | * transport mode security associations and apply them to individual sockets. Applications looking |
Benedict Wong | c85b7b0 | 2019-11-12 22:31:51 -0800 | [diff] [blame] | 56 | * to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 57 | * |
| 58 | * @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 59 | * Internet Protocol</a> |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 60 | */ |
Jeff Sharkey | ad1cebe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 61 | @SystemService(Context.IPSEC_SERVICE) |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 62 | public final class IpSecManager { |
| 63 | private static final String TAG = "IpSecManager"; |
| 64 | |
| 65 | /** |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 66 | * Used when applying a transform to direct traffic through an {@link IpSecTransform} |
| 67 | * towards the host. |
| 68 | * |
| 69 | * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}. |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 70 | */ |
| 71 | public static final int DIRECTION_IN = 0; |
| 72 | |
| 73 | /** |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 74 | * Used when applying a transform to direct traffic through an {@link IpSecTransform} |
| 75 | * away from the host. |
| 76 | * |
| 77 | * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}. |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 78 | */ |
| 79 | public static final int DIRECTION_OUT = 1; |
| 80 | |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 81 | /** |
Benedict Wong | 908d34e | 2021-04-15 11:59:16 -0700 | [diff] [blame] | 82 | * Used when applying a transform to direct traffic through an {@link IpSecTransform} for |
| 83 | * forwarding between interfaces. |
| 84 | * |
| 85 | * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}. |
| 86 | * |
| 87 | * @hide |
| 88 | */ |
Aaron Huang | 4989e95 | 2021-12-15 23:27:18 +0800 | [diff] [blame] | 89 | @SystemApi(client = MODULE_LIBRARIES) |
Benedict Wong | 908d34e | 2021-04-15 11:59:16 -0700 | [diff] [blame] | 90 | public static final int DIRECTION_FWD = 2; |
| 91 | |
| 92 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 93 | * The Security Parameter Index (SPI) 0 indicates an unknown or invalid index. |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 94 | * |
| 95 | * <p>No IPsec packet may contain an SPI of 0. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 96 | * |
| 97 | * @hide |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 98 | */ |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 99 | @TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 100 | |
| 101 | /** @hide */ |
| 102 | public interface Status { |
Aaron Huang | fbae308 | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 103 | int OK = 0; |
| 104 | int RESOURCE_UNAVAILABLE = 1; |
| 105 | int SPI_UNAVAILABLE = 2; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** @hide */ |
Nathan Harold | 5e85446 | 2017-12-13 18:51:35 -0800 | [diff] [blame] | 109 | public static final int INVALID_RESOURCE_ID = -1; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 112 | * Thrown to indicate that a requested SPI is in use. |
| 113 | * |
| 114 | * <p>The combination of remote {@code InetAddress} and SPI must be unique across all apps on |
| 115 | * one device. If this error is encountered, a new SPI is required before a transform may be |
| 116 | * created. This error can be avoided by calling {@link |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 117 | * IpSecManager#allocateSecurityParameterIndex}. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 118 | */ |
| 119 | public static final class SpiUnavailableException extends AndroidException { |
| 120 | private final int mSpi; |
| 121 | |
| 122 | /** |
| 123 | * Construct an exception indicating that a transform with the given SPI is already in use |
| 124 | * or otherwise unavailable. |
| 125 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 126 | * @param msg description indicating the colliding SPI |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 127 | * @param spi the SPI that could not be used due to a collision |
| 128 | */ |
| 129 | SpiUnavailableException(String msg, int spi) { |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 130 | super(msg + " (spi: " + spi + ")"); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 131 | mSpi = spi; |
| 132 | } |
| 133 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 134 | /** Get the SPI that caused a collision. */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 135 | public int getSpi() { |
| 136 | return mSpi; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 141 | * Thrown to indicate that an IPsec resource is unavailable. |
| 142 | * |
| 143 | * <p>This could apply to resources such as sockets, {@link SecurityParameterIndex}, {@link |
| 144 | * IpSecTransform}, or other system resources. If this exception is thrown, users should release |
| 145 | * allocated objects of the type requested. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 146 | */ |
| 147 | public static final class ResourceUnavailableException extends AndroidException { |
| 148 | |
| 149 | ResourceUnavailableException(String msg) { |
| 150 | super(msg); |
| 151 | } |
| 152 | } |
| 153 | |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 154 | private final Context mContext; |
Nathan Harold | d2a1dad | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 155 | private final IIpSecService mService; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 156 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 157 | /** |
| 158 | * This class represents a reserved SPI. |
| 159 | * |
| 160 | * <p>Objects of this type are used to track reserved security parameter indices. They can be |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 161 | * obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 162 | * by calling {@link #close()} when they are no longer needed. |
| 163 | */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 164 | public static final class SecurityParameterIndex implements AutoCloseable { |
Nathan Harold | d2a1dad | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 165 | private final IIpSecService mService; |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 166 | private final InetAddress mDestinationAddress; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 167 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 168 | private int mSpi = INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 5e85446 | 2017-12-13 18:51:35 -0800 | [diff] [blame] | 169 | private int mResourceId = INVALID_RESOURCE_ID; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 170 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 171 | /** Get the underlying SPI held by this object. */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 172 | public int getSpi() { |
| 173 | return mSpi; |
| 174 | } |
| 175 | |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Release an SPI that was previously reserved. |
| 178 | * |
Nathan Harold | 909d66e | 2017-03-29 10:47:59 -0700 | [diff] [blame] | 179 | * <p>Release an SPI for use by other users in the system. If a SecurityParameterIndex is |
| 180 | * applied to an IpSecTransform, it will become unusable for future transforms but should |
| 181 | * still be closed to ensure system resources are released. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 182 | */ |
| 183 | @Override |
| 184 | public void close() { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 185 | try { |
| 186 | mService.releaseSecurityParameterIndex(mResourceId); |
| 187 | } catch (RemoteException e) { |
| 188 | throw e.rethrowFromSystemServer(); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 189 | } catch (Exception e) { |
| 190 | // On close we swallow all random exceptions since failure to close is not |
| 191 | // actionable by the user. |
| 192 | Log.e(TAG, "Failed to close " + this + ", Exception=" + e); |
| 193 | } finally { |
| 194 | mResourceId = INVALID_RESOURCE_ID; |
| 195 | mCloseGuard.close(); |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 196 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 199 | /** Check that the SPI was closed properly. */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 200 | @Override |
Nathan Harold | 2a30daf | 2017-11-07 17:17:45 -0800 | [diff] [blame] | 201 | protected void finalize() throws Throwable { |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 202 | if (mCloseGuard != null) { |
| 203 | mCloseGuard.warnIfOpen(); |
| 204 | } |
| 205 | |
| 206 | close(); |
| 207 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 208 | |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 209 | private SecurityParameterIndex( |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 210 | @NonNull IIpSecService service, InetAddress destinationAddress, int spi) |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 211 | throws ResourceUnavailableException, SpiUnavailableException { |
| 212 | mService = service; |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 213 | mDestinationAddress = destinationAddress; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 214 | try { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 215 | IpSecSpiResponse result = |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 216 | mService.allocateSecurityParameterIndex( |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 217 | destinationAddress.getHostAddress(), spi, new Binder()); |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 218 | |
| 219 | if (result == null) { |
| 220 | throw new NullPointerException("Received null response from IpSecService"); |
| 221 | } |
| 222 | |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 223 | int status = result.status; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 224 | switch (status) { |
| 225 | case Status.OK: |
| 226 | break; |
| 227 | case Status.RESOURCE_UNAVAILABLE: |
| 228 | throw new ResourceUnavailableException( |
| 229 | "No more SPIs may be allocated by this requester."); |
| 230 | case Status.SPI_UNAVAILABLE: |
| 231 | throw new SpiUnavailableException("Requested SPI is unavailable", spi); |
| 232 | default: |
| 233 | throw new RuntimeException( |
| 234 | "Unknown status returned by IpSecService: " + status); |
| 235 | } |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 236 | mSpi = result.spi; |
| 237 | mResourceId = result.resourceId; |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 238 | |
| 239 | if (mSpi == INVALID_SECURITY_PARAMETER_INDEX) { |
| 240 | throw new RuntimeException("Invalid SPI returned by IpSecService: " + status); |
| 241 | } |
| 242 | |
| 243 | if (mResourceId == INVALID_RESOURCE_ID) { |
| 244 | throw new RuntimeException( |
| 245 | "Invalid Resource ID returned by IpSecService: " + status); |
| 246 | } |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 247 | } catch (RemoteException e) { |
| 248 | throw e.rethrowFromSystemServer(); |
| 249 | } |
| 250 | mCloseGuard.open("open"); |
| 251 | } |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 252 | |
| 253 | /** @hide */ |
Nathan Harold | 19b99d9 | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 254 | @VisibleForTesting |
| 255 | public int getResourceId() { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 256 | return mResourceId; |
| 257 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 258 | |
| 259 | @Override |
| 260 | public String toString() { |
| 261 | return new StringBuilder() |
| 262 | .append("SecurityParameterIndex{spi=") |
| 263 | .append(mSpi) |
| 264 | .append(",resourceId=") |
| 265 | .append(mResourceId) |
| 266 | .append("}") |
| 267 | .toString(); |
| 268 | } |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 269 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 270 | |
| 271 | /** |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 272 | * Reserve a random SPI for traffic bound to or from the specified destination address. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 273 | * |
| 274 | * <p>If successful, this SPI is guaranteed available until released by a call to {@link |
| 275 | * SecurityParameterIndex#close()}. |
| 276 | * |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 277 | * @param destinationAddress the destination address for traffic bearing the requested SPI. |
| 278 | * For inbound traffic, the destination should be an address currently assigned on-device. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 279 | * @return the reserved SecurityParameterIndex |
Aaron Huang | fbae308 | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 280 | * @throws ResourceUnavailableException indicating that too many SPIs are |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 281 | * currently allocated for this user |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 282 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 283 | @NonNull |
| 284 | public SecurityParameterIndex allocateSecurityParameterIndex( |
| 285 | @NonNull InetAddress destinationAddress) throws ResourceUnavailableException { |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 286 | try { |
| 287 | return new SecurityParameterIndex( |
| 288 | mService, |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 289 | destinationAddress, |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 290 | IpSecManager.INVALID_SECURITY_PARAMETER_INDEX); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 291 | } catch (ServiceSpecificException e) { |
| 292 | throw rethrowUncheckedExceptionFromServiceSpecificException(e); |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 293 | } catch (SpiUnavailableException unlikely) { |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 294 | // Because this function allocates a totally random SPI, it really shouldn't ever |
| 295 | // fail to allocate an SPI; we simply need this because the exception is checked. |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 296 | throw new ResourceUnavailableException("No SPIs available"); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 301 | * Reserve the requested SPI for traffic bound to or from the specified destination address. |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 302 | * |
| 303 | * <p>If successful, this SPI is guaranteed available until released by a call to {@link |
| 304 | * SecurityParameterIndex#close()}. |
| 305 | * |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 306 | * @param destinationAddress the destination address for traffic bearing the requested SPI. |
| 307 | * For inbound traffic, the destination should be an address currently assigned on-device. |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 308 | * @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See |
| 309 | * RFC 4303 Section 2.1. |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 310 | * @return the reserved SecurityParameterIndex |
Aaron Huang | fbae308 | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 311 | * @throws ResourceUnavailableException indicating that too many SPIs are |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 312 | * currently allocated for this user |
Aaron Huang | fbae308 | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 313 | * @throws SpiUnavailableException indicating that the requested SPI could not be |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 314 | * reserved |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 315 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 316 | @NonNull |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 317 | public SecurityParameterIndex allocateSecurityParameterIndex( |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 318 | @NonNull InetAddress destinationAddress, int requestedSpi) |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 319 | throws SpiUnavailableException, ResourceUnavailableException { |
Nathan Harold | 0540679 | 2017-04-06 18:16:28 -0700 | [diff] [blame] | 320 | if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) { |
| 321 | throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI"); |
| 322 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 323 | try { |
| 324 | return new SecurityParameterIndex(mService, destinationAddress, requestedSpi); |
| 325 | } catch (ServiceSpecificException e) { |
| 326 | throw rethrowUncheckedExceptionFromServiceSpecificException(e); |
| 327 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 331 | * Apply an IPsec transform to a stream socket. |
| 332 | * |
| 333 | * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the |
| 334 | * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 335 | * the transform is removed from the socket by calling {@link #removeTransportModeTransforms}, |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 336 | * unprotected traffic can resume on that socket. |
| 337 | * |
| 338 | * <p>For security reasons, the destination address of any traffic on the socket must match the |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 339 | * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 340 | * other IP address will result in an IOException. In addition, reads and writes on the socket |
| 341 | * will throw IOException if the user deactivates the transform (by calling {@link |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 342 | * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 343 | * |
Benedict Wong | c5106f3 | 2018-03-14 19:01:14 -0700 | [diff] [blame] | 344 | * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an |
| 345 | * applied transform before completion of graceful shutdown may result in the shutdown sequence |
| 346 | * failing to complete. As such, applications requiring graceful shutdown MUST close the socket |
| 347 | * prior to deactivating the applied transform. Socket closure may be performed asynchronously |
| 348 | * (in batches), so the returning of a close function does not guarantee shutdown of a socket. |
| 349 | * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is |
| 350 | * sufficient to ensure shutdown. |
| 351 | * |
| 352 | * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}), |
| 353 | * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST] |
| 354 | * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the |
| 355 | * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped. |
| 356 | * |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 357 | * <h4>Rekey Procedure</h4> |
| 358 | * |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 359 | * <p>When applying a new tranform to a socket in the outbound direction, the previous transform |
| 360 | * will be removed and the new transform will take effect immediately, sending all traffic on |
| 361 | * the new transform; however, when applying a transform in the inbound direction, traffic |
| 362 | * on the old transform will continue to be decrypted and delivered until that transform is |
| 363 | * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey |
| 364 | * procedures where both transforms are valid until both endpoints are using the new transform |
| 365 | * and all in-flight packets have been received. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 366 | * |
| 367 | * @param socket a stream socket |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 368 | * @param direction the direction in which the transform should be applied |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 369 | * @param transform a transport mode {@code IpSecTransform} |
| 370 | * @throws IOException indicating that the transform could not be applied |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 371 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 372 | public void applyTransportModeTransform(@NonNull Socket socket, |
| 373 | @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException { |
Benedict Wong | 57ab3f6 | 2018-04-02 18:12:34 -0700 | [diff] [blame] | 374 | // Ensure creation of FD. See b/77548890 for more details. |
| 375 | socket.getSoLinger(); |
| 376 | |
Nathan Harold | 3167625 | 2018-01-16 12:08:43 -0800 | [diff] [blame] | 377 | applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 381 | * Apply an IPsec transform to a datagram socket. |
| 382 | * |
| 383 | * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the |
| 384 | * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 385 | * the transform is removed from the socket by calling {@link #removeTransportModeTransforms}, |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 386 | * unprotected traffic can resume on that socket. |
| 387 | * |
| 388 | * <p>For security reasons, the destination address of any traffic on the socket must match the |
| 389 | * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any |
| 390 | * other IP address will result in an IOException. In addition, reads and writes on the socket |
| 391 | * will throw IOException if the user deactivates the transform (by calling {@link |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 392 | * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 393 | * |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 394 | * <h4>Rekey Procedure</h4> |
| 395 | * |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 396 | * <p>When applying a new tranform to a socket in the outbound direction, the previous transform |
| 397 | * will be removed and the new transform will take effect immediately, sending all traffic on |
| 398 | * the new transform; however, when applying a transform in the inbound direction, traffic |
| 399 | * on the old transform will continue to be decrypted and delivered until that transform is |
| 400 | * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey |
| 401 | * procedures where both transforms are valid until both endpoints are using the new transform |
| 402 | * and all in-flight packets have been received. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 403 | * |
| 404 | * @param socket a datagram socket |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 405 | * @param direction the direction in which the transform should be applied |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 406 | * @param transform a transport mode {@code IpSecTransform} |
| 407 | * @throws IOException indicating that the transform could not be applied |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 408 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 409 | public void applyTransportModeTransform(@NonNull DatagramSocket socket, |
| 410 | @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException { |
Nathan Harold | 3167625 | 2018-01-16 12:08:43 -0800 | [diff] [blame] | 411 | applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform); |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 412 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 413 | |
| 414 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 415 | * Apply an IPsec transform to a socket. |
| 416 | * |
| 417 | * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the |
| 418 | * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 419 | * the transform is removed from the socket by calling {@link #removeTransportModeTransforms}, |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 420 | * unprotected traffic can resume on that socket. |
| 421 | * |
| 422 | * <p>For security reasons, the destination address of any traffic on the socket must match the |
| 423 | * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any |
| 424 | * other IP address will result in an IOException. In addition, reads and writes on the socket |
| 425 | * will throw IOException if the user deactivates the transform (by calling {@link |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 426 | * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 427 | * |
Benedict Wong | c5106f3 | 2018-03-14 19:01:14 -0700 | [diff] [blame] | 428 | * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an |
| 429 | * applied transform before completion of graceful shutdown may result in the shutdown sequence |
| 430 | * failing to complete. As such, applications requiring graceful shutdown MUST close the socket |
| 431 | * prior to deactivating the applied transform. Socket closure may be performed asynchronously |
| 432 | * (in batches), so the returning of a close function does not guarantee shutdown of a socket. |
| 433 | * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is |
| 434 | * sufficient to ensure shutdown. |
| 435 | * |
| 436 | * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}), |
| 437 | * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST] |
| 438 | * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the |
| 439 | * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped. |
| 440 | * |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 441 | * <h4>Rekey Procedure</h4> |
| 442 | * |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 443 | * <p>When applying a new tranform to a socket in the outbound direction, the previous transform |
| 444 | * will be removed and the new transform will take effect immediately, sending all traffic on |
| 445 | * the new transform; however, when applying a transform in the inbound direction, traffic |
| 446 | * on the old transform will continue to be decrypted and delivered until that transform is |
| 447 | * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey |
| 448 | * procedures where both transforms are valid until both endpoints are using the new transform |
| 449 | * and all in-flight packets have been received. |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 450 | * |
| 451 | * @param socket a socket file descriptor |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 452 | * @param direction the direction in which the transform should be applied |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 453 | * @param transform a transport mode {@code IpSecTransform} |
| 454 | * @throws IOException indicating that the transform could not be applied |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 455 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 456 | public void applyTransportModeTransform(@NonNull FileDescriptor socket, |
| 457 | @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 458 | // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor() |
Nathan Harold | 3167625 | 2018-01-16 12:08:43 -0800 | [diff] [blame] | 459 | // constructor takes control and closes the user's FD when we exit the method. |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 460 | try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) { |
Nathan Harold | 5a19b95 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 461 | mService.applyTransportModeTransform(pfd, direction, transform.getResourceId()); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 462 | } catch (ServiceSpecificException e) { |
| 463 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 464 | } catch (RemoteException e) { |
| 465 | throw e.rethrowFromSystemServer(); |
| 466 | } |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 470 | * Remove an IPsec transform from a stream socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 471 | * |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 472 | * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a |
| 473 | * socket allows the socket to be reused for communication in the clear. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 474 | * |
| 475 | * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling |
| 476 | * {@link IpSecTransform#close()}, then communication on the socket will fail until this method |
| 477 | * is called. |
| 478 | * |
| 479 | * @param socket a socket that previously had a transform applied to it |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 480 | * @throws IOException indicating that the transform could not be removed from the socket |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 481 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 482 | public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException { |
Benedict Wong | 57ab3f6 | 2018-04-02 18:12:34 -0700 | [diff] [blame] | 483 | // Ensure creation of FD. See b/77548890 for more details. |
| 484 | socket.getSoLinger(); |
| 485 | |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 486 | removeTransportModeTransforms(socket.getFileDescriptor$()); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 490 | * Remove an IPsec transform from a datagram socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 491 | * |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 492 | * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a |
| 493 | * socket allows the socket to be reused for communication in the clear. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 494 | * |
| 495 | * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling |
| 496 | * {@link IpSecTransform#close()}, then communication on the socket will fail until this method |
| 497 | * is called. |
| 498 | * |
| 499 | * @param socket a socket that previously had a transform applied to it |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 500 | * @throws IOException indicating that the transform could not be removed from the socket |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 501 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 502 | public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException { |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 503 | removeTransportModeTransforms(socket.getFileDescriptor$()); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 506 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 507 | * Remove an IPsec transform from a socket. |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 508 | * |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 509 | * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a |
| 510 | * socket allows the socket to be reused for communication in the clear. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 511 | * |
| 512 | * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling |
| 513 | * {@link IpSecTransform#close()}, then communication on the socket will fail until this method |
| 514 | * is called. |
| 515 | * |
| 516 | * @param socket a socket that previously had a transform applied to it |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 517 | * @throws IOException indicating that the transform could not be removed from the socket |
Nathan Harold | 0072e19 | 2017-04-06 17:46:00 -0700 | [diff] [blame] | 518 | */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 519 | public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 520 | try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) { |
Nathan Harold | 0d483b7 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 521 | mService.removeTransportModeTransforms(pfd); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 522 | } catch (ServiceSpecificException e) { |
| 523 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 524 | } catch (RemoteException e) { |
| 525 | throw e.rethrowFromSystemServer(); |
| 526 | } |
| 527 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * Remove a Tunnel Mode IPsec Transform from a {@link Network}. This must be used as part of |
| 531 | * cleanup if a tunneled Network experiences a change in default route. The Network will drop |
| 532 | * all traffic that cannot be routed to the Tunnel's outbound interface. If that interface is |
| 533 | * lost, all traffic will drop. |
| 534 | * |
Jonathan Basseri | 20e96c5 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 535 | * <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked. |
| 536 | * |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 537 | * @param net a network that currently has transform applied to it. |
| 538 | * @param transform a Tunnel Mode IPsec Transform that has been previously applied to the given |
| 539 | * network |
| 540 | * @hide |
| 541 | */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 542 | public void removeTunnelModeTransform(Network net, IpSecTransform transform) {} |
| 543 | |
| 544 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 545 | * This class provides access to a UDP encapsulation Socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 546 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 547 | * <p>{@code UdpEncapsulationSocket} wraps a system-provided datagram socket intended for IKEv2 |
| 548 | * signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link |
| 549 | * IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the |
| 550 | * caller. The caller should not close the {@code FileDescriptor} returned by {@link |
Benedict Wong | a386e37 | 2018-03-27 16:55:48 -0700 | [diff] [blame] | 551 | * #getFileDescriptor}, but should use {@link #close} instead. |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 552 | * |
| 553 | * <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic |
| 554 | * of the next user who binds to that port. To prevent this scenario, these sockets are held |
| 555 | * open by the system so that they may only be closed by calling {@link #close} or when the user |
| 556 | * process exits. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 557 | */ |
| 558 | public static final class UdpEncapsulationSocket implements AutoCloseable { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 559 | private final ParcelFileDescriptor mPfd; |
Nathan Harold | d2a1dad | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 560 | private final IIpSecService mService; |
Nathan Harold | 5e85446 | 2017-12-13 18:51:35 -0800 | [diff] [blame] | 561 | private int mResourceId = INVALID_RESOURCE_ID; |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 562 | private final int mPort; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 563 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 564 | |
Nathan Harold | 031acb8 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 565 | private UdpEncapsulationSocket(@NonNull IIpSecService service, int port) |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 566 | throws ResourceUnavailableException, IOException { |
Nathan Harold | d2a1dad | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 567 | mService = service; |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 568 | try { |
| 569 | IpSecUdpEncapResponse result = |
| 570 | mService.openUdpEncapsulationSocket(port, new Binder()); |
| 571 | switch (result.status) { |
| 572 | case Status.OK: |
| 573 | break; |
| 574 | case Status.RESOURCE_UNAVAILABLE: |
| 575 | throw new ResourceUnavailableException( |
| 576 | "No more Sockets may be allocated by this requester."); |
| 577 | default: |
| 578 | throw new RuntimeException( |
| 579 | "Unknown status returned by IpSecService: " + result.status); |
| 580 | } |
| 581 | mResourceId = result.resourceId; |
| 582 | mPort = result.port; |
| 583 | mPfd = result.fileDescriptor; |
| 584 | } catch (RemoteException e) { |
| 585 | throw e.rethrowFromSystemServer(); |
| 586 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 587 | mCloseGuard.open("constructor"); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Benedict Wong | a386e37 | 2018-03-27 16:55:48 -0700 | [diff] [blame] | 590 | /** Get the encapsulation socket's file descriptor. */ |
| 591 | public FileDescriptor getFileDescriptor() { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 592 | if (mPfd == null) { |
| 593 | return null; |
| 594 | } |
| 595 | return mPfd.getFileDescriptor(); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 598 | /** Get the bound port of the wrapped socket. */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 599 | public int getPort() { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 600 | return mPort; |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 603 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 604 | * Close this socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 605 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 606 | * <p>This closes the wrapped socket. Open encapsulation sockets count against a user's |
| 607 | * resource limits, and forgetting to close them eventually will result in {@link |
| 608 | * ResourceUnavailableException} being thrown. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 609 | */ |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 610 | @Override |
Nathan Harold | a64b019 | 2017-04-17 17:11:58 -0700 | [diff] [blame] | 611 | public void close() throws IOException { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 612 | try { |
| 613 | mService.closeUdpEncapsulationSocket(mResourceId); |
Nathan Harold | 5e85446 | 2017-12-13 18:51:35 -0800 | [diff] [blame] | 614 | mResourceId = INVALID_RESOURCE_ID; |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 615 | } catch (RemoteException e) { |
| 616 | throw e.rethrowFromSystemServer(); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 617 | } catch (Exception e) { |
| 618 | // On close we swallow all random exceptions since failure to close is not |
| 619 | // actionable by the user. |
| 620 | Log.e(TAG, "Failed to close " + this + ", Exception=" + e); |
| 621 | } finally { |
| 622 | mResourceId = INVALID_RESOURCE_ID; |
| 623 | mCloseGuard.close(); |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | try { |
| 627 | mPfd.close(); |
| 628 | } catch (IOException e) { |
| 629 | Log.e(TAG, "Failed to close UDP Encapsulation Socket with Port= " + mPort); |
| 630 | throw e; |
| 631 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 634 | /** Check that the socket was closed properly. */ |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 635 | @Override |
| 636 | protected void finalize() throws Throwable { |
| 637 | if (mCloseGuard != null) { |
| 638 | mCloseGuard.warnIfOpen(); |
| 639 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 640 | close(); |
| 641 | } |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 642 | |
| 643 | /** @hide */ |
Chiachang Wang | a034331 | 2021-01-12 18:48:13 +0800 | [diff] [blame] | 644 | @SystemApi(client = MODULE_LIBRARIES) |
Nathan Harold | 19b99d9 | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 645 | public int getResourceId() { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 646 | return mResourceId; |
| 647 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 648 | |
| 649 | @Override |
| 650 | public String toString() { |
| 651 | return new StringBuilder() |
| 652 | .append("UdpEncapsulationSocket{port=") |
| 653 | .append(mPort) |
| 654 | .append(",resourceId=") |
| 655 | .append(mResourceId) |
| 656 | .append("}") |
| 657 | .toString(); |
| 658 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 662 | * Open a socket for UDP encapsulation and bind to the given port. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 663 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 664 | * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 665 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 666 | * @param port a local UDP port |
| 667 | * @return a socket that is bound to the given port |
| 668 | * @throws IOException indicating that the socket could not be opened or bound |
| 669 | * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 670 | */ |
| 671 | // Returning a socket in this fashion that has been created and bound by the system |
| 672 | // is the only safe way to ensure that a socket is both accessible to the user and |
| 673 | // safely usable for Encapsulation without allowing a user to possibly unbind from/close |
| 674 | // the port, which could potentially impact the traffic of the next user who binds to that |
| 675 | // socket. |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 676 | @NonNull |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 677 | public UdpEncapsulationSocket openUdpEncapsulationSocket(int port) |
| 678 | throws IOException, ResourceUnavailableException { |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 679 | /* |
| 680 | * Most range checking is done in the service, but this version of the constructor expects |
| 681 | * a valid port number, and zero cannot be checked after being passed to the service. |
| 682 | */ |
| 683 | if (port == 0) { |
| 684 | throw new IllegalArgumentException("Specified port must be a valid port number!"); |
| 685 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 686 | try { |
| 687 | return new UdpEncapsulationSocket(mService, port); |
| 688 | } catch (ServiceSpecificException e) { |
| 689 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
| 690 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 694 | * Open a socket for UDP encapsulation. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 695 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 696 | * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 697 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 698 | * <p>The local port of the returned socket can be obtained by calling {@link |
| 699 | * UdpEncapsulationSocket#getPort()}. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 700 | * |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 701 | * @return a socket that is bound to a local port |
| 702 | * @throws IOException indicating that the socket could not be opened or bound |
| 703 | * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 704 | */ |
| 705 | // Returning a socket in this fashion that has been created and bound by the system |
| 706 | // is the only safe way to ensure that a socket is both accessible to the user and |
| 707 | // safely usable for Encapsulation without allowing a user to possibly unbind from/close |
| 708 | // the port, which could potentially impact the traffic of the next user who binds to that |
| 709 | // socket. |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 710 | @NonNull |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 711 | public UdpEncapsulationSocket openUdpEncapsulationSocket() |
| 712 | throws IOException, ResourceUnavailableException { |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 713 | try { |
| 714 | return new UdpEncapsulationSocket(mService, 0); |
| 715 | } catch (ServiceSpecificException e) { |
| 716 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
| 717 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /** |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 721 | * This class represents an IpSecTunnelInterface |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 722 | * |
| 723 | * <p>IpSecTunnelInterface objects track tunnel interfaces that serve as |
| 724 | * local endpoints for IPsec tunnels. |
| 725 | * |
| 726 | * <p>Creating an IpSecTunnelInterface creates a device to which IpSecTransforms may be |
| 727 | * applied to provide IPsec security to packets sent through the tunnel. While a tunnel |
| 728 | * cannot be used in standalone mode within Android, the higher layers may use the tunnel |
| 729 | * to create Network objects which are accessible to the Android system. |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 730 | * @hide |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 731 | */ |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 732 | @SystemApi |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 733 | public static final class IpSecTunnelInterface implements AutoCloseable { |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 734 | private final String mOpPackageName; |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 735 | private final IIpSecService mService; |
| 736 | private final InetAddress mRemoteAddress; |
| 737 | private final InetAddress mLocalAddress; |
| 738 | private final Network mUnderlyingNetwork; |
| 739 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 740 | private String mInterfaceName; |
| 741 | private int mResourceId = INVALID_RESOURCE_ID; |
| 742 | |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 743 | /** Get the underlying SPI held by this object. */ |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 744 | @NonNull |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 745 | public String getInterfaceName() { |
| 746 | return mInterfaceName; |
| 747 | } |
| 748 | |
| 749 | /** |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 750 | * Add an address to the IpSecTunnelInterface |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 751 | * |
| 752 | * <p>Add an address which may be used as the local inner address for |
| 753 | * tunneled traffic. |
| 754 | * |
| 755 | * @param address the local address for traffic inside the tunnel |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 756 | * @param prefixLen length of the InetAddress prefix |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 757 | * @hide |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 758 | */ |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 759 | @SystemApi |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 760 | @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
Nathan Harold | 00e7724 | 2018-03-21 15:32:42 -0700 | [diff] [blame] | 761 | @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 762 | public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException { |
Benedict Wong | 97c3c94 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 763 | try { |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 764 | mService.addAddressToTunnelInterface( |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 765 | mResourceId, new LinkAddress(address, prefixLen), mOpPackageName); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 766 | } catch (ServiceSpecificException e) { |
| 767 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
Benedict Wong | 97c3c94 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 768 | } catch (RemoteException e) { |
| 769 | throw e.rethrowFromSystemServer(); |
| 770 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 774 | * Remove an address from the IpSecTunnelInterface |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 775 | * |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 776 | * <p>Remove an address which was previously added to the IpSecTunnelInterface |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 777 | * |
| 778 | * @param address to be removed |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 779 | * @param prefixLen length of the InetAddress prefix |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 780 | * @hide |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 781 | */ |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 782 | @SystemApi |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 783 | @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
Nathan Harold | 00e7724 | 2018-03-21 15:32:42 -0700 | [diff] [blame] | 784 | @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 785 | public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException { |
Benedict Wong | 97c3c94 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 786 | try { |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 787 | mService.removeAddressFromTunnelInterface( |
Benedict Wong | d39837f | 2018-04-03 20:30:54 -0700 | [diff] [blame] | 788 | mResourceId, new LinkAddress(address, prefixLen), mOpPackageName); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 789 | } catch (ServiceSpecificException e) { |
| 790 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
Benedict Wong | 97c3c94 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 791 | } catch (RemoteException e) { |
| 792 | throw e.rethrowFromSystemServer(); |
| 793 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Yan Yan | a2f3b49 | 2020-09-29 23:38:00 -0700 | [diff] [blame] | 796 | /** |
| 797 | * Update the underlying network for this IpSecTunnelInterface. |
| 798 | * |
| 799 | * <p>This new underlying network will be used for all transforms applied AFTER this call is |
| 800 | * complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to |
| 801 | * this tunnel interface, traffic will still use the old SA, and be routed on the old |
| 802 | * underlying network. |
| 803 | * |
| 804 | * <p>To migrate IPsec tunnel mode traffic, a caller should: |
| 805 | * |
| 806 | * <ol> |
| 807 | * <li>Update the IpSecTunnelInterface’s underlying network. |
| 808 | * <li>Apply {@link IpSecTransform}(s) with matching addresses to this |
| 809 | * IpSecTunnelInterface. |
| 810 | * </ol> |
| 811 | * |
| 812 | * @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel. |
| 813 | * This network MUST never be the network exposing this IpSecTunnelInterface, otherwise |
Yan Yan | cf4d2eb | 2021-02-16 11:48:55 -0800 | [diff] [blame] | 814 | * this method will throw an {@link IllegalArgumentException}. If the |
| 815 | * IpSecTunnelInterface is later added to this network, all outbound traffic will be |
| 816 | * blackholed. |
Yan Yan | a2f3b49 | 2020-09-29 23:38:00 -0700 | [diff] [blame] | 817 | */ |
| 818 | // TODO: b/169171001 Update the documentation when transform migration is supported. |
| 819 | // The purpose of making updating network and applying transforms separate is to leave open |
| 820 | // the possibility to support lossless migration procedures. To do that, Android platform |
| 821 | // will need to support multiple inbound tunnel mode transforms, just like it can support |
| 822 | // multiple transport mode transforms. |
| 823 | @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 824 | @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) |
| 825 | public void setUnderlyingNetwork(@NonNull Network underlyingNetwork) throws IOException { |
| 826 | try { |
| 827 | mService.setNetworkForTunnelInterface( |
| 828 | mResourceId, underlyingNetwork, mOpPackageName); |
| 829 | } catch (RemoteException e) { |
| 830 | throw e.rethrowFromSystemServer(); |
| 831 | } |
| 832 | } |
| 833 | |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 834 | private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service, |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 835 | @NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress, |
| 836 | @NonNull Network underlyingNetwork) |
| 837 | throws ResourceUnavailableException, IOException { |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 838 | mOpPackageName = ctx.getOpPackageName(); |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 839 | mService = service; |
| 840 | mLocalAddress = localAddress; |
| 841 | mRemoteAddress = remoteAddress; |
| 842 | mUnderlyingNetwork = underlyingNetwork; |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 843 | |
| 844 | try { |
| 845 | IpSecTunnelInterfaceResponse result = |
| 846 | mService.createTunnelInterface( |
| 847 | localAddress.getHostAddress(), |
| 848 | remoteAddress.getHostAddress(), |
| 849 | underlyingNetwork, |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 850 | new Binder(), |
| 851 | mOpPackageName); |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 852 | switch (result.status) { |
| 853 | case Status.OK: |
| 854 | break; |
| 855 | case Status.RESOURCE_UNAVAILABLE: |
| 856 | throw new ResourceUnavailableException( |
| 857 | "No more tunnel interfaces may be allocated by this requester."); |
| 858 | default: |
| 859 | throw new RuntimeException( |
| 860 | "Unknown status returned by IpSecService: " + result.status); |
| 861 | } |
| 862 | mResourceId = result.resourceId; |
| 863 | mInterfaceName = result.interfaceName; |
| 864 | } catch (RemoteException e) { |
| 865 | throw e.rethrowFromSystemServer(); |
| 866 | } |
| 867 | mCloseGuard.open("constructor"); |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /** |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 871 | * Delete an IpSecTunnelInterface |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 872 | * |
| 873 | * <p>Calling close will deallocate the IpSecTunnelInterface and all of its system |
| 874 | * resources. Any packets bound for this interface either inbound or outbound will |
| 875 | * all be lost. |
| 876 | */ |
| 877 | @Override |
| 878 | public void close() { |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 879 | try { |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 880 | mService.deleteTunnelInterface(mResourceId, mOpPackageName); |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 881 | } catch (RemoteException e) { |
| 882 | throw e.rethrowFromSystemServer(); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 883 | } catch (Exception e) { |
| 884 | // On close we swallow all random exceptions since failure to close is not |
| 885 | // actionable by the user. |
| 886 | Log.e(TAG, "Failed to close " + this + ", Exception=" + e); |
| 887 | } finally { |
| 888 | mResourceId = INVALID_RESOURCE_ID; |
| 889 | mCloseGuard.close(); |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 890 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 893 | /** Check that the Interface was closed properly. */ |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 894 | @Override |
| 895 | protected void finalize() throws Throwable { |
| 896 | if (mCloseGuard != null) { |
| 897 | mCloseGuard.warnIfOpen(); |
| 898 | } |
| 899 | close(); |
| 900 | } |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 901 | |
| 902 | /** @hide */ |
| 903 | @VisibleForTesting |
| 904 | public int getResourceId() { |
| 905 | return mResourceId; |
| 906 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 907 | |
Aurimas Liutikas | 3a78d9d | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 908 | @NonNull |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 909 | @Override |
| 910 | public String toString() { |
| 911 | return new StringBuilder() |
| 912 | .append("IpSecTunnelInterface{ifname=") |
| 913 | .append(mInterfaceName) |
| 914 | .append(",resourceId=") |
| 915 | .append(mResourceId) |
| 916 | .append("}") |
| 917 | .toString(); |
| 918 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic. |
| 923 | * |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 924 | * <p>An application that creates tunnels is responsible for cleaning up the tunnel when the |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 925 | * underlying network goes away, and the onLost() callback is received. |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 926 | * |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 927 | * @param localAddress The local addres of the tunnel |
| 928 | * @param remoteAddress The local addres of the tunnel |
| 929 | * @param underlyingNetwork the {@link Network} that will carry traffic for this tunnel. |
| 930 | * This network should almost certainly be a network such as WiFi with an L2 address. |
| 931 | * @return a new {@link IpSecManager#IpSecTunnelInterface} with the specified properties |
| 932 | * @throws IOException indicating that the socket could not be opened or bound |
| 933 | * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open |
Yan Yan | 160aac3 | 2020-12-03 17:08:29 -0800 | [diff] [blame] | 934 | * @hide |
Yan Yan | 160aac3 | 2020-12-03 17:08:29 -0800 | [diff] [blame] | 935 | */ |
Nathan Harold | 3a287d5 | 2018-04-25 12:01:34 -0700 | [diff] [blame] | 936 | @SystemApi |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 937 | @NonNull |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 938 | @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
Nathan Harold | 00e7724 | 2018-03-21 15:32:42 -0700 | [diff] [blame] | 939 | @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 940 | public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress, |
| 941 | @NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork) |
| 942 | throws ResourceUnavailableException, IOException { |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 943 | try { |
| 944 | return new IpSecTunnelInterface( |
| 945 | mContext, mService, localAddress, remoteAddress, underlyingNetwork); |
| 946 | } catch (ServiceSpecificException e) { |
| 947 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
| 948 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /** |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 952 | * Apply an active Tunnel Mode IPsec Transform to a {@link IpSecTunnelInterface}, which will |
| 953 | * tunnel all traffic for the given direction through the underlying network's interface with |
| 954 | * IPsec (applies an outer IP header and IPsec Header to all traffic, and expects an additional |
| 955 | * IP header and IPsec Header on all inbound traffic). |
| 956 | * <p>Applications should probably not use this API directly. |
| 957 | * |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 958 | * |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 959 | * @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 960 | * transform. |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 961 | * @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 962 | * the transform will be used. |
| 963 | * @param transform an {@link IpSecTransform} created in tunnel mode |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 964 | * @throws IOException indicating that the transform could not be applied due to a lower |
| 965 | * layer failure. |
| 966 | * @hide |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 967 | */ |
Lorenzo Colitti | 388f416 | 2021-02-01 08:12:34 +0000 | [diff] [blame] | 968 | @SystemApi |
Benedict Wong | e976375 | 2018-11-08 19:45:34 -0800 | [diff] [blame] | 969 | @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
Nathan Harold | 00e7724 | 2018-03-21 15:32:42 -0700 | [diff] [blame] | 970 | @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) |
Nathan Harold | 6303d9e | 2018-03-16 17:27:30 -0700 | [diff] [blame] | 971 | public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel, |
| 972 | @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException { |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 973 | try { |
| 974 | mService.applyTunnelModeTransform( |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 975 | tunnel.getResourceId(), direction, |
| 976 | transform.getResourceId(), mContext.getOpPackageName()); |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 977 | } catch (ServiceSpecificException e) { |
| 978 | throw rethrowCheckedExceptionFromServiceSpecificException(e); |
Benedict Wong | 8bc9073 | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 979 | } catch (RemoteException e) { |
| 980 | throw e.rethrowFromSystemServer(); |
| 981 | } |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 982 | } |
Nathan Harold | e3536f0 | 2018-03-06 13:22:22 -0800 | [diff] [blame] | 983 | |
Nathan Harold | 67e7673 | 2018-01-17 16:09:24 -0800 | [diff] [blame] | 984 | /** |
Aaron Huang | 80a73fd | 2021-12-28 02:33:53 +0800 | [diff] [blame^] | 985 | * @hide |
| 986 | */ |
| 987 | public IpSecTransformResponse createTransform(IpSecConfig config, IBinder binder, |
| 988 | String callingPackage) { |
| 989 | try { |
| 990 | return mService.createTransform(config, binder, callingPackage); |
| 991 | } catch (RemoteException e) { |
| 992 | throw e.rethrowFromSystemServer(); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * @hide |
| 998 | */ |
| 999 | public void deleteTransform(int resourceId) { |
| 1000 | try { |
| 1001 | mService.deleteTransform(resourceId); |
| 1002 | } catch (RemoteException e) { |
| 1003 | throw e.rethrowFromSystemServer(); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /** |
Jonathan Basseri | 2725a23 | 2017-04-21 15:53:51 -0700 | [diff] [blame] | 1008 | * Construct an instance of IpSecManager within an application context. |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 1009 | * |
| 1010 | * @param context the application context for this manager |
| 1011 | * @hide |
| 1012 | */ |
Nathan Harold | 65ef843 | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1013 | public IpSecManager(Context ctx, IIpSecService service) { |
| 1014 | mContext = ctx; |
Aaron Huang | 49eef6f | 2021-12-22 16:35:13 +0800 | [diff] [blame] | 1015 | mService = Objects.requireNonNull(service, "missing service"); |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 1016 | } |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1017 | |
| 1018 | private static void maybeHandleServiceSpecificException(ServiceSpecificException sse) { |
| 1019 | // OsConstants are late binding, so switch statements can't be used. |
| 1020 | if (sse.errorCode == OsConstants.EINVAL) { |
| 1021 | throw new IllegalArgumentException(sse); |
| 1022 | } else if (sse.errorCode == OsConstants.EAGAIN) { |
| 1023 | throw new IllegalStateException(sse); |
Benedict Wong | c45974b | 2018-07-16 14:56:20 -0700 | [diff] [blame] | 1024 | } else if (sse.errorCode == OsConstants.EOPNOTSUPP |
| 1025 | || sse.errorCode == OsConstants.EPROTONOSUPPORT) { |
Nathan Harold | beed0b6 | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1026 | throw new UnsupportedOperationException(sse); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Convert an Errno SSE to the correct Unchecked exception type. |
| 1032 | * |
| 1033 | * This method never actually returns. |
| 1034 | */ |
| 1035 | // package |
| 1036 | static RuntimeException |
| 1037 | rethrowUncheckedExceptionFromServiceSpecificException(ServiceSpecificException sse) { |
| 1038 | maybeHandleServiceSpecificException(sse); |
| 1039 | throw new RuntimeException(sse); |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * Convert an Errno SSE to the correct Checked or Unchecked exception type. |
| 1044 | * |
| 1045 | * This method may throw IOException, or it may throw an unchecked exception; it will never |
| 1046 | * actually return. |
| 1047 | */ |
| 1048 | // package |
| 1049 | static IOException rethrowCheckedExceptionFromServiceSpecificException( |
| 1050 | ServiceSpecificException sse) throws IOException { |
| 1051 | // First see if this is an unchecked exception of a type we know. |
| 1052 | // If so, then we prefer the unchecked (specific) type of exception. |
| 1053 | maybeHandleServiceSpecificException(sse); |
| 1054 | // If not, then all we can do is provide the SSE in the form of an IOException. |
| 1055 | throw new ErrnoException( |
| 1056 | "IpSec encountered errno=" + sse.errorCode, sse.errorCode).rethrowAsIOException(); |
| 1057 | } |
Nathan Harold | b1f605f | 2017-01-12 18:38:57 -0800 | [diff] [blame] | 1058 | } |