NetworkConfig.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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: 2026-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 "../include/ZeroTierOne.h"
  16. #include "Address.hpp"
  17. #include "Buffer.hpp"
  18. #include "Capability.hpp"
  19. #include "CertificateOfMembership.hpp"
  20. #include "CertificateOfOwnership.hpp"
  21. #include "Constants.hpp"
  22. #include "DNS.hpp"
  23. #include "Dictionary.hpp"
  24. #include "Hashtable.hpp"
  25. #include "Identity.hpp"
  26. #include "InetAddress.hpp"
  27. #include "MulticastGroup.hpp"
  28. #include "Tag.hpp"
  29. #include "Trace.hpp"
  30. #include "Utils.hpp"
  31. #include <algorithm>
  32. #include <stdexcept>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <vector>
  37. /**
  38. * Default time delta for COMs, tags, and capabilities
  39. */
  40. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_DFL_MAX_DELTA ((int64_t)(1000 * 60 * 30))
  41. /**
  42. * Maximum time delta for COMs, tags, and capabilities
  43. */
  44. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA ((int64_t)(1000 * 60 * 60 * 2))
  45. /**
  46. * Minimum credential TTL and maxDelta for COM timestamps
  47. */
  48. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MIN_MAX_DELTA ((int64_t)(1000 * 60 * 5))
  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. * Flag: disable frame compression
  63. */
  64. #define ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION 0x0000000000000010ULL
  65. /**
  66. * Device can bridge to other Ethernet networks and gets unknown recipient multicasts
  67. */
  68. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE 0x0000020000000000ULL
  69. /**
  70. * Anchors are stable devices on this network that can act like roots when none are up
  71. */
  72. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR 0x0000040000000000ULL
  73. /**
  74. * Designated multicast replicators replicate multicast in place of sender-side replication
  75. */
  76. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR 0x0000080000000000ULL
  77. namespace ZeroTier {
  78. // Dictionary capacity needed for max size network config
  79. #define ZT_NETWORKCONFIG_DICT_CAPACITY \
  80. (4096 + (sizeof(ZT_VirtualNetworkConfig)) + (sizeof(ZT_VirtualNetworkRule) * ZT_MAX_NETWORK_RULES) + (sizeof(Capability) * ZT_MAX_NETWORK_CAPABILITIES) + (sizeof(Tag) * ZT_MAX_NETWORK_TAGS) \
  81. + (sizeof(CertificateOfOwnership) * ZT_MAX_CERTIFICATES_OF_OWNERSHIP))
  82. // Dictionary capacity needed for max size network meta-data
  83. #define ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY 1024
  84. // Network config version
  85. #define ZT_NETWORKCONFIG_VERSION 7
  86. // Fields for meta-data sent with network config requests
  87. // Network config version
  88. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION "v"
  89. // Network config version
  90. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_OS_ARCH "o"
  91. // Protocol version (see Packet.hpp)
  92. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION "pv"
  93. // Software vendor
  94. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_VENDOR "vend"
  95. // Software major version
  96. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  97. // Software minor version
  98. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  99. // Software revision
  100. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  101. // Rules engine revision
  102. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV "revr"
  103. // Maximum number of rules per network this node can accept
  104. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_RULES "mr"
  105. // Maximum number of capabilities this node can accept
  106. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_CAPABILITIES "mc"
  107. // Maximum number of rules per capability this node can accept
  108. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_CAPABILITY_RULES "mcr"
  109. // Maximum number of tags this node can accept
  110. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS "mt"
  111. // Network join authorization token (if any)
  112. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_AUTH "a"
  113. // Network configuration meta-data flags
  114. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS "f"
  115. // These dictionary keys are short so they don't take up much room.
  116. // By convention we use upper case for binary blobs, but it doesn't really matter.
  117. // network config version
  118. #define ZT_NETWORKCONFIG_DICT_KEY_VERSION "v"
  119. // network ID
  120. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  121. // integer(hex)
  122. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  123. // integer(hex)
  124. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  125. // address of member
  126. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  127. // remote trace target
  128. #define ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET "tt"
  129. // remote trace level
  130. #define ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL "tl"
  131. // flags(hex)
  132. #define ZT_NETWORKCONFIG_DICT_KEY_FLAGS "f"
  133. // integer(hex)
  134. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  135. // network type (hex)
  136. #define ZT_NETWORKCONFIG_DICT_KEY_TYPE "t"
  137. // text
  138. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  139. // network MTU
  140. #define ZT_NETWORKCONFIG_DICT_KEY_MTU "mtu"
  141. // credential time max delta in ms
  142. #define ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA "ctmd"
  143. // binary serialized certificate of membership
  144. #define ZT_NETWORKCONFIG_DICT_KEY_COM "C"
  145. // specialists (binary array of uint64_t)
  146. #define ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS "S"
  147. // routes (binary blob)
  148. #define ZT_NETWORKCONFIG_DICT_KEY_ROUTES "RT"
  149. // static IPs (binary blob)
  150. #define ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS "I"
  151. // rules (binary blob)
  152. #define ZT_NETWORKCONFIG_DICT_KEY_RULES "R"
  153. // capabilities (binary blobs)
  154. #define ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES "CAP"
  155. // tags (binary blobs)
  156. #define ZT_NETWORKCONFIG_DICT_KEY_TAGS "TAG"
  157. // tags (binary blobs)
  158. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP "COO"
  159. // dns (binary blobs)
  160. #define ZT_NETWORKCONFIG_DICT_KEY_DNS "DNS"
  161. // sso enabled
  162. #define ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED "ssoe"
  163. // so version
  164. #define ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION "ssov"
  165. // authentication URL
  166. #define ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL "aurl"
  167. // authentication expiry
  168. #define ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_EXPIRY_TIME "aexpt"
  169. // oidc issuer URL
  170. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUER_URL "iurl"
  171. // central endpoint
  172. #define ZT_NETWORKCONFIG_DICT_KEY_CENTRAL_ENDPOINT_URL "ssoce"
  173. // nonce
  174. #define ZT_NETWORKCONFIG_DICT_KEY_NONCE "sson"
  175. // state
  176. #define ZT_NETWORKCONFIG_DICT_KEY_STATE "ssos"
  177. // client ID
  178. #define ZT_NETWORKCONFIG_DICT_KEY_CLIENT_ID "ssocid"
  179. // SSO Provider
  180. #define ZT_NETWORKCONFIG_DICT_KEY_SSO_PROVIDER "ssop"
  181. // AuthInfo fields -- used by ncSendError for sso
  182. // AuthInfo Version
  183. #define ZT_AUTHINFO_DICT_KEY_VERSION "aV"
  184. // authentication URL
  185. #define ZT_AUTHINFO_DICT_KEY_AUTHENTICATION_URL "aU"
  186. // issuer URL
  187. #define ZT_AUTHINFO_DICT_KEY_ISSUER_URL "iU"
  188. // Central endpoint URL
  189. #define ZT_AUTHINFO_DICT_KEY_CENTRAL_ENDPOINT_URL "aCU"
  190. // Nonce
  191. #define ZT_AUTHINFO_DICT_KEY_NONCE "aN"
  192. // State
  193. #define ZT_AUTHINFO_DICT_KEY_STATE "aS"
  194. // Client ID
  195. #define ZT_AUTHINFO_DICT_KEY_CLIENT_ID "aCID"
  196. // SSO Provider
  197. #define ZT_AUTHINFO_DICT_KEY_SSO_PROVIDER "aSSOp"
  198. // Legacy fields -- these are obsoleted but are included when older clients query
  199. // boolean (now a flag)
  200. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD "eb"
  201. // IP/bits[,IP/bits,...]
  202. // Note that IPs that end in all zeroes are routes with no assignment in them.
  203. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD "v4s"
  204. // IP/bits[,IP/bits,...]
  205. // Note that IPs that end in all zeroes are routes with no assignment in them.
  206. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD "v6s"
  207. // 0/1
  208. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD "p"
  209. // integer(hex)[,integer(hex),...]
  210. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD "et"
  211. // string-serialized CertificateOfMembership
  212. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD "com"
  213. // node[,node,...]
  214. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD "ab"
  215. // node;IP/port[,node;IP/port]
  216. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD "rl"
  217. // End legacy fields
  218. /**
  219. * Network configuration received from network controller nodes
  220. *
  221. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  222. * without locks.
  223. */
  224. class NetworkConfig {
  225. public:
  226. NetworkConfig()
  227. : networkId(0)
  228. , timestamp(0)
  229. , credentialTimeMaxDelta(0)
  230. , revision(0)
  231. , issuedTo()
  232. , remoteTraceTarget()
  233. , flags(0)
  234. , remoteTraceLevel(Trace::LEVEL_NORMAL)
  235. , mtu(0)
  236. , multicastLimit(0)
  237. , specialistCount(0)
  238. , routeCount(0)
  239. , staticIpCount(0)
  240. , ruleCount(0)
  241. , capabilityCount(0)
  242. , tagCount(0)
  243. , certificateOfOwnershipCount(0)
  244. , capabilities()
  245. , tags()
  246. , certificatesOfOwnership()
  247. , type(ZT_NETWORK_TYPE_PRIVATE)
  248. , dnsCount(0)
  249. , ssoEnabled(false)
  250. , authenticationURL()
  251. , authenticationExpiryTime(0)
  252. , issuerURL()
  253. , centralAuthURL()
  254. , ssoNonce()
  255. , ssoState()
  256. , ssoClientID()
  257. {
  258. name[0] = 0;
  259. memset(specialists, 0, sizeof(uint64_t) * ZT_MAX_NETWORK_SPECIALISTS);
  260. memset(routes, 0, sizeof(ZT_VirtualNetworkRoute) * ZT_MAX_NETWORK_ROUTES);
  261. memset(staticIps, 0, sizeof(InetAddress) * ZT_MAX_ZT_ASSIGNED_ADDRESSES);
  262. memset(rules, 0, sizeof(ZT_VirtualNetworkRule) * ZT_MAX_NETWORK_RULES);
  263. memset(&dns, 0, sizeof(ZT_VirtualNetworkDNS));
  264. memset(authenticationURL, 0, sizeof(authenticationURL));
  265. memset(issuerURL, 0, sizeof(issuerURL));
  266. memset(centralAuthURL, 0, sizeof(centralAuthURL));
  267. memset(ssoNonce, 0, sizeof(ssoNonce));
  268. memset(ssoState, 0, sizeof(ssoState));
  269. memset(ssoClientID, 0, sizeof(ssoClientID));
  270. strncpy(ssoProvider, "default", sizeof(ssoProvider));
  271. }
  272. /**
  273. * Write this network config to a dictionary for transport
  274. *
  275. * @param d Dictionary
  276. * @param includeLegacy If true, include legacy fields for old node versions
  277. * @return True if dictionary was successfully created, false if e.g. overflow
  278. */
  279. bool toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>& d, bool includeLegacy) const;
  280. /**
  281. * Read this network config from a dictionary
  282. *
  283. * @param d Dictionary (non-const since it might be modified during parse, should not be used after call)
  284. * @return True if dictionary was valid and network config successfully initialized
  285. */
  286. bool fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>& d);
  287. /**
  288. * @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
  289. */
  290. inline bool enableBroadcast() const
  291. {
  292. return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0);
  293. }
  294. /**
  295. * @return True if IPv6 NDP emulation should be allowed for certain "magic" IPv6 address patterns
  296. */
  297. inline bool ndpEmulation() const
  298. {
  299. return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION) != 0);
  300. }
  301. /**
  302. * @return True if frames should not be compressed
  303. */
  304. inline bool disableCompression() const
  305. {
  306. #ifndef ZT_DISABLE_COMPRESSION
  307. return ((this->flags & ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION) != 0);
  308. #else
  309. /* Compression is disabled for libzt builds since it causes non-obvious chaotic
  310. interference with lwIP's TCP congestion algorithm. Compression is also disabled
  311. for some NAS builds due to the usage of low-performance processors in certain
  312. older and budget models. */
  313. return false;
  314. #endif
  315. }
  316. /**
  317. * @return Network type is public (no access control)
  318. */
  319. inline bool isPublic() const
  320. {
  321. return (this->type == ZT_NETWORK_TYPE_PUBLIC);
  322. }
  323. /**
  324. * @return Network type is private (certificate access control)
  325. */
  326. inline bool isPrivate() const
  327. {
  328. return (this->type == ZT_NETWORK_TYPE_PRIVATE);
  329. }
  330. /**
  331. * @return ZeroTier addresses of devices on this network designated as active bridges
  332. */
  333. inline std::vector<Address> activeBridges() const
  334. {
  335. std::vector<Address> r;
  336. for (unsigned int i = 0; i < specialistCount; ++i) {
  337. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
  338. r.push_back(Address(specialists[i]));
  339. }
  340. }
  341. return r;
  342. }
  343. inline unsigned int activeBridges(Address ab[ZT_MAX_NETWORK_SPECIALISTS]) const
  344. {
  345. unsigned int c = 0;
  346. for (unsigned int i = 0; i < specialistCount; ++i) {
  347. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
  348. ab[c++] = specialists[i];
  349. }
  350. }
  351. return c;
  352. }
  353. inline bool isActiveBridge(const Address& a) const
  354. {
  355. for (unsigned int i = 0; i < specialistCount; ++i) {
  356. if (((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) && (a == specialists[i])) {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. inline std::vector<Address> anchors() const
  363. {
  364. std::vector<Address> r;
  365. for (unsigned int i = 0; i < specialistCount; ++i) {
  366. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0) {
  367. r.push_back(Address(specialists[i]));
  368. }
  369. }
  370. return r;
  371. }
  372. inline std::vector<Address> multicastReplicators() const
  373. {
  374. std::vector<Address> r;
  375. for (unsigned int i = 0; i < specialistCount; ++i) {
  376. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR) != 0) {
  377. r.push_back(Address(specialists[i]));
  378. }
  379. }
  380. return r;
  381. }
  382. inline unsigned int multicastReplicators(Address mr[ZT_MAX_NETWORK_SPECIALISTS]) const
  383. {
  384. unsigned int c = 0;
  385. for (unsigned int i = 0; i < specialistCount; ++i) {
  386. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR) != 0) {
  387. mr[c++] = specialists[i];
  388. }
  389. }
  390. return c;
  391. }
  392. inline bool isMulticastReplicator(const Address& a) const
  393. {
  394. for (unsigned int i = 0; i < specialistCount; ++i) {
  395. if (((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR) != 0) && (a == specialists[i])) {
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. inline std::vector<Address> alwaysContactAddresses() const
  402. {
  403. std::vector<Address> r;
  404. for (unsigned int i = 0; i < specialistCount; ++i) {
  405. if ((specialists[i] & (ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR | ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR)) != 0) {
  406. r.push_back(Address(specialists[i]));
  407. }
  408. }
  409. return r;
  410. }
  411. inline unsigned int alwaysContactAddresses(Address ac[ZT_MAX_NETWORK_SPECIALISTS]) const
  412. {
  413. unsigned int c = 0;
  414. for (unsigned int i = 0; i < specialistCount; ++i) {
  415. if ((specialists[i] & (ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR | ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR)) != 0) {
  416. ac[c++] = specialists[i];
  417. }
  418. }
  419. return c;
  420. }
  421. inline void alwaysContactAddresses(Hashtable<Address, std::vector<InetAddress> >& a) const
  422. {
  423. for (unsigned int i = 0; i < specialistCount; ++i) {
  424. if ((specialists[i] & (ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR | ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR)) != 0) {
  425. a[Address(specialists[i])];
  426. }
  427. }
  428. }
  429. /**
  430. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  431. * @return True if this network allows bridging
  432. */
  433. inline bool permitsBridging(const Address& fromPeer) const
  434. {
  435. for (unsigned int i = 0; i < specialistCount; ++i) {
  436. if ((fromPeer == specialists[i]) && ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)) {
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. inline operator bool() const
  443. {
  444. return (networkId != 0);
  445. }
  446. inline bool operator==(const NetworkConfig& nc) const
  447. {
  448. return (memcmp(this, &nc, sizeof(NetworkConfig)) == 0);
  449. }
  450. inline bool operator!=(const NetworkConfig& nc) const
  451. {
  452. return (! (*this == nc));
  453. }
  454. /**
  455. * Add a specialist or mask flags if already present
  456. *
  457. * This masks the existing flags if the specialist is already here or adds
  458. * it otherwise.
  459. *
  460. * @param a Address of specialist
  461. * @param f Flags (OR of specialist role/type flags)
  462. * @return True if successfully masked or added
  463. */
  464. inline bool addSpecialist(const Address& a, const uint64_t f)
  465. {
  466. const uint64_t aint = a.toInt();
  467. for (unsigned int i = 0; i < specialistCount; ++i) {
  468. if ((specialists[i] & 0xffffffffffULL) == aint) {
  469. specialists[i] |= f;
  470. return true;
  471. }
  472. }
  473. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  474. specialists[specialistCount++] = f | aint;
  475. return true;
  476. }
  477. return false;
  478. }
  479. const Capability* capability(const uint32_t id) const
  480. {
  481. for (unsigned int i = 0; i < capabilityCount; ++i) {
  482. if (capabilities[i].id() == id) {
  483. return &(capabilities[i]);
  484. }
  485. }
  486. return (Capability*)0;
  487. }
  488. const Tag* tag(const uint32_t id) const
  489. {
  490. for (unsigned int i = 0; i < tagCount; ++i) {
  491. if (tags[i].id() == id) {
  492. return &(tags[i]);
  493. }
  494. }
  495. return (Tag*)0;
  496. }
  497. /**
  498. * Network ID that this configuration applies to
  499. */
  500. uint64_t networkId;
  501. /**
  502. * Controller-side time of config generation/issue
  503. */
  504. int64_t timestamp;
  505. /**
  506. * Max difference between timestamp and tag/capability timestamp
  507. */
  508. int64_t credentialTimeMaxDelta;
  509. /**
  510. * Controller-side revision counter for this configuration
  511. */
  512. uint64_t revision;
  513. /**
  514. * Address of device to which this config is issued
  515. */
  516. Address issuedTo;
  517. /**
  518. * If non-NULL, remote traces related to this network are sent here
  519. */
  520. Address remoteTraceTarget;
  521. /**
  522. * Flags (64-bit)
  523. */
  524. uint64_t flags;
  525. /**
  526. * Remote trace level
  527. */
  528. Trace::Level remoteTraceLevel;
  529. /**
  530. * Network MTU
  531. */
  532. unsigned int mtu;
  533. /**
  534. * Maximum number of recipients per multicast (not including active bridges)
  535. */
  536. unsigned int multicastLimit;
  537. /**
  538. * Number of specialists
  539. */
  540. unsigned int specialistCount;
  541. /**
  542. * Number of routes
  543. */
  544. unsigned int routeCount;
  545. /**
  546. * Number of ZT-managed static IP assignments
  547. */
  548. unsigned int staticIpCount;
  549. /**
  550. * Number of rule table entries
  551. */
  552. unsigned int ruleCount;
  553. /**
  554. * Number of capabilities
  555. */
  556. unsigned int capabilityCount;
  557. /**
  558. * Number of tags
  559. */
  560. unsigned int tagCount;
  561. /**
  562. * Number of certificates of ownership
  563. */
  564. unsigned int certificateOfOwnershipCount;
  565. /**
  566. * Specialist devices
  567. *
  568. * For each entry the least significant 40 bits are the device's ZeroTier
  569. * address and the most significant 24 bits are flags indicating its role.
  570. */
  571. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  572. /**
  573. * Statically defined "pushed" routes (including default gateways)
  574. */
  575. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  576. /**
  577. * Static IP assignments
  578. */
  579. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  580. /**
  581. * Base network rules
  582. */
  583. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  584. /**
  585. * Capabilities for this node on this network, in ascending order of capability ID
  586. */
  587. Capability capabilities[ZT_MAX_NETWORK_CAPABILITIES];
  588. /**
  589. * Tags for this node on this network, in ascending order of tag ID
  590. */
  591. Tag tags[ZT_MAX_NETWORK_TAGS];
  592. /**
  593. * Certificates of ownership for this network member
  594. */
  595. CertificateOfOwnership certificatesOfOwnership[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  596. /**
  597. * Network type (currently just public or private)
  598. */
  599. ZT_VirtualNetworkType type;
  600. /**
  601. * Network short name or empty string if not defined
  602. */
  603. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  604. /**
  605. * Certificate of membership (for private networks)
  606. */
  607. CertificateOfMembership com;
  608. /**
  609. * Number of ZT-pushed DNS configurations
  610. */
  611. unsigned int dnsCount;
  612. /**
  613. * ZT pushed DNS configuration
  614. */
  615. ZT_VirtualNetworkDNS dns;
  616. /**
  617. * SSO enabled flag.
  618. */
  619. bool ssoEnabled;
  620. /**
  621. * SSO version
  622. */
  623. uint64_t ssoVersion;
  624. /**
  625. * Authentication URL if authentication is required
  626. */
  627. char authenticationURL[2048];
  628. /**
  629. * Time current authentication expires or 0 if external authentication is disabled
  630. *
  631. * Not used if authVersion >= 1
  632. */
  633. uint64_t authenticationExpiryTime;
  634. /**
  635. * OIDC issuer URL
  636. */
  637. char issuerURL[2048];
  638. /**
  639. * central base URL.
  640. */
  641. char centralAuthURL[2048];
  642. /**
  643. * sso nonce
  644. */
  645. char ssoNonce[128];
  646. /**
  647. * sso state
  648. */
  649. char ssoState[256];
  650. /**
  651. * oidc client id
  652. */
  653. char ssoClientID[256];
  654. /**
  655. * oidc provider
  656. *
  657. * because certain providers require specific scopes to be requested
  658. * and others to be not requested in order to make everything work
  659. * correctly
  660. **/
  661. char ssoProvider[64];
  662. };
  663. } // namespace ZeroTier
  664. #endif