]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[ethernet] Provide eth_random_addr() to generate random Ethernet addresses
authorHannes Reinecke <hare@suse.de>
Sun, 1 Jun 2014 22:26:20 +0000 (23:26 +0100)
committerMichael Brown <mcb30@ipxe.org>
Sun, 1 Jun 2014 22:32:24 +0000 (23:32 +0100)
Modified-by: Michael Brown <mcb30@ipxe.org>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/include/ipxe/ethernet.h
src/net/ethernet.c

index 5ffc45b738ffb1de8dcdf9899a05ef393daf6637..b28a6b8e14af0e9c03b1064b37007bfbc8458240 100644 (file)
@@ -87,6 +87,7 @@ extern int eth_pull ( struct net_device *netdev, struct io_buffer *iobuf,
                      const void **ll_dest, const void **ll_source,
                      uint16_t *net_proto, unsigned int *flags );
 extern void eth_init_addr ( const void *hw_addr, void *ll_addr );
+extern void eth_random_addr ( void *hw_addr );
 extern const char * eth_ntoa ( const void *ll_addr );
 extern int eth_mc_hash ( unsigned int af, const void *net_addr,
                         void *ll_addr );
index a2e565899aad0fa1f9521c1d2283b9b52ff2a363..03978c2a81ffb68f4d024f1e0d4baa2fbf65be7c 100644 (file)
@@ -20,6 +20,7 @@
 FILE_LICENCE ( GPL2_OR_LATER );
 
 #include <stdint.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <byteswap.h>
@@ -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
  *