MAC.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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 :
  29. 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) )
  30. {}
  31. explicit ZT_INLINE MAC(const uint64_t m) noexcept :
  32. m_mac(m)
  33. {}
  34. explicit ZT_INLINE MAC(const uint8_t b[6]) noexcept
  35. { setTo(b); }
  36. ZT_INLINE MAC(const Address &ztaddr,const uint64_t nwid) noexcept
  37. { fromAddress(ztaddr,nwid); }
  38. /**
  39. * @return MAC in 64-bit integer
  40. */
  41. ZT_INLINE uint64_t toInt() const noexcept { return m_mac; }
  42. /**
  43. * Set MAC to zero
  44. */
  45. ZT_INLINE void zero() noexcept { m_mac = 0ULL; }
  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_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];
  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_mac >> 40U);
  61. b[1] = (uint8_t)(m_mac >> 32U);
  62. b[2] = (uint8_t)(m_mac >> 24U);
  63. b[3] = (uint8_t)(m_mac >> 16U);
  64. b[4] = (uint8_t)(m_mac >> 8U);
  65. b[5] = (uint8_t)m_mac;
  66. }
  67. /**
  68. * @return True if this is broadcast (all 0xff)
  69. */
  70. ZT_INLINE bool isBroadcast() const noexcept { return m_mac; }
  71. /**
  72. * @return True if this is a multicast MAC
  73. */
  74. ZT_INLINE bool isMulticast() const noexcept { return ((m_mac & 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_mac = 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_mac & 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_mac >> (unsigned int)(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_mac); }
  128. ZT_INLINE operator bool() const noexcept { return (m_mac != 0ULL); }
  129. ZT_INLINE operator uint64_t() const noexcept { return m_mac; }
  130. /**
  131. * Convert this MAC to a standard format colon-separated hex string
  132. *
  133. * @param buf Buffer to store string
  134. * @return Pointer to buf
  135. */
  136. ZT_INLINE char *toString(char buf[18]) const noexcept
  137. {
  138. buf[0] = Utils::HEXCHARS[(m_mac >> 44U) & 0xfU];
  139. buf[1] = Utils::HEXCHARS[(m_mac >> 40U) & 0xfU];
  140. buf[2] = ':';
  141. buf[3] = Utils::HEXCHARS[(m_mac >> 36U) & 0xfU];
  142. buf[4] = Utils::HEXCHARS[(m_mac >> 32U) & 0xfU];
  143. buf[5] = ':';
  144. buf[6] = Utils::HEXCHARS[(m_mac >> 28U) & 0xfU];
  145. buf[7] = Utils::HEXCHARS[(m_mac >> 24U) & 0xfU];
  146. buf[8] = ':';
  147. buf[9] = Utils::HEXCHARS[(m_mac >> 20U) & 0xfU];
  148. buf[10] = Utils::HEXCHARS[(m_mac >> 16U) & 0xfU];
  149. buf[11] = ':';
  150. buf[12] = Utils::HEXCHARS[(m_mac >> 12U) & 0xfU];
  151. buf[13] = Utils::HEXCHARS[(m_mac >> 8U) & 0xfU];
  152. buf[14] = ':';
  153. buf[15] = Utils::HEXCHARS[(m_mac >> 4U) & 0xfU];
  154. buf[16] = Utils::HEXCHARS[m_mac & 0xfU];
  155. buf[17] = (char)0;
  156. return buf;
  157. }
  158. ZT_INLINE String toString() const { char tmp[18]; return String(toString(tmp)); }
  159. /**
  160. * Parse a MAC address in hex format with or without : separators and ignoring non-hex characters.
  161. *
  162. * @param s String to parse
  163. */
  164. ZT_INLINE void fromString(const char *s) noexcept
  165. {
  166. m_mac = 0;
  167. if (s) {
  168. while (*s) {
  169. uint64_t c;
  170. const char hc = *s++;
  171. if ((hc >= 48)&&(hc <= 57))
  172. c = (uint64_t)hc - 48;
  173. else if ((hc >= 97)&&(hc <= 102))
  174. c = (uint64_t)hc - 87;
  175. else if ((hc >= 65)&&(hc <= 70))
  176. c = (uint64_t)hc - 55;
  177. else continue;
  178. m_mac = (m_mac << 4U) | c;
  179. }
  180. m_mac &= 0xffffffffffffULL;
  181. }
  182. }
  183. ZT_INLINE MAC &operator=(const uint64_t m) noexcept { m_mac = m; return *this; }
  184. ZT_INLINE bool operator==(const MAC &m) const noexcept { return (m_mac == m.m_mac); }
  185. ZT_INLINE bool operator!=(const MAC &m) const noexcept { return (m_mac != m.m_mac); }
  186. ZT_INLINE bool operator<(const MAC &m) const noexcept { return (m_mac < m.m_mac); }
  187. ZT_INLINE bool operator<=(const MAC &m) const noexcept { return (m_mac <= m.m_mac); }
  188. ZT_INLINE bool operator>(const MAC &m) const noexcept { return (m_mac > m.m_mac); }
  189. ZT_INLINE bool operator>=(const MAC &m) const noexcept { return (m_mac >= m.m_mac); }
  190. ZT_INLINE bool operator==(const uint64_t m) const noexcept { return (m_mac == m); }
  191. ZT_INLINE bool operator!=(const uint64_t m) const noexcept { return (m_mac != m); }
  192. ZT_INLINE bool operator<(const uint64_t m) const noexcept { return (m_mac < m); }
  193. ZT_INLINE bool operator<=(const uint64_t m) const noexcept { return (m_mac <= m); }
  194. ZT_INLINE bool operator>(const uint64_t m) const noexcept { return (m_mac > m); }
  195. ZT_INLINE bool operator>=(const uint64_t m) const noexcept { return (m_mac >= m); }
  196. private:
  197. uint64_t m_mac;
  198. };
  199. } // namespace ZeroTier
  200. #endif