WifiServiceImpl.java revision 3f8483262cf59572d0c0b3c42e72d8ce65a096ff
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        // If SoftAp is enabled, only Settings is allowed to toggle wifi
784        boolean apEnabled =
785                mWifiStateMachine.syncGetWifiApState() != WifiManager.WIFI_AP_STATE_DISABLED;
786        boolean isFromSettings =
787                mWifiPermissionsUtil.checkNetworkSettingsPermission(Binder.getCallingUid());
788        if (apEnabled && !isFromSettings) {
789            mLog.trace("setWifiEnabled SoftAp not disabled: only Settings can enable wifi").flush();
790            return false;
791        }
792
793        /*
794        * Caller might not have WRITE_SECURE_SETTINGS,
795        * only CHANGE_WIFI_STATE is enforced
796        */
797        long ident = Binder.clearCallingIdentity();
798        try {
799            if (! mSettingsStore.handleWifiToggled(enable)) {
800                // Nothing to do if wifi cannot be toggled
801                return true;
802            }
803        } finally {
804            Binder.restoreCallingIdentity(ident);
805        }
806
807
808        if (mPermissionReviewRequired) {
809            final int wiFiEnabledState = getWifiEnabledState();
810            if (enable) {
811                if (wiFiEnabledState == WifiManager.WIFI_STATE_DISABLING
812                        || wiFiEnabledState == WifiManager.WIFI_STATE_DISABLED) {
813                    if (startConsentUi(packageName, Binder.getCallingUid(),
814                            WifiManager.ACTION_REQUEST_ENABLE)) {
815                        return true;
816                    }
817                }
818            } else if (wiFiEnabledState == WifiManager.WIFI_STATE_ENABLING
819                    || wiFiEnabledState == WifiManager.WIFI_STATE_ENABLED) {
820                if (startConsentUi(packageName, Binder.getCallingUid(),
821                        WifiManager.ACTION_REQUEST_DISABLE)) {
822                    return true;
823                }
824            }
825        }
826
827        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
828        return true;
829    }
830
831    /**
832     * see {@link WifiManager#getWifiState()}
833     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
834     *         {@link WifiManager#WIFI_STATE_DISABLING},
835     *         {@link WifiManager#WIFI_STATE_ENABLED},
836     *         {@link WifiManager#WIFI_STATE_ENABLING},
837     *         {@link WifiManager#WIFI_STATE_UNKNOWN}
838     */
839    @Override
840    public int getWifiEnabledState() {
841        enforceAccessPermission();
842        mLog.trace("getWifiEnabledState uid=%").c(Binder.getCallingUid()).flush();
843        return mWifiStateMachine.syncGetWifiState();
844    }
845
846    /**
847     * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
848     * @param wifiConfig SSID, security and channel details as
849     *        part of WifiConfiguration
850     * @param enabled true to enable and false to disable
851     */
852    @Override
853    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
854        enforceChangePermission();
855        mWifiPermissionsUtil.enforceTetherChangePermission(mContext);
856
857        mLog.trace("setWifiApEnabled uid=% enable=%").c(Binder.getCallingUid()).c(enabled).flush();
858
859        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
860            throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
861        }
862        // null wifiConfig is a meaningful input for CMD_SET_AP
863        if (wifiConfig == null || isValid(wifiConfig)) {
864            int mode = WifiManager.IFACE_IP_MODE_UNSPECIFIED;
865            SoftApModeConfiguration softApConfig = new SoftApModeConfiguration(mode, wifiConfig);
866            mWifiController.sendMessage(CMD_SET_AP, enabled ? 1 : 0, 0, softApConfig);
867        } else {
868            Slog.e(TAG, "Invalid WifiConfiguration");
869        }
870    }
871
872    /**
873     * see {@link WifiManager#getWifiApState()}
874     * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
875     *         {@link WifiManager#WIFI_AP_STATE_DISABLING},
876     *         {@link WifiManager#WIFI_AP_STATE_ENABLED},
877     *         {@link WifiManager#WIFI_AP_STATE_ENABLING},
878     *         {@link WifiManager#WIFI_AP_STATE_FAILED}
879     */
880    @Override
881    public int getWifiApEnabledState() {
882        enforceAccessPermission();
883        mLog.trace("getWifiApEnabledState uid=%").c(Binder.getCallingUid()).flush();
884        return mWifiStateMachine.syncGetWifiApState();
885    }
886
887    /**
888     * see {@link android.net.wifi.WifiManager#updateInterfaceIpState(String, int)}
889     *
890     * The possible modes include: {@link WifiManager#IFACE_IP_MODE_TETHERED},
891     *                             {@link WifiManager#IFACE_IP_MODE_LOCAL_ONLY},
892     *                             {@link WifiManager#IFACE_IP_MODE_CONFIGURATION_ERROR}
893     *
894     * @param ifaceName String name of the updated interface
895     * @param mode new operating mode of the interface
896     *
897     * @throws SecurityException if the caller does not have permission to call update
898     */
899    @Override
900    public void updateInterfaceIpState(String ifaceName, int mode) {
901        // NETWORK_STACK is a signature only permission.
902        enforceNetworkStackPermission();
903
904        // hand off the work to our handler thread
905        mClientHandler.post(() -> {
906            updateInterfaceIpStateInternal(ifaceName, mode);
907        });
908    }
909
910    private void updateInterfaceIpStateInternal(String ifaceName, int mode) {
911        // update interface IP state related to tethering and hotspot
912        synchronized (mLocalOnlyHotspotRequests) {
913            // update the mode tracker here - we clear out state below
914            Integer previousMode = WifiManager.IFACE_IP_MODE_UNSPECIFIED;
915            if (ifaceName != null) {
916                previousMode = mIfaceIpModes.put(ifaceName, mode);
917            }
918            Slog.d(TAG, "updateInterfaceIpState: ifaceName=" + ifaceName + " mode=" + mode
919                    + " previous mode= " + previousMode);
920
921            switch (mode) {
922                case WifiManager.IFACE_IP_MODE_LOCAL_ONLY:
923                    // first make sure we have registered requests..  otherwise clean up
924                    if (mLocalOnlyHotspotRequests.isEmpty()) {
925                        // we don't have requests...  stop the hotspot
926                        stopSoftAp();
927                        updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED);
928                        return;
929                    }
930                    // LOHS is ready to go!  Call our registered requestors!
931                    sendHotspotStartedMessageToAllLOHSRequestInfoEntriesLocked();
932                    break;
933                case WifiManager.IFACE_IP_MODE_TETHERED:
934                    // we have tethered an interface. we don't really act on this now other than if
935                    // we have LOHS requests, and this is an issue.  return incompatible mode for
936                    // onFailed for the registered requestors since this can result from a race
937                    // between a tether request and a hotspot request (tethering wins).
938                    sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(
939                            LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE);
940                    break;
941                case WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR:
942                    // there was an error setting up the hotspot...  trigger onFailed for the
943                    // registered LOHS requestors
944                    sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(
945                            LocalOnlyHotspotCallback.ERROR_GENERIC);
946                    updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED);
947                    break;
948                case WifiManager.IFACE_IP_MODE_UNSPECIFIED:
949                    if (ifaceName == null) {
950                        // interface name is null, this is due to softap teardown.  clear all
951                        // entries for now.
952                        // TODO: Deal with individual interfaces when we receive updates for them
953                        mIfaceIpModes.clear();
954                        return;
955                    }
956                    break;
957                default:
958                    mLog.trace("updateInterfaceIpStateInternal: unknown mode %").c(mode).flush();
959            }
960        }
961    }
962
963    /**
964     * see {@link android.net.wifi.WifiManager#startSoftAp(WifiConfiguration)}
965     * @param wifiConfig SSID, security and channel details as part of WifiConfiguration
966     * @return {@code true} if softap start was triggered
967     * @throws SecurityException if the caller does not have permission to start softap
968     */
969    @Override
970    public boolean startSoftAp(WifiConfiguration wifiConfig) {
971        // NETWORK_STACK is a signature only permission.
972        enforceNetworkStackPermission();
973
974        mLog.trace("startSoftAp uid=%").c(Binder.getCallingUid()).flush();
975
976        synchronized (mLocalOnlyHotspotRequests) {
977            // If a tethering request comes in while we have LOHS running (or requested), call stop
978            // for softap mode and restart softap with the tethering config.
979            if (!mLocalOnlyHotspotRequests.isEmpty()) {
980                stopSoftApInternal();
981            }
982
983            return startSoftApInternal(wifiConfig, WifiManager.IFACE_IP_MODE_TETHERED);
984        }
985    }
986
987    /**
988     * Internal method to start softap mode. Callers of this method should have already checked
989     * proper permissions beyond the NetworkStack permission.
990     */
991    private boolean startSoftApInternal(WifiConfiguration wifiConfig, int mode) {
992        mLog.trace("startSoftApInternal uid=% mode=%")
993                .c(Binder.getCallingUid()).c(mode).flush();
994
995        // null wifiConfig is a meaningful input for CMD_SET_AP
996        if (wifiConfig == null || isValid(wifiConfig)) {
997            SoftApModeConfiguration softApConfig = new SoftApModeConfiguration(mode, wifiConfig);
998            mWifiController.sendMessage(CMD_SET_AP, 1, 0, softApConfig);
999            return true;
1000        }
1001        Slog.e(TAG, "Invalid WifiConfiguration");
1002        return false;
1003    }
1004
1005    /**
1006     * see {@link android.net.wifi.WifiManager#stopSoftAp()}
1007     * @return {@code true} if softap stop was triggered
1008     * @throws SecurityException if the caller does not have permission to stop softap
1009     */
1010    @Override
1011    public boolean stopSoftAp() {
1012        // NETWORK_STACK is a signature only permission.
1013        enforceNetworkStackPermission();
1014
1015        // only permitted callers are allowed to this point - they must have gone through
1016        // connectivity service since this method is protected with the NETWORK_STACK PERMISSION
1017
1018        mLog.trace("stopSoftAp uid=%").c(Binder.getCallingUid()).flush();
1019
1020        synchronized (mLocalOnlyHotspotRequests) {
1021            // If a tethering request comes in while we have LOHS running (or requested), call stop
1022            // for softap mode and restart softap with the tethering config.
1023            if (!mLocalOnlyHotspotRequests.isEmpty()) {
1024                mLog.trace("Call to stop Tethering while LOHS is active,"
1025                        + " Registered LOHS callers will be updated when softap stopped.");
1026            }
1027
1028            return stopSoftApInternal();
1029        }
1030    }
1031
1032    /**
1033     * Internal method to stop softap mode.  Callers of this method should have already checked
1034     * proper permissions beyond the NetworkStack permission.
1035     */
1036    private boolean stopSoftApInternal() {
1037        mLog.trace("stopSoftApInternal uid=%").c(Binder.getCallingUid()).flush();
1038
1039        mWifiController.sendMessage(CMD_SET_AP, 0, 0);
1040        return true;
1041    }
1042
1043    /**
1044     * Private method to handle SoftAp state changes
1045     */
1046    private void handleWifiApStateChange(
1047            int currentState, int previousState, int errorCode, String ifaceName, int mode) {
1048        // The AP state update from WifiStateMachine for softap
1049        Slog.d(TAG, "handleWifiApStateChange: currentState=" + currentState
1050                + " previousState=" + previousState + " errorCode= " + errorCode
1051                + " ifaceName=" + ifaceName + " mode=" + mode);
1052
1053        // check if we have a failure - since it is possible (worst case scenario where
1054        // WifiController and WifiStateMachine are out of sync wrt modes) to get two FAILED
1055        // notifications in a row, we need to handle this first.
1056        if (currentState == WIFI_AP_STATE_FAILED) {
1057            // update registered LOHS callbacks if we see a failure
1058            synchronized (mLocalOnlyHotspotRequests) {
1059                int errorToReport = ERROR_GENERIC;
1060                if (errorCode == SAP_START_FAILURE_NO_CHANNEL) {
1061                    errorToReport = ERROR_NO_CHANNEL;
1062                }
1063                // holding the required lock: send message to requestors and clear the list
1064                sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(
1065                        errorToReport);
1066                // also need to clear interface ip state - send null for now since we don't know
1067                // what interface (and we have one anyway)
1068                updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED);
1069            }
1070            return;
1071        }
1072
1073        if (currentState == WIFI_AP_STATE_DISABLING || currentState == WIFI_AP_STATE_DISABLED) {
1074            // softap is shutting down or is down...  let requestors know via the onStopped call
1075            synchronized (mLocalOnlyHotspotRequests) {
1076                // if we are currently in hotspot mode, then trigger onStopped for registered
1077                // requestors, otherwise something odd happened and we should clear state
1078                if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_LOCAL_ONLY)) {
1079                    // holding the required lock: send message to requestors and clear the list
1080                    sendHotspotStoppedMessageToAllLOHSRequestInfoEntriesLocked();
1081                } else {
1082                    // LOHS not active: report an error (still holding the required lock)
1083                    sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(ERROR_GENERIC);
1084                }
1085                // also clear interface ip state - send null for now since we don't know what
1086                // interface (and we only have one anyway)
1087                updateInterfaceIpState(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED);
1088            }
1089            return;
1090        }
1091
1092        // remaining states are enabling or enabled...  those are not used for the callbacks
1093    }
1094
1095    /**
1096     * Helper method to send a HOTSPOT_FAILED message to all registered LocalOnlyHotspotRequest
1097     * callers and clear the registrations.
1098     *
1099     * Callers should already hold the mLocalOnlyHotspotRequests lock.
1100     */
1101    private void sendHotspotFailedMessageToAllLOHSRequestInfoEntriesLocked(int arg1) {
1102        for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) {
1103            try {
1104                requestor.sendHotspotFailedMessage(arg1);
1105                requestor.unlinkDeathRecipient();
1106            } catch (RemoteException e) {
1107                // This will be cleaned up by binder death handling
1108            }
1109        }
1110
1111        // Since all callers were notified, now clear the registrations.
1112        mLocalOnlyHotspotRequests.clear();
1113    }
1114
1115    /**
1116     * Helper method to send a HOTSPOT_STOPPED message to all registered LocalOnlyHotspotRequest
1117     * callers and clear the registrations.
1118     *
1119     * Callers should already hold the mLocalOnlyHotspotRequests lock.
1120     */
1121    private void sendHotspotStoppedMessageToAllLOHSRequestInfoEntriesLocked() {
1122        for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) {
1123            try {
1124                requestor.sendHotspotStoppedMessage();
1125                requestor.unlinkDeathRecipient();
1126            } catch (RemoteException e) {
1127                // This will be cleaned up by binder death handling
1128            }
1129        }
1130
1131        // Since all callers were notified, now clear the registrations.
1132        mLocalOnlyHotspotRequests.clear();
1133    }
1134
1135    /**
1136     * Helper method to send a HOTSPOT_STARTED message to all registered LocalOnlyHotspotRequest
1137     * callers.
1138     *
1139     * Callers should already hold the mLocalOnlyHotspotRequests lock.
1140     */
1141    private void sendHotspotStartedMessageToAllLOHSRequestInfoEntriesLocked() {
1142        for (LocalOnlyHotspotRequestInfo requestor : mLocalOnlyHotspotRequests.values()) {
1143            try {
1144                requestor.sendHotspotStartedMessage(mLocalOnlyHotspotConfig);
1145            } catch (RemoteException e) {
1146                // This will be cleaned up by binder death handling
1147            }
1148        }
1149    }
1150
1151    /**
1152     * Temporary method used for testing while startLocalOnlyHotspot is not fully implemented.  This
1153     * method allows unit tests to register callbacks directly for testing mechanisms triggered by
1154     * softap mode changes.
1155     */
1156    @VisibleForTesting
1157    void registerLOHSForTest(int pid, LocalOnlyHotspotRequestInfo request) {
1158        mLocalOnlyHotspotRequests.put(pid, request);
1159    }
1160
1161    /**
1162     * Method to start LocalOnlyHotspot.  In this method, permissions, settings and modes are
1163     * checked to verify that we can enter softapmode.  This method returns
1164     * {@link LocalOnlyHotspotCallback#REQUEST_REGISTERED} if we will attempt to start, otherwise,
1165     * possible startup erros may include tethering being disallowed failure reason {@link
1166     * LocalOnlyHotspotCallback#ERROR_TETHERING_DISALLOWED} or an incompatible mode failure reason
1167     * {@link LocalOnlyHotspotCallback#ERROR_INCOMPATIBLE_MODE}.
1168     *
1169     * see {@link WifiManager#startLocalOnlyHotspot(LocalOnlyHotspotCallback)}
1170     *
1171     * @param messenger Messenger to send messages to the corresponding WifiManager.
1172     * @param binder IBinder instance to allow cleanup if the app dies
1173     * @param packageName String name of the calling package
1174     *
1175     * @return int return code for attempt to start LocalOnlyHotspot.
1176     *
1177     * @throws SecurityException if the caller does not have permission to start a Local Only
1178     * Hotspot.
1179     * @throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they
1180     * have an outstanding request.
1181     */
1182    @Override
1183    public int startLocalOnlyHotspot(Messenger messenger, IBinder binder, String packageName) {
1184        // first check if the caller has permission to start a local only hotspot
1185        // need to check for WIFI_STATE_CHANGE and location permission
1186        final int uid = Binder.getCallingUid();
1187        final int pid = Binder.getCallingPid();
1188
1189        enforceChangePermission();
1190        enforceLocationPermission(packageName, uid);
1191        // also need to verify that Locations services are enabled.
1192        if (mSettingsStore.getLocationModeSetting(mContext) == Settings.Secure.LOCATION_MODE_OFF) {
1193            throw new SecurityException("Location mode is not enabled.");
1194        }
1195
1196        // verify that tethering is not disabled
1197        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
1198            return LocalOnlyHotspotCallback.ERROR_TETHERING_DISALLOWED;
1199        }
1200
1201        // the app should be in the foreground
1202        try {
1203            if (!mFrameworkFacade.isAppForeground(uid)) {
1204                return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE;
1205            }
1206        } catch (RemoteException e) {
1207            mLog.trace("RemoteException during isAppForeground when calling startLOHS");
1208            return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE;
1209        }
1210
1211        mLog.trace("startLocalOnlyHotspot uid=% pid=%").c(uid).c(pid).flush();
1212
1213        synchronized (mLocalOnlyHotspotRequests) {
1214            // check if we are currently tethering
1215            if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_TETHERED)) {
1216                // Tethering is enabled, cannot start LocalOnlyHotspot
1217                mLog.trace("Cannot start localOnlyHotspot when WiFi Tethering is active.");
1218                return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE;
1219            }
1220
1221            // does this caller already have a request?
1222            LocalOnlyHotspotRequestInfo request = mLocalOnlyHotspotRequests.get(pid);
1223            if (request != null) {
1224                mLog.trace("caller already has an active request");
1225                throw new IllegalStateException(
1226                        "Caller already has an active LocalOnlyHotspot request");
1227            }
1228
1229            // now create the new LOHS request info object
1230            request = new LocalOnlyHotspotRequestInfo(binder, messenger,
1231                    new LocalOnlyRequestorCallback());
1232
1233            // check current operating state and take action if needed
1234            if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_LOCAL_ONLY)) {
1235                // LOHS is already active, send out what is running
1236                try {
1237                    mLog.trace("LOHS already up, trigger onStarted callback");
1238                    request.sendHotspotStartedMessage(mLocalOnlyHotspotConfig);
1239                } catch (RemoteException e) {
1240                    return LocalOnlyHotspotCallback.ERROR_GENERIC;
1241                }
1242            } else if (mLocalOnlyHotspotRequests.isEmpty()) {
1243                // this is the first request, then set up our config and start LOHS
1244                mLocalOnlyHotspotConfig =
1245                        WifiApConfigStore.generateLocalOnlyHotspotConfig(mContext);
1246                startSoftApInternal(mLocalOnlyHotspotConfig, WifiManager.IFACE_IP_MODE_LOCAL_ONLY);
1247            }
1248
1249            mLocalOnlyHotspotRequests.put(pid, request);
1250            return LocalOnlyHotspotCallback.REQUEST_REGISTERED;
1251        }
1252    }
1253
1254    /**
1255     * see {@link WifiManager#stopLocalOnlyHotspot()}
1256     *
1257     * @throws SecurityException if the caller does not have permission to stop a Local Only
1258     * Hotspot.
1259     */
1260    @Override
1261    public void stopLocalOnlyHotspot() {
1262        // first check if the caller has permission to stop a local only hotspot
1263        enforceChangePermission();
1264        final int uid = Binder.getCallingUid();
1265        final int pid = Binder.getCallingPid();
1266
1267        mLog.trace("stopLocalOnlyHotspot uid=% pid=%").c(uid).c(pid).flush();
1268
1269        synchronized (mLocalOnlyHotspotRequests) {
1270            // was the caller already registered?  check request tracker - return false if not
1271            LocalOnlyHotspotRequestInfo requestInfo = mLocalOnlyHotspotRequests.get(pid);
1272            if (requestInfo == null) {
1273                return;
1274            }
1275            requestInfo.unlinkDeathRecipient();
1276            unregisterCallingAppAndStopLocalOnlyHotspot(requestInfo);
1277        } // end synchronized
1278    }
1279
1280    /**
1281     * Helper method to unregister LocalOnlyHotspot requestors and stop the hotspot if needed.
1282     */
1283    private void unregisterCallingAppAndStopLocalOnlyHotspot(LocalOnlyHotspotRequestInfo request) {
1284        mLog.trace("unregisterCallingAppAndStopLocalOnlyHotspot pid=%").c(request.getPid()).flush();
1285
1286        synchronized (mLocalOnlyHotspotRequests) {
1287            if (mLocalOnlyHotspotRequests.remove(request.getPid()) == null) {
1288                mLog.trace("LocalOnlyHotspotRequestInfo not found to remove");
1289                return;
1290            }
1291
1292            if (mLocalOnlyHotspotRequests.isEmpty()) {
1293                mLocalOnlyHotspotConfig = null;
1294                updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED);
1295                // if that was the last caller, then call stopSoftAp as WifiService
1296                long identity = Binder.clearCallingIdentity();
1297                try {
1298                    stopSoftApInternal();
1299                } finally {
1300                    Binder.restoreCallingIdentity(identity);
1301                }
1302            }
1303        }
1304    }
1305
1306    /**
1307     * see {@link WifiManager#watchLocalOnlyHotspot(LocalOnlyHotspotObserver)}
1308     *
1309     * This call requires the android.permission.NETWORK_SETTINGS permission.
1310     *
1311     * @param messenger Messenger to send messages to the corresponding WifiManager.
1312     * @param binder IBinder instance to allow cleanup if the app dies
1313     *
1314     * @throws SecurityException if the caller does not have permission to watch Local Only Hotspot
1315     * status updates.
1316     * @throws IllegalStateException if the caller attempts to watch LocalOnlyHotspot updates with
1317     * an existing subscription.
1318     */
1319    @Override
1320    public void startWatchLocalOnlyHotspot(Messenger messenger, IBinder binder) {
1321        final String packageName = mContext.getOpPackageName();
1322
1323        // NETWORK_SETTINGS is a signature only permission.
1324        enforceNetworkSettingsPermission();
1325
1326        throw new UnsupportedOperationException("LocalOnlyHotspot is still in development");
1327    }
1328
1329    /**
1330     * see {@link WifiManager#unregisterLocalOnlyHotspotObserver()}
1331     */
1332    @Override
1333    public void stopWatchLocalOnlyHotspot() {
1334        // NETWORK_STACK is a signature only permission.
1335        enforceNetworkSettingsPermission();
1336        throw new UnsupportedOperationException("LocalOnlyHotspot is still in development");
1337    }
1338
1339    /**
1340     * see {@link WifiManager#getWifiApConfiguration()}
1341     * @return soft access point configuration
1342     * @throws SecurityException if the caller does not have permission to retrieve the softap
1343     * config
1344     */
1345    @Override
1346    public WifiConfiguration getWifiApConfiguration() {
1347        enforceAccessPermission();
1348        int uid = Binder.getCallingUid();
1349        // only allow Settings UI to get the saved SoftApConfig
1350        if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) {
1351            // random apps should not be allowed to read the user specified config
1352            throw new SecurityException("App not allowed to read or update stored WiFi Ap config "
1353                    + "(uid = " + uid + ")");
1354        }
1355        mLog.trace("getWifiApConfiguration uid=%").c(uid).flush();
1356        return mWifiStateMachine.syncGetWifiApConfiguration();
1357    }
1358
1359    /**
1360     * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
1361     * @param wifiConfig WifiConfiguration details for soft access point
1362     * @throws SecurityException if the caller does not have permission to write the sotap config
1363     */
1364    @Override
1365    public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
1366        enforceChangePermission();
1367        int uid = Binder.getCallingUid();
1368        // only allow Settings UI to write the stored SoftApConfig
1369        if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) {
1370            // random apps should not be allowed to read the user specified config
1371            throw new SecurityException("App not allowed to read or update stored WiFi AP config "
1372                    + "(uid = " + uid + ")");
1373        }
1374        mLog.trace("setWifiApConfiguration uid=%").c(uid).flush();
1375        if (wifiConfig == null)
1376            return;
1377        if (isValid(wifiConfig)) {
1378            mWifiStateMachine.setWifiApConfiguration(wifiConfig);
1379        } else {
1380            Slog.e(TAG, "Invalid WifiConfiguration");
1381        }
1382    }
1383
1384    /**
1385     * see {@link android.net.wifi.WifiManager#isScanAlwaysAvailable()}
1386     */
1387    @Override
1388    public boolean isScanAlwaysAvailable() {
1389        enforceAccessPermission();
1390        mLog.trace("isScanAlwaysAvailable uid=%").c(Binder.getCallingUid()).flush();
1391        return mSettingsStore.isScanAlwaysAvailable();
1392    }
1393
1394    /**
1395     * see {@link android.net.wifi.WifiManager#disconnect()}
1396     */
1397    @Override
1398    public void disconnect() {
1399        enforceChangePermission();
1400        mLog.trace("disconnect uid=%").c(Binder.getCallingUid()).flush();
1401        mWifiStateMachine.disconnectCommand();
1402    }
1403
1404    /**
1405     * see {@link android.net.wifi.WifiManager#reconnect()}
1406     */
1407    @Override
1408    public void reconnect() {
1409        enforceChangePermission();
1410        mLog.trace("reconnect uid=%").c(Binder.getCallingUid()).flush();
1411        mWifiStateMachine.reconnectCommand();
1412    }
1413
1414    /**
1415     * see {@link android.net.wifi.WifiManager#reassociate()}
1416     */
1417    @Override
1418    public void reassociate() {
1419        enforceChangePermission();
1420        mLog.trace("reassociate uid=%").c(Binder.getCallingUid()).flush();
1421        mWifiStateMachine.reassociateCommand();
1422    }
1423
1424    /**
1425     * see {@link android.net.wifi.WifiManager#getSupportedFeatures}
1426     */
1427    @Override
1428    public int getSupportedFeatures() {
1429        enforceAccessPermission();
1430        mLog.trace("getSupportedFeatures uid=%").c(Binder.getCallingUid()).flush();
1431        if (mWifiStateMachineChannel != null) {
1432            return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel);
1433        } else {
1434            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1435            return 0;
1436        }
1437    }
1438
1439    @Override
1440    public void requestActivityInfo(ResultReceiver result) {
1441        Bundle bundle = new Bundle();
1442        mLog.trace("requestActivityInfo uid=%").c(Binder.getCallingUid()).flush();
1443        bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, reportActivityInfo());
1444        result.send(0, bundle);
1445    }
1446
1447    /**
1448     * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)}
1449     */
1450    @Override
1451    public WifiActivityEnergyInfo reportActivityInfo() {
1452        enforceAccessPermission();
1453        mLog.trace("reportActivityInfo uid=%").c(Binder.getCallingUid()).flush();
1454        if ((getSupportedFeatures() & WifiManager.WIFI_FEATURE_LINK_LAYER_STATS) == 0) {
1455            return null;
1456        }
1457        WifiLinkLayerStats stats;
1458        WifiActivityEnergyInfo energyInfo = null;
1459        if (mWifiStateMachineChannel != null) {
1460            stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel);
1461            if (stats != null) {
1462                final long rxIdleCurrent = mContext.getResources().getInteger(
1463                        com.android.internal.R.integer.config_wifi_idle_receive_cur_ma);
1464                final long rxCurrent = mContext.getResources().getInteger(
1465                        com.android.internal.R.integer.config_wifi_active_rx_cur_ma);
1466                final long txCurrent = mContext.getResources().getInteger(
1467                        com.android.internal.R.integer.config_wifi_tx_cur_ma);
1468                final double voltage = mContext.getResources().getInteger(
1469                        com.android.internal.R.integer.config_wifi_operating_voltage_mv)
1470                        / 1000.0;
1471
1472                final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time;
1473                final long[] txTimePerLevel;
1474                if (stats.tx_time_per_level != null) {
1475                    txTimePerLevel = new long[stats.tx_time_per_level.length];
1476                    for (int i = 0; i < txTimePerLevel.length; i++) {
1477                        txTimePerLevel[i] = stats.tx_time_per_level[i];
1478                        // TODO(b/27227497): Need to read the power consumed per level from config
1479                    }
1480                } else {
1481                    // This will happen if the HAL get link layer API returned null.
1482                    txTimePerLevel = new long[0];
1483                }
1484                final long energyUsed = (long)((stats.tx_time * txCurrent +
1485                        stats.rx_time * rxCurrent +
1486                        rxIdleTime * rxIdleCurrent) * voltage);
1487                if (VDBG || rxIdleTime < 0 || stats.on_time < 0 || stats.tx_time < 0 ||
1488                        stats.rx_time < 0 || energyUsed < 0) {
1489                    StringBuilder sb = new StringBuilder();
1490                    sb.append(" rxIdleCur=" + rxIdleCurrent);
1491                    sb.append(" rxCur=" + rxCurrent);
1492                    sb.append(" txCur=" + txCurrent);
1493                    sb.append(" voltage=" + voltage);
1494                    sb.append(" on_time=" + stats.on_time);
1495                    sb.append(" tx_time=" + stats.tx_time);
1496                    sb.append(" tx_time_per_level=" + Arrays.toString(txTimePerLevel));
1497                    sb.append(" rx_time=" + stats.rx_time);
1498                    sb.append(" rxIdleTime=" + rxIdleTime);
1499                    sb.append(" energy=" + energyUsed);
1500                    Log.d(TAG, " reportActivityInfo: " + sb.toString());
1501                }
1502
1503                // Convert the LinkLayerStats into EnergyActivity
1504                energyInfo = new WifiActivityEnergyInfo(mClock.getElapsedSinceBootMillis(),
1505                        WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time,
1506                        txTimePerLevel, stats.rx_time, rxIdleTime, energyUsed);
1507            }
1508            if (energyInfo != null && energyInfo.isValid()) {
1509                return energyInfo;
1510            } else {
1511                return null;
1512            }
1513        } else {
1514            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1515            return null;
1516        }
1517    }
1518
1519    /**
1520     * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
1521     * @return the list of configured networks
1522     */
1523    @Override
1524    public ParceledListSlice<WifiConfiguration> getConfiguredNetworks() {
1525        enforceAccessPermission();
1526        mLog.trace("getConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush();
1527        if (mWifiStateMachineChannel != null) {
1528            List<WifiConfiguration> configs = mWifiStateMachine.syncGetConfiguredNetworks(
1529                    Binder.getCallingUid(), mWifiStateMachineChannel);
1530            if (configs != null) {
1531                return new ParceledListSlice<WifiConfiguration>(configs);
1532            }
1533        } else {
1534            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1535        }
1536        return null;
1537    }
1538
1539    /**
1540     * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()}
1541     * @return the list of configured networks with real preSharedKey
1542     */
1543    @Override
1544    public ParceledListSlice<WifiConfiguration> getPrivilegedConfiguredNetworks() {
1545        enforceReadCredentialPermission();
1546        enforceAccessPermission();
1547        mLog.trace("getPrivilegedConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush();
1548        if (mWifiStateMachineChannel != null) {
1549            List<WifiConfiguration> configs =
1550                    mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
1551            if (configs != null) {
1552                return new ParceledListSlice<WifiConfiguration>(configs);
1553            }
1554        } else {
1555            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1556        }
1557        return null;
1558    }
1559
1560    /**
1561     * Returns a WifiConfiguration for a Passpoint network matching this ScanResult.
1562     *
1563     * @param scanResult scanResult that represents the BSSID
1564     * @return {@link WifiConfiguration} that matches this BSSID or null
1565     */
1566    @Override
1567    public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) {
1568        enforceAccessPermission();
1569        mLog.trace("getMatchingWifiConfig uid=%").c(Binder.getCallingUid()).flush();
1570        if (!mContext.getPackageManager().hasSystemFeature(
1571                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1572            throw new UnsupportedOperationException("Passpoint not enabled");
1573        }
1574        return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel);
1575    }
1576
1577    /**
1578     * Returns list of OSU (Online Sign-Up) providers associated with the given Passpoint network.
1579     *
1580     * @param scanResult scanResult of the Passpoint AP
1581     * @return List of {@link OsuProvider}
1582     */
1583    @Override
1584    public List<OsuProvider> getMatchingOsuProviders(ScanResult scanResult) {
1585        enforceAccessPermission();
1586        mLog.trace("getMatchingOsuProviders uid=%").c(Binder.getCallingUid()).flush();
1587        if (!mContext.getPackageManager().hasSystemFeature(
1588                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1589            throw new UnsupportedOperationException("Passpoint not enabled");
1590        }
1591        return mWifiStateMachine.syncGetMatchingOsuProviders(scanResult, mWifiStateMachineChannel);
1592    }
1593
1594    /**
1595     * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
1596     * @return the supplicant-assigned identifier for the new or updated
1597     * network if the operation succeeds, or {@code -1} if it fails
1598     */
1599    @Override
1600    public int addOrUpdateNetwork(WifiConfiguration config) {
1601        enforceChangePermission();
1602        mLog.trace("addOrUpdateNetwork uid=%").c(Binder.getCallingUid()).flush();
1603
1604        // Previously, this API is overloaded for installing Passpoint profiles.  Now
1605        // that we have a dedicated API for doing it, redirect the call to the dedicated API.
1606        if (config.isPasspoint()) {
1607            PasspointConfiguration passpointConfig =
1608                    PasspointProvider.convertFromWifiConfig(config);
1609            if (passpointConfig.getCredential() == null) {
1610                Slog.e(TAG, "Missing credential for Passpoint profile");
1611                return -1;
1612            }
1613            // Copy over certificates and keys.
1614            passpointConfig.getCredential().setCaCertificate(
1615                    config.enterpriseConfig.getCaCertificate());
1616            passpointConfig.getCredential().setClientCertificateChain(
1617                    config.enterpriseConfig.getClientCertificateChain());
1618            passpointConfig.getCredential().setClientPrivateKey(
1619                    config.enterpriseConfig.getClientPrivateKey());
1620            if (!addOrUpdatePasspointConfiguration(passpointConfig)) {
1621                Slog.e(TAG, "Failed to add Passpoint profile");
1622                return -1;
1623            }
1624            // There is no network ID associated with a Passpoint profile.
1625            return 0;
1626        }
1627
1628        if (config != null) {
1629            //TODO: pass the Uid the WifiStateMachine as a message parameter
1630            Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid())
1631                    + " SSID " + config.SSID
1632                    + " nid=" + Integer.toString(config.networkId));
1633            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
1634                config.creatorUid = Binder.getCallingUid();
1635            } else {
1636                config.lastUpdateUid = Binder.getCallingUid();
1637            }
1638            if (mWifiStateMachineChannel != null) {
1639                return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
1640            } else {
1641                Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1642                return -1;
1643            }
1644        } else {
1645            Slog.e(TAG, "bad network configuration");
1646            return -1;
1647        }
1648    }
1649
1650    public static void verifyCert(X509Certificate caCert)
1651            throws GeneralSecurityException, IOException {
1652        CertificateFactory factory = CertificateFactory.getInstance("X.509");
1653        CertPathValidator validator =
1654                CertPathValidator.getInstance(CertPathValidator.getDefaultType());
1655        CertPath path = factory.generateCertPath(
1656                Arrays.asList(caCert));
1657        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
1658        ks.load(null, null);
1659        PKIXParameters params = new PKIXParameters(ks);
1660        params.setRevocationEnabled(false);
1661        validator.validate(path, params);
1662    }
1663
1664    /**
1665     * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
1666     * @param netId the integer that identifies the network configuration
1667     * to the supplicant
1668     * @return {@code true} if the operation succeeded
1669     */
1670    @Override
1671    public boolean removeNetwork(int netId) {
1672        enforceChangePermission();
1673        mLog.trace("removeNetwork uid=%").c(Binder.getCallingUid()).flush();
1674        // TODO Add private logging for netId b/33807876
1675        if (mWifiStateMachineChannel != null) {
1676            return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
1677        } else {
1678            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1679            return false;
1680        }
1681    }
1682
1683    /**
1684     * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
1685     * @param netId the integer that identifies the network configuration
1686     * to the supplicant
1687     * @param disableOthers if true, disable all other networks.
1688     * @return {@code true} if the operation succeeded
1689     */
1690    @Override
1691    public boolean enableNetwork(int netId, boolean disableOthers) {
1692        enforceChangePermission();
1693        // TODO b/33807876 Log netId
1694        mLog.trace("enableNetwork uid=% disableOthers=%")
1695                .c(Binder.getCallingUid())
1696                .c(disableOthers).flush();
1697
1698        if (mWifiStateMachineChannel != null) {
1699            return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
1700                    disableOthers);
1701        } else {
1702            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1703            return false;
1704        }
1705    }
1706
1707    /**
1708     * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1709     * @param netId the integer that identifies the network configuration
1710     * to the supplicant
1711     * @return {@code true} if the operation succeeded
1712     */
1713    @Override
1714    public boolean disableNetwork(int netId) {
1715        enforceChangePermission();
1716        // TODO b/33807876 Log netId
1717        mLog.trace("disableNetwork uid=%").c(Binder.getCallingUid()).flush();
1718
1719        if (mWifiStateMachineChannel != null) {
1720            return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
1721        } else {
1722            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1723            return false;
1724        }
1725    }
1726
1727    /**
1728     * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1729     * @return the Wi-Fi information, contained in {@link WifiInfo}.
1730     */
1731    @Override
1732    public WifiInfo getConnectionInfo() {
1733        enforceAccessPermission();
1734        mLog.trace("getConnectionInfo uid=%").c(Binder.getCallingUid()).flush();
1735        /*
1736         * Make sure we have the latest information, by sending
1737         * a status request to the supplicant.
1738         */
1739        return mWifiStateMachine.syncRequestConnectionInfo();
1740    }
1741
1742    /**
1743     * Return the results of the most recent access point scan, in the form of
1744     * a list of {@link ScanResult} objects.
1745     * @return the list of results
1746     */
1747    @Override
1748    public List<ScanResult> getScanResults(String callingPackage) {
1749        enforceAccessPermission();
1750        int uid = Binder.getCallingUid();
1751        long ident = Binder.clearCallingIdentity();
1752        try {
1753            if (!mWifiPermissionsUtil.canAccessScanResults(callingPackage,
1754                      uid, Build.VERSION_CODES.M)) {
1755                return new ArrayList<ScanResult>();
1756            }
1757            if (mWifiScanner == null) {
1758                mWifiScanner = mWifiInjector.getWifiScanner();
1759            }
1760            return mWifiScanner.getSingleScanResults();
1761        } finally {
1762            Binder.restoreCallingIdentity(ident);
1763        }
1764    }
1765
1766    /**
1767     * Add or update a Passpoint configuration.
1768     *
1769     * @param config The Passpoint configuration to be added
1770     * @return true on success or false on failure
1771     */
1772    @Override
1773    public boolean addOrUpdatePasspointConfiguration(PasspointConfiguration config) {
1774        enforceChangePermission();
1775        mLog.trace("addorUpdatePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush();
1776        if (!mContext.getPackageManager().hasSystemFeature(
1777                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1778            throw new UnsupportedOperationException("Passpoint not enabled");
1779        }
1780        return mWifiStateMachine.syncAddOrUpdatePasspointConfig(mWifiStateMachineChannel, config,
1781                Binder.getCallingUid());
1782    }
1783
1784    /**
1785     * Remove the Passpoint configuration identified by its FQDN (Fully Qualified Domain Name).
1786     *
1787     * @param fqdn The FQDN of the Passpoint configuration to be removed
1788     * @return true on success or false on failure
1789     */
1790    @Override
1791    public boolean removePasspointConfiguration(String fqdn) {
1792        enforceChangePermission();
1793        mLog.trace("removePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush();
1794        if (!mContext.getPackageManager().hasSystemFeature(
1795                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1796            throw new UnsupportedOperationException("Passpoint not enabled");
1797        }
1798        return mWifiStateMachine.syncRemovePasspointConfig(mWifiStateMachineChannel, fqdn);
1799    }
1800
1801    /**
1802     * Return the list of the installed Passpoint configurations.
1803     *
1804     * An empty list will be returned when no configuration is installed.
1805     *
1806     * @return A list of {@link PasspointConfiguration}
1807     */
1808    @Override
1809    public List<PasspointConfiguration> getPasspointConfigurations() {
1810        enforceAccessPermission();
1811        mLog.trace("getPasspointConfigurations uid=%").c(Binder.getCallingUid()).flush();
1812        if (!mContext.getPackageManager().hasSystemFeature(
1813                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1814            throw new UnsupportedOperationException("Passpoint not enabled");
1815        }
1816        return mWifiStateMachine.syncGetPasspointConfigs(mWifiStateMachineChannel);
1817    }
1818
1819    /**
1820     * Query for a Hotspot 2.0 release 2 OSU icon
1821     * @param bssid The BSSID of the AP
1822     * @param fileName Icon file name
1823     */
1824    @Override
1825    public void queryPasspointIcon(long bssid, String fileName) {
1826        enforceAccessPermission();
1827        mLog.trace("queryPasspointIcon uid=%").c(Binder.getCallingUid()).flush();
1828        if (!mContext.getPackageManager().hasSystemFeature(
1829                PackageManager.FEATURE_WIFI_PASSPOINT)) {
1830            throw new UnsupportedOperationException("Passpoint not enabled");
1831        }
1832        mWifiStateMachine.syncQueryPasspointIcon(mWifiStateMachineChannel, bssid, fileName);
1833    }
1834
1835    /**
1836     * Match the currently associated network against the SP matching the given FQDN
1837     * @param fqdn FQDN of the SP
1838     * @return ordinal [HomeProvider, RoamingProvider, Incomplete, None, Declined]
1839     */
1840    @Override
1841    public int matchProviderWithCurrentNetwork(String fqdn) {
1842        mLog.trace("matchProviderWithCurrentNetwork uid=%").c(Binder.getCallingUid()).flush();
1843        return mWifiStateMachine.matchProviderWithCurrentNetwork(mWifiStateMachineChannel, fqdn);
1844    }
1845
1846    /**
1847     * Deauthenticate and set the re-authentication hold off time for the current network
1848     * @param holdoff hold off time in milliseconds
1849     * @param ess set if the hold off pertains to an ESS rather than a BSS
1850     */
1851    @Override
1852    public void deauthenticateNetwork(long holdoff, boolean ess) {
1853        mLog.trace("deauthenticateNetwork uid=%").c(Binder.getCallingUid()).flush();
1854        mWifiStateMachine.deauthenticateNetwork(mWifiStateMachineChannel, holdoff, ess);
1855    }
1856
1857    /**
1858     * Tell the supplicant to persist the current list of configured networks.
1859     * @return {@code true} if the operation succeeded
1860     *
1861     * TODO: deprecate this
1862     */
1863    @Override
1864    public boolean saveConfiguration() {
1865        enforceChangePermission();
1866        mLog.trace("saveConfiguration uid=%").c(Binder.getCallingUid()).flush();
1867        if (mWifiStateMachineChannel != null) {
1868            return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
1869        } else {
1870            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1871            return false;
1872        }
1873    }
1874
1875    /**
1876     * Set the country code
1877     * @param countryCode ISO 3166 country code.
1878     * @param persist {@code true} if the setting should be remembered.
1879     *
1880     * The persist behavior exists so that wifi can fall back to the last
1881     * persisted country code on a restart, when the locale information is
1882     * not available from telephony.
1883     */
1884    @Override
1885    public void setCountryCode(String countryCode, boolean persist) {
1886        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
1887                " with persist set to " + persist);
1888        enforceConnectivityInternalPermission();
1889        mLog.trace("setCountryCode uid=%").c(Binder.getCallingUid()).flush();
1890        final long token = Binder.clearCallingIdentity();
1891        mCountryCode.setCountryCode(countryCode);
1892        Binder.restoreCallingIdentity(token);
1893    }
1894
1895     /**
1896     * Get the country code
1897     * @return Get the best choice country code for wifi, regardless of if it was set or
1898     * not.
1899     * Returns null when there is no country code available.
1900     */
1901    @Override
1902    public String getCountryCode() {
1903        enforceConnectivityInternalPermission();
1904        mLog.trace("getCountryCode uid=%").c(Binder.getCallingUid()).flush();
1905        String country = mCountryCode.getCountryCode();
1906        return country;
1907    }
1908
1909    @Override
1910    public boolean isDualBandSupported() {
1911        //TODO: Should move towards adding a driver API that checks at runtime
1912        mLog.trace("isDualBandSupported uid=%").c(Binder.getCallingUid()).flush();
1913        return mContext.getResources().getBoolean(
1914                com.android.internal.R.bool.config_wifi_dual_band_support);
1915    }
1916
1917    /**
1918     * Return the DHCP-assigned addresses from the last successful DHCP request,
1919     * if any.
1920     * @return the DHCP information
1921     * @deprecated
1922     */
1923    @Override
1924    @Deprecated
1925    public DhcpInfo getDhcpInfo() {
1926        enforceAccessPermission();
1927        mLog.trace("getDhcpInfo uid=%").c(Binder.getCallingUid()).flush();
1928        DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
1929
1930        DhcpInfo info = new DhcpInfo();
1931
1932        if (dhcpResults.ipAddress != null &&
1933                dhcpResults.ipAddress.getAddress() instanceof Inet4Address) {
1934            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress());
1935        }
1936
1937        if (dhcpResults.gateway != null) {
1938            info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway);
1939        }
1940
1941        int dnsFound = 0;
1942        for (InetAddress dns : dhcpResults.dnsServers) {
1943            if (dns instanceof Inet4Address) {
1944                if (dnsFound == 0) {
1945                    info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1946                } else {
1947                    info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1948                }
1949                if (++dnsFound > 1) break;
1950            }
1951        }
1952        Inet4Address serverAddress = dhcpResults.serverAddress;
1953        if (serverAddress != null) {
1954            info.serverAddress = NetworkUtils.inetAddressToInt(serverAddress);
1955        }
1956        info.leaseDuration = dhcpResults.leaseDuration;
1957
1958        return info;
1959    }
1960
1961    /**
1962     * enable TDLS for the local NIC to remote NIC
1963     * The APPs don't know the remote MAC address to identify NIC though,
1964     * so we need to do additional work to find it from remote IP address
1965     */
1966
1967    class TdlsTaskParams {
1968        public String remoteIpAddress;
1969        public boolean enable;
1970    }
1971
1972    class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> {
1973        @Override
1974        protected Integer doInBackground(TdlsTaskParams... params) {
1975
1976            // Retrieve parameters for the call
1977            TdlsTaskParams param = params[0];
1978            String remoteIpAddress = param.remoteIpAddress.trim();
1979            boolean enable = param.enable;
1980
1981            // Get MAC address of Remote IP
1982            String macAddress = null;
1983
1984            BufferedReader reader = null;
1985
1986            try {
1987                reader = new BufferedReader(new FileReader("/proc/net/arp"));
1988
1989                // Skip over the line bearing colum titles
1990                String line = reader.readLine();
1991
1992                while ((line = reader.readLine()) != null) {
1993                    String[] tokens = line.split("[ ]+");
1994                    if (tokens.length < 6) {
1995                        continue;
1996                    }
1997
1998                    // ARP column format is
1999                    // Address HWType HWAddress Flags Mask IFace
2000                    String ip = tokens[0];
2001                    String mac = tokens[3];
2002
2003                    if (remoteIpAddress.equals(ip)) {
2004                        macAddress = mac;
2005                        break;
2006                    }
2007                }
2008
2009                if (macAddress == null) {
2010                    Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " +
2011                            "/proc/net/arp");
2012                } else {
2013                    enableTdlsWithMacAddress(macAddress, enable);
2014                }
2015
2016            } catch (FileNotFoundException e) {
2017                Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address");
2018            } catch (IOException e) {
2019                Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address");
2020            } finally {
2021                try {
2022                    if (reader != null) {
2023                        reader.close();
2024                    }
2025                }
2026                catch (IOException e) {
2027                    // Do nothing
2028                }
2029            }
2030
2031            return 0;
2032        }
2033    }
2034
2035    @Override
2036    public void enableTdls(String remoteAddress, boolean enable) {
2037        if (remoteAddress == null) {
2038          throw new IllegalArgumentException("remoteAddress cannot be null");
2039        }
2040        mLog.trace("enableTdls uid=% enable=%").c(Binder.getCallingUid()).c(enable).flush();
2041        TdlsTaskParams params = new TdlsTaskParams();
2042        params.remoteIpAddress = remoteAddress;
2043        params.enable = enable;
2044        new TdlsTask().execute(params);
2045    }
2046
2047
2048    @Override
2049    public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) {
2050        mLog.trace("enableTdlsWithMacAddress uid=% enable=%")
2051                .c(Binder.getCallingUid())
2052                .c(enable)
2053                .flush();
2054        if (remoteMacAddress == null) {
2055          throw new IllegalArgumentException("remoteMacAddress cannot be null");
2056        }
2057
2058        mWifiStateMachine.enableTdls(remoteMacAddress, enable);
2059    }
2060
2061    /**
2062     * Get a reference to handler. This is used by a client to establish
2063     * an AsyncChannel communication with WifiService
2064     */
2065    @Override
2066    public Messenger getWifiServiceMessenger() {
2067        enforceAccessPermission();
2068        enforceChangePermission();
2069        mLog.trace("getWifiServiceMessenger uid=%").c(Binder.getCallingUid()).flush();
2070        return new Messenger(mClientHandler);
2071    }
2072
2073    /**
2074     * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer
2075     */
2076    @Override
2077    public void disableEphemeralNetwork(String SSID) {
2078        enforceAccessPermission();
2079        enforceChangePermission();
2080        mLog.trace("disableEphemeralNetwork uid=%").c(Binder.getCallingUid()).flush();
2081        mWifiStateMachine.disableEphemeralNetwork(SSID);
2082    }
2083
2084    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
2085        @Override
2086        public void onReceive(Context context, Intent intent) {
2087            String action = intent.getAction();
2088            if (action.equals(Intent.ACTION_SCREEN_ON)) {
2089                mWifiController.sendMessage(CMD_SCREEN_ON);
2090            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
2091                mWifiController.sendMessage(CMD_USER_PRESENT);
2092            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
2093                mWifiController.sendMessage(CMD_SCREEN_OFF);
2094            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
2095                int pluggedType = intent.getIntExtra("plugged", 0);
2096                mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null);
2097            } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
2098                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
2099                        BluetoothAdapter.STATE_DISCONNECTED);
2100                mWifiStateMachine.sendBluetoothAdapterStateChange(state);
2101            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
2102                boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false);
2103                mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0);
2104            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED)) {
2105                boolean inCall = intent.getBooleanExtra(PhoneConstants.PHONE_IN_EMERGENCY_CALL, false);
2106                mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, inCall ? 1 : 0, 0);
2107            } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) {
2108                handleIdleModeChanged();
2109            }
2110        }
2111    };
2112
2113    private boolean startConsentUi(String packageName,
2114            int callingUid, String intentAction) throws RemoteException {
2115        if (UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
2116            return false;
2117        }
2118        try {
2119            // Validate the package only if we are going to use it
2120            ApplicationInfo applicationInfo = mContext.getPackageManager()
2121                    .getApplicationInfoAsUser(packageName,
2122                            PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
2123                            UserHandle.getUserId(callingUid));
2124            if (applicationInfo.uid != callingUid) {
2125                throw new SecurityException("Package " + callingUid
2126                        + " not in uid " + callingUid);
2127            }
2128
2129            // Permission review mode, trigger a user prompt
2130            Intent intent = new Intent(intentAction);
2131            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2132                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
2133            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
2134            mContext.startActivity(intent);
2135            return true;
2136        } catch (PackageManager.NameNotFoundException e) {
2137            throw new RemoteException(e.getMessage());
2138        }
2139    }
2140
2141    /**
2142     * Observes settings changes to scan always mode.
2143     */
2144    private void registerForScanModeChange() {
2145        ContentObserver contentObserver = new ContentObserver(null) {
2146            @Override
2147            public void onChange(boolean selfChange) {
2148                mSettingsStore.handleWifiScanAlwaysAvailableToggled();
2149                mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED);
2150            }
2151        };
2152        mFrameworkFacade.registerContentObserver(mContext,
2153                Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE),
2154                false, contentObserver);
2155
2156    }
2157
2158    // Monitors settings changes related to background wifi scan throttling.
2159    private void registerForBackgroundThrottleChanges() {
2160        mFrameworkFacade.registerContentObserver(
2161                mContext,
2162                Settings.Global.getUriFor(
2163                        Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS),
2164                false,
2165                new ContentObserver(null) {
2166                    @Override
2167                    public void onChange(boolean selfChange) {
2168                        updateBackgroundThrottleInterval();
2169                    }
2170                }
2171        );
2172        mFrameworkFacade.registerContentObserver(
2173                mContext,
2174                Settings.Global.getUriFor(
2175                        Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_PACKAGE_WHITELIST),
2176                false,
2177                new ContentObserver(null) {
2178                    @Override
2179                    public void onChange(boolean selfChange) {
2180                        updateBackgroundThrottlingWhitelist();
2181                    }
2182                }
2183        );
2184    }
2185
2186    private void updateBackgroundThrottleInterval() {
2187        mBackgroundThrottleInterval = mFrameworkFacade.getLongSetting(
2188                mContext,
2189                Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS,
2190                DEFAULT_SCAN_BACKGROUND_THROTTLE_INTERVAL_MS);
2191    }
2192
2193    private void updateBackgroundThrottlingWhitelist() {
2194        String setting = mFrameworkFacade.getStringSetting(
2195                mContext,
2196                Settings.Global.WIFI_SCAN_BACKGROUND_THROTTLE_PACKAGE_WHITELIST);
2197        mBackgroundThrottlePackageWhitelist.clear();
2198        if (setting != null) {
2199            mBackgroundThrottlePackageWhitelist.addAll(Arrays.asList(setting.split(",")));
2200        }
2201    }
2202
2203    private void registerForBroadcasts() {
2204        IntentFilter intentFilter = new IntentFilter();
2205        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
2206        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
2207        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
2208        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
2209        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
2210        intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
2211        intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
2212        intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
2213
2214        boolean trackEmergencyCallState = mContext.getResources().getBoolean(
2215                com.android.internal.R.bool.config_wifi_turn_off_during_emergency_call);
2216        if (trackEmergencyCallState) {
2217            intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED);
2218        }
2219
2220        mContext.registerReceiver(mReceiver, intentFilter);
2221    }
2222
2223    private void registerForPackageOrUserRemoval() {
2224        IntentFilter intentFilter = new IntentFilter();
2225        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
2226        intentFilter.addAction(Intent.ACTION_USER_REMOVED);
2227        mContext.registerReceiverAsUser(new BroadcastReceiver() {
2228            @Override
2229            public void onReceive(Context context, Intent intent) {
2230                switch (intent.getAction()) {
2231                    case Intent.ACTION_PACKAGE_REMOVED: {
2232                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
2233                            return;
2234                        }
2235                        int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
2236                        Uri uri = intent.getData();
2237                        if (uid == -1 || uri == null) {
2238                            return;
2239                        }
2240                        String pkgName = uri.getSchemeSpecificPart();
2241                        mWifiStateMachine.removeAppConfigs(pkgName, uid);
2242                        break;
2243                    }
2244                    case Intent.ACTION_USER_REMOVED: {
2245                        int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
2246                        mWifiStateMachine.removeUserConfigs(userHandle);
2247                        break;
2248                    }
2249                }
2250            }
2251        }, UserHandle.ALL, intentFilter, null, null);
2252    }
2253
2254    @Override
2255    public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
2256            String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
2257        (new WifiShellCommand(mWifiStateMachine)).exec(this, in, out, err, args, callback,
2258                resultReceiver);
2259    }
2260
2261    @Override
2262    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2263        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2264                != PackageManager.PERMISSION_GRANTED) {
2265            pw.println("Permission Denial: can't dump WifiService from from pid="
2266                    + Binder.getCallingPid()
2267                    + ", uid=" + Binder.getCallingUid());
2268            return;
2269        }
2270        if (args != null && args.length > 0 && WifiMetrics.PROTO_DUMP_ARG.equals(args[0])) {
2271            // WifiMetrics proto bytes were requested. Dump only these.
2272            mWifiStateMachine.updateWifiMetrics();
2273            mWifiMetrics.dump(fd, pw, args);
2274        } else if (args != null && args.length > 0 && IpManager.DUMP_ARG.equals(args[0])) {
2275            // IpManager dump was requested. Pass it along and take no further action.
2276            String[] ipManagerArgs = new String[args.length - 1];
2277            System.arraycopy(args, 1, ipManagerArgs, 0, ipManagerArgs.length);
2278            mWifiStateMachine.dumpIpManager(fd, pw, ipManagerArgs);
2279        } else if (args != null && args.length > 0 && WifiScoreReport.DUMP_ARG.equals(args[0])) {
2280            WifiScoreReport wifiScoreReport = mWifiStateMachine.getWifiScoreReport();
2281            if (wifiScoreReport != null) wifiScoreReport.dump(fd, pw, args);
2282        } else {
2283            pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
2284            pw.println("Stay-awake conditions: " +
2285                    mFacade.getIntegerSetting(mContext,
2286                            Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0));
2287            pw.println("mInIdleMode " + mInIdleMode);
2288            pw.println("mScanPending " + mScanPending);
2289            mWifiController.dump(fd, pw, args);
2290            mSettingsStore.dump(fd, pw, args);
2291            mTrafficPoller.dump(fd, pw, args);
2292            pw.println();
2293            pw.println("Locks held:");
2294            mWifiLockManager.dump(pw);
2295            pw.println();
2296            mWifiMulticastLockManager.dump(pw);
2297            pw.println();
2298            mWifiStateMachine.dump(fd, pw, args);
2299            pw.println();
2300            mWifiStateMachine.updateWifiMetrics();
2301            mWifiMetrics.dump(fd, pw, args);
2302            pw.println();
2303            mWifiBackupRestore.dump(fd, pw, args);
2304            pw.println();
2305            WifiScoreReport wifiScoreReport = mWifiStateMachine.getWifiScoreReport();
2306            if (wifiScoreReport != null) {
2307                pw.println("WifiScoreReport:");
2308                wifiScoreReport.dump(fd, pw, args);
2309            }
2310            pw.println();
2311        }
2312    }
2313
2314    @Override
2315    public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
2316        mLog.trace("acquireWifiLock uid=% lockMode=%")
2317                .c(Binder.getCallingUid())
2318                .c(lockMode).flush();
2319        if (mWifiLockManager.acquireWifiLock(lockMode, tag, binder, ws)) {
2320            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
2321            return true;
2322        }
2323        return false;
2324    }
2325
2326    @Override
2327    public void updateWifiLockWorkSource(IBinder binder, WorkSource ws) {
2328        mLog.trace("updateWifiLockWorkSource uid=%").c(Binder.getCallingUid()).flush();
2329        mWifiLockManager.updateWifiLockWorkSource(binder, ws);
2330    }
2331
2332    @Override
2333    public boolean releaseWifiLock(IBinder binder) {
2334        mLog.trace("releaseWifiLock uid=%").c(Binder.getCallingUid()).flush();
2335        if (mWifiLockManager.releaseWifiLock(binder)) {
2336            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
2337            return true;
2338        }
2339        return false;
2340    }
2341
2342    @Override
2343    public void initializeMulticastFiltering() {
2344        enforceMulticastChangePermission();
2345        mLog.trace("initializeMulticastFiltering uid=%").c(Binder.getCallingUid()).flush();
2346        mWifiMulticastLockManager.initializeFiltering();
2347    }
2348
2349    @Override
2350    public void acquireMulticastLock(IBinder binder, String tag) {
2351        enforceMulticastChangePermission();
2352        mLog.trace("acquireMulticastLock uid=%").c(Binder.getCallingUid()).flush();
2353        mWifiMulticastLockManager.acquireLock(binder, tag);
2354    }
2355
2356    @Override
2357    public void releaseMulticastLock() {
2358        enforceMulticastChangePermission();
2359        mLog.trace("releaseMulticastLock uid=%").c(Binder.getCallingUid()).flush();
2360        mWifiMulticastLockManager.releaseLock();
2361    }
2362
2363    @Override
2364    public boolean isMulticastEnabled() {
2365        enforceAccessPermission();
2366        mLog.trace("isMulticastEnabled uid=%").c(Binder.getCallingUid()).flush();
2367        return mWifiMulticastLockManager.isMulticastEnabled();
2368    }
2369
2370    @Override
2371    public void enableVerboseLogging(int verbose) {
2372        enforceAccessPermission();
2373        mLog.trace("enableVerboseLogging uid=% verbose=%")
2374                .c(Binder.getCallingUid())
2375                .c(verbose).flush();
2376        mFacade.setIntegerSetting(
2377                mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, verbose);
2378        enableVerboseLoggingInternal(verbose);
2379    }
2380
2381    void enableVerboseLoggingInternal(int verbose) {
2382        mWifiStateMachine.enableVerboseLogging(verbose);
2383        mWifiLockManager.enableVerboseLogging(verbose);
2384        mWifiMulticastLockManager.enableVerboseLogging(verbose);
2385        mWifiInjector.getWifiLastResortWatchdog().enableVerboseLogging(verbose);
2386        mWifiInjector.getWifiBackupRestore().enableVerboseLogging(verbose);
2387        LogcatLog.enableVerboseLogging(verbose);
2388    }
2389
2390    @Override
2391    public int getVerboseLoggingLevel() {
2392        enforceAccessPermission();
2393        mLog.trace("getVerboseLoggingLevel uid=%").c(Binder.getCallingUid()).flush();
2394        return mFacade.getIntegerSetting(
2395                mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, 0);
2396    }
2397
2398    @Override
2399    public void enableAggressiveHandover(int enabled) {
2400        enforceAccessPermission();
2401        mLog.trace("enableAggressiveHandover uid=% enabled=%")
2402            .c(Binder.getCallingUid())
2403            .c(enabled)
2404            .flush();
2405        mWifiStateMachine.enableAggressiveHandover(enabled);
2406    }
2407
2408    @Override
2409    public int getAggressiveHandover() {
2410        enforceAccessPermission();
2411        mLog.trace("getAggressiveHandover uid=%").c(Binder.getCallingUid()).flush();
2412        return mWifiStateMachine.getAggressiveHandover();
2413    }
2414
2415    @Override
2416    public void setAllowScansWithTraffic(int enabled) {
2417        enforceAccessPermission();
2418        mLog.trace("setAllowScansWithTraffic uid=% enabled=%")
2419                .c(Binder.getCallingUid())
2420                .c(enabled).flush();
2421        mWifiStateMachine.setAllowScansWithTraffic(enabled);
2422    }
2423
2424    @Override
2425    public int getAllowScansWithTraffic() {
2426        enforceAccessPermission();
2427        mLog.trace("getAllowScansWithTraffic uid=%").c(Binder.getCallingUid()).flush();
2428        return mWifiStateMachine.getAllowScansWithTraffic();
2429    }
2430
2431    @Override
2432    public boolean setEnableAutoJoinWhenAssociated(boolean enabled) {
2433        enforceChangePermission();
2434        mLog.trace("setEnableAutoJoinWhenAssociated uid=% enabled=%")
2435                .c(Binder.getCallingUid())
2436                .c(enabled).flush();
2437        return mWifiStateMachine.setEnableAutoJoinWhenAssociated(enabled);
2438    }
2439
2440    @Override
2441    public boolean getEnableAutoJoinWhenAssociated() {
2442        enforceAccessPermission();
2443        mLog.trace("getEnableAutoJoinWhenAssociated uid=%").c(Binder.getCallingUid()).flush();
2444        return mWifiStateMachine.getEnableAutoJoinWhenAssociated();
2445    }
2446
2447    /* Return the Wifi Connection statistics object */
2448    @Override
2449    public WifiConnectionStatistics getConnectionStatistics() {
2450        enforceAccessPermission();
2451        enforceReadCredentialPermission();
2452        mLog.trace("getConnectionStatistics uid=%").c(Binder.getCallingUid()).flush();
2453        if (mWifiStateMachineChannel != null) {
2454            return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel);
2455        } else {
2456            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
2457            return null;
2458        }
2459    }
2460
2461    @Override
2462    public void factoryReset() {
2463        enforceConnectivityInternalPermission();
2464        mLog.trace("factoryReset uid=%").c(Binder.getCallingUid()).flush();
2465        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2466            return;
2467        }
2468
2469        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
2470            // Turn mobile hotspot off - will also clear any registered LOHS requests when it is
2471            // shut down
2472            stopSoftApInternal();
2473        }
2474
2475        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) {
2476            // Enable wifi
2477            try {
2478                setWifiEnabled(mContext.getOpPackageName(), true);
2479            } catch (RemoteException e) {
2480                /* ignore - local call */
2481            }
2482            // Delete all Wifi SSIDs
2483            if (mWifiStateMachineChannel != null) {
2484                List<WifiConfiguration> networks = mWifiStateMachine.syncGetConfiguredNetworks(
2485                        Binder.getCallingUid(), mWifiStateMachineChannel);
2486                if (networks != null) {
2487                    for (WifiConfiguration config : networks) {
2488                        removeNetwork(config.networkId);
2489                    }
2490                    saveConfiguration();
2491                }
2492            }
2493        }
2494    }
2495
2496    /* private methods */
2497    static boolean logAndReturnFalse(String s) {
2498        Log.d(TAG, s);
2499        return false;
2500    }
2501
2502    public static boolean isValid(WifiConfiguration config) {
2503        String validity = checkValidity(config);
2504        return validity == null || logAndReturnFalse(validity);
2505    }
2506
2507    public static String checkValidity(WifiConfiguration config) {
2508        if (config.allowedKeyManagement == null)
2509            return "allowed kmgmt";
2510
2511        if (config.allowedKeyManagement.cardinality() > 1) {
2512            if (config.allowedKeyManagement.cardinality() != 2) {
2513                return "cardinality != 2";
2514            }
2515            if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
2516                return "not WPA_EAP";
2517            }
2518            if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X))
2519                    && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) {
2520                return "not PSK or 8021X";
2521            }
2522        }
2523        if (config.getIpAssignment() == IpConfiguration.IpAssignment.STATIC) {
2524            StaticIpConfiguration staticIpConf = config.getStaticIpConfiguration();
2525            if (staticIpConf == null) {
2526                return "null StaticIpConfiguration";
2527            }
2528            if (staticIpConf.ipAddress == null) {
2529                return "null static ip Address";
2530            }
2531        }
2532        return null;
2533    }
2534
2535    @Override
2536    public Network getCurrentNetwork() {
2537        enforceAccessPermission();
2538        mLog.trace("getCurrentNetwork uid=%").c(Binder.getCallingUid()).flush();
2539        return mWifiStateMachine.getCurrentNetwork();
2540    }
2541
2542    public static String toHexString(String s) {
2543        if (s == null) {
2544            return "null";
2545        }
2546        StringBuilder sb = new StringBuilder();
2547        sb.append('\'').append(s).append('\'');
2548        for (int n = 0; n < s.length(); n++) {
2549            sb.append(String.format(" %02x", s.charAt(n) & 0xffff));
2550        }
2551        return sb.toString();
2552    }
2553
2554    public void hideCertFromUnaffiliatedUsers(String alias) {
2555        mCertManager.hideCertFromUnaffiliatedUsers(alias);
2556    }
2557
2558    public String[] listClientCertsForCurrentUser() {
2559        return mCertManager.listClientCertsForCurrentUser();
2560    }
2561
2562    /**
2563     * Enable/disable WifiConnectivityManager at runtime
2564     *
2565     * @param enabled true-enable; false-disable
2566     */
2567    @Override
2568    public void enableWifiConnectivityManager(boolean enabled) {
2569        enforceConnectivityInternalPermission();
2570        mLog.trace("enableWifiConnectivityManager uid=% enabled=%")
2571            .c(Binder.getCallingUid())
2572            .c(enabled).flush();
2573        mWifiStateMachine.enableWifiConnectivityManager(enabled);
2574    }
2575
2576    /**
2577     * Retrieve the data to be backed to save the current state.
2578     *
2579     * @return  Raw byte stream of the data to be backed up.
2580     */
2581    @Override
2582    public byte[] retrieveBackupData() {
2583        enforceNetworkSettingsPermission();
2584        mLog.trace("retrieveBackupData uid=%").c(Binder.getCallingUid()).flush();
2585        if (mWifiStateMachineChannel == null) {
2586            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
2587            return null;
2588        }
2589
2590        Slog.d(TAG, "Retrieving backup data");
2591        List<WifiConfiguration> wifiConfigurations =
2592                mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
2593        byte[] backupData =
2594                mWifiBackupRestore.retrieveBackupDataFromConfigurations(wifiConfigurations);
2595        Slog.d(TAG, "Retrieved backup data");
2596        return backupData;
2597    }
2598
2599    /**
2600     * Helper method to restore networks retrieved from backup data.
2601     *
2602     * @param configurations list of WifiConfiguration objects parsed from the backup data.
2603     */
2604    private void restoreNetworks(List<WifiConfiguration> configurations) {
2605        if (configurations == null) {
2606            Slog.e(TAG, "Backup data parse failed");
2607            return;
2608        }
2609        for (WifiConfiguration configuration : configurations) {
2610            int networkId = mWifiStateMachine.syncAddOrUpdateNetwork(
2611                    mWifiStateMachineChannel, configuration);
2612            if (networkId == WifiConfiguration.INVALID_NETWORK_ID) {
2613                Slog.e(TAG, "Restore network failed: " + configuration.configKey());
2614                continue;
2615            }
2616            // Enable all networks restored.
2617            mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, networkId, false);
2618        }
2619    }
2620
2621    /**
2622     * Restore state from the backed up data.
2623     *
2624     * @param data Raw byte stream of the backed up data.
2625     */
2626    @Override
2627    public void restoreBackupData(byte[] data) {
2628        enforceNetworkSettingsPermission();
2629        mLog.trace("restoreBackupData uid=%").c(Binder.getCallingUid()).flush();
2630        if (mWifiStateMachineChannel == null) {
2631            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
2632            return;
2633        }
2634
2635        Slog.d(TAG, "Restoring backup data");
2636        List<WifiConfiguration> wifiConfigurations =
2637                mWifiBackupRestore.retrieveConfigurationsFromBackupData(data);
2638        restoreNetworks(wifiConfigurations);
2639        Slog.d(TAG, "Restored backup data");
2640    }
2641
2642    /**
2643     * Restore state from the older supplicant back up data.
2644     * The old backup data was essentially a backup of wpa_supplicant.conf & ipconfig.txt file.
2645     *
2646     * @param supplicantData Raw byte stream of wpa_supplicant.conf
2647     * @param ipConfigData Raw byte stream of ipconfig.txt
2648     */
2649    public void restoreSupplicantBackupData(byte[] supplicantData, byte[] ipConfigData) {
2650        enforceNetworkSettingsPermission();
2651        mLog.trace("restoreSupplicantBackupData uid=%").c(Binder.getCallingUid()).flush();
2652        if (mWifiStateMachineChannel == null) {
2653            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
2654            return;
2655        }
2656
2657        Slog.d(TAG, "Restoring supplicant backup data");
2658        List<WifiConfiguration> wifiConfigurations =
2659                mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData(
2660                        supplicantData, ipConfigData);
2661        restoreNetworks(wifiConfigurations);
2662        Slog.d(TAG, "Restored supplicant backup data");
2663    }
2664}
2665