MAC.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: 2025-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 "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "Address.hpp"
  18. #include "TriviallyCopyable.hpp"
  19. #include "Containers.hpp"
  20. namespace ZeroTier {
  21. /**
  22. * 48-byte Ethernet MAC address
  23. */
  24. class MAC : public TriviallyCopyable
  25. {
  26. public:
  27. ZT_INLINE MAC() noexcept: m_mac(0ULL)
  28. {}
  29. 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:
  30. m_mac((((uint64_t)a) << 40U) | (((uint64_t)b) << 32U) | (((uint64_t)c) << 24U) | (((uint64_t)d) << 16U) | (((uint64_t)e) << 8U) | ((uint64_t)f))
  31. {}
  32. explicit ZT_INLINE MAC(const uint64_t m) noexcept:
  33. m_mac(m)
  34. {}
  35. explicit ZT_INLINE MAC(const uint8_t b[6]) noexcept
  36. { setTo(b); }
  37. ZT_INLINE MAC(const Address &ztaddr, const uint64_t nwid) noexcept
  38. { fromAddress(ztaddr, nwid); }
  39. /**
  40. * @return MAC in 64-bit integer
  41. */
  42. ZT_INLINE uint64_t toInt() const noexcept
  43. { return m_mac; }
  44. /**
  45. * Set MAC to zero
  46. */
  47. ZT_INLINE void zero() noexcept
  48. { m_mac = 0ULL; }
  49. /**
  50. * @param bits Raw MAC in big-endian byte order
  51. * @param len Length, must be >= 6 or result is zero
  52. */
  53. ZT_INLINE void setTo(const uint8_t b[6]) noexcept
  54. { m_mac = ((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]; }
  55. /**
  56. * @param buf Destination buffer for MAC in big-endian byte order
  57. * @param len Length of buffer, must be >= 6 or nothing is copied
  58. */
  59. ZT_INLINE void copyTo(uint8_t b[6]) const noexcept
  60. {
  61. b[0] = (uint8_t)(m_mac >> 40U);
  62. b[1] = (uint8_t)(m_mac >> 32U);
  63. b[2] = (uint8_t)(m_mac >> 24U);
  64. b[3] = (uint8_t)(m_mac >> 16U);
  65. b[4] = (uint8_t)(m_mac >> 8U);
  66. b[5] = (uint8_t)m_mac;
  67. }
  68. /**
  69. * @return True if this is broadcast (all 0xff)
  70. */
  71. ZT_INLINE bool isBroadcast() const noexcept
  72. { return m_mac; }
  73. /**
  74. * @return True if this is a multicast MAC
  75. */
  76. ZT_INLINE bool isMulticast() const noexcept
  77. { return ((m_mac & 0x010000000000ULL) != 0ULL); }
  78. /**
  79. * Set this MAC to a MAC derived from an address and a network ID
  80. *
  81. * @param ztaddr ZeroTier address
  82. * @param nwid 64-bit network ID
  83. */
  84. ZT_INLINE void fromAddress(const Address &ztaddr, uint64_t nwid) noexcept
  85. {
  86. uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40U;
  87. m |= ztaddr.toInt(); // a is 40 bits
  88. m ^= ((nwid >> 8U) & 0xffU) << 32U;
  89. m ^= ((nwid >> 16U) & 0xffU) << 24U;
  90. m ^= ((nwid >> 24U) & 0xffU) << 16U;
  91. m ^= ((nwid >> 32U) & 0xffU) << 8U;
  92. m ^= (nwid >> 40U) & 0xffU;
  93. m_mac = m;
  94. }
  95. /**
  96. * Get the ZeroTier address for this MAC on this network (assuming no bridging of course, basic unicast)
  97. *
  98. * This just XORs the next-lest-significant 5 bytes of the network ID again to unmask.
  99. *
  100. * @param nwid Network ID
  101. */
  102. ZT_INLINE Address toAddress(uint64_t nwid) const noexcept
  103. {
  104. uint64_t a = m_mac & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
  105. a ^= ((nwid >> 8U) & 0xffU) << 32U; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
  106. a ^= ((nwid >> 16U) & 0xffU) << 24U;
  107. a ^= ((nwid >> 24U) & 0xffU) << 16U;
  108. a ^= ((nwid >> 32U) & 0xffU) << 8U;
  109. a ^= (nwid >> 40U) & 0xffU;
  110. return Address(a);
  111. }
  112. /**
  113. * @param nwid Network ID
  114. * @return First octet of MAC for this network
  115. */
  116. static ZT_INLINE unsigned char firstOctetForNetwork(uint64_t nwid) noexcept
  117. {
  118. const uint8_t a = ((uint8_t)(nwid & 0xfeU) | 0x02U); // locally administered, not multicast, from LSB of network ID
  119. 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
  120. }
  121. /**
  122. * @param i Value from 0 to 5 (inclusive)
  123. * @return Byte at said position (address interpreted in big-endian order)
  124. */
  125. ZT_INLINE uint8_t operator[](unsigned int i) const noexcept
  126. { return (uint8_t)(m_mac >> (unsigned int)(40 - (i * 8))); }
  127. /**
  128. * @return 6, which is the number of bytes in a MAC, for container compliance
  129. */
  130. ZT_INLINE unsigned int size() const noexcept
  131. { return 6; }
  132. ZT_INLINE unsigned long hashCode() const noexcept
  133. { return (unsigned long)Utils::hash64(m_mac); }
  134. ZT_INLINE operator bool() const noexcept
  135. { return (m_mac != 0ULL); }
  136. ZT_INLINE operator uint64_t() const noexcept
  137. { return m_mac; }
  138. /**
  139. * Convert this MAC to a standard format colon-separated hex string
  140. *
  141. * @param buf Buffer to store string
  142. * @return Pointer to buf
  143. */
  144. ZT_INLINE char *toString(char buf[18]) const noexcept
  145. {
  146. buf[0] = Utils::HEXCHARS[(m_mac >> 44U) & 0xfU];
  147. buf[1] = Utils::HEXCHARS[(m_mac >> 40U) & 0xfU];
  148. buf[2] = ':';
  149. buf[3] = Utils::HEXCHARS[(m_mac >> 36U) & 0xfU];
  150. buf[4] = Utils::HEXCHARS[(m_mac >> 32U) & 0xfU];
  151. buf[5] = ':';
  152. buf[6] = Utils::HEXCHARS[(m_mac >> 28U) & 0xfU];
  153. buf[7] = Utils::HEXCHARS[(m_mac >> 24U) & 0xfU];
  154. buf[8] = ':';
  155. buf[9] = Utils::HEXCHARS[(m_mac >> 20U) & 0xfU];
  156. buf[10] = Utils::HEXCHARS[(m_mac >> 16U) & 0xfU];
  157. buf[11] = ':';
  158. buf[12] = Utils::HEXCHARS[(m_mac >> 12U) & 0xfU];
  159. buf[13] = Utils::HEXCHARS[(m_mac >> 8U) & 0xfU];
  160. buf[14] = ':';
  161. buf[15] = Utils::HEXCHARS[(m_mac >> 4U) & 0xfU];
  162. buf[16] = Utils::HEXCHARS[m_mac & 0xfU];
  163. buf[17] = (char)0;
  164. return buf;
  165. }
  166. ZT_INLINE String toString() const
  167. {
  168. char tmp[18];
  169. return String(toString(tmp));
  170. }
  171. /**
  172. * Parse a MAC address in hex format with or without : separators and ignoring non-hex characters.
  173. *
  174. * @param s String to parse
  175. */
  176. ZT_INLINE void fromString(const char *s) noexcept
  177. {
  178. m_mac = 0;
  179. if (s) {
  180. while (*s) {
  181. uint64_t c;
  182. const char hc = *s++;
  183. if ((hc >= 48) && (hc <= 57))
  184. c = (uint64_t)hc - 48;
  185. else if ((hc >= 97) && (hc <= 102))
  186. c = (uint64_t)hc - 87;
  187. else if ((hc >= 65) && (hc <= 70))
  188. c = (uint64_t)hc - 55;
  189. else continue;
  190. m_mac = (m_mac << 4U) | c;
  191. }
  192. m_mac &= 0xffffffffffffULL;
  193. }
  194. }
  195. ZT_INLINE MAC &operator=(const uint64_t m) noexcept
  196. {
  197. m_mac = m;
  198. return *this;
  199. }
  200. ZT_INLINE bool operator==(const MAC &m) const noexcept
  201. { return (m_mac == m.m_mac); }
  202. ZT_INLINE bool operator!=(const MAC &m) const noexcept
  203. { return (m_mac != m.m_mac); }
  204. ZT_INLINE bool operator<(const MAC &m) const noexcept
  205. { return (m_mac < m.m_mac); }
  206. ZT_INLINE bool operator<=(const MAC &m) const noexcept
  207. { return (m_mac <= m.m_mac); }
  208. ZT_INLINE bool operator>(const MAC &m) const noexcept
  209. { return (m_mac > m.m_mac); }
  210. ZT_INLINE bool operator>=(const MAC &m) const noexcept
  211. { return (m_mac >= m.m_mac); }
  212. ZT_INLINE bool operator==(const uint64_t m) const noexcept
  213. { return (m_mac == m); }
  214. ZT_INLINE bool operator!=(const uint64_t m) const noexcept
  215. { return (m_mac != m); }
  216. ZT_INLINE bool operator<(const uint64_t m) const noexcept
  217. { return (m_mac < m); }
  218. ZT_INLINE bool operator<=(const uint64_t m) const noexcept
  219. { return (m_mac <= m); }
  220. ZT_INLINE bool operator>(const uint64_t m) const noexcept
  221. { return (m_mac > m); }
  222. ZT_INLINE bool operator>=(const uint64_t m) const noexcept
  223. { return (m_mac >= m); }
  224. private:
  225. uint64_t m_mac;
  226. };
  227. static_assert(sizeof(MAC) == sizeof(uint64_t),"MAC contains unnecessary padding");
  228. } // namespace ZeroTier
  229. #endif