diff options
| author | 2022-09-20 21:37:36 +0800 | |
|---|---|---|
| committer | 2022-09-21 11:37:52 +0000 | |
| commit | fc43b2a00da83fc73c450c5a1fd6150f8b3aae45 (patch) | |
| tree | 434383fe59c4be80e4c7acf466457763525f119c | |
| parent | b65741930f85808ced26b99fea1f4544b5ec5cb6 (diff) | |
Disconnect VPN when the underlying network is lost
This commit mainly does 2 things:
1. Reset IKE state and disconnect VPN network when the underlying
network is lost.
- It can prevent giving a false sense of connectivity to the
app when the VPN is not working due to losing the underlying
network, update the state of VPN is needed.
2. Update the exception of handleSessionLost() from null to
IkeNetworkLostException.
- It fixes the problem that the ERROR_CODE_NETWORK_LOST will
not be sent when the underlying network is lost.
Bug: 245250164
Bug: 223843049
Test: atest FrameworksNetTests:VpnTest
Test: Manually test with VPN on and enable airplane mode to see if
VPN network is disconnected and brought back when default
network is back.
Change-Id: I8f250a2956a1eea5d4a6be20e59f4437d8c8f462
| -rw-r--r-- | services/core/java/com/android/server/connectivity/Vpn.java | 69 |
1 files changed, 36 insertions, 33 deletions
diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java index 676dc196f20c..5e0ba55829e8 100644 --- a/services/core/java/com/android/server/connectivity/Vpn.java +++ b/services/core/java/com/android/server/connectivity/Vpn.java @@ -3210,41 +3210,44 @@ public class Vpn { return; } - if (mSession != null && mMobikeEnabled) { - Log.d( - TAG, - "IKE Session has mobility. Delay handleSessionLost for losing network " - + network - + " on session with token " - + mCurrentToken); - - final int token = mCurrentToken; - // Delay the teardown in case a new network will be available soon. For example, - // during handover between two WiFi networks, Android will disconnect from the - // first WiFi and then connects to the second WiFi. - mScheduledHandleNetworkLostFuture = - mExecutor.schedule( - () -> { - if (isActiveToken(token)) { - handleSessionLost(null /* exception */, network); - } else { - Log.d( - TAG, - "Scheduled handleSessionLost fired for " - + "obsolete token " - + token); + Log.d(TAG, "Schedule a delay handleSessionLost for losing network " + + network + + " on session with token " + + mCurrentToken); + + final int token = mCurrentToken; + // Delay the teardown in case a new network will be available soon. For example, + // during handover between two WiFi networks, Android will disconnect from the + // first WiFi and then connects to the second WiFi. + mScheduledHandleNetworkLostFuture = + mExecutor.schedule( + () -> { + if (isActiveToken(token)) { + handleSessionLost(new IkeNetworkLostException(network), + network); + + synchronized (Vpn.this) { + // Ignore stale runner. + if (mVpnRunner != this) return; + + updateState(DetailedState.DISCONNECTED, + "Network lost"); } + } else { + Log.d( + TAG, + "Scheduled handleSessionLost fired for " + + "obsolete token " + + token); + } + + // Reset mScheduledHandleNetworkLostFuture since it's + // already run on executor thread. + mScheduledHandleNetworkLostFuture = null; + }, + NETWORK_LOST_TIMEOUT_MS, + TimeUnit.MILLISECONDS); - // Reset mScheduledHandleNetworkLostFuture since it's - // already run on executor thread. - mScheduledHandleNetworkLostFuture = null; - }, - NETWORK_LOST_TIMEOUT_MS, - TimeUnit.MILLISECONDS); - } else { - Log.d(TAG, "Call handleSessionLost for losing network " + network); - handleSessionLost(null /* exception */, network); - } } private void cancelHandleNetworkLostTimeout() { |