Merge "Refactor IpSecServiceConfiguration to Dependencies"
This commit is contained in:
@@ -119,6 +119,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
|
|
||||||
/* Binder context for this service */
|
/* Binder context for this service */
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
private final Dependencies mDeps;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The next non-repeating global ID for tracking resources between users, this service, and
|
* The next non-repeating global ID for tracking resources between users, this service, and
|
||||||
@@ -129,23 +130,24 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
@GuardedBy("IpSecService.this")
|
@GuardedBy("IpSecService.this")
|
||||||
private int mNextResourceId = 1;
|
private int mNextResourceId = 1;
|
||||||
|
|
||||||
interface IpSecServiceConfiguration {
|
/**
|
||||||
INetd getNetdInstance() throws RemoteException;
|
* Dependencies of IpSecService, for injection in tests.
|
||||||
|
*/
|
||||||
IpSecServiceConfiguration GETSRVINSTANCE =
|
@VisibleForTesting
|
||||||
new IpSecServiceConfiguration() {
|
public static class Dependencies {
|
||||||
@Override
|
/**
|
||||||
public INetd getNetdInstance() throws RemoteException {
|
* Get a reference to INetd.
|
||||||
final INetd netd = NetdService.getInstance();
|
*/
|
||||||
if (netd == null) {
|
public INetd getNetdInstance(Context context) throws RemoteException {
|
||||||
throw new RemoteException("Failed to Get Netd Instance");
|
final INetd netd = INetd.Stub.asInterface((IBinder)
|
||||||
}
|
context.getSystemService(Context.NETD_SERVICE));
|
||||||
return netd;
|
if (netd == null) {
|
||||||
}
|
throw new RemoteException("Failed to Get Netd Instance");
|
||||||
};
|
}
|
||||||
|
return netd;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final IpSecServiceConfiguration mSrvConfig;
|
|
||||||
final UidFdTagger mUidFdTagger;
|
final UidFdTagger mUidFdTagger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -625,8 +627,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
public void freeUnderlyingResources() {
|
public void freeUnderlyingResources() {
|
||||||
int spi = mSpi.getSpi();
|
int spi = mSpi.getSpi();
|
||||||
try {
|
try {
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecDeleteSecurityAssociation(
|
.ipSecDeleteSecurityAssociation(
|
||||||
mUid,
|
mUid,
|
||||||
mConfig.getSourceAddress(),
|
mConfig.getSourceAddress(),
|
||||||
@@ -678,11 +680,14 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
private final String mSourceAddress;
|
private final String mSourceAddress;
|
||||||
private final String mDestinationAddress;
|
private final String mDestinationAddress;
|
||||||
private int mSpi;
|
private int mSpi;
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
private boolean mOwnedByTransform = false;
|
private boolean mOwnedByTransform = false;
|
||||||
|
|
||||||
SpiRecord(int resourceId, String sourceAddress, String destinationAddress, int spi) {
|
SpiRecord(Context context, int resourceId, String sourceAddress,
|
||||||
|
String destinationAddress, int spi) {
|
||||||
super(resourceId);
|
super(resourceId);
|
||||||
|
mContext = context;
|
||||||
mSourceAddress = sourceAddress;
|
mSourceAddress = sourceAddress;
|
||||||
mDestinationAddress = destinationAddress;
|
mDestinationAddress = destinationAddress;
|
||||||
mSpi = spi;
|
mSpi = spi;
|
||||||
@@ -693,8 +698,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
public void freeUnderlyingResources() {
|
public void freeUnderlyingResources() {
|
||||||
try {
|
try {
|
||||||
if (!mOwnedByTransform) {
|
if (!mOwnedByTransform) {
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecDeleteSecurityAssociation(
|
.ipSecDeleteSecurityAssociation(
|
||||||
mUid, mSourceAddress, mDestinationAddress, mSpi, 0 /* mark */,
|
mUid, mSourceAddress, mDestinationAddress, mSpi, 0 /* mark */,
|
||||||
0 /* mask */, 0 /* if_id */);
|
0 /* mask */, 0 /* if_id */);
|
||||||
@@ -816,8 +821,10 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
private final int mIfId;
|
private final int mIfId;
|
||||||
|
|
||||||
private Network mUnderlyingNetwork;
|
private Network mUnderlyingNetwork;
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
TunnelInterfaceRecord(
|
TunnelInterfaceRecord(
|
||||||
|
Context context,
|
||||||
int resourceId,
|
int resourceId,
|
||||||
String interfaceName,
|
String interfaceName,
|
||||||
Network underlyingNetwork,
|
Network underlyingNetwork,
|
||||||
@@ -828,6 +835,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
int intfId) {
|
int intfId) {
|
||||||
super(resourceId);
|
super(resourceId);
|
||||||
|
|
||||||
|
mContext = context;
|
||||||
mInterfaceName = interfaceName;
|
mInterfaceName = interfaceName;
|
||||||
mUnderlyingNetwork = underlyingNetwork;
|
mUnderlyingNetwork = underlyingNetwork;
|
||||||
mLocalAddress = localAddr;
|
mLocalAddress = localAddr;
|
||||||
@@ -844,7 +852,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
// Teardown VTI
|
// Teardown VTI
|
||||||
// Delete global policies
|
// Delete global policies
|
||||||
try {
|
try {
|
||||||
final INetd netd = mSrvConfig.getNetdInstance();
|
final INetd netd = mDeps.getNetdInstance(mContext);
|
||||||
netd.ipSecRemoveTunnelInterface(mInterfaceName);
|
netd.ipSecRemoveTunnelInterface(mInterfaceName);
|
||||||
|
|
||||||
for (int selAddrFamily : ADDRESS_FAMILIES) {
|
for (int selAddrFamily : ADDRESS_FAMILIES) {
|
||||||
@@ -1012,7 +1020,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
* @param context Binder context for this service
|
* @param context Binder context for this service
|
||||||
*/
|
*/
|
||||||
private IpSecService(Context context) {
|
private IpSecService(Context context) {
|
||||||
this(context, IpSecServiceConfiguration.GETSRVINSTANCE);
|
this(context, new Dependencies());
|
||||||
}
|
}
|
||||||
|
|
||||||
static IpSecService create(Context context)
|
static IpSecService create(Context context)
|
||||||
@@ -1031,10 +1039,10 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public IpSecService(Context context, IpSecServiceConfiguration config) {
|
public IpSecService(Context context, Dependencies deps) {
|
||||||
this(
|
this(
|
||||||
context,
|
context,
|
||||||
config,
|
deps,
|
||||||
(fd, uid) -> {
|
(fd, uid) -> {
|
||||||
try {
|
try {
|
||||||
TrafficStats.setThreadStatsUid(uid);
|
TrafficStats.setThreadStatsUid(uid);
|
||||||
@@ -1047,10 +1055,9 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public IpSecService(Context context, IpSecServiceConfiguration config,
|
public IpSecService(Context context, Dependencies deps, UidFdTagger uidFdTagger) {
|
||||||
UidFdTagger uidFdTagger) {
|
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mSrvConfig = config;
|
mDeps = deps;
|
||||||
mUidFdTagger = uidFdTagger;
|
mUidFdTagger = uidFdTagger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1077,7 +1084,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
|
|
||||||
synchronized boolean isNetdAlive() {
|
synchronized boolean isNetdAlive() {
|
||||||
try {
|
try {
|
||||||
final INetd netd = mSrvConfig.getNetdInstance();
|
final INetd netd = mDeps.getNetdInstance(mContext);
|
||||||
if (netd == null) {
|
if (netd == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1143,14 +1150,15 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
spi =
|
spi =
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecAllocateSpi(callingUid, "", destinationAddress, requestedSpi);
|
.ipSecAllocateSpi(callingUid, "", destinationAddress, requestedSpi);
|
||||||
Log.d(TAG, "Allocated SPI " + spi);
|
Log.d(TAG, "Allocated SPI " + spi);
|
||||||
userRecord.mSpiRecords.put(
|
userRecord.mSpiRecords.put(
|
||||||
resourceId,
|
resourceId,
|
||||||
new RefcountedResource<SpiRecord>(
|
new RefcountedResource<SpiRecord>(
|
||||||
new SpiRecord(resourceId, "", destinationAddress, spi), binder));
|
new SpiRecord(mContext, resourceId, "",
|
||||||
|
destinationAddress, spi), binder));
|
||||||
} catch (ServiceSpecificException e) {
|
} catch (ServiceSpecificException e) {
|
||||||
if (e.errorCode == OsConstants.ENOENT) {
|
if (e.errorCode == OsConstants.ENOENT) {
|
||||||
return new IpSecSpiResponse(
|
return new IpSecSpiResponse(
|
||||||
@@ -1267,7 +1275,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
OsConstants.UDP_ENCAP,
|
OsConstants.UDP_ENCAP,
|
||||||
OsConstants.UDP_ENCAP_ESPINUDP);
|
OsConstants.UDP_ENCAP_ESPINUDP);
|
||||||
|
|
||||||
mSrvConfig.getNetdInstance().ipSecSetEncapSocketOwner(
|
mDeps.getNetdInstance(mContext).ipSecSetEncapSocketOwner(
|
||||||
new ParcelFileDescriptor(sockFd), callingUid);
|
new ParcelFileDescriptor(sockFd), callingUid);
|
||||||
if (port != 0) {
|
if (port != 0) {
|
||||||
Log.v(TAG, "Binding to port " + port);
|
Log.v(TAG, "Binding to port " + port);
|
||||||
@@ -1330,7 +1338,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
// Create VTI
|
// Create VTI
|
||||||
// Add inbound/outbound global policies
|
// Add inbound/outbound global policies
|
||||||
// (use reqid = 0)
|
// (use reqid = 0)
|
||||||
final INetd netd = mSrvConfig.getNetdInstance();
|
final INetd netd = mDeps.getNetdInstance(mContext);
|
||||||
netd.ipSecAddTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey, resourceId);
|
netd.ipSecAddTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey, resourceId);
|
||||||
|
|
||||||
BinderUtils.withCleanCallingIdentity(() -> {
|
BinderUtils.withCleanCallingIdentity(() -> {
|
||||||
@@ -1385,6 +1393,7 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
resourceId,
|
resourceId,
|
||||||
new RefcountedResource<TunnelInterfaceRecord>(
|
new RefcountedResource<TunnelInterfaceRecord>(
|
||||||
new TunnelInterfaceRecord(
|
new TunnelInterfaceRecord(
|
||||||
|
mContext,
|
||||||
resourceId,
|
resourceId,
|
||||||
intfName,
|
intfName,
|
||||||
underlyingNetwork,
|
underlyingNetwork,
|
||||||
@@ -1426,8 +1435,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
try {
|
try {
|
||||||
// We can assume general validity of the IP address, since we get them as a
|
// We can assume general validity of the IP address, since we get them as a
|
||||||
// LinkAddress, which does some validation.
|
// LinkAddress, which does some validation.
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.interfaceAddAddress(
|
.interfaceAddAddress(
|
||||||
tunnelInterfaceInfo.mInterfaceName,
|
tunnelInterfaceInfo.mInterfaceName,
|
||||||
localAddr.getAddress().getHostAddress(),
|
localAddr.getAddress().getHostAddress(),
|
||||||
@@ -1455,8 +1464,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
try {
|
try {
|
||||||
// We can assume general validity of the IP address, since we get them as a
|
// We can assume general validity of the IP address, since we get them as a
|
||||||
// LinkAddress, which does some validation.
|
// LinkAddress, which does some validation.
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.interfaceDelAddress(
|
.interfaceDelAddress(
|
||||||
tunnelInterfaceInfo.mInterfaceName,
|
tunnelInterfaceInfo.mInterfaceName,
|
||||||
localAddr.getAddress().getHostAddress(),
|
localAddr.getAddress().getHostAddress(),
|
||||||
@@ -1670,8 +1679,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
cryptName = crypt.getName();
|
cryptName = crypt.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecAddSecurityAssociation(
|
.ipSecAddSecurityAssociation(
|
||||||
Binder.getCallingUid(),
|
Binder.getCallingUid(),
|
||||||
c.getMode(),
|
c.getMode(),
|
||||||
@@ -1782,8 +1791,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
c.getMode() == IpSecTransform.MODE_TRANSPORT,
|
c.getMode() == IpSecTransform.MODE_TRANSPORT,
|
||||||
"Transform mode was not Transport mode; cannot be applied to a socket");
|
"Transform mode was not Transport mode; cannot be applied to a socket");
|
||||||
|
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecApplyTransportModeTransform(
|
.ipSecApplyTransportModeTransform(
|
||||||
socket,
|
socket,
|
||||||
callingUid,
|
callingUid,
|
||||||
@@ -1802,8 +1811,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
@Override
|
@Override
|
||||||
public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket)
|
public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket)
|
||||||
throws RemoteException {
|
throws RemoteException {
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecRemoveTransportModeTransform(socket);
|
.ipSecRemoveTransportModeTransform(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1879,8 +1888,8 @@ public class IpSecService extends IIpSecService.Stub {
|
|||||||
|
|
||||||
// Always update the policy with the relevant XFRM_IF_ID
|
// Always update the policy with the relevant XFRM_IF_ID
|
||||||
for (int selAddrFamily : ADDRESS_FAMILIES) {
|
for (int selAddrFamily : ADDRESS_FAMILIES) {
|
||||||
mSrvConfig
|
mDeps
|
||||||
.getNetdInstance()
|
.getNetdInstance(mContext)
|
||||||
.ipSecUpdateSecurityPolicy(
|
.ipSecUpdateSecurityPolicy(
|
||||||
callingUid,
|
callingUid,
|
||||||
selAddrFamily,
|
selAddrFamily,
|
||||||
|
|||||||
Reference in New Issue
Block a user