drivers: eth: Refactor generation of random mac into help function

Rather than having each driver have its own slightly different way of
generating a random mac address, add a helper function that they all can
call so we do it one way.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
This commit is contained in:
Kumar Gala 2020-04-23 12:13:14 -05:00 committed by Carles Cufí
commit 8178f76470
7 changed files with 45 additions and 57 deletions

30
drivers/ethernet/eth.h Normal file
View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2020 Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_ETHERNET_ETH_H_
#define ZEPHYR_DRIVERS_ETHERNET_ETH_H_
#include <zephyr/types.h>
static inline void gen_random_mac(u8_t *mac_addr, u8_t b0, u8_t b1, u8_t b2)
{
u32_t entropy;
entropy = sys_rand32_get();
mac_addr[0] = b0;
mac_addr[1] = b1;
mac_addr[2] = b2;
/* Set MAC address locally administered, unicast (LAA) */
mac_addr[0] |= 0x02;
mac_addr[3] = (entropy >> 16) & 0xff;
mac_addr[4] = (entropy >> 8) & 0xff;
mac_addr[5] = (entropy >> 0) & 0xff;
}
#endif /* ZEPHYR_DRIVERS_ETHERNET_ETH_H_ */

View file

@ -29,6 +29,7 @@ LOG_MODULE_REGISTER(eth_gecko, CONFIG_ETHERNET_LOG_LEVEL);
#include "phy_gecko.h" #include "phy_gecko.h"
#include "eth_gecko_priv.h" #include "eth_gecko_priv.h"
#include "eth.h"
static u8_t dma_tx_buffer[ETH_TX_BUF_COUNT][ETH_TX_BUF_SIZE] static u8_t dma_tx_buffer[ETH_TX_BUF_COUNT][ETH_TX_BUF_SIZE]
__aligned(ETH_BUF_ALIGNMENT); __aligned(ETH_BUF_ALIGNMENT);
@ -490,21 +491,7 @@ static int eth_init(struct device *dev)
#if defined(CONFIG_ETH_GECKO_RANDOM_MAC) #if defined(CONFIG_ETH_GECKO_RANDOM_MAC)
static void generate_random_mac(u8_t mac_addr[6]) static void generate_random_mac(u8_t mac_addr[6])
{ {
u32_t entropy; gen_random_mac(mac_addr, SILABS_OUI_B0, SILABS_OUI_B1, SILABS_OUI_B2);
entropy = sys_rand32_get();
/* SiLabs' OUI */
mac_addr[0] = SILABS_OUI_B0;
mac_addr[1] = SILABS_OUI_B1;
mac_addr[2] = SILABS_OUI_B2;
mac_addr[3] = entropy >> 0;
mac_addr[4] = entropy >> 8;
mac_addr[5] = entropy >> 16;
/* Set MAC address locally administered, unicast (LAA) */
mac_addr[0] |= 0x02;
} }
#endif #endif

View file

@ -22,6 +22,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <sys/printk.h> #include <sys/printk.h>
#include "eth.h"
/* flags */ /* flags */
#define LITEETH_EV_TX 0x1 #define LITEETH_EV_TX 0x1
#define LITEETH_EV_RX 0x1 #define LITEETH_EV_RX 0x1
@ -177,15 +179,7 @@ static void eth_irq_handler(struct device *port)
#ifdef CONFIG_ETH_LITEETH_0_RANDOM_MAC #ifdef CONFIG_ETH_LITEETH_0_RANDOM_MAC
static void generate_mac(u8_t *mac_addr) static void generate_mac(u8_t *mac_addr)
{ gen_random_mac(mac_addr, 0x10, 0xe2, 0xd5);
u32_t entropy;
entropy = sys_rand32_get();
mac_addr[3] = entropy >> 8;
mac_addr[4] = entropy >> 16;
/* Locally administered, unicast */
mac_addr[5] = ((entropy >> 0) & 0xfc) | 0x02;
} }
#endif #endif

View file

