MAC.hpp 6.7 KB

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