1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.networkstack.tethering; 18 19 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; 20 import static android.net.NetworkCapabilities.TRANSPORT_TEST; 21 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.net.LinkProperties; 25 import android.net.NetworkCapabilities; 26 import android.net.RouteInfo; 27 28 import com.android.net.module.util.NetUtils; 29 import com.android.networkstack.tethering.util.InterfaceSet; 30 31 import java.net.InetAddress; 32 import java.net.UnknownHostException; 33 34 /** 35 * @hide 36 */ 37 public final class TetheringInterfaceUtils { 38 private static final InetAddress IN6ADDR_ANY = getByAddress( 39 new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 40 private static final InetAddress INADDR_ANY = getByAddress(new byte[] {0, 0, 0, 0}); 41 42 /** 43 * Get upstream interfaces for tethering based on default routes for IPv4/IPv6. 44 * @return null if there is no usable interface, or a set of at least one interface otherwise. 45 */ getTetheringInterfaces(UpstreamNetworkState ns)46 public static @Nullable InterfaceSet getTetheringInterfaces(UpstreamNetworkState ns) { 47 if (ns == null) { 48 return null; 49 } 50 51 final LinkProperties lp = ns.linkProperties; 52 final String if4 = getInterfaceForDestination(lp, INADDR_ANY); 53 final String if6 = getIPv6Interface(ns); 54 55 return (if4 == null && if6 == null) ? null : new InterfaceSet(if4, if6); 56 } 57 58 /** 59 * Get the upstream interface for IPv6 tethering. 60 * @return null if there is no usable interface, or the interface name otherwise. 61 */ getIPv6Interface(UpstreamNetworkState ns)62 public static @Nullable String getIPv6Interface(UpstreamNetworkState ns) { 63 // Broadly speaking: 64 // 65 // [1] does the upstream have an IPv6 default route? 66 // 67 // and 68 // 69 // [2] does the upstream have one or more global IPv6 /64s 70 // dedicated to this device? 71 // 72 // In lieu of Prefix Delegation and other evaluation of whether a 73 // prefix may or may not be dedicated to this device, for now just 74 // check whether the upstream is TRANSPORT_CELLULAR. This works 75 // because "[t]he 3GPP network allocates each default bearer a unique 76 // /64 prefix", per RFC 6459, Section 5.2. 77 final boolean canTether = 78 (ns != null) && (ns.network != null) 79 && (ns.linkProperties != null) && (ns.networkCapabilities != null) 80 // At least one upstream DNS server: 81 && ns.linkProperties.hasIpv6DnsServer() 82 // Minimal amount of IPv6 provisioning: 83 && ns.linkProperties.hasGlobalIpv6Address() 84 // Temporary approximation of "dedicated prefix": 85 && allowIpv6Tethering(ns.networkCapabilities); 86 87 return canTether 88 ? getInterfaceForDestination(ns.linkProperties, IN6ADDR_ANY) 89 : null; 90 } 91 allowIpv6Tethering(@onNull final NetworkCapabilities nc)92 private static boolean allowIpv6Tethering(@NonNull final NetworkCapabilities nc) { 93 return nc.hasTransport(TRANSPORT_CELLULAR) || nc.hasTransport(TRANSPORT_TEST); 94 } 95 getInterfaceForDestination(LinkProperties lp, InetAddress dst)96 private static String getInterfaceForDestination(LinkProperties lp, InetAddress dst) { 97 final RouteInfo ri = (lp != null) 98 ? NetUtils.selectBestRoute(lp.getAllRoutes(), dst) 99 : null; 100 return (ri != null) ? ri.getInterface() : null; 101 } 102 getByAddress(final byte[] addr)103 private static InetAddress getByAddress(final byte[] addr) { 104 try { 105 return InetAddress.getByAddress(null, addr); 106 } catch (UnknownHostException e) { 107 throw new AssertionError("illegal address length" + addr.length); 108 } 109 } 110 } 111