102-pseudo-random-mac.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ===============================================================================
  2. This patch makes MAC addresses of network interfaces predictable. In
  3. particular, it adds a small routine that computes MAC addresses of based on
  4. a SHA1 hash of the virtual machine name and interface ID.
  5. TECHNICAL INFORMATION:
  6. Applies to vanilla kernel 3.9.4.
  7. ===============================================================================
  8. --- a/arch/um/Kconfig.net
  9. +++ b/arch/um/Kconfig.net
  10. @@ -21,6 +21,19 @@ config UML_NET
  11. enable at least one of the following transport options to actually
  12. make use of UML networking.
  13. +config UML_NET_RANDOM_MAC
  14. + bool "Use random MAC addresses for network interfaces"
  15. + default n
  16. + depends on UML_NET
  17. + help
  18. + Virtual network devices inside a User-Mode Linux instance must be
  19. + assigned a MAC (Ethernet) address. If none is specified on the UML
  20. + command line, one must be automatically computed. If this option is
  21. + enabled, a randomly generated address is used. Otherwise, if this
  22. + option is disabled, the address is generated from a SHA1 hash of
  23. + the umid of the UML instance and the interface name. The latter choice
  24. + is useful to make MAC addresses predictable.
  25. +
  26. config UML_NET_ETHERTAP
  27. bool "Ethertap transport"
  28. depends on UML_NET
  29. --- a/arch/um/drivers/net_kern.c
  30. +++ b/arch/um/drivers/net_kern.c
  31. @@ -25,6 +25,13 @@
  32. #include <net_kern.h>
  33. #include <net_user.h>
  34. +#include <crypto/sha.h>
  35. +#include <linux/string.h>
  36. +#include <linux/crypto.h>
  37. +#include <linux/err.h>
  38. +#include <linux/scatterlist.h>
  39. +#include "os.h"
  40. +
  41. #define DRIVER_NAME "uml-netdev"
  42. static DEFINE_SPINLOCK(opened_lock);
  43. @@ -295,11 +302,47 @@ static void uml_net_user_timer_expire(un
  44. #endif
  45. }
  46. +#ifndef CONFIG_UML_NET_RANDOM_MAC
  47. +
  48. +/* Compute a SHA1 hash of the UML instance's id and
  49. + * * an interface name. */
  50. +static int compute_hash(const char *umid, const char *ifname, char *hash) {
  51. + char vmif[1024];
  52. + struct scatterlist sg;
  53. + struct crypto_hash *tfm;
  54. + struct hash_desc desc;
  55. +
  56. + strcpy (vmif, umid);
  57. + strcat (vmif, ifname);
  58. +
  59. + tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
  60. + if (IS_ERR(tfm))
  61. + return 1;
  62. +
  63. + desc.tfm = tfm;
  64. + desc.flags = 0;
  65. +
  66. + sg_init_table(&sg, 1);
  67. + sg_set_buf(&sg, vmif, strlen(vmif));
  68. +
  69. + if (crypto_hash_digest(&desc, &sg, strlen(vmif), hash)) {
  70. + crypto_free_hash(tfm);
  71. + return 1;
  72. + }
  73. +
  74. + crypto_free_hash(tfm);
  75. +
  76. + return 0;
  77. +}
  78. +
  79. +#endif
  80. +
  81. static void setup_etheraddr(struct net_device *dev, char *str)
  82. {
  83. unsigned char *addr = dev->dev_addr;
  84. char *end;
  85. int i;
  86. + u8 hash[SHA1_DIGEST_SIZE];
  87. if (str == NULL)
  88. goto random;
  89. @@ -340,9 +383,26 @@ static void setup_etheraddr(struct net_d
  90. return;
  91. random:
  92. +#ifdef CONFIG_UML_NET_RANDOM_MAC
  93. printk(KERN_INFO
  94. "Choosing a random ethernet address for device %s\n", dev->name);
  95. eth_hw_addr_random(dev);
  96. +#else
  97. + printk(KERN_INFO
  98. + "Computing a digest to use as ethernet address for device %s\n", dev->name);
  99. + if (compute_hash(get_umid(), dev->name, hash)) {
  100. + printk(KERN_WARNING
  101. + "Could not compute digest to use as ethernet address for device %s. "
  102. + "Using random address instead.\n", dev->name);
  103. + random_ether_addr(addr);
  104. + }
  105. + else {
  106. + for (i=0; i < 6; i++)
  107. + addr[i] = (hash[i] + hash[i+6]) % 0x100;
  108. + }
  109. + addr [0] &= 0xfe; /* clear multicast bit */
  110. + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
  111. +#endif
  112. }
  113. static DEFINE_SPINLOCK(devices_lock);