NetworkConfig.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. /**
  37. * Default maximum time delta for COMs, tags, and capabilities
  38. *
  39. * The current value is two hours, providing ample time for a controller to
  40. * experience fail-over, etc.
  41. */
  42. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA 7200000ULL
  43. /**
  44. * Default minimum credential TTL and maxDelta for COM timestamps
  45. *
  46. * This is just slightly over three minutes and provides three retries for
  47. * all currently online members to refresh.
  48. */
  49. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MIN_MAX_DELTA 185000ULL
  50. /**
  51. * Flag: enable broadcast
  52. */
  53. #define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0000000000000002ULL
  54. /**
  55. * Flag: enable IPv6 NDP emulation for certain V6 address patterns
  56. */
  57. #define ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION 0x0000000000000004ULL
  58. /**
  59. * Flag: result of unrecognized MATCH entries in a rules table: match if set, no-match if clear
  60. */
  61. #define ZT_NETWORKCONFIG_FLAG_RULES_RESULT_OF_UNSUPPORTED_MATCH 0x0000000000000008ULL
  62. /**
  63. * Flag: disable frame compression
  64. */
  65. #define ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION 0x0000000000000010ULL
  66. /**
  67. * Device can bridge to other Ethernet networks and gets unknown recipient multicasts
  68. */
  69. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE 0x0000020000000000ULL
  70. namespace ZeroTier {
  71. // Dictionary capacity needed for max size network config
  72. #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))
  73. // Dictionary capacity needed for max size network meta-data
  74. #define ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY 8192
  75. // Network config version
  76. #define ZT_NETWORKCONFIG_VERSION 7
  77. // Fields for meta-data sent with network config requests
  78. // Network config version
  79. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION "v"
  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. // Legacy fields -- these are obsoleted but are included when older clients query
  149. // boolean (now a flag)
  150. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD "eb"
  151. // IP/bits[,IP/bits,...]
  152. // Note that IPs that end in all zeroes are routes with no assignment in them.
  153. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD "v4s"
  154. // IP/bits[,IP/bits,...]
  155. // Note that IPs that end in all zeroes are routes with no assignment in them.
  156. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD "v6s"
  157. // 0/1
  158. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD "p"
  159. // integer(hex)[,integer(hex),...]
  160. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD "et"
  161. // string-serialized CertificateOfMembership
  162. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD "com"
  163. // node[,node,...]
  164. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD "ab"
  165. // node;IP/port[,node;IP/port]
  166. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD "rl"
  167. // End legacy fields
  168. /**
  169. * Network configuration received from network controller nodes
  170. *
  171. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  172. * without locks.
  173. */
  174. struct NetworkConfig
  175. {
  176. inline NetworkConfig() :
  177. networkId(0),
  178. timestamp(0),
  179. credentialTimeMaxDelta(0),
  180. revision(0),
  181. issuedTo(),
  182. remoteTraceTarget(),
  183. flags(0),
  184. remoteTraceLevel(Trace::LEVEL_NORMAL),
  185. mtu(0),
  186. multicastLimit(0),
  187. specialistCount(0),
  188. routeCount(0),
  189. staticIpCount(0),
  190. ruleCount(0),
  191. capabilityCount(0),
  192. tagCount(0),
  193. certificateOfOwnershipCount(0),
  194. type(ZT_NETWORK_TYPE_PRIVATE)
  195. {
  196. name[0] = 0;
  197. }
  198. /**
  199. * Write this network config to a dictionary for transport
  200. *
  201. * @param d Dictionary
  202. * @param includeLegacy If true, include legacy fields for old node versions
  203. * @return True if dictionary was successfully created, false if e.g. overflow
  204. */
  205. bool toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const;
  206. /**
  207. * Read this network config from a dictionary
  208. *
  209. * @param d Dictionary (non-const since it might be modified during parse, should not be used after call)
  210. * @return True if dictionary was valid and network config successfully initialized
  211. */
  212. bool fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d);
  213. /**
  214. * @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
  215. */
  216. inline bool enableBroadcast() const { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0); }
  217. /**
  218. * @return True if IPv6 NDP emulation should be allowed for certain "magic" IPv6 address patterns
  219. */
  220. inline bool ndpEmulation() const { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION) != 0); }
  221. /**
  222. * @return True if frames should not be compressed
  223. */
  224. inline bool disableCompression() const
  225. {
  226. #ifndef ZT_DISABLE_COMPRESSION
  227. return ((this->flags & ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION) != 0);
  228. #else
  229. /* Compression is disabled for libzt builds since it causes non-obvious chaotic
  230. interference with lwIP's TCP congestion algorithm. Compression is also disabled
  231. for some NAS builds due to the usage of low-performance processors in certain
  232. older and budget models. */
  233. return true;
  234. #endif
  235. }
  236. /**
  237. * @return Network type is public (no access control)
  238. */
  239. inline bool isPublic() const { return (this->type == ZT_NETWORK_TYPE_PUBLIC); }
  240. /**
  241. * @return Network type is private (certificate access control)
  242. */
  243. inline bool isPrivate() const { return (this->type == ZT_NETWORK_TYPE_PRIVATE); }
  244. /**
  245. * @return ZeroTier addresses of devices on this network designated as active bridges
  246. */
  247. inline std::vector<Address> activeBridges() const
  248. {
  249. std::vector<Address> r;
  250. for(unsigned int i=0;i<specialistCount;++i) {
  251. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  252. r.push_back(Address(specialists[i]));
  253. }
  254. return r;
  255. }
  256. inline unsigned int activeBridges(Address ab[ZT_MAX_NETWORK_SPECIALISTS]) const
  257. {
  258. unsigned int c = 0;
  259. for(unsigned int i=0;i<specialistCount;++i) {
  260. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  261. ab[c++] = specialists[i];
  262. }
  263. return c;
  264. }
  265. inline bool isActiveBridge(const Address &a) const
  266. {
  267. for(unsigned int i=0;i<specialistCount;++i) {
  268. if (((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)&&(a == specialists[i]))
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  275. * @return True if this network allows bridging
  276. */
  277. inline bool permitsBridging(const Address &fromPeer) const
  278. {
  279. for(unsigned int i=0;i<specialistCount;++i) {
  280. if ((fromPeer == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
  281. return true;
  282. }
  283. return false;
  284. }
  285. inline operator bool() const { return (networkId != 0); }
  286. inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  287. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  288. /**
  289. * Add a specialist or mask flags if already present
  290. *
  291. * This masks the existing flags if the specialist is already here or adds
  292. * it otherwise.
  293. *
  294. * @param a Address of specialist
  295. * @param f Flags (OR of specialist role/type flags)
  296. * @return True if successfully masked or added
  297. */
  298. inline bool addSpecialist(const Address &a,const uint64_t f)
  299. {
  300. const uint64_t aint = a.toInt();
  301. for(unsigned int i=0;i<specialistCount;++i) {
  302. if ((specialists[i] & 0xffffffffffULL) == aint) {
  303. specialists[i] |= f;
  304. return true;
  305. }
  306. }
  307. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  308. specialists[specialistCount++] = f | aint;
  309. return true;
  310. }
  311. return false;
  312. }
  313. const Capability *capability(const uint32_t id) const
  314. {
  315. for(unsigned int i=0;i<capabilityCount;++i) {
  316. if (capabilities[i].id() == id)
  317. return &(capabilities[i]);
  318. }
  319. return (Capability *)0;
  320. }
  321. const Tag *tag(const uint32_t id) const
  322. {
  323. for(unsigned int i=0;i<tagCount;++i) {
  324. if (tags[i].id() == id)
  325. return &(tags[i]);
  326. }
  327. return (Tag *)0;
  328. }
  329. /**
  330. * Network ID that this configuration applies to
  331. */
  332. uint64_t networkId;
  333. /**
  334. * Controller-side time of config generation/issue
  335. */
  336. int64_t timestamp;
  337. /**
  338. * Max difference between timestamp and tag/capability timestamp
  339. */
  340. int64_t credentialTimeMaxDelta;
  341. /**
  342. * Controller-side revision counter for this configuration
  343. */
  344. uint64_t revision;
  345. /**
  346. * Address of device to which this config is issued
  347. */
  348. Address issuedTo;
  349. /**
  350. * If non-NULL, remote traces related to this network are sent here
  351. */
  352. Address remoteTraceTarget;
  353. /**
  354. * Flags (64-bit)
  355. */
  356. uint64_t flags;
  357. /**
  358. * Remote trace level
  359. */
  360. Trace::Level remoteTraceLevel;
  361. /**
  362. * Network MTU
  363. */
  364. unsigned int mtu;
  365. /**
  366. * Maximum number of recipients per multicast (not including active bridges)
  367. */
  368. unsigned int multicastLimit;
  369. /**
  370. * Number of specialists
  371. */
  372. unsigned int specialistCount;
  373. /**
  374. * Number of routes
  375. */
  376. unsigned int routeCount;
  377. /**
  378. * Number of ZT-managed static IP assignments
  379. */
  380. unsigned int staticIpCount;
  381. /**
  382. * Number of rule table entries
  383. */
  384. unsigned int ruleCount;
  385. /**
  386. * Number of capabilities
  387. */
  388. unsigned int capabilityCount;
  389. /**
  390. * Number of tags
  391. */
  392. unsigned int tagCount;
  393. /**
  394. * Number of certificates of ownership
  395. */
  396. unsigned int certificateOfOwnershipCount;
  397. /**
  398. * Specialist devices
  399. *
  400. * For each entry the least significant 40 bits are the device's ZeroTier
  401. * address and the most significant 24 bits are flags indicating its role.
  402. */
  403. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  404. /**
  405. * Statically defined "pushed" routes (including default gateways)
  406. */
  407. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  408. /**
  409. * Static IP assignments
  410. */
  411. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  412. /**
  413. * Base network rules
  414. */
  415. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  416. /**
  417. * Capabilities for this node on this network, in ascending order of capability ID
  418. */
  419. Capability capabilities[ZT_MAX_NETWORK_CAPABILITIES];
  420. /**
  421. * Tags for this node on this network, in ascending order of tag ID
  422. */
  423. Tag tags[ZT_MAX_NETWORK_TAGS];
  424. /**
  425. * Certificates of ownership for this network member
  426. */
  427. CertificateOfOwnership certificatesOfOwnership[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  428. /**
  429. * Network type (currently just public or private)
  430. */
  431. ZT_VirtualNetworkType type;
  432. /**
  433. * Network short name or empty string if not defined
  434. */
  435. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  436. /**
  437. * Certificate of membership (for private networks)
  438. */
  439. CertificateOfMembership com;
  440. };
  441. } // namespace ZeroTier
  442. #endif