NetworkConfig.hpp 15 KB

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