2
0

NetworkConfig.hpp 11 KB

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