NetworkConfig.hpp 22 KB

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