1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.net; 17 18 import android.Manifest; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.RequiresPermission; 22 import android.annotation.SystemApi; 23 import android.os.IBinder; 24 import android.os.RemoteException; 25 26 import java.util.Arrays; 27 import java.util.Collection; 28 import java.util.Objects; 29 30 /** 31 * Class that allows creation and management of per-app, test-only networks 32 * 33 * @hide 34 */ 35 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 36 public class TestNetworkManager { 37 /** 38 * Prefix for tun interfaces created by this class. 39 * @hide 40 */ 41 public static final String TEST_TUN_PREFIX = "testtun"; 42 43 /** 44 * Prefix for tap interfaces created by this class. 45 */ 46 public static final String TEST_TAP_PREFIX = "testtap"; 47 48 /** 49 * Prefix for clat interfaces. 50 * @hide 51 */ 52 public static final String CLAT_INTERFACE_PREFIX = "v4-"; 53 54 @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName(); 55 56 @NonNull private final ITestNetworkManager mService; 57 58 private static final boolean TAP = false; 59 private static final boolean TUN = true; 60 private static final boolean BRING_UP = true; 61 private static final LinkAddress[] NO_ADDRS = new LinkAddress[0]; 62 63 /** @hide */ TestNetworkManager(@onNull ITestNetworkManager service)64 public TestNetworkManager(@NonNull ITestNetworkManager service) { 65 mService = Objects.requireNonNull(service, "missing ITestNetworkManager"); 66 } 67 68 /** 69 * Teardown the capability-limited, testing-only network for a given interface 70 * 71 * @param network The test network that should be torn down 72 * @hide 73 */ 74 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 75 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) teardownTestNetwork(@onNull Network network)76 public void teardownTestNetwork(@NonNull Network network) { 77 try { 78 mService.teardownTestNetwork(network.netId); 79 } catch (RemoteException e) { 80 throw e.rethrowFromSystemServer(); 81 } 82 } 83 setupTestNetwork( @onNull String iface, @Nullable LinkProperties lp, boolean isMetered, @NonNull int[] administratorUids, @NonNull IBinder binder)84 private void setupTestNetwork( 85 @NonNull String iface, 86 @Nullable LinkProperties lp, 87 boolean isMetered, 88 @NonNull int[] administratorUids, 89 @NonNull IBinder binder) { 90 try { 91 mService.setupTestNetwork(iface, lp, isMetered, administratorUids, binder); 92 } catch (RemoteException e) { 93 throw e.rethrowFromSystemServer(); 94 } 95 } 96 97 /** 98 * Sets up a capability-limited, testing-only network for a given interface 99 * 100 * @param lp The LinkProperties for the TestNetworkService to use for this test network. Note 101 * that the interface name and link addresses will be overwritten, and the passed-in values 102 * discarded. 103 * @param isMetered Whether or not the network should be considered metered. 104 * @param binder A binder object guarding the lifecycle of this test network. 105 * @hide 106 */ setupTestNetwork( @onNull LinkProperties lp, boolean isMetered, @NonNull IBinder binder)107 public void setupTestNetwork( 108 @NonNull LinkProperties lp, boolean isMetered, @NonNull IBinder binder) { 109 Objects.requireNonNull(lp, "Invalid LinkProperties"); 110 setupTestNetwork(lp.getInterfaceName(), lp, isMetered, new int[0], binder); 111 } 112 113 /** 114 * Sets up a capability-limited, testing-only network for a given interface 115 * 116 * @param iface the name of the interface to be used for the Network LinkProperties. 117 * @param binder A binder object guarding the lifecycle of this test network. 118 * @hide 119 */ 120 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 121 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) setupTestNetwork(@onNull String iface, @NonNull IBinder binder)122 public void setupTestNetwork(@NonNull String iface, @NonNull IBinder binder) { 123 setupTestNetwork(iface, null, true, new int[0], binder); 124 } 125 126 /** 127 * Sets up a capability-limited, testing-only network for a given interface with the given 128 * administrator UIDs. 129 * 130 * @param iface the name of the interface to be used for the Network LinkProperties. 131 * @param administratorUids The administrator UIDs to be used for the test-only network 132 * @param binder A binder object guarding the lifecycle of this test network. 133 * @hide 134 */ setupTestNetwork( @onNull String iface, @NonNull int[] administratorUids, @NonNull IBinder binder)135 public void setupTestNetwork( 136 @NonNull String iface, @NonNull int[] administratorUids, @NonNull IBinder binder) { 137 setupTestNetwork(iface, null, true, administratorUids, binder); 138 } 139 140 /** 141 * Create a tun interface for testing purposes 142 * 143 * @param linkAddrs an array of LinkAddresses to assign to the TUN interface 144 * @return A ParcelFileDescriptor of the underlying TUN interface. Close this to tear down the 145 * TUN interface. 146 * @deprecated Use {@link #createTunInterface(Collection)} instead. 147 * @hide 148 */ 149 @Deprecated 150 @NonNull createTunInterface(@onNull LinkAddress[] linkAddrs)151 public TestNetworkInterface createTunInterface(@NonNull LinkAddress[] linkAddrs) { 152 return createTunInterface(Arrays.asList(linkAddrs)); 153 } 154 155 /** 156 * Create a tun interface for testing purposes 157 * 158 * @param linkAddrs an array of LinkAddresses to assign to the TUN interface 159 * @return A ParcelFileDescriptor of the underlying TUN interface. Close this to tear down the 160 * TUN interface. 161 * @hide 162 */ 163 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 164 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 165 @NonNull createTunInterface(@onNull Collection<LinkAddress> linkAddrs)166 public TestNetworkInterface createTunInterface(@NonNull Collection<LinkAddress> linkAddrs) { 167 try { 168 final LinkAddress[] arr = new LinkAddress[linkAddrs.size()]; 169 return mService.createInterface(TUN, BRING_UP, linkAddrs.toArray(arr), 170 null /* iface */); 171 } catch (RemoteException e) { 172 throw e.rethrowFromSystemServer(); 173 } 174 } 175 176 /** 177 * Create a tap interface for testing purposes 178 * 179 * @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the 180 * TAP interface. 181 * @hide 182 */ 183 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 184 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 185 @NonNull createTapInterface()186 public TestNetworkInterface createTapInterface() { 187 try { 188 return mService.createInterface(TAP, BRING_UP, NO_ADDRS, null /* iface */); 189 } catch (RemoteException e) { 190 throw e.rethrowFromSystemServer(); 191 } 192 } 193 194 /** 195 * Create a tap interface for testing purposes 196 * 197 * @param bringUp whether to bring up the interface before returning it. 198 * 199 * @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the 200 * TAP interface. 201 * @hide 202 */ 203 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 204 @NonNull createTapInterface(boolean bringUp)205 public TestNetworkInterface createTapInterface(boolean bringUp) { 206 try { 207 return mService.createInterface(TAP, bringUp, NO_ADDRS, null /* iface */); 208 } catch (RemoteException e) { 209 throw e.rethrowFromSystemServer(); 210 } 211 } 212 213 /** 214 * Create a tap interface with a given interface name for testing purposes 215 * 216 * @param bringUp whether to bring up the interface before returning it. 217 * @param iface interface name to be assigned, so far only interface name which starts with 218 * "v4-testtap" or "v4-testtun" is allowed to be created. If it's null, then use 219 * the default name(e.g. testtap or testtun). 220 * 221 * @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the 222 * TAP interface. 223 * @hide 224 */ 225 @RequiresPermission(Manifest.permission.MANAGE_TEST_NETWORKS) 226 @NonNull createTapInterface(boolean bringUp, @NonNull String iface)227 public TestNetworkInterface createTapInterface(boolean bringUp, @NonNull String iface) { 228 try { 229 return mService.createInterface(TAP, bringUp, NO_ADDRS, iface); 230 } catch (RemoteException e) { 231 throw e.rethrowFromSystemServer(); 232 } 233 } 234 } 235