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