Merge "Add a method to create a TAP interface with a given interface name." am: 7d7e7cd1fe
Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2059333 Change-Id: Id882dfb9510ddc794719e9b3860d0d98ef09f41c Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
3c78a24a9a
3 changed files with 56 additions and 11 deletions
|
|
@ -29,7 +29,8 @@ import android.os.ParcelFileDescriptor;
|
|||
*/
|
||||
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,
|
||||
in int[] administratorUids, in IBinder binder);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,12 @@ public class TestNetworkManager {
|
|||
*/
|
||||
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 final ITestNetworkManager mService;
|
||||
|
|
@ -160,7 +166,8 @@ public class TestNetworkManager {
|
|||
public TestNetworkInterface createTunInterface(@NonNull Collection<LinkAddress> linkAddrs) {
|
||||
try {
|
||||
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) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
|
|
@ -178,7 +185,7 @@ public class TestNetworkManager {
|
|||
@NonNull
|
||||
public TestNetworkInterface createTapInterface() {
|
||||
try {
|
||||
return mService.createInterface(TAP, BRING_UP, NO_ADDRS);
|
||||
return mService.createInterface(TAP, BRING_UP, NO_ADDRS, null /* iface */);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
|
|
@ -197,7 +204,29 @@ public class TestNetworkManager {
|
|||
@NonNull
|
||||
public TestNetworkInterface createTapInterface(boolean bringUp) {
|
||||
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) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
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_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.
|
||||
*
|
||||
|
|
@ -106,29 +115,35 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||
*/
|
||||
@Override
|
||||
public TestNetworkInterface createInterface(boolean isTun, boolean bringUp,
|
||||
LinkAddress[] linkAddrs) {
|
||||
LinkAddress[] linkAddrs, @Nullable String iface) {
|
||||
enforceTestNetworkPermissions(mContext);
|
||||
|
||||
Objects.requireNonNull(linkAddrs, "missing linkAddrs");
|
||||
|
||||
String ifacePrefix = isTun ? TEST_TUN_PREFIX : TEST_TAP_PREFIX;
|
||||
String iface = ifacePrefix + sTestTunIndex.getAndIncrement();
|
||||
String interfaceName = iface;
|
||||
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();
|
||||
try {
|
||||
ParcelFileDescriptor tunIntf =
|
||||
ParcelFileDescriptor.adoptFd(jniCreateTunTap(isTun, iface));
|
||||
ParcelFileDescriptor.adoptFd(jniCreateTunTap(isTun, interfaceName));
|
||||
for (LinkAddress addr : linkAddrs) {
|
||||
mNetd.interfaceAddAddress(
|
||||
iface,
|
||||
interfaceName,
|
||||
addr.getAddress().getHostAddress(),
|
||||
addr.getPrefixLength());
|
||||
}
|
||||
|
||||
if (bringUp) {
|
||||
NetdUtils.setInterfaceUp(mNetd, iface);
|
||||
NetdUtils.setInterfaceUp(mNetd, interfaceName);
|
||||
}
|
||||
|
||||
return new TestNetworkInterface(tunIntf, iface);
|
||||
return new TestNetworkInterface(tunIntf, interfaceName);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
} finally {
|
||||
|
|
|
|||
Loading…
Reference in a new issue