2017-01-12 18:38:57 -08:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
|
*
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
package android.net;
|
|
|
|
|
|
|
2021-01-12 18:48:13 +08:00
|
|
|
|
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
|
|
|
|
|
|
|
2017-03-07 13:23:36 -08:00
|
|
|
|
import android.annotation.NonNull;
|
2018-11-08 19:45:34 -08:00
|
|
|
|
import android.annotation.RequiresFeature;
|
2018-02-02 18:34:25 -08:00
|
|
|
|
import android.annotation.RequiresPermission;
|
2018-04-25 12:01:34 -07:00
|
|
|
|
import android.annotation.SystemApi;
|
2017-06-02 17:36:26 -06:00
|
|
|
|
import android.annotation.SystemService;
|
2017-04-21 15:53:51 -07:00
|
|
|
|
import android.annotation.TestApi;
|
2017-06-02 17:36:26 -06:00
|
|
|
|
import android.content.Context;
|
2018-11-08 19:45:34 -08:00
|
|
|
|
import android.content.pm.PackageManager;
|
2019-12-30 16:05:38 -08:00
|
|
|
|
import android.net.annotations.PolicyDirection;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
import android.os.Binder;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
import android.os.ParcelFileDescriptor;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
import android.os.RemoteException;
|
2018-04-03 16:13:19 -07:00
|
|
|
|
import android.os.ServiceSpecificException;
|
|
|
|
|
|
import android.system.ErrnoException;
|
|
|
|
|
|
import android.system.OsConstants;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
import android.util.AndroidException;
|
2017-04-04 19:37:48 -07:00
|
|
|
|
import android.util.Log;
|
2017-09-11 19:50:19 -07:00
|
|
|
|
|
2017-08-23 13:46:33 -07:00
|
|
|
|
import com.android.internal.annotations.VisibleForTesting;
|
|
|
|
|
|
|
2017-01-12 18:38:57 -08:00
|
|
|
|
import dalvik.system.CloseGuard;
|
2017-09-11 19:50:19 -07:00
|
|
|
|
|
2017-01-12 18:38:57 -08:00
|
|
|
|
import java.io.FileDescriptor;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
|
import java.net.Socket;
|
2021-12-22 16:35:13 +08:00
|
|
|
|
import java.util.Objects;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* This class contains methods for managing IPsec sessions. Once configured, the kernel will apply
|
|
|
|
|
|
* confidentiality (encryption) and integrity (authentication) to IP traffic.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>Note that not all aspects of IPsec are permitted by this API. Applications may create
|
|
|
|
|
|
* transport mode security associations and apply them to individual sockets. Applications looking
|
2019-11-12 22:31:51 -08:00
|
|
|
|
* to create an IPsec VPN should use {@link VpnManager} and {@link Ikev2VpnProfile}.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* @see <a href="https://tools.ietf.org/html/rfc4301">RFC 4301, Security Architecture for the
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* Internet Protocol</a>
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2017-06-02 17:36:26 -06:00
|
|
|
|
@SystemService(Context.IPSEC_SERVICE)
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public final class IpSecManager {
|
|
|
|
|
|
private static final String TAG = "IpSecManager";
|
|
|
|
|
|
|
2018-01-05 19:25:13 -08:00
|
|
|
|
/**
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* Used when applying a transform to direct traffic through an {@link IpSecTransform}
|
|
|
|
|
|
* towards the host.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
|
2018-01-05 19:25:13 -08:00
|
|
|
|
*/
|
|
|
|
|
|
public static final int DIRECTION_IN = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* Used when applying a transform to direct traffic through an {@link IpSecTransform}
|
|
|
|
|
|
* away from the host.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
|
2018-01-05 19:25:13 -08:00
|
|
|
|
*/
|
|
|
|
|
|
public static final int DIRECTION_OUT = 1;
|
|
|
|
|
|
|
2021-04-15 11:59:16 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Used when applying a transform to direct traffic through an {@link IpSecTransform} for
|
|
|
|
|
|
* forwarding between interfaces.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>See {@link #applyTransportModeTransform(Socket, int, IpSecTransform)}.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @hide
|
|
|
|
|
|
*/
|
2021-12-15 23:27:18 +08:00
|
|
|
|
@SystemApi(client = MODULE_LIBRARIES)
|
2021-04-15 11:59:16 -07:00
|
|
|
|
public static final int DIRECTION_FWD = 2;
|
|
|
|
|
|
|
2017-03-07 13:23:36 -08:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* The Security Parameter Index (SPI) 0 indicates an unknown or invalid index.
|
2017-03-07 13:23:36 -08:00
|
|
|
|
*
|
|
|
|
|
|
* <p>No IPsec packet may contain an SPI of 0.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* @hide
|
2017-03-07 13:23:36 -08:00
|
|
|
|
*/
|
2017-11-16 10:58:01 -08:00
|
|
|
|
@TestApi public static final int INVALID_SECURITY_PARAMETER_INDEX = 0;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
|
|
|
|
|
|
/** @hide */
|
|
|
|
|
|
public interface Status {
|
2021-12-06 15:18:42 +08:00
|
|
|
|
int OK = 0;
|
|
|
|
|
|
int RESOURCE_UNAVAILABLE = 1;
|
|
|
|
|
|
int SPI_UNAVAILABLE = 2;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @hide */
|
2017-12-13 18:51:35 -08:00
|
|
|
|
public static final int INVALID_RESOURCE_ID = -1;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
|
2017-01-12 18:38:57 -08:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Thrown to indicate that a requested SPI is in use.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>The combination of remote {@code InetAddress} and SPI must be unique across all apps on
|
|
|
|
|
|
* one device. If this error is encountered, a new SPI is required before a transform may be
|
|
|
|
|
|
* created. This error can be avoided by calling {@link
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* IpSecManager#allocateSecurityParameterIndex}.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
public static final class SpiUnavailableException extends AndroidException {
|
|
|
|
|
|
private final int mSpi;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Construct an exception indicating that a transform with the given SPI is already in use
|
|
|
|
|
|
* or otherwise unavailable.
|
|
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @param msg description indicating the colliding SPI
|
2017-01-12 18:38:57 -08:00
|
|
|
|
* @param spi the SPI that could not be used due to a collision
|
|
|
|
|
|
*/
|
|
|
|
|
|
SpiUnavailableException(String msg, int spi) {
|
2017-04-21 15:53:51 -07:00
|
|
|
|
super(msg + " (spi: " + spi + ")");
|
2017-01-12 18:38:57 -08:00
|
|
|
|
mSpi = spi;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/** Get the SPI that caused a collision. */
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public int getSpi() {
|
|
|
|
|
|
return mSpi;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Thrown to indicate that an IPsec resource is unavailable.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>This could apply to resources such as sockets, {@link SecurityParameterIndex}, {@link
|
|
|
|
|
|
* IpSecTransform}, or other system resources. If this exception is thrown, users should release
|
|
|
|
|
|
* allocated objects of the type requested.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
public static final class ResourceUnavailableException extends AndroidException {
|
|
|
|
|
|
|
|
|
|
|
|
ResourceUnavailableException(String msg) {
|
|
|
|
|
|
super(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-15 18:06:06 -07:00
|
|
|
|
private final Context mContext;
|
2017-03-01 18:55:06 -08:00
|
|
|
|
private final IIpSecService mService;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* This class represents a reserved SPI.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>Objects of this type are used to track reserved security parameter indices. They can be
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* obtained by calling {@link IpSecManager#allocateSecurityParameterIndex} and must be released
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* by calling {@link #close()} when they are no longer needed.
|
|
|
|
|
|
*/
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public static final class SecurityParameterIndex implements AutoCloseable {
|
2017-03-01 18:55:06 -08:00
|
|
|
|
private final IIpSecService mService;
|
2018-01-05 19:25:13 -08:00
|
|
|
|
private final InetAddress mDestinationAddress;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
private final CloseGuard mCloseGuard = CloseGuard.get();
|
2017-03-07 13:23:36 -08:00
|
|
|
|
private int mSpi = INVALID_SECURITY_PARAMETER_INDEX;
|
2017-12-13 18:51:35 -08:00
|
|
|
|
private int mResourceId = INVALID_RESOURCE_ID;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/** Get the underlying SPI held by this object. */
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public int getSpi() {
|
|
|
|
|
|
return mSpi;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Release an SPI that was previously reserved.
|
|
|
|
|
|
*
|
2017-03-29 10:47:59 -07:00
|
|
|
|
* <p>Release an SPI for use by other users in the system. If a SecurityParameterIndex is
|
|
|
|
|
|
* applied to an IpSecTransform, it will become unusable for future transforms but should
|
|
|
|
|
|
* still be closed to ensure system resources are released.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void close() {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
try {
|
|
|
|
|
|
mService.releaseSecurityParameterIndex(mResourceId);
|
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
// On close we swallow all random exceptions since failure to close is not
|
|
|
|
|
|
// actionable by the user.
|
|
|
|
|
|
Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
mResourceId = INVALID_RESOURCE_ID;
|
|
|
|
|
|
mCloseGuard.close();
|
2017-04-04 19:37:48 -07:00
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/** Check that the SPI was closed properly. */
|
2017-01-12 18:38:57 -08:00
|
|
|
|
@Override
|
2017-11-07 17:17:45 -08:00
|
|
|
|
protected void finalize() throws Throwable {
|
2017-01-12 18:38:57 -08:00
|
|
|
|
if (mCloseGuard != null) {
|
|
|
|
|
|
mCloseGuard.warnIfOpen();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-07 13:23:36 -08:00
|
|
|
|
private SecurityParameterIndex(
|
2018-01-05 19:25:13 -08:00
|
|
|
|
@NonNull IIpSecService service, InetAddress destinationAddress, int spi)
|
2017-03-07 13:23:36 -08:00
|
|
|
|
throws ResourceUnavailableException, SpiUnavailableException {
|
|
|
|
|
|
mService = service;
|
2018-01-05 19:25:13 -08:00
|
|
|
|
mDestinationAddress = destinationAddress;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
try {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
IpSecSpiResponse result =
|
2017-11-16 10:58:01 -08:00
|
|
|
|
mService.allocateSecurityParameterIndex(
|
2018-01-05 19:25:13 -08:00
|
|
|
|
destinationAddress.getHostAddress(), spi, new Binder());
|
2017-03-07 13:23:36 -08:00
|
|
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
|
|
throw new NullPointerException("Received null response from IpSecService");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-04 19:37:48 -07:00
|
|
|
|
int status = result.status;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
switch (status) {
|
|
|
|
|
|
case Status.OK:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Status.RESOURCE_UNAVAILABLE:
|
|
|
|
|
|
throw new ResourceUnavailableException(
|
|
|
|
|
|
"No more SPIs may be allocated by this requester.");
|
|
|
|
|
|
case Status.SPI_UNAVAILABLE:
|
|
|
|
|
|
throw new SpiUnavailableException("Requested SPI is unavailable", spi);
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
"Unknown status returned by IpSecService: " + status);
|
|
|
|
|
|
}
|
2017-04-04 19:37:48 -07:00
|
|
|
|
mSpi = result.spi;
|
|
|
|
|
|
mResourceId = result.resourceId;
|
2017-03-07 13:23:36 -08:00
|
|
|
|
|
|
|
|
|
|
if (mSpi == INVALID_SECURITY_PARAMETER_INDEX) {
|
|
|
|
|
|
throw new RuntimeException("Invalid SPI returned by IpSecService: " + status);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mResourceId == INVALID_RESOURCE_ID) {
|
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
"Invalid Resource ID returned by IpSecService: " + status);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
|
|
|
|
|
mCloseGuard.open("open");
|
|
|
|
|
|
}
|
2017-04-04 19:37:48 -07:00
|
|
|
|
|
|
|
|
|
|
/** @hide */
|
2017-08-23 13:46:33 -07:00
|
|
|
|
@VisibleForTesting
|
|
|
|
|
|
public int getResourceId() {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
return mResourceId;
|
|
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
|
return new StringBuilder()
|
|
|
|
|
|
.append("SecurityParameterIndex{spi=")
|
|
|
|
|
|
.append(mSpi)
|
|
|
|
|
|
.append(",resourceId=")
|
|
|
|
|
|
.append(mResourceId)
|
|
|
|
|
|
.append("}")
|
|
|
|
|
|
.toString();
|
|
|
|
|
|
}
|
2017-03-07 13:23:36 -08:00
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* Reserve a random SPI for traffic bound to or from the specified destination address.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
|
|
|
|
|
* <p>If successful, this SPI is guaranteed available until released by a call to {@link
|
|
|
|
|
|
* SecurityParameterIndex#close()}.
|
|
|
|
|
|
*
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* @param destinationAddress the destination address for traffic bearing the requested SPI.
|
|
|
|
|
|
* For inbound traffic, the destination should be an address currently assigned on-device.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
* @return the reserved SecurityParameterIndex
|
2021-12-06 15:18:42 +08:00
|
|
|
|
* @throws ResourceUnavailableException indicating that too many SPIs are
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* currently allocated for this user
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
|
|
|
|
|
public SecurityParameterIndex allocateSecurityParameterIndex(
|
|
|
|
|
|
@NonNull InetAddress destinationAddress) throws ResourceUnavailableException {
|
2017-04-06 18:16:28 -07:00
|
|
|
|
try {
|
|
|
|
|
|
return new SecurityParameterIndex(
|
|
|
|
|
|
mService,
|
2018-01-05 19:25:13 -08:00
|
|
|
|
destinationAddress,
|
2017-04-06 18:16:28 -07:00
|
|
|
|
IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowUncheckedExceptionFromServiceSpecificException(e);
|
2017-04-06 18:16:28 -07:00
|
|
|
|
} catch (SpiUnavailableException unlikely) {
|
2018-04-03 16:13:19 -07:00
|
|
|
|
// Because this function allocates a totally random SPI, it really shouldn't ever
|
|
|
|
|
|
// fail to allocate an SPI; we simply need this because the exception is checked.
|
2017-04-06 18:16:28 -07:00
|
|
|
|
throw new ResourceUnavailableException("No SPIs available");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* Reserve the requested SPI for traffic bound to or from the specified destination address.
|
2017-04-06 18:16:28 -07:00
|
|
|
|
*
|
|
|
|
|
|
* <p>If successful, this SPI is guaranteed available until released by a call to {@link
|
|
|
|
|
|
* SecurityParameterIndex#close()}.
|
|
|
|
|
|
*
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* @param destinationAddress the destination address for traffic bearing the requested SPI.
|
|
|
|
|
|
* For inbound traffic, the destination should be an address currently assigned on-device.
|
2018-04-03 16:13:19 -07:00
|
|
|
|
* @param requestedSpi the requested SPI. The range 1-255 is reserved and may not be used. See
|
|
|
|
|
|
* RFC 4303 Section 2.1.
|
2017-04-06 18:16:28 -07:00
|
|
|
|
* @return the reserved SecurityParameterIndex
|
2021-12-06 15:18:42 +08:00
|
|
|
|
* @throws ResourceUnavailableException indicating that too many SPIs are
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* currently allocated for this user
|
2021-12-06 15:18:42 +08:00
|
|
|
|
* @throws SpiUnavailableException indicating that the requested SPI could not be
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* reserved
|
2017-04-06 18:16:28 -07:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
2017-11-16 10:58:01 -08:00
|
|
|
|
public SecurityParameterIndex allocateSecurityParameterIndex(
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull InetAddress destinationAddress, int requestedSpi)
|
2017-01-12 18:38:57 -08:00
|
|
|
|
throws SpiUnavailableException, ResourceUnavailableException {
|
2017-04-06 18:16:28 -07:00
|
|
|
|
if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
|
|
|
|
|
|
throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
|
|
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
try {
|
|
|
|
|
|
return new SecurityParameterIndex(mService, destinationAddress, requestedSpi);
|
|
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowUncheckedExceptionFromServiceSpecificException(e);
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Apply an IPsec transform to a stream socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
|
|
|
|
|
|
* socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* unprotected traffic can resume on that socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>For security reasons, the destination address of any traffic on the socket must match the
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* other IP address will result in an IOException. In addition, reads and writes on the socket
|
|
|
|
|
|
* will throw IOException if the user deactivates the transform (by calling {@link
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
2018-03-14 19:01:14 -07:00
|
|
|
|
* <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
|
|
|
|
|
|
* applied transform before completion of graceful shutdown may result in the shutdown sequence
|
|
|
|
|
|
* failing to complete. As such, applications requiring graceful shutdown MUST close the socket
|
|
|
|
|
|
* prior to deactivating the applied transform. Socket closure may be performed asynchronously
|
|
|
|
|
|
* (in batches), so the returning of a close function does not guarantee shutdown of a socket.
|
|
|
|
|
|
* Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
|
|
|
|
|
|
* sufficient to ensure shutdown.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
|
|
|
|
|
|
* prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
|
|
|
|
|
|
* packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
|
|
|
|
|
|
* SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
|
|
|
|
|
|
*
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* <h4>Rekey Procedure</h4>
|
|
|
|
|
|
*
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* <p>When applying a new tranform to a socket in the outbound direction, the previous transform
|
|
|
|
|
|
* will be removed and the new transform will take effect immediately, sending all traffic on
|
|
|
|
|
|
* the new transform; however, when applying a transform in the inbound direction, traffic
|
|
|
|
|
|
* on the old transform will continue to be decrypted and delivered until that transform is
|
|
|
|
|
|
* deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
|
|
|
|
|
|
* procedures where both transforms are valid until both endpoints are using the new transform
|
|
|
|
|
|
* and all in-flight packets have been received.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
|
|
|
|
|
* @param socket a stream socket
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* @param direction the direction in which the transform should be applied
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @param transform a transport mode {@code IpSecTransform}
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be applied
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void applyTransportModeTransform(@NonNull Socket socket,
|
|
|
|
|
|
@PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
|
2018-04-02 18:12:34 -07:00
|
|
|
|
// Ensure creation of FD. See b/77548890 for more details.
|
|
|
|
|
|
socket.getSoLinger();
|
|
|
|
|
|
|
2018-01-16 12:08:43 -08:00
|
|
|
|
applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Apply an IPsec transform to a datagram socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
|
|
|
|
|
|
* socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* unprotected traffic can resume on that socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>For security reasons, the destination address of any traffic on the socket must match the
|
|
|
|
|
|
* remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
|
|
|
|
|
|
* other IP address will result in an IOException. In addition, reads and writes on the socket
|
|
|
|
|
|
* will throw IOException if the user deactivates the transform (by calling {@link
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* <h4>Rekey Procedure</h4>
|
|
|
|
|
|
*
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* <p>When applying a new tranform to a socket in the outbound direction, the previous transform
|
|
|
|
|
|
* will be removed and the new transform will take effect immediately, sending all traffic on
|
|
|
|
|
|
* the new transform; however, when applying a transform in the inbound direction, traffic
|
|
|
|
|
|
* on the old transform will continue to be decrypted and delivered until that transform is
|
|
|
|
|
|
* deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
|
|
|
|
|
|
* procedures where both transforms are valid until both endpoints are using the new transform
|
|
|
|
|
|
* and all in-flight packets have been received.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
|
|
|
|
|
* @param socket a datagram socket
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* @param direction the direction in which the transform should be applied
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @param transform a transport mode {@code IpSecTransform}
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be applied
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void applyTransportModeTransform(@NonNull DatagramSocket socket,
|
|
|
|
|
|
@PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
|
2018-01-16 12:08:43 -08:00
|
|
|
|
applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
|
2017-03-07 13:23:36 -08:00
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
2017-04-06 17:46:00 -07:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Apply an IPsec transform to a socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>This applies transport mode encapsulation to the given socket. Once applied, I/O on the
|
|
|
|
|
|
* socket will be encapsulated according to the parameters of the {@code IpSecTransform}. When
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* the transform is removed from the socket by calling {@link #removeTransportModeTransforms},
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* unprotected traffic can resume on that socket.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>For security reasons, the destination address of any traffic on the socket must match the
|
|
|
|
|
|
* remote {@code InetAddress} of the {@code IpSecTransform}. Attempts to send traffic to any
|
|
|
|
|
|
* other IP address will result in an IOException. In addition, reads and writes on the socket
|
|
|
|
|
|
* will throw IOException if the user deactivates the transform (by calling {@link
|
2018-01-05 19:25:13 -08:00
|
|
|
|
* IpSecTransform#close()}) without calling {@link #removeTransportModeTransforms}.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
2018-03-14 19:01:14 -07:00
|
|
|
|
* <p>Note that when applied to TCP sockets, calling {@link IpSecTransform#close()} on an
|
|
|
|
|
|
* applied transform before completion of graceful shutdown may result in the shutdown sequence
|
|
|
|
|
|
* failing to complete. As such, applications requiring graceful shutdown MUST close the socket
|
|
|
|
|
|
* prior to deactivating the applied transform. Socket closure may be performed asynchronously
|
|
|
|
|
|
* (in batches), so the returning of a close function does not guarantee shutdown of a socket.
|
|
|
|
|
|
* Setting an SO_LINGER timeout results in socket closure being performed synchronously, and is
|
|
|
|
|
|
* sufficient to ensure shutdown.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Specifically, if the transform is deactivated (by calling {@link IpSecTransform#close()}),
|
|
|
|
|
|
* prior to the socket being closed, the standard [FIN - FIN/ACK - ACK], or the reset [RST]
|
|
|
|
|
|
* packets are dropped due to the lack of a valid Transform. Similarly, if a socket without the
|
|
|
|
|
|
* SO_LINGER option set is closed, the delayed/batched FIN packets may be dropped.
|
|
|
|
|
|
*
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* <h4>Rekey Procedure</h4>
|
|
|
|
|
|
*
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* <p>When applying a new tranform to a socket in the outbound direction, the previous transform
|
|
|
|
|
|
* will be removed and the new transform will take effect immediately, sending all traffic on
|
|
|
|
|
|
* the new transform; however, when applying a transform in the inbound direction, traffic
|
|
|
|
|
|
* on the old transform will continue to be decrypted and delivered until that transform is
|
|
|
|
|
|
* deallocated by calling {@link IpSecTransform#close()}. This overlap allows lossless rekey
|
|
|
|
|
|
* procedures where both transforms are valid until both endpoints are using the new transform
|
|
|
|
|
|
* and all in-flight packets have been received.
|
2017-04-06 17:46:00 -07:00
|
|
|
|
*
|
|
|
|
|
|
* @param socket a socket file descriptor
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* @param direction the direction in which the transform should be applied
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @param transform a transport mode {@code IpSecTransform}
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be applied
|
2017-04-06 17:46:00 -07:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void applyTransportModeTransform(@NonNull FileDescriptor socket,
|
|
|
|
|
|
@PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
// We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
|
2018-01-16 12:08:43 -08:00
|
|
|
|
// constructor takes control and closes the user's FD when we exit the method.
|
2017-04-04 19:37:48 -07:00
|
|
|
|
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
|
2018-01-05 19:25:13 -08:00
|
|
|
|
mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
2017-04-04 19:37:48 -07:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
2017-04-06 17:46:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-12 18:38:57 -08:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Remove an IPsec transform from a stream socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2018-01-17 01:00:20 -08:00
|
|
|
|
* <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
|
|
|
|
|
|
* socket allows the socket to be reused for communication in the clear.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
|
|
|
|
|
|
* {@link IpSecTransform#close()}, then communication on the socket will fail until this method
|
|
|
|
|
|
* is called.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param socket a socket that previously had a transform applied to it
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be removed from the socket
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void removeTransportModeTransforms(@NonNull Socket socket) throws IOException {
|
2018-04-02 18:12:34 -07:00
|
|
|
|
// Ensure creation of FD. See b/77548890 for more details.
|
|
|
|
|
|
socket.getSoLinger();
|
|
|
|
|
|
|
2018-01-17 01:00:20 -08:00
|
|
|
|
removeTransportModeTransforms(socket.getFileDescriptor$());
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Remove an IPsec transform from a datagram socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2018-01-17 01:00:20 -08:00
|
|
|
|
* <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
|
|
|
|
|
|
* socket allows the socket to be reused for communication in the clear.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
|
|
|
|
|
|
* {@link IpSecTransform#close()}, then communication on the socket will fail until this method
|
|
|
|
|
|
* is called.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param socket a socket that previously had a transform applied to it
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be removed from the socket
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void removeTransportModeTransforms(@NonNull DatagramSocket socket) throws IOException {
|
2018-01-17 01:00:20 -08:00
|
|
|
|
removeTransportModeTransforms(socket.getFileDescriptor$());
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-06 17:46:00 -07:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Remove an IPsec transform from a socket.
|
2017-04-06 17:46:00 -07:00
|
|
|
|
*
|
2018-01-17 01:00:20 -08:00
|
|
|
|
* <p>Once removed, traffic on the socket will not be encrypted. Removing transforms from a
|
|
|
|
|
|
* socket allows the socket to be reused for communication in the clear.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* <p>If an {@code IpSecTransform} object applied to this socket was deallocated by calling
|
|
|
|
|
|
* {@link IpSecTransform#close()}, then communication on the socket will fail until this method
|
|
|
|
|
|
* is called.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param socket a socket that previously had a transform applied to it
|
|
|
|
|
|
* @throws IOException indicating that the transform could not be removed from the socket
|
2017-04-06 17:46:00 -07:00
|
|
|
|
*/
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void removeTransportModeTransforms(@NonNull FileDescriptor socket) throws IOException {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
|
2018-01-17 01:00:20 -08:00
|
|
|
|
mService.removeTransportModeTransforms(pfd);
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
2017-03-07 13:23:36 -08:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Remove a Tunnel Mode IPsec Transform from a {@link Network}. This must be used as part of
|
|
|
|
|
|
* cleanup if a tunneled Network experiences a change in default route. The Network will drop
|
|
|
|
|
|
* all traffic that cannot be routed to the Tunnel's outbound interface. If that interface is
|
|
|
|
|
|
* lost, all traffic will drop.
|
|
|
|
|
|
*
|
2017-11-16 10:58:01 -08:00
|
|
|
|
* <p>TODO: Update javadoc for tunnel mode APIs at the same time the APIs are re-worked.
|
|
|
|
|
|
*
|
2017-01-12 18:38:57 -08:00
|
|
|
|
* @param net a network that currently has transform applied to it.
|
|
|
|
|
|
* @param transform a Tunnel Mode IPsec Transform that has been previously applied to the given
|
|
|
|
|
|
* network
|
|
|
|
|
|
* @hide
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void removeTunnelModeTransform(Network net, IpSecTransform transform) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* This class provides access to a UDP encapsulation Socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>{@code UdpEncapsulationSocket} wraps a system-provided datagram socket intended for IKEv2
|
|
|
|
|
|
* signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link
|
|
|
|
|
|
* IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the
|
|
|
|
|
|
* caller. The caller should not close the {@code FileDescriptor} returned by {@link
|
2018-03-27 16:55:48 -07:00
|
|
|
|
* #getFileDescriptor}, but should use {@link #close} instead.
|
2017-04-21 15:53:51 -07:00
|
|
|
|
*
|
|
|
|
|
|
* <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic
|
|
|
|
|
|
* of the next user who binds to that port. To prevent this scenario, these sockets are held
|
|
|
|
|
|
* open by the system so that they may only be closed by calling {@link #close} or when the user
|
|
|
|
|
|
* process exits.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
public static final class UdpEncapsulationSocket implements AutoCloseable {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
private final ParcelFileDescriptor mPfd;
|
2017-03-01 18:55:06 -08:00
|
|
|
|
private final IIpSecService mService;
|
2017-12-13 18:51:35 -08:00
|
|
|
|
private int mResourceId = INVALID_RESOURCE_ID;
|
2017-04-04 19:37:48 -07:00
|
|
|
|
private final int mPort;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
private final CloseGuard mCloseGuard = CloseGuard.get();
|
|
|
|
|
|
|
2017-03-07 13:23:36 -08:00
|
|
|
|
private UdpEncapsulationSocket(@NonNull IIpSecService service, int port)
|
2017-04-04 19:37:48 -07:00
|
|
|
|
throws ResourceUnavailableException, IOException {
|
2017-03-01 18:55:06 -08:00
|
|
|
|
mService = service;
|
2017-04-04 19:37:48 -07:00
|
|
|
|
try {
|
|
|
|
|
|
IpSecUdpEncapResponse result =
|
|
|
|
|
|
mService.openUdpEncapsulationSocket(port, new Binder());
|
|
|
|
|
|
switch (result.status) {
|
|
|
|
|
|
case Status.OK:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Status.RESOURCE_UNAVAILABLE:
|
|
|
|
|
|
throw new ResourceUnavailableException(
|
|
|
|
|
|
"No more Sockets may be allocated by this requester.");
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
"Unknown status returned by IpSecService: " + result.status);
|
|
|
|
|
|
}
|
|
|
|
|
|
mResourceId = result.resourceId;
|
|
|
|
|
|
mPort = result.port;
|
|
|
|
|
|
mPfd = result.fileDescriptor;
|
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
mCloseGuard.open("constructor");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-27 16:55:48 -07:00
|
|
|
|
/** Get the encapsulation socket's file descriptor. */
|
|
|
|
|
|
public FileDescriptor getFileDescriptor() {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
if (mPfd == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return mPfd.getFileDescriptor();
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/** Get the bound port of the wrapped socket. */
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public int getPort() {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
return mPort;
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Close this socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>This closes the wrapped socket. Open encapsulation sockets count against a user's
|
|
|
|
|
|
* resource limits, and forgetting to close them eventually will result in {@link
|
|
|
|
|
|
* ResourceUnavailableException} being thrown.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
2017-04-21 15:53:51 -07:00
|
|
|
|
@Override
|
2017-04-17 17:11:58 -07:00
|
|
|
|
public void close() throws IOException {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
try {
|
|
|
|
|
|
mService.closeUdpEncapsulationSocket(mResourceId);
|
2017-12-13 18:51:35 -08:00
|
|
|
|
mResourceId = INVALID_RESOURCE_ID;
|
2017-04-04 19:37:48 -07:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
// On close we swallow all random exceptions since failure to close is not
|
|
|
|
|
|
// actionable by the user.
|
|
|
|
|
|
Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
mResourceId = INVALID_RESOURCE_ID;
|
|
|
|
|
|
mCloseGuard.close();
|
2017-04-04 19:37:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
mPfd.close();
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
Log.e(TAG, "Failed to close UDP Encapsulation Socket with Port= " + mPort);
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-21 15:53:51 -07:00
|
|
|
|
/** Check that the socket was closed properly. */
|
2017-01-12 18:38:57 -08:00
|
|
|
|
@Override
|
|
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
|
|
if (mCloseGuard != null) {
|
|
|
|
|
|
mCloseGuard.warnIfOpen();
|
|
|
|
|
|
}
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
2017-04-04 19:37:48 -07:00
|
|
|
|
|
|
|
|
|
|
/** @hide */
|
2021-01-12 18:48:13 +08:00
|
|
|
|
@SystemApi(client = MODULE_LIBRARIES)
|
2017-08-23 13:46:33 -07:00
|
|
|
|
public int getResourceId() {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
return mResourceId;
|
|
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
|
return new StringBuilder()
|
|
|
|
|
|
.append("UdpEncapsulationSocket{port=")
|
|
|
|
|
|
.append(mPort)
|
|
|
|
|
|
.append(",resourceId=")
|
|
|
|
|
|
.append(mResourceId)
|
|
|
|
|
|
.append("}")
|
|
|
|
|
|
.toString();
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Open a socket for UDP encapsulation and bind to the given port.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @param port a local UDP port
|
|
|
|
|
|
* @return a socket that is bound to the given port
|
|
|
|
|
|
* @throws IOException indicating that the socket could not be opened or bound
|
|
|
|
|
|
* @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
// Returning a socket in this fashion that has been created and bound by the system
|
|
|
|
|
|
// is the only safe way to ensure that a socket is both accessible to the user and
|
|
|
|
|
|
// safely usable for Encapsulation without allowing a user to possibly unbind from/close
|
|
|
|
|
|
// the port, which could potentially impact the traffic of the next user who binds to that
|
|
|
|
|
|
// socket.
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public UdpEncapsulationSocket openUdpEncapsulationSocket(int port)
|
|
|
|
|
|
throws IOException, ResourceUnavailableException {
|
2017-04-04 19:37:48 -07:00
|
|
|
|
/*
|
|
|
|
|
|
* Most range checking is done in the service, but this version of the constructor expects
|
|
|
|
|
|
* a valid port number, and zero cannot be checked after being passed to the service.
|
|
|
|
|
|
*/
|
|
|
|
|
|
if (port == 0) {
|
|
|
|
|
|
throw new IllegalArgumentException("Specified port must be a valid port number!");
|
|
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
try {
|
|
|
|
|
|
return new UdpEncapsulationSocket(mService, port);
|
|
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Open a socket for UDP encapsulation.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>See {@link UdpEncapsulationSocket} for the proper way to close the returned socket.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* <p>The local port of the returned socket can be obtained by calling {@link
|
|
|
|
|
|
* UdpEncapsulationSocket#getPort()}.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* @return a socket that is bound to a local port
|
|
|
|
|
|
* @throws IOException indicating that the socket could not be opened or bound
|
|
|
|
|
|
* @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*/
|
|
|
|
|
|
// Returning a socket in this fashion that has been created and bound by the system
|
|
|
|
|
|
// is the only safe way to ensure that a socket is both accessible to the user and
|
|
|
|
|
|
// safely usable for Encapsulation without allowing a user to possibly unbind from/close
|
|
|
|
|
|
// the port, which could potentially impact the traffic of the next user who binds to that
|
|
|
|
|
|
// socket.
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
2017-01-12 18:38:57 -08:00
|
|
|
|
public UdpEncapsulationSocket openUdpEncapsulationSocket()
|
|
|
|
|
|
throws IOException, ResourceUnavailableException {
|
2018-04-03 16:13:19 -07:00
|
|
|
|
try {
|
|
|
|
|
|
return new UdpEncapsulationSocket(mService, 0);
|
|
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-17 16:09:24 -08:00
|
|
|
|
/**
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* This class represents an IpSecTunnelInterface
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
|
|
|
|
|
* <p>IpSecTunnelInterface objects track tunnel interfaces that serve as
|
|
|
|
|
|
* local endpoints for IPsec tunnels.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>Creating an IpSecTunnelInterface creates a device to which IpSecTransforms may be
|
|
|
|
|
|
* applied to provide IPsec security to packets sent through the tunnel. While a tunnel
|
|
|
|
|
|
* cannot be used in standalone mode within Android, the higher layers may use the tunnel
|
|
|
|
|
|
* to create Network objects which are accessible to the Android system.
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @hide
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*/
|
2021-02-01 08:12:34 +00:00
|
|
|
|
@SystemApi
|
2018-01-17 16:09:24 -08:00
|
|
|
|
public static final class IpSecTunnelInterface implements AutoCloseable {
|
2018-03-15 18:06:06 -07:00
|
|
|
|
private final String mOpPackageName;
|
2018-01-17 16:09:24 -08:00
|
|
|
|
private final IIpSecService mService;
|
|
|
|
|
|
private final InetAddress mRemoteAddress;
|
|
|
|
|
|
private final InetAddress mLocalAddress;
|
|
|
|
|
|
private final Network mUnderlyingNetwork;
|
|
|
|
|
|
private final CloseGuard mCloseGuard = CloseGuard.get();
|
|
|
|
|
|
private String mInterfaceName;
|
|
|
|
|
|
private int mResourceId = INVALID_RESOURCE_ID;
|
|
|
|
|
|
|
2021-02-01 08:12:34 +00:00
|
|
|
|
/** Get the underlying SPI held by this object. */
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
2018-01-17 16:09:24 -08:00
|
|
|
|
public String getInterfaceName() {
|
|
|
|
|
|
return mInterfaceName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* Add an address to the IpSecTunnelInterface
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
|
|
|
|
|
* <p>Add an address which may be used as the local inner address for
|
|
|
|
|
|
* tunneled traffic.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param address the local address for traffic inside the tunnel
|
2018-04-03 20:30:54 -07:00
|
|
|
|
* @param prefixLen length of the InetAddress prefix
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @hide
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*/
|
2021-02-01 08:12:34 +00:00
|
|
|
|
@SystemApi
|
2018-11-08 19:45:34 -08:00
|
|
|
|
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
|
2018-03-21 15:32:42 -07:00
|
|
|
|
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
2018-04-03 20:30:54 -07:00
|
|
|
|
public void addAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
|
2018-03-01 18:53:07 -08:00
|
|
|
|
try {
|
2018-03-15 18:06:06 -07:00
|
|
|
|
mService.addAddressToTunnelInterface(
|
2018-04-03 20:30:54 -07:00
|
|
|
|
mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
2018-03-01 18:53:07 -08:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* Remove an address from the IpSecTunnelInterface
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* <p>Remove an address which was previously added to the IpSecTunnelInterface
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
|
|
|
|
|
* @param address to be removed
|
2018-04-03 20:30:54 -07:00
|
|
|
|
* @param prefixLen length of the InetAddress prefix
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @hide
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*/
|
2021-02-01 08:12:34 +00:00
|
|
|
|
@SystemApi
|
2018-11-08 19:45:34 -08:00
|
|
|
|
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
|
2018-03-21 15:32:42 -07:00
|
|
|
|
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
2018-04-03 20:30:54 -07:00
|
|
|
|
public void removeAddress(@NonNull InetAddress address, int prefixLen) throws IOException {
|
2018-03-01 18:53:07 -08:00
|
|
|
|
try {
|
2018-03-15 18:06:06 -07:00
|
|
|
|
mService.removeAddressFromTunnelInterface(
|
2018-04-03 20:30:54 -07:00
|
|
|
|
mResourceId, new LinkAddress(address, prefixLen), mOpPackageName);
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
2018-03-01 18:53:07 -08:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-29 23:38:00 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Update the underlying network for this IpSecTunnelInterface.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>This new underlying network will be used for all transforms applied AFTER this call is
|
|
|
|
|
|
* complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to
|
|
|
|
|
|
* this tunnel interface, traffic will still use the old SA, and be routed on the old
|
|
|
|
|
|
* underlying network.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <p>To migrate IPsec tunnel mode traffic, a caller should:
|
|
|
|
|
|
*
|
|
|
|
|
|
* <ol>
|
|
|
|
|
|
* <li>Update the IpSecTunnelInterface’s underlying network.
|
|
|
|
|
|
* <li>Apply {@link IpSecTransform}(s) with matching addresses to this
|
|
|
|
|
|
* IpSecTunnelInterface.
|
|
|
|
|
|
* </ol>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel.
|
|
|
|
|
|
* This network MUST never be the network exposing this IpSecTunnelInterface, otherwise
|
2021-02-16 11:48:55 -08:00
|
|
|
|
* this method will throw an {@link IllegalArgumentException}. If the
|
|
|
|
|
|
* IpSecTunnelInterface is later added to this network, all outbound traffic will be
|
|
|
|
|
|
* blackholed.
|
2020-09-29 23:38:00 -07:00
|
|
|
|
*/
|
|
|
|
|
|
// TODO: b/169171001 Update the documentation when transform migration is supported.
|
|
|
|
|
|
// The purpose of making updating network and applying transforms separate is to leave open
|
|
|
|
|
|
// the possibility to support lossless migration procedures. To do that, Android platform
|
|
|
|
|
|
// will need to support multiple inbound tunnel mode transforms, just like it can support
|
|
|
|
|
|
// multiple transport mode transforms.
|
|
|
|
|
|
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
|
|
|
|
|
|
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
|
|
|
|
|
public void setUnderlyingNetwork(@NonNull Network underlyingNetwork) throws IOException {
|
|
|
|
|
|
try {
|
|
|
|
|
|
mService.setNetworkForTunnelInterface(
|
|
|
|
|
|
mResourceId, underlyingNetwork, mOpPackageName);
|
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-15 18:06:06 -07:00
|
|
|
|
private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
|
2018-01-17 16:09:24 -08:00
|
|
|
|
@NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
|
|
|
|
|
|
@NonNull Network underlyingNetwork)
|
|
|
|
|
|
throws ResourceUnavailableException, IOException {
|
2018-03-15 18:06:06 -07:00
|
|
|
|
mOpPackageName = ctx.getOpPackageName();
|
2018-01-17 16:09:24 -08:00
|
|
|
|
mService = service;
|
|
|
|
|
|
mLocalAddress = localAddress;
|
|
|
|
|
|
mRemoteAddress = remoteAddress;
|
|
|
|
|
|
mUnderlyingNetwork = underlyingNetwork;
|
2018-01-18 18:31:45 -08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
IpSecTunnelInterfaceResponse result =
|
|
|
|
|
|
mService.createTunnelInterface(
|
|
|
|
|
|
localAddress.getHostAddress(),
|
|
|
|
|
|
remoteAddress.getHostAddress(),
|
|
|
|
|
|
underlyingNetwork,
|
2018-03-15 18:06:06 -07:00
|
|
|
|
new Binder(),
|
|
|
|
|
|
mOpPackageName);
|
2018-01-18 18:31:45 -08:00
|
|
|
|
switch (result.status) {
|
|
|
|
|
|
case Status.OK:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Status.RESOURCE_UNAVAILABLE:
|
|
|
|
|
|
throw new ResourceUnavailableException(
|
|
|
|
|
|
"No more tunnel interfaces may be allocated by this requester.");
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
"Unknown status returned by IpSecService: " + result.status);
|
|
|
|
|
|
}
|
|
|
|
|
|
mResourceId = result.resourceId;
|
|
|
|
|
|
mInterfaceName = result.interfaceName;
|
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
|
|
|
|
|
mCloseGuard.open("constructor");
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* Delete an IpSecTunnelInterface
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
|
|
|
|
|
* <p>Calling close will deallocate the IpSecTunnelInterface and all of its system
|
|
|
|
|
|
* resources. Any packets bound for this interface either inbound or outbound will
|
|
|
|
|
|
* all be lost.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void close() {
|
2018-01-18 18:31:45 -08:00
|
|
|
|
try {
|
2018-03-15 18:06:06 -07:00
|
|
|
|
mService.deleteTunnelInterface(mResourceId, mOpPackageName);
|
2018-01-18 18:31:45 -08:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
// On close we swallow all random exceptions since failure to close is not
|
|
|
|
|
|
// actionable by the user.
|
|
|
|
|
|
Log.e(TAG, "Failed to close " + this + ", Exception=" + e);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
mResourceId = INVALID_RESOURCE_ID;
|
|
|
|
|
|
mCloseGuard.close();
|
2018-01-18 18:31:45 -08:00
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-01 08:12:34 +00:00
|
|
|
|
/** Check that the Interface was closed properly. */
|
2018-01-17 16:09:24 -08:00
|
|
|
|
@Override
|
|
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
|
|
if (mCloseGuard != null) {
|
|
|
|
|
|
mCloseGuard.warnIfOpen();
|
|
|
|
|
|
}
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
2018-01-18 18:31:45 -08:00
|
|
|
|
|
|
|
|
|
|
/** @hide */
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
|
public int getResourceId() {
|
|
|
|
|
|
return mResourceId;
|
|
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
|
2019-08-28 13:01:05 -07:00
|
|
|
|
@NonNull
|
2018-04-03 16:13:19 -07:00
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
|
return new StringBuilder()
|
|
|
|
|
|
.append("IpSecTunnelInterface{ifname=")
|
|
|
|
|
|
.append(mInterfaceName)
|
|
|
|
|
|
.append(",resourceId=")
|
|
|
|
|
|
.append(mResourceId)
|
|
|
|
|
|
.append("}")
|
|
|
|
|
|
.toString();
|
|
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Create a new IpSecTunnelInterface as a local endpoint for tunneled IPsec traffic.
|
|
|
|
|
|
*
|
2018-01-18 18:31:45 -08:00
|
|
|
|
* <p>An application that creates tunnels is responsible for cleaning up the tunnel when the
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* underlying network goes away, and the onLost() callback is received.
|
2018-01-18 18:31:45 -08:00
|
|
|
|
*
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @param localAddress The local addres of the tunnel
|
|
|
|
|
|
* @param remoteAddress The local addres of the tunnel
|
|
|
|
|
|
* @param underlyingNetwork the {@link Network} that will carry traffic for this tunnel.
|
|
|
|
|
|
* This network should almost certainly be a network such as WiFi with an L2 address.
|
|
|
|
|
|
* @return a new {@link IpSecManager#IpSecTunnelInterface} with the specified properties
|
|
|
|
|
|
* @throws IOException indicating that the socket could not be opened or bound
|
|
|
|
|
|
* @throws ResourceUnavailableException indicating that too many encapsulation sockets are open
|
2020-12-03 17:08:29 -08:00
|
|
|
|
* @hide
|
|
|
|
|
|
*/
|
2018-04-25 12:01:34 -07:00
|
|
|
|
@SystemApi
|
2018-03-16 17:27:30 -07:00
|
|
|
|
@NonNull
|
2018-11-08 19:45:34 -08:00
|
|
|
|
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
|
2018-03-21 15:32:42 -07:00
|
|
|
|
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
2018-01-17 16:09:24 -08:00
|
|
|
|
public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
|
|
|
|
|
|
@NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
|
|
|
|
|
|
throws ResourceUnavailableException, IOException {
|
2018-04-03 16:13:19 -07:00
|
|
|
|
try {
|
|
|
|
|
|
return new IpSecTunnelInterface(
|
|
|
|
|
|
mContext, mService, localAddress, remoteAddress, underlyingNetwork);
|
|
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
|
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-06 13:22:22 -08:00
|
|
|
|
* Apply an active Tunnel Mode IPsec Transform to a {@link IpSecTunnelInterface}, which will
|
|
|
|
|
|
* tunnel all traffic for the given direction through the underlying network's interface with
|
|
|
|
|
|
* IPsec (applies an outer IP header and IPsec Header to all traffic, and expects an additional
|
|
|
|
|
|
* IP header and IPsec Header on all inbound traffic).
|
|
|
|
|
|
* <p>Applications should probably not use this API directly.
|
|
|
|
|
|
*
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @param tunnel The {@link IpSecManager#IpSecTunnelInterface} that will use the supplied
|
2018-01-17 16:09:24 -08:00
|
|
|
|
* transform.
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @param direction the direction, {@link DIRECTION_OUT} or {@link #DIRECTION_IN} in which
|
2018-01-17 16:09:24 -08:00
|
|
|
|
* the transform will be used.
|
|
|
|
|
|
* @param transform an {@link IpSecTransform} created in tunnel mode
|
2021-02-01 08:12:34 +00:00
|
|
|
|
* @throws IOException indicating that the transform could not be applied due to a lower
|
|
|
|
|
|
* layer failure.
|
|
|
|
|
|
* @hide
|
2018-01-17 16:09:24 -08:00
|
|
|
|
*/
|
2021-02-01 08:12:34 +00:00
|
|
|
|
@SystemApi
|
2018-11-08 19:45:34 -08:00
|
|
|
|
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS)
|
2018-03-21 15:32:42 -07:00
|
|
|
|
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
2018-03-16 17:27:30 -07:00
|
|
|
|
public void applyTunnelModeTransform(@NonNull IpSecTunnelInterface tunnel,
|
|
|
|
|
|
@PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
|
2018-01-18 18:31:45 -08:00
|
|
|
|
try {
|
|
|
|
|
|
mService.applyTunnelModeTransform(
|
2018-03-15 18:06:06 -07:00
|
|
|
|
tunnel.getResourceId(), direction,
|
|
|
|
|
|
transform.getResourceId(), mContext.getOpPackageName());
|
2018-04-03 16:13:19 -07:00
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
|
throw rethrowCheckedExceptionFromServiceSpecificException(e);
|
2018-01-18 18:31:45 -08:00
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
|
throw e.rethrowFromSystemServer();
|
|
|
|
|
|
}
|
2018-01-17 16:09:24 -08:00
|
|
|
|
}
|
2018-03-06 13:22:22 -08:00
|
|
|
|
|
2017-01-12 18:38:57 -08:00
|
|
|
|
/**
|
2017-04-21 15:53:51 -07:00
|
|
|
|
* Construct an instance of IpSecManager within an application context.
|
2017-01-12 18:38:57 -08:00
|
|
|
|
*
|
|
|
|
|
|
* @param context the application context for this manager
|
|
|
|
|
|
* @hide
|
|
|
|
|
|
*/
|
2018-03-15 18:06:06 -07:00
|
|
|
|
public IpSecManager(Context ctx, IIpSecService service) {
|
|
|
|
|
|
mContext = ctx;
|
2021-12-22 16:35:13 +08:00
|
|
|
|
mService = Objects.requireNonNull(service, "missing service");
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|
2018-04-03 16:13:19 -07:00
|
|
|
|
|
|
|
|
|
|
private static void maybeHandleServiceSpecificException(ServiceSpecificException sse) {
|
|
|
|
|
|
// OsConstants are late binding, so switch statements can't be used.
|
|
|
|
|
|
if (sse.errorCode == OsConstants.EINVAL) {
|
|
|
|
|
|
throw new IllegalArgumentException(sse);
|
|
|
|
|
|
} else if (sse.errorCode == OsConstants.EAGAIN) {
|
|
|
|
|
|
throw new IllegalStateException(sse);
|
2018-07-16 14:56:20 -07:00
|
|
|
|
} else if (sse.errorCode == OsConstants.EOPNOTSUPP
|
|
|
|
|
|
|| sse.errorCode == OsConstants.EPROTONOSUPPORT) {
|
2018-04-03 16:13:19 -07:00
|
|
|
|
throw new UnsupportedOperationException(sse);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Convert an Errno SSE to the correct Unchecked exception type.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This method never actually returns.
|
|
|
|
|
|
*/
|
|
|
|
|
|
// package
|
|
|
|
|
|
static RuntimeException
|
|
|
|
|
|
rethrowUncheckedExceptionFromServiceSpecificException(ServiceSpecificException sse) {
|
|
|
|
|
|
maybeHandleServiceSpecificException(sse);
|
|
|
|
|
|
throw new RuntimeException(sse);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Convert an Errno SSE to the correct Checked or Unchecked exception type.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This method may throw IOException, or it may throw an unchecked exception; it will never
|
|
|
|
|
|
* actually return.
|
|
|
|
|
|
*/
|
|
|
|
|
|
// package
|
|
|
|
|
|
static IOException rethrowCheckedExceptionFromServiceSpecificException(
|
|
|
|
|
|
ServiceSpecificException sse) throws IOException {
|
|
|
|
|
|
// First see if this is an unchecked exception of a type we know.
|
|
|
|
|
|
// If so, then we prefer the unchecked (specific) type of exception.
|
|
|
|
|
|
maybeHandleServiceSpecificException(sse);
|
|
|
|
|
|
// If not, then all we can do is provide the SSE in the form of an IOException.
|
|
|
|
|
|
throw new ErrnoException(
|
|
|
|
|
|
"IpSec encountered errno=" + sse.errorCode, sse.errorCode).rethrowAsIOException();
|
|
|
|
|
|
}
|
2017-01-12 18:38:57 -08:00
|
|
|
|
}
|