NetworkConfig.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_NETWORKCONFIG_HPP
  27. #define ZT_NETWORKCONFIG_HPP
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <vector>
  32. #include <stdexcept>
  33. #include <algorithm>
  34. #include "../include/ZeroTierOne.h"
  35. #include "Constants.hpp"
  36. #include "Buffer.hpp"
  37. #include "InetAddress.hpp"
  38. #include "MulticastGroup.hpp"
  39. #include "Address.hpp"
  40. #include "CertificateOfMembership.hpp"
  41. #include "CertificateOfOwnership.hpp"
  42. #include "Capability.hpp"
  43. #include "Tag.hpp"
  44. #include "Dictionary.hpp"
  45. #include "Hashtable.hpp"
  46. #include "Identity.hpp"
  47. #include "Utils.hpp"
  48. #include "Trace.hpp"
  49. /**
  50. * Default maximum time delta for COMs, tags, and capabilities
  51. *
  52. * The current value is two hours, providing ample time for a controller to
  53. * experience fail-over, etc.
  54. */
  55. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA 7200000ULL
  56. /**
  57. * Default minimum credential TTL and maxDelta for COM timestamps
  58. *
  59. * This is just slightly over three minutes and provides three retries for
  60. * all currently online members to refresh.
  61. */
  62. #define ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MIN_MAX_DELTA 185000ULL
  63. /**
  64. * Flag: enable broadcast
  65. */
  66. #define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0000000000000002ULL
  67. /**
  68. * Flag: enable IPv6 NDP emulation for certain V6 address patterns
  69. */
  70. #define ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION 0x0000000000000004ULL
  71. /**
  72. * Flag: result of unrecognized MATCH entries in a rules table: match if set, no-match if clear
  73. */
  74. #define ZT_NETWORKCONFIG_FLAG_RULES_RESULT_OF_UNSUPPORTED_MATCH 0x0000000000000008ULL
  75. /**
  76. * Flag: disable frame compression
  77. */
  78. #define ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION 0x0000000000000010ULL
  79. /**
  80. * Device can bridge to other Ethernet networks and gets unknown recipient multicasts
  81. */
  82. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE 0x0000020000000000ULL
  83. /**
  84. * Anchors are stable devices on this network that can act like roots when none are up
  85. */
  86. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR 0x0000040000000000ULL
  87. namespace ZeroTier {
  88. // Dictionary capacity needed for max size network config
  89. #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))
  90. // Dictionary capacity needed for max size network meta-data
  91. #define ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY 1024
  92. // Network config version
  93. #define ZT_NETWORKCONFIG_VERSION 7
  94. // Fields for meta-data sent with network config requests
  95. // Network config version
  96. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION "v"
  97. // Protocol version (see Packet.hpp)
  98. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION "pv"
  99. // Software vendor
  100. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_VENDOR "vend"
  101. // Software major version
  102. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  103. // Software minor version
  104. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  105. // Software revision
  106. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  107. // Rules engine revision
  108. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV "revr"
  109. // Maximum number of rules per network this node can accept
  110. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_RULES "mr"
  111. // Maximum number of capabilities this node can accept
  112. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_CAPABILITIES "mc"
  113. // Maximum number of rules per capability this node can accept
  114. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_CAPABILITY_RULES "mcr"
  115. // Maximum number of tags this node can accept
  116. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS "mt"
  117. // Network join authorization token (if any)
  118. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_AUTH "a"
  119. // Network configuration meta-data flags
  120. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS "f"
  121. // These dictionary keys are short so they don't take up much room.
  122. // By convention we use upper case for binary blobs, but it doesn't really matter.
  123. // network config version
  124. #define ZT_NETWORKCONFIG_DICT_KEY_VERSION "v"
  125. // network ID
  126. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  127. // integer(hex)
  128. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  129. // integer(hex)
  130. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  131. // address of member
  132. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  133. // remote trace target
  134. #define ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET "tt"
  135. // remote trace level
  136. #define ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL "tl"
  137. // flags(hex)
  138. #define ZT_NETWORKCONFIG_DICT_KEY_FLAGS "f"
  139. // integer(hex)
  140. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  141. // network type (hex)
  142. #define ZT_NETWORKCONFIG_DICT_KEY_TYPE "t"
  143. // text
  144. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  145. // network MTU
  146. #define ZT_NETWORKCONFIG_DICT_KEY_MTU "mtu"
  147. // credential time max delta in ms
  148. #define ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA "ctmd"
  149. // binary serialized certificate of membership
  150. #define ZT_NETWORKCONFIG_DICT_KEY_COM "C"
  151. // specialists (binary array of uint64_t)
  152. #define ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS "S"
  153. // routes (binary blob)
  154. #define ZT_NETWORKCONFIG_DICT_KEY_ROUTES "RT"
  155. // static IPs (binary blob)
  156. #define ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS "I"
  157. // rules (binary blob)
  158. #define ZT_NETWORKCONFIG_DICT_KEY_RULES "R"
  159. // capabilities (binary blobs)
  160. #define ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES "CAP"
  161. // tags (binary blobs)
  162. #define ZT_NETWORKCONFIG_DICT_KEY_TAGS "TAG"
  163. // tags (binary blobs)
  164. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP "COO"
  165. // Legacy fields -- these are obsoleted but are included when older clients query
  166. // boolean (now a flag)
  167. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD "eb"
  168. // IP/bits[,IP/bits,...]
  169. // Note that IPs that end in all zeroes are routes with no assignment in them.
  170. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD "v4s"
  171. // IP/bits[,IP/bits,...]
  172. // Note that IPs that end in all zeroes are routes with no assignment in them.
  173. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD "v6s"
  174. // 0/1
  175. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD "p"
  176. // integer(hex)[,integer(hex),...]
  177. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD "et"
  178. // string-serialized CertificateOfMembership
  179. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD "com"
  180. // node[,node,...]
  181. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD "ab"
  182. // node;IP/port[,node;IP/port]
  183. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD "rl"
  184. // End legacy fields
  185. /**
  186. * Network configuration received from network controller nodes
  187. *
  188. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  189. * without locks.
  190. */
  191. class NetworkConfig
  192. {
  193. public:
  194. NetworkConfig() :
  195. networkId(0),
  196. timestamp(0),
  197. credentialTimeMaxDelta(0),
  198. revision(0),
  199. issuedTo(),
  200. remoteTraceTarget(),
  201. flags(0),
  202. remoteTraceLevel(Trace::LEVEL_NORMAL),
  203. mtu(0),
  204. multicastLimit(0),
  205. specialistCount(0),
  206. routeCount(0),
  207. staticIpCount(0),
  208. ruleCount(0),
  209. capabilityCount(0),
  210. tagCount(0),
  211. certificateOfOwnershipCount(0),
  212. type(ZT_NETWORK_TYPE_PRIVATE)
  213. {
  214. name[0] = 0;
  215. }
  216. /**
  217. * Write this network config to a dictionary for transport
  218. *
  219. * @param d Dictionary
  220. * @param includeLegacy If true, include legacy fields for old node versions
  221. * @return True if dictionary was successfully created, false if e.g. overflow
  222. */
  223. bool toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const;
  224. /**
  225. * Read this network config from a dictionary
  226. *
  227. * @param d Dictionary (non-const since it might be modified during parse, should not be used after call)
  228. * @return True if dictionary was valid and network config successfully initialized
  229. */
  230. bool fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d);
  231. /**
  232. * @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
  233. */
  234. inline bool enableBroadcast() const { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0); }
  235. /**
  236. * @return True if IPv6 NDP emulation should be allowed for certain "magic" IPv6 address patterns
  237. */
  238. inline bool ndpEmulation() const { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION) != 0); }
  239. /**
  240. * @return True if frames should not be compressed
  241. */
  242. inline bool disableCompression() const
  243. {
  244. #ifndef ZT_DISABLE_COMPRESSION
  245. return ((this->flags & ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION) != 0);
  246. #else
  247. /* Compression is disabled for libzt builds since it causes non-obvious chaotic
  248. interference with lwIP's TCP congestion algorithm. Compression is also disabled
  249. for some NAS builds due to the usage of low-performance processors in certain
  250. older and budget models. */
  251. return false;
  252. #endif
  253. }
  254. /**
  255. * @return Network type is public (no access control)
  256. */
  257. inline bool isPublic() const { return (this->type == ZT_NETWORK_TYPE_PUBLIC); }
  258. /**
  259. * @return Network type is private (certificate access control)
  260. */
  261. inline bool isPrivate() const { return (this->type == ZT_NETWORK_TYPE_PRIVATE); }
  262. /**
  263. * @return ZeroTier addresses of devices on this network designated as active bridges
  264. */
  265. inline std::vector<Address> activeBridges() const
  266. {
  267. std::vector<Address> r;
  268. for(unsigned int i=0;i<specialistCount;++i) {
  269. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  270. r.push_back(Address(specialists[i]));
  271. }
  272. return r;
  273. }
  274. inline unsigned int activeBridges(Address ab[ZT_MAX_NETWORK_SPECIALISTS]) const
  275. {
  276. unsigned int c = 0;
  277. for(unsigned int i=0;i<specialistCount;++i) {
  278. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  279. ab[c++] = specialists[i];
  280. }
  281. return c;
  282. }
  283. inline bool isActiveBridge(const Address &a) const
  284. {
  285. for(unsigned int i=0;i<specialistCount;++i) {
  286. if (((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)&&(a == specialists[i]))
  287. return true;
  288. }
  289. return false;
  290. }
  291. inline std::vector<Address> anchors() const
  292. {
  293. std::vector<Address> r;
  294. for(unsigned int i=0;i<specialistCount;++i) {
  295. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0)
  296. r.push_back(Address(specialists[i]));
  297. }
  298. return r;
  299. }
  300. inline std::vector<Address> alwaysContactAddresses() const
  301. {
  302. std::vector<Address> r;
  303. for(unsigned int i=0;i<specialistCount;++i) {
  304. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0)
  305. r.push_back(Address(specialists[i]));
  306. }
  307. return r;
  308. }
  309. inline unsigned int alwaysContactAddresses(Address ac[ZT_MAX_NETWORK_SPECIALISTS]) const
  310. {
  311. unsigned int c = 0;
  312. for(unsigned int i=0;i<specialistCount;++i) {
  313. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0)
  314. ac[c++] = specialists[i];
  315. }
  316. return c;
  317. }
  318. inline void alwaysContactAddresses(Hashtable< Address,std::vector<InetAddress> > &a) const
  319. {
  320. for(unsigned int i=0;i<specialistCount;++i) {
  321. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0) {
  322. a[Address(specialists[i])];
  323. }
  324. }
  325. }
  326. /**
  327. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  328. * @return True if this network allows bridging
  329. */
  330. inline bool permitsBridging(const Address &fromPeer) const
  331. {
  332. for(unsigned int i=0;i<specialistCount;++i) {
  333. if ((fromPeer == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
  334. return true;
  335. }
  336. return false;
  337. }
  338. inline operator bool() const { return (networkId != 0); }
  339. inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  340. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  341. /**
  342. * Add a specialist or mask flags if already present
  343. *
  344. * This masks the existing flags if the specialist is already here or adds
  345. * it otherwise.
  346. *
  347. * @param a Address of specialist
  348. * @param f Flags (OR of specialist role/type flags)
  349. * @return True if successfully masked or added
  350. */
  351. inline bool addSpecialist(const Address &a,const uint64_t f)
  352. {
  353. const uint64_t aint = a.toInt();
  354. for(unsigned int i=0;i<specialistCount;++i) {
  355. if ((specialists[i] & 0xffffffffffULL) == aint) {
  356. specialists[i] |= f;
  357. return true;
  358. }
  359. }
  360. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  361. specialists[specialistCount++] = f | aint;
  362. return true;
  363. }
  364. return false;
  365. }
  366. const Capability *capability(const uint32_t id) const
  367. {
  368. for(unsigned int i=0;i<capabilityCount;++i) {
  369. if (capabilities[i].id() == id)
  370. return &(capabilities[i]);
  371. }
  372. return (Capability *)0;
  373. }
  374. const Tag *tag(const uint32_t id) const
  375. {
  376. for(unsigned int i=0;i<tagCount;++i) {
  377. if (tags[i].id() == id)
  378. return &(tags[i]);
  379. }
  380. return (Tag *)0;
  381. }
  382. /**
  383. * Network ID that this configuration applies to
  384. */
  385. uint64_t networkId;
  386. /**
  387. * Controller-side time of config generation/issue
  388. */
  389. int64_t timestamp;
  390. /**
  391. * Max difference between timestamp and tag/capability timestamp
  392. */
  393. int64_t credentialTimeMaxDelta;
  394. /**
  395. * Controller-side revision counter for this configuration
  396. */
  397. uint64_t revision;
  398. /**
  399. * Address of device to which this config is issued
  400. */
  401. Address issuedTo;
  402. /**
  403. * If non-NULL, remote traces related to this network are sent here
  404. */
  405. Address remoteTraceTarget;
  406. /**
  407. * Flags (64-bit)
  408. */
  409. uint64_t flags;
  410. /**
  411. * Remote trace level
  412. */
  413. Trace::Level remoteTraceLevel;
  414. /**
  415. * Network MTU
  416. */
  417. unsigned int mtu;
  418. /**
  419. * Maximum number of recipients per multicast (not including active bridges)
  420. */
  421. unsigned int multicastLimit;
  422. /**
  423. * Number of specialists
  424. */
  425. unsigned int specialistCount;
  426. /**
  427. * Number of routes
  428. */
  429. unsigned int routeCount;
  430. /**
  431. * Number of ZT-managed static IP assignments
  432. */
  433. unsigned int staticIpCount;
  434. /**
  435. * Number of rule table entries
  436. */
  437. unsigned int ruleCount;
  438. /**
  439. * Number of capabilities
  440. */
  441. unsigned int capabilityCount;
  442. /**
  443. * Number of tags
  444. */
  445. unsigned int tagCount;
  446. /**
  447. * Number of certificates of ownership
  448. */
  449. unsigned int certificateOfOwnershipCount;
  450. /**
  451. * Specialist devices
  452. *
  453. * For each entry the least significant 40 bits are the device's ZeroTier
  454. * address and the most significant 24 bits are flags indicating its role.
  455. */
  456. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  457. /**
  458. * Statically defined "pushed" routes (including default gateways)
  459. */
  460. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  461. /**
  462. * Static IP assignments
  463. */
  464. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  465. /**
  466. * Base network rules
  467. */
  468. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  469. /**
  470. * Capabilities for this node on this network, in ascending order of capability ID
  471. */
  472. Capability capabilities[ZT_MAX_NETWORK_CAPABILITIES];
  473. /**
  474. * Tags for this node on this network, in ascending order of tag ID
  475. */
  476. Tag tags[ZT_MAX_NETWORK_TAGS];
  477. /**
  478. * Certificates of ownership for this network member
  479. */
  480. CertificateOfOwnership certificatesOfOwnership[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  481. /**
  482. * Network type (currently just public or private)
  483. */
  484. ZT_VirtualNetworkType type;
  485. /**
  486. * Network short name or empty string if not defined
  487. */
  488. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  489. /**
  490. * Certificate of membership (for private networks)
  491. */
  492. CertificateOfMembership com;
  493. };
  494. } // namespace ZeroTier
  495. #endif