• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.wifi;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.net.wifi.WifiConfiguration;
22 import android.util.ArraySet;
23 
24 import com.android.modules.utils.build.SdkLevel;
25 import com.android.wifi.resources.R;
26 
27 import java.io.FileDescriptor;
28 import java.io.PrintWriter;
29 import java.util.Set;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import java.util.concurrent.atomic.AtomicInteger;
32 
33 import javax.annotation.concurrent.ThreadSafe;
34 
35 
36 /** Global wifi service in-memory state that is not persisted. */
37 @ThreadSafe
38 public class WifiGlobals {
39 
40     private final Context mContext;
41 
42     private final AtomicInteger mPollRssiIntervalMillis = new AtomicInteger(-1);
43     private final AtomicBoolean mIpReachabilityDisconnectEnabled = new AtomicBoolean(true);
44     private final AtomicBoolean mIsBluetoothConnected = new AtomicBoolean(false);
45 
46     // These are read from the overlay, cache them after boot up.
47     private final boolean mIsWpa3SaeUpgradeEnabled;
48     private final boolean mIsWpa3SaeUpgradeOffloadEnabled;
49     private final boolean mIsOweUpgradeEnabled;
50     private final boolean mFlushAnqpCacheOnWifiToggleOffEvent;
51     private final boolean mIsWpa3SaeH2eSupported;
52     private final String mP2pDeviceNamePrefix;
53     private final int mP2pDeviceNamePostfixNumDigits;
54     private final int mClientModeImplNumLogRecs;
55     private final boolean mSaveFactoryMacToConfigStoreEnabled;
56     private final int mWifiLowConnectedScoreThresholdToTriggerScanForMbb;
57     private final int mWifiLowConnectedScoreScanPeriodSeconds;
58     private final boolean mWifiAllowInsecureEnterpriseConfiguration;
59     private final boolean mIsP2pMacRandomizationSupported;
60     private final int mPollRssiShortIntervalMillis;
61     private final int mPollRssiLongIntervalMillis;
62     private final int mClientRssiMonitorThresholdDbm;
63     private final int mClientRssiMonitorHysteresisDb;
64     private final boolean mAdjustPollRssiIntervalEnabled;
65     private final boolean mWifiInterfaceAddedSelfRecoveryEnabled;
66     private final int mNetworkNotFoundEventThreshold;
67     private final boolean mIsWepDeprecated;
68     private final boolean mIsWpaPersonalDeprecated;
69 
70     // This is set by WifiManager#setVerboseLoggingEnabled(int).
71     private boolean mIsShowKeyVerboseLoggingModeEnabled = false;
72     private boolean mIsUsingExternalScorer = false;
73     private boolean mDisableUnwantedNetworkOnLowRssi = false;
74     private Set<String> mMacRandomizationUnsupportedSsidPrefixes = new ArraySet<>();
75 
WifiGlobals(Context context)76     public WifiGlobals(Context context) {
77         mContext = context;
78 
79         mIsWpa3SaeUpgradeEnabled = mContext.getResources()
80                 .getBoolean(R.bool.config_wifiSaeUpgradeEnabled);
81         mIsWpa3SaeUpgradeOffloadEnabled = mContext.getResources()
82                 .getBoolean(R.bool.config_wifiSaeUpgradeOffloadEnabled);
83         mIsOweUpgradeEnabled = mContext.getResources()
84                 .getBoolean(R.bool.config_wifiOweUpgradeEnabled);
85         mFlushAnqpCacheOnWifiToggleOffEvent = mContext.getResources()
86                 .getBoolean(R.bool.config_wifiFlushAnqpCacheOnWifiToggleOffEvent);
87         mIsWpa3SaeH2eSupported = mContext.getResources()
88                 .getBoolean(R.bool.config_wifiSaeH2eSupported);
89         mP2pDeviceNamePrefix = mContext.getResources()
90                 .getString(R.string.config_wifiP2pDeviceNamePrefix);
91         mP2pDeviceNamePostfixNumDigits = mContext.getResources()
92                 .getInteger(R.integer.config_wifiP2pDeviceNamePostfixNumDigits);
93         mClientModeImplNumLogRecs = mContext.getResources()
94                 .getInteger(R.integer.config_wifiClientModeImplNumLogRecs);
95         mSaveFactoryMacToConfigStoreEnabled = mContext.getResources()
96                 .getBoolean(R.bool.config_wifiSaveFactoryMacToWifiConfigStore);
97         mWifiLowConnectedScoreThresholdToTriggerScanForMbb = mContext.getResources().getInteger(
98                 R.integer.config_wifiLowConnectedScoreThresholdToTriggerScanForMbb);
99         mWifiLowConnectedScoreScanPeriodSeconds = mContext.getResources().getInteger(
100                 R.integer.config_wifiLowConnectedScoreScanPeriodSeconds);
101         mWifiAllowInsecureEnterpriseConfiguration = mContext.getResources().getBoolean(
102                 R.bool.config_wifiAllowInsecureEnterpriseConfigurationsForSettingsAndSUW);
103         mIsP2pMacRandomizationSupported = mContext.getResources().getBoolean(
104                 R.bool.config_wifi_p2p_mac_randomization_supported);
105         mPollRssiIntervalMillis.set(mContext.getResources().getInteger(
106                 R.integer.config_wifiPollRssiIntervalMilliseconds));
107         mPollRssiShortIntervalMillis = mContext.getResources().getInteger(
108                 R.integer.config_wifiPollRssiIntervalMilliseconds);
109         mPollRssiLongIntervalMillis = mContext.getResources().getInteger(
110                 R.integer.config_wifiPollRssiLongIntervalMilliseconds);
111         mClientRssiMonitorThresholdDbm = mContext.getResources().getInteger(
112                 R.integer.config_wifiClientRssiMonitorThresholdDbm);
113         mClientRssiMonitorHysteresisDb = mContext.getResources().getInteger(
114                 R.integer.config_wifiClientRssiMonitorHysteresisDb);
115         mAdjustPollRssiIntervalEnabled = mContext.getResources().getBoolean(
116                 R.bool.config_wifiAdjustPollRssiIntervalEnabled);
117         mWifiInterfaceAddedSelfRecoveryEnabled = mContext.getResources().getBoolean(
118                 R.bool.config_wifiInterfaceAddedSelfRecoveryEnabled);
119         mDisableUnwantedNetworkOnLowRssi = mContext.getResources().getBoolean(
120                 R.bool.config_wifiDisableUnwantedNetworkOnLowRssi);
121         mNetworkNotFoundEventThreshold = mContext.getResources().getInteger(
122                 R.integer.config_wifiNetworkNotFoundEventThreshold);
123         mIsWepDeprecated = mContext.getResources()
124                 .getBoolean(R.bool.config_wifiWepDeprecated);
125         mIsWpaPersonalDeprecated = mContext.getResources()
126                 .getBoolean(R.bool.config_wifiWpaPersonalDeprecated);
127         Set<String> unsupportedSsidPrefixes = new ArraySet<>(mContext.getResources().getStringArray(
128                 R.array.config_wifiForceDisableMacRandomizationSsidPrefixList));
129         if (!unsupportedSsidPrefixes.isEmpty()) {
130             for (String ssid : unsupportedSsidPrefixes) {
131                 String cleanedSsid = ssid.length() > 1 && (ssid.charAt(0) == '"')
132                         && (ssid.charAt(ssid.length() - 1) == '"')
133                         ? ssid.substring(0, ssid.length() - 1) : ssid;
134                 mMacRandomizationUnsupportedSsidPrefixes.add(cleanedSsid);
135             }
136         }
137     }
138 
getMacRandomizationUnsupportedSsidPrefixes()139     public Set<String> getMacRandomizationUnsupportedSsidPrefixes() {
140         return mMacRandomizationUnsupportedSsidPrefixes;
141     }
142 
143     /** Get the interval between RSSI polls, in milliseconds. */
getPollRssiIntervalMillis()144     public int getPollRssiIntervalMillis() {
145         return mPollRssiIntervalMillis.get();
146     }
147 
148     /** Set the interval between RSSI polls, in milliseconds. */
setPollRssiIntervalMillis(int newPollIntervalMillis)149     public void setPollRssiIntervalMillis(int newPollIntervalMillis) {
150         mPollRssiIntervalMillis.set(newPollIntervalMillis);
151     }
152 
153     /** Returns whether CMD_IP_REACHABILITY_LOST events should trigger disconnects. */
getIpReachabilityDisconnectEnabled()154     public boolean getIpReachabilityDisconnectEnabled() {
155         return mIpReachabilityDisconnectEnabled.get();
156     }
157 
158     /** Sets whether CMD_IP_REACHABILITY_LOST events should trigger disconnects. */
setIpReachabilityDisconnectEnabled(boolean enabled)159     public void setIpReachabilityDisconnectEnabled(boolean enabled) {
160         mIpReachabilityDisconnectEnabled.set(enabled);
161     }
162 
163     /** Set whether bluetooth is enabled. */
setBluetoothEnabled(boolean isEnabled)164     public void setBluetoothEnabled(boolean isEnabled) {
165         // If BT was connected and then turned off, there is no CONNECTION_STATE_CHANGE message.
166         // So set mIsBluetoothConnected to false if we get a bluetooth disable while connected.
167         // But otherwise, Bluetooth being turned on doesn't mean that we're connected.
168         if (!isEnabled) {
169             mIsBluetoothConnected.set(false);
170         }
171     }
172 
173     /** Set whether bluetooth is connected. */
setBluetoothConnected(boolean isConnected)174     public void setBluetoothConnected(boolean isConnected) {
175         mIsBluetoothConnected.set(isConnected);
176     }
177 
178     /** Get whether bluetooth is connected */
isBluetoothConnected()179     public boolean isBluetoothConnected() {
180         return mIsBluetoothConnected.get();
181     }
182 
183     /**
184      * Helper method to check if Connected MAC Randomization is supported - onDown events are
185      * skipped if this feature is enabled (b/72459123).
186      *
187      * @return boolean true if Connected MAC randomization is supported, false otherwise
188      */
isConnectedMacRandomizationEnabled()189     public boolean isConnectedMacRandomizationEnabled() {
190         return mContext.getResources().getBoolean(
191                 R.bool.config_wifi_connected_mac_randomization_supported);
192     }
193 
194     /**
195      * Helper method to check if WEP networks are deprecated.
196      *
197      * @return boolean true if WEP networks are deprecated, false otherwise.
198      */
isWepDeprecated()199     public boolean isWepDeprecated() {
200         return mIsWepDeprecated;
201     }
202 
203     /**
204      * Helper method to check if WPA-Personal networks are deprecated.
205      *
206      * @return boolean true if WPA-Personal networks are deprecated, false otherwise.
207      */
isWpaPersonalDeprecated()208     public boolean isWpaPersonalDeprecated() {
209         return mIsWpaPersonalDeprecated;
210     }
211 
212     /**
213      * Helper method to check if the device may not connect to the configuration
214      * due to deprecated security type
215      */
isDeprecatedSecurityTypeNetwork(@ullable WifiConfiguration config)216     public boolean isDeprecatedSecurityTypeNetwork(@Nullable WifiConfiguration config) {
217         if (config == null) {
218             return false;
219         }
220         if (isWepDeprecated() && config.isSecurityType(WifiConfiguration.SECURITY_TYPE_WEP)) {
221             return true;
222         }
223         if (isWpaPersonalDeprecated() && config.isWpaPersonalOnlyConfiguration()) {
224             return true;
225         }
226         return false;
227     }
228 
229     /**
230      * Help method to check if WPA3 SAE auto-upgrade is enabled.
231      *
232      * @return boolean true if auto-upgrade is enabled, false otherwise.
233      */
isWpa3SaeUpgradeEnabled()234     public boolean isWpa3SaeUpgradeEnabled() {
235         return mIsWpa3SaeUpgradeEnabled;
236     }
237 
238     /**
239      * Help method to check if WPA3 SAE auto-upgrade offload is enabled.
240      *
241      * @return boolean true if auto-upgrade offload is enabled, false otherwise.
242      */
isWpa3SaeUpgradeOffloadEnabled()243     public boolean isWpa3SaeUpgradeOffloadEnabled() {
244         return mIsWpa3SaeUpgradeOffloadEnabled;
245     }
246 
247     /**
248      * Help method to check if OWE auto-upgrade is enabled.
249      *
250      * @return boolean true if auto-upgrade is enabled, false otherwise.
251      */
isOweUpgradeEnabled()252     public boolean isOweUpgradeEnabled() {
253         // OWE auto-upgrade is supported on S or newer releases.
254         return SdkLevel.isAtLeastS() && mIsOweUpgradeEnabled;
255     }
256 
257     /**
258      * Help method to check if the setting to flush ANQP cache when Wi-Fi is toggled off.
259      *
260      * @return boolean true to flush ANQP cache on Wi-Fi toggle off event, false otherwise.
261      */
flushAnqpCacheOnWifiToggleOffEvent()262     public boolean flushAnqpCacheOnWifiToggleOffEvent() {
263         return mFlushAnqpCacheOnWifiToggleOffEvent;
264     }
265 
266     /*
267      * Help method to check if WPA3 SAE Hash-to-Element is supported on this device.
268      *
269      * @return boolean true if supported;otherwise false.
270      */
isWpa3SaeH2eSupported()271     public boolean isWpa3SaeH2eSupported() {
272         return mIsWpa3SaeH2eSupported;
273     }
274 
275     /** Set if show key verbose logging mode is enabled. */
setShowKeyVerboseLoggingModeEnabled(boolean enable)276     public void setShowKeyVerboseLoggingModeEnabled(boolean enable) {
277         mIsShowKeyVerboseLoggingModeEnabled = enable;
278     }
279 
280     /** Check if show key verbose logging mode is enabled. */
getShowKeyVerboseLoggingModeEnabled()281     public boolean getShowKeyVerboseLoggingModeEnabled() {
282         return mIsShowKeyVerboseLoggingModeEnabled;
283     }
284 
285     /** Set whether the external scorer is being used **/
setUsingExternalScorer(boolean isUsingExternalScorer)286     public void setUsingExternalScorer(boolean isUsingExternalScorer) {
287         mIsUsingExternalScorer = isUsingExternalScorer;
288     }
289 
290     /** Get whether the external scorer is being used **/
isUsingExternalScorer()291     public boolean isUsingExternalScorer() {
292         return mIsUsingExternalScorer;
293     }
294 
295     /** Get the prefix of the default wifi p2p device name. */
getWifiP2pDeviceNamePrefix()296     public String getWifiP2pDeviceNamePrefix() {
297         return mP2pDeviceNamePrefix;
298     }
299 
300     /** Get the number of the default wifi p2p device name postfix digit. */
getWifiP2pDeviceNamePostfixNumDigits()301     public int getWifiP2pDeviceNamePostfixNumDigits() {
302         return mP2pDeviceNamePostfixNumDigits;
303     }
304 
305     /** Get the number of log records to maintain. */
getClientModeImplNumLogRecs()306     public int getClientModeImplNumLogRecs() {
307         return mClientModeImplNumLogRecs;
308     }
309 
310     /** Get whether to use the saved factory MAC address when available **/
isSaveFactoryMacToConfigStoreEnabled()311     public boolean isSaveFactoryMacToConfigStoreEnabled() {
312         return mSaveFactoryMacToConfigStoreEnabled;
313     }
314 
315     /** Get the low score threshold to do scan for MBB when external scorer is not used. **/
getWifiLowConnectedScoreThresholdToTriggerScanForMbb()316     public int getWifiLowConnectedScoreThresholdToTriggerScanForMbb() {
317         return mWifiLowConnectedScoreThresholdToTriggerScanForMbb;
318     }
319 
320     /** Get the minimum period between the extra scans triggered for MBB when score is low **/
getWifiLowConnectedScoreScanPeriodSeconds()321     public int getWifiLowConnectedScoreScanPeriodSeconds() {
322         return mWifiLowConnectedScoreScanPeriodSeconds;
323     }
324 
325     /** Get whether or not insecure enterprise configuration is allowed. */
isInsecureEnterpriseConfigurationAllowed()326     public boolean isInsecureEnterpriseConfigurationAllowed() {
327         return mWifiAllowInsecureEnterpriseConfiguration;
328     }
329 
330     /** Get whether or not P2P MAC randomization is supported */
isP2pMacRandomizationSupported()331     public boolean isP2pMacRandomizationSupported() {
332         return mIsP2pMacRandomizationSupported;
333     }
334 
335     /** Get the regular (short) interval between RSSI polls, in milliseconds. */
getPollRssiShortIntervalMillis()336     public int getPollRssiShortIntervalMillis() {
337         return mPollRssiShortIntervalMillis;
338     }
339 
340     /**
341      * Get the long interval between RSSI polls, in milliseconds. The long interval is to
342      * reduce power consumption of the polls. This value should be greater than the regular
343      * interval.
344      */
getPollRssiLongIntervalMillis()345     public int getPollRssiLongIntervalMillis() {
346         return mPollRssiLongIntervalMillis;
347     }
348 
349     /**
350      * Get the RSSI threshold for client mode RSSI monitor, in dBm. If the device is stationary
351      * and current RSSI >= Threshold + Hysteresis value, set long interval and enable RSSI
352      * monitoring using the RSSI threshold. If device is non-stationary or current RSSI <=
353      * Threshold, set regular interval and disable RSSI monitoring.
354      */
getClientRssiMonitorThresholdDbm()355     public int getClientRssiMonitorThresholdDbm() {
356         return mClientRssiMonitorThresholdDbm;
357     }
358 
359     /**
360      * Get the hysteresis value in dB for the client mode RSSI monitor threshold. It can avoid
361      * frequent switch between regular and long polling intervals.
362      */
getClientRssiMonitorHysteresisDb()363     public int getClientRssiMonitorHysteresisDb() {
364         return mClientRssiMonitorHysteresisDb;
365     }
366 
367     /**
368      * Get whether adjusting the RSSI polling interval between regular and long intervals
369      * is enabled.
370      */
isAdjustPollRssiIntervalEnabled()371     public boolean isAdjustPollRssiIntervalEnabled() {
372         return mAdjustPollRssiIntervalEnabled;
373     }
374 
375     /**
376      * Get whether hot-plugging an interface will trigger a restart of the wifi stack.
377      */
isWifiInterfaceAddedSelfRecoveryEnabled()378     public boolean isWifiInterfaceAddedSelfRecoveryEnabled() {
379         return mWifiInterfaceAddedSelfRecoveryEnabled;
380     }
381 
382     /**
383      * Get whether to temporarily disable a unwanted network that has low RSSI.
384      */
disableUnwantedNetworkOnLowRssi()385     public boolean disableUnwantedNetworkOnLowRssi() {
386         return mDisableUnwantedNetworkOnLowRssi;
387     }
388 
389     /**
390      * Get the threshold to use for blocking a network due to NETWORK_NOT_FOUND_EVENT failure.
391      */
getNetworkNotFoundEventThreshold()392     public int getNetworkNotFoundEventThreshold() {
393         return mNetworkNotFoundEventThreshold;
394     }
395 
396     /** Dump method for debugging */
dump(FileDescriptor fd, PrintWriter pw, String[] args)397     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
398         pw.println("Dump of WifiGlobals");
399         pw.println("mPollRssiIntervalMillis=" + mPollRssiIntervalMillis.get());
400         pw.println("mIpReachabilityDisconnectEnabled=" + mIpReachabilityDisconnectEnabled.get());
401         pw.println("mIsBluetoothConnected=" + mIsBluetoothConnected.get());
402         pw.println("mIsWpa3SaeUpgradeEnabled=" + mIsWpa3SaeUpgradeEnabled);
403         pw.println("mIsWpa3SaeUpgradeOffloadEnabled=" + mIsWpa3SaeUpgradeOffloadEnabled);
404         pw.println("mIsOweUpgradeEnabled=" + mIsOweUpgradeEnabled);
405         pw.println("mFlushAnqpCacheOnWifiToggleOffEvent=" + mFlushAnqpCacheOnWifiToggleOffEvent);
406         pw.println("mIsWpa3SaeH2eSupported=" + mIsWpa3SaeH2eSupported);
407         pw.println("mP2pDeviceNamePrefix=" + mP2pDeviceNamePrefix);
408         pw.println("mP2pDeviceNamePostfixNumDigits=" + mP2pDeviceNamePostfixNumDigits);
409         pw.println("mClientModeImplNumLogRecs=" + mClientModeImplNumLogRecs);
410         pw.println("mSaveFactoryMacToConfigStoreEnabled=" + mSaveFactoryMacToConfigStoreEnabled);
411         pw.println("mWifiLowConnectedScoreThresholdToTriggerScanForMbb="
412                 + mWifiLowConnectedScoreThresholdToTriggerScanForMbb);
413         pw.println("mWifiLowConnectedScoreScanPeriodSeconds="
414                 + mWifiLowConnectedScoreScanPeriodSeconds);
415         pw.println("mIsUsingExternalScorer="
416                 + mIsUsingExternalScorer);
417         pw.println("mWifiAllowInsecureEnterpriseConfiguration="
418                 + mWifiAllowInsecureEnterpriseConfiguration);
419         pw.println("mIsP2pMacRandomizationSupported" + mIsP2pMacRandomizationSupported);
420         pw.println("mWifiInterfaceAddedSelfRecoveryEnabled="
421                 + mWifiInterfaceAddedSelfRecoveryEnabled);
422         pw.println("mDisableUnwantedNetworkOnLowRssi=" + mDisableUnwantedNetworkOnLowRssi);
423         pw.println("mNetworkNotFoundEventThreshold=" + mNetworkNotFoundEventThreshold);
424         pw.println("mIsWepDeprecated=" + mIsWepDeprecated);
425         pw.println("mIsWpaPersonalDeprecated=" + mIsWpaPersonalDeprecated);
426     }
427 }
428