Packet.hpp 54 KB

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