From fc5cee94fe6719372bbb3188e28fa7054eccb396 Mon Sep 17 00:00:00 2001 From: xshu Date: Mon, 24 Aug 2020 02:14:51 -0700 Subject: [PATCH] Suggestion API to set macRandomization setting Allows suggestions to either use enhanced randomization or persistent randomization. If not set, enhanced randomization will be used by default. Bug: 162801581 Test: atest android.net.wifi Change-Id: Ia41511677221e4aa1d79830c898d90f808c4e7c8 --- api/current.txt | 1 + wifi/api/current.txt | 1 + .../net/wifi/WifiNetworkSuggestion.java | 35 +++++++++++++++++++ .../net/wifi/WifiNetworkSuggestionTest.java | 27 ++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/api/current.txt b/api/current.txt index c6af6beca60be..1b0f0543b86f3 100644 --- a/api/current.txt +++ b/api/current.txt @@ -31644,6 +31644,7 @@ package android.net.wifi { method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setBssid(@NonNull android.net.MacAddress); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setCredentialSharedWithUser(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsAppInteractionRequired(boolean); + method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsEnhancedMacRandomizationEnabled(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsEnhancedOpen(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsHiddenSsid(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsInitialAutojoinEnabled(boolean); diff --git a/wifi/api/current.txt b/wifi/api/current.txt index ee7320f9a5ef2..b104decab73cb 100644 --- a/wifi/api/current.txt +++ b/wifi/api/current.txt @@ -507,6 +507,7 @@ package android.net.wifi { method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setBssid(@NonNull android.net.MacAddress); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setCredentialSharedWithUser(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsAppInteractionRequired(boolean); + method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsEnhancedMacRandomizationEnabled(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsEnhancedOpen(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsHiddenSsid(boolean); method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsInitialAutojoinEnabled(boolean); diff --git a/wifi/java/android/net/wifi/WifiNetworkSuggestion.java b/wifi/java/android/net/wifi/WifiNetworkSuggestion.java index a3c4ae764612e..e4e900ffe12a3 100644 --- a/wifi/java/android/net/wifi/WifiNetworkSuggestion.java +++ b/wifi/java/android/net/wifi/WifiNetworkSuggestion.java @@ -149,6 +149,11 @@ public final class WifiNetworkSuggestion implements Parcelable { */ private boolean mIsNetworkUntrusted; + /** + * Whether this network will use enhanced MAC randomization. + */ + private boolean mIsEnhancedMacRandomizationEnabled; + public Builder() { mSsid = null; mBssid = null; @@ -171,6 +176,7 @@ public final class WifiNetworkSuggestion implements Parcelable { mWapiEnterpriseConfig = null; mIsNetworkUntrusted = false; mPriorityGroup = 0; + mIsEnhancedMacRandomizationEnabled = true; } /** @@ -393,6 +399,29 @@ public final class WifiNetworkSuggestion implements Parcelable { return this; } + /** + * Specifies the MAC randomization method. + *

+ * Suggested networks will never use the device (factory) MAC address to associate to the + * network - instead they use a locally generated random MAC address. This method controls + * the strategy for generating the random MAC address: + *

  • Persisted MAC randomization (false): generates the MAC address from a secret seed + * and information from the Wi-Fi configuration (SSID or Passpoint profile). That means that + * the same generated MAC address will be used for each subsequent association.
  • + *
  • Enhanced MAC randomization (true - the default): periodically generates a new MAC + * address new connections. Under this option, the randomized MAC address should change + * if the suggestion is removed and then added back.
  • + * + * @param enabled {@code true} to periodically change the randomized MAC address. + * {@code false} to use the same randomized MAC for all connections to this + * network. + * @return Instance of {@link Builder} to enable chaining of the builder method. + */ + public @NonNull Builder setIsEnhancedMacRandomizationEnabled(boolean enabled) { + mIsEnhancedMacRandomizationEnabled = enabled; + return this; + } + /** * Specifies whether the app needs to log in to a captive portal to obtain Internet access. *

    @@ -577,6 +606,9 @@ public final class WifiNetworkSuggestion implements Parcelable { wifiConfiguration.meteredOverride = mMeteredOverride; wifiConfiguration.carrierId = mCarrierId; wifiConfiguration.trusted = !mIsNetworkUntrusted; + wifiConfiguration.macRandomizationSetting = mIsEnhancedMacRandomizationEnabled + ? WifiConfiguration.RANDOMIZATION_ENHANCED + : WifiConfiguration.RANDOMIZATION_PERSISTENT; return wifiConfiguration; } @@ -607,6 +639,9 @@ public final class WifiNetworkSuggestion implements Parcelable { wifiConfiguration.trusted = !mIsNetworkUntrusted; mPasspointConfiguration.setCarrierId(mCarrierId); mPasspointConfiguration.setMeteredOverride(wifiConfiguration.meteredOverride); + wifiConfiguration.macRandomizationSetting = mIsEnhancedMacRandomizationEnabled + ? WifiConfiguration.RANDOMIZATION_ENHANCED + : WifiConfiguration.RANDOMIZATION_PERSISTENT; return wifiConfiguration; } diff --git a/wifi/tests/src/android/net/wifi/WifiNetworkSuggestionTest.java b/wifi/tests/src/android/net/wifi/WifiNetworkSuggestionTest.java index 3744a51837936..7d5a45216fc4d 100644 --- a/wifi/tests/src/android/net/wifi/WifiNetworkSuggestionTest.java +++ b/wifi/tests/src/android/net/wifi/WifiNetworkSuggestionTest.java @@ -609,6 +609,33 @@ public class WifiNetworkSuggestionTest { .build(); } + /** + * Verify that the macRandomizationSetting defaults to RANDOMIZATION_ENHANCED and could be set + * to RANDOMIZATION_PERSISTENT. + */ + @Test + public void testWifiNetworkSuggestionBuilderSetMacRandomization() { + WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder() + .setSsid(TEST_SSID) + .build(); + assertEquals(WifiConfiguration.RANDOMIZATION_ENHANCED, + suggestion.wifiConfiguration.macRandomizationSetting); + + suggestion = new WifiNetworkSuggestion.Builder() + .setSsid(TEST_SSID) + .setIsEnhancedMacRandomizationEnabled(false) + .build(); + assertEquals(WifiConfiguration.RANDOMIZATION_PERSISTENT, + suggestion.wifiConfiguration.macRandomizationSetting); + + suggestion = new WifiNetworkSuggestion.Builder() + .setSsid(TEST_SSID) + .setIsEnhancedMacRandomizationEnabled(true) + .build(); + assertEquals(WifiConfiguration.RANDOMIZATION_ENHANCED, + suggestion.wifiConfiguration.macRandomizationSetting); + } + /** * Check that parcel marshalling/unmarshalling works */