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 @@ -22,6 +22,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 <crypto/hash.h>
42 +#include <linux/string.h>
43 +#include <linux/crypto.h>
44 +#include <linux/err.h>
45 +#include <linux/scatterlist.h>
48 #define DRIVER_NAME "uml-netdev"
50 static DEFINE_SPINLOCK(opened_lock);
51 @@ -288,11 +296,53 @@ static void uml_net_user_timer_expire(un
55 +#ifndef CONFIG_UML_NET_RANDOM_MAC
57 +/* Compute a SHA1 hash of the UML instance's id and
58 + * * an interface name. */
59 +static int compute_hash(const char *umid, const char *ifname, char *hash)
61 + struct ahash_request *desc;
62 + struct crypto_ahash *tfm;
63 + struct scatterlist sg;
67 + strcpy (vmif, umid);
68 + strcat (vmif, ifname);
70 + tfm = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
74 + desc = ahash_request_alloc(tfm, GFP_KERNEL);
80 + crypto_ahash_clear_flags(tfm, ~0);
82 + sg_init_table(&sg, 1);
83 + sg_set_buf(&sg, vmif, strlen(vmif));
85 + ahash_request_set_crypt(desc, &sg, hash, strlen(vmif));
87 + ret = crypto_ahash_digest(desc);
89 + crypto_free_ahash(tfm);
96 static void setup_etheraddr(struct net_device *dev, char *str)
98 unsigned char *addr = dev->dev_addr;
101 + u8 hash[SHA1_DIGEST_SIZE];
105 @@ -333,9 +383,26 @@ static void setup_etheraddr(struct net_d
109 +#ifdef CONFIG_UML_NET_RANDOM_MAC
111 "Choosing a random ethernet address for device %s\n", dev->name);
112 eth_hw_addr_random(dev);
115 + "Computing a digest to use as ethernet address for device %s\n", dev->name);
116 + if (compute_hash(get_umid(), dev->name, hash) < 0) {
117 + printk(KERN_WARNING
118 + "Could not compute digest to use as ethernet address for device %s. "
119 + "Using random address instead.\n", dev->name);
120 + random_ether_addr(addr);
123 + for (i=0; i < 6; i++)
124 + addr[i] = (hash[i] + hash[i+6]) % 0x100;
126 + addr [0] &= 0xfe; /* clear multicast bit */
127 + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
131 static DEFINE_SPINLOCK(devices_lock);