1
0

102-pseudo-random-mac.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @@ -22,6 +22,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,14 @@
  32. #include <net_kern.h>
  33. #include <net_user.h>
  34. +#include <crypto/sha.h>
  35. +#include <crypto/hash.h>
  36. +#include <linux/string.h>
  37. +#include <linux/crypto.h>
  38. +#include <linux/err.h>
  39. +#include <linux/scatterlist.h>
  40. +#include "os.h"
  41. +
  42. #define DRIVER_NAME "uml-netdev"
  43. static DEFINE_SPINLOCK(opened_lock);
  44. @@ -288,11 +296,53 @@ static void uml_net_user_timer_expire(un
  45. #endif
  46. }
  47. +#ifndef CONFIG_UML_NET_RANDOM_MAC
  48. +
  49. +/* Compute a SHA1 hash of the UML instance's id and
  50. + * * an interface name. */
  51. +static int compute_hash(const char *umid, const char *ifname, char *hash)
  52. +{
  53. + struct ahash_request *desc;
  54. + struct crypto_ahash *tfm;
  55. + struct scatterlist sg;
  56. + char vmif[1024];
  57. + int ret;
  58. +
  59. + strcpy (vmif, umid);
  60. + strcat (vmif, ifname);
  61. +
  62. + tfm = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
  63. + if (IS_ERR(tfm))
  64. + return -ENOMEM;
  65. +
  66. + desc = ahash_request_alloc(tfm, GFP_KERNEL);
  67. + if (!desc) {
  68. + ret = -ENOMEM;
  69. + goto out;
  70. + }
  71. +
  72. + crypto_ahash_clear_flags(tfm, ~0);
  73. +
  74. + sg_init_table(&sg, 1);
  75. + sg_set_buf(&sg, vmif, strlen(vmif));
  76. +
  77. + ahash_request_set_crypt(desc, &sg, hash, strlen(vmif));
  78. +
  79. + ret = crypto_ahash_digest(desc);
  80. +out:
  81. + crypto_free_ahash(tfm);
  82. +
  83. + return ret;
  84. +}
  85. +
  86. +#endif
  87. +
  88. static void setup_etheraddr(struct net_device *dev, char *str)
  89. {
  90. unsigned char *addr = dev->dev_addr;
  91. char *end;
  92. int i;
  93. + u8 hash[SHA1_DIGEST_SIZE];
  94. if (str == NULL)
  95. goto random;
  96. @@ -333,9 +383,26 @@ static void setup_etheraddr(struct net_d
  97. return;
  98. random:
  99. +#ifdef CONFIG_UML_NET_RANDOM_MAC
  100. printk(KERN_INFO
  101. "Choosing a random ethernet address for device %s\n", dev->name);
  102. eth_hw_addr_random(dev);
  103. +#else
  104. + printk(KERN_INFO
  105. + "Computing a digest to use as ethernet address for device %s\n", dev->name);
  106. + if (compute_hash(get_umid(), dev->name, hash) < 0) {
  107. + printk(KERN_WARNING
  108. + "Could not compute digest to use as ethernet address for device %s. "
  109. + "Using random address instead.\n", dev->name);
  110. + random_ether_addr(addr);
  111. + }
  112. + else {
  113. + for (i=0; i < 6; i++)
  114. + addr[i] = (hash[i] + hash[i+6]) % 0x100;
  115. + }
  116. + addr [0] &= 0xfe; /* clear multicast bit */
  117. + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
  118. +#endif
  119. }
  120. static DEFINE_SPINLOCK(devices_lock);