2
0

VL1.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 "Salsa20.hpp"
  19. #include "LZ4.hpp"
  20. #include "Poly1305.hpp"
  21. #include "Identity.hpp"
  22. #include "SelfAwareness.hpp"
  23. #include "SHA512.hpp"
  24. #include "Peer.hpp"
  25. #include "Path.hpp"
  26. #include "Expect.hpp"
  27. namespace ZeroTier {
  28. namespace {
  29. ZT_INLINE const Identity &identityFromPeerPtr(const SharedPtr<Peer> &p)
  30. {
  31. if (p)
  32. return p->identity();
  33. return Identity::NIL;
  34. }
  35. } // anonymous namespace
  36. VL1::VL1(const RuntimeEnvironment *renv) :
  37. RR(renv)
  38. {
  39. }
  40. VL1::~VL1()
  41. {
  42. }
  43. void VL1::onRemotePacket(void *const tPtr,const int64_t localSocket,const InetAddress &fromAddr,SharedPtr<Buf> &data,const unsigned int len)
  44. {
  45. // Get canonical Path object for this originating address and local socket pair.
  46. const SharedPtr<Path> path(RR->topology->path(localSocket,fromAddr));
  47. const int64_t now = RR->node->now();
  48. // Update path's last receive time (this is updated when anything is received at all, even if invalid or a keepalive)
  49. path->received(now,len);
  50. try {
  51. // Handle 8-byte short probes, which are used as a low-bandwidth way to initiate a real handshake.
  52. if (len == ZT_PROTO_PROBE_LENGTH) {
  53. const SharedPtr<Peer> peer(RR->topology->peerByProbe(data->lI64(0)));
  54. if ((peer)&&(peer->rateGateInboundProbe(now)))
  55. path->sent(now,peer->sendNOP(tPtr,path->localSocket(),path->address(),now));
  56. return;
  57. }
  58. // Discard any other runt packets that aren't probes. These are likely to be keepalives.
  59. // No reason to bother even logging them. Note that the last receive time for the path
  60. // was still updated, so tiny keepalives do keep the path alive.
  61. if (len < ZT_PROTO_MIN_FRAGMENT_LENGTH)
  62. return;
  63. // A vector of slices of buffers that aspires to eventually hold an assembled packet.
  64. // These are reassembled into a single contiguous buffer at the same time as decryption
  65. // and authentication.
  66. FCV<Buf::Slice,ZT_MAX_PACKET_FRAGMENTS> pktv;
  67. // Destination address of packet (filled below)
  68. Address destination;
  69. if (data->lI8(ZT_PROTO_PACKET_FRAGMENT_INDICATOR_INDEX) == ZT_PROTO_PACKET_FRAGMENT_INDICATOR) {
  70. // Fragment -----------------------------------------------------------------------------------------------------
  71. const Protocol::FragmentHeader &fragmentHeader = data->as<Protocol::FragmentHeader>();
  72. destination.setTo(fragmentHeader.destination);
  73. if (destination != RR->identity.address()) {
  74. _relay(tPtr,path,destination,data,len);
  75. return;
  76. }
  77. switch (_inputPacketAssembler.assemble(
  78. fragmentHeader.packetId,
  79. pktv,
  80. data,
  81. ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT,
  82. (unsigned int)(len - ZT_PROTO_PACKET_FRAGMENT_PAYLOAD_START_AT),
  83. fragmentHeader.counts & 0xfU, // fragment number
  84. fragmentHeader.counts >> 4U, // total number of fragments in message is specified in each fragment
  85. now,
  86. path,
  87. ZT_MAX_INCOMING_FRAGMENTS_PER_PATH)) {
  88. case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::COMPLETE:
  89. break;
  90. default:
  91. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::OK:
  92. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_DUPLICATE_FRAGMENT:
  93. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_INVALID_FRAGMENT:
  94. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_TOO_MANY_FRAGMENTS_FOR_PATH:
  95. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_OUT_OF_MEMORY:
  96. return;
  97. }
  98. } else {
  99. // Not fragment, meaning whole packet or head of series with fragments ------------------------------------------
  100. if (len < ZT_PROTO_MIN_PACKET_LENGTH)
  101. return;
  102. const Protocol::Header &packetHeader = data->as<Protocol::Header>();
  103. destination.setTo(packetHeader.destination);
  104. if (destination != RR->identity.address()) {
  105. _relay(tPtr,path,destination,data,len);
  106. return;
  107. }
  108. if ((packetHeader.flags & ZT_PROTO_FLAG_FRAGMENTED) != 0) {
  109. switch (_inputPacketAssembler.assemble(
  110. packetHeader.packetId,
  111. pktv,
  112. data,
  113. 0,
  114. len,
  115. 0, // always the zero'eth fragment
  116. 0, // this is specified in fragments, not in the head
  117. now,
  118. path,
  119. ZT_MAX_INCOMING_FRAGMENTS_PER_PATH)) {
  120. case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::COMPLETE:
  121. break;
  122. default:
  123. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::OK:
  124. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_DUPLICATE_FRAGMENT:
  125. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_INVALID_FRAGMENT:
  126. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_TOO_MANY_FRAGMENTS_FOR_PATH:
  127. //case Defragmenter<ZT_MAX_PACKET_FRAGMENTS>::ERR_OUT_OF_MEMORY:
  128. return;
  129. }
  130. } else { // packet isn't fragmented, so skip the Defragmenter logic completely.
  131. Buf::Slice &s = pktv.push();
  132. s.b.swap(data);
  133. s.s = 0;
  134. s.e = len;
  135. }
  136. }
  137. // Packet defragmented and apparently addressed to this node ------------------------------------------------------
  138. // Subject pktv to a few sanity checks just to make sure Defragmenter worked correctly and
  139. // there is enough room in each slice to shift their contents to sizes that are multiples
  140. // of 64 if needed for crypto.
  141. if ((pktv.empty()) || (((int)pktv[0].e - (int)pktv[0].s) < (int)sizeof(Protocol::Header))) {
  142. RR->t->unexpectedError(tPtr,0x3df19990,"empty or undersized packet vector after parsing packet from %s of length %d",Trace::str(path->address()).s,(int)len);
  143. return;
  144. }
  145. for(FCV<Buf::Slice,ZT_MAX_PACKET_FRAGMENTS>::const_iterator s(pktv.begin());s!=pktv.end();++s) {
  146. if ((s->e > (ZT_BUF_MEM_SIZE - 64))||(s->s > s->e))
  147. return;
  148. }
  149. Protocol::Header *ph = &(pktv[0].b->as<Protocol::Header>(pktv[0].s));
  150. const Address source(ph->source);
  151. if (source == RR->identity.address())
  152. return;
  153. SharedPtr<Peer> peer(RR->topology->peer(tPtr,source));
  154. Buf::Slice pkt;
  155. bool authenticated = false;
  156. const uint8_t hops = Protocol::packetHops(*ph);
  157. const uint8_t cipher = Protocol::packetCipher(*ph);
  158. unsigned int packetSize = pktv[0].e - pktv[0].s;
  159. for(FCV<Buf::Slice,ZT_MAX_PACKET_FRAGMENTS>::const_iterator s(pktv.begin()+1);s!=pktv.end();++s)
  160. packetSize += s->e - s->s;
  161. if (packetSize > ZT_PROTO_MAX_PACKET_LENGTH) {
  162. RR->t->incomingPacketDropped(tPtr,0x010348da,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  163. return;
  164. }
  165. // If we don't know this peer and this is not a HELLO, issue a WHOIS and enqueue this packet to try again.
  166. if ((!peer)&&(!(((cipher == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE)||(cipher == ZT_PROTO_CIPHER_SUITE__NONE))&&((ph->verb & 0x1fU) == Protocol::VERB_HELLO)))) {
  167. pkt = Buf::assembleSliceVector(pktv);
  168. if (pkt.e < ZT_PROTO_MIN_PACKET_LENGTH) {
  169. RR->t->incomingPacketDropped(tPtr,0xbada9366,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  170. return;
  171. }
  172. {
  173. Mutex::Lock wl(_whoisQueue_l);
  174. _WhoisQueueItem &wq = _whoisQueue[source];
  175. wq.inboundPackets.push_back(pkt);
  176. }
  177. _sendPendingWhois(tPtr,now);
  178. return;
  179. }
  180. switch(cipher) {
  181. case ZT_PROTO_CIPHER_SUITE__POLY1305_NONE:
  182. if (peer) {
  183. pkt = Buf::assembleSliceVector(pktv);
  184. if (pkt.e < ZT_PROTO_MIN_PACKET_LENGTH) {
  185. RR->t->incomingPacketDropped(tPtr,0x432aa9da,ph->packetId,0,peer->identity(),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  186. return;
  187. }
  188. ph = &(pkt.b->as<Protocol::Header>());
  189. // Generate one-time-use MAC key using Salsa20.
  190. uint8_t perPacketKey[ZT_SYMMETRIC_KEY_SIZE];
  191. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  192. Protocol::salsa2012DeriveKey(peer->key(),perPacketKey,*pktv[0].b,packetSize);
  193. Salsa20(perPacketKey,&ph->packetId).crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_SIZE);
  194. // Verify packet MAC.
  195. uint64_t mac[2];
  196. poly1305(mac,pkt.b->unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  197. if (ph->mac != mac[0]) {
  198. RR->t->incomingPacketDropped(tPtr,0xcc89c812,ph->packetId,0,peer->identity(),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  199. return;
  200. }
  201. authenticated = true;
  202. }
  203. break;
  204. case ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012:
  205. if (peer) {
  206. // Derive per-packet key using symmetric key plus some data from the packet header.
  207. uint8_t perPacketKey[ZT_SYMMETRIC_KEY_SIZE];
  208. Protocol::salsa2012DeriveKey(peer->key(),perPacketKey,*pktv[0].b,packetSize);
  209. Salsa20 s20(perPacketKey,&ph->packetId);
  210. // Do one Salsa20 block to generate the one-time-use Poly1305 key.
  211. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  212. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_SIZE);
  213. // Get a buffer to store the decrypted and fully contiguous packet.
  214. pkt.b.set(new Buf());
  215. // Salsa20 is a stream cipher but it's only seekable to multiples of 64 bytes.
  216. // This moves data in slices around so that all slices have sizes that are
  217. // multiples of 64 except the last slice. Note that this does not corrupt
  218. // the assembled slice vector, just moves data around.
  219. if (pktv.size() > 1) {
  220. unsigned int prevOverflow,i;
  221. for (typename FCV<Buf::Slice,ZT_MAX_PACKET_FRAGMENTS>::iterator ps(pktv.begin()),s(ps + 1);s!=pktv.end();) {
  222. prevOverflow = (ps->e - ps->s) & 63U; // amount by which previous exceeds a multiple of 64
  223. for(i=0;i<prevOverflow;++i) {
  224. if (s->s >= s->e)
  225. goto next_slice;
  226. ps->b->unsafeData[ps->e++] = s->b->unsafeData[s->s++]; // move from head of current to end of previous
  227. }
  228. next_slice: ps = s++;
  229. }
  230. }
  231. // Simultaneously decrypt and assemble packet into a contiguous buffer.
  232. // Since we moved data around above all slices will have sizes that are
  233. // multiples of 64.
  234. Utils::copy<sizeof(Protocol::Header)>(pkt.b->unsafeData,ph);
  235. pkt.e = sizeof(Protocol::Header);
  236. for(FCV<Buf::Slice,ZT_MAX_PACKET_FRAGMENTS>::iterator s(pktv.begin());s!=pktv.end();++s) {
  237. const unsigned int sliceSize = s->e - s->s;
  238. s20.crypt12(s->b->unsafeData + s->s,pkt.b->unsafeData + pkt.e,sliceSize);
  239. pkt.e += sliceSize;
  240. }
  241. ph = &(pkt.b->as<Protocol::Header>());
  242. // Verify packet MAC.
  243. uint64_t mac[2];
  244. poly1305(mac,pkt.b->unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  245. if (ph->mac != mac[0]) {
  246. RR->t->incomingPacketDropped(tPtr,0xbc881231,ph->packetId,0,peer->identity(),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  247. return;
  248. }
  249. authenticated = true;
  250. } else {
  251. RR->t->incomingPacketDropped(tPtr,0xb0b01999,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  252. return;
  253. }
  254. break;
  255. case ZT_PROTO_CIPHER_SUITE__NONE: {
  256. // CIPHER_SUITE__NONE is only used with trusted paths. Verification is performed by
  257. // checking the address and the presence of its corresponding trusted path ID in the
  258. // packet header's MAC field.
  259. pkt = Buf::assembleSliceVector(pktv);
  260. if (pkt.e < ZT_PROTO_MIN_PACKET_LENGTH)
  261. RR->t->incomingPacketDropped(tPtr,0x3d3337df,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  262. ph = &(pkt.b->as<Protocol::Header>());
  263. if (RR->topology->shouldInboundPathBeTrusted(path->address(),Utils::ntoh(ph->mac))) {
  264. authenticated = true;
  265. } else {
  266. RR->t->incomingPacketDropped(tPtr,0x2dfa910b,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_NOT_TRUSTED_PATH);
  267. return;
  268. }
  269. } break;
  270. //case ZT_PROTO_CIPHER_SUITE__AES_GCM_NRH:
  271. // if (peer) {
  272. // }
  273. // break;
  274. default:
  275. RR->t->incomingPacketDropped(tPtr,0x5b001099,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  276. return;
  277. }
  278. // Packet fully assembled, authenticated 'true' if already authenticated via MAC ----------------------------------
  279. // Return any still held buffers in pktv to the buffer pool.
  280. pktv.clear();
  281. const Protocol::Verb verb = (Protocol::Verb)(ph->verb & ZT_PROTO_VERB_MASK);
  282. // All verbs except HELLO require authentication before being handled. The HELLO
  283. // handler does its own authentication.
  284. if (((!authenticated)||(!peer))&&(verb != Protocol::VERB_HELLO)) {
  285. RR->t->incomingPacketDropped(tPtr,0x5b001099,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,verb,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  286. return;
  287. }
  288. // Decompress packet payload if compressed. For additional safety decompression is
  289. // only performed on packets whose MACs have already been validated. (Only HELLO is
  290. // sent without this, and HELLO doesn't benefit from compression.)
  291. if ((ph->verb & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0) {
  292. if (!authenticated) {
  293. RR->t->incomingPacketDropped(tPtr,0x390bcd0a,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,verb,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  294. return;
  295. }
  296. SharedPtr<Buf> nb(new Buf());
  297. const int uncompressedLen = LZ4_decompress_safe(
  298. reinterpret_cast<const char *>(pkt.b->unsafeData + ZT_PROTO_PACKET_PAYLOAD_START),
  299. reinterpret_cast<char *>(nb->unsafeData),
  300. (int)(packetSize - ZT_PROTO_PACKET_PAYLOAD_START),
  301. ZT_BUF_MEM_SIZE - ZT_PROTO_PACKET_PAYLOAD_START);
  302. if ((uncompressedLen > 0)&&(uncompressedLen <= (ZT_BUF_MEM_SIZE - ZT_PROTO_PACKET_PAYLOAD_START))) {
  303. pkt.b.swap(nb);
  304. pkt.e = packetSize = (unsigned int)uncompressedLen;
  305. } else {
  306. RR->t->incomingPacketDropped(tPtr,0xee9e4392,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,verb,ZT_TRACE_PACKET_DROP_REASON_INVALID_COMPRESSED_DATA);
  307. return;
  308. }
  309. }
  310. /*
  311. * Important notes:
  312. *
  313. * All verbs except HELLO assume that authenticated is true and peer is non-NULL.
  314. * This is checked above. HELLO will accept either case and always performs its
  315. * own secondary validation. The path argument is never NULL.
  316. *
  317. * VL1 and VL2 are conceptually separate layers of the ZeroTier protocol. In the
  318. * code they are almost entirely logically separate. To make the code easier to
  319. * understand the handlers for VL2 data paths have been moved to a VL2 class.
  320. */
  321. bool ok = true; // set to false if a packet turns out to be invalid
  322. Protocol::Verb inReVerb = Protocol::VERB_NOP; // set via result parameter to _ERROR and _OK
  323. switch(verb) {
  324. case Protocol::VERB_NOP: break;
  325. case Protocol::VERB_HELLO: ok = _HELLO(tPtr,path,peer,*pkt.b,(int)packetSize,authenticated); break;
  326. case Protocol::VERB_ERROR: ok = _ERROR(tPtr,path,peer,*pkt.b,(int)packetSize,inReVerb); break;
  327. case Protocol::VERB_OK: ok = _OK(tPtr,path,peer,*pkt.b,(int)packetSize,inReVerb); break;
  328. case Protocol::VERB_WHOIS: ok = _WHOIS(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  329. case Protocol::VERB_RENDEZVOUS: ok = _RENDEZVOUS(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  330. case Protocol::VERB_FRAME: ok = RR->vl2->_FRAME(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  331. case Protocol::VERB_EXT_FRAME: ok = RR->vl2->_EXT_FRAME(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  332. case Protocol::VERB_ECHO: ok = _ECHO(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  333. case Protocol::VERB_MULTICAST_LIKE: ok = RR->vl2->_MULTICAST_LIKE(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  334. case Protocol::VERB_NETWORK_CREDENTIALS: ok = RR->vl2->_NETWORK_CREDENTIALS(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  335. case Protocol::VERB_NETWORK_CONFIG_REQUEST: ok = RR->vl2->_NETWORK_CONFIG_REQUEST(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  336. case Protocol::VERB_NETWORK_CONFIG: ok = RR->vl2->_NETWORK_CONFIG(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  337. case Protocol::VERB_MULTICAST_GATHER: ok = RR->vl2->_MULTICAST_GATHER(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  338. case Protocol::VERB_MULTICAST_FRAME_deprecated: ok = RR->vl2->_MULTICAST_FRAME_deprecated(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  339. case Protocol::VERB_PUSH_DIRECT_PATHS: ok = _PUSH_DIRECT_PATHS(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  340. case Protocol::VERB_USER_MESSAGE: ok = _USER_MESSAGE(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  341. case Protocol::VERB_MULTICAST: ok = RR->vl2->_MULTICAST(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  342. case Protocol::VERB_ENCAP: ok = _ENCAP(tPtr,path,peer,*pkt.b,(int)packetSize); break;
  343. default:
  344. RR->t->incomingPacketDropped(tPtr,0xeeeeeff0,ph->packetId,0,identityFromPeerPtr(peer),path->address(),hops,verb,ZT_TRACE_PACKET_DROP_REASON_UNRECOGNIZED_VERB);
  345. break;
  346. }
  347. if (ok)
  348. peer->received(tPtr,path,hops,ph->packetId,packetSize - ZT_PROTO_PACKET_PAYLOAD_START,verb,inReVerb);
  349. } catch ( ... ) {
  350. RR->t->unexpectedError(tPtr,0xea1b6dea,"unexpected exception in onRemotePacket() parsing packet from %s",Trace::str(path->address()).s);
  351. }
  352. }
  353. void VL1::_relay(void *tPtr,const SharedPtr<Path> &path,const Address &destination,SharedPtr<Buf> &data,unsigned int len)
  354. {
  355. const uint8_t newHopCount = (data->lI8(ZT_PROTO_PACKET_FLAGS_INDEX) & 7U) + 1;
  356. if (newHopCount >= ZT_RELAY_MAX_HOPS)
  357. return;
  358. data->sI8(ZT_PROTO_PACKET_FLAGS_INDEX,(data->lI8(ZT_PROTO_PACKET_FLAGS_INDEX) & 0xf8U) | newHopCount);
  359. const SharedPtr<Peer> toPeer(RR->topology->peer(tPtr,destination,false));
  360. if (!toPeer)
  361. return;
  362. const uint64_t now = RR->node->now();
  363. const SharedPtr<Path> toPath(toPeer->path(now));
  364. if (!toPath)
  365. return;
  366. toPath->send(RR,tPtr,data->unsafeData,len,now);
  367. }
  368. void VL1::_sendPendingWhois(void *const tPtr,const int64_t now)
  369. {
  370. SharedPtr<Peer> root(RR->topology->root());
  371. if (!root)
  372. return;
  373. SharedPtr<Path> rootPath(root->path(now));
  374. if (!rootPath)
  375. return;
  376. std::vector<Address> toSend;
  377. {
  378. Mutex::Lock wl(_whoisQueue_l);
  379. for(Map<Address,_WhoisQueueItem>::iterator wi(_whoisQueue.begin());wi!=_whoisQueue.end();++wi) {
  380. if ((now - wi->second.lastRetry) >= ZT_WHOIS_RETRY_DELAY) {
  381. wi->second.lastRetry = now;
  382. ++wi->second.retries;
  383. toSend.push_back(wi->first);
  384. }
  385. }
  386. }
  387. Buf outp;
  388. Protocol::Header &ph = outp.as<Protocol::Header>();
  389. std::vector<Address>::iterator a(toSend.begin());
  390. while (a != toSend.end()) {
  391. ph.packetId = Protocol::getPacketId();
  392. root->address().copyTo(ph.destination);
  393. RR->identity.address().copyTo(ph.source);
  394. ph.flags = 0;
  395. ph.verb = Protocol::VERB_OK;
  396. int outl = sizeof(Protocol::Header);
  397. while ((a != toSend.end())&&(outl < ZT_PROTO_MAX_PACKET_LENGTH)) {
  398. a->copyTo(outp.unsafeData + outl);
  399. ++a;
  400. outl += ZT_ADDRESS_LENGTH;
  401. }
  402. if (outl > (int)sizeof(Protocol::Header)) {
  403. Protocol::armor(outp,outl,root->key(),root->cipher());
  404. RR->expect->sending(ph.packetId,now);
  405. rootPath->send(RR,tPtr,outp.unsafeData,outl,now);
  406. }
  407. }
  408. }
  409. bool VL1::_HELLO(void *tPtr,const SharedPtr<Path> &path,SharedPtr<Peer> &peer,Buf &pkt,int packetSize,const bool authenticated)
  410. {
  411. if (packetSize < (int)sizeof(Protocol::HELLO)) {
  412. RR->t->incomingPacketDropped(tPtr,0x2bdb0001,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  413. return false;
  414. }
  415. Protocol::HELLO &p = pkt.as<Protocol::HELLO>();
  416. const uint8_t hops = Protocol::packetHops(p.h);
  417. p.h.flags &= (uint8_t)~ZT_PROTO_FLAG_FIELD_HOPS_MASK; // mask off hops for MAC calculation
  418. int ptr = sizeof(Protocol::HELLO);
  419. if (p.versionProtocol < ZT_PROTO_VERSION_MIN) {
  420. RR->t->incomingPacketDropped(tPtr,0xe8d12bad,p.h.packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_PEER_TOO_OLD);
  421. return false;
  422. }
  423. Identity id;
  424. if (pkt.rO(ptr,id) < 0) {
  425. RR->t->incomingPacketDropped(tPtr,0x707a9810,p.h.packetId,0,identityFromPeerPtr(peer),path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  426. return false;
  427. }
  428. if (Address(p.h.source) != id.address()) {
  429. RR->t->incomingPacketDropped(tPtr,0x06aa9ff1,p.h.packetId,0,Identity::NIL,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  430. return false;
  431. }
  432. // Packet is basically valid and identity unmarshaled successfully --------------------------------------------------
  433. uint8_t key[ZT_SYMMETRIC_KEY_SIZE];
  434. if ((peer) && (id == peer->identity())) {
  435. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key,peer->key());
  436. } else {
  437. peer.zero();
  438. if (!RR->identity.agree(id,key)) {
  439. RR->t->incomingPacketDropped(tPtr,0x46db8010,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  440. return false;
  441. }
  442. }
  443. if ((!peer)||(!authenticated)) {
  444. uint8_t perPacketKey[ZT_SYMMETRIC_KEY_SIZE];
  445. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  446. Protocol::salsa2012DeriveKey(peer->key(),perPacketKey,pkt,packetSize);
  447. Salsa20(perPacketKey,&p.h.packetId).crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_SIZE);
  448. uint64_t mac[2];
  449. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  450. if (p.h.mac != mac[0]) {
  451. RR->t->incomingPacketDropped(tPtr,0x11bfff81,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  452. return false;
  453. }
  454. }
  455. // Packet has passed Poly1305 MAC authentication --------------------------------------------------------------------
  456. uint8_t hmacKey[ZT_SYMMETRIC_KEY_SIZE],hmac[ZT_HMACSHA384_LEN];
  457. if (peer->remoteVersionProtocol() >= 11) {
  458. if (packetSize <= ZT_HMACSHA384_LEN) { // sanity check, should be impossible
  459. RR->t->incomingPacketDropped(tPtr,0x1000662a,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  460. return false;
  461. }
  462. packetSize -= ZT_HMACSHA384_LEN;
  463. KBKDFHMACSHA384(key,ZT_PROTO_KDF_KEY_LABEL_HELLO_HMAC,0,0,hmacKey); // iter == 0 for HELLO, 1 for OK(HELLO)
  464. HMACSHA384(hmacKey,pkt.unsafeData,packetSize,hmac);
  465. if (!Utils::secureEq(pkt.unsafeData + packetSize,hmac,ZT_HMACSHA384_LEN)) {
  466. RR->t->incomingPacketDropped(tPtr,0x1000662a,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  467. return false;
  468. }
  469. }
  470. // Packet has passed HMAC-SHA384 (if present) -----------------------------------------------------------------------
  471. InetAddress externalSurfaceAddress;
  472. Dictionary nodeMetaData;
  473. // Get external surface address if present.
  474. if (ptr < packetSize) {
  475. if (pkt.rO(ptr,externalSurfaceAddress) < 0) {
  476. RR->t->incomingPacketDropped(tPtr,0x10001003,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  477. return false;
  478. }
  479. }
  480. if ((ptr < packetSize)&&(peer->remoteVersionProtocol() >= 11)) {
  481. // Everything after this point is encrypted with Salsa20/12. This is only a privacy measure
  482. // since there's nothing truly secret in a HELLO packet. It also means that an observer
  483. // can't even get ephemeral public keys without first knowing the long term secret key,
  484. // adding a little defense in depth.
  485. uint8_t iv[8];
  486. for (int i = 0; i < 8; ++i) iv[i] = pkt.unsafeData[i];
  487. iv[7] &= 0xf8U; // this exists for pure legacy reasons, meh...
  488. Salsa20 s20(key,iv);
  489. s20.crypt12(pkt.unsafeData + ptr,pkt.unsafeData + ptr,packetSize - ptr);
  490. ptr += pkt.rI16(ptr); // skip length field which currently is always zero in v2.0+
  491. if (ptr < packetSize) {
  492. const unsigned int dictionarySize = pkt.rI16(ptr);
  493. const void *const dictionaryBytes = pkt.rBnc(ptr,dictionarySize);
  494. if (Buf::readOverflow(ptr,packetSize)) {
  495. RR->t->incomingPacketDropped(tPtr,0x0d0f0112,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  496. return false;
  497. }
  498. if (dictionarySize) {
  499. if (!nodeMetaData.decode(dictionaryBytes,dictionarySize)) {
  500. RR->t->incomingPacketDropped(tPtr,0x67192344,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  501. return false;
  502. }
  503. }
  504. ptr += pkt.rI16(ptr); // skip any additional fields, currently always 0
  505. }
  506. }
  507. if (Buf::readOverflow(ptr,packetSize)) { // sanity check, should be impossible
  508. RR->t->incomingPacketDropped(tPtr,0x50003470,0,p.h.packetId,id,path->address(),0,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  509. return false;
  510. }
  511. // Packet is fully decoded and has passed all tests -----------------------------------------------------------------
  512. const int64_t now = RR->node->now();
  513. if (!peer) {
  514. if (!id.locallyValidate()) {
  515. RR->t->incomingPacketDropped(tPtr,0x2ff7a909,p.h.packetId,0,id,path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  516. return false;
  517. }
  518. peer.set(new Peer(RR));
  519. if (!peer)
  520. return false;
  521. peer->init(id);
  522. peer = RR->topology->add(tPtr,peer);
  523. }
  524. // All validation steps complete, peer learned if not yet known -----------------------------------------------------
  525. if ((hops == 0) && (externalSurfaceAddress))
  526. RR->sa->iam(tPtr,id,path->localSocket(),path->address(),externalSurfaceAddress,RR->topology->isRoot(id),now);
  527. peer->setRemoteVersion(p.versionProtocol,p.versionMajor,p.versionMinor,Utils::ntoh(p.versionRev));
  528. // Compose and send OK(HELLO) ---------------------------------------------------------------------------------------
  529. std::vector<uint8_t> myNodeMetaDataBin;
  530. {
  531. Dictionary myNodeMetaData;
  532. myNodeMetaData.encode(myNodeMetaDataBin);
  533. }
  534. if (myNodeMetaDataBin.size() > ZT_PROTO_MAX_PACKET_LENGTH) {
  535. RR->t->unexpectedError(tPtr,0xbc8861e0,"node meta-data dictionary exceeds maximum packet length while composing OK(HELLO) to %s",Trace::str(id.address(),path).s);
  536. return false;
  537. }
  538. Buf outp;
  539. Protocol::OK::HELLO &ok = outp.as<Protocol::OK::HELLO>();
  540. ok.h.h.packetId = Protocol::getPacketId();
  541. id.address().copyTo(ok.h.h.destination);
  542. RR->identity.address().copyTo(ok.h.h.source);
  543. ok.h.h.flags = 0;
  544. ok.h.h.verb = Protocol::VERB_OK;
  545. ok.h.inReVerb = Protocol::VERB_HELLO;
  546. ok.h.inRePacketId = p.h.packetId;
  547. ok.timestampEcho = p.timestamp;
  548. ok.versionProtocol = ZT_PROTO_VERSION;
  549. ok.versionMajor = ZEROTIER_VERSION_MAJOR;
  550. ok.versionMinor = ZEROTIER_VERSION_MINOR;
  551. ok.versionRev = ZT_CONST_TO_BE_UINT16(ZEROTIER_VERSION_REVISION);
  552. int outl = sizeof(Protocol::OK::HELLO);
  553. outp.wO(outl,path->address());
  554. outp.wI16(outl,0); // legacy field, always 0
  555. if (p.versionProtocol >= 11) {
  556. outp.wI16(outl,(uint16_t)myNodeMetaDataBin.size());
  557. outp.wB(outl,myNodeMetaDataBin.data(),(unsigned int)myNodeMetaDataBin.size());
  558. outp.wI16(outl,0); // length of additional fields, currently 0
  559. if ((outl + ZT_HMACSHA384_LEN) > ZT_PROTO_MAX_PACKET_LENGTH) // sanity check, shouldn't be possible
  560. return false;
  561. KBKDFHMACSHA384(key,ZT_PROTO_KDF_KEY_LABEL_HELLO_HMAC,0,1,hmacKey); // iter == 1 for OK
  562. HMACSHA384(hmacKey,outp.unsafeData + sizeof(ok.h),outl - sizeof(ok.h),outp.unsafeData + outl);
  563. outl += ZT_HMACSHA384_LEN;
  564. }
  565. Protocol::armor(outp,outl,peer->key(),peer->cipher());
  566. path->send(RR,tPtr,outp.unsafeData,outl,now);
  567. return true;
  568. }
  569. bool VL1::_ERROR(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize,Protocol::Verb &inReVerb)
  570. {
  571. if (packetSize < (int)sizeof(Protocol::ERROR::Header)) {
  572. RR->t->incomingPacketDropped(tPtr,0x3beb1947,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_ERROR,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  573. return false;
  574. }
  575. Protocol::ERROR::Header &eh = pkt.as<Protocol::ERROR::Header>();
  576. inReVerb = (Protocol::Verb)eh.inReVerb;
  577. const int64_t now = RR->node->now();
  578. if (!RR->expect->expecting(eh.inRePacketId,now)) {
  579. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_REPLY_NOT_EXPECTED);
  580. return false;
  581. }
  582. switch(eh.error) {
  583. //case Protocol::ERROR_INVALID_REQUEST:
  584. //case Protocol::ERROR_BAD_PROTOCOL_VERSION:
  585. //case Protocol::ERROR_CANNOT_DELIVER:
  586. default:
  587. break;
  588. case Protocol::ERROR_OBJ_NOT_FOUND:
  589. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  590. }
  591. break;
  592. case Protocol::ERROR_UNSUPPORTED_OPERATION:
  593. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  594. }
  595. break;
  596. case Protocol::ERROR_NEED_MEMBERSHIP_CERTIFICATE:
  597. break;
  598. case Protocol::ERROR_NETWORK_ACCESS_DENIED_:
  599. if (eh.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  600. }
  601. break;
  602. }
  603. return true;
  604. }
  605. bool VL1::_OK(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize,Protocol::Verb &inReVerb)
  606. {
  607. if (packetSize < (int)sizeof(Protocol::OK::Header)) {
  608. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  609. return false;
  610. }
  611. Protocol::OK::Header &oh = pkt.as<Protocol::OK::Header>();
  612. inReVerb = (Protocol::Verb)oh.inReVerb;
  613. const int64_t now = RR->node->now();
  614. if (!RR->expect->expecting(oh.inRePacketId,now)) {
  615. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_REPLY_NOT_EXPECTED);
  616. return false;
  617. }
  618. switch(oh.inReVerb) {
  619. case Protocol::VERB_HELLO:
  620. break;
  621. case Protocol::VERB_WHOIS:
  622. break;
  623. case Protocol::VERB_NETWORK_CONFIG_REQUEST:
  624. break;
  625. case Protocol::VERB_MULTICAST_GATHER:
  626. break;
  627. }
  628. return true;
  629. }
  630. bool VL1::_WHOIS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  631. {
  632. if (packetSize < (int)sizeof(Protocol::OK::Header)) {
  633. RR->t->incomingPacketDropped(tPtr,0x4c1f1ff7,0,0,identityFromPeerPtr(peer),path->address(),0,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  634. return false;
  635. }
  636. Protocol::Header &ph = pkt.as<Protocol::Header>();
  637. if (!peer->rateGateInboundWhoisRequest(RR->node->now())) {
  638. 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);
  639. return true;
  640. }
  641. Buf outp;
  642. Protocol::OK::WHOIS &outh = outp.as<Protocol::OK::WHOIS>();
  643. int ptr = sizeof(Protocol::Header);
  644. while ((ptr + ZT_ADDRESS_LENGTH) <= packetSize) {
  645. outh.h.h.packetId = Protocol::getPacketId();
  646. peer->address().copyTo(outh.h.h.destination);
  647. RR->identity.address().copyTo(outh.h.h.source);
  648. outh.h.h.flags = 0;
  649. outh.h.h.verb = Protocol::VERB_OK;
  650. outh.h.inReVerb = Protocol::VERB_WHOIS;
  651. outh.h.inRePacketId = ph.packetId;
  652. int outl = sizeof(Protocol::OK::WHOIS);
  653. while ( ((ptr + ZT_ADDRESS_LENGTH) <= packetSize) && ((outl + ZT_IDENTITY_MARSHAL_SIZE_MAX + ZT_LOCATOR_MARSHAL_SIZE_MAX) < ZT_PROTO_MAX_PACKET_LENGTH) ) {
  654. const SharedPtr<Peer> &wp(RR->topology->peer(tPtr,Address(pkt.unsafeData + ptr)));
  655. if (wp) {
  656. outp.wO(outl,wp->identity());
  657. if (peer->remoteVersionProtocol() >= 11) { // older versions don't know what a locator is
  658. const Locator loc(wp->locator());
  659. outp.wO(outl,loc);
  660. }
  661. if (Buf::writeOverflow(outl)) { // sanity check, shouldn't be possible
  662. RR->t->unexpectedError(tPtr,0xabc0f183,"Buf write overflow building OK(WHOIS) to reply to %s",Trace::str(peer->address(),path).s);
  663. return false;
  664. }
  665. }
  666. ptr += ZT_ADDRESS_LENGTH;
  667. }
  668. if (outl > (int)sizeof(Protocol::OK::WHOIS)) {
  669. Protocol::armor(outp,outl,peer->key(),peer->cipher());
  670. path->send(RR,tPtr,outp.unsafeData,outl,RR->node->now());
  671. }
  672. }
  673. return true;
  674. }
  675. bool VL1::_RENDEZVOUS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  676. {
  677. if (RR->topology->isRoot(peer->identity())) {
  678. if (packetSize < (int)sizeof(Protocol::RENDEZVOUS)) {
  679. 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);
  680. return false;
  681. }
  682. Protocol::RENDEZVOUS &rdv = pkt.as<Protocol::RENDEZVOUS>();
  683. const SharedPtr<Peer> with(RR->topology->peer(tPtr,Address(rdv.peerAddress)));
  684. if (with) {
  685. const int64_t now = RR->node->now();
  686. const unsigned int port = Utils::ntoh(rdv.port);
  687. if (port != 0) {
  688. switch(rdv.addressLength) {
  689. case 4:
  690. case 16:
  691. if ((int)(sizeof(Protocol::RENDEZVOUS) + rdv.addressLength) <= packetSize) {
  692. const InetAddress atAddr(pkt.unsafeData + sizeof(Protocol::RENDEZVOUS),rdv.addressLength,port);
  693. peer->tryToContactAt(tPtr,Endpoint(atAddr),now,false);
  694. 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);
  695. }
  696. break;
  697. case 255: {
  698. Endpoint ep;
  699. int p = sizeof(Protocol::RENDEZVOUS);
  700. int epl = pkt.rO(p,ep);
  701. if ((epl > 0) && (ep) && (!Buf::readOverflow(p,packetSize))) {
  702. switch (ep.type()) {
  703. case Endpoint::TYPE_INETADDR_V4:
  704. case Endpoint::TYPE_INETADDR_V6:
  705. peer->tryToContactAt(tPtr,ep,now,false);
  706. 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);
  707. break;
  708. default:
  709. break;
  710. }
  711. }
  712. } break;
  713. }
  714. }
  715. }
  716. }
  717. return true;
  718. }
  719. bool VL1::_ECHO(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  720. {
  721. const uint64_t packetId = Protocol::packetId(pkt,packetSize);
  722. const uint64_t now = RR->node->now();
  723. if (packetSize < (int)sizeof(Protocol::Header)) {
  724. 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);
  725. return false;
  726. }
  727. if (peer->rateGateEchoRequest(now)) {
  728. Buf outp;
  729. Protocol::OK::ECHO &outh = outp.as<Protocol::OK::ECHO>();
  730. outh.h.h.packetId = Protocol::getPacketId();
  731. peer->address().copyTo(outh.h.h.destination);
  732. RR->identity.address().copyTo(outh.h.h.source);
  733. outh.h.h.flags = 0;
  734. outh.h.h.verb = Protocol::VERB_OK;
  735. outh.h.inReVerb = Protocol::VERB_ECHO;
  736. outh.h.inRePacketId = packetId;
  737. int outl = sizeof(Protocol::OK::ECHO);
  738. outp.wB(outl,pkt.unsafeData + sizeof(Protocol::Header),packetSize - sizeof(Protocol::Header));
  739. if (Buf::writeOverflow(outl)) {
  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. Protocol::armor(outp,outl,peer->key(),peer->cipher());
  744. path->send(RR,tPtr,outp.unsafeData,outl,now);
  745. } else {
  746. 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);
  747. }
  748. return true;
  749. }
  750. bool VL1::_PUSH_DIRECT_PATHS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  751. {
  752. if (packetSize < (int)sizeof(Protocol::PUSH_DIRECT_PATHS)) {
  753. 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);
  754. return false;
  755. }
  756. Protocol::PUSH_DIRECT_PATHS &pdp = pkt.as<Protocol::PUSH_DIRECT_PATHS>();
  757. int ptr = sizeof(Protocol::PUSH_DIRECT_PATHS);
  758. const unsigned int numPaths = Utils::ntoh(pdp.numPaths);
  759. InetAddress a;
  760. Endpoint ep;
  761. for(unsigned int pi=0;pi<numPaths;++pi) {
  762. /*const uint8_t flags = pkt.rI8(ptr);*/ ++ptr; // flags are not presently used
  763. const int xas = (int)pkt.rI16(ptr);
  764. //const uint8_t *const extendedAttrs = pkt.rBnc(ptr,xas);
  765. ptr += xas;
  766. const unsigned int addrType = pkt.rI8(ptr);
  767. const unsigned int addrRecordLen = pkt.rI8(ptr);
  768. if (addrRecordLen == 0) {
  769. 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);
  770. return false;
  771. }
  772. if (Buf::readOverflow(ptr,packetSize)) {
  773. 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);
  774. return false;
  775. }
  776. const void *addrBytes = nullptr;
  777. unsigned int addrLen = 0;
  778. unsigned int addrPort = 0;
  779. switch(addrType) {
  780. case 0:
  781. addrBytes = pkt.rBnc(ptr,addrRecordLen);
  782. addrLen = addrRecordLen;
  783. break;
  784. case 4:
  785. addrBytes = pkt.rBnc(ptr,4);
  786. addrLen = 4;
  787. addrPort = pkt.rI16(ptr);
  788. break;
  789. case 6:
  790. addrBytes = pkt.rBnc(ptr,16);
  791. addrLen = 16;
  792. addrPort = pkt.rI16(ptr);
  793. break;
  794. //case 200:
  795. // TODO: this would be a WebRTC SDP offer contained in the extended attrs field
  796. //break;
  797. default: break;
  798. }
  799. if (Buf::readOverflow(ptr,packetSize)) {
  800. 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);
  801. return false;
  802. }
  803. if (addrPort) {
  804. a.set(addrBytes,addrLen,addrPort);
  805. } else if (addrLen) {
  806. if (ep.unmarshal(reinterpret_cast<const uint8_t *>(addrBytes),(int)addrLen) <= 0) {
  807. 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);
  808. return false;
  809. }
  810. switch(ep.type()) {
  811. case Endpoint::TYPE_INETADDR_V4:
  812. case Endpoint::TYPE_INETADDR_V6:
  813. a = ep.inetAddr();
  814. break;
  815. default: // other types are not supported yet
  816. break;
  817. }
  818. }
  819. if (a) {
  820. 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);
  821. }
  822. ptr += (int)addrRecordLen;
  823. }
  824. // TODO: add to a peer try-queue
  825. return true;
  826. }
  827. bool VL1::_USER_MESSAGE(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  828. {
  829. // TODO
  830. return true;
  831. }
  832. bool VL1::_ENCAP(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize)
  833. {
  834. // TODO: not implemented yet
  835. return true;
  836. }
  837. } // namespace ZeroTier