Packet.hpp 48 KB

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