blob: 49aa99b0975b854b0f9a0c09e60b65142b2cee24 [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;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080030import android.os.ParcelFileDescriptor;
Nathan Harold031acb82017-03-07 13:23:36 -080031import android.os.RemoteException;
Nathan Haroldbeed0b62018-04-03 16:13:19 -070032import android.os.ServiceSpecificException;
33import android.system.ErrnoException;
34import android.system.OsConstants;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080035import android.util.AndroidException;
Nathan Harold80865392017-04-04 19:37:48 -070036import android.util.Log;
Nathan Harold81f54632017-09-11 19:50:19 -070037
Nathan Harold19b99d92017-08-23 13:46:33 -070038import com.android.internal.annotations.VisibleForTesting;
39
Nathan Haroldb1f605f2017-01-12 18:38:57 -080040import dalvik.system.CloseGuard;
Nathan Harold81f54632017-09-11 19:50:19 -070041
Nathan Haroldb1f605f2017-01-12 18:38:57 -080042import java.io.FileDescriptor;
43import java.io.IOException;
Aaron Huangca9c1df2021-12-16 00:11:15 +080044import java.lang.annotation.Retention;
45import java.lang.annotation.RetentionPolicy;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080046import java.net.DatagramSocket;
47import java.net.InetAddress;
48import java.net.Socket;
Aaron Huang49eef6f2021-12-22 16:35:13 +080049import java.util.Objects;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080050
51/**
Jonathan Basseri2725a232017-04-21 15:53:51 -070052 * This class contains methods for managing IPsec sessions. Once configured, the kernel will apply
53 * confidentiality (encryption) and integrity (authentication) to IP traffic.
Nathan Haroldb1f605f2017-01-12 18:38:57 -080054 *
Jonathan Basseri2725a232017-04-21 15:53:51 -070055 * <p>Note that not all aspects of IPsec are permitted by this API. Applications may create
56 * transport mode security associations and apply them to individual sockets. Applications looking
Benedict Wongc85b7b02019-11-12 22:31:51 -080057 * to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}.
Jonathan Basseri2725a232017-04-21 15:53:51 -070058 *
59 * @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the
Jonathan Basseri20e96c52017-11-16 10:58:01 -080060 * Internet Protocol</a>
Nathan Haroldb1f605f2017-01-12 18:38:57 -080061 */
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060062@SystemService(Context.IPSEC_SERVICE)
Nathan Haroldb1f605f2017-01-12 18:38:57 -080063public final class IpSecManager {
64 private static final String TAG = "IpSecManager";
65
66 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080067 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
68 * towards the host.
69 *
70 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080071 */
72 public static final int DIRECTION_IN = 0;
73
74 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080075 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
76 * away from the host.
77 *
78 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080079 */
80 public static final int DIRECTION_OUT = 1;
81
Nathan Harold5a19b952018-01-05 19:25:13 -080082 /**
Benedict Wong908d34e2021-04-15 11:59:16 -070083 * Used when applying a transform to direct traffic through an {@link IpSecTransform} for
84 * forwarding between interfaces.
85 *
86 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
87 *
88 * @hide
89 */
Aaron Huang4989e952021-12-15 23:27:18 +080090 @SystemApi(client = MODULE_LIBRARIES)
Benedict Wong908d34e2021-04-15 11:59:16 -070091 public static final int DIRECTION_FWD = 2;
92
Aaron Huangca9c1df2021-12-16 00:11:15 +080093 /** @hide */
94 @IntDef(value = {DIRECTION_IN, DIRECTION_OUT})
95 @Retention(RetentionPolicy.SOURCE)
96 public @interface PolicyDirection {}
97
Benedict Wong908d34e2021-04-15 11:59:16 -070098 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -070099 * The Security Parameter Index (SPI) 0 indicates an unknown or invalid index.
Nathan Harold031acb82017-03-07 13:23:36 -0800100 *
101 * <p>No IPsec packet may contain an SPI of 0.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700102 *
103 * @hide
Nathan Harold031acb82017-03-07 13:23:36 -0800104 */
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800105 @TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0;
Nathan Harold031acb82017-03-07 13:23:36 -0800106
107 /** @hide */
108 public interface Status {
Aaron Huangfbae3082021-12-06 15:18:42 +0800109 int OK = 0;
110 int RESOURCE_UNAVAILABLE = 1;
111 int SPI_UNAVAILABLE = 2;
Nathan Harold031acb82017-03-07 13:23:36 -0800112 }
113
114 /** @hide */
Nathan Harold5e854462017-12-13 18:51:35 -0800115 public static final int INVALID_RESOURCE_ID = -1;
Nathan Harold031acb82017-03-07 13:23:36 -0800116
117 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700118 * Thrown to indicate that a requested SPI is in use.
119 *
120 * <p>The combination of remote {@code InetAddress} and SPI must be unique across all apps on
121 * one device. If this error is encountered, a new SPI is required before a transform may be
122 * created. This error can be avoided by calling {@link
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800123 * IpSecManager#allocateSecurityParameterIndex}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800124 */
125 public static final class SpiUnavailableException extends AndroidException {
126 private final int mSpi;
127
128 /**
129 * Construct an exception indicating that a transform with the given SPI is already in use
130 * or otherwise unavailable.
131 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700132 * @param msg description indicating the colliding SPI
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800133 * @param spi the SPI that could not be used due to a collision
134 */
135 SpiUnavailableException(String msg, int spi) {
Jonathan Basseri2725a232017-04-21 15:53:51 -0700136 super(msg + " (spi: " + spi + ")");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800137 mSpi = spi;
138 }
139
Jonathan Basseri2725a232017-04-21 15:53:51 -0700140 /** Get the SPI that caused a collision. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800141 public int getSpi() {
142 return mSpi;
143 }
144 }
145
146 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700147 * Thrown to indicate that an IPsec resource is unavailable.
148 *
149 * <p>This could apply to resources such as sockets, {@link SecurityParameterIndex}, {@link
150 * IpSecTransform}, or other system resources. If this exception is thrown, users should release
151 * allocated objects of the type requested.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800152 */
153 public static final class ResourceUnavailableException extends AndroidException {
154
155 ResourceUnavailableException(String msg) {
156 super(msg);
157 }
158 }
159
Nathan Harold65ef8432018-03-15 18:06:06 -0700160 private final Context mContext;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800161 private final IIpSecService mService;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800162
Jonathan Basseri2725a232017-04-21 15:53:51 -0700163 /**
164 * This class represents a reserved SPI.
165 *
166 * <p>Objects of this type are used to track reserved security parameter indices. They can be
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800167 * obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released
Jonathan Basseri2725a232017-04-21 15:53:51 -0700168 * by calling {@link #close()} when they are no longer needed.
169 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800170 public static final class SecurityParameterIndex implements AutoCloseable {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800171 private final IIpSecService mService;
Nathan Harold5a19b952018-01-05 19:25:13 -0800172 private final InetAddress mDestinationAddress;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800173 private final CloseGuard mCloseGuard = CloseGuard.get();
Nathan Harold031acb82017-03-07 13:23:36 -0800174 private int mSpi = INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold5e854462017-12-13 18:51:35 -0800175 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800176
Jonathan Basseri2725a232017-04-21 15:53:51 -0700177 /** Get the underlying SPI held by this object. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800178 public int getSpi() {
179 return mSpi;
180 }
181
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800182 /**
183 * Release an SPI that was previously reserved.
184 *
Nathan Harold909d66e2017-03-29 10:47:59 -0700185 * <p>Release an SPI for use by other users in the system. If a SecurityParameterIndex is
186 * applied to an IpSecTransform, it will become unusable for future transforms but should
187 * still be closed to ensure system resources are released.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800188 */
189 @Override
190 public void close() {
Nathan Harold80865392017-04-04 19:37:48 -0700191 try {
192 mService.releaseSecurityParameterIndex(mResourceId);
193 } catch (RemoteException e) {
194 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700195 } catch (Exception e) {
196 // On close we swallow all random exceptions since failure to close is not
197 // actionable by the user.
198 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
199 } finally {
200 mResourceId = INVALID_RESOURCE_ID;
201 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700202 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800203 }
204
Jonathan Basseri2725a232017-04-21 15:53:51 -0700205 /** Check that the SPI was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800206 @Override
Nathan Harold2a30daf2017-11-07 17:17:45 -0800207 protected void finalize() throws Throwable {
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800208 if (mCloseGuard != null) {
209 mCloseGuard.warnIfOpen();
210 }
211
212 close();
213 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800214
Nathan Harold031acb82017-03-07 13:23:36 -0800215 private SecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800216 @NonNull IIpSecService service, InetAddress destinationAddress, int spi)
Nathan Harold031acb82017-03-07 13:23:36 -0800217 throws ResourceUnavailableException, SpiUnavailableException {
218 mService = service;
Nathan Harold5a19b952018-01-05 19:25:13 -0800219 mDestinationAddress = destinationAddress;
Nathan Harold031acb82017-03-07 13:23:36 -0800220 try {
Nathan Harold80865392017-04-04 19:37:48 -0700221 IpSecSpiResponse result =
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800222 mService.allocateSecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800223 destinationAddress.getHostAddress(), spi, new Binder());
Nathan Harold031acb82017-03-07 13:23:36 -0800224
225 if (result == null) {
226 throw new NullPointerException("Received null response from IpSecService");
227 }
228
Nathan Harold80865392017-04-04 19:37:48 -0700229 int status = result.status;
Nathan Harold031acb82017-03-07 13:23:36 -0800230 switch (status) {
231 case Status.OK:
232 break;
233 case Status.RESOURCE_UNAVAILABLE:
234 throw new ResourceUnavailableException(
235 "No more SPIs may be allocated by this requester.");
236 case Status.SPI_UNAVAILABLE:
237 throw new SpiUnavailableException("Requested SPI is unavailable", spi);
238 default:
239 throw new RuntimeException(
240 "Unknown status returned by IpSecService: " + status);
241 }
Nathan Harold80865392017-04-04 19:37:48 -0700242 mSpi = result.spi;
243 mResourceId = result.resourceId;
Nathan Harold031acb82017-03-07 13:23:36 -0800244
245 if (mSpi == INVALID_SECURITY_PARAMETER_INDEX) {
246 throw new RuntimeException("Invalid SPI returned by IpSecService: " + status);
247 }
248
249 if (mResourceId == INVALID_RESOURCE_ID) {
250 throw new RuntimeException(
251 "Invalid Resource ID returned by IpSecService: " + status);
252 }
Nathan Harold031acb82017-03-07 13:23:36 -0800253 } catch (RemoteException e) {
254 throw e.rethrowFromSystemServer();
255 }
256 mCloseGuard.open("open");
257 }
Nathan Harold80865392017-04-04 19:37:48 -0700258
259 /** @hide */
Nathan Harold19b99d92017-08-23 13:46:33 -0700260 @VisibleForTesting
261 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700262 return mResourceId;
263 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700264
265 @Override
266 public String toString() {
267 return new StringBuilder()
268 .append("SecurityParameterIndex{spi=")
269 .append(mSpi)
270 .append(",resourceId=")
271 .append(mResourceId)
272 .append("}")
273 .toString();
274 }
Nathan Harold031acb82017-03-07 13:23:36 -0800275 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800276
277 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800278 * Reserve a random SPI for traffic bound to or from the specified destination address.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800279 *
280 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
281 * SecurityParameterIndex#close()}.
282 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800283 * @param destinationAddress the destination address for traffic bearing the requested SPI.
284 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800285 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800286 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800287 * currently allocated for this user
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800288 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700289 @NonNull
290 public SecurityParameterIndex allocateSecurityParameterIndex(
291 @NonNull InetAddress destinationAddress) throws ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700292 try {
293 return new SecurityParameterIndex(
294 mService,
Nathan Harold5a19b952018-01-05 19:25:13 -0800295 destinationAddress,
Nathan Harold05406792017-04-06 18:16:28 -0700296 IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700297 } catch (ServiceSpecificException e) {
298 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
Nathan Harold05406792017-04-06 18:16:28 -0700299 } catch (SpiUnavailableException unlikely) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700300 // Because this function allocates a totally random SPI, it really shouldn't ever
301 // fail to allocate an SPI; we simply need this because the exception is checked.
Nathan Harold05406792017-04-06 18:16:28 -0700302 throw new ResourceUnavailableException("No SPIs available");
303 }
304 }
305
306 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800307 * Reserve the requested SPI for traffic bound to or from the specified destination address.
Nathan Harold05406792017-04-06 18:16:28 -0700308 *
309 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
310 * SecurityParameterIndex#close()}.
311 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800312 * @param destinationAddress the destination address for traffic bearing the requested SPI.
313 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700314 * @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See
315 * RFC 4303 Section 2.1.
Nathan Harold05406792017-04-06 18:16:28 -0700316 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800317 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800318 * currently allocated for this user
Aaron Huangfbae3082021-12-06 15:18:42 +0800319 * @throws SpiUnavailableException indicating that the requested SPI could not be
Nathan Harold5a19b952018-01-05 19:25:13 -0800320 * reserved
Nathan Harold05406792017-04-06 18:16:28 -0700321 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700322 @NonNull
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800323 public SecurityParameterIndex allocateSecurityParameterIndex(
Nathan Harold6303d9e2018-03-16 17:27:30 -0700324 @NonNull InetAddress destinationAddress, int requestedSpi)
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800325 throws SpiUnavailableException, ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700326 if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
327 throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
328 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700329 try {
330 return new SecurityParameterIndex(mService, destinationAddress, requestedSpi);
331 } catch (ServiceSpecificException e) {
332 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
333 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800334 }
335
336 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700337 * Apply an IPsec transform to a stream socket.
338 *
339 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
340 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800341 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700342 * unprotected traffic can resume on that socket.
343 *
344 * <p>For security reasons, the destination address of any traffic on the socket must match the
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800345 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
Jonathan Basseri2725a232017-04-21 15:53:51 -0700346 * other IP address will result in an IOException. In addition, reads and writes on the socket
347 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800348 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700349 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700350 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
351 * applied transform before completion of graceful shutdown may result in the shutdown sequence
352 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
353 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
354 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
355 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
356 * sufficient to ensure shutdown.
357 *
358 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
359 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
360 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
361 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
362 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800363 * <h4>Rekey Procedure</h4>
364 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800365 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
366 * will be removed and the new transform will take effect immediately, sending all traffic on
367 * the new transform; however, when applying a transform in the inbound direction, traffic
368 * on the old transform will continue to be decrypted and delivered until that transform is
369 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
370 * procedures where both transforms are valid until both endpoints are using the new transform
371 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800372 *
373 * @param socket a stream socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800374 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700375 * @param transform a transport mode {@code IpSecTransform}
376 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800377 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700378 public void applyTransportModeTransform(@NonNull Socket socket,
379 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700380 // Ensure creation of FD. See b/77548890 for more details.
381 socket.getSoLinger();
382
Nathan Harold31676252018-01-16 12:08:43 -0800383 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800384 }
385
386 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700387 * Apply an IPsec transform to a datagram socket.
388 *
389 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
390 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800391 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700392 * unprotected traffic can resume on that socket.
393 *
394 * <p>For security reasons, the destination address of any traffic on the socket must match the
395 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
396 * other IP address will result in an IOException. In addition, reads and writes on the socket
397 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800398 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700399 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800400 * <h4>Rekey Procedure</h4>
401 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800402 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
403 * will be removed and the new transform will take effect immediately, sending all traffic on
404 * the new transform; however, when applying a transform in the inbound direction, traffic
405 * on the old transform will continue to be decrypted and delivered until that transform is
406 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
407 * procedures where both transforms are valid until both endpoints are using the new transform
408 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800409 *
410 * @param socket a datagram socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800411 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700412 * @param transform a transport mode {@code IpSecTransform}
413 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800414 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700415 public void applyTransportModeTransform(@NonNull DatagramSocket socket,
416 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold31676252018-01-16 12:08:43 -0800417 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Harold031acb82017-03-07 13:23:36 -0800418 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800419
420 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700421 * Apply an IPsec transform to a socket.
422 *
423 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
424 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800425 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700426 * unprotected traffic can resume on that socket.
427 *
428 * <p>For security reasons, the destination address of any traffic on the socket must match the
429 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
430 * other IP address will result in an IOException. In addition, reads and writes on the socket
431 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800432 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700433 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700434 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
435 * applied transform before completion of graceful shutdown may result in the shutdown sequence
436 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
437 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
438 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
439 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
440 * sufficient to ensure shutdown.
441 *
442 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
443 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
444 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
445 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
446 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800447 * <h4>Rekey Procedure</h4>
448 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800449 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
450 * will be removed and the new transform will take effect immediately, sending all traffic on
451 * the new transform; however, when applying a transform in the inbound direction, traffic
452 * on the old transform will continue to be decrypted and delivered until that transform is
453 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
454 * procedures where both transforms are valid until both endpoints are using the new transform
455 * and all in-flight packets have been received.
Nathan Harold0072e192017-04-06 17:46:00 -0700456 *
457 * @param socket a socket file descriptor
Nathan Harolde3536f02018-03-06 13:22:22 -0800458 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700459 * @param transform a transport mode {@code IpSecTransform}
460 * @throws IOException indicating that the transform could not be applied
Nathan Harold0072e192017-04-06 17:46:00 -0700461 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700462 public void applyTransportModeTransform(@NonNull FileDescriptor socket,
463 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700464 // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
Nathan Harold31676252018-01-16 12:08:43 -0800465 // constructor takes control and closes the user's FD when we exit the method.
Nathan Harold80865392017-04-04 19:37:48 -0700466 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold5a19b952018-01-05 19:25:13 -0800467 mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700468 } catch (ServiceSpecificException e) {
469 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold80865392017-04-04 19:37:48 -0700470 } catch (RemoteException e) {
471 throw e.rethrowFromSystemServer();
472 }
Nathan Harold0072e192017-04-06 17:46:00 -0700473 }
474
475 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700476 * Remove an IPsec transform from a stream socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800477 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800478 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
479 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700480 *
481 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
482 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
483 * is called.
484 *
485 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700486 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800487 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700488 public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700489 // Ensure creation of FD. See b/77548890 for more details.
490 socket.getSoLinger();
491
Nathan Harold0d483b72018-01-17 01:00:20 -0800492 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800493 }
494
495 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700496 * Remove an IPsec transform from a datagram socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800497 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800498 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
499 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700500 *
501 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
502 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
503 * is called.
504 *
505 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700506 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800507 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700508 public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException {
Nathan Harold0d483b72018-01-17 01:00:20 -0800509 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800510 }
511
Nathan Harold0072e192017-04-06 17:46:00 -0700512 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700513 * Remove an IPsec transform from a socket.
Nathan Harold0072e192017-04-06 17:46:00 -0700514 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800515 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
516 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700517 *
518 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
519 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
520 * is called.
521 *
522 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700523 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Harold0072e192017-04-06 17:46:00 -0700524 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700525 public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700526 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold0d483b72018-01-17 01:00:20 -0800527 mService.removeTransportModeTransforms(pfd);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700528 } catch (ServiceSpecificException e) {
529 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold031acb82017-03-07 13:23:36 -0800530 } catch (RemoteException e) {
531 throw e.rethrowFromSystemServer();
532 }
533 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800534
535 /**
536 * Remove a Tunnel Mode IPsec Transform from a {@link Network}. This must be used as part of
537 * cleanup if a tunneled Network experiences a change in default route. The Network will drop
538 * all traffic that cannot be routed to the Tunnel's outbound interface. If that interface is
539 * lost, all traffic will drop.
540 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800541 * <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked.
542 *
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800543 * @param net a network that currently has transform applied to it.
544 * @param transform a Tunnel Mode IPsec Transform that has been previously applied to the given
545 * network
546 * @hide
547 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800548 public void removeTunnelModeTransform(Network net, IpSecTransform transform) {}
549
550 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700551 * This class provides access to a UDP encapsulation Socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800552 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700553 * <p>{@code UdpEncapsulationSocket} wraps a system-provided datagram socket intended for IKEv2
554 * signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link
555 * IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the
556 * caller. The caller should not close the {@code FileDescriptor} returned by {@link
Benedict Wonga386e372018-03-27 16:55:48 -0700557 * #getFileDescriptor}, but should use {@link #close} instead.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700558 *
559 * <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic
560 * of the next user who binds to that port. To prevent this scenario, these sockets are held
561 * open by the system so that they may only be closed by calling {@link #close} or when the user
562 * process exits.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800563 */
564 public static final class UdpEncapsulationSocket implements AutoCloseable {
Nathan Harold80865392017-04-04 19:37:48 -0700565 private final ParcelFileDescriptor mPfd;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800566 private final IIpSecService mService;
Nathan Harold5e854462017-12-13 18:51:35 -0800567 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700568 private final int mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800569 private final CloseGuard mCloseGuard = CloseGuard.get();
570
Nathan Harold031acb82017-03-07 13:23:36 -0800571 private UdpEncapsulationSocket(@NonNull IIpSecService service, int port)
Nathan Harold80865392017-04-04 19:37:48 -0700572 throws ResourceUnavailableException, IOException {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800573 mService = service;
Nathan Harold80865392017-04-04 19:37:48 -0700574 try {
575 IpSecUdpEncapResponse result =
576 mService.openUdpEncapsulationSocket(port, new Binder());
577 switch (result.status) {
578 case Status.OK:
579 break;
580 case Status.RESOURCE_UNAVAILABLE:
581 throw new ResourceUnavailableException(
582 "No more Sockets may be allocated by this requester.");
583 default:
584 throw new RuntimeException(
585 "Unknown status returned by IpSecService: " + result.status);
586 }
587 mResourceId = result.resourceId;
588 mPort = result.port;
589 mPfd = result.fileDescriptor;
590 } catch (RemoteException e) {
591 throw e.rethrowFromSystemServer();
592 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800593 mCloseGuard.open("constructor");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800594 }
595
Benedict Wonga386e372018-03-27 16:55:48 -0700596 /** Get the encapsulation socket's file descriptor. */
597 public FileDescriptor getFileDescriptor() {
Nathan Harold80865392017-04-04 19:37:48 -0700598 if (mPfd == null) {
599 return null;
600 }
601 return mPfd.getFileDescriptor();
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800602 }
603
Jonathan Basseri2725a232017-04-21 15:53:51 -0700604 /** Get the bound port of the wrapped socket. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800605 public int getPort() {
Nathan Harold80865392017-04-04 19:37:48 -0700606 return mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800607 }
608
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800609 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700610 * Close this socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800611 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700612 * <p>This closes the wrapped socket. Open encapsulation sockets count against a user's
613 * resource limits, and forgetting to close them eventually will result in {@link
614 * ResourceUnavailableException} being thrown.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800615 */
Jonathan Basseri2725a232017-04-21 15:53:51 -0700616 @Override
Nathan Harolda64b0192017-04-17 17:11:58 -0700617 public void close() throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700618 try {
619 mService.closeUdpEncapsulationSocket(mResourceId);
Nathan Harold5e854462017-12-13 18:51:35 -0800620 mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700621 } catch (RemoteException e) {
622 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700623 } catch (Exception e) {
624 // On close we swallow all random exceptions since failure to close is not
625 // actionable by the user.
626 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
627 } finally {
628 mResourceId = INVALID_RESOURCE_ID;
629 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700630 }
631
632 try {
633 mPfd.close();
634 } catch (IOException e) {
635 Log.e(TAG, "Failed to close UDP Encapsulation Socket with Port= " + mPort);
636 throw e;
637 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800638 }
639
Jonathan Basseri2725a232017-04-21 15:53:51 -0700640 /** Check that the socket was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800641 @Override
642 protected void finalize() throws Throwable {
643 if (mCloseGuard != null) {
644 mCloseGuard.warnIfOpen();
645 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800646 close();
647 }
Nathan Harold80865392017-04-04 19:37:48 -0700648
649 /** @hide */
Chiachang Wanga0343312021-01-12 18:48:13 +0800650 @SystemApi(client = MODULE_LIBRARIES)
Nathan Harold19b99d92017-08-23 13:46:33 -0700651 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700652 return mResourceId;
653 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700654
655 @Override
656 public String toString() {
657 return new StringBuilder()
658 .append("UdpEncapsulationSocket{port=")
659 .append(mPort)
660 .append(",resourceId=")
661 .append(mResourceId)
662 .append("}")
663 .toString();
664 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800665 };
666
667 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700668 * Open a socket for UDP encapsulation and bind to the given port.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800669 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700670 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800671 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700672 * @param port a local UDP port
673 * @return a socket that is bound to the given port
674 * @throws IOException indicating that the socket could not be opened or bound
675 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800676 */
677 // Returning a socket in this fashion that has been created and bound by the system
678 // is the only safe way to ensure that a socket is both accessible to the user and
679 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
680 // the port, which could potentially impact the traffic of the next user who binds to that
681 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700682 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800683 public UdpEncapsulationSocket openUdpEncapsulationSocket(int port)
684 throws IOException, ResourceUnavailableException {
Nathan Harold80865392017-04-04 19:37:48 -0700685 /*
686 * Most range checking is done in the service, but this version of the constructor expects
687 * a valid port number, and zero cannot be checked after being passed to the service.
688 */
689 if (port == 0) {
690 throw new IllegalArgumentException("Specified port must be a valid port number!");
691 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700692 try {
693 return new UdpEncapsulationSocket(mService, port);
694 } catch (ServiceSpecificException e) {
695 throw rethrowCheckedExceptionFromServiceSpecificException(e);
696 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800697 }
698
699 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700700 * Open a socket for UDP encapsulation.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800701 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700702 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800703 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700704 * <p>The local port of the returned socket can be obtained by calling {@link
705 * UdpEncapsulationSocket#getPort()}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800706 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700707 * @return a socket that is bound to a local port
708 * @throws IOException indicating that the socket could not be opened or bound
709 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800710 */
711 // Returning a socket in this fashion that has been created and bound by the system
712 // is the only safe way to ensure that a socket is both accessible to the user and
713 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
714 // the port, which could potentially impact the traffic of the next user who binds to that
715 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700716 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800717 public UdpEncapsulationSocket openUdpEncapsulationSocket()
718 throws IOException, ResourceUnavailableException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700719 try {
720 return new UdpEncapsulationSocket(mService, 0);
721 } catch (ServiceSpecificException e) {
722 throw rethrowCheckedExceptionFromServiceSpecificException(e);
723 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800724 }
725
726 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000727 * This class represents an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800728 *
729 * <p>IpSecTunnelInterface objects track tunnel interfaces that serve as
730 * local endpoints for IPsec tunnels.
731 *
732 * <p>Creating an IpSecTunnelInterface creates a device to which IpSecTransforms may be
733 * applied to provide IPsec security to packets sent through the tunnel. While a tunnel
734 * cannot be used in standalone mode within Android, the higher layers may use the tunnel
735 * to create Network objects which are accessible to the Android system.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000736 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800737 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000738 @SystemApi
Nathan Harold67e76732018-01-17 16:09:24 -0800739 public static final class IpSecTunnelInterface implements AutoCloseable {
Nathan Harold65ef8432018-03-15 18:06:06 -0700740 private final String mOpPackageName;
Nathan Harold67e76732018-01-17 16:09:24 -0800741 private final IIpSecService mService;
742 private final InetAddress mRemoteAddress;
743 private final InetAddress mLocalAddress;
744 private final Network mUnderlyingNetwork;
745 private final CloseGuard mCloseGuard = CloseGuard.get();
746 private String mInterfaceName;
747 private int mResourceId = INVALID_RESOURCE_ID;
748
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000749 /** Get the underlying SPI held by this object. */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700750 @NonNull
Nathan Harold67e76732018-01-17 16:09:24 -0800751 public String getInterfaceName() {
752 return mInterfaceName;
753 }
754
755 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000756 * Add an address to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800757 *
758 * <p>Add an address which may be used as the local inner address for
759 * tunneled traffic.
760 *
761 * @param address the local address for traffic inside the tunnel
Benedict Wongd39837f2018-04-03 20:30:54 -0700762 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000763 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800764 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000765 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800766 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700767 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700768 public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800769 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700770 mService.addAddressToTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700771 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700772 } catch (ServiceSpecificException e) {
773 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800774 } catch (RemoteException e) {
775 throw e.rethrowFromSystemServer();
776 }
Nathan Harold67e76732018-01-17 16:09:24 -0800777 }
778
779 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000780 * Remove an address from the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800781 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000782 * <p>Remove an address which was previously added to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800783 *
784 * @param address to be removed
Benedict Wongd39837f2018-04-03 20:30:54 -0700785 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000786 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800787 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000788 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800789 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700790 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700791 public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800792 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700793 mService.removeAddressFromTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700794 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700795 } catch (ServiceSpecificException e) {
796 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800797 } catch (RemoteException e) {
798 throw e.rethrowFromSystemServer();
799 }
Nathan Harold67e76732018-01-17 16:09:24 -0800800 }
801
Yan Yana2f3b492020-09-29 23:38:00 -0700802 /**
803 * Update the underlying network for this IpSecTunnelInterface.
804 *
805 * <p>This new underlying network will be used for all transforms applied AFTER this call is
806 * complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to
807 * this tunnel interface, traffic will still use the old SA, and be routed on the old
808 * underlying network.
809 *
810 * <p>To migrate IPsec tunnel mode traffic, a caller should:
811 *
812 * <ol>
813 * <li>Update the IpSecTunnelInterface’s underlying network.
814 * <li>Apply {@link IpSecTransform}(s) with matching addresses to this
815 * IpSecTunnelInterface.
816 * </ol>
817 *
818 * @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel.
819 * This network MUST never be the network exposing this IpSecTunnelInterface, otherwise
Yan Yancf4d2eb2021-02-16 11:48:55 -0800820 * this method will throw an {@link IllegalArgumentException}. If the
821 * IpSecTunnelInterface is later added to this network, all outbound traffic will be
822 * blackholed.
Yan Yana2f3b492020-09-29 23:38:00 -0700823 */
824 // TODO: b/169171001 Update the documentation when transform migration is supported.
825 // The purpose of making updating network and applying transforms separate is to leave open
826 // the possibility to support lossless migration procedures. To do that, Android platform
827 // will need to support multiple inbound tunnel mode transforms, just like it can support
828 // multiple transport mode transforms.
829 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
830 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
831 public void setUnderlyingNetwork(@NonNull Network underlyingNetwork) throws IOException {
832 try {
833 mService.setNetworkForTunnelInterface(
834 mResourceId, underlyingNetwork, mOpPackageName);
835 } catch (RemoteException e) {
836 throw e.rethrowFromSystemServer();
837 }
838 }
839
Nathan Harold65ef8432018-03-15 18:06:06 -0700840 private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
Nathan Harold67e76732018-01-17 16:09:24 -0800841 @NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
842 @NonNull Network underlyingNetwork)
843 throws ResourceUnavailableException, IOException {
Nathan Harold65ef8432018-03-15 18:06:06 -0700844 mOpPackageName = ctx.getOpPackageName();
Nathan Harold67e76732018-01-17 16:09:24 -0800845 mService = service;
846 mLocalAddress = localAddress;
847 mRemoteAddress = remoteAddress;
848 mUnderlyingNetwork = underlyingNetwork;
Benedict Wong8bc90732018-01-18 18:31:45 -0800849
850 try {
851 IpSecTunnelInterfaceResponse result =
852 mService.createTunnelInterface(
853 localAddress.getHostAddress(),
854 remoteAddress.getHostAddress(),
855 underlyingNetwork,
Nathan Harold65ef8432018-03-15 18:06:06 -0700856 new Binder(),
857 mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800858 switch (result.status) {
859 case Status.OK:
860 break;
861 case Status.RESOURCE_UNAVAILABLE:
862 throw new ResourceUnavailableException(
863 "No more tunnel interfaces may be allocated by this requester.");
864 default:
865 throw new RuntimeException(
866 "Unknown status returned by IpSecService: " + result.status);
867 }
868 mResourceId = result.resourceId;
869 mInterfaceName = result.interfaceName;
870 } catch (RemoteException e) {
871 throw e.rethrowFromSystemServer();
872 }
873 mCloseGuard.open("constructor");
Nathan Harold67e76732018-01-17 16:09:24 -0800874 }
875
876 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000877 * Delete an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800878 *
879 * <p>Calling close will deallocate the IpSecTunnelInterface and all of its system
880 * resources. Any packets bound for this interface either inbound or outbound will
881 * all be lost.
882 */
883 @Override
884 public void close() {
Benedict Wong8bc90732018-01-18 18:31:45 -0800885 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700886 mService.deleteTunnelInterface(mResourceId, mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800887 } catch (RemoteException e) {
888 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700889 } catch (Exception e) {
890 // On close we swallow all random exceptions since failure to close is not
891 // actionable by the user.
892 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
893 } finally {
894 mResourceId = INVALID_RESOURCE_ID;
895 mCloseGuard.close();
Benedict Wong8bc90732018-01-18 18:31:45 -0800896 }
Nathan Harold67e76732018-01-17 16:09:24 -0800897 }
898
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000899 /** Check that the Interface was closed properly. */
Nathan Harold67e76732018-01-17 16:09:24 -0800900 @Override
901 protected void finalize() throws Throwable {
902 if (mCloseGuard != null) {
903 mCloseGuard.warnIfOpen();
904 }
905 close();
906 }
Benedict Wong8bc90732018-01-18 18:31:45 -0800907
908 /** @hide */
909 @VisibleForTesting
910 public int getResourceId() {
911 return mResourceId;
912 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700913
Aurimas Liutikas3a78d9d2019-08-28 13:01:05 -0700914 @NonNull
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700915 @Override
916 public String toString() {
917 return new StringBuilder()
918 .append("IpSecTunnelInterface{ifname=")
919 .append(mInterfaceName)
920 .append(",resourceId=")
921 .append(mResourceId)
922 .append("}")
923 .toString();
924 }
Nathan Harold67e76732018-01-17 16:09:24 -0800925 }
926
927 /**
928 * Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic.
929 *
Benedict Wong8bc90732018-01-18 18:31:45 -0800930 * <p>An application that creates tunnels is responsible for cleaning up the tunnel when the
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000931 * underlying network goes away, and the onLost() callback is received.
Benedict Wong8bc90732018-01-18 18:31:45 -0800932 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000933 * @param localAddress The local addres of the tunnel
934 * @param remoteAddress The local addres of the tunnel
935 * @param underlyingNetwork the {@link Network} that will carry traffic for this tunnel.
936 * This network should almost certainly be a network such as WiFi with an L2 address.
937 * @return a new {@link IpSecManager#IpSecTunnelInterface} with the specified properties
938 * @throws IOException indicating that the socket could not be opened or bound
939 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Yan Yan160aac32020-12-03 17:08:29 -0800940 * @hide
Yan Yan160aac32020-12-03 17:08:29 -0800941 */
Nathan Harold3a287d52018-04-25 12:01:34 -0700942 @SystemApi
Nathan Harold6303d9e2018-03-16 17:27:30 -0700943 @NonNull
Benedict Wonge9763752018-11-08 19:45:34 -0800944 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700945 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold67e76732018-01-17 16:09:24 -0800946 public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
947 @NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
948 throws ResourceUnavailableException, IOException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700949 try {
950 return new IpSecTunnelInterface(
951 mContext, mService, localAddress, remoteAddress, underlyingNetwork);
952 } catch (ServiceSpecificException e) {
953 throw rethrowCheckedExceptionFromServiceSpecificException(e);
954 }
Nathan Harold67e76732018-01-17 16:09:24 -0800955 }
956
957 /**
Nathan Harolde3536f02018-03-06 13:22:22 -0800958 * Apply an active Tunnel Mode IPsec Transform to a {@link IpSecTunnelInterface}, which will
959 * tunnel all traffic for the given direction through the underlying network's interface with
960 * IPsec (applies an outer IP header and IPsec Header to all traffic, and expects an additional
961 * IP header and IPsec Header on all inbound traffic).
962 * <p>Applications should probably not use this API directly.
963 *
Nathan Harold67e76732018-01-17 16:09:24 -0800964 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000965 * @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied
Nathan Harold67e76732018-01-17 16:09:24 -0800966 * transform.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000967 * @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which
Nathan Harold67e76732018-01-17 16:09:24 -0800968 * the transform will be used.
969 * @param transform an {@link IpSecTransform} created in tunnel mode
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000970 * @throws IOException indicating that the transform could not be applied due to a lower
971 * layer failure.
972 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800973 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000974 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800975 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700976 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold6303d9e2018-03-16 17:27:30 -0700977 public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel,
978 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong8bc90732018-01-18 18:31:45 -0800979 try {
980 mService.applyTunnelModeTransform(
Nathan Harold65ef8432018-03-15 18:06:06 -0700981 tunnel.getResourceId(), direction,
982 transform.getResourceId(), mContext.getOpPackageName());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700983 } catch (ServiceSpecificException e) {
984 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong8bc90732018-01-18 18:31:45 -0800985 } catch (RemoteException e) {
986 throw e.rethrowFromSystemServer();
987 }
Nathan Harold67e76732018-01-17 16:09:24 -0800988 }
Nathan Harolde3536f02018-03-06 13:22:22 -0800989
Nathan Harold67e76732018-01-17 16:09:24 -0800990 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700991 * Construct an instance of IpSecManager within an application context.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800992 *
993 * @param context the application context for this manager
994 * @hide
995 */
Nathan Harold65ef8432018-03-15 18:06:06 -0700996 public IpSecManager(Context ctx, IIpSecService service) {
997 mContext = ctx;
Aaron Huang49eef6f2021-12-22 16:35:13 +0800998 mService = Objects.requireNonNull(service, "missing service");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800999 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001000
1001 private static void maybeHandleServiceSpecificException(ServiceSpecificException sse) {
1002 // OsConstants are late binding, so switch statements can't be used.
1003 if (sse.errorCode == OsConstants.EINVAL) {
1004 throw new IllegalArgumentException(sse);
1005 } else if (sse.errorCode == OsConstants.EAGAIN) {
1006 throw new IllegalStateException(sse);
Benedict Wongc45974b2018-07-16 14:56:20 -07001007 } else if (sse.errorCode == OsConstants.EOPNOTSUPP
1008 || sse.errorCode == OsConstants.EPROTONOSUPPORT) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001009 throw new UnsupportedOperationException(sse);
1010 }
1011 }
1012
1013 /**
1014 * Convert an Errno SSE to the correct Unchecked exception type.
1015 *
1016 * This method never actually returns.
1017 */
1018 // package
1019 static RuntimeException
1020 rethrowUncheckedExceptionFromServiceSpecificException(ServiceSpecificException sse) {
1021 maybeHandleServiceSpecificException(sse);
1022 throw new RuntimeException(sse);
1023 }
1024
1025 /**
1026 * Convert an Errno SSE to the correct Checked or Unchecked exception type.
1027 *
1028 * This method may throw IOException, or it may throw an unchecked exception; it will never
1029 * actually return.
1030 */
1031 // package
1032 static IOException rethrowCheckedExceptionFromServiceSpecificException(
1033 ServiceSpecificException sse) throws IOException {
1034 // First see if this is an unchecked exception of a type we know.
1035 // If so, then we prefer the unchecked (specific) type of exception.
1036 maybeHandleServiceSpecificException(sse);
1037 // If not, then all we can do is provide the SSE in the form of an IOException.
1038 throw new ErrnoException(
1039 "IpSec encountered errno=" + sse.errorCode, sse.errorCode).rethrowAsIOException();
1040 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001041}