NetworkConfig.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_NETWORKCONFIG_HPP
  14. #define ZT_NETWORKCONFIG_HPP
  15. #include <cstdint>
  16. #include <cstring>
  17. #include <cstdlib>
  18. #include <vector>
  19. #include <stdexcept>
  20. #include <algorithm>
  21. #include "Constants.hpp"
  22. #include "InetAddress.hpp"
  23. #include "MulticastGroup.hpp"
  24. #include "Address.hpp"
  25. #include "CertificateOfMembership.hpp"
  26. #include "CertificateOfOwnership.hpp"
  27. #include "Capability.hpp"
  28. #include "Tag.hpp"
  29. #include "Dictionary.hpp"
  30. #include "Identity.hpp"
  31. #include "Utils.hpp"
  32. #include "Trace.hpp"
  33. #include "TriviallyCopyable.hpp"
  34. namespace ZeroTier {
  35. /**
  36. * Default maximum time delta for COMs, tags, and capabilities
  37. *
  38. * The current value is two hours, providing ample time for a controller to
  39. * experience fail-over, etc.
  40. */
  41. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA 7200000ULL
  42. /**
  43. * Default minimum credential TTL and maxDelta for COM timestamps
  44. *
  45. * This is just slightly over three minutes and provides three retries for
  46. * all currently online members to refresh.
  47. */
  48. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MIN_MAX_DELTA 185000ULL
  49. /**
  50. * Flag: enable broadcast
  51. */
  52. #define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0000000000000002ULL
  53. /**
  54. * Flag: enable IPv6 NDP emulation for certain V6 address patterns
  55. */
  56. #define ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION 0x0000000000000004ULL
  57. /**
  58. * Flag: result of unrecognized MATCH entries in a rules table: match if set, no-match if clear
  59. */
  60. #define ZT_NETWORKCONFIG_FLAG_RULES_RESULT_OF_UNSUPPORTED_MATCH 0x0000000000000008ULL
  61. /**
  62. * Device can bridge to other Ethernet networks and gets unknown recipient multicasts
  63. */
  64. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE 0x0000020000000000ULL
  65. /**
  66. * Device that replicates multicasts
  67. */
  68. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR 0x0000040000000000ULL
  69. /**
  70. * Device that is allowed to remotely debug this network and query other peers for e.g. remote trace data
  71. */
  72. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_DIAGNOSTICIAN 0x0000080000000000ULL
  73. // Fields for meta-data sent with network config requests
  74. // Protocol version (see Packet.hpp)
  75. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION "pv"
  76. // Software vendor
  77. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_VENDOR "vend"
  78. // Software major version
  79. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  80. // Software minor version
  81. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  82. // Software revision
  83. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  84. // Rules engine revision
  85. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV "revr"
  86. // Maximum number of rules per network this node can accept
  87. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_RULES "mr"
  88. // Maximum number of capabilities this node can accept
  89. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_CAPABILITIES "mc"
  90. // Maximum number of rules per capability this node can accept
  91. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_CAPABILITY_RULES "mcr"
  92. // Maximum number of tags this node can accept
  93. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS "mt"
  94. // Network join authorization token (if any)
  95. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_AUTH "a"
  96. // Network configuration meta-data flags
  97. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS "f"
  98. // These dictionary keys are short so they don't take up much room.
  99. // By convention we use upper case for binary blobs, but it doesn't really matter.
  100. // network config version
  101. #define ZT_NETWORKCONFIG_DICT_KEY_VERSION "v"
  102. // network ID
  103. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  104. // integer(hex)
  105. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  106. // integer(hex)
  107. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  108. // address of member
  109. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  110. // full identity hash of member
  111. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO_IDENTITY_HASH "IDH"
  112. // flags(hex)
  113. #define ZT_NETWORKCONFIG_DICT_KEY_FLAGS "f"
  114. // integer(hex)
  115. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  116. // network type (hex)
  117. #define ZT_NETWORKCONFIG_DICT_KEY_TYPE "t"
  118. // text
  119. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  120. // network MTU
  121. #define ZT_NETWORKCONFIG_DICT_KEY_MTU "mtu"
  122. // credential time max delta in ms
  123. #define ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA "ctmd"
  124. // binary serialized certificate of membership
  125. #define ZT_NETWORKCONFIG_DICT_KEY_COM "C"
  126. // specialists (binary array of uint64_t)
  127. #define ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS "S"
  128. // routes (binary blob)
  129. #define ZT_NETWORKCONFIG_DICT_KEY_ROUTES "RT"
  130. // static IPs (binary blob)
  131. #define ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS "I"
  132. // rules (binary blob)
  133. #define ZT_NETWORKCONFIG_DICT_KEY_RULES "R"
  134. // capabilities (binary blobs)
  135. #define ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES "CAP"
  136. // tags (binary blobs)
  137. #define ZT_NETWORKCONFIG_DICT_KEY_TAGS "TAG"
  138. // tags (binary blobs)
  139. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP "COO"
  140. /**
  141. * Network configuration received from network controller nodes
  142. */
  143. struct NetworkConfig : TriviallyCopyable
  144. {
  145. ZT_INLINE NetworkConfig() noexcept { memoryZero(this); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  146. /**
  147. * Write this network config to a dictionary for transport
  148. *
  149. * @param d Dictionary
  150. * @param includeLegacy If true, include legacy fields for old node versions
  151. * @return True if dictionary was successfully created, false if e.g. overflow
  152. */
  153. bool toDictionary(Dictionary &d,bool includeLegacy) const;
  154. /**
  155. * Read this network config from a dictionary
  156. *
  157. * @param d Dictionary (non-const since it might be modified during parse, should not be used after call)
  158. * @return True if dictionary was valid and network config successfully initialized
  159. */
  160. bool fromDictionary(const Dictionary &d);
  161. /**
  162. * @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
  163. */
  164. ZT_INLINE bool enableBroadcast() const noexcept { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0); }
  165. /**
  166. * @return True if IPv6 NDP emulation should be allowed for certain "magic" IPv6 address patterns
  167. */
  168. ZT_INLINE bool ndpEmulation() const noexcept { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION) != 0); }
  169. /**
  170. * @return Network type is public (no access control)
  171. */
  172. ZT_INLINE bool isPublic() const noexcept { return (this->type == ZT_NETWORK_TYPE_PUBLIC); }
  173. /**
  174. * @return Network type is private (certificate access control)
  175. */
  176. ZT_INLINE bool isPrivate() const noexcept { return (this->type == ZT_NETWORK_TYPE_PRIVATE); }
  177. /**
  178. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  179. * @return True if this network allows bridging
  180. */
  181. ZT_INLINE bool permitsBridging(const Address &fromPeer) const noexcept
  182. {
  183. for(unsigned int i=0;i<specialistCount;++i) {
  184. if ((fromPeer.toInt() == (specialists[i] & ZT_ADDRESS_MASK))&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
  185. return true;
  186. }
  187. return false;
  188. }
  189. ZT_INLINE operator bool() const noexcept { return (networkId != 0); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
  190. ZT_INLINE bool operator==(const NetworkConfig &nc) const noexcept { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  191. ZT_INLINE bool operator!=(const NetworkConfig &nc) const noexcept { return (!(*this == nc)); }
  192. /**
  193. * Add a specialist or mask flags if already present
  194. *
  195. * This masks the existing flags if the specialist is already here or adds
  196. * it otherwise.
  197. *
  198. * @param a Address of specialist
  199. * @param f Flags (OR of specialist role/type flags)
  200. * @return True if successfully masked or added
  201. */
  202. bool addSpecialist(const Address &a,uint64_t f) noexcept;
  203. ZT_INLINE const Capability *capability(const uint32_t id) const
  204. {
  205. for(unsigned int i=0;i<capabilityCount;++i) {
  206. if (capabilities[i].id() == id)
  207. return &(capabilities[i]);
  208. }
  209. return nullptr;
  210. }
  211. ZT_INLINE const Tag *tag(const uint32_t id) const
  212. {
  213. for(unsigned int i=0;i<tagCount;++i) {
  214. if (tags[i].id() == id)
  215. return &(tags[i]);
  216. }
  217. return nullptr;
  218. }
  219. /**
  220. * Network ID that this configuration applies to
  221. */
  222. uint64_t networkId;
  223. /**
  224. * Controller-side time of config generation/issue
  225. */
  226. int64_t timestamp;
  227. /**
  228. * Max difference between timestamp and tag/capability timestamp
  229. */
  230. int64_t credentialTimeMaxDelta;
  231. /**
  232. * Controller-side revision counter for this configuration
  233. */
  234. uint64_t revision;
  235. /**
  236. * Address of device to which this config is issued
  237. */
  238. Address issuedTo;
  239. /**
  240. * Hash of identity public key(s) of node to whom this is issued
  241. *
  242. * If this field is all zero it is treated as undefined since old controllers
  243. * do not set it.
  244. */
  245. uint8_t issuedToFingerprintHash[ZT_FINGERPRINT_HASH_SIZE];
  246. /**
  247. * Flags (64-bit)
  248. */
  249. uint64_t flags;
  250. /**
  251. * Network MTU
  252. */
  253. unsigned int mtu;
  254. /**
  255. * Maximum number of recipients per multicast (not including active bridges)
  256. */
  257. unsigned int multicastLimit;
  258. /**
  259. * Number of specialists
  260. */
  261. unsigned int specialistCount;
  262. /**
  263. * Number of routes
  264. */
  265. unsigned int routeCount;
  266. /**
  267. * Number of ZT-managed static IP assignments
  268. */
  269. unsigned int staticIpCount;
  270. /**
  271. * Number of rule table entries
  272. */
  273. unsigned int ruleCount;
  274. /**
  275. * Number of capabilities
  276. */
  277. unsigned int capabilityCount;
  278. /**
  279. * Number of tags
  280. */
  281. unsigned int tagCount;
  282. /**
  283. * Number of certificates of ownership
  284. */
  285. unsigned int certificateOfOwnershipCount;
  286. /**
  287. * Specialist devices
  288. *
  289. * For each entry the least significant 40 bits are the device's ZeroTier
  290. * address and the most significant 24 bits are flags indicating its role.
  291. */
  292. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  293. /**
  294. * Statically defined "pushed" routes (including default gateways)
  295. */
  296. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  297. /**
  298. * Static IP assignments
  299. */
  300. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  301. /**
  302. * Base network rules
  303. */
  304. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  305. /**
  306. * Capabilities for this node on this network, in ascending order of capability ID
  307. */
  308. Capability capabilities[ZT_MAX_NETWORK_CAPABILITIES];
  309. /**
  310. * Tags for this node on this network, in ascending order of tag ID
  311. */
  312. Tag tags[ZT_MAX_NETWORK_TAGS];
  313. /**
  314. * Certificates of ownership for this network member
  315. */
  316. CertificateOfOwnership certificatesOfOwnership[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  317. /**
  318. * Network type (currently just public or private)
  319. */
  320. ZT_VirtualNetworkType type;
  321. /**
  322. * Network short name or empty string if not defined
  323. */
  324. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  325. /**
  326. * Certificate of membership (for private networks)
  327. */
  328. CertificateOfMembership com;
  329. };
  330. } // namespace ZeroTier
  331. #endif