WifiServiceImpl.java revision 3e954f2804d376c9ca08e23c00b266c668f65e53
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 com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED; 20import static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED; 21import static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED; 22import static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED; 23import static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED; 24import static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED; 25import static com.android.server.wifi.WifiController.CMD_SCREEN_OFF; 26import static com.android.server.wifi.WifiController.CMD_SCREEN_ON; 27import static com.android.server.wifi.WifiController.CMD_SET_AP; 28import static com.android.server.wifi.WifiController.CMD_USER_PRESENT; 29import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED; 30 31import android.Manifest; 32import android.app.ActivityManager; 33import android.app.AppOpsManager; 34import android.bluetooth.BluetoothAdapter; 35import android.content.BroadcastReceiver; 36import android.content.Context; 37import android.content.Intent; 38import android.content.IntentFilter; 39import android.content.pm.PackageManager; 40import android.content.pm.UserInfo; 41import android.database.ContentObserver; 42import android.net.ConnectivityManager; 43import android.net.ip.IpManager; 44import android.net.DhcpInfo; 45import android.net.DhcpResults; 46import android.net.Network; 47import android.net.NetworkScorerAppManager; 48import android.net.NetworkUtils; 49import android.net.Uri; 50import android.net.wifi.IWifiManager; 51import android.net.wifi.PasspointManagementObjectDefinition; 52import android.net.wifi.ScanResult; 53import android.net.wifi.ScanSettings; 54import android.net.wifi.WifiActivityEnergyInfo; 55import android.net.wifi.WifiConfiguration; 56import android.net.wifi.WifiConnectionStatistics; 57import android.net.wifi.WifiEnterpriseConfig; 58import android.net.wifi.WifiInfo; 59import android.net.wifi.WifiLinkLayerStats; 60import android.net.wifi.WifiManager; 61import android.os.AsyncTask; 62import android.os.Binder; 63import android.os.Build; 64import android.os.Handler; 65import android.os.HandlerThread; 66import android.os.IBinder; 67import android.os.Looper; 68import android.os.Message; 69import android.os.Messenger; 70import android.os.PowerManager; 71import android.os.RemoteException; 72import android.os.SystemClock; 73import android.os.SystemProperties; 74import android.os.UserHandle; 75import android.os.UserManager; 76import android.os.WorkSource; 77import android.provider.Settings; 78import android.text.TextUtils; 79import android.util.Log; 80import android.util.Slog; 81 82import com.android.internal.R; 83import com.android.internal.app.IBatteryStats; 84import com.android.internal.telephony.IccCardConstants; 85import com.android.internal.telephony.PhoneConstants; 86import com.android.internal.telephony.TelephonyIntents; 87import com.android.internal.util.AsyncChannel; 88import com.android.server.am.BatteryStatsService; 89import com.android.server.wifi.configparse.ConfigBuilder; 90 91import org.xml.sax.SAXException; 92 93import java.io.BufferedReader; 94import java.io.FileDescriptor; 95import java.io.FileNotFoundException; 96import java.io.FileReader; 97import java.io.IOException; 98import java.io.PrintWriter; 99import java.net.Inet4Address; 100import java.net.InetAddress; 101import java.security.GeneralSecurityException; 102import java.security.KeyStore; 103import java.security.cert.CertPath; 104import java.security.cert.CertPathValidator; 105import java.security.cert.CertPathValidatorException; 106import java.security.cert.CertificateFactory; 107import java.security.cert.PKIXParameters; 108import java.security.cert.X509Certificate; 109import java.util.ArrayList; 110import java.util.Arrays; 111import java.util.List; 112 113/** 114 * WifiService handles remote WiFi operation requests by implementing 115 * the IWifiManager interface. 116 * 117 * @hide 118 */ 119public class WifiServiceImpl extends IWifiManager.Stub { 120 private static final String TAG = "WifiService"; 121 private static final boolean DBG = true; 122 private static final boolean VDBG = false; 123 private static final String BOOT_DEFAULT_WIFI_COUNTRY_CODE = "ro.boot.wificountrycode"; 124 125 final WifiStateMachine mWifiStateMachine; 126 127 private final Context mContext; 128 129 final LockList mLocks = new LockList(); 130 // some wifi lock statistics 131 private int mFullHighPerfLocksAcquired; 132 private int mFullHighPerfLocksReleased; 133 private int mFullLocksAcquired; 134 private int mFullLocksReleased; 135 private int mScanLocksAcquired; 136 private int mScanLocksReleased; 137 138 private final List<Multicaster> mMulticasters = 139 new ArrayList<Multicaster>(); 140 private int mMulticastEnabled; 141 private int mMulticastDisabled; 142 143 private final IBatteryStats mBatteryStats; 144 private final PowerManager mPowerManager; 145 private final AppOpsManager mAppOps; 146 private final UserManager mUserManager; 147 private final WifiCountryCode mCountryCode; 148 // Debug counter tracking scan requests sent by WifiManager 149 private int scanRequestCounter = 0; 150 151 /* Tracks the open wi-fi network notification */ 152 private WifiNotificationController mNotificationController; 153 /* Polls traffic stats and notifies clients */ 154 private WifiTrafficPoller mTrafficPoller; 155 /* Tracks the persisted states for wi-fi & airplane mode */ 156 final WifiSettingsStore mSettingsStore; 157 /* Logs connection events and some general router and scan stats */ 158 private final WifiMetrics mWifiMetrics; 159 /* Manages affiliated certificates for current user */ 160 private final WifiCertManager mCertManager; 161 162 private final WifiInjector mWifiInjector; 163 /** 164 * Asynchronous channel to WifiStateMachine 165 */ 166 private AsyncChannel mWifiStateMachineChannel; 167 168 /** 169 * Handles client connections 170 */ 171 private class ClientHandler extends Handler { 172 173 ClientHandler(Looper looper) { 174 super(looper); 175 } 176 177 @Override 178 public void handleMessage(Message msg) { 179 switch (msg.what) { 180 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 181 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 182 if (DBG) Slog.d(TAG, "New client listening to asynchronous messages"); 183 // We track the clients by the Messenger 184 // since it is expected to be always available 185 mTrafficPoller.addClient(msg.replyTo); 186 } else { 187 Slog.e(TAG, "Client connection failure, error=" + msg.arg1); 188 } 189 break; 190 } 191 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 192 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) { 193 if (DBG) Slog.d(TAG, "Send failed, client connection lost"); 194 } else { 195 if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1); 196 } 197 mTrafficPoller.removeClient(msg.replyTo); 198 break; 199 } 200 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { 201 AsyncChannel ac = new AsyncChannel(); 202 ac.connect(mContext, this, msg.replyTo); 203 break; 204 } 205 /* Client commands are forwarded to state machine */ 206 case WifiManager.CONNECT_NETWORK: 207 case WifiManager.SAVE_NETWORK: { 208 WifiConfiguration config = (WifiConfiguration) msg.obj; 209 int networkId = msg.arg1; 210 if (msg.what == WifiManager.SAVE_NETWORK) { 211 Slog.d("WiFiServiceImpl ", "SAVE" 212 + " nid=" + Integer.toString(networkId) 213 + " uid=" + msg.sendingUid 214 + " name=" 215 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 216 } 217 if (msg.what == WifiManager.CONNECT_NETWORK) { 218 Slog.d("WiFiServiceImpl ", "CONNECT " 219 + " nid=" + Integer.toString(networkId) 220 + " uid=" + msg.sendingUid 221 + " name=" 222 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 223 } 224 225 if (config != null && isValid(config)) { 226 if (DBG) Slog.d(TAG, "Connect with config" + config); 227 mWifiStateMachine.sendMessage(Message.obtain(msg)); 228 } else if (config == null 229 && networkId != WifiConfiguration.INVALID_NETWORK_ID) { 230 if (DBG) Slog.d(TAG, "Connect with networkId" + networkId); 231 mWifiStateMachine.sendMessage(Message.obtain(msg)); 232 } else { 233 Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg); 234 if (msg.what == WifiManager.CONNECT_NETWORK) { 235 replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED, 236 WifiManager.INVALID_ARGS); 237 } else { 238 replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED, 239 WifiManager.INVALID_ARGS); 240 } 241 } 242 break; 243 } 244 case WifiManager.FORGET_NETWORK: 245 mWifiStateMachine.sendMessage(Message.obtain(msg)); 246 break; 247 case WifiManager.START_WPS: 248 case WifiManager.CANCEL_WPS: 249 case WifiManager.DISABLE_NETWORK: 250 case WifiManager.RSSI_PKTCNT_FETCH: { 251 mWifiStateMachine.sendMessage(Message.obtain(msg)); 252 break; 253 } 254 default: { 255 Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg); 256 break; 257 } 258 } 259 } 260 261 private void replyFailed(Message msg, int what, int why) { 262 Message reply = msg.obtain(); 263 reply.what = what; 264 reply.arg1 = why; 265 try { 266 msg.replyTo.send(reply); 267 } catch (RemoteException e) { 268 // There's not much we can do if reply can't be sent! 269 } 270 } 271 } 272 private ClientHandler mClientHandler; 273 274 /** 275 * Handles interaction with WifiStateMachine 276 */ 277 private class WifiStateMachineHandler extends Handler { 278 private AsyncChannel mWsmChannel; 279 280 WifiStateMachineHandler(Looper looper) { 281 super(looper); 282 mWsmChannel = new AsyncChannel(); 283 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 284 } 285 286 @Override 287 public void handleMessage(Message msg) { 288 switch (msg.what) { 289 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 290 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 291 mWifiStateMachineChannel = mWsmChannel; 292 } else { 293 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1); 294 mWifiStateMachineChannel = null; 295 } 296 break; 297 } 298 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 299 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1); 300 mWifiStateMachineChannel = null; 301 //Re-establish connection to state machine 302 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 303 break; 304 } 305 default: { 306 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg); 307 break; 308 } 309 } 310 } 311 } 312 313 WifiStateMachineHandler mWifiStateMachineHandler; 314 315 private WifiController mWifiController; 316 317 public WifiServiceImpl(Context context) { 318 mContext = context; 319 mWifiInjector = WifiInjector.getInstance(); 320 FrameworkFacade facade = new FrameworkFacade(); 321 HandlerThread wifiThread = new HandlerThread("WifiService"); 322 wifiThread.start(); 323 mWifiMetrics = mWifiInjector.getWifiMetrics(); 324 mTrafficPoller = new WifiTrafficPoller(mContext, wifiThread.getLooper(), 325 WifiNative.getWlanNativeInterface().getInterfaceName()); 326 mUserManager = UserManager.get(mContext); 327 HandlerThread wifiStateMachineThread = new HandlerThread("WifiStateMachine"); 328 wifiStateMachineThread.start(); 329 mCountryCode = new WifiCountryCode(WifiNative.getWlanNativeInterface(), 330 SystemProperties.get(BOOT_DEFAULT_WIFI_COUNTRY_CODE), 331 mContext.getResources().getBoolean( 332 R.bool.config_wifi_revert_country_code_on_cellular_loss)); 333 mWifiStateMachine = new WifiStateMachine(mContext, facade, 334 wifiStateMachineThread.getLooper(), mUserManager, mWifiInjector, 335 new BackupManagerProxy(), mCountryCode); 336 mSettingsStore = new WifiSettingsStore(mContext); 337 mWifiStateMachine.enableRssiPolling(true); 338 mBatteryStats = BatteryStatsService.getService(); 339 mPowerManager = context.getSystemService(PowerManager.class); 340 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); 341 mCertManager = new WifiCertManager(mContext); 342 343 mNotificationController = new WifiNotificationController(mContext, 344 wifiThread.getLooper(), mWifiStateMachine, facade, null); 345 346 mClientHandler = new ClientHandler(wifiThread.getLooper()); 347 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper()); 348 mWifiController = new WifiController(mContext, mWifiStateMachine, 349 mSettingsStore, mLocks, wifiThread.getLooper(), facade); 350 } 351 352 353 /** 354 * Check if Wi-Fi needs to be enabled and start 355 * if needed 356 * 357 * This function is used only at boot time 358 */ 359 public void checkAndStartWifi() { 360 /* Check if wi-fi needs to be enabled */ 361 boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled(); 362 Slog.i(TAG, "WifiService starting up with Wi-Fi " + 363 (wifiEnabled ? "enabled" : "disabled")); 364 365 registerForScanModeChange(); 366 mContext.registerReceiver( 367 new BroadcastReceiver() { 368 @Override 369 public void onReceive(Context context, Intent intent) { 370 if (mSettingsStore.handleAirplaneModeToggled()) { 371 mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED); 372 } 373 if (mSettingsStore.isAirplaneModeOn()) { 374 Log.d(TAG, "resetting country code because Airplane mode is ON"); 375 mCountryCode.airplaneModeEnabled(); 376 } 377 } 378 }, 379 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); 380 381 mContext.registerReceiver( 382 new BroadcastReceiver() { 383 @Override 384 public void onReceive(Context context, Intent intent) { 385 String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE); 386 if (state.equals(IccCardConstants.INTENT_VALUE_ICC_ABSENT)) { 387 Log.d(TAG, "resetting networks because SIM was removed"); 388 mWifiStateMachine.resetSimAuthNetworks(); 389 Log.d(TAG, "resetting country code because SIM is removed"); 390 mCountryCode.simCardRemoved(); 391 } 392 } 393 }, 394 new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED)); 395 396 // Adding optimizations of only receiving broadcasts when wifi is enabled 397 // can result in race conditions when apps toggle wifi in the background 398 // without active user involvement. Always receive broadcasts. 399 registerForBroadcasts(); 400 registerForPackageOrUserRemoval(); 401 mInIdleMode = mPowerManager.isDeviceIdleMode(); 402 403 mWifiController.start(); 404 405 // If we are already disabled (could be due to airplane mode), avoid changing persist 406 // state here 407 if (wifiEnabled) setWifiEnabled(wifiEnabled); 408 } 409 410 public void handleUserSwitch(int userId) { 411 mWifiStateMachine.handleUserSwitch(userId); 412 } 413 414 /** 415 * see {@link android.net.wifi.WifiManager#pingSupplicant()} 416 * @return {@code true} if the operation succeeds, {@code false} otherwise 417 */ 418 public boolean pingSupplicant() { 419 enforceAccessPermission(); 420 if (mWifiStateMachineChannel != null) { 421 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel); 422 } else { 423 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 424 return false; 425 } 426 } 427 428 /** 429 * see {@link android.net.wifi.WifiManager#startScan} 430 * and {@link android.net.wifi.WifiManager#startCustomizedScan} 431 * 432 * @param settings If null, use default parameter, i.e. full scan. 433 * @param workSource If null, all blame is given to the calling uid. 434 */ 435 public void startScan(ScanSettings settings, WorkSource workSource) { 436 enforceChangePermission(); 437 synchronized (this) { 438 if (mInIdleMode) { 439 // Need to send an immediate scan result broadcast in case the 440 // caller is waiting for a result .. 441 442 // clear calling identity to send broadcast 443 long callingIdentity = Binder.clearCallingIdentity(); 444 try { 445 mWifiStateMachine.sendScanResultsAvailableBroadcast(/* scanSucceeded = */ false); 446 } finally { 447 // restore calling identity 448 Binder.restoreCallingIdentity(callingIdentity); 449 } 450 mScanPending = true; 451 return; 452 } 453 } 454 if (settings != null) { 455 settings = new ScanSettings(settings); 456 if (!settings.isValid()) { 457 Slog.e(TAG, "invalid scan setting"); 458 return; 459 } 460 } 461 if (workSource != null) { 462 enforceWorkSourcePermission(); 463 // WifiManager currently doesn't use names, so need to clear names out of the 464 // supplied WorkSource to allow future WorkSource combining. 465 workSource.clearNames(); 466 } 467 mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++, 468 settings, workSource); 469 } 470 471 public String getWpsNfcConfigurationToken(int netId) { 472 enforceConnectivityInternalPermission(); 473 return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId); 474 } 475 476 boolean mInIdleMode; 477 boolean mScanPending; 478 479 void handleIdleModeChanged() { 480 boolean doScan = false; 481 synchronized (this) { 482 boolean idle = mPowerManager.isDeviceIdleMode(); 483 if (mInIdleMode != idle) { 484 mInIdleMode = idle; 485 if (!idle) { 486 if (mScanPending) { 487 mScanPending = false; 488 doScan = true; 489 } 490 } 491 } 492 } 493 if (doScan) { 494 // Someone requested a scan while we were idle; do a full scan now. 495 startScan(null, null); 496 } 497 } 498 499 private void enforceAccessPermission() { 500 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, 501 "WifiService"); 502 } 503 504 private void enforceChangePermission() { 505 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, 506 "WifiService"); 507 } 508 509 private void enforceLocationHardwarePermission() { 510 mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE, 511 "LocationHardware"); 512 } 513 514 private void enforceReadCredentialPermission() { 515 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL, 516 "WifiService"); 517 } 518 519 private void enforceWorkSourcePermission() { 520 mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS, 521 "WifiService"); 522 523 } 524 525 private void enforceMulticastChangePermission() { 526 mContext.enforceCallingOrSelfPermission( 527 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, 528 "WifiService"); 529 } 530 531 private void enforceConnectivityInternalPermission() { 532 mContext.enforceCallingOrSelfPermission( 533 android.Manifest.permission.CONNECTIVITY_INTERNAL, 534 "ConnectivityService"); 535 } 536 537 /** 538 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} 539 * @param enable {@code true} to enable, {@code false} to disable. 540 * @return {@code true} if the enable/disable operation was 541 * started or is already in the queue. 542 */ 543 public synchronized boolean setWifiEnabled(boolean enable) { 544 enforceChangePermission(); 545 Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid() 546 + ", uid=" + Binder.getCallingUid()); 547 548 /* 549 * Caller might not have WRITE_SECURE_SETTINGS, 550 * only CHANGE_WIFI_STATE is enforced 551 */ 552 553 long ident = Binder.clearCallingIdentity(); 554 try { 555 if (! mSettingsStore.handleWifiToggled(enable)) { 556 // Nothing to do if wifi cannot be toggled 557 return true; 558 } 559 } finally { 560 Binder.restoreCallingIdentity(ident); 561 } 562 563 mWifiController.sendMessage(CMD_WIFI_TOGGLED); 564 return true; 565 } 566 567 /** 568 * see {@link WifiManager#getWifiState()} 569 * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, 570 * {@link WifiManager#WIFI_STATE_DISABLING}, 571 * {@link WifiManager#WIFI_STATE_ENABLED}, 572 * {@link WifiManager#WIFI_STATE_ENABLING}, 573 * {@link WifiManager#WIFI_STATE_UNKNOWN} 574 */ 575 public int getWifiEnabledState() { 576 enforceAccessPermission(); 577 return mWifiStateMachine.syncGetWifiState(); 578 } 579 580 /** 581 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} 582 * @param wifiConfig SSID, security and channel details as 583 * part of WifiConfiguration 584 * @param enabled true to enable and false to disable 585 */ 586 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { 587 enforceChangePermission(); 588 ConnectivityManager.enforceTetherChangePermission(mContext); 589 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 590 throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user."); 591 } 592 // null wifiConfig is a meaningful input for CMD_SET_AP 593 if (wifiConfig == null || isValid(wifiConfig)) { 594 mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget(); 595 } else { 596 Slog.e(TAG, "Invalid WifiConfiguration"); 597 } 598 } 599 600 /** 601 * see {@link WifiManager#getWifiApState()} 602 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, 603 * {@link WifiManager#WIFI_AP_STATE_DISABLING}, 604 * {@link WifiManager#WIFI_AP_STATE_ENABLED}, 605 * {@link WifiManager#WIFI_AP_STATE_ENABLING}, 606 * {@link WifiManager#WIFI_AP_STATE_FAILED} 607 */ 608 public int getWifiApEnabledState() { 609 enforceAccessPermission(); 610 return mWifiStateMachine.syncGetWifiApState(); 611 } 612 613 /** 614 * see {@link WifiManager#getWifiApConfiguration()} 615 * @return soft access point configuration 616 */ 617 public WifiConfiguration getWifiApConfiguration() { 618 enforceAccessPermission(); 619 return mWifiStateMachine.syncGetWifiApConfiguration(); 620 } 621 622 /** 623 * see {@link WifiManager#buildWifiConfig()} 624 * @return a WifiConfiguration. 625 */ 626 public WifiConfiguration buildWifiConfig(String uriString, String mimeType, byte[] data) { 627 if (mimeType.equals(ConfigBuilder.WifiConfigType)) { 628 try { 629 return ConfigBuilder.buildConfig(uriString, data, mContext); 630 } 631 catch (IOException | GeneralSecurityException | SAXException e) { 632 Log.e(TAG, "Failed to parse wi-fi configuration: " + e); 633 } 634 } 635 else { 636 Log.i(TAG, "Unknown wi-fi config type: " + mimeType); 637 } 638 return null; 639 } 640 641 /** 642 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)} 643 * @param wifiConfig WifiConfiguration details for soft access point 644 */ 645 public void setWifiApConfiguration(WifiConfiguration wifiConfig) { 646 enforceChangePermission(); 647 if (wifiConfig == null) 648 return; 649 if (isValid(wifiConfig)) { 650 mWifiStateMachine.setWifiApConfiguration(wifiConfig); 651 } else { 652 Slog.e(TAG, "Invalid WifiConfiguration"); 653 } 654 } 655 656 /** 657 * @param enable {@code true} to enable, {@code false} to disable. 658 * @return {@code true} if the enable/disable operation was 659 * started or is already in the queue. 660 */ 661 public boolean isScanAlwaysAvailable() { 662 enforceAccessPermission(); 663 return mSettingsStore.isScanAlwaysAvailable(); 664 } 665 666 /** 667 * see {@link android.net.wifi.WifiManager#disconnect()} 668 */ 669 public void disconnect() { 670 enforceChangePermission(); 671 mWifiStateMachine.disconnectCommand(); 672 } 673 674 /** 675 * see {@link android.net.wifi.WifiManager#reconnect()} 676 */ 677 public void reconnect() { 678 enforceChangePermission(); 679 mWifiStateMachine.reconnectCommand(); 680 } 681 682 /** 683 * see {@link android.net.wifi.WifiManager#reassociate()} 684 */ 685 public void reassociate() { 686 enforceChangePermission(); 687 mWifiStateMachine.reassociateCommand(); 688 } 689 690 /** 691 * see {@link android.net.wifi.WifiManager#getSupportedFeatures} 692 */ 693 public int getSupportedFeatures() { 694 enforceAccessPermission(); 695 if (mWifiStateMachineChannel != null) { 696 return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel); 697 } else { 698 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 699 return 0; 700 } 701 } 702 703 /** 704 * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)} 705 */ 706 public WifiActivityEnergyInfo reportActivityInfo() { 707 enforceAccessPermission(); 708 if ((getSupportedFeatures() & WifiManager.WIFI_FEATURE_LINK_LAYER_STATS) == 0) { 709 return null; 710 } 711 WifiLinkLayerStats stats; 712 WifiActivityEnergyInfo energyInfo = null; 713 if (mWifiStateMachineChannel != null) { 714 stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel); 715 if (stats != null) { 716 final long rxIdleCurrent = mContext.getResources().getInteger( 717 com.android.internal.R.integer.config_wifi_idle_receive_cur_ma); 718 final long rxCurrent = mContext.getResources().getInteger( 719 com.android.internal.R.integer.config_wifi_active_rx_cur_ma); 720 final long txCurrent = mContext.getResources().getInteger( 721 com.android.internal.R.integer.config_wifi_tx_cur_ma); 722 final double voltage = mContext.getResources().getInteger( 723 com.android.internal.R.integer.config_wifi_operating_voltage_mv) 724 / 1000.0; 725 726 final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time; 727 final long energyUsed = (long)((stats.tx_time * txCurrent + 728 stats.rx_time * rxCurrent + 729 rxIdleTime * rxIdleCurrent) * voltage); 730 if (VDBG || rxIdleTime < 0 || stats.on_time < 0 || stats.tx_time < 0 || 731 stats.rx_time < 0 || energyUsed < 0) { 732 StringBuilder sb = new StringBuilder(); 733 sb.append(" rxIdleCur=" + rxIdleCurrent); 734 sb.append(" rxCur=" + rxCurrent); 735 sb.append(" txCur=" + txCurrent); 736 sb.append(" voltage=" + voltage); 737 sb.append(" on_time=" + stats.on_time); 738 sb.append(" tx_time=" + stats.tx_time); 739 sb.append(" rx_time=" + stats.rx_time); 740 sb.append(" rxIdleTime=" + rxIdleTime); 741 sb.append(" energy=" + energyUsed); 742 Log.d(TAG, " reportActivityInfo: " + sb.toString()); 743 } 744 745 // Convert the LinkLayerStats into EnergyActivity 746 energyInfo = new WifiActivityEnergyInfo(SystemClock.elapsedRealtime(), 747 WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time, 748 stats.rx_time, rxIdleTime, energyUsed); 749 } 750 if (energyInfo != null && energyInfo.isValid()) { 751 return energyInfo; 752 } else { 753 return null; 754 } 755 } else { 756 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 757 return null; 758 } 759 } 760 761 /** 762 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} 763 * @return the list of configured networks 764 */ 765 public List<WifiConfiguration> getConfiguredNetworks() { 766 enforceAccessPermission(); 767 if (mWifiStateMachineChannel != null) { 768 return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(), 769 mWifiStateMachineChannel); 770 } else { 771 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 772 return null; 773 } 774 } 775 776 /** 777 * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()} 778 * @return the list of configured networks with real preSharedKey 779 */ 780 public List<WifiConfiguration> getPrivilegedConfiguredNetworks() { 781 enforceReadCredentialPermission(); 782 enforceAccessPermission(); 783 if (mWifiStateMachineChannel != null) { 784 return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel); 785 } else { 786 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 787 return null; 788 } 789 } 790 791 /** 792 * Returns a WifiConfiguration matching this ScanResult 793 * @param scanResult scanResult that represents the BSSID 794 * @return {@link WifiConfiguration} that matches this BSSID or null 795 */ 796 public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) { 797 enforceAccessPermission(); 798 return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel); 799 } 800 801 /** 802 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} 803 * @return the supplicant-assigned identifier for the new or updated 804 * network if the operation succeeds, or {@code -1} if it fails 805 */ 806 public int addOrUpdateNetwork(WifiConfiguration config) { 807 enforceChangePermission(); 808 if (isValid(config) && isValidPasspoint(config)) { 809 810 WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig; 811 812 if (config.isPasspoint() && 813 (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS || 814 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS)) { 815 if (config.updateIdentifier != null) { 816 enforceAccessPermission(); 817 } 818 else { 819 try { 820 verifyCert(enterpriseConfig.getCaCertificate()); 821 } catch (CertPathValidatorException cpve) { 822 Slog.e(TAG, "CA Cert " + 823 enterpriseConfig.getCaCertificate().getSubjectX500Principal() + 824 " untrusted: " + cpve.getMessage()); 825 return -1; 826 } catch (GeneralSecurityException | IOException e) { 827 Slog.e(TAG, "Failed to verify certificate" + 828 enterpriseConfig.getCaCertificate().getSubjectX500Principal() + 829 ": " + e); 830 return -1; 831 } 832 } 833 } 834 835 //TODO: pass the Uid the WifiStateMachine as a message parameter 836 Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid()) 837 + " SSID " + config.SSID 838 + " nid=" + Integer.toString(config.networkId)); 839 if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) { 840 config.creatorUid = Binder.getCallingUid(); 841 } else { 842 config.lastUpdateUid = Binder.getCallingUid(); 843 } 844 if (mWifiStateMachineChannel != null) { 845 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config); 846 } else { 847 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 848 return -1; 849 } 850 } else { 851 Slog.e(TAG, "bad network configuration"); 852 return -1; 853 } 854 } 855 856 public static void verifyCert(X509Certificate caCert) 857 throws GeneralSecurityException, IOException { 858 CertificateFactory factory = CertificateFactory.getInstance("X.509"); 859 CertPathValidator validator = 860 CertPathValidator.getInstance(CertPathValidator.getDefaultType()); 861 CertPath path = factory.generateCertPath( 862 Arrays.asList(caCert)); 863 KeyStore ks = KeyStore.getInstance("AndroidCAStore"); 864 ks.load(null, null); 865 PKIXParameters params = new PKIXParameters(ks); 866 params.setRevocationEnabled(false); 867 validator.validate(path, params); 868 } 869 870 /** 871 * See {@link android.net.wifi.WifiManager#removeNetwork(int)} 872 * @param netId the integer that identifies the network configuration 873 * to the supplicant 874 * @return {@code true} if the operation succeeded 875 */ 876 public boolean removeNetwork(int netId) { 877 enforceChangePermission(); 878 879 if (mWifiStateMachineChannel != null) { 880 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId); 881 } else { 882 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 883 return false; 884 } 885 } 886 887 /** 888 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} 889 * @param netId the integer that identifies the network configuration 890 * to the supplicant 891 * @param disableOthers if true, disable all other networks. 892 * @return {@code true} if the operation succeeded 893 */ 894 public boolean enableNetwork(int netId, boolean disableOthers) { 895 enforceChangePermission(); 896 if (mWifiStateMachineChannel != null) { 897 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId, 898 disableOthers); 899 } else { 900 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 901 return false; 902 } 903 } 904 905 /** 906 * See {@link android.net.wifi.WifiManager#disableNetwork(int)} 907 * @param netId the integer that identifies the network configuration 908 * to the supplicant 909 * @return {@code true} if the operation succeeded 910 */ 911 public boolean disableNetwork(int netId) { 912 enforceChangePermission(); 913 if (mWifiStateMachineChannel != null) { 914 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId); 915 } else { 916 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 917 return false; 918 } 919 } 920 921 /** 922 * See {@link android.net.wifi.WifiManager#getConnectionInfo()} 923 * @return the Wi-Fi information, contained in {@link WifiInfo}. 924 */ 925 public WifiInfo getConnectionInfo() { 926 enforceAccessPermission(); 927 /* 928 * Make sure we have the latest information, by sending 929 * a status request to the supplicant. 930 */ 931 return mWifiStateMachine.syncRequestConnectionInfo(); 932 } 933 934 /** 935 * Return the results of the most recent access point scan, in the form of 936 * a list of {@link ScanResult} objects. 937 * @return the list of results 938 */ 939 public List<ScanResult> getScanResults(String callingPackage) { 940 enforceAccessPermission(); 941 int userId = UserHandle.getCallingUserId(); 942 int uid = Binder.getCallingUid(); 943 boolean canReadPeerMacAddresses = checkPeersMacAddress(); 944 boolean isActiveNetworkScorer = 945 NetworkScorerAppManager.isCallerActiveScorer(mContext, uid); 946 boolean hasInteractUsersFull = checkInteractAcrossUsersFull(); 947 long ident = Binder.clearCallingIdentity(); 948 try { 949 if (!canReadPeerMacAddresses && !isActiveNetworkScorer 950 && !isLocationEnabled(callingPackage)) { 951 return new ArrayList<ScanResult>(); 952 } 953 if (!canReadPeerMacAddresses && !isActiveNetworkScorer 954 && !checkCallerCanAccessScanResults(callingPackage, uid)) { 955 return new ArrayList<ScanResult>(); 956 } 957 if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage) 958 != AppOpsManager.MODE_ALLOWED) { 959 return new ArrayList<ScanResult>(); 960 } 961 if (!isCurrentProfile(userId) && !hasInteractUsersFull) { 962 return new ArrayList<ScanResult>(); 963 } 964 return mWifiStateMachine.syncGetScanResultsList(); 965 } finally { 966 Binder.restoreCallingIdentity(ident); 967 } 968 } 969 970 /** 971 * Add a Hotspot 2.0 release 2 Management Object 972 * @param mo The MO in XML form 973 * @return -1 for failure 974 */ 975 public int addPasspointManagementObject(String mo) { 976 return mWifiStateMachine.syncAddPasspointManagementObject(mWifiStateMachineChannel, mo); 977 } 978 979 /** 980 * Modify a Hotspot 2.0 release 2 Management Object 981 * @param fqdn The FQDN of the service provider 982 * @param mos A List of MO definitions to be updated 983 * @return the number of nodes updated, or -1 for failure 984 */ 985 public int modifyPasspointManagementObject(String fqdn, List<PasspointManagementObjectDefinition> mos) { 986 return mWifiStateMachine.syncModifyPasspointManagementObject(mWifiStateMachineChannel, fqdn, mos); 987 } 988 989 /** 990 * Query for a Hotspot 2.0 release 2 OSU icon 991 * @param bssid The BSSID of the AP 992 * @param fileName Icon file name 993 */ 994 public void queryPasspointIcon(long bssid, String fileName) { 995 mWifiStateMachine.syncQueryPasspointIcon(mWifiStateMachineChannel, bssid, fileName); 996 } 997 998 /** 999 * Match the currently associated network against the SP matching the given FQDN 1000 * @param fqdn FQDN of the SP 1001 * @return ordinal [HomeProvider, RoamingProvider, Incomplete, None, Declined] 1002 */ 1003 public int matchProviderWithCurrentNetwork(String fqdn) { 1004 return mWifiStateMachine.matchProviderWithCurrentNetwork(mWifiStateMachineChannel, fqdn); 1005 } 1006 1007 /** 1008 * Deauthenticate and set the re-authentication hold off time for the current network 1009 * @param holdoff hold off time in milliseconds 1010 * @param ess set if the hold off pertains to an ESS rather than a BSS 1011 */ 1012 public void deauthenticateNetwork(long holdoff, boolean ess) { 1013 mWifiStateMachine.deauthenticateNetwork(mWifiStateMachineChannel, holdoff, ess); 1014 } 1015 1016 private boolean isLocationEnabled(String callingPackage) { 1017 boolean legacyForegroundApp = !isMApp(mContext, callingPackage) 1018 && isForegroundApp(callingPackage); 1019 return legacyForegroundApp || Settings.Secure.getInt(mContext.getContentResolver(), 1020 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF) 1021 != Settings.Secure.LOCATION_MODE_OFF; 1022 } 1023 1024 /** 1025 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL. 1026 */ 1027 private boolean checkInteractAcrossUsersFull() { 1028 return mContext.checkCallingOrSelfPermission( 1029 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 1030 == PackageManager.PERMISSION_GRANTED; 1031 } 1032 1033 /** 1034 * Returns true if the caller holds PEERS_MAC_ADDRESS. 1035 */ 1036 private boolean checkPeersMacAddress() { 1037 return mContext.checkCallingOrSelfPermission( 1038 android.Manifest.permission.PEERS_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED; 1039 } 1040 1041 /** 1042 * Returns true if the calling user is the current one or a profile of the 1043 * current user.. 1044 */ 1045 private boolean isCurrentProfile(int userId) { 1046 int currentUser = ActivityManager.getCurrentUser(); 1047 if (userId == currentUser) { 1048 return true; 1049 } 1050 List<UserInfo> profiles = mUserManager.getProfiles(currentUser); 1051 for (UserInfo user : profiles) { 1052 if (userId == user.id) { 1053 return true; 1054 } 1055 } 1056 return false; 1057 } 1058 1059 /** 1060 * Tell the supplicant to persist the current list of configured networks. 1061 * @return {@code true} if the operation succeeded 1062 * 1063 * TODO: deprecate this 1064 */ 1065 public boolean saveConfiguration() { 1066 boolean result = true; 1067 enforceChangePermission(); 1068 if (mWifiStateMachineChannel != null) { 1069 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel); 1070 } else { 1071 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1072 return false; 1073 } 1074 } 1075 1076 /** 1077 * Set the country code 1078 * @param countryCode ISO 3166 country code. 1079 * @param persist {@code true} if the setting should be remembered. 1080 * 1081 * The persist behavior exists so that wifi can fall back to the last 1082 * persisted country code on a restart, when the locale information is 1083 * not available from telephony. 1084 */ 1085 public void setCountryCode(String countryCode, boolean persist) { 1086 Slog.i(TAG, "WifiService trying to set country code to " + countryCode + 1087 " with persist set to " + persist); 1088 enforceConnectivityInternalPermission(); 1089 final long token = Binder.clearCallingIdentity(); 1090 try { 1091 mCountryCode.setCountryCode(countryCode); 1092 } finally { 1093 Binder.restoreCallingIdentity(token); 1094 } 1095 } 1096 1097 /** 1098 * Get the country code 1099 * @return ISO 3166 country code. 1100 */ 1101 public String getCountryCode() { 1102 enforceConnectivityInternalPermission(); 1103 String country = mCountryCode.getCurrentCountryCode(); 1104 return country; 1105 } 1106 /** 1107 * Set the operational frequency band 1108 * @param band One of 1109 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, 1110 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, 1111 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, 1112 * @param persist {@code true} if the setting should be remembered. 1113 * 1114 */ 1115 public void setFrequencyBand(int band, boolean persist) { 1116 enforceChangePermission(); 1117 if (!isDualBandSupported()) return; 1118 Slog.i(TAG, "WifiService trying to set frequency band to " + band + 1119 " with persist set to " + persist); 1120 final long token = Binder.clearCallingIdentity(); 1121 try { 1122 mWifiStateMachine.setFrequencyBand(band, persist); 1123 } finally { 1124 Binder.restoreCallingIdentity(token); 1125 } 1126 } 1127 1128 1129 /** 1130 * Get the operational frequency band 1131 */ 1132 public int getFrequencyBand() { 1133 enforceAccessPermission(); 1134 return mWifiStateMachine.getFrequencyBand(); 1135 } 1136 1137 public boolean isDualBandSupported() { 1138 //TODO: Should move towards adding a driver API that checks at runtime 1139 return mContext.getResources().getBoolean( 1140 com.android.internal.R.bool.config_wifi_dual_band_support); 1141 } 1142 1143 /** 1144 * Return the DHCP-assigned addresses from the last successful DHCP request, 1145 * if any. 1146 * @return the DHCP information 1147 * @deprecated 1148 */ 1149 public DhcpInfo getDhcpInfo() { 1150 enforceAccessPermission(); 1151 DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults(); 1152 1153 DhcpInfo info = new DhcpInfo(); 1154 1155 if (dhcpResults.ipAddress != null && 1156 dhcpResults.ipAddress.getAddress() instanceof Inet4Address) { 1157 info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress()); 1158 } 1159 1160 if (dhcpResults.gateway != null) { 1161 info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway); 1162 } 1163 1164 int dnsFound = 0; 1165 for (InetAddress dns : dhcpResults.dnsServers) { 1166 if (dns instanceof Inet4Address) { 1167 if (dnsFound == 0) { 1168 info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1169 } else { 1170 info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1171 } 1172 if (++dnsFound > 1) break; 1173 } 1174 } 1175 Inet4Address serverAddress = dhcpResults.serverAddress; 1176 if (serverAddress != null) { 1177 info.serverAddress = NetworkUtils.inetAddressToInt(serverAddress); 1178 } 1179 info.leaseDuration = dhcpResults.leaseDuration; 1180 1181 return info; 1182 } 1183 1184 /** 1185 * see {@link android.net.wifi.WifiManager#addToBlacklist} 1186 * 1187 */ 1188 public void addToBlacklist(String bssid) { 1189 enforceChangePermission(); 1190 1191 mWifiStateMachine.addToBlacklist(bssid); 1192 } 1193 1194 /** 1195 * see {@link android.net.wifi.WifiManager#clearBlacklist} 1196 * 1197 */ 1198 public void clearBlacklist() { 1199 enforceChangePermission(); 1200 1201 mWifiStateMachine.clearBlacklist(); 1202 } 1203 1204 /** 1205 * enable TDLS for the local NIC to remote NIC 1206 * The APPs don't know the remote MAC address to identify NIC though, 1207 * so we need to do additional work to find it from remote IP address 1208 */ 1209 1210 class TdlsTaskParams { 1211 public String remoteIpAddress; 1212 public boolean enable; 1213 } 1214 1215 class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> { 1216 @Override 1217 protected Integer doInBackground(TdlsTaskParams... params) { 1218 1219 // Retrieve parameters for the call 1220 TdlsTaskParams param = params[0]; 1221 String remoteIpAddress = param.remoteIpAddress.trim(); 1222 boolean enable = param.enable; 1223 1224 // Get MAC address of Remote IP 1225 String macAddress = null; 1226 1227 BufferedReader reader = null; 1228 1229 try { 1230 reader = new BufferedReader(new FileReader("/proc/net/arp")); 1231 1232 // Skip over the line bearing colum titles 1233 String line = reader.readLine(); 1234 1235 while ((line = reader.readLine()) != null) { 1236 String[] tokens = line.split("[ ]+"); 1237 if (tokens.length < 6) { 1238 continue; 1239 } 1240 1241 // ARP column format is 1242 // Address HWType HWAddress Flags Mask IFace 1243 String ip = tokens[0]; 1244 String mac = tokens[3]; 1245 1246 if (remoteIpAddress.equals(ip)) { 1247 macAddress = mac; 1248 break; 1249 } 1250 } 1251 1252 if (macAddress == null) { 1253 Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " + 1254 "/proc/net/arp"); 1255 } else { 1256 enableTdlsWithMacAddress(macAddress, enable); 1257 } 1258 1259 } catch (FileNotFoundException e) { 1260 Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address"); 1261 } catch (IOException e) { 1262 Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address"); 1263 } finally { 1264 try { 1265 if (reader != null) { 1266 reader.close(); 1267 } 1268 } 1269 catch (IOException e) { 1270 // Do nothing 1271 } 1272 } 1273 1274 return 0; 1275 } 1276 } 1277 1278 public void enableTdls(String remoteAddress, boolean enable) { 1279 if (remoteAddress == null) { 1280 throw new IllegalArgumentException("remoteAddress cannot be null"); 1281 } 1282 1283 TdlsTaskParams params = new TdlsTaskParams(); 1284 params.remoteIpAddress = remoteAddress; 1285 params.enable = enable; 1286 new TdlsTask().execute(params); 1287 } 1288 1289 1290 public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) { 1291 if (remoteMacAddress == null) { 1292 throw new IllegalArgumentException("remoteMacAddress cannot be null"); 1293 } 1294 1295 mWifiStateMachine.enableTdls(remoteMacAddress, enable); 1296 } 1297 1298 /** 1299 * Get a reference to handler. This is used by a client to establish 1300 * an AsyncChannel communication with WifiService 1301 */ 1302 public Messenger getWifiServiceMessenger() { 1303 enforceAccessPermission(); 1304 enforceChangePermission(); 1305 return new Messenger(mClientHandler); 1306 } 1307 1308 /** 1309 * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer 1310 */ 1311 public void disableEphemeralNetwork(String SSID) { 1312 enforceAccessPermission(); 1313 enforceChangePermission(); 1314 mWifiStateMachine.disableEphemeralNetwork(SSID); 1315 } 1316 1317 /** 1318 * Get the IP and proxy configuration file 1319 */ 1320 public String getConfigFile() { 1321 enforceAccessPermission(); 1322 return mWifiStateMachine.getConfigFile(); 1323 } 1324 1325 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 1326 @Override 1327 public void onReceive(Context context, Intent intent) { 1328 String action = intent.getAction(); 1329 if (action.equals(Intent.ACTION_SCREEN_ON)) { 1330 mWifiController.sendMessage(CMD_SCREEN_ON); 1331 } else if (action.equals(Intent.ACTION_USER_PRESENT)) { 1332 mWifiController.sendMessage(CMD_USER_PRESENT); 1333 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { 1334 mWifiController.sendMessage(CMD_SCREEN_OFF); 1335 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 1336 int pluggedType = intent.getIntExtra("plugged", 0); 1337 mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null); 1338 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 1339 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 1340 BluetoothAdapter.STATE_DISCONNECTED); 1341 mWifiStateMachine.sendBluetoothAdapterStateChange(state); 1342 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { 1343 boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false); 1344 mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0); 1345 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED)) { 1346 boolean inCall = intent.getBooleanExtra(PhoneConstants.PHONE_IN_EMERGENCY_CALL, false); 1347 mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, inCall ? 1 : 0, 0); 1348 } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) { 1349 handleIdleModeChanged(); 1350 } 1351 } 1352 }; 1353 1354 /** 1355 * Observes settings changes to scan always mode. 1356 */ 1357 private void registerForScanModeChange() { 1358 ContentObserver contentObserver = new ContentObserver(null) { 1359 @Override 1360 public void onChange(boolean selfChange) { 1361 mSettingsStore.handleWifiScanAlwaysAvailableToggled(); 1362 mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED); 1363 } 1364 }; 1365 1366 mContext.getContentResolver().registerContentObserver( 1367 Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE), 1368 false, contentObserver); 1369 } 1370 1371 private void registerForBroadcasts() { 1372 IntentFilter intentFilter = new IntentFilter(); 1373 intentFilter.addAction(Intent.ACTION_SCREEN_ON); 1374 intentFilter.addAction(Intent.ACTION_USER_PRESENT); 1375 intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 1376 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 1377 intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 1378 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 1379 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); 1380 intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); 1381 1382 boolean trackEmergencyCallState = mContext.getResources().getBoolean( 1383 com.android.internal.R.bool.config_wifi_turn_off_during_emergency_call); 1384 if (trackEmergencyCallState) { 1385 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED); 1386 } 1387 1388 mContext.registerReceiver(mReceiver, intentFilter); 1389 } 1390 1391 private void registerForPackageOrUserRemoval() { 1392 IntentFilter intentFilter = new IntentFilter(); 1393 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 1394 intentFilter.addAction(Intent.ACTION_USER_REMOVED); 1395 mContext.registerReceiverAsUser(new BroadcastReceiver() { 1396 @Override 1397 public void onReceive(Context context, Intent intent) { 1398 switch (intent.getAction()) { 1399 case Intent.ACTION_PACKAGE_REMOVED: { 1400 if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 1401 return; 1402 } 1403 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); 1404 Uri uri = intent.getData(); 1405 if (uid == -1 || uri == null) { 1406 return; 1407 } 1408 String pkgName = uri.getSchemeSpecificPart(); 1409 mWifiStateMachine.removeAppConfigs(pkgName, uid); 1410 break; 1411 } 1412 case Intent.ACTION_USER_REMOVED: { 1413 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); 1414 mWifiStateMachine.removeUserConfigs(userHandle); 1415 break; 1416 } 1417 } 1418 } 1419 }, UserHandle.ALL, intentFilter, null, null); 1420 } 1421 1422 @Override 1423 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1424 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 1425 != PackageManager.PERMISSION_GRANTED) { 1426 pw.println("Permission Denial: can't dump WifiService from from pid=" 1427 + Binder.getCallingPid() 1428 + ", uid=" + Binder.getCallingUid()); 1429 return; 1430 } 1431 if (args.length > 0 && WifiMetrics.PROTO_DUMP_ARG.equals(args[0])) { 1432 // WifiMetrics proto bytes were requested. Dump only these. 1433 mWifiStateMachine.updateWifiMetrics(); 1434 mWifiMetrics.dump(fd, pw, args); 1435 } else if (args.length > 0 && IpManager.DUMP_ARG.equals(args[0])) { 1436 // IpManager dump was requested. Pass it along and take no further action. 1437 String[] ipManagerArgs = new String[args.length - 1]; 1438 System.arraycopy(args, 1, ipManagerArgs, 0, ipManagerArgs.length); 1439 mWifiStateMachine.dumpIpManager(fd, pw, ipManagerArgs); 1440 } else { 1441 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName()); 1442 pw.println("Stay-awake conditions: " + 1443 Settings.Global.getInt(mContext.getContentResolver(), 1444 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0)); 1445 pw.println("mMulticastEnabled " + mMulticastEnabled); 1446 pw.println("mMulticastDisabled " + mMulticastDisabled); 1447 pw.println("mInIdleMode " + mInIdleMode); 1448 pw.println("mScanPending " + mScanPending); 1449 mWifiController.dump(fd, pw, args); 1450 mSettingsStore.dump(fd, pw, args); 1451 mNotificationController.dump(fd, pw, args); 1452 mTrafficPoller.dump(fd, pw, args); 1453 1454 pw.println("Latest scan results:"); 1455 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList(); 1456 long nowMs = System.currentTimeMillis(); 1457 if (scanResults != null && scanResults.size() != 0) { 1458 pw.println(" BSSID Frequency RSSI Age SSID " + 1459 " Flags"); 1460 for (ScanResult r : scanResults) { 1461 long ageSec = 0; 1462 long ageMilli = 0; 1463 if (nowMs > r.seen && r.seen > 0) { 1464 ageSec = (nowMs - r.seen) / 1000; 1465 ageMilli = (nowMs - r.seen) % 1000; 1466 } 1467 String candidate = " "; 1468 if (r.isAutoJoinCandidate > 0) candidate = "+"; 1469 pw.printf(" %17s %9d %5d %3d.%03d%s %-32s %s\n", 1470 r.BSSID, 1471 r.frequency, 1472 r.level, 1473 ageSec, ageMilli, 1474 candidate, 1475 r.SSID == null ? "" : r.SSID, 1476 r.capabilities); 1477 } 1478 } 1479 pw.println(); 1480 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " + 1481 mFullHighPerfLocksAcquired + " full high perf, " + 1482 mScanLocksAcquired + " scan"); 1483 pw.println("Locks released: " + mFullLocksReleased + " full, " + 1484 mFullHighPerfLocksReleased + " full high perf, " + 1485 mScanLocksReleased + " scan"); 1486 pw.println(); 1487 pw.println("Locks held:"); 1488 mLocks.dump(pw); 1489 1490 pw.println("Multicast Locks held:"); 1491 for (Multicaster l : mMulticasters) { 1492 pw.print(" "); 1493 pw.println(l); 1494 } 1495 1496 pw.println(); 1497 mWifiStateMachine.dump(fd, pw, args); 1498 pw.println(); 1499 } 1500 } 1501 1502 private class WifiLock extends DeathRecipient { 1503 int mMode; 1504 WorkSource mWorkSource; 1505 1506 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) { 1507 super(tag, binder); 1508 mMode = lockMode; 1509 mWorkSource = ws; 1510 } 1511 1512 public void binderDied() { 1513 synchronized (mLocks) { 1514 releaseWifiLockLocked(mBinder); 1515 } 1516 } 1517 1518 public String toString() { 1519 return "WifiLock{" + mTag + " type=" + mMode + " uid=" + mUid + "}"; 1520 } 1521 } 1522 1523 public class LockList { 1524 private List<WifiLock> mList; 1525 1526 private LockList() { 1527 mList = new ArrayList<WifiLock>(); 1528 } 1529 1530 synchronized boolean hasLocks() { 1531 return !mList.isEmpty(); 1532 } 1533 1534 synchronized int getStrongestLockMode() { 1535 if (mList.isEmpty()) { 1536 return WifiManager.WIFI_MODE_FULL; 1537 } 1538 1539 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) { 1540 return WifiManager.WIFI_MODE_FULL_HIGH_PERF; 1541 } 1542 1543 if (mFullLocksAcquired > mFullLocksReleased) { 1544 return WifiManager.WIFI_MODE_FULL; 1545 } 1546 1547 return WifiManager.WIFI_MODE_SCAN_ONLY; 1548 } 1549 1550 synchronized void updateWorkSource(WorkSource ws) { 1551 for (int i = 0; i < mLocks.mList.size(); i++) { 1552 ws.add(mLocks.mList.get(i).mWorkSource); 1553 } 1554 } 1555 1556 private void addLock(WifiLock lock) { 1557 if (findLockByBinder(lock.mBinder) < 0) { 1558 mList.add(lock); 1559 } 1560 } 1561 1562 private WifiLock removeLock(IBinder binder) { 1563 int index = findLockByBinder(binder); 1564 if (index >= 0) { 1565 WifiLock ret = mList.remove(index); 1566 ret.unlinkDeathRecipient(); 1567 return ret; 1568 } else { 1569 return null; 1570 } 1571 } 1572 1573 private int findLockByBinder(IBinder binder) { 1574 int size = mList.size(); 1575 for (int i = size - 1; i >= 0; i--) { 1576 if (mList.get(i).mBinder == binder) 1577 return i; 1578 } 1579 return -1; 1580 } 1581 1582 private void dump(PrintWriter pw) { 1583 for (WifiLock l : mList) { 1584 pw.print(" "); 1585 pw.println(l); 1586 } 1587 } 1588 } 1589 1590 void enforceWakeSourcePermission(int uid, int pid) { 1591 if (uid == android.os.Process.myUid()) { 1592 return; 1593 } 1594 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, 1595 pid, uid, null); 1596 } 1597 1598 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) { 1599 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); 1600 if (lockMode != WifiManager.WIFI_MODE_FULL && 1601 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY && 1602 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) { 1603 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode); 1604 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode); 1605 return false; 1606 } 1607 if (ws != null && ws.size() == 0) { 1608 ws = null; 1609 } 1610 if (ws != null) { 1611 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid()); 1612 } 1613 if (ws == null) { 1614 ws = new WorkSource(Binder.getCallingUid()); 1615 } 1616 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws); 1617 synchronized (mLocks) { 1618 return acquireWifiLockLocked(wifiLock); 1619 } 1620 } 1621 1622 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException { 1623 switch(wifiLock.mMode) { 1624 case WifiManager.WIFI_MODE_FULL: 1625 case WifiManager.WIFI_MODE_FULL_HIGH_PERF: 1626 case WifiManager.WIFI_MODE_SCAN_ONLY: 1627 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource); 1628 break; 1629 } 1630 } 1631 1632 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException { 1633 switch(wifiLock.mMode) { 1634 case WifiManager.WIFI_MODE_FULL: 1635 case WifiManager.WIFI_MODE_FULL_HIGH_PERF: 1636 case WifiManager.WIFI_MODE_SCAN_ONLY: 1637 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource); 1638 break; 1639 } 1640 } 1641 1642 private boolean acquireWifiLockLocked(WifiLock wifiLock) { 1643 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock); 1644 1645 mLocks.addLock(wifiLock); 1646 1647 long ident = Binder.clearCallingIdentity(); 1648 try { 1649 noteAcquireWifiLock(wifiLock); 1650 switch(wifiLock.mMode) { 1651 case WifiManager.WIFI_MODE_FULL: 1652 ++mFullLocksAcquired; 1653 break; 1654 case WifiManager.WIFI_MODE_FULL_HIGH_PERF: 1655 ++mFullHighPerfLocksAcquired; 1656 break; 1657 1658 case WifiManager.WIFI_MODE_SCAN_ONLY: 1659 ++mScanLocksAcquired; 1660 break; 1661 } 1662 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 1663 return true; 1664 } catch (RemoteException e) { 1665 return false; 1666 } finally { 1667 Binder.restoreCallingIdentity(ident); 1668 } 1669 } 1670 1671 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) { 1672 int uid = Binder.getCallingUid(); 1673 int pid = Binder.getCallingPid(); 1674 if (ws != null && ws.size() == 0) { 1675 ws = null; 1676 } 1677 if (ws != null) { 1678 enforceWakeSourcePermission(uid, pid); 1679 } 1680 long ident = Binder.clearCallingIdentity(); 1681 try { 1682 synchronized (mLocks) { 1683 int index = mLocks.findLockByBinder(lock); 1684 if (index < 0) { 1685 throw new IllegalArgumentException("Wifi lock not active"); 1686 } 1687 WifiLock wl = mLocks.mList.get(index); 1688 noteReleaseWifiLock(wl); 1689 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid); 1690 noteAcquireWifiLock(wl); 1691 } 1692 } catch (RemoteException e) { 1693 } finally { 1694 Binder.restoreCallingIdentity(ident); 1695 } 1696 } 1697 1698 public boolean releaseWifiLock(IBinder lock) { 1699 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); 1700 synchronized (mLocks) { 1701 return releaseWifiLockLocked(lock); 1702 } 1703 } 1704 1705 private boolean releaseWifiLockLocked(IBinder lock) { 1706 boolean hadLock; 1707 1708 WifiLock wifiLock = mLocks.removeLock(lock); 1709 1710 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock); 1711 1712 hadLock = (wifiLock != null); 1713 1714 long ident = Binder.clearCallingIdentity(); 1715 try { 1716 if (hadLock) { 1717 noteReleaseWifiLock(wifiLock); 1718 switch(wifiLock.mMode) { 1719 case WifiManager.WIFI_MODE_FULL: 1720 ++mFullLocksReleased; 1721 break; 1722 case WifiManager.WIFI_MODE_FULL_HIGH_PERF: 1723 ++mFullHighPerfLocksReleased; 1724 break; 1725 case WifiManager.WIFI_MODE_SCAN_ONLY: 1726 ++mScanLocksReleased; 1727 break; 1728 } 1729 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 1730 } 1731 } catch (RemoteException e) { 1732 } finally { 1733 Binder.restoreCallingIdentity(ident); 1734 } 1735 1736 return hadLock; 1737 } 1738 1739 private abstract class DeathRecipient 1740 implements IBinder.DeathRecipient { 1741 String mTag; 1742 int mUid; 1743 IBinder mBinder; 1744 1745 DeathRecipient(String tag, IBinder binder) { 1746 super(); 1747 mTag = tag; 1748 mUid = Binder.getCallingUid(); 1749 mBinder = binder; 1750 try { 1751 mBinder.linkToDeath(this, 0); 1752 } catch (RemoteException e) { 1753 binderDied(); 1754 } 1755 } 1756 1757 void unlinkDeathRecipient() { 1758 mBinder.unlinkToDeath(this, 0); 1759 } 1760 1761 public int getUid() { 1762 return mUid; 1763 } 1764 } 1765 1766 private class Multicaster extends DeathRecipient { 1767 Multicaster(String tag, IBinder binder) { 1768 super(tag, binder); 1769 } 1770 1771 public void binderDied() { 1772 Slog.e(TAG, "Multicaster binderDied"); 1773 synchronized (mMulticasters) { 1774 int i = mMulticasters.indexOf(this); 1775 if (i != -1) { 1776 removeMulticasterLocked(i, mUid); 1777 } 1778 } 1779 } 1780 1781 public String toString() { 1782 return "Multicaster{" + mTag + " uid=" + mUid + "}"; 1783 } 1784 } 1785 1786 public void initializeMulticastFiltering() { 1787 enforceMulticastChangePermission(); 1788 1789 synchronized (mMulticasters) { 1790 // if anybody had requested filters be off, leave off 1791 if (mMulticasters.size() != 0) { 1792 return; 1793 } else { 1794 mWifiStateMachine.startFilteringMulticastPackets(); 1795 } 1796 } 1797 } 1798 1799 public void acquireMulticastLock(IBinder binder, String tag) { 1800 enforceMulticastChangePermission(); 1801 1802 synchronized (mMulticasters) { 1803 mMulticastEnabled++; 1804 mMulticasters.add(new Multicaster(tag, binder)); 1805 // Note that we could call stopFilteringMulticastPackets only when 1806 // our new size == 1 (first call), but this function won't 1807 // be called often and by making the stopPacket call each 1808 // time we're less fragile and self-healing. 1809 mWifiStateMachine.stopFilteringMulticastPackets(); 1810 } 1811 1812 int uid = Binder.getCallingUid(); 1813 final long ident = Binder.clearCallingIdentity(); 1814 try { 1815 mBatteryStats.noteWifiMulticastEnabled(uid); 1816 } catch (RemoteException e) { 1817 } finally { 1818 Binder.restoreCallingIdentity(ident); 1819 } 1820 } 1821 1822 public void releaseMulticastLock() { 1823 enforceMulticastChangePermission(); 1824 1825 int uid = Binder.getCallingUid(); 1826 synchronized (mMulticasters) { 1827 mMulticastDisabled++; 1828 int size = mMulticasters.size(); 1829 for (int i = size - 1; i >= 0; i--) { 1830 Multicaster m = mMulticasters.get(i); 1831 if ((m != null) && (m.getUid() == uid)) { 1832 removeMulticasterLocked(i, uid); 1833 } 1834 } 1835 } 1836 } 1837 1838 private void removeMulticasterLocked(int i, int uid) 1839 { 1840 Multicaster removed = mMulticasters.remove(i); 1841 1842 if (removed != null) { 1843 removed.unlinkDeathRecipient(); 1844 } 1845 if (mMulticasters.size() == 0) { 1846 mWifiStateMachine.startFilteringMulticastPackets(); 1847 } 1848 1849 final long ident = Binder.clearCallingIdentity(); 1850 try { 1851 mBatteryStats.noteWifiMulticastDisabled(uid); 1852 } catch (RemoteException e) { 1853 } finally { 1854 Binder.restoreCallingIdentity(ident); 1855 } 1856 } 1857 1858 public boolean isMulticastEnabled() { 1859 enforceAccessPermission(); 1860 1861 synchronized (mMulticasters) { 1862 return (mMulticasters.size() > 0); 1863 } 1864 } 1865 1866 public void enableVerboseLogging(int verbose) { 1867 enforceAccessPermission(); 1868 mWifiStateMachine.enableVerboseLogging(verbose); 1869 } 1870 1871 public int getVerboseLoggingLevel() { 1872 enforceAccessPermission(); 1873 return mWifiStateMachine.getVerboseLoggingLevel(); 1874 } 1875 1876 public void enableAggressiveHandover(int enabled) { 1877 enforceAccessPermission(); 1878 mWifiStateMachine.enableAggressiveHandover(enabled); 1879 } 1880 1881 public int getAggressiveHandover() { 1882 enforceAccessPermission(); 1883 return mWifiStateMachine.getAggressiveHandover(); 1884 } 1885 1886 public void setAllowScansWithTraffic(int enabled) { 1887 enforceAccessPermission(); 1888 mWifiStateMachine.setAllowScansWithTraffic(enabled); 1889 } 1890 1891 public int getAllowScansWithTraffic() { 1892 enforceAccessPermission(); 1893 return mWifiStateMachine.getAllowScansWithTraffic(); 1894 } 1895 1896 public boolean enableAutoJoinWhenAssociated(boolean enabled) { 1897 enforceChangePermission(); 1898 return mWifiStateMachine.enableAutoJoinWhenAssociated(enabled); 1899 } 1900 1901 public boolean getEnableAutoJoinWhenAssociated() { 1902 enforceAccessPermission(); 1903 return mWifiStateMachine.getEnableAutoJoinWhenAssociated(); 1904 } 1905 public void setHalBasedAutojoinOffload(int enabled) { 1906 enforceConnectivityInternalPermission(); 1907 mWifiStateMachine.setHalBasedAutojoinOffload(enabled); 1908 } 1909 1910 public int getHalBasedAutojoinOffload() { 1911 enforceAccessPermission(); 1912 return mWifiStateMachine.getHalBasedAutojoinOffload(); 1913 } 1914 1915 /* Return the Wifi Connection statistics object */ 1916 public WifiConnectionStatistics getConnectionStatistics() { 1917 enforceAccessPermission(); 1918 enforceReadCredentialPermission(); 1919 if (mWifiStateMachineChannel != null) { 1920 return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel); 1921 } else { 1922 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1923 return null; 1924 } 1925 } 1926 1927 public void factoryReset() { 1928 enforceConnectivityInternalPermission(); 1929 1930 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { 1931 return; 1932 } 1933 1934 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 1935 // Turn mobile hotspot off 1936 setWifiApEnabled(null, false); 1937 } 1938 1939 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) { 1940 // Enable wifi 1941 setWifiEnabled(true); 1942 // Delete all Wifi SSIDs 1943 List<WifiConfiguration> networks = getConfiguredNetworks(); 1944 if (networks != null) { 1945 for (WifiConfiguration config : networks) { 1946 removeNetwork(config.networkId); 1947 } 1948 saveConfiguration(); 1949 } 1950 } 1951 } 1952 1953 /* private methods */ 1954 static boolean logAndReturnFalse(String s) { 1955 Log.d(TAG, s); 1956 return false; 1957 } 1958 1959 public static boolean isValid(WifiConfiguration config) { 1960 String validity = checkValidity(config); 1961 return validity == null || logAndReturnFalse(validity); 1962 } 1963 1964 public static boolean isValidPasspoint(WifiConfiguration config) { 1965 String validity = checkPasspointValidity(config); 1966 return validity == null || logAndReturnFalse(validity); 1967 } 1968 1969 public static String checkValidity(WifiConfiguration config) { 1970 if (config.allowedKeyManagement == null) 1971 return "allowed kmgmt"; 1972 1973 if (config.allowedKeyManagement.cardinality() > 1) { 1974 if (config.allowedKeyManagement.cardinality() != 2) { 1975 return "cardinality != 2"; 1976 } 1977 if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) { 1978 return "not WPA_EAP"; 1979 } 1980 if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) 1981 && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) { 1982 return "not PSK or 8021X"; 1983 } 1984 } 1985 return null; 1986 } 1987 1988 public static String checkPasspointValidity(WifiConfiguration config) { 1989 if (!TextUtils.isEmpty(config.FQDN)) { 1990 /* this is passpoint configuration; it must not have an SSID */ 1991 if (!TextUtils.isEmpty(config.SSID)) { 1992 return "SSID not expected for Passpoint: '" + config.SSID + 1993 "' FQDN " + toHexString(config.FQDN); 1994 } 1995 /* this is passpoint configuration; it must have a providerFriendlyName */ 1996 if (TextUtils.isEmpty(config.providerFriendlyName)) { 1997 return "no provider friendly name"; 1998 } 1999 WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig; 2000 /* this is passpoint configuration; it must have enterprise config */ 2001 if (enterpriseConfig == null 2002 || enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) { 2003 return "no enterprise config"; 2004 } 2005 if ((enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS || 2006 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS || 2007 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.PEAP) && 2008 enterpriseConfig.getCaCertificate() == null) { 2009 return "no CA certificate"; 2010 } 2011 } 2012 return null; 2013 } 2014 2015 public Network getCurrentNetwork() { 2016 enforceAccessPermission(); 2017 return mWifiStateMachine.getCurrentNetwork(); 2018 } 2019 2020 public static String toHexString(String s) { 2021 if (s == null) { 2022 return "null"; 2023 } 2024 StringBuilder sb = new StringBuilder(); 2025 sb.append('\'').append(s).append('\''); 2026 for (int n = 0; n < s.length(); n++) { 2027 sb.append(String.format(" %02x", s.charAt(n) & 0xffff)); 2028 } 2029 return sb.toString(); 2030 } 2031 2032 /** 2033 * Checks that calling process has android.Manifest.permission.ACCESS_COARSE_LOCATION or 2034 * android.Manifest.permission.ACCESS_FINE_LOCATION and a corresponding app op is allowed 2035 */ 2036 private boolean checkCallerCanAccessScanResults(String callingPackage, int uid) { 2037 if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_FINE_LOCATION, uid) 2038 == PackageManager.PERMISSION_GRANTED 2039 && checkAppOppAllowed(AppOpsManager.OP_FINE_LOCATION, callingPackage, uid)) { 2040 return true; 2041 } 2042 2043 if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_COARSE_LOCATION, uid) 2044 == PackageManager.PERMISSION_GRANTED 2045 && checkAppOppAllowed(AppOpsManager.OP_COARSE_LOCATION, callingPackage, uid)) { 2046 return true; 2047 } 2048 boolean apiLevel23App = isMApp(mContext, callingPackage); 2049 // Pre-M apps running in the foreground should continue getting scan results 2050 if (!apiLevel23App && isForegroundApp(callingPackage)) { 2051 return true; 2052 } 2053 Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION " 2054 + "permission to get scan results"); 2055 return false; 2056 } 2057 2058 private boolean checkAppOppAllowed(int op, String callingPackage, int uid) { 2059 return mAppOps.noteOp(op, uid, callingPackage) == AppOpsManager.MODE_ALLOWED; 2060 } 2061 2062 private static boolean isMApp(Context context, String pkgName) { 2063 try { 2064 return context.getPackageManager().getApplicationInfo(pkgName, 0) 2065 .targetSdkVersion >= Build.VERSION_CODES.M; 2066 } catch (PackageManager.NameNotFoundException e) { 2067 // In case of exception, assume M app (more strict checking) 2068 } 2069 return true; 2070 } 2071 2072 public void hideCertFromUnaffiliatedUsers(String alias) { 2073 mCertManager.hideCertFromUnaffiliatedUsers(alias); 2074 } 2075 2076 public String[] listClientCertsForCurrentUser() { 2077 return mCertManager.listClientCertsForCurrentUser(); 2078 } 2079 2080 /** 2081 * Return true if the specified package name is a foreground app. 2082 * 2083 * @param pkgName application package name. 2084 */ 2085 private boolean isForegroundApp(String pkgName) { 2086 ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE); 2087 List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); 2088 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName()); 2089 } 2090 2091} 2092