blob: ca9be1cc40c7014fd12f36eda4f8b4433f7155f8 [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;
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;
44import java.net.DatagramSocket;
45import java.net.InetAddress;
46import java.net.Socket;
Aaron Huang49eef6f2021-12-22 16:35:13 +080047import java.util.Objects;
Nathan Haroldb1f605f2017-01-12 18:38:57 -080048
49/**
Jonathan Basseri2725a232017-04-21 15:53:51 -070050 * This class contains methods for managing IPsec sessions. Once configured, the kernel will apply
51 * confidentiality (encryption) and integrity (authentication) to IP traffic.
Nathan Haroldb1f605f2017-01-12 18:38:57 -080052 *
Jonathan Basseri2725a232017-04-21 15:53:51 -070053 * <p>Note that not all aspects of IPsec are permitted by this API. Applications may create
54 * transport mode security associations and apply them to individual sockets. Applications looking
Benedict Wongc85b7b02019-11-12 22:31:51 -080055 * to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}.
Jonathan Basseri2725a232017-04-21 15:53:51 -070056 *
57 * @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the
Jonathan Basseri20e96c52017-11-16 10:58:01 -080058 * Internet Protocol</a>
Nathan Haroldb1f605f2017-01-12 18:38:57 -080059 */
Jeff Sharkeyad1cebe2017-06-02 17:36:26 -060060@SystemService(Context.IPSEC_SERVICE)
Nathan Haroldb1f605f2017-01-12 18:38:57 -080061public final class IpSecManager {
62 private static final String TAG = "IpSecManager";
63
64 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080065 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
66 * towards the host.
67 *
68 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080069 */
70 public static final int DIRECTION_IN = 0;
71
72 /**
Nathan Harolde3536f02018-03-06 13:22:22 -080073 * Used when applying a transform to direct traffic through an {@link IpSecTransform}
74 * away from the host.
75 *
76 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
Nathan Harold5a19b952018-01-05 19:25:13 -080077 */
78 public static final int DIRECTION_OUT = 1;
79
Nathan Harold5a19b952018-01-05 19:25:13 -080080 /**
Benedict Wong908d34e2021-04-15 11:59:16 -070081 * Used when applying a transform to direct traffic through an {@link IpSecTransform} for
82 * forwarding between interfaces.
83 *
84 * <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
85 *
86 * @hide
87 */
88 public static final int DIRECTION_FWD = 2;
89
90 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -070091 * The Security Parameter Index (SPI) 0 indicates an unknown or invalid index.
Nathan Harold031acb82017-03-07 13:23:36 -080092 *
93 * <p>No IPsec packet may contain an SPI of 0.
Jonathan Basseri2725a232017-04-21 15:53:51 -070094 *
95 * @hide
Nathan Harold031acb82017-03-07 13:23:36 -080096 */
Jonathan Basseri20e96c52017-11-16 10:58:01 -080097 @TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0;
Nathan Harold031acb82017-03-07 13:23:36 -080098
99 /** @hide */
100 public interface Status {
Aaron Huangfbae3082021-12-06 15:18:42 +0800101 int OK = 0;
102 int RESOURCE_UNAVAILABLE = 1;
103 int SPI_UNAVAILABLE = 2;
Nathan Harold031acb82017-03-07 13:23:36 -0800104 }
105
106 /** @hide */
Nathan Harold5e854462017-12-13 18:51:35 -0800107 public static final int INVALID_RESOURCE_ID = -1;
Nathan Harold031acb82017-03-07 13:23:36 -0800108
109 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700110 * Thrown to indicate that a requested SPI is in use.
111 *
112 * <p>The combination of remote {@code InetAddress} and SPI must be unique across all apps on
113 * one device. If this error is encountered, a new SPI is required before a transform may be
114 * created. This error can be avoided by calling {@link
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800115 * IpSecManager#allocateSecurityParameterIndex}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800116 */
117 public static final class SpiUnavailableException extends AndroidException {
118 private final int mSpi;
119
120 /**
121 * Construct an exception indicating that a transform with the given SPI is already in use
122 * or otherwise unavailable.
123 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700124 * @param msg description indicating the colliding SPI
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800125 * @param spi the SPI that could not be used due to a collision
126 */
127 SpiUnavailableException(String msg, int spi) {
Jonathan Basseri2725a232017-04-21 15:53:51 -0700128 super(msg + " (spi: " + spi + ")");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800129 mSpi = spi;
130 }
131
Jonathan Basseri2725a232017-04-21 15:53:51 -0700132 /** Get the SPI that caused a collision. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800133 public int getSpi() {
134 return mSpi;
135 }
136 }
137
138 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700139 * Thrown to indicate that an IPsec resource is unavailable.
140 *
141 * <p>This could apply to resources such as sockets, {@link SecurityParameterIndex}, {@link
142 * IpSecTransform}, or other system resources. If this exception is thrown, users should release
143 * allocated objects of the type requested.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800144 */
145 public static final class ResourceUnavailableException extends AndroidException {
146
147 ResourceUnavailableException(String msg) {
148 super(msg);
149 }
150 }
151
Nathan Harold65ef8432018-03-15 18:06:06 -0700152 private final Context mContext;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800153 private final IIpSecService mService;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800154
Jonathan Basseri2725a232017-04-21 15:53:51 -0700155 /**
156 * This class represents a reserved SPI.
157 *
158 * <p>Objects of this type are used to track reserved security parameter indices. They can be
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800159 * obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released
Jonathan Basseri2725a232017-04-21 15:53:51 -0700160 * by calling {@link #close()} when they are no longer needed.
161 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800162 public static final class SecurityParameterIndex implements AutoCloseable {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800163 private final IIpSecService mService;
Nathan Harold5a19b952018-01-05 19:25:13 -0800164 private final InetAddress mDestinationAddress;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800165 private final CloseGuard mCloseGuard = CloseGuard.get();
Nathan Harold031acb82017-03-07 13:23:36 -0800166 private int mSpi = INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold5e854462017-12-13 18:51:35 -0800167 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800168
Jonathan Basseri2725a232017-04-21 15:53:51 -0700169 /** Get the underlying SPI held by this object. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800170 public int getSpi() {
171 return mSpi;
172 }
173
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800174 /**
175 * Release an SPI that was previously reserved.
176 *
Nathan Harold909d66e2017-03-29 10:47:59 -0700177 * <p>Release an SPI for use by other users in the system. If a SecurityParameterIndex is
178 * applied to an IpSecTransform, it will become unusable for future transforms but should
179 * still be closed to ensure system resources are released.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800180 */
181 @Override
182 public void close() {
Nathan Harold80865392017-04-04 19:37:48 -0700183 try {
184 mService.releaseSecurityParameterIndex(mResourceId);
185 } catch (RemoteException e) {
186 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700187 } catch (Exception e) {
188 // On close we swallow all random exceptions since failure to close is not
189 // actionable by the user.
190 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
191 } finally {
192 mResourceId = INVALID_RESOURCE_ID;
193 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700194 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800195 }
196
Jonathan Basseri2725a232017-04-21 15:53:51 -0700197 /** Check that the SPI was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800198 @Override
Nathan Harold2a30daf2017-11-07 17:17:45 -0800199 protected void finalize() throws Throwable {
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800200 if (mCloseGuard != null) {
201 mCloseGuard.warnIfOpen();
202 }
203
204 close();
205 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800206
Nathan Harold031acb82017-03-07 13:23:36 -0800207 private SecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800208 @NonNull IIpSecService service, InetAddress destinationAddress, int spi)
Nathan Harold031acb82017-03-07 13:23:36 -0800209 throws ResourceUnavailableException, SpiUnavailableException {
210 mService = service;
Nathan Harold5a19b952018-01-05 19:25:13 -0800211 mDestinationAddress = destinationAddress;
Nathan Harold031acb82017-03-07 13:23:36 -0800212 try {
Nathan Harold80865392017-04-04 19:37:48 -0700213 IpSecSpiResponse result =
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800214 mService.allocateSecurityParameterIndex(
Nathan Harold5a19b952018-01-05 19:25:13 -0800215 destinationAddress.getHostAddress(), spi, new Binder());
Nathan Harold031acb82017-03-07 13:23:36 -0800216
217 if (result == null) {
218 throw new NullPointerException("Received null response from IpSecService");
219 }
220
Nathan Harold80865392017-04-04 19:37:48 -0700221 int status = result.status;
Nathan Harold031acb82017-03-07 13:23:36 -0800222 switch (status) {
223 case Status.OK:
224 break;
225 case Status.RESOURCE_UNAVAILABLE:
226 throw new ResourceUnavailableException(
227 "No more SPIs may be allocated by this requester.");
228 case Status.SPI_UNAVAILABLE:
229 throw new SpiUnavailableException("Requested SPI is unavailable", spi);
230 default:
231 throw new RuntimeException(
232 "Unknown status returned by IpSecService: " + status);
233 }
Nathan Harold80865392017-04-04 19:37:48 -0700234 mSpi = result.spi;
235 mResourceId = result.resourceId;
Nathan Harold031acb82017-03-07 13:23:36 -0800236
237 if (mSpi == INVALID_SECURITY_PARAMETER_INDEX) {
238 throw new RuntimeException("Invalid SPI returned by IpSecService: " + status);
239 }
240
241 if (mResourceId == INVALID_RESOURCE_ID) {
242 throw new RuntimeException(
243 "Invalid Resource ID returned by IpSecService: " + status);
244 }
Nathan Harold031acb82017-03-07 13:23:36 -0800245 } catch (RemoteException e) {
246 throw e.rethrowFromSystemServer();
247 }
248 mCloseGuard.open("open");
249 }
Nathan Harold80865392017-04-04 19:37:48 -0700250
251 /** @hide */
Nathan Harold19b99d92017-08-23 13:46:33 -0700252 @VisibleForTesting
253 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700254 return mResourceId;
255 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700256
257 @Override
258 public String toString() {
259 return new StringBuilder()
260 .append("SecurityParameterIndex{spi=")
261 .append(mSpi)
262 .append(",resourceId=")
263 .append(mResourceId)
264 .append("}")
265 .toString();
266 }
Nathan Harold031acb82017-03-07 13:23:36 -0800267 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800268
269 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800270 * Reserve a random SPI for traffic bound to or from the specified destination address.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800271 *
272 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
273 * SecurityParameterIndex#close()}.
274 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800275 * @param destinationAddress the destination address for traffic bearing the requested SPI.
276 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800277 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800278 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800279 * currently allocated for this user
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800280 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700281 @NonNull
282 public SecurityParameterIndex allocateSecurityParameterIndex(
283 @NonNull InetAddress destinationAddress) throws ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700284 try {
285 return new SecurityParameterIndex(
286 mService,
Nathan Harold5a19b952018-01-05 19:25:13 -0800287 destinationAddress,
Nathan Harold05406792017-04-06 18:16:28 -0700288 IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700289 } catch (ServiceSpecificException e) {
290 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
Nathan Harold05406792017-04-06 18:16:28 -0700291 } catch (SpiUnavailableException unlikely) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700292 // Because this function allocates a totally random SPI, it really shouldn't ever
293 // fail to allocate an SPI; we simply need this because the exception is checked.
Nathan Harold05406792017-04-06 18:16:28 -0700294 throw new ResourceUnavailableException("No SPIs available");
295 }
296 }
297
298 /**
Nathan Harold5a19b952018-01-05 19:25:13 -0800299 * Reserve the requested SPI for traffic bound to or from the specified destination address.
Nathan Harold05406792017-04-06 18:16:28 -0700300 *
301 * <p>If successful, this SPI is guaranteed available until released by a call to {@link
302 * SecurityParameterIndex#close()}.
303 *
Nathan Harold5a19b952018-01-05 19:25:13 -0800304 * @param destinationAddress the destination address for traffic bearing the requested SPI.
305 * For inbound traffic, the destination should be an address currently assigned on-device.
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700306 * @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See
307 * RFC 4303 Section 2.1.
Nathan Harold05406792017-04-06 18:16:28 -0700308 * @return the reserved SecurityParameterIndex
Aaron Huangfbae3082021-12-06 15:18:42 +0800309 * @throws ResourceUnavailableException indicating that too many SPIs are
Nathan Harold5a19b952018-01-05 19:25:13 -0800310 * currently allocated for this user
Aaron Huangfbae3082021-12-06 15:18:42 +0800311 * @throws SpiUnavailableException indicating that the requested SPI could not be
Nathan Harold5a19b952018-01-05 19:25:13 -0800312 * reserved
Nathan Harold05406792017-04-06 18:16:28 -0700313 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700314 @NonNull
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800315 public SecurityParameterIndex allocateSecurityParameterIndex(
Nathan Harold6303d9e2018-03-16 17:27:30 -0700316 @NonNull InetAddress destinationAddress, int requestedSpi)
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800317 throws SpiUnavailableException, ResourceUnavailableException {
Nathan Harold05406792017-04-06 18:16:28 -0700318 if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
319 throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
320 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700321 try {
322 return new SecurityParameterIndex(mService, destinationAddress, requestedSpi);
323 } catch (ServiceSpecificException e) {
324 throw rethrowUncheckedExceptionFromServiceSpecificException(e);
325 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800326 }
327
328 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700329 * Apply an IPsec transform to a stream socket.
330 *
331 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
332 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800333 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700334 * unprotected traffic can resume on that socket.
335 *
336 * <p>For security reasons, the destination address of any traffic on the socket must match the
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800337 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
Jonathan Basseri2725a232017-04-21 15:53:51 -0700338 * other IP address will result in an IOException. In addition, reads and writes on the socket
339 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800340 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700341 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700342 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
343 * applied transform before completion of graceful shutdown may result in the shutdown sequence
344 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
345 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
346 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
347 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
348 * sufficient to ensure shutdown.
349 *
350 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
351 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
352 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
353 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
354 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800355 * <h4>Rekey Procedure</h4>
356 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800357 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
358 * will be removed and the new transform will take effect immediately, sending all traffic on
359 * the new transform; however, when applying a transform in the inbound direction, traffic
360 * on the old transform will continue to be decrypted and delivered until that transform is
361 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
362 * procedures where both transforms are valid until both endpoints are using the new transform
363 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800364 *
365 * @param socket a stream socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800366 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700367 * @param transform a transport mode {@code IpSecTransform}
368 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800369 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700370 public void applyTransportModeTransform(@NonNull Socket socket,
371 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700372 // Ensure creation of FD. See b/77548890 for more details.
373 socket.getSoLinger();
374
Nathan Harold31676252018-01-16 12:08:43 -0800375 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800376 }
377
378 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700379 * Apply an IPsec transform to a datagram socket.
380 *
381 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
382 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800383 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700384 * unprotected traffic can resume on that socket.
385 *
386 * <p>For security reasons, the destination address of any traffic on the socket must match the
387 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
388 * other IP address will result in an IOException. In addition, reads and writes on the socket
389 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800390 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700391 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800392 * <h4>Rekey Procedure</h4>
393 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800394 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
395 * will be removed and the new transform will take effect immediately, sending all traffic on
396 * the new transform; however, when applying a transform in the inbound direction, traffic
397 * on the old transform will continue to be decrypted and delivered until that transform is
398 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
399 * procedures where both transforms are valid until both endpoints are using the new transform
400 * and all in-flight packets have been received.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800401 *
402 * @param socket a datagram socket
Nathan Harolde3536f02018-03-06 13:22:22 -0800403 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700404 * @param transform a transport mode {@code IpSecTransform}
405 * @throws IOException indicating that the transform could not be applied
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800406 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700407 public void applyTransportModeTransform(@NonNull DatagramSocket socket,
408 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold31676252018-01-16 12:08:43 -0800409 applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
Nathan Harold031acb82017-03-07 13:23:36 -0800410 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800411
412 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700413 * Apply an IPsec transform to a socket.
414 *
415 * <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
416 * socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
Nathan Harold5a19b952018-01-05 19:25:13 -0800417 * the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
Jonathan Basseri2725a232017-04-21 15:53:51 -0700418 * unprotected traffic can resume on that socket.
419 *
420 * <p>For security reasons, the destination address of any traffic on the socket must match the
421 * remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
422 * other IP address will result in an IOException. In addition, reads and writes on the socket
423 * will throw IOException if the user deactivates the transform (by calling {@link
Nathan Harold5a19b952018-01-05 19:25:13 -0800424 * IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700425 *
Benedict Wongc5106f32018-03-14 19:01:14 -0700426 * <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
427 * applied transform before completion of graceful shutdown may result in the shutdown sequence
428 * failing to complete. As such, applications requiring graceful shutdown MUST close the socket
429 * prior to deactivating the applied transform. Socket closure may be performed asynchronously
430 * (in batches), so the returning of a close function does not guarantee shutdown of a socket.
431 * Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
432 * sufficient to ensure shutdown.
433 *
434 * Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
435 * prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
436 * packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
437 * SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
438 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800439 * <h4>Rekey Procedure</h4>
440 *
Nathan Harolde3536f02018-03-06 13:22:22 -0800441 * <p>When applying a new tranform to a socket in the outbound direction, the previous transform
442 * will be removed and the new transform will take effect immediately, sending all traffic on
443 * the new transform; however, when applying a transform in the inbound direction, traffic
444 * on the old transform will continue to be decrypted and delivered until that transform is
445 * deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
446 * procedures where both transforms are valid until both endpoints are using the new transform
447 * and all in-flight packets have been received.
Nathan Harold0072e192017-04-06 17:46:00 -0700448 *
449 * @param socket a socket file descriptor
Nathan Harolde3536f02018-03-06 13:22:22 -0800450 * @param direction the direction in which the transform should be applied
Jonathan Basseri2725a232017-04-21 15:53:51 -0700451 * @param transform a transport mode {@code IpSecTransform}
452 * @throws IOException indicating that the transform could not be applied
Nathan Harold0072e192017-04-06 17:46:00 -0700453 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700454 public void applyTransportModeTransform(@NonNull FileDescriptor socket,
455 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700456 // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
Nathan Harold31676252018-01-16 12:08:43 -0800457 // constructor takes control and closes the user's FD when we exit the method.
Nathan Harold80865392017-04-04 19:37:48 -0700458 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold5a19b952018-01-05 19:25:13 -0800459 mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700460 } catch (ServiceSpecificException e) {
461 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold80865392017-04-04 19:37:48 -0700462 } catch (RemoteException e) {
463 throw e.rethrowFromSystemServer();
464 }
Nathan Harold0072e192017-04-06 17:46:00 -0700465 }
466
467 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700468 * Remove an IPsec transform from a stream socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800469 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800470 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
471 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700472 *
473 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
474 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
475 * is called.
476 *
477 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700478 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800479 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700480 public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException {
Benedict Wong57ab3f62018-04-02 18:12:34 -0700481 // Ensure creation of FD. See b/77548890 for more details.
482 socket.getSoLinger();
483
Nathan Harold0d483b72018-01-17 01:00:20 -0800484 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800485 }
486
487 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700488 * Remove an IPsec transform from a datagram socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800489 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800490 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
491 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700492 *
493 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
494 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
495 * is called.
496 *
497 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700498 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800499 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700500 public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException {
Nathan Harold0d483b72018-01-17 01:00:20 -0800501 removeTransportModeTransforms(socket.getFileDescriptor$());
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800502 }
503
Nathan Harold0072e192017-04-06 17:46:00 -0700504 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700505 * Remove an IPsec transform from a socket.
Nathan Harold0072e192017-04-06 17:46:00 -0700506 *
Nathan Harold0d483b72018-01-17 01:00:20 -0800507 * <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
508 * socket allows the socket to be reused for communication in the clear.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700509 *
510 * <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
511 * {@link IpSecTransform#close()}, then communication on the socket will fail until this method
512 * is called.
513 *
514 * @param socket a socket that previously had a transform applied to it
Jonathan Basseri2725a232017-04-21 15:53:51 -0700515 * @throws IOException indicating that the transform could not be removed from the socket
Nathan Harold0072e192017-04-06 17:46:00 -0700516 */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700517 public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700518 try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
Nathan Harold0d483b72018-01-17 01:00:20 -0800519 mService.removeTransportModeTransforms(pfd);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700520 } catch (ServiceSpecificException e) {
521 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Nathan Harold031acb82017-03-07 13:23:36 -0800522 } catch (RemoteException e) {
523 throw e.rethrowFromSystemServer();
524 }
525 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800526
527 /**
528 * Remove a Tunnel Mode IPsec Transform from a {@link Network}. This must be used as part of
529 * cleanup if a tunneled Network experiences a change in default route. The Network will drop
530 * all traffic that cannot be routed to the Tunnel's outbound interface. If that interface is
531 * lost, all traffic will drop.
532 *
Jonathan Basseri20e96c52017-11-16 10:58:01 -0800533 * <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked.
534 *
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800535 * @param net a network that currently has transform applied to it.
536 * @param transform a Tunnel Mode IPsec Transform that has been previously applied to the given
537 * network
538 * @hide
539 */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800540 public void removeTunnelModeTransform(Network net, IpSecTransform transform) {}
541
542 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700543 * This class provides access to a UDP encapsulation Socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800544 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700545 * <p>{@code UdpEncapsulationSocket} wraps a system-provided datagram socket intended for IKEv2
546 * signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link
547 * IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the
548 * caller. The caller should not close the {@code FileDescriptor} returned by {@link
Benedict Wonga386e372018-03-27 16:55:48 -0700549 * #getFileDescriptor}, but should use {@link #close} instead.
Jonathan Basseri2725a232017-04-21 15:53:51 -0700550 *
551 * <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic
552 * of the next user who binds to that port. To prevent this scenario, these sockets are held
553 * open by the system so that they may only be closed by calling {@link #close} or when the user
554 * process exits.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800555 */
556 public static final class UdpEncapsulationSocket implements AutoCloseable {
Nathan Harold80865392017-04-04 19:37:48 -0700557 private final ParcelFileDescriptor mPfd;
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800558 private final IIpSecService mService;
Nathan Harold5e854462017-12-13 18:51:35 -0800559 private int mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700560 private final int mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800561 private final CloseGuard mCloseGuard = CloseGuard.get();
562
Nathan Harold031acb82017-03-07 13:23:36 -0800563 private UdpEncapsulationSocket(@NonNull IIpSecService service, int port)
Nathan Harold80865392017-04-04 19:37:48 -0700564 throws ResourceUnavailableException, IOException {
Nathan Haroldd2a1dad2017-03-01 18:55:06 -0800565 mService = service;
Nathan Harold80865392017-04-04 19:37:48 -0700566 try {
567 IpSecUdpEncapResponse result =
568 mService.openUdpEncapsulationSocket(port, new Binder());
569 switch (result.status) {
570 case Status.OK:
571 break;
572 case Status.RESOURCE_UNAVAILABLE:
573 throw new ResourceUnavailableException(
574 "No more Sockets may be allocated by this requester.");
575 default:
576 throw new RuntimeException(
577 "Unknown status returned by IpSecService: " + result.status);
578 }
579 mResourceId = result.resourceId;
580 mPort = result.port;
581 mPfd = result.fileDescriptor;
582 } catch (RemoteException e) {
583 throw e.rethrowFromSystemServer();
584 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800585 mCloseGuard.open("constructor");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800586 }
587
Benedict Wonga386e372018-03-27 16:55:48 -0700588 /** Get the encapsulation socket's file descriptor. */
589 public FileDescriptor getFileDescriptor() {
Nathan Harold80865392017-04-04 19:37:48 -0700590 if (mPfd == null) {
591 return null;
592 }
593 return mPfd.getFileDescriptor();
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800594 }
595
Jonathan Basseri2725a232017-04-21 15:53:51 -0700596 /** Get the bound port of the wrapped socket. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800597 public int getPort() {
Nathan Harold80865392017-04-04 19:37:48 -0700598 return mPort;
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800599 }
600
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800601 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700602 * Close this socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800603 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700604 * <p>This closes the wrapped socket. Open encapsulation sockets count against a user's
605 * resource limits, and forgetting to close them eventually will result in {@link
606 * ResourceUnavailableException} being thrown.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800607 */
Jonathan Basseri2725a232017-04-21 15:53:51 -0700608 @Override
Nathan Harolda64b0192017-04-17 17:11:58 -0700609 public void close() throws IOException {
Nathan Harold80865392017-04-04 19:37:48 -0700610 try {
611 mService.closeUdpEncapsulationSocket(mResourceId);
Nathan Harold5e854462017-12-13 18:51:35 -0800612 mResourceId = INVALID_RESOURCE_ID;
Nathan Harold80865392017-04-04 19:37:48 -0700613 } catch (RemoteException e) {
614 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700615 } catch (Exception e) {
616 // On close we swallow all random exceptions since failure to close is not
617 // actionable by the user.
618 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
619 } finally {
620 mResourceId = INVALID_RESOURCE_ID;
621 mCloseGuard.close();
Nathan Harold80865392017-04-04 19:37:48 -0700622 }
623
624 try {
625 mPfd.close();
626 } catch (IOException e) {
627 Log.e(TAG, "Failed to close UDP Encapsulation Socket with Port= " + mPort);
628 throw e;
629 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800630 }
631
Jonathan Basseri2725a232017-04-21 15:53:51 -0700632 /** Check that the socket was closed properly. */
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800633 @Override
634 protected void finalize() throws Throwable {
635 if (mCloseGuard != null) {
636 mCloseGuard.warnIfOpen();
637 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800638 close();
639 }
Nathan Harold80865392017-04-04 19:37:48 -0700640
641 /** @hide */
Chiachang Wanga0343312021-01-12 18:48:13 +0800642 @SystemApi(client = MODULE_LIBRARIES)
Nathan Harold19b99d92017-08-23 13:46:33 -0700643 public int getResourceId() {
Nathan Harold80865392017-04-04 19:37:48 -0700644 return mResourceId;
645 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700646
647 @Override
648 public String toString() {
649 return new StringBuilder()
650 .append("UdpEncapsulationSocket{port=")
651 .append(mPort)
652 .append(",resourceId=")
653 .append(mResourceId)
654 .append("}")
655 .toString();
656 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800657 };
658
659 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700660 * Open a socket for UDP encapsulation and bind to the given port.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800661 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700662 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800663 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700664 * @param port a local UDP port
665 * @return a socket that is bound to the given port
666 * @throws IOException indicating that the socket could not be opened or bound
667 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800668 */
669 // Returning a socket in this fashion that has been created and bound by the system
670 // is the only safe way to ensure that a socket is both accessible to the user and
671 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
672 // the port, which could potentially impact the traffic of the next user who binds to that
673 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700674 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800675 public UdpEncapsulationSocket openUdpEncapsulationSocket(int port)
676 throws IOException, ResourceUnavailableException {
Nathan Harold80865392017-04-04 19:37:48 -0700677 /*
678 * Most range checking is done in the service, but this version of the constructor expects
679 * a valid port number, and zero cannot be checked after being passed to the service.
680 */
681 if (port == 0) {
682 throw new IllegalArgumentException("Specified port must be a valid port number!");
683 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700684 try {
685 return new UdpEncapsulationSocket(mService, port);
686 } catch (ServiceSpecificException e) {
687 throw rethrowCheckedExceptionFromServiceSpecificException(e);
688 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800689 }
690
691 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700692 * Open a socket for UDP encapsulation.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800693 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700694 * <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800695 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700696 * <p>The local port of the returned socket can be obtained by calling {@link
697 * UdpEncapsulationSocket#getPort()}.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800698 *
Jonathan Basseri2725a232017-04-21 15:53:51 -0700699 * @return a socket that is bound to a local port
700 * @throws IOException indicating that the socket could not be opened or bound
701 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800702 */
703 // Returning a socket in this fashion that has been created and bound by the system
704 // is the only safe way to ensure that a socket is both accessible to the user and
705 // safely usable for Encapsulation without allowing a user to possibly unbind from/close
706 // the port, which could potentially impact the traffic of the next user who binds to that
707 // socket.
Nathan Harold6303d9e2018-03-16 17:27:30 -0700708 @NonNull
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800709 public UdpEncapsulationSocket openUdpEncapsulationSocket()
710 throws IOException, ResourceUnavailableException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700711 try {
712 return new UdpEncapsulationSocket(mService, 0);
713 } catch (ServiceSpecificException e) {
714 throw rethrowCheckedExceptionFromServiceSpecificException(e);
715 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800716 }
717
718 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000719 * This class represents an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800720 *
721 * <p>IpSecTunnelInterface objects track tunnel interfaces that serve as
722 * local endpoints for IPsec tunnels.
723 *
724 * <p>Creating an IpSecTunnelInterface creates a device to which IpSecTransforms may be
725 * applied to provide IPsec security to packets sent through the tunnel. While a tunnel
726 * cannot be used in standalone mode within Android, the higher layers may use the tunnel
727 * to create Network objects which are accessible to the Android system.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000728 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800729 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000730 @SystemApi
Nathan Harold67e76732018-01-17 16:09:24 -0800731 public static final class IpSecTunnelInterface implements AutoCloseable {
Nathan Harold65ef8432018-03-15 18:06:06 -0700732 private final String mOpPackageName;
Nathan Harold67e76732018-01-17 16:09:24 -0800733 private final IIpSecService mService;
734 private final InetAddress mRemoteAddress;
735 private final InetAddress mLocalAddress;
736 private final Network mUnderlyingNetwork;
737 private final CloseGuard mCloseGuard = CloseGuard.get();
738 private String mInterfaceName;
739 private int mResourceId = INVALID_RESOURCE_ID;
740
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000741 /** Get the underlying SPI held by this object. */
Nathan Harold6303d9e2018-03-16 17:27:30 -0700742 @NonNull
Nathan Harold67e76732018-01-17 16:09:24 -0800743 public String getInterfaceName() {
744 return mInterfaceName;
745 }
746
747 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000748 * Add an address to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800749 *
750 * <p>Add an address which may be used as the local inner address for
751 * tunneled traffic.
752 *
753 * @param address the local address for traffic inside the tunnel
Benedict Wongd39837f2018-04-03 20:30:54 -0700754 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000755 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800756 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000757 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800758 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700759 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700760 public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800761 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700762 mService.addAddressToTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700763 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700764 } catch (ServiceSpecificException e) {
765 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800766 } catch (RemoteException e) {
767 throw e.rethrowFromSystemServer();
768 }
Nathan Harold67e76732018-01-17 16:09:24 -0800769 }
770
771 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000772 * Remove an address from the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800773 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000774 * <p>Remove an address which was previously added to the IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800775 *
776 * @param address to be removed
Benedict Wongd39837f2018-04-03 20:30:54 -0700777 * @param prefixLen length of the InetAddress prefix
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000778 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800779 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000780 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800781 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700782 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Benedict Wongd39837f2018-04-03 20:30:54 -0700783 public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
Benedict Wong97c3c942018-03-01 18:53:07 -0800784 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700785 mService.removeAddressFromTunnelInterface(
Benedict Wongd39837f2018-04-03 20:30:54 -0700786 mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700787 } catch (ServiceSpecificException e) {
788 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong97c3c942018-03-01 18:53:07 -0800789 } catch (RemoteException e) {
790 throw e.rethrowFromSystemServer();
791 }
Nathan Harold67e76732018-01-17 16:09:24 -0800792 }
793
Yan Yana2f3b492020-09-29 23:38:00 -0700794 /**
795 * Update the underlying network for this IpSecTunnelInterface.
796 *
797 * <p>This new underlying network will be used for all transforms applied AFTER this call is
798 * complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to
799 * this tunnel interface, traffic will still use the old SA, and be routed on the old
800 * underlying network.
801 *
802 * <p>To migrate IPsec tunnel mode traffic, a caller should:
803 *
804 * <ol>
805 * <li>Update the IpSecTunnelInterface’s underlying network.
806 * <li>Apply {@link IpSecTransform}(s) with matching addresses to this
807 * IpSecTunnelInterface.
808 * </ol>
809 *
810 * @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel.
811 * This network MUST never be the network exposing this IpSecTunnelInterface, otherwise
Yan Yancf4d2eb2021-02-16 11:48:55 -0800812 * this method will throw an {@link IllegalArgumentException}. If the
813 * IpSecTunnelInterface is later added to this network, all outbound traffic will be
814 * blackholed.
Yan Yana2f3b492020-09-29 23:38:00 -0700815 */
816 // TODO: b/169171001 Update the documentation when transform migration is supported.
817 // The purpose of making updating network and applying transforms separate is to leave open
818 // the possibility to support lossless migration procedures. To do that, Android platform
819 // will need to support multiple inbound tunnel mode transforms, just like it can support
820 // multiple transport mode transforms.
821 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
822 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
823 public void setUnderlyingNetwork(@NonNull Network underlyingNetwork) throws IOException {
824 try {
825 mService.setNetworkForTunnelInterface(
826 mResourceId, underlyingNetwork, mOpPackageName);
827 } catch (RemoteException e) {
828 throw e.rethrowFromSystemServer();
829 }
830 }
831
Nathan Harold65ef8432018-03-15 18:06:06 -0700832 private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
Nathan Harold67e76732018-01-17 16:09:24 -0800833 @NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
834 @NonNull Network underlyingNetwork)
835 throws ResourceUnavailableException, IOException {
Nathan Harold65ef8432018-03-15 18:06:06 -0700836 mOpPackageName = ctx.getOpPackageName();
Nathan Harold67e76732018-01-17 16:09:24 -0800837 mService = service;
838 mLocalAddress = localAddress;
839 mRemoteAddress = remoteAddress;
840 mUnderlyingNetwork = underlyingNetwork;
Benedict Wong8bc90732018-01-18 18:31:45 -0800841
842 try {
843 IpSecTunnelInterfaceResponse result =
844 mService.createTunnelInterface(
845 localAddress.getHostAddress(),
846 remoteAddress.getHostAddress(),
847 underlyingNetwork,
Nathan Harold65ef8432018-03-15 18:06:06 -0700848 new Binder(),
849 mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800850 switch (result.status) {
851 case Status.OK:
852 break;
853 case Status.RESOURCE_UNAVAILABLE:
854 throw new ResourceUnavailableException(
855 "No more tunnel interfaces may be allocated by this requester.");
856 default:
857 throw new RuntimeException(
858 "Unknown status returned by IpSecService: " + result.status);
859 }
860 mResourceId = result.resourceId;
861 mInterfaceName = result.interfaceName;
862 } catch (RemoteException e) {
863 throw e.rethrowFromSystemServer();
864 }
865 mCloseGuard.open("constructor");
Nathan Harold67e76732018-01-17 16:09:24 -0800866 }
867
868 /**
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000869 * Delete an IpSecTunnelInterface
Nathan Harold67e76732018-01-17 16:09:24 -0800870 *
871 * <p>Calling close will deallocate the IpSecTunnelInterface and all of its system
872 * resources. Any packets bound for this interface either inbound or outbound will
873 * all be lost.
874 */
875 @Override
876 public void close() {
Benedict Wong8bc90732018-01-18 18:31:45 -0800877 try {
Nathan Harold65ef8432018-03-15 18:06:06 -0700878 mService.deleteTunnelInterface(mResourceId, mOpPackageName);
Benedict Wong8bc90732018-01-18 18:31:45 -0800879 } catch (RemoteException e) {
880 throw e.rethrowFromSystemServer();
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700881 } catch (Exception e) {
882 // On close we swallow all random exceptions since failure to close is not
883 // actionable by the user.
884 Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
885 } finally {
886 mResourceId = INVALID_RESOURCE_ID;
887 mCloseGuard.close();
Benedict Wong8bc90732018-01-18 18:31:45 -0800888 }
Nathan Harold67e76732018-01-17 16:09:24 -0800889 }
890
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000891 /** Check that the Interface was closed properly. */
Nathan Harold67e76732018-01-17 16:09:24 -0800892 @Override
893 protected void finalize() throws Throwable {
894 if (mCloseGuard != null) {
895 mCloseGuard.warnIfOpen();
896 }
897 close();
898 }
Benedict Wong8bc90732018-01-18 18:31:45 -0800899
900 /** @hide */
901 @VisibleForTesting
902 public int getResourceId() {
903 return mResourceId;
904 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700905
Aurimas Liutikas3a78d9d2019-08-28 13:01:05 -0700906 @NonNull
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700907 @Override
908 public String toString() {
909 return new StringBuilder()
910 .append("IpSecTunnelInterface{ifname=")
911 .append(mInterfaceName)
912 .append(",resourceId=")
913 .append(mResourceId)
914 .append("}")
915 .toString();
916 }
Nathan Harold67e76732018-01-17 16:09:24 -0800917 }
918
919 /**
920 * Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic.
921 *
Benedict Wong8bc90732018-01-18 18:31:45 -0800922 * <p>An application that creates tunnels is responsible for cleaning up the tunnel when the
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000923 * underlying network goes away, and the onLost() callback is received.
Benedict Wong8bc90732018-01-18 18:31:45 -0800924 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000925 * @param localAddress The local addres of the tunnel
926 * @param remoteAddress The local addres of the tunnel
927 * @param underlyingNetwork the {@link Network} that will carry traffic for this tunnel.
928 * This network should almost certainly be a network such as WiFi with an L2 address.
929 * @return a new {@link IpSecManager#IpSecTunnelInterface} with the specified properties
930 * @throws IOException indicating that the socket could not be opened or bound
931 * @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
Yan Yan160aac32020-12-03 17:08:29 -0800932 * @hide
Yan Yan160aac32020-12-03 17:08:29 -0800933 */
Nathan Harold3a287d52018-04-25 12:01:34 -0700934 @SystemApi
Nathan Harold6303d9e2018-03-16 17:27:30 -0700935 @NonNull
Benedict Wonge9763752018-11-08 19:45:34 -0800936 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700937 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold67e76732018-01-17 16:09:24 -0800938 public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
939 @NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
940 throws ResourceUnavailableException, IOException {
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700941 try {
942 return new IpSecTunnelInterface(
943 mContext, mService, localAddress, remoteAddress, underlyingNetwork);
944 } catch (ServiceSpecificException e) {
945 throw rethrowCheckedExceptionFromServiceSpecificException(e);
946 }
Nathan Harold67e76732018-01-17 16:09:24 -0800947 }
948
949 /**
Nathan Harolde3536f02018-03-06 13:22:22 -0800950 * Apply an active Tunnel Mode IPsec Transform to a {@link IpSecTunnelInterface}, which will
951 * tunnel all traffic for the given direction through the underlying network's interface with
952 * IPsec (applies an outer IP header and IPsec Header to all traffic, and expects an additional
953 * IP header and IPsec Header on all inbound traffic).
954 * <p>Applications should probably not use this API directly.
955 *
Nathan Harold67e76732018-01-17 16:09:24 -0800956 *
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000957 * @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied
Nathan Harold67e76732018-01-17 16:09:24 -0800958 * transform.
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000959 * @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which
Nathan Harold67e76732018-01-17 16:09:24 -0800960 * the transform will be used.
961 * @param transform an {@link IpSecTransform} created in tunnel mode
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000962 * @throws IOException indicating that the transform could not be applied due to a lower
963 * layer failure.
964 * @hide
Nathan Harold67e76732018-01-17 16:09:24 -0800965 */
Lorenzo Colitti388f4162021-02-01 08:12:34 +0000966 @SystemApi
Benedict Wonge9763752018-11-08 19:45:34 -0800967 @RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
Nathan Harold00e77242018-03-21 15:32:42 -0700968 @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
Nathan Harold6303d9e2018-03-16 17:27:30 -0700969 public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel,
970 @PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
Benedict Wong8bc90732018-01-18 18:31:45 -0800971 try {
972 mService.applyTunnelModeTransform(
Nathan Harold65ef8432018-03-15 18:06:06 -0700973 tunnel.getResourceId(), direction,
974 transform.getResourceId(), mContext.getOpPackageName());
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700975 } catch (ServiceSpecificException e) {
976 throw rethrowCheckedExceptionFromServiceSpecificException(e);
Benedict Wong8bc90732018-01-18 18:31:45 -0800977 } catch (RemoteException e) {
978 throw e.rethrowFromSystemServer();
979 }
Nathan Harold67e76732018-01-17 16:09:24 -0800980 }
Nathan Harolde3536f02018-03-06 13:22:22 -0800981
Nathan Harold67e76732018-01-17 16:09:24 -0800982 /**
Jonathan Basseri2725a232017-04-21 15:53:51 -0700983 * Construct an instance of IpSecManager within an application context.
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800984 *
985 * @param context the application context for this manager
986 * @hide
987 */
Nathan Harold65ef8432018-03-15 18:06:06 -0700988 public IpSecManager(Context ctx, IIpSecService service) {
989 mContext = ctx;
Aaron Huang49eef6f2021-12-22 16:35:13 +0800990 mService = Objects.requireNonNull(service, "missing service");
Nathan Haroldb1f605f2017-01-12 18:38:57 -0800991 }
Nathan Haroldbeed0b62018-04-03 16:13:19 -0700992
993 private static void maybeHandleServiceSpecificException(ServiceSpecificException sse) {
994 // OsConstants are late binding, so switch statements can't be used.
995 if (sse.errorCode == OsConstants.EINVAL) {
996 throw new IllegalArgumentException(sse);
997 } else if (sse.errorCode == OsConstants.EAGAIN) {
998 throw new IllegalStateException(sse);
Benedict Wongc45974b2018-07-16 14:56:20 -0700999 } else if (sse.errorCode == OsConstants.EOPNOTSUPP
1000 || sse.errorCode == OsConstants.EPROTONOSUPPORT) {
Nathan Haroldbeed0b62018-04-03 16:13:19 -07001001 throw new UnsupportedOperationException(sse);
1002 }
1003 }
1004
1005 /**
1006 * Convert an Errno SSE to the correct Unchecked exception type.
1007 *
1008 * This method never actually returns.
1009 */
1010 // package
1011 static RuntimeException
1012 rethrowUncheckedExceptionFromServiceSpecificException(ServiceSpecificException sse) {
1013 maybeHandleServiceSpecificException(sse);
1014 throw new RuntimeException(sse);
1015 }
1016
1017 /**
1018 * Convert an Errno SSE to the correct Checked or Unchecked exception type.
1019 *
1020 * This method may throw IOException, or it may throw an unchecked exception; it will never
1021 * actually return.
1022 */
1023 // package
1024 static IOException rethrowCheckedExceptionFromServiceSpecificException(
1025 ServiceSpecificException sse) throws IOException {
1026 // First see if this is an unchecked exception of a type we know.
1027 // If so, then we prefer the unchecked (specific) type of exception.
1028 maybeHandleServiceSpecificException(sse);
1029 // If not, then all we can do is provide the SSE in the form of an IOException.
1030 throw new ErrnoException(
1031 "IpSec encountered errno=" + sse.errorCode, sse.errorCode).rethrowAsIOException();
1032 }
Nathan Haroldb1f605f2017-01-12 18:38:57 -08001033}