Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85362a4b authored by lucaslin's avatar lucaslin Committed by Lucas Lin
Browse files

Improve the code of handleSessionLost()

Bug: 229350078
Test: atest FrameworksNetTests:VpnTest
Change-Id: Ia5fee854fb6017c08b7b7fb7ad666eed9a932195
(cherry picked from commit 8c37a315)
Merged-In: Ia5fee854fb6017c08b7b7fb7ad666eed9a932195
parent a7aa18c1
Loading
Loading
Loading
Loading
+34 −81
Original line number Original line Diff line number Diff line
@@ -3315,8 +3315,13 @@ public class Vpn {
                // Ignore stale runner.
                // Ignore stale runner.
                if (mVpnRunner != this) return;
                if (mVpnRunner != this) return;


                String category = null;
                int errorClass = -1;
                int errorCode = -1;
                if (exception instanceof IkeProtocolException) {
                if (exception instanceof IkeProtocolException) {
                    final IkeProtocolException ikeException = (IkeProtocolException) exception;
                    final IkeProtocolException ikeException = (IkeProtocolException) exception;
                    category = VpnManager.CATEGORY_EVENT_IKE_ERROR;
                    errorCode = ikeException.getErrorType();


                    switch (ikeException.getErrorType()) {
                    switch (ikeException.getErrorType()) {
                        case IkeProtocolException.ERROR_TYPE_NO_PROPOSAL_CHOSEN: // Fallthrough
                        case IkeProtocolException.ERROR_TYPE_NO_PROPOSAL_CHOSEN: // Fallthrough
@@ -3326,106 +3331,54 @@ public class Vpn {
                        case IkeProtocolException.ERROR_TYPE_FAILED_CP_REQUIRED: // Fallthrough
                        case IkeProtocolException.ERROR_TYPE_FAILED_CP_REQUIRED: // Fallthrough
                        case IkeProtocolException.ERROR_TYPE_TS_UNACCEPTABLE:
                        case IkeProtocolException.ERROR_TYPE_TS_UNACCEPTABLE:
                            // All the above failures are configuration errors, and are terminal
                            // All the above failures are configuration errors, and are terminal
                            // TODO(b/230548427): Remove SDK check once VPN related stuff are
                            errorClass = VpnManager.ERROR_CLASS_NOT_RECOVERABLE;
                            //  decoupled from ConnectivityServiceTest.
                            break;
                            if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                                sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_IKE_ERROR,
                                        VpnManager.ERROR_CLASS_NOT_RECOVERABLE,
                                        ikeException.getErrorType(),
                                        getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                        network,
                                        getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                                mUnderlyingNetworkCapabilities),
                                        getRedactedLinkPropertiesOfUnderlyingNetwork(
                                                mUnderlyingLinkProperties));
                            }
                            markFailedAndDisconnect(exception);
                            return;
                        // All other cases possibly recoverable.
                        // All other cases possibly recoverable.
                        default:
                        default:
                            // All the above failures are configuration errors, and are terminal
                            // All the above failures are configuration errors, and are terminal
                            // TODO(b/230548427): Remove SDK check once VPN related stuff are
                            errorClass = VpnManager.ERROR_CLASS_RECOVERABLE;
                            //  decoupled from ConnectivityServiceTest.
                            if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                                sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_IKE_ERROR,
                                        VpnManager.ERROR_CLASS_RECOVERABLE,
                                        ikeException.getErrorType(),
                                        getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                        network,
                                        getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                                mUnderlyingNetworkCapabilities),
                                        getRedactedLinkPropertiesOfUnderlyingNetwork(
                                                mUnderlyingLinkProperties));
                            }
                    }
                    }
                } else if (exception instanceof IllegalArgumentException) {
                } else if (exception instanceof IllegalArgumentException) {
                    // Failed to build IKE/ChildSessionParams; fatal profile configuration error
                    // Failed to build IKE/ChildSessionParams; fatal profile configuration error
                    markFailedAndDisconnect(exception);
                    markFailedAndDisconnect(exception);
                    return;
                    return;
                } else if (exception instanceof IkeNetworkLostException) {
                } else if (exception instanceof IkeNetworkLostException) {
                    // TODO(b/230548427): Remove SDK check once VPN related stuff are
                    category = VpnManager.CATEGORY_EVENT_NETWORK_ERROR;
                    //  decoupled from ConnectivityServiceTest.
                    errorClass = VpnManager.ERROR_CLASS_RECOVERABLE;
                    if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                    errorCode = VpnManager.ERROR_CODE_NETWORK_LOST;
                        sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_NETWORK_ERROR,
                                VpnManager.ERROR_CLASS_RECOVERABLE,
                                VpnManager.ERROR_CODE_NETWORK_LOST,
                                getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                network,
                                getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                        mUnderlyingNetworkCapabilities),
                                getRedactedLinkPropertiesOfUnderlyingNetwork(
                                        mUnderlyingLinkProperties));
                    }
                } else if (exception instanceof IkeNonProtocolException) {
                } else if (exception instanceof IkeNonProtocolException) {
                    category = VpnManager.CATEGORY_EVENT_NETWORK_ERROR;
                    errorClass = VpnManager.ERROR_CLASS_RECOVERABLE;
                    if (exception.getCause() instanceof UnknownHostException) {
                    if (exception.getCause() instanceof UnknownHostException) {
                        // TODO(b/230548427): Remove SDK check once VPN related stuff are
                        errorCode = VpnManager.ERROR_CODE_NETWORK_UNKNOWN_HOST;
                        //  decoupled from ConnectivityServiceTest.
                        if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                            sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_NETWORK_ERROR,
                                    VpnManager.ERROR_CLASS_RECOVERABLE,
                                    VpnManager.ERROR_CODE_NETWORK_UNKNOWN_HOST,
                                    getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                    network,
                                    getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                            mUnderlyingNetworkCapabilities),
                                    getRedactedLinkPropertiesOfUnderlyingNetwork(
                                            mUnderlyingLinkProperties));
                        }
                    } else if (exception.getCause() instanceof IkeTimeoutException) {
                    } else if (exception.getCause() instanceof IkeTimeoutException) {
                        // TODO(b/230548427): Remove SDK check once VPN related stuff are
                        errorCode = VpnManager.ERROR_CODE_NETWORK_PROTOCOL_TIMEOUT;
                        //  decoupled from ConnectivityServiceTest.
                        if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                            sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_NETWORK_ERROR,
                                    VpnManager.ERROR_CLASS_RECOVERABLE,
                                    VpnManager.ERROR_CODE_NETWORK_PROTOCOL_TIMEOUT,
                                    getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                    network,
                                    getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                            mUnderlyingNetworkCapabilities),
                                    getRedactedLinkPropertiesOfUnderlyingNetwork(
                                            mUnderlyingLinkProperties));
                        }
                    } else if (exception.getCause() instanceof IOException) {
                    } else if (exception.getCause() instanceof IOException) {
                        errorCode = VpnManager.ERROR_CODE_NETWORK_IO;
                    }
                } else if (exception != null) {
                    Log.wtf(TAG, "onSessionLost: exception = " + exception);
                }

                // TODO(b/230548427): Remove SDK check once VPN related stuff are
                // TODO(b/230548427): Remove SDK check once VPN related stuff are
                //  decoupled from ConnectivityServiceTest.
                //  decoupled from ConnectivityServiceTest.
                        if (SdkLevel.isAtLeastT() && isVpnApp(mPackage)) {
                if (SdkLevel.isAtLeastT() && category != null && isVpnApp(mPackage)) {
                            sendEventToVpnManagerApp(VpnManager.CATEGORY_EVENT_NETWORK_ERROR,
                    sendEventToVpnManagerApp(category, errorClass, errorCode,
                                    VpnManager.ERROR_CLASS_RECOVERABLE,
                                    VpnManager.ERROR_CODE_NETWORK_IO,
                            getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                            getPackage(), mSessionKey, makeVpnProfileStateLocked(),
                                    network,
                            mActiveNetwork,
                            getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                            getRedactedNetworkCapabilitiesOfUnderlyingNetwork(
                                    mUnderlyingNetworkCapabilities),
                                    mUnderlyingNetworkCapabilities),
                            getRedactedLinkPropertiesOfUnderlyingNetwork(
                            getRedactedLinkPropertiesOfUnderlyingNetwork(
                                    mUnderlyingLinkProperties));
                                    mUnderlyingLinkProperties));
                }
                }
                    }
                } else if (exception != null) {
                    Log.wtf(TAG, "onSessionLost: exception = " + exception);
                }


                if (errorClass == VpnManager.ERROR_CLASS_NOT_RECOVERABLE) {
                    markFailedAndDisconnect(exception);
                    return;
                } else {
                    scheduleRetryNewIkeSession();
                    scheduleRetryNewIkeSession();
                }
                }
            }


            mUnderlyingNetworkCapabilities = null;
            mUnderlyingNetworkCapabilities = null;
            mUnderlyingLinkProperties = null;
            mUnderlyingLinkProperties = null;