NetworkConfig.hpp 13 KB

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