Capability.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_CAPABILITY_HPP
  14. #define ZT_CAPABILITY_HPP
  15. #include "../include/ZeroTierOne.h"
  16. #include "Address.hpp"
  17. #include "Buffer.hpp"
  18. #include "C25519.hpp"
  19. #include "Constants.hpp"
  20. #include "Credential.hpp"
  21. #include "Identity.hpp"
  22. #include "Utils.hpp"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. /**
  29. * A set of grouped and signed network flow rules
  30. *
  31. * On the sending side the sender does the following for each packet:
  32. *
  33. * (1) Evaluates its capabilities in ascending order of ID to determine
  34. * which capability allows it to transmit this packet.
  35. * (2) If it has not done so lately, it then sends this capability to the
  36. * receiving peer ("presents" it).
  37. * (3) The sender then sends the packet.
  38. *
  39. * On the receiving side the receiver evaluates the capabilities presented
  40. * by the sender. If any valid un-expired capability allows this packet it
  41. * is accepted.
  42. *
  43. * Note that this is after evaluation of network scope rules and only if
  44. * network scope rules do not deliver an explicit match.
  45. *
  46. * Capabilities support a chain of custody. This is currently unused but
  47. * in the future would allow the publication of capabilities that can be
  48. * handed off between nodes. Limited transferability of capabilities is
  49. * a feature of true capability based security.
  50. */
  51. class Capability : public Credential {
  52. public:
  53. static inline Credential::Type credentialType()
  54. {
  55. return Credential::CREDENTIAL_TYPE_CAPABILITY;
  56. }
  57. Capability() : _nwid(0), _ts(0), _id(0), _maxCustodyChainLength(0), _ruleCount(0)
  58. {
  59. memset(_rules, 0, sizeof(_rules));
  60. memset(_custody, 0, sizeof(_custody));
  61. }
  62. /**
  63. * @param id Capability ID
  64. * @param nwid Network ID
  65. * @param ts Timestamp (at controller)
  66. * @param mccl Maximum custody chain length (1 to create non-transferable capability)
  67. * @param rules Network flow rules for this capability
  68. * @param ruleCount Number of flow rules
  69. */
  70. Capability(uint32_t id, uint64_t nwid, int64_t ts, unsigned int mccl, const ZT_VirtualNetworkRule* rules, unsigned int ruleCount)
  71. : _nwid(nwid)
  72. , _ts(ts)
  73. , _id(id)
  74. , _maxCustodyChainLength((mccl > 0) ? ((mccl < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) ? mccl : (unsigned int)ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) : 1)
  75. , _ruleCount((ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES)
  76. {
  77. if (_ruleCount > 0) {
  78. memcpy(_rules, rules, sizeof(ZT_VirtualNetworkRule) * _ruleCount);
  79. }
  80. }
  81. /**
  82. * @return Rules -- see ruleCount() for size of array
  83. */
  84. inline const ZT_VirtualNetworkRule* rules() const
  85. {
  86. return _rules;
  87. }
  88. /**
  89. * @return Number of rules in rules()
  90. */
  91. inline unsigned int ruleCount() const
  92. {
  93. return _ruleCount;
  94. }
  95. /**
  96. * @return ID and evaluation order of this capability in network
  97. */
  98. inline uint32_t id() const
  99. {
  100. return _id;
  101. }
  102. /**
  103. * @return Network ID for which this capability was issued
  104. */
  105. inline uint64_t networkId() const
  106. {
  107. return _nwid;
  108. }
  109. /**
  110. * @return Timestamp
  111. */
  112. inline int64_t timestamp() const
  113. {
  114. return _ts;
  115. }
  116. /**
  117. * @return Last 'to' address in chain of custody
  118. */
  119. inline Address issuedTo() const
  120. {
  121. Address i2;
  122. for (unsigned int i = 0; i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH; ++i) {
  123. if (! _custody[i].to) {
  124. return i2;
  125. }
  126. else {
  127. i2 = _custody[i].to;
  128. }
  129. }
  130. return i2;
  131. }
  132. /**
  133. * Sign this capability and add signature to its chain of custody
  134. *
  135. * If this returns false, this object should be considered to be
  136. * in an undefined state and should be discarded. False can be returned
  137. * if there is no more room for signatures (max chain length reached)
  138. * or if the 'from' identity does not include a secret key to allow
  139. * it to sign anything.
  140. *
  141. * @param from Signing identity (must have secret)
  142. * @param to Recipient of this signature
  143. * @return True if signature successful and chain of custody appended
  144. */
  145. inline bool sign(const Identity& from, const Address& to)
  146. {
  147. try {
  148. for (unsigned int i = 0; ((i < _maxCustodyChainLength) && (i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)); ++i) {
  149. if (! (_custody[i].to)) {
  150. Buffer<(sizeof(Capability) * 2)> tmp;
  151. this->serialize(tmp, true);
  152. _custody[i].to = to;
  153. _custody[i].from = from.address();
  154. _custody[i].signature = from.sign(tmp.data(), tmp.size());
  155. return true;
  156. }
  157. }
  158. }
  159. catch (...) {
  160. }
  161. return false;
  162. }
  163. /**
  164. * Verify this capability's chain of custody and signatures
  165. *
  166. * @param RR Runtime environment to provide for peer lookup, etc.
  167. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  168. */
  169. int verify(const RuntimeEnvironment* RR, void* tPtr) const;
  170. template <unsigned int C> static inline void serializeRules(Buffer<C>& b, const ZT_VirtualNetworkRule* rules, unsigned int ruleCount)
  171. {
  172. for (unsigned int i = 0; i < ruleCount; ++i) {
  173. // Each rule consists of its 8-bit type followed by the size of that type's
  174. // field followed by field data. The inclusion of the size will allow non-supported
  175. // rules to be ignored but still parsed.
  176. b.append((uint8_t)rules[i].t);
  177. switch ((ZT_VirtualNetworkRuleType)(rules[i].t & 0x3f)) {
  178. default:
  179. b.append((uint8_t)0);
  180. break;
  181. case ZT_NETWORK_RULE_ACTION_TEE:
  182. case ZT_NETWORK_RULE_ACTION_WATCH:
  183. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  184. b.append((uint8_t)14);
  185. b.append((uint64_t)rules[i].v.fwd.address);
  186. b.append((uint32_t)rules[i].v.fwd.flags);
  187. b.append((uint16_t)rules[i].v.fwd.length); // unused for redirect
  188. break;
  189. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  190. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  191. b.append((uint8_t)5);
  192. Address(rules[i].v.zt).appendTo(b);
  193. break;
  194. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  195. b.append((uint8_t)2);
  196. b.append((uint16_t)rules[i].v.vlanId);
  197. break;
  198. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  199. b.append((uint8_t)1);
  200. b.append((uint8_t)rules[i].v.vlanPcp);
  201. break;
  202. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  203. b.append((uint8_t)1);
  204. b.append((uint8_t)rules[i].v.vlanDei);
  205. break;
  206. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  207. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  208. b.append((uint8_t)6);
  209. b.append(rules[i].v.mac, 6);
  210. break;
  211. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  212. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  213. b.append((uint8_t)5);
  214. b.append(&(rules[i].v.ipv4.ip), 4);
  215. b.append((uint8_t)rules[i].v.ipv4.mask);
  216. break;
  217. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  218. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  219. b.append((uint8_t)17);
  220. b.append(rules[i].v.ipv6.ip, 16);
  221. b.append((uint8_t)rules[i].v.ipv6.mask);
  222. break;
  223. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  224. b.append((uint8_t)3);
  225. b.append((uint8_t)rules[i].v.ipTos.mask);
  226. b.append((uint8_t)rules[i].v.ipTos.value[0]);
  227. b.append((uint8_t)rules[i].v.ipTos.value[1]);
  228. break;
  229. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  230. b.append((uint8_t)1);
  231. b.append((uint8_t)rules[i].v.ipProtocol);
  232. break;
  233. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  234. b.append((uint8_t)2);
  235. b.append((uint16_t)rules[i].v.etherType);
  236. break;
  237. case ZT_NETWORK_RULE_MATCH_ICMP:
  238. b.append((uint8_t)3);
  239. b.append((uint8_t)rules[i].v.icmp.type);
  240. b.append((uint8_t)rules[i].v.icmp.code);
  241. b.append((uint8_t)rules[i].v.icmp.flags);
  242. break;
  243. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  244. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  245. b.append((uint8_t)4);
  246. b.append((uint16_t)rules[i].v.port[0]);
  247. b.append((uint16_t)rules[i].v.port[1]);
  248. break;
  249. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  250. b.append((uint8_t)8);
  251. b.append((uint64_t)rules[i].v.characteristics);
  252. break;
  253. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  254. b.append((uint8_t)4);
  255. b.append((uint16_t)rules[i].v.frameSize[0]);
  256. b.append((uint16_t)rules[i].v.frameSize[1]);
  257. break;
  258. case ZT_NETWORK_RULE_MATCH_RANDOM:
  259. b.append((uint8_t)4);
  260. b.append((uint32_t)rules[i].v.randomProbability);
  261. break;
  262. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  263. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  264. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  265. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  266. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL:
  267. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  268. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER:
  269. b.append((uint8_t)8);
  270. b.append((uint32_t)rules[i].v.tag.id);
  271. b.append((uint32_t)rules[i].v.tag.value);
  272. break;
  273. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE:
  274. b.append((uint8_t)19);
  275. b.append((uint64_t)rules[i].v.intRange.start);
  276. b.append((uint64_t)(rules[i].v.intRange.start + (uint64_t)rules[i].v.intRange.end)); // more future-proof
  277. b.append((uint16_t)rules[i].v.intRange.idx);
  278. b.append((uint8_t)rules[i].v.intRange.format);
  279. break;
  280. }
  281. }
  282. }
  283. template <unsigned int C> static inline void deserializeRules(const Buffer<C>& b, unsigned int& p, ZT_VirtualNetworkRule* rules, unsigned int& ruleCount, const unsigned int maxRuleCount)
  284. {
  285. while ((ruleCount < maxRuleCount) && (p < b.size())) {
  286. rules[ruleCount].t = (uint8_t)b[p++];
  287. const unsigned int fieldLen = (unsigned int)b[p++];
  288. switch ((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x3f)) {
  289. default:
  290. break;
  291. case ZT_NETWORK_RULE_ACTION_TEE:
  292. case ZT_NETWORK_RULE_ACTION_WATCH:
  293. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  294. rules[ruleCount].v.fwd.address = b.template at<uint64_t>(p);
  295. rules[ruleCount].v.fwd.flags = b.template at<uint32_t>(p + 8);
  296. rules[ruleCount].v.fwd.length = b.template at<uint16_t>(p + 12);
  297. break;
  298. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  299. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  300. rules[ruleCount].v.zt = Address(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH).toInt();
  301. break;
  302. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  303. rules[ruleCount].v.vlanId = b.template at<uint16_t>(p);
  304. break;
  305. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  306. rules[ruleCount].v.vlanPcp = (uint8_t)b[p];
  307. break;
  308. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  309. rules[ruleCount].v.vlanDei = (uint8_t)b[p];
  310. break;
  311. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  312. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  313. memcpy(rules[ruleCount].v.mac, b.field(p, 6), 6);
  314. break;
  315. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  316. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  317. memcpy(&(rules[ruleCount].v.ipv4.ip), b.field(p, 4), 4);
  318. rules[ruleCount].v.ipv4.mask = (uint8_t)b[p + 4];
  319. break;
  320. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  321. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  322. memcpy(rules[ruleCount].v.ipv6.ip, b.field(p, 16), 16);
  323. rules[ruleCount].v.ipv6.mask = (uint8_t)b[p + 16];
  324. break;
  325. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  326. rules[ruleCount].v.ipTos.mask = (uint8_t)b[p];
  327. rules[ruleCount].v.ipTos.value[0] = (uint8_t)b[p + 1];
  328. rules[ruleCount].v.ipTos.value[1] = (uint8_t)b[p + 2];
  329. break;
  330. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  331. rules[ruleCount].v.ipProtocol = (uint8_t)b[p];
  332. break;
  333. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  334. rules[ruleCount].v.etherType = b.template at<uint16_t>(p);
  335. break;
  336. case ZT_NETWORK_RULE_MATCH_ICMP:
  337. rules[ruleCount].v.icmp.type = (uint8_t)b[p];
  338. rules[ruleCount].v.icmp.code = (uint8_t)b[p + 1];
  339. rules[ruleCount].v.icmp.flags = (uint8_t)b[p + 2];
  340. break;
  341. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  342. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  343. rules[ruleCount].v.port[0] = b.template at<uint16_t>(p);
  344. rules[ruleCount].v.port[1] = b.template at<uint16_t>(p + 2);
  345. break;
  346. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  347. rules[ruleCount].v.characteristics = b.template at<uint64_t>(p);
  348. break;
  349. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  350. rules[ruleCount].v.frameSize[0] = b.template at<uint16_t>(p);
  351. rules[ruleCount].v.frameSize[1] = b.template at<uint16_t>(p + 2);
  352. break;
  353. case ZT_NETWORK_RULE_MATCH_RANDOM:
  354. rules[ruleCount].v.randomProbability = b.template at<uint32_t>(p);
  355. break;
  356. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  357. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  358. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  359. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  360. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL:
  361. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  362. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER:
  363. rules[ruleCount].v.tag.id = b.template at<uint32_t>(p);
  364. rules[ruleCount].v.tag.value = b.template at<uint32_t>(p + 4);
  365. break;
  366. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE:
  367. rules[ruleCount].v.intRange.start = b.template at<uint64_t>(p);
  368. rules[ruleCount].v.intRange.end = (uint32_t)(b.template at<uint64_t>(p + 8) - rules[ruleCount].v.intRange.start);
  369. rules[ruleCount].v.intRange.idx = b.template at<uint16_t>(p + 16);
  370. rules[ruleCount].v.intRange.format = (uint8_t)b[p + 18];
  371. break;
  372. }
  373. p += fieldLen;
  374. ++ruleCount;
  375. }
  376. }
  377. template <unsigned int C> inline void serialize(Buffer<C>& b, const bool forSign = false) const
  378. {
  379. if (forSign) {
  380. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  381. }
  382. // These are the same between Tag and Capability
  383. b.append(_nwid);
  384. b.append(_ts);
  385. b.append(_id);
  386. b.append((uint16_t)_ruleCount);
  387. serializeRules(b, _rules, _ruleCount);
  388. b.append((uint8_t)_maxCustodyChainLength);
  389. if (! forSign) {
  390. for (unsigned int i = 0;; ++i) {
  391. if ((i < _maxCustodyChainLength) && (i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) && (_custody[i].to)) {
  392. _custody[i].to.appendTo(b);
  393. _custody[i].from.appendTo(b);
  394. b.append((uint8_t)1); // 1 == Ed25519 signature
  395. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  396. b.append(_custody[i].signature.data, ZT_C25519_SIGNATURE_LEN);
  397. }
  398. else {
  399. b.append((unsigned char)0, ZT_ADDRESS_LENGTH); // zero 'to' terminates chain
  400. break;
  401. }
  402. }
  403. }
  404. // This is the size of any additional fields, currently 0.
  405. b.append((uint16_t)0);
  406. if (forSign) {
  407. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  408. }
  409. }
  410. template <unsigned int C> inline unsigned int deserialize(const Buffer<C>& b, unsigned int startAt = 0)
  411. {
  412. *this = Capability();
  413. unsigned int p = startAt;
  414. _nwid = b.template at<uint64_t>(p);
  415. p += 8;
  416. _ts = b.template at<uint64_t>(p);
  417. p += 8;
  418. _id = b.template at<uint32_t>(p);
  419. p += 4;
  420. const unsigned int rc = b.template at<uint16_t>(p);
  421. p += 2;
  422. if (rc > ZT_MAX_CAPABILITY_RULES) {
  423. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  424. }
  425. deserializeRules(b, p, _rules, _ruleCount, rc);
  426. _maxCustodyChainLength = (unsigned int)b[p++];
  427. if ((_maxCustodyChainLength < 1) || (_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)) {
  428. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  429. }
  430. for (unsigned int i = 0;; ++i) {
  431. const Address to(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  432. p += ZT_ADDRESS_LENGTH;
  433. if (! to) {
  434. break;
  435. }
  436. if ((i >= _maxCustodyChainLength) || (i >= ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)) {
  437. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  438. }
  439. _custody[i].to = to;
  440. _custody[i].from.setTo(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  441. p += ZT_ADDRESS_LENGTH;
  442. if (b[p++] == 1) {
  443. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN) {
  444. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  445. }
  446. p += 2;
  447. memcpy(_custody[i].signature.data, b.field(p, ZT_C25519_SIGNATURE_LEN), ZT_C25519_SIGNATURE_LEN);
  448. p += ZT_C25519_SIGNATURE_LEN;
  449. }
  450. else {
  451. p += 2 + b.template at<uint16_t>(p);
  452. }
  453. }
  454. p += 2 + b.template at<uint16_t>(p);
  455. if (p > b.size()) {
  456. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  457. }
  458. return (p - startAt);
  459. }
  460. // Provides natural sort order by ID
  461. inline bool operator<(const Capability& c) const
  462. {
  463. return (_id < c._id);
  464. }
  465. inline bool operator==(const Capability& c) const
  466. {
  467. return (memcmp(this, &c, sizeof(Capability)) == 0);
  468. }
  469. inline bool operator!=(const Capability& c) const
  470. {
  471. return (memcmp(this, &c, sizeof(Capability)) != 0);
  472. }
  473. private:
  474. uint64_t _nwid;
  475. int64_t _ts;
  476. uint32_t _id;
  477. unsigned int _maxCustodyChainLength;
  478. unsigned int _ruleCount;
  479. ZT_VirtualNetworkRule _rules[ZT_MAX_CAPABILITY_RULES];
  480. struct {
  481. Address to;
  482. Address from;
  483. C25519::Signature signature;
  484. } _custody[ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH];
  485. };
  486. } // namespace ZeroTier
  487. #endif