Browse Source

[ethernet] Provide eth_random_addr() to generate random Ethernet addresses

Modified-by: Michael Brown <mcb30@ipxe.org>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Hannes Reinecke 10 years ago
parent
commit
d630052e6f
2 changed files with 17 additions and 0 deletions
  1. 1
    0
      src/include/ipxe/ethernet.h
  2. 16
    0
      src/net/ethernet.c

+ 1
- 0
src/include/ipxe/ethernet.h View File

@@ -87,6 +87,7 @@ extern int eth_pull ( struct net_device *netdev, struct io_buffer *iobuf,
87 87
 		      const void **ll_dest, const void **ll_source,
88 88
 		      uint16_t *net_proto, unsigned int *flags );
89 89
 extern void eth_init_addr ( const void *hw_addr, void *ll_addr );
90
+extern void eth_random_addr ( void *hw_addr );
90 91
 extern const char * eth_ntoa ( const void *ll_addr );
91 92
 extern int eth_mc_hash ( unsigned int af, const void *net_addr,
92 93
 			 void *ll_addr );

+ 16
- 0
src/net/ethernet.c View File

@@ -20,6 +20,7 @@
20 20
 FILE_LICENCE ( GPL2_OR_LATER );
21 21
 
22 22
 #include <stdint.h>
23
+#include <stdlib.h>
23 24
 #include <stdio.h>
24 25
 #include <string.h>
25 26
 #include <byteswap.h>
@@ -112,6 +113,21 @@ void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
112 113
 	memcpy ( ll_addr, hw_addr, ETH_ALEN );
113 114
 }
114 115
 
116
+/**
117
+ * Generate random Ethernet address
118
+ *
119
+ * @v hw_addr		Generated hardware address
120
+ */
121
+void eth_random_addr ( void *hw_addr ) {
122
+	uint8_t *addr = hw_addr;
123
+	unsigned int i;
124
+
125
+	for ( i = 0 ; i < ETH_ALEN ; i++ )
126
+		addr[i] = random();
127
+	addr[0] &= ~0x01; /* Clear multicast bit */
128
+	addr[0] |= 0x02; /* Set locally-assigned bit */
129
+}
130
+
115 131
 /**
116 132
  * Transcribe Ethernet address
117 133
  *

Loading…
Cancel
Save