From 0ae29f0cd5d91362fe24fd8e1be855cb41e8a835 Mon Sep 17 00:00:00 2001 From: lucaslin Date: Fri, 18 Mar 2022 02:34:44 +0800 Subject: [PATCH] Define and implement getProvisionedVpnProfileState() CTS-Coverage-Bug: 225010642 Bug: 225010642 Test: CtsNetTestCases:Ikev2VpnTest Change-Id: I67470903bba2f783e316be47a50c438e049ca856 --- core/api/current.txt | 1 + core/java/android/net/IVpnManager.aidl | 2 + core/java/android/net/VpnManager.java | 15 +++++++ .../com/android/server/VpnManagerService.java | 19 +++++++++ .../com/android/server/connectivity/Vpn.java | 40 +++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index 9133df9cbc68e..1a5f7a78ebb3b 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -25507,6 +25507,7 @@ package android.net { public class VpnManager { method public void deleteProvisionedVpnProfile(); + method @Nullable public android.net.VpnProfileState getProvisionedVpnProfileState(); method @Nullable public android.content.Intent provisionVpnProfile(@NonNull android.net.PlatformVpnProfile); method @Deprecated public void startProvisionedVpnProfile(); method @NonNull public String startProvisionedVpnProfileSession(); diff --git a/core/java/android/net/IVpnManager.aidl b/core/java/android/net/IVpnManager.aidl index 070efa363cc01..b4647cabe1bc0 100644 --- a/core/java/android/net/IVpnManager.aidl +++ b/core/java/android/net/IVpnManager.aidl @@ -17,6 +17,7 @@ package android.net; import android.net.Network; +import android.net.VpnProfileState; import com.android.internal.net.LegacyVpnInfo; import com.android.internal.net.VpnConfig; @@ -40,6 +41,7 @@ interface IVpnManager { void deleteVpnProfile(String packageName); String startVpnProfile(String packageName); void stopVpnProfile(String packageName); + VpnProfileState getProvisionedVpnProfileState(String packageName); /** Always-on VPN APIs */ boolean isAlwaysOnVpnPackageSupported(int userId, String packageName); diff --git a/core/java/android/net/VpnManager.java b/core/java/android/net/VpnManager.java index 7ffff1a69b06a..ae7d91f92cb74 100644 --- a/core/java/android/net/VpnManager.java +++ b/core/java/android/net/VpnManager.java @@ -419,6 +419,21 @@ public class VpnManager { } } + /** + * Retrieve the VpnProfileState for the profile provisioned by the calling package. + * + * @return the VpnProfileState with current information, or null if there was no profile + * provisioned by the calling package. + */ + @Nullable + public VpnProfileState getProvisionedVpnProfileState() { + try { + return mService.getProvisionedVpnProfileState(mContext.getOpPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Resets all VPN settings back to factory defaults. * @hide diff --git a/services/core/java/com/android/server/VpnManagerService.java b/services/core/java/com/android/server/VpnManagerService.java index 7b8cce54c8a70..c1d8e7bf3dc05 100644 --- a/services/core/java/com/android/server/VpnManagerService.java +++ b/services/core/java/com/android/server/VpnManagerService.java @@ -37,6 +37,7 @@ import android.net.NetworkStack; import android.net.UnderlyingNetworkInfo; import android.net.Uri; import android.net.VpnManager; +import android.net.VpnProfileState; import android.net.VpnService; import android.net.util.NetdService; import android.os.Binder; @@ -373,6 +374,24 @@ public class VpnManagerService extends IVpnManager.Stub { } } + /** + * Retrieve the VpnProfileState for the profile provisioned by the given package. + * + * @return the VpnProfileState with current information, or null if there was no profile + * provisioned by the given package. + * @hide + */ + @Override + @Nullable + public VpnProfileState getProvisionedVpnProfileState(@NonNull String packageName) { + final int callingUid = Binder.getCallingUid(); + verifyCallingUidAndPackage(packageName, callingUid); + final int user = UserHandle.getUserId(callingUid); + synchronized (mVpns) { + return mVpns.get(user).getProvisionedVpnProfileState(packageName); + } + } + /** * Start legacy VPN, controlling native daemons as needed. Creates a * secondary thread to perform connection work, returning quickly. diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java index c0df095c32896..a6da4a6a42609 100644 --- a/services/core/java/com/android/server/connectivity/Vpn.java +++ b/services/core/java/com/android/server/connectivity/Vpn.java @@ -75,6 +75,7 @@ import android.net.RouteInfo; import android.net.UidRangeParcel; import android.net.UnderlyingNetworkInfo; import android.net.VpnManager; +import android.net.VpnProfileState; import android.net.VpnService; import android.net.VpnTransportInfo; import android.net.ipsec.ike.ChildSessionCallback; @@ -3438,6 +3439,45 @@ public class Vpn { } } + private @VpnProfileState.State int getStateFromLegacyState(int legacyState) { + switch (legacyState) { + case LegacyVpnInfo.STATE_CONNECTING: + return VpnProfileState.STATE_CONNECTING; + case LegacyVpnInfo.STATE_CONNECTED: + return VpnProfileState.STATE_CONNECTED; + case LegacyVpnInfo.STATE_DISCONNECTED: + return VpnProfileState.STATE_DISCONNECTED; + case LegacyVpnInfo.STATE_FAILED: + return VpnProfileState.STATE_FAILED; + default: + Log.wtf(TAG, "Unhandled state " + legacyState + + ", treat it as STATE_DISCONNECTED"); + return VpnProfileState.STATE_DISCONNECTED; + } + } + + private VpnProfileState makeVpnProfileState() { + // TODO: mSessionKey will be moved to Ikev2VpnRunner once aosp/2007077 is merged, so after + // merging aosp/2007077, here should check Ikev2VpnRunner is null or not. Session key will + // be null if Ikev2VpnRunner is null. + return new VpnProfileState(getStateFromLegacyState(mLegacyState), mSessionKey, mAlwaysOn, + mLockdown); + } + + /** + * Retrieve the VpnProfileState for the profile provisioned by the given package. + * + * @return the VpnProfileState with current information, or null if there was no profile + * provisioned by the given package. + */ + @Nullable + public synchronized VpnProfileState getProvisionedVpnProfileState( + @NonNull String packageName) { + requireNonNull(packageName, "No package name provided"); + enforceNotRestrictedUser(); + return isCurrentIkev2VpnLocked(packageName) ? makeVpnProfileState() : null; + } + /** * Proxy to allow testing *