blob: a8f7259050c14b1857286dfda7174a0d47234075 [file] [log] [blame]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001/*
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
17package com.android.server.connectivity;
18
Erik Klinec438e302017-07-04 22:02:49 +090019import static android.hardware.usb.UsbManager.USB_CONFIGURED;
Erik Kline2e88b5e2017-01-18 11:57:45 +090020import static android.hardware.usb.UsbManager.USB_CONNECTED;
21import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS;
Erik Kline7a26ba32018-03-09 14:18:02 +090022import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED;
23import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
24import static android.net.ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY;
25import static android.net.ConnectivityManager.EXTRA_ACTIVE_TETHER;
26import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE;
27import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER;
28import static android.net.ConnectivityManager.EXTRA_ERRORED_TETHER;
29import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO;
30import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK;
31import static android.net.ConnectivityManager.EXTRA_REM_TETHER_TYPE;
32import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION;
33import static android.net.ConnectivityManager.EXTRA_SET_ALARM;
34import static android.net.ConnectivityManager.TETHER_ERROR_MASTER_ERROR;
35import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
36import static android.net.ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL;
37import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
38import static android.net.ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
39import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
40import static android.net.ConnectivityManager.TETHERING_INVALID;
41import static android.net.ConnectivityManager.TETHERING_USB;
42import static android.net.ConnectivityManager.TETHERING_WIFI;
Erik Klinee8bdb402018-02-23 14:16:06 -080043import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
Erik Kline2efb8272017-05-31 15:53:53 +090044import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME;
45import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE;
Erik Kline2e88b5e2017-01-18 11:57:45 +090046import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE;
Erik Kline2efb8272017-05-31 15:53:53 +090047import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR;
48import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY;
49import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED;
50import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED;
Erik Kline2e88b5e2017-01-18 11:57:45 +090051import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED;
Erik Klinede637722017-10-12 22:16:01 +090052import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED;
Erik Kline7747fd42017-05-12 16:52:48 +090053import static com.android.server.ConnectivityService.SHORT_ARG;
Erik Kline2e88b5e2017-01-18 11:57:45 +090054
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080055import android.app.Notification;
56import android.app.NotificationManager;
57import android.app.PendingIntent;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080058import android.bluetooth.BluetoothAdapter;
59import android.bluetooth.BluetoothPan;
60import android.bluetooth.BluetoothProfile;
61import android.bluetooth.BluetoothProfile.ServiceListener;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080062import android.content.BroadcastReceiver;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -080063import android.content.ComponentName;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080064import android.content.Context;
65import android.content.Intent;
66import android.content.IntentFilter;
67import android.content.res.Resources;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080068import android.hardware.usb.UsbManager;
Christopher Wileydeebfec2016-09-16 11:14:36 -070069import android.net.INetworkPolicyManager;
Jeff Sharkey367d15a2011-09-22 14:59:51 -070070import android.net.INetworkStatsService;
Erik Kline7a4ccc62018-08-27 17:26:47 +090071import android.net.ip.IpServer;
Erik Kline3a5278f2017-06-24 19:29:10 +090072import android.net.IpPrefix;
Erik Klineed962a82017-07-06 19:49:35 +090073import android.net.LinkAddress;
Robert Greenwalt05d89362011-01-23 16:04:05 -080074import android.net.LinkProperties;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070075import android.net.Network;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080076import android.net.NetworkInfo;
Erik Kline6ff17f72015-12-10 20:42:12 +090077import android.net.NetworkState;
Robert Greenwalte5903732011-02-22 16:00:42 -080078import android.net.NetworkUtils;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090079import android.net.RouteInfo;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +090080import android.net.util.InterfaceSet;
Erik Kline32179ff2017-07-04 18:28:11 +090081import android.net.util.PrefixUtils;
Erik Kline7747fd42017-05-12 16:52:48 +090082import android.net.util.SharedLog;
Erik Klinede637722017-10-12 22:16:01 +090083import android.net.util.VersionedBroadcastListener;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080084import android.net.wifi.WifiManager;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080085import android.os.Binder;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080086import android.os.Bundle;
Erik Klinef4b6e342017-04-25 19:19:59 +090087import android.os.Handler;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080088import android.os.INetworkManagementService;
Robert Greenwaltdfadaea2010-03-11 15:03:08 -080089import android.os.Looper;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080090import android.os.Message;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080091import android.os.Parcel;
Erik Klinede637722017-10-12 22:16:01 +090092import android.os.PersistableBundle;
Christopher Wileydeebfec2016-09-16 11:14:36 -070093import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080094import android.os.ResultReceiver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070095import android.os.UserHandle;
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +010096import android.os.UserManager;
97import android.os.UserManagerInternal;
98import android.os.UserManagerInternal.UserRestrictionsListener;
Jeremy Klein15f3d212016-01-24 17:01:09 -080099import android.provider.Settings;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800100import android.telephony.CarrierConfigManager;
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900101import android.text.TextUtils;
Christopher Wileye9490392016-05-26 15:57:29 -0700102import android.util.ArrayMap;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800103import android.util.Log;
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900104import android.util.SparseArray;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800105
Christopher Wiley497c1472016-10-11 13:26:03 -0700106import com.android.internal.annotations.VisibleForTesting;
Chris Wren282cfef2017-03-27 15:01:44 -0400107import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500108import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600109import com.android.internal.util.DumpUtils;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800110import com.android.internal.util.IndentingPrintWriter;
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900111import com.android.internal.util.MessageUtils;
112import com.android.internal.util.Protocol;
Wink Saville64c42ca2011-04-18 14:55:10 -0700113import com.android.internal.util.State;
114import com.android.internal.util.StateMachine;
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100115import com.android.server.LocalServices;
Erik Kline1eb8c692016-07-08 17:21:26 +0900116import com.android.server.connectivity.tethering.IPv6TetheringCoordinator;
Erik Klinee0cce212017-03-06 14:05:23 +0900117import com.android.server.connectivity.tethering.OffloadController;
Hugo Benichib55fb222017-03-10 14:20:57 +0900118import com.android.server.connectivity.tethering.TetheringConfiguration;
Erik Kline47222fc2017-04-30 19:36:15 +0900119import com.android.server.connectivity.tethering.TetheringDependencies;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +0900120import com.android.server.connectivity.tethering.TetheringInterfaceUtils;
Erik Kline9bba3402017-01-13 16:46:52 +0900121import com.android.server.connectivity.tethering.UpstreamNetworkMonitor;
Lorenzo Colittif4e90ea2013-10-31 23:30:47 +0900122import com.android.server.net.BaseNetworkObserver;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800123
124import java.io.FileDescriptor;
125import java.io.PrintWriter;
Robert Greenwalt4f74d552011-12-19 16:59:31 -0800126import java.net.Inet4Address;
Erik Klinee8bdb402018-02-23 14:16:06 -0800127import java.net.Inet6Address;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800128import java.net.InetAddress;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800129import java.util.ArrayList;
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700130import java.util.Arrays;
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700131import java.util.Collection;
Erik Klineea9cc482017-03-10 19:35:34 +0900132import java.util.HashSet;
Erik Kline3a5278f2017-06-24 19:29:10 +0900133import java.util.Set;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -0800134
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700135
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800136/**
137 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800138 *
Christopher Wiley3b1d9222016-05-20 16:44:04 -0700139 * This class holds much of the business logic to allow Android devices
140 * to act as IP gateways via USB, BT, and WiFi interfaces.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800141 */
Erik Kline6e9a1012017-06-06 19:24:21 +0900142public class Tethering extends BaseNetworkObserver {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800143
Hugo Benichib55fb222017-03-10 14:20:57 +0900144 private final static String TAG = Tethering.class.getSimpleName();
Joe Onorato12acbd72016-02-01 17:49:31 -0800145 private final static boolean DBG = false;
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800146 private final static boolean VDBG = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800147
Christopher Wiley497c1472016-10-11 13:26:03 -0700148 protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning";
149
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900150 private static final Class[] messageClasses = {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900151 Tethering.class, TetherMasterSM.class, IpServer.class
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900152 };
153 private static final SparseArray<String> sMagicDecoderRing =
154 MessageUtils.findMessageNames(messageClasses);
155
Hugo Benichib55fb222017-03-10 14:20:57 +0900156 // {@link ComponentName} of the Service used to run tether provisioning.
157 private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources
158 .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable));
159
160 private static class TetherState {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900161 public final IpServer ipServer;
Hugo Benichib55fb222017-03-10 14:20:57 +0900162 public int lastState;
163 public int lastError;
Erik Klineea9cc482017-03-10 19:35:34 +0900164
Erik Kline7a4ccc62018-08-27 17:26:47 +0900165 public TetherState(IpServer ipServer) {
166 this.ipServer = ipServer;
Hugo Benichib55fb222017-03-10 14:20:57 +0900167 // Assume all state machines start out available and with no errors.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900168 lastState = IpServer.STATE_AVAILABLE;
Erik Kline7a26ba32018-03-09 14:18:02 +0900169 lastError = TETHER_ERROR_NO_ERROR;
Hugo Benichib55fb222017-03-10 14:20:57 +0900170 }
Erik Klineea9cc482017-03-10 19:35:34 +0900171
172 public boolean isCurrentlyServing() {
173 switch (lastState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900174 case IpServer.STATE_TETHERED:
175 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +0900176 return true;
177 default:
178 return false;
179 }
180 }
Hugo Benichib55fb222017-03-10 14:20:57 +0900181 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700182
Erik Kline7747fd42017-05-12 16:52:48 +0900183 private final SharedLog mLog = new SharedLog(TAG);
Erik Kline1fdc2e22017-05-08 17:56:35 +0900184
Robert Greenwaltb4453622011-11-03 16:01:40 -0700185 // used to synchronize public access to members
Erik Kline6ff17f72015-12-10 20:42:12 +0900186 private final Object mPublicSync;
Hugo Benichib55fb222017-03-10 14:20:57 +0900187 private final Context mContext;
188 private final ArrayMap<String, TetherState> mTetherStates;
189 private final BroadcastReceiver mStateReceiver;
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700190 private final INetworkManagementService mNMService;
191 private final INetworkStatsService mStatsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700192 private final INetworkPolicyManager mPolicyManager;
Erik Kline6ff17f72015-12-10 20:42:12 +0900193 private final Looper mLooper;
Christopher Wiley497c1472016-10-11 13:26:03 -0700194 private final MockableSystemProperties mSystemProperties;
Erik Kline6ff17f72015-12-10 20:42:12 +0900195 private final StateMachine mTetherMasterSM;
Erik Klinee0cce212017-03-06 14:05:23 +0900196 private final OffloadController mOffloadController;
Erik Kline6ff17f72015-12-10 20:42:12 +0900197 private final UpstreamNetworkMonitor mUpstreamNetworkMonitor;
Erik Kline6e9a1012017-06-06 19:24:21 +0900198 // TODO: Figure out how to merge this and other downstream-tracking objects
199 // into a single coherent structure.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900200 private final HashSet<IpServer> mForwardedDownstreams;
Erik Klinede637722017-10-12 22:16:01 +0900201 private final VersionedBroadcastListener mCarrierConfigChange;
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900202 private final TetheringDependencies mDeps;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800203
Hugo Benichib55fb222017-03-10 14:20:57 +0900204 private volatile TetheringConfiguration mConfig;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +0900205 private InterfaceSet mCurrentUpstreamIfaceSet;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400206 private Notification.Builder mTetheredNotificationBuilder;
207 private int mLastNotificationId;
Erik Klineea9cc482017-03-10 19:35:34 +0900208
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700209 private boolean mRndisEnabled; // track the RNDIS function enabled state
Erik Kline6e9a1012017-06-06 19:24:21 +0900210 // True iff. WiFi tethering should be started when soft AP is ready.
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700211 private boolean mWifiTetherRequested;
212
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700213 public Tethering(Context context, INetworkManagementService nmService,
Christopher Wiley497c1472016-10-11 13:26:03 -0700214 INetworkStatsService statsService, INetworkPolicyManager policyManager,
Erik Kline47222fc2017-04-30 19:36:15 +0900215 Looper looper, MockableSystemProperties systemProperties,
216 TetheringDependencies deps) {
Erik Kline7747fd42017-05-12 16:52:48 +0900217 mLog.mark("constructed");
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800218 mContext = context;
Chia-chi Yehc9338302011-05-11 16:35:13 -0700219 mNMService = nmService;
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700220 mStatsService = statsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700221 mPolicyManager = policyManager;
Christopher Wiley497c1472016-10-11 13:26:03 -0700222 mLooper = looper;
223 mSystemProperties = systemProperties;
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900224 mDeps = deps;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800225
Robert Greenwaltb4453622011-11-03 16:01:40 -0700226 mPublicSync = new Object();
227
Christopher Wileyd985dde2016-05-31 10:44:35 -0700228 mTetherStates = new ArrayMap<>();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800229
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900230 mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper, deps);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800231 mTetherMasterSM.start();
232
Erik Klinef4b6e342017-04-25 19:19:59 +0900233 final Handler smHandler = mTetherMasterSM.getHandler();
234 mOffloadController = new OffloadController(smHandler,
Erik Kline7a26ba32018-03-09 14:18:02 +0900235 mDeps.getOffloadHardwareInterface(smHandler, mLog),
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900236 mContext.getContentResolver(), mNMService,
Erik Klinef4b6e342017-04-25 19:19:59 +0900237 mLog);
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900238 mUpstreamNetworkMonitor = deps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog,
239 TetherMasterSM.EVENT_UPSTREAM_CALLBACK);
Erik Klineea9cc482017-03-10 19:35:34 +0900240 mForwardedDownstreams = new HashSet<>();
Erik Klinede637722017-10-12 22:16:01 +0900241
242 IntentFilter filter = new IntentFilter();
243 filter.addAction(ACTION_CARRIER_CONFIG_CHANGED);
244 mCarrierConfigChange = new VersionedBroadcastListener(
245 "CarrierConfigChangeListener", mContext, smHandler, filter,
246 (Intent ignored) -> {
247 mLog.log("OBSERVED carrier config change");
Erik Kline80b7a9f2018-02-28 15:01:35 +0900248 updateConfiguration();
Erik Klinede637722017-10-12 22:16:01 +0900249 reevaluateSimCardProvisioning();
250 });
Erik Kline6ff17f72015-12-10 20:42:12 +0900251
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800252 mStateReceiver = new StateReceiver();
Erik Kline80b7a9f2018-02-28 15:01:35 +0900253
254 // Load tethering configuration.
255 updateConfiguration();
256
257 startStateMachineUpdaters();
258 }
259
260 private void startStateMachineUpdaters() {
261 mCarrierConfigChange.startListening();
262
263 final Handler handler = mTetherMasterSM.getHandler();
264 IntentFilter filter = new IntentFilter();
Mike Lockwood770126a2010-12-09 22:30:37 -0800265 filter.addAction(UsbManager.ACTION_USB_STATE);
Erik Kline7a26ba32018-03-09 14:18:02 +0900266 filter.addAction(CONNECTIVITY_ACTION);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700267 filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700268 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Erik Kline80b7a9f2018-02-28 15:01:35 +0900269 mContext.registerReceiver(mStateReceiver, filter, null, handler);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800270
271 filter = new IntentFilter();
Robert Greenwalt030f5e12010-03-10 16:41:03 -0800272 filter.addAction(Intent.ACTION_MEDIA_SHARED);
273 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800274 filter.addDataScheme("file");
Erik Kline80b7a9f2018-02-28 15:01:35 +0900275 mContext.registerReceiver(mStateReceiver, filter, null, handler);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800276
Erik Kline80b7a9f2018-02-28 15:01:35 +0900277 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 Rotarufa6d5c52017-07-18 16:49:22 +0100281 }
Robert Greenwalt49348e72011-10-21 16:54:26 -0700282 }
283
Erik Kline216af6d2017-04-27 20:57:23 +0900284 private WifiManager getWifiManager() {
285 return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
286 }
287
Erik Kline93c4afa2017-06-04 11:36:01 +0900288 // NOTE: This is always invoked on the mLooper thread.
Erik Klined781fba2017-01-23 13:01:58 +0900289 private void updateConfiguration() {
Erik Kline6bd74532017-05-19 10:10:41 +0900290 mConfig = new TetheringConfiguration(mContext, mLog);
Erik Kline93c4afa2017-06-04 11:36:01 +0900291 mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800292 }
293
Erik Kline6ee73da2017-07-08 20:36:37 +0900294 private void maybeUpdateConfiguration() {
295 final int dunCheck = TetheringConfiguration.checkDunRequired(mContext);
296 if (dunCheck == mConfig.dunCheck) return;
297 updateConfiguration();
298 }
299
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900300 @Override
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700301 public void interfaceStatusChanged(String iface, boolean up) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900302 // Never called directly: only called from interfaceLinkStateChanged.
303 // See NetlinkHandler.cpp:71.
Wink Savillec9acde92011-09-21 11:05:43 -0700304 if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700305 synchronized (mPublicSync) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700306 if (up) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900307 maybeTrackNewInterfaceLocked(iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800308 } else {
Erik Kline7a26ba32018-03-09 14:18:02 +0900309 if (ifaceNameToType(iface) == TETHERING_BLUETOOTH) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900310 stopTrackingInterfaceLocked(iface);
Christopher Wileyd30aaeb2016-07-08 09:33:50 -0700311 } else {
312 // Ignore usb0 down after enabling RNDIS.
313 // We will handle disconnect in interfaceRemoved.
314 // Similarly, ignore interface down for WiFi. We monitor WiFi AP status
315 // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent.
316 if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800317 }
318 }
319 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800320 }
321
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900322 @Override
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700323 public void interfaceLinkStateChanged(String iface, boolean up) {
Irfan Sheriff23eb2972011-07-22 15:21:10 -0700324 interfaceStatusChanged(iface, up);
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700325 }
326
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700327 private int ifaceNameToType(String iface) {
Erik Kline3e756652017-01-17 13:42:19 +0900328 final TetheringConfiguration cfg = mConfig;
329
330 if (cfg.isWifi(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900331 return TETHERING_WIFI;
Erik Kline3e756652017-01-17 13:42:19 +0900332 } else if (cfg.isUsb(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900333 return TETHERING_USB;
Erik Kline3e756652017-01-17 13:42:19 +0900334 } else if (cfg.isBluetooth(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900335 return TETHERING_BLUETOOTH;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700336 }
Erik Kline7a26ba32018-03-09 14:18:02 +0900337 return TETHERING_INVALID;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700338 }
339
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900340 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800341 public void interfaceAdded(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700342 if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700343 synchronized (mPublicSync) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900344 maybeTrackNewInterfaceLocked(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800345 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800346 }
347
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900348 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800349 public void interfaceRemoved(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700350 if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700351 synchronized (mPublicSync) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900352 stopTrackingInterfaceLocked(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800353 }
354 }
355
Hugo Benichib55fb222017-03-10 14:20:57 +0900356 public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800357 if (!isTetherProvisioningRequired()) {
358 enableTetheringInternal(type, true, receiver);
359 return;
360 }
361
362 if (showProvisioningUi) {
363 runUiTetherProvisioningAndEnable(type, receiver);
364 } else {
365 runSilentTetherProvisioningAndEnable(type, receiver);
366 }
367 }
368
369 public void stopTethering(int type) {
370 enableTetheringInternal(type, false, null);
371 if (isTetherProvisioningRequired()) {
372 cancelTetherProvisioningRechecks(type);
373 }
374 }
375
376 /**
377 * Check if the device requires a provisioning check in order to enable tethering.
378 *
379 * @return a boolean - {@code true} indicating tether provisioning is required by the carrier.
380 */
Christopher Wiley497c1472016-10-11 13:26:03 -0700381 @VisibleForTesting
382 protected boolean isTetherProvisioningRequired() {
Erik Kline80b7a9f2018-02-28 15:01:35 +0900383 final TetheringConfiguration cfg = mConfig;
Christopher Wiley497c1472016-10-11 13:26:03 -0700384 if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false)
Erik Kline80b7a9f2018-02-28 15:01:35 +0900385 || cfg.provisioningApp.length == 0) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800386 return false;
387 }
Erik Klinede637722017-10-12 22:16:01 +0900388 if (carrierConfigAffirmsEntitlementCheckNotRequired()) {
389 return false;
390 }
Erik Kline80b7a9f2018-02-28 15:01:35 +0900391 return (cfg.provisioningApp.length == 2);
Erik Klinede637722017-10-12 22:16:01 +0900392 }
393
394 // The logic here is aimed solely at confirming that a CarrierConfig exists
395 // and affirms that entitlement checks are not required.
396 //
397 // TODO: find a better way to express this, or alter the checking process
398 // entirely so that this is more intuitive.
399 private boolean carrierConfigAffirmsEntitlementCheckNotRequired() {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800400 // Check carrier config for entitlement checks
401 final CarrierConfigManager configManager = (CarrierConfigManager) mContext
402 .getSystemService(Context.CARRIER_CONFIG_SERVICE);
Erik Klinede637722017-10-12 22:16:01 +0900403 if (configManager == null) return false;
404
405 final PersistableBundle carrierConfig = configManager.getConfig();
406 if (carrierConfig == null) return false;
407
408 // A CarrierConfigManager was found and it has a config.
409 final boolean isEntitlementCheckRequired = carrierConfig.getBoolean(
410 CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL);
411 return !isEntitlementCheckRequired;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800412 }
413
414 /**
415 * Enables or disables tethering for the given type. This should only be called once
416 * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks
417 * for the specified interface.
418 */
419 private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700420 boolean isProvisioningRequired = enable && isTetherProvisioningRequired();
421 int result;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800422 switch (type) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900423 case TETHERING_WIFI:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700424 result = setWifiTethering(enable);
Erik Kline7a26ba32018-03-09 14:18:02 +0900425 if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700426 scheduleProvisioningRechecks(type);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800427 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700428 sendTetherResult(receiver, result);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800429 break;
Erik Kline7a26ba32018-03-09 14:18:02 +0900430 case TETHERING_USB:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700431 result = setUsbTethering(enable);
Erik Kline7a26ba32018-03-09 14:18:02 +0900432 if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800433 scheduleProvisioningRechecks(type);
434 }
435 sendTetherResult(receiver, result);
436 break;
Erik Kline7a26ba32018-03-09 14:18:02 +0900437 case TETHERING_BLUETOOTH:
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800438 setBluetoothTethering(enable, receiver);
439 break;
440 default:
441 Log.w(TAG, "Invalid tether type.");
Erik Kline7a26ba32018-03-09 14:18:02 +0900442 sendTetherResult(receiver, TETHER_ERROR_UNKNOWN_IFACE);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800443 }
444 }
445
446 private void sendTetherResult(ResultReceiver receiver, int result) {
447 if (receiver != null) {
448 receiver.send(result, null);
449 }
450 }
451
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700452 private int setWifiTethering(final boolean enable) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900453 int rval = TETHER_ERROR_MASTER_ERROR;
Erik Kline256be782017-04-29 13:20:04 +0900454 final long ident = Binder.clearCallingIdentity();
455 try {
456 synchronized (mPublicSync) {
457 mWifiTetherRequested = enable;
458 final WifiManager mgr = getWifiManager();
459 if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) ||
460 (!enable && mgr.stopSoftAp())) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900461 rval = TETHER_ERROR_NO_ERROR;
Erik Kline256be782017-04-29 13:20:04 +0900462 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700463 }
Erik Kline256be782017-04-29 13:20:04 +0900464 } finally {
465 Binder.restoreCallingIdentity(ident);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700466 }
Erik Kline256be782017-04-29 13:20:04 +0900467 return rval;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700468 }
469
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800470 private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) {
471 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
472 if (adapter == null || !adapter.isEnabled()) {
473 Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " +
474 (adapter == null));
Erik Kline7a26ba32018-03-09 14:18:02 +0900475 sendTetherResult(receiver, TETHER_ERROR_SERVICE_UNAVAIL);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800476 return;
477 }
478
479 adapter.getProfileProxy(mContext, new ServiceListener() {
480 @Override
481 public void onServiceDisconnected(int profile) { }
482
483 @Override
484 public void onServiceConnected(int profile, BluetoothProfile proxy) {
485 ((BluetoothPan) proxy).setBluetoothTethering(enable);
486 // TODO: Enabling bluetooth tethering can fail asynchronously here.
487 // We should figure out a way to bubble up that failure instead of sending success.
Erik Kline7a26ba32018-03-09 14:18:02 +0900488 final int result = (((BluetoothPan) proxy).isTetheringOn() == enable)
489 ? TETHER_ERROR_NO_ERROR
490 : TETHER_ERROR_MASTER_ERROR;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800491 sendTetherResult(receiver, result);
492 if (enable && isTetherProvisioningRequired()) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900493 scheduleProvisioningRechecks(TETHERING_BLUETOOTH);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800494 }
495 adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
496 }
497 }, BluetoothProfile.PAN);
498 }
499
500 private void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) {
Jeremy Klein15f3d212016-01-24 17:01:09 -0800501 ResultReceiver proxyReceiver = getProxyReceiver(type, receiver);
502 sendUiTetherProvisionIntent(type, proxyReceiver);
503 }
504
505 private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) {
506 Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING);
Erik Kline7a26ba32018-03-09 14:18:02 +0900507 intent.putExtra(EXTRA_ADD_TETHER_TYPE, type);
508 intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver);
Jeremy Kleind50fc052018-01-19 11:25:28 -0800509 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Jeremy Klein15f3d212016-01-24 17:01:09 -0800510 final long ident = Binder.clearCallingIdentity();
511 try {
512 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
513 } finally {
514 Binder.restoreCallingIdentity(ident);
515 }
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800516 }
517
518 /**
Erik Klinefdd41112017-01-12 17:02:51 +0900519 * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result
520 * is successful before firing back up to the wrapped receiver.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800521 *
522 * @param type The type of tethering being enabled.
523 * @param receiver A ResultReceiver which will be called back with an int resultCode.
524 * @return The proxy receiver.
525 */
526 private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) {
527 ResultReceiver rr = new ResultReceiver(null) {
528 @Override
529 protected void onReceiveResult(int resultCode, Bundle resultData) {
530 // If provisioning is successful, enable tethering, otherwise just send the error.
Erik Kline7a26ba32018-03-09 14:18:02 +0900531 if (resultCode == TETHER_ERROR_NO_ERROR) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800532 enableTetheringInternal(type, true, receiver);
533 } else {
534 sendTetherResult(receiver, resultCode);
535 }
536 }
537 };
538
539 // The following is necessary to avoid unmarshalling issues when sending the receiver
Erik Kline6ff17f72015-12-10 20:42:12 +0900540 // across processes.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800541 Parcel parcel = Parcel.obtain();
542 rr.writeToParcel(parcel,0);
543 parcel.setDataPosition(0);
544 ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
545 parcel.recycle();
546 return receiverForSending;
547 }
548
549 private void scheduleProvisioningRechecks(int type) {
550 Intent intent = new Intent();
Erik Kline7a26ba32018-03-09 14:18:02 +0900551 intent.putExtra(EXTRA_ADD_TETHER_TYPE, type);
552 intent.putExtra(EXTRA_SET_ALARM, true);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800553 intent.setComponent(TETHER_SERVICE);
554 final long ident = Binder.clearCallingIdentity();
555 try {
556 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
557 } finally {
558 Binder.restoreCallingIdentity(ident);
559 }
560 }
561
562 private void runSilentTetherProvisioningAndEnable(int type, ResultReceiver receiver) {
563 ResultReceiver proxyReceiver = getProxyReceiver(type, receiver);
564 sendSilentTetherProvisionIntent(type, proxyReceiver);
565 }
566
567 private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) {
568 Intent intent = new Intent();
Erik Kline7a26ba32018-03-09 14:18:02 +0900569 intent.putExtra(EXTRA_ADD_TETHER_TYPE, type);
570 intent.putExtra(EXTRA_RUN_PROVISION, true);
571 intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800572 intent.setComponent(TETHER_SERVICE);
573 final long ident = Binder.clearCallingIdentity();
574 try {
575 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
576 } finally {
577 Binder.restoreCallingIdentity(ident);
578 }
579 }
580
581 private void cancelTetherProvisioningRechecks(int type) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900582 if (mDeps.isTetheringSupported()) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800583 Intent intent = new Intent();
Erik Kline7a26ba32018-03-09 14:18:02 +0900584 intent.putExtra(EXTRA_REM_TETHER_TYPE, type);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800585 intent.setComponent(TETHER_SERVICE);
586 final long ident = Binder.clearCallingIdentity();
587 try {
588 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
589 } finally {
590 Binder.restoreCallingIdentity(ident);
591 }
592 }
593 }
Erik Kline6ff17f72015-12-10 20:42:12 +0900594
Erik Kline227648f2017-01-20 20:26:10 +0900595 // Used by the SIM card change observation code.
596 // TODO: De-duplicate with above code, where possible.
597 private void startProvisionIntent(int tetherType) {
598 final Intent startProvIntent = new Intent();
Erik Kline7a26ba32018-03-09 14:18:02 +0900599 startProvIntent.putExtra(EXTRA_ADD_TETHER_TYPE, tetherType);
600 startProvIntent.putExtra(EXTRA_RUN_PROVISION, true);
Erik Kline227648f2017-01-20 20:26:10 +0900601 startProvIntent.setComponent(TETHER_SERVICE);
602 mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT);
603 }
604
Robert Greenwalt5a735062010-03-02 17:25:02 -0800605 public int tether(String iface) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900606 return tether(iface, IpServer.STATE_TETHERED);
Erik Klineea9cc482017-03-10 19:35:34 +0900607 }
608
609 private int tether(String iface, int requestedState) {
Wink Savillec9acde92011-09-21 11:05:43 -0700610 if (DBG) Log.d(TAG, "Tethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700611 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700612 TetherState tetherState = mTetherStates.get(iface);
613 if (tetherState == null) {
Erik Kline00019f42016-06-30 19:31:46 +0900614 Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900615 return TETHER_ERROR_UNKNOWN_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700616 }
617 // Ignore the error status of the interface. If the interface is available,
618 // the errors are referring to past tethering attempts anyway.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900619 if (tetherState.lastState != IpServer.STATE_AVAILABLE) {
Erik Kline00019f42016-06-30 19:31:46 +0900620 Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900621 return TETHER_ERROR_UNAVAIL_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700622 }
Erik Klineea9cc482017-03-10 19:35:34 +0900623 // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's
624 // queue but not yet processed, this will be a no-op and it will not
625 // return an error.
626 //
627 // TODO: reexamine the threading and messaging model.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900628 tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_REQUESTED, requestedState);
Erik Kline7a26ba32018-03-09 14:18:02 +0900629 return TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800630 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800631 }
632
Robert Greenwalt5a735062010-03-02 17:25:02 -0800633 public int untether(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700634 if (DBG) Log.d(TAG, "Untethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700635 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700636 TetherState tetherState = mTetherStates.get(iface);
637 if (tetherState == null) {
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700638 Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900639 return TETHER_ERROR_UNKNOWN_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700640 }
Erik Klineea9cc482017-03-10 19:35:34 +0900641 if (!tetherState.isCurrentlyServing()) {
642 Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900643 return TETHER_ERROR_UNAVAIL_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700644 }
Erik Kline7a4ccc62018-08-27 17:26:47 +0900645 tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_UNREQUESTED);
Erik Kline7a26ba32018-03-09 14:18:02 +0900646 return TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800647 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800648 }
649
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700650 public void untetherAll() {
Erik Kline7a26ba32018-03-09 14:18:02 +0900651 stopTethering(TETHERING_WIFI);
652 stopTethering(TETHERING_USB);
653 stopTethering(TETHERING_BLUETOOTH);
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700654 }
655
Robert Greenwalt5a735062010-03-02 17:25:02 -0800656 public int getLastTetherError(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700657 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700658 TetherState tetherState = mTetherStates.get(iface);
659 if (tetherState == null) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700660 Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
661 ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900662 return TETHER_ERROR_UNKNOWN_IFACE;
Robert Greenwaltb4453622011-11-03 16:01:40 -0700663 }
Hugo Benichib55fb222017-03-10 14:20:57 +0900664 return tetherState.lastError;
Robert Greenwalt5a735062010-03-02 17:25:02 -0800665 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800666 }
667
Erik Klineea9cc482017-03-10 19:35:34 +0900668 // TODO: Figure out how to update for local hotspot mode interfaces.
Christopher Wileyd985dde2016-05-31 10:44:35 -0700669 private void sendTetherStateChangedBroadcast() {
Erik Kline7a26ba32018-03-09 14:18:02 +0900670 if (!mDeps.isTetheringSupported()) return;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800671
Erik Kline8351faa2017-04-17 16:47:23 +0900672 final ArrayList<String> availableList = new ArrayList<>();
673 final ArrayList<String> tetherList = new ArrayList<>();
674 final ArrayList<String> localOnlyList = new ArrayList<>();
675 final ArrayList<String> erroredList = new ArrayList<>();
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800676
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800677 boolean wifiTethered = false;
678 boolean usbTethered = false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700679 boolean bluetoothTethered = false;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800680
Erik Kline3e756652017-01-17 13:42:19 +0900681 final TetheringConfiguration cfg = mConfig;
682
Robert Greenwaltb4453622011-11-03 16:01:40 -0700683 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700684 for (int i = 0; i < mTetherStates.size(); i++) {
685 TetherState tetherState = mTetherStates.valueAt(i);
686 String iface = mTetherStates.keyAt(i);
Erik Kline7a26ba32018-03-09 14:18:02 +0900687 if (tetherState.lastError != TETHER_ERROR_NO_ERROR) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700688 erroredList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900689 } else if (tetherState.lastState == IpServer.STATE_AVAILABLE) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700690 availableList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900691 } else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) {
Erik Kline8351faa2017-04-17 16:47:23 +0900692 localOnlyList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900693 } else if (tetherState.lastState == IpServer.STATE_TETHERED) {
Erik Kline3e756652017-01-17 13:42:19 +0900694 if (cfg.isUsb(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700695 usbTethered = true;
Erik Kline3e756652017-01-17 13:42:19 +0900696 } else if (cfg.isWifi(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700697 wifiTethered = true;
Erik Kline3e756652017-01-17 13:42:19 +0900698 } else if (cfg.isBluetooth(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700699 bluetoothTethered = true;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800700 }
Erik Kline8351faa2017-04-17 16:47:23 +0900701 tetherList.add(iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800702 }
703 }
704 }
Erik Kline7a26ba32018-03-09 14:18:02 +0900705 final Intent bcast = new Intent(ACTION_TETHER_STATE_CHANGED);
Erik Kline8351faa2017-04-17 16:47:23 +0900706 bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Robert Greenwalt98c79e52011-07-28 11:51:11 -0700707 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Erik Kline7a26ba32018-03-09 14:18:02 +0900708 bcast.putStringArrayListExtra(EXTRA_AVAILABLE_TETHER, availableList);
709 bcast.putStringArrayListExtra(EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList);
710 bcast.putStringArrayListExtra(EXTRA_ACTIVE_TETHER, tetherList);
711 bcast.putStringArrayListExtra(EXTRA_ERRORED_TETHER, erroredList);
Erik Kline8351faa2017-04-17 16:47:23 +0900712 mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL);
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800713 if (DBG) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900714 Log.d(TAG, String.format(
Erik Kline8351faa2017-04-17 16:47:23 +0900715 "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]",
716 "avail", TextUtils.join(",", availableList),
717 "local_only", TextUtils.join(",", localOnlyList),
718 "tether", TextUtils.join(",", tetherList),
719 "error", TextUtils.join(",", erroredList)));
Robert Greenwalt924cc942010-06-28 10:26:19 -0700720 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800721
722 if (usbTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700723 if (wifiTethered || bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400724 showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800725 } else {
Chris Wren282cfef2017-03-27 15:01:44 -0400726 showTetheredNotification(SystemMessage.NOTE_TETHER_USB);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800727 }
728 } else if (wifiTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700729 if (bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400730 showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL);
Danica Chang6fdd0c62010-08-11 14:54:43 -0700731 } else {
Vinit Deshpande2576a802014-11-18 13:56:15 -0800732 /* We now have a status bar icon for WifiTethering, so drop the notification */
733 clearTetheredNotification();
Danica Chang6fdd0c62010-08-11 14:54:43 -0700734 }
735 } else if (bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400736 showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800737 } else {
738 clearTetheredNotification();
739 }
740 }
741
Chris Wren282cfef2017-03-27 15:01:44 -0400742 private void showTetheredNotification(int id) {
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100743 showTetheredNotification(id, true);
744 }
745
746 @VisibleForTesting
747 protected void showTetheredNotification(int id, boolean tetheringOn) {
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800748 NotificationManager notificationManager =
Erik Klinec438e302017-07-04 22:02:49 +0900749 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800750 if (notificationManager == null) {
751 return;
752 }
Chris Wren282cfef2017-03-27 15:01:44 -0400753 int icon = 0;
754 switch(id) {
755 case SystemMessage.NOTE_TETHER_USB:
756 icon = com.android.internal.R.drawable.stat_sys_tether_usb;
757 break;
758 case SystemMessage.NOTE_TETHER_BLUETOOTH:
759 icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth;
760 break;
761 case SystemMessage.NOTE_TETHER_GENERAL:
762 default:
763 icon = com.android.internal.R.drawable.stat_sys_tether_general;
764 break;
765 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800766
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400767 if (mLastNotificationId != 0) {
768 if (mLastNotificationId == icon) {
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700769 return;
770 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400771 notificationManager.cancelAsUser(null, mLastNotificationId,
Robert Greenwalt3cab6b02012-10-04 16:44:26 -0700772 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400773 mLastNotificationId = 0;
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700774 }
775
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800776 Intent intent = new Intent();
777 intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
778 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
779
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700780 PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
781 null, UserHandle.CURRENT);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800782
783 Resources r = Resources.getSystem();
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100784 final CharSequence title;
785 final CharSequence message;
786
787 if (tetheringOn) {
788 title = r.getText(com.android.internal.R.string.tethered_notification_title);
789 message = r.getText(com.android.internal.R.string.tethered_notification_message);
790 } else {
791 title = r.getText(com.android.internal.R.string.disable_tether_notification_title);
792 message = r.getText(com.android.internal.R.string.disable_tether_notification_message);
793 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800794
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400795 if (mTetheredNotificationBuilder == null) {
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500796 mTetheredNotificationBuilder =
797 new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400798 mTetheredNotificationBuilder.setWhen(0)
799 .setOngoing(true)
800 .setColor(mContext.getColor(
801 com.android.internal.R.color.system_notification_accent_color))
802 .setVisibility(Notification.VISIBILITY_PUBLIC)
803 .setCategory(Notification.CATEGORY_STATUS);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800804 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400805 mTetheredNotificationBuilder.setSmallIcon(icon)
806 .setContentTitle(title)
807 .setContentText(message)
808 .setContentIntent(pi);
Chris Wren282cfef2017-03-27 15:01:44 -0400809 mLastNotificationId = id;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800810
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400811 notificationManager.notifyAsUser(null, mLastNotificationId,
zhouzhijie130d4592017-05-18 10:02:59 +0800812 mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800813 }
814
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100815 @VisibleForTesting
816 protected void clearTetheredNotification() {
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800817 NotificationManager notificationManager =
Erik Klinec438e302017-07-04 22:02:49 +0900818 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400819 if (notificationManager != null && mLastNotificationId != 0) {
820 notificationManager.cancelAsUser(null, mLastNotificationId,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700821 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400822 mLastNotificationId = 0;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800823 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800824 }
825
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800826 private class StateReceiver extends BroadcastReceiver {
Nick Kralevich70c117a2014-05-27 15:30:02 -0700827 @Override
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800828 public void onReceive(Context content, Intent intent) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900829 final String action = intent.getAction();
830 if (action == null) return;
831
Mike Lockwood770126a2010-12-09 22:30:37 -0800832 if (action.equals(UsbManager.ACTION_USB_STATE)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900833 handleUsbAction(intent);
Erik Kline7a26ba32018-03-09 14:18:02 +0900834 } else if (action.equals(CONNECTIVITY_ACTION)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900835 handleConnectivityAction(intent);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700836 } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900837 handleWifiApAction(intent);
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700838 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
Erik Klinede637722017-10-12 22:16:01 +0900839 mLog.log("OBSERVED configuration changed");
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700840 updateConfiguration();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800841 }
842 }
Erik Kline2e88b5e2017-01-18 11:57:45 +0900843
844 private void handleConnectivityAction(Intent intent) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900845 final NetworkInfo networkInfo =
846 (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900847 if (networkInfo == null ||
848 networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) {
849 return;
850 }
851
852 if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString());
853 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
854 }
855
856 private void handleUsbAction(Intent intent) {
857 final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false);
Erik Klinec438e302017-07-04 22:02:49 +0900858 final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900859 final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false);
Erik Klinec438e302017-07-04 22:02:49 +0900860
861 mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s",
862 usbConnected, usbConfigured, rndisEnabled));
863
864 // There are three types of ACTION_USB_STATE:
865 //
866 // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0)
867 // Meaning: USB connection has ended either because of
868 // software reset or hard unplug.
869 //
870 // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0)
871 // Meaning: the first stage of USB protocol handshake has
872 // occurred but it is not complete.
873 //
874 // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1)
875 // Meaning: the USB handshake is completely done and all the
876 // functions are ready to use.
877 //
878 // For more explanation, see b/62552150 .
Erik Kline2e88b5e2017-01-18 11:57:45 +0900879 synchronized (Tethering.this.mPublicSync) {
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800880 if (!usbConnected && mRndisEnabled) {
881 // Turn off tethering if it was enabled and there is a disconnect.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900882 tetherMatchingInterfaces(IpServer.STATE_AVAILABLE, TETHERING_USB);
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800883 } else if (usbConfigured && rndisEnabled) {
884 // Tether if rndis is enabled and usb is configured.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900885 tetherMatchingInterfaces(IpServer.STATE_TETHERED, TETHERING_USB);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900886 }
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800887 mRndisEnabled = usbConfigured && rndisEnabled;
Erik Kline2e88b5e2017-01-18 11:57:45 +0900888 }
889 }
890
891 private void handleWifiApAction(Intent intent) {
Erik Kline2efb8272017-05-31 15:53:53 +0900892 final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED);
893 final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME);
894 final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED);
895
Erik Kline2e88b5e2017-01-18 11:57:45 +0900896 synchronized (Tethering.this.mPublicSync) {
897 switch (curState) {
898 case WifiManager.WIFI_AP_STATE_ENABLING:
899 // We can see this state on the way to both enabled and failure states.
900 break;
901 case WifiManager.WIFI_AP_STATE_ENABLED:
Erik Kline2efb8272017-05-31 15:53:53 +0900902 enableWifiIpServingLocked(ifname, ipmode);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900903 break;
904 case WifiManager.WIFI_AP_STATE_DISABLED:
905 case WifiManager.WIFI_AP_STATE_DISABLING:
906 case WifiManager.WIFI_AP_STATE_FAILED:
907 default:
Erik Kline562e0c12017-06-09 16:36:29 +0900908 disableWifiIpServingLocked(ifname, curState);
Erik Kline2efb8272017-05-31 15:53:53 +0900909 break;
Erik Kline2e88b5e2017-01-18 11:57:45 +0900910 }
911 }
912 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800913 }
914
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100915 @VisibleForTesting
916 protected static class TetheringUserRestrictionListener implements UserRestrictionsListener {
917 private final Tethering mWrapper;
918
919 public TetheringUserRestrictionListener(Tethering wrapper) {
920 mWrapper = wrapper;
921 }
922
923 public void onUserRestrictionsChanged(int userId,
924 Bundle newRestrictions,
925 Bundle prevRestrictions) {
926 final boolean newlyDisallowed =
927 newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING);
928 final boolean previouslyDisallowed =
929 prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING);
930 final boolean tetheringDisallowedChanged = (newlyDisallowed != previouslyDisallowed);
931
932 if (!tetheringDisallowedChanged) {
933 return;
934 }
935
936 mWrapper.clearTetheredNotification();
937 final boolean isTetheringActiveOnDevice = (mWrapper.getTetheredIfaces().length != 0);
938
939 if (newlyDisallowed && isTetheringActiveOnDevice) {
940 mWrapper.showTetheredNotification(
941 com.android.internal.R.drawable.stat_sys_tether_general, false);
942 mWrapper.untetherAll();
943 }
944 }
945 }
946
Erik Kline562e0c12017-06-09 16:36:29 +0900947 private void disableWifiIpServingLocked(String ifname, int apState) {
948 mLog.log("Canceling WiFi tethering request - AP_STATE=" + apState);
Erik Kline2efb8272017-05-31 15:53:53 +0900949
Erik Kline562e0c12017-06-09 16:36:29 +0900950 // Regardless of whether we requested this transition, the AP has gone
951 // down. Don't try to tether again unless we're requested to do so.
952 // TODO: Remove this altogether, once Wi-Fi reliably gives us an
953 // interface name with every broadcast.
954 mWifiTetherRequested = false;
955
956 if (!TextUtils.isEmpty(ifname)) {
957 final TetherState ts = mTetherStates.get(ifname);
958 if (ts != null) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900959 ts.ipServer.unwanted();
Erik Kline562e0c12017-06-09 16:36:29 +0900960 return;
961 }
962 }
963
Erik Kline2efb8272017-05-31 15:53:53 +0900964 for (int i = 0; i < mTetherStates.size(); i++) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900965 final IpServer ipServer = mTetherStates.valueAt(i).ipServer;
966 if (ipServer.interfaceType() == TETHERING_WIFI) {
967 ipServer.unwanted();
Erik Kline562e0c12017-06-09 16:36:29 +0900968 return;
Erik Kline2efb8272017-05-31 15:53:53 +0900969 }
970 }
Erik Kline562e0c12017-06-09 16:36:29 +0900971
972 mLog.log("Error disabling Wi-Fi IP serving; " +
973 (TextUtils.isEmpty(ifname) ? "no interface name specified"
974 : "specified interface: " + ifname));
Erik Kline2efb8272017-05-31 15:53:53 +0900975 }
976
977 private void enableWifiIpServingLocked(String ifname, int wifiIpMode) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900978 // Map wifiIpMode values to IpServer.Callback serving states, inferring
Erik Kline2efb8272017-05-31 15:53:53 +0900979 // from mWifiTetherRequested as a final "best guess".
980 final int ipServingMode;
981 switch (wifiIpMode) {
982 case IFACE_IP_MODE_TETHERED:
Erik Kline7a4ccc62018-08-27 17:26:47 +0900983 ipServingMode = IpServer.STATE_TETHERED;
Erik Kline2efb8272017-05-31 15:53:53 +0900984 break;
985 case IFACE_IP_MODE_LOCAL_ONLY:
Erik Kline7a4ccc62018-08-27 17:26:47 +0900986 ipServingMode = IpServer.STATE_LOCAL_ONLY;
Erik Kline2efb8272017-05-31 15:53:53 +0900987 break;
988 default:
Erik Kline9e225542017-06-08 17:48:48 +0900989 mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode);
990 return;
Erik Kline2efb8272017-05-31 15:53:53 +0900991 }
992
993 if (!TextUtils.isEmpty(ifname)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900994 maybeTrackNewInterfaceLocked(ifname, TETHERING_WIFI);
Erik Kline2efb8272017-05-31 15:53:53 +0900995 changeInterfaceState(ifname, ipServingMode);
996 } else {
Erik Kline9e225542017-06-08 17:48:48 +0900997 mLog.e(String.format(
998 "Cannot enable IP serving in mode %s on missing interface name",
999 ipServingMode));
Erik Kline2efb8272017-05-31 15:53:53 +09001000 }
1001 }
1002
Erik Klineea9cc482017-03-10 19:35:34 +09001003 // TODO: Consider renaming to something more accurate in its description.
1004 // This method:
1005 // - allows requesting either tethering or local hotspot serving states
1006 // - handles both enabling and disabling serving states
1007 // - only tethers the first matching interface in listInterfaces()
1008 // order of a given type
1009 private void tetherMatchingInterfaces(int requestedState, int interfaceType) {
1010 if (VDBG) {
1011 Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")");
1012 }
Mike Lockwood3c2a2f62011-06-08 15:10:26 -07001013
Christopher Wiley5c0b10a2016-05-31 14:43:08 -07001014 String[] ifaces = null;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -08001015 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001016 ifaces = mNMService.listInterfaces();
Robert Greenwalt65ae29b2010-02-18 11:25:54 -08001017 } catch (Exception e) {
Mike Lockwood3c2a2f62011-06-08 15:10:26 -07001018 Log.e(TAG, "Error listing Interfaces", e);
Robert Greenwalt65ae29b2010-02-18 11:25:54 -08001019 return;
1020 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -07001021 String chosenIface = null;
1022 if (ifaces != null) {
1023 for (String iface : ifaces) {
1024 if (ifaceNameToType(iface) == interfaceType) {
1025 chosenIface = iface;
1026 break;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -08001027 }
1028 }
1029 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -07001030 if (chosenIface == null) {
1031 Log.e(TAG, "could not find iface of type " + interfaceType);
1032 return;
1033 }
1034
Erik Kline2efb8272017-05-31 15:53:53 +09001035 changeInterfaceState(chosenIface, requestedState);
1036 }
1037
1038 private void changeInterfaceState(String ifname, int requestedState) {
Erik Klineea9cc482017-03-10 19:35:34 +09001039 final int result;
1040 switch (requestedState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001041 case IpServer.STATE_UNAVAILABLE:
1042 case IpServer.STATE_AVAILABLE:
Erik Kline2efb8272017-05-31 15:53:53 +09001043 result = untether(ifname);
Erik Klineea9cc482017-03-10 19:35:34 +09001044 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001045 case IpServer.STATE_TETHERED:
1046 case IpServer.STATE_LOCAL_ONLY:
Erik Kline2efb8272017-05-31 15:53:53 +09001047 result = tether(ifname, requestedState);
Erik Klineea9cc482017-03-10 19:35:34 +09001048 break;
1049 default:
1050 Log.wtf(TAG, "Unknown interface state: " + requestedState);
1051 return;
1052 }
Erik Kline7a26ba32018-03-09 14:18:02 +09001053 if (result != TETHER_ERROR_NO_ERROR) {
Erik Kline2efb8272017-05-31 15:53:53 +09001054 Log.e(TAG, "unable start or stop tethering on iface " + ifname);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -07001055 return;
1056 }
Robert Greenwalt65ae29b2010-02-18 11:25:54 -08001057 }
1058
Erik Kline3e756652017-01-17 13:42:19 +09001059 public TetheringConfiguration getTetheringConfiguration() {
1060 return mConfig;
1061 }
1062
Erik Klined781fba2017-01-23 13:01:58 +09001063 public boolean hasTetherableConfiguration() {
1064 final TetheringConfiguration cfg = mConfig;
1065 final boolean hasDownstreamConfiguration =
1066 (cfg.tetherableUsbRegexs.length != 0) ||
1067 (cfg.tetherableWifiRegexs.length != 0) ||
1068 (cfg.tetherableBluetoothRegexs.length != 0);
1069 final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty();
1070
1071 return hasDownstreamConfiguration && hasUpstreamConfiguration;
1072 }
1073
Erik Kline3e756652017-01-17 13:42:19 +09001074 // TODO - update callers to use getTetheringConfiguration(),
1075 // which has only final members.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001076 public String[] getTetherableUsbRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +09001077 return copy(mConfig.tetherableUsbRegexs);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001078 }
1079
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001080 public String[] getTetherableWifiRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +09001081 return copy(mConfig.tetherableWifiRegexs);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001082 }
1083
Danica Chang6fdd0c62010-08-11 14:54:43 -07001084 public String[] getTetherableBluetoothRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +09001085 return copy(mConfig.tetherableBluetoothRegexs);
Danica Chang6fdd0c62010-08-11 14:54:43 -07001086 }
1087
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001088 public int setUsbTethering(boolean enable) {
Wink Savillec9acde92011-09-21 11:05:43 -07001089 if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
Erik Klinec438e302017-07-04 22:02:49 +09001090 UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
Robert Greenwaltb4453622011-11-03 16:01:40 -07001091 synchronized (mPublicSync) {
Jerry Zhang327b8092018-01-09 17:53:04 -08001092 usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_RNDIS
1093 : UsbManager.FUNCTION_NONE);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001094 }
Erik Kline7a26ba32018-03-09 14:18:02 +09001095 return TETHER_ERROR_NO_ERROR;
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001096 }
1097
Erik Kline1fdc2e22017-05-08 17:56:35 +09001098 // TODO review API - figure out how to delete these entirely.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001099 public String[] getTetheredIfaces() {
1100 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001101 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001102 for (int i = 0; i < mTetherStates.size(); i++) {
1103 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001104 if (tetherState.lastState == IpServer.STATE_TETHERED) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001105 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001106 }
1107 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001108 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001109 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001110 }
1111
1112 public String[] getTetherableIfaces() {
1113 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001114 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001115 for (int i = 0; i < mTetherStates.size(); i++) {
1116 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001117 if (tetherState.lastState == IpServer.STATE_AVAILABLE) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001118 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001119 }
1120 }
1121 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001122 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001123 }
1124
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001125 public String[] getTetheredDhcpRanges() {
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001126 // TODO: this is only valid for the old DHCP server. Latest search suggests it is only used
1127 // by WifiP2pServiceImpl to start dnsmasq: remove/deprecate after migrating callers.
1128 return mConfig.legacyDhcpRanges;
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001129 }
1130
Robert Greenwalt5a735062010-03-02 17:25:02 -08001131 public String[] getErroredIfaces() {
1132 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001133 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001134 for (int i = 0; i < mTetherStates.size(); i++) {
1135 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a26ba32018-03-09 14:18:02 +09001136 if (tetherState.lastError != TETHER_ERROR_NO_ERROR) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001137 list.add(mTetherStates.keyAt(i));
Robert Greenwalt5a735062010-03-02 17:25:02 -08001138 }
1139 }
1140 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001141 return list.toArray(new String[list.size()]);
Robert Greenwalt5a735062010-03-02 17:25:02 -08001142 }
1143
Erik Kline22108902017-07-06 16:40:06 +09001144 private void logMessage(State state, int what) {
1145 mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what)));
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001146 }
1147
Erik Klineea9cc482017-03-10 19:35:34 +09001148 private boolean upstreamWanted() {
1149 if (!mForwardedDownstreams.isEmpty()) return true;
1150
1151 synchronized (mPublicSync) {
Jerry Zhang656a7bc2017-12-20 14:26:39 -08001152 return mWifiTetherRequested;
Erik Klineea9cc482017-03-10 19:35:34 +09001153 }
1154 }
1155
Erik Kline00019f42016-06-30 19:31:46 +09001156 // Needed because the canonical source of upstream truth is just the
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001157 // upstream interface set, |mCurrentUpstreamIfaceSet|.
Erik Kline3a5278f2017-06-24 19:29:10 +09001158 private boolean pertainsToCurrentUpstream(NetworkState ns) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001159 if (ns != null && ns.linkProperties != null && mCurrentUpstreamIfaceSet != null) {
Erik Kline00019f42016-06-30 19:31:46 +09001160 for (String ifname : ns.linkProperties.getAllInterfaceNames()) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001161 if (mCurrentUpstreamIfaceSet.ifnames.contains(ifname)) {
Erik Kline00019f42016-06-30 19:31:46 +09001162 return true;
Erik Kline6ff17f72015-12-10 20:42:12 +09001163 }
1164 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001165 }
Erik Kline00019f42016-06-30 19:31:46 +09001166 return false;
Erik Kline6ff17f72015-12-10 20:42:12 +09001167 }
1168
Erik Kline227648f2017-01-20 20:26:10 +09001169 private void reevaluateSimCardProvisioning() {
Erik Kline80b7a9f2018-02-28 15:01:35 +09001170 if (!mConfig.hasMobileHotspotProvisionApp()) return;
Erik Klinede637722017-10-12 22:16:01 +09001171 if (carrierConfigAffirmsEntitlementCheckNotRequired()) return;
Erik Kline227648f2017-01-20 20:26:10 +09001172
1173 ArrayList<Integer> tethered = new ArrayList<>();
1174 synchronized (mPublicSync) {
1175 for (int i = 0; i < mTetherStates.size(); i++) {
1176 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001177 if (tetherState.lastState != IpServer.STATE_TETHERED) {
Erik Kline227648f2017-01-20 20:26:10 +09001178 continue; // Skip interfaces that aren't tethered.
1179 }
1180 String iface = mTetherStates.keyAt(i);
1181 int interfaceType = ifaceNameToType(iface);
Erik Kline7a26ba32018-03-09 14:18:02 +09001182 if (interfaceType != TETHERING_INVALID) {
Erik Kline227648f2017-01-20 20:26:10 +09001183 tethered.add(interfaceType);
1184 }
1185 }
1186 }
1187
1188 for (int tetherType : tethered) {
1189 startProvisionIntent(tetherType);
1190 }
1191 }
1192
Wink Saville64c42ca2011-04-18 14:55:10 -07001193 class TetherMasterSM extends StateMachine {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001194 private static final int BASE_MASTER = Protocol.BASE_TETHERING;
Erik Klineea9cc482017-03-10 19:35:34 +09001195 // an interface SM has requested Tethering/Local Hotspot
1196 static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1;
1197 // an interface SM has unrequested Tethering/Local Hotspot
1198 static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001199 // upstream connection change - do the right thing
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001200 static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001201 // we don't have a valid upstream conn, check again after a delay
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001202 static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4;
Erik Kline6ff17f72015-12-10 20:42:12 +09001203 // Events from NetworkCallbacks that we process on the master state
1204 // machine thread on behalf of the UpstreamNetworkMonitor.
Erik Kline00019f42016-06-30 19:31:46 +09001205 static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001206 // we treated the error and want now to clear it
1207 static final int CMD_CLEAR_ERROR = BASE_MASTER + 6;
Erik Kline6e9a1012017-06-06 19:24:21 +09001208 static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001209
Erik Kline32179ff2017-07-04 18:28:11 +09001210 private final State mInitialState;
1211 private final State mTetherModeAliveState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001212
Erik Kline32179ff2017-07-04 18:28:11 +09001213 private final State mSetIpForwardingEnabledErrorState;
1214 private final State mSetIpForwardingDisabledErrorState;
1215 private final State mStartTetheringErrorState;
1216 private final State mStopTetheringErrorState;
1217 private final State mSetDnsForwardersErrorState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001218
Christopher Wileyd985dde2016-05-31 10:44:35 -07001219 // This list is a little subtle. It contains all the interfaces that currently are
1220 // requesting tethering, regardless of whether these interfaces are still members of
1221 // mTetherStates. This allows us to maintain the following predicates:
1222 //
1223 // 1) mTetherStates contains the set of all currently existing, tetherable, link state up
1224 // interfaces.
1225 // 2) mNotifyList contains all state machines that may have outstanding tethering state
1226 // that needs to be torn down.
1227 //
1228 // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList
1229 // so that the garbage collector does not clean up the state machine before it has a chance
1230 // to tear itself down.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001231 private final ArrayList<IpServer> mNotifyList;
Erik Kline1eb8c692016-07-08 17:21:26 +09001232 private final IPv6TetheringCoordinator mIPv6TetheringCoordinator;
Erik Klineed962a82017-07-06 19:49:35 +09001233 private final OffloadWrapper mOffload;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001234
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001235 private static final int UPSTREAM_SETTLE_TIME_MS = 10000;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001236
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +09001237 TetherMasterSM(String name, Looper looper, TetheringDependencies deps) {
Robert Greenwaltdfadaea2010-03-11 15:03:08 -08001238 super(name, looper);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001239
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001240 mInitialState = new InitialState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001241 mTetherModeAliveState = new TetherModeAliveState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001242 mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001243 mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001244 mStartTetheringErrorState = new StartTetheringErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001245 mStopTetheringErrorState = new StopTetheringErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001246 mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
Erik Kline1fdc2e22017-05-08 17:56:35 +09001247
1248 addState(mInitialState);
1249 addState(mTetherModeAliveState);
1250 addState(mSetIpForwardingEnabledErrorState);
1251 addState(mSetIpForwardingDisabledErrorState);
1252 addState(mStartTetheringErrorState);
1253 addState(mStopTetheringErrorState);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001254 addState(mSetDnsForwardersErrorState);
1255
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001256 mNotifyList = new ArrayList<>();
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +09001257 mIPv6TetheringCoordinator = deps.getIPv6TetheringCoordinator(mNotifyList, mLog);
Erik Klineed962a82017-07-06 19:49:35 +09001258 mOffload = new OffloadWrapper();
Erik Kline32179ff2017-07-04 18:28:11 +09001259
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001260 setInitialState(mInitialState);
1261 }
1262
Erik Kline1fdc2e22017-05-08 17:56:35 +09001263 class InitialState extends State {
1264 @Override
1265 public boolean processMessage(Message message) {
Erik Kline22108902017-07-06 16:40:06 +09001266 logMessage(this, message.what);
Erik Kline1fdc2e22017-05-08 17:56:35 +09001267 switch (message.what) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001268 case EVENT_IFACE_SERVING_STATE_ACTIVE: {
1269 final IpServer who = (IpServer) message.obj;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001270 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
1271 handleInterfaceServingStateActive(message.arg1, who);
1272 transitionTo(mTetherModeAliveState);
1273 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001274 }
1275 case EVENT_IFACE_SERVING_STATE_INACTIVE: {
1276 final IpServer who = (IpServer) message.obj;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001277 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
1278 handleInterfaceServingStateInactive(who);
1279 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001280 }
Erik Kline6e9a1012017-06-06 19:24:21 +09001281 case EVENT_IFACE_UPDATE_LINKPROPERTIES:
1282 // Silently ignore these for now.
1283 break;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001284 default:
1285 return NOT_HANDLED;
1286 }
1287 return HANDLED;
1288 }
1289 }
1290
Erik Kline3a5278f2017-06-24 19:29:10 +09001291 protected boolean turnOnMasterTetherSettings() {
1292 final TetheringConfiguration cfg = mConfig;
1293 try {
1294 mNMService.setIpForwardingEnabled(true);
1295 } catch (Exception e) {
1296 mLog.e(e);
1297 transitionTo(mSetIpForwardingEnabledErrorState);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001298 return false;
1299 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001300 // TODO: Randomize DHCPv4 ranges, especially in hotspot mode.
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001301 // Legacy DHCP server is disabled if passed an empty ranges array
1302 final String[] dhcpRanges = cfg.enableLegacyDhcpServer
1303 ? cfg.legacyDhcpRanges
1304 : new String[0];
Erik Kline3a5278f2017-06-24 19:29:10 +09001305 try {
1306 // TODO: Find a more accurate method name (startDHCPv4()?).
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001307 mNMService.startTethering(dhcpRanges);
Erik Kline3a5278f2017-06-24 19:29:10 +09001308 } catch (Exception e) {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001309 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001310 mNMService.stopTethering();
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001311 mNMService.startTethering(dhcpRanges);
Erik Kline3a5278f2017-06-24 19:29:10 +09001312 } catch (Exception ee) {
1313 mLog.e(ee);
1314 transitionTo(mStartTetheringErrorState);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001315 return false;
1316 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001317 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001318 mLog.log("SET master tether settings: ON");
1319 return true;
1320 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001321
Erik Kline3a5278f2017-06-24 19:29:10 +09001322 protected boolean turnOffMasterTetherSettings() {
1323 try {
1324 mNMService.stopTethering();
1325 } catch (Exception e) {
1326 mLog.e(e);
1327 transitionTo(mStopTetheringErrorState);
1328 return false;
Erik Kline14f7faf2017-02-14 19:03:09 +09001329 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001330 try {
1331 mNMService.setIpForwardingEnabled(false);
1332 } catch (Exception e) {
1333 mLog.e(e);
1334 transitionTo(mSetIpForwardingDisabledErrorState);
1335 return false;
1336 }
1337 transitionTo(mInitialState);
1338 mLog.log("SET master tether settings: OFF");
1339 return true;
1340 }
Erik Kline14f7faf2017-02-14 19:03:09 +09001341
Erik Kline3a5278f2017-06-24 19:29:10 +09001342 protected void chooseUpstreamType(boolean tryCell) {
Erik Kline6ee73da2017-07-08 20:36:37 +09001343 // We rebuild configuration on ACTION_CONFIGURATION_CHANGED, but we
1344 // do not currently know how to watch for changes in DUN settings.
1345 maybeUpdateConfiguration();
Erik Kline1e2897d2017-06-09 17:08:52 +09001346
Erik Kline72302902018-06-14 17:36:40 +09001347 final TetheringConfiguration config = mConfig;
1348 final NetworkState ns = (config.chooseUpstreamAutomatically)
1349 ? mUpstreamNetworkMonitor.getCurrentPreferredUpstream()
1350 : mUpstreamNetworkMonitor.selectPreferredUpstreamType(
1351 config.preferredUpstreamIfaceTypes);
Erik Kline3a5278f2017-06-24 19:29:10 +09001352 if (ns == null) {
1353 if (tryCell) {
1354 mUpstreamNetworkMonitor.registerMobileNetworkRequest();
1355 // We think mobile should be coming up; don't set a retry.
1356 } else {
1357 sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
Erik Kline1e2897d2017-06-09 17:08:52 +09001358 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001359 }
Erik Kline60caf7d2017-10-10 11:54:08 +09001360 mUpstreamNetworkMonitor.setCurrentUpstream((ns != null) ? ns.network : null);
Erik Kline3a5278f2017-06-24 19:29:10 +09001361 setUpstreamNetwork(ns);
1362 }
1363
1364 protected void setUpstreamNetwork(NetworkState ns) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001365 InterfaceSet ifaces = null;
Erik Klinee8bdb402018-02-23 14:16:06 -08001366 if (ns != null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001367 // Find the interface with the default IPv4 route. It may be the
1368 // interface described by linkProperties, or one of the interfaces
1369 // stacked on top of it.
Erik Klinee8bdb402018-02-23 14:16:06 -08001370 mLog.i("Looking for default routes on: " + ns.linkProperties);
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001371 ifaces = TetheringInterfaceUtils.getTetheringInterfaces(ns);
1372 mLog.i("Found upstream interface(s): " + ifaces);
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001373 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001374
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001375 if (ifaces != null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001376 setDnsForwarders(ns.network, ns.linkProperties);
Erik Kline6ff17f72015-12-10 20:42:12 +09001377 }
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001378 notifyDownstreamsOfNewUpstreamIface(ifaces);
Erik Kline3a5278f2017-06-24 19:29:10 +09001379 if (ns != null && pertainsToCurrentUpstream(ns)) {
Erik Kline72302902018-06-14 17:36:40 +09001380 // If we already have NetworkState for this network update it immediately.
Erik Kline3a5278f2017-06-24 19:29:10 +09001381 handleNewUpstreamNetworkState(ns);
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001382 } else if (mCurrentUpstreamIfaceSet == null) {
1383 // There are no available upstream networks.
Erik Kline3a5278f2017-06-24 19:29:10 +09001384 handleNewUpstreamNetworkState(null);
1385 }
1386 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001387
Erik Kline3a5278f2017-06-24 19:29:10 +09001388 protected void setDnsForwarders(final Network network, final LinkProperties lp) {
1389 // TODO: Set v4 and/or v6 DNS per available connectivity.
1390 String[] dnsServers = mConfig.defaultIPv4DNS;
1391 final Collection<InetAddress> dnses = lp.getDnsServers();
1392 // TODO: Properly support the absence of DNS servers.
1393 if (dnses != null && !dnses.isEmpty()) {
1394 // TODO: remove this invocation of NetworkUtils.makeStrings().
1395 dnsServers = NetworkUtils.makeStrings(dnses);
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001396 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001397 try {
1398 mNMService.setDnsForwarders(network, dnsServers);
1399 mLog.log(String.format(
1400 "SET DNS forwarders: network=%s dnsServers=%s",
1401 network, Arrays.toString(dnsServers)));
1402 } catch (Exception e) {
1403 // TODO: Investigate how this can fail and what exactly
1404 // happens if/when such failures occur.
1405 mLog.e("setting DNS forwarders failed, " + e);
1406 transitionTo(mSetDnsForwardersErrorState);
1407 }
1408 }
Erik Kline00019f42016-06-30 19:31:46 +09001409
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001410 protected void notifyDownstreamsOfNewUpstreamIface(InterfaceSet ifaces) {
1411 mCurrentUpstreamIfaceSet = ifaces;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001412 for (IpServer ipServer : mNotifyList) {
1413 ipServer.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, ifaces);
Erik Kline00019f42016-06-30 19:31:46 +09001414 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001415 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001416
Erik Kline3a5278f2017-06-24 19:29:10 +09001417 protected void handleNewUpstreamNetworkState(NetworkState ns) {
1418 mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns);
Erik Klineed962a82017-07-06 19:49:35 +09001419 mOffload.updateUpstreamNetworkState(ns);
Erik Kline3a5278f2017-06-24 19:29:10 +09001420 }
1421
Erik Kline7a4ccc62018-08-27 17:26:47 +09001422 private void handleInterfaceServingStateActive(int mode, IpServer who) {
Erik Klineea9cc482017-03-10 19:35:34 +09001423 if (mNotifyList.indexOf(who) < 0) {
1424 mNotifyList.add(who);
1425 mIPv6TetheringCoordinator.addActiveDownstream(who, mode);
1426 }
1427
Erik Kline7a4ccc62018-08-27 17:26:47 +09001428 if (mode == IpServer.STATE_TETHERED) {
Erik Klineed962a82017-07-06 19:49:35 +09001429 // No need to notify OffloadController just yet as there are no
1430 // "offload-able" prefixes to pass along. This will handled
1431 // when the TISM informs Tethering of its LinkProperties.
Erik Klineea9cc482017-03-10 19:35:34 +09001432 mForwardedDownstreams.add(who);
1433 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001434 mOffload.excludeDownstreamInterface(who.interfaceName());
Erik Klineea9cc482017-03-10 19:35:34 +09001435 mForwardedDownstreams.remove(who);
1436 }
Erik Kline216af6d2017-04-27 20:57:23 +09001437
1438 // If this is a Wi-Fi interface, notify WifiManager of the active serving state.
Erik Kline7a26ba32018-03-09 14:18:02 +09001439 if (who.interfaceType() == TETHERING_WIFI) {
Erik Kline216af6d2017-04-27 20:57:23 +09001440 final WifiManager mgr = getWifiManager();
1441 final String iface = who.interfaceName();
1442 switch (mode) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001443 case IpServer.STATE_TETHERED:
Erik Kline2efb8272017-05-31 15:53:53 +09001444 mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED);
Erik Kline216af6d2017-04-27 20:57:23 +09001445 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001446 case IpServer.STATE_LOCAL_ONLY:
Erik Kline2efb8272017-05-31 15:53:53 +09001447 mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY);
Erik Kline216af6d2017-04-27 20:57:23 +09001448 break;
1449 default:
1450 Log.wtf(TAG, "Unknown active serving mode: " + mode);
1451 break;
1452 }
1453 }
Erik Klineea9cc482017-03-10 19:35:34 +09001454 }
1455
Erik Kline7a4ccc62018-08-27 17:26:47 +09001456 private void handleInterfaceServingStateInactive(IpServer who) {
Erik Klineea9cc482017-03-10 19:35:34 +09001457 mNotifyList.remove(who);
1458 mIPv6TetheringCoordinator.removeActiveDownstream(who);
Erik Klineed962a82017-07-06 19:49:35 +09001459 mOffload.excludeDownstreamInterface(who.interfaceName());
Erik Klineea9cc482017-03-10 19:35:34 +09001460 mForwardedDownstreams.remove(who);
Erik Kline216af6d2017-04-27 20:57:23 +09001461
1462 // If this is a Wi-Fi interface, tell WifiManager of any errors.
Erik Kline7a26ba32018-03-09 14:18:02 +09001463 if (who.interfaceType() == TETHERING_WIFI) {
1464 if (who.lastError() != TETHER_ERROR_NO_ERROR) {
Erik Kline216af6d2017-04-27 20:57:23 +09001465 getWifiManager().updateInterfaceIpState(
Erik Kline2efb8272017-05-31 15:53:53 +09001466 who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR);
Erik Kline216af6d2017-04-27 20:57:23 +09001467 }
1468 }
Erik Klineea9cc482017-03-10 19:35:34 +09001469 }
1470
Erik Kline3a5278f2017-06-24 19:29:10 +09001471 private void handleUpstreamNetworkMonitorCallback(int arg1, Object o) {
Erik Kline32179ff2017-07-04 18:28:11 +09001472 if (arg1 == UpstreamNetworkMonitor.NOTIFY_LOCAL_PREFIXES) {
Erik Klineed962a82017-07-06 19:49:35 +09001473 mOffload.sendOffloadExemptPrefixes((Set<IpPrefix>) o);
Erik Kline3a5278f2017-06-24 19:29:10 +09001474 return;
1475 }
1476
1477 final NetworkState ns = (NetworkState) o;
1478
1479 if (ns == null || !pertainsToCurrentUpstream(ns)) {
1480 // TODO: In future, this is where upstream evaluation and selection
1481 // could be handled for notifications which include sufficient data.
1482 // For example, after CONNECTIVITY_ACTION listening is removed, here
1483 // is where we could observe a Wi-Fi network becoming available and
1484 // passing validation.
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001485 if (mCurrentUpstreamIfaceSet == null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001486 // If we have no upstream interface, try to run through upstream
1487 // selection again. If, for example, IPv4 connectivity has shown up
1488 // after IPv6 (e.g., 464xlat became available) we want the chance to
1489 // notice and act accordingly.
1490 chooseUpstreamType(false);
1491 }
1492 return;
1493 }
1494
1495 switch (arg1) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001496 case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES:
1497 handleNewUpstreamNetworkState(ns);
1498 break;
1499 case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES:
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001500 chooseUpstreamType(false);
Erik Kline3a5278f2017-06-24 19:29:10 +09001501 break;
1502 case UpstreamNetworkMonitor.EVENT_ON_LOST:
1503 // TODO: Re-evaluate possible upstreams. Currently upstream
1504 // reevaluation is triggered via received CONNECTIVITY_ACTION
1505 // broadcasts that result in being passed a
1506 // TetherMasterSM.CMD_UPSTREAM_CHANGED.
1507 handleNewUpstreamNetworkState(null);
1508 break;
1509 default:
1510 mLog.e("Unknown arg1 value: " + arg1);
1511 break;
1512 }
1513 }
1514
1515 class TetherModeAliveState extends State {
Erik Klineea9cc482017-03-10 19:35:34 +09001516 boolean mUpstreamWanted = false;
Erik Kline6ff17f72015-12-10 20:42:12 +09001517 boolean mTryCell = true;
Erik Klinee0cce212017-03-06 14:05:23 +09001518
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001519 @Override
1520 public void enter() {
Erik Kline1fdc2e22017-05-08 17:56:35 +09001521 // If turning on master tether settings fails, we have already
1522 // transitioned to an error state; exit early.
1523 if (!turnOnMasterTetherSettings()) {
1524 return;
1525 }
1526
Erik Kline72302902018-06-14 17:36:40 +09001527 mUpstreamNetworkMonitor.start(mDeps.getDefaultNetworkRequest());
Robert Greenwalt4f74d552011-12-19 16:59:31 -08001528
Erik Klinef4b6e342017-04-25 19:19:59 +09001529 // TODO: De-duplicate with updateUpstreamWanted() below.
Erik Klineea9cc482017-03-10 19:35:34 +09001530 if (upstreamWanted()) {
1531 mUpstreamWanted = true;
Erik Klineed962a82017-07-06 19:49:35 +09001532 mOffload.start();
Erik Klineea9cc482017-03-10 19:35:34 +09001533 chooseUpstreamType(true);
1534 mTryCell = false;
1535 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001536 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001537
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001538 @Override
1539 public void exit() {
Erik Klineed962a82017-07-06 19:49:35 +09001540 mOffload.stop();
Erik Kline6ff17f72015-12-10 20:42:12 +09001541 mUpstreamNetworkMonitor.stop();
Erik Kline22108902017-07-06 16:40:06 +09001542 notifyDownstreamsOfNewUpstreamIface(null);
Erik Klinefa37b2f2016-08-02 18:27:03 +09001543 handleNewUpstreamNetworkState(null);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001544 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001545
Erik Klineea9cc482017-03-10 19:35:34 +09001546 private boolean updateUpstreamWanted() {
1547 final boolean previousUpstreamWanted = mUpstreamWanted;
1548 mUpstreamWanted = upstreamWanted();
Erik Klinef4b6e342017-04-25 19:19:59 +09001549 if (mUpstreamWanted != previousUpstreamWanted) {
1550 if (mUpstreamWanted) {
Erik Klineed962a82017-07-06 19:49:35 +09001551 mOffload.start();
Erik Klinef4b6e342017-04-25 19:19:59 +09001552 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001553 mOffload.stop();
Erik Klinef4b6e342017-04-25 19:19:59 +09001554 }
1555 }
Erik Klineea9cc482017-03-10 19:35:34 +09001556 return previousUpstreamWanted;
1557 }
1558
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001559 @Override
1560 public boolean processMessage(Message message) {
Erik Kline22108902017-07-06 16:40:06 +09001561 logMessage(this, message.what);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001562 boolean retValue = true;
1563 switch (message.what) {
Erik Klineea9cc482017-03-10 19:35:34 +09001564 case EVENT_IFACE_SERVING_STATE_ACTIVE: {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001565 IpServer who = (IpServer) message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001566 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
Erik Klineea9cc482017-03-10 19:35:34 +09001567 handleInterfaceServingStateActive(message.arg1, who);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001568 who.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED,
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001569 mCurrentUpstreamIfaceSet);
Erik Klineea9cc482017-03-10 19:35:34 +09001570 // If there has been a change and an upstream is now
1571 // desired, kick off the selection process.
1572 final boolean previousUpstreamWanted = updateUpstreamWanted();
1573 if (!previousUpstreamWanted && mUpstreamWanted) {
1574 chooseUpstreamType(true);
1575 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001576 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001577 }
Erik Klineea9cc482017-03-10 19:35:34 +09001578 case EVENT_IFACE_SERVING_STATE_INACTIVE: {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001579 IpServer who = (IpServer) message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001580 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
Erik Klineea9cc482017-03-10 19:35:34 +09001581 handleInterfaceServingStateInactive(who);
1582
1583 if (mNotifyList.isEmpty()) {
Erik Kline47222fc2017-04-30 19:36:15 +09001584 // This transitions us out of TetherModeAliveState,
1585 // either to InitialState or an error state.
Erik Kline1fdc2e22017-05-08 17:56:35 +09001586 turnOffMasterTetherSettings();
1587 break;
1588 }
1589
1590 if (DBG) {
1591 Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
1592 " live requests:");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001593 for (IpServer o : mNotifyList) {
Erik Kline1fdc2e22017-05-08 17:56:35 +09001594 Log.d(TAG, " " + o);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001595 }
1596 }
Erik Klineea9cc482017-03-10 19:35:34 +09001597 // If there has been a change and an upstream is no
1598 // longer desired, release any mobile requests.
1599 final boolean previousUpstreamWanted = updateUpstreamWanted();
1600 if (previousUpstreamWanted && !mUpstreamWanted) {
1601 mUpstreamNetworkMonitor.releaseMobileNetworkRequest();
1602 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001603 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001604 }
Erik Kline6e9a1012017-06-06 19:24:21 +09001605 case EVENT_IFACE_UPDATE_LINKPROPERTIES: {
1606 final LinkProperties newLp = (LinkProperties) message.obj;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001607 if (message.arg1 == IpServer.STATE_TETHERED) {
Erik Klineed962a82017-07-06 19:49:35 +09001608 mOffload.updateDownstreamLinkProperties(newLp);
Erik Kline6e9a1012017-06-06 19:24:21 +09001609 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001610 mOffload.excludeDownstreamInterface(newLp.getInterfaceName());
Erik Kline6e9a1012017-06-06 19:24:21 +09001611 }
1612 break;
1613 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001614 case CMD_UPSTREAM_CHANGED:
Erik Klineea9cc482017-03-10 19:35:34 +09001615 updateUpstreamWanted();
1616 if (!mUpstreamWanted) break;
1617
Erik Klinefb413432017-02-14 18:26:04 +09001618 // Need to try DUN immediately if Wi-Fi goes down.
1619 chooseUpstreamType(true);
1620 mTryCell = false;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001621 break;
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001622 case CMD_RETRY_UPSTREAM:
Erik Klineea9cc482017-03-10 19:35:34 +09001623 updateUpstreamWanted();
1624 if (!mUpstreamWanted) break;
1625
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001626 chooseUpstreamType(mTryCell);
1627 mTryCell = !mTryCell;
1628 break;
Erik Kline00019f42016-06-30 19:31:46 +09001629 case EVENT_UPSTREAM_CALLBACK: {
Erik Klineea9cc482017-03-10 19:35:34 +09001630 updateUpstreamWanted();
Erik Kline3a5278f2017-06-24 19:29:10 +09001631 if (mUpstreamWanted) {
1632 handleUpstreamNetworkMonitorCallback(message.arg1, message.obj);
Erik Kline6ff17f72015-12-10 20:42:12 +09001633 }
1634 break;
Erik Kline00019f42016-06-30 19:31:46 +09001635 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001636 default:
1637 retValue = false;
1638 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001639 }
1640 return retValue;
1641 }
1642 }
1643
Wink Saville64c42ca2011-04-18 14:55:10 -07001644 class ErrorState extends State {
Erik Kline8351faa2017-04-17 16:47:23 +09001645 private int mErrorNotification;
1646
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001647 @Override
1648 public boolean processMessage(Message message) {
1649 boolean retValue = true;
1650 switch (message.what) {
Erik Klineea9cc482017-03-10 19:35:34 +09001651 case EVENT_IFACE_SERVING_STATE_ACTIVE:
Erik Kline7a4ccc62018-08-27 17:26:47 +09001652 IpServer who = (IpServer) message.obj;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001653 who.sendMessage(mErrorNotification);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001654 break;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001655 case CMD_CLEAR_ERROR:
Erik Kline7a26ba32018-03-09 14:18:02 +09001656 mErrorNotification = TETHER_ERROR_NO_ERROR;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001657 transitionTo(mInitialState);
1658 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001659 default:
1660 retValue = false;
1661 }
1662 return retValue;
1663 }
Erik Kline8351faa2017-04-17 16:47:23 +09001664
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001665 void notify(int msgType) {
1666 mErrorNotification = msgType;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001667 for (IpServer ipServer : mNotifyList) {
1668 ipServer.sendMessage(msgType);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001669 }
1670 }
1671
1672 }
Erik Kline8351faa2017-04-17 16:47:23 +09001673
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001674 class SetIpForwardingEnabledErrorState extends ErrorState {
1675 @Override
1676 public void enter() {
1677 Log.e(TAG, "Error in setIpForwardingEnabled");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001678 notify(IpServer.CMD_IP_FORWARDING_ENABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001679 }
1680 }
1681
1682 class SetIpForwardingDisabledErrorState extends ErrorState {
1683 @Override
1684 public void enter() {
1685 Log.e(TAG, "Error in setIpForwardingDisabled");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001686 notify(IpServer.CMD_IP_FORWARDING_DISABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001687 }
1688 }
1689
1690 class StartTetheringErrorState extends ErrorState {
1691 @Override
1692 public void enter() {
1693 Log.e(TAG, "Error in startTethering");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001694 notify(IpServer.CMD_START_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001695 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001696 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001697 } catch (Exception e) {}
1698 }
1699 }
1700
1701 class StopTetheringErrorState extends ErrorState {
1702 @Override
1703 public void enter() {
1704 Log.e(TAG, "Error in stopTethering");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001705 notify(IpServer.CMD_STOP_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001706 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001707 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001708 } catch (Exception e) {}
1709 }
1710 }
1711
1712 class SetDnsForwardersErrorState extends ErrorState {
1713 @Override
1714 public void enter() {
1715 Log.e(TAG, "Error in setDnsForwarders");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001716 notify(IpServer.CMD_SET_DNS_FORWARDERS_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001717 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001718 mNMService.stopTethering();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001719 } catch (Exception e) {}
1720 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001721 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001722 } catch (Exception e) {}
1723 }
1724 }
Erik Klineed962a82017-07-06 19:49:35 +09001725
1726 // A wrapper class to handle multiple situations where several calls to
1727 // the OffloadController need to happen together.
1728 //
1729 // TODO: This suggests that the interface between OffloadController and
1730 // Tethering is in need of improvement. Refactor these calls into the
1731 // OffloadController implementation.
1732 class OffloadWrapper {
1733 public void start() {
1734 mOffloadController.start();
1735 sendOffloadExemptPrefixes();
1736 }
1737
1738 public void stop() {
1739 mOffloadController.stop();
1740 }
1741
1742 public void updateUpstreamNetworkState(NetworkState ns) {
1743 mOffloadController.setUpstreamLinkProperties(
1744 (ns != null) ? ns.linkProperties : null);
1745 }
1746
1747 public void updateDownstreamLinkProperties(LinkProperties newLp) {
1748 // Update the list of offload-exempt prefixes before adding
1749 // new prefixes on downstream interfaces to the offload HAL.
1750 sendOffloadExemptPrefixes();
1751 mOffloadController.notifyDownstreamLinkProperties(newLp);
1752 }
1753
1754 public void excludeDownstreamInterface(String ifname) {
1755 // This and other interfaces may be in local-only hotspot mode;
1756 // resend all local prefixes to the OffloadController.
1757 sendOffloadExemptPrefixes();
1758 mOffloadController.removeDownstreamInterface(ifname);
1759 }
1760
1761 public void sendOffloadExemptPrefixes() {
1762 sendOffloadExemptPrefixes(mUpstreamNetworkMonitor.getLocalPrefixes());
1763 }
1764
1765 public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) {
1766 // Add in well-known minimum set.
1767 PrefixUtils.addNonForwardablePrefixes(localPrefixes);
1768 // Add tragically hardcoded prefixes.
1769 localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX);
1770
1771 // Maybe add prefixes or addresses for downstreams, depending on
1772 // the IP serving mode of each.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001773 for (IpServer ipServer : mNotifyList) {
1774 final LinkProperties lp = ipServer.linkProperties();
Erik Klineed962a82017-07-06 19:49:35 +09001775
Erik Kline7a4ccc62018-08-27 17:26:47 +09001776 switch (ipServer.servingMode()) {
1777 case IpServer.STATE_UNAVAILABLE:
1778 case IpServer.STATE_AVAILABLE:
Erik Klineed962a82017-07-06 19:49:35 +09001779 // No usable LinkProperties in these states.
1780 continue;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001781 case IpServer.STATE_TETHERED:
Erik Klineed962a82017-07-06 19:49:35 +09001782 // Only add IPv4 /32 and IPv6 /128 prefixes. The
1783 // directly-connected prefixes will be sent as
1784 // downstream "offload-able" prefixes.
1785 for (LinkAddress addr : lp.getAllLinkAddresses()) {
1786 final InetAddress ip = addr.getAddress();
1787 if (ip.isLinkLocalAddress()) continue;
1788 localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip));
1789 }
1790 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001791 case IpServer.STATE_LOCAL_ONLY:
Erik Klineed962a82017-07-06 19:49:35 +09001792 // Add prefixes covering all local IPs.
1793 localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp));
1794 break;
1795 }
1796 }
1797
1798 mOffloadController.setLocalPrefixes(localPrefixes);
1799 }
1800 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001801 }
1802
Christopher Wiley499a57a2016-05-16 16:19:07 -07001803 @Override
Lorenzo Colittie3805462015-06-03 11:18:24 +09001804 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Christopher Wiley499a57a2016-05-16 16:19:07 -07001805 // Binder.java closes the resource for us.
1806 @SuppressWarnings("resource")
Lorenzo Colittie3805462015-06-03 11:18:24 +09001807 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001808 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001809
Lorenzo Colittie3805462015-06-03 11:18:24 +09001810 pw.println("Tethering:");
1811 pw.increaseIndent();
Lorenzo Colittie3805462015-06-03 11:18:24 +09001812
Erik Kline9db1b542017-03-16 14:10:27 +09001813 pw.println("Configuration:");
1814 pw.increaseIndent();
1815 final TetheringConfiguration cfg = mConfig;
1816 cfg.dump(pw);
1817 pw.decreaseIndent();
1818
1819 synchronized (mPublicSync) {
Robert Greenwaltb4453622011-11-03 16:01:40 -07001820 pw.println("Tether state:");
Lorenzo Colittie3805462015-06-03 11:18:24 +09001821 pw.increaseIndent();
Christopher Wileyd985dde2016-05-31 10:44:35 -07001822 for (int i = 0; i < mTetherStates.size(); i++) {
1823 final String iface = mTetherStates.keyAt(i);
1824 final TetherState tetherState = mTetherStates.valueAt(i);
1825 pw.print(iface + " - ");
1826
Hugo Benichib55fb222017-03-10 14:20:57 +09001827 switch (tetherState.lastState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001828 case IpServer.STATE_UNAVAILABLE:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001829 pw.print("UnavailableState");
1830 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001831 case IpServer.STATE_AVAILABLE:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001832 pw.print("AvailableState");
1833 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001834 case IpServer.STATE_TETHERED:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001835 pw.print("TetheredState");
1836 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001837 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +09001838 pw.print("LocalHotspotState");
1839 break;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001840 default:
1841 pw.print("UnknownState");
1842 break;
1843 }
Hugo Benichib55fb222017-03-10 14:20:57 +09001844 pw.println(" - lastError = " + tetherState.lastError);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001845 }
Erik Klineea9cc482017-03-10 19:35:34 +09001846 pw.println("Upstream wanted: " + upstreamWanted());
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001847 pw.println("Current upstream interface(s): " + mCurrentUpstreamIfaceSet);
Lorenzo Colittie3805462015-06-03 11:18:24 +09001848 pw.decreaseIndent();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001849 }
Erik Kline1fdc2e22017-05-08 17:56:35 +09001850
Lorenzo Colittic2519c52017-07-13 01:48:26 +09001851 pw.println("Hardware offload:");
1852 pw.increaseIndent();
1853 mOffloadController.dump(pw);
1854 pw.decreaseIndent();
1855
Erik Kline1fdc2e22017-05-08 17:56:35 +09001856 pw.println("Log:");
1857 pw.increaseIndent();
Erik Kline7747fd42017-05-12 16:52:48 +09001858 if (argsContain(args, SHORT_ARG)) {
1859 pw.println("<log removed for brevity>");
1860 } else {
1861 mLog.dump(fd, pw, args);
1862 }
Erik Kline1fdc2e22017-05-08 17:56:35 +09001863 pw.decreaseIndent();
1864
Lorenzo Colittie3805462015-06-03 11:18:24 +09001865 pw.decreaseIndent();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001866 }
Christopher Wileye03fb442016-05-18 13:45:20 -07001867
Erik Kline7747fd42017-05-12 16:52:48 +09001868 private static boolean argsContain(String[] args, String target) {
1869 for (String arg : args) {
Erik Klineee363c42017-05-29 09:11:03 +09001870 if (target.equals(arg)) return true;
Erik Kline7747fd42017-05-12 16:52:48 +09001871 }
1872 return false;
1873 }
1874
Erik Kline7a4ccc62018-08-27 17:26:47 +09001875 private IpServer.Callback makeControlCallback() {
1876 return new IpServer.Callback() {
Erik Kline6e9a1012017-06-06 19:24:21 +09001877 @Override
Erik Kline7a4ccc62018-08-27 17:26:47 +09001878 public void updateInterfaceState(IpServer who, int state, int lastError) {
1879 notifyInterfaceStateChange(who, state, lastError);
Erik Kline6e9a1012017-06-06 19:24:21 +09001880 }
1881
1882 @Override
Erik Kline7a4ccc62018-08-27 17:26:47 +09001883 public void updateLinkProperties(IpServer who, LinkProperties newLp) {
1884 notifyLinkPropertiesChanged(who, newLp);
Erik Kline6e9a1012017-06-06 19:24:21 +09001885 }
1886 };
1887 }
1888
1889 // TODO: Move into TetherMasterSM.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001890 private void notifyInterfaceStateChange(IpServer who, int state, int error) {
1891 final String iface = who.interfaceName();
Christopher Wileyd985dde2016-05-31 10:44:35 -07001892 synchronized (mPublicSync) {
Erik Kline216af6d2017-04-27 20:57:23 +09001893 final TetherState tetherState = mTetherStates.get(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001894 if (tetherState != null && tetherState.ipServer.equals(who)) {
Hugo Benichib55fb222017-03-10 14:20:57 +09001895 tetherState.lastState = state;
1896 tetherState.lastError = error;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001897 } else {
1898 if (DBG) Log.d(TAG, "got notification from stale iface " + iface);
1899 }
1900 }
1901
Erik Kline7747fd42017-05-12 16:52:48 +09001902 mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error));
Christopher Wileyd985dde2016-05-31 10:44:35 -07001903
Christopher Wileydeebfec2016-09-16 11:14:36 -07001904 try {
1905 // Notify that we're tethering (or not) this interface.
1906 // This is how data saver for instance knows if the user explicitly
1907 // turned on tethering (thus keeping us from being in data saver mode).
Erik Kline7a4ccc62018-08-27 17:26:47 +09001908 mPolicyManager.onTetheringChanged(iface, state == IpServer.STATE_TETHERED);
Christopher Wileydeebfec2016-09-16 11:14:36 -07001909 } catch (RemoteException e) {
1910 // Not really very much we can do here.
1911 }
1912
Yohei, Oshima977aad52016-12-08 13:39:20 +09001913 // If TetherMasterSM is in ErrorState, TetherMasterSM stays there.
1914 // Thus we give a chance for TetherMasterSM to recover to InitialState
1915 // by sending CMD_CLEAR_ERROR
Erik Kline7a26ba32018-03-09 14:18:02 +09001916 if (error == TETHER_ERROR_MASTER_ERROR) {
Yohei, Oshima977aad52016-12-08 13:39:20 +09001917 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who);
1918 }
Erik Klineea9cc482017-03-10 19:35:34 +09001919 int which;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001920 switch (state) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001921 case IpServer.STATE_UNAVAILABLE:
1922 case IpServer.STATE_AVAILABLE:
Erik Klineea9cc482017-03-10 19:35:34 +09001923 which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001924 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001925 case IpServer.STATE_TETHERED:
1926 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +09001927 which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001928 break;
Erik Klineea9cc482017-03-10 19:35:34 +09001929 default:
1930 Log.wtf(TAG, "Unknown interface state: " + state);
1931 return;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001932 }
Erik Klineea9cc482017-03-10 19:35:34 +09001933 mTetherMasterSM.sendMessage(which, state, 0, who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001934 sendTetherStateChangedBroadcast();
1935 }
1936
Erik Kline7a4ccc62018-08-27 17:26:47 +09001937 private void notifyLinkPropertiesChanged(IpServer who, LinkProperties newLp) {
1938 final String iface = who.interfaceName();
Erik Kline6e9a1012017-06-06 19:24:21 +09001939 final int state;
1940 synchronized (mPublicSync) {
1941 final TetherState tetherState = mTetherStates.get(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001942 if (tetherState != null && tetherState.ipServer.equals(who)) {
Erik Kline6e9a1012017-06-06 19:24:21 +09001943 state = tetherState.lastState;
1944 } else {
1945 mLog.log("got notification from stale iface " + iface);
1946 return;
1947 }
1948 }
1949
Erik Kline7fd696c2017-06-12 18:20:08 +09001950 mLog.log(String.format(
1951 "OBSERVED LinkProperties update iface=%s state=%s lp=%s",
Erik Kline7a4ccc62018-08-27 17:26:47 +09001952 iface, IpServer.getStateString(state), newLp));
Erik Kline6e9a1012017-06-06 19:24:21 +09001953 final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES;
1954 mTetherMasterSM.sendMessage(which, state, 0, newLp);
1955 }
1956
Erik Kline4dd9bb82017-04-26 11:11:07 +09001957 private void maybeTrackNewInterfaceLocked(final String iface) {
1958 // If we don't care about this type of interface, ignore.
1959 final int interfaceType = ifaceNameToType(iface);
Erik Kline7a26ba32018-03-09 14:18:02 +09001960 if (interfaceType == TETHERING_INVALID) {
Erik Kline4dd9bb82017-04-26 11:11:07 +09001961 mLog.log(iface + " is not a tetherable iface, ignoring");
1962 return;
1963 }
Erik Klinea9cde8b2017-06-20 21:18:31 +09001964 maybeTrackNewInterfaceLocked(iface, interfaceType);
1965 }
Erik Kline4dd9bb82017-04-26 11:11:07 +09001966
Erik Klinea9cde8b2017-06-20 21:18:31 +09001967 private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) {
Erik Kline4dd9bb82017-04-26 11:11:07 +09001968 // If we have already started a TISM for this interface, skip.
1969 if (mTetherStates.containsKey(iface)) {
1970 mLog.log("active iface (" + iface + ") reported as added, ignoring");
1971 return;
1972 }
1973
1974 mLog.log("adding TetheringInterfaceStateMachine for: " + iface);
1975 final TetherState tetherState = new TetherState(
Erik Kline7a4ccc62018-08-27 17:26:47 +09001976 new IpServer(iface, mLooper, interfaceType, mLog, mNMService, mStatsService,
1977 makeControlCallback(), mConfig.enableLegacyDhcpServer,
1978 mDeps.getIpServerDependencies()));
Christopher Wileyd985dde2016-05-31 10:44:35 -07001979 mTetherStates.put(iface, tetherState);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001980 tetherState.ipServer.start();
Christopher Wileye03fb442016-05-18 13:45:20 -07001981 }
Erik Kline3e756652017-01-17 13:42:19 +09001982
Erik Kline4dd9bb82017-04-26 11:11:07 +09001983 private void stopTrackingInterfaceLocked(final String iface) {
1984 final TetherState tetherState = mTetherStates.get(iface);
1985 if (tetherState == null) {
1986 mLog.log("attempting to remove unknown iface (" + iface + "), ignoring");
1987 return;
1988 }
Erik Kline7a4ccc62018-08-27 17:26:47 +09001989 tetherState.ipServer.stop();
Erik Kline4dd9bb82017-04-26 11:11:07 +09001990 mLog.log("removing TetheringInterfaceStateMachine for: " + iface);
1991 mTetherStates.remove(iface);
1992 }
1993
Erik Kline3e756652017-01-17 13:42:19 +09001994 private static String[] copy(String[] strarray) {
1995 return Arrays.copyOf(strarray, strarray.length);
1996 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001997}