2
0

102-pseudo-random-mac.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/drivers/Kconfig
  9. +++ b/arch/um/drivers/Kconfig
  10. @@ -146,6 +146,20 @@ 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_DETERMINISTIC_MAC
  14. + bool "Use deterministic MAC addresses for network interfaces"
  15. + default y
  16. + depends on UML_NET
  17. + select CRYPTO_SHA1
  18. + help
  19. + Virtual network devices inside a User-Mode Linux instance must be
  20. + assigned a MAC (Ethernet) address. If none is specified on the UML
  21. + command line, one must be automatically computed. If this option is
  22. + enabled, a randomly generated address is used. Otherwise, if this
  23. + option is disabled, the address is generated from a SHA1 hash of
  24. + the umid of the UML instance and the interface name. The latter choice
  25. + is useful to make MAC addresses predictable.
  26. +
  27. config UML_NET_ETHERTAP
  28. bool "Ethertap transport"
  29. depends on UML_NET
  30. --- a/arch/um/drivers/net_kern.c
  31. +++ b/arch/um/drivers/net_kern.c
  32. @@ -25,6 +25,14 @@
  33. #include <net_kern.h>
  34. #include <net_user.h>
  35. +#include <crypto/sha.h>
  36. +#include <crypto/hash.h>
  37. +#include <linux/string.h>
  38. +#include <linux/crypto.h>
  39. +#include <linux/err.h>
  40. +#include <linux/scatterlist.h>
  41. +#include "os.h"
  42. +
  43. #define DRIVER_NAME "uml-netdev"
  44. static DEFINE_SPINLOCK(opened_lock);
  45. @@ -286,9 +294,51 @@ static void uml_net_user_timer_expire(st
  46. #endif
  47. }
  48. +#ifdef CONFIG_UML_NET_DETERMINISTIC_MAC
  49. +
  50. +/* Compute a SHA1 hash of the UML instance's id and
  51. + * * an interface name. */
  52. +static int compute_hash(const char *umid, const char *ifname, char *hash)
  53. +{
  54. + struct ahash_request *desc;
  55. + struct crypto_ahash *tfm;
  56. + struct scatterlist sg;
  57. + char vmif[1024];
  58. + int ret;
  59. +
  60. + strcpy (vmif, umid);
  61. + strcat (vmif, ifname);
  62. +
  63. + tfm = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
  64. + if (IS_ERR(tfm))
  65. + return -ENOMEM;
  66. +
  67. + desc = ahash_request_alloc(tfm, GFP_KERNEL);
  68. + if (!desc) {
  69. + ret = -ENOMEM;
  70. + goto out;
  71. + }
  72. +
  73. + crypto_ahash_clear_flags(tfm, ~0);
  74. +
  75. + sg_init_table(&sg, 1);
  76. + sg_set_buf(&sg, vmif, strlen(vmif));
  77. +
  78. + ahash_request_set_crypt(desc, &sg, hash, strlen(vmif));
  79. +
  80. + ret = crypto_ahash_digest(desc);
  81. +out:
  82. + crypto_free_ahash(tfm);
  83. +
  84. + return ret;
  85. +}
  86. +
  87. +#endif
  88. +
  89. void uml_net_setup_etheraddr(struct net_device *dev, char *str)
  90. {
  91. unsigned char *addr = dev->dev_addr;
  92. + u8 hash[SHA1_DIGEST_SIZE];
  93. char *end;
  94. int i;
  95. @@ -331,9 +381,26 @@ void uml_net_setup_etheraddr(struct net_
  96. return;
  97. random:
  98. +#ifndef CONFIG_UML_NET_DETERMINISTIC_MAC
  99. printk(KERN_INFO
  100. "Choosing a random ethernet address for device %s\n", dev->name);
  101. eth_hw_addr_random(dev);
  102. +#else
  103. + printk(KERN_INFO
  104. + "Computing a digest to use as ethernet address for device %s\n", dev->name);
  105. + if (compute_hash(get_umid(), dev->name, hash) < 0) {
  106. + printk(KERN_WARNING
  107. + "Could not compute digest to use as ethernet address for device %s. "
  108. + "Using random address instead.\n", dev->name);
  109. + random_ether_addr(addr);
  110. + }
  111. + else {
  112. + for (i=0; i < 6; i++)
  113. + addr[i] = (hash[i] + hash[i+6]) % 0x100;
  114. + }
  115. + addr [0] &= 0xfe; /* clear multicast bit */
  116. + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
  117. +#endif
  118. }
  119. static DEFINE_SPINLOCK(devices_lock);