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

Commit 95c37334 authored by Erik Kline's avatar Erik Kline Committed by android-build-merger
Browse files

Merge "Slightly refactor IPv4 default router interface selection"

am: 701421c6

Change-Id: I1c8b9f95c8711c191e0e95cf72a5f4e5a3c3fc53
parents 282c0382 701421c6
Loading
Loading
Loading
Loading
+34 −10
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.connectivity;
import static android.hardware.usb.UsbManager.USB_CONFIGURED;
import static android.hardware.usb.UsbManager.USB_CONNECTED;
import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
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;
@@ -99,6 +100,7 @@ import com.android.server.net.BaseNetworkObserver;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
@@ -1302,19 +1304,16 @@ public class Tethering extends BaseNetworkObserver {

        protected void setUpstreamNetwork(NetworkState ns) {
            String iface = null;
            if (ns != null && ns.linkProperties != null) {
            if (ns != null) {
                // Find the interface with the default IPv4 route. It may be the
                // interface described by linkProperties, or one of the interfaces
                // stacked on top of it.
                mLog.i("Finding IPv4 upstream interface on: " + ns.linkProperties);
                RouteInfo ipv4Default = RouteInfo.selectBestRoute(
                    ns.linkProperties.getAllRoutes(), Inet4Address.ANY);
                if (ipv4Default != null) {
                    iface = ipv4Default.getInterface();
                    mLog.i("Found interface " + ipv4Default.getInterface());
                } else {
                    mLog.i("No IPv4 upstream interface, giving up.");
                }
                mLog.i("Looking for default routes on: " + ns.linkProperties);
                final String iface4 = getIPv4DefaultRouteInterface(ns);
                final String iface6 = getIPv6DefaultRouteInterface(ns);
                mLog.i("IPv4/IPv6 upstream interface(s): " + iface4 + "/" + iface6);

                iface = (iface4 != null) ? iface4 : null /* TODO: iface6 */;
            }

            if (iface != null) {
@@ -1957,6 +1956,31 @@ public class Tethering extends BaseNetworkObserver {
        mTetherStates.remove(iface);
    }

    private static String getIPv4DefaultRouteInterface(NetworkState ns) {
        if (ns == null) return null;
        return getInterfaceForDestination(ns.linkProperties, Inet4Address.ANY);
    }

    private static String getIPv6DefaultRouteInterface(NetworkState ns) {
        if (ns == null) return null;
        // An upstream network's IPv6 capability is currently only useful if it
        // can be 64share'd downstream (RFC 7278). For now, that means mobile
        // upstream networks only.
        if (ns.networkCapabilities == null ||
                !ns.networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) {
            return null;
        }

        return getInterfaceForDestination(ns.linkProperties, Inet6Address.ANY);
    }

    private static String getInterfaceForDestination(LinkProperties lp, InetAddress dst) {
        final RouteInfo ri = (lp != null)
                ? RouteInfo.selectBestRoute(lp.getAllRoutes(), dst)
                : null;
        return (ri != null) ? ri.getInterface() : null;
    }

    private static String[] copy(String[] strarray) {
        return Arrays.copyOf(strarray, strarray.length);
    }