VL1.cpp 39 KB

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