blob: 819ac6bdecf932aae9e88f1232c73b5ef9143b04 [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
Nathan Harold031acb82017-03-07 13:23:36 -080020import android.annotation.NonNull;
Benedict Wonge9763752018-11-08 19:45:34 -080021import android.annotation.RequiresFeature;
Nathan Harold025aae12018-02-02 18:34:25 -080022import android.annotation.RequiresPermission;
Nathan Harold3a287d52018-04-25 12:01:34 -070023import android.annotation.SystemApi;
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Jonathan Basseri2725a232017-04-21 15:53:51 -070025import android.annotation.TestApi;
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060026import android.content.Context;
Benedict Wonge9763752018-11-08 19:45:34 -080027import android.content.pm.PackageManager;
evitayan30878b02019-12-30 16:05:38 -080028import android.net.annotations.PolicyDirection;
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;
40
Nathan Haroldb1f605f2017-01-12 18:38:57 -080041import dalvik.system.CloseGuard;
Nathan Harold81f54632017-09-11 19:50:19 -070042
Nathan Haroldb1f605f2017-01-12 18:38:57 -080043import java.io.FileDescriptor;
44import java.io.IOException;
45import java.net.DatagramSocket;
46import java.net.InetAddress;
47import java.net.Socket;
Aaron Huang49eef6f2021-12-22 16:35:13 +080048import java.util.Objects;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080049
50/**
Jonathan Basseri2725a232017-04-21 15:53:51 -070051 * This class contains methods for managing IPsec sessions. Once configured, the kernel will apply
52 * confidentiality (encryption) and integrity (authentication) to IP traffic.
Nathan Haroldb1f605f2017-01-12 18:38:57 -080053 *
Jonathan Basseri2725a232017-04-21 15:53:51 -070054 * <p>Note that not all aspects of IPsec are permitted by this API. Applications may create
55 * transport mode security associations and apply them to individual sockets. Applications looking
Benedict Wongc85b7b02019-11-12 22:31:51 -080056 * to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}.
Jonathan Basseri2725a232017-04-21 15:53:51 -070057 *
58 * @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the
Jonathan Basseri20e96c52017-11-16 10:58:01 -080059 * Internet Protocol</a>
Nathan Haroldb1f605f2017-01-12 18:38:57 -080060 */
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060061@SystemService(Context.IPSEC_SERVICE)
Nathan Haroldb1f605f2017-01-12 18:38:57 -080062public final class IpSecManager {
63 private static final String TAG = "IpSecManager";
64
65 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080066 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
67 * towards the host.
68 *
69 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080070 */
71 public static final int DIRECTION_IN = 0;
72
73 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080074 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
75 * away from the host.
76 *
77 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080078 */
79 public static final int DIRECTION_OUT = 1;
80
Nathan Harold5a19b952018-01-05 19:25:13 -080081 /**
Benedict Wong908d34e2021-04-15 11:59:16 -070082 * Used when applying a transform to direct traffic through an {@link IpSecTransform} for
83 * forwarding between interfaces.
84 *
85 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
86 *
87 * @hide
88 */
Aaron Huang4989e952021-12-15 23:27:18 +080089 @SystemApi(client = MODULE_LIBRARIES)
Benedict Wong908d34e2021-04-15 11:59:16 -070090 public static final int DIRECTION_FWD = 2;
91
92 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -070093 * The Security Parameter Index (SPI) 0 indicates an unknown or invalid index.
Nathan Harold031acb82017-03-07 13:23:36 -080094 *
95 * <p>No IPsec packet may contain an SPI of 0.
Jonathan Basseri2725a232017-04-21 15:53:51 -070096 *
97 * @hide
Nathan Harold031acb82017-03-07 13:23:36 -080098 */
Jonathan Basseri20e96c52017-11-16 10:58:01 -080099 @TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0;
Nathan Harold031acb82017-03-07 13:23:36 -0800100
101 /** @hide */
102 public interface Status {
Aaron Huangfbae3082021-12-06 15:18:42 +0800103 int OK = 0;
104 int RESOURCE_UNAVAILABLE = 1;
105 int SPI_UNAVAILABLE = 2;
Nathan Harold031acb82017-03-07 13:23:36 -0800106 }
107
108 /** @hide */
Nathan Harold5e854462017-12-13 18:51:35 -0800109 public static final int INVALID_RESOURCE_ID = -1;
Nathan Harold031acb82017-03-07 13:23:36 -0800110
111 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700112 * Thrown to indicate that a requested SPI is in use.
113 *
114 * <p>The combination of remote {@code InetAddress} and SPI must be unique across all apps on
115 * one device. If this error is encountered, a new SPI is required before a transform may be
116 * created. This error can be avoided by calling {@link
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800117 * IpSecManager#allocateSecurityParameterIndex}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800118 */
119 public static final class SpiUnavailableException extends AndroidException {
120 private final int mSpi;
121
122 /**
123 * Construct an exception indicating that a transform with the given SPI is already in use
124 * or otherwise unavailable.
125 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700126 * @param msg description indicating the colliding SPI
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800127 * @param spi the SPI that could not be used due to a collision
128 */
129 SpiUnavailableException(String msg, int spi) {
Jonathan Basseri2725a232017-04-21 15:53:51 -0700130 super(msg + " (spi: " + spi + ")");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800131 mSpi = spi;
132 }
133
Jonathan Basseri2725a232017-04-21 15:53:51 -0700134 /** Get the SPI that caused a collision. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800135 public int getSpi() {
136 return mSpi;
137 }
138 }
139
140 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700141 * Thrown to indicate that an IPsec resource is unavailable.
142 *
143 * <p>This could apply to resources such as sockets, {@link SecurityParameterIndex}, {@link
144 * IpSecTransform}, or other system resources. If this exception is thrown, users should release
145 * allocated objects of the type requested.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800146 */
147 public static final class ResourceUnavailableException extends AndroidException {
148
149 ResourceUnavailableException(String msg) {
150 super(msg);
151 }
152 }
153
Nathan Harold65ef8432018-03-15 18:06:06 -0700154 private final Context mContext;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800155 private final IIpSecService mService;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800156
Jonathan Basseri2725a232017-04-21 15:53:51 -0700157 /**
158 * This class represents a reserved SPI.
159 *
160 * <p>Objects of this type are used to track reserved security parameter indices. They can be
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800161 * obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released
Jonathan Basseri2725a232017-04-21 15:53:51 -0700162 * by calling {@link #close()} when they are no longer needed.
163 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800164 public static final class SecurityParameterIndex implements AutoCloseable {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800165 private final IIpSecService mService;
Nathan Harold5a19b952018-01-05 19:25:13 -0800166 private final InetAddress mDestinationAddress;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800167 private final CloseGuard mCloseGuard = CloseGuard.get();
Nathan Harold031acb82017-03-07 13:23:36 -0800168 private int mSpi = INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold5e854462017-12-13 18:51:35 -0800169 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800170
Jonathan Basseri2725a232017-04-21 15:53:51 -0700171 /** Get the underlying SPI held by this object. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800172 public int getSpi() {
173 return mSpi;
174 }
175
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800176 /**
177 * Release an SPI that was previously reserved.
178 *
Nathan Harold909d66e2017-03-29 10:47:59 -0700179 * <p>Release an SPI for use by other users in the system. If a SecurityParameterIndex is
180 * applied to an IpSecTransform, it will become unusable for future transforms but should
181 * still be closed to ensure system resources are released.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800182 */
183 @Override
184 public void close() {
Nathan Harold80865392017-04-04 19:37:48 -0700185 try {
186 mService.releaseSecurityParameterIndex(mResourceId);
187 } catch (RemoteException e) {
188 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700189 } catch (Exception e) {
190 // On close we swallow all random exceptions since failure to close is not
191 // actionable by the user.
192 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
193 } finally {
194 mResourceId = INVALID_RESOURCE_ID;
195 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700196 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800197 }
198
Jonathan Basseri2725a232017-04-21 15:53:51 -0700199 /** Check that the SPI was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800200 @Override
Nathan Harold2a30daf2017-11-07 17:17:45 -0800201 protected void finalize() throws Throwable {
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800202 if (mCloseGuard != null) {
203 mCloseGuard.warnIfOpen();
204 }
205
206 close();
207 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800208
Nathan Harold031acb82017-03-07 13:23:36 -0800209 private SecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800210 @NonNull IIpSecService service, InetAddress destinationAddress, int spi)
Nathan Harold031acb82017-03-07 13:23:36 -0800211 throws ResourceUnavailableException, SpiUnavailableException {
212 mService = service;
Nathan Harold5a19b952018-01-05 19:25:13 -0800213 mDestinationAddress = destinationAddress;
Nathan Harold031acb82017-03-07 13:23:36 -0800214 try {
Nathan Harold80865392017-04-04 19:37:48 -0700215 IpSecSpiResponse result =
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800216 mService.allocateSecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800217 destinationAddress.getHostAddress(), spi, new Binder());
Nathan Harold031acb82017-03-07 13:23:36 -0800218
219 if (result == null) {
220 throw new NullPointerException("Received null response from IpSecService");
221 }
222
Nathan Harold80865392017-04-04 19:37:48 -0700223 int status = result.status;
Nathan Harold031acb82017-03-07 13:23:36 -0800224 switch (status) {
225 case Status.OK:
226 break;
227 case Status.RESOURCE_UNAVAILABLE:
228 throw new ResourceUnavailableException(
229 "No more SPIs may be allocated by this requester.");
230 case Status.SPI_UNAVAILABLE:
231 throw new SpiUnavailableException("Requested SPI is unavailable", spi);
232 default:
233 throw new RuntimeException(
234 "Unknown status returned by IpSecService: " + status);
235 }
Nathan Harold80865392017-04-04 19:37:48 -0700236 mSpi = result.spi;
237 mResourceId = result.resourceId;
Nathan Harold031acb82017-03-07 13:23:36 -0800238
239 if (mSpi == INVALID_SECURITY_PARAMETER_INDEX) {
240 throw new RuntimeException("Invalid SPI returned by IpSecService: " + status);
241 }
242
243 if (mResourceId == INVALID_RESOURCE_ID) {
244 throw new RuntimeException(
245 "Invalid Resource ID returned by IpSecService: " + status);
246 }
Nathan Harold031acb82017-03-07 13:23:36 -0800247 } catch (RemoteException e) {
248 throw e.rethrowFromSystemServer();
249 }
250 mCloseGuard.open("open");
251 }
Nathan Harold80865392017-04-04 19:37:48 -0700252
253 /** @hide */
Nathan Harold19b99d92017-08-23 13:46:33 -0700254 @VisibleForTesting
255 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700256 return mResourceId;
257 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700258
259 @Override
260 public String toString() {
261 return new StringBuilder()
262 .append("SecurityParameterIndex{spi=")
263 .append(mSpi)
264 .append(",resourceId=")
265 .append(mResourceId)
266 .append("}")
267 .toString();
268 }
Nathan Harold031acb82017-03-07 13:23:36 -0800269 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800270
271 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800272 * Reserve a random SPI for traffic bound to or from the specified destination address.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800273 *
274 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
275 * SecurityParameterIndex#close()}.
276 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800277 * @param destinationAddress the destination address for traffic bearing the requested SPI.
278 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800279 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800280 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800281 * currently allocated for this user
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800282 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700283 @NonNull
284 public SecurityParameterIndex allocateSecurityParameterIndex(
285 @NonNull InetAddress destinationAddress) throws ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700286 try {
287 return new SecurityParameterIndex(
288 mService,
Nathan Harold5a19b952018-01-05 19:25:13 -0800289 destinationAddress,
Nathan Harold05406792017-04-06 18:16:28 -0700290 IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700291 } catch (ServiceSpecificException e) {
292 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
Nathan Harold05406792017-04-06 18:16:28 -0700293 } catch (SpiUnavailableException unlikely) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700294 // Because this function allocates a totally random SPI, it really shouldn't ever
295 // fail to allocate an SPI; we simply need this because the exception is checked.
Nathan Harold05406792017-04-06 18:16:28 -0700296 throw new ResourceUnavailableException("No SPIs available");
297 }
298 }
299
300 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800301 * Reserve the requested SPI for traffic bound to or from the specified destination address.
Nathan Harold05406792017-04-06 18:16:28 -0700302 *
303 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
304 * SecurityParameterIndex#close()}.
305 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800306 * @param destinationAddress the destination address for traffic bearing the requested SPI.
307 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700308 * @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See
309 * RFC 4303 Section 2.1.
Nathan Harold05406792017-04-06 18:16:28 -0700310 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800311 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800312 * currently allocated for this user
Aaron Huangfbae3082021-12-06 15:18:42 +0800313 * @throws SpiUnavailableException indicating that the requested SPI could not be
Nathan Harold5a19b952018-01-05 19:25:13 -0800314 * reserved
Nathan Harold05406792017-04-06 18:16:28 -0700315 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700316 @NonNull
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800317 public SecurityParameterIndex allocateSecurityParameterIndex(
Nathan Harold6303d9e2018-03-16 17:27:30 -0700318 @NonNull InetAddress destinationAddress, int requestedSpi)
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800319 throws SpiUnavailableException, ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700320 if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
321 throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
322 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700323 try {
324 return new SecurityParameterIndex(mService, destinationAddress, requestedSpi);
325 } catch (ServiceSpecificException e) {
326 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
327 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800328 }
329
330 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700331 * Apply an IPsec transform to a stream socket.
332 *
333 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
334 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800335 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700336 * unprotected traffic can resume on that socket.
337 *
338 * <p>For security reasons, the destination address of any traffic on the socket must match the
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800339 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
Jonathan Basseri2725a232017-04-21 15:53:51 -0700340 * other IP address will result in an IOException. In addition, reads and writes on the socket
341 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800342 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700343 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700344 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
345 * applied transform before completion of graceful shutdown may result in the shutdown sequence
346 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
347 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
348 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
349 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
350 * sufficient to ensure shutdown.
351 *
352 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
353 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
354 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
355 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
356 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800357 * <h4>Rekey Procedure</h4>
358 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800359 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
360 * will be removed and the new transform will take effect immediately, sending all traffic on
361 * the new transform; however, when applying a transform in the inbound direction, traffic
362 * on the old transform will continue to be decrypted and delivered until that transform is
363 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
364 * procedures where both transforms are valid until both endpoints are using the new transform
365 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800366 *
367 * @param socket a stream socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800368 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700369 * @param transform a transport mode {@code IpSecTransform}
370 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800371 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700372 public void applyTransportModeTransform(@NonNull Socket socket,
373 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700374 // Ensure creation of FD. See b/77548890 for more details.
375 socket.getSoLinger();
376
Nathan Harold31676252018-01-16 12:08:43 -0800377 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800378 }
379
380 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700381 * Apply an IPsec transform to a datagram socket.
382 *
383 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
384 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800385 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700386 * unprotected traffic can resume on that socket.
387 *
388 * <p>For security reasons, the destination address of any traffic on the socket must match the
389 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
390 * other IP address will result in an IOException. In addition, reads and writes on the socket
391 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800392 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700393 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800394 * <h4>Rekey Procedure</h4>
395 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800396 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
397 * will be removed and the new transform will take effect immediately, sending all traffic on
398 * the new transform; however, when applying a transform in the inbound direction, traffic
399 * on the old transform will continue to be decrypted and delivered until that transform is
400 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
401 * procedures where both transforms are valid until both endpoints are using the new transform
402 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800403 *
404 * @param socket a datagram socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800405 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700406 * @param transform a transport mode {@code IpSecTransform}
407 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800408 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700409 public void applyTransportModeTransform(@NonNull DatagramSocket socket,
410 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold31676252018-01-16 12:08:43 -0800411 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Harold031acb82017-03-07 13:23:36 -0800412 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800413
414 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700415 * Apply an IPsec transform to a socket.
416 *
417 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
418 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800419 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700420 * unprotected traffic can resume on that socket.
421 *
422 * <p>For security reasons, the destination address of any traffic on the socket must match the
423 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
424 * other IP address will result in an IOException. In addition, reads and writes on the socket
425 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800426 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700427 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700428 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
429 * applied transform before completion of graceful shutdown may result in the shutdown sequence
430 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
431 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
432 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
433 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
434 * sufficient to ensure shutdown.
435 *
436 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
437 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
438 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
439 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
440 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800441 * <h4>Rekey Procedure</h4>
442 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800443 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
444 * will be removed and the new transform will take effect immediately, sending all traffic on
445 * the new transform; however, when applying a transform in the inbound direction, traffic
446 * on the old transform will continue to be decrypted and delivered until that transform is
447 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
448 * procedures where both transforms are valid until both endpoints are using the new transform
449 * and all in-flight packets have been received.
Nathan Harold0072e192017-04-06 17:46:00 -0700450 *
451 * @param socket a socket file descriptor
Nathan Harolde3536f02018-03-06 13:22:22 -0800452 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700453 * @param transform a transport mode {@code IpSecTransform}
454 * @throws IOException indicating that the transform could not be applied
Nathan Harold0072e192017-04-06 17:46:00 -0700455 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700456 public void applyTransportModeTransform(@NonNull FileDescriptor socket,
457 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700458 // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
Nathan Harold31676252018-01-16 12:08:43 -0800459 // constructor takes control and closes the user's FD when we exit the method.
Nathan Harold80865392017-04-04 19:37:48 -0700460 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold5a19b952018-01-05 19:25:13 -0800461 mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700462 } catch (ServiceSpecificException e) {
463 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold80865392017-04-04 19:37:48 -0700464 } catch (RemoteException e) {
465 throw e.rethrowFromSystemServer();
466 }
Nathan Harold0072e192017-04-06 17:46:00 -0700467 }
468
469 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700470 * Remove an IPsec transform from a stream socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800471 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800472 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
473 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700474 *
475 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
476 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
477 * is called.
478 *
479 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700480 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800481 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700482 public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700483 // Ensure creation of FD. See b/77548890 for more details.
484 socket.getSoLinger();
485
Nathan Harold0d483b72018-01-17 01:00:20 -0800486 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800487 }
488
489 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700490 * Remove an IPsec transform from a datagram socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800491 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800492 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
493 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700494 *
495 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
496 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
497 * is called.
498 *
499 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700500 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800501 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700502 public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException {
Nathan Harold0d483b72018-01-17 01:00:20 -0800503 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800504 }
505
Nathan Harold0072e192017-04-06 17:46:00 -0700506 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700507 * Remove an IPsec transform from a socket.
Nathan Harold0072e192017-04-06 17:46:00 -0700508 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800509 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
510 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700511 *
512 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
513 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
514 * is called.
515 *
516 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700517 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Harold0072e192017-04-06 17:46:00 -0700518 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700519 public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700520 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold0d483b72018-01-17 01:00:20 -0800521 mService.removeTransportModeTransforms(pfd);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700522 } catch (ServiceSpecificException e) {
523 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold031acb82017-03-07 13:23:36 -0800524 } catch (RemoteException e) {
525 throw e.rethrowFromSystemServer();
526 }
527 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800528
529 /**
530 * Remove a Tunnel Mode IPsec Transform from a {@link Network}. This must be used as part of
531 * cleanup if a tunneled Network experiences a change in default route. The Network will drop
532 * all traffic that cannot be routed to the Tunnel's outbound interface. If that interface is
533 * lost, all traffic will drop.
534 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800535 * <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked.
536 *
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800537 * @param net a network that currently has transform applied to it.
538 * @param transform a Tunnel Mode IPsec Transform that has been previously applied to the given
539 * network
540 * @hide
541 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800542 public void removeTunnelModeTransform(Network net, IpSecTransform transform) {}
543
544 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700545 * This class provides access to a UDP encapsulation Socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800546 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700547 * <p>{@code UdpEncapsulationSocket} wraps a system-provided datagram socket intended for IKEv2
548 * signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link
549 * IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the
550 * caller. The caller should not close the {@code FileDescriptor} returned by {@link
Benedict Wonga386e372018-03-27 16:55:48 -0700551 * #getFileDescriptor}, but should use {@link #close} instead.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700552 *
553 * <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic
554 * of the next user who binds to that port. To prevent this scenario, these sockets are held
555 * open by the system so that they may only be closed by calling {@link #close} or when the user
556 * process exits.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800557 */
558 public static final class UdpEncapsulationSocket implements AutoCloseable {
Nathan Harold80865392017-04-04 19:37:48 -0700559 private final ParcelFileDescriptor mPfd;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800560 private final IIpSecService mService;
Nathan Harold5e854462017-12-13 18:51:35 -0800561 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700562 private final int mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800563 private final CloseGuard mCloseGuard = CloseGuard.get();
564
Nathan Harold031acb82017-03-07 13:23:36 -0800565 private UdpEncapsulationSocket(@NonNull IIpSecService service, int port)
Nathan Harold80865392017-04-04 19:37:48 -0700566 throws ResourceUnavailableException, IOException {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800567 mService = service;
Nathan Harold80865392017-04-04 19:37:48 -0700568 try {
569 IpSecUdpEncapResponse result =
570 mService.openUdpEncapsulationSocket(port, new Binder());
571 switch (result.status) {
572 case Status.OK:
573 break;
574 case Status.RESOURCE_UNAVAILABLE:
575 throw new ResourceUnavailableException(
576 "No more Sockets may be allocated by this requester.");
577 default:
578 throw new RuntimeException(
579 "Unknown status returned by IpSecService: " + result.status);
580 }
581 mResourceId = result.resourceId;
582 mPort = result.port;
583 mPfd = result.fileDescriptor;
584 } catch (RemoteException e) {
585 throw e.rethrowFromSystemServer();
586 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800587 mCloseGuard.open("constructor");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800588 }
589
Benedict Wonga386e372018-03-27 16:55:48 -0700590 /** Get the encapsulation socket's file descriptor. */
591 public FileDescriptor getFileDescriptor() {
Nathan Harold80865392017-04-04 19:37:48 -0700592 if (mPfd == null) {
593 return null;
594 }
595 return mPfd.getFileDescriptor();
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800596 }
597
Jonathan Basseri2725a232017-04-21 15:53:51 -0700598 /** Get the bound port of the wrapped socket. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800599 public int getPort() {
Nathan Harold80865392017-04-04 19:37:48 -0700600 return mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800601 }
602
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800603 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700604 * Close this socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800605 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700606 * <p>This closes the wrapped socket. Open encapsulation sockets count against a user's
607 * resource limits, and forgetting to close them eventually will result in {@link
608 * ResourceUnavailableException} being thrown.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800609 */
Jonathan Basseri2725a232017-04-21 15:53:51 -0700610 @Override
Nathan Harolda64b0192017-04-17 17:11:58 -0700611 public void close() throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700612 try {
613 mService.closeUdpEncapsulationSocket(mResourceId);
Nathan Harold5e854462017-12-13 18:51:35 -0800614 mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700615 } catch (RemoteException e) {
616 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700617 } catch (Exception e) {
618 // On close we swallow all random exceptions since failure to close is not
619 // actionable by the user.
620 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
621 } finally {
622 mResourceId = INVALID_RESOURCE_ID;
623 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700624 }
625
626 try {
627 mPfd.close();
628 } catch (IOException e) {
629 Log.e(TAG, "Failed to close UDP Encapsulation Socket with Port= " + mPort);
630 throw e;
631 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800632 }
633
Jonathan Basseri2725a232017-04-21 15:53:51 -0700634 /** Check that the socket was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800635 @Override
636 protected void finalize() throws Throwable {
637 if (mCloseGuard != null) {
638 mCloseGuard.warnIfOpen();
639 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800640 close();
641 }
Nathan Harold80865392017-04-04 19:37:48 -0700642
643 /** @hide */
Chiachang Wanga0343312021-01-12 18:48:13 +0800644 @SystemApi(client = MODULE_LIBRARIES)
Nathan Harold19b99d92017-08-23 13:46:33 -0700645 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700646 return mResourceId;
647 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700648
649 @Override
650 public String toString() {
651 return new StringBuilder()
652 .append("UdpEncapsulationSocket{port=")
653 .append(mPort)
654 .append(",resourceId=")
655 .append(mResourceId)
656 .append("}")
657 .toString();
658 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800659 };
660
661 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700662 * Open a socket for UDP encapsulation and bind to the given port.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800663 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700664 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800665 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700666 * @param port a local UDP port
667 * @return a socket that is bound to the given port
668 * @throws IOException indicating that the socket could not be opened or bound
669 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800670 */
671 // Returning a socket in this fashion that has been created and bound by the system
672 // is the only safe way to ensure that a socket is both accessible to the user and
673 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
674 // the port, which could potentially impact the traffic of the next user who binds to that
675 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700676 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800677 public UdpEncapsulationSocket openUdpEncapsulationSocket(int port)
678 throws IOException, ResourceUnavailableException {
Nathan Harold80865392017-04-04 19:37:48 -0700679 /*
680 * Most range checking is done in the service, but this version of the constructor expects
681 * a valid port number, and zero cannot be checked after being passed to the service.
682 */
683 if (port == 0) {
684 throw new IllegalArgumentException("Specified port must be a valid port number!");
685 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700686 try {
687 return new UdpEncapsulationSocket(mService, port);
688 } catch (ServiceSpecificException e) {
689 throw rethrowCheckedExceptionFromServiceSpecificException(e);
690 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800691 }
692
693 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700694 * Open a socket for UDP encapsulation.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800695 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700696 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800697 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700698 * <p>The local port of the returned socket can be obtained by calling {@link
699 * UdpEncapsulationSocket#getPort()}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800700 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700701 * @return a socket that is bound to a local port
702 * @throws IOException indicating that the socket could not be opened or bound
703 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800704 */
705 // Returning a socket in this fashion that has been created and bound by the system
706 // is the only safe way to ensure that a socket is both accessible to the user and
707 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
708 // the port, which could potentially impact the traffic of the next user who binds to that
709 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700710 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800711 public UdpEncapsulationSocket openUdpEncapsulationSocket()
712 throws IOException, ResourceUnavailableException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700713 try {
714 return new UdpEncapsulationSocket(mService, 0);
715 } catch (ServiceSpecificException e) {
716 throw rethrowCheckedExceptionFromServiceSpecificException(e);
717 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800718 }
719
720 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000721 * This class represents an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800722 *
723 * <p>IpSecTunnelInterface objects track tunnel interfaces that serve as
724 * local endpoints for IPsec tunnels.
725 *
726 * <p>Creating an IpSecTunnelInterface creates a device to which IpSecTransforms may be
727 * applied to provide IPsec security to packets sent through the tunnel. While a tunnel
728 * cannot be used in standalone mode within Android, the higher layers may use the tunnel
729 * to create Network objects which are accessible to the Android system.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000730 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800731 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000732 @SystemApi
Nathan Harold67e76732018-01-17 16:09:24 -0800733 public static final class IpSecTunnelInterface implements AutoCloseable {
Nathan Harold65ef8432018-03-15 18:06:06 -0700734 private final String mOpPackageName;
Nathan Harold67e76732018-01-17 16:09:24 -0800735 private final IIpSecService mService;
736 private final InetAddress mRemoteAddress;
737 private final InetAddress mLocalAddress;
738 private final Network mUnderlyingNetwork;
739 private final CloseGuard mCloseGuard = CloseGuard.get();
740 private String mInterfaceName;
741 private int mResourceId = INVALID_RESOURCE_ID;
742
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000743 /** Get the underlying SPI held by this object. */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700744 @NonNull
Nathan Harold67e76732018-01-17 16:09:24 -0800745 public String getInterfaceName() {
746 return mInterfaceName;
747 }
748
749 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000750 * Add an address to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800751 *
752 * <p>Add an address which may be used as the local inner address for
753 * tunneled traffic.
754 *
755 * @param address the local address for traffic inside the tunnel
Benedict Wongd39837f2018-04-03 20:30:54 -0700756 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000757 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800758 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000759 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800760 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700761 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700762 public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800763 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700764 mService.addAddressToTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700765 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700766 } catch (ServiceSpecificException e) {
767 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800768 } catch (RemoteException e) {
769 throw e.rethrowFromSystemServer();
770 }
Nathan Harold67e76732018-01-17 16:09:24 -0800771 }
772
773 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000774 * Remove an address from the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800775 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000776 * <p>Remove an address which was previously added to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800777 *
778 * @param address to be removed
Benedict Wongd39837f2018-04-03 20:30:54 -0700779 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000780 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800781 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000782 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800783 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700784 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700785 public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800786 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700787 mService.removeAddressFromTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700788 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700789 } catch (ServiceSpecificException e) {
790 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800791 } catch (RemoteException e) {
792 throw e.rethrowFromSystemServer();
793 }
Nathan Harold67e76732018-01-17 16:09:24 -0800794 }
795
Yan Yana2f3b492020-09-29 23:38:00 -0700796 /**
797 * Update the underlying network for this IpSecTunnelInterface.
798 *
799 * <p>This new underlying network will be used for all transforms applied AFTER this call is
800 * complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to
801 * this tunnel interface, traffic will still use the old SA, and be routed on the old
802 * underlying network.
803 *
804 * <p>To migrate IPsec tunnel mode traffic, a caller should:
805 *
806 * <ol>
807 * <li>Update the IpSecTunnelInterface’s underlying network.
808 * <li>Apply {@link IpSecTransform}(s) with matching addresses to this
809 * IpSecTunnelInterface.
810 * </ol>
811 *
812 * @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel.
813 * This network MUST never be the network exposing this IpSecTunnelInterface, otherwise
Yan Yancf4d2eb2021-02-16 11:48:55 -0800814 * this method will throw an {@link IllegalArgumentException}. If the
815 * IpSecTunnelInterface is later added to this network, all outbound traffic will be
816 * blackholed.
Yan Yana2f3b492020-09-29 23:38:00 -0700817 */
818 // TODO: b/169171001 Update the documentation when transform migration is supported.
819 // The purpose of making updating network and applying transforms separate is to leave open
820 // the possibility to support lossless migration procedures. To do that, Android platform
821 // will need to support multiple inbound tunnel mode transforms, just like it can support
822 // multiple transport mode transforms.
823 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
824 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
825 public void setUnderlyingNetwork(@NonNull Network underlyingNetwork) throws IOException {
826 try {
827 mService.setNetworkForTunnelInterface(
828 mResourceId, underlyingNetwork, mOpPackageName);
829 } catch (RemoteException e) {
830 throw e.rethrowFromSystemServer();
831 }
832 }
833
Nathan Harold65ef8432018-03-15 18:06:06 -0700834 private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
Nathan Harold67e76732018-01-17 16:09:24 -0800835 @NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
836 @NonNull Network underlyingNetwork)
837 throws ResourceUnavailableException, IOException {
Nathan Harold65ef8432018-03-15 18:06:06 -0700838 mOpPackageName = ctx.getOpPackageName();
Nathan Harold67e76732018-01-17 16:09:24 -0800839 mService = service;
840 mLocalAddress = localAddress;
841 mRemoteAddress = remoteAddress;
842 mUnderlyingNetwork = underlyingNetwork;
Benedict Wong8bc90732018-01-18 18:31:45 -0800843
844 try {
845 IpSecTunnelInterfaceResponse result =
846 mService.createTunnelInterface(
847 localAddress.getHostAddress(),
848 remoteAddress.getHostAddress(),
849 underlyingNetwork,
Nathan Harold65ef8432018-03-15 18:06:06 -0700850 new Binder(),
851 mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800852 switch (result.status) {
853 case Status.OK:
854 break;
855 case Status.RESOURCE_UNAVAILABLE:
856 throw new ResourceUnavailableException(
857 "No more tunnel interfaces may be allocated by this requester.");
858 default:
859 throw new RuntimeException(
860 "Unknown status returned by IpSecService: " + result.status);
861 }
862 mResourceId = result.resourceId;
863 mInterfaceName = result.interfaceName;
864 } catch (RemoteException e) {
865 throw e.rethrowFromSystemServer();
866 }
867 mCloseGuard.open("constructor");
Nathan Harold67e76732018-01-17 16:09:24 -0800868 }
869
870 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000871 * Delete an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800872 *
873 * <p>Calling close will deallocate the IpSecTunnelInterface and all of its system
874 * resources. Any packets bound for this interface either inbound or outbound will
875 * all be lost.
876 */
877 @Override
878 public void close() {
Benedict Wong8bc90732018-01-18 18:31:45 -0800879 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700880 mService.deleteTunnelInterface(mResourceId, mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800881 } catch (RemoteException e) {
882 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700883 } catch (Exception e) {
884 // On close we swallow all random exceptions since failure to close is not
885 // actionable by the user.
886 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
887 } finally {
888 mResourceId = INVALID_RESOURCE_ID;
889 mCloseGuard.close();
Benedict Wong8bc90732018-01-18 18:31:45 -0800890 }
Nathan Harold67e76732018-01-17 16:09:24 -0800891 }
892
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000893 /** Check that the Interface was closed properly. */
Nathan Harold67e76732018-01-17 16:09:24 -0800894 @Override
895 protected void finalize() throws Throwable {
896 if (mCloseGuard != null) {
897 mCloseGuard.warnIfOpen();
898 }
899 close();
900 }
Benedict Wong8bc90732018-01-18 18:31:45 -0800901
902 /** @hide */
903 @VisibleForTesting
904 public int getResourceId() {
905 return mResourceId;
906 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700907
Aurimas Liutikas3a78d9d2019-08-28 13:01:05 -0700908 @NonNull
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700909 @Override
910 public String toString() {
911 return new StringBuilder()
912 .append("IpSecTunnelInterface{ifname=")
913 .append(mInterfaceName)
914 .append(",resourceId=")
915 .append(mResourceId)
916 .append("}")
917 .toString();
918 }
Nathan Harold67e76732018-01-17 16:09:24 -0800919 }
920
921 /**
922 * Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic.
923 *
Benedict Wong8bc90732018-01-18 18:31:45 -0800924 * <p>An application that creates tunnels is responsible for cleaning up the tunnel when the
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000925 * underlying network goes away, and the onLost() callback is received.
Benedict Wong8bc90732018-01-18 18:31:45 -0800926 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000927 * @param localAddress The local addres of the tunnel
928 * @param remoteAddress The local addres of the tunnel
929 * @param underlyingNetwork the {@link Network} that will carry traffic for this tunnel.
930 * This network should almost certainly be a network such as WiFi with an L2 address.
931 * @return a new {@link IpSecManager#IpSecTunnelInterface} with the specified properties
932 * @throws IOException indicating that the socket could not be opened or bound
933 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Yan Yan160aac32020-12-03 17:08:29 -0800934 * @hide
Yan Yan160aac32020-12-03 17:08:29 -0800935 */
Nathan Harold3a287d52018-04-25 12:01:34 -0700936 @SystemApi
Nathan Harold6303d9e2018-03-16 17:27:30 -0700937 @NonNull
Benedict Wonge9763752018-11-08 19:45:34 -0800938 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700939 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold67e76732018-01-17 16:09:24 -0800940 public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
941 @NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
942 throws ResourceUnavailableException, IOException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700943 try {
944 return new IpSecTunnelInterface(
945 mContext, mService, localAddress, remoteAddress, underlyingNetwork);
946 } catch (ServiceSpecificException e) {
947 throw rethrowCheckedExceptionFromServiceSpecificException(e);
948 }
Nathan Harold67e76732018-01-17 16:09:24 -0800949 }
950
951 /**
Nathan Harolde3536f02018-03-06 13:22:22 -0800952 * Apply an active Tunnel Mode IPsec Transform to a {@link IpSecTunnelInterface}, which will
953 * tunnel all traffic for the given direction through the underlying network's interface with
954 * IPsec (applies an outer IP header and IPsec Header to all traffic, and expects an additional
955 * IP header and IPsec Header on all inbound traffic).
956 * <p>Applications should probably not use this API directly.
957 *
Nathan Harold67e76732018-01-17 16:09:24 -0800958 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000959 * @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied
Nathan Harold67e76732018-01-17 16:09:24 -0800960 * transform.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000961 * @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which
Nathan Harold67e76732018-01-17 16:09:24 -0800962 * the transform will be used.
963 * @param transform an {@link IpSecTransform} created in tunnel mode
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000964 * @throws IOException indicating that the transform could not be applied due to a lower
965 * layer failure.
966 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800967 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000968 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800969 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700970 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold6303d9e2018-03-16 17:27:30 -0700971 public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel,
972 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong8bc90732018-01-18 18:31:45 -0800973 try {
974 mService.applyTunnelModeTransform(
Nathan Harold65ef8432018-03-15 18:06:06 -0700975 tunnel.getResourceId(), direction,
976 transform.getResourceId(), mContext.getOpPackageName());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700977 } catch (ServiceSpecificException e) {
978 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong8bc90732018-01-18 18:31:45 -0800979 } catch (RemoteException e) {
980 throw e.rethrowFromSystemServer();
981 }
Nathan Harold67e76732018-01-17 16:09:24 -0800982 }
Nathan Harolde3536f02018-03-06 13:22:22 -0800983
Nathan Harold67e76732018-01-17 16:09:24 -0800984 /**
Aaron Huang80a73fd2021-12-28 02:33:53 +0800985 * @hide
986 */
987 public IpSecTransformResponse createTransform(IpSecConfig config, IBinder binder,
988 String callingPackage) {
989 try {
990 return mService.createTransform(config, binder, callingPackage);
991 } catch (RemoteException e) {
992 throw e.rethrowFromSystemServer();
993 }
994 }
995
996 /**
997 * @hide
998 */
999 public void deleteTransform(int resourceId) {
1000 try {
1001 mService.deleteTransform(resourceId);
1002 } catch (RemoteException e) {
1003 throw e.rethrowFromSystemServer();
1004 }
1005 }
1006
1007 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -07001008 * Construct an instance of IpSecManager within an application context.
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001009 *
1010 * @param context the application context for this manager
1011 * @hide
1012 */
Nathan Harold65ef8432018-03-15 18:06:06 -07001013 public IpSecManager(Context ctx, IIpSecService service) {
1014 mContext = ctx;
Aaron Huang49eef6f2021-12-22 16:35:13 +08001015 mService = Objects.requireNonNull(service, "missing service");
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001016 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001017
1018 private static void maybeHandleServiceSpecificException(ServiceSpecificException sse) {
1019 // OsConstants are late binding, so switch statements can't be used.
1020 if (sse.errorCode == OsConstants.EINVAL) {
1021 throw new IllegalArgumentException(sse);
1022 } else if (sse.errorCode == OsConstants.EAGAIN) {
1023 throw new IllegalStateException(sse);
Benedict Wongc45974b2018-07-16 14:56:20 -07001024 } else if (sse.errorCode == OsConstants.EOPNOTSUPP
1025 || sse.errorCode == OsConstants.EPROTONOSUPPORT) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001026 throw new UnsupportedOperationException(sse);
1027 }
1028 }
1029
1030 /**
1031 * Convert an Errno SSE to the correct Unchecked exception type.
1032 *
1033 * This method never actually returns.
1034 */
1035 // package
1036 static RuntimeException
1037 rethrowUncheckedExceptionFromServiceSpecificException(ServiceSpecificException sse) {
1038 maybeHandleServiceSpecificException(sse);
1039 throw new RuntimeException(sse);
1040 }
1041
1042 /**
1043 * Convert an Errno SSE to the correct Checked or Unchecked exception type.
1044 *
1045 * This method may throw IOException, or it may throw an unchecked exception; it will never
1046 * actually return.
1047 */
1048 // package
1049 static IOException rethrowCheckedExceptionFromServiceSpecificException(
1050 ServiceSpecificException sse) throws IOException {
1051 // First see if this is an unchecked exception of a type we know.
1052 // If so, then we prefer the unchecked (specific) type of exception.
1053 maybeHandleServiceSpecificException(sse);
1054 // If not, then all we can do is provide the SSE in the form of an IOException.
1055 throw new ErrnoException(
1056 "IpSec encountered errno=" + sse.errorCode, sse.errorCode).rethrowAsIOException();
1057 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001058}