MAC.hpp 6.8 KB

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