NetworkConfig.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  34. #include "Dictionary.hpp"
  35. #include <string>
  36. #endif
  37. /**
  38. * Flag: allow passive bridging (experimental)
  39. */
  40. #define ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING 0x0001
  41. /**
  42. * Flag: enable broadcast
  43. */
  44. #define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0002
  45. /**
  46. * Device is a network preferred relay
  47. */
  48. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY 0x0000010000000000ULL
  49. /**
  50. * Device is an active bridge
  51. */
  52. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE 0x0000020000000000ULL
  53. /**
  54. * An anchor is a device that is willing to be one and has been online/stable for a long time on this network
  55. */
  56. #define ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR 0x0000040000000000ULL
  57. namespace ZeroTier {
  58. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  59. // Fields for meta-data sent with network config requests
  60. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  61. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  62. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  63. // These dictionary keys are short so they don't take up much room in
  64. // netconf response packets.
  65. // integer(hex)[,integer(hex),...]
  66. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  67. // network ID
  68. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  69. // integer(hex)
  70. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  71. // integer(hex)
  72. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  73. // address of member
  74. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  75. // integer(hex)
  76. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  77. // 0/1
  78. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
  79. // text
  80. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  81. // text
  82. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
  83. // IP/bits[,IP/bits,...]
  84. // Note that IPs that end in all zeroes are routes with no assignment in them.
  85. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  86. // IP/bits[,IP/bits,...]
  87. // Note that IPs that end in all zeroes are routes with no assignment in them.
  88. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  89. // serialized CertificateOfMembership
  90. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
  91. // 0/1
  92. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
  93. // 0/1
  94. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb"
  95. // node[,node,...]
  96. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab"
  97. // node;IP/port[,node;IP/port]
  98. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl"
  99. // IP/metric[,IP/metric,...]
  100. #define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw"
  101. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  102. /**
  103. * Network configuration received from network controller nodes
  104. *
  105. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  106. * without locks.
  107. */
  108. class NetworkConfig
  109. {
  110. public:
  111. /**
  112. * Network preferred relay with optional physical endpoint addresses
  113. *
  114. * This is used by the convenience relays() method.
  115. */
  116. struct Relay
  117. {
  118. Address address;
  119. InetAddress phy4,phy6;
  120. };
  121. /**
  122. * Create an instance of a NetworkConfig for the test network ID
  123. *
  124. * The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
  125. * "fake" network with no real controller and default options.
  126. *
  127. * @param self This node's ZT address
  128. * @return Configuration for test network ID
  129. */
  130. static inline NetworkConfig createTestNetworkConfig(const Address &self)
  131. {
  132. NetworkConfig nc;
  133. nc.networkId = ZT_TEST_NETWORK_ID;
  134. nc.timestamp = 1;
  135. nc.revision = 1;
  136. nc.issuedTo = self;
  137. nc.multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  138. nc.flags = ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
  139. nc.type = ZT_NETWORK_TYPE_PUBLIC;
  140. nc.rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
  141. nc.ruleCount = 1;
  142. Utils::snprintf(nc.name,sizeof(nc.name),"ZT_TEST_NETWORK");
  143. // Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
  144. // guarantee of uniqueness but collisions are unlikely.
  145. uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x
  146. if ((ip & 0x000000ff) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
  147. if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
  148. nc.staticIps[0] = InetAddress(Utils::hton(ip),8);
  149. // Assign an RFC4193-compliant IPv6 address -- will never collide
  150. nc.staticIps[1] = InetAddress::makeIpv6rfc4193(ZT_TEST_NETWORK_ID,self.toInt());
  151. nc.staticIpCount = 2;
  152. return nc;
  153. }
  154. NetworkConfig()
  155. {
  156. memset(this,0,sizeof(NetworkConfig));
  157. }
  158. NetworkConfig(const NetworkConfig &nc)
  159. {
  160. memcpy(this,&nc,sizeof(NetworkConfig));
  161. }
  162. inline NetworkConfig &operator=(const NetworkConfig &nc)
  163. {
  164. memcpy(this,&nc,sizeof(NetworkConfig));
  165. return *this;
  166. }
  167. /**
  168. * @param etherType Ethernet frame type to check
  169. * @return True if allowed on this network
  170. */
  171. inline bool permitsEtherType(unsigned int etherType) const
  172. {
  173. unsigned int et = 0;
  174. for(unsigned int i=0;i<ruleCount;++i) {
  175. ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
  176. if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
  177. et = rules[i].v.etherType;
  178. } else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
  179. if ((!et)||(et == etherType))
  180. return true;
  181. et = 0;
  182. }
  183. }
  184. return false;
  185. }
  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 Network type is public (no access control)
  196. */
  197. inline bool isPublic() const throw() { return (this->type == ZT_NETWORK_TYPE_PUBLIC); }
  198. /**
  199. * @return Network type is private (certificate access control)
  200. */
  201. inline bool isPrivate() const throw() { return (this->type == ZT_NETWORK_TYPE_PRIVATE); }
  202. /**
  203. * @return ZeroTier addresses of devices on this network designated as active bridges
  204. */
  205. inline std::vector<Address> activeBridges() const
  206. {
  207. std::vector<Address> r;
  208. for(unsigned int i=0;i<specialistCount;++i) {
  209. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  210. r.push_back(Address(specialists[i]));
  211. }
  212. return r;
  213. }
  214. /**
  215. * @return ZeroTier addresses of "anchor" devices on this network
  216. */
  217. inline std::vector<Address> anchors() const
  218. {
  219. std::vector<Address> r;
  220. for(unsigned int i=0;i<specialistCount;++i) {
  221. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0)
  222. r.push_back(Address(specialists[i]));
  223. }
  224. return r;
  225. }
  226. /**
  227. * Get pinned physical address for a given ZeroTier address, if any
  228. *
  229. * @param zt ZeroTier address
  230. * @param af Address family (e.g. AF_INET) or 0 for the first we find of any type
  231. * @return Physical address, if any
  232. */
  233. inline InetAddress findPinnedAddress(const Address &zt,unsigned int af) const
  234. {
  235. for(unsigned int i=0;i<pinnedCount;++i) {
  236. if (pinned[i].zt == zt) {
  237. if ((af == 0)||((unsigned int)pinned[i].phy.ss_family == af))
  238. return pinned[i].phy;
  239. }
  240. }
  241. return InetAddress();
  242. }
  243. /**
  244. * This gets network preferred relays with their static physical address if one is defined
  245. *
  246. * @return Network-preferred relays for this network (if none, only roots will be used)
  247. */
  248. inline std::vector<Relay> relays() const
  249. {
  250. std::vector<Relay> r;
  251. for(unsigned int i=0;i<specialistCount;++i) {
  252. if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
  253. r.push_back(Relay());
  254. r.back().address = specialists[i];
  255. r.back().phy4 = findPinnedAddress(r.back().address,AF_INET);
  256. r.back().phy6 = findPinnedAddress(r.back().address,AF_INET6);
  257. }
  258. }
  259. return r;
  260. }
  261. /**
  262. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  263. * @return True if this network allows bridging
  264. */
  265. inline bool permitsBridging(const Address &fromPeer) const
  266. {
  267. if ((flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0)
  268. return true;
  269. for(unsigned int i=0;i<specialistCount;++i) {
  270. if ((fromPeer == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
  271. return true;
  272. }
  273. return false;
  274. }
  275. /**
  276. * Iterate through relays efficiently
  277. *
  278. * @param ptr Value-result parameter -- start by initializing with zero, then call until return is null
  279. * @return Address of relay or NULL if no more
  280. */
  281. Address nextRelay(unsigned int &ptr) const
  282. {
  283. while (ptr < specialistCount) {
  284. if ((specialists[ptr] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
  285. return Address(specialists[ptr]);
  286. }
  287. ++ptr;
  288. }
  289. return Address();
  290. }
  291. /**
  292. * @param zt ZeroTier address
  293. * @return True if this address is a relay
  294. */
  295. bool isRelay(const Address &zt) const
  296. {
  297. for(unsigned int i=0;i<specialistCount;++i) {
  298. if ((zt == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0))
  299. return true;
  300. }
  301. return false;
  302. }
  303. /**
  304. * @return True if this network config is non-NULL
  305. */
  306. inline operator bool() const throw() { return (networkId != 0); }
  307. inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  308. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  309. template<unsigned int C>
  310. inline void serialize(Buffer<C> &b) const
  311. {
  312. b.append((uint16_t)1); // version
  313. b.append((uint64_t)networkId);
  314. b.append((uint64_t)timestamp);
  315. b.append((uint64_t)revision);
  316. issuedTo.appendTo(b);
  317. b.append((uint32_t)multicastLimit);
  318. b.append((uint32_t)flags);
  319. b.append((uint8_t)type);
  320. unsigned int nl = (unsigned int)strlen(name);
  321. if (nl > 255) nl = 255; // sanity check
  322. b.append((uint8_t)nl);
  323. b.append((const void *)name,nl);
  324. b.append((uint16_t)specialistCount);
  325. for(unsigned int i=0;i<specialistCount;++i)
  326. b.append((uint64_t)specialists[i]);
  327. b.append((uint16_t)routeCount);
  328. for(unsigned int i=0;i<routeCount;++i) {
  329. reinterpret_cast<const InetAddress *>(&(routes[i].target))->serialize(b);
  330. reinterpret_cast<const InetAddress *>(&(routes[i].via))->serialize(b);
  331. }
  332. b.append((uint16_t)staticIpCount);
  333. for(unsigned int i=0;i<staticIpCount;++i)
  334. staticIps[i].serialize(b);
  335. b.append((uint16_t)pinnedCount);
  336. for(unsigned int i=0;i<pinnedCount;++i) {
  337. pinned[i].zt.appendTo(b);
  338. pinned[i].phy.serialize(b);
  339. }
  340. b.append((uint16_t)ruleCount);
  341. for(unsigned int i=0;i<ruleCount;++i) {
  342. b.append((uint8_t)rules[i].t);
  343. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  344. //case ZT_NETWORK_RULE_ACTION_DROP:
  345. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  346. default:
  347. b.append((uint8_t)0);
  348. break;
  349. case ZT_NETWORK_RULE_ACTION_TEE:
  350. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  351. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  352. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  353. b.append((uint8_t)5);
  354. Address(rules[i].v.zt).appendTo(b);
  355. break;
  356. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  357. b.append((uint8_t)2);
  358. b.append((uint16_t)rules[i].v.vlanId);
  359. break;
  360. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  361. b.append((uint8_t)1);
  362. b.append((uint8_t)rules[i].v.vlanPcp);
  363. break;
  364. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  365. b.append((uint8_t)1);
  366. b.append((uint8_t)rules[i].v.vlanDei);
  367. break;
  368. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  369. b.append((uint8_t)2);
  370. b.append((uint16_t)rules[i].v.etherType);
  371. break;
  372. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  373. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  374. b.append((uint8_t)6);
  375. b.append(rules[i].v.mac,6);
  376. break;
  377. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  378. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  379. b.append((uint8_t)5);
  380. b.append(&(rules[i].v.ipv4.ip),4);
  381. b.append((uint8_t)rules[i].v.ipv4.mask);
  382. break;
  383. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  384. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  385. b.append((uint8_t)17);
  386. b.append(rules[i].v.ipv6.ip,16);
  387. b.append((uint8_t)rules[i].v.ipv6.mask);
  388. break;
  389. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  390. b.append((uint8_t)1);
  391. b.append((uint8_t)rules[i].v.ipTos);
  392. break;
  393. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  394. b.append((uint8_t)1);
  395. b.append((uint8_t)rules[i].v.ipProtocol);
  396. break;
  397. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  398. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  399. b.append((uint8_t)4);
  400. b.append((uint16_t)rules[i].v.port[0]);
  401. b.append((uint16_t)rules[i].v.port[1]);
  402. break;
  403. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  404. b.append((uint8_t)8);
  405. b.append((uint64_t)rules[i].v.characteristics);
  406. break;
  407. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  408. b.append((uint8_t)4);
  409. b.append((uint16_t)rules[i].v.frameSize[0]);
  410. b.append((uint16_t)rules[i].v.frameSize[1]);
  411. break;
  412. }
  413. }
  414. this->com.serialize(b);
  415. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  416. }
  417. template<unsigned int C>
  418. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  419. {
  420. memset(this,0,sizeof(NetworkConfig));
  421. unsigned int p = startAt;
  422. if (b.template at<uint16_t>(p) != 1)
  423. throw std::invalid_argument("unrecognized version");
  424. p += 2;
  425. networkId = b.template at<uint64_t>(p); p += 8;
  426. timestamp = b.template at<uint64_t>(p); p += 8;
  427. revision = b.template at<uint64_t>(p); p += 8;
  428. issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  429. multicastLimit = (unsigned int)b.template at<uint32_t>(p); p += 4;
  430. flags = (unsigned int)b.template at<uint32_t>(p); p += 4;
  431. type = (ZT_VirtualNetworkType)b[p++];
  432. unsigned int nl = (unsigned int)b[p++];
  433. memcpy(this->name,b.field(p,nl),std::max(nl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH));
  434. p += nl;
  435. // _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1
  436. specialistCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  437. if (specialistCount > ZT_MAX_NETWORK_SPECIALISTS)
  438. throw std::invalid_argument("overflow (specialists)");
  439. for(unsigned int i=0;i<specialistCount;++i) {
  440. specialists[i] = b.template at<uint64_t>(p); p += 8;
  441. }
  442. routeCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  443. if (routeCount > ZT_MAX_NETWORK_ROUTES)
  444. throw std::invalid_argument("overflow (routes)");
  445. for(unsigned int i=0;i<routeCount;++i) {
  446. p += reinterpret_cast<InetAddress *>(&(routes[i].target))->deserialize(b,p);
  447. p += reinterpret_cast<InetAddress *>(&(routes[i].via))->deserialize(b,p);
  448. }
  449. staticIpCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  450. if (staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES)
  451. throw std::invalid_argument("overflow (static IPs)");
  452. for(unsigned int i=0;i<staticIpCount;++i) {
  453. p += staticIps[i].deserialize(b,p);
  454. }
  455. pinnedCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  456. if (pinnedCount > ZT_MAX_NETWORK_PINNED)
  457. throw std::invalid_argument("overflow (static addresses)");
  458. for(unsigned int i=0;i<pinnedCount;++i) {
  459. pinned[i].zt.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  460. p += pinned[i].phy.deserialize(b,p);
  461. }
  462. ruleCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  463. if (ruleCount > ZT_MAX_NETWORK_RULES)
  464. throw std::invalid_argument("overflow (rules)");
  465. for(unsigned int i=0;i<ruleCount;++i) {
  466. rules[i].t = (uint8_t)b[p++];
  467. unsigned int rlen = (unsigned int)b[p++];
  468. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  469. //case ZT_NETWORK_RULE_ACTION_DROP:
  470. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  471. default:
  472. break;
  473. case ZT_NETWORK_RULE_ACTION_TEE:
  474. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  475. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  476. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: {
  477. Address tmp;
  478. tmp.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  479. rules[i].v.zt = tmp.toInt();
  480. } break;
  481. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  482. rules[i].v.vlanId = b.template at<uint16_t>(p);
  483. break;
  484. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  485. rules[i].v.vlanPcp = (uint8_t)b[p];
  486. break;
  487. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  488. rules[i].v.vlanDei = (uint8_t)b[p];
  489. break;
  490. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  491. rules[i].v.etherType = b.template at<uint16_t>(p);
  492. break;
  493. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  494. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  495. memcpy(rules[i].v.mac,b.field(p,6),6);
  496. break;
  497. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  498. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  499. memcpy(&(rules[i].v.ipv4.ip),b.field(p,4),4);
  500. rules[i].v.ipv4.mask = (uint8_t)b[p+4];
  501. break;
  502. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  503. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  504. memcpy(rules[i].v.ipv6.ip,b.field(p,16),16);
  505. rules[i].v.ipv6.mask = (uint8_t)b[p+16];
  506. break;
  507. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  508. rules[i].v.ipTos = (uint8_t)b[p];
  509. break;
  510. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  511. rules[i].v.ipProtocol = (uint8_t)b[p];
  512. break;
  513. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  514. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  515. rules[i].v.port[0] = b.template at<uint16_t>(p);
  516. rules[i].v.port[1] = b.template at<uint16_t>(p+2);
  517. break;
  518. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  519. rules[i].v.characteristics = b.template at<uint64_t>(p);
  520. break;
  521. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  522. rules[i].v.frameSize[0] = b.template at<uint16_t>(p);
  523. rules[i].v.frameSize[1] = b.template at<uint16_t>(p+2);
  524. break;
  525. }
  526. p += rlen;
  527. }
  528. p += this->com.deserialize(b,p);
  529. p += b.template at<uint16_t>(p) + 2;
  530. return (p - startAt);
  531. }
  532. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  533. void fromDictionary(const char *ds,unsigned int dslen);
  534. #endif
  535. /**
  536. * Network ID that this configuration applies to
  537. */
  538. uint64_t networkId;
  539. /**
  540. * Controller-side time of config generation/issue
  541. */
  542. uint64_t timestamp;
  543. /**
  544. * Controller-side revision counter for this configuration
  545. */
  546. uint64_t revision;
  547. /**
  548. * Address of device to which this config is issued
  549. */
  550. Address issuedTo;
  551. /**
  552. * Maximum number of recipients per multicast (not including active bridges)
  553. */
  554. unsigned int multicastLimit;
  555. /**
  556. * Flags (32-bit)
  557. */
  558. unsigned int flags;
  559. /**
  560. * Number of specialists
  561. */
  562. unsigned int specialistCount;
  563. /**
  564. * Number of routes
  565. */
  566. unsigned int routeCount;
  567. /**
  568. * Number of ZT-managed static IP assignments
  569. */
  570. unsigned int staticIpCount;
  571. /**
  572. * Number of pinned devices (devices with physical address hints)
  573. */
  574. unsigned int pinnedCount;
  575. /**
  576. * Number of rule table entries
  577. */
  578. unsigned int ruleCount;
  579. /**
  580. * Specialist devices
  581. *
  582. * For each entry the least significant 40 bits are the device's ZeroTier
  583. * address and the most significant 24 bits are flags indicating its role.
  584. */
  585. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  586. /**
  587. * Statically defined "pushed" routes (including default gateways)
  588. */
  589. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  590. /**
  591. * Static IP assignments
  592. */
  593. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  594. /**
  595. * Pinned devices with physical address hints
  596. *
  597. * These can be used to specify a physical address where a given device
  598. * can be reached. It's usually used with network relays (specialists).
  599. */
  600. struct {
  601. Address zt;
  602. InetAddress phy;
  603. } pinned[ZT_MAX_NETWORK_PINNED];
  604. /**
  605. * Rules table
  606. */
  607. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  608. /**
  609. * Network type (currently just public or private)
  610. */
  611. ZT_VirtualNetworkType type;
  612. /**
  613. * Network short name or empty string if not defined
  614. */
  615. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  616. /**
  617. * Certficiate of membership (for private networks)
  618. */
  619. CertificateOfMembership com;
  620. };
  621. } // namespace ZeroTier
  622. #endif