From 8438a85683c511d69382a9bb2f39f329a2b570fb Mon Sep 17 00:00:00 2001 From: xshu Date: Fri, 16 Aug 2019 10:20:22 -0700 Subject: [PATCH] avoid generating reserved local MACs Avoid generating WifiInfo.DEFAULT_MAC_ADDRESS as a randomized MAC address since it's being used for another purpose. Bug: 137796328 Test: atest MacAddressTest Change-Id: Ia7beef0d0af5d7b39845e662cd343d81aef97702 --- core/java/android/net/MacAddress.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java index c2b7d2c71b..52d485de5a 100644 --- a/core/java/android/net/MacAddress.java +++ b/core/java/android/net/MacAddress.java @@ -20,6 +20,7 @@ import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.UnsupportedAppUsage; +import android.net.wifi.WifiInfo; import android.os.Parcel; import android.os.Parcelable; @@ -364,7 +365,12 @@ public final class MacAddress implements Parcelable { long addr = r.nextLong() & VALID_LONG_MASK; addr |= LOCALLY_ASSIGNED_MASK; addr &= ~MULTICAST_MASK; - return new MacAddress(addr); + MacAddress mac = new MacAddress(addr); + // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. + if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { + return createRandomUnicastAddress(); + } + return mac; } /** @@ -383,7 +389,12 @@ public final class MacAddress implements Parcelable { long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); addr |= LOCALLY_ASSIGNED_MASK; addr &= ~MULTICAST_MASK; - return new MacAddress(addr); + MacAddress mac = new MacAddress(addr); + // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. + if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { + return createRandomUnicastAddress(base, r); + } + return mac; } // Convenience function for working around the lack of byte literals.