Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b6705fb authored by Roshan Pius's avatar Roshan Pius
Browse files

wifi(implementation): Set randomized MAC address for AP

Add the plumbing required for setting a random MAC address on AP
startup. The random MAC address is created once for the lifetime of
the daemon and then reused for any further AP creation. This would
ensure that the MAC address is changed on every reboot.

The feature is turned on by default, devices will need to set the
|WIFI_HIDL_FEATURE_DISABLE_AP_MAC_RANDOMIZATION| in their .mk file to
disable the feature at compile time.

Bug: 78353419
Test: ./hardware/interfaces/wifi/1.3/default/tests/runtests.sh
Change-Id: I054d5249c20cc582b76966313135295873cd0b61
parent 99dab386
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -33,6 +33,9 @@ endif
ifdef WIFI_HIDL_FEATURE_DISABLE_AP
LOCAL_CPPFLAGS += -DWIFI_HIDL_FEATURE_DISABLE_AP
endif
ifdef WIFI_HIDL_FEATURE_DISABLE_AP_MAC_RANDOMIZATION
LOCAL_CPPFLAGS += -DWIFI_HIDL_FEATURE_DISABLE_AP_MAC_RANDOMIZATION
endif
# Allow implicit fallthroughs in wifi_legacy_hal.cpp until they are fixed.
LOCAL_CFLAGS += -Wno-error=implicit-fallthrough
LOCAL_SRC_FILES := \
@@ -144,6 +147,7 @@ LOCAL_SRC_FILES := \
    tests/mock_wifi_legacy_hal.cpp \
    tests/mock_wifi_mode_controller.cpp \
    tests/ringbuffer_unit_tests.cpp \
    tests/wifi_ap_iface_unit_tests.cpp \
    tests/wifi_chip_unit_tests.cpp
LOCAL_STATIC_LIBRARIES := \
    libgmock \
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ class MockWifiFeatureFlags : public WifiFeatureFlags {
    MockWifiFeatureFlags();

    MOCK_METHOD0(getChipModes, std::vector<V1_0::IWifiChip::ChipMode>());
    MOCK_METHOD0(isApMacRandomizationDisabled, bool());
};

}  // namespace feature_flags
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ class MockWifiIfaceUtil : public WifiIfaceUtil {
                 std::array<uint8_t, 6>(const std::string&));
    MOCK_METHOD2(setMacAddress,
                 bool(const std::string&, const std::array<uint8_t, 6>&));
    MOCK_METHOD0(getOrCreateRandomMacAddress, std::array<uint8_t, 6>());
};
}  // namespace iface_util
}  // namespace implementation
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/logging.h>
#include <android-base/macros.h>
#include <cutils/properties.h>
#include <gmock/gmock.h>

#undef NAN  // This is weird, NAN is defined in bionic/libc/include/math.h:38
#include "wifi_ap_iface.h"

#include "mock_wifi_feature_flags.h"
#include "mock_wifi_iface_util.h"
#include "mock_wifi_legacy_hal.h"

using testing::NiceMock;
using testing::Return;
using testing::Test;

namespace {
constexpr char kIfaceName[] = "mockWlan0";
}  // namespace

namespace android {
namespace hardware {
namespace wifi {
namespace V1_3 {
namespace implementation {

class WifiApIfaceTest : public Test {
   protected:
    std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
        new NiceMock<legacy_hal::MockWifiLegacyHal>};
    std::shared_ptr<NiceMock<iface_util::MockWifiIfaceUtil>> iface_util_{
        new NiceMock<iface_util::MockWifiIfaceUtil>};
    std::shared_ptr<NiceMock<feature_flags::MockWifiFeatureFlags>>
        feature_flags_{new NiceMock<feature_flags::MockWifiFeatureFlags>};
};

TEST_F(WifiApIfaceTest, SetRandomMacAddressIfFeatureEnabled) {
    EXPECT_CALL(*feature_flags_, isApMacRandomizationDisabled())
        .WillOnce(testing::Return(false));
    EXPECT_CALL(*iface_util_, getOrCreateRandomMacAddress())
        .WillOnce(testing::Return(std::array<uint8_t, 6>{0, 0, 0, 0, 0, 0}));
    EXPECT_CALL(*iface_util_, setMacAddress(testing::_, testing::_))
        .WillOnce(testing::Return(true));
    sp<WifiApIface> ap_iface =
        new WifiApIface(kIfaceName, legacy_hal_, iface_util_, feature_flags_);
}

TEST_F(WifiApIfaceTest, DontSetRandomMacAddressIfFeatureDisabled) {
    EXPECT_CALL(*feature_flags_, isApMacRandomizationDisabled())
        .WillOnce(testing::Return(true));
    EXPECT_CALL(*iface_util_, getOrCreateRandomMacAddress()).Times(0);
    EXPECT_CALL(*iface_util_, setMacAddress(testing::_, testing::_)).Times(0);
    sp<WifiApIface> ap_iface =
        new WifiApIface(kIfaceName, legacy_hal_, iface_util_, feature_flags_);
}
}  // namespace implementation
}  // namespace V1_3
}  // namespace wifi
}  // namespace hardware
}  // namespace android
+17 −2
Original line number Diff line number Diff line
@@ -31,11 +31,26 @@ using hidl_return_util::validateAndCall;
WifiApIface::WifiApIface(
    const std::string& ifname,
    const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
    const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
    const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
    const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
    : ifname_(ifname),
      legacy_hal_(legacy_hal),
      iface_util_(iface_util),
      is_valid_(true) {}
      feature_flags_(feature_flags),
      is_valid_(true) {
    if (feature_flags_.lock()->isApMacRandomizationDisabled()) {
        LOG(INFO) << "AP MAC randomization disabled";
        return;
    }
    LOG(INFO) << "AP MAC randomization enabled";
    // Set random MAC address
    std::array<uint8_t, 6> randomized_mac =
        iface_util_.lock()->getOrCreateRandomMacAddress();
    bool status = iface_util_.lock()->setMacAddress(ifname_, randomized_mac);
    if (!status) {
        LOG(ERROR) << "Failed to set random mac address";
    }
}

void WifiApIface::invalidate() {
    legacy_hal_.reset();
Loading