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