uml: update to linux 4.1
[openwrt/openwrt.git] / target / linux / uml / patches-4.1 / 102-pseudo-random-mac.patch
1 ===============================================================================
2
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.
6
7 TECHNICAL INFORMATION:
8
9 Applies to vanilla kernel 3.9.4.
10
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.
17
18 +config UML_NET_RANDOM_MAC
19 + bool "Use random MAC addresses for network interfaces"
20 + default n
21 + depends on UML_NET
22 + help
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.
30 +
31 config UML_NET_ETHERTAP
32 bool "Ethertap transport"
33 depends on UML_NET
34 --- a/arch/um/drivers/net_kern.c
35 +++ b/arch/um/drivers/net_kern.c
36 @@ -25,6 +25,13 @@
37 #include <net_kern.h>
38 #include <net_user.h>
39
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>
45 +#include "os.h"
46 +
47 #define DRIVER_NAME "uml-netdev"
48
49 static DEFINE_SPINLOCK(opened_lock);
50 @@ -295,11 +302,47 @@ static void uml_net_user_timer_expire(un
51 #endif
52 }
53
54 +#ifndef CONFIG_UML_NET_RANDOM_MAC
55 +
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) {
59 + char vmif[1024];
60 + struct scatterlist sg;
61 + struct crypto_hash *tfm;
62 + struct hash_desc desc;
63 +
64 + strcpy (vmif, umid);
65 + strcat (vmif, ifname);
66 +
67 + tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
68 + if (IS_ERR(tfm))
69 + return 1;
70 +
71 + desc.tfm = tfm;
72 + desc.flags = 0;
73 +
74 + sg_init_table(&sg, 1);
75 + sg_set_buf(&sg, vmif, strlen(vmif));
76 +
77 + if (crypto_hash_digest(&desc, &sg, strlen(vmif), hash)) {
78 + crypto_free_hash(tfm);
79 + return 1;
80 + }
81 +
82 + crypto_free_hash(tfm);
83 +
84 + return 0;
85 +}
86 +
87 +#endif
88 +
89 static void setup_etheraddr(struct net_device *dev, char *str)
90 {
91 unsigned char *addr = dev->dev_addr;
92 char *end;
93 int i;
94 + u8 hash[SHA1_DIGEST_SIZE];
95
96 if (str == NULL)
97 goto random;
98 @@ -340,9 +383,26 @@ static void setup_etheraddr(struct net_d
99 return;
100
101 random:
102 +#ifdef CONFIG_UML_NET_RANDOM_MAC
103 printk(KERN_INFO
104 "Choosing a random ethernet address for device %s\n", dev->name);
105 eth_hw_addr_random(dev);
106 +#else
107 + printk(KERN_INFO
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);
114 + }
115 + else {
116 + for (i=0; i < 6; i++)
117 + addr[i] = (hash[i] + hash[i+6]) % 0x100;
118 + }
119 + addr [0] &= 0xfe; /* clear multicast bit */
120 + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
121 +#endif
122 }
123
124 static DEFINE_SPINLOCK(devices_lock);