MAC.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MAC_HPP
  14. #define ZT_MAC_HPP
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstdint>
  18. #include "Constants.hpp"
  19. #include "Utils.hpp"
  20. #include "Address.hpp"
  21. namespace ZeroTier {
  22. /**
  23. * 48-byte Ethernet MAC address
  24. */
  25. class MAC
  26. {
  27. public:
  28. ZT_ALWAYS_INLINE MAC() : _m(0ULL) {}
  29. ZT_ALWAYS_INLINE MAC(const unsigned char a,const unsigned char b,const unsigned char c,const unsigned char d,const unsigned char e,const unsigned char f) :
  30. _m( ((((uint64_t)a) & 0xffULL) << 40U) |
  31. ((((uint64_t)b) & 0xffULL) << 32U) |
  32. ((((uint64_t)c) & 0xffULL) << 24U) |
  33. ((((uint64_t)d) & 0xffULL) << 16U) |
  34. ((((uint64_t)e) & 0xffULL) << 8U) |
  35. (((uint64_t)f) & 0xffULL) ) {}
  36. explicit ZT_ALWAYS_INLINE MAC(const uint8_t b[6]) { setTo(b); }
  37. ZT_ALWAYS_INLINE MAC(const Address &ztaddr,uint64_t nwid) { fromAddress(ztaddr,nwid); }
  38. explicit ZT_ALWAYS_INLINE MAC(const uint64_t m) : _m(m & 0xffffffffffffULL) {}
  39. /**
  40. * @return MAC in 64-bit integer
  41. */
  42. ZT_ALWAYS_INLINE uint64_t toInt() const { return _m; }
  43. /**
  44. * Set MAC to zero
  45. */
  46. ZT_ALWAYS_INLINE void zero() { _m = 0ULL; }
  47. /**
  48. * @return True if MAC is non-zero
  49. */
  50. ZT_ALWAYS_INLINE operator bool() const { return (_m != 0ULL); }
  51. /**
  52. * @param bits Raw MAC in big-endian byte order
  53. * @param len Length, must be >= 6 or result is zero
  54. */
  55. ZT_ALWAYS_INLINE void setTo(const uint8_t b[6])
  56. {
  57. _m = ((uint64_t)b[0] << 40U) | ((uint64_t)b[1] << 32U) | ((uint64_t)b[2] << 24U) | ((uint64_t)b[3] << 16U) | ((uint64_t)b[4] << 8U) | (uint64_t)b[5];
  58. }
  59. /**
  60. * @param buf Destination buffer for MAC in big-endian byte order
  61. * @param len Length of buffer, must be >= 6 or nothing is copied
  62. */
  63. ZT_ALWAYS_INLINE void copyTo(uint8_t b[6]) const
  64. {
  65. b[0] = (uint8_t)(_m >> 40U);
  66. b[1] = (uint8_t)(_m >> 32U);
  67. b[2] = (uint8_t)(_m >> 24U);
  68. b[3] = (uint8_t)(_m >> 16U);
  69. b[4] = (uint8_t)(_m >> 8U);
  70. b[5] = (uint8_t)_m;
  71. }
  72. /**
  73. * @return True if this is broadcast (all 0xff)
  74. */
  75. ZT_ALWAYS_INLINE bool isBroadcast() const { return (_m == 0xffffffffffffULL); }
  76. /**
  77. * @return True if this is a multicast MAC
  78. */
  79. ZT_ALWAYS_INLINE bool isMulticast() const { return ((_m & 0x010000000000ULL) != 0ULL); }
  80. /**
  81. * Set this MAC to a MAC derived from an address and a network ID
  82. *
  83. * @param ztaddr ZeroTier address
  84. * @param nwid 64-bit network ID
  85. */
  86. ZT_ALWAYS_INLINE void fromAddress(const Address &ztaddr,uint64_t nwid)
  87. {
  88. uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40U;
  89. m |= ztaddr.toInt(); // a is 40 bits
  90. m ^= ((nwid >> 8U) & 0xffU) << 32U;
  91. m ^= ((nwid >> 16U) & 0xffU) << 24U;
  92. m ^= ((nwid >> 24U) & 0xffU) << 16U;
  93. m ^= ((nwid >> 32U) & 0xffU) << 8U;
  94. m ^= (nwid >> 40U) & 0xffU;
  95. _m = m;
  96. }
  97. /**
  98. * Get the ZeroTier address for this MAC on this network (assuming no bridging of course, basic unicast)
  99. *
  100. * This just XORs the next-lest-significant 5 bytes of the network ID again to unmask.
  101. *
  102. * @param nwid Network ID
  103. */
  104. ZT_ALWAYS_INLINE Address toAddress(uint64_t nwid) const
  105. {
  106. uint64_t a = _m & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
  107. a ^= ((nwid >> 8U) & 0xffU) << 32U; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
  108. a ^= ((nwid >> 16U) & 0xffU) << 24U;
  109. a ^= ((nwid >> 24U) & 0xffU) << 16U;
  110. a ^= ((nwid >> 32U) & 0xffU) << 8U;
  111. a ^= (nwid >> 40U) & 0xffU;
  112. return Address(a);
  113. }
  114. /**
  115. * @param nwid Network ID
  116. * @return First octet of MAC for this network
  117. */
  118. static ZT_ALWAYS_INLINE unsigned char firstOctetForNetwork(uint64_t nwid)
  119. {
  120. const uint8_t a = ((uint8_t)(nwid & 0xfeU) | 0x02U); // locally administered, not multicast, from LSB of network ID
  121. return ((a == 0x52) ? 0x32 : a); // blacklist 0x52 since it's used by KVM, libvirt, and other popular virtualization engines... seems de-facto standard on Linux
  122. }
  123. /**
  124. * @param i Value from 0 to 5 (inclusive)
  125. * @return Byte at said position (address interpreted in big-endian order)
  126. */
  127. ZT_ALWAYS_INLINE uint8_t operator[](unsigned int i) const { return (uint8_t)(_m >> (40 - (i * 8))); }
  128. /**
  129. * @return 6, which is the number of bytes in a MAC, for container compliance
  130. */
  131. ZT_ALWAYS_INLINE unsigned int size() const { return 6; }
  132. ZT_ALWAYS_INLINE unsigned long hashCode() const { return (unsigned long)_m; }
  133. ZT_ALWAYS_INLINE char *toString(char buf[18]) const
  134. {
  135. buf[0] = Utils::HEXCHARS[(_m >> 44U) & 0xfU];
  136. buf[1] = Utils::HEXCHARS[(_m >> 40U) & 0xfU];
  137. buf[2] = ':';
  138. buf[3] = Utils::HEXCHARS[(_m >> 36U) & 0xfU];
  139. buf[4] = Utils::HEXCHARS[(_m >> 32U) & 0xfU];
  140. buf[5] = ':';
  141. buf[6] = Utils::HEXCHARS[(_m >> 28U) & 0xfU];
  142. buf[7] = Utils::HEXCHARS[(_m >> 24U) & 0xfU];
  143. buf[8] = ':';
  144. buf[9] = Utils::HEXCHARS[(_m >> 20U) & 0xfU];
  145. buf[10] = Utils::HEXCHARS[(_m >> 16U) & 0xfU];
  146. buf[11] = ':';
  147. buf[12] = Utils::HEXCHARS[(_m >> 12U) & 0xfU];
  148. buf[13] = Utils::HEXCHARS[(_m >> 8U) & 0xfU];
  149. buf[14] = ':';
  150. buf[15] = Utils::HEXCHARS[(_m >> 4U) & 0xfU];
  151. buf[16] = Utils::HEXCHARS[_m & 0xfU];
  152. buf[17] = (char)0;
  153. return buf;
  154. }
  155. ZT_ALWAYS_INLINE MAC &operator=(const uint64_t m)
  156. {
  157. _m = m & 0xffffffffffffULL;
  158. return *this;
  159. }
  160. ZT_ALWAYS_INLINE bool operator==(const MAC &m) const { return (_m == m._m); }
  161. ZT_ALWAYS_INLINE bool operator!=(const MAC &m) const { return (_m != m._m); }
  162. ZT_ALWAYS_INLINE bool operator<(const MAC &m) const { return (_m < m._m); }
  163. ZT_ALWAYS_INLINE bool operator<=(const MAC &m) const { return (_m <= m._m); }
  164. ZT_ALWAYS_INLINE bool operator>(const MAC &m) const { return (_m > m._m); }
  165. ZT_ALWAYS_INLINE bool operator>=(const MAC &m) const { return (_m >= m._m); }
  166. private:
  167. uint64_t _m;
  168. };
  169. } // namespace ZeroTier
  170. #endif