Handling null interface in addLocalNetAccess, removeLocalNetAccess and getLocalNetAccess

Bug: 398022752
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.BpfNetMapsTest
Change-Id: I4627fb83727d81108ed4629beb80a36e5433e140
This commit is contained in:
Sarup Dalwani 2025-02-24 19:58:24 +00:00
commit 2e336acd5b
2 changed files with 56 additions and 3 deletions

View file

@ -906,7 +906,12 @@ public class BpfNetMaps {
final InetAddress address, final int protocol, final int remotePort,
final boolean isAllowed) {
throwIfPre25Q2("addLocalNetAccess is not available on pre-B devices");
final int ifIndex = mDeps.getIfIndex(iface);
final int ifIndex;
if (iface == null) {
ifIndex = 0;
} else {
ifIndex = mDeps.getIfIndex(iface);
}
if (ifIndex == 0) {
Log.e(TAG, "Failed to get if index, skip addLocalNetAccess for " + address
+ "(" + iface + ")");
@ -935,7 +940,12 @@ public class BpfNetMaps {
public void removeLocalNetAccess(final int lpmBitlen, final String iface,
final InetAddress address, final int protocol, final int remotePort) {
throwIfPre25Q2("removeLocalNetAccess is not available on pre-B devices");
final int ifIndex = mDeps.getIfIndex(iface);
final int ifIndex;
if (iface == null) {
ifIndex = 0;
} else {
ifIndex = mDeps.getIfIndex(iface);
}
if (ifIndex == 0) {
Log.e(TAG, "Failed to get if index, skip removeLocalNetAccess for " + address
+ "(" + iface + ")");
@ -966,7 +976,12 @@ public class BpfNetMaps {
public boolean getLocalNetAccess(final int lpmBitlen, final String iface,
final InetAddress address, final int protocol, final int remotePort) {
throwIfPre25Q2("getLocalNetAccess is not available on pre-B devices");
final int ifIndex = mDeps.getIfIndex(iface);
final int ifIndex;
if (iface == null) {
ifIndex = 0;
} else {
ifIndex = mDeps.getIfIndex(iface);
}
if (ifIndex == 0) {
Log.e(TAG, "Failed to get if index, returning default from getLocalNetAccess for "
+ address + "(" + iface + ")");

View file

@ -264,6 +264,18 @@ public final class BpfNetMapsTest {
Inet4Address.getByName("100.68.0.0"), 0, 0)));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testAddLocalNetAccessWithNullInterfaceAfterV() throws Exception {
assertTrue(mLocalNetAccessMap.isEmpty());
mBpfNetMaps.addLocalNetAccess(160, null,
Inet4Address.getByName("196.68.0.0"), 0, 0, true);
// As we tried to add null interface, it would be skipped and map should be empty.
assertTrue(mLocalNetAccessMap.isEmpty());
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testAddLocalNetAccessAfterVWithIncorrectInterface() throws Exception {
@ -302,6 +314,13 @@ public final class BpfNetMapsTest {
Inet4Address.getByName("100.68.0.0"), 0, 0));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testGetLocalNetAccessWithNullInterfaceAfterV() throws Exception {
assertTrue(mBpfNetMaps.getLocalNetAccess(160, null,
Inet4Address.getByName("100.68.0.0"), 0, 0));
}
@Test
@IgnoreAfter(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testRemoveLocalNetAccessBeforeV() {
@ -349,6 +368,25 @@ public final class BpfNetMapsTest {
Inet4Address.getByName("196.68.0.0"), 0, 0)));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testRemoveLocalNetAccessAfterVWithNullInterface() throws Exception {
assertTrue(mLocalNetAccessMap.isEmpty());
mBpfNetMaps.addLocalNetAccess(160, TEST_IF_NAME,
Inet4Address.getByName("196.68.0.0"), 0, 0, true);
assertNotNull(mLocalNetAccessMap.getValue(new LocalNetAccessKey(160, TEST_IF_INDEX,
Inet4Address.getByName("196.68.0.0"), 0, 0)));
assertNull(mLocalNetAccessMap.getValue(new LocalNetAccessKey(160, TEST_IF_INDEX,
Inet4Address.getByName("100.68.0.0"), 0, 0)));
mBpfNetMaps.removeLocalNetAccess(160, null,
Inet4Address.getByName("196.68.0.0"), 0, 0);
assertNotNull(mLocalNetAccessMap.getValue(new LocalNetAccessKey(160, TEST_IF_INDEX,
Inet4Address.getByName("196.68.0.0"), 0, 0)));
}
@Test
@IgnoreAfter(Build.VERSION_CODES.VANILLA_ICE_CREAM)
public void testAddUidToLocalNetBlockMapBeforeV() {