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