9
0
Fork 0

net: implement random_ether_addr

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2010-06-11 14:11:12 +02:00
parent 738d70e430
commit 7867ceb8dc
1 changed files with 16 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include <param.h>
#include <malloc.h>
#include <stdlib.h>
#include <clock.h>
#include <asm/byteorder.h> /* for nton* / ntoh* stuff */
@ -331,6 +332,21 @@ static inline int is_broadcast_ether_addr(const u8 *addr)
return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
}
/**
* random_ether_addr - Generate software assigned random Ethernet address
* @addr: Pointer to a six-byte array containing the Ethernet address
*
* Generate a random Ethernet address (MAC) that is not multicast
* and has the local assigned bit set.
*/
static inline void random_ether_addr(u8 *addr)
{
srand(get_time_ns());
get_random_bytes(addr, 6);
addr [0] &= 0xfe; /* clear multicast bit */
addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
}
/**
* is_valid_ether_addr - Determine if the given Ethernet address is valid
* @addr: Pointer to a six-byte array containing the Ethernet address