Packet.hpp 50 KB

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