VL1.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-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. #include "VL1.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Node.hpp"
  16. #include "Topology.hpp"
  17. #include "VL2.hpp"
  18. #include "AES.hpp"
  19. #include "Salsa20.hpp"
  20. #include "LZ4.hpp"
  21. #include "Poly1305.hpp"
  22. #include "SHA512.hpp"
  23. #include "Identity.hpp"
  24. #include "SelfAwareness.hpp"
  25. #include "Peer.hpp"
  26. #include "Path.hpp"
  27. #include "Expect.hpp"
  28. namespace ZeroTier {
  29. namespace {
  30. ZT_INLINE const Identity &identityFromPeerPtr(const SharedPtr< Peer > &p)
  31. {
  32. return (p) ? p->identity() : Identity::NIL;
  33. }
  34. struct p_SalsaPolyCopyFunction
  35. {
  36. Salsa20 s20;
  37. Poly1305 poly1305;
  38. unsigned int hdrRemaining;
  39. ZT_INLINE p_SalsaPolyCopyFunction(const void *salsaKey, const void *salsaIv) :
  40. s20(salsaKey, salsaIv),
  41. poly1305(),
  42. hdrRemaining(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START)
  43. {
  44. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  45. s20.crypt12(Utils::ZERO256, macKey, ZT_POLY1305_KEY_SIZE);
  46. poly1305.init(macKey);
  47. }
  48. ZT_INLINE void operator()(void *dest, const void *src, unsigned int len) noexcept
  49. {
  50. if (hdrRemaining != 0) {
  51. unsigned int hdrBytes = (len > hdrRemaining) ? hdrRemaining : len;
  52. Utils::copy(dest, src, hdrBytes);
  53. hdrRemaining -= hdrBytes;
  54. dest = reinterpret_cast<uint8_t *>(dest) + hdrBytes;
  55. src = reinterpret_cast<const uint8_t *>(src) + hdrBytes;
  56. len -= hdrBytes;
  57. }
  58. poly1305.update(src, len);
  59. s20.crypt12(src, dest, len);
  60. }
  61. };
  62. struct p_PolyCopyFunction
  63. {
  64. Poly1305 poly1305;
  65. unsigned int hdrRemaining;
  66. ZT_INLINE p_PolyCopyFunction(const void *salsaKey, const void *salsaIv) :
  67. poly1305(),
  68. hdrRemaining(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START)
  69. {
  70. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  71. Salsa20(salsaKey, salsaIv).crypt12(Utils::ZERO256, macKey, ZT_POLY1305_KEY_SIZE);
  72. poly1305.init(macKey);
  73. }
  74. ZT_INLINE void operator()(void *dest, const void *src, unsigned int len) noexcept
  75. {
  76. if (hdrRemaining != 0) {
  77. unsigned int hdrBytes = (len > hdrRemaining) ? hdrRemaining : len;
  78. Utils::copy(dest, src, hdrBytes);
  79. hdrRemaining -= hdrBytes;
  80. dest = reinterpret_cast<uint8_t *>(dest) + hdrBytes;
  81. src = reinterpret_cast<const uint8_t *>(src) + hdrBytes;
  82. len -= hdrBytes;
  83. }
  84. poly1305.update(src, len);
  85. Utils::copy(dest, src, len);
  86. }
  87. };
  88. } // anonymous namespace
  89. VL1::VL1(const RuntimeEnvironment *renv) :
  90. RR(renv)
  91. {
  92. }
  93. void VL1::onRemotePacket(void *const tPtr, const int64_t localSocket, const InetAddress &fromAddr, SharedPtr< Buf > &data, const unsigned int len) noexcept
  94. {
  95. const SharedPtr< Path > path(RR->topology->path(localSocket, fromAddr));
  96. const int64_t now = RR->node->now();
  97. ZT_SPEW("%u bytes from %s (local socket %lld)", len, fromAddr.toString().c_str(), localSocket);
  98. path->received(now, len);
  99. // NOTE: likely/unlikely are used here to highlight the most common code path
  100. // for valid data packets. This may allow the compiler to generate very slightly
  101. // faster code for that path.
  102. try {
  103. if (unlikely(len < ZT_PROTO_MIN_FRAGMENT_LENGTH))
  104. return;
  105. static_assert((ZT_PROTO_PACKET_ID_INDEX + sizeof(uint64_t)) < ZT_PROTO_MIN_FRAGMENT_LENGTH, "overflow");
  106. const uint64_t packetId = Utils::loadAsIsEndian< uint64_t >(data->unsafeData + ZT_PROTO_PACKET_ID_INDEX);
  107. static_assert((ZT_PROTO_PACKET_DESTINATION_INDEX + ZT_ADDRESS_LENGTH) < ZT_PROTO_MIN_FRAGMENT_LENGTH, "overflow");
  108. const Address destination(data->unsafeData + ZT_PROTO_PACKET_DESTINATION_INDEX);
  109. if (destination != RR->identity.address()) {
  110. m_relay(tPtr, path, destination, data, len);
  111. return;
  112. }
  113. // ----------------------------------------------------------------------------------------------------------------
  114. // If we made it this far, the packet is at least MIN_FRAGMENT_LENGTH and is addressed to this node's ZT address
  115. // ----------------------------------------------------------------------------------------------------------------
  116. Buf::PacketVector pktv;
  117. static_assert(ZT_PROTO_PACKET_FRAGMENT_INDICATOR_INDEX <= ZT_PROTO_MIN_FRAGMENT_LENGTH, "overflow");
  118. if (data->unsafeData[ZT_PROTO_PACKET_FRAGMENT_INDICATOR_INDEX] == ZT_PROTO_PACKET_FRAGMENT_INDICATOR) {
  119. // This looks like a fragment (excluding the head) of a larger packet.
  120. static_assert(ZT_PROTO_PACKET_FRAGMENT_COUNTS < ZT_PROTO_MIN_FRAGMENT_LENGTH, "overflow");
  121. const unsigned int totalFragments = (data->unsafeData[ZT_PROTO_PACKET_FRAGMENT_COUNTS] >> 4U) & 0x0fU;
  122. const unsigned int fragmentNo = data->unsafeData[ZT_PROTO_PACKET_FRAGMENT_COUNTS] & 0x0fU;
  123. switch (m_inputPacketAssembler.assemble(
  124. packetId,
  125. pktv,
  126. data,
  127. ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT,
  128. len - ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT,
  129. fragmentNo,
  130. totalFragments,
  131. now,
  132. path)) {
  133. case Defragmenter< ZT_MAX_PACKET_FRAGMENTS >::COMPLETE:
  134. break;
  135. default:
  136. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::OK:
  137. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_DUPLICATE_FRAGMENT:
  138. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_INVALID_FRAGMENT:
  139. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_TOO_MANY_FRAGMENTS_FOR_PATH:
  140. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_OUT_OF_MEMORY:
  141. return;
  142. }
  143. } else {
  144. if (unlikely(len < ZT_PROTO_MIN_PACKET_LENGTH))
  145. return;
  146. static_assert(ZT_PROTO_PACKET_FLAGS_INDEX < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  147. if ((data->unsafeData[ZT_PROTO_PACKET_FLAGS_INDEX] & ZT_PROTO_FLAG_FRAGMENTED) != 0) {
  148. // This is the head of a series of fragments that we may or may not already have.
  149. switch (m_inputPacketAssembler.assemble(
  150. packetId,
  151. pktv,
  152. data,
  153. 0, // fragment index is 0 since this is the head
  154. len,
  155. 0, // always the zero'eth fragment
  156. 0, // this is specified in fragments, not in the head
  157. now,
  158. path)) {
  159. case Defragmenter< ZT_MAX_PACKET_FRAGMENTS >::COMPLETE:
  160. break;
  161. default:
  162. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::OK:
  163. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_DUPLICATE_FRAGMENT:
  164. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_INVALID_FRAGMENT:
  165. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_TOO_MANY_FRAGMENTS_FOR_PATH:
  166. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_OUT_OF_MEMORY:
  167. return;
  168. }
  169. } else {
  170. // This is a single whole packet with no fragments.
  171. Buf::Slice s = pktv.push();
  172. s.b.swap(data);
  173. s.s = 0;
  174. s.e = len;
  175. }
  176. }
  177. // ----------------------------------------------------------------------------------------------------------------
  178. // If we made it this far without returning, a packet is fully assembled and ready to process.
  179. // ----------------------------------------------------------------------------------------------------------------
  180. const uint8_t *const hdr = pktv[0].b->unsafeData + pktv[0].s;
  181. static_assert((ZT_PROTO_PACKET_SOURCE_INDEX + ZT_ADDRESS_LENGTH) < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  182. const Address source(hdr + ZT_PROTO_PACKET_SOURCE_INDEX);
  183. static_assert(ZT_PROTO_PACKET_FLAGS_INDEX < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  184. const uint8_t hops = hdr[ZT_PROTO_PACKET_FLAGS_INDEX] & ZT_PROTO_FLAG_FIELD_HOPS_MASK;
  185. const uint8_t cipher = (hdr[ZT_PROTO_PACKET_FLAGS_INDEX] >> 3U) & 3U;
  186. SharedPtr< Buf > pkt(new Buf());
  187. int pktSize = 0;
  188. static_assert(ZT_PROTO_PACKET_VERB_INDEX < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  189. if (unlikely(((cipher == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE) || (cipher == ZT_PROTO_CIPHER_SUITE__NONE)) && ((hdr[ZT_PROTO_PACKET_VERB_INDEX] & ZT_PROTO_VERB_MASK) == Protocol::VERB_HELLO))) {
  190. // Handle unencrypted HELLO packets.
  191. pktSize = pktv.mergeCopy(*pkt);
  192. if (unlikely(pktSize < ZT_PROTO_MIN_PACKET_LENGTH)) {
  193. ZT_SPEW("discarding packet %.16llx from %s(%s): assembled packet size: %d", packetId, source.toString().c_str(), fromAddr.toString().c_str(), pktSize);
  194. return;
  195. }
  196. const SharedPtr< Peer > peer(m_HELLO(tPtr, path, *pkt, pktSize));
  197. if (likely(peer))
  198. peer->received(tPtr, path, hops, packetId, pktSize - ZT_PROTO_PACKET_PAYLOAD_START, Protocol::VERB_HELLO, Protocol::VERB_NOP);
  199. return;
  200. }
  201. // This remains zero if authentication fails. Otherwise it gets set to a bit mask
  202. // indicating authentication and other security flags like encryption and forward
  203. // secrecy status.
  204. unsigned int auth = 0;
  205. SharedPtr< Peer > peer(RR->topology->peer(tPtr, source));
  206. if (likely(peer)) {
  207. switch (cipher) {
  208. case ZT_PROTO_CIPHER_SUITE__POLY1305_NONE: {
  209. uint8_t perPacketKey[ZT_SALSA20_KEY_SIZE];
  210. Protocol::salsa2012DeriveKey(peer->rawIdentityKey(), perPacketKey, *pktv[0].b, pktv.totalSize());
  211. p_PolyCopyFunction s20cf(perPacketKey, &packetId);
  212. pktSize = pktv.mergeMap< p_PolyCopyFunction & >(*pkt, ZT_PROTO_PACKET_ENCRYPTED_SECTION_START, s20cf);
  213. if (unlikely(pktSize < ZT_PROTO_MIN_PACKET_LENGTH)) {
  214. ZT_SPEW("discarding packet %.16llx from %s(%s): assembled packet size: %d", packetId, source.toString().c_str(), fromAddr.toString().c_str(), pktSize);
  215. return;
  216. }
  217. uint64_t mac[2];
  218. s20cf.poly1305.finish(mac);
  219. static_assert((ZT_PROTO_PACKET_MAC_INDEX + 8) < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  220. if (unlikely(Utils::loadAsIsEndian< uint64_t >(hdr + ZT_PROTO_PACKET_MAC_INDEX) != mac[0])) {
  221. ZT_SPEW("discarding packet %.16llx from %s(%s): packet MAC failed (none/poly1305)", packetId, source.toString().c_str(), fromAddr.toString().c_str());
  222. RR->t->incomingPacketDropped(tPtr, 0xcc89c812, packetId, 0, peer->identity(), path->address(), hops, Protocol::VERB_NOP, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  223. return;
  224. }
  225. auth = ZT_VL1_AUTH_RESULT_FLAG_AUTHENTICATED;
  226. }
  227. break;
  228. case ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012: {
  229. uint8_t perPacketKey[ZT_SALSA20_KEY_SIZE];
  230. Protocol::salsa2012DeriveKey(peer->rawIdentityKey(), perPacketKey, *pktv[0].b, pktv.totalSize());
  231. p_SalsaPolyCopyFunction s20cf(perPacketKey, &packetId);
  232. pktSize = pktv.mergeMap< p_SalsaPolyCopyFunction & >(*pkt, ZT_PROTO_PACKET_ENCRYPTED_SECTION_START, s20cf);
  233. if (unlikely(pktSize < ZT_PROTO_MIN_PACKET_LENGTH)) {
  234. ZT_SPEW("discarding packet %.16llx from %s(%s): assembled packet size: %d", packetId, source.toString().c_str(), fromAddr.toString().c_str(), pktSize);
  235. return;
  236. }
  237. uint64_t mac[2];
  238. s20cf.poly1305.finish(mac);
  239. static_assert((ZT_PROTO_PACKET_MAC_INDEX + 8) < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  240. if (unlikely(Utils::loadAsIsEndian< uint64_t >(hdr + ZT_PROTO_PACKET_MAC_INDEX) != mac[0])) {
  241. ZT_SPEW("discarding packet %.16llx from %s(%s): packet MAC failed (salsa/poly1305)", packetId, source.toString().c_str(), fromAddr.toString().c_str());
  242. RR->t->incomingPacketDropped(tPtr, 0xcc89c812, packetId, 0, peer->identity(), path->address(), hops, Protocol::VERB_NOP, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  243. return;
  244. }
  245. auth = ZT_VL1_AUTH_RESULT_FLAG_AUTHENTICATED | ZT_VL1_AUTH_RESULT_FLAG_ENCRYPTED;
  246. }
  247. break;
  248. case ZT_PROTO_CIPHER_SUITE__NONE: {
  249. // TODO
  250. }
  251. break;
  252. case ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV: {
  253. // TODO
  254. }
  255. break;
  256. default:
  257. RR->t->incomingPacketDropped(tPtr, 0x5b001099, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_NOP, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  258. return;
  259. }
  260. }
  261. if (likely(auth != 0)) {
  262. // If authentication was successful go on and process the packet.
  263. if (unlikely(pktSize < ZT_PROTO_MIN_PACKET_LENGTH)) {
  264. ZT_SPEW("discarding packet %.16llx from %s(%s): assembled packet size %d is smaller than minimum packet length", packetId, source.toString().c_str(), fromAddr.toString().c_str(), pktSize);
  265. return;
  266. }
  267. // TODO: should take instance ID into account here once that is fully implemented.
  268. if (unlikely(peer->deduplicateIncomingPacket(packetId))) {
  269. ZT_SPEW("discarding packet %.16llx from %s(%s): duplicate!", packetId, source.toString().c_str(), fromAddr.toString().c_str());
  270. return;
  271. }
  272. static_assert(ZT_PROTO_PACKET_VERB_INDEX < ZT_PROTO_MIN_PACKET_LENGTH, "overflow");
  273. const uint8_t verbFlags = pkt->unsafeData[ZT_PROTO_PACKET_VERB_INDEX];
  274. const Protocol::Verb verb = (Protocol::Verb)(verbFlags & ZT_PROTO_VERB_MASK);
  275. // Decompress packet payload if compressed. For additional safety decompression is
  276. // only performed on packets whose MACs have already been validated. (Only HELLO is
  277. // sent without this, and HELLO doesn't benefit from compression.)
  278. if (((verbFlags & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0) && (pktSize > ZT_PROTO_PACKET_PAYLOAD_START)) {
  279. SharedPtr< Buf > dec(new Buf());
  280. Utils::copy< ZT_PROTO_PACKET_PAYLOAD_START >(dec->unsafeData, pkt->unsafeData);
  281. const int uncompressedLen = LZ4_decompress_safe(
  282. reinterpret_cast<const char *>(pkt->unsafeData + ZT_PROTO_PACKET_PAYLOAD_START),
  283. reinterpret_cast<char *>(dec->unsafeData + ZT_PROTO_PACKET_PAYLOAD_START),
  284. pktSize - ZT_PROTO_PACKET_PAYLOAD_START,
  285. ZT_BUF_MEM_SIZE - ZT_PROTO_PACKET_PAYLOAD_START);
  286. if (likely((uncompressedLen >= 0) && (uncompressedLen <= (ZT_BUF_MEM_SIZE - ZT_PROTO_PACKET_PAYLOAD_START)))) {
  287. pkt.swap(dec);
  288. ZT_SPEW("decompressed packet: %d -> %d", pktSize, ZT_PROTO_PACKET_PAYLOAD_START + uncompressedLen);
  289. pktSize = ZT_PROTO_PACKET_PAYLOAD_START + uncompressedLen;
  290. } else {
  291. RR->t->incomingPacketDropped(tPtr, 0xee9e4392, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, verb, ZT_TRACE_PACKET_DROP_REASON_INVALID_COMPRESSED_DATA);
  292. return;
  293. }
  294. }
  295. ZT_SPEW("%s from %s(%s) (%d bytes)", Protocol::verbName(verb), source.toString().c_str(), fromAddr.toString().c_str(), pktSize);
  296. // NOTE: HELLO is normally sent in the clear (in terms of our usual AEAD modes) and is handled
  297. // above. We will try to process it here, but if so it'll still get re-authenticated via HELLO's
  298. // own internal authentication logic as usual. It would be abnormal to make it here with HELLO
  299. // but not invalid.
  300. Protocol::Verb inReVerb = Protocol::VERB_NOP;
  301. bool ok = true;
  302. switch (verb) {
  303. case Protocol::VERB_NOP:
  304. break;
  305. case Protocol::VERB_HELLO:
  306. ok = (bool)(m_HELLO(tPtr, path, *pkt, pktSize));
  307. break;
  308. case Protocol::VERB_ERROR:
  309. ok = m_ERROR(tPtr, packetId, auth, path, peer, *pkt, pktSize, inReVerb);
  310. break;
  311. case Protocol::VERB_OK:
  312. ok = m_OK(tPtr, packetId, auth, path, peer, *pkt, pktSize, inReVerb);
  313. break;
  314. case Protocol::VERB_WHOIS:
  315. ok = m_WHOIS(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  316. break;
  317. case Protocol::VERB_RENDEZVOUS:
  318. ok = m_RENDEZVOUS(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  319. break;
  320. case Protocol::VERB_FRAME:
  321. ok = RR->vl2->m_FRAME(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  322. break;
  323. case Protocol::VERB_EXT_FRAME:
  324. ok = RR->vl2->m_EXT_FRAME(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  325. break;
  326. case Protocol::VERB_ECHO:
  327. ok = m_ECHO(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  328. break;
  329. case Protocol::VERB_MULTICAST_LIKE:
  330. ok = RR->vl2->m_MULTICAST_LIKE(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  331. break;
  332. case Protocol::VERB_NETWORK_CREDENTIALS:
  333. ok = RR->vl2->m_NETWORK_CREDENTIALS(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  334. break;
  335. case Protocol::VERB_NETWORK_CONFIG_REQUEST:
  336. ok = RR->vl2->m_NETWORK_CONFIG_REQUEST(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  337. break;
  338. case Protocol::VERB_NETWORK_CONFIG:
  339. ok = RR->vl2->m_NETWORK_CONFIG(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  340. break;
  341. case Protocol::VERB_MULTICAST_GATHER:
  342. ok = RR->vl2->m_MULTICAST_GATHER(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  343. break;
  344. case Protocol::VERB_MULTICAST_FRAME_deprecated:
  345. ok = RR->vl2->m_MULTICAST_FRAME_deprecated(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  346. break;
  347. case Protocol::VERB_PUSH_DIRECT_PATHS:
  348. ok = m_PUSH_DIRECT_PATHS(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  349. break;
  350. case Protocol::VERB_USER_MESSAGE:
  351. ok = m_USER_MESSAGE(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  352. break;
  353. case Protocol::VERB_MULTICAST:
  354. ok = RR->vl2->m_MULTICAST(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  355. break;
  356. case Protocol::VERB_ENCAP:
  357. ok = m_ENCAP(tPtr, packetId, auth, path, peer, *pkt, pktSize);
  358. break;
  359. default:
  360. RR->t->incomingPacketDropped(tPtr, 0xeeeeeff0, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, verb, ZT_TRACE_PACKET_DROP_REASON_UNRECOGNIZED_VERB);
  361. break;
  362. }
  363. if (likely(ok))
  364. peer->received(tPtr, path, hops, packetId, pktSize - ZT_PROTO_PACKET_PAYLOAD_START, verb, inReVerb);
  365. } else {
  366. // If decryption and authentication were not successful, try to look up identities.
  367. // This is rate limited by virtue of the retry rate limit timer.
  368. if (pktSize <= 0)
  369. pktSize = pktv.mergeCopy(*pkt);
  370. if (likely(pktSize >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  371. ZT_SPEW("authentication failed or no peers match, queueing WHOIS for %s", source.toString().c_str());
  372. bool sendPending;
  373. {
  374. Mutex::Lock wl(m_whoisQueue_l);
  375. p_WhoisQueueItem &wq = m_whoisQueue[source];
  376. const unsigned int wpidx = wq.waitingPacketCount++ % ZT_VL1_MAX_WHOIS_WAITING_PACKETS;
  377. wq.waitingPacketSize[wpidx] = (unsigned int)pktSize;
  378. wq.waitingPacket[wpidx] = pkt;
  379. sendPending = (now - wq.lastRetry) >= ZT_WHOIS_RETRY_DELAY;
  380. }
  381. if (sendPending)
  382. m_sendPendingWhois(tPtr, now);
  383. }
  384. }
  385. } catch (...) {
  386. RR->t->unexpectedError(tPtr, 0xea1b6dea, "unexpected exception in onRemotePacket() parsing packet from %s", path->address().toString().c_str());
  387. }
  388. }
  389. void VL1::m_relay(void *tPtr, const SharedPtr< Path > &path, Address destination, SharedPtr< Buf > &pkt, int pktSize)
  390. {
  391. }
  392. void VL1::m_sendPendingWhois(void *tPtr, int64_t now)
  393. {
  394. const SharedPtr< Peer > root(RR->topology->root());
  395. if (unlikely(!root))
  396. return;
  397. const SharedPtr< Path > rootPath(root->path(now));
  398. if (unlikely(!rootPath))
  399. return;
  400. Vector< Address > toSend;
  401. {
  402. Mutex::Lock wl(m_whoisQueue_l);
  403. for (Map< Address, p_WhoisQueueItem >::iterator wi(m_whoisQueue.begin()); wi != m_whoisQueue.end(); ++wi) {
  404. if ((now - wi->second.lastRetry) >= ZT_WHOIS_RETRY_DELAY) {
  405. wi->second.lastRetry = now;
  406. ++wi->second.retries;
  407. toSend.push_back(wi->first);
  408. }
  409. }
  410. }
  411. if (!toSend.empty()) {
  412. const SharedPtr< SymmetricKey > key(root->key());
  413. uint8_t outp[ZT_DEFAULT_UDP_MTU - ZT_PROTO_MIN_PACKET_LENGTH];
  414. Vector< Address >::iterator a(toSend.begin());
  415. while (a != toSend.end()) {
  416. const uint64_t packetId = key->nextMessage(RR->identity.address(), root->address());
  417. int p = Protocol::newPacket(outp, packetId, root->address(), RR->identity.address(), Protocol::VERB_WHOIS);
  418. while ((a != toSend.end()) && (p < (sizeof(outp) - ZT_ADDRESS_LENGTH))) {
  419. a->copyTo(outp + p);
  420. ++a;
  421. p += ZT_ADDRESS_LENGTH;
  422. }
  423. Protocol::armor(outp, p, key, root->cipher());
  424. RR->expect->sending(packetId, now);
  425. root->send(tPtr, now, outp, p, rootPath);
  426. }
  427. }
  428. }
  429. SharedPtr< Peer > VL1::m_HELLO(void *tPtr, const SharedPtr< Path > &path, Buf &pkt, int packetSize)
  430. {
  431. const uint64_t packetId = Utils::loadAsIsEndian< uint64_t >(pkt.unsafeData + ZT_PROTO_PACKET_ID_INDEX);
  432. const uint64_t mac = Utils::loadAsIsEndian< uint64_t >(pkt.unsafeData + ZT_PROTO_PACKET_MAC_INDEX);
  433. const uint8_t hops = pkt.unsafeData[ZT_PROTO_PACKET_FLAGS_INDEX] & ZT_PROTO_FLAG_FIELD_HOPS_MASK;
  434. const uint8_t protoVersion = pkt.lI8< ZT_PROTO_PACKET_PAYLOAD_START >();
  435. if (unlikely(protoVersion < ZT_PROTO_VERSION_MIN)) {
  436. RR->t->incomingPacketDropped(tPtr, 0x907a9891, packetId, 0, Identity::NIL, path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_PEER_TOO_OLD);
  437. return SharedPtr< Peer >();
  438. }
  439. const unsigned int versionMajor = pkt.lI8< ZT_PROTO_PACKET_PAYLOAD_START + 1 >();
  440. const unsigned int versionMinor = pkt.lI8< ZT_PROTO_PACKET_PAYLOAD_START + 2 >();
  441. const unsigned int versionRev = pkt.lI16< ZT_PROTO_PACKET_PAYLOAD_START + 3 >();
  442. const uint64_t timestamp = pkt.lI64< ZT_PROTO_PACKET_PAYLOAD_START + 5 >();
  443. int ii = ZT_PROTO_PACKET_PAYLOAD_START + 13;
  444. // Get identity and verify that it matches the sending address in the packet.
  445. Identity id;
  446. if (unlikely(pkt.rO(ii, id) < 0)) {
  447. RR->t->incomingPacketDropped(tPtr, 0x707a9810, packetId, 0, Identity::NIL, path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  448. return SharedPtr< Peer >();
  449. }
  450. if (unlikely(id.address() != Address(pkt.unsafeData + ZT_PROTO_PACKET_SOURCE_INDEX))) {
  451. RR->t->incomingPacketDropped(tPtr, 0x707a9010, packetId, 0, Identity::NIL, path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  452. return SharedPtr< Peer >();
  453. }
  454. // Get the peer that matches this identity, or learn a new one if we don't know it.
  455. SharedPtr< Peer > peer(RR->topology->peer(tPtr, id.address(), true));
  456. if (peer) {
  457. if (unlikely(peer->identity() != id)) {
  458. RR->t->incomingPacketDropped(tPtr, 0x707a9891, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  459. return SharedPtr< Peer >();
  460. }
  461. if (unlikely(peer->deduplicateIncomingPacket(packetId))) {
  462. ZT_SPEW("discarding packet %.16llx from %s(%s): duplicate!", packetId, id.address().toString().c_str(), path->address().toString().c_str());
  463. return SharedPtr< Peer >();
  464. }
  465. } else {
  466. if (unlikely(!id.locallyValidate())) {
  467. RR->t->incomingPacketDropped(tPtr, 0x707a9892, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  468. return SharedPtr< Peer >();
  469. }
  470. peer.set(new Peer(RR));
  471. if (unlikely(!peer->init(id))) {
  472. RR->t->incomingPacketDropped(tPtr, 0x707a9893, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_UNSPECIFIED);
  473. return SharedPtr< Peer >();
  474. }
  475. peer = RR->topology->add(tPtr, peer);
  476. }
  477. // ------------------------------------------------------------------------------------------------------------------
  478. // If we made it this far, peer is non-NULL and the identity is valid and matches it.
  479. // ------------------------------------------------------------------------------------------------------------------
  480. if (protoVersion >= 11) {
  481. // V2.x and newer use HMAC-SHA384 for HELLO, which offers a larger security margin
  482. // to guard key exchange and connection setup than typical AEAD. The packet MAC
  483. // field is ignored, and eventually it'll be undefined.
  484. uint8_t hmac[ZT_HMACSHA384_LEN];
  485. if (unlikely(packetSize < ZT_HMACSHA384_LEN)) {
  486. RR->t->incomingPacketDropped(tPtr, 0xab9c9891, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  487. return SharedPtr< Peer >();
  488. }
  489. packetSize -= ZT_HMACSHA384_LEN;
  490. pkt.unsafeData[ZT_PROTO_PACKET_FLAGS_INDEX] &= ~ZT_PROTO_FLAG_FIELD_HOPS_MASK; // mask hops to 0
  491. Utils::storeAsIsEndian< uint64_t >(pkt.unsafeData + ZT_PROTO_PACKET_MAC_INDEX, 0); // set MAC field to 0
  492. HMACSHA384(peer->identityHelloHmacKey(), pkt.unsafeData, packetSize, hmac);
  493. if (unlikely(!Utils::secureEq(hmac, pkt.unsafeData + packetSize, ZT_HMACSHA384_LEN))) {
  494. RR->t->incomingPacketDropped(tPtr, 0x707a9891, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  495. return SharedPtr< Peer >();
  496. }
  497. } else {
  498. // Older versions use Poly1305 MAC (but no whole packet encryption) for HELLO.
  499. if (likely(packetSize > ZT_PROTO_PACKET_ENCRYPTED_SECTION_START)) {
  500. uint8_t perPacketKey[ZT_SALSA20_KEY_SIZE];
  501. Protocol::salsa2012DeriveKey(peer->rawIdentityKey(), perPacketKey, pkt, packetSize);
  502. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  503. Salsa20(perPacketKey, &packetId).crypt12(Utils::ZERO256, macKey, ZT_POLY1305_KEY_SIZE);
  504. Poly1305 poly1305(macKey);
  505. poly1305.update(pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START, packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START);
  506. uint64_t polyMac[2];
  507. poly1305.finish(polyMac);
  508. if (unlikely(mac != polyMac[0])) {
  509. RR->t->incomingPacketDropped(tPtr, 0x11bfff82, packetId, 0, id, path->address(), hops, Protocol::VERB_NOP, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  510. return SharedPtr< Peer >();
  511. }
  512. } else {
  513. RR->t->incomingPacketDropped(tPtr, 0x11bfff81, packetId, 0, id, path->address(), hops, Protocol::VERB_NOP, ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  514. return SharedPtr< Peer >();
  515. }
  516. }
  517. // ------------------------------------------------------------------------------------------------------------------
  518. // This far means we passed MAC (Poly1305 or HMAC-SHA384 for newer peers)
  519. // ------------------------------------------------------------------------------------------------------------------
  520. InetAddress sentTo;
  521. if (unlikely(pkt.rO(ii, sentTo) < 0)) {
  522. RR->t->incomingPacketDropped(tPtr, 0x707a9811, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  523. return SharedPtr< Peer >();
  524. }
  525. const SharedPtr< SymmetricKey > key(peer->identityKey());
  526. if (protoVersion >= 11) {
  527. // V2.x and newer supports an encrypted section and has a new OK format.
  528. ii += 4; // skip reserved field
  529. if (likely((ii + 12) < packetSize)) {
  530. AES::CTR ctr(peer->identityHelloDictionaryEncryptionCipher());
  531. const uint8_t *const ctrNonce = pkt.unsafeData + ii;
  532. ii += 12;
  533. ctr.init(ctrNonce, 0, pkt.unsafeData + ii);
  534. ctr.crypt(pkt.unsafeData + ii, packetSize - ii);
  535. ctr.finish();
  536. ii += 2; // skip reserved field
  537. const unsigned int dictSize = pkt.rI16(ii);
  538. if (unlikely((ii + dictSize) > packetSize)) {
  539. RR->t->incomingPacketDropped(tPtr, 0x707a9815, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  540. return peer;
  541. }
  542. Dictionary md;
  543. if (!md.decode(pkt.unsafeData + ii, dictSize)) {
  544. RR->t->incomingPacketDropped(tPtr, 0x707a9816, packetId, 0, identityFromPeerPtr(peer), path->address(), hops, Protocol::VERB_HELLO, ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  545. return peer;
  546. }
  547. if (!md.empty()) {
  548. // TODO
  549. }
  550. }
  551. }
  552. Protocol::newPacket(pkt, key->nextMessage(RR->identity.address(), peer->address()), peer->address(), RR->identity.address(), Protocol::VERB_OK);
  553. ii = ZT_PROTO_PACKET_PAYLOAD_START;
  554. pkt.wI8(ii, Protocol::VERB_HELLO);
  555. pkt.wI64(ii, packetId);
  556. pkt.wI64(ii, timestamp);
  557. pkt.wI8(ii, ZT_PROTO_VERSION);
  558. pkt.wI8(ii, ZEROTIER_VERSION_MAJOR);
  559. pkt.wI8(ii, ZEROTIER_VERSION_MINOR);
  560. pkt.wI16(ii, ZEROTIER_VERSION_REVISION);
  561. pkt.wO(ii, path->address());
  562. pkt.wI16(ii, 0); // reserved, specifies no "moons" for older versions
  563. if (protoVersion >= 11) {
  564. FCV< uint8_t, 1024 > okmd;
  565. pkt.wI16(ii, (uint16_t)okmd.size());
  566. pkt.wB(ii, okmd.data(), okmd.size());
  567. if (unlikely((ii + ZT_HMACSHA384_LEN) > ZT_BUF_MEM_SIZE)) // sanity check, should be impossible
  568. return SharedPtr< Peer >();
  569. HMACSHA384(peer->identityHelloHmacKey(), pkt.unsafeData, ii, pkt.unsafeData + ii);
  570. ii += ZT_HMACSHA384_LEN;
  571. }
  572. peer->setRemoteVersion(protoVersion, versionMajor, versionMinor, versionRev);
  573. peer->send(tPtr, RR->node->now(), pkt.unsafeData, ii, path);
  574. return peer;
  575. }
  576. bool VL1::m_ERROR(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize, Protocol::Verb &inReVerb)
  577. {
  578. #if 0
  579. if (packetSize < (int)sizeof(Protocol::ERROR::Header)) {
  580. RR->t->incomingPacketDropped(tPtr,0x3beb1947,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_ERROR,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  581. return false;
  582. }
  583. Protocol::ERROR::Header &eh = pkt.as<Protocol::ERROR::Header>();
  584. inReVerb = (Protocol::Verb)eh.inReVerb;
  585. const int64_t now = RR->node->now();
  586. if (!RR->expect->expecting(eh.inRePacketId,now)) {
  587. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_REPLY_NOT_EXPECTED);
  588. return false;
  589. }
  590. switch(eh.error) {
  591. //case Protocol::ERROR_INVALID_REQUEST:
  592. //case Protocol::ERROR_BAD_PROTOCOL_VERSION:
  593. //case Protocol::ERROR_CANNOT_DELIVER:
  594. default:
  595. break;
  596. case Protocol::ERROR_OBJ_NOT_FOUND:
  597. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  598. }
  599. break;
  600. case Protocol::ERROR_UNSUPPORTED_OPERATION:
  601. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  602. }
  603. break;
  604. case Protocol::ERROR_NEED_MEMBERSHIP_CERTIFICATE:
  605. break;
  606. case Protocol::ERROR_NETWORK_ACCESS_DENIED_:
  607. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  608. }
  609. break;
  610. }
  611. return true;
  612. #endif
  613. }
  614. bool VL1::m_OK(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize, Protocol::Verb &inReVerb)
  615. {
  616. int ii = ZT_PROTO_PACKET_PAYLOAD_START + 13;
  617. inReVerb = (Protocol::Verb)pkt.rI8(ii);
  618. const uint64_t inRePacketId = pkt.rI64(ii);
  619. if (unlikely(Buf::readOverflow(ii, packetSize))) {
  620. RR->t->incomingPacketDropped(tPtr, 0x4c1f1ff7, packetId, 0, identityFromPeerPtr(peer), path->address(), 0, Protocol::VERB_OK, ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  621. return false;
  622. }
  623. const int64_t now = RR->node->now();
  624. if (unlikely(!RR->expect->expecting(inRePacketId, now))) {
  625. RR->t->incomingPacketDropped(tPtr, 0x4c1f1ff8, packetId, 0, identityFromPeerPtr(peer), path->address(), 0, Protocol::VERB_OK, ZT_TRACE_PACKET_DROP_REASON_REPLY_NOT_EXPECTED);
  626. return false;
  627. }
  628. ZT_SPEW("got OK in-re %s (packet ID %.16llx) from %s(%s)", Protocol::verbName(inReVerb), inRePacketId, peer->address().toString().c_str(), path->address().toString().c_str());
  629. switch (inReVerb) {
  630. case Protocol::VERB_HELLO:
  631. break;
  632. case Protocol::VERB_WHOIS:
  633. break;
  634. case Protocol::VERB_NETWORK_CONFIG_REQUEST:
  635. break;
  636. case Protocol::VERB_MULTICAST_GATHER:
  637. break;
  638. }
  639. return true;
  640. }
  641. bool VL1::m_WHOIS(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  642. {
  643. #if 0
  644. if (packetSize < (int)sizeof(Protocol::OK::Header)) {
  645. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  646. return false;
  647. }
  648. Protocol::Header &ph = pkt.as<Protocol::Header>();
  649. if (!peer->rateGateInboundWhoisRequest(RR->node->now())) {
  650. RR->t->incomingPacketDropped(tPtr,0x19f7194a,ph.packetId,0,peer->identity(),path->address(),Protocol::packetHops(ph),Protocol::VERB_WHOIS,ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED);
  651. return true;
  652. }
  653. Buf outp;
  654. Protocol::OK::WHOIS &outh = outp.as<Protocol::OK::WHOIS>();
  655. int ptr = sizeof(Protocol::Header);
  656. while ((ptr + ZT_ADDRESS_LENGTH) <= packetSize) {
  657. outh.h.h.packetId = Protocol::getPacketId();
  658. peer->address().copyTo(outh.h.h.destination);
  659. RR->identity.address().copyTo(outh.h.h.source);
  660. outh.h.h.flags = 0;
  661. outh.h.h.verb = Protocol::VERB_OK;
  662. outh.h.inReVerb = Protocol::VERB_WHOIS;
  663. outh.h.inRePacketId = ph.packetId;
  664. int outl = sizeof(Protocol::OK::WHOIS);
  665. while ( ((ptr + ZT_ADDRESS_LENGTH) <= packetSize) && ((outl + ZT_IDENTITY_MARSHAL_SIZE_MAX + ZT_LOCATOR_MARSHAL_SIZE_MAX) < ZT_PROTO_MAX_PACKET_LENGTH) ) {
  666. const SharedPtr<Peer> &wp(RR->topology->peer(tPtr,Address(pkt.unsafeData + ptr)));
  667. if (wp) {
  668. outp.wO(outl,wp->identity());
  669. if (peer->remoteVersionProtocol() >= 11) { // older versions don't know what a locator is
  670. const Locator loc(wp->locator());
  671. outp.wO(outl,loc);
  672. }
  673. if (Buf::writeOverflow(outl)) { // sanity check, shouldn't be possible
  674. RR->t->unexpectedError(tPtr,0xabc0f183,"Buf write overflow building OK(WHOIS) to reply to %s",Trace::str(peer->address(),path).s);
  675. return false;
  676. }
  677. }
  678. ptr += ZT_ADDRESS_LENGTH;
  679. }
  680. if (outl > (int)sizeof(Protocol::OK::WHOIS)) {
  681. Protocol::armor(outp,outl,peer->key(),peer->cipher());
  682. path->send(RR,tPtr,outp.unsafeData,outl,RR->node->now());
  683. }
  684. }
  685. return true;
  686. #endif
  687. }
  688. bool VL1::m_RENDEZVOUS(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  689. {
  690. #if 0
  691. if (RR->topology->isRoot(peer->identity())) {
  692. if (packetSize < (int)sizeof(Protocol::RENDEZVOUS)) {
  693. RR->t->incomingPacketDropped(tPtr,0x43e90ab3,Protocol::packetId(pkt,packetSize),0,peer->identity(),path->address(),Protocol::packetHops(pkt,packetSize),Protocol::VERB_RENDEZVOUS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  694. return false;
  695. }
  696. Protocol::RENDEZVOUS &rdv = pkt.as<Protocol::RENDEZVOUS>();
  697. const SharedPtr<Peer> with(RR->topology->peer(tPtr,Address(rdv.peerAddress)));
  698. if (with) {
  699. const int64_t now = RR->node->now();
  700. const unsigned int port = Utils::ntoh(rdv.port);
  701. if (port != 0) {
  702. switch(rdv.addressLength) {
  703. case 4:
  704. case 16:
  705. if ((int)(sizeof(Protocol::RENDEZVOUS) + rdv.addressLength) <= packetSize) {
  706. const InetAddress atAddr(pkt.unsafeData + sizeof(Protocol::RENDEZVOUS),rdv.addressLength,port);
  707. peer->tryToContactAt(tPtr,Endpoint(atAddr),now,false);
  708. RR->t->tryingNewPath(tPtr,0x55a19aaa,with->identity(),atAddr,path->address(),Protocol::packetId(pkt,packetSize),Protocol::VERB_RENDEZVOUS,peer->identity(),ZT_TRACE_TRYING_NEW_PATH_REASON_RENDEZVOUS);
  709. }
  710. break;
  711. case 255: {
  712. Endpoint ep;
  713. int p = sizeof(Protocol::RENDEZVOUS);
  714. int epl = pkt.rO(p,ep);
  715. if ((epl > 0) && (ep) && (!Buf::readOverflow(p,packetSize))) {
  716. switch (ep.type()) {
  717. case Endpoint::TYPE_INETADDR_V4:
  718. case Endpoint::TYPE_INETADDR_V6:
  719. peer->tryToContactAt(tPtr,ep,now,false);
  720. RR->t->tryingNewPath(tPtr,0x55a19aab,with->identity(),ep.inetAddr(),path->address(),Protocol::packetId(pkt,packetSize),Protocol::VERB_RENDEZVOUS,peer->identity(),ZT_TRACE_TRYING_NEW_PATH_REASON_RENDEZVOUS);
  721. break;
  722. default:
  723. break;
  724. }
  725. }
  726. } break;
  727. }
  728. }
  729. }
  730. }
  731. return true;
  732. #endif
  733. }
  734. bool VL1::m_ECHO(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  735. {
  736. #if 0
  737. const uint64_t packetId = Protocol::packetId(pkt,packetSize);
  738. const uint64_t now = RR->node->now();
  739. if (packetSize < (int)sizeof(Protocol::Header)) {
  740. RR->t->incomingPacketDropped(tPtr,0x14d70bb0,packetId,0,peer->identity(),path->address(),Protocol::packetHops(pkt,packetSize),Protocol::VERB_ECHO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  741. return false;
  742. }
  743. if (peer->rateGateEchoRequest(now)) {
  744. Buf outp;
  745. Protocol::OK::ECHO &outh = outp.as<Protocol::OK::ECHO>();
  746. outh.h.h.packetId = Protocol::getPacketId();
  747. peer->address().copyTo(outh.h.h.destination);
  748. RR->identity.address().copyTo(outh.h.h.source);
  749. outh.h.h.flags = 0;
  750. outh.h.h.verb = Protocol::VERB_OK;
  751. outh.h.inReVerb = Protocol::VERB_ECHO;
  752. outh.h.inRePacketId = packetId;
  753. int outl = sizeof(Protocol::OK::ECHO);
  754. outp.wB(outl,pkt.unsafeData + sizeof(Protocol::Header),packetSize - sizeof(Protocol::Header));
  755. if (Buf::writeOverflow(outl)) {
  756. RR->t->incomingPacketDropped(tPtr,0x14d70bb0,packetId,0,peer->identity(),path->address(),Protocol::packetHops(pkt,packetSize),Protocol::VERB_ECHO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  757. return false;
  758. }
  759. Protocol::armor(outp,outl,peer->key(),peer->cipher());
  760. path->send(RR,tPtr,outp.unsafeData,outl,now);
  761. } else {
  762. RR->t->incomingPacketDropped(tPtr,0x27878bc1,packetId,0,peer->identity(),path->address(),Protocol::packetHops(pkt,packetSize),Protocol::VERB_ECHO,ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED);
  763. }
  764. return true;
  765. #endif
  766. }
  767. bool VL1::m_PUSH_DIRECT_PATHS(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  768. {
  769. #if 0
  770. if (packetSize < (int)sizeof(Protocol::PUSH_DIRECT_PATHS)) {
  771. RR->t->incomingPacketDropped(tPtr,0x1bb1bbb1,Protocol::packetId(pkt,packetSize),0,peer->identity(),path->address(),Protocol::packetHops(pkt,packetSize),Protocol::VERB_PUSH_DIRECT_PATHS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  772. return false;
  773. }
  774. Protocol::PUSH_DIRECT_PATHS &pdp = pkt.as<Protocol::PUSH_DIRECT_PATHS>();
  775. int ptr = sizeof(Protocol::PUSH_DIRECT_PATHS);
  776. const unsigned int numPaths = Utils::ntoh(pdp.numPaths);
  777. InetAddress a;
  778. Endpoint ep;
  779. for(unsigned int pi=0;pi<numPaths;++pi) {
  780. /*const uint8_t flags = pkt.rI8(ptr);*/ ++ptr; // flags are not presently used
  781. const int xas = (int)pkt.rI16(ptr);
  782. //const uint8_t *const extendedAttrs = pkt.rBnc(ptr,xas);
  783. ptr += xas;
  784. const unsigned int addrType = pkt.rI8(ptr);
  785. const unsigned int addrRecordLen = pkt.rI8(ptr);
  786. if (addrRecordLen == 0) {
  787. RR->t->incomingPacketDropped(tPtr,0xaed00118,pdp.h.packetId,0,peer->identity(),path->address(),Protocol::packetHops(pdp.h),Protocol::VERB_PUSH_DIRECT_PATHS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  788. return false;
  789. }
  790. if (Buf::readOverflow(ptr,packetSize)) {
  791. RR->t->incomingPacketDropped(tPtr,0xb450e10f,pdp.h.packetId,0,peer->identity(),path->address(),Protocol::packetHops(pdp.h),Protocol::VERB_PUSH_DIRECT_PATHS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  792. return false;
  793. }
  794. const void *addrBytes = nullptr;
  795. unsigned int addrLen = 0;
  796. unsigned int addrPort = 0;
  797. switch(addrType) {
  798. case 0:
  799. addrBytes = pkt.rBnc(ptr,addrRecordLen);
  800. addrLen = addrRecordLen;
  801. break;
  802. case 4:
  803. addrBytes = pkt.rBnc(ptr,4);
  804. addrLen = 4;
  805. addrPort = pkt.rI16(ptr);
  806. break;
  807. case 6:
  808. addrBytes = pkt.rBnc(ptr,16);
  809. addrLen = 16;
  810. addrPort = pkt.rI16(ptr);
  811. break;
  812. //case 200:
  813. // TODO: this would be a WebRTC SDP offer contained in the extended attrs field
  814. //break;
  815. default: break;
  816. }
  817. if (Buf::readOverflow(ptr,packetSize)) {
  818. RR->t->incomingPacketDropped(tPtr,0xb4d0f10f,pdp.h.packetId,0,peer->identity(),path->address(),Protocol::packetHops(pdp.h),Protocol::VERB_PUSH_DIRECT_PATHS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  819. return false;
  820. }
  821. if (addrPort) {
  822. a.set(addrBytes,addrLen,addrPort);
  823. } else if (addrLen) {
  824. if (ep.unmarshal(reinterpret_cast<const uint8_t *>(addrBytes),(int)addrLen) <= 0) {
  825. RR->t->incomingPacketDropped(tPtr,0x00e0f00d,pdp.h.packetId,0,peer->identity(),path->address(),Protocol::packetHops(pdp.h),Protocol::VERB_PUSH_DIRECT_PATHS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  826. return false;
  827. }
  828. switch(ep.type()) {
  829. case Endpoint::TYPE_INETADDR_V4:
  830. case Endpoint::TYPE_INETADDR_V6:
  831. a = ep.inetAddr();
  832. break;
  833. default: // other types are not supported yet
  834. break;
  835. }
  836. }
  837. if (a) {
  838. RR->t->tryingNewPath(tPtr,0xa5ab1a43,peer->identity(),a,path->address(),Protocol::packetId(pkt,packetSize),Protocol::VERB_RENDEZVOUS,peer->identity(),ZT_TRACE_TRYING_NEW_PATH_REASON_RECEIVED_PUSH_DIRECT_PATHS);
  839. }
  840. ptr += (int)addrRecordLen;
  841. }
  842. // TODO: add to a peer try-queue
  843. return true;
  844. #endif
  845. }
  846. bool VL1::m_USER_MESSAGE(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  847. {
  848. // TODO
  849. return true;
  850. }
  851. bool VL1::m_ENCAP(void *tPtr, const uint64_t packetId, const unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize)
  852. {
  853. // TODO: not implemented yet
  854. return true;
  855. }
  856. } // namespace ZeroTier