diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java index d6f4572852..fa6ce95442 100644 --- a/Tethering/src/android/net/ip/IpServer.java +++ b/Tethering/src/android/net/ip/IpServer.java @@ -16,6 +16,7 @@ package android.net.ip; +import static android.net.INetd.LOCAL_NET_ID; import static android.net.RouteInfo.RTN_UNICAST; import static android.net.TetheringManager.CONNECTIVITY_SCOPE_GLOBAL; import static android.net.TetheringManager.CONNECTIVITY_SCOPE_LOCAL; @@ -906,7 +907,7 @@ public class IpServer extends StateMachineShim { ArraySet deprecatedPrefixes, ArraySet newPrefixes) { // [1] Remove the routes that are deprecated. if (!deprecatedPrefixes.isEmpty()) { - removeRoutesFromNetworkAndLinkProperties(INetd.LOCAL_NET_ID, + removeRoutesFromNetworkAndLinkProperties(LOCAL_NET_ID, getLocalRoutesFor(mIfaceName, deprecatedPrefixes)); } @@ -918,7 +919,7 @@ public class IpServer extends StateMachineShim { } if (!addedPrefixes.isEmpty()) { - addRoutesToNetworkAndLinkProperties(INetd.LOCAL_NET_ID, + addRoutesToNetworkAndLinkProperties(LOCAL_NET_ID, getLocalRoutesFor(mIfaceName, addedPrefixes)); } } @@ -1123,7 +1124,7 @@ public class IpServer extends StateMachineShim { } try { - NetdUtils.tetherInterface(mNetd, INetd.LOCAL_NET_ID, mIfaceName, + NetdUtils.tetherInterface(mNetd, LOCAL_NET_ID, mIfaceName, asIpPrefix(mIpv4Address)); } catch (RemoteException | ServiceSpecificException | IllegalStateException e) { mLog.e("Error Tethering", e); @@ -1146,7 +1147,7 @@ public class IpServer extends StateMachineShim { stopIPv6(); try { - NetdUtils.untetherInterface(mNetd, mIfaceName); + NetdUtils.untetherInterface(mNetd, LOCAL_NET_ID, mIfaceName); } catch (RemoteException | ServiceSpecificException e) { mLastError = TETHER_ERROR_UNTETHER_IFACE_ERROR; mLog.e("Failed to untether interface: " + e); @@ -1224,12 +1225,12 @@ public class IpServer extends StateMachineShim { } // Remove deprecated routes from downstream network. - removeRoutesFromNetworkAndLinkProperties(INetd.LOCAL_NET_ID, + removeRoutesFromNetworkAndLinkProperties(LOCAL_NET_ID, List.of(getDirectConnectedRoute(deprecatedLinkAddress))); mLinkProperties.removeLinkAddress(deprecatedLinkAddress); // Add new routes to downstream network. - addRoutesToNetworkAndLinkProperties(INetd.LOCAL_NET_ID, + addRoutesToNetworkAndLinkProperties(LOCAL_NET_ID, List.of(getDirectConnectedRoute(mIpv4Address))); mLinkProperties.addLinkAddress(mIpv4Address); diff --git a/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java b/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java index 553a24bc5d..8b2fe58d56 100644 --- a/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java +++ b/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java @@ -161,9 +161,9 @@ public class NetdUtils { netd.tetherInterfaceAdd(iface); networkAddInterface(netd, netId, iface, maxAttempts, pollingIntervalMs); // Activate a route to dest and IPv6 link local. - modifyRoute(netd, ModifyOperation.ADD, INetd.LOCAL_NET_ID, + modifyRoute(netd, ModifyOperation.ADD, netId, new RouteInfo(dest, null, iface, RTN_UNICAST)); - modifyRoute(netd, ModifyOperation.ADD, INetd.LOCAL_NET_ID, + modifyRoute(netd, ModifyOperation.ADD, netId, new RouteInfo(new IpPrefix("fe80::/64"), null, iface, RTN_UNICAST)); } @@ -194,12 +194,12 @@ public class NetdUtils { } /** Reset interface for tethering. */ - public static void untetherInterface(final INetd netd, String iface) + public static void untetherInterface(final INetd netd, int netId, String iface) throws RemoteException, ServiceSpecificException { try { netd.tetherInterfaceRemove(iface); } finally { - netd.networkRemoveInterface(INetd.LOCAL_NET_ID, iface); + netd.networkRemoveInterface(netId, iface); } } diff --git a/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java b/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java index ab90a507e3..c2fbb561e2 100644 --- a/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java +++ b/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java @@ -16,7 +16,6 @@ package com.android.net.module.util; -import static android.net.INetd.LOCAL_NET_ID; import static android.system.OsConstants.EBUSY; import static com.android.testutils.MiscAsserts.assertThrows; @@ -63,6 +62,7 @@ public class NetdUtilsTest { private static final String IFACE = "TEST_IFACE"; private static final IpPrefix TEST_IPPREFIX = new IpPrefix("192.168.42.1/24"); + private static final int TEST_NET_ID = 123; @Before public void setUp() throws Exception { @@ -134,7 +134,7 @@ public class NetdUtilsTest { } throw new ServiceSpecificException(EBUSY); - }).when(mNetd).networkAddInterface(LOCAL_NET_ID, IFACE); + }).when(mNetd).networkAddInterface(TEST_NET_ID, IFACE); } class Counter { @@ -163,7 +163,7 @@ public class NetdUtilsTest { setNetworkAddInterfaceOutcome(new ServiceSpecificException(expectedCode), expectedTries); try { - NetdUtils.tetherInterface(mNetd, LOCAL_NET_ID, IFACE, TEST_IPPREFIX, 20, 0); + NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE, TEST_IPPREFIX, 20, 0); fail("Expect throw ServiceSpecificException"); } catch (ServiceSpecificException e) { assertEquals(e.errorCode, expectedCode); @@ -177,7 +177,7 @@ public class NetdUtilsTest { setNetworkAddInterfaceOutcome(new RemoteException(), expectedTries); try { - NetdUtils.tetherInterface(mNetd, LOCAL_NET_ID, IFACE, TEST_IPPREFIX, 20, 0); + NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE, TEST_IPPREFIX, 20, 0); fail("Expect throw RemoteException"); } catch (RemoteException e) { } @@ -187,18 +187,19 @@ public class NetdUtilsTest { private void verifyNetworkAddInterfaceFails(int expectedTries) throws Exception { verify(mNetd).tetherInterfaceAdd(IFACE); - verify(mNetd, times(expectedTries)).networkAddInterface(LOCAL_NET_ID, IFACE); + verify(mNetd, times(expectedTries)).networkAddInterface(TEST_NET_ID, IFACE); verify(mNetd, never()).networkAddRoute(anyInt(), anyString(), any(), any()); + verifyNoMoreInteractions(mNetd); } private void verifyTetherInterfaceSucceeds(int expectedTries) throws Exception { setNetworkAddInterfaceOutcome(null, expectedTries); - NetdUtils.tetherInterface(mNetd, LOCAL_NET_ID, IFACE, TEST_IPPREFIX); + NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE, TEST_IPPREFIX); verify(mNetd).tetherInterfaceAdd(IFACE); - verify(mNetd, times(expectedTries)).networkAddInterface(LOCAL_NET_ID, IFACE); - verify(mNetd, times(2)).networkAddRoute(eq(LOCAL_NET_ID), eq(IFACE), any(), any()); + verify(mNetd, times(expectedTries)).networkAddInterface(TEST_NET_ID, IFACE); + verify(mNetd, times(2)).networkAddRoute(eq(TEST_NET_ID), eq(IFACE), any(), any()); verifyNoMoreInteractions(mNetd); reset(mNetd); }