From 88033a33dcead214184dd0c368ba69e0452a39f8 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Wed, 22 Apr 2020 10:18:32 +0800 Subject: Wifi: remove PMK cache if MAC is changed Bug: 152931766 Test: atest FrameworksWifiTests Change-Id: Ia4c555de7391d009359d68bbf6711c5922c415ab --- .../com/android/server/wifi/ClientModeImpl.java | 4 +++ .../wifi/SupplicantStaIfaceCallbackV1_3Impl.java | 8 +++-- .../android/server/wifi/SupplicantStaIfaceHal.java | 40 +++++++++++++++++++--- .../java/com/android/server/wifi/WifiNative.java | 9 +++++ 4 files changed, 55 insertions(+), 6 deletions(-) (limited to 'service') diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index 2887081c9..c1d2518b9 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -3265,6 +3265,9 @@ public class ClientModeImpl extends StateMachine { mWifiMetrics.logStaEvent(StaEvent.TYPE_MAC_CHANGE, config); boolean setMacSuccess = mWifiNative.setMacAddress(mInterfaceName, newMac); + if (setMacSuccess) { + mWifiNative.removeNetworkCachedDataIfNeeded(config.networkId, newMac); + } Log.d(TAG, "ConnectedMacRandomization SSID(" + config.getPrintableSsid() + "). setMacAddress(" + newMac.toString() + ") from " + currentMacString + " = " + setMacSuccess); @@ -3283,6 +3286,7 @@ public class ClientModeImpl extends StateMachine { String currentMacStr = mWifiNative.getMacAddress(mInterfaceName); if (!TextUtils.equals(currentMacStr, factoryMac.toString())) { if (mWifiNative.setMacAddress(mInterfaceName, factoryMac)) { + mWifiNative.removeNetworkCachedDataIfNeeded(config.networkId, factoryMac); mWifiMetrics.logStaEvent(StaEvent.TYPE_MAC_CHANGE, config); } else { Log.e(TAG, "Failed to set MAC address to " + "'" + factoryMac.toString() + "'"); diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackV1_3Impl.java b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackV1_3Impl.java index 5af65d0a5..cac84b543 100644 --- a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackV1_3Impl.java +++ b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackV1_3Impl.java @@ -199,9 +199,13 @@ abstract class SupplicantStaIfaceCallbackV1_3Impl extends if (WifiConfigurationUtil.isConfigForPskNetwork(curConfig)) return; - mStaIfaceHal.addPmkCacheEntry(curConfig.networkId, expirationTimeInSec, serializedEntry); + mStaIfaceHal.addPmkCacheEntry(mIfaceName, + curConfig.networkId, expirationTimeInSec, serializedEntry); mStaIfaceHal.logCallback( - "onPmkCacheAdded: update pmk cache for config id " + curConfig.networkId); + "onPmkCacheAdded: update pmk cache for config id " + + curConfig.networkId + + " on " + + mIfaceName); } @Override diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java index 4380f6ab4..657b081dd 100644 --- a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java +++ b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java @@ -43,6 +43,7 @@ import android.hardware.wifi.supplicant.V1_3.WifiTechnology; import android.hardware.wifi.supplicant.V1_3.WpaDriverCapabilitiesMask; import android.hidl.manager.V1_0.IServiceManager; import android.hidl.manager.V1_0.IServiceNotification; +import android.net.MacAddress; import android.net.wifi.ScanResult; import android.net.wifi.WifiAnnotations.WifiStandard; import android.net.wifi.WifiConfiguration; @@ -170,10 +171,12 @@ public class SupplicantStaIfaceHal { static class PmkCacheStoreData { public long expirationTimeInSec; public ArrayList data; + public MacAddress macAddress; - PmkCacheStoreData(long timeInSec, ArrayList serializedData) { + PmkCacheStoreData(long timeInSec, ArrayList serializedData, MacAddress macAddress) { expirationTimeInSec = timeInSec; data = serializedData; + this.macAddress = macAddress; } } @@ -1052,6 +1055,24 @@ public class SupplicantStaIfaceHal { } } + /** + * Clear HAL cached data if MAC address is changed. + * + * @param networkId network id of the network to be checked. + * @param curMacAddress current MAC address + */ + public void removeNetworkCachedDataIfNeeded(int networkId, MacAddress curMacAddress) { + synchronized (mLock) { + PmkCacheStoreData pmkData = mPmkCacheEntries.get(networkId); + + if (pmkData == null) return; + + if (curMacAddress.equals(pmkData.macAddress)) return; + + removeNetworkCachedData(networkId); + } + } + /** * Remove all networks from supplicant * @@ -2636,10 +2657,21 @@ public class SupplicantStaIfaceHal { } protected void addPmkCacheEntry( + String ifaceName, int networkId, long expirationTimeInSec, ArrayList serializedEntry) { - mPmkCacheEntries.put(networkId, - new PmkCacheStoreData(expirationTimeInSec, serializedEntry)); - updatePmkCacheExpiration(); + String macAddressStr = getMacAddress(ifaceName); + if (macAddressStr == null) { + Log.w(TAG, "Omit PMK cache due to no valid MAC address on " + ifaceName); + return; + } + try { + MacAddress macAddress = MacAddress.fromString(macAddressStr); + mPmkCacheEntries.put(networkId, + new PmkCacheStoreData(expirationTimeInSec, serializedEntry, macAddress)); + updatePmkCacheExpiration(); + } catch (IllegalArgumentException ex) { + Log.w(TAG, "Invalid MAC address string " + macAddressStr); + } } protected void removePmkCacheEntry(int networkId) { diff --git a/service/java/com/android/server/wifi/WifiNative.java b/service/java/com/android/server/wifi/WifiNative.java index 20701ca6a..db2c3ce17 100644 --- a/service/java/com/android/server/wifi/WifiNative.java +++ b/service/java/com/android/server/wifi/WifiNative.java @@ -2417,6 +2417,15 @@ public class WifiNative { mSupplicantStaIfaceHal.removeNetworkCachedData(networkId); } + /** Clear HAL cached data for |networkId| if MAC address is changed. + * + * @param networkId network id of the network to be checked. + * @param curMacAddress current MAC address + */ + public void removeNetworkCachedDataIfNeeded(int networkId, MacAddress curMacAddress) { + mSupplicantStaIfaceHal.removeNetworkCachedDataIfNeeded(networkId, curMacAddress); + } + /* * DPP */ -- cgit v1.2.3