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