Packet.hpp 51 KB

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