Packet.hpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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: 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_N_PACKET_HPP
  14. #define ZT_N_PACKET_HPP
  15. #include "AES.hpp"
  16. #include "Address.hpp"
  17. #include "Buffer.hpp"
  18. #include "Constants.hpp"
  19. #include "Poly1305.hpp"
  20. #include "Salsa20.hpp"
  21. #include "Utils.hpp"
  22. #include <iostream>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <string>
  27. /**
  28. * Protocol version -- incremented only for major changes
  29. *
  30. * 1 - 0.2.0 ... 0.2.5
  31. * 2 - 0.3.0 ... 0.4.5
  32. * + Added signature and originating peer to multicast frame
  33. * + Double size of multicast frame bloom filter
  34. * 3 - 0.5.0 ... 0.6.0
  35. * + Yet another multicast redesign
  36. * + New crypto completely changes key agreement cipher
  37. * 4 - 0.6.0 ... 1.0.6
  38. * + BREAKING CHANGE: New identity format based on hashcash design
  39. * 5 - 1.1.0 ... 1.1.5
  40. * + Supports echo
  41. * + Supports in-band world (root server definition) updates
  42. * + Clustering! (Though this will work with protocol v4 clients.)
  43. * + Otherwise backward compatible with protocol v4
  44. * 6 - 1.1.5 ... 1.1.10
  45. * + Network configuration format revisions including binary values
  46. * 7 - 1.1.10 ... 1.1.17
  47. * + Introduce trusted paths for local SDN use
  48. * 8 - 1.1.17 ... 1.2.0
  49. * + Multipart network configurations for large network configs
  50. * + Tags and Capabilities
  51. * + Inline push of CertificateOfMembership deprecated
  52. * 9 - 1.2.0 ... 1.2.14
  53. * 10 - 1.4.0 ... 1.4.6
  54. * 11 - 1.4.7 ... 1.4.8
  55. * + Multipath capability and load balancing (beta)
  56. * 12 - 1.4.8 ... CURRENT (1.4 series)
  57. * + AES-GMAC-SIV backported for faster peer-to-peer crypto
  58. */
  59. #define ZT_PROTO_VERSION 12
  60. /**
  61. * Minimum supported protocol version
  62. */
  63. #define ZT_PROTO_VERSION_MIN 4
  64. /**
  65. * Maximum hop count allowed by packet structure (3 bits, 0-7)
  66. *
  67. * This is a protocol constant. It's the maximum allowed by the length
  68. * of the hop counter -- three bits. See node/Constants.hpp for the
  69. * pragmatic forwarding limit, which is typically lower.
  70. */
  71. #define ZT_PROTO_MAX_HOPS 7
  72. /**
  73. * Cipher suite: Curve25519/Poly1305/Salsa20/12/NOCRYPT
  74. *
  75. * This specifies Poly1305 MAC using a 32-bit key derived from the first
  76. * 32 bytes of a Salsa20/12 keystream as in the Salsa20/12 cipher suite,
  77. * but the payload is not encrypted. This is currently only used to send
  78. * HELLO since that's the public key specification packet and must be
  79. * sent in the clear. Key agreement is performed using Curve25519 elliptic
  80. * curve Diffie-Hellman.
  81. */
  82. #define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE 0
  83. /**
  84. * Cipher suite: Curve25519/Poly1305/Salsa20/12
  85. *
  86. * This specifies Poly1305 using the first 32 bytes of a Salsa20/12 key
  87. * stream as its one-time-use key followed by payload encryption with
  88. * the remaining Salsa20/12 key stream. Key agreement is performed using
  89. * Curve25519 elliptic curve Diffie-Hellman.
  90. */
  91. #define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 1
  92. /**
  93. * AES-GMAC-SIV backported from 2.x
  94. */
  95. #define ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV 3
  96. /**
  97. * AES-GMAC-SIV first of two keys
  98. */
  99. #define ZT_KBKDF_LABEL_AES_GMAC_SIV_K0 '0'
  100. /**
  101. * AES-GMAC-SIV second of two keys
  102. */
  103. #define ZT_KBKDF_LABEL_AES_GMAC_SIV_K1 '1'
  104. /**
  105. * Cipher suite: NONE
  106. *
  107. * This differs from POLY1305/NONE in that *no* crypto is done, not even
  108. * authentication. This is for trusted local LAN interconnects for internal
  109. * SDN use within a data center.
  110. *
  111. * For this mode the MAC field becomes a trusted path ID and must match the
  112. * configured ID of a trusted path or the packet is discarded.
  113. */
  114. #define ZT_PROTO_CIPHER_SUITE__NO_CRYPTO_TRUSTED_PATH 2
  115. /**
  116. * DEPRECATED payload encrypted flag, may be re-used in the future.
  117. *
  118. * This has been replaced by the three-bit cipher suite selection field.
  119. */
  120. #define ZT_PROTO_FLAG_ENCRYPTED 0x80
  121. /**
  122. * Header flag indicating that a packet is fragmented
  123. *
  124. * If this flag is set, the receiver knows to expect more than one fragment.
  125. * See Packet::Fragment for details.
  126. */
  127. #define ZT_PROTO_FLAG_FRAGMENTED 0x40
  128. /**
  129. * Verb flag indicating payload is compressed with LZ4
  130. */
  131. #define ZT_PROTO_VERB_FLAG_COMPRESSED 0x80
  132. /**
  133. * Rounds used for Salsa20 encryption in ZT
  134. *
  135. * Discussion:
  136. *
  137. * DJB (Salsa20's designer) designed Salsa20 with a significant margin of 20
  138. * rounds, but has said repeatedly that 12 is likely sufficient. So far (as of
  139. * July 2015) there are no published attacks against 12 rounds, let alone 20.
  140. *
  141. * In cryptography, a "break" means something different from what it means in
  142. * common discussion. If a cipher is 256 bits strong and someone finds a way
  143. * to reduce key search to 254 bits, this constitutes a "break" in the academic
  144. * literature. 254 bits is still far beyond what can be leveraged to accomplish
  145. * a "break" as most people would understand it -- the actual decryption and
  146. * reading of traffic.
  147. *
  148. * Nevertheless, "attacks only get better" as cryptographers like to say. As
  149. * a result, they recommend not using anything that's shown any weakness even
  150. * if that weakness is so far only meaningful to academics. It may be a sign
  151. * of a deeper problem.
  152. *
  153. * So why choose a lower round count?
  154. *
  155. * Turns out the speed difference is nontrivial. On a Macbook Pro (Core i3) 20
  156. * rounds of SSE-optimized Salsa20 achieves ~508mb/sec/core, while 12 rounds
  157. * hits ~832mb/sec/core. ZeroTier is designed for multiple objectives:
  158. * security, simplicity, and performance. In this case a deference was made
  159. * for performance.
  160. *
  161. * Meta discussion:
  162. *
  163. * The cipher is not the thing you should be paranoid about.
  164. *
  165. * I'll qualify that. If the cipher is known to be weak, like RC4, or has a
  166. * key size that is too small, like DES, then yes you should worry about
  167. * the cipher.
  168. *
  169. * But if the cipher is strong and your adversary is anyone other than the
  170. * intelligence apparatus of a major superpower, you are fine in that
  171. * department.
  172. *
  173. * Go ahead. Search for the last ten vulnerabilities discovered in SSL. Not
  174. * a single one involved the breaking of a cipher. Now broaden your search.
  175. * Look for issues with SSH, IPSec, etc. The only cipher-related issues you
  176. * will find might involve the use of RC4 or MD5, algorithms with known
  177. * issues or small key/digest sizes. But even weak ciphers are difficult to
  178. * exploit in the real world -- you usually need a lot of data and a lot of
  179. * compute time. No, virtually EVERY security vulnerability you will find
  180. * involves a problem with the IMPLEMENTATION not with the cipher.
  181. *
  182. * A flaw in ZeroTier's protocol or code is incredibly, unbelievably
  183. * more likely than a flaw in Salsa20 or any other cipher or cryptographic
  184. * primitive it uses. We're talking odds of dying in a car wreck vs. odds of
  185. * being personally impacted on the head by a meteorite. Nobody without a
  186. * billion dollar budget is going to break into your network by actually
  187. * cracking Salsa20/12 (or even /8) in the field.
  188. *
  189. * So stop worrying about the cipher unless you are, say, the Kremlin and your
  190. * adversary is the NSA and the GCHQ. In that case... well that's above my
  191. * pay grade. I'll just say defense in depth.
  192. */
  193. #define ZT_PROTO_SALSA20_ROUNDS 12
  194. /**
  195. * PUSH_DIRECT_PATHS flag: forget path
  196. */
  197. #define ZT_PUSH_DIRECT_PATHS_FLAG_FORGET_PATH 0x01
  198. /**
  199. * PUSH_DIRECT_PATHS flag: cluster redirect
  200. */
  201. #define ZT_PUSH_DIRECT_PATHS_FLAG_CLUSTER_REDIRECT 0x02
  202. // Field indexes in packet header
  203. #define ZT_PACKET_IDX_IV 0
  204. #define ZT_PACKET_IDX_DEST 8
  205. #define ZT_PACKET_IDX_SOURCE 13
  206. #define ZT_PACKET_IDX_FLAGS 18
  207. #define ZT_PACKET_IDX_MAC 19
  208. #define ZT_PACKET_IDX_VERB 27
  209. #define ZT_PACKET_IDX_PAYLOAD 28
  210. /**
  211. * Packet buffer size (can be changed)
  212. */
  213. #define ZT_PROTO_MAX_PACKET_LENGTH (ZT_MAX_PACKET_FRAGMENTS * ZT_DEFAULT_PHYSMTU)
  214. /**
  215. * Minimum viable packet length (a.k.a. header length)
  216. */
  217. #define ZT_PROTO_MIN_PACKET_LENGTH ZT_PACKET_IDX_PAYLOAD
  218. // Indexes of fields in fragment header
  219. #define ZT_PACKET_FRAGMENT_IDX_PACKET_ID 0
  220. #define ZT_PACKET_FRAGMENT_IDX_DEST 8
  221. #define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR 13
  222. #define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO 14
  223. #define ZT_PACKET_FRAGMENT_IDX_HOPS 15
  224. #define ZT_PACKET_FRAGMENT_IDX_PAYLOAD 16
  225. /**
  226. * Magic number found at ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR
  227. */
  228. #define ZT_PACKET_FRAGMENT_INDICATOR ZT_ADDRESS_RESERVED_PREFIX
  229. /**
  230. * Minimum viable fragment length
  231. */
  232. #define ZT_PROTO_MIN_FRAGMENT_LENGTH ZT_PACKET_FRAGMENT_IDX_PAYLOAD
  233. // Field indices for parsing verbs -------------------------------------------
  234. // Some verbs have variable-length fields. Those aren't fully defined here
  235. // yet-- instead they are parsed using relative indexes in IncomingPacket.
  236. // See their respective handler functions.
  237. #define ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION (ZT_PACKET_IDX_PAYLOAD)
  238. #define ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION + 1)
  239. #define ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION + 1)
  240. #define ZT_PROTO_VERB_HELLO_IDX_REVISION (ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION + 1)
  241. #define ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP (ZT_PROTO_VERB_HELLO_IDX_REVISION + 2)
  242. #define ZT_PROTO_VERB_HELLO_IDX_IDENTITY (ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP + 8)
  243. #define ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
  244. #define ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB + 1)
  245. #define ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE (ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID + 8)
  246. #define ZT_PROTO_VERB_ERROR_IDX_PAYLOAD (ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE + 1)
  247. #define ZT_PROTO_VERB_OK_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
  248. #define ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_OK_IDX_IN_RE_VERB + 1)
  249. #define ZT_PROTO_VERB_OK_IDX_PAYLOAD (ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID + 8)
  250. #define ZT_PROTO_VERB_WHOIS_IDX_ZTADDRESS (ZT_PACKET_IDX_PAYLOAD)
  251. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS (ZT_PACKET_IDX_PAYLOAD)
  252. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS + 1)
  253. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT (ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS + 5)
  254. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN (ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT + 2)
  255. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN + 1)
  256. #define ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  257. #define ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID + 8)
  258. #define ZT_PROTO_VERB_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE + 2)
  259. #define ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  260. #define ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID 8
  261. #define ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS (ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID + ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID)
  262. #define ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS 1
  263. #define ZT_PROTO_VERB_EXT_FRAME_IDX_COM (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
  264. #define ZT_PROTO_VERB_EXT_FRAME_IDX_TO (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
  265. #define ZT_PROTO_VERB_EXT_FRAME_LEN_TO 6
  266. #define ZT_PROTO_VERB_EXT_FRAME_IDX_FROM (ZT_PROTO_VERB_EXT_FRAME_IDX_TO + ZT_PROTO_VERB_EXT_FRAME_LEN_TO)
  267. #define ZT_PROTO_VERB_EXT_FRAME_LEN_FROM 6
  268. #define ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_EXT_FRAME_IDX_FROM + ZT_PROTO_VERB_EXT_FRAME_LEN_FROM)
  269. #define ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE 2
  270. #define ZT_PROTO_VERB_EXT_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE + ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE)
  271. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  272. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID + 8)
  273. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN + 2)
  274. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  275. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID + 8)
  276. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS + 1)
  277. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC + 6)
  278. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI + 4)
  279. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_COM (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT + 4)
  280. // Note: COM, GATHER_LIMIT, and SOURCE_MAC are optional, and so are specified without size
  281. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  282. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID + 8)
  283. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_COM (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  284. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  285. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_SOURCE_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  286. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  287. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC + 6)
  288. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI + 4)
  289. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FRAME (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE + 2)
  290. #define ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  291. #define ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP + 8)
  292. #define ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION + 1)
  293. #define ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION + 1)
  294. #define ZT_PROTO_VERB_HELLO__OK__IDX_REVISION (ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION + 1)
  295. #define ZT_PROTO_VERB_WHOIS__OK__IDX_IDENTITY (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  296. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  297. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID + 8)
  298. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN + 2)
  299. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  300. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID + 8)
  301. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC + 6)
  302. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI + 4)
  303. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  304. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID + 8)
  305. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC + 6)
  306. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI + 4)
  307. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_COM_AND_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS + 1)
  308. // ---------------------------------------------------------------------------
  309. namespace ZeroTier {
  310. /**
  311. * ZeroTier packet
  312. *
  313. * Packet format:
  314. * <[8] 64-bit packet ID / crypto IV / packet counter>
  315. * <[5] destination ZT address>
  316. * <[5] source ZT address>
  317. * <[1] flags/cipher/hops>
  318. * <[8] 64-bit MAC (or trusted path ID in trusted path mode)>
  319. * [... -- begin encryption envelope -- ...]
  320. * <[1] encrypted flags (MS 3 bits) and verb (LS 5 bits)>
  321. * [... verb-specific payload ...]
  322. *
  323. * Packets smaller than 28 bytes are invalid and silently discarded.
  324. *
  325. * The 64-bit packet ID is a strongly random value used as a crypto IV.
  326. * Its least significant 3 bits are also used as a monotonically increasing
  327. * (and looping) counter for sending packets to a particular recipient. This
  328. * can be used for link quality monitoring and reporting and has no crypto
  329. * impact as it does not increase the likelihood of an IV collision. (The
  330. * crypto we use is not sensitive to the nature of the IV, only that it does
  331. * not repeat.)
  332. *
  333. * The flags/cipher/hops bit field is: FFCCCHHH where C is a 3-bit cipher
  334. * selection allowing up to 7 cipher suites, F is outside-envelope flags,
  335. * and H is hop count.
  336. *
  337. * The three-bit hop count is the only part of a packet that is mutable in
  338. * transit without invalidating the MAC. All other bits in the packet are
  339. * immutable. This is because intermediate nodes can increment the hop
  340. * count up to 7 (protocol max).
  341. *
  342. * For unencrypted packets, MAC is computed on plaintext. Only HELLO is ever
  343. * sent in the clear, as it's the "here is my public key" message.
  344. */
  345. class Packet : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH> {
  346. public:
  347. /**
  348. * A packet fragment
  349. *
  350. * Fragments are sent if a packet is larger than UDP MTU. The first fragment
  351. * is sent with its normal header with the fragmented flag set. Remaining
  352. * fragments are sent this way.
  353. *
  354. * The fragmented bit indicates that there is at least one fragment. Fragments
  355. * themselves contain the total, so the receiver must "learn" this from the
  356. * first fragment it receives.
  357. *
  358. * Fragments are sent with the following format:
  359. * <[8] packet ID of packet whose fragment this belongs to>
  360. * <[5] destination ZT address>
  361. * <[1] 0xff, a reserved address, signals that this isn't a normal packet>
  362. * <[1] total fragments (most significant 4 bits), fragment no (LS 4 bits)>
  363. * <[1] ZT hop count (top 5 bits unused and must be zero)>
  364. * <[...] fragment data>
  365. *
  366. * The protocol supports a maximum of 16 fragments. If a fragment is received
  367. * before its main packet header, it should be cached for a brief period of
  368. * time to see if its parent arrives. Loss of any fragment constitutes packet
  369. * loss; there is no retransmission mechanism. The receiver must wait for full
  370. * receipt to authenticate and decrypt; there is no per-fragment MAC. (But if
  371. * fragments are corrupt, the MAC will fail for the whole assembled packet.)
  372. */
  373. class Fragment : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH> {
  374. public:
  375. Fragment() : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>()
  376. {
  377. }
  378. template <unsigned int C2> Fragment(const Buffer<C2>& b) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
  379. {
  380. }
  381. Fragment(const void* data, unsigned int len) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(data, len)
  382. {
  383. }
  384. /**
  385. * Initialize from a packet
  386. *
  387. * @param p Original assembled packet
  388. * @param fragStart Start of fragment (raw index in packet data)
  389. * @param fragLen Length of fragment in bytes
  390. * @param fragNo Which fragment (>= 1, since 0 is Packet with end chopped off)
  391. * @param fragTotal Total number of fragments (including 0)
  392. */
  393. Fragment(const Packet& p, unsigned int fragStart, unsigned int fragLen, unsigned int fragNo, unsigned int fragTotal)
  394. {
  395. init(p, fragStart, fragLen, fragNo, fragTotal);
  396. }
  397. /**
  398. * Initialize from a packet
  399. *
  400. * @param p Original assembled packet
  401. * @param fragStart Start of fragment (raw index in packet data)
  402. * @param fragLen Length of fragment in bytes
  403. * @param fragNo Which fragment (>= 1, since 0 is Packet with end chopped off)
  404. * @param fragTotal Total number of fragments (including 0)
  405. */
  406. inline void init(const Packet& p, unsigned int fragStart, unsigned int fragLen, unsigned int fragNo, unsigned int fragTotal)
  407. {
  408. if ((fragStart + fragLen) > p.size()) {
  409. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  410. }
  411. setSize(fragLen + ZT_PROTO_MIN_FRAGMENT_LENGTH);
  412. // NOTE: this copies both the IV/packet ID and the destination address.
  413. memcpy(field(ZT_PACKET_FRAGMENT_IDX_PACKET_ID, 13), p.field(ZT_PACKET_IDX_IV, 13), 13);
  414. (*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] = ZT_PACKET_FRAGMENT_INDICATOR;
  415. (*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO] = (char)(((fragTotal & 0xf) << 4) | (fragNo & 0xf));
  416. (*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = 0;
  417. memcpy(field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD, fragLen), p.field(fragStart, fragLen), fragLen);
  418. }
  419. /**
  420. * Get this fragment's destination
  421. *
  422. * @return Destination ZT address
  423. */
  424. inline Address destination() const
  425. {
  426. return Address(field(ZT_PACKET_FRAGMENT_IDX_DEST, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  427. }
  428. /**
  429. * @return True if fragment is of a valid length
  430. */
  431. inline bool lengthValid() const
  432. {
  433. return (size() >= ZT_PACKET_FRAGMENT_IDX_PAYLOAD);
  434. }
  435. /**
  436. * @return ID of packet this is a fragment of
  437. */
  438. inline uint64_t packetId() const
  439. {
  440. return at<uint64_t>(ZT_PACKET_FRAGMENT_IDX_PACKET_ID);
  441. }
  442. /**
  443. * @return Total number of fragments in packet
  444. */
  445. inline unsigned int totalFragments() const
  446. {
  447. return (((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) >> 4) & 0xf);
  448. }
  449. /**
  450. * @return Fragment number of this fragment
  451. */
  452. inline unsigned int fragmentNumber() const
  453. {
  454. return ((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) & 0xf);
  455. }
  456. /**
  457. * @return Fragment ZT hop count
  458. */
  459. inline unsigned int hops() const
  460. {
  461. return (unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]);
  462. }
  463. /**
  464. * Increment this packet's hop count
  465. */
  466. inline void incrementHops()
  467. {
  468. (*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = (((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]) + 1) & ZT_PROTO_MAX_HOPS;
  469. }
  470. /**
  471. * @return Length of payload in bytes
  472. */
  473. inline unsigned int payloadLength() const
  474. {
  475. return ((size() > ZT_PACKET_FRAGMENT_IDX_PAYLOAD) ? (size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD) : 0);
  476. }
  477. /**
  478. * @return Raw packet payload
  479. */
  480. inline const unsigned char* payload() const
  481. {
  482. return field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD, size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD);
  483. }
  484. };
  485. /**
  486. * ZeroTier protocol verbs
  487. */
  488. enum Verb /* Max value: 32 (5 bits) */
  489. {
  490. /**
  491. * No operation (ignored, no reply)
  492. */
  493. VERB_NOP = 0x00,
  494. /**
  495. * Announcement of a node's existence and vitals:
  496. * <[1] protocol version>
  497. * <[1] software major version>
  498. * <[1] software minor version>
  499. * <[2] software revision>
  500. * <[8] timestamp for determining latency>
  501. * <[...] binary serialized identity (see Identity)>
  502. * <[...] physical destination address of packet>
  503. * <[8] 64-bit world ID of current planet>
  504. * <[8] 64-bit timestamp of current planet>
  505. * [... remainder if packet is encrypted using cryptField() ...]
  506. * <[2] 16-bit number of moons>
  507. * [<[1] 8-bit type ID of moon>]
  508. * [<[8] 64-bit world ID of moon>]
  509. * [<[8] 64-bit timestamp of moon>]
  510. * [... additional moon type/ID/timestamp tuples ...]
  511. *
  512. * HELLO is sent in the clear as it is how peers share their identity
  513. * public keys. A few additional fields are sent in the clear too, but
  514. * these are things that are public info or are easy to determine. As
  515. * of 1.2.0 we have added a few more fields, but since these could have
  516. * the potential to be sensitive we introduced the encryption of the
  517. * remainder of the packet. See cryptField(). Packet MAC is still
  518. * performed of course, so authentication occurs as normal.
  519. *
  520. * Destination address is the actual wire address to which the packet
  521. * was sent. See InetAddress::serialize() for format.
  522. *
  523. * OK payload:
  524. * <[8] HELLO timestamp field echo>
  525. * <[1] protocol version>
  526. * <[1] software major version>
  527. * <[1] software minor version>
  528. * <[2] software revision>
  529. * <[...] physical destination address of packet>
  530. * <[2] 16-bit length of world update(s) or 0 if none>
  531. * [[...] updates to planets and/or moons]
  532. *
  533. * With the exception of the timestamp, the other fields pertain to the
  534. * respondent who is sending OK and are not echoes.
  535. *
  536. * Note that OK is fully encrypted so no selective cryptField() of
  537. * potentially sensitive fields is needed.
  538. *
  539. * ERROR has no payload.
  540. */
  541. VERB_HELLO = 0x01,
  542. /**
  543. * Error response:
  544. * <[1] in-re verb>
  545. * <[8] in-re packet ID>
  546. * <[1] error code>
  547. * <[...] error-dependent payload>
  548. */
  549. VERB_ERROR = 0x02,
  550. /**
  551. * Success response:
  552. * <[1] in-re verb>
  553. * <[8] in-re packet ID>
  554. * <[...] request-specific payload>
  555. */
  556. VERB_OK = 0x03,
  557. /**
  558. * Query an identity by address:
  559. * <[5] address to look up>
  560. * [<[...] additional addresses to look up>
  561. *
  562. * OK response payload:
  563. * <[...] binary serialized identity>
  564. * [<[...] additional binary serialized identities>]
  565. *
  566. * If querying a cluster, duplicate OK responses may occasionally occur.
  567. * These must be tolerated, which is easy since they'll have info you
  568. * already have.
  569. *
  570. * If the address is not found, no response is generated. The semantics
  571. * of WHOIS is similar to ARP and NDP in that persistent retrying can
  572. * be performed.
  573. */
  574. VERB_WHOIS = 0x04,
  575. /**
  576. * Relay-mediated NAT traversal or firewall punching initiation:
  577. * <[1] flags (unused, currently 0)>
  578. * <[5] ZeroTier address of peer that might be found at this address>
  579. * <[2] 16-bit protocol address port>
  580. * <[1] protocol address length (4 for IPv4, 16 for IPv6)>
  581. * <[...] protocol address (network byte order)>
  582. *
  583. * An upstream node can send this to inform both sides of a relay of
  584. * information they might use to establish a direct connection.
  585. *
  586. * Upon receipt a peer sends HELLO to establish a direct link.
  587. *
  588. * No OK or ERROR is generated.
  589. */
  590. VERB_RENDEZVOUS = 0x05,
  591. /**
  592. * ZT-to-ZT unicast ethernet frame (shortened EXT_FRAME):
  593. * <[8] 64-bit network ID>
  594. * <[2] 16-bit ethertype>
  595. * <[...] ethernet payload>
  596. *
  597. * MAC addresses are derived from the packet's source and destination
  598. * ZeroTier addresses. This is a shortened EXT_FRAME that elides full
  599. * Ethernet framing and other optional flags and features when they
  600. * are not necessary.
  601. *
  602. * ERROR may be generated if a membership certificate is needed for a
  603. * closed network. Payload will be network ID.
  604. */
  605. VERB_FRAME = 0x06,
  606. /**
  607. * Full Ethernet frame with MAC addressing and optional fields:
  608. * <[8] 64-bit network ID>
  609. * <[1] flags>
  610. * <[6] destination MAC or all zero for destination node>
  611. * <[6] source MAC or all zero for node of origin>
  612. * <[2] 16-bit ethertype>
  613. * <[...] ethernet payload>
  614. *
  615. * Flags:
  616. * 0x01 - Certificate of network membership attached (DEPRECATED)
  617. * 0x02 - Most significant bit of subtype (see below)
  618. * 0x04 - Middle bit of subtype (see below)
  619. * 0x08 - Least significant bit of subtype (see below)
  620. * 0x10 - ACK requested in the form of OK(EXT_FRAME)
  621. *
  622. * Subtypes (0..7):
  623. * 0x0 - Normal frame (bridging can be determined by checking MAC)
  624. * 0x1 - TEEd outbound frame
  625. * 0x2 - REDIRECTed outbound frame
  626. * 0x3 - WATCHed outbound frame (TEE with ACK, ACK bit also set)
  627. * 0x4 - TEEd inbound frame
  628. * 0x5 - REDIRECTed inbound frame
  629. * 0x6 - WATCHed inbound frame
  630. * 0x7 - (reserved for future use)
  631. *
  632. * An extended frame carries full MAC addressing, making it a
  633. * superset of VERB_FRAME. It is used for bridged traffic,
  634. * redirected or observed traffic via rules, and can in theory
  635. * be used for multicast though MULTICAST_FRAME exists for that
  636. * purpose and has additional options and capabilities.
  637. *
  638. * OK payload (if ACK flag is set):
  639. * <[8] 64-bit network ID>
  640. */
  641. VERB_EXT_FRAME = 0x07,
  642. /**
  643. * ECHO request (a.k.a. ping):
  644. * <[...] arbitrary payload>
  645. *
  646. * This generates OK with a copy of the transmitted payload. No ERROR
  647. * is generated. Response to ECHO requests is optional and ECHO may be
  648. * ignored if a node detects a possible flood.
  649. */
  650. VERB_ECHO = 0x08,
  651. /**
  652. * Announce interest in multicast group(s):
  653. * <[8] 64-bit network ID>
  654. * <[6] multicast Ethernet address>
  655. * <[4] multicast additional distinguishing information (ADI)>
  656. * [... additional tuples of network/address/adi ...]
  657. *
  658. * LIKEs may be sent to any peer, though a good implementation should
  659. * restrict them to peers on the same network they're for and to network
  660. * controllers and root servers. In the current network, root servers
  661. * will provide the service of final multicast cache.
  662. *
  663. * VERB_NETWORK_CREDENTIALS should be pushed along with this, especially
  664. * if using upstream (e.g. root) nodes as multicast databases. This allows
  665. * GATHERs to be authenticated.
  666. *
  667. * OK/ERROR are not generated.
  668. */
  669. VERB_MULTICAST_LIKE = 0x09,
  670. /**
  671. * Network credentials push:
  672. * [<[...] one or more certificates of membership>]
  673. * <[1] 0x00, null byte marking end of COM array>
  674. * <[2] 16-bit number of capabilities>
  675. * <[...] one or more serialized Capability>
  676. * <[2] 16-bit number of tags>
  677. * <[...] one or more serialized Tags>
  678. * <[2] 16-bit number of revocations>
  679. * <[...] one or more serialized Revocations>
  680. * <[2] 16-bit number of certificates of ownership>
  681. * <[...] one or more serialized CertificateOfOwnership>
  682. *
  683. * This can be sent by anyone at any time to push network credentials.
  684. * These will of course only be accepted if they are properly signed.
  685. * Credentials can be for any number of networks.
  686. *
  687. * The use of a zero byte to terminate the COM section is for legacy
  688. * backward compatibility. Newer fields are prefixed with a length.
  689. *
  690. * OK/ERROR are not generated.
  691. */
  692. VERB_NETWORK_CREDENTIALS = 0x0a,
  693. /**
  694. * Network configuration request:
  695. * <[8] 64-bit network ID>
  696. * <[2] 16-bit length of request meta-data dictionary>
  697. * <[...] string-serialized request meta-data>
  698. * <[8] 64-bit revision of netconf we currently have>
  699. * <[8] 64-bit timestamp of netconf we currently have>
  700. *
  701. * This message requests network configuration from a node capable of
  702. * providing it.
  703. *
  704. * Responses to this are always whole configs intended for the recipient.
  705. * For patches and other updates a NETWORK_CONFIG is sent instead.
  706. *
  707. * It would be valid and correct as of 1.2.0 to use NETWORK_CONFIG always,
  708. * but OK(NETWORK_CONFIG_REQUEST) should be sent for compatibility.
  709. *
  710. * OK response payload:
  711. * <[8] 64-bit network ID>
  712. * <[2] 16-bit length of network configuration dictionary chunk>
  713. * <[...] network configuration dictionary (may be incomplete)>
  714. * [ ... end of legacy single chunk response ... ]
  715. * <[1] 8-bit flags>
  716. * <[8] 64-bit config update ID (should never be 0)>
  717. * <[4] 32-bit total length of assembled dictionary>
  718. * <[4] 32-bit index of chunk>
  719. * [ ... end signed portion ... ]
  720. * <[1] 8-bit chunk signature type>
  721. * <[2] 16-bit length of chunk signature>
  722. * <[...] chunk signature>
  723. *
  724. * The chunk signature signs the entire payload of the OK response.
  725. * Currently only one signature type is supported: ed25519 (1).
  726. *
  727. * Each config chunk is signed to prevent memory exhaustion or
  728. * traffic crowding DOS attacks against config fragment assembly.
  729. *
  730. * If the packet is from the network controller it is permitted to end
  731. * before the config update ID or other chunking related or signature
  732. * fields. This is to support older controllers that don't include
  733. * these fields and may be removed in the future.
  734. *
  735. * ERROR response payload:
  736. * <[8] 64-bit network ID>
  737. * <[2] 16-bit length of error-related data (optional)>
  738. * <[...] error-related data (optional)>
  739. *
  740. * Error related data is a Dictionary containing things like a URL
  741. * for authentication or a human-readable error message, and is
  742. * optional and may be absent or empty.
  743. */
  744. VERB_NETWORK_CONFIG_REQUEST = 0x0b,
  745. /**
  746. * Network configuration data push:
  747. * <[8] 64-bit network ID>
  748. * <[2] 16-bit length of network configuration dictionary chunk>
  749. * <[...] network configuration dictionary (may be incomplete)>
  750. * <[1] 8-bit flags>
  751. * <[8] 64-bit config update ID (should never be 0)>
  752. * <[4] 32-bit total length of assembled dictionary>
  753. * <[4] 32-bit index of chunk>
  754. * [ ... end signed portion ... ]
  755. * <[1] 8-bit chunk signature type>
  756. * <[2] 16-bit length of chunk signature>
  757. * <[...] chunk signature>
  758. *
  759. * This is a direct push variant for network config updates. It otherwise
  760. * carries the same payload as OK(NETWORK_CONFIG_REQUEST) and has the same
  761. * semantics.
  762. *
  763. * The legacy mode missing the additional chunking fields is not supported
  764. * here.
  765. *
  766. * Flags:
  767. * 0x01 - Use fast propagation
  768. *
  769. * An OK should be sent if the config is successfully received and
  770. * accepted.
  771. *
  772. * OK payload:
  773. * <[8] 64-bit network ID>
  774. * <[8] 64-bit config update ID>
  775. */
  776. VERB_NETWORK_CONFIG = 0x0c,
  777. /**
  778. * Request endpoints for multicast distribution:
  779. * <[8] 64-bit network ID>
  780. * <[1] flags>
  781. * <[6] MAC address of multicast group being queried>
  782. * <[4] 32-bit ADI for multicast group being queried>
  783. * <[4] 32-bit requested max number of multicast peers>
  784. * [<[...] network certificate of membership>]
  785. *
  786. * Flags:
  787. * 0x01 - COM is attached
  788. *
  789. * This message asks a peer for additional known endpoints that have
  790. * LIKEd a given multicast group. It's sent when the sender wishes
  791. * to send multicast but does not have the desired number of recipient
  792. * peers.
  793. *
  794. * More than one OK response can occur if the response is broken up across
  795. * multiple packets or if querying a clustered node.
  796. *
  797. * The COM should be included so that upstream nodes that are not
  798. * members of our network can validate our request.
  799. *
  800. * OK response payload:
  801. * <[8] 64-bit network ID>
  802. * <[6] MAC address of multicast group being queried>
  803. * <[4] 32-bit ADI for multicast group being queried>
  804. * [begin gather results -- these same fields can be in OK(MULTICAST_FRAME)]
  805. * <[4] 32-bit total number of known members in this multicast group>
  806. * <[2] 16-bit number of members enumerated in this packet>
  807. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  808. *
  809. * ERROR is not generated; queries that return no response are dropped.
  810. */
  811. VERB_MULTICAST_GATHER = 0x0d,
  812. /**
  813. * Multicast frame:
  814. * <[8] 64-bit network ID>
  815. * <[1] flags>
  816. * [<[4] 32-bit implicit gather limit>]
  817. * [<[6] source MAC>]
  818. * <[6] destination MAC (multicast address)>
  819. * <[4] 32-bit multicast ADI (multicast address extension)>
  820. * <[2] 16-bit ethertype>
  821. * <[...] ethernet payload>
  822. *
  823. * Flags:
  824. * 0x01 - Network certificate of membership attached (DEPRECATED)
  825. * 0x02 - Implicit gather limit field is present
  826. * 0x04 - Source MAC is specified -- otherwise it's computed from sender
  827. * 0x08 - Please replicate (sent to multicast replicators)
  828. *
  829. * OK and ERROR responses are optional. OK may be generated if there are
  830. * implicit gather results or if the recipient wants to send its own
  831. * updated certificate of network membership to the sender. ERROR may be
  832. * generated if a certificate is needed or if multicasts to this group
  833. * are no longer wanted (multicast unsubscribe).
  834. *
  835. * OK response payload:
  836. * <[8] 64-bit network ID>
  837. * <[6] MAC address of multicast group>
  838. * <[4] 32-bit ADI for multicast group>
  839. * <[1] flags>
  840. * [<[...] network certificate of membership (DEPRECATED)>]
  841. * [<[...] implicit gather results if flag 0x01 is set>]
  842. *
  843. * OK flags (same bits as request flags):
  844. * 0x01 - OK includes certificate of network membership (DEPRECATED)
  845. * 0x02 - OK includes implicit gather results
  846. *
  847. * ERROR response payload:
  848. * <[8] 64-bit network ID>
  849. * <[6] multicast group MAC>
  850. * <[4] 32-bit multicast group ADI>
  851. */
  852. VERB_MULTICAST_FRAME = 0x0e,
  853. /**
  854. * Push of potential endpoints for direct communication:
  855. * <[2] 16-bit number of paths>
  856. * <[...] paths>
  857. *
  858. * Path record format:
  859. * <[1] 8-bit path flags>
  860. * <[2] length of extended path characteristics or 0 for none>
  861. * <[...] extended path characteristics>
  862. * <[1] address type>
  863. * <[1] address length in bytes>
  864. * <[...] address>
  865. *
  866. * Path record flags:
  867. * 0x01 - Forget this path if currently known (not implemented yet)
  868. * 0x02 - Cluster redirect -- use this in preference to others
  869. *
  870. * The receiver may, upon receiving a push, attempt to establish a
  871. * direct link to one or more of the indicated addresses. It is the
  872. * responsibility of the sender to limit which peers it pushes direct
  873. * paths to to those with whom it has a trust relationship. The receiver
  874. * must obey any restrictions provided such as exclusivity or blacklists.
  875. * OK responses to this message are optional.
  876. *
  877. * Note that a direct path push does not imply that learned paths can't
  878. * be used unless they are blacklisted explicitly or unless flag 0x01
  879. * is set.
  880. *
  881. * OK and ERROR are not generated.
  882. */
  883. VERB_PUSH_DIRECT_PATHS = 0x10,
  884. // 0x11 -- deprecated
  885. /**
  886. * An acknowledgment of receipt of a series of recent packets from another
  887. * peer. This is used to calculate relative throughput values and to detect
  888. * packet loss. Only VERB_FRAME and VERB_EXT_FRAME packets are counted.
  889. *
  890. * ACK response format:
  891. * <[4] 32-bit number of bytes received since last ACK>
  892. *
  893. * Upon receipt of this packet, the local peer will verify that the correct
  894. * number of bytes were received by the remote peer. If these values do
  895. * not agree that could be an indication of packet loss.
  896. *
  897. * Additionally, the local peer knows the interval of time that has
  898. * elapsed since the last received ACK. With this information it can compute
  899. * a rough estimate of the current throughput.
  900. *
  901. * This is sent at a maximum rate of once per every ZT_QOS_ACK_INTERVAL
  902. */
  903. VERB_ACK = 0x12,
  904. /**
  905. * A packet containing timing measurements useful for estimating path quality.
  906. * Composed of a list of <packet ID:internal sojourn time> pairs for an
  907. * arbitrary set of recent packets. This is used to sample for latency and
  908. * packet delay variance (PDV, "jitter").
  909. *
  910. * QoS record format:
  911. *
  912. * <[8] 64-bit packet ID of previously-received packet>
  913. * <[1] 8-bit packet sojourn time>
  914. * <...repeat until end of max 1400 byte packet...>
  915. *
  916. * The number of possible records per QoS packet is: (1400 * 8) / 72 = 155
  917. * This packet should be sent very rarely (every few seconds) as it can be
  918. * somewhat large if the connection is saturated. Future versions might use
  919. * a bloom table to probabilistically determine these values in a vastly
  920. * more space-efficient manner.
  921. *
  922. * Note: The 'internal packet sojourn time' is a slight misnomer as it is a
  923. * measure of the amount of time between when a packet was received and the
  924. * egress time of its tracking QoS packet.
  925. *
  926. * This is sent at a maximum rate of once per every
  927. * ZT_QOS_MEASUREMENT_INTERVAL
  928. */
  929. VERB_QOS_MEASUREMENT = 0x13,
  930. /**
  931. * A message with arbitrary user-definable content:
  932. * <[8] 64-bit arbitrary message type ID>
  933. * [<[...] message payload>]
  934. *
  935. * This can be used to send arbitrary messages over VL1. It generates no
  936. * OK or ERROR and has no special semantics outside of whatever the user
  937. * (via the ZeroTier core API) chooses to give it.
  938. *
  939. * Message type IDs less than or equal to 65535 are reserved for use by
  940. * ZeroTier, Inc. itself. We recommend making up random ones for your own
  941. * implementations.
  942. */
  943. VERB_USER_MESSAGE = 0x14,
  944. /**
  945. * A trace for remote debugging or diagnostics:
  946. * <[...] null-terminated dictionary containing trace information>
  947. * [<[...] additional null-terminated dictionaries>]
  948. *
  949. * This message contains a remote trace event. Remote trace events can
  950. * be sent to observers configured at the network level for those that
  951. * pertain directly to activity on a network, or to global observers if
  952. * locally configured.
  953. *
  954. * The instance ID is a random 64-bit value generated by each ZeroTier
  955. * node on startup. This is helpful in identifying traces from different
  956. * members of a cluster.
  957. */
  958. VERB_REMOTE_TRACE = 0x15,
  959. /**
  960. * A request to a peer to use a specific path in a multi-path scenario:
  961. * <[2] 16-bit unsigned integer that encodes a path choice utility>
  962. *
  963. * This is sent when a node operating in multipath mode observes that
  964. * its inbound and outbound traffic aren't going over the same path. The
  965. * node will compute its perceived utility for using its chosen outbound
  966. * path and send this to a peer in an attempt to petition it to send
  967. * its traffic over this same path.
  968. *
  969. * Scenarios:
  970. *
  971. * (1) Remote peer utility is GREATER than ours:
  972. * - Remote peer will refuse the petition and continue using current path
  973. * (2) Remote peer utility is LESS than than ours:
  974. * - Remote peer will accept the petition and switch to our chosen path
  975. * (3) Remote peer utility is EQUAL to our own:
  976. * - To prevent confusion and flapping, both side will agree to use the
  977. * numerical values of their identities to determine which path to use.
  978. * The peer with the greatest identity will win.
  979. *
  980. * If a node petitions a peer repeatedly with no effect it will regard
  981. * that as a refusal by the remote peer, in this case if the utility is
  982. * negligible it will voluntarily switch to the remote peer's chosen path.
  983. */
  984. VERB_PATH_NEGOTIATION_REQUEST = 0x16
  985. };
  986. /**
  987. * Error codes for VERB_ERROR
  988. */
  989. enum ErrorCode {
  990. /* No error, not actually used in transit */
  991. ERROR_NONE = 0x00,
  992. /* Invalid request */
  993. ERROR_INVALID_REQUEST = 0x01,
  994. /* Bad/unsupported protocol version */
  995. ERROR_BAD_PROTOCOL_VERSION = 0x02,
  996. /* Unknown object queried */
  997. ERROR_OBJ_NOT_FOUND = 0x03,
  998. /* HELLO pushed an identity whose address is already claimed */
  999. ERROR_IDENTITY_COLLISION = 0x04,
  1000. /* Verb or use case not supported/enabled by this node */
  1001. ERROR_UNSUPPORTED_OPERATION = 0x05,
  1002. /* Network membership certificate update needed */
  1003. ERROR_NEED_MEMBERSHIP_CERTIFICATE = 0x06,
  1004. /* Tried to join network, but you're not a member */
  1005. ERROR_NETWORK_ACCESS_DENIED_ = 0x07, /* extra _ at end to avoid Windows name conflict */
  1006. /* Multicasts to this group are not wanted */
  1007. ERROR_UNWANTED_MULTICAST = 0x08,
  1008. /* Network requires external or 2FA authentication (e.g. SSO). */
  1009. ERROR_NETWORK_AUTHENTICATION_REQUIRED = 0x09
  1010. };
  1011. template <unsigned int C2> Packet(const Buffer<C2>& b) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
  1012. {
  1013. }
  1014. Packet(const void* data, unsigned int len) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(data, len)
  1015. {
  1016. }
  1017. /**
  1018. * Construct a new empty packet with a unique random packet ID
  1019. *
  1020. * Flags and hops will be zero. Other fields and data region are undefined.
  1021. * Use the header access methods (setDestination() and friends) to fill out
  1022. * the header. Payload should be appended; initial size is header size.
  1023. */
  1024. Packet() : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
  1025. {
  1026. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV, 8), 8);
  1027. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
  1028. }
  1029. /**
  1030. * Make a copy of a packet with a new initialization vector and destination address
  1031. *
  1032. * This can be used to take one draft prototype packet and quickly make copies to
  1033. * encrypt for different destinations.
  1034. *
  1035. * @param prototype Prototype packet
  1036. * @param dest Destination ZeroTier address for new packet
  1037. */
  1038. Packet(const Packet& prototype, const Address& dest) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(prototype)
  1039. {
  1040. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV, 8), 8);
  1041. setDestination(dest);
  1042. }
  1043. /**
  1044. * Construct a new empty packet with a unique random packet ID
  1045. *
  1046. * @param dest Destination ZT address
  1047. * @param source Source ZT address
  1048. * @param v Verb
  1049. */
  1050. Packet(const Address& dest, const Address& source, const Verb v) : Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
  1051. {
  1052. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV, 8), 8);
  1053. setDestination(dest);
  1054. setSource(source);
  1055. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
  1056. setVerb(v);
  1057. }
  1058. /**
  1059. * Reset this packet structure for reuse in place
  1060. *
  1061. * @param dest Destination ZT address
  1062. * @param source Source ZT address
  1063. * @param v Verb
  1064. */
  1065. inline void reset(const Address& dest, const Address& source, const Verb v)
  1066. {
  1067. setSize(ZT_PROTO_MIN_PACKET_LENGTH);
  1068. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV, 8), 8);
  1069. setDestination(dest);
  1070. setSource(source);
  1071. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
  1072. setVerb(v);
  1073. }
  1074. /**
  1075. * Generate a new IV / packet ID in place
  1076. *
  1077. * This can be used to re-use a packet buffer multiple times to send
  1078. * technically different but otherwise identical copies of the same
  1079. * packet.
  1080. */
  1081. inline void newInitializationVector()
  1082. {
  1083. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV, 8), 8);
  1084. }
  1085. /**
  1086. * Set this packet's destination
  1087. *
  1088. * @param dest ZeroTier address of destination
  1089. */
  1090. inline void setDestination(const Address& dest)
  1091. {
  1092. dest.copyTo(field(ZT_PACKET_IDX_DEST, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  1093. }
  1094. /**
  1095. * Set this packet's source
  1096. *
  1097. * @param source ZeroTier address of source
  1098. */
  1099. inline void setSource(const Address& source)
  1100. {
  1101. source.copyTo(field(ZT_PACKET_IDX_SOURCE, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  1102. }
  1103. /**
  1104. * Get this packet's destination
  1105. *
  1106. * @return Destination ZT address
  1107. */
  1108. inline Address destination() const
  1109. {
  1110. return Address(field(ZT_PACKET_IDX_DEST, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  1111. }
  1112. /**
  1113. * Get this packet's source
  1114. *
  1115. * @return Source ZT address
  1116. */
  1117. inline Address source() const
  1118. {
  1119. return Address(field(ZT_PACKET_IDX_SOURCE, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  1120. }
  1121. /**
  1122. * @return True if packet is of valid length
  1123. */
  1124. inline bool lengthValid() const
  1125. {
  1126. return (size() >= ZT_PROTO_MIN_PACKET_LENGTH);
  1127. }
  1128. /**
  1129. * @return True if packet is fragmented (expect fragments)
  1130. */
  1131. inline bool fragmented() const
  1132. {
  1133. return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0);
  1134. }
  1135. /**
  1136. * Set this packet's fragmented flag
  1137. *
  1138. * @param f Fragmented flag value
  1139. */
  1140. inline void setFragmented(bool f)
  1141. {
  1142. if (f) {
  1143. (*this)[ZT_PACKET_IDX_FLAGS] |= (char)ZT_PROTO_FLAG_FRAGMENTED;
  1144. }
  1145. else {
  1146. (*this)[ZT_PACKET_IDX_FLAGS] &= (char)(~ZT_PROTO_FLAG_FRAGMENTED);
  1147. }
  1148. }
  1149. /**
  1150. * @return True if compressed (result only valid if unencrypted)
  1151. */
  1152. inline bool compressed() const
  1153. {
  1154. return (((unsigned char)(*this)[ZT_PACKET_IDX_VERB] & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0);
  1155. }
  1156. /**
  1157. * @return ZeroTier forwarding hops (0 to 7)
  1158. */
  1159. inline unsigned int hops() const
  1160. {
  1161. return ((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x07);
  1162. }
  1163. /**
  1164. * Increment this packet's hop count
  1165. */
  1166. inline void incrementHops()
  1167. {
  1168. unsigned char& b = (*this)[ZT_PACKET_IDX_FLAGS];
  1169. b = (b & 0xf8) | ((b + 1) & 0x07);
  1170. }
  1171. /**
  1172. * @return Cipher suite selector: 0 - 7 (see #defines)
  1173. */
  1174. inline unsigned int cipher() const
  1175. {
  1176. return (((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x38) >> 3);
  1177. }
  1178. /**
  1179. * @return Whether this packet is currently encrypted
  1180. */
  1181. inline bool isEncrypted() const
  1182. {
  1183. return (cipher() == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) || (cipher() == ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV);
  1184. }
  1185. /**
  1186. * Set this packet's cipher suite
  1187. */
  1188. inline void setCipher(unsigned int c)
  1189. {
  1190. unsigned char& b = (*this)[ZT_PACKET_IDX_FLAGS];
  1191. b = (b & 0xc7) | (unsigned char)((c << 3) & 0x38); // bits: FFCCCHHH
  1192. // Set DEPRECATED "encrypted" flag -- used by pre-1.0.3 peers
  1193. if (c == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) {
  1194. b |= ZT_PROTO_FLAG_ENCRYPTED;
  1195. }
  1196. else {
  1197. b &= (~ZT_PROTO_FLAG_ENCRYPTED);
  1198. }
  1199. }
  1200. /**
  1201. * Get the trusted path ID for this packet (only meaningful if cipher is trusted path)
  1202. *
  1203. * @return Trusted path ID (from MAC field)
  1204. */
  1205. inline uint64_t trustedPathId() const
  1206. {
  1207. return at<uint64_t>(ZT_PACKET_IDX_MAC);
  1208. }
  1209. /**
  1210. * Set this packet's trusted path ID and set the cipher spec to trusted path
  1211. *
  1212. * @param tpid Trusted path ID
  1213. */
  1214. inline void setTrusted(const uint64_t tpid)
  1215. {
  1216. setCipher(ZT_PROTO_CIPHER_SUITE__NO_CRYPTO_TRUSTED_PATH);
  1217. setAt(ZT_PACKET_IDX_MAC, tpid);
  1218. }
  1219. /**
  1220. * Get this packet's unique ID (the IV field interpreted as uint64_t)
  1221. *
  1222. * Note that the least significant 3 bits of this ID will change when armor()
  1223. * is called to armor the packet for transport. This is because armor() will
  1224. * mask the last 3 bits against the send counter for QoS monitoring use prior
  1225. * to actually using the IV to encrypt and MAC the packet. Be aware of this
  1226. * when grabbing the packetId of a new packet prior to armor/send.
  1227. *
  1228. * @return Packet ID
  1229. */
  1230. inline uint64_t packetId() const
  1231. {
  1232. return at<uint64_t>(ZT_PACKET_IDX_IV);
  1233. }
  1234. /**
  1235. * Set packet verb
  1236. *
  1237. * This also has the side-effect of clearing any verb flags, such as
  1238. * compressed, and so must only be done during packet composition.
  1239. *
  1240. * @param v New packet verb
  1241. */
  1242. inline void setVerb(Verb v)
  1243. {
  1244. (*this)[ZT_PACKET_IDX_VERB] = (char)v;
  1245. }
  1246. /**
  1247. * @return Packet verb (not including flag bits)
  1248. */
  1249. inline Verb verb() const
  1250. {
  1251. return (Verb)((*this)[ZT_PACKET_IDX_VERB] & 0x1f);
  1252. }
  1253. /**
  1254. * @return Length of packet payload
  1255. */
  1256. inline unsigned int payloadLength() const
  1257. {
  1258. return ((size() < ZT_PROTO_MIN_PACKET_LENGTH) ? 0 : (size() - ZT_PROTO_MIN_PACKET_LENGTH));
  1259. }
  1260. /**
  1261. * @return Raw packet payload
  1262. */
  1263. inline const unsigned char* payload() const
  1264. {
  1265. return field(ZT_PACKET_IDX_PAYLOAD, size() - ZT_PACKET_IDX_PAYLOAD);
  1266. }
  1267. /**
  1268. * Armor packet for transport
  1269. *
  1270. * @param key 32-byte key
  1271. * @param encryptPayload If true, encrypt packet payload, else just MAC
  1272. * @param aesKeys If non-NULL these are the two keys for AES-GMAC-SIV
  1273. */
  1274. void armor(const void* key, bool encryptPayload, const AES aesKeys[2]);
  1275. /**
  1276. * Verify and (if encrypted) decrypt packet
  1277. *
  1278. * This does not handle trusted path mode packets and will return false
  1279. * for these. These are handled in IncomingPacket if the sending physical
  1280. * address and MAC field match a trusted path.
  1281. *
  1282. * @param key 32-byte key
  1283. * @param aesKeys If non-NULL these are the two keys for AES-GMAC-SIV
  1284. * @return False if packet is invalid or failed MAC authenticity check
  1285. */
  1286. bool dearmor(const void* key, const AES aesKeys[2]);
  1287. /**
  1288. * Encrypt/decrypt a separately armored portion of a packet
  1289. *
  1290. * This is currently only used to mask portions of HELLO as an extra
  1291. * security precaution since most of that message is sent in the clear.
  1292. *
  1293. * This must NEVER be used more than once in the same packet, as doing
  1294. * so will result in re-use of the same key stream.
  1295. *
  1296. * @param key 32-byte key
  1297. * @param start Start of encrypted portion
  1298. * @param len Length of encrypted portion
  1299. */
  1300. void cryptField(const void* key, unsigned int start, unsigned int len);
  1301. /**
  1302. * Attempt to compress payload if not already (must be unencrypted)
  1303. *
  1304. * This requires that the payload at least contain the verb byte already
  1305. * set. The compressed flag in the verb is set if compression successfully
  1306. * results in a size reduction. If no size reduction occurs, compression
  1307. * is not done and the flag is left cleared.
  1308. *
  1309. * @return True if compression occurred
  1310. */
  1311. bool compress();
  1312. /**
  1313. * Attempt to decompress payload if it is compressed (must be unencrypted)
  1314. *
  1315. * If payload is compressed, it is decompressed and the compressed verb
  1316. * flag is cleared. Otherwise nothing is done and true is returned.
  1317. *
  1318. * @return True if data is now decompressed and valid, false on error
  1319. */
  1320. bool uncompress();
  1321. private:
  1322. static const unsigned char ZERO_KEY[32];
  1323. /**
  1324. * Deterministically mangle a 256-bit crypto key based on packet
  1325. *
  1326. * This uses extra data from the packet to mangle the secret, giving us an
  1327. * effective IV that is somewhat more than 64 bits. This is "free" for
  1328. * Salsa20 since it has negligible key setup time so using a different
  1329. * key each time is fine.
  1330. *
  1331. * @param in Input key (32 bytes)
  1332. * @param out Output buffer (32 bytes)
  1333. */
  1334. inline void _salsa20MangleKey(const unsigned char* in, unsigned char* out) const
  1335. {
  1336. const unsigned char* d = (const unsigned char*)data();
  1337. // IV and source/destination addresses. Using the addresses divides the
  1338. // key space into two halves-- A->B and B->A (since order will change).
  1339. for (unsigned int i = 0; i < 18; ++i) { // 8 + (ZT_ADDRESS_LENGTH * 2) == 18
  1340. out[i] = in[i] ^ d[i];
  1341. }
  1342. // Flags, but with hop count masked off. Hop count is altered by forwarding
  1343. // nodes. It's one of the only parts of a packet modifiable by people
  1344. // without the key.
  1345. out[18] = in[18] ^ (d[ZT_PACKET_IDX_FLAGS] & 0xf8);
  1346. // Raw packet size in bytes -- thus each packet size defines a new
  1347. // key space.
  1348. out[19] = in[19] ^ (unsigned char)(size() & 0xff);
  1349. out[20] = in[20] ^ (unsigned char)((size() >> 8) & 0xff); // little endian
  1350. // Rest of raw key is used unchanged
  1351. for (unsigned int i = 21; i < 32; ++i) {
  1352. out[i] = in[i];
  1353. }
  1354. }
  1355. };
  1356. } // namespace ZeroTier
  1357. #endif