From 54ba76a1a6333f8faba17e0191b9a24b53b614e6 Mon Sep 17 00:00:00 2001 From: lucaslin Date: Thu, 15 Dec 2022 08:13:49 +0000 Subject: [PATCH] Set VPN as the preference network for specific apps Set VPN as the preference network for specific apps when IKEv2VpnRunner is running, and reset it when IKEv2VpnRunner is cleaned. VPN will be disconnected when its underlying network is gone. To prevent packets go throgh underlying network when underlying network is back but VPN is not connected yet, set VPN as the only preferred network for specefic apps. Bug: 231749077 Test: Create a test app to register default network and check if the VPN is the only default network for the test app. Change-Id: Ib1b65d142a5ccc935f3882f5665134a56277f19a --- .../com/android/server/connectivity/Vpn.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java index 19dbee778e29d..c15e419c073c1 100644 --- a/services/core/java/com/android/server/connectivity/Vpn.java +++ b/services/core/java/com/android/server/connectivity/Vpn.java @@ -140,6 +140,7 @@ import com.android.internal.net.LegacyVpnInfo; import com.android.internal.net.VpnConfig; import com.android.internal.net.VpnProfile; import com.android.modules.utils.build.SdkLevel; +import com.android.net.module.util.BinderUtils; import com.android.net.module.util.NetdUtils; import com.android.net.module.util.NetworkStackConstants; import com.android.server.DeviceIdleInternal; @@ -2783,6 +2784,16 @@ public class Vpn { return hasIPV6 && !hasIPV4; } + private void setVpnNetworkPreference(String session, Set> ranges) { + BinderUtils.withCleanCallingIdentity( + () -> mConnectivityManager.setVpnDefaultForUids(session, ranges)); + } + + private void clearVpnNetworkPreference(String session) { + BinderUtils.withCleanCallingIdentity( + () -> mConnectivityManager.setVpnDefaultForUids(session, Collections.EMPTY_LIST)); + } + /** * Internal class managing IKEv2/IPsec VPN connectivity * @@ -2894,6 +2905,9 @@ public class Vpn { (r, exe) -> { Log.d(TAG, "Runnable " + r + " rejected by the mExecutor"); }); + setVpnNetworkPreference(mSessionKey, + createUserAndRestrictedProfilesRanges(mUserId, + mConfig.allowedApplications, mConfig.disallowedApplications)); } @Override @@ -3047,7 +3061,6 @@ public class Vpn { mConfig.dnsServers.addAll(dnsAddrStrings); mConfig.underlyingNetworks = new Network[] {network}; - mConfig.disallowedApplications = getAppExclusionList(mPackage); networkAgent = mNetworkAgent; @@ -3750,6 +3763,7 @@ public class Vpn { mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); mConnectivityDiagnosticsManager.unregisterConnectivityDiagnosticsCallback( mDiagnosticsCallback); + clearVpnNetworkPreference(mSessionKey); mExecutor.shutdown(); } @@ -4310,6 +4324,7 @@ public class Vpn { mConfig.requiresInternetValidation = profile.requiresInternetValidation; mConfig.excludeLocalRoutes = profile.excludeLocalRoutes; mConfig.allowBypass = profile.isBypassable; + mConfig.disallowedApplications = getAppExclusionList(mPackage); switch (profile.type) { case VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS: @@ -4462,6 +4477,9 @@ public class Vpn { .setUids(createUserAndRestrictedProfilesRanges( mUserId, null /* allowedApplications */, excludedApps)) .build(); + setVpnNetworkPreference(getSessionKeyLocked(), + createUserAndRestrictedProfilesRanges(mUserId, + mConfig.allowedApplications, mConfig.disallowedApplications)); doSendNetworkCapabilities(mNetworkAgent, mNetworkCapabilities); } }