Packet.hpp 51 KB

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