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

Commit c4446793 authored by Roshan Pius's avatar Roshan Pius
Browse files

wifi(implementation): Generate randomized mac address

Generate random MAC address on the first creation of AP iface after
reboot.

Bug: 78353419
Test: ./hardware/interfaces/wifi/1.3/default/tests/runtests.sh
Test: Manually verified that the AP MAC address & BSSID changes on every
reboot.
Change-Id: Ie9984ec72b2dfec9b7de7a9ef33b9e9ebfaf945c
parent 3b6705fb
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -148,7 +148,8 @@ LOCAL_SRC_FILES := \
    tests/mock_wifi_mode_controller.cpp \
    tests/ringbuffer_unit_tests.cpp \
    tests/wifi_ap_iface_unit_tests.cpp \
    tests/wifi_chip_unit_tests.cpp
    tests/wifi_chip_unit_tests.cpp \
    tests/wifi_iface_util_unit_tests.cpp
LOCAL_STATIC_LIBRARIES := \
    libgmock \
    libgtest \
+58 −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 <gmock/gmock.h>

#undef NAN
#include "wifi_iface_util.h"

using testing::Test;

namespace {
constexpr uint8_t kValidUnicastLocallyAssignedMacAddressMask = 0x02;

bool isValidUnicastLocallyAssignedMacAddress(
    const std::array<uint8_t, 6>& mac_address) {
    uint8_t first_byte = mac_address[0];
    return (first_byte & 0x3) == kValidUnicastLocallyAssignedMacAddressMask;
}
}  // namespace

namespace android {
namespace hardware {
namespace wifi {
namespace V1_3 {
namespace implementation {
class WifiIfaceUtilTest : public Test {
   protected:
    iface_util::WifiIfaceUtil iface_util_;
};

TEST_F(WifiIfaceUtilTest, GetOrCreateRandomMacAddress) {
    auto mac_address = iface_util_.getOrCreateRandomMacAddress();
    ASSERT_TRUE(isValidUnicastLocallyAssignedMacAddress(mac_address));

    // All further calls should return the same MAC address.
    ASSERT_EQ(mac_address, iface_util_.getOrCreateRandomMacAddress());
    ASSERT_EQ(mac_address, iface_util_.getOrCreateRandomMacAddress());
}
}  // namespace implementation
}  // namespace V1_3
}  // namespace wifi
}  // namespace hardware
}  // namespace android
+25 −2
Original line number Diff line number Diff line
@@ -14,12 +14,24 @@
 * limitations under the License.
 */

#include <cstddef>
#include <iostream>
#include <limits>
#include <random>

#include <android-base/logging.h>
#include <android-base/macros.h>
#include <private/android_filesystem_config.h>

#undef NAN
#include "wifi_iface_util.h"

namespace {
// Constants to set the local bit & clear the multicast bit.
constexpr uint8_t kMacAddressMulticastMask = 0x01;
constexpr uint8_t kMacAddressLocallyAssignedMask = 0x02;
}  // namespace

namespace android {
namespace hardware {
namespace wifi {
@@ -62,8 +74,19 @@ std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() {
}

std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() {
    // TODO: Generate random MAC address here.
    return {};
    std::array<uint8_t, 6> address = {};
    std::random_device rd;
    std::default_random_engine engine(rd());
    std::uniform_int_distribution<uint8_t> dist(
        std::numeric_limits<uint8_t>::min(),
        std::numeric_limits<uint8_t>::max());
    for (size_t i = 0; i < address.size(); i++) {
        address[i] = dist(engine);
    }
    // Set the local bit and clear the multicast bit.
    address[0] |= kMacAddressLocallyAssignedMask;
    address[0] &= ~kMacAddressMulticastMask;
    return address;
}
}  // namespace iface_util
}  // namespace implementation