Packet.hpp 53 KB

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