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 diff -aur linux-3.9.4-orig/arch/um/Kconfig.net linux-3.9.4/arch/um/Kconfig.net
13 --- linux-3.9.4-orig/arch/um/Kconfig.net 2013-05-24 21:45:59.000000000 +0300
14 +++ linux-3.9.4/arch/um/Kconfig.net 2013-06-11 13:07:06.363999999 +0300
16 enable at least one of the following transport options to actually
17 make use of UML networking.
19 +config UML_NET_RANDOM_MAC
20 + bool "Use random MAC addresses for network interfaces"
24 + Virtual network devices inside a User-Mode Linux instance must be
25 + assigned a MAC (Ethernet) address. If none is specified on the UML
26 + command line, one must be automatically computed. If this option is
27 + enabled, a randomly generated address is used. Otherwise, if this
28 + option is disabled, the address is generated from a SHA1 hash of
29 + the umid of the UML instance and the interface name. The latter choice
30 + is useful to make MAC addresses predictable.
32 config UML_NET_ETHERTAP
33 bool "Ethertap transport"
35 diff -aur linux-3.9.4-orig/arch/um/drivers/net_kern.c linux-3.9.4/arch/um/drivers/net_kern.c
36 --- linux-3.9.4-orig/arch/um/drivers/net_kern.c 2013-05-24 21:45:59.000000000 +0300
37 +++ linux-3.9.4/arch/um/drivers/net_kern.c 2013-06-11 13:09:03.452000001 +0300
42 +#include <crypto/sha.h>
43 +#include <linux/string.h>
44 +#include <linux/crypto.h>
45 +#include <linux/err.h>
46 +#include <linux/scatterlist.h>
49 #define DRIVER_NAME "uml-netdev"
51 static DEFINE_SPINLOCK(opened_lock);
56 +#ifndef CONFIG_UML_NET_RANDOM_MAC
58 +/* Compute a SHA1 hash of the UML instance's id and
59 + * * an interface name. */
60 +static int compute_hash(const char *umid, const char *ifname, char *hash) {
62 + struct scatterlist sg;
63 + struct crypto_hash *tfm;
64 + struct hash_desc desc;
66 + strcpy (vmif, umid);
67 + strcat (vmif, ifname);
69 + tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
76 + sg_init_table(&sg, 1);
77 + sg_set_buf(&sg, vmif, strlen(vmif));
79 + if (crypto_hash_digest(&desc, &sg, strlen(vmif), hash)) {
80 + crypto_free_hash(tfm);
84 + crypto_free_hash(tfm);
91 static void setup_etheraddr(struct net_device *dev, char *str)
93 unsigned char *addr = dev->dev_addr;
96 + u8 hash[SHA1_DIGEST_SIZE];
104 +#ifdef CONFIG_UML_NET_RANDOM_MAC
106 "Choosing a random ethernet address for device %s\n", dev->name);
107 eth_hw_addr_random(dev);
110 + "Computing a digest to use as ethernet address for device %s\n", dev->name);
111 + if (compute_hash(get_umid(), dev->name, hash)) {
112 + printk(KERN_WARNING
113 + "Could not compute digest to use as ethernet address for device %s. "
114 + "Using random address instead.\n", dev->name);
115 + random_ether_addr(addr);
118 + for (i=0; i < 6; i++)
119 + addr[i] = (hash[i] + hash[i+6]) % 0x100;
121 + addr [0] &= 0xfe; /* clear multicast bit */
122 + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
126 static DEFINE_SPINLOCK(devices_lock);
127 Only in linux-3.9.4/arch/um/drivers: net_kern.c.orig
128 Only in linux-3.9.4/arch/um/drivers: net_kern.c.rej
129 Only in linux-3.9.4/arch/um/drivers: net_kern.c~