diff options
author | Patrik Fimml <patrikf@google.com> | 2019-10-17 09:16:26 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-10-17 09:16:26 +0000 |
commit | b8ea6a036eb37cce8f67366d23dd5278cf6d5daa (patch) | |
tree | 1f77610e37c1c2e61feb4be5cab6f881dc829a35 /service | |
parent | 528d86a62cc024f609c50866389c2b3593e3615f (diff) | |
parent | 2ac6f20ab826eed88ca124e6437d2055fd3d2fbb (diff) |
Merge "Wifi AP: Randomize MAC address in SoftApManager"
Diffstat (limited to 'service')
-rw-r--r-- | service/java/com/android/server/wifi/SoftApManager.java | 35 | ||||
-rw-r--r-- | service/java/com/android/server/wifi/WifiVendorHal.java | 52 |
2 files changed, 73 insertions, 14 deletions
diff --git a/service/java/com/android/server/wifi/SoftApManager.java b/service/java/com/android/server/wifi/SoftApManager.java index 8802b5a71..3c4a6797c 100644 --- a/service/java/com/android/server/wifi/SoftApManager.java +++ b/service/java/com/android/server/wifi/SoftApManager.java @@ -24,6 +24,7 @@ import android.annotation.NonNull; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; +import android.net.MacAddress; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; @@ -244,6 +245,33 @@ public class SoftApManager implements ActiveModeManager { mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); } + private int setMacAddress() { + boolean randomize = mContext.getResources().getBoolean( + R.bool.config_wifi_ap_mac_randomization_supported); + if (!randomize) { + MacAddress mac = mWifiNative.getFactoryMacAddress(mApInterfaceName); + if (mac == null) { + Log.e(TAG, "failed to get factory MAC address"); + return ERROR_GENERIC; + } + + // We're (re-)configuring the factory MAC address. Some drivers may not support setting + // the MAC at all, so fail soft in this case. + if (!mWifiNative.setMacAddress(mApInterfaceName, mac)) { + Log.w(TAG, "failed to reset to factory MAC address; continuing with current MAC"); + } + return SUCCESS; + } + + // We're configuring a random MAC address. In this case, driver support is mandatory. + MacAddress mac = MacAddress.createRandomUnicastAddress(); + if (!mWifiNative.setMacAddress(mApInterfaceName, mac)) { + Log.e(TAG, "failed to set random MAC address"); + return ERROR_GENERIC; + } + return SUCCESS; + } + private int setCountryCode() { int band = mApConfig.getWifiConfiguration().apBand; if (TextUtils.isEmpty(mCountryCode)) { @@ -284,7 +312,12 @@ public class SoftApManager implements ActiveModeManager { Log.d(TAG, "band " + config.apBand + " iface " + mApInterfaceName + " country " + mCountryCode); - int result = setCountryCode(); + int result = setMacAddress(); + if (result != SUCCESS) { + return result; + } + + result = setCountryCode(); if (result != SUCCESS) { return result; } diff --git a/service/java/com/android/server/wifi/WifiVendorHal.java b/service/java/com/android/server/wifi/WifiVendorHal.java index 5e3626d57..a1d98061b 100644 --- a/service/java/com/android/server/wifi/WifiVendorHal.java +++ b/service/java/com/android/server/wifi/WifiVendorHal.java @@ -1291,17 +1291,23 @@ public class WifiVendorHal { byte[] macByteArray = mac.toByteArray(); synchronized (sLock) { try { - android.hardware.wifi.V1_2.IWifiStaIface ifaceV12 = + android.hardware.wifi.V1_2.IWifiStaIface sta12 = getWifiStaIfaceForV1_2Mockable(ifaceName); - if (ifaceV12 == null) return boolResult(false); - WifiStatus status = ifaceV12.setMacAddress(macByteArray); - if (!ok(status)) return false; - return true; + if (sta12 != null) { + return ok(sta12.setMacAddress(macByteArray)); + } + + android.hardware.wifi.V1_4.IWifiApIface ap14 = + getWifiApIfaceForV1_4Mockable(ifaceName); + if (ap14 != null) { + return ok(ap14.setMacAddress(macByteArray)); + } } catch (RemoteException e) { handleRemoteException(e); return false; } } + return boolResult(false); } /** @@ -1316,20 +1322,33 @@ public class WifiVendorHal { } synchronized (sLock) { try { - android.hardware.wifi.V1_3.IWifiStaIface ifaceV13 = - getWifiStaIfaceForV1_3Mockable(ifaceName); - if (ifaceV13 == null) return null; AnswerBox box = new AnswerBox(); - ifaceV13.getFactoryMacAddress((status, macBytes) -> { - if (!ok(status)) return; - box.mac = MacAddress.fromBytes(macBytes); - }); - return box.mac; + + android.hardware.wifi.V1_3.IWifiStaIface sta13 = + getWifiStaIfaceForV1_3Mockable(ifaceName); + if (sta13 != null) { + sta13.getFactoryMacAddress((status, macBytes) -> { + if (!ok(status)) return; + box.mac = MacAddress.fromBytes(macBytes); + }); + return box.mac; + } + + android.hardware.wifi.V1_4.IWifiApIface ap14 = + getWifiApIfaceForV1_4Mockable(ifaceName); + if (ap14 != null) { + ap14.getFactoryMacAddress((status, macBytes) -> { + if (!ok(status)) return; + box.mac = MacAddress.fromBytes(macBytes); + }); + return box.mac; + } } catch (RemoteException e) { handleRemoteException(e); return null; } } + return null; } /** @@ -2307,6 +2326,13 @@ public class WifiVendorHal { return android.hardware.wifi.V1_3.IWifiStaIface.castFrom(iface); } + protected android.hardware.wifi.V1_4.IWifiApIface getWifiApIfaceForV1_4Mockable( + String ifaceName) { + IWifiApIface iface = getApIface(ifaceName); + if (iface == null) return null; + return android.hardware.wifi.V1_4.IWifiApIface.castFrom(iface); + } + /** * sarPowerBackoffRequired_1_1() * This method checks if we need to backoff wifi Tx power due to SAR requirements. |