NetworkConfig.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 ZeroTier-managed static IPs assigned to this device on this network
  239. */
  240. inline std::vector<InetAddress> staticIps() const
  241. {
  242. std::vector<InetAddress> r;
  243. for(unsigned int i=0;i<_staticIpCount;++i)
  244. r.push_back(_staticIps[i]);
  245. return r;
  246. }
  247. /**
  248. * @return ZeroTier addresses of devices on this network designated as active bridges
  249. */
  250. inline std::vector<Address> activeBridges() const
  251. {
  252. std::vector<Address> r;
  253. for(unsigned int i=0;i<_specialistCount;++i) {
  254. if ((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  255. r.push_back(Address(_specialists[i]));
  256. }
  257. return r;
  258. }
  259. /**
  260. * Look up a static physical address for a given ZeroTier address
  261. *
  262. * @param zt ZeroTier address
  263. * @param af Address family (e.g. AF_INET) or 0 for the first we find of any type
  264. * @return Physical address, if any
  265. */
  266. inline InetAddress staticPhysicalAddress(const Address &zt,unsigned int af) const
  267. {
  268. for(unsigned int i=0;i<_staticCount;++i) {
  269. if (_static[i].zt == zt) {
  270. if ((af == 0)||((unsigned int)_static[i].phy.ss_family == af))
  271. return _static[i].phy;
  272. }
  273. }
  274. return InetAddress();
  275. }
  276. /**
  277. * @return Network-preferred relays for this network (if none, only roots will be used)
  278. */
  279. inline std::vector<Relay> relays() const
  280. {
  281. std::vector<Relay> r;
  282. for(unsigned int i=0;i<_specialistCount;++i) {
  283. if ((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
  284. r.push_back(Relay());
  285. r.back().address = _specialists[i];
  286. r.back().phy4 = staticPhysicalAddress(r.back().address,AF_INET);
  287. r.back().phy6 = staticPhysicalAddress(r.back().address,AF_INET6);
  288. }
  289. }
  290. return r;
  291. }
  292. /**
  293. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  294. * @return True if this network allows bridging
  295. */
  296. inline bool permitsBridging(const Address &fromPeer) const
  297. {
  298. if ((_flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0)
  299. return true;
  300. for(unsigned int i=0;i<_specialistCount;++i) {
  301. if ((fromPeer == _specialists[i])&&((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
  302. return true;
  303. }
  304. return false;
  305. }
  306. /**
  307. * Iterate through relays efficiently
  308. *
  309. * @param ptr Value-result parameter -- start by initializing with zero, then call until return is null
  310. * @return Address of relay or NULL if no more
  311. */
  312. Address nextRelay(unsigned int &ptr) const
  313. {
  314. while (ptr < _specialistCount) {
  315. if ((_specialists[ptr] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
  316. return Address(_specialists[ptr]);
  317. }
  318. ++ptr;
  319. }
  320. return Address();
  321. }
  322. /**
  323. * @param zt ZeroTier address
  324. * @return True if this address is a relay
  325. */
  326. bool isRelay(const Address &zt) const
  327. {
  328. for(unsigned int i=0;i<_specialistCount;++i) {
  329. if ((zt == _specialists[i])&&((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0))
  330. return true;
  331. }
  332. return false;
  333. }
  334. /**
  335. * @return True if this network config is non-NULL
  336. */
  337. inline operator bool() const throw() { return (_nwid != 0); }
  338. inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  339. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  340. template<unsigned int C>
  341. inline void serialize(Buffer<C> &b) const
  342. {
  343. b.append((uint8_t)ZT_NETWORKCONFIG_V2_MARKER_BYTE);
  344. b.append((uint16_t)0); // version
  345. b.append((uint64_t)_nwid);
  346. b.append((uint64_t)_timestamp);
  347. b.append((uint64_t)_revision);
  348. _issuedTo.appendTo(b);
  349. b.append((uint32_t)_multicastLimit);
  350. b.append((uint32_t)_flags);
  351. b.append((uint8_t)_type);
  352. unsigned int nl = (unsigned int)strlen(_name);
  353. if (nl > 255) nl = 255; // sanity check
  354. b.append((uint8_t)nl);
  355. b.append((const void *)_name,nl);
  356. b.append((uint16_t)_specialistCount);
  357. for(unsigned int i=0;i<_specialistCount;++i)
  358. b.append((uint64_t)_specialists[i]);
  359. b.append((uint16_t)_routeCount);
  360. for(unsigned int i=0;i<_routeCount;++i) {
  361. reinterpret_cast<const InetAddress *>(&(_routes[i].target))->serialize(b);
  362. reinterpret_cast<const InetAddress *>(&(_routes[i].via))->serialize(b);
  363. }
  364. b.append((uint16_t)_staticIpCount);
  365. for(unsigned int i=0;i<_staticIpCount;++i)
  366. _staticIps[i].serialize(b);
  367. b.append((uint16_t)_staticCount);
  368. for(unsigned int i=0;i<_staticCount;++i) {
  369. _static[i].zt.appendTo(b);
  370. _static[i].phy.serialize(b);
  371. }
  372. b.append((uint16_t)_ruleCount);
  373. for(unsigned int i=0;i<_ruleCount;++i) {
  374. b.append((uint8_t)_rules[i].t);
  375. switch((ZT_VirtualNetworkRuleType)(_rules[i].t & 0x7f)) {
  376. //case ZT_NETWORK_RULE_ACTION_DROP:
  377. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  378. default:
  379. b.append((uint8_t)0);
  380. break;
  381. case ZT_NETWORK_RULE_ACTION_TEE:
  382. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  383. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  384. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  385. b.append((uint8_t)5);
  386. Address(_rules[i].v.zt).appendTo(b);
  387. break;
  388. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  389. b.append((uint8_t)2);
  390. b.append((uint16_t)_rules[i].v.vlanId);
  391. break;
  392. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  393. b.append((uint8_t)1);
  394. b.append((uint8_t)_rules[i].v.vlanPcp);
  395. break;
  396. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  397. b.append((uint8_t)1);
  398. b.append((uint8_t)_rules[i].v.vlanDei);
  399. break;
  400. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  401. b.append((uint8_t)2);
  402. b.append((uint16_t)_rules[i].v.etherType);
  403. break;
  404. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  405. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  406. b.append((uint8_t)6);
  407. b.append(_rules[i].v.mac,6);
  408. break;
  409. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  410. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  411. b.append((uint8_t)5);
  412. b.append(&(_rules[i].v.ipv4.ip),4);
  413. b.append((uint8_t)_rules[i].v.ipv4.mask);
  414. break;
  415. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  416. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  417. b.append((uint8_t)17);
  418. b.append(_rules[i].v.ipv6.ip,16);
  419. b.append((uint8_t)_rules[i].v.ipv6.mask);
  420. break;
  421. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  422. b.append((uint8_t)1);
  423. b.append((uint8_t)_rules[i].v.ipTos);
  424. break;
  425. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  426. b.append((uint8_t)1);
  427. b.append((uint8_t)_rules[i].v.ipProtocol);
  428. break;
  429. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  430. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  431. b.append((uint8_t)4);
  432. b.append((uint16_t)_rules[i].v.port[0]);
  433. b.append((uint16_t)_rules[i].v.port[1]);
  434. break;
  435. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  436. b.append((uint8_t)8);
  437. b.append((uint64_t)_rules[i].v.characteristics);
  438. break;
  439. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  440. b.append((uint8_t)4);
  441. b.append((uint16_t)_rules[i].v.frameSize[0]);
  442. b.append((uint16_t)_rules[i].v.frameSize[1]);
  443. break;
  444. }
  445. }
  446. _com.serialize(b);
  447. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  448. }
  449. template<unsigned int C>
  450. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  451. {
  452. memset(this,0,sizeof(NetworkConfig));
  453. unsigned int p = startAt;
  454. if (b[p++] != ZT_NETWORKCONFIG_V2_MARKER_BYTE)
  455. throw std::invalid_argument("unrecognized format");
  456. if (b.template at<uint16_t>(p) != 0)
  457. throw std::invalid_argument("unrecognized version");
  458. p += 2;
  459. _nwid = b.template at<uint64_t>(p); p += 8;
  460. _timestamp = b.template at<uint64_t>(p); p += 8;
  461. _revision = b.template at<uint64_t>(p); p += 8;
  462. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  463. _multicastLimit = (unsigned int)b.template at<uint32_t>(p); p += 4;
  464. _flags = (unsigned int)b.template at<uint32_t>(p); p += 4;
  465. _type = (ZT_VirtualNetworkType)b[p++];
  466. unsigned int nl = (unsigned int)b[p++];
  467. if (nl > ZT_MAX_NETWORK_SHORT_NAME_LENGTH)
  468. nl = ZT_MAX_NETWORK_SHORT_NAME_LENGTH;
  469. memcpy(_name,b.field(p,nl),nl);
  470. // _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1
  471. _specialistCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  472. if (_specialistCount > ZT_MAX_NETWORK_SPECIALISTS)
  473. throw std::invalid_argument("overflow (specialists)");
  474. for(unsigned int i=0;i<_specialistCount;++i) {
  475. _specialists[i] = b.template at<uint64_t>(p); p += 8;
  476. }
  477. _routeCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  478. if (_routeCount > ZT_MAX_NETWORK_ROUTES)
  479. throw std::invalid_argument("overflow (routes)");
  480. for(unsigned int i=0;i<_routeCount;++i) {
  481. p += reinterpret_cast<InetAddress *>(&(_routes[i].target))->deserialize(b,p);
  482. p += reinterpret_cast<InetAddress *>(&(_routes[i].via))->deserialize(b,p);
  483. }
  484. _staticIpCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  485. if (_staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES)
  486. throw std::invalid_argument("overflow (static IPs)");
  487. for(unsigned int i=0;i<_staticIpCount;++i) {
  488. p += _staticIps[i].deserialize(b,p);
  489. }
  490. _staticCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  491. if (_staticCount > ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES)
  492. throw std::invalid_argument("overflow (static addresses)");
  493. for(unsigned int i=0;i<_staticCount;++i) {
  494. _static[i].zt.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  495. p += _static[i].phy.deserialize(b,p);
  496. }
  497. _ruleCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
  498. if (_ruleCount > ZT_MAX_NETWORK_RULES)
  499. throw std::invalid_argument("overflow (rules)");
  500. for(unsigned int i=0;i<_ruleCount;++i) {
  501. _rules[i].t = (uint8_t)b[p++];
  502. unsigned int rlen = (unsigned int)b[p++];
  503. switch((ZT_VirtualNetworkRuleType)(_rules[i].t & 0x7f)) {
  504. //case ZT_NETWORK_RULE_ACTION_DROP:
  505. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  506. default:
  507. break;
  508. case ZT_NETWORK_RULE_ACTION_TEE:
  509. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  510. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  511. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: {
  512. Address tmp;
  513. tmp.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  514. _rules[i].v.zt = tmp.toInt();
  515. } break;
  516. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  517. _rules[i].v.vlanId = b.template at<uint16_t>(p);
  518. break;
  519. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  520. _rules[i].v.vlanPcp = (uint8_t)b[p];
  521. break;
  522. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  523. _rules[i].v.vlanDei = (uint8_t)b[p];
  524. break;
  525. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  526. _rules[i].v.etherType = b.template at<uint16_t>(p);
  527. break;
  528. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  529. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  530. memcpy(_rules[i].v.mac,b.field(p,6),6);
  531. break;
  532. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  533. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  534. memcpy(&(_rules[i].v.ipv4.ip),b.field(p,4),4);
  535. _rules[i].v.ipv4.mask = (uint8_t)b[p+4];
  536. break;
  537. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  538. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  539. memcpy(_rules[i].v.ipv6.ip,b.field(p,16),16);
  540. _rules[i].v.ipv6.mask = (uint8_t)b[p+16];
  541. break;
  542. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  543. _rules[i].v.ipTos = (uint8_t)b[p];
  544. break;
  545. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  546. _rules[i].v.ipProtocol = (uint8_t)b[p];
  547. break;
  548. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  549. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  550. _rules[i].v.port[0] = b.template at<uint16_t>(p);
  551. _rules[i].v.port[1] = b.template at<uint16_t>(p+2);
  552. break;
  553. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  554. _rules[i].v.characteristics = b.template at<uint64_t>(p);
  555. break;
  556. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  557. _rules[i].v.frameSize[0] = b.template at<uint16_t>(p);
  558. _rules[i].v.frameSize[1] = b.template at<uint16_t>(p+2);
  559. break;
  560. }
  561. p += rlen;
  562. }
  563. p += _com.deserialize(b,p);
  564. p += b.template at<uint16_t>(p) + 2;
  565. return (p - startAt);
  566. }
  567. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  568. void fromDictionary(const char *ds,unsigned int dslen);
  569. #endif
  570. protected: // protected so that a subclass can fill this out in network controller code
  571. uint64_t _nwid;
  572. uint64_t _timestamp;
  573. uint64_t _revision;
  574. Address _issuedTo;
  575. unsigned int _multicastLimit;
  576. unsigned int _flags;
  577. ZT_VirtualNetworkType _type;
  578. char _name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  579. // Special ZeroTier addresses -- most significant 40 bits are address, least 24 are specialist type flags
  580. uint64_t _specialists[ZT_MAX_NETWORK_SPECIALISTS];
  581. // ZeroTier-managed IPs and routing table entries and stuff
  582. ZT_VirtualNetworkRoute _routes[ZT_MAX_NETWORK_ROUTES];
  583. InetAddress _staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  584. // ZeroTier to physical static mappings
  585. struct {
  586. Address zt;
  587. InetAddress phy;
  588. } _static[ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES];
  589. // Network rules (only Ethernet type filtering is currently supported)
  590. ZT_VirtualNetworkRule _rules[ZT_MAX_NETWORK_RULES];
  591. unsigned int _specialistCount;
  592. unsigned int _routeCount;
  593. unsigned int _staticIpCount;
  594. unsigned int _staticCount;
  595. unsigned int _ruleCount;
  596. CertificateOfMembership _com;
  597. };
  598. } // namespace ZeroTier
  599. #endif