MAC.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_MAC_HPP
  27. #define ZT_MAC_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include "Constants.hpp"
  32. #include "Utils.hpp"
  33. #include "Address.hpp"
  34. #include "Buffer.hpp"
  35. namespace ZeroTier {
  36. /**
  37. * 48-byte Ethernet MAC address
  38. */
  39. class MAC
  40. {
  41. public:
  42. inline MAC() : _m(0ULL) {}
  43. inline MAC(const MAC &m) : _m(m._m) {}
  44. inline 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) :
  45. _m( ((((uint64_t)a) & 0xffULL) << 40) |
  46. ((((uint64_t)b) & 0xffULL) << 32) |
  47. ((((uint64_t)c) & 0xffULL) << 24) |
  48. ((((uint64_t)d) & 0xffULL) << 16) |
  49. ((((uint64_t)e) & 0xffULL) << 8) |
  50. (((uint64_t)f) & 0xffULL) ) {}
  51. inline MAC(const void *bits,unsigned int len) { setTo(bits,len); }
  52. inline MAC(const Address &ztaddr,uint64_t nwid) { fromAddress(ztaddr,nwid); }
  53. inline MAC(const uint64_t m) : _m(m & 0xffffffffffffULL) {}
  54. /**
  55. * @return MAC in 64-bit integer
  56. */
  57. inline uint64_t toInt() const { return _m; }
  58. /**
  59. * Set MAC to zero
  60. */
  61. inline void zero() { _m = 0ULL; }
  62. /**
  63. * @return True if MAC is non-zero
  64. */
  65. inline operator bool() const { return (_m != 0ULL); }
  66. /**
  67. * @param bits Raw MAC in big-endian byte order
  68. * @param len Length, must be >= 6 or result is zero
  69. */
  70. inline void setTo(const void *bits,unsigned int len)
  71. {
  72. if (len < 6) {
  73. _m = 0ULL;
  74. return;
  75. }
  76. const unsigned char *b = (const unsigned char *)bits;
  77. _m = ((((uint64_t)*b) & 0xff) << 40); ++b;
  78. _m |= ((((uint64_t)*b) & 0xff) << 32); ++b;
  79. _m |= ((((uint64_t)*b) & 0xff) << 24); ++b;
  80. _m |= ((((uint64_t)*b) & 0xff) << 16); ++b;
  81. _m |= ((((uint64_t)*b) & 0xff) << 8); ++b;
  82. _m |= (((uint64_t)*b) & 0xff);
  83. }
  84. /**
  85. * @param buf Destination buffer for MAC in big-endian byte order
  86. * @param len Length of buffer, must be >= 6 or nothing is copied
  87. */
  88. inline void copyTo(void *buf,unsigned int len) const
  89. {
  90. if (len < 6)
  91. return;
  92. unsigned char *b = (unsigned char *)buf;
  93. *(b++) = (unsigned char)((_m >> 40) & 0xff);
  94. *(b++) = (unsigned char)((_m >> 32) & 0xff);
  95. *(b++) = (unsigned char)((_m >> 24) & 0xff);
  96. *(b++) = (unsigned char)((_m >> 16) & 0xff);
  97. *(b++) = (unsigned char)((_m >> 8) & 0xff);
  98. *b = (unsigned char)(_m & 0xff);
  99. }
  100. /**
  101. * Append to a buffer in big-endian byte order
  102. *
  103. * @param b Buffer to append to
  104. */
  105. template<unsigned int C>
  106. inline void appendTo(Buffer<C> &b) const
  107. {
  108. unsigned char *p = (unsigned char *)b.appendField(6);
  109. *(p++) = (unsigned char)((_m >> 40) & 0xff);
  110. *(p++) = (unsigned char)((_m >> 32) & 0xff);
  111. *(p++) = (unsigned char)((_m >> 24) & 0xff);
  112. *(p++) = (unsigned char)((_m >> 16) & 0xff);
  113. *(p++) = (unsigned char)((_m >> 8) & 0xff);
  114. *p = (unsigned char)(_m & 0xff);
  115. }
  116. /**
  117. * @return True if this is broadcast (all 0xff)
  118. */
  119. inline bool isBroadcast() const { return (_m == 0xffffffffffffULL); }
  120. /**
  121. * @return True if this is a multicast MAC
  122. */
  123. inline bool isMulticast() const { return ((_m & 0x010000000000ULL) != 0ULL); }
  124. /**
  125. * @param True if this is a locally-administered MAC
  126. */
  127. inline bool isLocallyAdministered() const { return ((_m & 0x020000000000ULL) != 0ULL); }
  128. /**
  129. * Set this MAC to a MAC derived from an address and a network ID
  130. *
  131. * @param ztaddr ZeroTier address
  132. * @param nwid 64-bit network ID
  133. */
  134. inline void fromAddress(const Address &ztaddr,uint64_t nwid)
  135. {
  136. uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40;
  137. m |= ztaddr.toInt(); // a is 40 bits
  138. m ^= ((nwid >> 8) & 0xff) << 32;
  139. m ^= ((nwid >> 16) & 0xff) << 24;
  140. m ^= ((nwid >> 24) & 0xff) << 16;
  141. m ^= ((nwid >> 32) & 0xff) << 8;
  142. m ^= (nwid >> 40) & 0xff;
  143. _m = m;
  144. }
  145. /**
  146. * Get the ZeroTier address for this MAC on this network (assuming no bridging of course, basic unicast)
  147. *
  148. * This just XORs the next-lest-significant 5 bytes of the network ID again to unmask.
  149. *
  150. * @param nwid Network ID
  151. */
  152. inline Address toAddress(uint64_t nwid) const
  153. {
  154. uint64_t a = _m & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
  155. a ^= ((nwid >> 8) & 0xff) << 32; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
  156. a ^= ((nwid >> 16) & 0xff) << 24;
  157. a ^= ((nwid >> 24) & 0xff) << 16;
  158. a ^= ((nwid >> 32) & 0xff) << 8;
  159. a ^= (nwid >> 40) & 0xff;
  160. return Address(a);
  161. }
  162. /**
  163. * @param nwid Network ID
  164. * @return First octet of MAC for this network
  165. */
  166. static inline unsigned char firstOctetForNetwork(uint64_t nwid)
  167. {
  168. unsigned char a = ((unsigned char)(nwid & 0xfe) | 0x02); // locally administered, not multicast, from LSB of network ID
  169. 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
  170. }
  171. /**
  172. * @param i Value from 0 to 5 (inclusive)
  173. * @return Byte at said position (address interpreted in big-endian order)
  174. */
  175. inline unsigned char operator[](unsigned int i) const { return (unsigned char)((_m >> (40 - (i * 8))) & 0xff); }
  176. /**
  177. * @return 6, which is the number of bytes in a MAC, for container compliance
  178. */
  179. inline unsigned int size() const { return 6; }
  180. inline unsigned long hashCode() const { return (unsigned long)_m; }
  181. inline char *toString(char buf[18]) const
  182. {
  183. buf[0] = Utils::HEXCHARS[(_m >> 44) & 0xf];
  184. buf[1] = Utils::HEXCHARS[(_m >> 40) & 0xf];
  185. buf[2] = ':';
  186. buf[3] = Utils::HEXCHARS[(_m >> 36) & 0xf];
  187. buf[4] = Utils::HEXCHARS[(_m >> 32) & 0xf];
  188. buf[5] = ':';
  189. buf[6] = Utils::HEXCHARS[(_m >> 28) & 0xf];
  190. buf[7] = Utils::HEXCHARS[(_m >> 24) & 0xf];
  191. buf[8] = ':';
  192. buf[9] = Utils::HEXCHARS[(_m >> 20) & 0xf];
  193. buf[10] = Utils::HEXCHARS[(_m >> 16) & 0xf];
  194. buf[11] = ':';
  195. buf[12] = Utils::HEXCHARS[(_m >> 12) & 0xf];
  196. buf[13] = Utils::HEXCHARS[(_m >> 8) & 0xf];
  197. buf[14] = ':';
  198. buf[15] = Utils::HEXCHARS[(_m >> 4) & 0xf];
  199. buf[16] = Utils::HEXCHARS[_m & 0xf];
  200. buf[17] = (char)0;
  201. return buf;
  202. }
  203. inline MAC &operator=(const MAC &m)
  204. {
  205. _m = m._m;
  206. return *this;
  207. }
  208. inline MAC &operator=(const uint64_t m)
  209. {
  210. _m = m;
  211. return *this;
  212. }
  213. inline bool operator==(const MAC &m) const { return (_m == m._m); }
  214. inline bool operator!=(const MAC &m) const { return (_m != m._m); }
  215. inline bool operator<(const MAC &m) const { return (_m < m._m); }
  216. inline bool operator<=(const MAC &m) const { return (_m <= m._m); }
  217. inline bool operator>(const MAC &m) const { return (_m > m._m); }
  218. inline bool operator>=(const MAC &m) const { return (_m >= m._m); }
  219. private:
  220. uint64_t _m;
  221. };
  222. } // namespace ZeroTier
  223. #endif