blob: 6013769191c5ff23404d9f592da022d5fa01e4e0 [file] [log] [blame]
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001/*
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 */
16package android.net;
17
Chiachang Wanga0343312021-01-12 18:48:13 +080018import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
19
Aaron Huangca9c1df2021-12-16 00:11:15 +080020import android.annotation.IntDef;
Nathan Harold031acb82017-03-07 13:23:36 -080021import android.annotation.NonNull;
Benedict Wonge9763752018-11-08 19:45:34 -080022import android.annotation.RequiresFeature;
Nathan Harold025aae12018-02-02 18:34:25 -080023import android.annotation.RequiresPermission;
Nathan Harold3a287d52018-04-25 12:01:34 -070024import android.annotation.SystemApi;
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060025import android.annotation.SystemService;
Jonathan Basseri2725a232017-04-21 15:53:51 -070026import android.annotation.TestApi;
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060027import android.content.Context;
Benedict Wonge9763752018-11-08 19:45:34 -080028import android.content.pm.PackageManager;
Nathan Harold031acb82017-03-07 13:23:36 -080029import android.os.Binder;
Aaron Huang80a73fd2021-12-28 02:33:53 +080030import android.os.IBinder;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080031import android.os.ParcelFileDescriptor;
Nathan Harold031acb82017-03-07 13:23:36 -080032import android.os.RemoteException;
Nathan Haroldbeed0b62018-04-03 16:13:19 -070033import android.os.ServiceSpecificException;
34import android.system.ErrnoException;
35import android.system.OsConstants;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080036import android.util.AndroidException;
Nathan Harold80865392017-04-04 19:37:48 -070037import android.util.Log;
Nathan Harold81f54632017-09-11 19:50:19 -070038
Nathan Harold19b99d92017-08-23 13:46:33 -070039import com.android.internal.annotations.VisibleForTesting;
Yan Yane114b382021-02-16 16:29:48 -080040import com.android.modules.utils.build.SdkLevel;
Nathan Harold19b99d92017-08-23 13:46:33 -070041
Nathan Haroldb1f605f2017-01-12 18:38:57 -080042import dalvik.system.CloseGuard;
Nathan Harold81f54632017-09-11 19:50:19 -070043
Nathan Haroldb1f605f2017-01-12 18:38:57 -080044import java.io.FileDescriptor;
45import java.io.IOException;
Aaron Huangca9c1df2021-12-16 00:11:15 +080046import java.lang.annotation.Retention;
47import java.lang.annotation.RetentionPolicy;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080048import java.net.DatagramSocket;
49import java.net.InetAddress;
50import java.net.Socket;
Aaron Huang49eef6f2021-12-22 16:35:13 +080051import java.util.Objects;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080052
53/**
Jonathan Basseri2725a232017-04-21 15:53:51 -070054 * This class contains methods for managing IPsec sessions. Once configured, the kernel will apply
55 * confidentiality (encryption) and integrity (authentication) to IP traffic.
Nathan Haroldb1f605f2017-01-12 18:38:57 -080056 *
Jonathan Basseri2725a232017-04-21 15:53:51 -070057 * <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 Wongc85b7b02019-11-12 22:31:51 -080059 * to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}.
Jonathan Basseri2725a232017-04-21 15:53:51 -070060 *
61 * @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the
Jonathan Basseri20e96c52017-11-16 10:58:01 -080062 * Internet Protocol</a>
Nathan Haroldb1f605f2017-01-12 18:38:57 -080063 */
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060064@SystemService(Context.IPSEC_SERVICE)
Yan Yan98a3a032022-02-02 16:39:45 -080065public class IpSecManager {
Nathan Haroldb1f605f2017-01-12 18:38:57 -080066 private static final String TAG = "IpSecManager";
67
68 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080069 * 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 Harold5a19b952018-01-05 19:25:13 -080073 */
74 public static final int DIRECTION_IN = 0;
75
76 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080077 * 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 Harold5a19b952018-01-05 19:25:13 -080081 */
82 public static final int DIRECTION_OUT = 1;
83
Nathan Harold5a19b952018-01-05 19:25:13 -080084 /**
Benedict Wong908d34e2021-04-15 11:59:16 -070085 * 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 Huang4989e952021-12-15 23:27:18 +080092 @SystemApi(client = MODULE_LIBRARIES)
Benedict Wong908d34e2021-04-15 11:59:16 -070093 public static final int DIRECTION_FWD = 2;
94
Aaron Huangca9c1df2021-12-16 00:11:15 +080095 /** @hide */
96 @IntDef(value = {DIRECTION_IN, DIRECTION_OUT})
97 @Retention(RetentionPolicy.SOURCE)
98 public @interface PolicyDirection {}
99
Benedict Wong908d34e2021-04-15 11:59:16 -0700100 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700101 * The Security Parameter Index (SPI) 0 indicates an unknown or invalid index.
Nathan Harold031acb82017-03-07 13:23:36 -0800102 *
103 * <p>No IPsec packet may contain an SPI of 0.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700104 *
105 * @hide
Nathan Harold031acb82017-03-07 13:23:36 -0800106 */
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800107 @TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0;
Nathan Harold031acb82017-03-07 13:23:36 -0800108
109 /** @hide */
110 public interface Status {
Aaron Huangfbae3082021-12-06 15:18:42 +0800111 int OK = 0;
112 int RESOURCE_UNAVAILABLE = 1;
113 int SPI_UNAVAILABLE = 2;
Nathan Harold031acb82017-03-07 13:23:36 -0800114 }
115
116 /** @hide */
Nathan Harold5e854462017-12-13 18:51:35 -0800117 public static final int INVALID_RESOURCE_ID = -1;
Nathan Harold031acb82017-03-07 13:23:36 -0800118
119 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700120 * 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 Basseri20e96c52017-11-16 10:58:01 -0800125 * IpSecManager#allocateSecurityParameterIndex}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800126 */
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 Basseri2725a232017-04-21 15:53:51 -0700134 * @param msg description indicating the colliding SPI
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800135 * @param spi the SPI that could not be used due to a collision
136 */
137 SpiUnavailableException(String msg, int spi) {
Jonathan Basseri2725a232017-04-21 15:53:51 -0700138 super(msg + " (spi: " + spi + ")");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800139 mSpi = spi;
140 }
141
Jonathan Basseri2725a232017-04-21 15:53:51 -0700142 /** Get the SPI that caused a collision. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800143 public int getSpi() {
144 return mSpi;
145 }
146 }
147
148 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700149 * 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 Haroldb1f605f2017-01-12 18:38:57 -0800154 */
155 public static final class ResourceUnavailableException extends AndroidException {
156
157 ResourceUnavailableException(String msg) {
158 super(msg);
159 }
160 }
161
Nathan Harold65ef8432018-03-15 18:06:06 -0700162 private final Context mContext;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800163 private final IIpSecService mService;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800164
Jonathan Basseri2725a232017-04-21 15:53:51 -0700165 /**
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 Basseri20e96c52017-11-16 10:58:01 -0800169 * obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released
Jonathan Basseri2725a232017-04-21 15:53:51 -0700170 * by calling {@link #close()} when they are no longer needed.
171 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800172 public static final class SecurityParameterIndex implements AutoCloseable {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800173 private final IIpSecService mService;
Nathan Harold5a19b952018-01-05 19:25:13 -0800174 private final InetAddress mDestinationAddress;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800175 private final CloseGuard mCloseGuard = CloseGuard.get();
Nathan Harold031acb82017-03-07 13:23:36 -0800176 private int mSpi = INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold5e854462017-12-13 18:51:35 -0800177 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800178
Jonathan Basseri2725a232017-04-21 15:53:51 -0700179 /** Get the underlying SPI held by this object. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800180 public int getSpi() {
181 return mSpi;
182 }
183
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800184 /**
185 * Release an SPI that was previously reserved.
186 *
Nathan Harold909d66e2017-03-29 10:47:59 -0700187 * <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 Haroldb1f605f2017-01-12 18:38:57 -0800190 */
191 @Override
192 public void close() {
Nathan Harold80865392017-04-04 19:37:48 -0700193 try {
194 mService.releaseSecurityParameterIndex(mResourceId);
195 } catch (RemoteException e) {
196 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700197 } 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 Harold80865392017-04-04 19:37:48 -0700204 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800205 }
206
Jonathan Basseri2725a232017-04-21 15:53:51 -0700207 /** Check that the SPI was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800208 @Override
Nathan Harold2a30daf2017-11-07 17:17:45 -0800209 protected void finalize() throws Throwable {
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800210 if (mCloseGuard != null) {
211 mCloseGuard.warnIfOpen();
212 }
213
214 close();
215 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800216
Nathan Harold031acb82017-03-07 13:23:36 -0800217 private SecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800218 @NonNull IIpSecService service, InetAddress destinationAddress, int spi)
Nathan Harold031acb82017-03-07 13:23:36 -0800219 throws ResourceUnavailableException, SpiUnavailableException {
220 mService = service;
Nathan Harold5a19b952018-01-05 19:25:13 -0800221 mDestinationAddress = destinationAddress;
Nathan Harold031acb82017-03-07 13:23:36 -0800222 try {
Nathan Harold80865392017-04-04 19:37:48 -0700223 IpSecSpiResponse result =
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800224 mService.allocateSecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800225 destinationAddress.getHostAddress(), spi, new Binder());
Nathan Harold031acb82017-03-07 13:23:36 -0800226
227 if (result == null) {
228 throw new NullPointerException("Received null response from IpSecService");
229 }
230
Nathan Harold80865392017-04-04 19:37:48 -0700231 int status = result.status;
Nathan Harold031acb82017-03-07 13:23:36 -0800232 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 Harold80865392017-04-04 19:37:48 -0700244 mSpi = result.spi;
245 mResourceId = result.resourceId;
Nathan Harold031acb82017-03-07 13:23:36 -0800246
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 Harold031acb82017-03-07 13:23:36 -0800255 } catch (RemoteException e) {
256 throw e.rethrowFromSystemServer();
257 }
258 mCloseGuard.open("open");
259 }
Nathan Harold80865392017-04-04 19:37:48 -0700260
261 /** @hide */
Nathan Harold19b99d92017-08-23 13:46:33 -0700262 @VisibleForTesting
263 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700264 return mResourceId;
265 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700266
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 Harold031acb82017-03-07 13:23:36 -0800277 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800278
279 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800280 * Reserve a random SPI for traffic bound to or from the specified destination address.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800281 *
282 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
283 * SecurityParameterIndex#close()}.
284 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800285 * @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 Haroldb1f605f2017-01-12 18:38:57 -0800287 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800288 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800289 * currently allocated for this user
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800290 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700291 @NonNull
292 public SecurityParameterIndex allocateSecurityParameterIndex(
293 @NonNull InetAddress destinationAddress) throws ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700294 try {
295 return new SecurityParameterIndex(
296 mService,
Nathan Harold5a19b952018-01-05 19:25:13 -0800297 destinationAddress,
Nathan Harold05406792017-04-06 18:16:28 -0700298 IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700299 } catch (ServiceSpecificException e) {
300 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
Nathan Harold05406792017-04-06 18:16:28 -0700301 } catch (SpiUnavailableException unlikely) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700302 // 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 Harold05406792017-04-06 18:16:28 -0700304 throw new ResourceUnavailableException("No SPIs available");
305 }
306 }
307
308 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800309 * Reserve the requested SPI for traffic bound to or from the specified destination address.
Nathan Harold05406792017-04-06 18:16:28 -0700310 *
311 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
312 * SecurityParameterIndex#close()}.
313 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800314 * @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 Haroldbeed0b62018-04-03 16:13:19 -0700316 * @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See
317 * RFC 4303 Section 2.1.
Nathan Harold05406792017-04-06 18:16:28 -0700318 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800319 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800320 * currently allocated for this user
Aaron Huangfbae3082021-12-06 15:18:42 +0800321 * @throws SpiUnavailableException indicating that the requested SPI could not be
Nathan Harold5a19b952018-01-05 19:25:13 -0800322 * reserved
Nathan Harold05406792017-04-06 18:16:28 -0700323 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700324 @NonNull
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800325 public SecurityParameterIndex allocateSecurityParameterIndex(
Nathan Harold6303d9e2018-03-16 17:27:30 -0700326 @NonNull InetAddress destinationAddress, int requestedSpi)
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800327 throws SpiUnavailableException, ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700328 if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
329 throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
330 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700331 try {
332 return new SecurityParameterIndex(mService, destinationAddress, requestedSpi);
333 } catch (ServiceSpecificException e) {
334 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
335 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800336 }
337
338 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700339 * 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 Harold5a19b952018-01-05 19:25:13 -0800343 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700344 * 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 Basseri20e96c52017-11-16 10:58:01 -0800347 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
Jonathan Basseri2725a232017-04-21 15:53:51 -0700348 * 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 Harold5a19b952018-01-05 19:25:13 -0800350 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700351 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700352 * <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 Basseri20e96c52017-11-16 10:58:01 -0800365 * <h4>Rekey Procedure</h4>
366 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800367 * <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 Haroldb1f605f2017-01-12 18:38:57 -0800374 *
375 * @param socket a stream socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800376 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700377 * @param transform a transport mode {@code IpSecTransform}
378 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800379 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700380 public void applyTransportModeTransform(@NonNull Socket socket,
381 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700382 // Ensure creation of FD. See b/77548890 for more details.
383 socket.getSoLinger();
384
Nathan Harold31676252018-01-16 12:08:43 -0800385 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800386 }
387
388 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700389 * 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 Harold5a19b952018-01-05 19:25:13 -0800393 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700394 * 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 Harold5a19b952018-01-05 19:25:13 -0800400 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700401 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800402 * <h4>Rekey Procedure</h4>
403 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800404 * <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 Haroldb1f605f2017-01-12 18:38:57 -0800411 *
412 * @param socket a datagram socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800413 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700414 * @param transform a transport mode {@code IpSecTransform}
415 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800416 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700417 public void applyTransportModeTransform(@NonNull DatagramSocket socket,
418 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold31676252018-01-16 12:08:43 -0800419 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Harold031acb82017-03-07 13:23:36 -0800420 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800421
422 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700423 * 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 Harold5a19b952018-01-05 19:25:13 -0800427 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700428 * 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 Harold5a19b952018-01-05 19:25:13 -0800434 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700435 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700436 * <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 Basseri20e96c52017-11-16 10:58:01 -0800449 * <h4>Rekey Procedure</h4>
450 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800451 * <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 Harold0072e192017-04-06 17:46:00 -0700458 *
459 * @param socket a socket file descriptor
Nathan Harolde3536f02018-03-06 13:22:22 -0800460 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700461 * @param transform a transport mode {@code IpSecTransform}
462 * @throws IOException indicating that the transform could not be applied
Nathan Harold0072e192017-04-06 17:46:00 -0700463 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700464 public void applyTransportModeTransform(@NonNull FileDescriptor socket,
465 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700466 // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
Nathan Harold31676252018-01-16 12:08:43 -0800467 // constructor takes control and closes the user's FD when we exit the method.
Nathan Harold80865392017-04-04 19:37:48 -0700468 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold5a19b952018-01-05 19:25:13 -0800469 mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700470 } catch (ServiceSpecificException e) {
471 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold80865392017-04-04 19:37:48 -0700472 } catch (RemoteException e) {
473 throw e.rethrowFromSystemServer();
474 }
Nathan Harold0072e192017-04-06 17:46:00 -0700475 }
476
477 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700478 * Remove an IPsec transform from a stream socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800479 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800480 * <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 Basseri2725a232017-04-21 15:53:51 -0700482 *
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 Basseri2725a232017-04-21 15:53:51 -0700488 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800489 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700490 public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700491 // Ensure creation of FD. See b/77548890 for more details.
492 socket.getSoLinger();
493
Nathan Harold0d483b72018-01-17 01:00:20 -0800494 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800495 }
496
497 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700498 * Remove an IPsec transform from a datagram socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800499 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800500 * <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 Basseri2725a232017-04-21 15:53:51 -0700502 *
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 Basseri2725a232017-04-21 15:53:51 -0700508 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800509 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700510 public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException {
Nathan Harold0d483b72018-01-17 01:00:20 -0800511 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800512 }
513
Nathan Harold0072e192017-04-06 17:46:00 -0700514 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700515 * Remove an IPsec transform from a socket.
Nathan Harold0072e192017-04-06 17:46:00 -0700516 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800517 * <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 Basseri2725a232017-04-21 15:53:51 -0700519 *
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 Basseri2725a232017-04-21 15:53:51 -0700525 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Harold0072e192017-04-06 17:46:00 -0700526 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700527 public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700528 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold0d483b72018-01-17 01:00:20 -0800529 mService.removeTransportModeTransforms(pfd);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700530 } catch (ServiceSpecificException e) {
531 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold031acb82017-03-07 13:23:36 -0800532 } catch (RemoteException e) {
533 throw e.rethrowFromSystemServer();
534 }
535 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800536
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 Basseri20e96c52017-11-16 10:58:01 -0800543 * <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked.
544 *
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800545 * @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 Haroldb1f605f2017-01-12 18:38:57 -0800550 public void removeTunnelModeTransform(Network net, IpSecTransform transform) {}
551
552 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700553 * This class provides access to a UDP encapsulation Socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800554 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700555 * <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 Wonga386e372018-03-27 16:55:48 -0700559 * #getFileDescriptor}, but should use {@link #close} instead.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700560 *
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 Haroldb1f605f2017-01-12 18:38:57 -0800565 */
566 public static final class UdpEncapsulationSocket implements AutoCloseable {
Nathan Harold80865392017-04-04 19:37:48 -0700567 private final ParcelFileDescriptor mPfd;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800568 private final IIpSecService mService;
Nathan Harold5e854462017-12-13 18:51:35 -0800569 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700570 private final int mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800571 private final CloseGuard mCloseGuard = CloseGuard.get();
572
Nathan Harold031acb82017-03-07 13:23:36 -0800573 private UdpEncapsulationSocket(@NonNull IIpSecService service, int port)
Nathan Harold80865392017-04-04 19:37:48 -0700574 throws ResourceUnavailableException, IOException {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800575 mService = service;
Nathan Harold80865392017-04-04 19:37:48 -0700576 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 Haroldb1f605f2017-01-12 18:38:57 -0800595 mCloseGuard.open("constructor");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800596 }
597
Benedict Wonga386e372018-03-27 16:55:48 -0700598 /** Get the encapsulation socket's file descriptor. */
599 public FileDescriptor getFileDescriptor() {
Nathan Harold80865392017-04-04 19:37:48 -0700600 if (mPfd == null) {
601 return null;
602 }
603 return mPfd.getFileDescriptor();
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800604 }
605
Jonathan Basseri2725a232017-04-21 15:53:51 -0700606 /** Get the bound port of the wrapped socket. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800607 public int getPort() {
Nathan Harold80865392017-04-04 19:37:48 -0700608 return mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800609 }
610
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800611 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700612 * Close this socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800613 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700614 * <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 Haroldb1f605f2017-01-12 18:38:57 -0800617 */
Jonathan Basseri2725a232017-04-21 15:53:51 -0700618 @Override
Nathan Harolda64b0192017-04-17 17:11:58 -0700619 public void close() throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700620 try {
621 mService.closeUdpEncapsulationSocket(mResourceId);
Nathan Harold5e854462017-12-13 18:51:35 -0800622 mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700623 } catch (RemoteException e) {
624 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700625 } 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 Harold80865392017-04-04 19:37:48 -0700632 }
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 Haroldb1f605f2017-01-12 18:38:57 -0800640 }
641
Jonathan Basseri2725a232017-04-21 15:53:51 -0700642 /** Check that the socket was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800643 @Override
644 protected void finalize() throws Throwable {
645 if (mCloseGuard != null) {
646 mCloseGuard.warnIfOpen();
647 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800648 close();
649 }
Nathan Harold80865392017-04-04 19:37:48 -0700650
651 /** @hide */
Chiachang Wanga0343312021-01-12 18:48:13 +0800652 @SystemApi(client = MODULE_LIBRARIES)
Nathan Harold19b99d92017-08-23 13:46:33 -0700653 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700654 return mResourceId;
655 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700656
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 Haroldb1f605f2017-01-12 18:38:57 -0800667 };
668
669 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700670 * Open a socket for UDP encapsulation and bind to the given port.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800671 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700672 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800673 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700674 * @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 Haroldb1f605f2017-01-12 18:38:57 -0800678 */
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 Harold6303d9e2018-03-16 17:27:30 -0700684 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800685 public UdpEncapsulationSocket openUdpEncapsulationSocket(int port)
686 throws IOException, ResourceUnavailableException {
Nathan Harold80865392017-04-04 19:37:48 -0700687 /*
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 Haroldbeed0b62018-04-03 16:13:19 -0700694 try {
695 return new UdpEncapsulationSocket(mService, port);
696 } catch (ServiceSpecificException e) {
697 throw rethrowCheckedExceptionFromServiceSpecificException(e);
698 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800699 }
700
701 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700702 * Open a socket for UDP encapsulation.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800703 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700704 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800705 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700706 * <p>The local port of the returned socket can be obtained by calling {@link
707 * UdpEncapsulationSocket#getPort()}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800708 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700709 * @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 Haroldb1f605f2017-01-12 18:38:57 -0800712 */
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 Harold6303d9e2018-03-16 17:27:30 -0700718 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800719 public UdpEncapsulationSocket openUdpEncapsulationSocket()
720 throws IOException, ResourceUnavailableException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700721 try {
722 return new UdpEncapsulationSocket(mService, 0);
723 } catch (ServiceSpecificException e) {
724 throw rethrowCheckedExceptionFromServiceSpecificException(e);
725 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800726 }
727
728 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000729 * This class represents an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800730 *
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 Colitti388f4162021-02-01 08:12:34 +0000738 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800739 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000740 @SystemApi
Nathan Harold67e76732018-01-17 16:09:24 -0800741 public static final class IpSecTunnelInterface implements AutoCloseable {
Nathan Harold65ef8432018-03-15 18:06:06 -0700742 private final String mOpPackageName;
Nathan Harold67e76732018-01-17 16:09:24 -0800743 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 Colitti388f4162021-02-01 08:12:34 +0000751 /** Get the underlying SPI held by this object. */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700752 @NonNull
Nathan Harold67e76732018-01-17 16:09:24 -0800753 public String getInterfaceName() {
754 return mInterfaceName;
755 }
756
757 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000758 * Add an address to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800759 *
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 Wongd39837f2018-04-03 20:30:54 -0700764 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000765 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800766 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000767 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800768 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700769 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700770 public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800771 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700772 mService.addAddressToTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700773 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700774 } catch (ServiceSpecificException e) {
775 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800776 } catch (RemoteException e) {
777 throw e.rethrowFromSystemServer();
778 }
Nathan Harold67e76732018-01-17 16:09:24 -0800779 }
780
781 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000782 * Remove an address from the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800783 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000784 * <p>Remove an address which was previously added to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800785 *
786 * @param address to be removed
Benedict Wongd39837f2018-04-03 20:30:54 -0700787 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000788 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800789 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000790 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800791 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700792 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700793 public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800794 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700795 mService.removeAddressFromTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700796 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700797 } catch (ServiceSpecificException e) {
798 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800799 } catch (RemoteException e) {
800 throw e.rethrowFromSystemServer();
801 }
Nathan Harold67e76732018-01-17 16:09:24 -0800802 }
803
Yan Yana2f3b492020-09-29 23:38:00 -0700804 /**
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 Yanbe3eb3d2022-05-16 17:13:45 -0700821 * 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 Yana2f3b492020-09-29 23:38:00 -0700825 */
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 Harold65ef8432018-03-15 18:06:06 -0700842 private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
Nathan Harold67e76732018-01-17 16:09:24 -0800843 @NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
844 @NonNull Network underlyingNetwork)
845 throws ResourceUnavailableException, IOException {
Nathan Harold65ef8432018-03-15 18:06:06 -0700846 mOpPackageName = ctx.getOpPackageName();
Nathan Harold67e76732018-01-17 16:09:24 -0800847 mService = service;
848 mLocalAddress = localAddress;
849 mRemoteAddress = remoteAddress;
850 mUnderlyingNetwork = underlyingNetwork;
Benedict Wong8bc90732018-01-18 18:31:45 -0800851
852 try {
853 IpSecTunnelInterfaceResponse result =
854 mService.createTunnelInterface(
855 localAddress.getHostAddress(),
856 remoteAddress.getHostAddress(),
857 underlyingNetwork,
Nathan Harold65ef8432018-03-15 18:06:06 -0700858 new Binder(),
859 mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800860 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 Harold67e76732018-01-17 16:09:24 -0800876 }
877
878 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000879 * Delete an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800880 *
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 Wong8bc90732018-01-18 18:31:45 -0800887 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700888 mService.deleteTunnelInterface(mResourceId, mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800889 } catch (RemoteException e) {
890 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700891 } 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 Wong8bc90732018-01-18 18:31:45 -0800898 }
Nathan Harold67e76732018-01-17 16:09:24 -0800899 }
900
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000901 /** Check that the Interface was closed properly. */
Nathan Harold67e76732018-01-17 16:09:24 -0800902 @Override
903 protected void finalize() throws Throwable {
904 if (mCloseGuard != null) {
905 mCloseGuard.warnIfOpen();
906 }
907 close();
908 }
Benedict Wong8bc90732018-01-18 18:31:45 -0800909
910 /** @hide */
911 @VisibleForTesting
912 public int getResourceId() {
913 return mResourceId;
914 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700915
Aurimas Liutikas3a78d9d2019-08-28 13:01:05 -0700916 @NonNull
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700917 @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 Harold67e76732018-01-17 16:09:24 -0800927 }
928
929 /**
930 * Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic.
931 *
Benedict Wong8bc90732018-01-18 18:31:45 -0800932 * <p>An application that creates tunnels is responsible for cleaning up the tunnel when the
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000933 * underlying network goes away, and the onLost() callback is received.
Benedict Wong8bc90732018-01-18 18:31:45 -0800934 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000935 * @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 Yan160aac32020-12-03 17:08:29 -0800942 * @hide
Yan Yan160aac32020-12-03 17:08:29 -0800943 */
Nathan Harold3a287d52018-04-25 12:01:34 -0700944 @SystemApi
Nathan Harold6303d9e2018-03-16 17:27:30 -0700945 @NonNull
Benedict Wonge9763752018-11-08 19:45:34 -0800946 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700947 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold67e76732018-01-17 16:09:24 -0800948 public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
949 @NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
950 throws ResourceUnavailableException, IOException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700951 try {
952 return new IpSecTunnelInterface(
953 mContext, mService, localAddress, remoteAddress, underlyingNetwork);
954 } catch (ServiceSpecificException e) {
955 throw rethrowCheckedExceptionFromServiceSpecificException(e);
956 }
Nathan Harold67e76732018-01-17 16:09:24 -0800957 }
958
959 /**
Nathan Harolde3536f02018-03-06 13:22:22 -0800960 * 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 Colitti388f4162021-02-01 08:12:34 +0000966 * @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied
Nathan Harold67e76732018-01-17 16:09:24 -0800967 * transform.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000968 * @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which
Nathan Harold67e76732018-01-17 16:09:24 -0800969 * the transform will be used.
970 * @param transform an {@link IpSecTransform} created in tunnel mode
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000971 * @throws IOException indicating that the transform could not be applied due to a lower
972 * layer failure.
973 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800974 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000975 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800976 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700977 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold6303d9e2018-03-16 17:27:30 -0700978 public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel,
979 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong8bc90732018-01-18 18:31:45 -0800980 try {
981 mService.applyTunnelModeTransform(
Nathan Harold65ef8432018-03-15 18:06:06 -0700982 tunnel.getResourceId(), direction,
983 transform.getResourceId(), mContext.getOpPackageName());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700984 } catch (ServiceSpecificException e) {
985 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong8bc90732018-01-18 18:31:45 -0800986 } catch (RemoteException e) {
987 throw e.rethrowFromSystemServer();
988 }
Nathan Harold67e76732018-01-17 16:09:24 -0800989 }
Nathan Harolde3536f02018-03-06 13:22:22 -0800990
Nathan Harold67e76732018-01-17 16:09:24 -0800991 /**
Yan Yane114b382021-02-16 16:29:48 -0800992 * 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 Huang80a73fd2021-12-28 02:33:53 +08001046 * @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 Basseri2725a232017-04-21 15:53:51 -07001069 * Construct an instance of IpSecManager within an application context.
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001070 *
1071 * @param context the application context for this manager
1072 * @hide
1073 */
Nathan Harold65ef8432018-03-15 18:06:06 -07001074 public IpSecManager(Context ctx, IIpSecService service) {
1075 mContext = ctx;
Aaron Huang49eef6f2021-12-22 16:35:13 +08001076 mService = Objects.requireNonNull(service, "missing service");
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001077 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001078
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 Wongc45974b2018-07-16 14:56:20 -07001085 } else if (sse.errorCode == OsConstants.EOPNOTSUPP
1086 || sse.errorCode == OsConstants.EPROTONOSUPPORT) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001087 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 Haroldb1f605f2017-01-12 18:38:57 -08001119}