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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 19 | import static android.hardware.usb.UsbManager.USB_CONFIGURED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 20 | import static android.hardware.usb.UsbManager.USB_CONNECTED; |
| 21 | import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; |
| 23 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
| 24 | import static android.net.ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY; |
| 25 | import static android.net.ConnectivityManager.EXTRA_ACTIVE_TETHER; |
| 26 | import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE; |
| 27 | import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER; |
| 28 | import static android.net.ConnectivityManager.EXTRA_ERRORED_TETHER; |
| 29 | import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO; |
| 30 | import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK; |
| 31 | import static android.net.ConnectivityManager.EXTRA_REM_TETHER_TYPE; |
| 32 | import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION; |
| 33 | import static android.net.ConnectivityManager.EXTRA_SET_ALARM; |
| 34 | import static android.net.ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 35 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 36 | import static android.net.ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 37 | import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 38 | import static android.net.ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 39 | import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; |
| 40 | import static android.net.ConnectivityManager.TETHERING_INVALID; |
| 41 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 42 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 43 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 44 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 45 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 46 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 47 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; |
| 48 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 49 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
| 50 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 51 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 52 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 53 | import static com.android.server.ConnectivityService.SHORT_ARG; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 54 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 55 | import android.app.Notification; |
| 56 | import android.app.NotificationManager; |
| 57 | import android.app.PendingIntent; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 58 | import android.bluetooth.BluetoothAdapter; |
| 59 | import android.bluetooth.BluetoothPan; |
| 60 | import android.bluetooth.BluetoothProfile; |
| 61 | import android.bluetooth.BluetoothProfile.ServiceListener; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 62 | import android.content.BroadcastReceiver; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 63 | import android.content.ComponentName; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 64 | import android.content.Context; |
| 65 | import android.content.Intent; |
| 66 | import android.content.IntentFilter; |
| 67 | import android.content.res.Resources; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 68 | import android.hardware.usb.UsbManager; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 69 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 70 | import android.net.INetworkStatsService; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 71 | import android.net.ip.IpServer; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 72 | import android.net.IpPrefix; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 73 | import android.net.LinkAddress; |
Robert Greenwalt | 05d8936 | 2011-01-23 16:04:05 -0800 | [diff] [blame] | 74 | import android.net.LinkProperties; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 75 | import android.net.Network; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 76 | import android.net.NetworkInfo; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 77 | import android.net.NetworkState; |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 78 | import android.net.NetworkUtils; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 79 | import android.net.RouteInfo; |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 80 | import android.net.util.InterfaceSet; |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 81 | import android.net.util.PrefixUtils; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 82 | import android.net.util.SharedLog; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 83 | import android.net.util.VersionedBroadcastListener; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 84 | import android.net.wifi.WifiManager; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 85 | import android.os.Binder; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 86 | import android.os.Bundle; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 87 | import android.os.Handler; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 88 | import android.os.INetworkManagementService; |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 89 | import android.os.Looper; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 90 | import android.os.Message; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 91 | import android.os.Parcel; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 92 | import android.os.PersistableBundle; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 93 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 94 | import android.os.ResultReceiver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 95 | import android.os.UserHandle; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 96 | import android.os.UserManager; |
| 97 | import android.os.UserManagerInternal; |
| 98 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 99 | import android.provider.Settings; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 100 | import android.telephony.CarrierConfigManager; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 101 | import android.text.TextUtils; |
Christopher Wiley | e949039 | 2016-05-26 15:57:29 -0700 | [diff] [blame] | 102 | import android.util.ArrayMap; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 103 | import android.util.Log; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 104 | import android.util.SparseArray; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 105 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 106 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 107 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 108 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 109 | import com.android.internal.util.DumpUtils; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 110 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 111 | import com.android.internal.util.MessageUtils; |
| 112 | import com.android.internal.util.Protocol; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 113 | import com.android.internal.util.State; |
| 114 | import com.android.internal.util.StateMachine; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 115 | import com.android.server.LocalServices; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 116 | import com.android.server.connectivity.tethering.IPv6TetheringCoordinator; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 117 | import com.android.server.connectivity.tethering.OffloadController; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 118 | import com.android.server.connectivity.tethering.TetheringConfiguration; |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 119 | import com.android.server.connectivity.tethering.TetheringDependencies; |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 120 | import com.android.server.connectivity.tethering.TetheringInterfaceUtils; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 121 | import com.android.server.connectivity.tethering.UpstreamNetworkMonitor; |
Lorenzo Colitti | f4e90ea | 2013-10-31 23:30:47 +0900 | [diff] [blame] | 122 | import com.android.server.net.BaseNetworkObserver; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 123 | |
| 124 | import java.io.FileDescriptor; |
| 125 | import java.io.PrintWriter; |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 126 | import java.net.Inet4Address; |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 127 | import java.net.Inet6Address; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 128 | import java.net.InetAddress; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 129 | import java.util.ArrayList; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 130 | import java.util.Arrays; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 131 | import java.util.Collection; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 132 | import java.util.HashSet; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 133 | import java.util.Set; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 134 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 135 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 136 | /** |
| 137 | * @hide |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 138 | * |
Christopher Wiley | 3b1d922 | 2016-05-20 16:44:04 -0700 | [diff] [blame] | 139 | * This class holds much of the business logic to allow Android devices |
| 140 | * to act as IP gateways via USB, BT, and WiFi interfaces. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 141 | */ |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 142 | public class Tethering extends BaseNetworkObserver { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 143 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 144 | private final static String TAG = Tethering.class.getSimpleName(); |
Joe Onorato | 12acbd7 | 2016-02-01 17:49:31 -0800 | [diff] [blame] | 145 | private final static boolean DBG = false; |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 146 | private final static boolean VDBG = false; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 147 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 148 | protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning"; |
| 149 | |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 150 | private static final Class[] messageClasses = { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 151 | Tethering.class, TetherMasterSM.class, IpServer.class |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 152 | }; |
| 153 | private static final SparseArray<String> sMagicDecoderRing = |
| 154 | MessageUtils.findMessageNames(messageClasses); |
| 155 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 156 | // {@link ComponentName} of the Service used to run tether provisioning. |
| 157 | private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources |
| 158 | .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable)); |
| 159 | |
| 160 | private static class TetherState { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 161 | public final IpServer ipServer; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 162 | public int lastState; |
| 163 | public int lastError; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 164 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 165 | public TetherState(IpServer ipServer) { |
| 166 | this.ipServer = ipServer; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 167 | // Assume all state machines start out available and with no errors. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 168 | lastState = IpServer.STATE_AVAILABLE; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 169 | lastError = TETHER_ERROR_NO_ERROR; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 170 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 171 | |
| 172 | public boolean isCurrentlyServing() { |
| 173 | switch (lastState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 174 | case IpServer.STATE_TETHERED: |
| 175 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 176 | return true; |
| 177 | default: |
| 178 | return false; |
| 179 | } |
| 180 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 181 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 182 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 183 | private final SharedLog mLog = new SharedLog(TAG); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 184 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 185 | // used to synchronize public access to members |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 186 | private final Object mPublicSync; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 187 | private final Context mContext; |
| 188 | private final ArrayMap<String, TetherState> mTetherStates; |
| 189 | private final BroadcastReceiver mStateReceiver; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 190 | private final INetworkManagementService mNMService; |
| 191 | private final INetworkStatsService mStatsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 192 | private final INetworkPolicyManager mPolicyManager; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 193 | private final Looper mLooper; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 194 | private final MockableSystemProperties mSystemProperties; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 195 | private final StateMachine mTetherMasterSM; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 196 | private final OffloadController mOffloadController; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 197 | private final UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 198 | // TODO: Figure out how to merge this and other downstream-tracking objects |
| 199 | // into a single coherent structure. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 200 | private final HashSet<IpServer> mForwardedDownstreams; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 201 | private final VersionedBroadcastListener mCarrierConfigChange; |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 202 | private final TetheringDependencies mDeps; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 203 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 204 | private volatile TetheringConfiguration mConfig; |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 205 | private InterfaceSet mCurrentUpstreamIfaceSet; |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 206 | private Notification.Builder mTetheredNotificationBuilder; |
| 207 | private int mLastNotificationId; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 208 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 209 | private boolean mRndisEnabled; // track the RNDIS function enabled state |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 210 | // True iff. WiFi tethering should be started when soft AP is ready. |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 211 | private boolean mWifiTetherRequested; |
| 212 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 213 | public Tethering(Context context, INetworkManagementService nmService, |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 214 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 215 | Looper looper, MockableSystemProperties systemProperties, |
| 216 | TetheringDependencies deps) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 217 | mLog.mark("constructed"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 218 | mContext = context; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 219 | mNMService = nmService; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 220 | mStatsService = statsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 221 | mPolicyManager = policyManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 222 | mLooper = looper; |
| 223 | mSystemProperties = systemProperties; |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 224 | mDeps = deps; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 225 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 226 | mPublicSync = new Object(); |
| 227 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 228 | mTetherStates = new ArrayMap<>(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 229 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 230 | mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper, deps); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 231 | mTetherMasterSM.start(); |
| 232 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 233 | final Handler smHandler = mTetherMasterSM.getHandler(); |
| 234 | mOffloadController = new OffloadController(smHandler, |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 235 | mDeps.getOffloadHardwareInterface(smHandler, mLog), |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 236 | mContext.getContentResolver(), mNMService, |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 237 | mLog); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 238 | mUpstreamNetworkMonitor = deps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog, |
| 239 | TetherMasterSM.EVENT_UPSTREAM_CALLBACK); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 240 | mForwardedDownstreams = new HashSet<>(); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 241 | |
| 242 | IntentFilter filter = new IntentFilter(); |
| 243 | filter.addAction(ACTION_CARRIER_CONFIG_CHANGED); |
| 244 | mCarrierConfigChange = new VersionedBroadcastListener( |
| 245 | "CarrierConfigChangeListener", mContext, smHandler, filter, |
| 246 | (Intent ignored) -> { |
| 247 | mLog.log("OBSERVED carrier config change"); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 248 | updateConfiguration(); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 249 | reevaluateSimCardProvisioning(); |
| 250 | }); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 251 | |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 252 | mStateReceiver = new StateReceiver(); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 253 | |
| 254 | // Load tethering configuration. |
| 255 | updateConfiguration(); |
| 256 | |
| 257 | startStateMachineUpdaters(); |
| 258 | } |
| 259 | |
| 260 | private void startStateMachineUpdaters() { |
| 261 | mCarrierConfigChange.startListening(); |
| 262 | |
| 263 | final Handler handler = mTetherMasterSM.getHandler(); |
| 264 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 265 | filter.addAction(UsbManager.ACTION_USB_STATE); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 266 | filter.addAction(CONNECTIVITY_ACTION); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 267 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 268 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 269 | mContext.registerReceiver(mStateReceiver, filter, null, handler); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 270 | |
| 271 | filter = new IntentFilter(); |
Robert Greenwalt | 030f5e1 | 2010-03-10 16:41:03 -0800 | [diff] [blame] | 272 | filter.addAction(Intent.ACTION_MEDIA_SHARED); |
| 273 | filter.addAction(Intent.ACTION_MEDIA_UNSHARED); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 274 | filter.addDataScheme("file"); |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 275 | mContext.registerReceiver(mStateReceiver, filter, null, handler); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 276 | |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 277 | final UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class); |
| 278 | // This check is useful only for some unit tests; example: ConnectivityServiceTest. |
| 279 | if (umi != null) { |
| 280 | umi.addUserRestrictionsListener(new TetheringUserRestrictionListener(this)); |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 281 | } |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 284 | private WifiManager getWifiManager() { |
| 285 | return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| 286 | } |
| 287 | |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 288 | // NOTE: This is always invoked on the mLooper thread. |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 289 | private void updateConfiguration() { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 290 | mConfig = new TetheringConfiguration(mContext, mLog); |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 291 | mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 294 | private void maybeUpdateConfiguration() { |
| 295 | final int dunCheck = TetheringConfiguration.checkDunRequired(mContext); |
| 296 | if (dunCheck == mConfig.dunCheck) return; |
| 297 | updateConfiguration(); |
| 298 | } |
| 299 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 300 | @Override |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 301 | public void interfaceStatusChanged(String iface, boolean up) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 302 | // Never called directly: only called from interfaceLinkStateChanged. |
| 303 | // See NetlinkHandler.cpp:71. |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 304 | if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 305 | synchronized (mPublicSync) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 306 | if (up) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 307 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 308 | } else { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 309 | if (ifaceNameToType(iface) == TETHERING_BLUETOOTH) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 310 | stopTrackingInterfaceLocked(iface); |
Christopher Wiley | d30aaeb | 2016-07-08 09:33:50 -0700 | [diff] [blame] | 311 | } else { |
| 312 | // Ignore usb0 down after enabling RNDIS. |
| 313 | // We will handle disconnect in interfaceRemoved. |
| 314 | // Similarly, ignore interface down for WiFi. We monitor WiFi AP status |
| 315 | // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent. |
| 316 | if (VDBG) Log.d(TAG, "ignore interface down for " + iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 322 | @Override |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 323 | public void interfaceLinkStateChanged(String iface, boolean up) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 324 | interfaceStatusChanged(iface, up); |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 327 | private int ifaceNameToType(String iface) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 328 | final TetheringConfiguration cfg = mConfig; |
| 329 | |
| 330 | if (cfg.isWifi(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 331 | return TETHERING_WIFI; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 332 | } else if (cfg.isUsb(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 333 | return TETHERING_USB; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 334 | } else if (cfg.isBluetooth(iface)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 335 | return TETHERING_BLUETOOTH; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 336 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 337 | return TETHERING_INVALID; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 340 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 341 | public void interfaceAdded(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 342 | if (VDBG) Log.d(TAG, "interfaceAdded " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 343 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 344 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 345 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 348 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 349 | public void interfaceRemoved(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 350 | if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 351 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 352 | stopTrackingInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 356 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 357 | if (!isTetherProvisioningRequired()) { |
| 358 | enableTetheringInternal(type, true, receiver); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | if (showProvisioningUi) { |
| 363 | runUiTetherProvisioningAndEnable(type, receiver); |
| 364 | } else { |
| 365 | runSilentTetherProvisioningAndEnable(type, receiver); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | public void stopTethering(int type) { |
| 370 | enableTetheringInternal(type, false, null); |
| 371 | if (isTetherProvisioningRequired()) { |
| 372 | cancelTetherProvisioningRechecks(type); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Check if the device requires a provisioning check in order to enable tethering. |
| 378 | * |
| 379 | * @return a boolean - {@code true} indicating tether provisioning is required by the carrier. |
| 380 | */ |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 381 | @VisibleForTesting |
| 382 | protected boolean isTetherProvisioningRequired() { |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 383 | final TetheringConfiguration cfg = mConfig; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 384 | if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false) |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 385 | || cfg.provisioningApp.length == 0) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 386 | return false; |
| 387 | } |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 388 | if (carrierConfigAffirmsEntitlementCheckNotRequired()) { |
| 389 | return false; |
| 390 | } |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 391 | return (cfg.provisioningApp.length == 2); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // The logic here is aimed solely at confirming that a CarrierConfig exists |
| 395 | // and affirms that entitlement checks are not required. |
| 396 | // |
| 397 | // TODO: find a better way to express this, or alter the checking process |
| 398 | // entirely so that this is more intuitive. |
| 399 | private boolean carrierConfigAffirmsEntitlementCheckNotRequired() { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 400 | // Check carrier config for entitlement checks |
| 401 | final CarrierConfigManager configManager = (CarrierConfigManager) mContext |
| 402 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 403 | if (configManager == null) return false; |
| 404 | |
| 405 | final PersistableBundle carrierConfig = configManager.getConfig(); |
| 406 | if (carrierConfig == null) return false; |
| 407 | |
| 408 | // A CarrierConfigManager was found and it has a config. |
| 409 | final boolean isEntitlementCheckRequired = carrierConfig.getBoolean( |
| 410 | CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL); |
| 411 | return !isEntitlementCheckRequired; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Enables or disables tethering for the given type. This should only be called once |
| 416 | * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks |
| 417 | * for the specified interface. |
| 418 | */ |
| 419 | private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 420 | boolean isProvisioningRequired = enable && isTetherProvisioningRequired(); |
| 421 | int result; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 422 | switch (type) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 423 | case TETHERING_WIFI: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 424 | result = setWifiTethering(enable); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 425 | if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 426 | scheduleProvisioningRechecks(type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 427 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 428 | sendTetherResult(receiver, result); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 429 | break; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 430 | case TETHERING_USB: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 431 | result = setUsbTethering(enable); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 432 | if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 433 | scheduleProvisioningRechecks(type); |
| 434 | } |
| 435 | sendTetherResult(receiver, result); |
| 436 | break; |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 437 | case TETHERING_BLUETOOTH: |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 438 | setBluetoothTethering(enable, receiver); |
| 439 | break; |
| 440 | default: |
| 441 | Log.w(TAG, "Invalid tether type."); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 442 | sendTetherResult(receiver, TETHER_ERROR_UNKNOWN_IFACE); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | private void sendTetherResult(ResultReceiver receiver, int result) { |
| 447 | if (receiver != null) { |
| 448 | receiver.send(result, null); |
| 449 | } |
| 450 | } |
| 451 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 452 | private int setWifiTethering(final boolean enable) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 453 | int rval = TETHER_ERROR_MASTER_ERROR; |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 454 | final long ident = Binder.clearCallingIdentity(); |
| 455 | try { |
| 456 | synchronized (mPublicSync) { |
| 457 | mWifiTetherRequested = enable; |
| 458 | final WifiManager mgr = getWifiManager(); |
| 459 | if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) || |
| 460 | (!enable && mgr.stopSoftAp())) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 461 | rval = TETHER_ERROR_NO_ERROR; |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 462 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 463 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 464 | } finally { |
| 465 | Binder.restoreCallingIdentity(ident); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 466 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 467 | return rval; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 470 | private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) { |
| 471 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 472 | if (adapter == null || !adapter.isEnabled()) { |
| 473 | Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " + |
| 474 | (adapter == null)); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 475 | sendTetherResult(receiver, TETHER_ERROR_SERVICE_UNAVAIL); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | adapter.getProfileProxy(mContext, new ServiceListener() { |
| 480 | @Override |
| 481 | public void onServiceDisconnected(int profile) { } |
| 482 | |
| 483 | @Override |
| 484 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 485 | ((BluetoothPan) proxy).setBluetoothTethering(enable); |
| 486 | // TODO: Enabling bluetooth tethering can fail asynchronously here. |
| 487 | // 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] | 488 | final int result = (((BluetoothPan) proxy).isTetheringOn() == enable) |
| 489 | ? TETHER_ERROR_NO_ERROR |
| 490 | : TETHER_ERROR_MASTER_ERROR; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 491 | sendTetherResult(receiver, result); |
| 492 | if (enable && isTetherProvisioningRequired()) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 493 | scheduleProvisioningRechecks(TETHERING_BLUETOOTH); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 494 | } |
| 495 | adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); |
| 496 | } |
| 497 | }, BluetoothProfile.PAN); |
| 498 | } |
| 499 | |
| 500 | private void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 501 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 502 | sendUiTetherProvisionIntent(type, proxyReceiver); |
| 503 | } |
| 504 | |
| 505 | private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 506 | Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 507 | intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); |
| 508 | intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); |
Jeremy Klein | d50fc05 | 2018-01-19 11:25:28 -0800 | [diff] [blame] | 509 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 510 | final long ident = Binder.clearCallingIdentity(); |
| 511 | try { |
| 512 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 513 | } finally { |
| 514 | Binder.restoreCallingIdentity(ident); |
| 515 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
Erik Kline | fdd4111 | 2017-01-12 17:02:51 +0900 | [diff] [blame] | 519 | * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result |
| 520 | * is successful before firing back up to the wrapped receiver. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 521 | * |
| 522 | * @param type The type of tethering being enabled. |
| 523 | * @param receiver A ResultReceiver which will be called back with an int resultCode. |
| 524 | * @return The proxy receiver. |
| 525 | */ |
| 526 | private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) { |
| 527 | ResultReceiver rr = new ResultReceiver(null) { |
| 528 | @Override |
| 529 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 530 | // If provisioning is successful, enable tethering, otherwise just send the error. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 531 | if (resultCode == TETHER_ERROR_NO_ERROR) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 532 | enableTetheringInternal(type, true, receiver); |
| 533 | } else { |
| 534 | sendTetherResult(receiver, resultCode); |
| 535 | } |
| 536 | } |
| 537 | }; |
| 538 | |
| 539 | // The following is necessary to avoid unmarshalling issues when sending the receiver |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 540 | // across processes. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 541 | Parcel parcel = Parcel.obtain(); |
| 542 | rr.writeToParcel(parcel,0); |
| 543 | parcel.setDataPosition(0); |
| 544 | ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel); |
| 545 | parcel.recycle(); |
| 546 | return receiverForSending; |
| 547 | } |
| 548 | |
| 549 | private void scheduleProvisioningRechecks(int type) { |
| 550 | Intent intent = new Intent(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 551 | intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); |
| 552 | intent.putExtra(EXTRA_SET_ALARM, true); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 553 | intent.setComponent(TETHER_SERVICE); |
| 554 | final long ident = Binder.clearCallingIdentity(); |
| 555 | try { |
| 556 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 557 | } finally { |
| 558 | Binder.restoreCallingIdentity(ident); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | private void runSilentTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
| 563 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 564 | sendSilentTetherProvisionIntent(type, proxyReceiver); |
| 565 | } |
| 566 | |
| 567 | private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 568 | Intent intent = new Intent(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 569 | intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); |
| 570 | intent.putExtra(EXTRA_RUN_PROVISION, true); |
| 571 | intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 572 | intent.setComponent(TETHER_SERVICE); |
| 573 | final long ident = Binder.clearCallingIdentity(); |
| 574 | try { |
| 575 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 576 | } finally { |
| 577 | Binder.restoreCallingIdentity(ident); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | private void cancelTetherProvisioningRechecks(int type) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 582 | if (mDeps.isTetheringSupported()) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 583 | Intent intent = new Intent(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 584 | intent.putExtra(EXTRA_REM_TETHER_TYPE, type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 585 | intent.setComponent(TETHER_SERVICE); |
| 586 | final long ident = Binder.clearCallingIdentity(); |
| 587 | try { |
| 588 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 589 | } finally { |
| 590 | Binder.restoreCallingIdentity(ident); |
| 591 | } |
| 592 | } |
| 593 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 594 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 595 | // Used by the SIM card change observation code. |
| 596 | // TODO: De-duplicate with above code, where possible. |
| 597 | private void startProvisionIntent(int tetherType) { |
| 598 | final Intent startProvIntent = new Intent(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 599 | startProvIntent.putExtra(EXTRA_ADD_TETHER_TYPE, tetherType); |
| 600 | startProvIntent.putExtra(EXTRA_RUN_PROVISION, true); |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 601 | startProvIntent.setComponent(TETHER_SERVICE); |
| 602 | mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT); |
| 603 | } |
| 604 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 605 | public int tether(String iface) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 606 | return tether(iface, IpServer.STATE_TETHERED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | private int tether(String iface, int requestedState) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 610 | if (DBG) Log.d(TAG, "Tethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 611 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 612 | TetherState tetherState = mTetherStates.get(iface); |
| 613 | if (tetherState == null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 614 | Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 615 | return TETHER_ERROR_UNKNOWN_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 616 | } |
| 617 | // Ignore the error status of the interface. If the interface is available, |
| 618 | // the errors are referring to past tethering attempts anyway. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 619 | if (tetherState.lastState != IpServer.STATE_AVAILABLE) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 620 | Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 621 | return TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 622 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 623 | // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's |
| 624 | // queue but not yet processed, this will be a no-op and it will not |
| 625 | // return an error. |
| 626 | // |
| 627 | // TODO: reexamine the threading and messaging model. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 628 | tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_REQUESTED, requestedState); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 629 | return TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 630 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 633 | public int untether(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 634 | if (DBG) Log.d(TAG, "Untethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 635 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 636 | TetherState tetherState = mTetherStates.get(iface); |
| 637 | if (tetherState == null) { |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 638 | Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 639 | return TETHER_ERROR_UNKNOWN_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 640 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 641 | if (!tetherState.isCurrentlyServing()) { |
| 642 | Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 643 | return TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 644 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 645 | tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_UNREQUESTED); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 646 | return TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 647 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 650 | public void untetherAll() { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 651 | stopTethering(TETHERING_WIFI); |
| 652 | stopTethering(TETHERING_USB); |
| 653 | stopTethering(TETHERING_BLUETOOTH); |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 656 | public int getLastTetherError(String iface) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 657 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 658 | TetherState tetherState = mTetherStates.get(iface); |
| 659 | if (tetherState == null) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 660 | Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + |
| 661 | ", ignoring"); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 662 | return TETHER_ERROR_UNKNOWN_IFACE; |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 663 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 664 | return tetherState.lastError; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 665 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 668 | // TODO: Figure out how to update for local hotspot mode interfaces. |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 669 | private void sendTetherStateChangedBroadcast() { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 670 | if (!mDeps.isTetheringSupported()) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 671 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 672 | final ArrayList<String> availableList = new ArrayList<>(); |
| 673 | final ArrayList<String> tetherList = new ArrayList<>(); |
| 674 | final ArrayList<String> localOnlyList = new ArrayList<>(); |
| 675 | final ArrayList<String> erroredList = new ArrayList<>(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 676 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 677 | boolean wifiTethered = false; |
| 678 | boolean usbTethered = false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 679 | boolean bluetoothTethered = false; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 680 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 681 | final TetheringConfiguration cfg = mConfig; |
| 682 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 683 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 684 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 685 | TetherState tetherState = mTetherStates.valueAt(i); |
| 686 | String iface = mTetherStates.keyAt(i); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 687 | if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 688 | erroredList.add(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 689 | } else if (tetherState.lastState == IpServer.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 690 | availableList.add(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 691 | } else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 692 | localOnlyList.add(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 693 | } else if (tetherState.lastState == IpServer.STATE_TETHERED) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 694 | if (cfg.isUsb(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 695 | usbTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 696 | } else if (cfg.isWifi(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 697 | wifiTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 698 | } else if (cfg.isBluetooth(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 699 | bluetoothTethered = true; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 700 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 701 | tetherList.add(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 705 | final Intent bcast = new Intent(ACTION_TETHER_STATE_CHANGED); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 706 | bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
Robert Greenwalt | 98c79e5 | 2011-07-28 11:51:11 -0700 | [diff] [blame] | 707 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 708 | bcast.putStringArrayListExtra(EXTRA_AVAILABLE_TETHER, availableList); |
| 709 | bcast.putStringArrayListExtra(EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); |
| 710 | bcast.putStringArrayListExtra(EXTRA_ACTIVE_TETHER, tetherList); |
| 711 | bcast.putStringArrayListExtra(EXTRA_ERRORED_TETHER, erroredList); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 712 | mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL); |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 713 | if (DBG) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 714 | Log.d(TAG, String.format( |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 715 | "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]", |
| 716 | "avail", TextUtils.join(",", availableList), |
| 717 | "local_only", TextUtils.join(",", localOnlyList), |
| 718 | "tether", TextUtils.join(",", tetherList), |
| 719 | "error", TextUtils.join(",", erroredList))); |
Robert Greenwalt | 924cc94 | 2010-06-28 10:26:19 -0700 | [diff] [blame] | 720 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 721 | |
| 722 | if (usbTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 723 | if (wifiTethered || bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 724 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 725 | } else { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 726 | showTetheredNotification(SystemMessage.NOTE_TETHER_USB); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 727 | } |
| 728 | } else if (wifiTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 729 | if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 730 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 731 | } else { |
Vinit Deshpande | 2576a80 | 2014-11-18 13:56:15 -0800 | [diff] [blame] | 732 | /* We now have a status bar icon for WifiTethering, so drop the notification */ |
| 733 | clearTetheredNotification(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 734 | } |
| 735 | } else if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 736 | showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 737 | } else { |
| 738 | clearTetheredNotification(); |
| 739 | } |
| 740 | } |
| 741 | |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 742 | private void showTetheredNotification(int id) { |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 743 | showTetheredNotification(id, true); |
| 744 | } |
| 745 | |
| 746 | @VisibleForTesting |
| 747 | protected void showTetheredNotification(int id, boolean tetheringOn) { |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 748 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 749 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 750 | if (notificationManager == null) { |
| 751 | return; |
| 752 | } |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 753 | int icon = 0; |
| 754 | switch(id) { |
| 755 | case SystemMessage.NOTE_TETHER_USB: |
| 756 | icon = com.android.internal.R.drawable.stat_sys_tether_usb; |
| 757 | break; |
| 758 | case SystemMessage.NOTE_TETHER_BLUETOOTH: |
| 759 | icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth; |
| 760 | break; |
| 761 | case SystemMessage.NOTE_TETHER_GENERAL: |
| 762 | default: |
| 763 | icon = com.android.internal.R.drawable.stat_sys_tether_general; |
| 764 | break; |
| 765 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 766 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 767 | if (mLastNotificationId != 0) { |
| 768 | if (mLastNotificationId == icon) { |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 769 | return; |
| 770 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 771 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Robert Greenwalt | 3cab6b0 | 2012-10-04 16:44:26 -0700 | [diff] [blame] | 772 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 773 | mLastNotificationId = 0; |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 776 | Intent intent = new Intent(); |
| 777 | intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); |
| 778 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); |
| 779 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 780 | PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0, |
| 781 | null, UserHandle.CURRENT); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 782 | |
| 783 | Resources r = Resources.getSystem(); |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 784 | final CharSequence title; |
| 785 | final CharSequence message; |
| 786 | |
| 787 | if (tetheringOn) { |
| 788 | title = r.getText(com.android.internal.R.string.tethered_notification_title); |
| 789 | message = r.getText(com.android.internal.R.string.tethered_notification_message); |
| 790 | } else { |
| 791 | title = r.getText(com.android.internal.R.string.disable_tether_notification_title); |
| 792 | message = r.getText(com.android.internal.R.string.disable_tether_notification_message); |
| 793 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 794 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 795 | if (mTetheredNotificationBuilder == null) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 796 | mTetheredNotificationBuilder = |
| 797 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 798 | mTetheredNotificationBuilder.setWhen(0) |
| 799 | .setOngoing(true) |
| 800 | .setColor(mContext.getColor( |
| 801 | com.android.internal.R.color.system_notification_accent_color)) |
| 802 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 803 | .setCategory(Notification.CATEGORY_STATUS); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 804 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 805 | mTetheredNotificationBuilder.setSmallIcon(icon) |
| 806 | .setContentTitle(title) |
| 807 | .setContentText(message) |
| 808 | .setContentIntent(pi); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 809 | mLastNotificationId = id; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 810 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 811 | notificationManager.notifyAsUser(null, mLastNotificationId, |
zhouzhijie | 130d459 | 2017-05-18 10:02:59 +0800 | [diff] [blame] | 812 | mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 815 | @VisibleForTesting |
| 816 | protected void clearTetheredNotification() { |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 817 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 818 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 819 | if (notificationManager != null && mLastNotificationId != 0) { |
| 820 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 821 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 822 | mLastNotificationId = 0; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 823 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 826 | private class StateReceiver extends BroadcastReceiver { |
Nick Kralevich | 70c117a | 2014-05-27 15:30:02 -0700 | [diff] [blame] | 827 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 828 | public void onReceive(Context content, Intent intent) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 829 | final String action = intent.getAction(); |
| 830 | if (action == null) return; |
| 831 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 832 | if (action.equals(UsbManager.ACTION_USB_STATE)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 833 | handleUsbAction(intent); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 834 | } else if (action.equals(CONNECTIVITY_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 835 | handleConnectivityAction(intent); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 836 | } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 837 | handleWifiApAction(intent); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 838 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 839 | mLog.log("OBSERVED configuration changed"); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 840 | updateConfiguration(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 843 | |
| 844 | private void handleConnectivityAction(Intent intent) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 845 | final NetworkInfo networkInfo = |
| 846 | (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 847 | if (networkInfo == null || |
| 848 | networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) { |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString()); |
| 853 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); |
| 854 | } |
| 855 | |
| 856 | private void handleUsbAction(Intent intent) { |
| 857 | final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 858 | final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 859 | final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 860 | |
| 861 | mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s", |
| 862 | usbConnected, usbConfigured, rndisEnabled)); |
| 863 | |
| 864 | // There are three types of ACTION_USB_STATE: |
| 865 | // |
| 866 | // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0) |
| 867 | // Meaning: USB connection has ended either because of |
| 868 | // software reset or hard unplug. |
| 869 | // |
| 870 | // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0) |
| 871 | // Meaning: the first stage of USB protocol handshake has |
| 872 | // occurred but it is not complete. |
| 873 | // |
| 874 | // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1) |
| 875 | // Meaning: the USB handshake is completely done and all the |
| 876 | // functions are ready to use. |
| 877 | // |
| 878 | // For more explanation, see b/62552150 . |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 879 | synchronized (Tethering.this.mPublicSync) { |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 880 | if (!usbConnected && mRndisEnabled) { |
| 881 | // Turn off tethering if it was enabled and there is a disconnect. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 882 | tetherMatchingInterfaces(IpServer.STATE_AVAILABLE, TETHERING_USB); |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 883 | } else if (usbConfigured && rndisEnabled) { |
| 884 | // Tether if rndis is enabled and usb is configured. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 885 | tetherMatchingInterfaces(IpServer.STATE_TETHERED, TETHERING_USB); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 886 | } |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 887 | mRndisEnabled = usbConfigured && rndisEnabled; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | private void handleWifiApAction(Intent intent) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 892 | final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); |
| 893 | final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME); |
| 894 | final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED); |
| 895 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 896 | synchronized (Tethering.this.mPublicSync) { |
| 897 | switch (curState) { |
| 898 | case WifiManager.WIFI_AP_STATE_ENABLING: |
| 899 | // We can see this state on the way to both enabled and failure states. |
| 900 | break; |
| 901 | case WifiManager.WIFI_AP_STATE_ENABLED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 902 | enableWifiIpServingLocked(ifname, ipmode); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 903 | break; |
| 904 | case WifiManager.WIFI_AP_STATE_DISABLED: |
| 905 | case WifiManager.WIFI_AP_STATE_DISABLING: |
| 906 | case WifiManager.WIFI_AP_STATE_FAILED: |
| 907 | default: |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 908 | disableWifiIpServingLocked(ifname, curState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 909 | break; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 915 | @VisibleForTesting |
| 916 | protected static class TetheringUserRestrictionListener implements UserRestrictionsListener { |
| 917 | private final Tethering mWrapper; |
| 918 | |
| 919 | public TetheringUserRestrictionListener(Tethering wrapper) { |
| 920 | mWrapper = wrapper; |
| 921 | } |
| 922 | |
| 923 | public void onUserRestrictionsChanged(int userId, |
| 924 | Bundle newRestrictions, |
| 925 | Bundle prevRestrictions) { |
| 926 | final boolean newlyDisallowed = |
| 927 | newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING); |
| 928 | final boolean previouslyDisallowed = |
| 929 | prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING); |
| 930 | final boolean tetheringDisallowedChanged = (newlyDisallowed != previouslyDisallowed); |
| 931 | |
| 932 | if (!tetheringDisallowedChanged) { |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | mWrapper.clearTetheredNotification(); |
| 937 | final boolean isTetheringActiveOnDevice = (mWrapper.getTetheredIfaces().length != 0); |
| 938 | |
| 939 | if (newlyDisallowed && isTetheringActiveOnDevice) { |
| 940 | mWrapper.showTetheredNotification( |
| 941 | com.android.internal.R.drawable.stat_sys_tether_general, false); |
| 942 | mWrapper.untetherAll(); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 947 | private void disableWifiIpServingLocked(String ifname, int apState) { |
| 948 | mLog.log("Canceling WiFi tethering request - AP_STATE=" + apState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 949 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 950 | // Regardless of whether we requested this transition, the AP has gone |
| 951 | // down. Don't try to tether again unless we're requested to do so. |
| 952 | // TODO: Remove this altogether, once Wi-Fi reliably gives us an |
| 953 | // interface name with every broadcast. |
| 954 | mWifiTetherRequested = false; |
| 955 | |
| 956 | if (!TextUtils.isEmpty(ifname)) { |
| 957 | final TetherState ts = mTetherStates.get(ifname); |
| 958 | if (ts != null) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 959 | ts.ipServer.unwanted(); |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 960 | return; |
| 961 | } |
| 962 | } |
| 963 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 964 | for (int i = 0; i < mTetherStates.size(); i++) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 965 | final IpServer ipServer = mTetherStates.valueAt(i).ipServer; |
| 966 | if (ipServer.interfaceType() == TETHERING_WIFI) { |
| 967 | ipServer.unwanted(); |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 968 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 969 | } |
| 970 | } |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 971 | |
| 972 | mLog.log("Error disabling Wi-Fi IP serving; " + |
| 973 | (TextUtils.isEmpty(ifname) ? "no interface name specified" |
| 974 | : "specified interface: " + ifname)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | private void enableWifiIpServingLocked(String ifname, int wifiIpMode) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 978 | // Map wifiIpMode values to IpServer.Callback serving states, inferring |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 979 | // from mWifiTetherRequested as a final "best guess". |
| 980 | final int ipServingMode; |
| 981 | switch (wifiIpMode) { |
| 982 | case IFACE_IP_MODE_TETHERED: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 983 | ipServingMode = IpServer.STATE_TETHERED; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 984 | break; |
| 985 | case IFACE_IP_MODE_LOCAL_ONLY: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 986 | ipServingMode = IpServer.STATE_LOCAL_ONLY; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 987 | break; |
| 988 | default: |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 989 | mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode); |
| 990 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (!TextUtils.isEmpty(ifname)) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 994 | maybeTrackNewInterfaceLocked(ifname, TETHERING_WIFI); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 995 | changeInterfaceState(ifname, ipServingMode); |
| 996 | } else { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 997 | mLog.e(String.format( |
| 998 | "Cannot enable IP serving in mode %s on missing interface name", |
| 999 | ipServingMode)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1003 | // TODO: Consider renaming to something more accurate in its description. |
| 1004 | // This method: |
| 1005 | // - allows requesting either tethering or local hotspot serving states |
| 1006 | // - handles both enabling and disabling serving states |
| 1007 | // - only tethers the first matching interface in listInterfaces() |
| 1008 | // order of a given type |
| 1009 | private void tetherMatchingInterfaces(int requestedState, int interfaceType) { |
| 1010 | if (VDBG) { |
| 1011 | Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")"); |
| 1012 | } |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1013 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1014 | String[] ifaces = null; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1015 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1016 | ifaces = mNMService.listInterfaces(); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1017 | } catch (Exception e) { |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1018 | Log.e(TAG, "Error listing Interfaces", e); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1019 | return; |
| 1020 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1021 | String chosenIface = null; |
| 1022 | if (ifaces != null) { |
| 1023 | for (String iface : ifaces) { |
| 1024 | if (ifaceNameToType(iface) == interfaceType) { |
| 1025 | chosenIface = iface; |
| 1026 | break; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1030 | if (chosenIface == null) { |
| 1031 | Log.e(TAG, "could not find iface of type " + interfaceType); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1035 | changeInterfaceState(chosenIface, requestedState); |
| 1036 | } |
| 1037 | |
| 1038 | private void changeInterfaceState(String ifname, int requestedState) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1039 | final int result; |
| 1040 | switch (requestedState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1041 | case IpServer.STATE_UNAVAILABLE: |
| 1042 | case IpServer.STATE_AVAILABLE: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1043 | result = untether(ifname); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1044 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1045 | case IpServer.STATE_TETHERED: |
| 1046 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1047 | result = tether(ifname, requestedState); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1048 | break; |
| 1049 | default: |
| 1050 | Log.wtf(TAG, "Unknown interface state: " + requestedState); |
| 1051 | return; |
| 1052 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1053 | if (result != TETHER_ERROR_NO_ERROR) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1054 | Log.e(TAG, "unable start or stop tethering on iface " + ifname); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1055 | return; |
| 1056 | } |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1059 | public TetheringConfiguration getTetheringConfiguration() { |
| 1060 | return mConfig; |
| 1061 | } |
| 1062 | |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 1063 | public boolean hasTetherableConfiguration() { |
| 1064 | final TetheringConfiguration cfg = mConfig; |
| 1065 | final boolean hasDownstreamConfiguration = |
| 1066 | (cfg.tetherableUsbRegexs.length != 0) || |
| 1067 | (cfg.tetherableWifiRegexs.length != 0) || |
| 1068 | (cfg.tetherableBluetoothRegexs.length != 0); |
| 1069 | final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty(); |
| 1070 | |
| 1071 | return hasDownstreamConfiguration && hasUpstreamConfiguration; |
| 1072 | } |
| 1073 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1074 | // TODO - update callers to use getTetheringConfiguration(), |
| 1075 | // which has only final members. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1076 | public String[] getTetherableUsbRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1077 | return copy(mConfig.tetherableUsbRegexs); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1080 | public String[] getTetherableWifiRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1081 | return copy(mConfig.tetherableWifiRegexs); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1084 | public String[] getTetherableBluetoothRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1085 | return copy(mConfig.tetherableBluetoothRegexs); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1088 | public int setUsbTethering(boolean enable) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1089 | if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 1090 | UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1091 | synchronized (mPublicSync) { |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 1092 | usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_RNDIS |
| 1093 | : UsbManager.FUNCTION_NONE); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1094 | } |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1095 | return TETHER_ERROR_NO_ERROR; |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1098 | // TODO review API - figure out how to delete these entirely. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1099 | public String[] getTetheredIfaces() { |
| 1100 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1101 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1102 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1103 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1104 | if (tetherState.lastState == IpServer.STATE_TETHERED) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1105 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1108 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1109 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | public String[] getTetherableIfaces() { |
| 1113 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1114 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1115 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1116 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1117 | if (tetherState.lastState == IpServer.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1118 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1122 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1125 | public String[] getTetheredDhcpRanges() { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1126 | // TODO: this is only valid for the old DHCP server. Latest search suggests it is only used |
| 1127 | // by WifiP2pServiceImpl to start dnsmasq: remove/deprecate after migrating callers. |
| 1128 | return mConfig.legacyDhcpRanges; |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1131 | public String[] getErroredIfaces() { |
| 1132 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1133 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1134 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1135 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1136 | if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1137 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1141 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1144 | private void logMessage(State state, int what) { |
| 1145 | mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what))); |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1146 | } |
| 1147 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1148 | private boolean upstreamWanted() { |
| 1149 | if (!mForwardedDownstreams.isEmpty()) return true; |
| 1150 | |
| 1151 | synchronized (mPublicSync) { |
Jerry Zhang | 656a7bc | 2017-12-20 14:26:39 -0800 | [diff] [blame] | 1152 | return mWifiTetherRequested; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1156 | // Needed because the canonical source of upstream truth is just the |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1157 | // upstream interface set, |mCurrentUpstreamIfaceSet|. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1158 | private boolean pertainsToCurrentUpstream(NetworkState ns) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1159 | if (ns != null && ns.linkProperties != null && mCurrentUpstreamIfaceSet != null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1160 | for (String ifname : ns.linkProperties.getAllInterfaceNames()) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1161 | if (mCurrentUpstreamIfaceSet.ifnames.contains(ifname)) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1162 | return true; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1163 | } |
| 1164 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1165 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1166 | return false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1167 | } |
| 1168 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1169 | private void reevaluateSimCardProvisioning() { |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 1170 | if (!mConfig.hasMobileHotspotProvisionApp()) return; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 1171 | if (carrierConfigAffirmsEntitlementCheckNotRequired()) return; |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1172 | |
| 1173 | ArrayList<Integer> tethered = new ArrayList<>(); |
| 1174 | synchronized (mPublicSync) { |
| 1175 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1176 | TetherState tetherState = mTetherStates.valueAt(i); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1177 | if (tetherState.lastState != IpServer.STATE_TETHERED) { |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1178 | continue; // Skip interfaces that aren't tethered. |
| 1179 | } |
| 1180 | String iface = mTetherStates.keyAt(i); |
| 1181 | int interfaceType = ifaceNameToType(iface); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1182 | if (interfaceType != TETHERING_INVALID) { |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1183 | tethered.add(interfaceType); |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | for (int tetherType : tethered) { |
| 1189 | startProvisionIntent(tetherType); |
| 1190 | } |
| 1191 | } |
| 1192 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1193 | class TetherMasterSM extends StateMachine { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1194 | private static final int BASE_MASTER = Protocol.BASE_TETHERING; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1195 | // an interface SM has requested Tethering/Local Hotspot |
| 1196 | static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1; |
| 1197 | // an interface SM has unrequested Tethering/Local Hotspot |
| 1198 | static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1199 | // upstream connection change - do the right thing |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1200 | static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1201 | // 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] | 1202 | static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1203 | // Events from NetworkCallbacks that we process on the master state |
| 1204 | // machine thread on behalf of the UpstreamNetworkMonitor. |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1205 | static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1206 | // we treated the error and want now to clear it |
| 1207 | static final int CMD_CLEAR_ERROR = BASE_MASTER + 6; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1208 | static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1209 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1210 | private final State mInitialState; |
| 1211 | private final State mTetherModeAliveState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1212 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1213 | private final State mSetIpForwardingEnabledErrorState; |
| 1214 | private final State mSetIpForwardingDisabledErrorState; |
| 1215 | private final State mStartTetheringErrorState; |
| 1216 | private final State mStopTetheringErrorState; |
| 1217 | private final State mSetDnsForwardersErrorState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1218 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1219 | // This list is a little subtle. It contains all the interfaces that currently are |
| 1220 | // requesting tethering, regardless of whether these interfaces are still members of |
| 1221 | // mTetherStates. This allows us to maintain the following predicates: |
| 1222 | // |
| 1223 | // 1) mTetherStates contains the set of all currently existing, tetherable, link state up |
| 1224 | // interfaces. |
| 1225 | // 2) mNotifyList contains all state machines that may have outstanding tethering state |
| 1226 | // that needs to be torn down. |
| 1227 | // |
| 1228 | // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList |
| 1229 | // so that the garbage collector does not clean up the state machine before it has a chance |
| 1230 | // to tear itself down. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1231 | private final ArrayList<IpServer> mNotifyList; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1232 | private final IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1233 | private final OffloadWrapper mOffload; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1234 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1235 | private static final int UPSTREAM_SETTLE_TIME_MS = 10000; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1236 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 1237 | TetherMasterSM(String name, Looper looper, TetheringDependencies deps) { |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 1238 | super(name, looper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1239 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1240 | mInitialState = new InitialState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1241 | mTetherModeAliveState = new TetherModeAliveState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1242 | mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1243 | mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1244 | mStartTetheringErrorState = new StartTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1245 | mStopTetheringErrorState = new StopTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1246 | mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1247 | |
| 1248 | addState(mInitialState); |
| 1249 | addState(mTetherModeAliveState); |
| 1250 | addState(mSetIpForwardingEnabledErrorState); |
| 1251 | addState(mSetIpForwardingDisabledErrorState); |
| 1252 | addState(mStartTetheringErrorState); |
| 1253 | addState(mStopTetheringErrorState); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1254 | addState(mSetDnsForwardersErrorState); |
| 1255 | |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1256 | mNotifyList = new ArrayList<>(); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 1257 | mIPv6TetheringCoordinator = deps.getIPv6TetheringCoordinator(mNotifyList, mLog); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1258 | mOffload = new OffloadWrapper(); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1259 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1260 | setInitialState(mInitialState); |
| 1261 | } |
| 1262 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1263 | class InitialState extends State { |
| 1264 | @Override |
| 1265 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1266 | logMessage(this, message.what); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1267 | switch (message.what) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1268 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
| 1269 | final IpServer who = (IpServer) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1270 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
| 1271 | handleInterfaceServingStateActive(message.arg1, who); |
| 1272 | transitionTo(mTetherModeAliveState); |
| 1273 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1274 | } |
| 1275 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
| 1276 | final IpServer who = (IpServer) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1277 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
| 1278 | handleInterfaceServingStateInactive(who); |
| 1279 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1280 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1281 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: |
| 1282 | // Silently ignore these for now. |
| 1283 | break; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1284 | default: |
| 1285 | return NOT_HANDLED; |
| 1286 | } |
| 1287 | return HANDLED; |
| 1288 | } |
| 1289 | } |
| 1290 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1291 | protected boolean turnOnMasterTetherSettings() { |
| 1292 | final TetheringConfiguration cfg = mConfig; |
| 1293 | try { |
| 1294 | mNMService.setIpForwardingEnabled(true); |
| 1295 | } catch (Exception e) { |
| 1296 | mLog.e(e); |
| 1297 | transitionTo(mSetIpForwardingEnabledErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1300 | // TODO: Randomize DHCPv4 ranges, especially in hotspot mode. |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1301 | // Legacy DHCP server is disabled if passed an empty ranges array |
| 1302 | final String[] dhcpRanges = cfg.enableLegacyDhcpServer |
| 1303 | ? cfg.legacyDhcpRanges |
| 1304 | : new String[0]; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1305 | try { |
| 1306 | // TODO: Find a more accurate method name (startDHCPv4()?). |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1307 | mNMService.startTethering(dhcpRanges); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1308 | } catch (Exception e) { |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1309 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1310 | mNMService.stopTethering(); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 1311 | mNMService.startTethering(dhcpRanges); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1312 | } catch (Exception ee) { |
| 1313 | mLog.e(ee); |
| 1314 | transitionTo(mStartTetheringErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1317 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1318 | mLog.log("SET master tether settings: ON"); |
| 1319 | return true; |
| 1320 | } |
Robert Greenwalt | 1039872 | 2010-12-17 15:20:36 -0800 | [diff] [blame] | 1321 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1322 | protected boolean turnOffMasterTetherSettings() { |
| 1323 | try { |
| 1324 | mNMService.stopTethering(); |
| 1325 | } catch (Exception e) { |
| 1326 | mLog.e(e); |
| 1327 | transitionTo(mStopTetheringErrorState); |
| 1328 | return false; |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1329 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1330 | try { |
| 1331 | mNMService.setIpForwardingEnabled(false); |
| 1332 | } catch (Exception e) { |
| 1333 | mLog.e(e); |
| 1334 | transitionTo(mSetIpForwardingDisabledErrorState); |
| 1335 | return false; |
| 1336 | } |
| 1337 | transitionTo(mInitialState); |
| 1338 | mLog.log("SET master tether settings: OFF"); |
| 1339 | return true; |
| 1340 | } |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1341 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1342 | protected void chooseUpstreamType(boolean tryCell) { |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 1343 | // We rebuild configuration on ACTION_CONFIGURATION_CHANGED, but we |
| 1344 | // do not currently know how to watch for changes in DUN settings. |
| 1345 | maybeUpdateConfiguration(); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1346 | |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1347 | final TetheringConfiguration config = mConfig; |
| 1348 | final NetworkState ns = (config.chooseUpstreamAutomatically) |
| 1349 | ? mUpstreamNetworkMonitor.getCurrentPreferredUpstream() |
| 1350 | : mUpstreamNetworkMonitor.selectPreferredUpstreamType( |
| 1351 | config.preferredUpstreamIfaceTypes); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1352 | if (ns == null) { |
| 1353 | if (tryCell) { |
| 1354 | mUpstreamNetworkMonitor.registerMobileNetworkRequest(); |
| 1355 | // We think mobile should be coming up; don't set a retry. |
| 1356 | } else { |
| 1357 | sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1358 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1359 | } |
Erik Kline | 60caf7d | 2017-10-10 11:54:08 +0900 | [diff] [blame] | 1360 | mUpstreamNetworkMonitor.setCurrentUpstream((ns != null) ? ns.network : null); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1361 | setUpstreamNetwork(ns); |
| 1362 | } |
| 1363 | |
| 1364 | protected void setUpstreamNetwork(NetworkState ns) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1365 | InterfaceSet ifaces = null; |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 1366 | if (ns != null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1367 | // Find the interface with the default IPv4 route. It may be the |
| 1368 | // interface described by linkProperties, or one of the interfaces |
| 1369 | // stacked on top of it. |
Erik Kline | e8bdb40 | 2018-02-23 14:16:06 -0800 | [diff] [blame] | 1370 | mLog.i("Looking for default routes on: " + ns.linkProperties); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1371 | ifaces = TetheringInterfaceUtils.getTetheringInterfaces(ns); |
| 1372 | mLog.i("Found upstream interface(s): " + ifaces); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1373 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 1374 | |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1375 | if (ifaces != null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1376 | setDnsForwarders(ns.network, ns.linkProperties); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1377 | } |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1378 | notifyDownstreamsOfNewUpstreamIface(ifaces); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1379 | if (ns != null && pertainsToCurrentUpstream(ns)) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1380 | // If we already have NetworkState for this network update it immediately. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1381 | handleNewUpstreamNetworkState(ns); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1382 | } else if (mCurrentUpstreamIfaceSet == null) { |
| 1383 | // There are no available upstream networks. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1384 | handleNewUpstreamNetworkState(null); |
| 1385 | } |
| 1386 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1387 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1388 | protected void setDnsForwarders(final Network network, final LinkProperties lp) { |
| 1389 | // TODO: Set v4 and/or v6 DNS per available connectivity. |
| 1390 | String[] dnsServers = mConfig.defaultIPv4DNS; |
| 1391 | final Collection<InetAddress> dnses = lp.getDnsServers(); |
| 1392 | // TODO: Properly support the absence of DNS servers. |
| 1393 | if (dnses != null && !dnses.isEmpty()) { |
| 1394 | // TODO: remove this invocation of NetworkUtils.makeStrings(). |
| 1395 | dnsServers = NetworkUtils.makeStrings(dnses); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1396 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1397 | try { |
| 1398 | mNMService.setDnsForwarders(network, dnsServers); |
| 1399 | mLog.log(String.format( |
| 1400 | "SET DNS forwarders: network=%s dnsServers=%s", |
| 1401 | network, Arrays.toString(dnsServers))); |
| 1402 | } catch (Exception e) { |
| 1403 | // TODO: Investigate how this can fail and what exactly |
| 1404 | // happens if/when such failures occur. |
| 1405 | mLog.e("setting DNS forwarders failed, " + e); |
| 1406 | transitionTo(mSetDnsForwardersErrorState); |
| 1407 | } |
| 1408 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1409 | |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1410 | protected void notifyDownstreamsOfNewUpstreamIface(InterfaceSet ifaces) { |
| 1411 | mCurrentUpstreamIfaceSet = ifaces; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1412 | for (IpServer ipServer : mNotifyList) { |
| 1413 | ipServer.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, ifaces); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1414 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1415 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1416 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1417 | protected void handleNewUpstreamNetworkState(NetworkState ns) { |
| 1418 | mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1419 | mOffload.updateUpstreamNetworkState(ns); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1420 | } |
| 1421 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1422 | private void handleInterfaceServingStateActive(int mode, IpServer who) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1423 | if (mNotifyList.indexOf(who) < 0) { |
| 1424 | mNotifyList.add(who); |
| 1425 | mIPv6TetheringCoordinator.addActiveDownstream(who, mode); |
| 1426 | } |
| 1427 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1428 | if (mode == IpServer.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1429 | // No need to notify OffloadController just yet as there are no |
| 1430 | // "offload-able" prefixes to pass along. This will handled |
| 1431 | // when the TISM informs Tethering of its LinkProperties. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1432 | mForwardedDownstreams.add(who); |
| 1433 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1434 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1435 | mForwardedDownstreams.remove(who); |
| 1436 | } |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1437 | |
| 1438 | // 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] | 1439 | if (who.interfaceType() == TETHERING_WIFI) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1440 | final WifiManager mgr = getWifiManager(); |
| 1441 | final String iface = who.interfaceName(); |
| 1442 | switch (mode) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1443 | case IpServer.STATE_TETHERED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1444 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1445 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1446 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1447 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1448 | break; |
| 1449 | default: |
| 1450 | Log.wtf(TAG, "Unknown active serving mode: " + mode); |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1454 | } |
| 1455 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1456 | private void handleInterfaceServingStateInactive(IpServer who) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1457 | mNotifyList.remove(who); |
| 1458 | mIPv6TetheringCoordinator.removeActiveDownstream(who); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1459 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1460 | mForwardedDownstreams.remove(who); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1461 | |
| 1462 | // If this is a Wi-Fi interface, tell WifiManager of any errors. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1463 | if (who.interfaceType() == TETHERING_WIFI) { |
| 1464 | if (who.lastError() != TETHER_ERROR_NO_ERROR) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1465 | getWifiManager().updateInterfaceIpState( |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1466 | who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1467 | } |
| 1468 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1469 | } |
| 1470 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1471 | private void handleUpstreamNetworkMonitorCallback(int arg1, Object o) { |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1472 | if (arg1 == UpstreamNetworkMonitor.NOTIFY_LOCAL_PREFIXES) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1473 | mOffload.sendOffloadExemptPrefixes((Set<IpPrefix>) o); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1474 | return; |
| 1475 | } |
| 1476 | |
| 1477 | final NetworkState ns = (NetworkState) o; |
| 1478 | |
| 1479 | if (ns == null || !pertainsToCurrentUpstream(ns)) { |
| 1480 | // TODO: In future, this is where upstream evaluation and selection |
| 1481 | // could be handled for notifications which include sufficient data. |
| 1482 | // For example, after CONNECTIVITY_ACTION listening is removed, here |
| 1483 | // is where we could observe a Wi-Fi network becoming available and |
| 1484 | // passing validation. |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1485 | if (mCurrentUpstreamIfaceSet == null) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1486 | // If we have no upstream interface, try to run through upstream |
| 1487 | // selection again. If, for example, IPv4 connectivity has shown up |
| 1488 | // after IPv6 (e.g., 464xlat became available) we want the chance to |
| 1489 | // notice and act accordingly. |
| 1490 | chooseUpstreamType(false); |
| 1491 | } |
| 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | switch (arg1) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1496 | case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES: |
| 1497 | handleNewUpstreamNetworkState(ns); |
| 1498 | break; |
| 1499 | case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES: |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1500 | chooseUpstreamType(false); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1501 | break; |
| 1502 | case UpstreamNetworkMonitor.EVENT_ON_LOST: |
| 1503 | // TODO: Re-evaluate possible upstreams. Currently upstream |
| 1504 | // reevaluation is triggered via received CONNECTIVITY_ACTION |
| 1505 | // broadcasts that result in being passed a |
| 1506 | // TetherMasterSM.CMD_UPSTREAM_CHANGED. |
| 1507 | handleNewUpstreamNetworkState(null); |
| 1508 | break; |
| 1509 | default: |
| 1510 | mLog.e("Unknown arg1 value: " + arg1); |
| 1511 | break; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | class TetherModeAliveState extends State { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1516 | boolean mUpstreamWanted = false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1517 | boolean mTryCell = true; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 1518 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1519 | @Override |
| 1520 | public void enter() { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1521 | // If turning on master tether settings fails, we have already |
| 1522 | // transitioned to an error state; exit early. |
| 1523 | if (!turnOnMasterTetherSettings()) { |
| 1524 | return; |
| 1525 | } |
| 1526 | |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1527 | mUpstreamNetworkMonitor.start(mDeps.getDefaultNetworkRequest()); |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 1528 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1529 | // TODO: De-duplicate with updateUpstreamWanted() below. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1530 | if (upstreamWanted()) { |
| 1531 | mUpstreamWanted = true; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1532 | mOffload.start(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1533 | chooseUpstreamType(true); |
| 1534 | mTryCell = false; |
| 1535 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1536 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1537 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1538 | @Override |
| 1539 | public void exit() { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1540 | mOffload.stop(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1541 | mUpstreamNetworkMonitor.stop(); |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1542 | notifyDownstreamsOfNewUpstreamIface(null); |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1543 | handleNewUpstreamNetworkState(null); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1544 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1545 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1546 | private boolean updateUpstreamWanted() { |
| 1547 | final boolean previousUpstreamWanted = mUpstreamWanted; |
| 1548 | mUpstreamWanted = upstreamWanted(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1549 | if (mUpstreamWanted != previousUpstreamWanted) { |
| 1550 | if (mUpstreamWanted) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1551 | mOffload.start(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1552 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1553 | mOffload.stop(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1554 | } |
| 1555 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1556 | return previousUpstreamWanted; |
| 1557 | } |
| 1558 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1559 | @Override |
| 1560 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1561 | logMessage(this, message.what); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1562 | boolean retValue = true; |
| 1563 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1564 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1565 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1566 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1567 | handleInterfaceServingStateActive(message.arg1, who); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1568 | who.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1569 | mCurrentUpstreamIfaceSet); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1570 | // If there has been a change and an upstream is now |
| 1571 | // desired, kick off the selection process. |
| 1572 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1573 | if (!previousUpstreamWanted && mUpstreamWanted) { |
| 1574 | chooseUpstreamType(true); |
| 1575 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1576 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1577 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1578 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1579 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1580 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1581 | handleInterfaceServingStateInactive(who); |
| 1582 | |
| 1583 | if (mNotifyList.isEmpty()) { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1584 | // This transitions us out of TetherModeAliveState, |
| 1585 | // either to InitialState or an error state. |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1586 | turnOffMasterTetherSettings(); |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | if (DBG) { |
| 1591 | Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() + |
| 1592 | " live requests:"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1593 | for (IpServer o : mNotifyList) { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1594 | Log.d(TAG, " " + o); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1595 | } |
| 1596 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1597 | // If there has been a change and an upstream is no |
| 1598 | // longer desired, release any mobile requests. |
| 1599 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1600 | if (previousUpstreamWanted && !mUpstreamWanted) { |
| 1601 | mUpstreamNetworkMonitor.releaseMobileNetworkRequest(); |
| 1602 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1603 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1604 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1605 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: { |
| 1606 | final LinkProperties newLp = (LinkProperties) message.obj; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1607 | if (message.arg1 == IpServer.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1608 | mOffload.updateDownstreamLinkProperties(newLp); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1609 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1610 | mOffload.excludeDownstreamInterface(newLp.getInterfaceName()); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1611 | } |
| 1612 | break; |
| 1613 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1614 | case CMD_UPSTREAM_CHANGED: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1615 | updateUpstreamWanted(); |
| 1616 | if (!mUpstreamWanted) break; |
| 1617 | |
Erik Kline | fb41343 | 2017-02-14 18:26:04 +0900 | [diff] [blame] | 1618 | // Need to try DUN immediately if Wi-Fi goes down. |
| 1619 | chooseUpstreamType(true); |
| 1620 | mTryCell = false; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1621 | break; |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1622 | case CMD_RETRY_UPSTREAM: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1623 | updateUpstreamWanted(); |
| 1624 | if (!mUpstreamWanted) break; |
| 1625 | |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1626 | chooseUpstreamType(mTryCell); |
| 1627 | mTryCell = !mTryCell; |
| 1628 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1629 | case EVENT_UPSTREAM_CALLBACK: { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1630 | updateUpstreamWanted(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1631 | if (mUpstreamWanted) { |
| 1632 | handleUpstreamNetworkMonitorCallback(message.arg1, message.obj); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1633 | } |
| 1634 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1635 | } |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1636 | default: |
| 1637 | retValue = false; |
| 1638 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1639 | } |
| 1640 | return retValue; |
| 1641 | } |
| 1642 | } |
| 1643 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1644 | class ErrorState extends State { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1645 | private int mErrorNotification; |
| 1646 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1647 | @Override |
| 1648 | public boolean processMessage(Message message) { |
| 1649 | boolean retValue = true; |
| 1650 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1651 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1652 | IpServer who = (IpServer) message.obj; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1653 | who.sendMessage(mErrorNotification); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1654 | break; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1655 | case CMD_CLEAR_ERROR: |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1656 | mErrorNotification = TETHER_ERROR_NO_ERROR; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1657 | transitionTo(mInitialState); |
| 1658 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1659 | default: |
| 1660 | retValue = false; |
| 1661 | } |
| 1662 | return retValue; |
| 1663 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1664 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1665 | void notify(int msgType) { |
| 1666 | mErrorNotification = msgType; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1667 | for (IpServer ipServer : mNotifyList) { |
| 1668 | ipServer.sendMessage(msgType); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1673 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1674 | class SetIpForwardingEnabledErrorState extends ErrorState { |
| 1675 | @Override |
| 1676 | public void enter() { |
| 1677 | Log.e(TAG, "Error in setIpForwardingEnabled"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1678 | notify(IpServer.CMD_IP_FORWARDING_ENABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | class SetIpForwardingDisabledErrorState extends ErrorState { |
| 1683 | @Override |
| 1684 | public void enter() { |
| 1685 | Log.e(TAG, "Error in setIpForwardingDisabled"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1686 | notify(IpServer.CMD_IP_FORWARDING_DISABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | class StartTetheringErrorState extends ErrorState { |
| 1691 | @Override |
| 1692 | public void enter() { |
| 1693 | Log.e(TAG, "Error in startTethering"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1694 | notify(IpServer.CMD_START_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1695 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1696 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1697 | } catch (Exception e) {} |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | class StopTetheringErrorState extends ErrorState { |
| 1702 | @Override |
| 1703 | public void enter() { |
| 1704 | Log.e(TAG, "Error in stopTethering"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1705 | notify(IpServer.CMD_STOP_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1706 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1707 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1708 | } catch (Exception e) {} |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | class SetDnsForwardersErrorState extends ErrorState { |
| 1713 | @Override |
| 1714 | public void enter() { |
| 1715 | Log.e(TAG, "Error in setDnsForwarders"); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1716 | notify(IpServer.CMD_SET_DNS_FORWARDERS_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1717 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1718 | mNMService.stopTethering(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1719 | } catch (Exception e) {} |
| 1720 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1721 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1722 | } catch (Exception e) {} |
| 1723 | } |
| 1724 | } |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1725 | |
| 1726 | // A wrapper class to handle multiple situations where several calls to |
| 1727 | // the OffloadController need to happen together. |
| 1728 | // |
| 1729 | // TODO: This suggests that the interface between OffloadController and |
| 1730 | // Tethering is in need of improvement. Refactor these calls into the |
| 1731 | // OffloadController implementation. |
| 1732 | class OffloadWrapper { |
| 1733 | public void start() { |
| 1734 | mOffloadController.start(); |
| 1735 | sendOffloadExemptPrefixes(); |
| 1736 | } |
| 1737 | |
| 1738 | public void stop() { |
| 1739 | mOffloadController.stop(); |
| 1740 | } |
| 1741 | |
| 1742 | public void updateUpstreamNetworkState(NetworkState ns) { |
| 1743 | mOffloadController.setUpstreamLinkProperties( |
| 1744 | (ns != null) ? ns.linkProperties : null); |
| 1745 | } |
| 1746 | |
| 1747 | public void updateDownstreamLinkProperties(LinkProperties newLp) { |
| 1748 | // Update the list of offload-exempt prefixes before adding |
| 1749 | // new prefixes on downstream interfaces to the offload HAL. |
| 1750 | sendOffloadExemptPrefixes(); |
| 1751 | mOffloadController.notifyDownstreamLinkProperties(newLp); |
| 1752 | } |
| 1753 | |
| 1754 | public void excludeDownstreamInterface(String ifname) { |
| 1755 | // This and other interfaces may be in local-only hotspot mode; |
| 1756 | // resend all local prefixes to the OffloadController. |
| 1757 | sendOffloadExemptPrefixes(); |
| 1758 | mOffloadController.removeDownstreamInterface(ifname); |
| 1759 | } |
| 1760 | |
| 1761 | public void sendOffloadExemptPrefixes() { |
| 1762 | sendOffloadExemptPrefixes(mUpstreamNetworkMonitor.getLocalPrefixes()); |
| 1763 | } |
| 1764 | |
| 1765 | public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) { |
| 1766 | // Add in well-known minimum set. |
| 1767 | PrefixUtils.addNonForwardablePrefixes(localPrefixes); |
| 1768 | // Add tragically hardcoded prefixes. |
| 1769 | localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX); |
| 1770 | |
| 1771 | // Maybe add prefixes or addresses for downstreams, depending on |
| 1772 | // the IP serving mode of each. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1773 | for (IpServer ipServer : mNotifyList) { |
| 1774 | final LinkProperties lp = ipServer.linkProperties(); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1775 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1776 | switch (ipServer.servingMode()) { |
| 1777 | case IpServer.STATE_UNAVAILABLE: |
| 1778 | case IpServer.STATE_AVAILABLE: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1779 | // No usable LinkProperties in these states. |
| 1780 | continue; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1781 | case IpServer.STATE_TETHERED: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1782 | // Only add IPv4 /32 and IPv6 /128 prefixes. The |
| 1783 | // directly-connected prefixes will be sent as |
| 1784 | // downstream "offload-able" prefixes. |
| 1785 | for (LinkAddress addr : lp.getAllLinkAddresses()) { |
| 1786 | final InetAddress ip = addr.getAddress(); |
| 1787 | if (ip.isLinkLocalAddress()) continue; |
| 1788 | localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip)); |
| 1789 | } |
| 1790 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1791 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1792 | // Add prefixes covering all local IPs. |
| 1793 | localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp)); |
| 1794 | break; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | mOffloadController.setLocalPrefixes(localPrefixes); |
| 1799 | } |
| 1800 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1803 | @Override |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1804 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1805 | // Binder.java closes the resource for us. |
| 1806 | @SuppressWarnings("resource") |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1807 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1808 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1809 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1810 | pw.println("Tethering:"); |
| 1811 | pw.increaseIndent(); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1812 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 1813 | pw.println("Configuration:"); |
| 1814 | pw.increaseIndent(); |
| 1815 | final TetheringConfiguration cfg = mConfig; |
| 1816 | cfg.dump(pw); |
| 1817 | pw.decreaseIndent(); |
| 1818 | |
| 1819 | synchronized (mPublicSync) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1820 | pw.println("Tether state:"); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1821 | pw.increaseIndent(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1822 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1823 | final String iface = mTetherStates.keyAt(i); |
| 1824 | final TetherState tetherState = mTetherStates.valueAt(i); |
| 1825 | pw.print(iface + " - "); |
| 1826 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1827 | switch (tetherState.lastState) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1828 | case IpServer.STATE_UNAVAILABLE: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1829 | pw.print("UnavailableState"); |
| 1830 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1831 | case IpServer.STATE_AVAILABLE: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1832 | pw.print("AvailableState"); |
| 1833 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1834 | case IpServer.STATE_TETHERED: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1835 | pw.print("TetheredState"); |
| 1836 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1837 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1838 | pw.print("LocalHotspotState"); |
| 1839 | break; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1840 | default: |
| 1841 | pw.print("UnknownState"); |
| 1842 | break; |
| 1843 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1844 | pw.println(" - lastError = " + tetherState.lastError); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1845 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1846 | pw.println("Upstream wanted: " + upstreamWanted()); |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1847 | pw.println("Current upstream interface(s): " + mCurrentUpstreamIfaceSet); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1848 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1849 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1850 | |
Lorenzo Colitti | c2519c5 | 2017-07-13 01:48:26 +0900 | [diff] [blame] | 1851 | pw.println("Hardware offload:"); |
| 1852 | pw.increaseIndent(); |
| 1853 | mOffloadController.dump(pw); |
| 1854 | pw.decreaseIndent(); |
| 1855 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1856 | pw.println("Log:"); |
| 1857 | pw.increaseIndent(); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1858 | if (argsContain(args, SHORT_ARG)) { |
| 1859 | pw.println("<log removed for brevity>"); |
| 1860 | } else { |
| 1861 | mLog.dump(fd, pw, args); |
| 1862 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1863 | pw.decreaseIndent(); |
| 1864 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1865 | pw.decreaseIndent(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1866 | } |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 1867 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1868 | private static boolean argsContain(String[] args, String target) { |
| 1869 | for (String arg : args) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 1870 | if (target.equals(arg)) return true; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1871 | } |
| 1872 | return false; |
| 1873 | } |
| 1874 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1875 | private IpServer.Callback makeControlCallback() { |
| 1876 | return new IpServer.Callback() { |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1877 | @Override |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1878 | public void updateInterfaceState(IpServer who, int state, int lastError) { |
| 1879 | notifyInterfaceStateChange(who, state, lastError); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | @Override |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1883 | public void updateLinkProperties(IpServer who, LinkProperties newLp) { |
| 1884 | notifyLinkPropertiesChanged(who, newLp); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1885 | } |
| 1886 | }; |
| 1887 | } |
| 1888 | |
| 1889 | // TODO: Move into TetherMasterSM. |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1890 | private void notifyInterfaceStateChange(IpServer who, int state, int error) { |
| 1891 | final String iface = who.interfaceName(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1892 | synchronized (mPublicSync) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1893 | final TetherState tetherState = mTetherStates.get(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1894 | if (tetherState != null && tetherState.ipServer.equals(who)) { |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1895 | tetherState.lastState = state; |
| 1896 | tetherState.lastError = error; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1897 | } else { |
| 1898 | if (DBG) Log.d(TAG, "got notification from stale iface " + iface); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1902 | 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] | 1903 | |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 1904 | try { |
| 1905 | // Notify that we're tethering (or not) this interface. |
| 1906 | // This is how data saver for instance knows if the user explicitly |
| 1907 | // turned on tethering (thus keeping us from being in data saver mode). |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1908 | mPolicyManager.onTetheringChanged(iface, state == IpServer.STATE_TETHERED); |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 1909 | } catch (RemoteException e) { |
| 1910 | // Not really very much we can do here. |
| 1911 | } |
| 1912 | |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1913 | // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. |
| 1914 | // Thus we give a chance for TetherMasterSM to recover to InitialState |
| 1915 | // by sending CMD_CLEAR_ERROR |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1916 | if (error == TETHER_ERROR_MASTER_ERROR) { |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1917 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); |
| 1918 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1919 | int which; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1920 | switch (state) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1921 | case IpServer.STATE_UNAVAILABLE: |
| 1922 | case IpServer.STATE_AVAILABLE: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1923 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1924 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1925 | case IpServer.STATE_TETHERED: |
| 1926 | case IpServer.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1927 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1928 | break; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1929 | default: |
| 1930 | Log.wtf(TAG, "Unknown interface state: " + state); |
| 1931 | return; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1932 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1933 | mTetherMasterSM.sendMessage(which, state, 0, who); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1934 | sendTetherStateChangedBroadcast(); |
| 1935 | } |
| 1936 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1937 | private void notifyLinkPropertiesChanged(IpServer who, LinkProperties newLp) { |
| 1938 | final String iface = who.interfaceName(); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1939 | final int state; |
| 1940 | synchronized (mPublicSync) { |
| 1941 | final TetherState tetherState = mTetherStates.get(iface); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1942 | if (tetherState != null && tetherState.ipServer.equals(who)) { |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1943 | state = tetherState.lastState; |
| 1944 | } else { |
| 1945 | mLog.log("got notification from stale iface " + iface); |
| 1946 | return; |
| 1947 | } |
| 1948 | } |
| 1949 | |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1950 | mLog.log(String.format( |
| 1951 | "OBSERVED LinkProperties update iface=%s state=%s lp=%s", |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1952 | iface, IpServer.getStateString(state), newLp)); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1953 | final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; |
| 1954 | mTetherMasterSM.sendMessage(which, state, 0, newLp); |
| 1955 | } |
| 1956 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1957 | private void maybeTrackNewInterfaceLocked(final String iface) { |
| 1958 | // If we don't care about this type of interface, ignore. |
| 1959 | final int interfaceType = ifaceNameToType(iface); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1960 | if (interfaceType == TETHERING_INVALID) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1961 | mLog.log(iface + " is not a tetherable iface, ignoring"); |
| 1962 | return; |
| 1963 | } |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 1964 | maybeTrackNewInterfaceLocked(iface, interfaceType); |
| 1965 | } |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1966 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 1967 | private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1968 | // If we have already started a TISM for this interface, skip. |
| 1969 | if (mTetherStates.containsKey(iface)) { |
| 1970 | mLog.log("active iface (" + iface + ") reported as added, ignoring"); |
| 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | mLog.log("adding TetheringInterfaceStateMachine for: " + iface); |
| 1975 | final TetherState tetherState = new TetherState( |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1976 | new IpServer(iface, mLooper, interfaceType, mLog, mNMService, mStatsService, |
| 1977 | makeControlCallback(), mConfig.enableLegacyDhcpServer, |
| 1978 | mDeps.getIpServerDependencies())); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1979 | mTetherStates.put(iface, tetherState); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1980 | tetherState.ipServer.start(); |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 1981 | } |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1982 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1983 | private void stopTrackingInterfaceLocked(final String iface) { |
| 1984 | final TetherState tetherState = mTetherStates.get(iface); |
| 1985 | if (tetherState == null) { |
| 1986 | mLog.log("attempting to remove unknown iface (" + iface + "), ignoring"); |
| 1987 | return; |
| 1988 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1989 | tetherState.ipServer.stop(); |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1990 | mLog.log("removing TetheringInterfaceStateMachine for: " + iface); |
| 1991 | mTetherStates.remove(iface); |
| 1992 | } |
| 1993 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1994 | private static String[] copy(String[] strarray) { |
| 1995 | return Arrays.copyOf(strarray, strarray.length); |
| 1996 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1997 | } |