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