From 7867ceb8dc5c4d2c64d337aa130c57a05d9b556a Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Fri, 11 Jun 2010 14:11:12 +0200 Subject: net: implement random_ether_addr Signed-off-by: Sascha Hauer --- include/net.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/net.h b/include/net.h index 709e76c895..c695e5f72a 100644 --- a/include/net.h +++ b/include/net.h @@ -17,6 +17,7 @@ #include #include #include +#include #include /* 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 -- cgit v1.2.3