Pass VPN uid range to AutomaticOnOffKeepaliveTracker
This is a preliminary change for filtering sockets that is not in the uid ranges for automatic on/off keepalives. This commit itself is a no-op change to pass the uid information to AutomaticOnOffKeepaliveTracker. Bug: 311119352 Test: atest FrameworksNetTests Change-Id: I7d96e7a0d3f3054d1409de350420a24378b28cdb
This commit is contained in:
@@ -6225,8 +6225,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
if (!networkFound) return;
|
||||
|
||||
if (underpinnedNetworkFound) {
|
||||
final NetworkCapabilities underpinnedNc =
|
||||
getNetworkCapabilitiesInternal(underpinnedNetwork);
|
||||
mKeepaliveTracker.handleMonitorAutomaticKeepalive(ki,
|
||||
underpinnedNetwork.netId);
|
||||
underpinnedNetwork.netId, underpinnedNc.getUids());
|
||||
} else {
|
||||
// If no underpinned network, then make sure the keepalive is running.
|
||||
mKeepaliveTracker.handleMaybeResumeKeepalive(ki);
|
||||
|
||||
@@ -53,6 +53,7 @@ import android.system.StructTimeval;
|
||||
import android.util.LocalLog;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.util.Range;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
@@ -77,6 +78,7 @@ import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Manages automatic on/off socket keepalive requests.
|
||||
@@ -373,26 +375,27 @@ public class AutomaticOnOffKeepaliveTracker {
|
||||
* Determine if any state transition is needed for the specific automatic keepalive.
|
||||
*/
|
||||
public void handleMonitorAutomaticKeepalive(@NonNull final AutomaticOnOffKeepalive ki,
|
||||
final int vpnNetId) {
|
||||
final int vpnNetId, @NonNull Set<Range<Integer>> vpnUidRanges) {
|
||||
// Might happen if the automatic keepalive was removed by the app just as the alarm fires.
|
||||
if (!mAutomaticOnOffKeepalives.contains(ki)) return;
|
||||
if (STATE_ALWAYS_ON == ki.mAutomaticOnOffState) {
|
||||
throw new IllegalStateException("Should not monitor non-auto keepalive");
|
||||
}
|
||||
|
||||
handleMonitorTcpConnections(ki, vpnNetId);
|
||||
handleMonitorTcpConnections(ki, vpnNetId, vpnUidRanges);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if disable or re-enable keepalive is needed or not based on TCP sockets status.
|
||||
*/
|
||||
private void handleMonitorTcpConnections(@NonNull AutomaticOnOffKeepalive ki, int vpnNetId) {
|
||||
private void handleMonitorTcpConnections(@NonNull AutomaticOnOffKeepalive ki, int vpnNetId,
|
||||
@NonNull Set<Range<Integer>> vpnUidRanges) {
|
||||
// Might happen if the automatic keepalive was removed by the app just as the alarm fires.
|
||||
if (!mAutomaticOnOffKeepalives.contains(ki)) return;
|
||||
if (STATE_ALWAYS_ON == ki.mAutomaticOnOffState) {
|
||||
throw new IllegalStateException("Should not monitor non-auto keepalive");
|
||||
}
|
||||
if (!isAnyTcpSocketConnected(vpnNetId)) {
|
||||
if (!isAnyTcpSocketConnected(vpnNetId, vpnUidRanges)) {
|
||||
// No TCP socket exists. Stop keepalive if ENABLED, and remain SUSPENDED if currently
|
||||
// SUSPENDED.
|
||||
if (ki.mAutomaticOnOffState == STATE_ENABLED) {
|
||||
@@ -744,7 +747,7 @@ public class AutomaticOnOffKeepaliveTracker {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
boolean isAnyTcpSocketConnected(int netId) {
|
||||
boolean isAnyTcpSocketConnected(int netId, @NonNull Set<Range<Integer>> vpnUidRanges) {
|
||||
FileDescriptor fd = null;
|
||||
|
||||
try {
|
||||
@@ -757,7 +760,8 @@ public class AutomaticOnOffKeepaliveTracker {
|
||||
|
||||
// Send request for each IP family
|
||||
for (final int family : ADDRESS_FAMILIES) {
|
||||
if (isAnyTcpSocketConnectedForFamily(fd, family, networkMark, networkMask)) {
|
||||
if (isAnyTcpSocketConnectedForFamily(
|
||||
fd, family, networkMark, networkMask, vpnUidRanges)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -771,7 +775,8 @@ public class AutomaticOnOffKeepaliveTracker {
|
||||
}
|
||||
|
||||
private boolean isAnyTcpSocketConnectedForFamily(FileDescriptor fd, int family, int networkMark,
|
||||
int networkMask) throws ErrnoException, InterruptedIOException {
|
||||
int networkMask, @NonNull Set<Range<Integer>> vpnUidRanges)
|
||||
throws ErrnoException, InterruptedIOException {
|
||||
ensureRunningOnHandlerThread();
|
||||
// Build SocketDiag messages and cache it.
|
||||
if (mSockDiagMsg.get(family) == null) {
|
||||
|
||||
@@ -72,7 +72,9 @@ import android.os.Message;
|
||||
import android.os.SystemClock;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.util.Range;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -102,7 +104,9 @@ import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(DevSdkIgnoreRunner.class)
|
||||
@SmallTest
|
||||
@@ -232,6 +236,9 @@ public class AutomaticOnOffKeepaliveTrackerTest {
|
||||
private static final byte[] TEST_RESPONSE_BYTES =
|
||||
HexEncoding.decode(TEST_RESPONSE_HEX.toCharArray(), false);
|
||||
|
||||
private static final Set<Range<Integer>> TEST_UID_RANGES =
|
||||
new ArraySet<>(Arrays.asList(new Range<>(10000, 99999)));
|
||||
|
||||
private static class TestKeepaliveInfo {
|
||||
private static List<Socket> sOpenSockets = new ArrayList<>();
|
||||
|
||||
@@ -409,28 +416,28 @@ public class AutomaticOnOffKeepaliveTrackerTest {
|
||||
public void testIsAnyTcpSocketConnected_runOnNonHandlerThread() throws Exception {
|
||||
setupResponseWithSocketExisting();
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID));
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID, TEST_UID_RANGES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnyTcpSocketConnected_withTargetNetId() throws Exception {
|
||||
setupResponseWithSocketExisting();
|
||||
assertTrue(visibleOnHandlerThread(mTestHandler,
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID)));
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID, TEST_UID_RANGES)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnyTcpSocketConnected_withIncorrectNetId() throws Exception {
|
||||
setupResponseWithSocketExisting();
|
||||
assertFalse(visibleOnHandlerThread(mTestHandler,
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(OTHER_NETID)));
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(OTHER_NETID, TEST_UID_RANGES)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnyTcpSocketConnected_noSocketExists() throws Exception {
|
||||
setupResponseWithoutSocketExisting();
|
||||
assertFalse(visibleOnHandlerThread(mTestHandler,
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID)));
|
||||
() -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID, TEST_UID_RANGES)));
|
||||
}
|
||||
|
||||
private void triggerEventKeepalive(int slot, int reason) {
|
||||
@@ -474,14 +481,16 @@ public class AutomaticOnOffKeepaliveTrackerTest {
|
||||
setupResponseWithoutSocketExisting();
|
||||
visibleOnHandlerThread(
|
||||
mTestHandler,
|
||||
() -> mAOOKeepaliveTracker.handleMonitorAutomaticKeepalive(autoKi, TEST_NETID));
|
||||
() -> mAOOKeepaliveTracker.handleMonitorAutomaticKeepalive(
|
||||
autoKi, TEST_NETID, TEST_UID_RANGES));
|
||||
}
|
||||
|
||||
private void doResumeKeepalive(AutomaticOnOffKeepalive autoKi) throws Exception {
|
||||
setupResponseWithSocketExisting();
|
||||
visibleOnHandlerThread(
|
||||
mTestHandler,
|
||||
() -> mAOOKeepaliveTracker.handleMonitorAutomaticKeepalive(autoKi, TEST_NETID));
|
||||
() -> mAOOKeepaliveTracker.handleMonitorAutomaticKeepalive(
|
||||
autoKi, TEST_NETID, TEST_UID_RANGES));
|
||||
}
|
||||
|
||||
private void doStopKeepalive(AutomaticOnOffKeepalive autoKi) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user