Android 11 WiFi MAC Randomization

开启未连接

ClientModeImpl

service/java/com/android/server/wifi/ClientModeImpl.java

setupClientMode

3637         if (isConnectedMacRandomizationEnabled()) {
3638             mWifiNative.setMacAddress(mInterfaceName, MacAddressUtils.createRandomUnicastAddress());
3639         }

 打开WLAN,但不连接,关闭再打开WiFi MAC地址会变。

连接

更新MAC地址

updateWifiConfigOnStartConnection

连接时根据设置里的配置更新MAC地址,“网络详情"-》"高级”-》“隐私”,使用随机MAC(默认),使用设备MAC。

Network & internet -》WiFi -》Netowrk details -》Privacy:Use randomized MAC (default)、Use device MAC

6378     /**
6379      * Update the wifi configuration before sending connect to
6380      * supplicant/driver.
6381      *
6382      * @param config wifi configuration object.
6383      * @param bssid BSSID to assocaite with.
6384      */
6385     void updateWifiConfigOnStartConnection(WifiConfiguration config, String bssid) {


6470         if (isConnectedMacRandomizationEnabled()) {
6471             if (config.macRandomizationSetting == WifiConfiguration.RANDOMIZATION_PERSISTENT) {
6472                 configureRandomizedMacAddress(config);
6473             } else {
6474                 setCurrentMacToFactoryMac(config);
6475             }
6476         }

 使用随机MAC(默认)

configureRandomizedMacAddress

配置随机MAC地址。

3301      * Dynamically change the MAC address to use the locally randomized
3302      * MAC address generated for each network.
3303      * @param config WifiConfiguration with mRandomizedMacAddress to change into. If the address
3304      * is masked out or not set, it will generate a new random MAC address.
3305      */
3306     private void configureRandomizedMacAddress(WifiConfiguration config) {
3307         if (config == null) {
3308             Log.e(TAG, "No config to change MAC address to");
3309             return;
3310         }
3311         String currentMacString = mWifiNative.getMacAddress(mInterfaceName);
3312         MacAddress currentMac = currentMacString == null ? null :
3313                 MacAddress.fromString(currentMacString);
3314         MacAddress newMac = mWifiConfigManager.getRandomizedMacAndUpdateIfNeeded(config);
3315         if (!WifiConfiguration.isValidMacAddressForRandomization(newMac)) {
3316             Log.wtf(TAG, "Config generated an invalid MAC address");
3317         } else if (newMac.equals(currentMac)) {
3318             Log.d(TAG, "No changes in MAC address");
3319         } else {
3320             mWifiMetrics.logStaEvent(StaEvent.TYPE_MAC_CHANGE, config);
3321             boolean setMacSuccess =
3322                     mWifiNative.setMacAddress(mInterfaceName, newMac);
3323             if (setMacSuccess) {
3324                 mWifiNative.removeNetworkCachedDataIfNeeded(config.networkId, newMac);
3325             }
3326             Log.d(TAG, "ConnectedMacRandomization SSID(" + config.getPrintableSsid()
3327                     + "). setMacAddress(" + newMac.toString() + ") from "
3328                     + currentMacString + " = " + setMacSuccess);
3329         }
3330     }
获取随机MAC地址

getRandomizedMacAndUpdateIfNeeded

 enhanced_mac_randomization_force_enabled=1按需更新MAC地址。

 573     /**
 574      * Returns the randomized MAC address that should be used for this WifiCo
### Android 设备 WIFI 连接时使用随机 MAC 地址的实现方法 在 Android 系统中,自 Android 8.0 开始引入了 Wi-Fi 随机化 MAC 地址的功能,该功能允许设备在网络扫描和连接过程中动态生成并使用不同的 MAC 地址。以下是关于如何实现在 Android 设备上启用随机 MAC 地址的具体说明: #### 启用随机 MAC 地址的关键配置文件 1. **`config_wifi_connected_mac_randomization_supported` 参数** 在 `core/res/res/values/config.xml` 文件中定义了一个布尔参数 `<bool translatable="false" name="config_wifi_connected_mac_randomization_supported">true</bool>`[^3]。此参数用于指示系统是否支持已连接状态下的 MAC 地址随机化。 2. **Wi-Fi 设置控制器** 在 `packages/apps/Settings/src/com/android/settings/wifi/WifiConfigController.java` 中实现了对 Wi-Fi 配置的相关逻辑处理。通过调整这里的代码可以控制是否启用随机 MAC 地址功能。 #### 关键类及其作用 1. **ClientModeImpl 类** 它位于路径 `frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java`,负责管理客户端模式下 Wi-Fi 的行为,包括 MAC 地址的选择与分配过程[^2]。 2. **ExtendedWifiInfo 类** 此类 (`ExtendedWifiInfo.java`) 提供扩展的信息接口,在其中设置了连接后的具体 MAC 地址模式[^1]。 3. **WifiMetrics 类** 负责记录有关 Wi-Fi 性能指标的数据统计工作,同时也参与到了 MAC 地址策略的应用当中。 #### 修改流程概述 为了确保 Android 设备能够正常应用随机 MAC 地址机制,需按照如下方式操作: - 编辑 `config_wifi_connected_mac_randomization_supported` 值设为 true; - 更新或确认 `ClientModeImpl`, `ExtendedWifiInfo`, 和 `WifiMetrics` 等组件的行为符合预期需求; - 测试整个系统的兼容性和稳定性以验证更改效果。 下面是一个简单的伪代码示例展示可能涉及的部分逻辑更新: ```java // Example of enabling random MAC address logic within ClientModeImpl class. public void connectToNetwork(Network network) { String randomizedMacAddress = generateRandomizedMacAddress(); if (isRandomMacSupported()) { // Check support status from config file or system property setInterfaceMacAddress(randomizedMacAddress); // Set the interface with new generated mac addr } else { Log.w(TAG, "Random MAC Address not supported."); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值