Merge "Remove NetdService usage from IpSecService" am: da5df1a9d8 am: 1c036ae377

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1903978

Change-Id: I10800c8268ffb5ae2f96fc88b4acd6d66c0b0fbc
This commit is contained in:
Aaron Huang
2021-12-16 13:02:19 +00:00
committed by Automerger Merge Worker

View File

@@ -45,7 +45,6 @@ import android.net.LinkAddress;
import android.net.LinkProperties; import android.net.LinkProperties;
import android.net.Network; import android.net.Network;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.net.util.NetdService;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
@@ -96,8 +95,6 @@ import java.util.Objects;
public class IpSecService extends IIpSecService.Stub { public class IpSecService extends IIpSecService.Stub {
private static final String TAG = "IpSecService"; private static final String TAG = "IpSecService";
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final String NETD_SERVICE_NAME = "netd";
private static final int[] ADDRESS_FAMILIES = private static final int[] ADDRESS_FAMILIES =
new int[] {OsConstants.AF_INET, OsConstants.AF_INET6}; new int[] {OsConstants.AF_INET, OsConstants.AF_INET6};
@@ -106,6 +103,8 @@ public class IpSecService extends IIpSecService.Stub {
@VisibleForTesting static final int MAX_PORT_BIND_ATTEMPTS = 10; @VisibleForTesting static final int MAX_PORT_BIND_ATTEMPTS = 10;
private final INetd mNetd;
static { static {
try { try {
INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0});
@@ -627,9 +626,7 @@ public class IpSecService extends IIpSecService.Stub {
public void freeUnderlyingResources() { public void freeUnderlyingResources() {
int spi = mSpi.getSpi(); int spi = mSpi.getSpi();
try { try {
mDeps mNetd.ipSecDeleteSecurityAssociation(
.getNetdInstance(mContext)
.ipSecDeleteSecurityAssociation(
mUid, mUid,
mConfig.getSourceAddress(), mConfig.getSourceAddress(),
mConfig.getDestinationAddress(), mConfig.getDestinationAddress(),
@@ -680,14 +677,12 @@ 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(Context context, int resourceId, String sourceAddress, SpiRecord(int resourceId, String sourceAddress,
String destinationAddress, int spi) { String destinationAddress, int spi) {
super(resourceId); super(resourceId);
mContext = context;
mSourceAddress = sourceAddress; mSourceAddress = sourceAddress;
mDestinationAddress = destinationAddress; mDestinationAddress = destinationAddress;
mSpi = spi; mSpi = spi;
@@ -698,9 +693,7 @@ public class IpSecService extends IIpSecService.Stub {
public void freeUnderlyingResources() { public void freeUnderlyingResources() {
try { try {
if (!mOwnedByTransform) { if (!mOwnedByTransform) {
mDeps mNetd.ipSecDeleteSecurityAssociation(
.getNetdInstance(mContext)
.ipSecDeleteSecurityAssociation(
mUid, mSourceAddress, mDestinationAddress, mSpi, 0 /* mark */, mUid, mSourceAddress, mDestinationAddress, mSpi, 0 /* mark */,
0 /* mask */, 0 /* if_id */); 0 /* mask */, 0 /* if_id */);
} }
@@ -821,10 +814,8 @@ 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,
@@ -835,7 +826,6 @@ 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;
@@ -852,18 +842,17 @@ public class IpSecService extends IIpSecService.Stub {
// Teardown VTI // Teardown VTI
// Delete global policies // Delete global policies
try { try {
final INetd netd = mDeps.getNetdInstance(mContext); mNetd.ipSecRemoveTunnelInterface(mInterfaceName);
netd.ipSecRemoveTunnelInterface(mInterfaceName);
for (int selAddrFamily : ADDRESS_FAMILIES) { for (int selAddrFamily : ADDRESS_FAMILIES) {
netd.ipSecDeleteSecurityPolicy( mNetd.ipSecDeleteSecurityPolicy(
mUid, mUid,
selAddrFamily, selAddrFamily,
IpSecManager.DIRECTION_OUT, IpSecManager.DIRECTION_OUT,
mOkey, mOkey,
0xffffffff, 0xffffffff,
mIfId); mIfId);
netd.ipSecDeleteSecurityPolicy( mNetd.ipSecDeleteSecurityPolicy(
mUid, mUid,
selAddrFamily, selAddrFamily,
IpSecManager.DIRECTION_IN, IpSecManager.DIRECTION_IN,
@@ -1026,7 +1015,6 @@ public class IpSecService extends IIpSecService.Stub {
static IpSecService create(Context context) static IpSecService create(Context context)
throws InterruptedException { throws InterruptedException {
final IpSecService service = new IpSecService(context); final IpSecService service = new IpSecService(context);
service.connectNativeNetdService();
return service; return service;
} }
@@ -1057,8 +1045,13 @@ public class IpSecService extends IIpSecService.Stub {
@VisibleForTesting @VisibleForTesting
public IpSecService(Context context, Dependencies deps, UidFdTagger uidFdTagger) { public IpSecService(Context context, Dependencies deps, UidFdTagger uidFdTagger) {
mContext = context; mContext = context;
mDeps = deps; mDeps = Objects.requireNonNull(deps, "Missing dependencies.");
mUidFdTagger = uidFdTagger; mUidFdTagger = uidFdTagger;
try {
mNetd = mDeps.getNetdInstance(mContext);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
} }
/** Called by system server when system is ready. */ /** Called by system server when system is ready. */
@@ -1070,25 +1063,12 @@ public class IpSecService extends IIpSecService.Stub {
} }
} }
private void connectNativeNetdService() {
// Avoid blocking the system server to do this
new Thread() {
@Override
public void run() {
synchronized (IpSecService.this) {
NetdService.get(NETD_FETCH_TIMEOUT_MS);
}
}
}.start();
}
synchronized boolean isNetdAlive() { synchronized boolean isNetdAlive() {
try { try {
final INetd netd = mDeps.getNetdInstance(mContext); if (mNetd == null) {
if (netd == null) {
return false; return false;
} }
return netd.isAlive(); return mNetd.isAlive();
} catch (RemoteException re) { } catch (RemoteException re) {
return false; return false;
} }
@@ -1149,15 +1129,12 @@ public class IpSecService extends IIpSecService.Stub {
IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi); IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi);
} }
spi = spi = mNetd.ipSecAllocateSpi(callingUid, "", destinationAddress, requestedSpi);
mDeps
.getNetdInstance(mContext)
.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(mContext, resourceId, "", new SpiRecord(resourceId, "",
destinationAddress, spi), binder)); destinationAddress, spi), binder));
} catch (ServiceSpecificException e) { } catch (ServiceSpecificException e) {
if (e.errorCode == OsConstants.ENOENT) { if (e.errorCode == OsConstants.ENOENT) {
@@ -1275,8 +1252,7 @@ public class IpSecService extends IIpSecService.Stub {
OsConstants.UDP_ENCAP, OsConstants.UDP_ENCAP,
OsConstants.UDP_ENCAP_ESPINUDP); OsConstants.UDP_ENCAP_ESPINUDP);
mDeps.getNetdInstance(mContext).ipSecSetEncapSocketOwner( mNetd.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);
Os.bind(sockFd, INADDR_ANY, port); Os.bind(sockFd, INADDR_ANY, port);
@@ -1338,16 +1314,15 @@ 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 = mDeps.getNetdInstance(mContext); mNetd.ipSecAddTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey, resourceId);
netd.ipSecAddTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey, resourceId);
BinderUtils.withCleanCallingIdentity(() -> { BinderUtils.withCleanCallingIdentity(() -> {
NetdUtils.setInterfaceUp(netd, intfName); NetdUtils.setInterfaceUp(mNetd, intfName);
}); });
for (int selAddrFamily : ADDRESS_FAMILIES) { for (int selAddrFamily : ADDRESS_FAMILIES) {
// Always send down correct local/remote addresses for template. // Always send down correct local/remote addresses for template.
netd.ipSecAddSecurityPolicy( mNetd.ipSecAddSecurityPolicy(
callerUid, callerUid,
selAddrFamily, selAddrFamily,
IpSecManager.DIRECTION_OUT, IpSecManager.DIRECTION_OUT,
@@ -1357,7 +1332,7 @@ public class IpSecService extends IIpSecService.Stub {
okey, okey,
0xffffffff, 0xffffffff,
resourceId); resourceId);
netd.ipSecAddSecurityPolicy( mNetd.ipSecAddSecurityPolicy(
callerUid, callerUid,
selAddrFamily, selAddrFamily,
IpSecManager.DIRECTION_IN, IpSecManager.DIRECTION_IN,
@@ -1377,7 +1352,7 @@ public class IpSecService extends IIpSecService.Stub {
// //
// This is necessary only on the tunnel interface, and not any the interface to // This is necessary only on the tunnel interface, and not any the interface to
// which traffic will be forwarded to. // which traffic will be forwarded to.
netd.ipSecAddSecurityPolicy( mNetd.ipSecAddSecurityPolicy(
callerUid, callerUid,
selAddrFamily, selAddrFamily,
IpSecManager.DIRECTION_FWD, IpSecManager.DIRECTION_FWD,
@@ -1393,7 +1368,6 @@ 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,
@@ -1435,9 +1409,7 @@ 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.
mDeps mNetd.interfaceAddAddress(
.getNetdInstance(mContext)
.interfaceAddAddress(
tunnelInterfaceInfo.mInterfaceName, tunnelInterfaceInfo.mInterfaceName,
localAddr.getAddress().getHostAddress(), localAddr.getAddress().getHostAddress(),
localAddr.getPrefixLength()); localAddr.getPrefixLength());
@@ -1464,9 +1436,7 @@ 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.
mDeps mNetd.interfaceDelAddress(
.getNetdInstance(mContext)
.interfaceDelAddress(
tunnelInterfaceInfo.mInterfaceName, tunnelInterfaceInfo.mInterfaceName,
localAddr.getAddress().getHostAddress(), localAddr.getAddress().getHostAddress(),
localAddr.getPrefixLength()); localAddr.getPrefixLength());
@@ -1679,9 +1649,7 @@ public class IpSecService extends IIpSecService.Stub {
cryptName = crypt.getName(); cryptName = crypt.getName();
} }
mDeps mNetd.ipSecAddSecurityAssociation(
.getNetdInstance(mContext)
.ipSecAddSecurityAssociation(
Binder.getCallingUid(), Binder.getCallingUid(),
c.getMode(), c.getMode(),
c.getSourceAddress(), c.getSourceAddress(),
@@ -1791,9 +1759,7 @@ 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");
mDeps mNetd.ipSecApplyTransportModeTransform(
.getNetdInstance(mContext)
.ipSecApplyTransportModeTransform(
socket, socket,
callingUid, callingUid,
direction, direction,
@@ -1811,9 +1777,7 @@ public class IpSecService extends IIpSecService.Stub {
@Override @Override
public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket) public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket)
throws RemoteException { throws RemoteException {
mDeps mNetd.ipSecRemoveTransportModeTransform(socket);
.getNetdInstance(mContext)
.ipSecRemoveTransportModeTransform(socket);
} }
/** /**
@@ -1888,9 +1852,7 @@ 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) {
mDeps mNetd.ipSecUpdateSecurityPolicy(
.getNetdInstance(mContext)
.ipSecUpdateSecurityPolicy(
callingUid, callingUid,
selAddrFamily, selAddrFamily,
direction, direction,