WifiServiceImpl.java revision 0753c5118317683fe5f4864e74b3ba9c015a6e4f
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.wifi; 18 19import static android.net.wifi.WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE; 20import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_FAILURE_REASON; 21import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; 22import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; 23import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; 24import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_GENERIC; 25import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL; 26import static android.net.wifi.WifiManager.SAP_START_FAILURE_NO_CHANNEL; 27import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; 28import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING; 29import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; 30 31import static com.android.server.wifi.LocalOnlyHotspotRequestInfo.HOTSPOT_NO_ERROR; 32import static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED; 33import static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED; 34import static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED; 35import static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED; 36import static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED; 37import static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED; 38import static com.android.server.wifi.WifiController.CMD_SCREEN_OFF; 39import static com.android.server.wifi.WifiController.CMD_SCREEN_ON; 40import static com.android.server.wifi.WifiController.CMD_SET_AP; 41import static com.android.server.wifi.WifiController.CMD_USER_PRESENT; 42import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED; 43 44import android.Manifest; 45import android.app.ActivityManager; 46import android.app.ActivityManager.RunningAppProcessInfo; 47import android.app.AppOpsManager; 48import android.bluetooth.BluetoothAdapter; 49import android.content.BroadcastReceiver; 50import android.content.Context; 51import android.content.Intent; 52import android.content.IntentFilter; 53import android.content.pm.ApplicationInfo; 54import android.content.pm.PackageManager; 55import android.content.pm.ParceledListSlice; 56import android.database.ContentObserver; 57import android.net.DhcpInfo; 58import android.net.DhcpResults; 59import android.net.IpConfiguration; 60import android.net.Network; 61import android.net.NetworkUtils; 62import android.net.StaticIpConfiguration; 63import android.net.Uri; 64import android.net.ip.IpManager; 65import android.net.wifi.IWifiManager; 66import android.net.wifi.ScanResult; 67import android.net.wifi.ScanSettings; 68import android.net.wifi.WifiActivityEnergyInfo; 69import android.net.wifi.WifiConfiguration; 70import android.net.wifi.WifiConnectionStatistics; 71import android.net.wifi.WifiInfo; 72import android.net.wifi.WifiLinkLayerStats; 73import android.net.wifi.WifiManager; 74import android.net.wifi.WifiManager.LocalOnlyHotspotCallback; 75import android.net.wifi.WifiScanner; 76import android.net.wifi.hotspot2.OsuProvider; 77import android.net.wifi.hotspot2.PasspointConfiguration; 78import android.os.AsyncTask; 79import android.os.BatteryStats; 80import android.os.Binder; 81import android.os.Build; 82import android.os.Bundle; 83import android.os.HandlerThread; 84import android.os.IBinder; 85import android.os.Looper; 86import android.os.Message; 87import android.os.Messenger; 88import android.os.PowerManager; 89import android.os.Process; 90import android.os.RemoteException; 91import android.os.ResultReceiver; 92import android.os.ShellCallback; 93import android.os.UserHandle; 94import android.os.UserManager; 95import android.os.WorkSource; 96import android.provider.Settings; 97import android.util.ArrayMap; 98import android.util.ArraySet; 99import android.util.Log; 100import android.util.Slog; 101 102import com.android.internal.annotations.GuardedBy; 103import com.android.internal.annotations.VisibleForTesting; 104import com.android.internal.telephony.IccCardConstants; 105import com.android.internal.telephony.PhoneConstants; 106import com.android.internal.telephony.TelephonyIntents; 107import com.android.internal.util.AsyncChannel; 108import com.android.server.wifi.hotspot2.PasspointProvider; 109import com.android.server.wifi.util.WifiHandler; 110import com.android.server.wifi.util.WifiPermissionsUtil; 111 112import java.io.BufferedReader; 113import java.io.FileDescriptor; 114import java.io.FileNotFoundException; 115import java.io.FileReader; 116import java.io.IOException; 117import java.io.PrintWriter; 118import java.net.Inet4Address; 119import java.net.InetAddress; 120import java.security.GeneralSecurityException; 121import java.security.KeyStore; 122import java.security.cert.CertPath; 123import java.security.cert.CertPathValidator; 124import java.security.cert.CertificateFactory; 125import java.security.cert.PKIXParameters; 126import java.security.cert.X509Certificate; 127import java.util.ArrayList; 128import java.util.Arrays; 129import java.util.HashMap; 130import java.util.List; 131import java.util.concurrent.ConcurrentHashMap; 132 133/** 134 * WifiService handles remote WiFi operation requests by implementing 135 * the IWifiManager interface. 136 * 137 * @hide 138 */ 139public class WifiServiceImpl extends IWifiManager.Stub { 140 private static final String TAG = "WifiService"; 141 private static final boolean DBG = true; 142 private static final boolean VDBG = false; 143 144 // Dumpsys argument to enable/disable disconnect on IP reachability failures. 145 private static final String DUMP_ARG_SET_IPREACH_DISCONNECT = "set-ipreach-disconnect"; 146 private static final String DUMP_ARG_SET_IPREACH_DISCONNECT_ENABLED = "enabled"; 147 private static final String DUMP_ARG_SET_IPREACH_DISCONNECT_DISABLED = "disabled"; 148 149 // Default scan background throttling interval if not overriden in settings 150 private static final long DEFAULT_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS = 30 * 60 * 1000; 151 152 // Apps with importance higher than this value is considered as background app. 153 private static final int BACKGROUND_IMPORTANCE_CUTOFF = 154 RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; 155 156 final WifiStateMachine mWifiStateMachine; 157 158 private final Context mContext; 159 private final FrameworkFacade mFacade; 160 private final Clock mClock; 161 private final ArraySet<String> mBackgroundThrottlePackageWhitelist = new ArraySet<>(); 162 163 private final PowerManager mPowerManager; 164 private final AppOpsManager mAppOps; 165 private final UserManager mUserManager; 166 private final ActivityManager mActivityManager; 167 private final WifiCountryCode mCountryCode; 168 private long mBackgroundThrottleInterval; 169 // Debug counter tracking scan requests sent by WifiManager 170 private int scanRequestCounter = 0; 171 172 /* Polls traffic stats and notifies clients */ 173 private WifiTrafficPoller mTrafficPoller; 174 /* Tracks the persisted states for wi-fi & airplane mode */ 175 final WifiSettingsStore mSettingsStore; 176 /* Logs connection events and some general router and scan stats */ 177 private final WifiMetrics mWifiMetrics; 178 /* Manages affiliated certificates for current user */ 179 private final WifiCertManager mCertManager; 180 181 private final WifiInjector mWifiInjector; 182 /* Backup/Restore Module */ 183 private final WifiBackupRestore mWifiBackupRestore; 184 185 // Map of package name of background scan apps and last scan timestamp. 186 private final ArrayMap<String, Long> mLastScanTimestamps; 187 188 private WifiScanner mWifiScanner; 189 private WifiLog mLog; 190 191 /** 192 * Asynchronous channel to WifiStateMachine 193 */ 194 private AsyncChannel mWifiStateMachineChannel; 195 196 private final boolean mPermissionReviewRequired; 197 private final FrameworkFacade mFrameworkFacade; 198 199 private WifiPermissionsUtil mWifiPermissionsUtil; 200 201 @GuardedBy("mLocalOnlyHotspotRequests") 202 private final HashMap<Integer, LocalOnlyHotspotRequestInfo> mLocalOnlyHotspotRequests; 203 @GuardedBy("mLocalOnlyHotspotRequests") 204 private WifiConfiguration mLocalOnlyHotspotConfig = null; 205 @GuardedBy("mLocalOnlyHotspotRequests") 206 private final ConcurrentHashMap<String, Integer> mIfaceIpModes; 207 208 /** 209 * Callback for use with LocalOnlyHotspot to unregister requesting applications upon death. 210 * 211 * @hide 212 */ 213 public final class LocalOnlyRequestorCallback 214 implements LocalOnlyHotspotRequestInfo.RequestingApplicationDeathCallback { 215 /** 216 * Called with requesting app has died. 217 */ 218 @Override 219 public void onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor) { 220 unregisterCallingAppAndStopLocalOnlyHotspot(requestor); 221 }; 222 } 223 224 /** 225 * Handles client connections 226 */ 227 private class ClientHandler extends WifiHandler { 228 229 ClientHandler(String tag, Looper looper) { 230 super(tag, looper); 231 } 232 233 @Override 234 public void handleMessage(Message msg) { 235 super.handleMessage(msg); 236 switch (msg.what) { 237 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 238 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 239 if (DBG) Slog.d(TAG, "New client listening to asynchronous messages"); 240 // We track the clients by the Messenger 241 // since it is expected to be always available 242 mTrafficPoller.addClient(msg.replyTo); 243 } else { 244 Slog.e(TAG, "Client connection failure, error=" + msg.arg1); 245 } 246 break; 247 } 248 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 249 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) { 250 if (DBG) Slog.d(TAG, "Send failed, client connection lost"); 251 } else { 252 if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1); 253 } 254 mTrafficPoller.removeClient(msg.replyTo); 255 break; 256 } 257 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { 258 AsyncChannel ac = mFrameworkFacade.makeWifiAsyncChannel(TAG); 259 ac.connect(mContext, this, msg.replyTo); 260 break; 261 } 262 case WifiManager.CONNECT_NETWORK: { 263 if (checkChangePermissionAndReplyIfNotAuthorized( 264 msg, WifiManager.CONNECT_NETWORK_FAILED)) { 265 WifiConfiguration config = (WifiConfiguration) msg.obj; 266 int networkId = msg.arg1; 267 Slog.d(TAG, "CONNECT " 268 + " nid=" + Integer.toString(networkId) 269 + " uid=" + msg.sendingUid 270 + " name=" 271 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 272 if (config != null) { 273 if (DBG) Slog.d(TAG, "Connect with config " + config); 274 /* Command is forwarded to state machine */ 275 mWifiStateMachine.sendMessage(Message.obtain(msg)); 276 } else if (config == null 277 && networkId != WifiConfiguration.INVALID_NETWORK_ID) { 278 if (DBG) Slog.d(TAG, "Connect with networkId " + networkId); 279 mWifiStateMachine.sendMessage(Message.obtain(msg)); 280 } else { 281 Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg); 282 replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED, 283 WifiManager.INVALID_ARGS); 284 } 285 } 286 break; 287 } 288 case WifiManager.SAVE_NETWORK: { 289 if (checkChangePermissionAndReplyIfNotAuthorized( 290 msg, WifiManager.SAVE_NETWORK_FAILED)) { 291 WifiConfiguration config = (WifiConfiguration) msg.obj; 292 int networkId = msg.arg1; 293 Slog.d(TAG, "SAVE" 294 + " nid=" + Integer.toString(networkId) 295 + " uid=" + msg.sendingUid 296 + " name=" 297 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 298 if (config != null) { 299 if (DBG) Slog.d(TAG, "Save network with config " + config); 300 /* Command is forwarded to state machine */ 301 mWifiStateMachine.sendMessage(Message.obtain(msg)); 302 } else { 303 Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg); 304 replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED, 305 WifiManager.INVALID_ARGS); 306 } 307 } 308 break; 309 } 310 case WifiManager.FORGET_NETWORK: 311 if (checkChangePermissionAndReplyIfNotAuthorized( 312 msg, WifiManager.FORGET_NETWORK_FAILED)) { 313 mWifiStateMachine.sendMessage(Message.obtain(msg)); 314 } 315 break; 316 case WifiManager.START_WPS: 317 if (checkChangePermissionAndReplyIfNotAuthorized(msg, WifiManager.WPS_FAILED)) { 318 mWifiStateMachine.sendMessage(Message.obtain(msg)); 319 } 320 break; 321 case WifiManager.CANCEL_WPS: 322 if (checkChangePermissionAndReplyIfNotAuthorized( 323 msg, WifiManager.CANCEL_WPS_FAILED)) { 324 mWifiStateMachine.sendMessage(Message.obtain(msg)); 325 } 326 break; 327 case WifiManager.DISABLE_NETWORK: 328 if (checkChangePermissionAndReplyIfNotAuthorized( 329 msg, WifiManager.DISABLE_NETWORK_FAILED)) { 330 mWifiStateMachine.sendMessage(Message.obtain(msg)); 331 } 332 break; 333 case WifiManager.RSSI_PKTCNT_FETCH: { 334 if (checkChangePermissionAndReplyIfNotAuthorized( 335 msg, WifiManager.RSSI_PKTCNT_FETCH_FAILED)) { 336 mWifiStateMachine.sendMessage(Message.obtain(msg)); 337 } 338 break; 339 } 340 default: { 341 Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg); 342 break; 343 } 344 } 345 } 346 347 /** 348 * Helper method to check if the sender of the message holds the 349 * {@link Manifest.permission#CHANGE_WIFI_STATE} permission, and reply with a failure if it 350 * doesn't 351 * 352 * @param msg Incoming message. 353 * @param replyWhat Param to be filled in the {@link Message#what} field of the failure 354 * reply. 355 * @return true if the sender holds the permission, false otherwise. 356 */ 357 private boolean checkChangePermissionAndReplyIfNotAuthorized(Message msg, int replyWhat) { 358 if (!mWifiPermissionsUtil.checkChangePermission(msg.sendingUid)) { 359 Slog.e(TAG, "ClientHandler.handleMessage ignoring unauthorized msg=" + msg); 360 replyFailed(msg, replyWhat, WifiManager.NOT_AUTHORIZED); 361 return false; 362 } 363 return true; 364 } 365 366 private void replyFailed(Message msg, int what, int why) { 367 if (msg.replyTo == null) return; 368 Message reply = Message.obtain(); 369 reply.what = what; 370 reply.arg1 = why; 371 try { 372 msg.replyTo.send(reply); 373 } catch (RemoteException e) { 374 // There's not much we can do if reply can't be sent! 375 } 376 } 377 } 378 private ClientHandler mClientHandler; 379 380 /** 381 * Handles interaction with WifiStateMachine 382 */ 383 private class WifiStateMachineHandler extends WifiHandler { 384 private AsyncChannel mWsmChannel; 385 386 WifiStateMachineHandler(String tag, Looper looper, AsyncChannel asyncChannel) { 387 super(tag, looper); 388 mWsmChannel = asyncChannel; 389 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 390 } 391 392 @Override 393 public void handleMessage(Message msg) { 394 super.handleMessage(msg); 395 switch (msg.what) { 396 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 397 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 398 mWifiStateMachineChannel = mWsmChannel; 399 } else { 400 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1); 401 mWifiStateMachineChannel = null; 402 } 403 break; 404 } 405 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 406 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1); 407 mWifiStateMachineChannel = null; 408 //Re-establish connection to state machine 409 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 410 break; 411 } 412 default: { 413 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg); 414 break; 415 } 416 } 417 } 418 } 419 420 WifiStateMachineHandler mWifiStateMachineHandler; 421 private WifiController mWifiController; 422 private final WifiLockManager mWifiLockManager; 423 private final WifiMulticastLockManager mWifiMulticastLockManager; 424 425 public WifiServiceImpl(Context context, WifiInjector wifiInjector, AsyncChannel asyncChannel) { 426 mContext = context; 427 mWifiInjector = wifiInjector; 428 mClock = wifiInjector.getClock(); 429 430 mFacade = mWifiInjector.getFrameworkFacade(); 431 mWifiMetrics = mWifiInjector.getWifiMetrics(); 432 mTrafficPoller = mWifiInjector.getWifiTrafficPoller(); 433 mUserManager = mWifiInjector.getUserManager(); 434 mCountryCode = mWifiInjector.getWifiCountryCode(); 435 mWifiStateMachine = mWifiInjector.getWifiStateMachine(); 436 mWifiStateMachine.enableRssiPolling(true); 437 mSettingsStore = mWifiInjector.getWifiSettingsStore(); 438 mPowerManager = mContext.getSystemService(PowerManager.class); 439 mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 440 mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); 441 mCertManager = mWifiInjector.getWifiCertManager(); 442 mWifiLockManager = mWifiInjector.getWifiLockManager(); 443 mWifiMulticastLockManager = mWifiInjector.getWifiMulticastLockManager(); 444 HandlerThread wifiServiceHandlerThread = mWifiInjector.getWifiServiceHandlerThread(); 445 mClientHandler = new ClientHandler(TAG, wifiServiceHandlerThread.getLooper()); 446 mWifiStateMachineHandler = new WifiStateMachineHandler(TAG, 447 wifiServiceHandlerThread.getLooper(), asyncChannel); 448 mWifiController = mWifiInjector.getWifiController(); 449 mWifiBackupRestore = mWifiInjector.getWifiBackupRestore(); 450 mPermissionReviewRequired = Build.PERMISSIONS_REVIEW_REQUIRED 451 || context.getResources().getBoolean( 452 com.android.internal.R.bool.config_permissionReviewRequired); 453 mWifiPermissionsUtil = mWifiInjector.getWifiPermissionsUtil(); 454 mLog = mWifiInjector.makeLog(TAG); 455 mFrameworkFacade = wifiInjector.getFrameworkFacade(); 456 mLastScanTimestamps = new ArrayMap<>(); 457 updateBackgroundThrottleInterval(); 458 updateBackgroundThrottlingWhitelist(); 459 mIfaceIpModes = new ConcurrentHashMap<>(); 460 mLocalOnlyHotspotRequests = new HashMap<>(); 461 enableVerboseLoggingInternal(getVerboseLoggingLevel()); 462 } 463 464 /** 465 * Provide a way for unit tests to set valid log object in the WifiHandler 466 * @param log WifiLog object to assign to the clientHandler 467 */ 468 @VisibleForTesting 469 public void setWifiHandlerLogForTest(WifiLog log) { 470 mClientHandler.setWifiLog(log); 471 } 472 473 /** 474 * Check if we are ready to start wifi. 475 * 476 * First check if we will be restarting system services to decrypt the device. If the device is 477 * not encrypted, check if Wi-Fi needs to be enabled and start if needed 478 * 479 * This function is used only at boot time. 480 */ 481 public void checkAndStartWifi() { 482 // First check if we will end up restarting WifiService 483 if (mFrameworkFacade.inStorageManagerCryptKeeperBounce()) { 484 Log.d(TAG, "Device still encrypted. Need to restart SystemServer. Do not start wifi."); 485 return; 486 } 487 488 // Check if wi-fi needs to be enabled 489 boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled(); 490 Slog.i(TAG, "WifiService starting up with Wi-Fi " + 491 (wifiEnabled ? "enabled" : "disabled")); 492 493 registerForScanModeChange(); 494 registerForBackgroundThrottleChanges(); 495 mContext.registerReceiver( 496 new BroadcastReceiver() { 497 @Override 498 public void onReceive(Context context, Intent intent) { 499 if (mSettingsStore.handleAirplaneModeToggled()) { 500 mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED); 501 } 502 if (mSettingsStore.isAirplaneModeOn()) { 503 Log.d(TAG, "resetting country code because Airplane mode is ON"); 504 mCountryCode.airplaneModeEnabled(); 505 } 506 } 507 }, 508 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); 509 510 mContext.registerReceiver( 511 new BroadcastReceiver() { 512 @Override 513 public void onReceive(Context context, Intent intent) { 514 String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE); 515 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(state)) { 516 Log.d(TAG, "resetting networks because SIM was removed"); 517 mWifiStateMachine.resetSimAuthNetworks(false); 518 Log.d(TAG, "resetting country code because SIM is removed"); 519 mCountryCode.simCardRemoved(); 520 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(state)) { 521 Log.d(TAG, "resetting networks because SIM was loaded"); 522 mWifiStateMachine.resetSimAuthNetworks(true); 523 } 524 } 525 }, 526 new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED)); 527 528 mContext.registerReceiver( 529 new BroadcastReceiver() { 530 @Override 531 public void onReceive(Context context, Intent intent) { 532 final int currState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, 533 WIFI_AP_STATE_DISABLED); 534 final int prevState = intent.getIntExtra(EXTRA_PREVIOUS_WIFI_AP_STATE, 535 WIFI_AP_STATE_DISABLED); 536 final int errorCode = intent.getIntExtra(EXTRA_WIFI_AP_FAILURE_REASON, 537 HOTSPOT_NO_ERROR); 538 final String ifaceName = 539 intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME); 540 final int mode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, 541 WifiManager.IFACE_IP_MODE_UNSPECIFIED); 542 handleWifiApStateChange(currState, prevState, errorCode, ifaceName, mode); 543 } 544 }, 545 new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)); 546 547 // Adding optimizations of only receiving broadcasts when wifi is enabled 548 // can result in race conditions when apps toggle wifi in the background 549 // without active user involvement. Always receive broadcasts. 550 registerForBroadcasts(); 551 registerForPackageOrUserRemoval(); 552 mInIdleMode = mPowerManager.isDeviceIdleMode(); 553 554 if (!mWifiStateMachine.syncInitialize(mWifiStateMachineChannel)) { 555 Log.wtf(TAG, "Failed to initialize WifiStateMachine"); 556 } 557 mWifiController.start(); 558 559 // If we are already disabled (could be due to airplane mode), avoid changing persist 560 // state here 561 if (wifiEnabled) { 562 try { 563 setWifiEnabled(mContext.getPackageName(), wifiEnabled); 564 } catch (RemoteException e) { 565 /* ignore - local call */ 566 } 567 } 568 } 569 570 public void handleUserSwitch(int userId) { 571 mWifiStateMachine.handleUserSwitch(userId); 572 } 573 574 public void handleUserUnlock(int userId) { 575 mWifiStateMachine.handleUserUnlock(userId); 576 } 577 578 public void handleUserStop(int userId) { 579 mWifiStateMachine.handleUserStop(userId); 580 } 581 582 /** 583 * see {@link android.net.wifi.WifiManager#startScan} 584 * and {@link android.net.wifi.WifiManager#startCustomizedScan} 585 * 586 * @param settings If null, use default parameter, i.e. full scan. 587 * @param workSource If null, all blame is given to the calling uid. 588 * @param packageName Package name of the app that requests wifi scan. 589 */ 590 @Override 591 public void startScan(ScanSettings settings, WorkSource workSource, String packageName) { 592 enforceChangePermission(); 593 594 mLog.trace("startScan uid=%").c(Binder.getCallingUid()).flush(); 595 // Check and throttle background apps for wifi scan. 596 if (isRequestFromBackground(packageName)) { 597 long lastScanMs = mLastScanTimestamps.getOrDefault(packageName, 0L); 598 long elapsedRealtime = mClock.getElapsedSinceBootMillis(); 599 600 if (lastScanMs != 0 && (elapsedRealtime - lastScanMs) < mBackgroundThrottleInterval) { 601 sendFailedScanBroadcast(); 602 return; 603 } 604 // Proceed with the scan request and record the time. 605 mLastScanTimestamps.put(packageName, elapsedRealtime); 606 } 607 synchronized (this) { 608 if (mWifiScanner == null) { 609 mWifiScanner = mWifiInjector.getWifiScanner(); 610 } 611 if (mInIdleMode) { 612 // Need to send an immediate scan result broadcast in case the 613 // caller is waiting for a result .. 614 615 // TODO: investigate if the logic to cancel scans when idle can move to 616 // WifiScanningServiceImpl. This will 1 - clean up WifiServiceImpl and 2 - 617 // avoid plumbing an awkward path to report a cancelled/failed scan. This will 618 // be sent directly until b/31398592 is fixed. 619 sendFailedScanBroadcast(); 620 mScanPending = true; 621 return; 622 } 623 } 624 if (settings != null) { 625 settings = new ScanSettings(settings); 626 if (!settings.isValid()) { 627 Slog.e(TAG, "invalid scan setting"); 628 return; 629 } 630 } 631 if (workSource != null) { 632 enforceWorkSourcePermission(); 633 // WifiManager currently doesn't use names, so need to clear names out of the 634 // supplied WorkSource to allow future WorkSource combining. 635 workSource.clearNames(); 636 } 637 if (workSource == null && Binder.getCallingUid() >= 0) { 638 workSource = new WorkSource(Binder.getCallingUid()); 639 } 640 mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++, 641 settings, workSource); 642 } 643 644 // Send a failed scan broadcast to indicate the current scan request failed. 645 private void sendFailedScanBroadcast() { 646 // clear calling identity to send broadcast 647 long callingIdentity = Binder.clearCallingIdentity(); 648 try { 649 Intent intent = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 650 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 651 intent.putExtra(WifiManager.EXTRA_RESULTS_UPDATED, false); 652 mContext.sendBroadcastAsUser(intent, UserHandle.ALL); 653 } finally { 654 // restore calling identity 655 Binder.restoreCallingIdentity(callingIdentity); 656 } 657 658 } 659 660 // Check if the request comes from background. 661 private boolean isRequestFromBackground(String packageName) { 662 // Requests from system or wifi are not background. 663 if (Binder.getCallingUid() == Process.SYSTEM_UID 664 || Binder.getCallingUid() == Process.WIFI_UID) { 665 return false; 666 } 667 mAppOps.checkPackage(Binder.getCallingUid(), packageName); 668 if (mBackgroundThrottlePackageWhitelist.contains(packageName)) { 669 return false; 670 } 671 672 // getPackageImportance requires PACKAGE_USAGE_STATS permission, so clearing the incoming 673 // identify so the permission check can be done on system process where wifi runs in. 674 long callingIdentity = Binder.clearCallingIdentity(); 675 try { 676 return mActivityManager.getPackageImportance(packageName) 677 > BACKGROUND_IMPORTANCE_CUTOFF; 678 } finally { 679 Binder.restoreCallingIdentity(callingIdentity); 680 } 681 } 682 683 @Override 684 public String getCurrentNetworkWpsNfcConfigurationToken() { 685 enforceConnectivityInternalPermission(); 686 mLog.trace("getCurrentNetworkWpsNfcConfigurationToken uid=%") 687 .c(Binder.getCallingUid()).flush(); 688 // TODO Add private logging for netId b/33807876 689 return mWifiStateMachine.syncGetCurrentNetworkWpsNfcConfigurationToken(); 690 } 691 692 boolean mInIdleMode; 693 boolean mScanPending; 694 695 void handleIdleModeChanged() { 696 boolean doScan = false; 697 synchronized (this) { 698 boolean idle = mPowerManager.isDeviceIdleMode(); 699 if (mInIdleMode != idle) { 700 mInIdleMode = idle; 701 if (!idle) { 702 if (mScanPending) { 703 mScanPending = false; 704 doScan = true; 705 } 706 } 707 } 708 } 709 if (doScan) { 710 // Someone requested a scan while we were idle; do a full scan now. 711 // The package name doesn't matter as the request comes from System UID. 712 startScan(null, null, ""); 713 } 714 } 715 716 private void enforceNetworkSettingsPermission() { 717 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, 718 "WifiService"); 719 } 720 721 private void enforceNetworkStackPermission() { 722 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_STACK, 723 "WifiService"); 724 } 725 726 private void enforceAccessPermission() { 727 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, 728 "WifiService"); 729 } 730 731 private void enforceChangePermission() { 732 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, 733 "WifiService"); 734 } 735 736 private void enforceLocationHardwarePermission() { 737 mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE, 738 "LocationHardware"); 739 } 740 741 private void enforceReadCredentialPermission() { 742 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL, 743 "WifiService"); 744 } 745 746 private void enforceWorkSourcePermission() { 747 mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS, 748 "WifiService"); 749 750 } 751 752 private void enforceMulticastChangePermission() { 753 mContext.enforceCallingOrSelfPermission( 754 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, 755 "WifiService"); 756 } 757 758 private void enforceConnectivityInternalPermission() { 759 mContext.enforceCallingOrSelfPermission( 760 android.Manifest.permission.CONNECTIVITY_INTERNAL, 761 "ConnectivityService"); 762 } 763 764 private void enforceLocationPermission(String pkgName, int uid) { 765 mWifiPermissionsUtil.enforceLocationPermission(pkgName, uid); 766 } 767 768 /** 769 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} 770 * @param enable {@code true} to enable, {@code false} to disable. 771 * @return {@code true} if the enable/disable operation was 772 * started or is already in the queue. 773 */ 774 @Override 775 public synchronized boolean setWifiEnabled(String packageName, boolean enable) 776 throws RemoteException { 777 enforceChangePermission(); 778 Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid() 779 + ", uid=" + Binder.getCallingUid() + ", package=" + packageName); 780 mLog.trace("setWifiEnabled package=% uid=% enable=%").c(packageName) 781 .c(Binder.getCallingUid()).c(enable).flush(); 782 783 boolean isFromSettings = 784 mWifiPermissionsUtil.checkNetworkSettingsPermission(Binder.getCallingUid()); 785 786 // If Airplane mode is enabled, only Settings is allowed to toggle Wifi 787 if (mSettingsStore.isAirplaneModeOn() && !isFromSettings) { 788 mLog.trace("setWifiEnabled in Airplane mode: only Settings can enable wifi").flush(); 789 return false; 790 } 791 792 // If SoftAp is enabled, only Settings is allowed to toggle wifi 793 boolean apEnabled = 794 mWifiStateMachine.syncGetWifiApState() != WifiManager.WIFI_AP_STATE_DISABLED; 795 796 if (apEnabled && !isFromSettings) { 797 mLog.trace("setWifiEnabled SoftAp not disabled: only Settings can enable wifi").flush(); 798 return false; 799 } 800 801 /* 802 * Caller might not have WRITE_SECURE_SETTINGS, 803 * only CHANGE_WIFI_STATE is enforced 804 */ 805 long ident = Binder.clearCallingIdentity(); 806 try { 807 if (! mSettingsStore.handleWifiToggled(enable)) { 808 // Nothing to do if wifi cannot be toggled 809 return true; 810 } 811 } finally { 812 Binder.restoreCallingIdentity(ident); 813 } 814 815 816 if (mPermissionReviewRequired) { 817 final int wiFiEnabledState = getWifiEnabledState(); 818 if (enable) { 819 if (wiFiEnabledState == WifiManager.WIFI_STATE_DISABLING 820 || wiFiEnabledState == WifiManager.WIFI_STATE_DISABLED) { 821 if (startConsentUi(packageName, Binder.getCallingUid(), 822 WifiManager.ACTION_REQUEST_ENABLE)) { 823 return true; 824 } 825 } 826 } else if (wiFiEnabledState == WifiManager.WIFI_STATE_ENABLING 827 || wiFiEnabledState == WifiManager.WIFI_STATE_ENABLED) { 828 if (startConsentUi(packageName, Binder.getCallingUid(), 829 WifiManager.ACTION_REQUEST_DISABLE)) { 830 return true; 831 } 832 } 833 } 834 835 mWifiController.sendMessage(CMD_WIFI_TOGGLED); 836 return true; 837 } 838 839 /** 840 * see {@link WifiManager#getWifiState()} 841 * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, 842 * {@link WifiManager#WIFI_STATE_DISABLING}, 843 * {@link WifiManager#WIFI_STATE_ENABLED}, 844 * {@link WifiManager#WIFI_STATE_ENABLING}, 845 * {@link WifiManager#WIFI_STATE_UNKNOWN} 846 */ 847 @Override 848 public int getWifiEnabledState() { 849 enforceAccessPermission(); 850 mLog.trace("getWifiEnabledState uid=%").c(Binder.getCallingUid()).flush(); 851 return mWifiStateMachine.syncGetWifiState(); 852 } 853 854 /** 855 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} 856 * @param wifiConfig SSID, security and channel details as 857 * part of WifiConfiguration 858 * @param enabled true to enable and false to disable 859 */ 860 @Override 861 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { 862 enforceChangePermission(); 863 mWifiPermissionsUtil.enforceTetherChangePermission(mContext); 864 865 mLog.trace("setWifiApEnabled uid=% enable=%").c(Binder.getCallingUid()).c(enabled).flush(); 866 867 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 868 throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user."); 869 } 870 // null wifiConfig is a meaningful input for CMD_SET_AP 871 if (wifiConfig == null || isValid(wifiConfig)) { 872 int mode = WifiManager.IFACE_IP_MODE_UNSPECIFIED; 873 SoftApModeConfiguration softApConfig = new SoftApModeConfiguration(mode, wifiConfig); 874 mWifiController.sendMessage(CMD_SET_AP, enabled ? 1 : 0, 0, softApConfig); 875 } else { 876 Slog.e(TAG, "Invalid WifiConfiguration"); 877 } 878 } 879 880 /** 881 * see {@link WifiManager#getWifiApState()} 882 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, 883 * {@link WifiManager#WIFI_AP_STATE_DISABLING}, 884 * {@link WifiManager#WIFI_AP_STATE_ENABLED}, 885 * {@link WifiManager#WIFI_AP_STATE_ENABLING}, 886 * {@link WifiManager#WIFI_AP_STATE_FAILED} 887 */ 888 @Override 889 public int getWifiApEnabledState() { 890 enforceAccessPermission(); 891 mLog.trace("getWifiApEnabledState uid=%").c(Binder.getCallingUid()).flush(); 892 return mWifiStateMachine.syncGetWifiApState(); 893 } 894 895 /** 896 * see {@link android.net.wifi.WifiManager#updateInterfaceIpState(String, int)} 897 * 898 * The possible modes include: {@link WifiManager#IFACE_IP_MODE_TETHERED}, 899 * {@link WifiManager#IFACE_IP_MODE_LOCAL_ONLY}, 900 * {@link WifiManager#IFACE_IP_MODE_CONFIGURATION_ERROR} 901 * 902 * @param ifaceName String name of the updated interface 903 * @param mode new operating mode of the interface 904 * 905 * @throws SecurityException if the caller does not have permission to call update 906 */ 907 @Override 908 public void updateInterfaceIpState(String ifaceName, int mode) { 909 // NETWORK_STACK is a signature only permission. 910 enforceNetworkStackPermission(); 911 912 // hand off the work to our handler thread 913 mClientHandler.post(() -> { 914 updateInterfaceIpStateInternal(ifaceName, mode); 915 }); 916 } 917 918 private void updateInterfaceIpStateInternal(String ifaceName, int mode) { 919 // update interface IP state related to tethering and hotspot 920 synchronized (mLocalOnlyHotspotRequests) { 921 // update the mode tracker here - we clear out state below 922 Integer previousMode = WifiManager.IFACE_IP_MODE_UNSPECIFIED; 923 if (ifaceName != null) { 924 previousMode = mIfaceIpModes.put(ifaceName, mode); 925 } 926 Slog.d(TAG, "updateInterfaceIpState: ifaceName=" + ifaceName + " mode=" + mode 927 + " previous mode= " + previousMode); 928 929 switch (mode) { 930 case WifiManager.IFACE_IP_MODE_LOCAL_ONLY: 931 // first make sure we have registered requests.. otherwise clean up 932 if (mLocalOnlyHotspotRequests.isEmpty()) { 933 // we don't have requests... stop the hotspot 934 stopSoftAp(); 935 updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); 936 return; 937 } 938 // LOHS is ready to go! Call our registered requestors! 939 sendHotspotStartedMessageToAllLOHSRequestInfoEntriesLocked(); 940 break; 941 case WifiManager.IFACE_IP_MODE_TETHERED: 942 // we have tethered an interface. we don't really act on this now other than if 943 // we have LOHS requests, and this is an issue. return incompatible mode for 944 // onFailed for the registered requestors since this can result from a race 945 // between a tether request and a hotspot request (tethering wins). 946 sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked( 947 LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE); 948 break; 949 case WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR: 950 // there was an error setting up the hotspot... trigger onFailed for the 951 // registered LOHS requestors 952 sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked( 953 LocalOnlyHotspotCallback.ERROR_GENERIC); 954 updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); 955 break; 956 case WifiManager.IFACE_IP_MODE_UNSPECIFIED: 957 if (ifaceName == null) { 958 // interface name is null, this is due to softap teardown. clear all 959 // entries for now. 960 // TODO: Deal with individual interfaces when we receive updates for them 961 mIfaceIpModes.clear(); 962 return; 963 } 964 break; 965 default: 966 mLog.trace("updateInterfaceIpStateInternal: unknown mode %").c(mode).flush(); 967 } 968 } 969 } 970 971 /** 972 * see {@link android.net.wifi.WifiManager#startSoftAp(WifiConfiguration)} 973 * @param wifiConfig SSID, security and channel details as part of WifiConfiguration 974 * @return {@code true} if softap start was triggered 975 * @throws SecurityException if the caller does not have permission to start softap 976 */ 977 @Override 978 public boolean startSoftAp(WifiConfiguration wifiConfig) { 979 // NETWORK_STACK is a signature only permission. 980 enforceNetworkStackPermission(); 981 982 mLog.trace("startSoftAp uid=%").c(Binder.getCallingUid()).flush(); 983 984 synchronized (mLocalOnlyHotspotRequests) { 985 // If a tethering request comes in while we have LOHS running (or requested), call stop 986 // for softap mode and restart softap with the tethering config. 987 if (!mLocalOnlyHotspotRequests.isEmpty()) { 988 stopSoftApInternal(); 989 } 990 991 return startSoftApInternal(wifiConfig, WifiManager.IFACE_IP_MODE_TETHERED); 992 } 993 } 994 995 /** 996 * Internal method to start softap mode. Callers of this method should have already checked 997 * proper permissions beyond the NetworkStack permission. 998 */ 999 private boolean startSoftApInternal(WifiConfiguration wifiConfig, int mode) { 1000 mLog.trace("startSoftApInternal uid=% mode=%") 1001 .c(Binder.getCallingUid()).c(mode).flush(); 1002 1003 // null wifiConfig is a meaningful input for CMD_SET_AP 1004 if (wifiConfig == null || isValid(wifiConfig)) { 1005 SoftApModeConfiguration softApConfig = new SoftApModeConfiguration(mode, wifiConfig); 1006 mWifiController.sendMessage(CMD_SET_AP, 1, 0, softApConfig); 1007 return true; 1008 } 1009 Slog.e(TAG, "Invalid WifiConfiguration"); 1010 return false; 1011 } 1012 1013 /** 1014 * see {@link android.net.wifi.WifiManager#stopSoftAp()} 1015 * @return {@code true} if softap stop was triggered 1016 * @throws SecurityException if the caller does not have permission to stop softap 1017 */ 1018 @Override 1019 public boolean stopSoftAp() { 1020 // NETWORK_STACK is a signature only permission. 1021 enforceNetworkStackPermission(); 1022 1023 // only permitted callers are allowed to this point - they must have gone through 1024 // connectivity service since this method is protected with the NETWORK_STACK PERMISSION 1025 1026 mLog.trace("stopSoftAp uid=%").c(Binder.getCallingUid()).flush(); 1027 1028 synchronized (mLocalOnlyHotspotRequests) { 1029 // If a tethering request comes in while we have LOHS running (or requested), call stop 1030 // for softap mode and restart softap with the tethering config. 1031 if (!mLocalOnlyHotspotRequests.isEmpty()) { 1032 mLog.trace("Call to stop Tethering while LOHS is active," 1033 + " Registered LOHS callers will be updated when softap stopped."); 1034 } 1035 1036 return stopSoftApInternal(); 1037 } 1038 } 1039 1040 /** 1041 * Internal method to stop softap mode. Callers of this method should have already checked 1042 * proper permissions beyond the NetworkStack permission. 1043 */ 1044 private boolean stopSoftApInternal() { 1045 mLog.trace("stopSoftApInternal uid=%").c(Binder.getCallingUid()).flush(); 1046 1047 mWifiController.sendMessage(CMD_SET_AP, 0, 0); 1048 return true; 1049 } 1050 1051 /** 1052 * Private method to handle SoftAp state changes 1053 */ 1054 private void handleWifiApStateChange( 1055 int currentState, int previousState, int errorCode, String ifaceName, int mode) { 1056 // The AP state update from WifiStateMachine for softap 1057 Slog.d(TAG, "handleWifiApStateChange: currentState=" + currentState 1058 + " previousState=" + previousState + " errorCode= " + errorCode 1059 + " ifaceName=" + ifaceName + " mode=" + mode); 1060 1061 // check if we have a failure - since it is possible (worst case scenario where 1062 // WifiController and WifiStateMachine are out of sync wrt modes) to get two FAILED 1063 // notifications in a row, we need to handle this first. 1064 if (currentState == WIFI_AP_STATE_FAILED) { 1065 // update registered LOHS callbacks if we see a failure 1066 synchronized (mLocalOnlyHotspotRequests) { 1067 int errorToReport = ERROR_GENERIC; 1068 if (errorCode == SAP_START_FAILURE_NO_CHANNEL) { 1069 errorToReport = ERROR_NO_CHANNEL; 1070 } 1071 // holding the required lock: send message to requestors and clear the list 1072 sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked( 1073 errorToReport); 1074 // also need to clear interface ip state - send null for now since we don't know 1075 // what interface (and we have one anyway) 1076 updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); 1077 } 1078 return; 1079 } 1080 1081 if (currentState == WIFI_AP_STATE_DISABLING || currentState == WIFI_AP_STATE_DISABLED) { 1082 // softap is shutting down or is down... let requestors know via the onStopped call 1083 synchronized (mLocalOnlyHotspotRequests) { 1084 // if we are currently in hotspot mode, then trigger onStopped for registered 1085 // requestors, otherwise something odd happened and we should clear state 1086 if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_LOCAL_ONLY)) { 1087 // holding the required lock: send message to requestors and clear the list 1088 sendHotspotStoppedMessageToAllLOHSRequestInfoEntriesLocked(); 1089 } else { 1090 // LOHS not active: report an error (still holding the required lock) 1091 sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(ERROR_GENERIC); 1092 } 1093 // also clear interface ip state - send null for now since we don't know what 1094 // interface (and we only have one anyway) 1095 updateInterfaceIpState(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); 1096 } 1097 return; 1098 } 1099 1100 // remaining states are enabling or enabled... those are not used for the callbacks 1101 } 1102 1103 /** 1104 * Helper method to send a HOTSPOT_FAILED message to all registered LocalOnlyHotspotRequest 1105 * callers and clear the registrations. 1106 * 1107 * Callers should already hold the mLocalOnlyHotspotRequests lock. 1108 */ 1109 private void sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(int arg1) { 1110 for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) { 1111 try { 1112 requestor.sendHotspotFailedMessage(arg1); 1113 requestor.unlinkDeathRecipient(); 1114 } catch (RemoteException e) { 1115 // This will be cleaned up by binder death handling 1116 } 1117 } 1118 1119 // Since all callers were notified, now clear the registrations. 1120 mLocalOnlyHotspotRequests.clear(); 1121 } 1122 1123 /** 1124 * Helper method to send a HOTSPOT_STOPPED message to all registered LocalOnlyHotspotRequest 1125 * callers and clear the registrations. 1126 * 1127 * Callers should already hold the mLocalOnlyHotspotRequests lock. 1128 */ 1129 private void sendHotspotStoppedMessageToAllLOHSRequestInfoEntriesLocked() { 1130 for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) { 1131 try { 1132 requestor.sendHotspotStoppedMessage(); 1133 requestor.unlinkDeathRecipient(); 1134 } catch (RemoteException e) { 1135 // This will be cleaned up by binder death handling 1136 } 1137 } 1138 1139 // Since all callers were notified, now clear the registrations. 1140 mLocalOnlyHotspotRequests.clear(); 1141 } 1142 1143 /** 1144 * Helper method to send a HOTSPOT_STARTED message to all registered LocalOnlyHotspotRequest 1145 * callers. 1146 * 1147 * Callers should already hold the mLocalOnlyHotspotRequests lock. 1148 */ 1149 private void sendHotspotStartedMessageToAllLOHSRequestInfoEntriesLocked() { 1150 for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) { 1151 try { 1152 requestor.sendHotspotStartedMessage(mLocalOnlyHotspotConfig); 1153 } catch (RemoteException e) { 1154 // This will be cleaned up by binder death handling 1155 } 1156 } 1157 } 1158 1159 /** 1160 * Temporary method used for testing while startLocalOnlyHotspot is not fully implemented. This 1161 * method allows unit tests to register callbacks directly for testing mechanisms triggered by 1162 * softap mode changes. 1163 */ 1164 @VisibleForTesting 1165 void registerLOHSForTest(int pid, LocalOnlyHotspotRequestInfo request) { 1166 mLocalOnlyHotspotRequests.put(pid, request); 1167 } 1168 1169 /** 1170 * Method to start LocalOnlyHotspot. In this method, permissions, settings and modes are 1171 * checked to verify that we can enter softapmode. This method returns 1172 * {@link LocalOnlyHotspotCallback#REQUEST_REGISTERED} if we will attempt to start, otherwise, 1173 * possible startup erros may include tethering being disallowed failure reason {@link 1174 * LocalOnlyHotspotCallback#ERROR_TETHERING_DISALLOWED} or an incompatible mode failure reason 1175 * {@link LocalOnlyHotspotCallback#ERROR_INCOMPATIBLE_MODE}. 1176 * 1177 * see {@link WifiManager#startLocalOnlyHotspot(LocalOnlyHotspotCallback)} 1178 * 1179 * @param messenger Messenger to send messages to the corresponding WifiManager. 1180 * @param binder IBinder instance to allow cleanup if the app dies 1181 * @param packageName String name of the calling package 1182 * 1183 * @return int return code for attempt to start LocalOnlyHotspot. 1184 * 1185 * @throws SecurityException if the caller does not have permission to start a Local Only 1186 * Hotspot. 1187 * @throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they 1188 * have an outstanding request. 1189 */ 1190 @Override 1191 public int startLocalOnlyHotspot(Messenger messenger, IBinder binder, String packageName) { 1192 // first check if the caller has permission to start a local only hotspot 1193 // need to check for WIFI_STATE_CHANGE and location permission 1194 final int uid = Binder.getCallingUid(); 1195 final int pid = Binder.getCallingPid(); 1196 1197 enforceChangePermission(); 1198 enforceLocationPermission(packageName, uid); 1199 // also need to verify that Locations services are enabled. 1200 if (mSettingsStore.getLocationModeSetting(mContext) == Settings.Secure.LOCATION_MODE_OFF) { 1201 throw new SecurityException("Location mode is not enabled."); 1202 } 1203 1204 // verify that tethering is not disabled 1205 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 1206 return LocalOnlyHotspotCallback.ERROR_TETHERING_DISALLOWED; 1207 } 1208 1209 // the app should be in the foreground 1210 try { 1211 if (!mFrameworkFacade.isAppForeground(uid)) { 1212 return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE; 1213 } 1214 } catch (RemoteException e) { 1215 mLog.trace("RemoteException during isAppForeground when calling startLOHS"); 1216 return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE; 1217 } 1218 1219 mLog.trace("startLocalOnlyHotspot uid=% pid=%").c(uid).c(pid).flush(); 1220 1221 synchronized (mLocalOnlyHotspotRequests) { 1222 // check if we are currently tethering 1223 if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_TETHERED)) { 1224 // Tethering is enabled, cannot start LocalOnlyHotspot 1225 mLog.trace("Cannot start localOnlyHotspot when WiFi Tethering is active."); 1226 return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE; 1227 } 1228 1229 // does this caller already have a request? 1230 LocalOnlyHotspotRequestInfo request = mLocalOnlyHotspotRequests.get(pid); 1231 if (request != null) { 1232 mLog.trace("caller already has an active request"); 1233 throw new IllegalStateException( 1234 "Caller already has an active LocalOnlyHotspot request"); 1235 } 1236 1237 // now create the new LOHS request info object 1238 request = new LocalOnlyHotspotRequestInfo(binder, messenger, 1239 new LocalOnlyRequestorCallback()); 1240 1241 // check current operating state and take action if needed 1242 if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_LOCAL_ONLY)) { 1243 // LOHS is already active, send out what is running 1244 try { 1245 mLog.trace("LOHS already up, trigger onStarted callback"); 1246 request.sendHotspotStartedMessage(mLocalOnlyHotspotConfig); 1247 } catch (RemoteException e) { 1248 return LocalOnlyHotspotCallback.ERROR_GENERIC; 1249 } 1250 } else if (mLocalOnlyHotspotRequests.isEmpty()) { 1251 // this is the first request, then set up our config and start LOHS 1252 mLocalOnlyHotspotConfig = 1253 WifiApConfigStore.generateLocalOnlyHotspotConfig(mContext); 1254 startSoftApInternal(mLocalOnlyHotspotConfig, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); 1255 } 1256 1257 mLocalOnlyHotspotRequests.put(pid, request); 1258 return LocalOnlyHotspotCallback.REQUEST_REGISTERED; 1259 } 1260 } 1261 1262 /** 1263 * see {@link WifiManager#stopLocalOnlyHotspot()} 1264 * 1265 * @throws SecurityException if the caller does not have permission to stop a Local Only 1266 * Hotspot. 1267 */ 1268 @Override 1269 public void stopLocalOnlyHotspot() { 1270 // first check if the caller has permission to stop a local only hotspot 1271 enforceChangePermission(); 1272 final int uid = Binder.getCallingUid(); 1273 final int pid = Binder.getCallingPid(); 1274 1275 mLog.trace("stopLocalOnlyHotspot uid=% pid=%").c(uid).c(pid).flush(); 1276 1277 synchronized (mLocalOnlyHotspotRequests) { 1278 // was the caller already registered? check request tracker - return false if not 1279 LocalOnlyHotspotRequestInfo requestInfo = mLocalOnlyHotspotRequests.get(pid); 1280 if (requestInfo == null) { 1281 return; 1282 } 1283 requestInfo.unlinkDeathRecipient(); 1284 unregisterCallingAppAndStopLocalOnlyHotspot(requestInfo); 1285 } // end synchronized 1286 } 1287 1288 /** 1289 * Helper method to unregister LocalOnlyHotspot requestors and stop the hotspot if needed. 1290 */ 1291 private void unregisterCallingAppAndStopLocalOnlyHotspot(LocalOnlyHotspotRequestInfo request) { 1292 mLog.trace("unregisterCallingAppAndStopLocalOnlyHotspot pid=%").c(request.getPid()).flush(); 1293 1294 synchronized (mLocalOnlyHotspotRequests) { 1295 if (mLocalOnlyHotspotRequests.remove(request.getPid()) == null) { 1296 mLog.trace("LocalOnlyHotspotRequestInfo not found to remove"); 1297 return; 1298 } 1299 1300 if (mLocalOnlyHotspotRequests.isEmpty()) { 1301 mLocalOnlyHotspotConfig = null; 1302 updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); 1303 // if that was the last caller, then call stopSoftAp as WifiService 1304 long identity = Binder.clearCallingIdentity(); 1305 try { 1306 stopSoftApInternal(); 1307 } finally { 1308 Binder.restoreCallingIdentity(identity); 1309 } 1310 } 1311 } 1312 } 1313 1314 /** 1315 * see {@link WifiManager#watchLocalOnlyHotspot(LocalOnlyHotspotObserver)} 1316 * 1317 * This call requires the android.permission.NETWORK_SETTINGS permission. 1318 * 1319 * @param messenger Messenger to send messages to the corresponding WifiManager. 1320 * @param binder IBinder instance to allow cleanup if the app dies 1321 * 1322 * @throws SecurityException if the caller does not have permission to watch Local Only Hotspot 1323 * status updates. 1324 * @throws IllegalStateException if the caller attempts to watch LocalOnlyHotspot updates with 1325 * an existing subscription. 1326 */ 1327 @Override 1328 public void startWatchLocalOnlyHotspot(Messenger messenger, IBinder binder) { 1329 final String packageName = mContext.getOpPackageName(); 1330 1331 // NETWORK_SETTINGS is a signature only permission. 1332 enforceNetworkSettingsPermission(); 1333 1334 throw new UnsupportedOperationException("LocalOnlyHotspot is still in development"); 1335 } 1336 1337 /** 1338 * see {@link WifiManager#unregisterLocalOnlyHotspotObserver()} 1339 */ 1340 @Override 1341 public void stopWatchLocalOnlyHotspot() { 1342 // NETWORK_STACK is a signature only permission. 1343 enforceNetworkSettingsPermission(); 1344 throw new UnsupportedOperationException("LocalOnlyHotspot is still in development"); 1345 } 1346 1347 /** 1348 * see {@link WifiManager#getWifiApConfiguration()} 1349 * @return soft access point configuration 1350 * @throws SecurityException if the caller does not have permission to retrieve the softap 1351 * config 1352 */ 1353 @Override 1354 public WifiConfiguration getWifiApConfiguration() { 1355 enforceAccessPermission(); 1356 int uid = Binder.getCallingUid(); 1357 // only allow Settings UI to get the saved SoftApConfig 1358 if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) { 1359 // random apps should not be allowed to read the user specified config 1360 throw new SecurityException("App not allowed to read or update stored WiFi Ap config " 1361 + "(uid = " + uid + ")"); 1362 } 1363 mLog.trace("getWifiApConfiguration uid=%").c(uid).flush(); 1364 return mWifiStateMachine.syncGetWifiApConfiguration(); 1365 } 1366 1367 /** 1368 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)} 1369 * @param wifiConfig WifiConfiguration details for soft access point 1370 * @throws SecurityException if the caller does not have permission to write the sotap config 1371 */ 1372 @Override 1373 public void setWifiApConfiguration(WifiConfiguration wifiConfig) { 1374 enforceChangePermission(); 1375 int uid = Binder.getCallingUid(); 1376 // only allow Settings UI to write the stored SoftApConfig 1377 if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) { 1378 // random apps should not be allowed to read the user specified config 1379 throw new SecurityException("App not allowed to read or update stored WiFi AP config " 1380 + "(uid = " + uid + ")"); 1381 } 1382 mLog.trace("setWifiApConfiguration uid=%").c(uid).flush(); 1383 if (wifiConfig == null) 1384 return; 1385 if (isValid(wifiConfig)) { 1386 mWifiStateMachine.setWifiApConfiguration(wifiConfig); 1387 } else { 1388 Slog.e(TAG, "Invalid WifiConfiguration"); 1389 } 1390 } 1391 1392 /** 1393 * see {@link android.net.wifi.WifiManager#isScanAlwaysAvailable()} 1394 */ 1395 @Override 1396 public boolean isScanAlwaysAvailable() { 1397 enforceAccessPermission(); 1398 mLog.trace("isScanAlwaysAvailable uid=%").c(Binder.getCallingUid()).flush(); 1399 return mSettingsStore.isScanAlwaysAvailable(); 1400 } 1401 1402 /** 1403 * see {@link android.net.wifi.WifiManager#disconnect()} 1404 */ 1405 @Override 1406 public void disconnect() { 1407 enforceChangePermission(); 1408 mLog.trace("disconnect uid=%").c(Binder.getCallingUid()).flush(); 1409 mWifiStateMachine.disconnectCommand(); 1410 } 1411 1412 /** 1413 * see {@link android.net.wifi.WifiManager#reconnect()} 1414 */ 1415 @Override 1416 public void reconnect() { 1417 enforceChangePermission(); 1418 mLog.trace("reconnect uid=%").c(Binder.getCallingUid()).flush(); 1419 mWifiStateMachine.reconnectCommand(); 1420 } 1421 1422 /** 1423 * see {@link android.net.wifi.WifiManager#reassociate()} 1424 */ 1425 @Override 1426 public void reassociate() { 1427 enforceChangePermission(); 1428 mLog.trace("reassociate uid=%").c(Binder.getCallingUid()).flush(); 1429 mWifiStateMachine.reassociateCommand(); 1430 } 1431 1432 /** 1433 * see {@link android.net.wifi.WifiManager#getSupportedFeatures} 1434 */ 1435 @Override 1436 public int getSupportedFeatures() { 1437 enforceAccessPermission(); 1438 mLog.trace("getSupportedFeatures uid=%").c(Binder.getCallingUid()).flush(); 1439 if (mWifiStateMachineChannel != null) { 1440 return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel); 1441 } else { 1442 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1443 return 0; 1444 } 1445 } 1446 1447 @Override 1448 public void requestActivityInfo(ResultReceiver result) { 1449 Bundle bundle = new Bundle(); 1450 mLog.trace("requestActivityInfo uid=%").c(Binder.getCallingUid()).flush(); 1451 bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, reportActivityInfo()); 1452 result.send(0, bundle); 1453 } 1454 1455 /** 1456 * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)} 1457 */ 1458 @Override 1459 public WifiActivityEnergyInfo reportActivityInfo() { 1460 enforceAccessPermission(); 1461 mLog.trace("reportActivityInfo uid=%").c(Binder.getCallingUid()).flush(); 1462 if ((getSupportedFeatures() & WifiManager.WIFI_FEATURE_LINK_LAYER_STATS) == 0) { 1463 return null; 1464 } 1465 WifiLinkLayerStats stats; 1466 WifiActivityEnergyInfo energyInfo = null; 1467 if (mWifiStateMachineChannel != null) { 1468 stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel); 1469 if (stats != null) { 1470 final long rxIdleCurrent = mContext.getResources().getInteger( 1471 com.android.internal.R.integer.config_wifi_idle_receive_cur_ma); 1472 final long rxCurrent = mContext.getResources().getInteger( 1473 com.android.internal.R.integer.config_wifi_active_rx_cur_ma); 1474 final long txCurrent = mContext.getResources().getInteger( 1475 com.android.internal.R.integer.config_wifi_tx_cur_ma); 1476 final double voltage = mContext.getResources().getInteger( 1477 com.android.internal.R.integer.config_wifi_operating_voltage_mv) 1478 / 1000.0; 1479 1480 final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time; 1481 final long[] txTimePerLevel; 1482 if (stats.tx_time_per_level != null) { 1483 txTimePerLevel = new long[stats.tx_time_per_level.length]; 1484 for (int i = 0; i < txTimePerLevel.length; i++) { 1485 txTimePerLevel[i] = stats.tx_time_per_level[i]; 1486 // TODO(b/27227497): Need to read the power consumed per level from config 1487 } 1488 } else { 1489 // This will happen if the HAL get link layer API returned null. 1490 txTimePerLevel = new long[0]; 1491 } 1492 final long energyUsed = (long)((stats.tx_time * txCurrent + 1493 stats.rx_time * rxCurrent + 1494 rxIdleTime * rxIdleCurrent) * voltage); 1495 if (VDBG || rxIdleTime < 0 || stats.on_time < 0 || stats.tx_time < 0 || 1496 stats.rx_time < 0 || energyUsed < 0) { 1497 StringBuilder sb = new StringBuilder(); 1498 sb.append(" rxIdleCur=" + rxIdleCurrent); 1499 sb.append(" rxCur=" + rxCurrent); 1500 sb.append(" txCur=" + txCurrent); 1501 sb.append(" voltage=" + voltage); 1502 sb.append(" on_time=" + stats.on_time); 1503 sb.append(" tx_time=" + stats.tx_time); 1504 sb.append(" tx_time_per_level=" + Arrays.toString(txTimePerLevel)); 1505 sb.append(" rx_time=" + stats.rx_time); 1506 sb.append(" rxIdleTime=" + rxIdleTime); 1507 sb.append(" energy=" + energyUsed); 1508 Log.d(TAG, " reportActivityInfo: " + sb.toString()); 1509 } 1510 1511 // Convert the LinkLayerStats into EnergyActivity 1512 energyInfo = new WifiActivityEnergyInfo(mClock.getElapsedSinceBootMillis(), 1513 WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time, 1514 txTimePerLevel, stats.rx_time, rxIdleTime, energyUsed); 1515 } 1516 if (energyInfo != null && energyInfo.isValid()) { 1517 return energyInfo; 1518 } else { 1519 return null; 1520 } 1521 } else { 1522 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1523 return null; 1524 } 1525 } 1526 1527 /** 1528 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} 1529 * @return the list of configured networks 1530 */ 1531 @Override 1532 public ParceledListSlice<WifiConfiguration> getConfiguredNetworks() { 1533 enforceAccessPermission(); 1534 mLog.trace("getConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush(); 1535 if (mWifiStateMachineChannel != null) { 1536 List<WifiConfiguration> configs = mWifiStateMachine.syncGetConfiguredNetworks( 1537 Binder.getCallingUid(), mWifiStateMachineChannel); 1538 if (configs != null) { 1539 return new ParceledListSlice<WifiConfiguration>(configs); 1540 } 1541 } else { 1542 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1543 } 1544 return null; 1545 } 1546 1547 /** 1548 * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()} 1549 * @return the list of configured networks with real preSharedKey 1550 */ 1551 @Override 1552 public ParceledListSlice<WifiConfiguration> getPrivilegedConfiguredNetworks() { 1553 enforceReadCredentialPermission(); 1554 enforceAccessPermission(); 1555 mLog.trace("getPrivilegedConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush(); 1556 if (mWifiStateMachineChannel != null) { 1557 List<WifiConfiguration> configs = 1558 mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel); 1559 if (configs != null) { 1560 return new ParceledListSlice<WifiConfiguration>(configs); 1561 } 1562 } else { 1563 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1564 } 1565 return null; 1566 } 1567 1568 /** 1569 * Returns a WifiConfiguration for a Passpoint network matching this ScanResult. 1570 * 1571 * @param scanResult scanResult that represents the BSSID 1572 * @return {@link WifiConfiguration} that matches this BSSID or null 1573 */ 1574 @Override 1575 public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) { 1576 enforceAccessPermission(); 1577 mLog.trace("getMatchingWifiConfig uid=%").c(Binder.getCallingUid()).flush(); 1578 if (!mContext.getPackageManager().hasSystemFeature( 1579 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1580 throw new UnsupportedOperationException("Passpoint not enabled"); 1581 } 1582 return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel); 1583 } 1584 1585 /** 1586 * Returns list of OSU (Online Sign-Up) providers associated with the given Passpoint network. 1587 * 1588 * @param scanResult scanResult of the Passpoint AP 1589 * @return List of {@link OsuProvider} 1590 */ 1591 @Override 1592 public List<OsuProvider> getMatchingOsuProviders(ScanResult scanResult) { 1593 enforceAccessPermission(); 1594 mLog.trace("getMatchingOsuProviders uid=%").c(Binder.getCallingUid()).flush(); 1595 if (!mContext.getPackageManager().hasSystemFeature( 1596 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1597 throw new UnsupportedOperationException("Passpoint not enabled"); 1598 } 1599 return mWifiStateMachine.syncGetMatchingOsuProviders(scanResult, mWifiStateMachineChannel); 1600 } 1601 1602 /** 1603 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} 1604 * @return the supplicant-assigned identifier for the new or updated 1605 * network if the operation succeeds, or {@code -1} if it fails 1606 */ 1607 @Override 1608 public int addOrUpdateNetwork(WifiConfiguration config) { 1609 enforceChangePermission(); 1610 mLog.trace("addOrUpdateNetwork uid=%").c(Binder.getCallingUid()).flush(); 1611 1612 // Previously, this API is overloaded for installing Passpoint profiles. Now 1613 // that we have a dedicated API for doing it, redirect the call to the dedicated API. 1614 if (config.isPasspoint()) { 1615 PasspointConfiguration passpointConfig = 1616 PasspointProvider.convertFromWifiConfig(config); 1617 if (passpointConfig.getCredential() == null) { 1618 Slog.e(TAG, "Missing credential for Passpoint profile"); 1619 return -1; 1620 } 1621 // Copy over certificates and keys. 1622 passpointConfig.getCredential().setCaCertificate( 1623 config.enterpriseConfig.getCaCertificate()); 1624 passpointConfig.getCredential().setClientCertificateChain( 1625 config.enterpriseConfig.getClientCertificateChain()); 1626 passpointConfig.getCredential().setClientPrivateKey( 1627 config.enterpriseConfig.getClientPrivateKey()); 1628 if (!addOrUpdatePasspointConfiguration(passpointConfig)) { 1629 Slog.e(TAG, "Failed to add Passpoint profile"); 1630 return -1; 1631 } 1632 // There is no network ID associated with a Passpoint profile. 1633 return 0; 1634 } 1635 1636 if (config != null) { 1637 //TODO: pass the Uid the WifiStateMachine as a message parameter 1638 Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid()) 1639 + " SSID " + config.SSID 1640 + " nid=" + Integer.toString(config.networkId)); 1641 if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) { 1642 config.creatorUid = Binder.getCallingUid(); 1643 } else { 1644 config.lastUpdateUid = Binder.getCallingUid(); 1645 } 1646 if (mWifiStateMachineChannel != null) { 1647 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config); 1648 } else { 1649 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1650 return -1; 1651 } 1652 } else { 1653 Slog.e(TAG, "bad network configuration"); 1654 return -1; 1655 } 1656 } 1657 1658 public static void verifyCert(X509Certificate caCert) 1659 throws GeneralSecurityException, IOException { 1660 CertificateFactory factory = CertificateFactory.getInstance("X.509"); 1661 CertPathValidator validator = 1662 CertPathValidator.getInstance(CertPathValidator.getDefaultType()); 1663 CertPath path = factory.generateCertPath( 1664 Arrays.asList(caCert)); 1665 KeyStore ks = KeyStore.getInstance("AndroidCAStore"); 1666 ks.load(null, null); 1667 PKIXParameters params = new PKIXParameters(ks); 1668 params.setRevocationEnabled(false); 1669 validator.validate(path, params); 1670 } 1671 1672 /** 1673 * See {@link android.net.wifi.WifiManager#removeNetwork(int)} 1674 * @param netId the integer that identifies the network configuration 1675 * to the supplicant 1676 * @return {@code true} if the operation succeeded 1677 */ 1678 @Override 1679 public boolean removeNetwork(int netId) { 1680 enforceChangePermission(); 1681 mLog.trace("removeNetwork uid=%").c(Binder.getCallingUid()).flush(); 1682 // TODO Add private logging for netId b/33807876 1683 if (mWifiStateMachineChannel != null) { 1684 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId); 1685 } else { 1686 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1687 return false; 1688 } 1689 } 1690 1691 /** 1692 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} 1693 * @param netId the integer that identifies the network configuration 1694 * to the supplicant 1695 * @param disableOthers if true, disable all other networks. 1696 * @return {@code true} if the operation succeeded 1697 */ 1698 @Override 1699 public boolean enableNetwork(int netId, boolean disableOthers) { 1700 enforceChangePermission(); 1701 // TODO b/33807876 Log netId 1702 mLog.trace("enableNetwork uid=% disableOthers=%") 1703 .c(Binder.getCallingUid()) 1704 .c(disableOthers).flush(); 1705 1706 if (mWifiStateMachineChannel != null) { 1707 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId, 1708 disableOthers); 1709 } else { 1710 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1711 return false; 1712 } 1713 } 1714 1715 /** 1716 * See {@link android.net.wifi.WifiManager#disableNetwork(int)} 1717 * @param netId the integer that identifies the network configuration 1718 * to the supplicant 1719 * @return {@code true} if the operation succeeded 1720 */ 1721 @Override 1722 public boolean disableNetwork(int netId) { 1723 enforceChangePermission(); 1724 // TODO b/33807876 Log netId 1725 mLog.trace("disableNetwork uid=%").c(Binder.getCallingUid()).flush(); 1726 1727 if (mWifiStateMachineChannel != null) { 1728 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId); 1729 } else { 1730 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1731 return false; 1732 } 1733 } 1734 1735 /** 1736 * See {@link android.net.wifi.WifiManager#getConnectionInfo()} 1737 * @return the Wi-Fi information, contained in {@link WifiInfo}. 1738 */ 1739 @Override 1740 public WifiInfo getConnectionInfo() { 1741 enforceAccessPermission(); 1742 mLog.trace("getConnectionInfo uid=%").c(Binder.getCallingUid()).flush(); 1743 /* 1744 * Make sure we have the latest information, by sending 1745 * a status request to the supplicant. 1746 */ 1747 return mWifiStateMachine.syncRequestConnectionInfo(); 1748 } 1749 1750 /** 1751 * Return the results of the most recent access point scan, in the form of 1752 * a list of {@link ScanResult} objects. 1753 * @return the list of results 1754 */ 1755 @Override 1756 public List<ScanResult> getScanResults(String callingPackage) { 1757 enforceAccessPermission(); 1758 int uid = Binder.getCallingUid(); 1759 long ident = Binder.clearCallingIdentity(); 1760 try { 1761 if (!mWifiPermissionsUtil.canAccessScanResults(callingPackage, 1762 uid, Build.VERSION_CODES.M)) { 1763 return new ArrayList<ScanResult>(); 1764 } 1765 if (mWifiScanner == null) { 1766 mWifiScanner = mWifiInjector.getWifiScanner(); 1767 } 1768 return mWifiScanner.getSingleScanResults(); 1769 } finally { 1770 Binder.restoreCallingIdentity(ident); 1771 } 1772 } 1773 1774 /** 1775 * Add or update a Passpoint configuration. 1776 * 1777 * @param config The Passpoint configuration to be added 1778 * @return true on success or false on failure 1779 */ 1780 @Override 1781 public boolean addOrUpdatePasspointConfiguration(PasspointConfiguration config) { 1782 enforceChangePermission(); 1783 mLog.trace("addorUpdatePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush(); 1784 if (!mContext.getPackageManager().hasSystemFeature( 1785 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1786 throw new UnsupportedOperationException("Passpoint not enabled"); 1787 } 1788 return mWifiStateMachine.syncAddOrUpdatePasspointConfig(mWifiStateMachineChannel, config, 1789 Binder.getCallingUid()); 1790 } 1791 1792 /** 1793 * Remove the Passpoint configuration identified by its FQDN (Fully Qualified Domain Name). 1794 * 1795 * @param fqdn The FQDN of the Passpoint configuration to be removed 1796 * @return true on success or false on failure 1797 */ 1798 @Override 1799 public boolean removePasspointConfiguration(String fqdn) { 1800 enforceChangePermission(); 1801 mLog.trace("removePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush(); 1802 if (!mContext.getPackageManager().hasSystemFeature( 1803 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1804 throw new UnsupportedOperationException("Passpoint not enabled"); 1805 } 1806 return mWifiStateMachine.syncRemovePasspointConfig(mWifiStateMachineChannel, fqdn); 1807 } 1808 1809 /** 1810 * Return the list of the installed Passpoint configurations. 1811 * 1812 * An empty list will be returned when no configuration is installed. 1813 * 1814 * @return A list of {@link PasspointConfiguration} 1815 */ 1816 @Override 1817 public List<PasspointConfiguration> getPasspointConfigurations() { 1818 enforceAccessPermission(); 1819 mLog.trace("getPasspointConfigurations uid=%").c(Binder.getCallingUid()).flush(); 1820 if (!mContext.getPackageManager().hasSystemFeature( 1821 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1822 throw new UnsupportedOperationException("Passpoint not enabled"); 1823 } 1824 return mWifiStateMachine.syncGetPasspointConfigs(mWifiStateMachineChannel); 1825 } 1826 1827 /** 1828 * Query for a Hotspot 2.0 release 2 OSU icon 1829 * @param bssid The BSSID of the AP 1830 * @param fileName Icon file name 1831 */ 1832 @Override 1833 public void queryPasspointIcon(long bssid, String fileName) { 1834 enforceAccessPermission(); 1835 mLog.trace("queryPasspointIcon uid=%").c(Binder.getCallingUid()).flush(); 1836 if (!mContext.getPackageManager().hasSystemFeature( 1837 PackageManager.FEATURE_WIFI_PASSPOINT)) { 1838 throw new UnsupportedOperationException("Passpoint not enabled"); 1839 } 1840 mWifiStateMachine.syncQueryPasspointIcon(mWifiStateMachineChannel, bssid, fileName); 1841 } 1842 1843 /** 1844 * Match the currently associated network against the SP matching the given FQDN 1845 * @param fqdn FQDN of the SP 1846 * @return ordinal [HomeProvider, RoamingProvider, Incomplete, None, Declined] 1847 */ 1848 @Override 1849 public int matchProviderWithCurrentNetwork(String fqdn) { 1850 mLog.trace("matchProviderWithCurrentNetwork uid=%").c(Binder.getCallingUid()).flush(); 1851 return mWifiStateMachine.matchProviderWithCurrentNetwork(mWifiStateMachineChannel, fqdn); 1852 } 1853 1854 /** 1855 * Deauthenticate and set the re-authentication hold off time for the current network 1856 * @param holdoff hold off time in milliseconds 1857 * @param ess set if the hold off pertains to an ESS rather than a BSS 1858 */ 1859 @Override 1860 public void deauthenticateNetwork(long holdoff, boolean ess) { 1861 mLog.trace("deauthenticateNetwork uid=%").c(Binder.getCallingUid()).flush(); 1862 mWifiStateMachine.deauthenticateNetwork(mWifiStateMachineChannel, holdoff, ess); 1863 } 1864 1865 /** 1866 * Tell the supplicant to persist the current list of configured networks. 1867 * @return {@code true} if the operation succeeded 1868 * 1869 * TODO: deprecate this 1870 */ 1871 @Override 1872 public boolean saveConfiguration() { 1873 enforceChangePermission(); 1874 mLog.trace("saveConfiguration uid=%").c(Binder.getCallingUid()).flush(); 1875 if (mWifiStateMachineChannel != null) { 1876 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel); 1877 } else { 1878 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1879 return false; 1880 } 1881 } 1882 1883 /** 1884 * Set the country code 1885 * @param countryCode ISO 3166 country code. 1886 * @param persist {@code true} if the setting should be remembered. 1887 * 1888 * The persist behavior exists so that wifi can fall back to the last 1889 * persisted country code on a restart, when the locale information is 1890 * not available from telephony. 1891 */ 1892 @Override 1893 public void setCountryCode(String countryCode, boolean persist) { 1894 Slog.i(TAG, "WifiService trying to set country code to " + countryCode + 1895 " with persist set to " + persist); 1896 enforceConnectivityInternalPermission(); 1897 mLog.trace("setCountryCode uid=%").c(Binder.getCallingUid()).flush(); 1898 final long token = Binder.clearCallingIdentity(); 1899 mCountryCode.setCountryCode(countryCode); 1900 Binder.restoreCallingIdentity(token); 1901 } 1902 1903 /** 1904 * Get the country code 1905 * @return Get the best choice country code for wifi, regardless of if it was set or 1906 * not. 1907 * Returns null when there is no country code available. 1908 */ 1909 @Override 1910 public String getCountryCode() { 1911 enforceConnectivityInternalPermission(); 1912 mLog.trace("getCountryCode uid=%").c(Binder.getCallingUid()).flush(); 1913 String country = mCountryCode.getCountryCode(); 1914 return country; 1915 } 1916 1917 @Override 1918 public boolean isDualBandSupported() { 1919 //TODO: Should move towards adding a driver API that checks at runtime 1920 mLog.trace("isDualBandSupported uid=%").c(Binder.getCallingUid()).flush(); 1921 return mContext.getResources().getBoolean( 1922 com.android.internal.R.bool.config_wifi_dual_band_support); 1923 } 1924 1925 /** 1926 * Return the DHCP-assigned addresses from the last successful DHCP request, 1927 * if any. 1928 * @return the DHCP information 1929 * @deprecated 1930 */ 1931 @Override 1932 @Deprecated 1933 public DhcpInfo getDhcpInfo() { 1934 enforceAccessPermission(); 1935 mLog.trace("getDhcpInfo uid=%").c(Binder.getCallingUid()).flush(); 1936 DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults(); 1937 1938 DhcpInfo info = new DhcpInfo(); 1939 1940 if (dhcpResults.ipAddress != null && 1941 dhcpResults.ipAddress.getAddress() instanceof Inet4Address) { 1942 info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress()); 1943 } 1944 1945 if (dhcpResults.gateway != null) { 1946 info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway); 1947 } 1948 1949 int dnsFound = 0; 1950 for (InetAddress dns : dhcpResults.dnsServers) { 1951 if (dns instanceof Inet4Address) { 1952 if (dnsFound == 0) { 1953 info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1954 } else { 1955 info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1956 } 1957 if (++dnsFound > 1) break; 1958 } 1959 } 1960 Inet4Address serverAddress = dhcpResults.serverAddress; 1961 if (serverAddress != null) { 1962 info.serverAddress = NetworkUtils.inetAddressToInt(serverAddress); 1963 } 1964 info.leaseDuration = dhcpResults.leaseDuration; 1965 1966 return info; 1967 } 1968 1969 /** 1970 * enable TDLS for the local NIC to remote NIC 1971 * The APPs don't know the remote MAC address to identify NIC though, 1972 * so we need to do additional work to find it from remote IP address 1973 */ 1974 1975 class TdlsTaskParams { 1976 public String remoteIpAddress; 1977 public boolean enable; 1978 } 1979 1980 class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> { 1981 @Override 1982 protected Integer doInBackground(TdlsTaskParams... params) { 1983 1984 // Retrieve parameters for the call 1985 TdlsTaskParams param = params[0]; 1986 String remoteIpAddress = param.remoteIpAddress.trim(); 1987 boolean enable = param.enable; 1988 1989 // Get MAC address of Remote IP 1990 String macAddress = null; 1991 1992 BufferedReader reader = null; 1993 1994 try { 1995 reader = new BufferedReader(new FileReader("/proc/net/arp")); 1996 1997 // Skip over the line bearing colum titles 1998 String line = reader.readLine(); 1999 2000 while ((line = reader.readLine()) != null) { 2001 String[] tokens = line.split("[ ]+"); 2002 if (tokens.length < 6) { 2003 continue; 2004 } 2005 2006 // ARP column format is 2007 // Address HWType HWAddress Flags Mask IFace 2008 String ip = tokens[0]; 2009 String mac = tokens[3]; 2010 2011 if (remoteIpAddress.equals(ip)) { 2012 macAddress = mac; 2013 break; 2014 } 2015 } 2016 2017 if (macAddress == null) { 2018 Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " + 2019 "/proc/net/arp"); 2020 } else { 2021 enableTdlsWithMacAddress(macAddress, enable); 2022 } 2023 2024 } catch (FileNotFoundException e) { 2025 Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address"); 2026 } catch (IOException e) { 2027 Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address"); 2028 } finally { 2029 try { 2030 if (reader != null) { 2031 reader.close(); 2032 } 2033 } 2034 catch (IOException e) { 2035 // Do nothing 2036 } 2037 } 2038 2039 return 0; 2040 } 2041 } 2042 2043 @Override 2044 public void enableTdls(String remoteAddress, boolean enable) { 2045 if (remoteAddress == null) { 2046 throw new IllegalArgumentException("remoteAddress cannot be null"); 2047 } 2048 mLog.trace("enableTdls uid=% enable=%").c(Binder.getCallingUid()).c(enable).flush(); 2049 TdlsTaskParams params = new TdlsTaskParams(); 2050 params.remoteIpAddress = remoteAddress; 2051 params.enable = enable; 2052 new TdlsTask().execute(params); 2053 } 2054 2055 2056 @Override 2057 public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) { 2058 mLog.trace("enableTdlsWithMacAddress uid=% enable=%") 2059 .c(Binder.getCallingUid()) 2060 .c(enable) 2061 .flush(); 2062 if (remoteMacAddress == null) { 2063 throw new IllegalArgumentException("remoteMacAddress cannot be null"); 2064 } 2065 2066 mWifiStateMachine.enableTdls(remoteMacAddress, enable); 2067 } 2068 2069 /** 2070 * Get a reference to handler. This is used by a client to establish 2071 * an AsyncChannel communication with WifiService 2072 */ 2073 @Override 2074 public Messenger getWifiServiceMessenger() { 2075 enforceAccessPermission(); 2076 enforceChangePermission(); 2077 mLog.trace("getWifiServiceMessenger uid=%").c(Binder.getCallingUid()).flush(); 2078 return new Messenger(mClientHandler); 2079 } 2080 2081 /** 2082 * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer 2083 */ 2084 @Override 2085 public void disableEphemeralNetwork(String SSID) { 2086 enforceAccessPermission(); 2087 enforceChangePermission(); 2088 mLog.trace("disableEphemeralNetwork uid=%").c(Binder.getCallingUid()).flush(); 2089 mWifiStateMachine.disableEphemeralNetwork(SSID); 2090 } 2091 2092 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 2093 @Override 2094 public void onReceive(Context context, Intent intent) { 2095 String action = intent.getAction(); 2096 if (action.equals(Intent.ACTION_SCREEN_ON)) { 2097 mWifiController.sendMessage(CMD_SCREEN_ON); 2098 } else if (action.equals(Intent.ACTION_USER_PRESENT)) { 2099 mWifiController.sendMessage(CMD_USER_PRESENT); 2100 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { 2101 mWifiController.sendMessage(CMD_SCREEN_OFF); 2102 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 2103 int pluggedType = intent.getIntExtra("plugged", 0); 2104 mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null); 2105 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 2106 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 2107 BluetoothAdapter.STATE_DISCONNECTED); 2108 mWifiStateMachine.sendBluetoothAdapterStateChange(state); 2109 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { 2110 boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false); 2111 mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0); 2112 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED)) { 2113 boolean inCall = intent.getBooleanExtra(PhoneConstants.PHONE_IN_EMERGENCY_CALL, false); 2114 mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, inCall ? 1 : 0, 0); 2115 } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) { 2116 handleIdleModeChanged(); 2117 } 2118 } 2119 }; 2120 2121 private boolean startConsentUi(String packageName, 2122 int callingUid, String intentAction) throws RemoteException { 2123 if (UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) { 2124 return false; 2125 } 2126 try { 2127 // Validate the package only if we are going to use it 2128 ApplicationInfo applicationInfo = mContext.getPackageManager() 2129 .getApplicationInfoAsUser(packageName, 2130 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, 2131 UserHandle.getUserId(callingUid)); 2132 if (applicationInfo.uid != callingUid) { 2133 throw new SecurityException("Package " + callingUid 2134 + " not in uid " + callingUid); 2135 } 2136 2137 // Permission review mode, trigger a user prompt 2138 Intent intent = new Intent(intentAction); 2139 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 2140 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 2141 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName); 2142 mContext.startActivity(intent); 2143 return true; 2144 } catch (PackageManager.NameNotFoundException e) { 2145 throw new RemoteException(e.getMessage()); 2146 } 2147 } 2148 2149 /** 2150 * Observes settings changes to scan always mode. 2151 */ 2152 private void registerForScanModeChange() { 2153 ContentObserver contentObserver = new ContentObserver(null) { 2154 @Override 2155 public void onChange(boolean selfChange) { 2156 mSettingsStore.handleWifiScanAlwaysAvailableToggled(); 2157 mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED); 2158 } 2159 }; 2160 mFrameworkFacade.registerContentObserver(mContext, 2161 Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE), 2162 false, contentObserver); 2163 2164 } 2165 2166 // Monitors settings changes related to background wifi scan throttling. 2167 private void registerForBackgroundThrottleChanges() { 2168 mFrameworkFacade.registerContentObserver( 2169 mContext, 2170 Settings.Global.getUriFor( 2171 Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS), 2172 false, 2173 new ContentObserver(null) { 2174 @Override 2175 public void onChange(boolean selfChange) { 2176 updateBackgroundThrottleInterval(); 2177 } 2178 } 2179 ); 2180 mFrameworkFacade.registerContentObserver( 2181 mContext, 2182 Settings.Global.getUriFor( 2183 Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_PACKAGE_WHITELIST), 2184 false, 2185 new ContentObserver(null) { 2186 @Override 2187 public void onChange(boolean selfChange) { 2188 updateBackgroundThrottlingWhitelist(); 2189 } 2190 } 2191 ); 2192 } 2193 2194 private void updateBackgroundThrottleInterval() { 2195 mBackgroundThrottleInterval = mFrameworkFacade.getLongSetting( 2196 mContext, 2197 Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS, 2198 DEFAULT_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS); 2199 } 2200 2201 private void updateBackgroundThrottlingWhitelist() { 2202 String setting = mFrameworkFacade.getStringSetting( 2203 mContext, 2204 Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_PACKAGE_WHITELIST); 2205 mBackgroundThrottlePackageWhitelist.clear(); 2206 if (setting != null) { 2207 mBackgroundThrottlePackageWhitelist.addAll(Arrays.asList(setting.split(","))); 2208 } 2209 } 2210 2211 private void registerForBroadcasts() { 2212 IntentFilter intentFilter = new IntentFilter(); 2213 intentFilter.addAction(Intent.ACTION_SCREEN_ON); 2214 intentFilter.addAction(Intent.ACTION_USER_PRESENT); 2215 intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 2216 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 2217 intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 2218 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 2219 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); 2220 intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); 2221 2222 boolean trackEmergencyCallState = mContext.getResources().getBoolean( 2223 com.android.internal.R.bool.config_wifi_turn_off_during_emergency_call); 2224 if (trackEmergencyCallState) { 2225 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED); 2226 } 2227 2228 mContext.registerReceiver(mReceiver, intentFilter); 2229 } 2230 2231 private void registerForPackageOrUserRemoval() { 2232 IntentFilter intentFilter = new IntentFilter(); 2233 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 2234 intentFilter.addAction(Intent.ACTION_USER_REMOVED); 2235 mContext.registerReceiverAsUser(new BroadcastReceiver() { 2236 @Override 2237 public void onReceive(Context context, Intent intent) { 2238 switch (intent.getAction()) { 2239 case Intent.ACTION_PACKAGE_REMOVED: { 2240 if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 2241 return; 2242 } 2243 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); 2244 Uri uri = intent.getData(); 2245 if (uid == -1 || uri == null) { 2246 return; 2247 } 2248 String pkgName = uri.getSchemeSpecificPart(); 2249 mWifiStateMachine.removeAppConfigs(pkgName, uid); 2250 break; 2251 } 2252 case Intent.ACTION_USER_REMOVED: { 2253 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); 2254 mWifiStateMachine.removeUserConfigs(userHandle); 2255 break; 2256 } 2257 } 2258 } 2259 }, UserHandle.ALL, intentFilter, null, null); 2260 } 2261 2262 @Override 2263 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, 2264 String[] args, ShellCallback callback, ResultReceiver resultReceiver) { 2265 (new WifiShellCommand(mWifiStateMachine)).exec(this, in, out, err, args, callback, 2266 resultReceiver); 2267 } 2268 2269 @Override 2270 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 2271 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 2272 != PackageManager.PERMISSION_GRANTED) { 2273 pw.println("Permission Denial: can't dump WifiService from from pid=" 2274 + Binder.getCallingPid() 2275 + ", uid=" + Binder.getCallingUid()); 2276 return; 2277 } 2278 if (args != null && args.length > 0 && WifiMetrics.PROTO_DUMP_ARG.equals(args[0])) { 2279 // WifiMetrics proto bytes were requested. Dump only these. 2280 mWifiStateMachine.updateWifiMetrics(); 2281 mWifiMetrics.dump(fd, pw, args); 2282 } else if (args != null && args.length > 0 && IpManager.DUMP_ARG.equals(args[0])) { 2283 // IpManager dump was requested. Pass it along and take no further action. 2284 String[] ipManagerArgs = new String[args.length - 1]; 2285 System.arraycopy(args, 1, ipManagerArgs, 0, ipManagerArgs.length); 2286 mWifiStateMachine.dumpIpManager(fd, pw, ipManagerArgs); 2287 } else if (args != null && args.length > 0 && WifiScoreReport.DUMP_ARG.equals(args[0])) { 2288 WifiScoreReport wifiScoreReport = mWifiStateMachine.getWifiScoreReport(); 2289 if (wifiScoreReport != null) wifiScoreReport.dump(fd, pw, args); 2290 } else { 2291 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName()); 2292 pw.println("Stay-awake conditions: " + 2293 mFacade.getIntegerSetting(mContext, 2294 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0)); 2295 pw.println("mInIdleMode " + mInIdleMode); 2296 pw.println("mScanPending " + mScanPending); 2297 mWifiController.dump(fd, pw, args); 2298 mSettingsStore.dump(fd, pw, args); 2299 mTrafficPoller.dump(fd, pw, args); 2300 pw.println(); 2301 pw.println("Locks held:"); 2302 mWifiLockManager.dump(pw); 2303 pw.println(); 2304 mWifiMulticastLockManager.dump(pw); 2305 pw.println(); 2306 mWifiStateMachine.dump(fd, pw, args); 2307 pw.println(); 2308 mWifiStateMachine.updateWifiMetrics(); 2309 mWifiMetrics.dump(fd, pw, args); 2310 pw.println(); 2311 mWifiBackupRestore.dump(fd, pw, args); 2312 pw.println(); 2313 WifiScoreReport wifiScoreReport = mWifiStateMachine.getWifiScoreReport(); 2314 if (wifiScoreReport != null) { 2315 pw.println("WifiScoreReport:"); 2316 wifiScoreReport.dump(fd, pw, args); 2317 } 2318 pw.println(); 2319 } 2320 } 2321 2322 @Override 2323 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) { 2324 mLog.trace("acquireWifiLock uid=% lockMode=%") 2325 .c(Binder.getCallingUid()) 2326 .c(lockMode).flush(); 2327 if (mWifiLockManager.acquireWifiLock(lockMode, tag, binder, ws)) { 2328 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 2329 return true; 2330 } 2331 return false; 2332 } 2333 2334 @Override 2335 public void updateWifiLockWorkSource(IBinder binder, WorkSource ws) { 2336 mLog.trace("updateWifiLockWorkSource uid=%").c(Binder.getCallingUid()).flush(); 2337 mWifiLockManager.updateWifiLockWorkSource(binder, ws); 2338 } 2339 2340 @Override 2341 public boolean releaseWifiLock(IBinder binder) { 2342 mLog.trace("releaseWifiLock uid=%").c(Binder.getCallingUid()).flush(); 2343 if (mWifiLockManager.releaseWifiLock(binder)) { 2344 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 2345 return true; 2346 } 2347 return false; 2348 } 2349 2350 @Override 2351 public void initializeMulticastFiltering() { 2352 enforceMulticastChangePermission(); 2353 mLog.trace("initializeMulticastFiltering uid=%").c(Binder.getCallingUid()).flush(); 2354 mWifiMulticastLockManager.initializeFiltering(); 2355 } 2356 2357 @Override 2358 public void acquireMulticastLock(IBinder binder, String tag) { 2359 enforceMulticastChangePermission(); 2360 mLog.trace("acquireMulticastLock uid=%").c(Binder.getCallingUid()).flush(); 2361 mWifiMulticastLockManager.acquireLock(binder, tag); 2362 } 2363 2364 @Override 2365 public void releaseMulticastLock() { 2366 enforceMulticastChangePermission(); 2367 mLog.trace("releaseMulticastLock uid=%").c(Binder.getCallingUid()).flush(); 2368 mWifiMulticastLockManager.releaseLock(); 2369 } 2370 2371 @Override 2372 public boolean isMulticastEnabled() { 2373 enforceAccessPermission(); 2374 mLog.trace("isMulticastEnabled uid=%").c(Binder.getCallingUid()).flush(); 2375 return mWifiMulticastLockManager.isMulticastEnabled(); 2376 } 2377 2378 @Override 2379 public void enableVerboseLogging(int verbose) { 2380 enforceAccessPermission(); 2381 mLog.trace("enableVerboseLogging uid=% verbose=%") 2382 .c(Binder.getCallingUid()) 2383 .c(verbose).flush(); 2384 mFacade.setIntegerSetting( 2385 mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, verbose); 2386 enableVerboseLoggingInternal(verbose); 2387 } 2388 2389 void enableVerboseLoggingInternal(int verbose) { 2390 mWifiStateMachine.enableVerboseLogging(verbose); 2391 mWifiLockManager.enableVerboseLogging(verbose); 2392 mWifiMulticastLockManager.enableVerboseLogging(verbose); 2393 mWifiInjector.getWifiLastResortWatchdog().enableVerboseLogging(verbose); 2394 mWifiInjector.getWifiBackupRestore().enableVerboseLogging(verbose); 2395 LogcatLog.enableVerboseLogging(verbose); 2396 } 2397 2398 @Override 2399 public int getVerboseLoggingLevel() { 2400 enforceAccessPermission(); 2401 mLog.trace("getVerboseLoggingLevel uid=%").c(Binder.getCallingUid()).flush(); 2402 return mFacade.getIntegerSetting( 2403 mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, 0); 2404 } 2405 2406 @Override 2407 public void enableAggressiveHandover(int enabled) { 2408 enforceAccessPermission(); 2409 mLog.trace("enableAggressiveHandover uid=% enabled=%") 2410 .c(Binder.getCallingUid()) 2411 .c(enabled) 2412 .flush(); 2413 mWifiStateMachine.enableAggressiveHandover(enabled); 2414 } 2415 2416 @Override 2417 public int getAggressiveHandover() { 2418 enforceAccessPermission(); 2419 mLog.trace("getAggressiveHandover uid=%").c(Binder.getCallingUid()).flush(); 2420 return mWifiStateMachine.getAggressiveHandover(); 2421 } 2422 2423 @Override 2424 public void setAllowScansWithTraffic(int enabled) { 2425 enforceAccessPermission(); 2426 mLog.trace("setAllowScansWithTraffic uid=% enabled=%") 2427 .c(Binder.getCallingUid()) 2428 .c(enabled).flush(); 2429 mWifiStateMachine.setAllowScansWithTraffic(enabled); 2430 } 2431 2432 @Override 2433 public int getAllowScansWithTraffic() { 2434 enforceAccessPermission(); 2435 mLog.trace("getAllowScansWithTraffic uid=%").c(Binder.getCallingUid()).flush(); 2436 return mWifiStateMachine.getAllowScansWithTraffic(); 2437 } 2438 2439 @Override 2440 public boolean setEnableAutoJoinWhenAssociated(boolean enabled) { 2441 enforceChangePermission(); 2442 mLog.trace("setEnableAutoJoinWhenAssociated uid=% enabled=%") 2443 .c(Binder.getCallingUid()) 2444 .c(enabled).flush(); 2445 return mWifiStateMachine.setEnableAutoJoinWhenAssociated(enabled); 2446 } 2447 2448 @Override 2449 public boolean getEnableAutoJoinWhenAssociated() { 2450 enforceAccessPermission(); 2451 mLog.trace("getEnableAutoJoinWhenAssociated uid=%").c(Binder.getCallingUid()).flush(); 2452 return mWifiStateMachine.getEnableAutoJoinWhenAssociated(); 2453 } 2454 2455 /* Return the Wifi Connection statistics object */ 2456 @Override 2457 public WifiConnectionStatistics getConnectionStatistics() { 2458 enforceAccessPermission(); 2459 enforceReadCredentialPermission(); 2460 mLog.trace("getConnectionStatistics uid=%").c(Binder.getCallingUid()).flush(); 2461 if (mWifiStateMachineChannel != null) { 2462 return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel); 2463 } else { 2464 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 2465 return null; 2466 } 2467 } 2468 2469 @Override 2470 public void factoryReset() { 2471 enforceConnectivityInternalPermission(); 2472 mLog.trace("factoryReset uid=%").c(Binder.getCallingUid()).flush(); 2473 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { 2474 return; 2475 } 2476 2477 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 2478 // Turn mobile hotspot off - will also clear any registered LOHS requests when it is 2479 // shut down 2480 stopSoftApInternal(); 2481 } 2482 2483 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) { 2484 // Enable wifi 2485 try { 2486 setWifiEnabled(mContext.getOpPackageName(), true); 2487 } catch (RemoteException e) { 2488 /* ignore - local call */ 2489 } 2490 // Delete all Wifi SSIDs 2491 if (mWifiStateMachineChannel != null) { 2492 List<WifiConfiguration> networks = mWifiStateMachine.syncGetConfiguredNetworks( 2493 Binder.getCallingUid(), mWifiStateMachineChannel); 2494 if (networks != null) { 2495 for (WifiConfiguration config : networks) { 2496 removeNetwork(config.networkId); 2497 } 2498 saveConfiguration(); 2499 } 2500 } 2501 } 2502 } 2503 2504 /* private methods */ 2505 static boolean logAndReturnFalse(String s) { 2506 Log.d(TAG, s); 2507 return false; 2508 } 2509 2510 public static boolean isValid(WifiConfiguration config) { 2511 String validity = checkValidity(config); 2512 return validity == null || logAndReturnFalse(validity); 2513 } 2514 2515 public static String checkValidity(WifiConfiguration config) { 2516 if (config.allowedKeyManagement == null) 2517 return "allowed kmgmt"; 2518 2519 if (config.allowedKeyManagement.cardinality() > 1) { 2520 if (config.allowedKeyManagement.cardinality() != 2) { 2521 return "cardinality != 2"; 2522 } 2523 if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) { 2524 return "not WPA_EAP"; 2525 } 2526 if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) 2527 && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) { 2528 return "not PSK or 8021X"; 2529 } 2530 } 2531 if (config.getIpAssignment() == IpConfiguration.IpAssignment.STATIC) { 2532 StaticIpConfiguration staticIpConf = config.getStaticIpConfiguration(); 2533 if (staticIpConf == null) { 2534 return "null StaticIpConfiguration"; 2535 } 2536 if (staticIpConf.ipAddress == null) { 2537 return "null static ip Address"; 2538 } 2539 } 2540 return null; 2541 } 2542 2543 @Override 2544 public Network getCurrentNetwork() { 2545 enforceAccessPermission(); 2546 mLog.trace("getCurrentNetwork uid=%").c(Binder.getCallingUid()).flush(); 2547 return mWifiStateMachine.getCurrentNetwork(); 2548 } 2549 2550 public static String toHexString(String s) { 2551 if (s == null) { 2552 return "null"; 2553 } 2554 StringBuilder sb = new StringBuilder(); 2555 sb.append('\'').append(s).append('\''); 2556 for (int n = 0; n < s.length(); n++) { 2557 sb.append(String.format(" %02x", s.charAt(n) & 0xffff)); 2558 } 2559 return sb.toString(); 2560 } 2561 2562 public void hideCertFromUnaffiliatedUsers(String alias) { 2563 mCertManager.hideCertFromUnaffiliatedUsers(alias); 2564 } 2565 2566 public String[] listClientCertsForCurrentUser() { 2567 return mCertManager.listClientCertsForCurrentUser(); 2568 } 2569 2570 /** 2571 * Enable/disable WifiConnectivityManager at runtime 2572 * 2573 * @param enabled true-enable; false-disable 2574 */ 2575 @Override 2576 public void enableWifiConnectivityManager(boolean enabled) { 2577 enforceConnectivityInternalPermission(); 2578 mLog.trace("enableWifiConnectivityManager uid=% enabled=%") 2579 .c(Binder.getCallingUid()) 2580 .c(enabled).flush(); 2581 mWifiStateMachine.enableWifiConnectivityManager(enabled); 2582 } 2583 2584 /** 2585 * Retrieve the data to be backed to save the current state. 2586 * 2587 * @return Raw byte stream of the data to be backed up. 2588 */ 2589 @Override 2590 public byte[] retrieveBackupData() { 2591 enforceNetworkSettingsPermission(); 2592 mLog.trace("retrieveBackupData uid=%").c(Binder.getCallingUid()).flush(); 2593 if (mWifiStateMachineChannel == null) { 2594 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 2595 return null; 2596 } 2597 2598 Slog.d(TAG, "Retrieving backup data"); 2599 List<WifiConfiguration> wifiConfigurations = 2600 mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel); 2601 byte[] backupData = 2602 mWifiBackupRestore.retrieveBackupDataFromConfigurations(wifiConfigurations); 2603 Slog.d(TAG, "Retrieved backup data"); 2604 return backupData; 2605 } 2606 2607 /** 2608 * Helper method to restore networks retrieved from backup data. 2609 * 2610 * @param configurations list of WifiConfiguration objects parsed from the backup data. 2611 */ 2612 private void restoreNetworks(List<WifiConfiguration> configurations) { 2613 if (configurations == null) { 2614 Slog.e(TAG, "Backup data parse failed"); 2615 return; 2616 } 2617 for (WifiConfiguration configuration : configurations) { 2618 int networkId = mWifiStateMachine.syncAddOrUpdateNetwork( 2619 mWifiStateMachineChannel, configuration); 2620 if (networkId == WifiConfiguration.INVALID_NETWORK_ID) { 2621 Slog.e(TAG, "Restore network failed: " + configuration.configKey()); 2622 continue; 2623 } 2624 // Enable all networks restored. 2625 mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, networkId, false); 2626 } 2627 } 2628 2629 /** 2630 * Restore state from the backed up data. 2631 * 2632 * @param data Raw byte stream of the backed up data. 2633 */ 2634 @Override 2635 public void restoreBackupData(byte[] data) { 2636 enforceNetworkSettingsPermission(); 2637 mLog.trace("restoreBackupData uid=%").c(Binder.getCallingUid()).flush(); 2638 if (mWifiStateMachineChannel == null) { 2639 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 2640 return; 2641 } 2642 2643 Slog.d(TAG, "Restoring backup data"); 2644 List<WifiConfiguration> wifiConfigurations = 2645 mWifiBackupRestore.retrieveConfigurationsFromBackupData(data); 2646 restoreNetworks(wifiConfigurations); 2647 Slog.d(TAG, "Restored backup data"); 2648 } 2649 2650 /** 2651 * Restore state from the older supplicant back up data. 2652 * The old backup data was essentially a backup of wpa_supplicant.conf & ipconfig.txt file. 2653 * 2654 * @param supplicantData Raw byte stream of wpa_supplicant.conf 2655 * @param ipConfigData Raw byte stream of ipconfig.txt 2656 */ 2657 public void restoreSupplicantBackupData(byte[] supplicantData, byte[] ipConfigData) { 2658 enforceNetworkSettingsPermission(); 2659 mLog.trace("restoreSupplicantBackupData uid=%").c(Binder.getCallingUid()).flush(); 2660 if (mWifiStateMachineChannel == null) { 2661 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 2662 return; 2663 } 2664 2665 Slog.d(TAG, "Restoring supplicant backup data"); 2666 List<WifiConfiguration> wifiConfigurations = 2667 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 2668 supplicantData, ipConfigData); 2669 restoreNetworks(wifiConfigurations); 2670 Slog.d(TAG, "Restored supplicant backup data"); 2671 } 2672} 2673