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

Commit c71f7c3c authored by Sam Mortimer's avatar Sam Mortimer
Browse files

fw/b: [Squash] Add capability to allow tethering to use VPN upstreams

Squash of:

Author: Sam Mortimer <sam@mortimer.me.uk>
Date:   Thu Aug 15 19:40:55 2019 -0700

    fw/b: Add capability to allow tethering to use VPN upstreams

    * Toggled on/off at runtime via a new hotspot lineage setting.

    * Dynamically updates the tethering upstream for existing hotspot
      clients as VPNs are brought up / down or the hotspot setting
      is changed.

    * This implementation depends on fw/b config_tether_upstream_automatic
      being set to true.

    Change-Id: I2ac0b4acc0ea686dfdf54561cb3428808e337160

----

Author: Sam Mortimer <sam@mortimer.me.uk>
Date:   Fri Sep 13 16:27:37 2019 -0700

    fw/b: Prevent trying to enable hw offload for tethering via VPN upstreams

    * Tethering via VPN upstream requires a sw path.

    * hw offload setup happened to be being disabled anyway owing to a fail
      return code from setDataLimit().  However, it was causing offload to be
      disabled entirely (until next hotspot off / on event).

    * Gracefully skip hw offload for vpn upstreams so that it is automatically
      used again when a vpn is disconnected.

    Change-Id: I4df13f02889305560903b7b1e919eedc7af78c07

Change-Id: I77ed0142e653f4993486eea44e4dac21e3f67f17
parent 13def43d
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
import static android.net.ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL;
import static android.net.ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME;
import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE;
import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE;
@@ -59,6 +60,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.usb.UsbManager;
import android.net.INetworkPolicyManager;
import android.net.INetworkStatsService;
@@ -114,6 +116,8 @@ import com.android.server.connectivity.tethering.TetheringInterfaceUtils;
import com.android.server.connectivity.tethering.UpstreamNetworkMonitor;
import com.android.server.net.BaseNetworkObserver;

import lineageos.providers.LineageSettings;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.net.InetAddress;
@@ -296,6 +300,18 @@ public class Tethering extends BaseNetworkObserver {
        if (umi != null) {
            umi.addUserRestrictionsListener(new TetheringUserRestrictionListener(this));
        }

        // Listen for allowing tethering upstream via VPN settings changes
        final ContentObserver vpnSettingObserver = new ContentObserver(handler) {
            @Override
            public void onChange(boolean self) {
                // Reconsider tethering upstream
                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
            }
        };
        mContext.getContentResolver().registerContentObserver(LineageSettings.Secure.getUriFor(
                LineageSettings.Secure.TETHERING_ALLOW_VPN_UPSTREAMS), false, vpnSettingObserver,
                UserHandle.USER_ALL);
    }

    private WifiManager getWifiManager() {
@@ -1604,6 +1620,12 @@ public class Tethering extends BaseNetworkObserver {
            }

            public void updateUpstreamNetworkState(NetworkState ns) {
                // Disable hw offload on vpn upstream interfaces.
                // setUpstreamLinkProperties() interprets null as disable.
                if (ns != null && ns.networkCapabilities != null
                        && !ns.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_VPN)) {
                    ns = null;
                }
                mOffloadController.setUpstreamLinkProperties(
                        (ns != null) ? ns.linkProperties : null);
            }
+27 −0
Original line number Diff line number Diff line
@@ -21,10 +21,12 @@ import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
import static android.net.ConnectivityManager.TYPE_NONE;
import static android.net.ConnectivityManager.getNetworkTypeName;
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;

import android.content.Context;
import android.os.UserHandle;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.IpPrefix;
@@ -42,6 +44,8 @@ import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.StateMachine;

import lineageos.providers.LineageSettings;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
@@ -105,6 +109,9 @@ public class UpstreamNetworkMonitor {
    // The current upstream network used for tethering.
    private Network mTetheringUpstreamNetwork;

    // Set if the Internet is considered reachable via a VPN network
    private Network mVpnInternetNetwork;

    public UpstreamNetworkMonitor(Context ctx, StateMachine tgt, SharedLog log, int what) {
        mContext = ctx;
        mTarget = tgt;
@@ -158,6 +165,8 @@ public class UpstreamNetworkMonitor {
    public void stop() {
        releaseMobileNetworkRequest();

        mVpnInternetNetwork = null;

        releaseCallback(mListenAllCallback);
        mListenAllCallback = null;

@@ -256,6 +265,14 @@ public class UpstreamNetworkMonitor {

    // Returns null if no current upstream available.
    public NetworkState getCurrentPreferredUpstream() {
        // Use VPN upstreams if hotspot settings allow.
        if (mVpnInternetNetwork != null &&
                LineageSettings.Secure.getIntForUser(mContext.getContentResolver(),
                       LineageSettings.Secure.TETHERING_ALLOW_VPN_UPSTREAMS,
                       0, UserHandle.USER_CURRENT) == 1) {
            return mNetworkMap.get(mVpnInternetNetwork);
        }

        final NetworkState dfltState = (mDefaultInternetNetwork != null)
                ? mNetworkMap.get(mDefaultInternetNetwork)
                : null;
@@ -295,6 +312,8 @@ public class UpstreamNetworkMonitor {
    }

    private void handleNetCap(Network network, NetworkCapabilities newNc) {
        if (isVpnInternetNetwork(newNc)) mVpnInternetNetwork = network;

        final NetworkState prev = mNetworkMap.get(network);
        if (prev == null || newNc.equals(prev.networkCapabilities)) {
            // Ignore notifications about networks for which we have not yet
@@ -362,6 +381,9 @@ public class UpstreamNetworkMonitor {
        //       been lost (by any callback)
        //     - deletes the entry from the map only when the LISTEN_ALL
        //       callback gets notified.
        if (network.equals(mVpnInternetNetwork)) {
            mVpnInternetNetwork = null;
        }

        if (!mNetworkMap.containsKey(network)) {
            // Ignore loss of networks about which we had not previously
@@ -561,6 +583,11 @@ public class UpstreamNetworkMonitor {
               !isCellular(ns.networkCapabilities);
    }

    private static boolean isVpnInternetNetwork(NetworkCapabilities nc) {
        return (nc != null) && !nc.hasCapability(NET_CAPABILITY_NOT_VPN) &&
               nc.hasCapability(NET_CAPABILITY_INTERNET);
    }

    private static NetworkState findFirstDunNetwork(Iterable<NetworkState> netStates) {
        for (NetworkState ns : netStates) {
            if (isCellular(ns) && hasCapability(ns, NET_CAPABILITY_DUN)) return ns;