Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
markchien | 503be61 | 2020-04-12 21:41:29 +0800 | [diff] [blame] | 17 | package com.android.networkstack.tethering; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 18 | |
Chiachang Wang | 96ca8e9 | 2020-02-21 17:50:18 +0800 | [diff] [blame] | 19 | import static android.Manifest.permission.NETWORK_SETTINGS; |
| 20 | import static android.Manifest.permission.NETWORK_STACK; |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 23 | import static android.hardware.usb.UsbManager.USB_CONFIGURED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 24 | import static android.hardware.usb.UsbManager.USB_CONNECTED; |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 25 | import static android.hardware.usb.UsbManager.USB_FUNCTION_NCM; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 26 | import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 28 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 30 | import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 31 | import static android.net.TetheringManager.ACTION_TETHER_STATE_CHANGED; |
| 32 | import static android.net.TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY; |
| 33 | import static android.net.TetheringManager.EXTRA_ACTIVE_TETHER; |
| 34 | import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER; |
| 35 | import static android.net.TetheringManager.EXTRA_ERRORED_TETHER; |
| 36 | import static android.net.TetheringManager.TETHERING_BLUETOOTH; |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 37 | import static android.net.TetheringManager.TETHERING_ETHERNET; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 38 | import static android.net.TetheringManager.TETHERING_INVALID; |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 39 | import static android.net.TetheringManager.TETHERING_NCM; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 40 | import static android.net.TetheringManager.TETHERING_USB; |
| 41 | import static android.net.TetheringManager.TETHERING_WIFI; |
| 42 | import static android.net.TetheringManager.TETHERING_WIFI_P2P; |
Dedy Lansky | 04bef3c | 2019-11-21 00:36:14 +0200 | [diff] [blame] | 43 | import static android.net.TetheringManager.TETHERING_WIGIG; |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 44 | import static android.net.TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 45 | import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR; |
| 46 | import static android.net.TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 47 | import static android.net.TetheringManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 48 | import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 49 | import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_TYPE; |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 50 | import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_FAILED; |
| 51 | import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_STARTED; |
| 52 | import static android.net.TetheringManager.TETHER_HARDWARE_OFFLOAD_STOPPED; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 53 | import static android.net.util.TetheringMessageBase.BASE_MASTER; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 54 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 55 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 56 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 57 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; |
| 58 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 59 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
| 60 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 61 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 62 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 63 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 64 | |
markchien | 503be61 | 2020-04-12 21:41:29 +0800 | [diff] [blame] | 65 | import static com.android.networkstack.tethering.TetheringNotificationUpdater.DOWNSTREAM_NONE; |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 66 | |
Nucca Chen | 3f856b4 | 2020-06-17 07:03:39 +0000 | [diff] [blame] | 67 | import android.app.usage.NetworkStatsManager; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 68 | import android.bluetooth.BluetoothAdapter; |
| 69 | import android.bluetooth.BluetoothPan; |
| 70 | import android.bluetooth.BluetoothProfile; |
| 71 | import android.bluetooth.BluetoothProfile.ServiceListener; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 72 | import android.content.BroadcastReceiver; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 73 | import android.content.Context; |
| 74 | import android.content.Intent; |
| 75 | import android.content.IntentFilter; |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 76 | import android.content.pm.PackageManager; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 77 | import android.hardware.usb.UsbManager; |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 78 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 79 | import android.net.EthernetManager; |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 80 | import android.net.IIntResultListener; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 81 | import android.net.INetd; |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 82 | import android.net.ITetheringEventCallback; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 83 | import android.net.IpPrefix; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 84 | import android.net.LinkAddress; |
Robert Greenwalt | 05d8936 | 2011-01-23 16:04:05 -0800 | [diff] [blame] | 85 | import android.net.LinkProperties; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 86 | import android.net.Network; |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 87 | import android.net.NetworkCapabilities; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 88 | import android.net.NetworkInfo; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 89 | import android.net.TetherStatesParcel; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 90 | import android.net.TetheredClient; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 91 | import android.net.TetheringCallbackStartedParcel; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 92 | import android.net.TetheringConfigurationParcel; |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 93 | import android.net.TetheringRequestParcel; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 94 | import android.net.ip.IpServer; |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 95 | import android.net.shared.NetdUtils; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 96 | import android.net.util.BaseNetdUnsolicitedEventListener; |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 97 | import android.net.util.InterfaceSet; |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 98 | import android.net.util.PrefixUtils; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 99 | import android.net.util.SharedLog; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 100 | import android.net.util.TetheringUtils; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 101 | import android.net.util.VersionedBroadcastListener; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 102 | import android.net.wifi.WifiClient; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 103 | import android.net.wifi.WifiManager; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 104 | import android.net.wifi.p2p.WifiP2pGroup; |
| 105 | import android.net.wifi.p2p.WifiP2pInfo; |
| 106 | import android.net.wifi.p2p.WifiP2pManager; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 107 | import android.os.Binder; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 108 | import android.os.Bundle; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 109 | import android.os.Handler; |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 110 | import android.os.Looper; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 111 | import android.os.Message; |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 112 | import android.os.RemoteCallbackList; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 113 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 114 | import android.os.ResultReceiver; |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 115 | import android.os.ServiceSpecificException; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 116 | import android.os.UserHandle; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 117 | import android.os.UserManager; |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 118 | import android.provider.Settings; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 119 | import android.telephony.PhoneStateListener; |
| 120 | import android.telephony.TelephonyManager; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 121 | import android.text.TextUtils; |
Christopher Wiley | e949039 | 2016-05-26 15:57:29 -0700 | [diff] [blame] | 122 | import android.util.ArrayMap; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 123 | import android.util.Log; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 124 | import android.util.SparseArray; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 125 | |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 126 | import androidx.annotation.NonNull; |
| 127 | import androidx.annotation.Nullable; |
| 128 | |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 129 | import com.android.internal.annotations.GuardedBy; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 130 | import com.android.internal.annotations.VisibleForTesting; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 131 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 132 | import com.android.internal.util.MessageUtils; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 133 | import com.android.internal.util.State; |
| 134 | import com.android.internal.util.StateMachine; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 135 | |
| 136 | import java.io.FileDescriptor; |
| 137 | import java.io.PrintWriter; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 138 | import java.net.InetAddress; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 139 | import java.util.ArrayList; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 140 | import java.util.Arrays; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 141 | import java.util.Collection; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 142 | import java.util.Collections; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 143 | import java.util.Iterator; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 144 | import java.util.LinkedHashSet; |
| 145 | import java.util.List; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 146 | import java.util.Set; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 147 | import java.util.concurrent.Executor; |
| 148 | import java.util.concurrent.RejectedExecutionException; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 149 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 150 | /** |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 151 | * |
Christopher Wiley | 3b1d922 | 2016-05-20 16:44:04 -0700 | [diff] [blame] | 152 | * This class holds much of the business logic to allow Android devices |
| 153 | * to act as IP gateways via USB, BT, and WiFi interfaces. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 154 | */ |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 155 | public class Tethering { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 156 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 157 | private static final String TAG = Tethering.class.getSimpleName(); |
| 158 | private static final boolean DBG = false; |
| 159 | private static final boolean VDBG = false; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 160 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 161 | private static final Class[] sMessageClasses = { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 162 | Tethering.class, TetherMasterSM.class, IpServer.class |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 163 | }; |
| 164 | private static final SparseArray<String> sMagicDecoderRing = |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 165 | MessageUtils.findMessageNames(sMessageClasses); |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 166 | // Keep in sync with NETID_UNSET in system/netd/include/netid_client.h |
| 167 | private static final int NETID_UNSET = 0; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 168 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 169 | private static class TetherState { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 170 | public final IpServer ipServer; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 171 | public int lastState; |
| 172 | public int lastError; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 173 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 174 | TetherState(IpServer ipServer) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 175 | this.ipServer = ipServer; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 176 | // Assume all state machines start out available and with no errors. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 177 | lastState = IpServer.STATE_AVAILABLE; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 178 | lastError = TETHER_ERROR_NO_ERROR; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 179 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 180 | |
| 181 | public boolean isCurrentlyServing() { |
| 182 | switch (lastState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 183 | case IpServer.STATE_TETHERED: |
| 184 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 185 | return true; |
| 186 | default: |
| 187 | return false; |
| 188 | } |
| 189 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 190 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 191 | |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 192 | /** |
| 193 | * Cookie added when registering {@link android.net.TetheringManager.TetheringEventCallback}. |
| 194 | */ |
| 195 | private static class CallbackCookie { |
| 196 | public final boolean hasListClientsPermission; |
| 197 | |
| 198 | private CallbackCookie(boolean hasListClientsPermission) { |
| 199 | this.hasListClientsPermission = hasListClientsPermission; |
| 200 | } |
| 201 | } |
| 202 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 203 | private final SharedLog mLog = new SharedLog(TAG); |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 204 | private final RemoteCallbackList<ITetheringEventCallback> mTetheringEventCallbacks = |
| 205 | new RemoteCallbackList<>(); |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 206 | // Currently active tethering requests per tethering type. Only one of each type can be |
| 207 | // requested at a time. After a tethering type is requested, the map keeps tethering parameters |
| 208 | // to be used after the interface comes up asynchronously. |
| 209 | private final SparseArray<TetheringRequestParcel> mActiveTetheringRequests = |
| 210 | new SparseArray<>(); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 211 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 212 | // used to synchronize public access to members |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 213 | // TODO(b/153621704): remove mPublicSync to make Tethering lock free |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 214 | private final Object mPublicSync; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 215 | private final Context mContext; |
| 216 | private final ArrayMap<String, TetherState> mTetherStates; |
| 217 | private final BroadcastReceiver mStateReceiver; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 218 | private final Looper mLooper; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 219 | private final StateMachine mTetherMasterSM; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 220 | private final OffloadController mOffloadController; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 221 | private final UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 222 | // TODO: Figure out how to merge this and other downstream-tracking objects |
| 223 | // into a single coherent structure. |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 224 | // Use LinkedHashSet for predictable ordering order for ConnectedClientsTracker. |
| 225 | private final LinkedHashSet<IpServer> mForwardedDownstreams; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 226 | private final VersionedBroadcastListener mCarrierConfigChange; |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 227 | private final TetheringDependencies mDeps; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 228 | private final EntitlementManager mEntitlementMgr; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 229 | private final Handler mHandler; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 230 | private final INetd mNetd; |
| 231 | private final NetdCallback mNetdCallback; |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 232 | private final UserRestrictionActionListener mTetheringRestriction; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 233 | private final ActiveDataSubIdListener mActiveDataSubIdListener; |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 234 | private final ConnectedClientsTracker mConnectedClientsTracker; |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 235 | private final TetheringThreadExecutor mExecutor; |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 236 | private final TetheringNotificationUpdater mNotificationUpdater; |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 237 | private final UserManager mUserManager; |
Hungming Chen | be4827d | 2020-06-02 00:13:26 +0000 | [diff] [blame] | 238 | private final BpfCoordinator mBpfCoordinator; |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 239 | private final PrivateAddressCoordinator mPrivateAddressCoordinator; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 240 | private int mActiveDataSubId = INVALID_SUBSCRIPTION_ID; |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 241 | // All the usage of mTetheringEventCallback should run in the same thread. |
| 242 | private ITetheringEventCallback mTetheringEventCallback = null; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 243 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 244 | private volatile TetheringConfiguration mConfig; |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 245 | private InterfaceSet mCurrentUpstreamIfaceSet; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 246 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 247 | private boolean mRndisEnabled; // track the RNDIS function enabled state |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 248 | // True iff. WiFi tethering should be started when soft AP is ready. |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 249 | private boolean mWifiTetherRequested; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 250 | private Network mTetherUpstream; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 251 | private TetherStatesParcel mTetherStatesParcel; |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 252 | private boolean mDataSaverEnabled = false; |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 253 | private String mWifiP2pTetherInterface = null; |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 254 | private int mOffloadStatus = TETHER_HARDWARE_OFFLOAD_STOPPED; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 255 | |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 256 | @GuardedBy("mPublicSync") |
| 257 | private EthernetManager.TetheredInterfaceRequest mEthernetIfaceRequest; |
| 258 | @GuardedBy("mPublicSync") |
| 259 | private String mConfiguredEthernetIface; |
| 260 | @GuardedBy("mPublicSync") |
| 261 | private EthernetCallback mEthernetCallback; |
| 262 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 263 | public Tethering(TetheringDependencies deps) { |
| 264 | mLog.mark("Tethering.constructed"); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 265 | mDeps = deps; |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 266 | mContext = mDeps.getContext(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 267 | mNetd = mDeps.getINetd(mContext); |
| 268 | mLooper = mDeps.getTetheringLooper(); |
Paul Hu | 77fa8d6 | 2020-04-16 02:54:37 +0000 | [diff] [blame] | 269 | mNotificationUpdater = mDeps.getNotificationUpdater(mContext, mLooper); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 270 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 271 | mPublicSync = new Object(); |
| 272 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 273 | mTetherStates = new ArrayMap<>(); |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 274 | mConnectedClientsTracker = new ConnectedClientsTracker(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 275 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 276 | mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper, deps); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 277 | mTetherMasterSM.start(); |
| 278 | |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 279 | mHandler = mTetherMasterSM.getHandler(); |
Mark Chien | 66b9a4a | 2020-05-07 03:49:42 +0000 | [diff] [blame] | 280 | mOffloadController = mDeps.getOffloadController(mHandler, mLog, |
| 281 | new OffloadController.Dependencies() { |
Junyu Lai | 833f4f6 | 2020-05-05 10:45:38 +0000 | [diff] [blame] | 282 | |
| 283 | @Override |
| 284 | public TetheringConfiguration getTetherConfig() { |
| 285 | return mConfig; |
| 286 | } |
| 287 | }); |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 288 | mUpstreamNetworkMonitor = mDeps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog, |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 289 | TetherMasterSM.EVENT_UPSTREAM_CALLBACK); |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 290 | mForwardedDownstreams = new LinkedHashSet<>(); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 291 | |
| 292 | IntentFilter filter = new IntentFilter(); |
| 293 | filter.addAction(ACTION_CARRIER_CONFIG_CHANGED); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 294 | // EntitlementManager will send EVENT_UPSTREAM_PERMISSION_CHANGED when cellular upstream |
| 295 | // permission is changed according to entitlement check result. |
Mark Chien | 47430c0 | 2020-05-08 11:00:42 +0000 | [diff] [blame] | 296 | mEntitlementMgr = mDeps.getEntitlementManager(mContext, mHandler, mLog, |
| 297 | () -> mTetherMasterSM.sendMessage( |
| 298 | TetherMasterSM.EVENT_UPSTREAM_PERMISSION_CHANGED)); |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 299 | mEntitlementMgr.setOnUiEntitlementFailedListener((int downstream) -> { |
| 300 | mLog.log("OBSERVED UiEnitlementFailed"); |
| 301 | stopTethering(downstream); |
| 302 | }); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 303 | mEntitlementMgr.setTetheringConfigurationFetcher(() -> { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 304 | return mConfig; |
| 305 | }); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 306 | |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 307 | mCarrierConfigChange = new VersionedBroadcastListener( |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 308 | "CarrierConfigChangeListener", mContext, mHandler, filter, |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 309 | (Intent ignored) -> { |
| 310 | mLog.log("OBSERVED carrier config change"); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 311 | updateConfiguration(); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 312 | mEntitlementMgr.reevaluateSimCardProvisioning(mConfig); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 313 | }); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 314 | |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 315 | mStateReceiver = new StateReceiver(); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 316 | |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 317 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 318 | mTetheringRestriction = new UserRestrictionActionListener( |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 319 | mUserManager, this, mNotificationUpdater); |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 320 | mExecutor = new TetheringThreadExecutor(mHandler); |
| 321 | mActiveDataSubIdListener = new ActiveDataSubIdListener(mExecutor); |
Mark Chien | 674a012 | 2020-03-27 09:37:01 +0000 | [diff] [blame] | 322 | mNetdCallback = new NetdCallback(); |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 323 | mPrivateAddressCoordinator = new PrivateAddressCoordinator(mContext); |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 324 | |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 325 | // Load tethering configuration. |
| 326 | updateConfiguration(); |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 327 | |
Nucca Chen | 3f856b4 | 2020-06-17 07:03:39 +0000 | [diff] [blame] | 328 | // Must be initialized after tethering configuration is loaded because BpfCoordinator |
| 329 | // constructor needs to use the configuration. |
| 330 | mBpfCoordinator = mDeps.getBpfCoordinator( |
| 331 | new BpfCoordinator.Dependencies() { |
| 332 | @NonNull |
| 333 | public Handler getHandler() { |
| 334 | return mHandler; |
| 335 | } |
| 336 | |
| 337 | @NonNull |
| 338 | public INetd getNetd() { |
| 339 | return mNetd; |
| 340 | } |
| 341 | |
| 342 | @NonNull |
| 343 | public NetworkStatsManager getNetworkStatsManager() { |
Nucca Chen | 1c63ba3 | 2020-06-17 07:03:57 +0000 | [diff] [blame] | 344 | return mContext.getSystemService(NetworkStatsManager.class); |
Nucca Chen | 3f856b4 | 2020-06-17 07:03:39 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @NonNull |
| 348 | public SharedLog getSharedLog() { |
| 349 | return mLog; |
| 350 | } |
| 351 | |
| 352 | @Nullable |
| 353 | public TetheringConfiguration getTetherConfig() { |
| 354 | return mConfig; |
| 355 | } |
| 356 | }); |
| 357 | |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 358 | startStateMachineUpdaters(); |
Mark Chien | 674a012 | 2020-03-27 09:37:01 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Start to register callbacks. |
| 363 | * Call this function when tethering is ready to handle callback events. |
| 364 | */ |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 365 | private void startStateMachineUpdaters() { |
markchien | e67c564 | 2020-02-06 15:25:47 +0800 | [diff] [blame] | 366 | try { |
| 367 | mNetd.registerUnsolicitedEventListener(mNetdCallback); |
| 368 | } catch (RemoteException e) { |
| 369 | mLog.e("Unable to register netd UnsolicitedEventListener"); |
| 370 | } |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 371 | mCarrierConfigChange.startListening(); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 372 | mContext.getSystemService(TelephonyManager.class).listen(mActiveDataSubIdListener, |
| 373 | PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 374 | |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 375 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 376 | filter.addAction(UsbManager.ACTION_USB_STATE); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 377 | filter.addAction(CONNECTIVITY_ACTION); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 378 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 379 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 380 | filter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 381 | filter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED); |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 382 | filter.addAction(ACTION_RESTRICT_BACKGROUND_CHANGED); |
Mark Chien | 674a012 | 2020-03-27 09:37:01 +0000 | [diff] [blame] | 383 | mContext.registerReceiver(mStateReceiver, filter, null, mHandler); |
| 384 | |
Paul Hu | 77fa8d6 | 2020-04-16 02:54:37 +0000 | [diff] [blame] | 385 | final IntentFilter noUpstreamFilter = new IntentFilter(); |
| 386 | noUpstreamFilter.addAction(TetheringNotificationUpdater.ACTION_DISABLE_TETHERING); |
| 387 | mContext.registerReceiver( |
| 388 | mStateReceiver, noUpstreamFilter, PERMISSION_MAINLINE_NETWORK_STACK, mHandler); |
| 389 | |
Mark Chien | 674a012 | 2020-03-27 09:37:01 +0000 | [diff] [blame] | 390 | final WifiManager wifiManager = getWifiManager(); |
| 391 | if (wifiManager != null) { |
| 392 | wifiManager.registerSoftApCallback(mExecutor, new TetheringSoftApCallback()); |
| 393 | } |
| 394 | |
| 395 | startTrackDefaultNetwork(); |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 396 | } |
| 397 | |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 398 | private class TetheringThreadExecutor implements Executor { |
| 399 | private final Handler mTetherHandler; |
| 400 | TetheringThreadExecutor(Handler handler) { |
| 401 | mTetherHandler = handler; |
| 402 | } |
| 403 | @Override |
| 404 | public void execute(Runnable command) { |
| 405 | if (!mTetherHandler.post(command)) { |
| 406 | throw new RejectedExecutionException(mTetherHandler + " is shutting down"); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private class ActiveDataSubIdListener extends PhoneStateListener { |
| 412 | ActiveDataSubIdListener(Executor executor) { |
| 413 | super(executor); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public void onActiveDataSubscriptionIdChanged(int subId) { |
| 418 | mLog.log("OBSERVED active data subscription change, from " + mActiveDataSubId |
| 419 | + " to " + subId); |
| 420 | if (subId == mActiveDataSubId) return; |
| 421 | |
| 422 | mActiveDataSubId = subId; |
| 423 | updateConfiguration(); |
Paul Hu | 274cc1c | 2020-03-25 06:22:43 +0000 | [diff] [blame] | 424 | mNotificationUpdater.onActiveDataSubscriptionIdChanged(subId); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 425 | // To avoid launching unexpected provisioning checks, ignore re-provisioning |
| 426 | // when no CarrierConfig loaded yet. Assume reevaluateSimCardProvisioning() |
Paul Hu | 274cc1c | 2020-03-25 06:22:43 +0000 | [diff] [blame] | 427 | // will be triggered again when CarrierConfig is loaded. |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 428 | if (mEntitlementMgr.getCarrierConfig(mConfig) != null) { |
| 429 | mEntitlementMgr.reevaluateSimCardProvisioning(mConfig); |
| 430 | } else { |
| 431 | mLog.log("IGNORED reevaluate provisioning, no carrier config loaded"); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 436 | private WifiManager getWifiManager() { |
| 437 | return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| 438 | } |
| 439 | |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 440 | // NOTE: This is always invoked on the mLooper thread. |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 441 | private void updateConfiguration() { |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 442 | mConfig = mDeps.generateTetheringConfiguration(mContext, mLog, mActiveDataSubId); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 443 | mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 444 | reportConfigurationChanged(mConfig.toStableParcelable()); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | private void maybeDunSettingChanged() { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 448 | final boolean isDunRequired = TetheringConfiguration.checkDunRequired(mContext); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 449 | if (isDunRequired == mConfig.isDunRequired) return; |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 450 | updateConfiguration(); |
| 451 | } |
| 452 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 453 | private class NetdCallback extends BaseNetdUnsolicitedEventListener { |
| 454 | @Override |
| 455 | public void onInterfaceChanged(String ifName, boolean up) { |
| 456 | mHandler.post(() -> interfaceStatusChanged(ifName, up)); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public void onInterfaceLinkStateChanged(String ifName, boolean up) { |
| 461 | mHandler.post(() -> interfaceLinkStateChanged(ifName, up)); |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public void onInterfaceAdded(String ifName) { |
| 466 | mHandler.post(() -> interfaceAdded(ifName)); |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void onInterfaceRemoved(String ifName) { |
| 471 | mHandler.post(() -> interfaceRemoved(ifName)); |
| 472 | } |
| 473 | } |
| 474 | |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 475 | private class TetheringSoftApCallback implements WifiManager.SoftApCallback { |
| 476 | // TODO: Remove onStateChanged override when this method has default on |
| 477 | // WifiManager#SoftApCallback interface. |
| 478 | // Wifi listener for state change of the soft AP |
| 479 | @Override |
| 480 | public void onStateChanged(final int state, final int failureReason) { |
| 481 | // Nothing |
| 482 | } |
| 483 | |
| 484 | // Called by wifi when the number of soft AP clients changed. |
| 485 | @Override |
| 486 | public void onConnectedClientsChanged(final List<WifiClient> clients) { |
Treehugger Robot | 2506781 | 2020-03-25 15:32:41 +0000 | [diff] [blame] | 487 | updateConnectedClients(clients); |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 491 | void interfaceStatusChanged(String iface, boolean up) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 492 | // Never called directly: only called from interfaceLinkStateChanged. |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 493 | // See NetlinkHandler.cpp: notifyInterfaceChanged. |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 494 | if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 495 | synchronized (mPublicSync) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 496 | if (up) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 497 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 498 | } else { |
Dedy Lansky | 04bef3c | 2019-11-21 00:36:14 +0200 | [diff] [blame] | 499 | if (ifaceNameToType(iface) == TETHERING_BLUETOOTH |
| 500 | || ifaceNameToType(iface) == TETHERING_WIGIG) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 501 | stopTrackingInterfaceLocked(iface); |
Christopher Wiley | d30aaeb | 2016-07-08 09:33:50 -0700 | [diff] [blame] | 502 | } else { |
| 503 | // Ignore usb0 down after enabling RNDIS. |
| 504 | // We will handle disconnect in interfaceRemoved. |
| 505 | // Similarly, ignore interface down for WiFi. We monitor WiFi AP status |
| 506 | // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent. |
| 507 | if (VDBG) Log.d(TAG, "ignore interface down for " + iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 511 | } |
| 512 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 513 | void interfaceLinkStateChanged(String iface, boolean up) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 514 | interfaceStatusChanged(iface, up); |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 517 | private int ifaceNameToType(String iface) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 518 | final TetheringConfiguration cfg = mConfig; |
| 519 | |
| 520 | if (cfg.isWifi(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 521 | return TETHERING_WIFI; |
Dedy Lansky | 04bef3c | 2019-11-21 00:36:14 +0200 | [diff] [blame] | 522 | } else if (cfg.isWigig(iface)) { |
| 523 | return TETHERING_WIGIG; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 524 | } else if (cfg.isWifiP2p(iface)) { |
| 525 | return TETHERING_WIFI_P2P; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 526 | } else if (cfg.isUsb(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 527 | return TETHERING_USB; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 528 | } else if (cfg.isBluetooth(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 529 | return TETHERING_BLUETOOTH; |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 530 | } else if (cfg.isNcm(iface)) { |
| 531 | return TETHERING_NCM; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 532 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 533 | return TETHERING_INVALID; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 534 | } |
| 535 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 536 | void interfaceAdded(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 537 | if (VDBG) Log.d(TAG, "interfaceAdded " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 538 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 539 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 540 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 541 | } |
| 542 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 543 | void interfaceRemoved(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 544 | if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 545 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 546 | stopTrackingInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 550 | void startTethering(final TetheringRequestParcel request, final IIntResultListener listener) { |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 551 | mHandler.post(() -> { |
| 552 | final TetheringRequestParcel unfinishedRequest = mActiveTetheringRequests.get( |
| 553 | request.tetheringType); |
| 554 | // If tethering is already enabled with a different request, |
| 555 | // disable before re-enabling. |
| 556 | if (unfinishedRequest != null |
| 557 | && !TetheringUtils.isTetheringRequestEquals(unfinishedRequest, request)) { |
| 558 | enableTetheringInternal(request.tetheringType, false /* disabled */, null); |
| 559 | mEntitlementMgr.stopProvisioningIfNeeded(request.tetheringType); |
| 560 | } |
| 561 | mActiveTetheringRequests.put(request.tetheringType, request); |
| 562 | |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 563 | if (request.exemptFromEntitlementCheck) { |
| 564 | mEntitlementMgr.setExemptedDownstreamType(request.tetheringType); |
| 565 | } else { |
| 566 | mEntitlementMgr.startProvisioningIfNeeded(request.tetheringType, |
| 567 | request.showProvisioningUi); |
| 568 | } |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 569 | enableTetheringInternal(request.tetheringType, true /* enabled */, listener); |
| 570 | }); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 571 | } |
| 572 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 573 | void stopTethering(int type) { |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 574 | mHandler.post(() -> { |
| 575 | mActiveTetheringRequests.remove(type); |
| 576 | |
| 577 | enableTetheringInternal(type, false /* disabled */, null); |
| 578 | mEntitlementMgr.stopProvisioningIfNeeded(type); |
| 579 | }); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 583 | * Enables or disables tethering for the given type. If provisioning is required, it will |
| 584 | * schedule provisioning rechecks for the specified interface. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 585 | */ |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 586 | private void enableTetheringInternal(int type, boolean enable, |
| 587 | final IIntResultListener listener) { |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 588 | int result = TETHER_ERROR_NO_ERROR; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 589 | switch (type) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 590 | case TETHERING_WIFI: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 591 | result = setWifiTethering(enable); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 592 | break; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 593 | case TETHERING_USB: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 594 | result = setUsbTethering(enable); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 595 | break; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 596 | case TETHERING_BLUETOOTH: |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 597 | setBluetoothTethering(enable, listener); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 598 | break; |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 599 | case TETHERING_NCM: |
| 600 | result = setNcmTethering(enable); |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 601 | break; |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 602 | case TETHERING_ETHERNET: |
| 603 | result = setEthernetTethering(enable); |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 604 | break; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 605 | default: |
| 606 | Log.w(TAG, "Invalid tether type."); |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 607 | result = TETHER_ERROR_UNKNOWN_TYPE; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // The result of Bluetooth tethering will be sent by #setBluetoothTethering. |
| 611 | if (type != TETHERING_BLUETOOTH) { |
| 612 | sendTetherResult(listener, result, type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 616 | private void sendTetherResult(final IIntResultListener listener, final int result, |
| 617 | final int type) { |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 618 | if (listener != null) { |
| 619 | try { |
| 620 | listener.onResult(result); |
| 621 | } catch (RemoteException e) { } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 622 | } |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 623 | |
| 624 | // If changing tethering fail, remove corresponding request |
| 625 | // no matter who trigger the start/stop. |
| 626 | if (result != TETHER_ERROR_NO_ERROR) mActiveTetheringRequests.remove(type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 629 | private int setWifiTethering(final boolean enable) { |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 630 | final long ident = Binder.clearCallingIdentity(); |
| 631 | try { |
| 632 | synchronized (mPublicSync) { |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 633 | final WifiManager mgr = getWifiManager(); |
Ye Jiao | 21f9625 | 2019-01-28 12:54:22 +0800 | [diff] [blame] | 634 | if (mgr == null) { |
| 635 | mLog.e("setWifiTethering: failed to get WifiManager!"); |
| 636 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 637 | } |
lesl | 6b7551a | 2019-12-13 10:56:35 +0800 | [diff] [blame] | 638 | if ((enable && mgr.startTetheredHotspot(null /* use existing softap config */)) |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 639 | || (!enable && mgr.stopSoftAp())) { |
Ye Jiao | 21f9625 | 2019-01-28 12:54:22 +0800 | [diff] [blame] | 640 | mWifiTetherRequested = enable; |
| 641 | return TETHER_ERROR_NO_ERROR; |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 642 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 643 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 644 | } finally { |
| 645 | Binder.restoreCallingIdentity(ident); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 646 | } |
Ye Jiao | 21f9625 | 2019-01-28 12:54:22 +0800 | [diff] [blame] | 647 | |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 648 | return TETHER_ERROR_INTERNAL_ERROR; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 649 | } |
| 650 | |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 651 | private void setBluetoothTethering(final boolean enable, final IIntResultListener listener) { |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 652 | final BluetoothAdapter adapter = mDeps.getBluetoothAdapter(); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 653 | if (adapter == null || !adapter.isEnabled()) { |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 654 | Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " |
| 655 | + (adapter == null)); |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 656 | sendTetherResult(listener, TETHER_ERROR_SERVICE_UNAVAIL, TETHERING_BLUETOOTH); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | |
| 660 | adapter.getProfileProxy(mContext, new ServiceListener() { |
| 661 | @Override |
| 662 | public void onServiceDisconnected(int profile) { } |
| 663 | |
| 664 | @Override |
| 665 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
markchien | 560c3ac | 2019-08-16 15:33:28 +0800 | [diff] [blame] | 666 | // Clear identify is fine because caller already pass tethering permission at |
| 667 | // ConnectivityService#startTethering()(or stopTethering) before the control comes |
| 668 | // here. Bluetooth will check tethering permission again that there is |
| 669 | // Context#getOpPackageName() under BluetoothPan#setBluetoothTethering() to get |
| 670 | // caller's package name for permission check. |
| 671 | // Calling BluetoothPan#setBluetoothTethering() here means the package name always |
| 672 | // be system server. If calling identity is not cleared, that package's uid might |
| 673 | // not match calling uid and end up in permission denied. |
| 674 | final long identityToken = Binder.clearCallingIdentity(); |
| 675 | try { |
| 676 | ((BluetoothPan) proxy).setBluetoothTethering(enable); |
| 677 | } finally { |
| 678 | Binder.restoreCallingIdentity(identityToken); |
| 679 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 680 | // TODO: Enabling bluetooth tethering can fail asynchronously here. |
| 681 | // We should figure out a way to bubble up that failure instead of sending success. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 682 | final int result = (((BluetoothPan) proxy).isTetheringOn() == enable) |
| 683 | ? TETHER_ERROR_NO_ERROR |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 684 | : TETHER_ERROR_INTERNAL_ERROR; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 685 | sendTetherResult(listener, result, TETHERING_BLUETOOTH); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 686 | adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); |
| 687 | } |
| 688 | }, BluetoothProfile.PAN); |
| 689 | } |
| 690 | |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 691 | private int setEthernetTethering(final boolean enable) { |
| 692 | final EthernetManager em = (EthernetManager) mContext.getSystemService( |
| 693 | Context.ETHERNET_SERVICE); |
| 694 | synchronized (mPublicSync) { |
| 695 | if (enable) { |
Treehugger Robot | 191a03c | 2020-03-24 15:23:51 +0000 | [diff] [blame] | 696 | if (mEthernetCallback != null) { |
| 697 | Log.d(TAG, "Ethernet tethering already started"); |
| 698 | return TETHER_ERROR_NO_ERROR; |
| 699 | } |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 700 | |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 701 | mEthernetCallback = new EthernetCallback(); |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 702 | mEthernetIfaceRequest = em.requestTetheredInterface(mExecutor, mEthernetCallback); |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 703 | } else { |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 704 | stopEthernetTetheringLocked(); |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | return TETHER_ERROR_NO_ERROR; |
| 708 | } |
| 709 | |
| 710 | private void stopEthernetTetheringLocked() { |
Treehugger Robot | 41c8124 | 2020-03-19 15:23:45 +0000 | [diff] [blame] | 711 | if (mConfiguredEthernetIface != null) { |
| 712 | stopTrackingInterfaceLocked(mConfiguredEthernetIface); |
| 713 | mConfiguredEthernetIface = null; |
| 714 | } |
| 715 | if (mEthernetCallback != null) { |
| 716 | mEthernetIfaceRequest.release(); |
| 717 | mEthernetCallback = null; |
| 718 | mEthernetIfaceRequest = null; |
| 719 | } |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | private class EthernetCallback implements EthernetManager.TetheredInterfaceCallback { |
| 723 | @Override |
| 724 | public void onAvailable(String iface) { |
| 725 | synchronized (mPublicSync) { |
| 726 | if (this != mEthernetCallback) { |
| 727 | // Ethernet callback arrived after Ethernet tethering stopped. Ignore. |
| 728 | return; |
| 729 | } |
| 730 | maybeTrackNewInterfaceLocked(iface, TETHERING_ETHERNET); |
| 731 | changeInterfaceState(iface, IpServer.STATE_TETHERED); |
| 732 | mConfiguredEthernetIface = iface; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public void onUnavailable() { |
| 738 | synchronized (mPublicSync) { |
| 739 | if (this != mEthernetCallback) { |
| 740 | // onAvailable called after stopping Ethernet tethering. |
| 741 | return; |
| 742 | } |
| 743 | stopEthernetTetheringLocked(); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 748 | int tether(String iface) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 749 | return tether(iface, IpServer.STATE_TETHERED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | private int tether(String iface, int requestedState) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 753 | if (DBG) Log.d(TAG, "Tethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 754 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 755 | TetherState tetherState = mTetherStates.get(iface); |
| 756 | if (tetherState == null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 757 | Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 758 | return TETHER_ERROR_UNKNOWN_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 759 | } |
| 760 | // Ignore the error status of the interface. If the interface is available, |
| 761 | // the errors are referring to past tethering attempts anyway. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 762 | if (tetherState.lastState != IpServer.STATE_AVAILABLE) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 763 | Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 764 | return TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 765 | } |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 766 | // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's queue but not yet |
| 767 | // processed, this will be a no-op and it will not return an error. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 768 | // |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 769 | // This code cannot race with untether() because they both synchronize on mPublicSync. |
| 770 | // TODO: reexamine the threading and messaging model to totally remove mPublicSync. |
| 771 | final int type = tetherState.ipServer.interfaceType(); |
| 772 | final TetheringRequestParcel request = mActiveTetheringRequests.get(type, null); |
| 773 | if (request != null) { |
| 774 | mActiveTetheringRequests.delete(type); |
| 775 | } |
| 776 | tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_REQUESTED, requestedState, 0, |
| 777 | request); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 778 | return TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 779 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 780 | } |
| 781 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 782 | int untether(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 783 | if (DBG) Log.d(TAG, "Untethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 784 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 785 | TetherState tetherState = mTetherStates.get(iface); |
| 786 | if (tetherState == null) { |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 787 | Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 788 | return TETHER_ERROR_UNKNOWN_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 789 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 790 | if (!tetherState.isCurrentlyServing()) { |
| 791 | Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 792 | return TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 793 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 794 | tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_UNREQUESTED); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 795 | return TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 796 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 797 | } |
| 798 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 799 | void untetherAll() { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 800 | stopTethering(TETHERING_WIFI); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 801 | stopTethering(TETHERING_WIFI_P2P); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 802 | stopTethering(TETHERING_USB); |
| 803 | stopTethering(TETHERING_BLUETOOTH); |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 804 | stopTethering(TETHERING_ETHERNET); |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 805 | } |
| 806 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 807 | int getLastTetherError(String iface) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 808 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 809 | TetherState tetherState = mTetherStates.get(iface); |
| 810 | if (tetherState == null) { |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 811 | Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface |
| 812 | + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 813 | return TETHER_ERROR_UNKNOWN_IFACE; |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 814 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 815 | return tetherState.lastError; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 816 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 817 | } |
| 818 | |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 819 | private boolean isProvisioningNeededButUnavailable() { |
| 820 | return isTetherProvisioningRequired() && !doesEntitlementPackageExist(); |
| 821 | } |
| 822 | |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 823 | boolean isTetherProvisioningRequired() { |
| 824 | final TetheringConfiguration cfg = mConfig; |
| 825 | return mEntitlementMgr.isTetherProvisioningRequired(cfg); |
| 826 | } |
| 827 | |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 828 | private boolean doesEntitlementPackageExist() { |
| 829 | // provisioningApp must contain package and class name. |
| 830 | if (mConfig.provisioningApp.length != 2) { |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | final PackageManager pm = mContext.getPackageManager(); |
| 835 | try { |
| 836 | pm.getPackageInfo(mConfig.provisioningApp[0], GET_ACTIVITIES); |
| 837 | } catch (PackageManager.NameNotFoundException e) { |
| 838 | return false; |
| 839 | } |
| 840 | return true; |
| 841 | } |
| 842 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 843 | // TODO: Figure out how to update for local hotspot mode interfaces. |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 844 | private void sendTetherStateChangedBroadcast() { |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 845 | if (!isTetheringSupported()) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 846 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 847 | final ArrayList<String> availableList = new ArrayList<>(); |
| 848 | final ArrayList<String> tetherList = new ArrayList<>(); |
| 849 | final ArrayList<String> localOnlyList = new ArrayList<>(); |
| 850 | final ArrayList<String> erroredList = new ArrayList<>(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 851 | final ArrayList<Integer> lastErrorList = new ArrayList<>(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 852 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 853 | final TetheringConfiguration cfg = mConfig; |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 854 | mTetherStatesParcel = new TetherStatesParcel(); |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 855 | |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 856 | int downstreamTypesMask = DOWNSTREAM_NONE; |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 857 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 858 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 859 | TetherState tetherState = mTetherStates.valueAt(i); |
| 860 | String iface = mTetherStates.keyAt(i); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 861 | if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 862 | erroredList.add(iface); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 863 | lastErrorList.add(tetherState.lastError); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 864 | } else if (tetherState.lastState == IpServer.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 865 | availableList.add(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 866 | } else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 867 | localOnlyList.add(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 868 | } else if (tetherState.lastState == IpServer.STATE_TETHERED) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 869 | if (cfg.isUsb(iface)) { |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 870 | downstreamTypesMask |= (1 << TETHERING_USB); |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 871 | } else if (cfg.isWifi(iface)) { |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 872 | downstreamTypesMask |= (1 << TETHERING_WIFI); |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 873 | } else if (cfg.isBluetooth(iface)) { |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 874 | downstreamTypesMask |= (1 << TETHERING_BLUETOOTH); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 875 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 876 | tetherList.add(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 880 | |
| 881 | mTetherStatesParcel.availableList = availableList.toArray(new String[0]); |
| 882 | mTetherStatesParcel.tetheredList = tetherList.toArray(new String[0]); |
| 883 | mTetherStatesParcel.localOnlyList = localOnlyList.toArray(new String[0]); |
| 884 | mTetherStatesParcel.erroredIfaceList = erroredList.toArray(new String[0]); |
| 885 | mTetherStatesParcel.lastErrorList = new int[lastErrorList.size()]; |
| 886 | Iterator<Integer> iterator = lastErrorList.iterator(); |
| 887 | for (int i = 0; i < lastErrorList.size(); i++) { |
| 888 | mTetherStatesParcel.lastErrorList[i] = iterator.next().intValue(); |
| 889 | } |
| 890 | reportTetherStateChanged(mTetherStatesParcel); |
| 891 | |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 892 | final Intent bcast = new Intent(ACTION_TETHER_STATE_CHANGED); |
markchien | 4cbc1a9 | 2020-01-06 21:08:19 +0800 | [diff] [blame] | 893 | bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 894 | bcast.putStringArrayListExtra(EXTRA_AVAILABLE_TETHER, availableList); |
| 895 | bcast.putStringArrayListExtra(EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); |
| 896 | bcast.putStringArrayListExtra(EXTRA_ACTIVE_TETHER, tetherList); |
| 897 | bcast.putStringArrayListExtra(EXTRA_ERRORED_TETHER, erroredList); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 898 | mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL); |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 899 | if (DBG) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 900 | Log.d(TAG, String.format( |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 901 | "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]", |
| 902 | "avail", TextUtils.join(",", availableList), |
| 903 | "local_only", TextUtils.join(",", localOnlyList), |
| 904 | "tether", TextUtils.join(",", tetherList), |
| 905 | "error", TextUtils.join(",", erroredList))); |
Robert Greenwalt | 924cc94 | 2010-06-28 10:26:19 -0700 | [diff] [blame] | 906 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 907 | |
Automerger Merge Worker | 2d7bacc | 2020-03-09 08:54:00 +0000 | [diff] [blame] | 908 | mNotificationUpdater.onDownstreamChanged(downstreamTypesMask); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 911 | private class StateReceiver extends BroadcastReceiver { |
Nick Kralevich | 70c117a | 2014-05-27 15:30:02 -0700 | [diff] [blame] | 912 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 913 | public void onReceive(Context content, Intent intent) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 914 | final String action = intent.getAction(); |
| 915 | if (action == null) return; |
| 916 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 917 | if (action.equals(UsbManager.ACTION_USB_STATE)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 918 | handleUsbAction(intent); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 919 | } else if (action.equals(CONNECTIVITY_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 920 | handleConnectivityAction(intent); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 921 | } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 922 | handleWifiApAction(intent); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 923 | } else if (action.equals(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) { |
| 924 | handleWifiP2pAction(intent); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 925 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 926 | mLog.log("OBSERVED configuration changed"); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 927 | updateConfiguration(); |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 928 | } else if (action.equals(UserManager.ACTION_USER_RESTRICTIONS_CHANGED)) { |
| 929 | mLog.log("OBSERVED user restrictions changed"); |
| 930 | handleUserRestrictionAction(); |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 931 | } else if (action.equals(ACTION_RESTRICT_BACKGROUND_CHANGED)) { |
| 932 | mLog.log("OBSERVED data saver changed"); |
| 933 | handleDataSaverChanged(); |
Paul Hu | 77fa8d6 | 2020-04-16 02:54:37 +0000 | [diff] [blame] | 934 | } else if (action.equals(TetheringNotificationUpdater.ACTION_DISABLE_TETHERING)) { |
| 935 | untetherAll(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 936 | } |
| 937 | } |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 938 | |
| 939 | private void handleConnectivityAction(Intent intent) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 940 | final NetworkInfo networkInfo = |
| 941 | (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 942 | if (networkInfo == null |
| 943 | || networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | |
| 947 | if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString()); |
| 948 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); |
| 949 | } |
| 950 | |
| 951 | private void handleUsbAction(Intent intent) { |
| 952 | final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 953 | final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 954 | final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false); |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 955 | final boolean ncmEnabled = intent.getBooleanExtra(USB_FUNCTION_NCM, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 956 | |
| 957 | mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s", |
| 958 | usbConnected, usbConfigured, rndisEnabled)); |
| 959 | |
| 960 | // There are three types of ACTION_USB_STATE: |
| 961 | // |
| 962 | // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0) |
| 963 | // Meaning: USB connection has ended either because of |
| 964 | // software reset or hard unplug. |
| 965 | // |
| 966 | // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0) |
| 967 | // Meaning: the first stage of USB protocol handshake has |
| 968 | // occurred but it is not complete. |
| 969 | // |
| 970 | // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1) |
| 971 | // Meaning: the USB handshake is completely done and all the |
| 972 | // functions are ready to use. |
| 973 | // |
| 974 | // For more explanation, see b/62552150 . |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 975 | synchronized (Tethering.this.mPublicSync) { |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 976 | if (!usbConnected && mRndisEnabled) { |
| 977 | // Turn off tethering if it was enabled and there is a disconnect. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 978 | tetherMatchingInterfaces(IpServer.STATE_AVAILABLE, TETHERING_USB); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 979 | mEntitlementMgr.stopProvisioningIfNeeded(TETHERING_USB); |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 980 | } else if (usbConfigured && rndisEnabled) { |
| 981 | // Tether if rndis is enabled and usb is configured. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 982 | tetherMatchingInterfaces(IpServer.STATE_TETHERED, TETHERING_USB); |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 983 | } else if (usbConnected && ncmEnabled) { |
| 984 | tetherMatchingInterfaces(IpServer.STATE_LOCAL_ONLY, TETHERING_NCM); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 985 | } |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 986 | mRndisEnabled = usbConfigured && rndisEnabled; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | private void handleWifiApAction(Intent intent) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 991 | final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); |
| 992 | final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME); |
| 993 | final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED); |
| 994 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 995 | synchronized (Tethering.this.mPublicSync) { |
| 996 | switch (curState) { |
| 997 | case WifiManager.WIFI_AP_STATE_ENABLING: |
| 998 | // We can see this state on the way to both enabled and failure states. |
| 999 | break; |
| 1000 | case WifiManager.WIFI_AP_STATE_ENABLED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1001 | enableWifiIpServingLocked(ifname, ipmode); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 1002 | break; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 1003 | case WifiManager.WIFI_AP_STATE_DISABLING: |
Mark Chien | 19487bf | 2020-04-15 17:28:27 +0000 | [diff] [blame] | 1004 | // We can see this state on the way to disabled. |
| 1005 | break; |
| 1006 | case WifiManager.WIFI_AP_STATE_DISABLED: |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 1007 | case WifiManager.WIFI_AP_STATE_FAILED: |
| 1008 | default: |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1009 | disableWifiIpServingLocked(ifname, curState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1010 | break; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1014 | |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 1015 | private boolean isGroupOwner(WifiP2pGroup group) { |
| 1016 | return group != null && group.isGroupOwner() |
| 1017 | && !TextUtils.isEmpty(group.getInterface()); |
| 1018 | } |
| 1019 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1020 | private void handleWifiP2pAction(Intent intent) { |
| 1021 | if (mConfig.isWifiP2pLegacyTetheringMode()) return; |
| 1022 | |
| 1023 | final WifiP2pInfo p2pInfo = |
| 1024 | (WifiP2pInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO); |
| 1025 | final WifiP2pGroup group = |
| 1026 | (WifiP2pGroup) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); |
| 1027 | |
| 1028 | if (VDBG) { |
| 1029 | Log.d(TAG, "WifiP2pAction: P2pInfo: " + p2pInfo + " Group: " + group); |
| 1030 | } |
| 1031 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1032 | synchronized (Tethering.this.mPublicSync) { |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 1033 | // if no group is formed, bring it down if needed. |
| 1034 | if (p2pInfo == null || !p2pInfo.groupFormed) { |
| 1035 | disableWifiP2pIpServingLockedIfNeeded(mWifiP2pTetherInterface); |
| 1036 | mWifiP2pTetherInterface = null; |
| 1037 | return; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1038 | } |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 1039 | |
| 1040 | // If there is a group but the device is not the owner, bail out. |
| 1041 | if (!isGroupOwner(group)) return; |
| 1042 | |
| 1043 | // If already serving from the correct interface, nothing to do. |
| 1044 | if (group.getInterface().equals(mWifiP2pTetherInterface)) return; |
| 1045 | |
| 1046 | // If already serving from another interface, turn it down first. |
| 1047 | if (!TextUtils.isEmpty(mWifiP2pTetherInterface)) { |
| 1048 | mLog.w("P2P tethered interface " + mWifiP2pTetherInterface |
| 1049 | + "is different from current interface " |
| 1050 | + group.getInterface() + ", re-tether it"); |
| 1051 | disableWifiP2pIpServingLockedIfNeeded(mWifiP2pTetherInterface); |
| 1052 | } |
| 1053 | |
| 1054 | // Finally bring up serving on the new interface |
| 1055 | mWifiP2pTetherInterface = group.getInterface(); |
| 1056 | enableWifiIpServingLocked(mWifiP2pTetherInterface, IFACE_IP_MODE_LOCAL_ONLY); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1059 | |
| 1060 | private void handleUserRestrictionAction() { |
| 1061 | mTetheringRestriction.onUserRestrictionsChanged(); |
| 1062 | } |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 1063 | |
| 1064 | private void handleDataSaverChanged() { |
| 1065 | final ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService( |
| 1066 | Context.CONNECTIVITY_SERVICE); |
| 1067 | final boolean isDataSaverEnabled = connMgr.getRestrictBackgroundStatus() |
| 1068 | != ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 1069 | |
| 1070 | if (mDataSaverEnabled == isDataSaverEnabled) return; |
| 1071 | |
| 1072 | mDataSaverEnabled = isDataSaverEnabled; |
| 1073 | if (mDataSaverEnabled) { |
| 1074 | untetherAll(); |
| 1075 | } |
| 1076 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1079 | @VisibleForTesting |
Paul Hu | 4089bb1 | 2020-04-23 09:07:00 +0000 | [diff] [blame] | 1080 | boolean isTetheringActive() { |
| 1081 | return mActiveTetheringRequests.size() > 0; |
| 1082 | } |
| 1083 | |
| 1084 | @VisibleForTesting |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1085 | protected static class UserRestrictionActionListener { |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 1086 | private final UserManager mUserMgr; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1087 | private final Tethering mWrapper; |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1088 | private final TetheringNotificationUpdater mNotificationUpdater; |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1089 | public boolean mDisallowTethering; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1090 | |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1091 | public UserRestrictionActionListener(@NonNull UserManager um, @NonNull Tethering wrapper, |
| 1092 | @NonNull TetheringNotificationUpdater updater) { |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 1093 | mUserMgr = um; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1094 | mWrapper = wrapper; |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1095 | mNotificationUpdater = updater; |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1096 | mDisallowTethering = false; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1099 | public void onUserRestrictionsChanged() { |
| 1100 | // getUserRestrictions gets restriction for this process' user, which is the primary |
| 1101 | // user. This is fine because DISALLOW_CONFIG_TETHERING can only be set on the primary |
| 1102 | // user. See UserManager.DISALLOW_CONFIG_TETHERING. |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 1103 | final Bundle restrictions = mUserMgr.getUserRestrictions(); |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1104 | final boolean newlyDisallowed = |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1105 | restrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING); |
| 1106 | final boolean prevDisallowed = mDisallowTethering; |
| 1107 | mDisallowTethering = newlyDisallowed; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1108 | |
markchien | 1ddfba4 | 2019-11-27 21:20:33 +0800 | [diff] [blame] | 1109 | final boolean tetheringDisallowedChanged = (newlyDisallowed != prevDisallowed); |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1110 | if (!tetheringDisallowedChanged) { |
| 1111 | return; |
| 1112 | } |
| 1113 | |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1114 | if (!newlyDisallowed) { |
| 1115 | // Clear the restricted notification when user is allowed to have tethering |
| 1116 | // function. |
| 1117 | mNotificationUpdater.tetheringRestrictionLifted(); |
| 1118 | return; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1119 | } |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1120 | |
Paul Hu | 4089bb1 | 2020-04-23 09:07:00 +0000 | [diff] [blame] | 1121 | if (mWrapper.isTetheringActive()) { |
| 1122 | // Restricted notification is shown when tethering function is disallowed on |
| 1123 | // user's device. |
| 1124 | mNotificationUpdater.notifyTetheringDisabledByRestriction(); |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1125 | |
Paul Hu | 4089bb1 | 2020-04-23 09:07:00 +0000 | [diff] [blame] | 1126 | // Untether from all downstreams since tethering is disallowed. |
| 1127 | mWrapper.untetherAll(); |
| 1128 | } |
Paul Hu | f950dc9 | 2020-03-25 12:39:13 +0000 | [diff] [blame] | 1129 | // TODO(b/148139325): send tetheringSupported on restriction change |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1133 | private void disableWifiIpServingLockedCommon(int tetheringType, String ifname, int apState) { |
| 1134 | mLog.log("Canceling WiFi tethering request -" |
| 1135 | + " type=" + tetheringType |
| 1136 | + " interface=" + ifname |
| 1137 | + " state=" + apState); |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1138 | |
| 1139 | if (!TextUtils.isEmpty(ifname)) { |
| 1140 | final TetherState ts = mTetherStates.get(ifname); |
| 1141 | if (ts != null) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1142 | ts.ipServer.unwanted(); |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1143 | return; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1147 | for (int i = 0; i < mTetherStates.size(); i++) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1148 | final IpServer ipServer = mTetherStates.valueAt(i).ipServer; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1149 | if (ipServer.interfaceType() == tetheringType) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1150 | ipServer.unwanted(); |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1151 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1152 | } |
| 1153 | } |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1154 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1155 | mLog.log("Error disabling Wi-Fi IP serving; " |
| 1156 | + (TextUtils.isEmpty(ifname) ? "no interface name specified" |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 1157 | : "specified interface: " + ifname)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1158 | } |
| 1159 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1160 | private void disableWifiIpServingLocked(String ifname, int apState) { |
| 1161 | // Regardless of whether we requested this transition, the AP has gone |
| 1162 | // down. Don't try to tether again unless we're requested to do so. |
| 1163 | // TODO: Remove this altogether, once Wi-Fi reliably gives us an |
| 1164 | // interface name with every broadcast. |
| 1165 | mWifiTetherRequested = false; |
| 1166 | |
| 1167 | disableWifiIpServingLockedCommon(TETHERING_WIFI, ifname, apState); |
| 1168 | } |
| 1169 | |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 1170 | private void disableWifiP2pIpServingLockedIfNeeded(String ifname) { |
| 1171 | if (TextUtils.isEmpty(ifname)) return; |
| 1172 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1173 | disableWifiIpServingLockedCommon(TETHERING_WIFI_P2P, ifname, /* dummy */ 0); |
| 1174 | } |
| 1175 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1176 | private void enableWifiIpServingLocked(String ifname, int wifiIpMode) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1177 | // Map wifiIpMode values to IpServer.Callback serving states, inferring |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1178 | // from mWifiTetherRequested as a final "best guess". |
| 1179 | final int ipServingMode; |
| 1180 | switch (wifiIpMode) { |
| 1181 | case IFACE_IP_MODE_TETHERED: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1182 | ipServingMode = IpServer.STATE_TETHERED; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1183 | break; |
| 1184 | case IFACE_IP_MODE_LOCAL_ONLY: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1185 | ipServingMode = IpServer.STATE_LOCAL_ONLY; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1186 | break; |
| 1187 | default: |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 1188 | mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode); |
| 1189 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | if (!TextUtils.isEmpty(ifname)) { |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1193 | maybeTrackNewInterfaceLocked(ifname); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1194 | changeInterfaceState(ifname, ipServingMode); |
| 1195 | } else { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 1196 | mLog.e(String.format( |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1197 | "Cannot enable IP serving in mode %s on missing interface name", |
| 1198 | ipServingMode)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1202 | // TODO: Consider renaming to something more accurate in its description. |
| 1203 | // This method: |
| 1204 | // - allows requesting either tethering or local hotspot serving states |
| 1205 | // - handles both enabling and disabling serving states |
| 1206 | // - only tethers the first matching interface in listInterfaces() |
| 1207 | // order of a given type |
| 1208 | private void tetherMatchingInterfaces(int requestedState, int interfaceType) { |
| 1209 | if (VDBG) { |
| 1210 | Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")"); |
| 1211 | } |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1212 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1213 | String[] ifaces = null; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1214 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1215 | ifaces = mNetd.interfaceGetList(); |
| 1216 | } catch (RemoteException | ServiceSpecificException e) { |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1217 | Log.e(TAG, "Error listing Interfaces", e); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1218 | return; |
| 1219 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1220 | String chosenIface = null; |
| 1221 | if (ifaces != null) { |
| 1222 | for (String iface : ifaces) { |
| 1223 | if (ifaceNameToType(iface) == interfaceType) { |
| 1224 | chosenIface = iface; |
| 1225 | break; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1229 | if (chosenIface == null) { |
| 1230 | Log.e(TAG, "could not find iface of type " + interfaceType); |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1234 | changeInterfaceState(chosenIface, requestedState); |
| 1235 | } |
| 1236 | |
| 1237 | private void changeInterfaceState(String ifname, int requestedState) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1238 | final int result; |
| 1239 | switch (requestedState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1240 | case IpServer.STATE_UNAVAILABLE: |
| 1241 | case IpServer.STATE_AVAILABLE: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1242 | result = untether(ifname); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1243 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1244 | case IpServer.STATE_TETHERED: |
| 1245 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1246 | result = tether(ifname, requestedState); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1247 | break; |
| 1248 | default: |
| 1249 | Log.wtf(TAG, "Unknown interface state: " + requestedState); |
| 1250 | return; |
| 1251 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1252 | if (result != TETHER_ERROR_NO_ERROR) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1253 | Log.e(TAG, "unable start or stop tethering on iface " + ifname); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1254 | return; |
| 1255 | } |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1258 | TetheringConfiguration getTetheringConfiguration() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1259 | return mConfig; |
| 1260 | } |
| 1261 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1262 | boolean hasTetherableConfiguration() { |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 1263 | final TetheringConfiguration cfg = mConfig; |
| 1264 | final boolean hasDownstreamConfiguration = |
markchien | 1be8d8f | 2018-12-05 21:20:01 +0800 | [diff] [blame] | 1265 | (cfg.tetherableUsbRegexs.length != 0) |
| 1266 | || (cfg.tetherableWifiRegexs.length != 0) |
| 1267 | || (cfg.tetherableBluetoothRegexs.length != 0); |
| 1268 | final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty() |
| 1269 | || cfg.chooseUpstreamAutomatically; |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 1270 | |
| 1271 | return hasDownstreamConfiguration && hasUpstreamConfiguration; |
| 1272 | } |
| 1273 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1274 | // TODO - update callers to use getTetheringConfiguration(), |
| 1275 | // which has only final members. |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1276 | String[] getTetherableUsbRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1277 | return copy(mConfig.tetherableUsbRegexs); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1280 | String[] getTetherableWifiRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1281 | return copy(mConfig.tetherableWifiRegexs); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1284 | String[] getTetherableBluetoothRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1285 | return copy(mConfig.tetherableBluetoothRegexs); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1288 | int setUsbTethering(boolean enable) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1289 | if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 1290 | UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
Ye Jiao | 21f9625 | 2019-01-28 12:54:22 +0800 | [diff] [blame] | 1291 | if (usbManager == null) { |
| 1292 | mLog.e("setUsbTethering: failed to get UsbManager!"); |
| 1293 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1294 | } |
| 1295 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1296 | synchronized (mPublicSync) { |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 1297 | usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_RNDIS |
| 1298 | : UsbManager.FUNCTION_NONE); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1299 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1300 | return TETHER_ERROR_NO_ERROR; |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Milim Lee | 31ef4c0 | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 1303 | private int setNcmTethering(boolean enable) { |
| 1304 | if (VDBG) Log.d(TAG, "setNcmTethering(" + enable + ")"); |
| 1305 | UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
| 1306 | synchronized (mPublicSync) { |
| 1307 | usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_NCM |
| 1308 | : UsbManager.FUNCTION_NONE); |
| 1309 | } |
| 1310 | return TETHER_ERROR_NO_ERROR; |
| 1311 | } |
| 1312 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1313 | // TODO review API - figure out how to delete these entirely. |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1314 | String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1315 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1316 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1317 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1318 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1319 | if (tetherState.lastState == IpServer.STATE_TETHERED) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1320 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1321 | } |
| 1322 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1323 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1324 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1327 | String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1328 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1329 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1330 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1331 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1332 | if (tetherState.lastState == IpServer.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1333 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1337 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1340 | String[] getTetheredDhcpRanges() { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1341 | // TODO: this is only valid for the old DHCP server. Latest search suggests it is only used |
| 1342 | // by WifiP2pServiceImpl to start dnsmasq: remove/deprecate after migrating callers. |
| 1343 | return mConfig.legacyDhcpRanges; |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1346 | String[] getErroredIfaces() { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1347 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1348 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1349 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1350 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1351 | if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1352 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1356 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1359 | private void logMessage(State state, int what) { |
| 1360 | mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what))); |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1361 | } |
| 1362 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1363 | private boolean upstreamWanted() { |
| 1364 | if (!mForwardedDownstreams.isEmpty()) return true; |
| 1365 | |
| 1366 | synchronized (mPublicSync) { |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 1367 | return mWifiTetherRequested; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1368 | } |
| 1369 | } |
| 1370 | |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1371 | // Needed because the canonical source of upstream truth is just the |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1372 | // upstream interface set, |mCurrentUpstreamIfaceSet|. |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1373 | private boolean pertainsToCurrentUpstream(UpstreamNetworkState ns) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1374 | if (ns != null && ns.linkProperties != null && mCurrentUpstreamIfaceSet != null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1375 | for (String ifname : ns.linkProperties.getAllInterfaceNames()) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1376 | if (mCurrentUpstreamIfaceSet.ifnames.contains(ifname)) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1377 | return true; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1378 | } |
| 1379 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1380 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1381 | return false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1382 | } |
| 1383 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1384 | class TetherMasterSM extends StateMachine { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1385 | // an interface SM has requested Tethering/Local Hotspot |
| 1386 | static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1; |
| 1387 | // an interface SM has unrequested Tethering/Local Hotspot |
| 1388 | static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1389 | // upstream connection change - do the right thing |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1390 | static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1391 | // we don't have a valid upstream conn, check again after a delay |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1392 | static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1393 | // Events from NetworkCallbacks that we process on the master state |
| 1394 | // machine thread on behalf of the UpstreamNetworkMonitor. |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1395 | static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1396 | // we treated the error and want now to clear it |
| 1397 | static final int CMD_CLEAR_ERROR = BASE_MASTER + 6; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1398 | static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 1399 | // Events from EntitlementManager to choose upstream again. |
| 1400 | static final int EVENT_UPSTREAM_PERMISSION_CHANGED = BASE_MASTER + 8; |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1401 | private final State mInitialState; |
| 1402 | private final State mTetherModeAliveState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1403 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1404 | private final State mSetIpForwardingEnabledErrorState; |
| 1405 | private final State mSetIpForwardingDisabledErrorState; |
| 1406 | private final State mStartTetheringErrorState; |
| 1407 | private final State mStopTetheringErrorState; |
| 1408 | private final State mSetDnsForwardersErrorState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1409 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1410 | // This list is a little subtle. It contains all the interfaces that currently are |
| 1411 | // requesting tethering, regardless of whether these interfaces are still members of |
| 1412 | // mTetherStates. This allows us to maintain the following predicates: |
| 1413 | // |
| 1414 | // 1) mTetherStates contains the set of all currently existing, tetherable, link state up |
| 1415 | // interfaces. |
| 1416 | // 2) mNotifyList contains all state machines that may have outstanding tethering state |
| 1417 | // that needs to be torn down. |
| 1418 | // |
| 1419 | // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList |
| 1420 | // so that the garbage collector does not clean up the state machine before it has a chance |
| 1421 | // to tear itself down. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1422 | private final ArrayList<IpServer> mNotifyList; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1423 | private final IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1424 | private final OffloadWrapper mOffload; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1425 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1426 | private static final int UPSTREAM_SETTLE_TIME_MS = 10000; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1427 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 1428 | TetherMasterSM(String name, Looper looper, TetheringDependencies deps) { |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 1429 | super(name, looper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1430 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1431 | mInitialState = new InitialState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1432 | mTetherModeAliveState = new TetherModeAliveState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1433 | mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1434 | mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1435 | mStartTetheringErrorState = new StartTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1436 | mStopTetheringErrorState = new StopTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1437 | mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1438 | |
| 1439 | addState(mInitialState); |
| 1440 | addState(mTetherModeAliveState); |
| 1441 | addState(mSetIpForwardingEnabledErrorState); |
| 1442 | addState(mSetIpForwardingDisabledErrorState); |
| 1443 | addState(mStartTetheringErrorState); |
| 1444 | addState(mStopTetheringErrorState); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1445 | addState(mSetDnsForwardersErrorState); |
| 1446 | |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1447 | mNotifyList = new ArrayList<>(); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 1448 | mIPv6TetheringCoordinator = deps.getIPv6TetheringCoordinator(mNotifyList, mLog); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1449 | mOffload = new OffloadWrapper(); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1450 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1451 | setInitialState(mInitialState); |
| 1452 | } |
| 1453 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1454 | class InitialState extends State { |
| 1455 | @Override |
| 1456 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1457 | logMessage(this, message.what); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1458 | switch (message.what) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1459 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
| 1460 | final IpServer who = (IpServer) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1461 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
| 1462 | handleInterfaceServingStateActive(message.arg1, who); |
| 1463 | transitionTo(mTetherModeAliveState); |
| 1464 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1465 | } |
| 1466 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
| 1467 | final IpServer who = (IpServer) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1468 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
| 1469 | handleInterfaceServingStateInactive(who); |
| 1470 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1471 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1472 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: |
| 1473 | // Silently ignore these for now. |
| 1474 | break; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1475 | default: |
| 1476 | return NOT_HANDLED; |
| 1477 | } |
| 1478 | return HANDLED; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1482 | protected boolean turnOnMasterTetherSettings() { |
| 1483 | final TetheringConfiguration cfg = mConfig; |
| 1484 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1485 | mNetd.ipfwdEnableForwarding(TAG); |
| 1486 | } catch (RemoteException | ServiceSpecificException e) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1487 | mLog.e(e); |
| 1488 | transitionTo(mSetIpForwardingEnabledErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1489 | return false; |
| 1490 | } |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1491 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1492 | // TODO: Randomize DHCPv4 ranges, especially in hotspot mode. |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1493 | // Legacy DHCP server is disabled if passed an empty ranges array |
| 1494 | final String[] dhcpRanges = cfg.enableLegacyDhcpServer |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1495 | ? cfg.legacyDhcpRanges : new String[0]; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1496 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1497 | NetdUtils.tetherStart(mNetd, true /** usingLegacyDnsProxy */, dhcpRanges); |
| 1498 | } catch (RemoteException | ServiceSpecificException e) { |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1499 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1500 | // Stop and retry. |
| 1501 | mNetd.tetherStop(); |
| 1502 | NetdUtils.tetherStart(mNetd, true /** usingLegacyDnsProxy */, dhcpRanges); |
| 1503 | } catch (RemoteException | ServiceSpecificException ee) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1504 | mLog.e(ee); |
| 1505 | transitionTo(mStartTetheringErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1506 | return false; |
| 1507 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1508 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1509 | mLog.log("SET master tether settings: ON"); |
| 1510 | return true; |
| 1511 | } |
Robert Greenwalt | 1039872 | 2010-12-17 15:20:36 -0800 | [diff] [blame] | 1512 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1513 | protected boolean turnOffMasterTetherSettings() { |
| 1514 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1515 | mNetd.tetherStop(); |
| 1516 | } catch (RemoteException | ServiceSpecificException e) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1517 | mLog.e(e); |
| 1518 | transitionTo(mStopTetheringErrorState); |
| 1519 | return false; |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1520 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1521 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1522 | mNetd.ipfwdDisableForwarding(TAG); |
| 1523 | } catch (RemoteException | ServiceSpecificException e) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1524 | mLog.e(e); |
| 1525 | transitionTo(mSetIpForwardingDisabledErrorState); |
| 1526 | return false; |
| 1527 | } |
| 1528 | transitionTo(mInitialState); |
| 1529 | mLog.log("SET master tether settings: OFF"); |
| 1530 | return true; |
| 1531 | } |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1532 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1533 | protected void chooseUpstreamType(boolean tryCell) { |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 1534 | // We rebuild configuration on ACTION_CONFIGURATION_CHANGED, but we |
| 1535 | // do not currently know how to watch for changes in DUN settings. |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 1536 | maybeDunSettingChanged(); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1537 | |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1538 | final TetheringConfiguration config = mConfig; |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1539 | final UpstreamNetworkState ns = (config.chooseUpstreamAutomatically) |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1540 | ? mUpstreamNetworkMonitor.getCurrentPreferredUpstream() |
| 1541 | : mUpstreamNetworkMonitor.selectPreferredUpstreamType( |
| 1542 | config.preferredUpstreamIfaceTypes); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1543 | if (ns == null) { |
| 1544 | if (tryCell) { |
| 1545 | mUpstreamNetworkMonitor.registerMobileNetworkRequest(); |
| 1546 | // We think mobile should be coming up; don't set a retry. |
| 1547 | } else { |
| 1548 | sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1549 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1550 | } |
| 1551 | setUpstreamNetwork(ns); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 1552 | final Network newUpstream = (ns != null) ? ns.network : null; |
| 1553 | if (mTetherUpstream != newUpstream) { |
| 1554 | mTetherUpstream = newUpstream; |
| 1555 | mUpstreamNetworkMonitor.setCurrentUpstream(mTetherUpstream); |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 1556 | reportUpstreamChanged(ns); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 1557 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1558 | } |
| 1559 | |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1560 | protected void setUpstreamNetwork(UpstreamNetworkState ns) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1561 | InterfaceSet ifaces = null; |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 1562 | if (ns != null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1563 | // Find the interface with the default IPv4 route. It may be the |
| 1564 | // interface described by linkProperties, or one of the interfaces |
| 1565 | // stacked on top of it. |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 1566 | mLog.i("Looking for default routes on: " + ns.linkProperties); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1567 | ifaces = TetheringInterfaceUtils.getTetheringInterfaces(ns); |
| 1568 | mLog.i("Found upstream interface(s): " + ifaces); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1569 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 1570 | |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1571 | if (ifaces != null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1572 | setDnsForwarders(ns.network, ns.linkProperties); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1573 | } |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1574 | notifyDownstreamsOfNewUpstreamIface(ifaces); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1575 | if (ns != null && pertainsToCurrentUpstream(ns)) { |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1576 | // If we already have UpstreamNetworkState for this network update it immediately. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1577 | handleNewUpstreamNetworkState(ns); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1578 | } else if (mCurrentUpstreamIfaceSet == null) { |
| 1579 | // There are no available upstream networks. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1580 | handleNewUpstreamNetworkState(null); |
| 1581 | } |
| 1582 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1583 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1584 | protected void setDnsForwarders(final Network network, final LinkProperties lp) { |
| 1585 | // TODO: Set v4 and/or v6 DNS per available connectivity. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1586 | final Collection<InetAddress> dnses = lp.getDnsServers(); |
| 1587 | // TODO: Properly support the absence of DNS servers. |
markchien | 87d3f7d | 2020-01-08 20:58:23 +0800 | [diff] [blame] | 1588 | final String[] dnsServers; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1589 | if (dnses != null && !dnses.isEmpty()) { |
markchien | 87d3f7d | 2020-01-08 20:58:23 +0800 | [diff] [blame] | 1590 | dnsServers = new String[dnses.size()]; |
| 1591 | int i = 0; |
| 1592 | for (InetAddress dns : dnses) { |
| 1593 | dnsServers[i++] = dns.getHostAddress(); |
| 1594 | } |
| 1595 | } else { |
| 1596 | dnsServers = mConfig.defaultIPv4DNS; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1597 | } |
markchien | 587ecb3 | 2020-03-18 08:43:07 +0800 | [diff] [blame] | 1598 | final int netId = (network != null) ? network.getNetId() : NETID_UNSET; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1599 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1600 | mNetd.tetherDnsSet(netId, dnsServers); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1601 | mLog.log(String.format( |
| 1602 | "SET DNS forwarders: network=%s dnsServers=%s", |
| 1603 | network, Arrays.toString(dnsServers))); |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1604 | } catch (RemoteException | ServiceSpecificException e) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1605 | // TODO: Investigate how this can fail and what exactly |
| 1606 | // happens if/when such failures occur. |
| 1607 | mLog.e("setting DNS forwarders failed, " + e); |
| 1608 | transitionTo(mSetDnsForwardersErrorState); |
| 1609 | } |
| 1610 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1611 | |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1612 | protected void notifyDownstreamsOfNewUpstreamIface(InterfaceSet ifaces) { |
| 1613 | mCurrentUpstreamIfaceSet = ifaces; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1614 | for (IpServer ipServer : mNotifyList) { |
| 1615 | ipServer.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, ifaces); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1616 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1617 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1618 | |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1619 | protected void handleNewUpstreamNetworkState(UpstreamNetworkState ns) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1620 | mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1621 | mOffload.updateUpstreamNetworkState(ns); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1622 | } |
| 1623 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1624 | private void handleInterfaceServingStateActive(int mode, IpServer who) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1625 | if (mNotifyList.indexOf(who) < 0) { |
| 1626 | mNotifyList.add(who); |
| 1627 | mIPv6TetheringCoordinator.addActiveDownstream(who, mode); |
| 1628 | } |
| 1629 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1630 | if (mode == IpServer.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1631 | // No need to notify OffloadController just yet as there are no |
| 1632 | // "offload-able" prefixes to pass along. This will handled |
| 1633 | // when the TISM informs Tethering of its LinkProperties. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1634 | mForwardedDownstreams.add(who); |
| 1635 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1636 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1637 | mForwardedDownstreams.remove(who); |
| 1638 | } |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1639 | |
| 1640 | // If this is a Wi-Fi interface, notify WifiManager of the active serving state. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1641 | if (who.interfaceType() == TETHERING_WIFI) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1642 | final WifiManager mgr = getWifiManager(); |
| 1643 | final String iface = who.interfaceName(); |
| 1644 | switch (mode) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1645 | case IpServer.STATE_TETHERED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1646 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1647 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1648 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1649 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1650 | break; |
| 1651 | default: |
| 1652 | Log.wtf(TAG, "Unknown active serving mode: " + mode); |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1656 | } |
| 1657 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1658 | private void handleInterfaceServingStateInactive(IpServer who) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1659 | mNotifyList.remove(who); |
| 1660 | mIPv6TetheringCoordinator.removeActiveDownstream(who); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1661 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1662 | mForwardedDownstreams.remove(who); |
Treehugger Robot | 2506781 | 2020-03-25 15:32:41 +0000 | [diff] [blame] | 1663 | updateConnectedClients(null /* wifiClients */); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1664 | |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 1665 | // If this is a Wi-Fi interface, tell WifiManager of any errors |
| 1666 | // or the inactive serving state. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1667 | if (who.interfaceType() == TETHERING_WIFI) { |
| 1668 | if (who.lastError() != TETHER_ERROR_NO_ERROR) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1669 | getWifiManager().updateInterfaceIpState( |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1670 | who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 1671 | } else { |
| 1672 | getWifiManager().updateInterfaceIpState( |
| 1673 | who.interfaceName(), IFACE_IP_MODE_UNSPECIFIED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1674 | } |
| 1675 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1676 | } |
| 1677 | |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 1678 | private void addUpstreamPrefixes(final UpstreamNetworkState ns) { |
| 1679 | mPrivateAddressCoordinator.updateUpstreamPrefix(ns.network, ns.linkProperties); |
| 1680 | } |
| 1681 | |
| 1682 | private void removeUpstreamPrefixes(final UpstreamNetworkState ns) { |
| 1683 | mPrivateAddressCoordinator.removeUpstreamPrefix(ns.network); |
| 1684 | } |
| 1685 | |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 1686 | @VisibleForTesting |
| 1687 | void handleUpstreamNetworkMonitorCallback(int arg1, Object o) { |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1688 | if (arg1 == UpstreamNetworkMonitor.NOTIFY_LOCAL_PREFIXES) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1689 | mOffload.sendOffloadExemptPrefixes((Set<IpPrefix>) o); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1690 | return; |
| 1691 | } |
| 1692 | |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1693 | final UpstreamNetworkState ns = (UpstreamNetworkState) o; |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 1694 | switch (arg1) { |
| 1695 | case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES: |
| 1696 | addUpstreamPrefixes(ns); |
| 1697 | break; |
| 1698 | case UpstreamNetworkMonitor.EVENT_ON_LOST: |
| 1699 | removeUpstreamPrefixes(ns); |
| 1700 | break; |
| 1701 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1702 | |
| 1703 | if (ns == null || !pertainsToCurrentUpstream(ns)) { |
| 1704 | // TODO: In future, this is where upstream evaluation and selection |
| 1705 | // could be handled for notifications which include sufficient data. |
| 1706 | // For example, after CONNECTIVITY_ACTION listening is removed, here |
| 1707 | // is where we could observe a Wi-Fi network becoming available and |
| 1708 | // passing validation. |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1709 | if (mCurrentUpstreamIfaceSet == null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1710 | // If we have no upstream interface, try to run through upstream |
| 1711 | // selection again. If, for example, IPv4 connectivity has shown up |
| 1712 | // after IPv6 (e.g., 464xlat became available) we want the chance to |
| 1713 | // notice and act accordingly. |
| 1714 | chooseUpstreamType(false); |
| 1715 | } |
| 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | switch (arg1) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1720 | case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES: |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 1721 | if (ns.network.equals(mTetherUpstream)) { |
| 1722 | mNotificationUpdater.onUpstreamCapabilitiesChanged(ns.networkCapabilities); |
| 1723 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1724 | handleNewUpstreamNetworkState(ns); |
| 1725 | break; |
| 1726 | case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES: |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1727 | chooseUpstreamType(false); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1728 | break; |
| 1729 | case UpstreamNetworkMonitor.EVENT_ON_LOST: |
| 1730 | // TODO: Re-evaluate possible upstreams. Currently upstream |
| 1731 | // reevaluation is triggered via received CONNECTIVITY_ACTION |
| 1732 | // broadcasts that result in being passed a |
| 1733 | // TetherMasterSM.CMD_UPSTREAM_CHANGED. |
| 1734 | handleNewUpstreamNetworkState(null); |
| 1735 | break; |
| 1736 | default: |
| 1737 | mLog.e("Unknown arg1 value: " + arg1); |
| 1738 | break; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | class TetherModeAliveState extends State { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1743 | boolean mUpstreamWanted = false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1744 | boolean mTryCell = true; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 1745 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1746 | @Override |
| 1747 | public void enter() { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1748 | // If turning on master tether settings fails, we have already |
| 1749 | // transitioned to an error state; exit early. |
| 1750 | if (!turnOnMasterTetherSettings()) { |
| 1751 | return; |
| 1752 | } |
| 1753 | |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 1754 | mUpstreamNetworkMonitor.startObserveAllNetworks(); |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 1755 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1756 | // TODO: De-duplicate with updateUpstreamWanted() below. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1757 | if (upstreamWanted()) { |
| 1758 | mUpstreamWanted = true; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1759 | mOffload.start(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1760 | chooseUpstreamType(true); |
| 1761 | mTryCell = false; |
| 1762 | } |
Hungming Chen | be4827d | 2020-06-02 00:13:26 +0000 | [diff] [blame] | 1763 | |
| 1764 | // TODO: Check the upstream interface if it is managed by BPF offload. |
Treehugger Robot | 9689519 | 2020-06-09 13:28:38 +0000 | [diff] [blame] | 1765 | mBpfCoordinator.startPolling(); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1766 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1767 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1768 | @Override |
| 1769 | public void exit() { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1770 | mOffload.stop(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1771 | mUpstreamNetworkMonitor.stop(); |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1772 | notifyDownstreamsOfNewUpstreamIface(null); |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1773 | handleNewUpstreamNetworkState(null); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 1774 | if (mTetherUpstream != null) { |
| 1775 | mTetherUpstream = null; |
| 1776 | reportUpstreamChanged(null); |
| 1777 | } |
Treehugger Robot | 9689519 | 2020-06-09 13:28:38 +0000 | [diff] [blame] | 1778 | mBpfCoordinator.stopPolling(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1779 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1780 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1781 | private boolean updateUpstreamWanted() { |
| 1782 | final boolean previousUpstreamWanted = mUpstreamWanted; |
| 1783 | mUpstreamWanted = upstreamWanted(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1784 | if (mUpstreamWanted != previousUpstreamWanted) { |
| 1785 | if (mUpstreamWanted) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1786 | mOffload.start(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1787 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1788 | mOffload.stop(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1789 | } |
| 1790 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1791 | return previousUpstreamWanted; |
| 1792 | } |
| 1793 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1794 | @Override |
| 1795 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1796 | logMessage(this, message.what); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1797 | boolean retValue = true; |
| 1798 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1799 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1800 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1801 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1802 | handleInterfaceServingStateActive(message.arg1, who); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1803 | who.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1804 | mCurrentUpstreamIfaceSet); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1805 | // If there has been a change and an upstream is now |
| 1806 | // desired, kick off the selection process. |
| 1807 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1808 | if (!previousUpstreamWanted && mUpstreamWanted) { |
| 1809 | chooseUpstreamType(true); |
| 1810 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1811 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1812 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1813 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1814 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1815 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1816 | handleInterfaceServingStateInactive(who); |
| 1817 | |
| 1818 | if (mNotifyList.isEmpty()) { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1819 | // This transitions us out of TetherModeAliveState, |
| 1820 | // either to InitialState or an error state. |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1821 | turnOffMasterTetherSettings(); |
| 1822 | break; |
| 1823 | } |
| 1824 | |
| 1825 | if (DBG) { |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1826 | Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() |
| 1827 | + " live requests:"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1828 | for (IpServer o : mNotifyList) { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1829 | Log.d(TAG, " " + o); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1830 | } |
| 1831 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1832 | // If there has been a change and an upstream is no |
| 1833 | // longer desired, release any mobile requests. |
| 1834 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1835 | if (previousUpstreamWanted && !mUpstreamWanted) { |
| 1836 | mUpstreamNetworkMonitor.releaseMobileNetworkRequest(); |
| 1837 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1838 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1839 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1840 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: { |
| 1841 | final LinkProperties newLp = (LinkProperties) message.obj; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1842 | if (message.arg1 == IpServer.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1843 | mOffload.updateDownstreamLinkProperties(newLp); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1844 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1845 | mOffload.excludeDownstreamInterface(newLp.getInterfaceName()); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1846 | } |
| 1847 | break; |
| 1848 | } |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 1849 | case EVENT_UPSTREAM_PERMISSION_CHANGED: |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1850 | case CMD_UPSTREAM_CHANGED: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1851 | updateUpstreamWanted(); |
| 1852 | if (!mUpstreamWanted) break; |
| 1853 | |
Erik Kline | fb41343 | 2017-02-14 18:26:04 +0900 | [diff] [blame] | 1854 | // Need to try DUN immediately if Wi-Fi goes down. |
| 1855 | chooseUpstreamType(true); |
| 1856 | mTryCell = false; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1857 | break; |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1858 | case CMD_RETRY_UPSTREAM: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1859 | updateUpstreamWanted(); |
| 1860 | if (!mUpstreamWanted) break; |
| 1861 | |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1862 | chooseUpstreamType(mTryCell); |
| 1863 | mTryCell = !mTryCell; |
| 1864 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1865 | case EVENT_UPSTREAM_CALLBACK: { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1866 | updateUpstreamWanted(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1867 | if (mUpstreamWanted) { |
| 1868 | handleUpstreamNetworkMonitorCallback(message.arg1, message.obj); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1869 | } |
| 1870 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1871 | } |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1872 | default: |
| 1873 | retValue = false; |
| 1874 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1875 | } |
| 1876 | return retValue; |
| 1877 | } |
| 1878 | } |
| 1879 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1880 | class ErrorState extends State { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1881 | private int mErrorNotification; |
| 1882 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1883 | @Override |
| 1884 | public boolean processMessage(Message message) { |
| 1885 | boolean retValue = true; |
| 1886 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1887 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1888 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1889 | who.sendMessage(mErrorNotification); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1890 | break; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1891 | case CMD_CLEAR_ERROR: |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1892 | mErrorNotification = TETHER_ERROR_NO_ERROR; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1893 | transitionTo(mInitialState); |
| 1894 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1895 | default: |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1896 | retValue = false; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1897 | } |
| 1898 | return retValue; |
| 1899 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1900 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1901 | void notify(int msgType) { |
| 1902 | mErrorNotification = msgType; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1903 | for (IpServer ipServer : mNotifyList) { |
| 1904 | ipServer.sendMessage(msgType); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1909 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1910 | class SetIpForwardingEnabledErrorState extends ErrorState { |
| 1911 | @Override |
| 1912 | public void enter() { |
| 1913 | Log.e(TAG, "Error in setIpForwardingEnabled"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1914 | notify(IpServer.CMD_IP_FORWARDING_ENABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | class SetIpForwardingDisabledErrorState extends ErrorState { |
| 1919 | @Override |
| 1920 | public void enter() { |
| 1921 | Log.e(TAG, "Error in setIpForwardingDisabled"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1922 | notify(IpServer.CMD_IP_FORWARDING_DISABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | class StartTetheringErrorState extends ErrorState { |
| 1927 | @Override |
| 1928 | public void enter() { |
| 1929 | Log.e(TAG, "Error in startTethering"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1930 | notify(IpServer.CMD_START_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1931 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1932 | mNetd.ipfwdDisableForwarding(TAG); |
| 1933 | } catch (RemoteException | ServiceSpecificException e) { } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | class StopTetheringErrorState extends ErrorState { |
| 1938 | @Override |
| 1939 | public void enter() { |
| 1940 | Log.e(TAG, "Error in stopTethering"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1941 | notify(IpServer.CMD_STOP_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1942 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1943 | mNetd.ipfwdDisableForwarding(TAG); |
| 1944 | } catch (RemoteException | ServiceSpecificException e) { } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | class SetDnsForwardersErrorState extends ErrorState { |
| 1949 | @Override |
| 1950 | public void enter() { |
| 1951 | Log.e(TAG, "Error in setDnsForwarders"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1952 | notify(IpServer.CMD_SET_DNS_FORWARDERS_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1953 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1954 | mNetd.tetherStop(); |
| 1955 | } catch (RemoteException | ServiceSpecificException e) { } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1956 | try { |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1957 | mNetd.ipfwdDisableForwarding(TAG); |
| 1958 | } catch (RemoteException | ServiceSpecificException e) { } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1959 | } |
| 1960 | } |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1961 | |
| 1962 | // A wrapper class to handle multiple situations where several calls to |
| 1963 | // the OffloadController need to happen together. |
| 1964 | // |
| 1965 | // TODO: This suggests that the interface between OffloadController and |
| 1966 | // Tethering is in need of improvement. Refactor these calls into the |
| 1967 | // OffloadController implementation. |
| 1968 | class OffloadWrapper { |
| 1969 | public void start() { |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 1970 | final int status = mOffloadController.start() ? TETHER_HARDWARE_OFFLOAD_STARTED |
| 1971 | : TETHER_HARDWARE_OFFLOAD_FAILED; |
| 1972 | updateOffloadStatus(status); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1973 | sendOffloadExemptPrefixes(); |
| 1974 | } |
| 1975 | |
| 1976 | public void stop() { |
| 1977 | mOffloadController.stop(); |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 1978 | updateOffloadStatus(TETHER_HARDWARE_OFFLOAD_STOPPED); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1979 | } |
| 1980 | |
markchien | b0aca96 | 2019-12-05 16:21:17 +0800 | [diff] [blame] | 1981 | public void updateUpstreamNetworkState(UpstreamNetworkState ns) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1982 | mOffloadController.setUpstreamLinkProperties( |
| 1983 | (ns != null) ? ns.linkProperties : null); |
| 1984 | } |
| 1985 | |
| 1986 | public void updateDownstreamLinkProperties(LinkProperties newLp) { |
| 1987 | // Update the list of offload-exempt prefixes before adding |
| 1988 | // new prefixes on downstream interfaces to the offload HAL. |
| 1989 | sendOffloadExemptPrefixes(); |
| 1990 | mOffloadController.notifyDownstreamLinkProperties(newLp); |
| 1991 | } |
| 1992 | |
| 1993 | public void excludeDownstreamInterface(String ifname) { |
| 1994 | // This and other interfaces may be in local-only hotspot mode; |
| 1995 | // resend all local prefixes to the OffloadController. |
| 1996 | sendOffloadExemptPrefixes(); |
| 1997 | mOffloadController.removeDownstreamInterface(ifname); |
| 1998 | } |
| 1999 | |
| 2000 | public void sendOffloadExemptPrefixes() { |
| 2001 | sendOffloadExemptPrefixes(mUpstreamNetworkMonitor.getLocalPrefixes()); |
| 2002 | } |
| 2003 | |
| 2004 | public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) { |
| 2005 | // Add in well-known minimum set. |
| 2006 | PrefixUtils.addNonForwardablePrefixes(localPrefixes); |
| 2007 | // Add tragically hardcoded prefixes. |
| 2008 | localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX); |
| 2009 | |
| 2010 | // Maybe add prefixes or addresses for downstreams, depending on |
| 2011 | // the IP serving mode of each. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2012 | for (IpServer ipServer : mNotifyList) { |
| 2013 | final LinkProperties lp = ipServer.linkProperties(); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 2014 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2015 | switch (ipServer.servingMode()) { |
| 2016 | case IpServer.STATE_UNAVAILABLE: |
| 2017 | case IpServer.STATE_AVAILABLE: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 2018 | // No usable LinkProperties in these states. |
| 2019 | continue; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2020 | case IpServer.STATE_TETHERED: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 2021 | // Only add IPv4 /32 and IPv6 /128 prefixes. The |
| 2022 | // directly-connected prefixes will be sent as |
| 2023 | // downstream "offload-able" prefixes. |
| 2024 | for (LinkAddress addr : lp.getAllLinkAddresses()) { |
| 2025 | final InetAddress ip = addr.getAddress(); |
| 2026 | if (ip.isLinkLocalAddress()) continue; |
| 2027 | localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip)); |
| 2028 | } |
| 2029 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2030 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 2031 | // Add prefixes covering all local IPs. |
| 2032 | localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp)); |
| 2033 | break; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | mOffloadController.setLocalPrefixes(localPrefixes); |
| 2038 | } |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 2039 | |
| 2040 | private void updateOffloadStatus(final int newStatus) { |
| 2041 | if (newStatus == mOffloadStatus) return; |
| 2042 | |
| 2043 | mOffloadStatus = newStatus; |
| 2044 | reportOffloadStatusChanged(mOffloadStatus); |
| 2045 | } |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 2046 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2049 | private void startTrackDefaultNetwork() { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 2050 | mUpstreamNetworkMonitor.startTrackDefaultNetwork(mDeps.getDefaultNetworkRequest(), |
| 2051 | mEntitlementMgr); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 2052 | } |
| 2053 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2054 | /** Get the latest value of the tethering entitlement check. */ |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2055 | void requestLatestTetheringEntitlementResult(int type, ResultReceiver receiver, |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2056 | boolean showEntitlementUi) { |
Mark Chien | 3075fbb | 2020-04-16 02:47:22 +0000 | [diff] [blame] | 2057 | if (receiver == null) return; |
| 2058 | |
| 2059 | mHandler.post(() -> { |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2060 | mEntitlementMgr.requestLatestTetheringEntitlementResult(type, receiver, |
| 2061 | showEntitlementUi); |
Mark Chien | 3075fbb | 2020-04-16 02:47:22 +0000 | [diff] [blame] | 2062 | }); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2063 | } |
| 2064 | |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2065 | /** Register tethering event callback */ |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2066 | void registerTetheringEventCallback(ITetheringEventCallback callback) { |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2067 | final boolean hasListPermission = |
Chiachang Wang | 96ca8e9 | 2020-02-21 17:50:18 +0800 | [diff] [blame] | 2068 | hasCallingPermission(NETWORK_SETTINGS) |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2069 | || hasCallingPermission(PERMISSION_MAINLINE_NETWORK_STACK) |
Chiachang Wang | 96ca8e9 | 2020-02-21 17:50:18 +0800 | [diff] [blame] | 2070 | || hasCallingPermission(NETWORK_STACK); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2071 | mHandler.post(() -> { |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2072 | mTetheringEventCallbacks.register(callback, new CallbackCookie(hasListPermission)); |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2073 | final TetheringCallbackStartedParcel parcel = new TetheringCallbackStartedParcel(); |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 2074 | parcel.tetheringSupported = isTetheringSupported(); |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2075 | parcel.upstreamNetwork = mTetherUpstream; |
| 2076 | parcel.config = mConfig.toStableParcelable(); |
markchien | faa3728 | 2020-02-06 14:34:08 +0800 | [diff] [blame] | 2077 | parcel.states = |
| 2078 | mTetherStatesParcel != null ? mTetherStatesParcel : emptyTetherStatesParcel(); |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2079 | parcel.tetheredClients = hasListPermission |
| 2080 | ? mConnectedClientsTracker.getLastTetheredClients() |
| 2081 | : Collections.emptyList(); |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 2082 | parcel.offloadStatus = mOffloadStatus; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2083 | try { |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2084 | callback.onCallbackStarted(parcel); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2085 | } catch (RemoteException e) { |
| 2086 | // Not really very much to do here. |
| 2087 | } |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2088 | }); |
| 2089 | } |
| 2090 | |
markchien | faa3728 | 2020-02-06 14:34:08 +0800 | [diff] [blame] | 2091 | private TetherStatesParcel emptyTetherStatesParcel() { |
| 2092 | final TetherStatesParcel parcel = new TetherStatesParcel(); |
| 2093 | parcel.availableList = new String[0]; |
| 2094 | parcel.tetheredList = new String[0]; |
| 2095 | parcel.localOnlyList = new String[0]; |
| 2096 | parcel.erroredIfaceList = new String[0]; |
| 2097 | parcel.lastErrorList = new int[0]; |
| 2098 | |
| 2099 | return parcel; |
| 2100 | } |
| 2101 | |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2102 | private boolean hasCallingPermission(@NonNull String permission) { |
| 2103 | return mContext.checkCallingPermission(permission) == PERMISSION_GRANTED; |
| 2104 | } |
| 2105 | |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2106 | /** Unregister tethering event callback */ |
| 2107 | void unregisterTetheringEventCallback(ITetheringEventCallback callback) { |
| 2108 | mHandler.post(() -> { |
| 2109 | mTetheringEventCallbacks.unregister(callback); |
| 2110 | }); |
| 2111 | } |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2112 | |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 2113 | private void reportUpstreamChanged(UpstreamNetworkState ns) { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2114 | final int length = mTetheringEventCallbacks.beginBroadcast(); |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 2115 | final Network network = (ns != null) ? ns.network : null; |
| 2116 | final NetworkCapabilities capabilities = (ns != null) ? ns.networkCapabilities : null; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2117 | try { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2118 | for (int i = 0; i < length; i++) { |
| 2119 | try { |
| 2120 | mTetheringEventCallbacks.getBroadcastItem(i).onUpstreamChanged(network); |
| 2121 | } catch (RemoteException e) { |
| 2122 | // Not really very much to do here. |
| 2123 | } |
| 2124 | } |
| 2125 | } finally { |
| 2126 | mTetheringEventCallbacks.finishBroadcast(); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2127 | } |
Chalard Jean | c0fc276 | 2020-04-17 17:12:44 +0000 | [diff] [blame] | 2128 | // Need to notify capabilities change after upstream network changed because new network's |
| 2129 | // capabilities should be checked every time. |
| 2130 | mNotificationUpdater.onUpstreamCapabilitiesChanged(capabilities); |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2131 | } |
| 2132 | |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2133 | private void reportConfigurationChanged(TetheringConfigurationParcel config) { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2134 | final int length = mTetheringEventCallbacks.beginBroadcast(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2135 | try { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2136 | for (int i = 0; i < length; i++) { |
| 2137 | try { |
| 2138 | mTetheringEventCallbacks.getBroadcastItem(i).onConfigurationChanged(config); |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2139 | // TODO(b/148139325): send tetheringSupported on configuration change |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2140 | } catch (RemoteException e) { |
| 2141 | // Not really very much to do here. |
| 2142 | } |
| 2143 | } |
| 2144 | } finally { |
| 2145 | mTetheringEventCallbacks.finishBroadcast(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | private void reportTetherStateChanged(TetherStatesParcel states) { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2150 | final int length = mTetheringEventCallbacks.beginBroadcast(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2151 | try { |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2152 | for (int i = 0; i < length; i++) { |
| 2153 | try { |
| 2154 | mTetheringEventCallbacks.getBroadcastItem(i).onTetherStatesChanged(states); |
| 2155 | } catch (RemoteException e) { |
| 2156 | // Not really very much to do here. |
| 2157 | } |
| 2158 | } |
| 2159 | } finally { |
| 2160 | mTetheringEventCallbacks.finishBroadcast(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2164 | private void reportTetherClientsChanged(List<TetheredClient> clients) { |
| 2165 | final int length = mTetheringEventCallbacks.beginBroadcast(); |
| 2166 | try { |
| 2167 | for (int i = 0; i < length; i++) { |
| 2168 | try { |
| 2169 | final CallbackCookie cookie = |
| 2170 | (CallbackCookie) mTetheringEventCallbacks.getBroadcastCookie(i); |
| 2171 | if (!cookie.hasListClientsPermission) continue; |
| 2172 | mTetheringEventCallbacks.getBroadcastItem(i).onTetherClientsChanged(clients); |
| 2173 | } catch (RemoteException e) { |
| 2174 | // Not really very much to do here. |
| 2175 | } |
| 2176 | } |
| 2177 | } finally { |
| 2178 | mTetheringEventCallbacks.finishBroadcast(); |
| 2179 | } |
| 2180 | } |
| 2181 | |
Automerger Merge Worker | c22ab7b | 2020-03-09 04:07:07 +0000 | [diff] [blame] | 2182 | private void reportOffloadStatusChanged(final int status) { |
| 2183 | final int length = mTetheringEventCallbacks.beginBroadcast(); |
| 2184 | try { |
| 2185 | for (int i = 0; i < length; i++) { |
| 2186 | try { |
| 2187 | mTetheringEventCallbacks.getBroadcastItem(i).onOffloadStatusChanged(status); |
| 2188 | } catch (RemoteException e) { |
| 2189 | // Not really very much to do here. |
| 2190 | } |
| 2191 | } |
| 2192 | } finally { |
| 2193 | mTetheringEventCallbacks.finishBroadcast(); |
| 2194 | } |
| 2195 | } |
| 2196 | |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 2197 | // if ro.tether.denied = true we default to no tethering |
| 2198 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 2199 | // had previously been turned off. |
| 2200 | boolean isTetheringSupported() { |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 2201 | final int defaultVal = mDeps.isTetheringDenied() ? 0 : 1; |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 2202 | final boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(), |
| 2203 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0; |
| 2204 | final boolean tetherEnabledInSettings = tetherSupported |
| 2205 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
| 2206 | |
markchien | 0ed433e | 2020-06-03 12:27:37 +0800 | [diff] [blame] | 2207 | return tetherEnabledInSettings && hasTetherableConfiguration() |
| 2208 | && !isProvisioningNeededButUnavailable(); |
markchien | cb8860c | 2020-05-12 00:08:27 +0800 | [diff] [blame] | 2209 | } |
| 2210 | |
markchien | a9d9ef8 | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 2211 | void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter writer, @Nullable String[] args) { |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 2212 | // Binder.java closes the resource for us. |
| 2213 | @SuppressWarnings("resource") |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2214 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2215 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2216 | != PERMISSION_GRANTED) { |
| 2217 | pw.println("Permission Denial: can't dump."); |
| 2218 | return; |
| 2219 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2220 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2221 | pw.println("Tethering:"); |
| 2222 | pw.increaseIndent(); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2223 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 2224 | pw.println("Configuration:"); |
| 2225 | pw.increaseIndent(); |
| 2226 | final TetheringConfiguration cfg = mConfig; |
| 2227 | cfg.dump(pw); |
| 2228 | pw.decreaseIndent(); |
| 2229 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 2230 | pw.println("Entitlement:"); |
| 2231 | pw.increaseIndent(); |
| 2232 | mEntitlementMgr.dump(pw); |
| 2233 | pw.decreaseIndent(); |
| 2234 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 2235 | synchronized (mPublicSync) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 2236 | pw.println("Tether state:"); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2237 | pw.increaseIndent(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2238 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 2239 | final String iface = mTetherStates.keyAt(i); |
| 2240 | final TetherState tetherState = mTetherStates.valueAt(i); |
| 2241 | pw.print(iface + " - "); |
| 2242 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 2243 | switch (tetherState.lastState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2244 | case IpServer.STATE_UNAVAILABLE: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2245 | pw.print("UnavailableState"); |
| 2246 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2247 | case IpServer.STATE_AVAILABLE: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2248 | pw.print("AvailableState"); |
| 2249 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2250 | case IpServer.STATE_TETHERED: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2251 | pw.print("TetheredState"); |
| 2252 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2253 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2254 | pw.print("LocalHotspotState"); |
| 2255 | break; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2256 | default: |
| 2257 | pw.print("UnknownState"); |
| 2258 | break; |
| 2259 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 2260 | pw.println(" - lastError = " + tetherState.lastError); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2261 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2262 | pw.println("Upstream wanted: " + upstreamWanted()); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 2263 | pw.println("Current upstream interface(s): " + mCurrentUpstreamIfaceSet); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2264 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2265 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 2266 | |
Lorenzo Colitti | c2519c5 | 2017-07-13 01:48:26 +0900 | [diff] [blame] | 2267 | pw.println("Hardware offload:"); |
| 2268 | pw.increaseIndent(); |
| 2269 | mOffloadController.dump(pw); |
| 2270 | pw.decreaseIndent(); |
| 2271 | |
Nucca Chen | 1f9efaf | 2020-06-17 06:32:35 +0000 | [diff] [blame] | 2272 | pw.println("BPF offload:"); |
| 2273 | pw.increaseIndent(); |
| 2274 | mBpfCoordinator.dump(pw); |
| 2275 | pw.decreaseIndent(); |
| 2276 | |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 2277 | pw.println("Private address coordinator:"); |
| 2278 | pw.increaseIndent(); |
| 2279 | mPrivateAddressCoordinator.dump(pw); |
| 2280 | pw.decreaseIndent(); |
| 2281 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 2282 | pw.println("Log:"); |
| 2283 | pw.increaseIndent(); |
markchien | 0f63ca6 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 2284 | if (argsContain(args, "--short")) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 2285 | pw.println("<log removed for brevity>"); |
| 2286 | } else { |
| 2287 | mLog.dump(fd, pw, args); |
| 2288 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 2289 | pw.decreaseIndent(); |
| 2290 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2291 | pw.decreaseIndent(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2292 | } |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 2293 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 2294 | private static boolean argsContain(String[] args, String target) { |
| 2295 | for (String arg : args) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 2296 | if (target.equals(arg)) return true; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 2297 | } |
| 2298 | return false; |
| 2299 | } |
| 2300 | |
Treehugger Robot | 2506781 | 2020-03-25 15:32:41 +0000 | [diff] [blame] | 2301 | private void updateConnectedClients(final List<WifiClient> wifiClients) { |
| 2302 | if (mConnectedClientsTracker.updateConnectedClients(mForwardedDownstreams, wifiClients)) { |
| 2303 | reportTetherClientsChanged(mConnectedClientsTracker.getLastTetheredClients()); |
| 2304 | } |
| 2305 | } |
| 2306 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2307 | private IpServer.Callback makeControlCallback() { |
| 2308 | return new IpServer.Callback() { |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2309 | @Override |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2310 | public void updateInterfaceState(IpServer who, int state, int lastError) { |
| 2311 | notifyInterfaceStateChange(who, state, lastError); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | @Override |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2315 | public void updateLinkProperties(IpServer who, LinkProperties newLp) { |
| 2316 | notifyLinkPropertiesChanged(who, newLp); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2317 | } |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2318 | |
| 2319 | @Override |
| 2320 | public void dhcpLeasesChanged() { |
Treehugger Robot | 2506781 | 2020-03-25 15:32:41 +0000 | [diff] [blame] | 2321 | updateConnectedClients(null /* wifiClients */); |
Remi NGUYEN VAN | 43b0e5c | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 2322 | } |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 2323 | |
| 2324 | @Override |
| 2325 | public void requestEnableTethering(int tetheringType, boolean enabled) { |
| 2326 | enableTetheringInternal(tetheringType, enabled, null); |
| 2327 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2328 | }; |
| 2329 | } |
| 2330 | |
| 2331 | // TODO: Move into TetherMasterSM. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2332 | private void notifyInterfaceStateChange(IpServer who, int state, int error) { |
| 2333 | final String iface = who.interfaceName(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2334 | synchronized (mPublicSync) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 2335 | final TetherState tetherState = mTetherStates.get(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2336 | if (tetherState != null && tetherState.ipServer.equals(who)) { |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 2337 | tetherState.lastState = state; |
| 2338 | tetherState.lastError = error; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2339 | } else { |
| 2340 | if (DBG) Log.d(TAG, "got notification from stale iface " + iface); |
| 2341 | } |
| 2342 | } |
| 2343 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 2344 | mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error)); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2345 | |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 2346 | // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. |
| 2347 | // Thus we give a chance for TetherMasterSM to recover to InitialState |
| 2348 | // by sending CMD_CLEAR_ERROR |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2349 | if (error == TETHER_ERROR_INTERNAL_ERROR) { |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 2350 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); |
| 2351 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2352 | int which; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2353 | switch (state) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2354 | case IpServer.STATE_UNAVAILABLE: |
| 2355 | case IpServer.STATE_AVAILABLE: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2356 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2357 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2358 | case IpServer.STATE_TETHERED: |
| 2359 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2360 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2361 | break; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2362 | default: |
| 2363 | Log.wtf(TAG, "Unknown interface state: " + state); |
| 2364 | return; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2365 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 2366 | mTetherMasterSM.sendMessage(which, state, 0, who); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2367 | sendTetherStateChangedBroadcast(); |
| 2368 | } |
| 2369 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2370 | private void notifyLinkPropertiesChanged(IpServer who, LinkProperties newLp) { |
| 2371 | final String iface = who.interfaceName(); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2372 | final int state; |
| 2373 | synchronized (mPublicSync) { |
| 2374 | final TetherState tetherState = mTetherStates.get(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2375 | if (tetherState != null && tetherState.ipServer.equals(who)) { |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2376 | state = tetherState.lastState; |
| 2377 | } else { |
| 2378 | mLog.log("got notification from stale iface " + iface); |
| 2379 | return; |
| 2380 | } |
| 2381 | } |
| 2382 | |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 2383 | mLog.log(String.format( |
| 2384 | "OBSERVED LinkProperties update iface=%s state=%s lp=%s", |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2385 | iface, IpServer.getStateString(state), newLp)); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2386 | final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; |
| 2387 | mTetherMasterSM.sendMessage(which, state, 0, newLp); |
| 2388 | } |
| 2389 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2390 | private void maybeTrackNewInterfaceLocked(final String iface) { |
| 2391 | // If we don't care about this type of interface, ignore. |
| 2392 | final int interfaceType = ifaceNameToType(iface); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 2393 | if (interfaceType == TETHERING_INVALID) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2394 | mLog.log(iface + " is not a tetherable iface, ignoring"); |
| 2395 | return; |
| 2396 | } |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 2397 | maybeTrackNewInterfaceLocked(iface, interfaceType); |
| 2398 | } |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2399 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 2400 | private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2401 | // If we have already started a TISM for this interface, skip. |
| 2402 | if (mTetherStates.containsKey(iface)) { |
| 2403 | mLog.log("active iface (" + iface + ") reported as added, ignoring"); |
| 2404 | return; |
| 2405 | } |
| 2406 | |
| 2407 | mLog.log("adding TetheringInterfaceStateMachine for: " + iface); |
| 2408 | final TetherState tetherState = new TetherState( |
Hungming Chen | be4827d | 2020-06-02 00:13:26 +0000 | [diff] [blame] | 2409 | new IpServer(iface, mLooper, interfaceType, mLog, mNetd, mBpfCoordinator, |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2410 | makeControlCallback(), mConfig.enableLegacyDhcpServer, |
Nucca Chen | 1c63ba3 | 2020-06-17 07:03:57 +0000 | [diff] [blame] | 2411 | mConfig.isBpfOffloadEnabled(), mPrivateAddressCoordinator, |
Mark Chien | db6befb | 2020-05-29 22:07:37 +0000 | [diff] [blame] | 2412 | mDeps.getIpServerDependencies())); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2413 | mTetherStates.put(iface, tetherState); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2414 | tetherState.ipServer.start(); |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 2415 | } |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 2416 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2417 | private void stopTrackingInterfaceLocked(final String iface) { |
| 2418 | final TetherState tetherState = mTetherStates.get(iface); |
| 2419 | if (tetherState == null) { |
| 2420 | mLog.log("attempting to remove unknown iface (" + iface + "), ignoring"); |
| 2421 | return; |
| 2422 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 2423 | tetherState.ipServer.stop(); |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2424 | mLog.log("removing TetheringInterfaceStateMachine for: " + iface); |
| 2425 | mTetherStates.remove(iface); |
| 2426 | } |
| 2427 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 2428 | private static String[] copy(String[] strarray) { |
| 2429 | return Arrays.copyOf(strarray, strarray.length); |
| 2430 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2431 | } |