From f11733a4a86615cb07df524f08a3dd99c9c925c8 Mon Sep 17 00:00:00 2001 From: Chaitanya Tata Date: Thu, 5 Dec 2024 17:48:05 +0530 Subject: [PATCH] Add support for random MAC address Add a build time option to generate random MAC address, this helps in test deployments. Used Zephyr API in the BM code to simplify things, as srand() was giving same MAC address (seed problems). Signed-off-by: Chaitanya Tata --- nrf70_bm_lib/Kconfig | 9 ++++++- .../docs/source/nrf70_bm_porting_guide.rst | 4 +++ nrf70_bm_lib/source/nrf70_bm_core.c | 27 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/nrf70_bm_lib/Kconfig b/nrf70_bm_lib/Kconfig index ff23c6b..8add2ed 100644 --- a/nrf70_bm_lib/Kconfig +++ b/nrf70_bm_lib/Kconfig @@ -48,8 +48,8 @@ config NRF70_FIXED_MAC_ADDRESS choice prompt "Wi-Fi MAC address type" - default NRF70_OTP_MAC_ADDRESS if NRF70_FIXED_MAC_ADDRESS = "" default NRF70_FIXED_MAC_ADDRESS_ENABLED if NRF70_FIXED_MAC_ADDRESS != "" + default NRF70_OTP_MAC_ADDRESS help Select the type of MAC address to be used by the Wi-Fi driver @@ -62,6 +62,13 @@ config NRF70_FIXED_MAC_ADDRESS_ENABLED bool "Enable fixed MAC address" help Enable fixed MAC address + +config NRF70_RANDOM_MAC_ADDRESS + bool "Random MAC address generation at runtime" + select TEST_RANDOM_GENERATOR + help + This option enables random MAC address generation at runtime. + The random MAC address is generated using the libc srand() function. endchoice config NRF700X_LOG_VERBOSE diff --git a/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst b/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst index 97654ba..e1f3ca7 100644 --- a/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst +++ b/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst @@ -47,6 +47,10 @@ The reference implementation of the BM Driver for the Zephyr RTOS uses build-tim - Used to manage the timers for the nRF70 Series driver, esp. for low power mode. - timer_*** - k_work_delayable_*** + * - PRNG + - Used to generate random numbers for the nRF70 Series random MAC address generation. + - NA + - sys_rand8_get() .. note :: diff --git a/nrf70_bm_lib/source/nrf70_bm_core.c b/nrf70_bm_lib/source/nrf70_bm_core.c index e56f12a..fb479be 100644 --- a/nrf70_bm_lib/source/nrf70_bm_core.c +++ b/nrf70_bm_lib/source/nrf70_bm_core.c @@ -7,6 +7,10 @@ /** @file * @brief nRF70 Bare Metal initialization. */ +#ifdef CONFIG_NRF70_RANDOM_MAC_ADDRESS +#include +#endif /* CONFIG_NRF70_RANDOM_MAC_ADDRESS */ + #include "nrf70_bm_core.h" #include "nrf70_bm_lib.h" @@ -451,11 +455,28 @@ int nrf70_fmac_init(void) return -1; } +#ifdef CONFIG_NRF70_RANDOM_MAC_ADDRESS +static void generate_random_mac_address(uint8_t *mac_addr) +{ + // For simplicty use Zephyr API to generate random number + for (int i = 0; i < 6; i++) { + mac_addr[i] = sys_rand8_get(); + } + + // Set the locally administered bit (bit 1 of the first byte) + mac_addr[0] |= 0x02; + + // Clear the multicast bit (bit 0 of the first byte) + mac_addr[0] &= 0xFE; +} +#endif /* CONFIG_WIFI_RANDOM_MAC_ADDRESS */ enum nrf_wifi_status nrf_wifi_get_mac_addr(struct nrf70_wifi_vif_bm *vif) { enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL; +#ifndef CONFIG_NRF70_RANDOM_MAC_ADDRESS void *rpu_ctx = nrf70_bm_priv.rpu_ctx_bm.rpu_ctx; +#endif /* CONFIG_NRF70_RANDOM_MAC_ADDRESS */ struct nrf_wifi_fmac_priv *fmac_priv = nrf70_bm_priv.fmac_priv; unsigned char mac_addr_str[18]; #ifdef CONFIG_NRF70_FIXED_MAC_ADDRESS_ENABLED @@ -474,6 +495,8 @@ enum nrf_wifi_status nrf_wifi_get_mac_addr(struct nrf70_wifi_vif_bm *vif) } memcpy(vif->mac_addr, fixed_mac_addr, NR70_MAC_ADDR_LEN); +#elif CONFIG_NRF70_RANDOM_MAC_ADDRESS + generate_random_mac_address(vif->mac_addr); #elif CONFIG_NRF70_OTP_MAC_ADDRESS status = nrf_wifi_fmac_otp_mac_addr_get(rpu_ctx, vif->vif_idx, @@ -536,6 +559,10 @@ int nrf70_fmac_add_vif_sta(void) goto del_vif; } + NRF70_LOG_INF("MAC address set to %02X:%02X:%02X:%02X:%02X:%02X", + vif->mac_addr[0], vif->mac_addr[1], vif->mac_addr[2], + vif->mac_addr[3], vif->mac_addr[4], vif->mac_addr[5]); + memset(&vif_info, 0, sizeof(vif_info)); vif_info.state = NRF_WIFI_FMAC_IF_OP_STATE_UP; vif_info.if_index = vif->vif_idx;