diff options
author | Roshan Pius <rpius@google.com> | 2018-10-15 23:30:15 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-10-15 23:30:15 +0000 |
commit | b6b5b0d3859fec5ef0af9018a3334553f0a19ccf (patch) | |
tree | d79fdb455f52e937201a1859fcddd416eb792046 /service | |
parent | 5e35dc1f0ab42b7ee081d8ae94c5fc41e0a3cadd (diff) | |
parent | 29f2a62b0e6d38d5cc8a6b5ed5b096d6f3654340 (diff) |
Merge changes I5b1baad8,I24038fe5
* changes:
ClientModeImpl: Construct network factories in constructor
WifiNetworkFactory: Refactor to a new class
Diffstat (limited to 'service')
5 files changed, 244 insertions, 139 deletions
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index a4eb8eaf1..dd63d10b8 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -41,11 +41,9 @@ import android.net.MacAddress; import android.net.Network; import android.net.NetworkAgent; import android.net.NetworkCapabilities; -import android.net.NetworkFactory; import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; import android.net.NetworkMisc; -import android.net.NetworkRequest; import android.net.NetworkUtils; import android.net.RouteInfo; import android.net.StaticIpConfiguration; @@ -85,7 +83,6 @@ import android.util.Pair; import android.util.SparseArray; import com.android.internal.R; -import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.app.IBatteryStats; import com.android.internal.util.AsyncChannel; @@ -133,7 +130,6 @@ import java.util.concurrent.atomic.AtomicInteger; public class ClientModeImpl extends StateMachine { private static final String NETWORKTYPE = "WIFI"; - private static final String NETWORKTYPE_UNTRUSTED = "WIFI_UT"; @VisibleForTesting public static final short NUM_LOG_RECS_NORMAL = 100; @VisibleForTesting public static final short NUM_LOG_RECS_VERBOSE_LOW_MEMORY = 200; @VisibleForTesting public static final short NUM_LOG_RECS_VERBOSE = 3000; @@ -387,14 +383,9 @@ public class ClientModeImpl extends StateMachine { // Used to initiate a connection with WifiP2pService private AsyncChannel mWifiP2pChannel; - @GuardedBy("mWifiReqCountLock") - private int mConnectionReqCount = 0; private WifiNetworkFactory mNetworkFactory; - @GuardedBy("mWifiReqCountLock") - private int mUntrustedReqCount = 0; private UntrustedWifiNetworkFactory mUntrustedNetworkFactory; private WifiNetworkAgent mNetworkAgent; - private final Object mWifiReqCountLock = new Object(); private byte[] mRssiRanges; @@ -816,6 +807,16 @@ public class ClientModeImpl extends StateMachine { // TODO - needs to be a bit more dynamic mDfltNetworkCapabilities = new NetworkCapabilities(mNetworkCapabilitiesFilter); + // Make the network factories. + mNetworkFactory = mWifiInjector.makeWifiNetworkFactory( + mNetworkCapabilitiesFilter, mWifiConnectivityManager); + // We can't filter untrusted network in the capabilities filter because a trusted + // network would still satisfy a request that accepts untrusted ones. + // We need a second network factory for untrusted network requests because we need a + // different score filter for these requests. + mUntrustedNetworkFactory = mWifiInjector.makeUntrustedWifiNetworkFactory( + mNetworkCapabilitiesFilter, mWifiConnectivityManager); + IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); @@ -1916,18 +1917,8 @@ public class ClientModeImpl extends StateMachine { pw.println("mUserWantsSuspendOpt " + mUserWantsSuspendOpt); pw.println("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled); mCountryCode.dump(fd, pw, args); - - if (mNetworkFactory != null) { - mNetworkFactory.dump(fd, pw, args); - } else { - pw.println("mNetworkFactory is not initialized"); - } - - if (mUntrustedNetworkFactory != null) { - mUntrustedNetworkFactory.dump(fd, pw, args); - } else { - pw.println("mUntrustedNetworkFactory is not initialized"); - } + mNetworkFactory.dump(fd, pw, args); + mUntrustedNetworkFactory.dump(fd, pw, args); pw.println("Wlan Wake Reasons:" + mWifiNative.getWlanWakeReasonCount()); pw.println(); @@ -2432,10 +2423,15 @@ public class ClientModeImpl extends StateMachine { if (mVerboseLoggingEnabled) log("handleScreenStateChanged Exit: " + screenOn); } - private void checkAndSetConnectivityInstance() { + private boolean checkAndSetConnectivityInstance() { if (mCm == null) { mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); } + if (mCm == null) { + Log.e(TAG, "Cannot retrieve connectivity service"); + return false; + } + return true; } private void setSuspendOptimizationsNative(int reason, boolean enabled) { @@ -3100,98 +3096,10 @@ public class ClientModeImpl extends StateMachine { return true; } - private class WifiNetworkFactory extends NetworkFactory { - WifiNetworkFactory(Looper l, Context c, String tag, NetworkCapabilities f) { - super(l, c, tag, f); - } - - @Override - protected void needNetworkFor(NetworkRequest networkRequest, int score) { - synchronized (mWifiReqCountLock) { - if (++mConnectionReqCount == 1) { - if (mUntrustedReqCount == 0) { - mWifiConnectivityManager.enable(true); - } - } - } - } - - @Override - protected void releaseNetworkFor(NetworkRequest networkRequest) { - synchronized (mWifiReqCountLock) { - if (--mConnectionReqCount == 0) { - if (mUntrustedReqCount == 0) { - mWifiConnectivityManager.enable(false); - } - } - } - } - - @Override - public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - pw.println("mConnectionReqCount " + mConnectionReqCount); - } - - } - - private class UntrustedWifiNetworkFactory extends NetworkFactory { - UntrustedWifiNetworkFactory(Looper l, Context c, String tag, NetworkCapabilities f) { - super(l, c, tag, f); - } - - @Override - protected void needNetworkFor(NetworkRequest networkRequest, int score) { - if (!networkRequest.networkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_TRUSTED)) { - synchronized (mWifiReqCountLock) { - if (++mUntrustedReqCount == 1) { - if (mConnectionReqCount == 0) { - mWifiConnectivityManager.enable(true); - } - mWifiConnectivityManager.setUntrustedConnectionAllowed(true); - } - } - } - } - - @Override - protected void releaseNetworkFor(NetworkRequest networkRequest) { - if (!networkRequest.networkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_TRUSTED)) { - synchronized (mWifiReqCountLock) { - if (--mUntrustedReqCount == 0) { - mWifiConnectivityManager.setUntrustedConnectionAllowed(false); - if (mConnectionReqCount == 0) { - mWifiConnectivityManager.enable(false); - } - } - } - } - } - - @Override - public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - pw.println("mUntrustedReqCount " + mUntrustedReqCount); - } - } - - void maybeRegisterNetworkFactory() { - if (mNetworkFactory == null) { - checkAndSetConnectivityInstance(); - if (mCm != null) { - mNetworkFactory = new WifiNetworkFactory(getHandler().getLooper(), mContext, - NETWORKTYPE, mNetworkCapabilitiesFilter); - mNetworkFactory.setScoreFilter(60); - mNetworkFactory.register(); - - // We can't filter untrusted network in the capabilities filter because a trusted - // network would still satisfy a request that accepts untrusted ones. - mUntrustedNetworkFactory = new UntrustedWifiNetworkFactory(getHandler().getLooper(), - mContext, NETWORKTYPE_UNTRUSTED, mNetworkCapabilitiesFilter); - mUntrustedNetworkFactory.setScoreFilter(Integer.MAX_VALUE); - mUntrustedNetworkFactory.register(); - } - } + void registerNetworkFactory() { + if (!checkAndSetConnectivityInstance()) return; + mNetworkFactory.register(); + mUntrustedNetworkFactory.register(); } /** @@ -3380,7 +3288,7 @@ public class ClientModeImpl extends StateMachine { if (!mWifiConfigManager.loadFromStore()) { Log.e(TAG, "Failed to load from config store"); } - maybeRegisterNetworkFactory(); + registerNetworkFactory(); break; case CMD_SCREEN_STATE_CHANGED: handleScreenStateChanged(message.arg1 != 0); @@ -4129,20 +4037,17 @@ public class ClientModeImpl extends StateMachine { int uid = message.arg2; bssid = (String) message.obj; - synchronized (mWifiReqCountLock) { - if (!hasConnectionRequests()) { - if (mNetworkAgent == null) { - loge("CMD_START_CONNECT but no requests and not connected," - + " bailing"); - break; - } else if (!mWifiPermissionsUtil.checkNetworkSettingsPermission(uid)) { - loge("CMD_START_CONNECT but no requests and connected, but app " - + "does not have sufficient permissions, bailing"); - break; - } + if (!hasConnectionRequests()) { + if (mNetworkAgent == null) { + loge("CMD_START_CONNECT but no requests and not connected," + + " bailing"); + break; + } else if (!mWifiPermissionsUtil.checkNetworkSettingsPermission(uid)) { + loge("CMD_START_CONNECT but no requests and connected, but app " + + "does not have sufficient permissions, bailing"); + break; } } - config = mWifiConfigManager.getConfiguredNetworkWithoutMasking(netId); logd("CMD_START_CONNECT sup state " + mSupplicantStateTracker.getSupplicantStateName() @@ -5874,10 +5779,10 @@ public class ClientModeImpl extends StateMachine { /** * Check if there is any connection request for WiFi network. - * Note, caller of this helper function must acquire mWifiReqCountLock. */ private boolean hasConnectionRequests() { - return mConnectionReqCount > 0 || mUntrustedReqCount > 0; + return mNetworkFactory.hasConnectionRequests() + || mUntrustedNetworkFactory.hasConnectionRequests(); } /** diff --git a/service/java/com/android/server/wifi/UntrustedWifiNetworkFactory.java b/service/java/com/android/server/wifi/UntrustedWifiNetworkFactory.java new file mode 100644 index 000000000..7eae75827 --- /dev/null +++ b/service/java/com/android/server/wifi/UntrustedWifiNetworkFactory.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import android.content.Context; +import android.net.NetworkCapabilities; +import android.net.NetworkFactory; +import android.net.NetworkRequest; +import android.os.Looper; +import android.util.Log; + +import java.io.FileDescriptor; +import java.io.PrintWriter; + +/** + * Network factory to handle untrusted (used for {@link ScoredNetworkEvaluator}) wifi network + * requests. + */ +public class UntrustedWifiNetworkFactory extends NetworkFactory { + private static final String TAG = "UntrustedWifiNetworkFactory"; + private static final int SCORE_FILTER = Integer.MAX_VALUE; + + private final WifiConnectivityManager mWifiConnectivityManager; + private int mConnectionReqCount = 0; + + public UntrustedWifiNetworkFactory(Looper l, Context c, NetworkCapabilities f, + WifiConnectivityManager connectivityManager) { + super(l, c, TAG, f); + mWifiConnectivityManager = connectivityManager; + + setScoreFilter(SCORE_FILTER); + } + + @Override + protected void needNetworkFor(NetworkRequest networkRequest, int score) { + if (!networkRequest.networkCapabilities.hasCapability( + NetworkCapabilities.NET_CAPABILITY_TRUSTED)) { + if (++mConnectionReqCount == 1) { + mWifiConnectivityManager.setUntrustedConnectionAllowed(true); + } + } + } + + @Override + protected void releaseNetworkFor(NetworkRequest networkRequest) { + if (!networkRequest.networkCapabilities.hasCapability( + NetworkCapabilities.NET_CAPABILITY_TRUSTED)) { + if (mConnectionReqCount == 0) { + Log.e(TAG, "No valid network request to release"); + return; + } + if (--mConnectionReqCount == 0) { + mWifiConnectivityManager.setUntrustedConnectionAllowed(false); + } + } + } + + @Override + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + super.dump(fd, pw, args); + pw.println(TAG + ": mConnectionReqCount " + mConnectionReqCount); + } + + /** + * Check if there is at-least one connection request. + */ + public boolean hasConnectionRequests() { + return mConnectionReqCount > 0; + } +} + diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 07d9cd393..cd3e4c8c2 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -153,9 +153,11 @@ public class WifiConnectivityManager { private boolean mDbg = false; private boolean mWifiEnabled = false; private boolean mWifiConnectivityManagerEnabled = false; + private boolean mRunning = false; private boolean mScreenOn = false; private int mWifiState = WIFI_STATE_UNKNOWN; private boolean mUntrustedConnectionAllowed = false; + private boolean mTrustedConnectionAllowed = false; private int mScanRestartCount = 0; private int mSingleScanRestartCount = 0; private int mTotalConnectivityAttemptsRateLimited = 0; @@ -577,8 +579,7 @@ public class WifiConnectivityManager { WifiLastResortWatchdog wifiLastResortWatchdog, OpenNetworkNotifier openNetworkNotifier, CarrierNetworkNotifier carrierNetworkNotifier, CarrierNetworkConfig carrierNetworkConfig, WifiMetrics wifiMetrics, Looper looper, - Clock clock, LocalLog localLog, boolean enable, - SavedNetworkEvaluator savedNetworkEvaluator, + Clock clock, LocalLog localLog, SavedNetworkEvaluator savedNetworkEvaluator, ScoredNetworkEvaluator scoredNetworkEvaluator, PasspointNetworkEvaluator passpointNetworkEvaluator) { mStateMachine = stateMachine; @@ -648,11 +649,6 @@ public class WifiConnectivityManager { // Listen to WifiConfigManager network update events mConfigManager.setOnSavedNetworkUpdateListener(new OnSavedNetworkUpdateListener()); - - mWifiConnectivityManagerEnabled = enable; - - localLog("ConnectivityScanManager initialized and " - + (enable ? "enabled" : "disabled")); } /** @@ -1152,13 +1148,30 @@ public class WifiConnectivityManager { } /** - * Handler when user toggles whether untrusted connection is allowed + * Handler when connectivity allows/disallows trusted connections (all of autojoin). + */ + public void setTrustedConnectionAllowed(boolean allowed) { + localLog("setTrustedConnectionAllowed: allowed=" + allowed); + + if (mTrustedConnectionAllowed != allowed) { + mTrustedConnectionAllowed = allowed; + // Enable auto-join if we have any pending network request (trusted or untrusted). + enable(mUntrustedConnectionAllowed || mTrustedConnectionAllowed); + startConnectivityScan(SCAN_IMMEDIATELY); + } + } + + + /** + * Handler when connectivity allows/disallows untrusted connections (ephemeral networks). */ public void setUntrustedConnectionAllowed(boolean allowed) { localLog("setUntrustedConnectionAllowed: allowed=" + allowed); if (mUntrustedConnectionAllowed != allowed) { mUntrustedConnectionAllowed = allowed; + // Enable auto-join if we have any pending network request (trusted or untrusted). + enable(mUntrustedConnectionAllowed || mTrustedConnectionAllowed); startConnectivityScan(SCAN_IMMEDIATELY); } } @@ -1374,16 +1387,20 @@ public class WifiConnectivityManager { * Start WifiConnectivityManager */ private void start() { + if (mRunning) return; retrieveWifiScanner(); mConnectivityHelper.getFirmwareRoamingInfo(); clearBssidBlacklist(); startConnectivityScan(SCAN_IMMEDIATELY); + mRunning = true; } /** * Stop and reset WifiConnectivityManager */ private void stop() { + if (!mRunning) return; + mRunning = false; stopConnectivityScan(); clearBssidBlacklist(); resetLastPeriodicSingleScanTimeStamp(); @@ -1417,7 +1434,6 @@ public class WifiConnectivityManager { mWifiEnabled = enable; updateRunningState(); - } /** diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 24e125a29..bb0a37176 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -21,6 +21,7 @@ import android.app.ActivityManager; import android.app.AppOpsManager; import android.content.Context; import android.hardware.SystemSensorManager; +import android.net.NetworkCapabilities; import android.net.NetworkKey; import android.net.NetworkScoreManager; import android.net.wifi.IWifiScanner; @@ -533,10 +534,30 @@ public class WifiInjector { mWifiNetworkSelector, mWifiConnectivityHelper, mWifiLastResortWatchdog, mOpenNetworkNotifier, mCarrierNetworkNotifier, mCarrierNetworkConfig, mWifiMetrics, mWifiCoreHandlerThread.getLooper(), - mClock, mConnectivityLocalLog, true, + mClock, mConnectivityLocalLog, mSavedNetworkEvaluator, mScoredNetworkEvaluator, mPasspointNetworkEvaluator); } + /** + * Construct a new instance of {@link WifiNetworkFactory}. + * TODO(b/116233964): Remove cyclic dependency between WifiConnectivityManager & ClientModeImpl. + */ + public WifiNetworkFactory makeWifiNetworkFactory( + NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager) { + return new WifiNetworkFactory( + mWifiCoreHandlerThread.getLooper(), mContext, nc, wifiConnectivityManager); + } + + /** + * Construct a new instance of {@link UntrustedWifiNetworkFactory}. + * TODO(b/116233964): Remove cyclic dependency between WifiConnectivityManager & ClientModeImpl. + */ + public UntrustedWifiNetworkFactory makeUntrustedWifiNetworkFactory( + NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager) { + return new UntrustedWifiNetworkFactory( + mWifiCoreHandlerThread.getLooper(), mContext, nc, wifiConnectivityManager); + } + public WifiPermissionsUtil getWifiPermissionsUtil() { return mWifiPermissionsUtil; } diff --git a/service/java/com/android/server/wifi/WifiNetworkFactory.java b/service/java/com/android/server/wifi/WifiNetworkFactory.java new file mode 100644 index 000000000..489601d0d --- /dev/null +++ b/service/java/com/android/server/wifi/WifiNetworkFactory.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import android.content.Context; +import android.net.NetworkCapabilities; +import android.net.NetworkFactory; +import android.net.NetworkRequest; +import android.os.Looper; +import android.util.Log; + +import java.io.FileDescriptor; +import java.io.PrintWriter; + +/** + * Network factory to handle trusted wifi network requests. + */ +public class WifiNetworkFactory extends NetworkFactory { + private static final String TAG = "WifiNetworkFactory"; + private static final int SCORE_FILTER = 60; + + private final WifiConnectivityManager mWifiConnectivityManager; + private int mConnectionReqCount = 0; + + public WifiNetworkFactory(Looper l, Context c, NetworkCapabilities f, + WifiConnectivityManager connectivityManager) { + super(l, c, TAG, f); + mWifiConnectivityManager = connectivityManager; + + setScoreFilter(SCORE_FILTER); + } + + @Override + protected void needNetworkFor(NetworkRequest networkRequest, int score) { + if (++mConnectionReqCount == 1) { + mWifiConnectivityManager.setTrustedConnectionAllowed(true); + } + } + + @Override + protected void releaseNetworkFor(NetworkRequest networkRequest) { + if (mConnectionReqCount == 0) { + Log.e(TAG, "No valid network request to release"); + return; + } + if (--mConnectionReqCount == 0) { + mWifiConnectivityManager.setTrustedConnectionAllowed(false); + } + } + + @Override + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + super.dump(fd, pw, args); + pw.println(TAG + ": mConnectionReqCount " + mConnectionReqCount); + } + + /** + * Check if there is at-least one connection request. + */ + public boolean hasConnectionRequests() { + return mConnectionReqCount > 0; + } +} + |