Packet.hpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_N_PACKET_HPP
  28. #define ZT_N_PACKET_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <string>
  33. #include <iostream>
  34. #include "Constants.hpp"
  35. #include "Address.hpp"
  36. #include "Poly1305.hpp"
  37. #include "Salsa20.hpp"
  38. #include "Utils.hpp"
  39. #include "Buffer.hpp"
  40. #include "../ext/lz4/lz4.h"
  41. /**
  42. * Protocol version -- incremented only for MAJOR changes
  43. *
  44. * 1 - 0.2.0 ... 0.2.5
  45. * 2 - 0.3.0 ... 0.4.5
  46. * * Added signature and originating peer to multicast frame
  47. * * Double size of multicast frame bloom filter
  48. * 3 - 0.5.0 ... 0.6.0
  49. * * Yet another multicast redesign
  50. * * New crypto completely changes key agreement cipher
  51. * 4 - 0.6.0 ... CURRENT
  52. * * New identity format based on hashcash design
  53. *
  54. * This isn't going to change again for a long time unless your
  55. * author wakes up again at 4am with another great idea. :P
  56. */
  57. #define ZT_PROTO_VERSION 4
  58. /**
  59. * Minimum supported protocol version
  60. */
  61. #define ZT_PROTO_VERSION_MIN 4
  62. /**
  63. * Maximum hop count allowed by packet structure (3 bits, 0-7)
  64. *
  65. * This is not necessarily the maximum hop counter after which
  66. * relaying is no longer performed.
  67. */
  68. #define ZT_PROTO_MAX_HOPS 7
  69. /**
  70. * Cipher suite: Curve25519/Poly1305/Salsa20/12 without payload encryption
  71. *
  72. * This specifies Poly1305 MAC using a 32-bit key derived from the first
  73. * 32 bytes of a Salsa20/12 keystream as in the Salsa20/12 cipher suite,
  74. * but the payload is not encrypted. This is currently only used to send
  75. * HELLO since that's the public key specification packet and must be
  76. * sent in the clear. Key agreement is performed using Curve25519 elliptic
  77. * curve Diffie-Hellman.
  78. */
  79. #define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE 0
  80. /**
  81. * Cipher suite: Curve25519/Poly1305/Salsa20/12
  82. *
  83. * This specifies Poly1305 using the first 32 bytes of a Salsa20/12 key
  84. * stream as its one-time-use key followed by payload encryption with
  85. * the remaining Salsa20/12 key stream. Key agreement is performed using
  86. * Curve25519 elliptic curve Diffie-Hellman.
  87. */
  88. #define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 1
  89. /**
  90. * Cipher suite: Curve25519/AES256-GCM
  91. *
  92. * This specifies AES256 in GCM mode using GCM's built-in authentication
  93. * with Curve25519 elliptic curve Diffie-Hellman.
  94. *
  95. * (Not implemented yet in client but reserved for future use.)
  96. */
  97. #define ZT_PROTO_CIPHER_SUITE__C25519_AES256_GCM 2
  98. /**
  99. * Header flag indicating that a packet is fragmented
  100. *
  101. * If this flag is set, the receiver knows to expect more than one fragment.
  102. * See Packet::Fragment for details.
  103. */
  104. #define ZT_PROTO_FLAG_FRAGMENTED 0x40
  105. /**
  106. * Verb flag indicating payload is compressed with LZ4
  107. */
  108. #define ZT_PROTO_VERB_FLAG_COMPRESSED 0x80
  109. /**
  110. * Rounds used for Salsa20 encryption in ZT
  111. */
  112. #define ZT_PROTO_SALSA20_ROUNDS 12
  113. // Indices of fields in normal packet header -- do not change as this
  114. // might require both code rework and will break compatibility.
  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_UDP_DEFAULT_PAYLOAD_MTU)
  126. /**
  127. * Minimum viable packet length (also length of header)
  128. */
  129. #define ZT_PROTO_MIN_PACKET_LENGTH ZT_PACKET_IDX_PAYLOAD
  130. // Indexes of fields in fragment header -- also can't be changed without
  131. // breaking compatibility.
  132. #define ZT_PACKET_FRAGMENT_IDX_PACKET_ID 0
  133. #define ZT_PACKET_FRAGMENT_IDX_DEST 8
  134. #define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR 13
  135. #define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO 14
  136. #define ZT_PACKET_FRAGMENT_IDX_HOPS 15
  137. #define ZT_PACKET_FRAGMENT_IDX_PAYLOAD 16
  138. /**
  139. * Value found at ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR in fragments
  140. */
  141. #define ZT_PACKET_FRAGMENT_INDICATOR ZT_ADDRESS_RESERVED_PREFIX
  142. /**
  143. * Minimum viable fragment length
  144. */
  145. #define ZT_PROTO_MIN_FRAGMENT_LENGTH ZT_PACKET_FRAGMENT_IDX_PAYLOAD
  146. /**
  147. * Length of LAN beacon packets
  148. */
  149. #define ZT_PROTO_BEACON_LENGTH 13
  150. /**
  151. * Index of address in a LAN beacon
  152. */
  153. #define ZT_PROTO_BEACON_IDX_ADDRESS 8
  154. // Field incides for parsing verbs -------------------------------------------
  155. // Some verbs have variable-length fields. Those aren't fully defined here
  156. // yet-- instead they are parsed using relative indexes in IncomingPacket.
  157. // See their respective handler functions.
  158. #define ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION (ZT_PACKET_IDX_PAYLOAD)
  159. #define ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION + 1)
  160. #define ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION + 1)
  161. #define ZT_PROTO_VERB_HELLO_IDX_REVISION (ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION + 1)
  162. #define ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP (ZT_PROTO_VERB_HELLO_IDX_REVISION + 2)
  163. #define ZT_PROTO_VERB_HELLO_IDX_IDENTITY (ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP + 8)
  164. #define ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
  165. #define ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB + 1)
  166. #define ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE (ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID + 8)
  167. #define ZT_PROTO_VERB_ERROR_IDX_PAYLOAD (ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE + 1)
  168. #define ZT_PROTO_VERB_OK_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
  169. #define ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_OK_IDX_IN_RE_VERB + 1)
  170. #define ZT_PROTO_VERB_OK_IDX_PAYLOAD (ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID + 8)
  171. #define ZT_PROTO_VERB_WHOIS_IDX_ZTADDRESS (ZT_PACKET_IDX_PAYLOAD)
  172. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS (ZT_PACKET_IDX_PAYLOAD)
  173. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS + 1)
  174. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT (ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS + 5)
  175. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN (ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT + 2)
  176. #define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN + 1)
  177. #define ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  178. #define ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID + 8)
  179. #define ZT_PROTO_VERB_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE + 2)
  180. #define ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  181. #define ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID 8
  182. #define ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS (ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID + ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID)
  183. #define ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS 1
  184. #define ZT_PROTO_VERB_EXT_FRAME_IDX_COM (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
  185. #define ZT_PROTO_VERB_EXT_FRAME_IDX_TO (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
  186. #define ZT_PROTO_VERB_EXT_FRAME_LEN_TO 6
  187. #define ZT_PROTO_VERB_EXT_FRAME_IDX_FROM (ZT_PROTO_VERB_EXT_FRAME_IDX_TO + ZT_PROTO_VERB_EXT_FRAME_LEN_TO)
  188. #define ZT_PROTO_VERB_EXT_FRAME_LEN_FROM 6
  189. #define ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_EXT_FRAME_IDX_FROM + ZT_PROTO_VERB_EXT_FRAME_LEN_FROM)
  190. #define ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE 2
  191. #define ZT_PROTO_VERB_EXT_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE + ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE)
  192. // P5_MULTICAST_FRAME is deprecated
  193. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_DEPTH (ZT_PACKET_IDX_PAYLOAD)
  194. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_DEPTH 2
  195. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_FIFO (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_DEPTH + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_DEPTH)
  196. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_FIFO 320
  197. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_BLOOM (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_FIFO + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_FIFO)
  198. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_BLOOM 1024
  199. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FLAGS (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_BLOOM + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_BLOOM)
  200. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_FLAGS 1
  201. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX__START_OF_SIGNED_PORTION (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FLAGS)
  202. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_NETWORK_ID (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FLAGS + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_FLAGS)
  203. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_NETWORK_ID 8
  204. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_BLOOM_NONCE (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_NETWORK_ID + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_NETWORK_ID)
  205. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_BLOOM_NONCE 2
  206. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_PREFIX_BITS (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_BLOOM_NONCE + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_BLOOM_NONCE)
  207. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_PREFIX_BITS 1
  208. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_PREFIX (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_PREFIX_BITS + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_PREFIX_BITS)
  209. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_PREFIX 1
  210. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ORIGIN (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_PROPAGATION_PREFIX + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_PROPAGATION_PREFIX)
  211. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ORIGIN 5
  212. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ORIGIN_MCID (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ORIGIN + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ORIGIN)
  213. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ORIGIN_MCID 3
  214. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_GUID (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ORIGIN)
  215. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_GUID 8
  216. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_SOURCE_MAC (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ORIGIN_MCID + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ORIGIN_MCID)
  217. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_SOURCE_MAC 6
  218. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_DEST_MAC (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_SOURCE_MAC + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_SOURCE_MAC)
  219. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_DEST_MAC 6
  220. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_DEST_ADI (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_DEST_MAC + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_DEST_MAC)
  221. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_DEST_ADI 4
  222. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_DEST_ADI + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_DEST_ADI)
  223. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ETHERTYPE 2
  224. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FRAME_LEN (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_ETHERTYPE + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_ETHERTYPE)
  225. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_FRAME_LEN 2
  226. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FRAME (ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX_FRAME_LEN + ZT_PROTO_VERB_P5_MULTICAST_FRAME_LEN_FRAME_LEN)
  227. #define ZT_PROTO_VERB_P5_MULTICAST_FRAME_FLAGS_HAS_MEMBERSHIP_CERTIFICATE 0x01
  228. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  229. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID + 8)
  230. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN + 2)
  231. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  232. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID + 8)
  233. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS + 1)
  234. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC + 6)
  235. #define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI + 4)
  236. // Note: COM, GATHER_LIMIT, and SOURCE_MAC are optional, and so are specified without size
  237. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
  238. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID + 8)
  239. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_COM (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  240. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  241. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_SOURCE_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  242. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
  243. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC + 6)
  244. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI + 4)
  245. #define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FRAME (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE + 2)
  246. #define ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  247. #define ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP + 8)
  248. #define ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION + 1)
  249. #define ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION + 1)
  250. #define ZT_PROTO_VERB_HELLO__OK__IDX_REVISION (ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION + 1)
  251. #define ZT_PROTO_VERB_WHOIS__OK__IDX_IDENTITY (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  252. #define ZT_PROTO_VERB_WHOIS__ERROR__IDX_ZTADDRESS (ZT_PROTO_VERB_ERROR_IDX_PAYLOAD)
  253. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  254. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID + 8)
  255. #define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN + 2)
  256. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  257. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID + 8)
  258. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC + 6)
  259. #define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI + 4)
  260. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
  261. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID + 8)
  262. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC + 6)
  263. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI + 4)
  264. #define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_COM_AND_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS + 1)
  265. // ---------------------------------------------------------------------------
  266. namespace ZeroTier {
  267. /**
  268. * ZeroTier packet
  269. *
  270. * Packet format:
  271. * <[8] random initialization vector (doubles as 64-bit packet ID)>
  272. * <[5] destination ZT address>
  273. * <[5] source ZT address>
  274. * <[1] flags/cipher (top 5 bits) and ZT hop count (last 3 bits)>
  275. * <[8] 8-bit MAC (currently first 8 bytes of poly1305 tag)>
  276. * [... -- begin encryption envelope -- ...]
  277. * <[1] encrypted flags (top 3 bits) and verb (last 5 bits)>
  278. * [... verb-specific payload ...]
  279. *
  280. * Packets smaller than 28 bytes are invalid and silently discarded.
  281. *
  282. * The flags/cipher/hops bit field is: FFCCCHHH where C is a 3-bit cipher
  283. * selection allowing up to 8 cipher suites, F is flags (reserved, currently
  284. * all zero), and H is hop count.
  285. *
  286. * The three-bit hop count is the only part of a packet that is mutable in
  287. * transit without invalidating the MAC. All other bits in the packet are
  288. * immutable. This is because intermediate nodes can increment the hop
  289. * count up to 7 (protocol max).
  290. *
  291. * http://tonyarcieri.com/all-the-crypto-code-youve-ever-written-is-probably-broken
  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. * Beacon format and beacon packets:
  297. * <[8] 8 random bytes>
  298. * <[5] sender ZT address>
  299. *
  300. * A beacon is a 13-byte packet containing only the address of the sender.
  301. * Receiving peers may or may not respond to beacons with a HELLO or other
  302. * message to initiate direct communication.
  303. *
  304. * Beacons may be used for direct LAN announcement or NAT traversal.
  305. */
  306. class Packet : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH>
  307. {
  308. public:
  309. /**
  310. * A packet fragment
  311. *
  312. * Fragments are sent if a packet is larger than UDP MTU. The first fragment
  313. * is sent with its normal header with the fragmented flag set. Remaining
  314. * fragments are sent this way.
  315. *
  316. * The fragmented bit indicates that there is at least one fragment. Fragments
  317. * themselves contain the total, so the receiver must "learn" this from the
  318. * first fragment it receives.
  319. *
  320. * Fragments are sent with the following format:
  321. * <[8] packet ID of packet whose fragment this belongs to>
  322. * <[5] destination ZT address>
  323. * <[1] 0xff, a reserved address, signals that this isn't a normal packet>
  324. * <[1] total fragments (most significant 4 bits), fragment no (LS 4 bits)>
  325. * <[1] ZT hop count (top 5 bits unused and must be zero)>
  326. * <[...] fragment data>
  327. *
  328. * The protocol supports a maximum of 16 fragments. If a fragment is received
  329. * before its main packet header, it should be cached for a brief period of
  330. * time to see if its parent arrives. Loss of any fragment constitutes packet
  331. * loss; there is no retransmission mechanism. The receiver must wait for full
  332. * receipt to authenticate and decrypt; there is no per-fragment MAC. (But if
  333. * fragments are corrupt, the MAC will fail for the whole assembled packet.)
  334. */
  335. class Fragment : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH>
  336. {
  337. public:
  338. Fragment() :
  339. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>()
  340. {
  341. }
  342. template<unsigned int C2>
  343. Fragment(const Buffer<C2> &b)
  344. throw(std::out_of_range) :
  345. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
  346. {
  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. * @throws std::out_of_range Packet size would exceed buffer
  357. */
  358. Fragment(const Packet &p,unsigned int fragStart,unsigned int fragLen,unsigned int fragNo,unsigned int fragTotal)
  359. throw(std::out_of_range)
  360. {
  361. init(p,fragStart,fragLen,fragNo,fragTotal);
  362. }
  363. /**
  364. * Initialize from a packet
  365. *
  366. * @param p Original assembled packet
  367. * @param fragStart Start of fragment (raw index in packet data)
  368. * @param fragLen Length of fragment in bytes
  369. * @param fragNo Which fragment (>= 1, since 0 is Packet with end chopped off)
  370. * @param fragTotal Total number of fragments (including 0)
  371. * @throws std::out_of_range Packet size would exceed buffer
  372. */
  373. inline void init(const Packet &p,unsigned int fragStart,unsigned int fragLen,unsigned int fragNo,unsigned int fragTotal)
  374. throw(std::out_of_range)
  375. {
  376. if ((fragStart + fragLen) > p.size())
  377. throw std::out_of_range("Packet::Fragment: tried to construct fragment of packet past its length");
  378. setSize(fragLen + ZT_PROTO_MIN_FRAGMENT_LENGTH);
  379. // NOTE: this copies both the IV/packet ID and the destination address.
  380. memcpy(field(ZT_PACKET_FRAGMENT_IDX_PACKET_ID,13),p.field(ZT_PACKET_IDX_IV,13),13);
  381. (*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] = ZT_PACKET_FRAGMENT_INDICATOR;
  382. (*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO] = (char)(((fragTotal & 0xf) << 4) | (fragNo & 0xf));
  383. (*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = 0;
  384. memcpy(field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD,fragLen),p.field(fragStart,fragLen),fragLen);
  385. }
  386. /**
  387. * Get this fragment's destination
  388. *
  389. * @return Destination ZT address
  390. */
  391. inline Address destination() const { return Address(field(ZT_PACKET_FRAGMENT_IDX_DEST,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
  392. /**
  393. * @return True if fragment is of a valid length
  394. */
  395. inline bool lengthValid() const { return (size() >= ZT_PACKET_FRAGMENT_IDX_PAYLOAD); }
  396. /**
  397. * @return ID of packet this is a fragment of
  398. */
  399. inline uint64_t packetId() const { return at<uint64_t>(ZT_PACKET_FRAGMENT_IDX_PACKET_ID); }
  400. /**
  401. * @return Total number of fragments in packet
  402. */
  403. inline unsigned int totalFragments() const { return (((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) >> 4) & 0xf); }
  404. /**
  405. * @return Fragment number of this fragment
  406. */
  407. inline unsigned int fragmentNumber() const { return ((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) & 0xf); }
  408. /**
  409. * @return Fragment ZT hop count
  410. */
  411. inline unsigned int hops() const { return (unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]); }
  412. /**
  413. * Increment this packet's hop count
  414. */
  415. inline void incrementHops()
  416. {
  417. (*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = (((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]) + 1) & ZT_PROTO_MAX_HOPS;
  418. }
  419. /**
  420. * @return Length of payload in bytes
  421. */
  422. inline unsigned int payloadLength() const { return ((size() > ZT_PACKET_FRAGMENT_IDX_PAYLOAD) ? (size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD) : 0); }
  423. /**
  424. * @return Raw packet payload
  425. */
  426. inline const unsigned char *payload() const
  427. {
  428. return field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD,size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD);
  429. }
  430. };
  431. /**
  432. * ZeroTier protocol verbs
  433. */
  434. enum Verb /* Max value: 32 (5 bits) */
  435. {
  436. /* No operation, payload ignored, no reply */
  437. VERB_NOP = 0,
  438. /* Announcement of a node's existence:
  439. * <[1] protocol version>
  440. * <[1] software major version>
  441. * <[1] software minor version>
  442. * <[2] software revision>
  443. * <[8] timestamp (ms since epoch)>
  444. * <[...] binary serialized identity (see Identity)>
  445. *
  446. * OK payload:
  447. * <[8] timestamp (echoed from original HELLO)>
  448. * <[1] protocol version (of responder)>
  449. * <[1] software major version (of responder)>
  450. * <[1] software minor version (of responder)>
  451. * <[2] software revision (of responder)>
  452. *
  453. * ERROR has no payload.
  454. */
  455. VERB_HELLO = 1,
  456. /* Error response:
  457. * <[1] in-re verb>
  458. * <[8] in-re packet ID>
  459. * <[1] error code>
  460. * <[...] error-dependent payload>
  461. */
  462. VERB_ERROR = 2,
  463. /* Success response:
  464. * <[1] in-re verb>
  465. * <[8] in-re packet ID>
  466. * <[...] request-specific payload>
  467. */
  468. VERB_OK = 3,
  469. /* Query an identity by address:
  470. * <[5] address to look up>
  471. *
  472. * OK response payload:
  473. * <[...] binary serialized identity>
  474. *
  475. * ERROR response payload:
  476. * <[5] address>
  477. */
  478. VERB_WHOIS = 4,
  479. /* Meet another node at a given protocol address:
  480. * <[1] flags (unused, currently 0)>
  481. * <[5] ZeroTier address of peer that might be found at this address>
  482. * <[2] 16-bit protocol address port>
  483. * <[1] protocol address length (4 for IPv4, 16 for IPv6)>
  484. * <[...] protocol address (network byte order)>
  485. *
  486. * This is sent by a relaying node to initiate NAT traversal between two
  487. * peers that are communicating by way of indirect relay. The relay will
  488. * send this to both peers at the same time on a periodic basis, telling
  489. * each where it might find the other on the network.
  490. *
  491. * Upon receipt a peer sends HELLO to establish a direct link.
  492. *
  493. * Nodes should implement rate control, limiting the rate at which they
  494. * respond to these packets to prevent their use in DDOS attacks. Nodes
  495. * may also ignore these messages if a peer is not known or is not being
  496. * actively communicated with.
  497. *
  498. * No OK or ERROR is generated.
  499. */
  500. VERB_RENDEZVOUS = 5,
  501. /* ZT-to-ZT unicast ethernet frame (shortened EXT_FRAME):
  502. * <[8] 64-bit network ID>
  503. * <[2] 16-bit ethertype>
  504. * <[...] ethernet payload>
  505. *
  506. * MAC addresses are derived from the packet's source and destination
  507. * ZeroTier addresses. This is a shortened EXT_FRAME that elides full
  508. * Ethernet framing and other optional flags and features when they
  509. * are not necessary.
  510. *
  511. * ERROR may be generated if a membership certificate is needed for a
  512. * closed network. Payload will be network ID.
  513. */
  514. VERB_FRAME = 6,
  515. /* Full Ethernet frame with MAC addressing and optional fields:
  516. * <[8] 64-bit network ID>
  517. * <[1] flags>
  518. * [<[...] certificate of network membership>]
  519. * <[6] destination MAC or all zero for destination node>
  520. * <[6] source MAC or all zero for node of origin>
  521. * <[2] 16-bit ethertype>
  522. * <[...] ethernet payload>
  523. *
  524. * Flags:
  525. * 0x01 - Certificate of network membership is attached
  526. *
  527. * An extended frame carries full MAC addressing, making them a
  528. * superset of VERB_FRAME. They're used for bridging or when we
  529. * want to attach a certificate since FRAME does not support that.
  530. *
  531. * Multicast frames may not be sent as EXT_FRAME.
  532. *
  533. * ERROR may be generated if a membership certificate is needed for a
  534. * closed network. Payload will be network ID.
  535. */
  536. VERB_EXT_FRAME = 7,
  537. /* A multicast frame [old multicast protocol, deprecated]:
  538. * <[2] 16-bit propagation depth or 0xffff for "do not forward">
  539. * <[320] propagation FIFO>
  540. * <[1024] propagation bloom filter>
  541. * [... begin signed portion ...]
  542. * <[1] 8-bit flags, currently unused and must be 0>
  543. * <[8] 64-bit network ID>
  544. * <[2] 16-bit random propagation bloom filter nonce>
  545. * <[1] number of significant bits in propagation restrict prefix>
  546. * <[1] propagation restriction prefix (sig bits right to left)>
  547. * <[5] ZeroTier address of node of origin>
  548. * <[3] 24-bit multicast ID, together with origin forms GUID>
  549. * <[6] source MAC address>
  550. * <[6] destination multicast group MAC address>
  551. * <[4] destination multicast group ADI field>
  552. * <[2] 16-bit frame ethertype>
  553. * <[2] 16-bit length of payload>
  554. * <[...] ethernet frame payload>
  555. * [... end of signed portion ...]
  556. * <[2] 16-bit length of signature>
  557. * <[...] signature (currently Ed25519/SHA-512, 96 bytes in length)>
  558. * [<[...] network membership certificate (optional)>]
  559. *
  560. * Flags:
  561. * 0x01 - Multicast frame includes network membership certificate
  562. * for original sender for this network.
  563. *
  564. * When a multicast frame is received:
  565. *
  566. * (1) Check the signature of the signed portion of packet, discard on fail
  567. * (2) Check for duplicate multicast, STOP if duplicate
  568. * (3) Check rate limits, STOP if over limit
  569. * (4) Inject into tap if member of network and packet passes other checks
  570. * (5) Increment propagation depth, STOP if over limit
  571. * (6) Pop topmost element off FIFO -- this is next hop
  572. * (7) Push suggested next hops onto FIFO until full -- set corresponding
  573. * bits in bloom filter
  574. * (8) Send to next hop, or to a supernode if none
  575. *
  576. * When choosing next hops, exclude addresses corresponding to bits already
  577. * set in the bloom filter and addresses outside the propagation restrict
  578. * prefix.
  579. *
  580. * Active bridges on a network are always added as next hops for all
  581. * multicast and broadcast traffic, as if they "like" all groups.
  582. *
  583. * Algorithm for setting bits in bloom filter:
  584. *
  585. * (1) Place the address in the least significant 40 bits of a 64-bit int.
  586. * (2) Add the bloom filter nonce to this value.
  587. * (3) XOR the least significant 13 bits of this value with the next most
  588. * significant 13 bits and so on, 4 times.
  589. * (4) This value ANDed with 0x1fff is the bit to set in the bloom filter.
  590. * (5) Set this bit via: byte[bit >> 3] |= (0x80 >> (bit & 7))
  591. *
  592. * To check bits in bloom filter perform the same computation but mask the
  593. * bit instead of ORing it.
  594. *
  595. * Propagation occurs within a restrict prefix. The restrict prefix is
  596. * applied to the least significant 16 bits of an address. The original
  597. * sender of the multicast sets the restrict prefix and sends 2^N copies
  598. * of the multicast frame, one for each address prefix.
  599. *
  600. * ERROR may be generated if a membership certificate is needed for a
  601. * closed network. Payload will be network ID.
  602. */
  603. VERB_P5_MULTICAST_FRAME = 8,
  604. /* Announce interest in multicast group(s):
  605. * <[8] 64-bit network ID>
  606. * <[6] multicast Ethernet address>
  607. * <[4] multicast additional distinguishing information (ADI)>
  608. * [... additional tuples of network/address/adi ...]
  609. *
  610. * LIKEs are sent to peers with whom you have a direct peer to peer
  611. * connection, and always including supernodes.
  612. *
  613. * OK/ERROR are not generated.
  614. */
  615. VERB_MULTICAST_LIKE = 9,
  616. /* Network member certificate replication/push:
  617. * <[...] serialized certificate of membership>
  618. * [ ... additional certificates may follow ...]
  619. *
  620. * Certificate contains network ID, peer it was issued for, etc.
  621. *
  622. * OK/ERROR are not generated.
  623. */
  624. VERB_NETWORK_MEMBERSHIP_CERTIFICATE = 10,
  625. /* Network configuration request:
  626. * <[8] 64-bit network ID>
  627. * <[2] 16-bit length of request meta-data dictionary>
  628. * <[...] string-serialized request meta-data>
  629. * [<[8] 64-bit timestamp of netconf we currently have>]
  630. *
  631. * This message requests network configuration from a node capable of
  632. * providing it. If the optional timestamp is included, a response is
  633. * only generated if there is a newer network configuration available.
  634. *
  635. * OK response payload:
  636. * <[8] 64-bit network ID>
  637. * <[2] 16-bit length of network configuration dictionary>
  638. * <[...] network configuration dictionary>
  639. *
  640. * OK returns a Dictionary (string serialized) containing the network's
  641. * configuration and IP address assignment information for the querying
  642. * node. It also contains a membership certificate that the querying
  643. * node can push to other peers to demonstrate its right to speak on
  644. * a given network.
  645. *
  646. * When a new network configuration is received, another config request
  647. * should be sent with the new netconf's timestamp. This confirms receipt
  648. * and also causes any subsequent changes to rapidly propagate as this
  649. * cycle will repeat until there are no changes. This is optional but
  650. * recommended behavior.
  651. *
  652. * ERROR response payload:
  653. * <[8] 64-bit network ID>
  654. *
  655. * UNSUPPORTED_OPERATION is returned if this service is not supported,
  656. * and OBJ_NOT_FOUND if the queried network ID was not found.
  657. */
  658. VERB_NETWORK_CONFIG_REQUEST = 11,
  659. /* Network configuration refresh request:
  660. * <[...] array of 64-bit network IDs>
  661. *
  662. * This message can be sent by the network configuration master node
  663. * to request that nodes refresh their network configuration. It can
  664. * thus be used to "push" updates so that network config changes will
  665. * take effect quickly.
  666. *
  667. * It does not generate an OK or ERROR message, and is treated only as
  668. * a hint to refresh now.
  669. */
  670. VERB_NETWORK_CONFIG_REFRESH = 12,
  671. /* Request endpoints for multicast distribution:
  672. * <[8] 64-bit network ID>
  673. * <[1] flags>
  674. * <[6] MAC address of multicast group being queried>
  675. * <[4] 32-bit ADI for multicast group being queried>
  676. * <[4] 32-bit requested max number of multicast peers>
  677. * [<[...] network certificate of membership>]
  678. *
  679. * Flags:
  680. * 0x01 - Network certificate of membership is attached
  681. *
  682. * This message asks a peer for additional known endpoints that have
  683. * LIKEd a given multicast group. It's sent when the sender wishes
  684. * to send multicast but does not have the desired number of recipient
  685. * peers.
  686. *
  687. * OK response payload:
  688. * <[8] 64-bit network ID>
  689. * <[6] MAC address of multicast group being queried>
  690. * <[4] 32-bit ADI for multicast group being queried>
  691. * [begin gather results -- these same fields can be in OK(MULTICAST_FRAME)]
  692. * <[4] 32-bit total number of known members in this multicast group>
  693. * <[2] 16-bit number of members enumerated in this packet>
  694. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  695. *
  696. * If no endpoints are known, OK and ERROR are both optional. It's okay
  697. * to return nothing in that case since gathering is "lazy."
  698. *
  699. * ERROR response payload:
  700. * <[8] 64-bit network ID>
  701. * <[6] MAC address of multicast group being queried>
  702. * <[4] 32-bit ADI for multicast group being queried>
  703. *
  704. * ERRORs are optional and are only generated if permission is denied,
  705. * certificate of membership is out of date, etc.
  706. */
  707. VERB_MULTICAST_GATHER = 13,
  708. /* Multicast frame:
  709. * <[8] 64-bit network ID>
  710. * <[1] flags>
  711. * [<[...] network certificate of membership>]
  712. * [<[4] 32-bit implicit gather limit>]
  713. * [<[6] source MAC>]
  714. * <[6] destination MAC (multicast address)>
  715. * <[4] 32-bit multicast ADI (multicast address extension)>
  716. * <[2] 16-bit ethertype>
  717. * <[...] ethernet payload>
  718. *
  719. * Flags:
  720. * 0x01 - Network certificate of membership is attached
  721. * 0x02 - Implicit gather limit field is present
  722. * 0x04 - Source MAC is specified -- otherwise it's computed from sender
  723. *
  724. * OK and ERROR responses are optional. OK may be generated if there are
  725. * implicit gather results or if the recipient wants to send its own
  726. * updated certificate of network membership to the sender. ERROR may be
  727. * generated if a certificate is needed or if multicasts to this group
  728. * are no longer wanted (multicast unsubscribe).
  729. *
  730. * OK response payload:
  731. * <[8] 64-bit network ID>
  732. * <[6] MAC address of multicast group>
  733. * <[4] 32-bit ADI for multicast group>
  734. * <[1] flags>
  735. * [<[...] network certficate of membership>]
  736. * [<[...] implicit gather results if flag 0x01 is set>]
  737. *
  738. * OK flags (same bits as request flags):
  739. * 0x01 - OK includes certificate of network membership
  740. * 0x02 - OK includes implicit gather results
  741. *
  742. * ERROR response payload:
  743. * <[8] 64-bit network ID>
  744. * <[6] multicast group MAC>
  745. * <[4] 32-bit multicast group ADI>
  746. */
  747. VERB_MULTICAST_FRAME = 14
  748. };
  749. /**
  750. * Error codes for VERB_ERROR
  751. */
  752. enum ErrorCode
  753. {
  754. /* No error, not actually used in transit */
  755. ERROR_NONE = 0,
  756. /* Invalid request */
  757. ERROR_INVALID_REQUEST = 1,
  758. /* Bad/unsupported protocol version */
  759. ERROR_BAD_PROTOCOL_VERSION = 2,
  760. /* Unknown object queried (e.g. with WHOIS) */
  761. ERROR_OBJ_NOT_FOUND = 3,
  762. /* HELLO pushed an identity whose address is already claimed */
  763. ERROR_IDENTITY_COLLISION = 4,
  764. /* Verb or use case not supported/enabled by this node */
  765. ERROR_UNSUPPORTED_OPERATION = 5,
  766. /* Message to private network rejected -- no unexpired certificate on file */
  767. ERROR_NEED_MEMBERSHIP_CERTIFICATE = 6,
  768. /* Tried to join network, but you're not a member */
  769. ERROR_NETWORK_ACCESS_DENIED_ = 7, /* extra _ to avoid Windows name conflict */
  770. /* Multicasts to this group are not wanted */
  771. ERROR_UNWANTED_MULTICAST = 8
  772. };
  773. /**
  774. * @param v Verb
  775. * @return String representation (e.g. HELLO, OK)
  776. */
  777. static const char *verbString(Verb v)
  778. throw();
  779. /**
  780. * @param e Error code
  781. * @return String error name
  782. */
  783. static const char *errorString(ErrorCode e)
  784. throw();
  785. template<unsigned int C2>
  786. Packet(const Buffer<C2> &b)
  787. throw(std::out_of_range) :
  788. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
  789. {
  790. }
  791. /**
  792. * Construct a new empty packet with a unique random packet ID
  793. *
  794. * Flags and hops will be zero. Other fields and data region are undefined.
  795. * Use the header access methods (setDestination() and friends) to fill out
  796. * the header. Payload should be appended; initial size is header size.
  797. */
  798. Packet() :
  799. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
  800. {
  801. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
  802. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
  803. }
  804. /**
  805. * Make a copy of a packet with a new initialization vector and destination address
  806. *
  807. * This can be used to take one draft prototype packet and quickly make copies to
  808. * encrypt for different destinations.
  809. *
  810. * @param prototype Prototype packet
  811. * @param dest Destination ZeroTier address for new packet
  812. */
  813. Packet(const Packet &prototype,const Address &dest) :
  814. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(prototype)
  815. {
  816. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
  817. setDestination(dest);
  818. }
  819. /**
  820. * Construct a new empty packet with a unique random packet ID
  821. *
  822. * @param dest Destination ZT address
  823. * @param source Source ZT address
  824. * @param v Verb
  825. */
  826. Packet(const Address &dest,const Address &source,const Verb v) :
  827. Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
  828. {
  829. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
  830. setDestination(dest);
  831. setSource(source);
  832. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
  833. setVerb(v);
  834. }
  835. /**
  836. * Reset this packet structure for reuse in place
  837. *
  838. * @param dest Destination ZT address
  839. * @param source Source ZT address
  840. * @param v Verb
  841. */
  842. inline void reset(const Address &dest,const Address &source,const Verb v)
  843. {
  844. setSize(ZT_PROTO_MIN_PACKET_LENGTH);
  845. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
  846. setDestination(dest);
  847. setSource(source);
  848. (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
  849. setVerb(v);
  850. }
  851. /**
  852. * Generate a new IV / packet ID in place
  853. *
  854. * This can be used to re-use a packet buffer multiple times to send
  855. * technically different but otherwise identical copies of the same
  856. * packet.
  857. */
  858. inline void newInitializationVector()
  859. {
  860. Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
  861. }
  862. /**
  863. * Set this packet's destination
  864. *
  865. * @param dest ZeroTier address of destination
  866. */
  867. inline void setDestination(const Address &dest)
  868. {
  869. unsigned char *d = field(ZT_PACKET_IDX_DEST,ZT_ADDRESS_LENGTH);
  870. for(unsigned int i=0;i<ZT_ADDRESS_LENGTH;++i)
  871. d[i] = dest[i];
  872. }
  873. /**
  874. * Set this packet's source
  875. *
  876. * @param source ZeroTier address of source
  877. */
  878. inline void setSource(const Address &source)
  879. {
  880. unsigned char *s = field(ZT_PACKET_IDX_SOURCE,ZT_ADDRESS_LENGTH);
  881. for(unsigned int i=0;i<ZT_ADDRESS_LENGTH;++i)
  882. s[i] = source[i];
  883. }
  884. /**
  885. * Get this packet's destination
  886. *
  887. * @return Destination ZT address
  888. */
  889. inline Address destination() const { return Address(field(ZT_PACKET_IDX_DEST,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
  890. /**
  891. * Get this packet's source
  892. *
  893. * @return Source ZT address
  894. */
  895. inline Address source() const { return Address(field(ZT_PACKET_IDX_SOURCE,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
  896. /**
  897. * @return True if packet is of valid length
  898. */
  899. inline bool lengthValid() const { return (size() >= ZT_PROTO_MIN_PACKET_LENGTH); }
  900. /**
  901. * @return True if packet is fragmented (expect fragments)
  902. */
  903. inline bool fragmented() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0); }
  904. /**
  905. * Set this packet's fragmented flag
  906. *
  907. * @param f Fragmented flag value
  908. */
  909. inline void setFragmented(bool f)
  910. {
  911. if (f)
  912. (*this)[ZT_PACKET_IDX_FLAGS] |= (char)ZT_PROTO_FLAG_FRAGMENTED;
  913. else (*this)[ZT_PACKET_IDX_FLAGS] &= (char)(~ZT_PROTO_FLAG_FRAGMENTED);
  914. }
  915. /**
  916. * @return True if compressed (result only valid if unencrypted)
  917. */
  918. inline bool compressed() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_VERB] & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0); }
  919. /**
  920. * @return ZeroTier forwarding hops (0 to 7)
  921. */
  922. inline unsigned int hops() const { return ((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x07); }
  923. /**
  924. * Increment this packet's hop count
  925. */
  926. inline void incrementHops()
  927. {
  928. unsigned char &b = (*this)[ZT_PACKET_IDX_FLAGS];
  929. b = (b & 0xf8) | ((b + 1) & 0x07);
  930. }
  931. /**
  932. * @return Cipher suite selector: 0 - 7 (see #defines)
  933. */
  934. inline unsigned int cipher() const
  935. {
  936. //return (((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x38) >> 3);
  937. // Use DEPRECATED 0x80 "encrypted" flag -- this will go away once there are no more <1.0.0 peers on the net
  938. return (((*this)[ZT_PACKET_IDX_FLAGS] & 0x80) == 0) ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012;
  939. }
  940. /**
  941. * Set this packet's cipher suite
  942. *
  943. * This normally shouldn't be called directly as armor() will set it after
  944. * encrypting and MACing the packet.
  945. */
  946. inline void setCipher(unsigned int c)
  947. {
  948. unsigned char &b = (*this)[ZT_PACKET_IDX_FLAGS];
  949. b = (b & 0xc7) | (unsigned char)((c << 3) & 0x38);
  950. // Set both the new cipher suite spec field and the old DEPRECATED "encrypted" flag as long as there's <1.0.0 peers online
  951. if (c == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
  952. b |= 0x80;
  953. else b &= 0x7f;
  954. }
  955. /**
  956. * Get this packet's unique ID (the IV field interpreted as uint64_t)
  957. *
  958. * @return Packet ID
  959. */
  960. inline uint64_t packetId() const { return at<uint64_t>(ZT_PACKET_IDX_IV); }
  961. /**
  962. * Set packet verb
  963. *
  964. * This also has the side-effect of clearing any verb flags, such as
  965. * compressed, and so must only be done during packet composition.
  966. *
  967. * @param v New packet verb
  968. */
  969. inline void setVerb(Verb v) { (*this)[ZT_PACKET_IDX_VERB] = (char)v; }
  970. /**
  971. * @return Packet verb (not including flag bits)
  972. */
  973. inline Verb verb() const { return (Verb)((*this)[ZT_PACKET_IDX_VERB] & 0x1f); }
  974. /**
  975. * @return Length of packet payload
  976. */
  977. inline unsigned int payloadLength() const { return ((size() < ZT_PROTO_MIN_PACKET_LENGTH) ? 0 : (size() - ZT_PROTO_MIN_PACKET_LENGTH)); }
  978. /**
  979. * @return Raw packet payload
  980. */
  981. inline const unsigned char *payload() const
  982. {
  983. return field(ZT_PACKET_IDX_PAYLOAD,size() - ZT_PACKET_IDX_PAYLOAD);
  984. }
  985. /**
  986. * Armor packet for transport
  987. *
  988. * @param key 32-byte key
  989. * @param encryptPayload If true, encrypt packet payload, else just MAC
  990. */
  991. inline void armor(const void *key,bool encryptPayload)
  992. {
  993. unsigned char mangledKey[32];
  994. unsigned char macKey[32];
  995. unsigned char mac[16];
  996. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  997. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  998. // Set flag now, since it affects key mangle function
  999. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
  1000. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  1001. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
  1002. // MAC key is always the first 32 bytes of the Salsa20 key stream
  1003. // This is the same construction DJB's NaCl library uses
  1004. s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
  1005. if (encryptPayload)
  1006. s20.encrypt(payload,payload,payloadLen);
  1007. Poly1305::compute(mac,payload,payloadLen,macKey);
  1008. memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8);
  1009. }
  1010. /**
  1011. * Verify and (if encrypted) decrypt packet
  1012. *
  1013. * @param key 32-byte key
  1014. * @return False if packet is invalid or failed MAC authenticity check
  1015. */
  1016. inline bool dearmor(const void *key)
  1017. {
  1018. unsigned char mangledKey[32];
  1019. unsigned char macKey[32];
  1020. unsigned char mac[16];
  1021. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  1022. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  1023. unsigned int cs = cipher();
  1024. if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
  1025. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  1026. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
  1027. s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
  1028. Poly1305::compute(mac,payload,payloadLen,macKey);
  1029. if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8))
  1030. return false;
  1031. if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
  1032. s20.decrypt(payload,payload,payloadLen);
  1033. return true;
  1034. } else if (cs == ZT_PROTO_CIPHER_SUITE__C25519_AES256_GCM) {
  1035. return false; // not implemented yet
  1036. } else return false; // unrecognized cipher suite
  1037. }
  1038. /**
  1039. * Attempt to compress payload if not already (must be unencrypted)
  1040. *
  1041. * This requires that the payload at least contain the verb byte already
  1042. * set. The compressed flag in the verb is set if compression successfully
  1043. * results in a size reduction. If no size reduction occurs, compression
  1044. * is not done and the flag is left cleared.
  1045. *
  1046. * @return True if compression occurred
  1047. */
  1048. inline bool compress()
  1049. {
  1050. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  1051. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 32))) {
  1052. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  1053. int cl = LZ4_compress((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl);
  1054. if ((cl > 0)&&(cl < pl)) {
  1055. (*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  1056. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  1057. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
  1058. return true;
  1059. }
  1060. }
  1061. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  1062. return false;
  1063. }
  1064. /**
  1065. * Attempt to decompress payload if it is compressed (must be unencrypted)
  1066. *
  1067. * If payload is compressed, it is decompressed and the compressed verb
  1068. * flag is cleared. Otherwise nothing is done and true is returned.
  1069. *
  1070. * @return True if data is now decompressed and valid, false on error
  1071. */
  1072. inline bool uncompress()
  1073. {
  1074. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  1075. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  1076. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  1077. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  1078. int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
  1079. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  1080. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  1081. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
  1082. } else return false;
  1083. }
  1084. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  1085. }
  1086. return true;
  1087. }
  1088. private:
  1089. static const unsigned char ZERO_KEY[32];
  1090. /**
  1091. * Deterministically mangle a 256-bit crypto key based on packet
  1092. *
  1093. * This uses extra data from the packet to mangle the secret, giving us an
  1094. * effective IV that is somewhat more than 64 bits. This is "free" for
  1095. * Salsa20 since it has negligible key setup time so using a different
  1096. * key each time is fine.
  1097. *
  1098. * @param in Input key (32 bytes)
  1099. * @param out Output buffer (32 bytes)
  1100. */
  1101. inline void _salsa20MangleKey(const unsigned char *in,unsigned char *out) const
  1102. {
  1103. const unsigned char *d = (const unsigned char *)data();
  1104. // IV and source/destination addresses. Using the addresses divides the
  1105. // key space into two halves-- A->B and B->A (since order will change).
  1106. for(unsigned int i=0;i<18;++i) // 8 + (ZT_ADDRESS_LENGTH * 2) == 18
  1107. out[i] = in[i] ^ d[i];
  1108. // Flags, but with hop count masked off. Hop count is altered by forwarding
  1109. // nodes. It's one of the only parts of a packet modifiable by people
  1110. // without the key.
  1111. out[18] = in[18] ^ (d[ZT_PACKET_IDX_FLAGS] & 0xf8);
  1112. // Raw packet size in bytes -- thus each packet size defines a new
  1113. // key space.
  1114. out[19] = in[19] ^ (unsigned char)(size() & 0xff);
  1115. out[20] = in[20] ^ (unsigned char)((size() >> 8) & 0xff); // little endian
  1116. // Rest of raw key is used unchanged
  1117. for(unsigned int i=21;i<32;++i)
  1118. out[i] = in[i];
  1119. }
  1120. };
  1121. } // namespace ZeroTier
  1122. #endif