blob: 37fe3d0941794385d5f25d07f920f4a05a9e462f [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;
Erik Kline7a26ba32018-03-09 14:18:02 +090026import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER;
27import static android.net.ConnectivityManager.EXTRA_ERRORED_TETHER;
28import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO;
Erik Kline7a26ba32018-03-09 14:18:02 +090029import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
30import static android.net.ConnectivityManager.TETHERING_INVALID;
31import static android.net.ConnectivityManager.TETHERING_USB;
32import static android.net.ConnectivityManager.TETHERING_WIFI;
markchienb6eb2c22018-07-18 14:29:20 +080033import static android.net.ConnectivityManager.TETHER_ERROR_MASTER_ERROR;
34import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
35import static android.net.ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL;
36import static android.net.ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
37import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
Erik Kline2efb8272017-05-31 15:53:53 +090038import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME;
39import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE;
Erik Kline2e88b5e2017-01-18 11:57:45 +090040import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE;
Erik Kline2efb8272017-05-31 15:53:53 +090041import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR;
42import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY;
43import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED;
44import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED;
Erik Kline2e88b5e2017-01-18 11:57:45 +090045import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED;
Erik Klinede637722017-10-12 22:16:01 +090046import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED;
markchienb6eb2c22018-07-18 14:29:20 +080047
Erik Kline7747fd42017-05-12 16:52:48 +090048import static com.android.server.ConnectivityService.SHORT_ARG;
Erik Kline2e88b5e2017-01-18 11:57:45 +090049
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080050import android.app.Notification;
51import android.app.NotificationManager;
52import android.app.PendingIntent;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080053import android.bluetooth.BluetoothAdapter;
54import android.bluetooth.BluetoothPan;
55import android.bluetooth.BluetoothProfile;
56import android.bluetooth.BluetoothProfile.ServiceListener;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080057import android.content.BroadcastReceiver;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080058import android.content.Context;
59import android.content.Intent;
60import android.content.IntentFilter;
61import android.content.res.Resources;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080062import android.hardware.usb.UsbManager;
Christopher Wileydeebfec2016-09-16 11:14:36 -070063import android.net.INetworkPolicyManager;
Jeff Sharkey367d15a2011-09-22 14:59:51 -070064import android.net.INetworkStatsService;
markchien26299ed2019-02-27 14:56:11 +080065import android.net.ITetheringEventCallback;
Erik Kline3a5278f2017-06-24 19:29:10 +090066import android.net.IpPrefix;
Erik Klineed962a82017-07-06 19:49:35 +090067import android.net.LinkAddress;
Robert Greenwalt05d89362011-01-23 16:04:05 -080068import android.net.LinkProperties;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070069import android.net.Network;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080070import android.net.NetworkInfo;
Erik Kline6ff17f72015-12-10 20:42:12 +090071import android.net.NetworkState;
Robert Greenwalte5903732011-02-22 16:00:42 -080072import android.net.NetworkUtils;
markchienb6eb2c22018-07-18 14:29:20 +080073import android.net.ip.IpServer;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +090074import android.net.util.InterfaceSet;
Erik Kline32179ff2017-07-04 18:28:11 +090075import android.net.util.PrefixUtils;
Erik Kline7747fd42017-05-12 16:52:48 +090076import android.net.util.SharedLog;
Erik Klinede637722017-10-12 22:16:01 +090077import android.net.util.VersionedBroadcastListener;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080078import android.net.wifi.WifiManager;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080079import android.os.Binder;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080080import android.os.Bundle;
Erik Klinef4b6e342017-04-25 19:19:59 +090081import android.os.Handler;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082import android.os.INetworkManagementService;
Robert Greenwaltdfadaea2010-03-11 15:03:08 -080083import android.os.Looper;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080084import android.os.Message;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080085import android.os.Parcel;
markchien26299ed2019-02-27 14:56:11 +080086import android.os.RemoteCallbackList;
Christopher Wileydeebfec2016-09-16 11:14:36 -070087import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080088import android.os.ResultReceiver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070089import android.os.UserHandle;
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +010090import android.os.UserManager;
91import android.os.UserManagerInternal;
92import android.os.UserManagerInternal.UserRestrictionsListener;
Lorenzo Colitticd63d242016-04-10 15:39:53 +090093import android.text.TextUtils;
Christopher Wileye9490392016-05-26 15:57:29 -070094import android.util.ArrayMap;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080095import android.util.Log;
Lorenzo Colitticd63d242016-04-10 15:39:53 +090096import android.util.SparseArray;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080097
Christopher Wiley497c1472016-10-11 13:26:03 -070098import com.android.internal.annotations.VisibleForTesting;
Chris Wren282cfef2017-03-27 15:01:44 -040099import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500100import com.android.internal.notification.SystemNotificationChannels;
markchien0b595072019-01-08 23:52:21 +0800101import com.android.internal.telephony.TelephonyIntents;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600102import com.android.internal.util.DumpUtils;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800103import com.android.internal.util.IndentingPrintWriter;
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900104import com.android.internal.util.MessageUtils;
105import com.android.internal.util.Protocol;
Wink Saville64c42ca2011-04-18 14:55:10 -0700106import com.android.internal.util.State;
107import com.android.internal.util.StateMachine;
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100108import com.android.server.LocalServices;
markchienb6eb2c22018-07-18 14:29:20 +0800109import com.android.server.connectivity.tethering.EntitlementManager;
Erik Kline1eb8c692016-07-08 17:21:26 +0900110import com.android.server.connectivity.tethering.IPv6TetheringCoordinator;
Erik Klinee0cce212017-03-06 14:05:23 +0900111import com.android.server.connectivity.tethering.OffloadController;
Hugo Benichib55fb222017-03-10 14:20:57 +0900112import com.android.server.connectivity.tethering.TetheringConfiguration;
Erik Kline47222fc2017-04-30 19:36:15 +0900113import com.android.server.connectivity.tethering.TetheringDependencies;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +0900114import com.android.server.connectivity.tethering.TetheringInterfaceUtils;
Erik Kline9bba3402017-01-13 16:46:52 +0900115import com.android.server.connectivity.tethering.UpstreamNetworkMonitor;
Lorenzo Colittif4e90ea2013-10-31 23:30:47 +0900116import com.android.server.net.BaseNetworkObserver;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800117
118import java.io.FileDescriptor;
119import java.io.PrintWriter;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800120import java.net.InetAddress;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800121import java.util.ArrayList;
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700122import java.util.Arrays;
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700123import java.util.Collection;
Erik Klineea9cc482017-03-10 19:35:34 +0900124import java.util.HashSet;
Erik Kline3a5278f2017-06-24 19:29:10 +0900125import java.util.Set;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -0800126
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800127/**
128 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800129 *
Christopher Wiley3b1d9222016-05-20 16:44:04 -0700130 * This class holds much of the business logic to allow Android devices
131 * to act as IP gateways via USB, BT, and WiFi interfaces.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800132 */
Erik Kline6e9a1012017-06-06 19:24:21 +0900133public class Tethering extends BaseNetworkObserver {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800134
Hugo Benichib55fb222017-03-10 14:20:57 +0900135 private final static String TAG = Tethering.class.getSimpleName();
Joe Onorato12acbd72016-02-01 17:49:31 -0800136 private final static boolean DBG = false;
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800137 private final static boolean VDBG = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800138
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900139 private static final Class[] messageClasses = {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900140 Tethering.class, TetherMasterSM.class, IpServer.class
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900141 };
142 private static final SparseArray<String> sMagicDecoderRing =
143 MessageUtils.findMessageNames(messageClasses);
144
Hugo Benichib55fb222017-03-10 14:20:57 +0900145 private static class TetherState {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900146 public final IpServer ipServer;
Hugo Benichib55fb222017-03-10 14:20:57 +0900147 public int lastState;
148 public int lastError;
Erik Klineea9cc482017-03-10 19:35:34 +0900149
Erik Kline7a4ccc62018-08-27 17:26:47 +0900150 public TetherState(IpServer ipServer) {
151 this.ipServer = ipServer;
Hugo Benichib55fb222017-03-10 14:20:57 +0900152 // Assume all state machines start out available and with no errors.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900153 lastState = IpServer.STATE_AVAILABLE;
Erik Kline7a26ba32018-03-09 14:18:02 +0900154 lastError = TETHER_ERROR_NO_ERROR;
Hugo Benichib55fb222017-03-10 14:20:57 +0900155 }
Erik Klineea9cc482017-03-10 19:35:34 +0900156
157 public boolean isCurrentlyServing() {
158 switch (lastState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900159 case IpServer.STATE_TETHERED:
160 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +0900161 return true;
162 default:
163 return false;
164 }
165 }
Hugo Benichib55fb222017-03-10 14:20:57 +0900166 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700167
Erik Kline7747fd42017-05-12 16:52:48 +0900168 private final SharedLog mLog = new SharedLog(TAG);
Erik Kline1fdc2e22017-05-08 17:56:35 +0900169
Robert Greenwaltb4453622011-11-03 16:01:40 -0700170 // used to synchronize public access to members
Erik Kline6ff17f72015-12-10 20:42:12 +0900171 private final Object mPublicSync;
Hugo Benichib55fb222017-03-10 14:20:57 +0900172 private final Context mContext;
173 private final ArrayMap<String, TetherState> mTetherStates;
174 private final BroadcastReceiver mStateReceiver;
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700175 private final INetworkManagementService mNMService;
176 private final INetworkStatsService mStatsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700177 private final INetworkPolicyManager mPolicyManager;
Erik Kline6ff17f72015-12-10 20:42:12 +0900178 private final Looper mLooper;
Erik Kline6ff17f72015-12-10 20:42:12 +0900179 private final StateMachine mTetherMasterSM;
Erik Klinee0cce212017-03-06 14:05:23 +0900180 private final OffloadController mOffloadController;
Erik Kline6ff17f72015-12-10 20:42:12 +0900181 private final UpstreamNetworkMonitor mUpstreamNetworkMonitor;
Erik Kline6e9a1012017-06-06 19:24:21 +0900182 // TODO: Figure out how to merge this and other downstream-tracking objects
183 // into a single coherent structure.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900184 private final HashSet<IpServer> mForwardedDownstreams;
Erik Klinede637722017-10-12 22:16:01 +0900185 private final VersionedBroadcastListener mCarrierConfigChange;
markchien0b595072019-01-08 23:52:21 +0800186 private final VersionedBroadcastListener mDefaultSubscriptionChange;
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900187 private final TetheringDependencies mDeps;
markchienb6eb2c22018-07-18 14:29:20 +0800188 private final EntitlementManager mEntitlementMgr;
markchien26299ed2019-02-27 14:56:11 +0800189 private final Handler mHandler;
190 private final RemoteCallbackList<ITetheringEventCallback> mTetheringEventCallbacks =
191 new RemoteCallbackList<>();
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800192
Hugo Benichib55fb222017-03-10 14:20:57 +0900193 private volatile TetheringConfiguration mConfig;
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +0900194 private InterfaceSet mCurrentUpstreamIfaceSet;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400195 private Notification.Builder mTetheredNotificationBuilder;
196 private int mLastNotificationId;
Erik Klineea9cc482017-03-10 19:35:34 +0900197
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700198 private boolean mRndisEnabled; // track the RNDIS function enabled state
Erik Kline6e9a1012017-06-06 19:24:21 +0900199 // True iff. WiFi tethering should be started when soft AP is ready.
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700200 private boolean mWifiTetherRequested;
markchien26299ed2019-02-27 14:56:11 +0800201 private Network mTetherUpstream;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700202
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700203 public Tethering(Context context, INetworkManagementService nmService,
Christopher Wiley497c1472016-10-11 13:26:03 -0700204 INetworkStatsService statsService, INetworkPolicyManager policyManager,
Erik Kline47222fc2017-04-30 19:36:15 +0900205 Looper looper, MockableSystemProperties systemProperties,
206 TetheringDependencies deps) {
Erik Kline7747fd42017-05-12 16:52:48 +0900207 mLog.mark("constructed");
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800208 mContext = context;
Chia-chi Yehc9338302011-05-11 16:35:13 -0700209 mNMService = nmService;
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700210 mStatsService = statsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700211 mPolicyManager = policyManager;
Christopher Wiley497c1472016-10-11 13:26:03 -0700212 mLooper = looper;
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900213 mDeps = deps;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800214
Robert Greenwaltb4453622011-11-03 16:01:40 -0700215 mPublicSync = new Object();
216
Christopher Wileyd985dde2016-05-31 10:44:35 -0700217 mTetherStates = new ArrayMap<>();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800218
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900219 mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper, deps);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800220 mTetherMasterSM.start();
221
markchien26299ed2019-02-27 14:56:11 +0800222 mHandler = mTetherMasterSM.getHandler();
223 mOffloadController = new OffloadController(mHandler,
224 mDeps.getOffloadHardwareInterface(mHandler, mLog),
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900225 mContext.getContentResolver(), mNMService,
Erik Klinef4b6e342017-04-25 19:19:59 +0900226 mLog);
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +0900227 mUpstreamNetworkMonitor = deps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog,
228 TetherMasterSM.EVENT_UPSTREAM_CALLBACK);
Erik Klineea9cc482017-03-10 19:35:34 +0900229 mForwardedDownstreams = new HashSet<>();
Erik Klinede637722017-10-12 22:16:01 +0900230
231 IntentFilter filter = new IntentFilter();
232 filter.addAction(ACTION_CARRIER_CONFIG_CHANGED);
markchienf2731272019-01-16 17:44:13 +0800233 mEntitlementMgr = mDeps.getEntitlementManager(mContext, mTetherMasterSM,
234 mLog, systemProperties);
Erik Klinede637722017-10-12 22:16:01 +0900235 mCarrierConfigChange = new VersionedBroadcastListener(
markchien26299ed2019-02-27 14:56:11 +0800236 "CarrierConfigChangeListener", mContext, mHandler, filter,
Erik Klinede637722017-10-12 22:16:01 +0900237 (Intent ignored) -> {
238 mLog.log("OBSERVED carrier config change");
Erik Kline80b7a9f2018-02-28 15:01:35 +0900239 updateConfiguration();
markchienb6eb2c22018-07-18 14:29:20 +0800240 mEntitlementMgr.reevaluateSimCardProvisioning();
Erik Klinede637722017-10-12 22:16:01 +0900241 });
Erik Kline6ff17f72015-12-10 20:42:12 +0900242
markchien0b595072019-01-08 23:52:21 +0800243 filter = new IntentFilter();
244 filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
245 mDefaultSubscriptionChange = new VersionedBroadcastListener(
markchien26299ed2019-02-27 14:56:11 +0800246 "DefaultSubscriptionChangeListener", mContext, mHandler, filter,
markchien0b595072019-01-08 23:52:21 +0800247 (Intent ignored) -> {
248 mLog.log("OBSERVED default data subscription change");
249 updateConfiguration();
markchien29a650c2019-03-19 20:57:04 +0800250 // To avoid launch unexpected provisioning checks, ignore re-provisioning when
251 // no CarrierConfig loaded yet. Assume reevaluateSimCardProvisioning() will be
252 // triggered again when CarrierConfig is loaded.
253 if (mEntitlementMgr.getCarrierConfig() != null) {
254 mEntitlementMgr.reevaluateSimCardProvisioning();
255 } else {
256 mLog.log("IGNORED reevaluate provisioning due to no carrier config loaded");
257 }
markchien0b595072019-01-08 23:52:21 +0800258 });
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800259 mStateReceiver = new StateReceiver();
Erik Kline80b7a9f2018-02-28 15:01:35 +0900260
261 // Load tethering configuration.
262 updateConfiguration();
263
markchien26299ed2019-02-27 14:56:11 +0800264 startStateMachineUpdaters(mHandler);
Erik Kline80b7a9f2018-02-28 15:01:35 +0900265 }
266
markchien26299ed2019-02-27 14:56:11 +0800267 private void startStateMachineUpdaters(Handler handler) {
Erik Kline80b7a9f2018-02-28 15:01:35 +0900268 mCarrierConfigChange.startListening();
markchien0b595072019-01-08 23:52:21 +0800269 mDefaultSubscriptionChange.startListening();
Erik Kline80b7a9f2018-02-28 15:01:35 +0900270
Erik Kline80b7a9f2018-02-28 15:01:35 +0900271 IntentFilter filter = new IntentFilter();
Mike Lockwood770126a2010-12-09 22:30:37 -0800272 filter.addAction(UsbManager.ACTION_USB_STATE);
Erik Kline7a26ba32018-03-09 14:18:02 +0900273 filter.addAction(CONNECTIVITY_ACTION);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700274 filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700275 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Erik Kline80b7a9f2018-02-28 15:01:35 +0900276 mContext.registerReceiver(mStateReceiver, filter, null, handler);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800277
278 filter = new IntentFilter();
Robert Greenwalt030f5e12010-03-10 16:41:03 -0800279 filter.addAction(Intent.ACTION_MEDIA_SHARED);
280 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800281 filter.addDataScheme("file");
Erik Kline80b7a9f2018-02-28 15:01:35 +0900282 mContext.registerReceiver(mStateReceiver, filter, null, handler);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800283
Erik Kline80b7a9f2018-02-28 15:01:35 +0900284 final UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
285 // This check is useful only for some unit tests; example: ConnectivityServiceTest.
286 if (umi != null) {
287 umi.addUserRestrictionsListener(new TetheringUserRestrictionListener(this));
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100288 }
Robert Greenwalt49348e72011-10-21 16:54:26 -0700289 }
290
Erik Kline216af6d2017-04-27 20:57:23 +0900291 private WifiManager getWifiManager() {
292 return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
293 }
294
Erik Kline93c4afa2017-06-04 11:36:01 +0900295 // NOTE: This is always invoked on the mLooper thread.
Erik Klined781fba2017-01-23 13:01:58 +0900296 private void updateConfiguration() {
markchien0b595072019-01-08 23:52:21 +0800297 final int subId = mDeps.getDefaultDataSubscriptionId();
298 mConfig = new TetheringConfiguration(mContext, mLog, subId);
Erik Kline93c4afa2017-06-04 11:36:01 +0900299 mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired);
markchienb6eb2c22018-07-18 14:29:20 +0800300 mEntitlementMgr.updateConfiguration(mConfig);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800301 }
302
Erik Kline6ee73da2017-07-08 20:36:37 +0900303 private void maybeUpdateConfiguration() {
markchiendb3a2362018-10-05 12:36:08 +0800304 final boolean isDunRequired = TetheringConfiguration.checkDunRequired(mContext);
305 if (isDunRequired == mConfig.isDunRequired) return;
Erik Kline6ee73da2017-07-08 20:36:37 +0900306 updateConfiguration();
307 }
308
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900309 @Override
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700310 public void interfaceStatusChanged(String iface, boolean up) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900311 // Never called directly: only called from interfaceLinkStateChanged.
312 // See NetlinkHandler.cpp:71.
Wink Savillec9acde92011-09-21 11:05:43 -0700313 if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700314 synchronized (mPublicSync) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700315 if (up) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900316 maybeTrackNewInterfaceLocked(iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800317 } else {
Erik Kline7a26ba32018-03-09 14:18:02 +0900318 if (ifaceNameToType(iface) == TETHERING_BLUETOOTH) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900319 stopTrackingInterfaceLocked(iface);
Christopher Wileyd30aaeb2016-07-08 09:33:50 -0700320 } else {
321 // Ignore usb0 down after enabling RNDIS.
322 // We will handle disconnect in interfaceRemoved.
323 // Similarly, ignore interface down for WiFi. We monitor WiFi AP status
324 // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent.
325 if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800326 }
327 }
328 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800329 }
330
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900331 @Override
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700332 public void interfaceLinkStateChanged(String iface, boolean up) {
Irfan Sheriff23eb2972011-07-22 15:21:10 -0700333 interfaceStatusChanged(iface, up);
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700334 }
335
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700336 private int ifaceNameToType(String iface) {
Erik Kline3e756652017-01-17 13:42:19 +0900337 final TetheringConfiguration cfg = mConfig;
338
339 if (cfg.isWifi(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900340 return TETHERING_WIFI;
Erik Kline3e756652017-01-17 13:42:19 +0900341 } else if (cfg.isUsb(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900342 return TETHERING_USB;
Erik Kline3e756652017-01-17 13:42:19 +0900343 } else if (cfg.isBluetooth(iface)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900344 return TETHERING_BLUETOOTH;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700345 }
Erik Kline7a26ba32018-03-09 14:18:02 +0900346 return TETHERING_INVALID;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700347 }
348
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900349 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800350 public void interfaceAdded(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700351 if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700352 synchronized (mPublicSync) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900353 maybeTrackNewInterfaceLocked(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800354 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800355 }
356
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900357 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800358 public void interfaceRemoved(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700359 if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700360 synchronized (mPublicSync) {
Erik Kline4dd9bb82017-04-26 11:11:07 +0900361 stopTrackingInterfaceLocked(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800362 }
363 }
364
Hugo Benichib55fb222017-03-10 14:20:57 +0900365 public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) {
markchienb6eb2c22018-07-18 14:29:20 +0800366 mEntitlementMgr.startTethering(type);
367 if (!mEntitlementMgr.isTetherProvisioningRequired()) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800368 enableTetheringInternal(type, true, receiver);
369 return;
370 }
371
markchienb6eb2c22018-07-18 14:29:20 +0800372 final ResultReceiver proxyReceiver = getProxyReceiver(type, receiver);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800373 if (showProvisioningUi) {
markchienb6eb2c22018-07-18 14:29:20 +0800374 mEntitlementMgr.runUiTetherProvisioningAndEnable(type, proxyReceiver);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800375 } else {
markchienb6eb2c22018-07-18 14:29:20 +0800376 mEntitlementMgr.runSilentTetherProvisioningAndEnable(type, proxyReceiver);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800377 }
378 }
379
380 public void stopTethering(int type) {
381 enableTetheringInternal(type, false, null);
markchienb6eb2c22018-07-18 14:29:20 +0800382 mEntitlementMgr.stopTethering(type);
383 if (mEntitlementMgr.isTetherProvisioningRequired()) {
384 // There are lurking bugs where the notion of "provisioning required" or
385 // "tethering supported" may change without notifying tethering properly, then
386 // tethering can't shutdown correctly.
387 // TODO: cancel re-check all the time
388 if (mDeps.isTetheringSupported()) {
389 mEntitlementMgr.cancelTetherProvisioningRechecks(type);
390 }
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800391 }
392 }
393
394 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800395 * Enables or disables tethering for the given type. This should only be called once
396 * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks
397 * for the specified interface.
398 */
399 private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) {
markchienb6eb2c22018-07-18 14:29:20 +0800400 boolean isProvisioningRequired = enable && mEntitlementMgr.isTetherProvisioningRequired();
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700401 int result;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800402 switch (type) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900403 case TETHERING_WIFI:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700404 result = setWifiTethering(enable);
Erik Kline7a26ba32018-03-09 14:18:02 +0900405 if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) {
markchienb6eb2c22018-07-18 14:29:20 +0800406 mEntitlementMgr.scheduleProvisioningRechecks(type);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800407 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700408 sendTetherResult(receiver, result);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800409 break;
Erik Kline7a26ba32018-03-09 14:18:02 +0900410 case TETHERING_USB:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700411 result = setUsbTethering(enable);
Erik Kline7a26ba32018-03-09 14:18:02 +0900412 if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) {
markchienb6eb2c22018-07-18 14:29:20 +0800413 mEntitlementMgr.scheduleProvisioningRechecks(type);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800414 }
415 sendTetherResult(receiver, result);
416 break;
Erik Kline7a26ba32018-03-09 14:18:02 +0900417 case TETHERING_BLUETOOTH:
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800418 setBluetoothTethering(enable, receiver);
419 break;
420 default:
421 Log.w(TAG, "Invalid tether type.");
Erik Kline7a26ba32018-03-09 14:18:02 +0900422 sendTetherResult(receiver, TETHER_ERROR_UNKNOWN_IFACE);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800423 }
424 }
425
426 private void sendTetherResult(ResultReceiver receiver, int result) {
427 if (receiver != null) {
428 receiver.send(result, null);
429 }
430 }
431
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700432 private int setWifiTethering(final boolean enable) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900433 int rval = TETHER_ERROR_MASTER_ERROR;
Erik Kline256be782017-04-29 13:20:04 +0900434 final long ident = Binder.clearCallingIdentity();
435 try {
436 synchronized (mPublicSync) {
437 mWifiTetherRequested = enable;
438 final WifiManager mgr = getWifiManager();
439 if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) ||
440 (!enable && mgr.stopSoftAp())) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900441 rval = TETHER_ERROR_NO_ERROR;
Erik Kline256be782017-04-29 13:20:04 +0900442 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700443 }
Erik Kline256be782017-04-29 13:20:04 +0900444 } finally {
445 Binder.restoreCallingIdentity(ident);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700446 }
Erik Kline256be782017-04-29 13:20:04 +0900447 return rval;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700448 }
449
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800450 private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) {
451 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
452 if (adapter == null || !adapter.isEnabled()) {
453 Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " +
454 (adapter == null));
Erik Kline7a26ba32018-03-09 14:18:02 +0900455 sendTetherResult(receiver, TETHER_ERROR_SERVICE_UNAVAIL);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800456 return;
457 }
458
459 adapter.getProfileProxy(mContext, new ServiceListener() {
460 @Override
461 public void onServiceDisconnected(int profile) { }
462
463 @Override
464 public void onServiceConnected(int profile, BluetoothProfile proxy) {
465 ((BluetoothPan) proxy).setBluetoothTethering(enable);
466 // TODO: Enabling bluetooth tethering can fail asynchronously here.
467 // We should figure out a way to bubble up that failure instead of sending success.
Erik Kline7a26ba32018-03-09 14:18:02 +0900468 final int result = (((BluetoothPan) proxy).isTetheringOn() == enable)
469 ? TETHER_ERROR_NO_ERROR
470 : TETHER_ERROR_MASTER_ERROR;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800471 sendTetherResult(receiver, result);
markchienb6eb2c22018-07-18 14:29:20 +0800472 if (enable && mEntitlementMgr.isTetherProvisioningRequired()) {
473 mEntitlementMgr.scheduleProvisioningRechecks(TETHERING_BLUETOOTH);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800474 }
475 adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
476 }
477 }, BluetoothProfile.PAN);
478 }
479
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800480 /**
Erik Klinefdd41112017-01-12 17:02:51 +0900481 * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result
482 * is successful before firing back up to the wrapped receiver.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800483 *
484 * @param type The type of tethering being enabled.
485 * @param receiver A ResultReceiver which will be called back with an int resultCode.
486 * @return The proxy receiver.
487 */
488 private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) {
489 ResultReceiver rr = new ResultReceiver(null) {
490 @Override
491 protected void onReceiveResult(int resultCode, Bundle resultData) {
492 // If provisioning is successful, enable tethering, otherwise just send the error.
Erik Kline7a26ba32018-03-09 14:18:02 +0900493 if (resultCode == TETHER_ERROR_NO_ERROR) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800494 enableTetheringInternal(type, true, receiver);
495 } else {
496 sendTetherResult(receiver, resultCode);
497 }
markchienf2731272019-01-16 17:44:13 +0800498 mEntitlementMgr.updateEntitlementCacheValue(type, resultCode);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800499 }
500 };
501
502 // The following is necessary to avoid unmarshalling issues when sending the receiver
Erik Kline6ff17f72015-12-10 20:42:12 +0900503 // across processes.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800504 Parcel parcel = Parcel.obtain();
505 rr.writeToParcel(parcel,0);
506 parcel.setDataPosition(0);
507 ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
508 parcel.recycle();
509 return receiverForSending;
510 }
511
Robert Greenwalt5a735062010-03-02 17:25:02 -0800512 public int tether(String iface) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900513 return tether(iface, IpServer.STATE_TETHERED);
Erik Klineea9cc482017-03-10 19:35:34 +0900514 }
515
516 private int tether(String iface, int requestedState) {
Wink Savillec9acde92011-09-21 11:05:43 -0700517 if (DBG) Log.d(TAG, "Tethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700518 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700519 TetherState tetherState = mTetherStates.get(iface);
520 if (tetherState == null) {
Erik Kline00019f42016-06-30 19:31:46 +0900521 Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900522 return TETHER_ERROR_UNKNOWN_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700523 }
524 // Ignore the error status of the interface. If the interface is available,
525 // the errors are referring to past tethering attempts anyway.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900526 if (tetherState.lastState != IpServer.STATE_AVAILABLE) {
Erik Kline00019f42016-06-30 19:31:46 +0900527 Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900528 return TETHER_ERROR_UNAVAIL_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700529 }
Erik Klineea9cc482017-03-10 19:35:34 +0900530 // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's
531 // queue but not yet processed, this will be a no-op and it will not
532 // return an error.
533 //
534 // TODO: reexamine the threading and messaging model.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900535 tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_REQUESTED, requestedState);
Erik Kline7a26ba32018-03-09 14:18:02 +0900536 return TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800537 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800538 }
539
Robert Greenwalt5a735062010-03-02 17:25:02 -0800540 public int untether(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700541 if (DBG) Log.d(TAG, "Untethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700542 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700543 TetherState tetherState = mTetherStates.get(iface);
544 if (tetherState == null) {
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700545 Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900546 return TETHER_ERROR_UNKNOWN_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700547 }
Erik Klineea9cc482017-03-10 19:35:34 +0900548 if (!tetherState.isCurrentlyServing()) {
549 Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900550 return TETHER_ERROR_UNAVAIL_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700551 }
Erik Kline7a4ccc62018-08-27 17:26:47 +0900552 tetherState.ipServer.sendMessage(IpServer.CMD_TETHER_UNREQUESTED);
Erik Kline7a26ba32018-03-09 14:18:02 +0900553 return TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800554 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800555 }
556
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700557 public void untetherAll() {
Erik Kline7a26ba32018-03-09 14:18:02 +0900558 stopTethering(TETHERING_WIFI);
559 stopTethering(TETHERING_USB);
560 stopTethering(TETHERING_BLUETOOTH);
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700561 }
562
Robert Greenwalt5a735062010-03-02 17:25:02 -0800563 public int getLastTetherError(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700564 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700565 TetherState tetherState = mTetherStates.get(iface);
566 if (tetherState == null) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700567 Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
568 ", ignoring");
Erik Kline7a26ba32018-03-09 14:18:02 +0900569 return TETHER_ERROR_UNKNOWN_IFACE;
Robert Greenwaltb4453622011-11-03 16:01:40 -0700570 }
Hugo Benichib55fb222017-03-10 14:20:57 +0900571 return tetherState.lastError;
Robert Greenwalt5a735062010-03-02 17:25:02 -0800572 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800573 }
574
Erik Klineea9cc482017-03-10 19:35:34 +0900575 // TODO: Figure out how to update for local hotspot mode interfaces.
Christopher Wileyd985dde2016-05-31 10:44:35 -0700576 private void sendTetherStateChangedBroadcast() {
Erik Kline7a26ba32018-03-09 14:18:02 +0900577 if (!mDeps.isTetheringSupported()) return;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800578
Erik Kline8351faa2017-04-17 16:47:23 +0900579 final ArrayList<String> availableList = new ArrayList<>();
580 final ArrayList<String> tetherList = new ArrayList<>();
581 final ArrayList<String> localOnlyList = new ArrayList<>();
582 final ArrayList<String> erroredList = new ArrayList<>();
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800583
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800584 boolean wifiTethered = false;
585 boolean usbTethered = false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700586 boolean bluetoothTethered = false;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800587
Erik Kline3e756652017-01-17 13:42:19 +0900588 final TetheringConfiguration cfg = mConfig;
589
Robert Greenwaltb4453622011-11-03 16:01:40 -0700590 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700591 for (int i = 0; i < mTetherStates.size(); i++) {
592 TetherState tetherState = mTetherStates.valueAt(i);
593 String iface = mTetherStates.keyAt(i);
Erik Kline7a26ba32018-03-09 14:18:02 +0900594 if (tetherState.lastError != TETHER_ERROR_NO_ERROR) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700595 erroredList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900596 } else if (tetherState.lastState == IpServer.STATE_AVAILABLE) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700597 availableList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900598 } else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) {
Erik Kline8351faa2017-04-17 16:47:23 +0900599 localOnlyList.add(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +0900600 } else if (tetherState.lastState == IpServer.STATE_TETHERED) {
Erik Kline3e756652017-01-17 13:42:19 +0900601 if (cfg.isUsb(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700602 usbTethered = true;
Erik Kline3e756652017-01-17 13:42:19 +0900603 } else if (cfg.isWifi(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700604 wifiTethered = true;
Erik Kline3e756652017-01-17 13:42:19 +0900605 } else if (cfg.isBluetooth(iface)) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700606 bluetoothTethered = true;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800607 }
Erik Kline8351faa2017-04-17 16:47:23 +0900608 tetherList.add(iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800609 }
610 }
611 }
Erik Kline7a26ba32018-03-09 14:18:02 +0900612 final Intent bcast = new Intent(ACTION_TETHER_STATE_CHANGED);
Erik Kline8351faa2017-04-17 16:47:23 +0900613 bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Robert Greenwalt98c79e52011-07-28 11:51:11 -0700614 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Erik Kline7a26ba32018-03-09 14:18:02 +0900615 bcast.putStringArrayListExtra(EXTRA_AVAILABLE_TETHER, availableList);
616 bcast.putStringArrayListExtra(EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList);
617 bcast.putStringArrayListExtra(EXTRA_ACTIVE_TETHER, tetherList);
618 bcast.putStringArrayListExtra(EXTRA_ERRORED_TETHER, erroredList);
Erik Kline8351faa2017-04-17 16:47:23 +0900619 mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL);
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800620 if (DBG) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900621 Log.d(TAG, String.format(
Erik Kline8351faa2017-04-17 16:47:23 +0900622 "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]",
623 "avail", TextUtils.join(",", availableList),
624 "local_only", TextUtils.join(",", localOnlyList),
625 "tether", TextUtils.join(",", tetherList),
626 "error", TextUtils.join(",", erroredList)));
Robert Greenwalt924cc942010-06-28 10:26:19 -0700627 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800628
629 if (usbTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700630 if (wifiTethered || bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400631 showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800632 } else {
Chris Wren282cfef2017-03-27 15:01:44 -0400633 showTetheredNotification(SystemMessage.NOTE_TETHER_USB);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800634 }
635 } else if (wifiTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700636 if (bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400637 showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL);
Danica Chang6fdd0c62010-08-11 14:54:43 -0700638 } else {
Vinit Deshpande2576a802014-11-18 13:56:15 -0800639 /* We now have a status bar icon for WifiTethering, so drop the notification */
640 clearTetheredNotification();
Danica Chang6fdd0c62010-08-11 14:54:43 -0700641 }
642 } else if (bluetoothTethered) {
Chris Wren282cfef2017-03-27 15:01:44 -0400643 showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800644 } else {
645 clearTetheredNotification();
646 }
647 }
648
Chris Wren282cfef2017-03-27 15:01:44 -0400649 private void showTetheredNotification(int id) {
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100650 showTetheredNotification(id, true);
651 }
652
653 @VisibleForTesting
654 protected void showTetheredNotification(int id, boolean tetheringOn) {
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800655 NotificationManager notificationManager =
Erik Klinec438e302017-07-04 22:02:49 +0900656 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800657 if (notificationManager == null) {
658 return;
659 }
Chris Wren282cfef2017-03-27 15:01:44 -0400660 int icon = 0;
661 switch(id) {
662 case SystemMessage.NOTE_TETHER_USB:
663 icon = com.android.internal.R.drawable.stat_sys_tether_usb;
664 break;
665 case SystemMessage.NOTE_TETHER_BLUETOOTH:
666 icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth;
667 break;
668 case SystemMessage.NOTE_TETHER_GENERAL:
669 default:
670 icon = com.android.internal.R.drawable.stat_sys_tether_general;
671 break;
672 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800673
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400674 if (mLastNotificationId != 0) {
675 if (mLastNotificationId == icon) {
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700676 return;
677 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400678 notificationManager.cancelAsUser(null, mLastNotificationId,
Robert Greenwalt3cab6b02012-10-04 16:44:26 -0700679 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400680 mLastNotificationId = 0;
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700681 }
682
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800683 Intent intent = new Intent();
684 intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
685 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
686
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700687 PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
688 null, UserHandle.CURRENT);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800689
690 Resources r = Resources.getSystem();
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100691 final CharSequence title;
692 final CharSequence message;
693
694 if (tetheringOn) {
695 title = r.getText(com.android.internal.R.string.tethered_notification_title);
696 message = r.getText(com.android.internal.R.string.tethered_notification_message);
697 } else {
698 title = r.getText(com.android.internal.R.string.disable_tether_notification_title);
699 message = r.getText(com.android.internal.R.string.disable_tether_notification_message);
700 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800701
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400702 if (mTetheredNotificationBuilder == null) {
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500703 mTetheredNotificationBuilder =
704 new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400705 mTetheredNotificationBuilder.setWhen(0)
706 .setOngoing(true)
707 .setColor(mContext.getColor(
708 com.android.internal.R.color.system_notification_accent_color))
709 .setVisibility(Notification.VISIBILITY_PUBLIC)
710 .setCategory(Notification.CATEGORY_STATUS);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800711 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400712 mTetheredNotificationBuilder.setSmallIcon(icon)
713 .setContentTitle(title)
714 .setContentText(message)
715 .setContentIntent(pi);
Chris Wren282cfef2017-03-27 15:01:44 -0400716 mLastNotificationId = id;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800717
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400718 notificationManager.notifyAsUser(null, mLastNotificationId,
zhouzhijie130d4592017-05-18 10:02:59 +0800719 mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800720 }
721
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100722 @VisibleForTesting
723 protected void clearTetheredNotification() {
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800724 NotificationManager notificationManager =
Erik Klinec438e302017-07-04 22:02:49 +0900725 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400726 if (notificationManager != null && mLastNotificationId != 0) {
727 notificationManager.cancelAsUser(null, mLastNotificationId,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700728 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400729 mLastNotificationId = 0;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800730 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800731 }
732
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800733 private class StateReceiver extends BroadcastReceiver {
Nick Kralevich70c117a2014-05-27 15:30:02 -0700734 @Override
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800735 public void onReceive(Context content, Intent intent) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900736 final String action = intent.getAction();
737 if (action == null) return;
738
Mike Lockwood770126a2010-12-09 22:30:37 -0800739 if (action.equals(UsbManager.ACTION_USB_STATE)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900740 handleUsbAction(intent);
Erik Kline7a26ba32018-03-09 14:18:02 +0900741 } else if (action.equals(CONNECTIVITY_ACTION)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900742 handleConnectivityAction(intent);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700743 } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
Erik Kline2e88b5e2017-01-18 11:57:45 +0900744 handleWifiApAction(intent);
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700745 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
Erik Klinede637722017-10-12 22:16:01 +0900746 mLog.log("OBSERVED configuration changed");
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700747 updateConfiguration();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800748 }
749 }
Erik Kline2e88b5e2017-01-18 11:57:45 +0900750
751 private void handleConnectivityAction(Intent intent) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900752 final NetworkInfo networkInfo =
753 (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900754 if (networkInfo == null ||
755 networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) {
756 return;
757 }
758
759 if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString());
760 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
761 }
762
763 private void handleUsbAction(Intent intent) {
764 final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false);
Erik Klinec438e302017-07-04 22:02:49 +0900765 final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900766 final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false);
Erik Klinec438e302017-07-04 22:02:49 +0900767
768 mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s",
769 usbConnected, usbConfigured, rndisEnabled));
770
771 // There are three types of ACTION_USB_STATE:
772 //
773 // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0)
774 // Meaning: USB connection has ended either because of
775 // software reset or hard unplug.
776 //
777 // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0)
778 // Meaning: the first stage of USB protocol handshake has
779 // occurred but it is not complete.
780 //
781 // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1)
782 // Meaning: the USB handshake is completely done and all the
783 // functions are ready to use.
784 //
785 // For more explanation, see b/62552150 .
Erik Kline2e88b5e2017-01-18 11:57:45 +0900786 synchronized (Tethering.this.mPublicSync) {
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800787 if (!usbConnected && mRndisEnabled) {
788 // Turn off tethering if it was enabled and there is a disconnect.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900789 tetherMatchingInterfaces(IpServer.STATE_AVAILABLE, TETHERING_USB);
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800790 } else if (usbConfigured && rndisEnabled) {
791 // Tether if rndis is enabled and usb is configured.
Erik Kline7a4ccc62018-08-27 17:26:47 +0900792 tetherMatchingInterfaces(IpServer.STATE_TETHERED, TETHERING_USB);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900793 }
Jerry Zhang656a7bc2017-12-20 14:26:39 -0800794 mRndisEnabled = usbConfigured && rndisEnabled;
Erik Kline2e88b5e2017-01-18 11:57:45 +0900795 }
796 }
797
798 private void handleWifiApAction(Intent intent) {
Erik Kline2efb8272017-05-31 15:53:53 +0900799 final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED);
800 final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME);
801 final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED);
802
Erik Kline2e88b5e2017-01-18 11:57:45 +0900803 synchronized (Tethering.this.mPublicSync) {
804 switch (curState) {
805 case WifiManager.WIFI_AP_STATE_ENABLING:
806 // We can see this state on the way to both enabled and failure states.
807 break;
808 case WifiManager.WIFI_AP_STATE_ENABLED:
Erik Kline2efb8272017-05-31 15:53:53 +0900809 enableWifiIpServingLocked(ifname, ipmode);
Erik Kline2e88b5e2017-01-18 11:57:45 +0900810 break;
811 case WifiManager.WIFI_AP_STATE_DISABLED:
812 case WifiManager.WIFI_AP_STATE_DISABLING:
813 case WifiManager.WIFI_AP_STATE_FAILED:
814 default:
Erik Kline562e0c12017-06-09 16:36:29 +0900815 disableWifiIpServingLocked(ifname, curState);
Erik Kline2efb8272017-05-31 15:53:53 +0900816 break;
Erik Kline2e88b5e2017-01-18 11:57:45 +0900817 }
818 }
819 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800820 }
821
Alexandru-Andrei Rotarufa6d5c52017-07-18 16:49:22 +0100822 @VisibleForTesting
823 protected static class TetheringUserRestrictionListener implements UserRestrictionsListener {
824 private final Tethering mWrapper;
825
826 public TetheringUserRestrictionListener(Tethering wrapper) {
827 mWrapper = wrapper;
828 }
829
830 public void onUserRestrictionsChanged(int userId,
831 Bundle newRestrictions,
832 Bundle prevRestrictions) {
833 final boolean newlyDisallowed =
834 newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING);
835 final boolean previouslyDisallowed =
836 prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING);
837 final boolean tetheringDisallowedChanged = (newlyDisallowed != previouslyDisallowed);
838
839 if (!tetheringDisallowedChanged) {
840 return;
841 }
842
843 mWrapper.clearTetheredNotification();
844 final boolean isTetheringActiveOnDevice = (mWrapper.getTetheredIfaces().length != 0);
845
846 if (newlyDisallowed && isTetheringActiveOnDevice) {
847 mWrapper.showTetheredNotification(
848 com.android.internal.R.drawable.stat_sys_tether_general, false);
849 mWrapper.untetherAll();
850 }
851 }
852 }
853
Erik Kline562e0c12017-06-09 16:36:29 +0900854 private void disableWifiIpServingLocked(String ifname, int apState) {
855 mLog.log("Canceling WiFi tethering request - AP_STATE=" + apState);
Erik Kline2efb8272017-05-31 15:53:53 +0900856
Erik Kline562e0c12017-06-09 16:36:29 +0900857 // Regardless of whether we requested this transition, the AP has gone
858 // down. Don't try to tether again unless we're requested to do so.
859 // TODO: Remove this altogether, once Wi-Fi reliably gives us an
860 // interface name with every broadcast.
861 mWifiTetherRequested = false;
862
863 if (!TextUtils.isEmpty(ifname)) {
864 final TetherState ts = mTetherStates.get(ifname);
865 if (ts != null) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900866 ts.ipServer.unwanted();
Erik Kline562e0c12017-06-09 16:36:29 +0900867 return;
868 }
869 }
870
Erik Kline2efb8272017-05-31 15:53:53 +0900871 for (int i = 0; i < mTetherStates.size(); i++) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900872 final IpServer ipServer = mTetherStates.valueAt(i).ipServer;
873 if (ipServer.interfaceType() == TETHERING_WIFI) {
874 ipServer.unwanted();
Erik Kline562e0c12017-06-09 16:36:29 +0900875 return;
Erik Kline2efb8272017-05-31 15:53:53 +0900876 }
877 }
Erik Kline562e0c12017-06-09 16:36:29 +0900878
879 mLog.log("Error disabling Wi-Fi IP serving; " +
880 (TextUtils.isEmpty(ifname) ? "no interface name specified"
881 : "specified interface: " + ifname));
Erik Kline2efb8272017-05-31 15:53:53 +0900882 }
883
884 private void enableWifiIpServingLocked(String ifname, int wifiIpMode) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900885 // Map wifiIpMode values to IpServer.Callback serving states, inferring
Erik Kline2efb8272017-05-31 15:53:53 +0900886 // from mWifiTetherRequested as a final "best guess".
887 final int ipServingMode;
888 switch (wifiIpMode) {
889 case IFACE_IP_MODE_TETHERED:
Erik Kline7a4ccc62018-08-27 17:26:47 +0900890 ipServingMode = IpServer.STATE_TETHERED;
Erik Kline2efb8272017-05-31 15:53:53 +0900891 break;
892 case IFACE_IP_MODE_LOCAL_ONLY:
Erik Kline7a4ccc62018-08-27 17:26:47 +0900893 ipServingMode = IpServer.STATE_LOCAL_ONLY;
Erik Kline2efb8272017-05-31 15:53:53 +0900894 break;
895 default:
Erik Kline9e225542017-06-08 17:48:48 +0900896 mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode);
897 return;
Erik Kline2efb8272017-05-31 15:53:53 +0900898 }
899
900 if (!TextUtils.isEmpty(ifname)) {
Erik Kline7a26ba32018-03-09 14:18:02 +0900901 maybeTrackNewInterfaceLocked(ifname, TETHERING_WIFI);
Erik Kline2efb8272017-05-31 15:53:53 +0900902 changeInterfaceState(ifname, ipServingMode);
903 } else {
Erik Kline9e225542017-06-08 17:48:48 +0900904 mLog.e(String.format(
905 "Cannot enable IP serving in mode %s on missing interface name",
906 ipServingMode));
Erik Kline2efb8272017-05-31 15:53:53 +0900907 }
908 }
909
Erik Klineea9cc482017-03-10 19:35:34 +0900910 // TODO: Consider renaming to something more accurate in its description.
911 // This method:
912 // - allows requesting either tethering or local hotspot serving states
913 // - handles both enabling and disabling serving states
914 // - only tethers the first matching interface in listInterfaces()
915 // order of a given type
916 private void tetherMatchingInterfaces(int requestedState, int interfaceType) {
917 if (VDBG) {
918 Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")");
919 }
Mike Lockwood3c2a2f62011-06-08 15:10:26 -0700920
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700921 String[] ifaces = null;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800922 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -0700923 ifaces = mNMService.listInterfaces();
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800924 } catch (Exception e) {
Mike Lockwood3c2a2f62011-06-08 15:10:26 -0700925 Log.e(TAG, "Error listing Interfaces", e);
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800926 return;
927 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700928 String chosenIface = null;
929 if (ifaces != null) {
930 for (String iface : ifaces) {
931 if (ifaceNameToType(iface) == interfaceType) {
932 chosenIface = iface;
933 break;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800934 }
935 }
936 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700937 if (chosenIface == null) {
938 Log.e(TAG, "could not find iface of type " + interfaceType);
939 return;
940 }
941
Erik Kline2efb8272017-05-31 15:53:53 +0900942 changeInterfaceState(chosenIface, requestedState);
943 }
944
945 private void changeInterfaceState(String ifname, int requestedState) {
Erik Klineea9cc482017-03-10 19:35:34 +0900946 final int result;
947 switch (requestedState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +0900948 case IpServer.STATE_UNAVAILABLE:
949 case IpServer.STATE_AVAILABLE:
Erik Kline2efb8272017-05-31 15:53:53 +0900950 result = untether(ifname);
Erik Klineea9cc482017-03-10 19:35:34 +0900951 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +0900952 case IpServer.STATE_TETHERED:
953 case IpServer.STATE_LOCAL_ONLY:
Erik Kline2efb8272017-05-31 15:53:53 +0900954 result = tether(ifname, requestedState);
Erik Klineea9cc482017-03-10 19:35:34 +0900955 break;
956 default:
957 Log.wtf(TAG, "Unknown interface state: " + requestedState);
958 return;
959 }
Erik Kline7a26ba32018-03-09 14:18:02 +0900960 if (result != TETHER_ERROR_NO_ERROR) {
Erik Kline2efb8272017-05-31 15:53:53 +0900961 Log.e(TAG, "unable start or stop tethering on iface " + ifname);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700962 return;
963 }
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800964 }
965
Erik Kline3e756652017-01-17 13:42:19 +0900966 public TetheringConfiguration getTetheringConfiguration() {
967 return mConfig;
968 }
969
Erik Klined781fba2017-01-23 13:01:58 +0900970 public boolean hasTetherableConfiguration() {
971 final TetheringConfiguration cfg = mConfig;
972 final boolean hasDownstreamConfiguration =
markchien1be8d8f2018-12-05 21:20:01 +0800973 (cfg.tetherableUsbRegexs.length != 0)
974 || (cfg.tetherableWifiRegexs.length != 0)
975 || (cfg.tetherableBluetoothRegexs.length != 0);
976 final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty()
977 || cfg.chooseUpstreamAutomatically;
Erik Klined781fba2017-01-23 13:01:58 +0900978
979 return hasDownstreamConfiguration && hasUpstreamConfiguration;
980 }
981
Erik Kline3e756652017-01-17 13:42:19 +0900982 // TODO - update callers to use getTetheringConfiguration(),
983 // which has only final members.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800984 public String[] getTetherableUsbRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +0900985 return copy(mConfig.tetherableUsbRegexs);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800986 }
987
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800988 public String[] getTetherableWifiRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +0900989 return copy(mConfig.tetherableWifiRegexs);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800990 }
991
Danica Chang6fdd0c62010-08-11 14:54:43 -0700992 public String[] getTetherableBluetoothRegexs() {
Erik Kline3e756652017-01-17 13:42:19 +0900993 return copy(mConfig.tetherableBluetoothRegexs);
Danica Chang6fdd0c62010-08-11 14:54:43 -0700994 }
995
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700996 public int setUsbTethering(boolean enable) {
Wink Savillec9acde92011-09-21 11:05:43 -0700997 if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
Erik Klinec438e302017-07-04 22:02:49 +0900998 UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700999 synchronized (mPublicSync) {
Jerry Zhang327b8092018-01-09 17:53:04 -08001000 usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_RNDIS
1001 : UsbManager.FUNCTION_NONE);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001002 }
Erik Kline7a26ba32018-03-09 14:18:02 +09001003 return TETHER_ERROR_NO_ERROR;
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001004 }
1005
Erik Kline1fdc2e22017-05-08 17:56:35 +09001006 // TODO review API - figure out how to delete these entirely.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001007 public String[] getTetheredIfaces() {
1008 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001009 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001010 for (int i = 0; i < mTetherStates.size(); i++) {
1011 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001012 if (tetherState.lastState == IpServer.STATE_TETHERED) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001013 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001014 }
1015 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001016 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001017 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001018 }
1019
1020 public String[] getTetherableIfaces() {
1021 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001022 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001023 for (int i = 0; i < mTetherStates.size(); i++) {
1024 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001025 if (tetherState.lastState == IpServer.STATE_AVAILABLE) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001026 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001027 }
1028 }
1029 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001030 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001031 }
1032
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001033 public String[] getTetheredDhcpRanges() {
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001034 // TODO: this is only valid for the old DHCP server. Latest search suggests it is only used
1035 // by WifiP2pServiceImpl to start dnsmasq: remove/deprecate after migrating callers.
1036 return mConfig.legacyDhcpRanges;
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001037 }
1038
Robert Greenwalt5a735062010-03-02 17:25:02 -08001039 public String[] getErroredIfaces() {
1040 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001041 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001042 for (int i = 0; i < mTetherStates.size(); i++) {
1043 TetherState tetherState = mTetherStates.valueAt(i);
Erik Kline7a26ba32018-03-09 14:18:02 +09001044 if (tetherState.lastError != TETHER_ERROR_NO_ERROR) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001045 list.add(mTetherStates.keyAt(i));
Robert Greenwalt5a735062010-03-02 17:25:02 -08001046 }
1047 }
1048 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001049 return list.toArray(new String[list.size()]);
Robert Greenwalt5a735062010-03-02 17:25:02 -08001050 }
1051
Erik Kline22108902017-07-06 16:40:06 +09001052 private void logMessage(State state, int what) {
1053 mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what)));
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001054 }
1055
Erik Klineea9cc482017-03-10 19:35:34 +09001056 private boolean upstreamWanted() {
1057 if (!mForwardedDownstreams.isEmpty()) return true;
1058
1059 synchronized (mPublicSync) {
Jerry Zhang656a7bc2017-12-20 14:26:39 -08001060 return mWifiTetherRequested;
Erik Klineea9cc482017-03-10 19:35:34 +09001061 }
1062 }
1063
Erik Kline00019f42016-06-30 19:31:46 +09001064 // Needed because the canonical source of upstream truth is just the
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001065 // upstream interface set, |mCurrentUpstreamIfaceSet|.
Erik Kline3a5278f2017-06-24 19:29:10 +09001066 private boolean pertainsToCurrentUpstream(NetworkState ns) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001067 if (ns != null && ns.linkProperties != null && mCurrentUpstreamIfaceSet != null) {
Erik Kline00019f42016-06-30 19:31:46 +09001068 for (String ifname : ns.linkProperties.getAllInterfaceNames()) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001069 if (mCurrentUpstreamIfaceSet.ifnames.contains(ifname)) {
Erik Kline00019f42016-06-30 19:31:46 +09001070 return true;
Erik Kline6ff17f72015-12-10 20:42:12 +09001071 }
1072 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001073 }
Erik Kline00019f42016-06-30 19:31:46 +09001074 return false;
Erik Kline6ff17f72015-12-10 20:42:12 +09001075 }
1076
Wink Saville64c42ca2011-04-18 14:55:10 -07001077 class TetherMasterSM extends StateMachine {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001078 private static final int BASE_MASTER = Protocol.BASE_TETHERING;
Erik Klineea9cc482017-03-10 19:35:34 +09001079 // an interface SM has requested Tethering/Local Hotspot
1080 static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1;
1081 // an interface SM has unrequested Tethering/Local Hotspot
1082 static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001083 // upstream connection change - do the right thing
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001084 static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001085 // we don't have a valid upstream conn, check again after a delay
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001086 static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4;
Erik Kline6ff17f72015-12-10 20:42:12 +09001087 // Events from NetworkCallbacks that we process on the master state
1088 // machine thread on behalf of the UpstreamNetworkMonitor.
Erik Kline00019f42016-06-30 19:31:46 +09001089 static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001090 // we treated the error and want now to clear it
1091 static final int CMD_CLEAR_ERROR = BASE_MASTER + 6;
Erik Kline6e9a1012017-06-06 19:24:21 +09001092 static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001093
Erik Kline32179ff2017-07-04 18:28:11 +09001094 private final State mInitialState;
1095 private final State mTetherModeAliveState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001096
Erik Kline32179ff2017-07-04 18:28:11 +09001097 private final State mSetIpForwardingEnabledErrorState;
1098 private final State mSetIpForwardingDisabledErrorState;
1099 private final State mStartTetheringErrorState;
1100 private final State mStopTetheringErrorState;
1101 private final State mSetDnsForwardersErrorState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001102
Christopher Wileyd985dde2016-05-31 10:44:35 -07001103 // This list is a little subtle. It contains all the interfaces that currently are
1104 // requesting tethering, regardless of whether these interfaces are still members of
1105 // mTetherStates. This allows us to maintain the following predicates:
1106 //
1107 // 1) mTetherStates contains the set of all currently existing, tetherable, link state up
1108 // interfaces.
1109 // 2) mNotifyList contains all state machines that may have outstanding tethering state
1110 // that needs to be torn down.
1111 //
1112 // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList
1113 // so that the garbage collector does not clean up the state machine before it has a chance
1114 // to tear itself down.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001115 private final ArrayList<IpServer> mNotifyList;
Erik Kline1eb8c692016-07-08 17:21:26 +09001116 private final IPv6TetheringCoordinator mIPv6TetheringCoordinator;
Erik Klineed962a82017-07-06 19:49:35 +09001117 private final OffloadWrapper mOffload;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001118
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001119 private static final int UPSTREAM_SETTLE_TIME_MS = 10000;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001120
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +09001121 TetherMasterSM(String name, Looper looper, TetheringDependencies deps) {
Robert Greenwaltdfadaea2010-03-11 15:03:08 -08001122 super(name, looper);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001123
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001124 mInitialState = new InitialState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001125 mTetherModeAliveState = new TetherModeAliveState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001126 mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001127 mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001128 mStartTetheringErrorState = new StartTetheringErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001129 mStopTetheringErrorState = new StopTetheringErrorState();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001130 mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
Erik Kline1fdc2e22017-05-08 17:56:35 +09001131
1132 addState(mInitialState);
1133 addState(mTetherModeAliveState);
1134 addState(mSetIpForwardingEnabledErrorState);
1135 addState(mSetIpForwardingDisabledErrorState);
1136 addState(mStartTetheringErrorState);
1137 addState(mStopTetheringErrorState);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001138 addState(mSetDnsForwardersErrorState);
1139
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001140 mNotifyList = new ArrayList<>();
Remi NGUYEN VAN5d0dc452018-03-15 11:57:14 +09001141 mIPv6TetheringCoordinator = deps.getIPv6TetheringCoordinator(mNotifyList, mLog);
Erik Klineed962a82017-07-06 19:49:35 +09001142 mOffload = new OffloadWrapper();
Erik Kline32179ff2017-07-04 18:28:11 +09001143
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001144 setInitialState(mInitialState);
1145 }
1146
Erik Kline1fdc2e22017-05-08 17:56:35 +09001147 class InitialState extends State {
1148 @Override
1149 public boolean processMessage(Message message) {
Erik Kline22108902017-07-06 16:40:06 +09001150 logMessage(this, message.what);
Erik Kline1fdc2e22017-05-08 17:56:35 +09001151 switch (message.what) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001152 case EVENT_IFACE_SERVING_STATE_ACTIVE: {
1153 final IpServer who = (IpServer) message.obj;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001154 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
1155 handleInterfaceServingStateActive(message.arg1, who);
1156 transitionTo(mTetherModeAliveState);
1157 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001158 }
1159 case EVENT_IFACE_SERVING_STATE_INACTIVE: {
1160 final IpServer who = (IpServer) message.obj;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001161 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
1162 handleInterfaceServingStateInactive(who);
1163 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001164 }
Erik Kline6e9a1012017-06-06 19:24:21 +09001165 case EVENT_IFACE_UPDATE_LINKPROPERTIES:
1166 // Silently ignore these for now.
1167 break;
Erik Kline1fdc2e22017-05-08 17:56:35 +09001168 default:
1169 return NOT_HANDLED;
1170 }
1171 return HANDLED;
1172 }
1173 }
1174
Erik Kline3a5278f2017-06-24 19:29:10 +09001175 protected boolean turnOnMasterTetherSettings() {
1176 final TetheringConfiguration cfg = mConfig;
1177 try {
1178 mNMService.setIpForwardingEnabled(true);
1179 } catch (Exception e) {
1180 mLog.e(e);
1181 transitionTo(mSetIpForwardingEnabledErrorState);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001182 return false;
1183 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001184 // TODO: Randomize DHCPv4 ranges, especially in hotspot mode.
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001185 // Legacy DHCP server is disabled if passed an empty ranges array
1186 final String[] dhcpRanges = cfg.enableLegacyDhcpServer
1187 ? cfg.legacyDhcpRanges
1188 : new String[0];
Erik Kline3a5278f2017-06-24 19:29:10 +09001189 try {
1190 // TODO: Find a more accurate method name (startDHCPv4()?).
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001191 mNMService.startTethering(dhcpRanges);
Erik Kline3a5278f2017-06-24 19:29:10 +09001192 } catch (Exception e) {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001193 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001194 mNMService.stopTethering();
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +09001195 mNMService.startTethering(dhcpRanges);
Erik Kline3a5278f2017-06-24 19:29:10 +09001196 } catch (Exception ee) {
1197 mLog.e(ee);
1198 transitionTo(mStartTetheringErrorState);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001199 return false;
1200 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001201 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001202 mLog.log("SET master tether settings: ON");
1203 return true;
1204 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001205
Erik Kline3a5278f2017-06-24 19:29:10 +09001206 protected boolean turnOffMasterTetherSettings() {
1207 try {
1208 mNMService.stopTethering();
1209 } catch (Exception e) {
1210 mLog.e(e);
1211 transitionTo(mStopTetheringErrorState);
1212 return false;
Erik Kline14f7faf2017-02-14 19:03:09 +09001213 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001214 try {
1215 mNMService.setIpForwardingEnabled(false);
1216 } catch (Exception e) {
1217 mLog.e(e);
1218 transitionTo(mSetIpForwardingDisabledErrorState);
1219 return false;
1220 }
1221 transitionTo(mInitialState);
1222 mLog.log("SET master tether settings: OFF");
1223 return true;
1224 }
Erik Kline14f7faf2017-02-14 19:03:09 +09001225
Erik Kline3a5278f2017-06-24 19:29:10 +09001226 protected void chooseUpstreamType(boolean tryCell) {
Erik Kline6ee73da2017-07-08 20:36:37 +09001227 // We rebuild configuration on ACTION_CONFIGURATION_CHANGED, but we
1228 // do not currently know how to watch for changes in DUN settings.
1229 maybeUpdateConfiguration();
Erik Kline1e2897d2017-06-09 17:08:52 +09001230
Erik Kline72302902018-06-14 17:36:40 +09001231 final TetheringConfiguration config = mConfig;
1232 final NetworkState ns = (config.chooseUpstreamAutomatically)
1233 ? mUpstreamNetworkMonitor.getCurrentPreferredUpstream()
1234 : mUpstreamNetworkMonitor.selectPreferredUpstreamType(
1235 config.preferredUpstreamIfaceTypes);
Erik Kline3a5278f2017-06-24 19:29:10 +09001236 if (ns == null) {
1237 if (tryCell) {
1238 mUpstreamNetworkMonitor.registerMobileNetworkRequest();
1239 // We think mobile should be coming up; don't set a retry.
1240 } else {
1241 sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
Erik Kline1e2897d2017-06-09 17:08:52 +09001242 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001243 }
1244 setUpstreamNetwork(ns);
markchien26299ed2019-02-27 14:56:11 +08001245 final Network newUpstream = (ns != null) ? ns.network : null;
1246 if (mTetherUpstream != newUpstream) {
1247 mTetherUpstream = newUpstream;
1248 mUpstreamNetworkMonitor.setCurrentUpstream(mTetherUpstream);
1249 reportUpstreamChanged(mTetherUpstream);
1250 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001251 }
1252
1253 protected void setUpstreamNetwork(NetworkState ns) {
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001254 InterfaceSet ifaces = null;
Erik Klinee8bdb402018-02-23 14:16:06 -08001255 if (ns != null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001256 // Find the interface with the default IPv4 route. It may be the
1257 // interface described by linkProperties, or one of the interfaces
1258 // stacked on top of it.
Erik Klinee8bdb402018-02-23 14:16:06 -08001259 mLog.i("Looking for default routes on: " + ns.linkProperties);
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001260 ifaces = TetheringInterfaceUtils.getTetheringInterfaces(ns);
1261 mLog.i("Found upstream interface(s): " + ifaces);
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001262 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001263
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001264 if (ifaces != null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001265 setDnsForwarders(ns.network, ns.linkProperties);
Erik Kline6ff17f72015-12-10 20:42:12 +09001266 }
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001267 notifyDownstreamsOfNewUpstreamIface(ifaces);
Erik Kline3a5278f2017-06-24 19:29:10 +09001268 if (ns != null && pertainsToCurrentUpstream(ns)) {
Erik Kline72302902018-06-14 17:36:40 +09001269 // If we already have NetworkState for this network update it immediately.
Erik Kline3a5278f2017-06-24 19:29:10 +09001270 handleNewUpstreamNetworkState(ns);
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001271 } else if (mCurrentUpstreamIfaceSet == null) {
1272 // There are no available upstream networks.
Erik Kline3a5278f2017-06-24 19:29:10 +09001273 handleNewUpstreamNetworkState(null);
1274 }
1275 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001276
Erik Kline3a5278f2017-06-24 19:29:10 +09001277 protected void setDnsForwarders(final Network network, final LinkProperties lp) {
1278 // TODO: Set v4 and/or v6 DNS per available connectivity.
1279 String[] dnsServers = mConfig.defaultIPv4DNS;
1280 final Collection<InetAddress> dnses = lp.getDnsServers();
1281 // TODO: Properly support the absence of DNS servers.
1282 if (dnses != null && !dnses.isEmpty()) {
1283 // TODO: remove this invocation of NetworkUtils.makeStrings().
1284 dnsServers = NetworkUtils.makeStrings(dnses);
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001285 }
Erik Kline3a5278f2017-06-24 19:29:10 +09001286 try {
1287 mNMService.setDnsForwarders(network, dnsServers);
1288 mLog.log(String.format(
1289 "SET DNS forwarders: network=%s dnsServers=%s",
1290 network, Arrays.toString(dnsServers)));
1291 } catch (Exception e) {
1292 // TODO: Investigate how this can fail and what exactly
1293 // happens if/when such failures occur.
1294 mLog.e("setting DNS forwarders failed, " + e);
1295 transitionTo(mSetDnsForwardersErrorState);
1296 }
1297 }
Erik Kline00019f42016-06-30 19:31:46 +09001298
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001299 protected void notifyDownstreamsOfNewUpstreamIface(InterfaceSet ifaces) {
1300 mCurrentUpstreamIfaceSet = ifaces;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001301 for (IpServer ipServer : mNotifyList) {
1302 ipServer.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, ifaces);
Erik Kline00019f42016-06-30 19:31:46 +09001303 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001304 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001305
Erik Kline3a5278f2017-06-24 19:29:10 +09001306 protected void handleNewUpstreamNetworkState(NetworkState ns) {
1307 mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns);
Erik Klineed962a82017-07-06 19:49:35 +09001308 mOffload.updateUpstreamNetworkState(ns);
Erik Kline3a5278f2017-06-24 19:29:10 +09001309 }
1310
Erik Kline7a4ccc62018-08-27 17:26:47 +09001311 private void handleInterfaceServingStateActive(int mode, IpServer who) {
Erik Klineea9cc482017-03-10 19:35:34 +09001312 if (mNotifyList.indexOf(who) < 0) {
1313 mNotifyList.add(who);
1314 mIPv6TetheringCoordinator.addActiveDownstream(who, mode);
1315 }
1316
Erik Kline7a4ccc62018-08-27 17:26:47 +09001317 if (mode == IpServer.STATE_TETHERED) {
Erik Klineed962a82017-07-06 19:49:35 +09001318 // No need to notify OffloadController just yet as there are no
1319 // "offload-able" prefixes to pass along. This will handled
1320 // when the TISM informs Tethering of its LinkProperties.
Erik Klineea9cc482017-03-10 19:35:34 +09001321 mForwardedDownstreams.add(who);
1322 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001323 mOffload.excludeDownstreamInterface(who.interfaceName());
Erik Klineea9cc482017-03-10 19:35:34 +09001324 mForwardedDownstreams.remove(who);
1325 }
Erik Kline216af6d2017-04-27 20:57:23 +09001326
1327 // If this is a Wi-Fi interface, notify WifiManager of the active serving state.
Erik Kline7a26ba32018-03-09 14:18:02 +09001328 if (who.interfaceType() == TETHERING_WIFI) {
Erik Kline216af6d2017-04-27 20:57:23 +09001329 final WifiManager mgr = getWifiManager();
1330 final String iface = who.interfaceName();
1331 switch (mode) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001332 case IpServer.STATE_TETHERED:
Erik Kline2efb8272017-05-31 15:53:53 +09001333 mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED);
Erik Kline216af6d2017-04-27 20:57:23 +09001334 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001335 case IpServer.STATE_LOCAL_ONLY:
Erik Kline2efb8272017-05-31 15:53:53 +09001336 mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY);
Erik Kline216af6d2017-04-27 20:57:23 +09001337 break;
1338 default:
1339 Log.wtf(TAG, "Unknown active serving mode: " + mode);
1340 break;
1341 }
1342 }
Erik Klineea9cc482017-03-10 19:35:34 +09001343 }
1344
Erik Kline7a4ccc62018-08-27 17:26:47 +09001345 private void handleInterfaceServingStateInactive(IpServer who) {
Erik Klineea9cc482017-03-10 19:35:34 +09001346 mNotifyList.remove(who);
1347 mIPv6TetheringCoordinator.removeActiveDownstream(who);
Erik Klineed962a82017-07-06 19:49:35 +09001348 mOffload.excludeDownstreamInterface(who.interfaceName());
Erik Klineea9cc482017-03-10 19:35:34 +09001349 mForwardedDownstreams.remove(who);
Erik Kline216af6d2017-04-27 20:57:23 +09001350
1351 // If this is a Wi-Fi interface, tell WifiManager of any errors.
Erik Kline7a26ba32018-03-09 14:18:02 +09001352 if (who.interfaceType() == TETHERING_WIFI) {
1353 if (who.lastError() != TETHER_ERROR_NO_ERROR) {
Erik Kline216af6d2017-04-27 20:57:23 +09001354 getWifiManager().updateInterfaceIpState(
Erik Kline2efb8272017-05-31 15:53:53 +09001355 who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR);
Erik Kline216af6d2017-04-27 20:57:23 +09001356 }
1357 }
Erik Klineea9cc482017-03-10 19:35:34 +09001358 }
1359
Erik Kline3a5278f2017-06-24 19:29:10 +09001360 private void handleUpstreamNetworkMonitorCallback(int arg1, Object o) {
Erik Kline32179ff2017-07-04 18:28:11 +09001361 if (arg1 == UpstreamNetworkMonitor.NOTIFY_LOCAL_PREFIXES) {
Erik Klineed962a82017-07-06 19:49:35 +09001362 mOffload.sendOffloadExemptPrefixes((Set<IpPrefix>) o);
Erik Kline3a5278f2017-06-24 19:29:10 +09001363 return;
1364 }
1365
1366 final NetworkState ns = (NetworkState) o;
1367
1368 if (ns == null || !pertainsToCurrentUpstream(ns)) {
1369 // TODO: In future, this is where upstream evaluation and selection
1370 // could be handled for notifications which include sufficient data.
1371 // For example, after CONNECTIVITY_ACTION listening is removed, here
1372 // is where we could observe a Wi-Fi network becoming available and
1373 // passing validation.
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001374 if (mCurrentUpstreamIfaceSet == null) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001375 // If we have no upstream interface, try to run through upstream
1376 // selection again. If, for example, IPv4 connectivity has shown up
1377 // after IPv6 (e.g., 464xlat became available) we want the chance to
1378 // notice and act accordingly.
1379 chooseUpstreamType(false);
1380 }
1381 return;
1382 }
1383
1384 switch (arg1) {
Erik Kline3a5278f2017-06-24 19:29:10 +09001385 case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES:
1386 handleNewUpstreamNetworkState(ns);
1387 break;
1388 case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES:
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001389 chooseUpstreamType(false);
Erik Kline3a5278f2017-06-24 19:29:10 +09001390 break;
1391 case UpstreamNetworkMonitor.EVENT_ON_LOST:
1392 // TODO: Re-evaluate possible upstreams. Currently upstream
1393 // reevaluation is triggered via received CONNECTIVITY_ACTION
1394 // broadcasts that result in being passed a
1395 // TetherMasterSM.CMD_UPSTREAM_CHANGED.
1396 handleNewUpstreamNetworkState(null);
1397 break;
1398 default:
1399 mLog.e("Unknown arg1 value: " + arg1);
1400 break;
1401 }
1402 }
1403
1404 class TetherModeAliveState extends State {
Erik Klineea9cc482017-03-10 19:35:34 +09001405 boolean mUpstreamWanted = false;
Erik Kline6ff17f72015-12-10 20:42:12 +09001406 boolean mTryCell = true;
Erik Klinee0cce212017-03-06 14:05:23 +09001407
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001408 @Override
1409 public void enter() {
Erik Kline1fdc2e22017-05-08 17:56:35 +09001410 // If turning on master tether settings fails, we have already
1411 // transitioned to an error state; exit early.
1412 if (!turnOnMasterTetherSettings()) {
1413 return;
1414 }
1415
markchiena6c72872018-11-13 18:34:56 +09001416 mUpstreamNetworkMonitor.startObserveAllNetworks();
Robert Greenwalt4f74d552011-12-19 16:59:31 -08001417
Erik Klinef4b6e342017-04-25 19:19:59 +09001418 // TODO: De-duplicate with updateUpstreamWanted() below.
Erik Klineea9cc482017-03-10 19:35:34 +09001419 if (upstreamWanted()) {
1420 mUpstreamWanted = true;
Erik Klineed962a82017-07-06 19:49:35 +09001421 mOffload.start();
Erik Klineea9cc482017-03-10 19:35:34 +09001422 chooseUpstreamType(true);
1423 mTryCell = false;
1424 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001425 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001426
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001427 @Override
1428 public void exit() {
Erik Klineed962a82017-07-06 19:49:35 +09001429 mOffload.stop();
Erik Kline6ff17f72015-12-10 20:42:12 +09001430 mUpstreamNetworkMonitor.stop();
Erik Kline22108902017-07-06 16:40:06 +09001431 notifyDownstreamsOfNewUpstreamIface(null);
Erik Klinefa37b2f2016-08-02 18:27:03 +09001432 handleNewUpstreamNetworkState(null);
markchien26299ed2019-02-27 14:56:11 +08001433 if (mTetherUpstream != null) {
1434 mTetherUpstream = null;
1435 reportUpstreamChanged(null);
1436 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001437 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001438
Erik Klineea9cc482017-03-10 19:35:34 +09001439 private boolean updateUpstreamWanted() {
1440 final boolean previousUpstreamWanted = mUpstreamWanted;
1441 mUpstreamWanted = upstreamWanted();
Erik Klinef4b6e342017-04-25 19:19:59 +09001442 if (mUpstreamWanted != previousUpstreamWanted) {
1443 if (mUpstreamWanted) {
Erik Klineed962a82017-07-06 19:49:35 +09001444 mOffload.start();
Erik Klinef4b6e342017-04-25 19:19:59 +09001445 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001446 mOffload.stop();
Erik Klinef4b6e342017-04-25 19:19:59 +09001447 }
1448 }
Erik Klineea9cc482017-03-10 19:35:34 +09001449 return previousUpstreamWanted;
1450 }
1451
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001452 @Override
1453 public boolean processMessage(Message message) {
Erik Kline22108902017-07-06 16:40:06 +09001454 logMessage(this, message.what);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001455 boolean retValue = true;
1456 switch (message.what) {
Erik Klineea9cc482017-03-10 19:35:34 +09001457 case EVENT_IFACE_SERVING_STATE_ACTIVE: {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001458 IpServer who = (IpServer) message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001459 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
Erik Klineea9cc482017-03-10 19:35:34 +09001460 handleInterfaceServingStateActive(message.arg1, who);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001461 who.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED,
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001462 mCurrentUpstreamIfaceSet);
Erik Klineea9cc482017-03-10 19:35:34 +09001463 // If there has been a change and an upstream is now
1464 // desired, kick off the selection process.
1465 final boolean previousUpstreamWanted = updateUpstreamWanted();
1466 if (!previousUpstreamWanted && mUpstreamWanted) {
1467 chooseUpstreamType(true);
1468 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001469 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001470 }
Erik Klineea9cc482017-03-10 19:35:34 +09001471 case EVENT_IFACE_SERVING_STATE_INACTIVE: {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001472 IpServer who = (IpServer) message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001473 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
Erik Klineea9cc482017-03-10 19:35:34 +09001474 handleInterfaceServingStateInactive(who);
1475
1476 if (mNotifyList.isEmpty()) {
Erik Kline47222fc2017-04-30 19:36:15 +09001477 // This transitions us out of TetherModeAliveState,
1478 // either to InitialState or an error state.
Erik Kline1fdc2e22017-05-08 17:56:35 +09001479 turnOffMasterTetherSettings();
1480 break;
1481 }
1482
1483 if (DBG) {
1484 Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
1485 " live requests:");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001486 for (IpServer o : mNotifyList) {
Erik Kline1fdc2e22017-05-08 17:56:35 +09001487 Log.d(TAG, " " + o);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001488 }
1489 }
Erik Klineea9cc482017-03-10 19:35:34 +09001490 // If there has been a change and an upstream is no
1491 // longer desired, release any mobile requests.
1492 final boolean previousUpstreamWanted = updateUpstreamWanted();
1493 if (previousUpstreamWanted && !mUpstreamWanted) {
1494 mUpstreamNetworkMonitor.releaseMobileNetworkRequest();
1495 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001496 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001497 }
Erik Kline6e9a1012017-06-06 19:24:21 +09001498 case EVENT_IFACE_UPDATE_LINKPROPERTIES: {
1499 final LinkProperties newLp = (LinkProperties) message.obj;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001500 if (message.arg1 == IpServer.STATE_TETHERED) {
Erik Klineed962a82017-07-06 19:49:35 +09001501 mOffload.updateDownstreamLinkProperties(newLp);
Erik Kline6e9a1012017-06-06 19:24:21 +09001502 } else {
Erik Klineed962a82017-07-06 19:49:35 +09001503 mOffload.excludeDownstreamInterface(newLp.getInterfaceName());
Erik Kline6e9a1012017-06-06 19:24:21 +09001504 }
1505 break;
1506 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001507 case CMD_UPSTREAM_CHANGED:
Erik Klineea9cc482017-03-10 19:35:34 +09001508 updateUpstreamWanted();
1509 if (!mUpstreamWanted) break;
1510
Erik Klinefb413432017-02-14 18:26:04 +09001511 // Need to try DUN immediately if Wi-Fi goes down.
1512 chooseUpstreamType(true);
1513 mTryCell = false;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001514 break;
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001515 case CMD_RETRY_UPSTREAM:
Erik Klineea9cc482017-03-10 19:35:34 +09001516 updateUpstreamWanted();
1517 if (!mUpstreamWanted) break;
1518
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001519 chooseUpstreamType(mTryCell);
1520 mTryCell = !mTryCell;
1521 break;
Erik Kline00019f42016-06-30 19:31:46 +09001522 case EVENT_UPSTREAM_CALLBACK: {
Erik Klineea9cc482017-03-10 19:35:34 +09001523 updateUpstreamWanted();
Erik Kline3a5278f2017-06-24 19:29:10 +09001524 if (mUpstreamWanted) {
1525 handleUpstreamNetworkMonitorCallback(message.arg1, message.obj);
Erik Kline6ff17f72015-12-10 20:42:12 +09001526 }
1527 break;
Erik Kline00019f42016-06-30 19:31:46 +09001528 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001529 default:
1530 retValue = false;
1531 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001532 }
1533 return retValue;
1534 }
1535 }
1536
Wink Saville64c42ca2011-04-18 14:55:10 -07001537 class ErrorState extends State {
Erik Kline8351faa2017-04-17 16:47:23 +09001538 private int mErrorNotification;
1539
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001540 @Override
1541 public boolean processMessage(Message message) {
1542 boolean retValue = true;
1543 switch (message.what) {
Erik Klineea9cc482017-03-10 19:35:34 +09001544 case EVENT_IFACE_SERVING_STATE_ACTIVE:
Erik Kline7a4ccc62018-08-27 17:26:47 +09001545 IpServer who = (IpServer) message.obj;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001546 who.sendMessage(mErrorNotification);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001547 break;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001548 case CMD_CLEAR_ERROR:
Erik Kline7a26ba32018-03-09 14:18:02 +09001549 mErrorNotification = TETHER_ERROR_NO_ERROR;
Yohei, Oshima977aad52016-12-08 13:39:20 +09001550 transitionTo(mInitialState);
1551 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001552 default:
1553 retValue = false;
1554 }
1555 return retValue;
1556 }
Erik Kline8351faa2017-04-17 16:47:23 +09001557
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001558 void notify(int msgType) {
1559 mErrorNotification = msgType;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001560 for (IpServer ipServer : mNotifyList) {
1561 ipServer.sendMessage(msgType);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001562 }
1563 }
1564
1565 }
Erik Kline8351faa2017-04-17 16:47:23 +09001566
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001567 class SetIpForwardingEnabledErrorState extends ErrorState {
1568 @Override
1569 public void enter() {
1570 Log.e(TAG, "Error in setIpForwardingEnabled");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001571 notify(IpServer.CMD_IP_FORWARDING_ENABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001572 }
1573 }
1574
1575 class SetIpForwardingDisabledErrorState extends ErrorState {
1576 @Override
1577 public void enter() {
1578 Log.e(TAG, "Error in setIpForwardingDisabled");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001579 notify(IpServer.CMD_IP_FORWARDING_DISABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001580 }
1581 }
1582
1583 class StartTetheringErrorState extends ErrorState {
1584 @Override
1585 public void enter() {
1586 Log.e(TAG, "Error in startTethering");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001587 notify(IpServer.CMD_START_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001588 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001589 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001590 } catch (Exception e) {}
1591 }
1592 }
1593
1594 class StopTetheringErrorState extends ErrorState {
1595 @Override
1596 public void enter() {
1597 Log.e(TAG, "Error in stopTethering");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001598 notify(IpServer.CMD_STOP_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001599 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001600 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001601 } catch (Exception e) {}
1602 }
1603 }
1604
1605 class SetDnsForwardersErrorState extends ErrorState {
1606 @Override
1607 public void enter() {
1608 Log.e(TAG, "Error in setDnsForwarders");
Erik Kline7a4ccc62018-08-27 17:26:47 +09001609 notify(IpServer.CMD_SET_DNS_FORWARDERS_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001610 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001611 mNMService.stopTethering();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001612 } catch (Exception e) {}
1613 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001614 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001615 } catch (Exception e) {}
1616 }
1617 }
Erik Klineed962a82017-07-06 19:49:35 +09001618
1619 // A wrapper class to handle multiple situations where several calls to
1620 // the OffloadController need to happen together.
1621 //
1622 // TODO: This suggests that the interface between OffloadController and
1623 // Tethering is in need of improvement. Refactor these calls into the
1624 // OffloadController implementation.
1625 class OffloadWrapper {
1626 public void start() {
1627 mOffloadController.start();
1628 sendOffloadExemptPrefixes();
1629 }
1630
1631 public void stop() {
1632 mOffloadController.stop();
1633 }
1634
1635 public void updateUpstreamNetworkState(NetworkState ns) {
1636 mOffloadController.setUpstreamLinkProperties(
1637 (ns != null) ? ns.linkProperties : null);
1638 }
1639
1640 public void updateDownstreamLinkProperties(LinkProperties newLp) {
1641 // Update the list of offload-exempt prefixes before adding
1642 // new prefixes on downstream interfaces to the offload HAL.
1643 sendOffloadExemptPrefixes();
1644 mOffloadController.notifyDownstreamLinkProperties(newLp);
1645 }
1646
1647 public void excludeDownstreamInterface(String ifname) {
1648 // This and other interfaces may be in local-only hotspot mode;
1649 // resend all local prefixes to the OffloadController.
1650 sendOffloadExemptPrefixes();
1651 mOffloadController.removeDownstreamInterface(ifname);
1652 }
1653
1654 public void sendOffloadExemptPrefixes() {
1655 sendOffloadExemptPrefixes(mUpstreamNetworkMonitor.getLocalPrefixes());
1656 }
1657
1658 public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) {
1659 // Add in well-known minimum set.
1660 PrefixUtils.addNonForwardablePrefixes(localPrefixes);
1661 // Add tragically hardcoded prefixes.
1662 localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX);
1663
1664 // Maybe add prefixes or addresses for downstreams, depending on
1665 // the IP serving mode of each.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001666 for (IpServer ipServer : mNotifyList) {
1667 final LinkProperties lp = ipServer.linkProperties();
Erik Klineed962a82017-07-06 19:49:35 +09001668
Erik Kline7a4ccc62018-08-27 17:26:47 +09001669 switch (ipServer.servingMode()) {
1670 case IpServer.STATE_UNAVAILABLE:
1671 case IpServer.STATE_AVAILABLE:
Erik Klineed962a82017-07-06 19:49:35 +09001672 // No usable LinkProperties in these states.
1673 continue;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001674 case IpServer.STATE_TETHERED:
Erik Klineed962a82017-07-06 19:49:35 +09001675 // Only add IPv4 /32 and IPv6 /128 prefixes. The
1676 // directly-connected prefixes will be sent as
1677 // downstream "offload-able" prefixes.
1678 for (LinkAddress addr : lp.getAllLinkAddresses()) {
1679 final InetAddress ip = addr.getAddress();
1680 if (ip.isLinkLocalAddress()) continue;
1681 localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip));
1682 }
1683 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001684 case IpServer.STATE_LOCAL_ONLY:
Erik Klineed962a82017-07-06 19:49:35 +09001685 // Add prefixes covering all local IPs.
1686 localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp));
1687 break;
1688 }
1689 }
1690
1691 mOffloadController.setLocalPrefixes(localPrefixes);
1692 }
1693 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001694 }
1695
markchiena6c72872018-11-13 18:34:56 +09001696 public void systemReady() {
1697 mUpstreamNetworkMonitor.startTrackDefaultNetwork(mDeps.getDefaultNetworkRequest());
1698 }
1699
markchienf2731272019-01-16 17:44:13 +08001700 /** Get the latest value of the tethering entitlement check. */
markchien9554abf2019-03-06 16:25:00 +08001701 public void getLatestTetheringEntitlementResult(int type, ResultReceiver receiver,
markchienf2731272019-01-16 17:44:13 +08001702 boolean showEntitlementUi) {
1703 if (receiver != null) {
markchien9554abf2019-03-06 16:25:00 +08001704 mEntitlementMgr.getLatestTetheringEntitlementResult(type, receiver, showEntitlementUi);
markchienf2731272019-01-16 17:44:13 +08001705 }
1706 }
1707
markchien26299ed2019-02-27 14:56:11 +08001708 /** Register tethering event callback */
1709 public void registerTetheringEventCallback(ITetheringEventCallback callback) {
1710 mHandler.post(() -> {
1711 try {
1712 callback.onUpstreamChanged(mTetherUpstream);
1713 } catch (RemoteException e) {
1714 // Not really very much to do here.
1715 }
1716 mTetheringEventCallbacks.register(callback);
1717 });
1718 }
1719
1720 /** Unregister tethering event callback */
1721 public void unregisterTetheringEventCallback(ITetheringEventCallback callback) {
1722 mHandler.post(() -> {
1723 mTetheringEventCallbacks.unregister(callback);
1724 });
1725 }
1726
1727 private void reportUpstreamChanged(Network network) {
1728 final int length = mTetheringEventCallbacks.beginBroadcast();
1729 try {
1730 for (int i = 0; i < length; i++) {
1731 try {
1732 mTetheringEventCallbacks.getBroadcastItem(i).onUpstreamChanged(network);
1733 } catch (RemoteException e) {
1734 // Not really very much to do here.
1735 }
1736 }
1737 } finally {
1738 mTetheringEventCallbacks.finishBroadcast();
1739 }
1740 }
1741
Christopher Wiley499a57a2016-05-16 16:19:07 -07001742 @Override
Lorenzo Colittie3805462015-06-03 11:18:24 +09001743 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Christopher Wiley499a57a2016-05-16 16:19:07 -07001744 // Binder.java closes the resource for us.
1745 @SuppressWarnings("resource")
Lorenzo Colittie3805462015-06-03 11:18:24 +09001746 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001747 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001748
Lorenzo Colittie3805462015-06-03 11:18:24 +09001749 pw.println("Tethering:");
1750 pw.increaseIndent();
Lorenzo Colittie3805462015-06-03 11:18:24 +09001751
Erik Kline9db1b542017-03-16 14:10:27 +09001752 pw.println("Configuration:");
1753 pw.increaseIndent();
1754 final TetheringConfiguration cfg = mConfig;
1755 cfg.dump(pw);
1756 pw.decreaseIndent();
1757
1758 synchronized (mPublicSync) {
Robert Greenwaltb4453622011-11-03 16:01:40 -07001759 pw.println("Tether state:");
Lorenzo Colittie3805462015-06-03 11:18:24 +09001760 pw.increaseIndent();
Christopher Wileyd985dde2016-05-31 10:44:35 -07001761 for (int i = 0; i < mTetherStates.size(); i++) {
1762 final String iface = mTetherStates.keyAt(i);
1763 final TetherState tetherState = mTetherStates.valueAt(i);
1764 pw.print(iface + " - ");
1765
Hugo Benichib55fb222017-03-10 14:20:57 +09001766 switch (tetherState.lastState) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001767 case IpServer.STATE_UNAVAILABLE:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001768 pw.print("UnavailableState");
1769 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001770 case IpServer.STATE_AVAILABLE:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001771 pw.print("AvailableState");
1772 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001773 case IpServer.STATE_TETHERED:
Christopher Wileyd985dde2016-05-31 10:44:35 -07001774 pw.print("TetheredState");
1775 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001776 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +09001777 pw.print("LocalHotspotState");
1778 break;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001779 default:
1780 pw.print("UnknownState");
1781 break;
1782 }
Hugo Benichib55fb222017-03-10 14:20:57 +09001783 pw.println(" - lastError = " + tetherState.lastError);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001784 }
Erik Klineea9cc482017-03-10 19:35:34 +09001785 pw.println("Upstream wanted: " + upstreamWanted());
Remi NGUYEN VAN6c02f992018-03-09 14:07:18 +09001786 pw.println("Current upstream interface(s): " + mCurrentUpstreamIfaceSet);
Lorenzo Colittie3805462015-06-03 11:18:24 +09001787 pw.decreaseIndent();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001788 }
Erik Kline1fdc2e22017-05-08 17:56:35 +09001789
Lorenzo Colittic2519c52017-07-13 01:48:26 +09001790 pw.println("Hardware offload:");
1791 pw.increaseIndent();
1792 mOffloadController.dump(pw);
1793 pw.decreaseIndent();
1794
Erik Kline1fdc2e22017-05-08 17:56:35 +09001795 pw.println("Log:");
1796 pw.increaseIndent();
Erik Kline7747fd42017-05-12 16:52:48 +09001797 if (argsContain(args, SHORT_ARG)) {
1798 pw.println("<log removed for brevity>");
1799 } else {
1800 mLog.dump(fd, pw, args);
1801 }
Erik Kline1fdc2e22017-05-08 17:56:35 +09001802 pw.decreaseIndent();
1803
Lorenzo Colittie3805462015-06-03 11:18:24 +09001804 pw.decreaseIndent();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001805 }
Christopher Wileye03fb442016-05-18 13:45:20 -07001806
Erik Kline7747fd42017-05-12 16:52:48 +09001807 private static boolean argsContain(String[] args, String target) {
1808 for (String arg : args) {
Erik Klineee363c42017-05-29 09:11:03 +09001809 if (target.equals(arg)) return true;
Erik Kline7747fd42017-05-12 16:52:48 +09001810 }
1811 return false;
1812 }
1813
Erik Kline7a4ccc62018-08-27 17:26:47 +09001814 private IpServer.Callback makeControlCallback() {
1815 return new IpServer.Callback() {
Erik Kline6e9a1012017-06-06 19:24:21 +09001816 @Override
Erik Kline7a4ccc62018-08-27 17:26:47 +09001817 public void updateInterfaceState(IpServer who, int state, int lastError) {
1818 notifyInterfaceStateChange(who, state, lastError);
Erik Kline6e9a1012017-06-06 19:24:21 +09001819 }
1820
1821 @Override
Erik Kline7a4ccc62018-08-27 17:26:47 +09001822 public void updateLinkProperties(IpServer who, LinkProperties newLp) {
1823 notifyLinkPropertiesChanged(who, newLp);
Erik Kline6e9a1012017-06-06 19:24:21 +09001824 }
1825 };
1826 }
1827
1828 // TODO: Move into TetherMasterSM.
Erik Kline7a4ccc62018-08-27 17:26:47 +09001829 private void notifyInterfaceStateChange(IpServer who, int state, int error) {
1830 final String iface = who.interfaceName();
Christopher Wileyd985dde2016-05-31 10:44:35 -07001831 synchronized (mPublicSync) {
Erik Kline216af6d2017-04-27 20:57:23 +09001832 final TetherState tetherState = mTetherStates.get(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001833 if (tetherState != null && tetherState.ipServer.equals(who)) {
Hugo Benichib55fb222017-03-10 14:20:57 +09001834 tetherState.lastState = state;
1835 tetherState.lastError = error;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001836 } else {
1837 if (DBG) Log.d(TAG, "got notification from stale iface " + iface);
1838 }
1839 }
1840
Erik Kline7747fd42017-05-12 16:52:48 +09001841 mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error));
Christopher Wileyd985dde2016-05-31 10:44:35 -07001842
Christopher Wileydeebfec2016-09-16 11:14:36 -07001843 try {
1844 // Notify that we're tethering (or not) this interface.
1845 // This is how data saver for instance knows if the user explicitly
1846 // turned on tethering (thus keeping us from being in data saver mode).
Erik Kline7a4ccc62018-08-27 17:26:47 +09001847 mPolicyManager.onTetheringChanged(iface, state == IpServer.STATE_TETHERED);
Christopher Wileydeebfec2016-09-16 11:14:36 -07001848 } catch (RemoteException e) {
1849 // Not really very much we can do here.
1850 }
1851
Yohei, Oshima977aad52016-12-08 13:39:20 +09001852 // If TetherMasterSM is in ErrorState, TetherMasterSM stays there.
1853 // Thus we give a chance for TetherMasterSM to recover to InitialState
1854 // by sending CMD_CLEAR_ERROR
Erik Kline7a26ba32018-03-09 14:18:02 +09001855 if (error == TETHER_ERROR_MASTER_ERROR) {
Yohei, Oshima977aad52016-12-08 13:39:20 +09001856 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who);
1857 }
Erik Klineea9cc482017-03-10 19:35:34 +09001858 int which;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001859 switch (state) {
Erik Kline7a4ccc62018-08-27 17:26:47 +09001860 case IpServer.STATE_UNAVAILABLE:
1861 case IpServer.STATE_AVAILABLE:
Erik Klineea9cc482017-03-10 19:35:34 +09001862 which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001863 break;
Erik Kline7a4ccc62018-08-27 17:26:47 +09001864 case IpServer.STATE_TETHERED:
1865 case IpServer.STATE_LOCAL_ONLY:
Erik Klineea9cc482017-03-10 19:35:34 +09001866 which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001867 break;
Erik Klineea9cc482017-03-10 19:35:34 +09001868 default:
1869 Log.wtf(TAG, "Unknown interface state: " + state);
1870 return;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001871 }
Erik Klineea9cc482017-03-10 19:35:34 +09001872 mTetherMasterSM.sendMessage(which, state, 0, who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001873 sendTetherStateChangedBroadcast();
1874 }
1875
Erik Kline7a4ccc62018-08-27 17:26:47 +09001876 private void notifyLinkPropertiesChanged(IpServer who, LinkProperties newLp) {
1877 final String iface = who.interfaceName();
Erik Kline6e9a1012017-06-06 19:24:21 +09001878 final int state;
1879 synchronized (mPublicSync) {
1880 final TetherState tetherState = mTetherStates.get(iface);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001881 if (tetherState != null && tetherState.ipServer.equals(who)) {
Erik Kline6e9a1012017-06-06 19:24:21 +09001882 state = tetherState.lastState;
1883 } else {
1884 mLog.log("got notification from stale iface " + iface);
1885 return;
1886 }
1887 }
1888
Erik Kline7fd696c2017-06-12 18:20:08 +09001889 mLog.log(String.format(
1890 "OBSERVED LinkProperties update iface=%s state=%s lp=%s",
Erik Kline7a4ccc62018-08-27 17:26:47 +09001891 iface, IpServer.getStateString(state), newLp));
Erik Kline6e9a1012017-06-06 19:24:21 +09001892 final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES;
1893 mTetherMasterSM.sendMessage(which, state, 0, newLp);
1894 }
1895
Erik Kline4dd9bb82017-04-26 11:11:07 +09001896 private void maybeTrackNewInterfaceLocked(final String iface) {
1897 // If we don't care about this type of interface, ignore.
1898 final int interfaceType = ifaceNameToType(iface);
Erik Kline7a26ba32018-03-09 14:18:02 +09001899 if (interfaceType == TETHERING_INVALID) {
Erik Kline4dd9bb82017-04-26 11:11:07 +09001900 mLog.log(iface + " is not a tetherable iface, ignoring");
1901 return;
1902 }
Erik Klinea9cde8b2017-06-20 21:18:31 +09001903 maybeTrackNewInterfaceLocked(iface, interfaceType);
1904 }
Erik Kline4dd9bb82017-04-26 11:11:07 +09001905
Erik Klinea9cde8b2017-06-20 21:18:31 +09001906 private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) {
Erik Kline4dd9bb82017-04-26 11:11:07 +09001907 // If we have already started a TISM for this interface, skip.
1908 if (mTetherStates.containsKey(iface)) {
1909 mLog.log("active iface (" + iface + ") reported as added, ignoring");
1910 return;
1911 }
1912
1913 mLog.log("adding TetheringInterfaceStateMachine for: " + iface);
1914 final TetherState tetherState = new TetherState(
Erik Kline7a4ccc62018-08-27 17:26:47 +09001915 new IpServer(iface, mLooper, interfaceType, mLog, mNMService, mStatsService,
1916 makeControlCallback(), mConfig.enableLegacyDhcpServer,
Remi NGUYEN VAN5db454c2019-02-14 18:04:20 +09001917 mDeps.getIpServerDependencies()));
Christopher Wileyd985dde2016-05-31 10:44:35 -07001918 mTetherStates.put(iface, tetherState);
Erik Kline7a4ccc62018-08-27 17:26:47 +09001919 tetherState.ipServer.start();
Christopher Wileye03fb442016-05-18 13:45:20 -07001920 }
Erik Kline3e756652017-01-17 13:42:19 +09001921
Erik Kline4dd9bb82017-04-26 11:11:07 +09001922 private void stopTrackingInterfaceLocked(final String iface) {
1923 final TetherState tetherState = mTetherStates.get(iface);
1924 if (tetherState == null) {
1925 mLog.log("attempting to remove unknown iface (" + iface + "), ignoring");
1926 return;
1927 }
Erik Kline7a4ccc62018-08-27 17:26:47 +09001928 tetherState.ipServer.stop();
Erik Kline4dd9bb82017-04-26 11:11:07 +09001929 mLog.log("removing TetheringInterfaceStateMachine for: " + iface);
1930 mTetherStates.remove(iface);
1931 }
1932
Erik Kline3e756652017-01-17 13:42:19 +09001933 private static String[] copy(String[] strarray) {
1934 return Arrays.copyOf(strarray, strarray.length);
1935 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001936}