VL1.cpp 38 KB

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