MAC.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "TriviallyCopyable.hpp"
  22. namespace ZeroTier {
  23. /**
  24. * 48-byte Ethernet MAC address
  25. */
  26. class MAC : public TriviallyCopyable
  27. {
  28. public:
  29. ZT_INLINE MAC() noexcept : _m(0ULL) {}
  30. ZT_INLINE MAC(const uint8_t a,const uint8_t b,const uint8_t c,const uint8_t d,const uint8_t e,const uint8_t f) noexcept : _m((((uint64_t)a) << 40U) | (((uint64_t)b) << 32U) | (((uint64_t)c) << 24U) | (((uint64_t)d) << 16U) | (((uint64_t)e) << 8U) | ((uint64_t)f) ) {}
  31. explicit ZT_INLINE MAC(const uint64_t m) noexcept : _m(m) {}
  32. explicit ZT_INLINE MAC(const uint8_t b[6]) noexcept { setTo(b); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  33. ZT_INLINE MAC(const Address &ztaddr,const uint64_t nwid) noexcept { fromAddress(ztaddr,nwid); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  34. /**
  35. * @return MAC in 64-bit integer
  36. */
  37. ZT_INLINE uint64_t toInt() const noexcept { return _m; }
  38. /**
  39. * Set MAC to zero
  40. */
  41. ZT_INLINE void zero() noexcept { _m = 0ULL; }
  42. /**
  43. * @return True if MAC is non-zero
  44. */
  45. ZT_INLINE operator bool() const noexcept { return (_m != 0ULL); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
  46. /**
  47. * @param bits Raw MAC in big-endian byte order
  48. * @param len Length, must be >= 6 or result is zero
  49. */
  50. ZT_INLINE void setTo(const uint8_t b[6]) noexcept
  51. {
  52. _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];
  53. }
  54. /**
  55. * @param buf Destination buffer for MAC in big-endian byte order
  56. * @param len Length of buffer, must be >= 6 or nothing is copied
  57. */
  58. ZT_INLINE void copyTo(uint8_t b[6]) const noexcept
  59. {
  60. b[0] = (uint8_t)(_m >> 40U);
  61. b[1] = (uint8_t)(_m >> 32U);
  62. b[2] = (uint8_t)(_m >> 24U);
  63. b[3] = (uint8_t)(_m >> 16U);
  64. b[4] = (uint8_t)(_m >> 8U);
  65. b[5] = (uint8_t)_m;
  66. }
  67. /**
  68. * @return True if this is broadcast (all 0xff)
  69. */
  70. ZT_INLINE bool isBroadcast() const noexcept { return _m; }
  71. /**
  72. * @return True if this is a multicast MAC
  73. */
  74. ZT_INLINE bool isMulticast() const noexcept { return ((_m & 0x010000000000ULL) != 0ULL); }
  75. /**
  76. * Set this MAC to a MAC derived from an address and a network ID
  77. *
  78. * @param ztaddr ZeroTier address
  79. * @param nwid 64-bit network ID
  80. */
  81. ZT_INLINE void fromAddress(const Address &ztaddr,uint64_t nwid) noexcept
  82. {
  83. uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40U;
  84. m |= ztaddr.toInt(); // a is 40 bits
  85. m ^= ((nwid >> 8U) & 0xffU) << 32U;
  86. m ^= ((nwid >> 16U) & 0xffU) << 24U;
  87. m ^= ((nwid >> 24U) & 0xffU) << 16U;
  88. m ^= ((nwid >> 32U) & 0xffU) << 8U;
  89. m ^= (nwid >> 40U) & 0xffU;
  90. _m = m;
  91. }
  92. /**
  93. * Get the ZeroTier address for this MAC on this network (assuming no bridging of course, basic unicast)
  94. *
  95. * This just XORs the next-lest-significant 5 bytes of the network ID again to unmask.
  96. *
  97. * @param nwid Network ID
  98. */
  99. ZT_INLINE Address toAddress(uint64_t nwid) const noexcept
  100. {
  101. uint64_t a = _m & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
  102. a ^= ((nwid >> 8U) & 0xffU) << 32U; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
  103. a ^= ((nwid >> 16U) & 0xffU) << 24U;
  104. a ^= ((nwid >> 24U) & 0xffU) << 16U;
  105. a ^= ((nwid >> 32U) & 0xffU) << 8U;
  106. a ^= (nwid >> 40U) & 0xffU;
  107. return Address(a);
  108. }
  109. /**
  110. * @param nwid Network ID
  111. * @return First octet of MAC for this network
  112. */
  113. static ZT_INLINE unsigned char firstOctetForNetwork(uint64_t nwid) noexcept
  114. {
  115. const uint8_t a = ((uint8_t)(nwid & 0xfeU) | 0x02U); // locally administered, not multicast, from LSB of network ID
  116. 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
  117. }
  118. /**
  119. * @param i Value from 0 to 5 (inclusive)
  120. * @return Byte at said position (address interpreted in big-endian order)
  121. */
  122. ZT_INLINE uint8_t operator[](unsigned int i) const noexcept { return (uint8_t)(_m >> (40 - (i * 8))); }
  123. /**
  124. * @return 6, which is the number of bytes in a MAC, for container compliance
  125. */
  126. ZT_INLINE unsigned int size() const noexcept { return 6; }
  127. ZT_INLINE unsigned long hashCode() const noexcept { return (unsigned long)Utils::hash64(_m); }
  128. ZT_INLINE char *toString(char buf[18]) const noexcept
  129. {
  130. buf[0] = Utils::HEXCHARS[(_m >> 44U) & 0xfU];
  131. buf[1] = Utils::HEXCHARS[(_m >> 40U) & 0xfU];
  132. buf[2] = ':';
  133. buf[3] = Utils::HEXCHARS[(_m >> 36U) & 0xfU];
  134. buf[4] = Utils::HEXCHARS[(_m >> 32U) & 0xfU];
  135. buf[5] = ':';
  136. buf[6] = Utils::HEXCHARS[(_m >> 28U) & 0xfU];
  137. buf[7] = Utils::HEXCHARS[(_m >> 24U) & 0xfU];
  138. buf[8] = ':';
  139. buf[9] = Utils::HEXCHARS[(_m >> 20U) & 0xfU];
  140. buf[10] = Utils::HEXCHARS[(_m >> 16U) & 0xfU];
  141. buf[11] = ':';
  142. buf[12] = Utils::HEXCHARS[(_m >> 12U) & 0xfU];
  143. buf[13] = Utils::HEXCHARS[(_m >> 8U) & 0xfU];
  144. buf[14] = ':';
  145. buf[15] = Utils::HEXCHARS[(_m >> 4U) & 0xfU];
  146. buf[16] = Utils::HEXCHARS[_m & 0xfU];
  147. buf[17] = (char)0;
  148. return buf;
  149. }
  150. ZT_INLINE MAC &operator=(const uint64_t m) noexcept { _m = m; return *this; }
  151. ZT_INLINE bool operator==(const MAC &m) const noexcept { return (_m == m._m); }
  152. ZT_INLINE bool operator!=(const MAC &m) const noexcept { return (_m != m._m); }
  153. ZT_INLINE bool operator<(const MAC &m) const noexcept { return (_m < m._m); }
  154. ZT_INLINE bool operator<=(const MAC &m) const noexcept { return (_m <= m._m); }
  155. ZT_INLINE bool operator>(const MAC &m) const noexcept { return (_m > m._m); }
  156. ZT_INLINE bool operator>=(const MAC &m) const noexcept { return (_m >= m._m); }
  157. private:
  158. uint64_t _m;
  159. };
  160. } // namespace ZeroTier
  161. #endif