102-pseudo-random-mac.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. @@ -143,6 +143,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 (obsolete)"
  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/sha1.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. @@ -274,9 +282,55 @@ static const struct ethtool_ops uml_net_
  46. .get_ts_info = ethtool_op_get_ts_info,
  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 = NULL;
  55. + struct crypto_ahash *tfm = NULL;
  56. + struct scatterlist sg;
  57. + char *vmif = NULL;
  58. + int ret = -ENOMEM;
  59. +
  60. + vmif = kmalloc(1024, GFP_KERNEL);
  61. + if (!vmif)
  62. + goto out;
  63. +
  64. + strcpy (vmif, umid);
  65. + strcat (vmif, ifname);
  66. +
  67. + tfm = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
  68. + if (IS_ERR(tfm))
  69. + goto out;
  70. +
  71. + desc = ahash_request_alloc(tfm, GFP_KERNEL);
  72. + if (!desc)
  73. + goto out;
  74. +
  75. + crypto_ahash_clear_flags(tfm, ~0);
  76. +
  77. + sg_init_table(&sg, 1);
  78. + sg_set_buf(&sg, vmif, strlen(vmif));
  79. +
  80. + ahash_request_set_crypt(desc, &sg, hash, strlen(vmif));
  81. +
  82. + ret = crypto_ahash_digest(desc);
  83. +out:
  84. + crypto_free_ahash(tfm);
  85. + ahash_request_free(desc);
  86. + kfree(vmif);
  87. +
  88. + return ret;
  89. +}
  90. +
  91. +#endif
  92. +
  93. void uml_net_setup_etheraddr(struct net_device *dev, char *str)
  94. {
  95. u8 addr[ETH_ALEN];
  96. + u8 hash[SHA1_DIGEST_SIZE];
  97. char *end;
  98. int i;
  99. @@ -320,9 +374,26 @@ void uml_net_setup_etheraddr(struct net_
  100. return;
  101. random:
  102. +#ifndef CONFIG_UML_NET_DETERMINISTIC_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) < 0) {
  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. + eth_random_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. static DEFINE_SPINLOCK(devices_lock);
  124. --- a/kernel/umh.c
  125. +++ b/kernel/umh.c
  126. @@ -357,12 +357,12 @@ static void helper_unlock(void)
  127. }
  128. int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
  129. - struct file **filp)
  130. + struct file **filp)
  131. {
  132. struct file *f[2];
  133. if (create_pipe_files(f, 0) < 0)
  134. - return PTR_ERR(f);
  135. + return PTR_ERR(f);
  136. sub_info->stdout = f[1];
  137. *filp = f[0];