From d630052e6f44ce215503fb7f20819a1382ebfa9b Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Sun, 1 Jun 2014 23:26:20 +0100 Subject: [ethernet] Provide eth_random_addr() to generate random Ethernet addresses Modified-by: Michael Brown Signed-off-by: Michael Brown --- src/net/ethernet.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/net') diff --git a/src/net/ethernet.c b/src/net/ethernet.c index a2e565899..03978c2a8 100644 --- a/src/net/ethernet.c +++ b/src/net/ethernet.c @@ -20,6 +20,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include +#include #include #include #include @@ -112,6 +113,21 @@ void eth_init_addr ( const void *hw_addr, void *ll_addr ) { memcpy ( ll_addr, hw_addr, ETH_ALEN ); } +/** + * Generate random Ethernet address + * + * @v hw_addr Generated hardware address + */ +void eth_random_addr ( void *hw_addr ) { + uint8_t *addr = hw_addr; + unsigned int i; + + for ( i = 0 ; i < ETH_ALEN ; i++ ) + addr[i] = random(); + addr[0] &= ~0x01; /* Clear multicast bit */ + addr[0] |= 0x02; /* Set locally-assigned bit */ +} + /** * Transcribe Ethernet address * -- cgit v1.2.3-55-g7522