Protocol.hpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. /**
  22. * Protocol version -- incremented only for major changes
  23. *
  24. * 1 - 0.2.0 ... 0.2.5
  25. * 2 - 0.3.0 ... 0.4.5
  26. * + Added signature and originating peer to multicast frame
  27. * + Double size of multicast frame bloom filter
  28. * 3 - 0.5.0 ... 0.6.0
  29. * + Yet another multicast redesign
  30. * + New crypto completely changes key agreement cipher
  31. * 4 - 0.6.0 ... 1.0.6
  32. * + BREAKING CHANGE: New identity format based on hashcash design
  33. * 5 - 1.1.0 ... 1.1.5
  34. * + Supports echo
  35. * + Supports in-band world (root server definition) updates
  36. * + Clustering! (Though this will work with protocol v4 clients.)
  37. * + Otherwise backward compatible with protocol v4
  38. * 6 - 1.1.5 ... 1.1.10
  39. * + Network configuration format revisions including binary values
  40. * 7 - 1.1.10 ... 1.1.17
  41. * + Introduce trusted paths for local SDN use
  42. * 8 - 1.1.17 ... 1.2.0
  43. * + Multipart network configurations for large network configs
  44. * + Tags and Capabilities
  45. * + inline push of CertificateOfMembership deprecated
  46. * 9 - 1.2.0 ... 1.2.14
  47. * 10 - 1.4.0 ... 1.4.6
  48. * 11 - 2.0.0 ... CURRENT
  49. * + Peer-to-peer multicast replication
  50. * + Old planet/moon stuff is DEAD!
  51. * + AES encryption support
  52. * + NIST P-384 (type 1) identities
  53. * + Ephemeral keys
  54. */
  55. #define ZT_PROTO_VERSION 11
  56. /**
  57. * Minimum supported protocol version
  58. *
  59. * As of v2 we don't "officially" support anything older than 1.2.14, but this
  60. * is the hard cutoff before which peers will be flat out rejected.
  61. */
  62. #define ZT_PROTO_VERSION_MIN 6
  63. /**
  64. * Packet buffer size (can be changed)
  65. */
  66. #define ZT_PROTO_MAX_PACKET_LENGTH (ZT_MAX_PACKET_FRAGMENTS * ZT_DEFAULT_PHYSMTU)
  67. /**
  68. * Minimum viable packet length (outer header + verb)
  69. */
  70. #define ZT_PROTO_MIN_PACKET_LENGTH 28
  71. /**
  72. * Index at which the encrypted section of a packet begins
  73. */
  74. #define ZT_PROTO_PACKET_ENCRYPTED_SECTION_START 27
  75. /**
  76. * Index at which packet payload begins (after verb)
  77. */
  78. #define ZT_PROTO_PACKET_PAYLOAD_START 28
  79. /**
  80. * Maximum hop count allowed by packet structure (3 bits, 0-7)
  81. *
  82. * This is a protocol constant. It's the maximum allowed by the length
  83. * of the hop counter -- three bits. See node/Constants.hpp for the
  84. * pragmatic forwarding limit, which is typically lower.
  85. */
  86. #define ZT_PROTO_MAX_HOPS 7
  87. /**
  88. * NONE/Poly1305 (using Salsa20/12 to generate poly1305 key)
  89. */
  90. #define ZT_PROTO_CIPHER_SUITE__POLY1305_NONE 0
  91. /**
  92. * Salsa2012/Poly1305
  93. */
  94. #define ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012 1
  95. /**
  96. * No encryption or authentication at all
  97. *
  98. * For trusted paths the MAC field is the trusted path ID.
  99. */
  100. #define ZT_PROTO_CIPHER_SUITE__NONE 2
  101. /**
  102. * AES-GCM-NRH (AES-GCM with nonce reuse hardening) w/AES-256
  103. */
  104. #define ZT_PROTO_CIPHER_SUITE__AES_GCM_NRH 3
  105. /**
  106. * Magic number indicating a fragment
  107. */
  108. #define ZT_PROTO_PACKET_FRAGMENT_INDICATOR 0xff
  109. /**
  110. * Index at which fragment indicator is found in fragments
  111. */
  112. #define ZT_PROTO_PACKET_FRAGMENT_INDICATOR_INDEX 13
  113. /**
  114. * Minimum viable length for a fragment
  115. */
  116. #define ZT_PROTO_MIN_FRAGMENT_LENGTH 16
  117. /**
  118. * Index at which packet fragment payload starts
  119. */
  120. #define ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT ZT_PROTO_MIN_FRAGMENT_LENGTH
  121. /**
  122. * Header flag indicating that a packet is fragmented and more fragments should be expected
  123. */
  124. #define ZT_PROTO_FLAG_FRAGMENTED 0x40U
  125. /**
  126. * Verb flag indicating payload is compressed with LZ4
  127. */
  128. #define ZT_PROTO_VERB_FLAG_COMPRESSED 0x80U
  129. /**
  130. * Mask to extract just the verb from the verb field, which also includes flags
  131. */
  132. #define ZT_PROTO_VERB_MASK 0x1fU
  133. /**
  134. * Key derivation function label for the key used with HMAC-384 in HELLO
  135. */
  136. #define ZT_PROTO_KDF_KEY_LABEL_HELLO_HMAC 'H'
  137. /**
  138. * HELLO exchange meta-data: signed locator for this node
  139. */
  140. #define ZT_PROTO_HELLO_NODE_META_LOCATOR "l"
  141. /**
  142. * HELLO exchange meta-data: ephemeral C25519 public key
  143. */
  144. #define ZT_PROTO_HELLO_NODE_META_EPHEMERAL_KEY_C25519 "e0"
  145. /**
  146. * HELLO exchange meta-data: ephemeral NIST P-384 public key
  147. */
  148. #define ZT_PROTO_HELLO_NODE_META_EPHEMERAL_KEY_P384 "e1"
  149. /**
  150. * HELLO exchange meta-data: address(es) of nodes to whom this node will relay
  151. */
  152. #define ZT_PROTO_HELLO_NODE_META_WILL_RELAY_TO "wr"
  153. /**
  154. * HELLO exchange meta-data: X coordinate of your node (sent in OK(HELLO))
  155. */
  156. #define ZT_PROTO_HELLO_NODE_META_LOCATION_X "gX"
  157. /**
  158. * HELLO exchange meta-data: Y coordinate of your node (sent in OK(HELLO))
  159. */
  160. #define ZT_PROTO_HELLO_NODE_META_LOCATION_Y "gY"
  161. /**
  162. * HELLO exchange meta-data: Z coordinate of your node (sent in OK(HELLO))
  163. */
  164. #define ZT_PROTO_HELLO_NODE_META_LOCATION_Z "gZ"
  165. /****************************************************************************/
  166. /*
  167. * Packet format:
  168. * <[8] 64-bit packet ID / crypto IV>
  169. * <[5] destination ZT address>
  170. * <[5] source ZT address>
  171. * <[1] flags/cipher/hops>
  172. * <[8] 64-bit MAC (or trusted path ID in trusted path mode)>
  173. * [... -- begin encryption envelope -- ...]
  174. * <[1] encrypted flags (MS 3 bits) and verb (LS 5 bits)>
  175. * [... verb-specific payload ...]
  176. *
  177. * Packets smaller than 28 bytes are invalid and silently discarded.
  178. *
  179. * The flags/cipher/hops bit field is: FFCCCHHH where C is a 3-bit cipher
  180. * selection allowing up to 7 cipher suites, F is outside-envelope flags,
  181. * and H is hop count.
  182. *
  183. * The three-bit hop count is the only part of a packet that is mutable in
  184. * transit without invalidating the MAC. All other bits in the packet are
  185. * immutable. This is because intermediate nodes can increment the hop
  186. * count up to 7 (protocol max).
  187. *
  188. * For unencrypted packets, MAC is computed on plaintext. Only HELLO is ever
  189. * sent in the clear, as it's the "here is my public key" message.
  190. *
  191. * The fragmented bit indicates that there is at least one fragment. Fragments
  192. * themselves contain the total, so the receiver must "learn" this from the
  193. * first fragment it receives.
  194. *
  195. * Fragments are sent with the following format:
  196. * <[8] packet ID of packet to which this fragment belongs>
  197. * <[5] destination ZT address>
  198. * <[1] 0xff here signals that this is a fragment>
  199. * <[1] total fragments (most significant 4 bits), fragment no (LS 4 bits)>
  200. * <[1] ZT hop count (least significant 3 bits; others are reserved)>
  201. * <[...] fragment data>
  202. *
  203. * The protocol supports a maximum of 16 fragments. If a fragment is received
  204. * before its main packet header, it should be cached for a brief period of
  205. * time to see if its parent arrives. Loss of any fragment constitutes packet
  206. * loss; there is no retransmission mechanism. The receiver must wait for full
  207. * receipt to authenticate and decrypt; there is no per-fragment MAC. (But if
  208. * fragments are corrupt, the MAC will fail for the whole assembled packet.)
  209. */
  210. namespace ZeroTier {
  211. namespace Protocol {
  212. /**
  213. * Packet verb (message type)
  214. */
  215. enum Verb
  216. {
  217. VERB_NOP = 0x00,
  218. /**
  219. * Announcement of a node's existence and vitals:
  220. * [... HMAC-384 starts here ...]
  221. * <[1] protocol version>
  222. * <[1] software major version>
  223. * <[1] software minor version>
  224. * <[2] software revision>
  225. * <[8] timestamp for determining latency>
  226. * <[...] binary serialized identity>
  227. * <[...] physical destination address of packet>
  228. * [... begin encrypted region ...]
  229. * <[2] 16-bit reserved (legacy) field, always 0>
  230. * <[2] 16-bit length of meta-data dictionary>
  231. * <[...] meta-data dictionary>
  232. * <[2] 16-bit length of any additional fields>
  233. * <[48] HMAC-SHA384 of full plaintext payload>
  234. * [... end encrypted region ...]
  235. *
  236. * HELLO is sent with authentication but without the usual encryption so
  237. * that peers can exchange identities.
  238. *
  239. * Destination address is the actual wire address to which the packet
  240. * was sent. See InetAddress::serialize() for format.
  241. *
  242. * Starting at "begin encrypted section" the reset of the packet is
  243. * encrypted with Salsa20/12. This is not the normal packet encryption
  244. * and is technically not necessary as nothing in HELLO is secret. It
  245. * exists merely to shield meta-data info from passive listeners to
  246. * slightly improve privacy, and for backward compatibility with older
  247. * nodes that required it.
  248. *
  249. * HELLO (and its OK response) ends with a large 384-bit HMAC to allow
  250. * identity exchanges to be authenticated with additional strength beyond
  251. * ordinary packet authentication.
  252. *
  253. * OK payload:
  254. * <[8] HELLO timestamp field echo>
  255. * <[1] protocol version>
  256. * <[1] software major version>
  257. * <[1] software minor version>
  258. * <[2] software revision>
  259. * <[...] physical destination address of packet>
  260. * <[2] 16-bit reserved (legacy) field, always 0>
  261. * <[2] 16-bit length of meta-data dictionary>
  262. * <[...] meta-data dictionary>
  263. * <[48] HMAC-SHA384 of all fields to this point (as plaintext)>
  264. *
  265. * With the exception of the timestamp, the other fields pertain to the
  266. * respondent who is sending OK and are not echoes.
  267. *
  268. * ERROR has no payload.
  269. */
  270. VERB_HELLO = 0x01,
  271. /**
  272. * Error response:
  273. * <[1] in-re verb>
  274. * <[8] in-re packet ID>
  275. * <[1] error code>
  276. * <[...] error-dependent payload>
  277. *
  278. * If this is not in response to a single packet then verb can be
  279. * NOP and packet ID can be zero.
  280. */
  281. VERB_ERROR = 0x02,
  282. /**
  283. * Success response:
  284. * <[1] in-re verb>
  285. * <[8] in-re packet ID>
  286. * <[...] request-specific payload>
  287. */
  288. VERB_OK = 0x03,
  289. /**
  290. * Query an identity by address:
  291. * <[5] address to look up>
  292. * [<[...] additional addresses to look up>
  293. *
  294. * OK response payload:
  295. * <[...] identity>
  296. * <[...] locator>
  297. * [... additional identity/locator pairs]
  298. *
  299. * If the address is not found, no response is generated. The semantics
  300. * of WHOIS is similar to ARP and NDP in that persistent retrying can
  301. * be performed.
  302. *
  303. * It is possible for an identity but a null/empty locator to be returned
  304. * if no locator is known for a node. Older versions will also send no
  305. * locator field at all.
  306. */
  307. VERB_WHOIS = 0x04,
  308. /**
  309. * Relay-mediated NAT traversal or firewall punching initiation:
  310. * <[1] flags (unused, currently 0)>
  311. * <[5] ZeroTier address of peer that might be found at this address>
  312. * <[2] 16-bit protocol address port>
  313. * <[1] protocol address length (4 for IPv4, 16 for IPv6)>
  314. * <[...] protocol address (network byte order)>
  315. *
  316. * An upstream node can send this to inform both sides of a relay of
  317. * information they might use to establish a direct connection.
  318. *
  319. * Upon receipt a peer sends HELLO to establish a direct link.
  320. *
  321. * No OK or ERROR is generated.
  322. */
  323. VERB_RENDEZVOUS = 0x05,
  324. /**
  325. * ZT-to-ZT unicast ethernet frame (shortened EXT_FRAME):
  326. * <[8] 64-bit network ID>
  327. * <[2] 16-bit ethertype>
  328. * <[...] ethernet payload>
  329. *
  330. * MAC addresses are derived from the packet's source and destination
  331. * ZeroTier addresses. This is a shortened EXT_FRAME that elides full
  332. * Ethernet framing and other optional flags and features when they
  333. * are not necessary.
  334. *
  335. * ERROR may be generated if a membership certificate is needed for a
  336. * closed network. Payload will be network ID.
  337. */
  338. VERB_FRAME = 0x06,
  339. /**
  340. * Full Ethernet frame with MAC addressing and optional fields:
  341. * <[8] 64-bit network ID>
  342. * <[1] flags>
  343. * <[6] destination MAC or all zero for destination node>
  344. * <[6] source MAC or all zero for node of origin>
  345. * <[2] 16-bit ethertype>
  346. * <[...] ethernet payload>
  347. *
  348. * Flags:
  349. * 0x01 - Certificate of network membership attached (DEPRECATED)
  350. * 0x02 - Most significant bit of subtype (see below)
  351. * 0x04 - Middle bit of subtype (see below)
  352. * 0x08 - Least significant bit of subtype (see below)
  353. * 0x10 - ACK requested in the form of OK(EXT_FRAME)
  354. *
  355. * Subtypes (0..7):
  356. * 0x0 - Normal frame (bridging can be determined by checking MAC)
  357. * 0x1 - TEEd outbound frame
  358. * 0x2 - REDIRECTed outbound frame
  359. * 0x3 - WATCHed outbound frame (TEE with ACK, ACK bit also set)
  360. * 0x4 - TEEd inbound frame
  361. * 0x5 - REDIRECTed inbound frame
  362. * 0x6 - WATCHed inbound frame
  363. * 0x7 - (reserved for future use)
  364. *
  365. * An extended frame carries full MAC addressing, making it a
  366. * superset of VERB_FRAME. If 0x20 is set then p2p or hub and
  367. * spoke multicast propagation is requested.
  368. *
  369. * OK payload (if ACK flag is set):
  370. * <[8] 64-bit network ID>
  371. * <[1] flags>
  372. * <[6] destination MAC or all zero for destination node>
  373. * <[6] source MAC or all zero for node of origin>
  374. * <[2] 16-bit ethertype>
  375. */
  376. VERB_EXT_FRAME = 0x07,
  377. /**
  378. * ECHO request (a.k.a. ping):
  379. * <[...] arbitrary payload>
  380. *
  381. * This generates OK with a copy of the transmitted payload. No ERROR
  382. * is generated. Response to ECHO requests is optional and ECHO may be
  383. * ignored if a node detects a possible flood.
  384. */
  385. VERB_ECHO = 0x08,
  386. /**
  387. * Announce interest in multicast group(s):
  388. * <[8] 64-bit network ID>
  389. * <[6] multicast Ethernet address>
  390. * <[4] multicast additional distinguishing information (ADI)>
  391. * [... additional tuples of network/address/adi ...]
  392. *
  393. * LIKEs may be sent to any peer, though a good implementation should
  394. * restrict them to peers on the same network they're for and to network
  395. * controllers and root servers. In the current network, root servers
  396. * will provide the service of final multicast cache.
  397. */
  398. VERB_MULTICAST_LIKE = 0x09,
  399. /**
  400. * Network credentials push:
  401. * [<[...] one or more certificates of membership>]
  402. * <[1] 0x00, null byte marking end of COM array>
  403. * <[2] 16-bit number of capabilities>
  404. * <[...] one or more serialized Capability>
  405. * <[2] 16-bit number of tags>
  406. * <[...] one or more serialized Tags>
  407. * <[2] 16-bit number of revocations>
  408. * <[...] one or more serialized Revocations>
  409. * <[2] 16-bit number of certificates of ownership>
  410. * <[...] one or more serialized CertificateOfOwnership>
  411. *
  412. * This can be sent by anyone at any time to push network credentials.
  413. * These will of course only be accepted if they are properly signed.
  414. * Credentials can be for any number of networks.
  415. *
  416. * The use of a zero byte to terminate the COM section is for legacy
  417. * backward compatibility. Newer fields are prefixed with a length.
  418. *
  419. * OK/ERROR are not generated.
  420. */
  421. VERB_NETWORK_CREDENTIALS = 0x0a,
  422. /**
  423. * Network configuration request:
  424. * <[8] 64-bit network ID>
  425. * <[2] 16-bit length of request meta-data dictionary>
  426. * <[...] string-serialized request meta-data>
  427. * <[8] 64-bit revision of netconf we currently have>
  428. * <[8] 64-bit timestamp of netconf we currently have>
  429. *
  430. * This message requests network configuration from a node capable of
  431. * providing it. Responses can be sent as OK(NETWORK_CONFIG_REQUEST)
  432. * or NETWORK_CONFIG messages. NETWORK_CONFIG can also be sent by
  433. * network controllers or other nodes unsolicited.
  434. *
  435. * OK response payload:
  436. * (same as VERB_NETWORK_CONFIG payload)
  437. *
  438. * ERROR response payload:
  439. * <[8] 64-bit network ID>
  440. */
  441. VERB_NETWORK_CONFIG_REQUEST = 0x0b,
  442. /**
  443. * Network configuration data push:
  444. * <[8] 64-bit network ID>
  445. * <[2] 16-bit length of network configuration dictionary chunk>
  446. * <[...] network configuration dictionary (may be incomplete)>
  447. * <[1] 8-bit flags>
  448. * <[8] 64-bit config update ID (should never be 0)>
  449. * <[4] 32-bit total length of assembled dictionary>
  450. * <[4] 32-bit index of chunk>
  451. * [ ... end signed portion ... ]
  452. * <[1] 8-bit reserved field (legacy)>
  453. * <[2] 16-bit length of chunk signature>
  454. * <[...] chunk signature>
  455. *
  456. * Network configurations can come from network controllers or theoretically
  457. * any other node, but each chunk must be signed by the network controller
  458. * that generated it originally. The config update ID is arbitrary and is merely
  459. * used by the receiver to group chunks. Chunk indexes must be sequential and
  460. * the total delivered chunks must yield a total network config equal to the
  461. * specified total length.
  462. *
  463. * Flags:
  464. * 0x01 - Use fast propagation -- rumor mill flood this chunk to other members
  465. *
  466. * An OK should be sent if the config is successfully received and
  467. * accepted.
  468. *
  469. * OK payload:
  470. * <[8] 64-bit network ID>
  471. * <[8] 64-bit config update ID>
  472. */
  473. VERB_NETWORK_CONFIG = 0x0c,
  474. /**
  475. * Request endpoints for multicast distribution:
  476. * <[8] 64-bit network ID>
  477. * <[1] flags>
  478. * <[6] MAC address of multicast group being queried>
  479. * <[4] 32-bit ADI for multicast group being queried>
  480. * <[4] 32-bit requested max number of multicast peers>
  481. *
  482. * This message asks a peer for additional known endpoints that have
  483. * LIKEd a given multicast group. It's sent when the sender wishes
  484. * to send multicast but does not have the desired number of recipient
  485. * peers.
  486. *
  487. * OK response payload: (multiple OKs can be generated)
  488. * <[8] 64-bit network ID>
  489. * <[6] MAC address of multicast group being queried>
  490. * <[4] 32-bit ADI for multicast group being queried>
  491. * <[4] 32-bit total number of known members in this multicast group>
  492. * <[2] 16-bit number of members enumerated in this packet>
  493. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  494. *
  495. * ERROR is not generated; queries that return no response are dropped.
  496. */
  497. VERB_MULTICAST_GATHER = 0x0d,
  498. /** *** DEPRECATED ***
  499. * Multicast frame:
  500. * <[8] 64-bit network ID>
  501. * <[1] flags>
  502. * [<[4] 32-bit implicit gather limit>]
  503. * [<[6] source MAC>]
  504. * <[6] destination MAC (multicast address)>
  505. * <[4] 32-bit multicast ADI (multicast address extension)>
  506. * <[2] 16-bit ethertype>
  507. * <[...] ethernet payload>
  508. *
  509. * Flags:
  510. * 0x01 - Network certificate of membership attached (DEPRECATED)
  511. * 0x02 - Implicit gather limit field is present
  512. * 0x04 - Source MAC is specified -- otherwise it's computed from sender
  513. * 0x08 - Please replicate (sent to multicast replicators)
  514. *
  515. * OK and ERROR responses are optional. OK may be generated if there are
  516. * implicit gather results or if the recipient wants to send its own
  517. * updated certificate of network membership to the sender. ERROR may be
  518. * generated if a certificate is needed or if multicasts to this group
  519. * are no longer wanted (multicast unsubscribe).
  520. *
  521. * OK response payload:
  522. * <[8] 64-bit network ID>
  523. * <[6] MAC address of multicast group>
  524. * <[4] 32-bit ADI for multicast group>
  525. * <[1] flags>
  526. * [<[...] network certificate of membership (DEPRECATED)>]
  527. * [<[...] implicit gather results if flag 0x01 is set>]
  528. *
  529. * OK flags (same bits as request flags):
  530. * 0x01 - OK includes certificate of network membership (DEPRECATED)
  531. * 0x02 - OK includes implicit gather results
  532. *
  533. * ERROR response payload:
  534. * <[8] 64-bit network ID>
  535. * <[6] multicast group MAC>
  536. * <[4] 32-bit multicast group ADI>
  537. */
  538. VERB_MULTICAST_FRAME_deprecated = 0x0e,
  539. /**
  540. * Push of potential endpoints for direct communication:
  541. * <[2] 16-bit number of paths>
  542. * <[...] paths>
  543. *
  544. * Path record format:
  545. * <[1] 8-bit path flags (always 0, currently unused)>
  546. * <[2] length of extended path characteristics or 0 for none>
  547. * <[...] extended path characteristics>
  548. * <[1] address type>
  549. * <[1] address length in bytes>
  550. * <[...] address>
  551. *
  552. * The receiver may, upon receiving a push, attempt to establish a
  553. * direct link to one or more of the indicated addresses. It is the
  554. * responsibility of the sender to limit which peers it pushes direct
  555. * paths to to those with whom it has a trust relationship. The receiver
  556. * must obey any restrictions provided such as exclusivity or blacklists.
  557. * OK responses to this message are optional.
  558. *
  559. * Note that a direct path push does not imply that learned paths can't
  560. * be used unless they are blacklisted explicitly or unless flag 0x01
  561. * is set.
  562. *
  563. * OK and ERROR are not generated.
  564. */
  565. VERB_PUSH_DIRECT_PATHS = 0x10,
  566. /**
  567. * A message with arbitrary user-definable content:
  568. * <[8] 64-bit arbitrary message type ID>
  569. * [<[...] message payload>]
  570. *
  571. * This can be used to send arbitrary messages over VL1. It generates no
  572. * OK or ERROR and has no special semantics outside of whatever the user
  573. * (via the ZeroTier core API) chooses to give it.
  574. *
  575. * Message type IDs less than or equal to 65535 are reserved for use by
  576. * ZeroTier, Inc. itself. We recommend making up random ones for your own
  577. * implementations.
  578. */
  579. VERB_USER_MESSAGE = 0x14,
  580. /**
  581. * Encapsulate a ZeroTier packet for multicast distribution:
  582. * [... begin signed portion ...]
  583. * <[1] 8-bit flags>
  584. * <[5] 40-bit ZeroTier address of sender>
  585. * <[2] 16-bit length of inner payload>
  586. * <[1] inner payload verb>
  587. * <[...] inner payload data>
  588. * [... end signed portion ...]
  589. * <[2] 16-bit length of signature or 0 if un-signed>
  590. * [<[...] optional signature of multicast>]
  591. * <[...] address (min prefix) list>
  592. */
  593. VERB_MULTICAST = 0x16,
  594. /**
  595. * Encapsulate a full ZeroTier packet in another:
  596. * <[...] raw encapsulated packet>
  597. *
  598. * Encapsulation exists to enable secure relaying as opposed to the usual
  599. * "dumb" relaying. The latter is faster but secure relaying has roles
  600. * where endpoint privacy is desired. Multiply nested ENCAP packets
  601. * could allow ZeroTier to act as an onion router.
  602. *
  603. * When encapsulated packets are forwarded they do have their hop count
  604. * field incremented.
  605. */
  606. VERB_ENCAP = 0x17
  607. // protocol max: 0x1f
  608. };
  609. /**
  610. * Error codes used in ERROR packets.
  611. */
  612. enum ErrorCode
  613. {
  614. /* Invalid request */
  615. ERROR_INVALID_REQUEST = 0x01,
  616. /* Bad/unsupported protocol version */
  617. ERROR_BAD_PROTOCOL_VERSION = 0x02,
  618. /* Unknown object queried */
  619. ERROR_OBJ_NOT_FOUND = 0x03,
  620. /* Verb or use case not supported/enabled by this node */
  621. ERROR_UNSUPPORTED_OPERATION = 0x05,
  622. /* Network access denied; updated credentials needed */
  623. ERROR_NEED_MEMBERSHIP_CERTIFICATE = 0x06,
  624. /* Tried to join network, but you're not a member */
  625. ERROR_NETWORK_ACCESS_DENIED_ = 0x07, /* extra _ at end to avoid Windows name conflict */
  626. /* Cannot deliver a forwarded ZeroTier packet (for any reason) */
  627. ERROR_CANNOT_DELIVER = 0x09
  628. };
  629. /**
  630. * EXT_FRAME subtypes, which are packed into three bits in the flags field.
  631. *
  632. * This allows the node to know whether this is a normal frame or one generated
  633. * by a special tee or redirect type flow rule.
  634. */
  635. enum ExtFrameSubtype
  636. {
  637. EXT_FRAME_SUBTYPE_NORMAL = 0x0,
  638. EXT_FRAME_SUBTYPE_TEE_OUTBOUND = 0x1,
  639. EXT_FRAME_SUBTYPE_REDIRECT_OUTBOUND = 0x2,
  640. EXT_FRAME_SUBTYPE_WATCH_OUTBOUND = 0x3,
  641. EXT_FRAME_SUBTYPE_TEE_INBOUND = 0x4,
  642. EXT_FRAME_SUBTYPE_REDIRECT_INBOUND = 0x5,
  643. EXT_FRAME_SUBTYPE_WATCH_INBOUND = 0x6
  644. };
  645. /**
  646. * EXT_FRAME flags
  647. */
  648. enum ExtFrameFlag
  649. {
  650. /**
  651. * A certifiate of membership was included (no longer used but still accepted)
  652. */
  653. EXT_FRAME_FLAG_COM_ATTACHED_deprecated = 0x01,
  654. // bits 0x02, 0x04, and 0x08 are occupied by the 3-bit ExtFrameSubtype value.
  655. /**
  656. * An OK(EXT_FRAME) acknowledgement was requested by the sender.
  657. */
  658. EXT_FRAME_FLAG_ACK_REQUESTED = 0x10
  659. };
  660. /**
  661. * NETWORK_CONFIG (or OK(NETWORK_CONFIG_REQUEST)) flags
  662. */
  663. enum NetworkConfigFlag
  664. {
  665. /**
  666. * Indicates that this network config chunk should be fast propagated via rumor mill flooding.
  667. */
  668. NETWORK_CONFIG_FLAG_FAST_PROPAGATE = 0x01
  669. };
  670. /****************************************************************************/
  671. /*
  672. * These are bit-packed structures for rapid parsing of packets or at least
  673. * the fixed size headers thereof. Not all packet types have these as some
  674. * are full of variable length fields are are more easily parsed through
  675. * incremental decoding.
  676. *
  677. * All fields larger than one byte are in big-endian byte order on the wire.
  678. */
  679. /**
  680. * Normal packet header
  681. *
  682. * @tparam PT Packet payload type (default: uint8_t[])
  683. */
  684. ZT_PACKED_STRUCT(struct Header
  685. {
  686. uint64_t packetId;
  687. uint8_t destination[5];
  688. uint8_t source[5];
  689. uint8_t flags;
  690. uint64_t mac;
  691. // --- begin encrypted envelope ---
  692. uint8_t verb;
  693. });
  694. /**
  695. * Packet fragment header
  696. */
  697. ZT_PACKED_STRUCT(struct FragmentHeader
  698. {
  699. uint64_t packetId;
  700. uint8_t destination[5];
  701. uint8_t fragmentIndicator; // always 0xff for fragments
  702. uint8_t counts; // total: most significant four bits, number: least significant four bits
  703. uint8_t hops; // top 5 bits unused and must be zero
  704. });
  705. ZT_PACKED_STRUCT(struct HELLO
  706. {
  707. Header h;
  708. uint8_t versionProtocol;
  709. uint8_t versionMajor;
  710. uint8_t versionMinor;
  711. uint16_t versionRev;
  712. uint64_t timestamp;
  713. });
  714. ZT_PACKED_STRUCT(struct RENDEZVOUS
  715. {
  716. Header h;
  717. uint8_t flags;
  718. uint8_t peerAddress[5];
  719. uint16_t port;
  720. uint8_t addressLength;
  721. });
  722. ZT_PACKED_STRUCT(struct FRAME
  723. {
  724. Header h;
  725. uint64_t networkId;
  726. uint16_t etherType;
  727. });
  728. ZT_PACKED_STRUCT(struct EXT_FRAME
  729. {
  730. Header h;
  731. uint64_t networkId;
  732. uint8_t flags;
  733. });
  734. ZT_PACKED_STRUCT(struct MULTICAST_LIKE
  735. {
  736. ZT_PACKED_STRUCT(struct Entry
  737. {
  738. uint64_t networkId;
  739. uint8_t mac[6];
  740. uint32_t adi;
  741. });
  742. Header h;
  743. });
  744. namespace OK {
  745. /**
  746. * OK response header
  747. *
  748. * @tparam PT OK payload type (default: uint8_t[])
  749. */
  750. ZT_PACKED_STRUCT(struct Header
  751. {
  752. uint8_t inReVerb;
  753. uint64_t inRePacketId;
  754. });
  755. ZT_PACKED_STRUCT(struct WHOIS
  756. {
  757. Protocol::Header h;
  758. OK::Header oh;
  759. });
  760. ZT_PACKED_STRUCT(struct ECHO
  761. {
  762. Protocol::Header h;
  763. OK::Header oh;
  764. });
  765. ZT_PACKED_STRUCT(struct HELLO
  766. {
  767. Protocol::Header h;
  768. OK::Header oh;
  769. uint64_t timestampEcho;
  770. uint8_t versionProtocol;
  771. uint8_t versionMajor;
  772. uint8_t versionMinor;
  773. uint16_t versionRev;
  774. });
  775. ZT_PACKED_STRUCT(struct EXT_FRAME
  776. {
  777. Protocol::Header h;
  778. OK::Header oh;
  779. uint64_t networkId;
  780. uint8_t flags;
  781. uint8_t destMac[6];
  782. uint8_t sourceMac[6];
  783. uint16_t etherType;
  784. });
  785. ZT_PACKED_STRUCT(struct NETWORK_CONFIG
  786. {
  787. Protocol::Header h;
  788. OK::Header oh;
  789. uint64_t networkId;
  790. uint64_t configUpdateId;
  791. });
  792. } // namespace OK
  793. namespace ERROR {
  794. /**
  795. * Error header
  796. *
  797. * The error header comes after the packet header but before type-specific payloads.
  798. *
  799. * @tparam PT Error payload type (default: uint8_t[])
  800. */
  801. ZT_PACKED_STRUCT(struct Header
  802. {
  803. int8_t inReVerb;
  804. uint64_t inRePacketId;
  805. uint8_t error;
  806. });
  807. ZT_PACKED_STRUCT(struct NEED_MEMBERSHIP_CERTIFICATE
  808. {
  809. Protocol::Header h;
  810. ERROR::Header eh;
  811. uint64_t networkId;
  812. });
  813. ZT_PACKED_STRUCT(struct UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST
  814. {
  815. Protocol::Header h;
  816. ERROR::Header eh;
  817. uint64_t networkId;
  818. });
  819. } // namespace ERROR
  820. /****************************************************************************/
  821. /**
  822. * Increment the 3-bit hops field embedded in the packet flags field
  823. */
  824. ZT_ALWAYS_INLINE unsigned int incrementPacketHops(Header &h)
  825. {
  826. uint8_t flags = h.flags;
  827. uint8_t hops = flags;
  828. flags &= 0xf8U;
  829. ++hops;
  830. h.flags = flags | (hops & 0x07U);
  831. return (unsigned int)hops;
  832. }
  833. /**
  834. * @return 3-bit hops field embedded in packet flags field
  835. */
  836. ZT_ALWAYS_INLINE uint8_t packetHops(const Header &h) { return (h.flags & 0x07U); }
  837. /**
  838. * @return 3-bit cipher field embedded in packet flags field
  839. */
  840. ZT_ALWAYS_INLINE uint8_t packetCipher(const Header &h) { return ((h.flags >> 3U) & 0x07U); }
  841. /**
  842. * Deterministically mangle a 256-bit crypto key based on packet characteristics
  843. *
  844. * This uses extra data from the packet to mangle the secret, yielding when
  845. * combined with Salsa20's conventional 64-bit nonce an effective nonce that's
  846. * more like 68 bits.
  847. *
  848. * @param in Input key (32 bytes)
  849. * @param out Output buffer (32 bytes)
  850. */
  851. ZT_ALWAYS_INLINE void salsa2012DeriveKey(const uint8_t *const in,uint8_t *const out,const Buf &packet,const unsigned int packetSize)
  852. {
  853. // IV and source/destination addresses. Using the addresses divides the
  854. // key space into two halves-- A->B and B->A (since order will change).
  855. #ifdef ZT_NO_UNALIGNED_ACCESS
  856. for(int i=0;i<18;++i)
  857. out[i] = in[i] ^ packet.b[i];
  858. #else
  859. *reinterpret_cast<uint64_t *>(out) = *reinterpret_cast<const uint64_t *>(in) ^ *reinterpret_cast<const uint64_t *>(packet.b);
  860. *reinterpret_cast<uint64_t *>(out + 8) = *reinterpret_cast<const uint64_t *>(in + 8) ^ *reinterpret_cast<const uint64_t *>(packet.b + 8);
  861. *reinterpret_cast<uint16_t *>(out + 16) = *reinterpret_cast<const uint16_t *>(in + 16) ^ *reinterpret_cast<const uint16_t *>(packet.b + 16);
  862. #endif
  863. // Flags, but with hop count masked off. Hop count is altered by forwarding
  864. // nodes and is the only field that is mutable by unauthenticated third parties.
  865. out[18] = in[18] ^ (packet.b[18] & 0xf8U);
  866. // Raw packet size in bytes -- thus each packet size defines a new key space.
  867. out[19] = in[19] ^ (uint8_t)packetSize;
  868. out[20] = in[20] ^ (uint8_t)(packetSize >> 8U); // little endian
  869. // Rest of raw key is used unchanged
  870. #ifdef ZT_NO_UNALIGNED_ACCESS
  871. for(int i=21;i<32;++i)
  872. out[i] = in[i];
  873. #else
  874. out[21] = in[21];
  875. out[22] = in[22];
  876. out[23] = in[23];
  877. *reinterpret_cast<uint64_t *>(out + 24) = *reinterpret_cast<const uint64_t *>(in + 24);
  878. #endif
  879. }
  880. /**
  881. * Get a sequential non-repeating packet ID for the next packet (thread-safe)
  882. *
  883. * @return Next packet ID / cryptographic nonce
  884. */
  885. uint64_t getPacketId();
  886. /**
  887. * Encrypt and compute packet MAC
  888. *
  889. * @param pkt Packet data to encrypt (in place)
  890. * @param packetSize Packet size, must be at least ZT_PROTO_MIN_PACKET_LENGTH or crash will occur
  891. * @param key Key to use for encryption (not per-packet key)
  892. * @param cipherSuite Cipher suite to use for AEAD encryption or just MAC
  893. */
  894. void armor(Buf &pkt,unsigned int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],uint8_t cipherSuite);
  895. /**
  896. * Attempt to compress packet payload
  897. *
  898. * This attempts compression and swaps the pointer in 'pkt' for a buffer holding
  899. * compressed data on success. If compression did not shrink the packet, the original
  900. * packet size is returned and 'pkt' remains unchanged. If compression is successful
  901. * the compressed verb flag is also set.
  902. *
  903. * @param pkt Packet buffer value/result parameter: pointer may be swapped if compression is successful
  904. * @param packetSize Total size of packet in bytes (including headers)
  905. * @return New size of packet after compression or original size of compression wasn't helpful
  906. */
  907. unsigned int compress(SharedPtr<Buf> &pkt,unsigned int packetSize);
  908. } // namespace Protocol
  909. } // namespace ZeroTier
  910. #endif