Protocol.hpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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_PROTOCOL_HPP
  14. #define ZT_PROTOCOL_HPP
  15. #include "Constants.hpp"
  16. #include "AES.hpp"
  17. #include "Salsa20.hpp"
  18. #include "Poly1305.hpp"
  19. #include "LZ4.hpp"
  20. #include "Buf.hpp"
  21. #include "Address.hpp"
  22. #include "Identity.hpp"
  23. // TODO: mlock
  24. /*
  25. * Core ZeroTier protocol packet formats ------------------------------------------------------------------------------
  26. *
  27. * Packet format:
  28. * <[8] 64-bit packet ID / crypto IV>
  29. * <[5] destination ZT address>
  30. * <[5] source ZT address>
  31. * <[1] outer visible flags, cipher, and hop count (bits: FFCCHHH)>
  32. * <[8] 64-bit MAC (or trusted path ID in trusted path mode)>
  33. * [... -- begin encryption envelope -- ...]
  34. * <[1] inner envelope flags (MS 3 bits) and verb (LS 5 bits)>
  35. * [... verb-specific payload ...]
  36. *
  37. * Packets smaller than 28 bytes are invalid and silently discarded.
  38. *
  39. * The hop count field is masked during message authentication computation
  40. * and is thus the only field that is mutable in transit. It's incremented
  41. * when roots or other nodes forward packets and exists to prevent infinite
  42. * forwarding loops and to detect direct paths.
  43. *
  44. * HELLO is normally sent in the clear with the POLY1305_NONE cipher suite
  45. * and with Poly1305 computed on plain text (Salsa20/12 is still used to
  46. * generate a one time use Poly1305 key). As of protocol version 11 HELLO
  47. * also includes a terminating HMAC (last 48 bytes) that significantly
  48. * hardens HELLO authentication beyond what a 64-bit MAC can guarantee.
  49. *
  50. * Fragmented packets begin with a packet header whose fragment bit (bit
  51. * 0x40 in the flags field) is set. This constitutes fragment zero. The
  52. * total number of expected fragments is contained in each subsequent
  53. * fragment packet. Unfragmented packets must not have the fragment bit
  54. * set or the receiver will expect at least one additional fragment.
  55. *
  56. * --
  57. *
  58. * Packet fragment format (fragments beyond 0):
  59. * <[8] packet ID of packet to which this fragment belongs>
  60. * <[5] destination ZT address>
  61. * <[1] 0xff here signals that this is a fragment>
  62. * <[1] total fragments (most significant 4 bits), fragment no (LS 4 bits)>
  63. * <[1] ZT hop count (least significant 3 bits; others are reserved)>
  64. * <[...] fragment data>
  65. *
  66. * The protocol supports a maximum of 16 fragments including fragment 0
  67. * which contains the full packet header (with fragment bit set). Fragments
  68. * thus always carry fragment numbers between 1 and 15. All fragments
  69. * belonging to the same packet must carry the same total fragment count in
  70. * the most significant 4 bits of the fragment numbering field.
  71. *
  72. * All fragments have the same packet ID and destination. The packet ID
  73. * doubles as the grouping identifier for fragment reassembly.
  74. *
  75. * Fragments do not carry their own packet MAC. The entire packet is
  76. * authenticated once it is assembled by the receiver. Incomplete packets
  77. * are discarded after a receiver configured period of time.
  78. *
  79. * --------------------------------------------------------------------------------------------------------------------
  80. */
  81. /*
  82. * Protocol versions
  83. *
  84. * 1 - 0.2.0 ... 0.2.5
  85. * 2 - 0.3.0 ... 0.4.5
  86. * + Added signature and originating peer to multicast frame
  87. * + Double size of multicast frame bloom filter
  88. * 3 - 0.5.0 ... 0.6.0
  89. * + Yet another multicast redesign
  90. * + New crypto completely changes key agreement cipher
  91. * 4 - 0.6.0 ... 1.0.6
  92. * + BREAKING CHANGE: New identity format based on hashcash design
  93. * 5 - 1.1.0 ... 1.1.5
  94. * + Supports echo
  95. * + Supports in-band world (root server definition) updates
  96. * + Clustering! (Though this will work with protocol v4 clients.)
  97. * + Otherwise backward compatible with protocol v4
  98. * 6 - 1.1.5 ... 1.1.10
  99. * + Network configuration format revisions including binary values
  100. * 7 - 1.1.10 ... 1.1.17
  101. * + Introduce trusted paths for local SDN use
  102. * 8 - 1.1.17 ... 1.2.0
  103. * + Multipart network configurations for large network configs
  104. * + Tags and Capabilities
  105. * + inline push of CertificateOfMembership deprecated
  106. * 9 - 1.2.0 ... 1.2.14
  107. * 10 - 1.4.0 ... 1.4.6
  108. * + Contained early pre-alpha versions of multipath, which are deprecated
  109. * 11 - 2.0.0 ... CURRENT
  110. * + New more WAN-efficient P2P-assisted multicast algorithm
  111. * + HELLO and OK(HELLO) include an extra HMAC to harden authentication
  112. * + HELLO and OK(HELLO) can carry structured meta-data
  113. * + Ephemeral keys for forward secrecy and limited key lifetime
  114. * + Old planet/moon stuff is DEAD! Independent roots are easier.
  115. * + AES encryption is now the default
  116. * + New combined Curve25519/NIST P-384 identity type (type 1)
  117. * + Short probe packets to reduce probe bandwidth
  118. * + Aggressive NAT traversal techniques for IPv4 symmetric NATs
  119. * + Remote diagnostics including rewrite of remote tracing
  120. */
  121. #define ZT_PROTO_VERSION 11
  122. /**
  123. * Minimum supported protocol version
  124. */
  125. #define ZT_PROTO_VERSION_MIN 8
  126. /**
  127. * Maximum allowed packet size (can technically be increased up to 16384)
  128. */
  129. #define ZT_PROTO_MAX_PACKET_LENGTH (ZT_MAX_PACKET_FRAGMENTS * ZT_MIN_UDP_MTU)
  130. /**
  131. * Minimum viable packet length (outer header + verb)
  132. */
  133. #define ZT_PROTO_MIN_PACKET_LENGTH 28
  134. /**
  135. * Index at which the encrypted section of a packet begins
  136. */
  137. #define ZT_PROTO_PACKET_ENCRYPTED_SECTION_START 27
  138. /**
  139. * Index at which packet payload begins (after verb)
  140. */
  141. #define ZT_PROTO_PACKET_PAYLOAD_START 28
  142. /**
  143. * Maximum hop count allowed by packet structure (3 bits, 0-7)
  144. *
  145. * This is a protocol constant. It's the maximum allowed by the length
  146. * of the hop counter -- three bits. A lower limit is specified as
  147. * the actual maximum hop count.
  148. */
  149. #define ZT_PROTO_MAX_HOPS 7
  150. /**
  151. * NONE/Poly1305 (using Salsa20/12 to generate poly1305 key)
  152. */
  153. #define ZT_PROTO_CIPHER_SUITE__POLY1305_NONE 0
  154. /**
  155. * Salsa2012/Poly1305
  156. */
  157. #define ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012 1
  158. /**
  159. * No encryption or authentication at all!
  160. *
  161. * This is used for trusted paths. The MAC field will contain the
  162. * 64-bit trusted path ID. Both sides of a link must be configured
  163. * to trust a given network with the same trusted path ID for this
  164. * to be used. It's a high performance mode designed for use on
  165. * secure LANs.
  166. */
  167. #define ZT_PROTO_CIPHER_SUITE__NONE 2
  168. /**
  169. * AES-GMAC-SIV (AES-256)
  170. */
  171. #define ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV 3
  172. /**
  173. * Minimum viable length for a fragment
  174. */
  175. #define ZT_PROTO_MIN_FRAGMENT_LENGTH 16
  176. /**
  177. * Magic number indicating a fragment if present at index 13
  178. */
  179. #define ZT_PROTO_PACKET_FRAGMENT_INDICATOR 0xff
  180. /**
  181. * Index at which fragment indicator is found in fragments
  182. */
  183. #define ZT_PROTO_PACKET_FRAGMENT_INDICATOR_INDEX 13
  184. /**
  185. * Index of flags field in regular packet headers
  186. */
  187. #define ZT_PROTO_PACKET_FLAGS_INDEX 18
  188. /**
  189. * Length of a probe packet
  190. */
  191. #define ZT_PROTO_PROBE_LENGTH 8
  192. /**
  193. * Index at which packet fragment payload starts
  194. */
  195. #define ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT ZT_PROTO_MIN_FRAGMENT_LENGTH
  196. /**
  197. * Header flag indicating that a packet is fragmented and more fragments should be expected
  198. */
  199. #define ZT_PROTO_FLAG_FRAGMENTED 0x40U
  200. /**
  201. * Mask for obtaining hops from the combined flags, cipher, and hops field
  202. */
  203. #define ZT_PROTO_FLAG_FIELD_HOPS_MASK 0x07U
  204. /**
  205. * Verb flag indicating payload is compressed with LZ4
  206. */
  207. #define ZT_PROTO_VERB_FLAG_COMPRESSED 0x80U
  208. /**
  209. * Mask to extract just the verb from the verb field, which also includes flags
  210. */
  211. #define ZT_PROTO_VERB_MASK 0x1fU
  212. /**
  213. * Key derivation function label for the keys used with HMAC-384 in HELLO
  214. *
  215. * With the KDF the 'iter' parameter is 0 for the key used for
  216. * HMAC in HELLO and 1 for the one used in OK(HELLO).
  217. */
  218. #define ZT_PROTO_KDF_KEY_LABEL_HELLO_HMAC 'H'
  219. /**
  220. * HELLO exchange meta-data: random 128-bit identifier for each running instance
  221. */
  222. #define ZT_PROTO_HELLO_NODE_META_INSTANCE_ID "i"
  223. /**
  224. * HELLO exchange meta-data: signed locator for this node
  225. */
  226. #define ZT_PROTO_HELLO_NODE_META_LOCATOR "l"
  227. /**
  228. * HELLO exchange meta-data: ephemeral C25519 public key
  229. */
  230. #define ZT_PROTO_HELLO_NODE_META_EPHEMERAL_C25519 "e0"
  231. /**
  232. * HELLO exchange meta-data: ephemeral NIST P-384 public key
  233. */
  234. #define ZT_PROTO_HELLO_NODE_META_EPHEMERAL_P384 "e1"
  235. /**
  236. * HELLO exchange meta-data: address(es) of nodes to whom this node will relay
  237. */
  238. #define ZT_PROTO_HELLO_NODE_META_NEIGHBORS "wr"
  239. /**
  240. * HELLO exchange meta-data: X coordinate of your node (sent in OK(HELLO))
  241. */
  242. #define ZT_PROTO_HELLO_NODE_META_LOCATION_X "gX"
  243. /**
  244. * HELLO exchange meta-data: Y coordinate of your node (sent in OK(HELLO))
  245. */
  246. #define ZT_PROTO_HELLO_NODE_META_LOCATION_Y "gY"
  247. /**
  248. * HELLO exchange meta-data: Z coordinate of your node (sent in OK(HELLO))
  249. */
  250. #define ZT_PROTO_HELLO_NODE_META_LOCATION_Z "gZ"
  251. /**
  252. * HELLO exchange meta-data: preferred cipher suite (may be ignored)
  253. */
  254. #define ZT_PROTO_HELLO_NODE_META_PREFERRED_CIPHER_SUITE "c"
  255. namespace ZeroTier {
  256. namespace Protocol {
  257. /**
  258. * Packet verb (message type)
  259. */
  260. enum Verb
  261. {
  262. /**
  263. * No operation
  264. *
  265. * This packet does nothing, but it is sometimes sent as a probe to
  266. * trigger a HELLO exchange as the code will attempt HELLO when it
  267. * receives a packet from an unidentified source.
  268. */
  269. VERB_NOP = 0x00,
  270. /**
  271. * Announcement of a node's existence and vitals:
  272. * <[1] protocol version>
  273. * <[1] software major version (LEGACY)>
  274. * <[1] software minor version (LEGACY)>
  275. * <[2] software revision (LEGACY)>
  276. * <[8] timestamp for determining latency (LEGACY)>
  277. * <[...] binary serialized identity>
  278. * <[...] physical destination address of packet (LEGACY)>
  279. * <[2] 16-bit reserved "encrypted zero" field (LEGACY)>
  280. * <[...] encrypted dictionary>
  281. * <[2] 16-bit length of preceding encrypted dictionary>
  282. * <[48] HMAC-SHA384 of plaintext packet (with hops masked to 0)>
  283. *
  284. * HELLO is sent to initiate a new pairing between two nodes.
  285. *
  286. * HELLO is the only packet ever sent without normal payload encryption,
  287. * though an inner encrypted envelope exists to obscure all fields that
  288. * do not need to be sent in the clear. HELLO's MAC field contains a
  289. * Poly1305 MAC for backward compatibility, and v2.x adds an additional
  290. * HMAC-SHA384 at the end for stronger authentication of sessions. HELLO
  291. * authentication is performed using the long-lived identity key only,
  292. * and the encryption of the inner dictionary field is done using a key
  293. * derived from this identity key explicitly for this purpose.
  294. *
  295. * The main payload of HELLO is the protocol version and the full identity
  296. * of the sender, which includes the sender's public key(s). An encrypted
  297. * dictionary (key/value store) is also included for additional information.
  298. * This is encrypted using AES-CTR with a derived key and using the final
  299. * 96 bits of the packet's HMAC-SHA384 as the CTR IV. (The HMAC authenticates
  300. * the packet prior to this field being encrypted, making this a SIV
  301. * construction much like AES-GMAC-SIV.)
  302. *
  303. * The length of the dictionary field is included immediately after it so
  304. * that it can be decrypted and the HMAC validated without performing any
  305. * parsing of anything else, since it's a good idea to authenticate any
  306. * message as early as possible in any secure protocol.
  307. *
  308. * V1.x will ignore the HMAC and dictionary fields as it doesn't understand
  309. * them, but the packet is constructed so that 1.x nodes will parse what
  310. * they need to communicate with 2.x nodes (without forward secrecy) as long
  311. * as we wish to support this.
  312. *
  313. * Several legacy fields are present as well for the benefit of 1.x nodes.
  314. * These will go away and become simple reserved space once 1.x is no longer
  315. * supported. Some are self-explanatory. The "encrypted zero" is rather
  316. * strange. It's a 16-bit zero value encrypted using Salsa20/12 and the
  317. * long-lived identity key shared by the two peers. It tells 1.x that an
  318. * old encrypted field is no longer there and that it should stop parsing
  319. * the packet at that point.
  320. *
  321. * The following fields are nearly always present and must exist to support
  322. * forward secrecy (in the case of the instance ID, keys, and key revision)
  323. * or federated root membership (in the case of the locator).
  324. *
  325. * TIMESTAMP - node's timestamp in milliseconds (supersedes legacy field)
  326. * INSTANCE_ID - a 64-bit unique value generated on each node start
  327. * EPHEMERAL_C25519 - an ephemeral Curve25519 public key
  328. * EPHEMERAL_P384 - an ephemeral NIST P-384 public key
  329. * EPHEMERAL_REVISION - 64-bit monotonically increasing per-instance counter
  330. * LOCATOR - signed record enumerating this node's trusted contact points
  331. *
  332. * The following optional fields may also be present:
  333. *
  334. * NAME - arbitrary short user-defined name for this node
  335. * CONTACT - arbitrary short contact information string for this node
  336. * NEIGHBORS - addresses of node(s) to whom we'll relay (mesh-like routing)
  337. * LOC_X, LOC_Y, LOC_Z - location relative to the nearest large center of mass
  338. * PEER_LOC_X, PEER_LOC_Y, PEER_LOC_Z - where sender thinks peer is located
  339. * SOFTWARE_VENDOR - short name or description of vendor, such as a URL
  340. * SOFTWARE_VERSION - major, minor, revision, and build, and 16-bit integers
  341. * PHYSICAL_DEST - serialized Endpoint to which this message was sent
  342. * VIRTUAL_DEST - ZeroTier address of first hop (if first hop wasn't destination)
  343. * COMPLIANCE - bit mask containing bits for e.g. a FIPS-compliant node
  344. *
  345. * A valid and successfully authenticated HELLO will generate the following
  346. * OK response. It contains an echo of the timestamp supplied by the
  347. * initiating peer, the protocol version, and a dictionary containing
  348. * the same information about the responding peer as the originating peer
  349. * sent.
  350. *
  351. * Note that OK(HELLO) as well as HELLO itself is always sent using the long
  352. * lived identity key, not ephemeral keys. This allows ephemeral re-keying to
  353. * always succeed if one side's ephemeral keys are out of date.
  354. *
  355. * OK payload:
  356. * <[8] timestamp echoed from original HELLO>
  357. * <[1] protocol version>
  358. * <[1] software major version (LEGACY)>
  359. * <[1] software minor version (LEGACY)>
  360. * <[2] software revision (LEGACY)>
  361. * <[...] physical destination address of packet (LEGACY)>
  362. * <[2] 16-bit reserved zero field (LEGACY)>
  363. * <[...] dictionary>
  364. * <[48] HMAC-SHA384 of plaintext packet (with hops masked to 0)>
  365. */
  366. VERB_HELLO = 0x01,
  367. /**
  368. * Error response:
  369. * <[1] in-re verb>
  370. * <[8] in-re packet ID>
  371. * <[1] error code>
  372. * <[...] error-dependent payload, may be empty>
  373. *
  374. * An ERROR that does not pertain to a specific packet will have its verb
  375. * set to VERB_NOP and its packet ID set to zero.
  376. */
  377. VERB_ERROR = 0x02,
  378. /**
  379. * Success response:
  380. * <[1] in-re verb>
  381. * <[8] in-re packet ID>
  382. * <[...] request-specific payload>
  383. */
  384. VERB_OK = 0x03,
  385. /**
  386. * Query an identity by address:
  387. * <[5] address to look up>
  388. * [<[...] additional addresses to look up>
  389. *
  390. * OK response payload:
  391. * <[...] identity>
  392. * <[...] locator>
  393. * [... additional identity/locator pairs]
  394. *
  395. * If the address is not found, no response is generated. The semantics
  396. * of WHOIS is similar to ARP and NDP in that persistent retrying can
  397. * be performed.
  398. *
  399. * It is possible for an identity but a null/empty locator to be returned
  400. * if no locator is known for a node. Older versions may omit the locator.
  401. */
  402. VERB_WHOIS = 0x04,
  403. /**
  404. * Relay-mediated NAT traversal or firewall punching initiation:
  405. * <[1] flags (unused, currently 0)>
  406. * <[5] ZeroTier address of peer that might be found at this address>
  407. * <[2] 16-bit protocol address port>
  408. * <[1] protocol address length / type>
  409. * <[...] protocol address (network byte order)>
  410. *
  411. * This is sent by a third party node to inform a node of where another
  412. * may be located. These are currently only allowed from roots.
  413. *
  414. * The protocol address format differs from the standard InetAddress
  415. * encoding for legacy reasons, but it's not hard to decode. The following
  416. * values are valid for the protocol address length (type) field:
  417. *
  418. * 4 - IPv4 IP address
  419. * 16 - IPv6 IP address
  420. * 255 - Endpoint object, unmarshaled in place (port ignored)
  421. *
  422. * No OK or ERROR is generated.
  423. */
  424. VERB_RENDEZVOUS = 0x05,
  425. /**
  426. * ZT-to-ZT unicast ethernet frame (shortened EXT_FRAME):
  427. * <[8] 64-bit network ID>
  428. * <[2] 16-bit ethertype>
  429. * <[...] ethernet payload>
  430. *
  431. * MAC addresses are derived from the packet's source and destination
  432. * ZeroTier addresses. This is a shortened EXT_FRAME that elides full
  433. * Ethernet framing and other optional flags and features when they
  434. * are not necessary.
  435. *
  436. * ERROR may be generated if a membership certificate is needed for a
  437. * closed network. Payload will be network ID.
  438. */
  439. VERB_FRAME = 0x06,
  440. /**
  441. * Full Ethernet frame with MAC addressing and optional fields:
  442. * <[8] 64-bit network ID>
  443. * <[1] flags>
  444. * <[6] destination MAC or all zero for destination node>
  445. * <[6] source MAC or all zero for node of origin>
  446. * <[2] 16-bit ethertype>
  447. * <[...] ethernet payload>
  448. *
  449. * Flags:
  450. * 0x01 - Certificate of network membership attached (DEPRECATED)
  451. * 0x02 - Most significant bit of subtype (see below)
  452. * 0x04 - Middle bit of subtype (see below)
  453. * 0x08 - Least significant bit of subtype (see below)
  454. * 0x10 - ACK requested in the form of OK(EXT_FRAME)
  455. *
  456. * Subtypes (0..7):
  457. * 0x0 - Normal frame (bridging can be determined by checking MAC)
  458. * 0x1 - TEEd outbound frame
  459. * 0x2 - REDIRECTed outbound frame
  460. * 0x3 - WATCHed outbound frame (TEE with ACK, ACK bit also set)
  461. * 0x4 - TEEd inbound frame
  462. * 0x5 - REDIRECTed inbound frame
  463. * 0x6 - WATCHed inbound frame
  464. * 0x7 - (reserved for future use)
  465. *
  466. * An extended frame carries full MAC addressing, making it a
  467. * superset of VERB_FRAME. If 0x20 is set then p2p or hub and
  468. * spoke multicast propagation is requested.
  469. *
  470. * OK payload (if ACK flag is set):
  471. * <[8] 64-bit network ID>
  472. * <[1] flags>
  473. * <[6] destination MAC or all zero for destination node>
  474. * <[6] source MAC or all zero for node of origin>
  475. * <[2] 16-bit ethertype>
  476. */
  477. VERB_EXT_FRAME = 0x07,
  478. /**
  479. * ECHO request (a.k.a. ping):
  480. * <[...] arbitrary payload>
  481. *
  482. * This generates OK with a copy of the transmitted payload. No ERROR
  483. * is generated. Response to ECHO requests is optional and ECHO may be
  484. * ignored if a node detects a possible flood.
  485. */
  486. VERB_ECHO = 0x08,
  487. /**
  488. * Announce interest in multicast group(s):
  489. * <[8] 64-bit network ID>
  490. * <[6] multicast Ethernet address>
  491. * <[4] multicast additional distinguishing information (ADI)>
  492. * [... additional tuples of network/address/adi ...]
  493. *
  494. * LIKEs may be sent to any peer, though a good implementation should
  495. * restrict them to peers on the same network they're for and to network
  496. * controllers and root servers. In the current network, root servers
  497. * will provide the service of final multicast cache.
  498. */
  499. VERB_MULTICAST_LIKE = 0x09,
  500. /**
  501. * Network credentials push:
  502. * [<[...] one or more certificates of membership>]
  503. * <[1] 0x00, null byte marking end of COM array>
  504. * <[2] 16-bit number of capabilities>
  505. * <[...] one or more serialized Capability>
  506. * <[2] 16-bit number of tags>
  507. * <[...] one or more serialized Tags>
  508. * <[2] 16-bit number of revocations>
  509. * <[...] one or more serialized Revocations>
  510. * <[2] 16-bit number of certificates of ownership>
  511. * <[...] one or more serialized CertificateOfOwnership>
  512. *
  513. * This can be sent by anyone at any time to push network credentials.
  514. * These will of course only be accepted if they are properly signed.
  515. * Credentials can be for any number of networks.
  516. *
  517. * The use of a zero byte to terminate the COM section is for legacy
  518. * backward compatibility. Newer fields are prefixed with a length.
  519. *
  520. * OK/ERROR are not generated.
  521. */
  522. VERB_NETWORK_CREDENTIALS = 0x0a,
  523. /**
  524. * Network configuration request:
  525. * <[8] 64-bit network ID>
  526. * <[2] 16-bit length of request meta-data dictionary>
  527. * <[...] string-serialized request meta-data>
  528. * <[8] 64-bit revision of netconf we currently have>
  529. * <[8] 64-bit timestamp of netconf we currently have>
  530. *
  531. * This message requests network configuration from a node capable of
  532. * providing it. Responses can be sent as OK(NETWORK_CONFIG_REQUEST)
  533. * or NETWORK_CONFIG messages. NETWORK_CONFIG can also be sent by
  534. * network controllers or other nodes unsolicited.
  535. *
  536. * OK response payload:
  537. * (same as VERB_NETWORK_CONFIG payload)
  538. *
  539. * ERROR response payload:
  540. * <[8] 64-bit network ID>
  541. */
  542. VERB_NETWORK_CONFIG_REQUEST = 0x0b,
  543. /**
  544. * Network configuration data push:
  545. * <[8] 64-bit network ID>
  546. * <[2] 16-bit length of network configuration dictionary chunk>
  547. * <[...] network configuration dictionary (may be incomplete)>
  548. * <[1] 8-bit flags>
  549. * <[8] 64-bit config update ID (should never be 0)>
  550. * <[4] 32-bit total length of assembled dictionary>
  551. * <[4] 32-bit index of chunk>
  552. * [ ... end signed portion ... ]
  553. * <[1] 8-bit reserved field (legacy)>
  554. * <[2] 16-bit length of chunk signature>
  555. * <[...] chunk signature>
  556. *
  557. * Network configurations can come from network controllers or theoretically
  558. * any other node, but each chunk must be signed by the network controller
  559. * that generated it originally. The config update ID is arbitrary and is merely
  560. * used by the receiver to group chunks. Chunk indexes must be sequential and
  561. * the total delivered chunks must yield a total network config equal to the
  562. * specified total length.
  563. *
  564. * Flags:
  565. * 0x01 - Use fast propagation -- rumor mill flood this chunk to other members
  566. *
  567. * An OK should be sent if the config is successfully received and
  568. * accepted.
  569. *
  570. * OK payload:
  571. * <[8] 64-bit network ID>
  572. * <[8] 64-bit config update ID>
  573. */
  574. VERB_NETWORK_CONFIG = 0x0c,
  575. /**
  576. * Request endpoints for multicast distribution:
  577. * <[8] 64-bit network ID>
  578. * <[1] flags>
  579. * <[6] MAC address of multicast group being queried>
  580. * <[4] 32-bit ADI for multicast group being queried>
  581. * <[4] 32-bit requested max number of multicast peers>
  582. *
  583. * This message asks a peer for additional known endpoints that have
  584. * LIKEd a given multicast group. It's sent when the sender wishes
  585. * to send multicast but does not have the desired number of recipient
  586. * peers.
  587. *
  588. * OK response payload: (multiple OKs can be generated)
  589. * <[8] 64-bit network ID>
  590. * <[6] MAC address of multicast group being queried>
  591. * <[4] 32-bit ADI for multicast group being queried>
  592. * <[4] 32-bit total number of known members in this multicast group>
  593. * <[2] 16-bit number of members enumerated in this packet>
  594. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  595. *
  596. * ERROR is not generated; queries that return no response are dropped.
  597. */
  598. VERB_MULTICAST_GATHER = 0x0d,
  599. /** *** DEPRECATED ***
  600. * Multicast frame:
  601. * <[8] 64-bit network ID>
  602. * <[1] flags>
  603. * [<[4] 32-bit implicit gather limit>]
  604. * [<[6] source MAC>]
  605. * <[6] destination MAC (multicast address)>
  606. * <[4] 32-bit multicast ADI (multicast address extension)>
  607. * <[2] 16-bit ethertype>
  608. * <[...] ethernet payload>
  609. *
  610. * Flags:
  611. * 0x01 - Network certificate of membership attached (DEPRECATED)
  612. * 0x02 - Implicit gather limit field is present
  613. * 0x04 - Source MAC is specified -- otherwise it's computed from sender
  614. * 0x08 - Please replicate (sent to multicast replicators)
  615. *
  616. * OK and ERROR responses are optional. OK may be generated if there are
  617. * implicit gather results or if the recipient wants to send its own
  618. * updated certificate of network membership to the sender. ERROR may be
  619. * generated if a certificate is needed or if multicasts to this group
  620. * are no longer wanted (multicast unsubscribe).
  621. *
  622. * OK response payload:
  623. * <[8] 64-bit network ID>
  624. * <[6] MAC address of multicast group>
  625. * <[4] 32-bit ADI for multicast group>
  626. * <[1] flags>
  627. * [<[...] network certificate of membership (DEPRECATED)>]
  628. * [<[...] implicit gather results if flag 0x01 is set>]
  629. *
  630. * OK flags (same bits as request flags):
  631. * 0x01 - OK includes certificate of network membership (DEPRECATED)
  632. * 0x02 - OK includes implicit gather results
  633. *
  634. * ERROR response payload:
  635. * <[8] 64-bit network ID>
  636. * <[6] multicast group MAC>
  637. * <[4] 32-bit multicast group ADI>
  638. */
  639. VERB_MULTICAST_FRAME_deprecated = 0x0e,
  640. /**
  641. * Push of potential endpoints for direct communication:
  642. * <[2] 16-bit number of paths>
  643. * <[...] paths>
  644. *
  645. * Path record format:
  646. * <[1] 8-bit path flags>
  647. * <[2] length of extended path characteristics or 0 for none>
  648. * <[...] extended path characteristics>
  649. * <[1] address type>
  650. * <[1] address record length in bytes>
  651. * <[...] address>
  652. *
  653. * Path flags:
  654. * 0x01 - Sender is likely behind a symmetric NAT
  655. * 0x02 - Use BFG1024 algorithm for symmetric NAT-t if conditions met
  656. *
  657. * The receiver may, upon receiving a push, attempt to establish a
  658. * direct link to one or more of the indicated addresses. It is the
  659. * responsibility of the sender to limit which peers it pushes direct
  660. * paths to to those with whom it has a trust relationship. The receiver
  661. * must obey any restrictions provided such as exclusivity or blacklists.
  662. * OK responses to this message are optional.
  663. *
  664. * Note that a direct path push does not imply that learned paths can't
  665. * be used unless they are blacklisted explicitly or unless flag 0x01
  666. * is set.
  667. *
  668. * OK and ERROR are not generated.
  669. */
  670. VERB_PUSH_DIRECT_PATHS = 0x10,
  671. /**
  672. * A message with arbitrary user-definable content:
  673. * <[8] 64-bit arbitrary message type ID>
  674. * [<[...] message payload>]
  675. *
  676. * This can be used to send arbitrary messages over VL1. It generates no
  677. * OK or ERROR and has no special semantics outside of whatever the user
  678. * (via the ZeroTier core API) chooses to give it.
  679. *
  680. * Message type IDs less than or equal to 65535 are reserved for use by
  681. * ZeroTier, Inc. itself. We recommend making up random ones for your own
  682. * implementations.
  683. */
  684. VERB_USER_MESSAGE = 0x14,
  685. /**
  686. * Encapsulate a ZeroTier packet for multicast distribution:
  687. * [... begin signed portion ...]
  688. * <[1] 8-bit flags>
  689. * <[5] 40-bit ZeroTier address of sender>
  690. * <[2] 16-bit length of inner payload>
  691. * <[1] inner payload verb>
  692. * <[...] inner payload data>
  693. * [... end signed portion ...]
  694. * <[2] 16-bit length of signature or 0 if un-signed>
  695. * [<[...] optional signature of multicast>]
  696. * <[...] address (min prefix) list>
  697. */
  698. VERB_MULTICAST = 0x16,
  699. /**
  700. * Encapsulate a full ZeroTier packet in another:
  701. * <[...] raw encapsulated packet>
  702. *
  703. * Encapsulation exists to enable secure relaying as opposed to the usual
  704. * "dumb" relaying. The latter is faster but secure relaying has roles
  705. * where endpoint privacy is desired. Multiply nested ENCAP packets
  706. * could allow ZeroTier to act as an onion router.
  707. *
  708. * When encapsulated packets are forwarded they do have their hop count
  709. * field incremented.
  710. */
  711. VERB_ENCAP = 0x17
  712. // protocol max: 0x1f
  713. };
  714. /**
  715. * Error codes used in ERROR packets.
  716. */
  717. enum ErrorCode
  718. {
  719. /* Invalid request */
  720. ERROR_INVALID_REQUEST = 0x01,
  721. /* Bad/unsupported protocol version */
  722. ERROR_BAD_PROTOCOL_VERSION = 0x02,
  723. /* Unknown object queried */
  724. ERROR_OBJ_NOT_FOUND = 0x03,
  725. /* Verb or use case not supported/enabled by this node */
  726. ERROR_UNSUPPORTED_OPERATION = 0x05,
  727. /* Network access denied; updated credentials needed */
  728. ERROR_NEED_MEMBERSHIP_CERTIFICATE = 0x06,
  729. /* Tried to join network, but you're not a member */
  730. ERROR_NETWORK_ACCESS_DENIED_ = 0x07, /* extra _ at end to avoid Windows name conflict */
  731. /* Cannot deliver a forwarded ZeroTier packet (for any reason) */
  732. ERROR_CANNOT_DELIVER = 0x09
  733. };
  734. /**
  735. * EXT_FRAME subtypes, which are packed into three bits in the flags field.
  736. *
  737. * This allows the node to know whether this is a normal frame or one generated
  738. * by a special tee or redirect type flow rule.
  739. */
  740. enum ExtFrameSubtype
  741. {
  742. EXT_FRAME_SUBTYPE_NORMAL = 0x0,
  743. EXT_FRAME_SUBTYPE_TEE_OUTBOUND = 0x1,
  744. EXT_FRAME_SUBTYPE_REDIRECT_OUTBOUND = 0x2,
  745. EXT_FRAME_SUBTYPE_WATCH_OUTBOUND = 0x3,
  746. EXT_FRAME_SUBTYPE_TEE_INBOUND = 0x4,
  747. EXT_FRAME_SUBTYPE_REDIRECT_INBOUND = 0x5,
  748. EXT_FRAME_SUBTYPE_WATCH_INBOUND = 0x6
  749. };
  750. /**
  751. * EXT_FRAME flags
  752. */
  753. enum ExtFrameFlag
  754. {
  755. /**
  756. * A certifiate of membership was included (no longer used but still accepted)
  757. */
  758. EXT_FRAME_FLAG_COM_ATTACHED_deprecated = 0x01,
  759. // bits 0x02, 0x04, and 0x08 are occupied by the 3-bit ExtFrameSubtype value.
  760. /**
  761. * An OK(EXT_FRAME) acknowledgement was requested by the sender.
  762. */
  763. EXT_FRAME_FLAG_ACK_REQUESTED = 0x10
  764. };
  765. /**
  766. * NETWORK_CONFIG (or OK(NETWORK_CONFIG_REQUEST)) flags
  767. */
  768. enum NetworkConfigFlag
  769. {
  770. /**
  771. * Indicates that this network config chunk should be fast propagated via rumor mill flooding.
  772. */
  773. NETWORK_CONFIG_FLAG_FAST_PROPAGATE = 0x01
  774. };
  775. /****************************************************************************/
  776. /*
  777. * These are bit-packed structures for rapid parsing of packets or at least
  778. * the fixed size headers thereof. Not all packet types have these as some
  779. * are full of variable length fields are are more easily parsed through
  780. * incremental decoding.
  781. *
  782. * All fields larger than one byte are in big-endian byte order on the wire.
  783. */
  784. /**
  785. * Normal packet header
  786. *
  787. * @tparam PT Packet payload type (default: uint8_t[])
  788. */
  789. ZT_PACKED_STRUCT(struct Header
  790. {
  791. uint64_t packetId;
  792. uint8_t destination[5];
  793. uint8_t source[5];
  794. uint8_t flags;
  795. uint64_t mac;
  796. // --- begin encrypted envelope ---
  797. uint8_t verb;
  798. });
  799. /**
  800. * Packet fragment header
  801. */
  802. ZT_PACKED_STRUCT(struct FragmentHeader
  803. {
  804. uint64_t packetId;
  805. uint8_t destination[5];
  806. uint8_t fragmentIndicator; // always 0xff for fragments
  807. uint8_t counts; // total: most significant four bits, number: least significant four bits
  808. uint8_t hops; // top 5 bits unused and must be zero
  809. });
  810. ZT_PACKED_STRUCT(struct HELLO
  811. {
  812. Header h;
  813. uint8_t versionProtocol;
  814. uint8_t versionMajor;
  815. uint8_t versionMinor;
  816. uint16_t versionRev;
  817. uint64_t timestamp;
  818. });
  819. ZT_PACKED_STRUCT(struct RENDEZVOUS
  820. {
  821. Header h;
  822. uint8_t flags;
  823. uint8_t peerAddress[5];
  824. uint16_t port;
  825. uint8_t addressLength;
  826. });
  827. ZT_PACKED_STRUCT(struct FRAME
  828. {
  829. Header h;
  830. uint64_t networkId;
  831. uint16_t etherType;
  832. });
  833. ZT_PACKED_STRUCT(struct EXT_FRAME
  834. {
  835. Header h;
  836. uint64_t networkId;
  837. uint8_t flags;
  838. });
  839. ZT_PACKED_STRUCT(struct PUSH_DIRECT_PATHS
  840. {
  841. Header h;
  842. uint16_t numPaths;
  843. });
  844. ZT_PACKED_STRUCT(struct MULTICAST_LIKE
  845. {
  846. ZT_PACKED_STRUCT(struct Entry
  847. {
  848. uint64_t networkId;
  849. uint8_t mac[6];
  850. uint32_t adi;
  851. });
  852. Header h;
  853. });
  854. namespace OK {
  855. /**
  856. * OK response header
  857. *
  858. * @tparam PT OK payload type (default: uint8_t[])
  859. */
  860. ZT_PACKED_STRUCT(struct Header
  861. {
  862. Protocol::Header h;
  863. uint8_t inReVerb;
  864. uint64_t inRePacketId;
  865. });
  866. ZT_PACKED_STRUCT(struct WHOIS
  867. {
  868. OK::Header h;
  869. });
  870. ZT_PACKED_STRUCT(struct ECHO
  871. {
  872. OK::Header h;
  873. });
  874. ZT_PACKED_STRUCT(struct HELLO
  875. {
  876. OK::Header h;
  877. uint64_t timestampEcho;
  878. uint8_t versionProtocol;
  879. uint8_t versionMajor;
  880. uint8_t versionMinor;
  881. uint16_t versionRev;
  882. });
  883. ZT_PACKED_STRUCT(struct EXT_FRAME
  884. {
  885. OK::Header h;
  886. uint64_t networkId;
  887. uint8_t flags;
  888. uint8_t destMac[6];
  889. uint8_t sourceMac[6];
  890. uint16_t etherType;
  891. });
  892. ZT_PACKED_STRUCT(struct NETWORK_CONFIG
  893. {
  894. OK::Header h;
  895. uint64_t networkId;
  896. uint64_t configUpdateId;
  897. });
  898. } // namespace OK
  899. namespace ERROR {
  900. /**
  901. * Error header
  902. *
  903. * The error header comes after the packet header but before type-specific payloads.
  904. *
  905. * @tparam PT Error payload type (default: uint8_t[])
  906. */
  907. ZT_PACKED_STRUCT(struct Header
  908. {
  909. Protocol::Header h;
  910. int8_t inReVerb;
  911. uint64_t inRePacketId;
  912. uint8_t error;
  913. });
  914. ZT_PACKED_STRUCT(struct NEED_MEMBERSHIP_CERTIFICATE
  915. {
  916. ERROR::Header h;
  917. uint64_t networkId;
  918. });
  919. ZT_PACKED_STRUCT(struct UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST
  920. {
  921. ERROR::Header h;
  922. uint64_t networkId;
  923. });
  924. } // namespace ERROR
  925. /****************************************************************************/
  926. static_assert(sizeof(Protocol::Header) == ZT_PROTO_MIN_PACKET_LENGTH,"Protocol::Header struct packing error");
  927. static_assert(sizeof(Protocol::FragmentHeader) == ZT_PROTO_MIN_FRAGMENT_LENGTH,"Protocol::FragmentHeader struct packing error");
  928. static_assert(ZT_PROTO_MAX_PACKET_LENGTH < ZT_BUF_MEM_SIZE,"maximum packet length won't fit in Buf");
  929. static_assert(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START == (ZT_PROTO_MIN_PACKET_LENGTH-1),"encrypted packet section must start right before protocol verb at one less than minimum packet size");
  930. /**
  931. * Convenience function to pull packet ID from a raw buffer
  932. *
  933. * @param pkt Packet to read first 8 bytes from
  934. * @param packetSize Packet's actual size in bytes
  935. * @return Packet ID or 0 if packet size is less than 8
  936. */
  937. static ZT_INLINE uint64_t packetId(const Buf &pkt,const unsigned int packetSize) noexcept { return (packetSize >= 8) ? Utils::loadBigEndian<uint64_t>(pkt.unsafeData) : 0ULL; }
  938. /**
  939. * @param Packet to extract hops from
  940. * @param packetSize Packet's actual size in bytes
  941. * @return 3-bit hops field embedded in packet flags field
  942. */
  943. static ZT_INLINE uint8_t packetHops(const Buf &pkt,const unsigned int packetSize) noexcept { return (packetSize >= ZT_PROTO_PACKET_FLAGS_INDEX) ? (pkt.unsafeData[ZT_PROTO_PACKET_FLAGS_INDEX] & ZT_PROTO_FLAG_FIELD_HOPS_MASK) : 0; }
  944. /**
  945. * @param Packet to extract cipher ID from
  946. * @param packetSize Packet's actual size in bytes
  947. * @return 3-bit cipher field embedded in packet flags field
  948. */
  949. static ZT_INLINE uint8_t packetCipher(const Buf &pkt,const unsigned int packetSize) noexcept { return (packetSize >= ZT_PROTO_PACKET_FLAGS_INDEX) ? ((pkt.unsafeData[ZT_PROTO_PACKET_FLAGS_INDEX] >> 3U) & 0x07U) : 0; }
  950. /**
  951. * @return 3-bit hops field embedded in packet flags field
  952. */
  953. static ZT_INLINE uint8_t packetHops(const Header &ph) noexcept { return (ph.flags & 0x07U); }
  954. /**
  955. * @return 3-bit cipher field embedded in packet flags field
  956. */
  957. static ZT_INLINE uint8_t packetCipher(const Header &ph) noexcept { return ((ph.flags >> 3U) & 0x07U); }
  958. /**
  959. * Deterministically mangle a 256-bit crypto key based on packet characteristics
  960. *
  961. * This uses extra data from the packet to mangle the secret, yielding when
  962. * combined with Salsa20's conventional 64-bit nonce an effective nonce that's
  963. * more like 68 bits.
  964. *
  965. * @param in Input key (32 bytes)
  966. * @param out Output buffer (32 bytes)
  967. */
  968. static ZT_INLINE void salsa2012DeriveKey(const uint8_t *const in,uint8_t *const out,const Buf &packet,const unsigned int packetSize) noexcept
  969. {
  970. // IV and source/destination addresses. Using the addresses divides the
  971. // key space into two halves-- A->B and B->A (since order will change).
  972. #ifdef ZT_NO_UNALIGNED_ACCESS
  973. for(int i=0;i<18;++i)
  974. out[i] = in[i] ^ packet.unsafeData[i];
  975. #else
  976. *reinterpret_cast<uint64_t *>(out) = *reinterpret_cast<const uint64_t *>(in) ^ *reinterpret_cast<const uint64_t *>(packet.unsafeData);
  977. *reinterpret_cast<uint64_t *>(out + 8) = *reinterpret_cast<const uint64_t *>(in + 8) ^ *reinterpret_cast<const uint64_t *>(packet.unsafeData + 8);
  978. *reinterpret_cast<uint16_t *>(out + 16) = *reinterpret_cast<const uint16_t *>(in + 16) ^ *reinterpret_cast<const uint16_t *>(packet.unsafeData + 16);
  979. #endif
  980. // Flags, but with hop count masked off. Hop count is altered by forwarding
  981. // nodes and is the only field that is mutable by unauthenticated third parties.
  982. out[18] = in[18] ^ (packet.unsafeData[18] & 0xf8U);
  983. // Raw packet size in bytes -- thus each packet size defines a new key space.
  984. out[19] = in[19] ^ (uint8_t)packetSize;
  985. out[20] = in[20] ^ (uint8_t)(packetSize >> 8U); // little endian
  986. // Rest of raw key is used unchanged
  987. #ifdef ZT_NO_UNALIGNED_ACCESS
  988. for(int i=21;i<32;++i)
  989. out[i] = in[i];
  990. #else
  991. out[21] = in[21];
  992. out[22] = in[22];
  993. out[23] = in[23];
  994. *reinterpret_cast<uint64_t *>(out + 24) = *reinterpret_cast<const uint64_t *>(in + 24);
  995. #endif
  996. }
  997. /**
  998. * Create a short probe packet for probing a recipient for e.g. NAT traversal and path setup
  999. *
  1000. * @param sender Sender identity
  1001. * @param recipient Recipient identity
  1002. * @param key Long-term shared secret key resulting from sender and recipient agreement
  1003. * @return Probe packed into 64-bit integer (in big-endian byte order)
  1004. */
  1005. uint64_t createProbe(const Identity &sender,const Identity &recipient,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) noexcept;
  1006. // Do not use directly
  1007. extern std::atomic<uint64_t> _s_packetIdCtr;
  1008. /**
  1009. * Get a packet ID (and nonce) for a new packet
  1010. *
  1011. * @return Next packet ID
  1012. */
  1013. static ZT_INLINE uint64_t getPacketId() noexcept { return ++_s_packetIdCtr; }
  1014. /**
  1015. * Encrypt and compute packet MAC
  1016. *
  1017. * @param pkt Packet data to encrypt (in place)
  1018. * @param packetSize Packet size, must be at least ZT_PROTO_MIN_PACKET_LENGTH or crash will occur
  1019. * @param key Key to use for encryption (not per-packet key)
  1020. * @param cipherSuite Cipher suite to use for AEAD encryption or just MAC
  1021. */
  1022. void armor(Buf &pkt,int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],uint8_t cipherSuite) noexcept;
  1023. /**
  1024. * Attempt to compress packet payload
  1025. *
  1026. * This attempts compression and swaps the pointer in 'pkt' for a buffer holding
  1027. * compressed data on success. If compression did not shrink the packet, the original
  1028. * packet size is returned and 'pkt' remains unchanged. If compression is successful
  1029. * the compressed verb flag is also set.
  1030. *
  1031. * @param pkt Packet buffer value/result parameter: pointer may be swapped if compression is successful
  1032. * @param packetSize Total size of packet in bytes (including headers)
  1033. * @return New size of packet after compression or original size of compression wasn't helpful
  1034. */
  1035. int compress(SharedPtr<Buf> &pkt,int packetSize) noexcept;
  1036. } // namespace Protocol
  1037. } // namespace ZeroTier
  1038. #endif