NetworkConfig.hpp 16 KB

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