Skip to contentSkip to conversation

UML - Assign random MACs to interfaces if necessary

PATCH In review Jeff Dike 3 participants 3 messages Sep 27, 2006 User-mode Linux (UML)

lore
Share & tools
lore

Jeff Dike

author
#0 lore raw

PATCH 1/5 UML - Assign random MACs to interfaces if necessary

Assign a random MAC to an ethernet interface if one was not provided on the command line. This became pressing when distros started bringing interfaces up before assigning IPs to them. The previous pattern of assigning an IP then bringing it up allowed the MAC to be generated from the first IP assigned. However, once the thing is up, it's probably a bad idea to change the MAC, so the MAC stayed initialized to fe:fd:0:0:0:0.

Now, if there is no MAC from the command line, one is generated. We use the microseconds from gettimeofday (20 bits), plus the low 12 bits of the pid to seed the random number generator. random() is called twice, with 16 bits of each result used. I didn't want to have to try to fill in 32 bits optimally given an arbitrary RAND_MAX, so I just assume that it is greater than 65536 and use 16 bits of each random() return.

There is also a bit of reformatting and whitespace cleanup here.

---

 arch/um/drivers/net_kern.c |   15 +++++++++++----
 arch/um/drivers/net_user.c |   30 ++++++++++++++++++++++++++++++
 arch/um/include/net_user.h |   19 +++++--------------
 3 files changed, 46 insertions(+), 18 deletions(-)
arch/um/drivers/net_user.c +30 -0
Index: linux-2.6.18-mm/arch/um/drivers/net_user.c
===================================================================
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
+#include <sys/time.h>
 #include "user.h"
 #include "user_util.h"
 #include "kern_util.h"
@@ -258,3 +259,32 @@ char *split_if_spec(char *str, ...)
 	va_end(ap);
 	return str;
 }
+
+void random_mac(unsigned char *addr)
+{
+	struct timeval tv;
+	long n;
+	unsigned int seed;
+
+	gettimeofday(&tv, NULL);
+
+	/* Assume that 20 bits of microseconds and 12 bits of the pid are
+	 * reasonably unpredictable.
+	 */
+	seed = tv.tv_usec | (os_getpid() << 20);
+	srandom(seed);
+
+	/* Don't care about endianness here - switching endianness
+	 * just rearranges what are hopefully random numbers.
+	 *
+	 * Assume that RAND_MAX > 65536, so random is called twice and
+	 * we use 16 bits of the result.
+	 */
+	n = random();
+	addr[2] = (n >> 8) & 255;
+	addr[3] = n % 255;
+
+	n = random();
+	addr[4] = (n >> 8) & 255;
+	addr[5] = n % 255;
+}
arch/um/include/net_user.h +5 -14
Index: linux-2.6.18-mm/arch/um/include/net_user.h
===================================================================
@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2002 Jeff Dike ([email protected])
  * Licensed under the GPL
  */
@@ -26,8 +26,8 @@ struct net_user_info {
 
 extern void ether_user_init(void *data, void *dev);
 extern void dev_ip_addr(void *d, unsigned char *bin_buf);
-extern void iter_addresses(void *d, void (*cb)(unsigned char *, 
-					       unsigned char *, void *), 
+extern void iter_addresses(void *d, void (*cb)(unsigned char *,
+					       unsigned char *, void *),
 			   void *arg);
 
 extern void *get_output_buffer(int *len_out);
@@ -51,15 +51,6 @@ extern char *split_if_spec(char *str, ..
 
 extern int dev_netmask(void *d, void *m);
 
-#endif
+extern void random_mac(unsigned char *addr);
 
-/*
- * Overrides for Emacs so that we follow Linus's tabbing style.
- * Emacs will notice this stuff at the end of the file and automatically
- * adjust the settings for this buffer only.  This must remain at the end
- * of the file.
- * ---------------------------------------------------------------------------
- * Local variables:
- * c-file-style: "linux"
- * End:
- */
+#endif
arch/um/drivers/net_kern.c +11 -4
Index: linux-2.6.18-mm/arch/um/drivers/net_kern.c
===================================================================
@@ -752,7 +752,8 @@ int setup_etheraddr(char *str, unsigned 
 	int i;
 
 	if(str == NULL)
-		return(0);
+		goto random;
+
 	for(i=0;i<6;i++){
 		addr[i] = simple_strtoul(str, &end, 16);
 		if((end == str) ||
@@ -760,7 +761,7 @@ int setup_etheraddr(char *str, unsigned 
 			printk(KERN_ERR 
 			       "setup_etheraddr: failed to parse '%s' "
 			       "as an ethernet address\n", str);
-			return(0);
+			goto random;
 		}
 		str = end + 1;
 	}
@@ -768,9 +769,15 @@ int setup_etheraddr(char *str, unsigned 
 		printk(KERN_ERR 
 		       "Attempt to assign a broadcast ethernet address to a "
 		       "device disallowed\n");
-		return(0);
+		goto random;
 	}
-	return(1);
+	return 1;
+
+random:
+	addr[0] = 0xfe;
+	addr[1] = 0xfd;
+	random_mac(addr);
+	return 1;
 }
 
 void dev_ip_addr(void *d, unsigned char *bin_buf)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

#1 lore raw

Quoting Jeff Dike <[email protected]>:

Quoted 16 lines

Assign a random MAC to an ethernet interface if one was not provided on the command line. This became pressing when distros started bringing interfaces up before assigning IPs to them. The previous pattern of assigning an IP then bringing it up allowed the MAC to be generated from the first IP assigned. However, once the thing is up, it's probably a bad idea to change the MAC, so the MAC stayed initialized to fe:fd:0:0:0:0.

Now, if there is no MAC from the command line, one is generated. We use the microseconds from gettimeofday (20 bits), plus the low 12 bits of the pid to seed the random number generator. random() is called twice, with 16 bits of each result used. I didn't want to have to try to fill in 32 bits optimally given an arbitrary RAND_MAX, so I just assume that it is greater than 65536 and use 16 bits of each random() return.

Quoted 4 lines

There is also a bit of reformatting and whitespace cleanup here.

Couldn't you use random_ether_addr() from linux/etherdevice.h?

static inline void random_ether_addr(u8 *addr)

{
         get_random_bytes (addr, ETH_ALEN);
         addr [0] &= 0xfe;       /* clear multicast bit */
         addr [0] |= 0x02;       /* set local assignment bit (IEEE802) */
}

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

#2 lore raw
In reply to Jeff Dike
Quoted 7 lines · Jeff Dike

Now, if there is no MAC from the command line, one is generated. We use the microseconds from gettimeofday (20 bits), plus the low 12 bits of the pid to seed the random number generator. random() is called twice, with 16 bits of each result used. I didn't want to have to try to fill in 32 bits optimally given an arbitrary RAND_MAX, so I just assume that it is greater than 65536 and use 16 bits of each random() return.

Why not use random_ether_addr() like dummy, tun, ifb and others?

Jason