1 ===============================================================================
3 This patch makes MAC addresses of network interfaces predictable. In
4 particular, it adds a small routine that computes MAC addresses of based on
5 a SHA1 hash of the virtual machine name and interface ID.
9 Applies to vanilla kernel 3.9.4.
11 ===============================================================================
12 --- a/arch/um/Kconfig.net
13 +++ b/arch/um/Kconfig.net
14 @@ -21,6 +21,19 @@ config UML_NET
15 enable at least one of the following transport options to actually
16 make use of UML networking.
18 +config UML_NET_RANDOM_MAC
19 + bool "Use random MAC addresses for network interfaces"
23 + Virtual network devices inside a User-Mode Linux instance must be
24 + assigned a MAC (Ethernet) address. If none is specified on the UML
25 + command line, one must be automatically computed. If this option is
26 + enabled, a randomly generated address is used. Otherwise, if this
27 + option is disabled, the address is generated from a SHA1 hash of
28 + the umid of the UML instance and the interface name. The latter choice
29 + is useful to make MAC addresses predictable.
31 config UML_NET_ETHERTAP
32 bool "Ethertap transport"
34 --- a/arch/um/drivers/net_kern.c
35 +++ b/arch/um/drivers/net_kern.c
40 +#include <crypto/sha.h>
41 +#include <linux/string.h>
42 +#include <linux/crypto.h>
43 +#include <linux/err.h>
44 +#include <linux/scatterlist.h>
47 #define DRIVER_NAME "uml-netdev"
49 static DEFINE_SPINLOCK(opened_lock);
50 @@ -295,11 +302,47 @@ static void uml_net_user_timer_expire(un
54 +#ifndef CONFIG_UML_NET_RANDOM_MAC
56 +/* Compute a SHA1 hash of the UML instance's id and
57 + * * an interface name. */
58 +static int compute_hash(const char *umid, const char *ifname, char *hash) {
60 + struct scatterlist sg;
61 + struct crypto_hash *tfm;
62 + struct hash_desc desc;
64 + strcpy (vmif, umid);
65 + strcat (vmif, ifname);
67 + tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
74 + sg_init_table(&sg, 1);
75 + sg_set_buf(&sg, vmif, strlen(vmif));
77 + if (crypto_hash_digest(&desc, &sg, strlen(vmif), hash)) {
78 + crypto_free_hash(tfm);
82 + crypto_free_hash(tfm);
89 static void setup_etheraddr(struct net_device *dev, char *str)
91 unsigned char *addr = dev->dev_addr;
94 + u8 hash[SHA1_DIGEST_SIZE];
98 @@ -340,9 +383,26 @@ static void setup_etheraddr(struct net_d
102 +#ifdef CONFIG_UML_NET_RANDOM_MAC
104 "Choosing a random ethernet address for device %s\n", dev->name);
105 eth_hw_addr_random(dev);
108 + "Computing a digest to use as ethernet address for device %s\n", dev->name);
109 + if (compute_hash(get_umid(), dev->name, hash)) {
110 + printk(KERN_WARNING
111 + "Could not compute digest to use as ethernet address for device %s. "
112 + "Using random address instead.\n", dev->name);
113 + random_ether_addr(addr);
116 + for (i=0; i < 6; i++)
117 + addr[i] = (hash[i] + hash[i+6]) % 0x100;
119 + addr [0] &= 0xfe; /* clear multicast bit */
120 + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
124 static DEFINE_SPINLOCK(devices_lock);