Merge "Add a method to create a TAP interface with a given interface name."

This commit is contained in:
Xiao Ma 2022-04-22 13:10:19 +00:00 • committed by Gerrit Code Review
commit 7d7e7cd1fe
3 changed files with 56 additions and 11 deletions

View file

@ -29,7 +29,8 @@ import android.os.ParcelFileDescriptor;
*/ */
interface ITestNetworkManager interface ITestNetworkManager
{ {
TestNetworkInterface createInterface(boolean isTun, boolean bringUp, in LinkAddress[] addrs); TestNetworkInterface createInterface(boolean isTun, boolean bringUp, in LinkAddress[] addrs,
in @nullable String iface);
void setupTestNetwork(in String iface, in LinkProperties lp, in boolean isMetered, void setupTestNetwork(in String iface, in LinkProperties lp, in boolean isMetered,
in int[] administratorUids, in IBinder binder); in int[] administratorUids, in IBinder binder);

View file

@ -45,6 +45,12 @@ public class TestNetworkManager {
*/ */
public static final String TEST_TAP_PREFIX = "testtap"; public static final String TEST_TAP_PREFIX = "testtap";
/**
* Prefix for clat interfaces.
* @hide
*/
public static final String CLAT_INTERFACE_PREFIX = "v4-";
@NonNull private static final String TAG = TestNetworkManager.class.getSimpleName(); @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName();
@NonNull private final ITestNetworkManager mService; @NonNull private final ITestNetworkManager mService;
@ -160,7 +166,8 @@ public class TestNetworkManager {
public TestNetworkInterface createTunInterface(@NonNull Collection<LinkAddress> linkAddrs) { public TestNetworkInterface createTunInterface(@NonNull Collection<LinkAddress> linkAddrs) {
try { try {
final LinkAddress[] arr = new LinkAddress[linkAddrs.size()]; final LinkAddress[] arr = new LinkAddress[linkAddrs.size()];
return mService.createInterface(TUN, BRING_UP, linkAddrs.toArray(arr)); return mService.createInterface(TUN, BRING_UP, linkAddrs.toArray(arr),
null /* iface */);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
@ -178,7 +185,7 @@ public class TestNetworkManager {
@NonNull @NonNull
public TestNetworkInterface createTapInterface() { public TestNetworkInterface createTapInterface() {
try { try {
return mService.createInterface(TAP, BRING_UP, NO_ADDRS); return mService.createInterface(TAP, BRING_UP, NO_ADDRS, null /* iface */);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
@ -197,7 +204,29 @@ public class TestNetworkManager {
@NonNull @NonNull
public TestNetworkInterface createTapInterface(boolean bringUp) { public TestNetworkInterface createTapInterface(boolean bringUp) {
try { try {
return mService.createInterface(TAP, bringUp, NO_ADDRS); return mService.createInterface(TAP, bringUp, NO_ADDRS, null /* iface */);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Create a tap interface with a given interface name for testing purposes
*
* @param bringUp whether to bring up the interface before returning it.
* @param iface interface name to be assigned, so far only interface name which starts with
* "v4-testtap" or "v4-testtun" is allowed to be created. If it's null, then use
* the default name(e.g. testtap or testtun).
*
* @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the
* TAP interface.
* @hide
*/
@RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS)
@NonNull
public TestNetworkInterface createTapInterface(boolean bringUp, @NonNull String iface) {
try {
return mService.createInterface(TAP, bringUp, NO_ADDRS, iface);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View file

@ -16,6 +16,7 @@
package com.android.server; package com.android.server;
import static android.net.TestNetworkManager.CLAT_INTERFACE_PREFIX;
import static android.net.TestNetworkManager.TEST_TAP_PREFIX; import static android.net.TestNetworkManager.TEST_TAP_PREFIX;
import static android.net.TestNetworkManager.TEST_TUN_PREFIX; import static android.net.TestNetworkManager.TEST_TUN_PREFIX;
@ -98,6 +99,14 @@ class TestNetworkService extends ITestNetworkManager.Stub {
} }
} }
// TODO: find a way to allow the caller to pass in non-clat interface names, ensuring that
// those names do not conflict with names created by callers that do not pass in an interface
// name.
private static boolean isValidInterfaceName(@NonNull final String iface) {
return iface.startsWith(CLAT_INTERFACE_PREFIX + TEST_TUN_PREFIX)
|| iface.startsWith(CLAT_INTERFACE_PREFIX + TEST_TAP_PREFIX);
}
/** /**
* Create a TUN or TAP interface with the specified parameters. * Create a TUN or TAP interface with the specified parameters.
* *
@ -106,29 +115,35 @@ class TestNetworkService extends ITestNetworkManager.Stub {
*/ */
@Override @Override
public TestNetworkInterface createInterface(boolean isTun, boolean bringUp, public TestNetworkInterface createInterface(boolean isTun, boolean bringUp,
LinkAddress[] linkAddrs) { LinkAddress[] linkAddrs, @Nullable String iface) {
enforceTestNetworkPermissions(mContext); enforceTestNetworkPermissions(mContext);
Objects.requireNonNull(linkAddrs, "missing linkAddrs"); Objects.requireNonNull(linkAddrs, "missing linkAddrs");
String ifacePrefix = isTun ? TEST_TUN_PREFIX : TEST_TAP_PREFIX; String interfaceName = iface;
String iface = ifacePrefix + sTestTunIndex.getAndIncrement(); if (iface == null) {
String ifacePrefix = isTun ? TEST_TUN_PREFIX : TEST_TAP_PREFIX;
interfaceName = ifacePrefix + sTestTunIndex.getAndIncrement();
} else if (!isValidInterfaceName(iface)) {
throw new IllegalArgumentException("invalid interface name requested: " + iface);
}
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
ParcelFileDescriptor tunIntf = ParcelFileDescriptor tunIntf =
ParcelFileDescriptor.adoptFd(jniCreateTunTap(isTun, iface)); ParcelFileDescriptor.adoptFd(jniCreateTunTap(isTun, interfaceName));
for (LinkAddress addr : linkAddrs) { for (LinkAddress addr : linkAddrs) {
mNetd.interfaceAddAddress( mNetd.interfaceAddAddress(
iface, interfaceName,
addr.getAddress().getHostAddress(), addr.getAddress().getHostAddress(),
addr.getPrefixLength()); addr.getPrefixLength());
} }
if (bringUp) { if (bringUp) {
NetdUtils.setInterfaceUp(mNetd, iface); NetdUtils.setInterfaceUp(mNetd, interfaceName);
} }
return new TestNetworkInterface(tunIntf, iface); return new TestNetworkInterface(tunIntf, interfaceName);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} finally { } finally {