NetworkConfig.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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((uint64_t)flags);
  318. b.append((uint32_t)multicastLimit);
  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. b.append((uint16_t)routes[i].flags);
  332. b.append((uint16_t)routes[i].metric);
  333. }
  334. b.append((uint16_t)staticIpCount);
  335. for(unsigned int i=0;i<staticIpCount;++i)
  336. staticIps[i].serialize(b);
  337. b.append((uint16_t)pinnedCount);
  338. for(unsigned int i=0;i<pinnedCount;++i) {
  339. pinned[i].zt.appendTo(b);
  340. pinned[i].phy.serialize(b);
  341. }
  342. b.append((uint16_t)ruleCount);
  343. for(unsigned int i=0;i<ruleCount;++i) {
  344. b.append((uint8_t)rules[i].t);
  345. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  346. //case ZT_NETWORK_RULE_ACTION_DROP:
  347. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  348. default:
  349. b.append((uint8_t)0);
  350. break;
  351. case ZT_NETWORK_RULE_ACTION_TEE:
  352. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  353. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  354. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  355. b.append((uint8_t)5);
  356. Address(rules[i].v.zt).appendTo(b);
  357. break;
  358. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  359. b.append((uint8_t)2);
  360. b.append((uint16_t)rules[i].v.vlanId);
  361. break;
  362. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  363. b.append((uint8_t)1);
  364. b.append((uint8_t)rules[i].v.vlanPcp);
  365. break;
  366. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  367. b.append((uint8_t)1);
  368. b.append((uint8_t)rules[i].v.vlanDei);
  369. break;
  370. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  371. b.append((uint8_t)2);
  372. b.append((uint16_t)rules[i].v.etherType);
  373. break;
  374. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  375. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  376. b.append((uint8_t)6);
  377. b.append(rules[i].v.mac,6);
  378. break;
  379. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  380. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  381. b.append((uint8_t)5);
  382. b.append(&(rules[i].v.ipv4.ip),4);
  383. b.append((uint8_t)rules[i].v.ipv4.mask);
  384. break;
  385. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  386. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  387. b.append((uint8_t)17);
  388. b.append(rules[i].v.ipv6.ip,16);
  389. b.append((uint8_t)rules[i].v.ipv6.mask);
  390. break;
  391. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  392. b.append((uint8_t)1);
  393. b.append((uint8_t)rules[i].v.ipTos);
  394. break;
  395. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  396. b.append((uint8_t)1);
  397. b.append((uint8_t)rules[i].v.ipProtocol);
  398. break;
  399. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  400. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  401. b.append((uint8_t)4);
  402. b.append((uint16_t)rules[i].v.port[0]);
  403. b.append((uint16_t)rules[i].v.port[1]);
  404. break;
  405. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  406. b.append((uint8_t)8);
  407. b.append((uint64_t)rules[i].v.characteristics);
  408. break;
  409. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  410. b.append((uint8_t)4);
  411. b.append((uint16_t)rules[i].v.frameSize[0]);
  412. b.append((uint16_t)rules[i].v.frameSize[1]);
  413. break;
  414. case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE:
  415. b.append((uint8_t)8);
  416. b.append((uint32_t)rules[i].v.tcpseq[0]);
  417. b.append((uint32_t)rules[i].v.tcpseq[1]);
  418. break;
  419. }
  420. }
  421. this->com.serialize(b);
  422. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  423. }
  424. template<unsigned int C>
  425. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  426. {
  427. memset(this,0,sizeof(NetworkConfig));
  428. unsigned int p = startAt;
  429. if (b.template at<uint16_t>(p) != 1)
  430. throw std::invalid_argument("unrecognized version");
  431. p += 2;
  432. networkId = b.template at<uint64_t>(p); p += 8;
  433. timestamp = b.template at<uint64_t>(p); p += 8;
  434. revision = b.template at<uint64_t>(p); p += 8;
  435. issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  436. flags = b.template at<uint64_t>(p); p += 8;
  437. multicastLimit = (unsigned int)b.template at<uint32_t>(p); p += 4;
  438. type = (ZT_VirtualNetworkType)b[p++];
  439. unsigned int nl = (unsigned int)b[p++];
  440. memcpy(this->name,b.field(p,nl),std::min(nl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH));
  441. p += nl;
  442. // _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1
  443. specialistCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  444. if (specialistCount > ZT_MAX_NETWORK_SPECIALISTS)
  445. throw std::invalid_argument("overflow (specialists)");
  446. for(unsigned int i=0;i<specialistCount;++i) {
  447. specialists[i] = b.template at<uint64_t>(p); p += 8;
  448. }
  449. routeCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  450. if (routeCount > ZT_MAX_NETWORK_ROUTES)
  451. throw std::invalid_argument("overflow (routes)");
  452. for(unsigned int i=0;i<routeCount;++i) {
  453. p += reinterpret_cast<InetAddress *>(&(routes[i].target))->deserialize(b,p);
  454. p += reinterpret_cast<InetAddress *>(&(routes[i].via))->deserialize(b,p);
  455. routes[i].flags = b.template at<uint16_t>(p); p += 2;
  456. routes[i].metric = b.template at<uint16_t>(p); p += 2;
  457. }
  458. staticIpCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  459. if (staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES)
  460. throw std::invalid_argument("overflow (static IPs)");
  461. for(unsigned int i=0;i<staticIpCount;++i) {
  462. p += staticIps[i].deserialize(b,p);
  463. }
  464. pinnedCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  465. if (pinnedCount > ZT_MAX_NETWORK_PINNED)
  466. throw std::invalid_argument("overflow (static addresses)");
  467. for(unsigned int i=0;i<pinnedCount;++i) {
  468. pinned[i].zt.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  469. p += pinned[i].phy.deserialize(b,p);
  470. }
  471. ruleCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  472. if (ruleCount > ZT_MAX_NETWORK_RULES)
  473. throw std::invalid_argument("overflow (rules)");
  474. for(unsigned int i=0;i<ruleCount;++i) {
  475. rules[i].t = (uint8_t)b[p++];
  476. unsigned int rlen = (unsigned int)b[p++];
  477. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  478. //case ZT_NETWORK_RULE_ACTION_DROP:
  479. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  480. default:
  481. break;
  482. case ZT_NETWORK_RULE_ACTION_TEE:
  483. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  484. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  485. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: {
  486. Address tmp;
  487. tmp.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  488. rules[i].v.zt = tmp.toInt();
  489. } break;
  490. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  491. rules[i].v.vlanId = b.template at<uint16_t>(p);
  492. break;
  493. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  494. rules[i].v.vlanPcp = (uint8_t)b[p];
  495. break;
  496. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  497. rules[i].v.vlanDei = (uint8_t)b[p];
  498. break;
  499. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  500. rules[i].v.etherType = b.template at<uint16_t>(p);
  501. break;
  502. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  503. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  504. memcpy(rules[i].v.mac,b.field(p,6),6);
  505. break;
  506. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  507. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  508. memcpy(&(rules[i].v.ipv4.ip),b.field(p,4),4);
  509. rules[i].v.ipv4.mask = (uint8_t)b[p+4];
  510. break;
  511. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  512. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  513. memcpy(rules[i].v.ipv6.ip,b.field(p,16),16);
  514. rules[i].v.ipv6.mask = (uint8_t)b[p+16];
  515. break;
  516. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  517. rules[i].v.ipTos = (uint8_t)b[p];
  518. break;
  519. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  520. rules[i].v.ipProtocol = (uint8_t)b[p];
  521. break;
  522. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  523. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  524. rules[i].v.port[0] = b.template at<uint16_t>(p);
  525. rules[i].v.port[1] = b.template at<uint16_t>(p+2);
  526. break;
  527. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  528. rules[i].v.characteristics = b.template at<uint64_t>(p);
  529. break;
  530. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  531. rules[i].v.frameSize[0] = b.template at<uint16_t>(p);
  532. rules[i].v.frameSize[1] = b.template at<uint16_t>(p+2);
  533. break;
  534. case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE:
  535. rules[i].v.tcpseq[0] = b.template at<uint32_t>(p);
  536. rules[i].v.tcpseq[1] = b.template at<uint32_t>(p + 4);
  537. break;
  538. }
  539. p += rlen;
  540. }
  541. p += this->com.deserialize(b,p);
  542. p += b.template at<uint16_t>(p) + 2;
  543. return (p - startAt);
  544. }
  545. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  546. void fromDictionary(const char *ds,unsigned int dslen);
  547. #endif
  548. /*
  549. inline void dump() const
  550. {
  551. printf("networkId==%.16llx\n",networkId);
  552. printf("timestamp==%llu\n",timestamp);
  553. printf("revision==%llu\n",revision);
  554. printf("issuedTo==%.10llx\n",issuedTo.toInt());
  555. printf("multicastLimit==%u\n",multicastLimit);
  556. printf("flags=%.8lx\n",(unsigned long)flags);
  557. printf("specialistCount==%u\n",specialistCount);
  558. for(unsigned int i=0;i<specialistCount;++i)
  559. printf(" specialists[%u]==%.16llx\n",i,specialists[i]);
  560. printf("routeCount==%u\n",routeCount);
  561. for(unsigned int i=0;i<routeCount;++i) {
  562. printf(" routes[i].target==%s\n",reinterpret_cast<const struct sockaddr_storage *>(&(routes[i].target))->toString().c_str());
  563. printf(" routes[i].via==%s\n",reinterpret_cast<const struct sockaddr_storage *>(&(routes[i].via))->toIpString().c_str());
  564. }
  565. printf("staticIpCount==%u\n",staticIpCount);
  566. for(unsigned int i=0;i<staticIpCount;++i)
  567. printf(" staticIps[i]==%s\n",staticIps[i].toString().c_str());
  568. printf("pinnedCount==%u\n",pinnedCount);
  569. for(unsigned int i=0;i<pinnedCount;++i) {
  570. printf(" pinned[i].zt==%s\n",pinned[i].zt->toString().c_str());
  571. printf(" pinned[i].phy==%s\n",pinned[i].zt->toString().c_str());
  572. }
  573. printf("ruleCount==%u\n",ruleCount);
  574. printf("name==%s\n",name);
  575. printf("com==%s\n",com.toString().c_str());
  576. }
  577. */
  578. /**
  579. * Network ID that this configuration applies to
  580. */
  581. uint64_t networkId;
  582. /**
  583. * Controller-side time of config generation/issue
  584. */
  585. uint64_t timestamp;
  586. /**
  587. * Controller-side revision counter for this configuration
  588. */
  589. uint64_t revision;
  590. /**
  591. * Address of device to which this config is issued
  592. */
  593. Address issuedTo;
  594. /**
  595. * Flags (64-bit)
  596. */
  597. uint64_t flags;
  598. /**
  599. * Maximum number of recipients per multicast (not including active bridges)
  600. */
  601. unsigned int multicastLimit;
  602. /**
  603. * Number of specialists
  604. */
  605. unsigned int specialistCount;
  606. /**
  607. * Number of routes
  608. */
  609. unsigned int routeCount;
  610. /**
  611. * Number of ZT-managed static IP assignments
  612. */
  613. unsigned int staticIpCount;
  614. /**
  615. * Number of pinned devices (devices with physical address hints)
  616. */
  617. unsigned int pinnedCount;
  618. /**
  619. * Number of rule table entries
  620. */
  621. unsigned int ruleCount;
  622. /**
  623. * Specialist devices
  624. *
  625. * For each entry the least significant 40 bits are the device's ZeroTier
  626. * address and the most significant 24 bits are flags indicating its role.
  627. */
  628. uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
  629. /**
  630. * Statically defined "pushed" routes (including default gateways)
  631. */
  632. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  633. /**
  634. * Static IP assignments
  635. */
  636. InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  637. /**
  638. * Pinned devices with physical address hints
  639. *
  640. * These can be used to specify a physical address where a given device
  641. * can be reached. It's usually used with network relays (specialists).
  642. */
  643. struct {
  644. Address zt;
  645. InetAddress phy;
  646. } pinned[ZT_MAX_NETWORK_PINNED];
  647. /**
  648. * Rules table
  649. */
  650. ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
  651. /**
  652. * Network type (currently just public or private)
  653. */
  654. ZT_VirtualNetworkType type;
  655. /**
  656. * Network short name or empty string if not defined
  657. */
  658. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  659. /**
  660. * Certficiate of membership (for private networks)
  661. */
  662. CertificateOfMembership com;
  663. };
  664. } // namespace ZeroTier
  665. #endif