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