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
 		      const void **ll_dest, const void **ll_source,
87
 		      const void **ll_dest, const void **ll_source,
88
 		      uint16_t *net_proto, unsigned int *flags );
88
 		      uint16_t *net_proto, unsigned int *flags );
89
 extern void eth_init_addr ( const void *hw_addr, void *ll_addr );
89
 extern void eth_init_addr ( const void *hw_addr, void *ll_addr );
90
+extern void eth_random_addr ( void *hw_addr );
90
 extern const char * eth_ntoa ( const void *ll_addr );
91
 extern const char * eth_ntoa ( const void *ll_addr );
91
 extern int eth_mc_hash ( unsigned int af, const void *net_addr,
92
 extern int eth_mc_hash ( unsigned int af, const void *net_addr,
92
 			 void *ll_addr );
93
 			 void *ll_addr );

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

20
 FILE_LICENCE ( GPL2_OR_LATER );
20
 FILE_LICENCE ( GPL2_OR_LATER );
21
 
21
 
22
 #include <stdint.h>
22
 #include <stdint.h>
23
+#include <stdlib.h>
23
 #include <stdio.h>
24
 #include <stdio.h>
24
 #include <string.h>
25
 #include <string.h>
25
 #include <byteswap.h>
26
 #include <byteswap.h>
112
 	memcpy ( ll_addr, hw_addr, ETH_ALEN );
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
  * Transcribe Ethernet address
132
  * Transcribe Ethernet address
117
  *
133
  *

Loading…
Cancel
Save