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