• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.annotation.Nullable;
20 import android.net.LinkProperties;
21 import android.net.NetworkCapabilities;
22 import android.net.RouteInfo;
23 import android.net.util.InterfaceSet;
24 
25 import com.android.net.module.util.NetUtils;
26 
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 
30 /**
31  * @hide
32  */
33 public final class TetheringInterfaceUtils {
34     private static final InetAddress IN6ADDR_ANY = getByAddress(
35             new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
36     private static final InetAddress INADDR_ANY = getByAddress(new byte[] {0, 0, 0, 0});
37 
38     /**
39      * Get upstream interfaces for tethering based on default routes for IPv4/IPv6.
40      * @return null if there is no usable interface, or a set of at least one interface otherwise.
41      */
getTetheringInterfaces(UpstreamNetworkState ns)42     public static @Nullable InterfaceSet getTetheringInterfaces(UpstreamNetworkState ns) {
43         if (ns == null) {
44             return null;
45         }
46 
47         final LinkProperties lp = ns.linkProperties;
48         final String if4 = getInterfaceForDestination(lp, INADDR_ANY);
49         final String if6 = getIPv6Interface(ns);
50 
51         return (if4 == null && if6 == null) ? null : new InterfaceSet(if4, if6);
52     }
53 
54     /**
55      * Get the upstream interface for IPv6 tethering.
56      * @return null if there is no usable interface, or the interface name otherwise.
57      */
getIPv6Interface(UpstreamNetworkState ns)58     public static @Nullable String getIPv6Interface(UpstreamNetworkState ns) {
59         // Broadly speaking:
60         //
61         //     [1] does the upstream have an IPv6 default route?
62         //
63         // and
64         //
65         //     [2] does the upstream have one or more global IPv6 /64s
66         //         dedicated to this device?
67         //
68         // In lieu of Prefix Delegation and other evaluation of whether a
69         // prefix may or may not be dedicated to this device, for now just
70         // check whether the upstream is TRANSPORT_CELLULAR. This works
71         // because "[t]he 3GPP network allocates each default bearer a unique
72         // /64 prefix", per RFC 6459, Section 5.2.
73         final boolean canTether =
74                 (ns != null) && (ns.network != null)
75                 && (ns.linkProperties != null) && (ns.networkCapabilities != null)
76                 // At least one upstream DNS server:
77                 && ns.linkProperties.hasIpv6DnsServer()
78                 // Minimal amount of IPv6 provisioning:
79                 && ns.linkProperties.hasGlobalIpv6Address()
80                 // Temporary approximation of "dedicated prefix":
81                 && ns.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
82 
83         return canTether
84                 ? getInterfaceForDestination(ns.linkProperties, IN6ADDR_ANY)
85                 : null;
86     }
87 
getInterfaceForDestination(LinkProperties lp, InetAddress dst)88     private static String getInterfaceForDestination(LinkProperties lp, InetAddress dst) {
89         final RouteInfo ri = (lp != null)
90                 ? NetUtils.selectBestRoute(lp.getAllRoutes(), dst)
91                 : null;
92         return (ri != null) ? ri.getInterface() : null;
93     }
94 
getByAddress(final byte[] addr)95     private static InetAddress getByAddress(final byte[] addr) {
96         try {
97             return InetAddress.getByAddress(null, addr);
98         } catch (UnknownHostException e) {
99             throw new AssertionError("illegal address length" + addr.length);
100         }
101     }
102 }
103