MAC.hpp 7.3 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: 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 "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. {
  55. 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];
  56. }
  57. /**
  58. * @param buf Destination buffer for MAC in big-endian byte order
  59. * @param len Length of buffer, must be >= 6 or nothing is copied
  60. */
  61. ZT_INLINE void copyTo(uint8_t b[6]) const noexcept
  62. {
  63. b[0] = (uint8_t)(m_mac >> 40U);
  64. b[1] = (uint8_t)(m_mac >> 32U);
  65. b[2] = (uint8_t)(m_mac >> 24U);
  66. b[3] = (uint8_t)(m_mac >> 16U);
  67. b[4] = (uint8_t)(m_mac >> 8U);
  68. b[5] = (uint8_t)m_mac;
  69. }
  70. /**
  71. * @return True if this is broadcast (all 0xff)
  72. */
  73. ZT_INLINE bool isBroadcast() const noexcept
  74. { return m_mac; }
  75. /**
  76. * @return True if this is a multicast MAC
  77. */
  78. ZT_INLINE bool isMulticast() const noexcept
  79. { return ((m_mac & 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_INLINE void fromAddress(const Address &ztaddr, uint64_t nwid) noexcept
  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_mac = 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_INLINE Address toAddress(uint64_t nwid) const noexcept
  105. {
  106. uint64_t a = m_mac & 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_INLINE unsigned char firstOctetForNetwork(uint64_t nwid) noexcept
  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_INLINE uint8_t operator[](unsigned int i) const noexcept
  128. { return (uint8_t)(m_mac >> (unsigned int)(40 - (i * 8))); }
  129. /**
  130. * @return 6, which is the number of bytes in a MAC, for container compliance
  131. */
  132. ZT_INLINE unsigned int size() const noexcept
  133. { return 6; }
  134. ZT_INLINE unsigned long hashCode() const noexcept
  135. { return (unsigned long)Utils::hash64(m_mac); }
  136. ZT_INLINE operator bool() const noexcept
  137. { return (m_mac != 0ULL); }
  138. ZT_INLINE operator uint64_t() const noexcept
  139. { return m_mac; }
  140. /**
  141. * Convert this MAC to a standard format colon-separated hex string
  142. *
  143. * @param buf Buffer to store string
  144. * @return Pointer to buf
  145. */
  146. ZT_INLINE char *toString(char buf[18]) const noexcept
  147. {
  148. buf[0] = Utils::HEXCHARS[(m_mac >> 44U) & 0xfU];
  149. buf[1] = Utils::HEXCHARS[(m_mac >> 40U) & 0xfU];
  150. buf[2] = ':';
  151. buf[3] = Utils::HEXCHARS[(m_mac >> 36U) & 0xfU];
  152. buf[4] = Utils::HEXCHARS[(m_mac >> 32U) & 0xfU];
  153. buf[5] = ':';
  154. buf[6] = Utils::HEXCHARS[(m_mac >> 28U) & 0xfU];
  155. buf[7] = Utils::HEXCHARS[(m_mac >> 24U) & 0xfU];
  156. buf[8] = ':';
  157. buf[9] = Utils::HEXCHARS[(m_mac >> 20U) & 0xfU];
  158. buf[10] = Utils::HEXCHARS[(m_mac >> 16U) & 0xfU];
  159. buf[11] = ':';
  160. buf[12] = Utils::HEXCHARS[(m_mac >> 12U) & 0xfU];
  161. buf[13] = Utils::HEXCHARS[(m_mac >> 8U) & 0xfU];
  162. buf[14] = ':';
  163. buf[15] = Utils::HEXCHARS[(m_mac >> 4U) & 0xfU];
  164. buf[16] = Utils::HEXCHARS[m_mac & 0xfU];
  165. buf[17] = (char)0;
  166. return buf;
  167. }
  168. ZT_INLINE String toString() const
  169. {
  170. char tmp[18];
  171. return String(toString(tmp));
  172. }
  173. /**
  174. * Parse a MAC address in hex format with or without : separators and ignoring non-hex characters.
  175. *
  176. * @param s String to parse
  177. */
  178. ZT_INLINE void fromString(const char *s) noexcept
  179. {
  180. m_mac = 0;
  181. if (s) {
  182. while (*s) {
  183. uint64_t c;
  184. const char hc = *s++;
  185. if ((hc >= 48) && (hc <= 57))
  186. c = (uint64_t)hc - 48;
  187. else if ((hc >= 97) && (hc <= 102))
  188. c = (uint64_t)hc - 87;
  189. else if ((hc >= 65) && (hc <= 70))
  190. c = (uint64_t)hc - 55;
  191. else continue;
  192. m_mac = (m_mac << 4U) | c;
  193. }
  194. m_mac &= 0xffffffffffffULL;
  195. }
  196. }
  197. ZT_INLINE MAC &operator=(const uint64_t m) noexcept
  198. {
  199. m_mac = m;
  200. return *this;
  201. }
  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 MAC &m) const noexcept
  213. { return (m_mac >= m.m_mac); }
  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. ZT_INLINE bool operator>=(const uint64_t m) const noexcept
  225. { return (m_mac >= m); }
  226. private:
  227. uint64_t m_mac;
  228. };
  229. } // namespace ZeroTier
  230. #endif