From e0e3176269f5eb846119429a5583935b36919bf8 Mon Sep 17 00:00:00 2001 From: Akshay Thakker Date: Fri, 26 Jun 2020 20:38:02 +0000 Subject: [PATCH 1/8] Make change and version bump to r_aml_309999900 for mainline module file: packages/Tethering/apex/manifest.json Change-Id: I0a80cda2e4a62ccd87e4fbbe1faa6d6fdb23f014 Exempt-From-Owner-Approval: Version bump only --- Tethering/apex/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tethering/apex/manifest.json b/Tethering/apex/manifest.json index 538ffb3ad6..11e205d1b7 100644 --- a/Tethering/apex/manifest.json +++ b/Tethering/apex/manifest.json @@ -1,4 +1,4 @@ { "name": "com.android.tethering", - "version": 300000000 + "version": 309999900 } From a732b3a5a2bd72c350f2247f0727e1a5318a6e14 Mon Sep 17 00:00:00 2001 From: markchien Date: Wed, 22 Jul 2020 21:28:48 +0800 Subject: [PATCH 2/8] Always stop dhcp server even it is obsolete If dhcp server is obsolete, explicitly stop it to shut down its thread. Bug: 161418295 Test: atest CtsTetheringTest Change-Id: Ic5b876bd23711ec8d832879a7baee0495246b218 Merged-In: Ic5b876bd23711ec8d832879a7baee0495246b218 --- Tethering/src/android/net/ip/IpServer.java | 10 ++-- .../unit/src/android/net/ip/IpServerTest.java | 54 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java index 8af1797a9d..9131317e8d 100644 --- a/Tethering/src/android/net/ip/IpServer.java +++ b/Tethering/src/android/net/ip/IpServer.java @@ -423,9 +423,13 @@ public class IpServer extends StateMachine { getHandler().post(() -> { // We are on the handler thread: mDhcpServerStartIndex can be read safely. if (mStartIndex != mDhcpServerStartIndex) { - // This start request is obsolete. When the |server| binder token goes out of - // scope, the garbage collector will finalize it, which causes the network stack - // process garbage collector to collect the server itself. + // This start request is obsolete. Explicitly stop the DHCP server to shut + // down its thread. When the |server| binder token goes out of scope, the + // garbage collector will finalize it, which causes the network stack process + // garbage collector to collect the server itself. + try { + server.stop(null); + } catch (RemoteException e) { } return; } diff --git a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java index 30a9d2252e..3b72b5b471 100644 --- a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java +++ b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java @@ -50,6 +50,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; @@ -73,6 +74,7 @@ import android.net.MacAddress; import android.net.RouteInfo; import android.net.TetherOffloadRuleParcel; import android.net.TetherStatsParcel; +import android.net.dhcp.DhcpServerCallbacks; import android.net.dhcp.DhcpServingParamsParcel; import android.net.dhcp.IDhcpEventCallbacks; import android.net.dhcp.IDhcpServer; @@ -163,17 +165,6 @@ public class IpServerTest { private void initStateMachine(int interfaceType, boolean usingLegacyDhcp, boolean usingBpfOffload) throws Exception { - doAnswer(inv -> { - final IDhcpServerCallbacks cb = inv.getArgument(2); - new Thread(() -> { - try { - cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); - } catch (RemoteException e) { - fail(e.getMessage()); - } - }).run(); - return null; - }).when(mDependencies).makeDhcpServer(any(), mDhcpParamsCaptor.capture(), any()); when(mDependencies.getRouterAdvertisementDaemon(any())).thenReturn(mRaDaemon); when(mDependencies.getInterfaceParams(IFACE_NAME)).thenReturn(TEST_IFACE_PARAMS); @@ -225,6 +216,20 @@ public class IpServerTest { when(mAddressCoordinator.requestDownstreamAddress(any())).thenReturn(mTestAddress); } + private void setUpDhcpServer() throws Exception { + doAnswer(inv -> { + final IDhcpServerCallbacks cb = inv.getArgument(2); + new Thread(() -> { + try { + cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); + } catch (RemoteException e) { + fail(e.getMessage()); + } + }).run(); + return null; + }).when(mDependencies).makeDhcpServer(any(), mDhcpParamsCaptor.capture(), any()); + } + @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mSharedLog.forSubComponent(anyString())).thenReturn(mSharedLog); @@ -258,6 +263,8 @@ public class IpServerTest { return mTetherConfig; } })); + + setUpDhcpServer(); } @Test @@ -965,6 +972,31 @@ public class IpServerTest { reset(mRaDaemon); } + @Test + public void testStopObsoleteDhcpServer() throws Exception { + final ArgumentCaptor cbCaptor = + ArgumentCaptor.forClass(DhcpServerCallbacks.class); + doNothing().when(mDependencies).makeDhcpServer(any(), mDhcpParamsCaptor.capture(), + cbCaptor.capture()); + initStateMachine(TETHERING_WIFI); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED); + verify(mDhcpServer, never()).startWithCallbacks(any(), any()); + + // No stop dhcp server because dhcp server is not created yet. + dispatchCommand(IpServer.CMD_TETHER_UNREQUESTED); + verify(mDhcpServer, never()).stop(any()); + + // Stop obsolete dhcp server. + try { + final DhcpServerCallbacks cb = cbCaptor.getValue(); + cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); + mLooper.dispatchAll(); + } catch (RemoteException e) { + fail(e.getMessage()); + } + verify(mDhcpServer).stop(any()); + } + private void assertDhcpServingParams(final DhcpServingParamsParcel params, final IpPrefix prefix) { // Last address byte is random From af5885b7d40fdcb45569d49d6d19286a7645d695 Mon Sep 17 00:00:00 2001 From: Treehugger Robot Date: Tue, 9 Jun 2020 17:59:03 +0000 Subject: [PATCH 3/8] Address comment from aosp/1232197 1. Call maybeRemoveDeprecatedUpstreams from Tethering rather than inside PrivateAddressCoordinator because the building logic of this method based on implementation details of Tethering. 2. Fix typo Bug: 130879722 Test: -build, flash, boot -atest TetheringTests Merged-In: I7584253b728bc17fc648fc19e492ca9f7ad2ff46 Change-Id: I7584253b728bc17fc648fc19e492ca9f7ad2ff46 --- .../tethering/PrivateAddressCoordinator.java | 29 ++++++++++--------- .../networkstack/tethering/Tethering.java | 1 + .../PrivateAddressCoordinatorTest.java | 15 ++++++---- .../networkstack/tethering/TetheringTest.java | 6 ++-- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java b/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java index 160a166b63..aa58a4b6a3 100644 --- a/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java @@ -15,6 +15,8 @@ */ package com.android.networkstack.tethering; +import static java.util.Arrays.asList; + import android.content.Context; import android.net.ConnectivityManager; import android.net.IpPrefix; @@ -34,9 +36,10 @@ import com.android.internal.util.IndentingPrintWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Random; +import java.util.Set; /** * This class coordinate IP addresses conflict problem. @@ -60,8 +63,8 @@ public class PrivateAddressCoordinator { // Upstream monitor would be stopped when tethering is down. When tethering restart, downstream // address may be requested before coordinator get current upstream notification. To ensure // coordinator do not select conflict downstream prefix, mUpstreamPrefixMap would not be cleared - // when tethering is down. Instead coordinator would remove all depcreted upstreams from - // mUpstreamPrefixMap when tethering is starting. See #maybeRemoveDeprectedUpstreams(). + // when tethering is down. Instead tethering would remove all deprecated upstreams from + // mUpstreamPrefixMap when tethering is starting. See #maybeRemoveDeprecatedUpstreams(). private final ArrayMap> mUpstreamPrefixMap; private final ArraySet mDownstreams; // IANA has reserved the following three blocks of the IP address space for private intranets: @@ -124,15 +127,16 @@ public class PrivateAddressCoordinator { mUpstreamPrefixMap.remove(network); } - private void maybeRemoveDeprectedUpstreams() { - if (!mDownstreams.isEmpty() || mUpstreamPrefixMap.isEmpty()) return; + /** + * Maybe remove deprecated upstream records, this would be called once tethering started without + * any exiting tethered downstream. + */ + public void maybeRemoveDeprecatedUpstreams() { + if (mUpstreamPrefixMap.isEmpty()) return; - final ArrayList toBeRemoved = new ArrayList<>(); - List allNetworks = Arrays.asList(mConnectivityMgr.getAllNetworks()); - for (int i = 0; i < mUpstreamPrefixMap.size(); i++) { - final Network network = mUpstreamPrefixMap.keyAt(i); - if (!allNetworks.contains(network)) toBeRemoved.add(network); - } + // Remove all upstreams that are no longer valid networks + final Set toBeRemoved = new HashSet<>(mUpstreamPrefixMap.keySet()); + toBeRemoved.removeAll(asList(mConnectivityMgr.getAllNetworks())); mUpstreamPrefixMap.removeAll(toBeRemoved); } @@ -143,8 +147,6 @@ public class PrivateAddressCoordinator { */ @Nullable public LinkAddress requestDownstreamAddress(final IpServer ipServer) { - maybeRemoveDeprectedUpstreams(); - // Address would be 192.168.[subAddress]/24. final byte[] bytes = mTetheringPrefix.getRawAddress(); final int subAddress = getRandomSubAddr(); @@ -237,7 +239,6 @@ public class PrivateAddressCoordinator { } void dump(final IndentingPrintWriter pw) { - pw.decreaseIndent(); pw.println("mUpstreamPrefixMap:"); pw.increaseIndent(); for (int i = 0; i < mUpstreamPrefixMap.size(); i++) { diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index 3695ec65d5..7508a65359 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -1751,6 +1751,7 @@ public class Tethering { return; } + mPrivateAddressCoordinator.maybeRemoveDeprecatedUpstreams(); mUpstreamNetworkMonitor.startObserveAllNetworks(); // TODO: De-duplicate with updateUpstreamWanted() below. diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java index 93efd49a6d..2c0df6fc63 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java @@ -127,10 +127,15 @@ public final class PrivateAddressCoordinatorTest { mPrivateAddressCoordinator.releaseDownstream(mHotspotIpServer); } + private int getBluetoothSubAddress() { + final byte[] rawAddress = mBluetoothPrefix.getRawAddress(); + int bluetoothSubNet = rawAddress[2] & 0xff; + return (bluetoothSubNet << 8) + 0x5; + } + @Test public void testReserveBluetoothPrefix() throws Exception { - final int fakeSubAddr = 0x2c05; - when(mPrivateAddressCoordinator.getRandomSubAddr()).thenReturn(fakeSubAddr); + when(mPrivateAddressCoordinator.getRandomSubAddr()).thenReturn(getBluetoothSubAddress()); LinkAddress address = mPrivateAddressCoordinator.requestDownstreamAddress( mHotspotIpServer); final IpPrefix hotspotPrefix = PrefixUtils.asIpPrefix(address); @@ -146,7 +151,7 @@ public final class PrivateAddressCoordinatorTest { LinkAddress address = mPrivateAddressCoordinator.requestDownstreamAddress( mHotspotIpServer); final IpPrefix hotspotPrefix = PrefixUtils.asIpPrefix(address); - assertEquals("Wrong wifi perfix: ", predefinedPrefix, hotspotPrefix); + assertEquals("Wrong wifi prefix: ", predefinedPrefix, hotspotPrefix); when(mHotspotIpServer.getAddress()).thenReturn(address); address = mPrivateAddressCoordinator.requestDownstreamAddress( @@ -159,7 +164,7 @@ public final class PrivateAddressCoordinatorTest { address = mPrivateAddressCoordinator.requestDownstreamAddress( mUsbIpServer); final IpPrefix allowUseFreePrefix = PrefixUtils.asIpPrefix(address); - assertEquals("Fail to reselect available perfix: ", predefinedPrefix, allowUseFreePrefix); + assertEquals("Fail to reselect available prefix: ", predefinedPrefix, allowUseFreePrefix); } private LinkProperties buildUpstreamLinkProperties(boolean withIPv4, boolean withIPv6, @@ -202,7 +207,7 @@ public final class PrivateAddressCoordinatorTest { final LinkAddress hotspotAddr = mPrivateAddressCoordinator.requestDownstreamAddress( mHotspotIpServer); final IpPrefix hotspotPrefix = PrefixUtils.asIpPrefix(hotspotAddr); - assertEquals("Wrong wifi perfix: ", predefinedPrefix, hotspotPrefix); + assertEquals("Wrong wifi prefix: ", predefinedPrefix, hotspotPrefix); when(mHotspotIpServer.getAddress()).thenReturn(hotspotAddr); // 2. Update v6 only mobile network, hotspot prefix should not be removed. List testConflicts; diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index 64538c7d97..1b710d00d0 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -1897,7 +1897,7 @@ public class TetheringTest { 0, upstreamNetwork); mLooper.dispatchAll(); - // verify trun off usb tethering + // verify turn off usb tethering verify(mUsbManager).setCurrentFunctions(UsbManager.FUNCTION_NONE); mTethering.interfaceRemoved(TEST_USB_IFNAME); mLooper.dispatchAll(); @@ -1935,9 +1935,9 @@ public class TetheringTest { 0, upstreamNetwork); mLooper.dispatchAll(); - // verify trun off usb tethering + // verify turn off usb tethering verify(mUsbManager).setCurrentFunctions(UsbManager.FUNCTION_NONE); - // verify trun off ethernet tethering + // verify turn off ethernet tethering verify(mockRequest).release(); mTethering.interfaceRemoved(TEST_USB_IFNAME); ethCallback.onUnavailable(); From be484d2ca145d39b979ca3ab75819907eb4dd24b Mon Sep 17 00:00:00 2001 From: markchien Date: Mon, 3 Aug 2020 12:01:59 +0800 Subject: [PATCH 4/8] Allow wifi p2p to use legacy dedicated address Some exsting applications may expect wifi p2p use legacy "192.168.49.1/24" address. Have a configuration for wifi p2p to decide whether to use legacy dedicated address or random address. Bug: 161520826 Test: atest TetheringTests Change-Id: If79973416a6780ee19ee785c65772b1a2dc1fbf7 Merged-In: If79973416a6780ee19ee785c65772b1a2dc1fbf7 --- Tethering/res/values/config.xml | 3 ++ Tethering/res/values/overlayable.xml | 1 + .../tethering/PrivateAddressCoordinator.java | 19 ++++++- .../networkstack/tethering/Tethering.java | 5 +- .../tethering/TetheringConfiguration.java | 16 ++++++ .../PrivateAddressCoordinatorTest.java | 53 ++++++++++++++++++- .../tethering/TetheringConfigurationTest.java | 15 ++++++ 7 files changed, 108 insertions(+), 4 deletions(-) diff --git a/Tethering/res/values/config.xml b/Tethering/res/values/config.xml index 9b9dcde910..5f8d299719 100644 --- a/Tethering/res/values/config.xml +++ b/Tethering/res/values/config.xml @@ -73,6 +73,9 @@ false + + false + diff --git a/Tethering/res/values/overlayable.xml b/Tethering/res/values/overlayable.xml index 6a33d55cb0..0ee7a992ee 100644 --- a/Tethering/res/values/overlayable.xml +++ b/Tethering/res/values/overlayable.xml @@ -30,6 +30,7 @@ --> + diff --git a/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java b/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java index aa58a4b6a3..fd9e36080c 100644 --- a/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/PrivateAddressCoordinator.java @@ -15,6 +15,8 @@ */ package com.android.networkstack.tethering; +import static android.net.TetheringManager.TETHERING_WIFI_P2P; + import static java.util.Arrays.asList; import android.content.Context; @@ -58,6 +60,7 @@ public class PrivateAddressCoordinator { private static final int BYTE_MASK = 0xff; // reserved for bluetooth tethering. private static final int BLUETOOTH_RESERVED = 44; + private static final int WIFI_P2P_RESERVED = 49; private static final byte DEFAULT_ID = (byte) 42; // Upstream monitor would be stopped when tethering is down. When tethering restart, downstream @@ -71,15 +74,18 @@ public class PrivateAddressCoordinator { // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 // Tethering use 192.168.0.0/16 that has 256 contiguous class C network numbers. private static final String DEFAULT_TETHERING_PREFIX = "192.168.0.0/16"; + private static final String LEGACY_WIFI_P2P_IFACE_ADDRESS = "192.168.49.1/24"; private final IpPrefix mTetheringPrefix; private final ConnectivityManager mConnectivityMgr; + private final TetheringConfiguration mConfig; - public PrivateAddressCoordinator(Context context) { + public PrivateAddressCoordinator(Context context, TetheringConfiguration config) { mDownstreams = new ArraySet<>(); mUpstreamPrefixMap = new ArrayMap<>(); mTetheringPrefix = new IpPrefix(DEFAULT_TETHERING_PREFIX); mConnectivityMgr = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE); + mConfig = config; } /** @@ -141,12 +147,21 @@ public class PrivateAddressCoordinator { mUpstreamPrefixMap.removeAll(toBeRemoved); } + private boolean isReservedSubnet(final int subnet) { + return subnet == BLUETOOTH_RESERVED || subnet == WIFI_P2P_RESERVED; + } + /** * Pick a random available address and mark its prefix as in use for the provided IpServer, * returns null if there is no available address. */ @Nullable public LinkAddress requestDownstreamAddress(final IpServer ipServer) { + if (mConfig.shouldEnableWifiP2pDedicatedIp() + && ipServer.interfaceType() == TETHERING_WIFI_P2P) { + return new LinkAddress(LEGACY_WIFI_P2P_IFACE_ADDRESS); + } + // Address would be 192.168.[subAddress]/24. final byte[] bytes = mTetheringPrefix.getRawAddress(); final int subAddress = getRandomSubAddr(); @@ -154,7 +169,7 @@ public class PrivateAddressCoordinator { bytes[3] = getSanitizedAddressSuffix(subAddress, (byte) 0, (byte) 1, (byte) 0xff); for (int i = 0; i < MAX_UBYTE; i++) { final int newSubNet = (subNet + i) & BYTE_MASK; - if (newSubNet == BLUETOOTH_RESERVED) continue; + if (isReservedSubnet(newSubNet)) continue; bytes[2] = (byte) newSubNet; final InetAddress addr; diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index 7508a65359..ae6334b37e 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -320,10 +320,13 @@ public class Tethering { mExecutor = new TetheringThreadExecutor(mHandler); mActiveDataSubIdListener = new ActiveDataSubIdListener(mExecutor); mNetdCallback = new NetdCallback(); - mPrivateAddressCoordinator = new PrivateAddressCoordinator(mContext); // Load tethering configuration. updateConfiguration(); + // It is OK for the configuration to be passed to the PrivateAddressCoordinator at + // construction time because the only part of the configuration it uses is + // shouldEnableWifiP2pDedicatedIp(), and currently do not support changing that. + mPrivateAddressCoordinator = new PrivateAddressCoordinator(mContext, mConfig); // Must be initialized after tethering configuration is loaded because BpfCoordinator // constructor needs to use the configuration. diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java b/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java index e1771a5613..5783805861 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java @@ -84,6 +84,9 @@ public class TetheringConfiguration { public static final String TETHER_ENABLE_LEGACY_DHCP_SERVER = "tether_enable_legacy_dhcp_server"; + public static final String USE_LEGACY_WIFI_P2P_DEDICATED_IP = + "use_legacy_wifi_p2p_dedicated_ip"; + /** * Default value that used to periodic polls tether offload stats from tethering offload HAL * to make the data warnings work. @@ -113,6 +116,7 @@ public class TetheringConfiguration { private final int mOffloadPollInterval; // TODO: Add to TetheringConfigurationParcel if required. private final boolean mEnableBpfOffload; + private final boolean mEnableWifiP2pDedicatedIp; public TetheringConfiguration(Context ctx, SharedLog log, int id) { final SharedLog configLog = log.forSubComponent("config"); @@ -156,6 +160,10 @@ public class TetheringConfiguration { R.integer.config_tether_offload_poll_interval, DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS); + mEnableWifiP2pDedicatedIp = getResourceBoolean(res, + R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip, + false /* defaultValue */); + configLog.log(toString()); } @@ -199,6 +207,11 @@ public class TetheringConfiguration { return !TextUtils.isEmpty(provisioningAppNoUi); } + /** Check whether dedicated wifi p2p address is enabled. */ + public boolean shouldEnableWifiP2pDedicatedIp() { + return mEnableWifiP2pDedicatedIp; + } + /** Does the dumping.*/ public void dump(PrintWriter pw) { pw.print("activeDataSubId: "); @@ -233,6 +246,9 @@ public class TetheringConfiguration { pw.print("enableLegacyDhcpServer: "); pw.println(enableLegacyDhcpServer); + + pw.print("enableWifiP2pDedicatedIp: "); + pw.println(mEnableWifiP2pDedicatedIp); } /** Returns the string representation of this object.*/ diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java index 2c0df6fc63..8e93c2e447 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java @@ -15,6 +15,11 @@ */ package com.android.networkstack.tethering; +import static android.net.TetheringManager.TETHERING_ETHERNET; +import static android.net.TetheringManager.TETHERING_USB; +import static android.net.TetheringManager.TETHERING_WIFI; +import static android.net.TetheringManager.TETHERING_WIFI_P2P; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.mockito.Mockito.never; @@ -54,22 +59,34 @@ public final class PrivateAddressCoordinatorTest { @Mock private IpServer mHotspotIpServer; @Mock private IpServer mUsbIpServer; @Mock private IpServer mEthernetIpServer; + @Mock private IpServer mWifiP2pIpServer; @Mock private Context mContext; @Mock private ConnectivityManager mConnectivityMgr; + @Mock private TetheringConfiguration mConfig; private PrivateAddressCoordinator mPrivateAddressCoordinator; private final IpPrefix mBluetoothPrefix = new IpPrefix("192.168.44.0/24"); + private final LinkAddress mLegacyWifiP2pAddress = new LinkAddress("192.168.49.1/24"); private final Network mWifiNetwork = new Network(1); private final Network mMobileNetwork = new Network(2); private final Network[] mAllNetworks = {mMobileNetwork, mWifiNetwork}; + private void setUpIpServers() throws Exception { + when(mUsbIpServer.interfaceType()).thenReturn(TETHERING_USB); + when(mEthernetIpServer.interfaceType()).thenReturn(TETHERING_ETHERNET); + when(mHotspotIpServer.interfaceType()).thenReturn(TETHERING_WIFI); + when(mWifiP2pIpServer.interfaceType()).thenReturn(TETHERING_WIFI_P2P); + } + @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityMgr); when(mConnectivityMgr.getAllNetworks()).thenReturn(mAllNetworks); - mPrivateAddressCoordinator = spy(new PrivateAddressCoordinator(mContext)); + when(mConfig.shouldEnableWifiP2pDedicatedIp()).thenReturn(false); + setUpIpServers(); + mPrivateAddressCoordinator = spy(new PrivateAddressCoordinator(mContext, mConfig)); } @Test @@ -256,4 +273,38 @@ public final class PrivateAddressCoordinatorTest { final IpPrefix ethPrefix = PrefixUtils.asIpPrefix(ethAddr); assertEquals(predefinedPrefix, ethPrefix); } + + private int getSubAddress(final byte... ipv4Address) { + assertEquals(4, ipv4Address.length); + + int subnet = Byte.toUnsignedInt(ipv4Address[2]); + return (subnet << 8) + ipv4Address[3]; + } + + private void assertReseveredWifiP2pPrefix() throws Exception { + LinkAddress address = mPrivateAddressCoordinator.requestDownstreamAddress( + mHotspotIpServer); + final IpPrefix hotspotPrefix = PrefixUtils.asIpPrefix(address); + final IpPrefix legacyWifiP2pPrefix = PrefixUtils.asIpPrefix(mLegacyWifiP2pAddress); + assertNotEquals(legacyWifiP2pPrefix, hotspotPrefix); + mPrivateAddressCoordinator.releaseDownstream(mHotspotIpServer); + } + + @Test + public void testEnableLegacyWifiP2PAddress() throws Exception { + when(mPrivateAddressCoordinator.getRandomSubAddr()).thenReturn( + getSubAddress(mLegacyWifiP2pAddress.getAddress().getAddress())); + // No matter #shouldEnableWifiP2pDedicatedIp() is enabled or not, legacy wifi p2p prefix + // is resevered. + assertReseveredWifiP2pPrefix(); + + when(mConfig.shouldEnableWifiP2pDedicatedIp()).thenReturn(true); + assertReseveredWifiP2pPrefix(); + + // If #shouldEnableWifiP2pDedicatedIp() is enabled, wifi P2P gets the configured address. + LinkAddress address = mPrivateAddressCoordinator.requestDownstreamAddress( + mWifiP2pIpServer); + assertEquals(mLegacyWifiP2pAddress, address); + mPrivateAddressCoordinator.releaseDownstream(mWifiP2pIpServer); + } } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java index a9ac4e2851..dc0940cc02 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java @@ -128,6 +128,8 @@ public class TetheringConfigurationTest { .thenReturn(new String[0]); when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( false); + when(mResources.getBoolean(R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip)) + .thenReturn(false); initializeBpfOffloadConfiguration(true, null /* unset */); mHasTelephonyManager = true; @@ -413,4 +415,17 @@ public class TetheringConfigurationTest { R.string.config_mobile_hotspot_provision_response)).thenReturn( PROVISIONING_APP_RESPONSE); } + + @Test + public void testEnableLegacyWifiP2PAddress() throws Exception { + final TetheringConfiguration defaultCfg = new TetheringConfiguration( + mMockContext, mLog, INVALID_SUBSCRIPTION_ID); + assertFalse(defaultCfg.shouldEnableWifiP2pDedicatedIp()); + + when(mResources.getBoolean(R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip)) + .thenReturn(true); + final TetheringConfiguration testCfg = new TetheringConfiguration( + mMockContext, mLog, INVALID_SUBSCRIPTION_ID); + assertTrue(testCfg.shouldEnableWifiP2pDedicatedIp()); + } } From 10235e5c7969050beab9418d5abdb0db8294ebe8 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Wed, 29 Jul 2020 12:05:04 +0800 Subject: [PATCH 5/8] Update language to comply with Android's inclusive language guidance See https://source.android.com/setup/contribute/respectful-code for reference. Test: m ; atest TetheringTests Bug: 161896447 Change-Id: Idc58697c72fb00896bee00185fefc50c1a24dd35 Merged-In: Idc58697c72fb00896bee00185fefc50c1a24dd35 --- Tethering/AndroidManifest.xml | 2 +- Tethering/proguard.flags | 2 +- Tethering/src/android/net/ip/IpServer.java | 16 ++-- .../net/util/TetheringMessageBase.java | 2 +- .../tethering/EntitlementManager.java | 6 +- .../networkstack/tethering/Tethering.java | 80 +++++++++---------- .../tethering/UpstreamNetworkMonitor.java | 2 +- .../networkstack/tethering/TetheringTest.java | 28 +++---- 8 files changed, 71 insertions(+), 67 deletions(-) diff --git a/Tethering/AndroidManifest.xml b/Tethering/AndroidManifest.xml index 2b2fe4534c..e6444f3ead 100644 --- a/Tethering/AndroidManifest.xml +++ b/Tethering/AndroidManifest.xml @@ -24,7 +24,7 @@ + added to the privileged permissions allowlist for that package. --> diff --git a/Tethering/proguard.flags b/Tethering/proguard.flags index 051fbd19fc..86b903353c 100644 --- a/Tethering/proguard.flags +++ b/Tethering/proguard.flags @@ -1,5 +1,5 @@ # Keep class's integer static field for MessageUtils to parsing their name. --keep class com.android.networkstack.tethering.Tethering$TetherMasterSM { +-keep class com.android.networkstack.tethering.Tethering$TetherMainSM { static final int CMD_*; static final int EVENT_*; } diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java index 9131317e8d..673cbf09d2 100644 --- a/Tethering/src/android/net/ip/IpServer.java +++ b/Tethering/src/android/net/ip/IpServer.java @@ -197,15 +197,19 @@ public class IpServer extends StateMachine { public static final int CMD_TETHER_UNREQUESTED = BASE_IPSERVER + 2; // notification that this interface is down public static final int CMD_INTERFACE_DOWN = BASE_IPSERVER + 3; - // notification from the master SM that it had trouble enabling IP Forwarding + // notification from the {@link Tethering.TetherMainSM} that it had trouble enabling IP + // Forwarding public static final int CMD_IP_FORWARDING_ENABLE_ERROR = BASE_IPSERVER + 4; - // notification from the master SM that it had trouble disabling IP Forwarding + // notification from the {@link Tethering.TetherMainSM} SM that it had trouble disabling IP + // Forwarding public static final int CMD_IP_FORWARDING_DISABLE_ERROR = BASE_IPSERVER + 5; - // notification from the master SM that it had trouble starting tethering + // notification from the {@link Tethering.TetherMainSM} SM that it had trouble starting + // tethering public static final int CMD_START_TETHERING_ERROR = BASE_IPSERVER + 6; - // notification from the master SM that it had trouble stopping tethering + // notification from the {@link Tethering.TetherMainSM} that it had trouble stopping tethering public static final int CMD_STOP_TETHERING_ERROR = BASE_IPSERVER + 7; - // notification from the master SM that it had trouble setting the DNS forwarders + // notification from the {@link Tethering.TetherMainSM} that it had trouble setting the DNS + // forwarders public static final int CMD_SET_DNS_FORWARDERS_ERROR = BASE_IPSERVER + 8; // the upstream connection has changed public static final int CMD_TETHER_CONNECTION_CHANGED = BASE_IPSERVER + 9; @@ -1320,7 +1324,7 @@ public class IpServer extends StateMachine { /** * This state is terminal for the per interface state machine. At this - * point, the master state machine should have removed this interface + * point, the tethering main state machine should have removed this interface * specific state machine from its list of possible recipients of * tethering requests. The state machine itself will hang around until * the garbage collector finds it. diff --git a/Tethering/src/android/net/util/TetheringMessageBase.java b/Tethering/src/android/net/util/TetheringMessageBase.java index 1b763ce920..29c0a817b6 100644 --- a/Tethering/src/android/net/util/TetheringMessageBase.java +++ b/Tethering/src/android/net/util/TetheringMessageBase.java @@ -19,7 +19,7 @@ package android.net.util; * This class defines Message.what base addresses for various state machine. */ public class TetheringMessageBase { - public static final int BASE_MASTER = 0; + public static final int BASE_MAIN_SM = 0; public static final int BASE_IPSERVER = 100; } diff --git a/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java b/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java index 9dace709d7..bb7322f2a0 100644 --- a/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java +++ b/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java @@ -296,16 +296,16 @@ public class EntitlementManager { * Reference TetheringManager.TETHERING_{@code *} for each tether type. * * @param config an object that encapsulates the various tethering configuration elements. - * Note: this method is only called from TetherMaster on the handler thread. + * Note: this method is only called from @{link Tethering.TetherMainSM} on the handler thread. * If there are new callers from different threads, the logic should move to - * masterHandler to avoid race conditions. + * @{link Tethering.TetherMainSM} handler to avoid race conditions. */ public void reevaluateSimCardProvisioning(final TetheringConfiguration config) { if (DBG) mLog.i("reevaluateSimCardProvisioning"); if (!mHandler.getLooper().isCurrentThread()) { // Except for test, this log should not appear in normal flow. - mLog.log("reevaluateSimCardProvisioning() don't run in TetherMaster thread"); + mLog.log("reevaluateSimCardProvisioning() don't run in TetherMainSM thread"); } mEntitlementCacheValue.clear(); mCurrentEntitlementResults.clear(); diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index ae6334b37e..7dd5290ee8 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -50,7 +50,7 @@ import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_TYPE; import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_FAILED; import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_STARTED; import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_STOPPED; -import static android.net.util.TetheringMessageBase.BASE_MASTER; +import static android.net.util.TetheringMessageBase.BASE_MAIN_SM; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; @@ -159,7 +159,7 @@ public class Tethering { private static final boolean VDBG = false; private static final Class[] sMessageClasses = { - Tethering.class, TetherMasterSM.class, IpServer.class + Tethering.class, TetherMainSM.class, IpServer.class }; private static final SparseArray sMagicDecoderRing = MessageUtils.findMessageNames(sMessageClasses); @@ -216,7 +216,7 @@ public class Tethering { private final ArrayMap mTetherStates; private final BroadcastReceiver mStateReceiver; private final Looper mLooper; - private final StateMachine mTetherMasterSM; + private final StateMachine mTetherMainSM; private final OffloadController mOffloadController; private final UpstreamNetworkMonitor mUpstreamNetworkMonitor; // TODO: Figure out how to merge this and other downstream-tracking objects @@ -273,10 +273,10 @@ public class Tethering { mTetherStates = new ArrayMap<>(); mConnectedClientsTracker = new ConnectedClientsTracker(); - mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper, deps); - mTetherMasterSM.start(); + mTetherMainSM = new TetherMainSM("TetherMain", mLooper, deps); + mTetherMainSM.start(); - mHandler = mTetherMasterSM.getHandler(); + mHandler = mTetherMainSM.getHandler(); mOffloadController = mDeps.getOffloadController(mHandler, mLog, new OffloadController.Dependencies() { @@ -285,8 +285,8 @@ public class Tethering { return mConfig; } }); - mUpstreamNetworkMonitor = mDeps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog, - TetherMasterSM.EVENT_UPSTREAM_CALLBACK); + mUpstreamNetworkMonitor = mDeps.getUpstreamNetworkMonitor(mContext, mTetherMainSM, mLog, + TetherMainSM.EVENT_UPSTREAM_CALLBACK); mForwardedDownstreams = new LinkedHashSet<>(); IntentFilter filter = new IntentFilter(); @@ -294,8 +294,8 @@ public class Tethering { // EntitlementManager will send EVENT_UPSTREAM_PERMISSION_CHANGED when cellular upstream // permission is changed according to entitlement check result. mEntitlementMgr = mDeps.getEntitlementManager(mContext, mHandler, mLog, - () -> mTetherMasterSM.sendMessage( - TetherMasterSM.EVENT_UPSTREAM_PERMISSION_CHANGED)); + () -> mTetherMainSM.sendMessage( + TetherMainSM.EVENT_UPSTREAM_PERMISSION_CHANGED)); mEntitlementMgr.setOnUiEntitlementFailedListener((int downstream) -> { mLog.log("OBSERVED UiEnitlementFailed"); stopTethering(downstream); @@ -948,7 +948,7 @@ public class Tethering { } if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString()); - mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); + mTetherMainSM.sendMessage(TetherMainSM.CMD_UPSTREAM_CHANGED); } private void handleUsbAction(Intent intent) { @@ -1173,7 +1173,7 @@ public class Tethering { private void disableWifiP2pIpServingLockedIfNeeded(String ifname) { if (TextUtils.isEmpty(ifname)) return; - disableWifiIpServingLockedCommon(TETHERING_WIFI_P2P, ifname, /* dummy */ 0); + disableWifiIpServingLockedCommon(TETHERING_WIFI_P2P, ifname, /* fake */ 0); } private void enableWifiIpServingLocked(String ifname, int wifiIpMode) { @@ -1384,23 +1384,23 @@ public class Tethering { return false; } - class TetherMasterSM extends StateMachine { + class TetherMainSM extends StateMachine { // an interface SM has requested Tethering/Local Hotspot - static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1; + static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MAIN_SM + 1; // an interface SM has unrequested Tethering/Local Hotspot - static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2; + static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MAIN_SM + 2; // upstream connection change - do the right thing - static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3; + static final int CMD_UPSTREAM_CHANGED = BASE_MAIN_SM + 3; // we don't have a valid upstream conn, check again after a delay - static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4; - // Events from NetworkCallbacks that we process on the master state + static final int CMD_RETRY_UPSTREAM = BASE_MAIN_SM + 4; + // Events from NetworkCallbacks that we process on the main state // machine thread on behalf of the UpstreamNetworkMonitor. - static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5; + static final int EVENT_UPSTREAM_CALLBACK = BASE_MAIN_SM + 5; // we treated the error and want now to clear it - static final int CMD_CLEAR_ERROR = BASE_MASTER + 6; - static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7; + static final int CMD_CLEAR_ERROR = BASE_MAIN_SM + 6; + static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MAIN_SM + 7; // Events from EntitlementManager to choose upstream again. - static final int EVENT_UPSTREAM_PERMISSION_CHANGED = BASE_MASTER + 8; + static final int EVENT_UPSTREAM_PERMISSION_CHANGED = BASE_MAIN_SM + 8; private final State mInitialState; private final State mTetherModeAliveState; @@ -1428,7 +1428,7 @@ public class Tethering { private static final int UPSTREAM_SETTLE_TIME_MS = 10000; - TetherMasterSM(String name, Looper looper, TetheringDependencies deps) { + TetherMainSM(String name, Looper looper, TetheringDependencies deps) { super(name, looper); mInitialState = new InitialState(); @@ -1482,7 +1482,7 @@ public class Tethering { } } - protected boolean turnOnMasterTetherSettings() { + protected boolean turnOnMainTetherSettings() { final TetheringConfiguration cfg = mConfig; try { mNetd.ipfwdEnableForwarding(TAG); @@ -1509,11 +1509,11 @@ public class Tethering { return false; } } - mLog.log("SET master tether settings: ON"); + mLog.log("SET main tether settings: ON"); return true; } - protected boolean turnOffMasterTetherSettings() { + protected boolean turnOffMainTetherSettings() { try { mNetd.tetherStop(); } catch (RemoteException | ServiceSpecificException e) { @@ -1529,7 +1529,7 @@ public class Tethering { return false; } transitionTo(mInitialState); - mLog.log("SET master tether settings: OFF"); + mLog.log("SET main tether settings: OFF"); return true; } @@ -1733,7 +1733,7 @@ public class Tethering { // TODO: Re-evaluate possible upstreams. Currently upstream // reevaluation is triggered via received CONNECTIVITY_ACTION // broadcasts that result in being passed a - // TetherMasterSM.CMD_UPSTREAM_CHANGED. + // TetherMainSM.CMD_UPSTREAM_CHANGED. handleNewUpstreamNetworkState(null); break; default: @@ -1748,9 +1748,9 @@ public class Tethering { @Override public void enter() { - // If turning on master tether settings fails, we have already + // If turning on main tether settings fails, we have already // transitioned to an error state; exit early. - if (!turnOnMasterTetherSettings()) { + if (!turnOnMainTetherSettings()) { return; } @@ -1822,7 +1822,7 @@ public class Tethering { if (mNotifyList.isEmpty()) { // This transitions us out of TetherModeAliveState, // either to InitialState or an error state. - turnOffMasterTetherSettings(); + turnOffMainTetherSettings(); break; } @@ -2332,7 +2332,7 @@ public class Tethering { }; } - // TODO: Move into TetherMasterSM. + // TODO: Move into TetherMainSM. private void notifyInterfaceStateChange(IpServer who, int state, int error) { final String iface = who.interfaceName(); synchronized (mPublicSync) { @@ -2347,27 +2347,27 @@ public class Tethering { mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error)); - // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. - // Thus we give a chance for TetherMasterSM to recover to InitialState + // If TetherMainSM is in ErrorState, TetherMainSM stays there. + // Thus we give a chance for TetherMainSM to recover to InitialState // by sending CMD_CLEAR_ERROR if (error == TETHER_ERROR_INTERNAL_ERROR) { - mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); + mTetherMainSM.sendMessage(TetherMainSM.CMD_CLEAR_ERROR, who); } int which; switch (state) { case IpServer.STATE_UNAVAILABLE: case IpServer.STATE_AVAILABLE: - which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE; + which = TetherMainSM.EVENT_IFACE_SERVING_STATE_INACTIVE; break; case IpServer.STATE_TETHERED: case IpServer.STATE_LOCAL_ONLY: - which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE; + which = TetherMainSM.EVENT_IFACE_SERVING_STATE_ACTIVE; break; default: Log.wtf(TAG, "Unknown interface state: " + state); return; } - mTetherMasterSM.sendMessage(which, state, 0, who); + mTetherMainSM.sendMessage(which, state, 0, who); sendTetherStateChangedBroadcast(); } @@ -2387,8 +2387,8 @@ public class Tethering { mLog.log(String.format( "OBSERVED LinkProperties update iface=%s state=%s lp=%s", iface, IpServer.getStateString(state), newLp)); - final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; - mTetherMasterSM.sendMessage(which, state, 0, newLp); + final int which = TetherMainSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; + mTetherMainSM.sendMessage(which, state, 0, newLp); } private void maybeTrackNewInterfaceLocked(final String iface) { diff --git a/Tethering/src/com/android/networkstack/tethering/UpstreamNetworkMonitor.java b/Tethering/src/com/android/networkstack/tethering/UpstreamNetworkMonitor.java index 320427c393..b17065cb78 100644 --- a/Tethering/src/com/android/networkstack/tethering/UpstreamNetworkMonitor.java +++ b/Tethering/src/com/android/networkstack/tethering/UpstreamNetworkMonitor.java @@ -63,7 +63,7 @@ import java.util.Set; * Calling #registerMobileNetworkRequest() to bring up mobile DUN/HIPRI network. * * The methods and data members of this class are only to be accessed and - * modified from the tethering master state machine thread. Any other + * modified from the tethering main state machine thread. Any other * access semantics would necessitate the addition of locking. * * TODO: Move upstream selection logic here. diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index 1b710d00d0..46fe5cf093 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -337,11 +337,11 @@ public class TetheringTest { } public class MockTetheringDependencies extends TetheringDependencies { - StateMachine mUpstreamNetworkMonitorMasterSM; + StateMachine mUpstreamNetworkMonitorSM; ArrayList mIpv6CoordinatorNotifyList; public void reset() { - mUpstreamNetworkMonitorMasterSM = null; + mUpstreamNetworkMonitorSM = null; mIpv6CoordinatorNotifyList = null; } @@ -368,7 +368,7 @@ public class TetheringTest { @Override public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, StateMachine target, SharedLog log, int what) { - mUpstreamNetworkMonitorMasterSM = target; + mUpstreamNetworkMonitorSM = target; return mUpstreamNetworkMonitor; } @@ -911,8 +911,8 @@ public class TetheringTest { initTetheringUpstream(upstreamState); // Upstream LinkProperties changed: UpstreamNetworkMonitor sends EVENT_ON_LINKPROPERTIES. - mTetheringDependencies.mUpstreamNetworkMonitorMasterSM.sendMessage( - Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, + mTetheringDependencies.mUpstreamNetworkMonitorSM.sendMessage( + Tethering.TetherMainSM.EVENT_UPSTREAM_CALLBACK, UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, 0, upstreamState); @@ -1126,7 +1126,7 @@ public class TetheringTest { verify(mNetd, times(1)).ipfwdEnableForwarding(TETHERING_NAME); // This never gets called because of the exception thrown above. verify(mNetd, times(0)).tetherStartWithConfiguration(any()); - // When the master state machine transitions to an error state it tells + // When the main state machine transitions to an error state it tells // downstream interfaces, which causes us to tell Wi-Fi about the error // so it can take down AP mode. verify(mNetd, times(1)).tetherApplyDnsInterfaces(); @@ -1753,8 +1753,8 @@ public class TetheringTest { @Test public void testUpstreamNetworkChanged() { - final Tethering.TetherMasterSM stateMachine = (Tethering.TetherMasterSM) - mTetheringDependencies.mUpstreamNetworkMonitorMasterSM; + final Tethering.TetherMainSM stateMachine = (Tethering.TetherMainSM) + mTetheringDependencies.mUpstreamNetworkMonitorSM; final UpstreamNetworkState upstreamState = buildMobileIPv4UpstreamState(); initTetheringUpstream(upstreamState); stateMachine.chooseUpstreamType(true); @@ -1765,8 +1765,8 @@ public class TetheringTest { @Test public void testUpstreamCapabilitiesChanged() { - final Tethering.TetherMasterSM stateMachine = (Tethering.TetherMasterSM) - mTetheringDependencies.mUpstreamNetworkMonitorMasterSM; + final Tethering.TetherMainSM stateMachine = (Tethering.TetherMainSM) + mTetheringDependencies.mUpstreamNetworkMonitorSM; final UpstreamNetworkState upstreamState = buildMobileIPv4UpstreamState(); initTetheringUpstream(upstreamState); stateMachine.chooseUpstreamType(true); @@ -1891,8 +1891,8 @@ public class TetheringTest { any(), any()); reset(mNetd, mUsbManager); upstreamNetwork = buildV4WifiUpstreamState(ipv4Address, 30, wifiNetwork); - mTetheringDependencies.mUpstreamNetworkMonitorMasterSM.sendMessage( - Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, + mTetheringDependencies.mUpstreamNetworkMonitorSM.sendMessage( + Tethering.TetherMainSM.EVENT_UPSTREAM_CALLBACK, UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, 0, upstreamNetwork); @@ -1929,8 +1929,8 @@ public class TetheringTest { final UpstreamNetworkState upstreamNetwork = buildV4WifiUpstreamState( upstreamAddress, 16, wifiNetwork); - mTetheringDependencies.mUpstreamNetworkMonitorMasterSM.sendMessage( - Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, + mTetheringDependencies.mUpstreamNetworkMonitorSM.sendMessage( + Tethering.TetherMainSM.EVENT_UPSTREAM_CALLBACK, UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, 0, upstreamNetwork); From d7b755f92e93cfc23c28d5354ada2b12cf08db5b Mon Sep 17 00:00:00 2001 From: Mark Chien Date: Tue, 18 Aug 2020 09:28:57 +0000 Subject: [PATCH 6/8] Add usesCleartextTraffic to Tethering usesCleartextTraffic needs to be true for the networkstack process so that the NetworkStack module can use no-encrypted probes to detect captive portals. When loaded in the networkstack process, all packages in process must set usesCleartextTraffic=true, otherwise there may be races causing the flag not to be set for the process. Bug: 161860610 Test: CtsTetheringTest, TetheringTests Change-Id: If1ea472e2b7e715ab97851394dc8980ad269b7a1 Merged-In: Ife03ee0c7096ea242eb701b297a69b471e15b436 (cherry picked from commit 36fd800cb156c676642f3ab707464973994c1cb3) --- Tethering/AndroidManifestBase.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tethering/AndroidManifestBase.xml b/Tethering/AndroidManifestBase.xml index fa85f66489..97c3988829 100644 --- a/Tethering/AndroidManifestBase.xml +++ b/Tethering/AndroidManifestBase.xml @@ -23,6 +23,7 @@ + android:directBootAware="true" + android:usesCleartextTraffic="true"> From 63e74e3131bb943c68bd01c831ca8ef8d78fa7ad Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Fri, 26 Jun 2020 00:19:33 +0900 Subject: [PATCH 7/8] Move utils from network stack to libs. This package is using some common utilities from a library that used to live in the network stack. A better home for these utilities is frameworks/libs, so this topic moves the files ther and also changes the package of some utilities. See aosp/1350222 and aosp/1350182 for a detailed description of the specific files that moved. Test: checkbuild Original-change: aosp/1350083 Merged-In: I76a9b7790f3997e3e6b3c2f75ba6308286457cde Change-Id: I76a9b7790f3997e3e6b3c2f75ba6308286457cde --- Tethering/Android.bp | 1 + 1 file changed, 1 insertion(+) diff --git a/Tethering/Android.bp b/Tethering/Android.bp index 12daa6142d..bb92bee6d5 100644 --- a/Tethering/Android.bp +++ b/Tethering/Android.bp @@ -31,6 +31,7 @@ java_defaults { "android.hardware.tetheroffload.config-V1.0-java", "android.hardware.tetheroffload.control-V1.0-java", "net-utils-framework-common", + "net-utils-device-common", ], libs: [ "framework-statsd.stubs.module_lib", From 1433252635bd81d1088b67ab6c775322baf26caf Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Fri, 26 Jun 2020 00:41:26 +0900 Subject: [PATCH 8/8] Rename Kotlin util files to not include the Kt suffix Callers don't care what language the utilities are written in This is a partial cherry-pick of the change in the packages/Tethering, tests/net/common, tests/net/integration, wifi/tests directories. Other tests cannot be kept in sync as the latest versions verify platform functionalities that do not exist in the module branch, so disabling them is less time-consuming than always resolving merge conflicts. Test: builds Merged-In: Ie212144f36c50db223c05f3fcb6bad745842cb5e Change-Id: Ie212144f36c50db223c05f3fcb6bad745842cb5e --- .../integration/src/android/net/EthernetTetheringTest.java | 4 ++-- .../tests/unit/src/android/net/util/TetheringUtilsTest.java | 4 ++-- .../android/networkstack/tethering/OffloadControllerTest.java | 4 ++-- .../src/com/android/networkstack/tethering/TetheringTest.java | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java index 9bb01ae5df..64be2d9a55 100644 --- a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java +++ b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java @@ -50,7 +50,7 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; -import com.android.testutils.HandlerUtilsKt; +import com.android.testutils.HandlerUtils; import com.android.testutils.TapPacketReader; import org.junit.After; @@ -366,7 +366,7 @@ public class EthernetTetheringTest { private TapPacketReader makePacketReader(FileDescriptor fd, int mtu) { final TapPacketReader reader = new TapPacketReader(mHandler, fd, mtu); mHandler.post(() -> reader.start()); - HandlerUtilsKt.waitForIdle(mHandler, TIMEOUT_MS); + HandlerUtils.waitForIdle(mHandler, TIMEOUT_MS); return reader; } diff --git a/Tethering/tests/unit/src/android/net/util/TetheringUtilsTest.java b/Tethering/tests/unit/src/android/net/util/TetheringUtilsTest.java index 1499f3be22..91c7771cc7 100644 --- a/Tethering/tests/unit/src/android/net/util/TetheringUtilsTest.java +++ b/Tethering/tests/unit/src/android/net/util/TetheringUtilsTest.java @@ -27,7 +27,7 @@ import android.net.TetheringRequestParcel; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; -import com.android.testutils.MiscAssertsKt; +import com.android.testutils.MiscAsserts; import org.junit.Before; import org.junit.Test; @@ -82,6 +82,6 @@ public class TetheringUtilsTest { request.showProvisioningUi = false; assertFalse(TetheringUtils.isTetheringRequestEquals(mTetheringRequest, request)); - MiscAssertsKt.assertFieldCountEquals(5, TetheringRequestParcel.class); + MiscAsserts.assertFieldCountEquals(5, TetheringRequestParcel.class); } } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/OffloadControllerTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/OffloadControllerTest.java index b291438937..ce52ae22ec 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/OffloadControllerTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/OffloadControllerTest.java @@ -30,8 +30,8 @@ import static com.android.networkstack.tethering.OffloadController.StatsType.STA import static com.android.networkstack.tethering.OffloadController.StatsType.STATS_PER_UID; import static com.android.networkstack.tethering.OffloadHardwareInterface.ForwardedStats; import static com.android.networkstack.tethering.TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS; -import static com.android.testutils.MiscAssertsKt.assertContainsAll; -import static com.android.testutils.MiscAssertsKt.assertThrows; +import static com.android.testutils.MiscAsserts.assertContainsAll; +import static com.android.testutils.MiscAsserts.assertThrows; import static com.android.testutils.NetworkStatsUtilsKt.assertNetworkStatsEquals; import static junit.framework.Assert.assertNotNull; diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index 46fe5cf093..1fe3840b51 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -143,7 +143,7 @@ import com.android.internal.util.ArrayUtils; import com.android.internal.util.StateMachine; import com.android.internal.util.test.BroadcastInterceptingContext; import com.android.internal.util.test.FakeSettingsProvider; -import com.android.testutils.MiscAssertsKt; +import com.android.testutils.MiscAsserts; import org.junit.After; import org.junit.AfterClass; @@ -1360,7 +1360,7 @@ public class TetheringTest { assertEquals(0, parcel.localOnlyList.length); assertEquals(0, parcel.erroredIfaceList.length); assertEquals(0, parcel.lastErrorList.length); - MiscAssertsKt.assertFieldCountEquals(5, TetherStatesParcel.class); + MiscAsserts.assertFieldCountEquals(5, TetherStatesParcel.class); } @Test