@ -43,6 +43,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <drivers/clock_control.h> #include <drivers/clock_control.h>
#endif #endif
#include "eth.h"
#define FREESCALE_OUI_B0 0x00 #define FREESCALE_OUI_B0 0x00
#define FREESCALE_OUI_B1 0x04 #define FREESCALE_OUI_B1 0x04
#define FREESCALE_OUI_B2 0x9f #define FREESCALE_OUI_B2 0x9f
@ -880,15 +882,8 @@ static void eth_callback(ENET_Type *base, enet_handle_t *handle,
defined(CONFIG_ETH_MCUX_1_RANDOM_MAC) defined(CONFIG_ETH_MCUX_1_RANDOM_MAC)
static void generate_random_mac(u8_t *mac_addr) static void generate_random_mac(u8_t *mac_addr)
{ {
u32_t entropy; gen_random_mac(mac_addr, FREESCALE_OUI_B0,
FREESCALE_OUI_B1, FREESCALE_OUI_B2);
entropy = sys_rand32_get();
mac_addr[0] |= 0x02; /* force LAA bit */
mac_addr[3] = entropy >> 8;
mac_addr[4] = entropy >> 16;
mac_addr[5] = entropy >> 0;
} }
#endif #endif

View file

@ -35,6 +35,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <net/lldp.h> #include <net/lldp.h>
#include "eth_native_posix_priv.h" #include "eth_native_posix_priv.h"
#include "eth.h"
#define NET_BUF_TIMEOUT K_MSEC(100) #define NET_BUF_TIMEOUT K_MSEC(100)
@ -415,12 +416,10 @@ static void eth_iface_init(struct net_if *iface)
#if defined(CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC) #if defined(CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC)
/* 00-00-5E-00-53-xx Documentation RFC 7042 */ /* 00-00-5E-00-53-xx Documentation RFC 7042 */
ctx->mac_addr[0] = 0x00; gen_random_mac(ctx->mac_addr, 0x00, 0x00, 0x5E);
ctx->mac_addr[1] = 0x00;
ctx->mac_addr[2] = 0x5E;
ctx->mac_addr[3] = 0x00; ctx->mac_addr[3] = 0x00;
ctx->mac_addr[4] = 0x53; ctx->mac_addr[4] = 0x53;
ctx->mac_addr[5] = sys_rand32_get();
/* The TUN/TAP setup script will by default set the MAC address of host /* The TUN/TAP setup script will by default set the MAC address of host
* interface to 00:00:5E:00:53:FF so do not allow that. * interface to 00:00:5E:00:53:FF so do not allow that.

View file

@ -1800,17 +1800,7 @@ static void get_mac_addr_from_i2c_eeprom(u8_t mac_addr[6])
#if defined(CONFIG_ETH_SAM_GMAC_RANDOM_MAC) #if defined(CONFIG_ETH_SAM_GMAC_RANDOM_MAC)
static void generate_random_mac(u8_t mac_addr[6]) static void generate_random_mac(u8_t mac_addr[6])
{ {
u32_t entropy; gen_random_mac(mac_addr, ATMEL_OUI_B0, ATMEL_OUI_B1, ATMEL_OUI_B2);
entropy = sys_rand32_get();
mac_addr[0] = ATMEL_OUI_B0 | 0x02; /* force LAA bit */
mac_addr[1] = ATMEL_OUI_B1;
mac_addr[2] = ATMEL_OUI_B2;
mac_addr[3] = entropy >> 8;
mac_addr[4] = entropy >> 16;
mac_addr[5] = entropy >> 0;
} }
#endif #endif

View file

@ -24,6 +24,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <drivers/clock_control.h> #include <drivers/clock_control.h>
#include <drivers/clock_control/stm32_clock_control.h> #include <drivers/clock_control/stm32_clock_control.h>
#include "eth.h"
#include "eth_stm32_hal_priv.h" #include "eth_stm32_hal_priv.h"
#if defined(CONFIG_ETH_STM32_HAL_USE_DTCM_FOR_DMA_BUFFER) && \ #if defined(CONFIG_ETH_STM32_HAL_USE_DTCM_FOR_DMA_BUFFER) && \
@ -362,15 +363,7 @@ void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth_handle)
#if defined(CONFIG_ETH_STM32_HAL_RANDOM_MAC) #if defined(CONFIG_ETH_STM32_HAL_RANDOM_MAC)
static void generate_mac(u8_t *mac_addr) static void generate_mac(u8_t *mac_addr)
{ {
u32_t entropy; gen_random_mac(mac_addr, ST_OUI_B0, ST_OUI_B1, ST_OUI_B2);
entropy = sys_rand32_get();
mac_addr[0] |= 0x02; /* force LAA bit */
mac_addr[3] = entropy >> 16;
mac_addr[4] = entropy >> 8;
mac_addr[5] = entropy >> 0;
} }
#endif #endif