MAC.hpp 6.8 KB

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