b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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/drivers/Kconfig |
| 13 | +++ b/arch/um/drivers/Kconfig |
| 14 | @@ -146,6 +146,20 @@ 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_DETERMINISTIC_MAC |
| 19 | + bool "Use deterministic MAC addresses for network interfaces" |
| 20 | + default y |
| 21 | + depends on UML_NET |
| 22 | + select CRYPTO_SHA1 |
| 23 | + help |
| 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. |
| 31 | + |
| 32 | config UML_NET_ETHERTAP |
| 33 | bool "Ethertap transport" |
| 34 | depends on UML_NET |
| 35 | --- a/arch/um/drivers/net_kern.c |
| 36 | +++ b/arch/um/drivers/net_kern.c |
| 37 | @@ -25,6 +25,14 @@ |
| 38 | #include <net_kern.h> |
| 39 | #include <net_user.h> |
| 40 | |
| 41 | +#include <crypto/sha.h> |
| 42 | +#include <crypto/hash.h> |
| 43 | +#include <linux/string.h> |
| 44 | +#include <linux/crypto.h> |
| 45 | +#include <linux/err.h> |
| 46 | +#include <linux/scatterlist.h> |
| 47 | +#include "os.h" |
| 48 | + |
| 49 | #define DRIVER_NAME "uml-netdev" |
| 50 | |
| 51 | static DEFINE_SPINLOCK(opened_lock); |
| 52 | @@ -286,9 +294,51 @@ static void uml_net_user_timer_expire(st |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | +#ifdef CONFIG_UML_NET_DETERMINISTIC_MAC |
| 57 | + |
| 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) |
| 61 | +{ |
| 62 | + struct ahash_request *desc; |
| 63 | + struct crypto_ahash *tfm; |
| 64 | + struct scatterlist sg; |
| 65 | + char vmif[1024]; |
| 66 | + int ret; |
| 67 | + |
| 68 | + strcpy (vmif, umid); |
| 69 | + strcat (vmif, ifname); |
| 70 | + |
| 71 | + tfm = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC); |
| 72 | + if (IS_ERR(tfm)) |
| 73 | + return -ENOMEM; |
| 74 | + |
| 75 | + desc = ahash_request_alloc(tfm, GFP_KERNEL); |
| 76 | + if (!desc) { |
| 77 | + ret = -ENOMEM; |
| 78 | + goto out; |
| 79 | + } |
| 80 | + |
| 81 | + crypto_ahash_clear_flags(tfm, ~0); |
| 82 | + |
| 83 | + sg_init_table(&sg, 1); |
| 84 | + sg_set_buf(&sg, vmif, strlen(vmif)); |
| 85 | + |
| 86 | + ahash_request_set_crypt(desc, &sg, hash, strlen(vmif)); |
| 87 | + |
| 88 | + ret = crypto_ahash_digest(desc); |
| 89 | +out: |
| 90 | + crypto_free_ahash(tfm); |
| 91 | + |
| 92 | + return ret; |
| 93 | +} |
| 94 | + |
| 95 | +#endif |
| 96 | + |
| 97 | void uml_net_setup_etheraddr(struct net_device *dev, char *str) |
| 98 | { |
| 99 | unsigned char *addr = dev->dev_addr; |
| 100 | + u8 hash[SHA1_DIGEST_SIZE]; |
| 101 | char *end; |
| 102 | int i; |
| 103 | |
| 104 | @@ -331,9 +381,26 @@ void uml_net_setup_etheraddr(struct net_ |
| 105 | return; |
| 106 | |
| 107 | random: |
| 108 | +#ifndef CONFIG_UML_NET_DETERMINISTIC_MAC |
| 109 | printk(KERN_INFO |
| 110 | "Choosing a random ethernet address for device %s\n", dev->name); |
| 111 | eth_hw_addr_random(dev); |
| 112 | +#else |
| 113 | + printk(KERN_INFO |
| 114 | + "Computing a digest to use as ethernet address for device %s\n", dev->name); |
| 115 | + if (compute_hash(get_umid(), dev->name, hash) < 0) { |
| 116 | + printk(KERN_WARNING |
| 117 | + "Could not compute digest to use as ethernet address for device %s. " |
| 118 | + "Using random address instead.\n", dev->name); |
| 119 | + random_ether_addr(addr); |
| 120 | + } |
| 121 | + else { |
| 122 | + for (i=0; i < 6; i++) |
| 123 | + addr[i] = (hash[i] + hash[i+6]) % 0x100; |
| 124 | + } |
| 125 | + addr [0] &= 0xfe; /* clear multicast bit */ |
| 126 | + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */ |
| 127 | +#endif |
| 128 | } |
| 129 | |
| 130 | static DEFINE_SPINLOCK(devices_lock); |