IncomingPacket.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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 "Constants.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "IncomingPacket.hpp"
  16. #include "Topology.hpp"
  17. #include "Switch.hpp"
  18. #include "Peer.hpp"
  19. #include "NetworkController.hpp"
  20. #include "SelfAwareness.hpp"
  21. #include "Salsa20.hpp"
  22. #include "Node.hpp"
  23. #include "CertificateOfMembership.hpp"
  24. #include "Capability.hpp"
  25. #include "Tag.hpp"
  26. #include "Revocation.hpp"
  27. #include "Trace.hpp"
  28. #include "Buf.hpp"
  29. #include <cstring>
  30. #include <cstdlib>
  31. // Macro to avoid calling hton() on values known at compile time.
  32. #if __BYTE_ORDER == __LITTLE_ENDIAN
  33. #define CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
  34. #else
  35. #define CONST_TO_BE_UINT16(x) ((uint16_t)(x))
  36. #endif
  37. namespace ZeroTier {
  38. namespace {
  39. volatile uint16_t junk = 0;
  40. void _sendErrorNeedCredentials(IncomingPacket &p,const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer,const uint64_t nwid)
  41. {
  42. ZT_GET_NEW_BUF(outp,Protocol::ERROR::NEED_MEMBERSHIP_CERTIFICATE);
  43. outp->data.fields.h.packetId = Protocol::getPacketId();
  44. peer->address().copyTo(outp->data.fields.h.destination);
  45. RR->identity.address().copyTo(outp->data.fields.h.source);
  46. outp->data.fields.h.flags = 0;
  47. outp->data.fields.h.verb = Protocol::VERB_ERROR;
  48. outp->data.fields.eh.inRePacketId = p.idBE;
  49. outp->data.fields.eh.inReVerb = p.pkt->data.fields.verb;
  50. outp->data.fields.eh.error = Protocol::ERROR_NEED_MEMBERSHIP_CERTIFICATE;
  51. outp->data.fields.networkId = nwid;
  52. Protocol::armor(*outp,sizeof(Protocol::ERROR::NEED_MEMBERSHIP_CERTIFICATE),peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  53. p.path->send(RR,tPtr,outp->data.bytes,sizeof(Protocol::ERROR::NEED_MEMBERSHIP_CERTIFICATE),RR->node->now());
  54. }
  55. ZT_ALWAYS_INLINE bool _doHELLO(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const bool alreadyAuthenticated)
  56. {
  57. if (p.size < sizeof(Protocol::HELLO)) {
  58. RR->t->incomingPacketDropped(tPtr,p.idBE,0,Identity(),p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  59. return true;
  60. }
  61. Buf< Protocol::HELLO > &pkt = reinterpret_cast<Buf< Protocol::HELLO > &>(*p.pkt);
  62. Identity id;
  63. int ptr = sizeof(Protocol::HELLO);
  64. if (pkt.rO(ptr,id) < 0) {
  65. RR->t->incomingPacketDropped(tPtr,p.idBE,0,Identity(),p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  66. return true;
  67. }
  68. if (pkt.data.fields.versionProtocol < ZT_PROTO_VERSION_MIN) {
  69. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_PEER_TOO_OLD);
  70. return true;
  71. }
  72. if (Address(pkt.data.fields.h.source) != id.address()) {
  73. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  74. return true;
  75. }
  76. const int64_t now = RR->node->now();
  77. SharedPtr<Peer> peer(RR->topology->get(tPtr,id.address()));
  78. if (peer) {
  79. // We already have an identity with this address -- check for collisions
  80. if (!alreadyAuthenticated) {
  81. if (peer->identity() != id) {
  82. // Identity is different from the one we already have -- address collision
  83. // Check rate limits
  84. if (!RR->node->rateGateIdentityVerification(now,p.path->address()))
  85. return true;
  86. uint8_t key[ZT_PEER_SECRET_KEY_LENGTH];
  87. if (RR->identity.agree(id,key)) {
  88. if (Protocol::dearmor(pkt,p.size,key) < 0) { // ensure packet is authentic, otherwise drop
  89. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  90. return true;
  91. } else {
  92. // TODO: we handle identity collisions differently now
  93. }
  94. } else {
  95. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  96. return true;
  97. }
  98. return true;
  99. } else {
  100. // Identity is the same as the one we already have -- check packet integrity
  101. if (Protocol::dearmor(pkt,p.size,peer->key()) < 0) {
  102. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  103. return true;
  104. }
  105. // Continue at // VALID
  106. }
  107. } // else if alreadyAuthenticated then continue at // VALID
  108. } else {
  109. // We don't already have an identity with this address -- validate and learn it
  110. // Sanity check: this basically can't happen
  111. if (alreadyAuthenticated) {
  112. RR->t->incomingPacketDropped(tPtr,p.idBE,0,Identity(),p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_UNSPECIFIED);
  113. return true;
  114. }
  115. // Check rate limits
  116. if (!RR->node->rateGateIdentityVerification(now,p.path->address())) {
  117. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED);
  118. return true;
  119. }
  120. // Check packet integrity and MAC (this is faster than locallyValidate() so do it first to filter out total crap)
  121. SharedPtr<Peer> newPeer(new Peer(RR));
  122. if (!newPeer->init(RR->identity,id)) {
  123. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  124. return true;
  125. }
  126. if (Protocol::dearmor(pkt,p.size,newPeer->key())) {
  127. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  128. return true;
  129. }
  130. // Check that identity's address is valid as per the derivation function
  131. if (!id.locallyValidate()) {
  132. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  133. return true;
  134. }
  135. peer = RR->topology->add(tPtr,newPeer);
  136. // Continue at // VALID
  137. }
  138. // VALID -- if we made it here, packet passed identity and authenticity checks!
  139. // Get address to which this packet was sent to learn our external surface address if packet was direct.
  140. InetAddress externalSurfaceAddress;
  141. if (ptr < p.size) {
  142. if (pkt.rO(ptr,externalSurfaceAddress) < 0) {
  143. RR->t->incomingPacketDropped(tPtr,p.idBE,0,id,p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  144. return true;
  145. }
  146. if ((p.hops == 0)&&(externalSurfaceAddress))
  147. RR->sa->iam(tPtr,id,p.path->localSocket(),p.path->address(),externalSurfaceAddress,RR->topology->isRoot(id),now);
  148. }
  149. // Send OK(HELLO) with an echo of the packet's timestamp and some of the same
  150. // information about us: version, sent-to address, etc.
  151. ZT_GET_NEW_BUF(outp,Protocol::OK::HELLO);
  152. outp->data.fields.h.packetId = Protocol::getPacketId();
  153. peer->address().copyTo(outp->data.fields.h.destination);
  154. RR->identity.address().copyTo(outp->data.fields.h.source);
  155. outp->data.fields.h.flags = 0;
  156. outp->data.fields.h.verb = Protocol::VERB_OK;
  157. outp->data.fields.oh.inReVerb = Protocol::VERB_HELLO;
  158. outp->data.fields.oh.inRePacketId = p.idBE;
  159. outp->data.fields.timestampEcho = pkt.data.fields.timestamp;
  160. outp->data.fields.versionProtocol = ZT_PROTO_VERSION;
  161. outp->data.fields.versionMajor = ZEROTIER_ONE_VERSION_MAJOR;
  162. outp->data.fields.versionMinor = ZEROTIER_ONE_VERSION_MINOR;
  163. outp->data.fields.versionRev = CONST_TO_BE_UINT16(ZEROTIER_ONE_VERSION_REVISION);
  164. int outl = sizeof(Protocol::OK::HELLO);
  165. outp->wO(outl,p.path->address());
  166. if (!Buf<>::writeOverflow(outl)) {
  167. Protocol::armor(*outp,outl,peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  168. p.path->send(RR,tPtr,outp->data.bytes,outl,RR->node->now());
  169. }
  170. peer->setRemoteVersion(pkt.data.fields.versionProtocol,pkt.data.fields.versionMajor,pkt.data.fields.versionMinor,Utils::ntoh(pkt.data.fields.versionRev));
  171. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_HELLO,0,Protocol::VERB_NOP,0);
  172. return true;
  173. }
  174. ZT_ALWAYS_INLINE bool _doERROR(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  175. {
  176. if (p.size < sizeof(Protocol::ERROR::Header)) {
  177. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_ERROR,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  178. return true;
  179. }
  180. Buf< Protocol::ERROR::Header > &pkt = reinterpret_cast<Buf< Protocol::ERROR::Header > &>(*p.pkt);
  181. uint64_t networkId = 0;
  182. int ptr = sizeof(Protocol::ERROR::Header);
  183. /* Security note: we do not gate doERROR() with expectingReplyTo() to
  184. * avoid having to log every outgoing packet ID. Instead we put the
  185. * logic to determine whether we should consider an ERROR in each
  186. * error handler. In most cases these are only trusted in specific
  187. * circumstances. */
  188. switch(pkt.data.fields.error) {
  189. case Protocol::ERROR_OBJ_NOT_FOUND:
  190. // Object not found, currently only meaningful from network controllers.
  191. if (pkt.data.fields.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  192. networkId = pkt.rI64(ptr);
  193. const SharedPtr<Network> network(RR->node->network(networkId));
  194. if ((network)&&(network->controller() == peer->address()))
  195. network->setNotFound();
  196. }
  197. break;
  198. case Protocol::ERROR_UNSUPPORTED_OPERATION:
  199. // This can be sent in response to any operation, though right now we only
  200. // consider it meaningful from network controllers. This would indicate
  201. // that the queried node does not support acting as a controller.
  202. if (pkt.data.fields.inReVerb == Protocol::VERB_NETWORK_CONFIG_REQUEST) {
  203. networkId = pkt.rI64(ptr);
  204. const SharedPtr<Network> network(RR->node->network(networkId));
  205. if ((network)&&(network->controller() == peer->address()))
  206. network->setNotFound();
  207. }
  208. break;
  209. case Protocol::ERROR_NEED_MEMBERSHIP_CERTIFICATE: {
  210. // Peers can send this to ask for a cert for a network.
  211. networkId = pkt.rI64(ptr);
  212. const SharedPtr<Network> network(RR->node->network(networkId));
  213. const int64_t now = RR->node->now();
  214. if ((network)&&(network->config().com))
  215. network->pushCredentialsNow(tPtr,peer->address(),now);
  216. } break;
  217. case Protocol::ERROR_NETWORK_ACCESS_DENIED_: {
  218. // Network controller: network access denied.
  219. networkId = pkt.rI64(ptr);
  220. const SharedPtr<Network> network(RR->node->network(networkId));
  221. if ((network)&&(network->controller() == peer->address()))
  222. network->setAccessDenied();
  223. } break;
  224. default: break;
  225. }
  226. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_ERROR,pkt.data.fields.inRePacketId,(Protocol::Verb)pkt.data.fields.inReVerb,networkId);
  227. return true;
  228. }
  229. ZT_ALWAYS_INLINE bool _doOK(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  230. {
  231. if (p.size < sizeof(Protocol::OK::Header)) {
  232. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_OK,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  233. return true;
  234. }
  235. Buf< Protocol::OK::Header > &pkt = reinterpret_cast<Buf< Protocol::OK::Header > &>(*p.pkt);
  236. uint64_t networkId = 0;
  237. int ptr = sizeof(Protocol::OK::Header);
  238. if (!RR->node->expectingReplyTo(p.idBE))
  239. return true;
  240. switch(pkt.data.fields.inReVerb) {
  241. case Protocol::VERB_HELLO: {
  242. if (p.size < sizeof(Protocol::OK::HELLO)) {
  243. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  244. return true;
  245. }
  246. Buf< Protocol::OK::HELLO > &pkt2 = reinterpret_cast<Buf< Protocol::OK::HELLO > &>(pkt);
  247. if (pkt2.data.fields.versionProtocol < ZT_PROTO_VERSION_MIN)
  248. return true;
  249. peer->updateLatency((unsigned int)(p.receiveTime - Utils::ntoh(pkt2.data.fields.timestampEcho)));
  250. peer->setRemoteVersion(pkt2.data.fields.versionProtocol,pkt2.data.fields.versionMajor,pkt2.data.fields.versionMinor,Utils::ntoh(pkt2.data.fields.versionRev));
  251. ptr = sizeof(Protocol::OK::HELLO);
  252. if (ptr < p.size) {
  253. InetAddress externalSurfaceAddress;
  254. if (pkt2.rO(ptr,externalSurfaceAddress) < 0)
  255. return true;
  256. if ((externalSurfaceAddress)&&(p.hops == 0))
  257. RR->sa->iam(tPtr,peer->identity(),p.path->localSocket(),p.path->address(),externalSurfaceAddress,RR->topology->isRoot(peer->identity()),RR->node->now());
  258. }
  259. } break;
  260. case Protocol::VERB_WHOIS:
  261. if (RR->topology->isRoot(peer->identity())) {
  262. while (ptr < p.size) {
  263. Identity id;
  264. if (pkt.rO(ptr,id) < 0)
  265. break;
  266. Locator loc;
  267. if (ptr < p.size) { // older nodes did not send the locator
  268. if (pkt.rO(ptr,loc) < 0)
  269. break;
  270. }
  271. if (id) {
  272. SharedPtr<Peer> ptmp(RR->topology->add(tPtr,SharedPtr<Peer>(new Peer(RR))));
  273. ptmp->init(RR->identity,id);
  274. RR->sw->doAnythingWaitingForPeer(tPtr,ptmp);
  275. }
  276. }
  277. }
  278. break;
  279. case Protocol::VERB_NETWORK_CONFIG_REQUEST: {
  280. networkId = pkt.rI64(ptr);
  281. const SharedPtr<Network> network(RR->node->network(networkId));
  282. if (network)
  283. network->handleConfigChunk(tPtr,p.idBE,peer,pkt,sizeof(Protocol::OK::Header),(int)p.size);
  284. } break;
  285. case Protocol::VERB_MULTICAST_GATHER: {
  286. // TODO
  287. } break;
  288. default: break;
  289. }
  290. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_OK,pkt.data.fields.inRePacketId,(Protocol::Verb)pkt.data.fields.inReVerb,networkId);
  291. return true;
  292. }
  293. ZT_ALWAYS_INLINE bool _doWHOIS(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  294. {
  295. if (!peer->rateGateInboundWhoisRequest(RR->node->now())) {
  296. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_WHOIS,ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED);
  297. return true;
  298. }
  299. ZT_GET_NEW_BUF(outp,Protocol::OK::WHOIS);
  300. outp->data.fields.h.packetId = Protocol::getPacketId();
  301. peer->address().copyTo(outp->data.fields.h.destination);
  302. RR->identity.address().copyTo(outp->data.fields.h.source);
  303. outp->data.fields.h.flags = 0;
  304. outp->data.fields.h.verb = Protocol::VERB_OK;
  305. outp->data.fields.oh.inReVerb = Protocol::VERB_WHOIS;
  306. outp->data.fields.oh.inRePacketId = p.idBE;
  307. int ptr = sizeof(Protocol::Header);
  308. int outl = sizeof(Protocol::OK::WHOIS);
  309. while ((ptr + ZT_ADDRESS_LENGTH) <= p.size) {
  310. const SharedPtr<Peer> ptmp(RR->topology->get(tPtr,Address(p.pkt->data.bytes + ptr)));
  311. if (ptmp) {
  312. outp->wO(outl,ptmp->identity());
  313. Locator loc(ptmp->locator());
  314. outp->wO(outl,loc);
  315. }
  316. ptr += ZT_ADDRESS_LENGTH;
  317. }
  318. if ((outl > sizeof(Protocol::OK::WHOIS))&&(!Buf<>::writeOverflow(outl))) {
  319. Protocol::armor(*outp,outl,peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  320. p.path->send(RR,tPtr,outp->data.bytes,outl,RR->node->now());
  321. }
  322. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_WHOIS,0,Protocol::VERB_NOP,0);
  323. return true;
  324. }
  325. ZT_ALWAYS_INLINE bool _doRENDEZVOUS(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  326. {
  327. if (RR->topology->isRoot(peer->identity())) {
  328. if (p.size < sizeof(Protocol::RENDEZVOUS)) {
  329. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_RENDEZVOUS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  330. return true;
  331. }
  332. Buf< Protocol::RENDEZVOUS > &pkt = reinterpret_cast<Buf< Protocol::RENDEZVOUS > &>(*p.pkt);
  333. const SharedPtr<Peer> with(RR->topology->get(tPtr,Address(pkt.data.fields.peerAddress)));
  334. if (with) {
  335. const unsigned int port = Utils::ntoh(pkt.data.fields.port);
  336. if (port != 0) {
  337. switch(pkt.data.fields.addressLength) {
  338. case 4:
  339. if ((sizeof(Protocol::RENDEZVOUS) + 4) <= p.size) {
  340. InetAddress atAddr(pkt.data.fields.address,4,port);
  341. ++junk;
  342. RR->node->putPacket(tPtr,p.path->localSocket(),atAddr,(const void *)&junk,2,2); // IPv4 "firewall opener" hack
  343. with->sendHELLO(tPtr,p.path->localSocket(),atAddr,RR->node->now());
  344. RR->t->tryingNewPath(tPtr,with->identity(),atAddr,p.path->address(),p.idBE,Protocol::VERB_RENDEZVOUS,peer->address(),peer->identity().hash(),ZT_TRACE_TRYING_NEW_PATH_REASON_RENDEZVOUS);
  345. }
  346. break;
  347. case 16:
  348. if ((sizeof(Protocol::RENDEZVOUS) + 16) <= p.size) {
  349. InetAddress atAddr(pkt.data.fields.address,16,port);
  350. with->sendHELLO(tPtr,p.path->localSocket(),atAddr,RR->node->now());
  351. RR->t->tryingNewPath(tPtr,with->identity(),atAddr,p.path->address(),p.idBE,Protocol::VERB_RENDEZVOUS,peer->address(),peer->identity().hash(),ZT_TRACE_TRYING_NEW_PATH_REASON_RENDEZVOUS);
  352. }
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_RENDEZVOUS,0,Protocol::VERB_NOP,0);
  359. return true;
  360. }
  361. ZT_ALWAYS_INLINE bool _doFRAME(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  362. {
  363. if (p.size < sizeof(Protocol::FRAME)) {
  364. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_EXT_FRAME,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  365. return true;
  366. }
  367. Buf< Protocol::FRAME > &pkt = reinterpret_cast<Buf< Protocol::FRAME > &>(*p.pkt);
  368. const SharedPtr<Network> network(RR->node->network(Utils::ntoh(pkt.data.fields.networkId)));
  369. if (network) {
  370. if (network->gate(tPtr,peer)) {
  371. const unsigned int etherType = Utils::ntoh(pkt.data.fields.etherType);
  372. const MAC sourceMac(peer->address(),network->id());
  373. const unsigned int frameLen = (unsigned int)(p.size - sizeof(Protocol::FRAME));
  374. if (network->filterIncomingPacket(tPtr,peer,RR->identity.address(),sourceMac,network->mac(),pkt.data.fields.data,frameLen,etherType,0) > 0)
  375. RR->node->putFrame(tPtr,network->id(),network->userPtr(),sourceMac,network->mac(),etherType,0,pkt.data.fields.data,frameLen);
  376. } else {
  377. RR->t->incomingNetworkFrameDropped(tPtr,network->id(),MAC(),MAC(),peer->identity(),p.path->address(),p.hops,0,nullptr,Protocol::VERB_FRAME,true,ZT_TRACE_FRAME_DROP_REASON_PERMISSION_DENIED);
  378. _sendErrorNeedCredentials(p,RR,tPtr,peer,network->id());
  379. return false; // try to decode again after we get credentials?
  380. }
  381. }
  382. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_FRAME,0,Protocol::VERB_NOP,0);
  383. return true;
  384. }
  385. ZT_ALWAYS_INLINE bool _doEXT_FRAME(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  386. {
  387. if (p.size < sizeof(Protocol::EXT_FRAME)) {
  388. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_EXT_FRAME,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  389. return true;
  390. }
  391. Buf< Protocol::EXT_FRAME > &pkt = reinterpret_cast<Buf< Protocol::EXT_FRAME > &>(*p.pkt);
  392. const SharedPtr<Network> network(RR->node->network(Utils::ntoh(pkt.data.fields.networkId)));
  393. if (network) {
  394. int ptr = sizeof(Protocol::EXT_FRAME);
  395. const uint8_t flags = pkt.data.fields.flags;
  396. if ((flags & Protocol::EXT_FRAME_FLAG_COM_ATTACHED_deprecated) != 0) {
  397. CertificateOfMembership com;
  398. if (pkt.rO(ptr,com) < 0) {
  399. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_EXT_FRAME,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  400. return true;
  401. }
  402. if (com)
  403. network->addCredential(tPtr,peer->identity(),com);
  404. }
  405. if (!network->gate(tPtr,peer)) {
  406. RR->t->incomingNetworkFrameDropped(tPtr,network->id(),MAC(),MAC(),peer->identity(),p.path->address(),p.hops,0,nullptr,Protocol::VERB_EXT_FRAME,true,ZT_TRACE_FRAME_DROP_REASON_PERMISSION_DENIED);
  407. _sendErrorNeedCredentials(p,RR,tPtr,peer,network->id());
  408. return false; // try to parse again if we get credentials
  409. }
  410. const MAC to(pkt.rBnc(ptr,6));
  411. const MAC from(pkt.rBnc(ptr,6));
  412. const unsigned int etherType = pkt.rI16(ptr);
  413. if ((from)&&(from != network->mac())&&(!Buf<>::readOverflow(ptr,p.size))) {
  414. const int frameSize = (int)(p.size - ptr);
  415. if (frameSize >= 0) {
  416. const uint64_t nwid = network->id();
  417. const uint8_t *const frameData = pkt.data.bytes + ptr;
  418. switch (network->filterIncomingPacket(tPtr,peer,RR->identity.address(),from,to,frameData,frameSize,etherType,0)) {
  419. case 1:
  420. if (from != MAC(peer->address(),nwid)) {
  421. if (network->config().permitsBridging(peer->address())) {
  422. network->learnBridgeRoute(from,peer->address());
  423. } else {
  424. RR->t->incomingNetworkFrameDropped(tPtr,nwid,from,to,peer->identity(),p.path->address(),p.hops,(uint16_t)frameSize,frameData,Protocol::VERB_EXT_FRAME,true,ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_REMOTE);
  425. goto packet_dropped;
  426. }
  427. } else if (to != network->mac()) {
  428. if (to.isMulticast()) {
  429. if (network->config().multicastLimit == 0) {
  430. RR->t->incomingNetworkFrameDropped(tPtr,nwid,from,to,peer->identity(),p.path->address(),p.hops,(uint16_t)frameSize,frameData,Protocol::VERB_EXT_FRAME,true,ZT_TRACE_FRAME_DROP_REASON_MULTICAST_DISABLED);
  431. goto packet_dropped;
  432. }
  433. } else if (!network->config().permitsBridging(RR->identity.address())) {
  434. RR->t->incomingNetworkFrameDropped(tPtr,nwid,from,to,peer->identity(),p.path->address(),p.hops,(uint16_t)frameSize,frameData,Protocol::VERB_EXT_FRAME,true,ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_LOCAL);
  435. goto packet_dropped;
  436. }
  437. }
  438. // fall through -- 2 means accept regardless of bridging checks or other restrictions
  439. case 2:
  440. RR->node->putFrame(tPtr,nwid,network->userPtr(),from,to,etherType,0,frameData,frameSize);
  441. break;
  442. }
  443. }
  444. }
  445. if ((flags & Protocol::EXT_FRAME_FLAG_ACK_REQUESTED) != 0) {
  446. ZT_GET_NEW_BUF(outp,Protocol::OK::EXT_FRAME);
  447. outp->data.fields.h.packetId = Protocol::getPacketId();
  448. peer->address().copyTo(outp->data.fields.h.destination);
  449. RR->identity.address().copyTo(outp->data.fields.h.source);
  450. outp->data.fields.h.flags = 0;
  451. outp->data.fields.h.verb = Protocol::VERB_OK;
  452. outp->data.fields.oh.inReVerb = Protocol::VERB_EXT_FRAME;
  453. outp->data.fields.oh.inRePacketId = p.idBE;
  454. outp->data.fields.networkId = pkt.data.fields.networkId;
  455. outp->data.fields.flags = 0;
  456. to.copyTo(outp->data.fields.destMac);
  457. from.copyTo(outp->data.fields.sourceMac);
  458. outp->data.fields.etherType = Utils::hton((uint16_t)etherType);
  459. Protocol::armor(*outp,sizeof(Protocol::OK::EXT_FRAME),peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  460. p.path->send(RR,tPtr,outp->data.bytes,sizeof(Protocol::OK::EXT_FRAME),RR->node->now());
  461. }
  462. }
  463. packet_dropped:
  464. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_EXT_FRAME,0,Protocol::VERB_NOP,0);
  465. return true;
  466. }
  467. ZT_ALWAYS_INLINE bool _doECHO(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  468. {
  469. if (!peer->rateGateEchoRequest(RR->node->now())) {
  470. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_ECHO,ZT_TRACE_PACKET_DROP_REASON_RATE_LIMIT_EXCEEDED);
  471. return true;
  472. }
  473. ZT_GET_NEW_BUF(outp,Protocol::OK::ECHO);
  474. outp->data.fields.h.packetId = Protocol::getPacketId();
  475. peer->address().copyTo(outp->data.fields.h.destination);
  476. RR->identity.address().copyTo(outp->data.fields.h.source);
  477. outp->data.fields.h.flags = 0;
  478. outp->data.fields.h.verb = Protocol::VERB_OK;
  479. outp->data.fields.oh.inReVerb = Protocol::VERB_ECHO;
  480. outp->data.fields.oh.inRePacketId = p.idBE;
  481. int outl = sizeof(Protocol::OK::ECHO);
  482. if (p.size > sizeof(Protocol::Header)) {
  483. outp->wB(outl,p.pkt->data.bytes + sizeof(Protocol::Header),p.size - sizeof(Protocol::Header));
  484. }
  485. if (!Buf<>::writeOverflow(outl)) {
  486. Protocol::armor(*outp,outl,peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  487. p.path->send(RR,tPtr,outp->data.bytes,outl,RR->node->now());
  488. }
  489. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_ECHO,0,Protocol::VERB_NOP,0);
  490. return true;
  491. }
  492. ZT_ALWAYS_INLINE bool _doNETWORK_CREDENTIALS(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  493. {
  494. int ptr = sizeof(Protocol::Header);
  495. const uint8_t *payload = p.pkt->data.bytes;
  496. SharedPtr<Network> network;
  497. // Early versions of ZeroTier sent only the certificate of membership. The COM always
  498. // starts with a non-zero byte. To extend this message we then parse COMs until we find
  499. // a zero byte, then parse the other types (which are prefaced by a count for better
  500. // extensibility) if they are present.
  501. // Also note that technically these can be for different networks but in practice they
  502. // are always for the same network (when talking with current nodes). This code therefore
  503. // accepts different networks for each credential and ignores any credentials for
  504. // networks that we've not currently joined.
  505. while ((ptr < p.size)&&(payload[ptr] != 0)) {
  506. CertificateOfMembership com;
  507. int l = com.unmarshal(payload + ptr,(int)(p.size - ptr));
  508. if (l < 0) {
  509. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  510. return true;
  511. }
  512. ptr += l;
  513. const uint64_t nwid = com.networkId();
  514. if ((!network)||(network->id() != nwid))
  515. network = RR->node->network(nwid);
  516. if (network) {
  517. if (network->addCredential(tPtr,peer->identity(),com) == Membership::ADD_DEFERRED_FOR_WHOIS)
  518. return false;
  519. }
  520. }
  521. ++ptr; // skip trailing 0 after COMs if present
  522. // The following code is copypasta for each credential type: capability, tag, revocation,
  523. // and certificate of ownership. Each type is prefaced by a count, but it's legal for the
  524. // packet to terminate prematurely if all remaining counts are zero.
  525. if (ptr >= p.size)
  526. return true;
  527. unsigned int count = p.pkt->rI16(ptr);
  528. for(unsigned int i=0;i<count;++i) {
  529. if (ptr >= p.size) {
  530. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  531. return true;
  532. }
  533. Capability cap;
  534. int l = cap.unmarshal(payload + ptr,(int)(p.size - ptr));
  535. if (l < 0) {
  536. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  537. return true;
  538. }
  539. ptr += l;
  540. const uint64_t nwid = cap.networkId();
  541. if ((!network)||(network->id() != nwid))
  542. network = RR->node->network(nwid);
  543. if (network) {
  544. if (network->addCredential(tPtr,peer->identity(),cap) == Membership::ADD_DEFERRED_FOR_WHOIS)
  545. return false;
  546. }
  547. }
  548. if (ptr >= p.size)
  549. return true;
  550. count = p.pkt->rI16(ptr);
  551. for(unsigned int i=0;i<count;++i) {
  552. if (ptr >= p.size) {
  553. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  554. return true;
  555. }
  556. Tag tag;
  557. int l = tag.unmarshal(payload + ptr,(int)(p.size - ptr));
  558. if (l < 0) {
  559. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  560. return true;
  561. }
  562. ptr += l;
  563. const uint64_t nwid = tag.networkId();
  564. if ((!network)||(network->id() != nwid))
  565. network = RR->node->network(nwid);
  566. if (network) {
  567. if (network->addCredential(tPtr,peer->identity(),tag) == Membership::ADD_DEFERRED_FOR_WHOIS)
  568. return false;
  569. }
  570. }
  571. if (ptr >= p.size)
  572. return true;
  573. count = p.pkt->rI16(ptr);
  574. for(unsigned int i=0;i<count;++i) {
  575. if (ptr >= p.size) {
  576. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  577. return true;
  578. }
  579. Revocation rev;
  580. int l = rev.unmarshal(payload + ptr,(int)(p.size - ptr));
  581. if (l < 0) {
  582. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  583. return true;
  584. }
  585. ptr += l;
  586. const uint64_t nwid = rev.networkId();
  587. if ((!network)||(network->id() != nwid))
  588. network = RR->node->network(nwid);
  589. if (network) {
  590. if (network->addCredential(tPtr,peer->identity(),rev) == Membership::ADD_DEFERRED_FOR_WHOIS)
  591. return false;
  592. }
  593. }
  594. if (ptr >= p.size)
  595. return true;
  596. count = p.pkt->rI16(ptr);
  597. for(unsigned int i=0;i<count;++i) {
  598. if (ptr >= p.size) {
  599. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  600. return true;
  601. }
  602. CertificateOfOwnership coo;
  603. int l = coo.unmarshal(payload + ptr,(int)(p.size - ptr));
  604. if (l < 0) {
  605. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CREDENTIALS,ZT_TRACE_PACKET_DROP_REASON_INVALID_OBJECT);
  606. return true;
  607. }
  608. ptr += l;
  609. const uint64_t nwid = coo.networkId();
  610. if ((!network)||(network->id() != nwid))
  611. network = RR->node->network(nwid);
  612. if (network) {
  613. if (network->addCredential(tPtr,peer->identity(),coo) == Membership::ADD_DEFERRED_FOR_WHOIS)
  614. return false;
  615. }
  616. }
  617. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_NETWORK_CREDENTIALS,0,Protocol::VERB_NOP,(network) ? network->id() : 0);
  618. return true;
  619. }
  620. ZT_ALWAYS_INLINE bool _doNETWORK_CONFIG_REQUEST(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  621. {
  622. int ptr = sizeof(Protocol::Header);
  623. const uint64_t nwid = p.pkt->rI64(ptr);
  624. const unsigned int dictSize = p.pkt->rI16(ptr);
  625. const uint8_t *dictData = p.pkt->rBnc(ptr,dictSize);
  626. if (Buf<>::readOverflow(ptr,p.size)) {
  627. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CONFIG_REQUEST,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  628. return true;
  629. }
  630. if (RR->localNetworkController) {
  631. Dictionary requestMetaData;
  632. if ((dictSize > 0)&&(dictData)) {
  633. if (!requestMetaData.decode(dictData,dictSize)) {
  634. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CONFIG_REQUEST,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  635. return true;
  636. }
  637. }
  638. RR->localNetworkController->request(nwid,(p.hops > 0) ? InetAddress::NIL : p.path->address(),Utils::ntoh(p.idBE),peer->identity(),requestMetaData);
  639. } else {
  640. ZT_GET_NEW_BUF(outp,Protocol::ERROR::UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST);
  641. outp->data.fields.h.packetId = Protocol::getPacketId();
  642. peer->address().copyTo(outp->data.fields.h.destination);
  643. RR->identity.address().copyTo(outp->data.fields.h.source);
  644. outp->data.fields.h.flags = 0;
  645. outp->data.fields.h.verb = Protocol::VERB_OK;
  646. outp->data.fields.eh.inReVerb = Protocol::VERB_NETWORK_CONFIG_REQUEST;
  647. outp->data.fields.eh.inRePacketId = p.idBE;
  648. outp->data.fields.eh.error = Protocol::ERROR_UNSUPPORTED_OPERATION;
  649. outp->data.fields.networkId = Utils::hton(nwid);
  650. Protocol::armor(*outp,sizeof(Protocol::ERROR::UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST),peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  651. p.path->send(RR,tPtr,outp->data.bytes,sizeof(Protocol::ERROR::UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST),RR->node->now());
  652. }
  653. // Note that NETWORK_CONFIG_REQUEST does not pertain to a network we have *joined*, but one
  654. // we may control. The network ID parameter to peer->received() is therefore zero.
  655. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_NETWORK_CONFIG_REQUEST,0,Protocol::VERB_NOP,0);
  656. return true;
  657. }
  658. ZT_ALWAYS_INLINE bool _doNETWORK_CONFIG(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  659. {
  660. int ptr = sizeof(Protocol::Header);
  661. const uint64_t nwid = p.pkt->rI64(ptr);
  662. if (ptr >= (int)p.size) {
  663. RR->t->incomingPacketDropped(tPtr,p.idBE,0,peer->identity(),p.path->address(),p.hops,Protocol::VERB_NETWORK_CONFIG,ZT_TRACE_PACKET_DROP_REASON_MALFORMED_PACKET);
  664. return true;
  665. }
  666. const SharedPtr<Network> network(RR->node->network(nwid));
  667. if (network) {
  668. const uint64_t configUpdateId = network->handleConfigChunk(tPtr,p.idBE,peer,*p.pkt,ptr,(int)p.size - ptr);
  669. if (configUpdateId != 0) {
  670. ZT_GET_NEW_BUF(outp,Protocol::OK::NETWORK_CONFIG);
  671. outp->data.fields.h.packetId = Protocol::getPacketId();
  672. peer->address().copyTo(outp->data.fields.h.destination);
  673. RR->identity.address().copyTo(outp->data.fields.h.source);
  674. outp->data.fields.h.flags = 0;
  675. outp->data.fields.h.verb = Protocol::VERB_OK;
  676. outp->data.fields.oh.inReVerb = Protocol::VERB_NETWORK_CONFIG;
  677. outp->data.fields.oh.inRePacketId = p.idBE;
  678. outp->data.fields.networkId = Utils::hton(nwid);
  679. outp->data.fields.configUpdateId = Utils::hton(configUpdateId);
  680. Protocol::armor(*outp,sizeof(Protocol::OK::NETWORK_CONFIG),peer->key(),ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012);
  681. p.path->send(RR,tPtr,outp->data.bytes,sizeof(Protocol::OK::NETWORK_CONFIG),RR->node->now());
  682. }
  683. }
  684. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_NETWORK_CONFIG,0,Protocol::VERB_NOP,nwid);
  685. return true;
  686. }
  687. ZT_ALWAYS_INLINE bool _doMULTICAST_GATHER(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  688. {
  689. return true;
  690. }
  691. ZT_ALWAYS_INLINE bool _doPUSH_DIRECT_PATHS(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  692. {
  693. /*
  694. const int64_t now = RR->node->now();
  695. if (peer->rateGateInboundPushDirectPaths(now)) {
  696. uint8_t countPerScope[ZT_INETADDRESS_MAX_SCOPE+1][2]; // [][0] is v4, [][1] is v6
  697. memset(countPerScope,0,sizeof(countPerScope));
  698. unsigned int count = pkt.at<uint16_t>(ZT_PACKET_IDX_PAYLOAD);
  699. unsigned int ptr = ZT_PACKET_IDX_PAYLOAD + 2;
  700. while (count--) {
  701. // unsigned int flags = (*this)[ptr++];
  702. ++ptr;
  703. unsigned int extLen = pkt.at<uint16_t>(ptr); ptr += 2;
  704. ptr += extLen; // unused right now
  705. unsigned int addrType = pkt[ptr++];
  706. unsigned int addrLen = pkt[ptr++];
  707. switch(addrType) {
  708. case 4: {
  709. const InetAddress a(pkt.field(ptr,4),4,pkt.at<uint16_t>(ptr + 4));
  710. if (peer->shouldTryPath(tPtr,now,peer,a)) {
  711. if (++countPerScope[(int)a.ipScope()][0] <= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY) {
  712. RR->node->putPacket(tPtr,path->localSocket(),a,(const void *)&junk,sizeof(junk),2); // IPv4 "firewall opener"
  713. ++junk;
  714. peer->sendHELLO(tPtr,-1,a,now);
  715. RR->t->tryingNewPath(tPtr,peer->identity(),a,path->address(),pkt.packetId(),Packet::VERB_PUSH_DIRECT_PATHS,peer->address(),peer->identity().hash(),ZT_TRACE_TRYING_NEW_PATH_REASON_RECEIVED_PUSH_DIRECT_PATHS);
  716. }
  717. }
  718. } break;
  719. case 6: {
  720. const InetAddress a(pkt.field(ptr,16),16,pkt.at<uint16_t>(ptr + 16));
  721. if (peer->shouldTryPath(tPtr,now,peer,a)) {
  722. if (++countPerScope[(int)a.ipScope()][1] <= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY) {
  723. peer->sendHELLO(tPtr,-1,a,now);
  724. RR->t->tryingNewPath(tPtr,peer->identity(),a,path->address(),pkt.packetId(),Packet::VERB_PUSH_DIRECT_PATHS,peer->address(),peer->identity().hash(),ZT_TRACE_TRYING_NEW_PATH_REASON_RECEIVED_PUSH_DIRECT_PATHS);
  725. }
  726. }
  727. } break;
  728. }
  729. ptr += addrLen;
  730. }
  731. }
  732. peer->received(tPtr,path,pkt.hops(),pkt.packetId(),pkt.payloadLength(),Packet::VERB_PUSH_DIRECT_PATHS,0,Packet::VERB_NOP,0);
  733. */
  734. return true;
  735. }
  736. ZT_ALWAYS_INLINE bool _doUSER_MESSAGE(IncomingPacket &p,const RuntimeEnvironment *const RR,void *const tPtr,const SharedPtr<Peer> &peer)
  737. {
  738. ZT_UserMessage um;
  739. int ptr = sizeof(Protocol::Header);
  740. um.id = reinterpret_cast<const ZT_Identity *>(&(peer->identity()));
  741. um.typeId = p.pkt->rI64(ptr);
  742. int ds = (int)p.size - ptr;
  743. if (ds > 0) {
  744. um.data = p.pkt->data.bytes + ptr;
  745. um.length = (unsigned int)ds;
  746. RR->node->postEvent(tPtr,ZT_EVENT_USER_MESSAGE,reinterpret_cast<const void *>(&um));
  747. }
  748. peer->received(tPtr,p.path,p.hops,p.idBE,p.size,Protocol::VERB_USER_MESSAGE,0,Protocol::VERB_NOP,0);
  749. return true;
  750. }
  751. //////////////////////////////////////////////////////////////////////////////
  752. } // anonymous namespace
  753. bool IncomingPacket::tryDecode(const RuntimeEnvironment *RR,void *tPtr)
  754. {
  755. const Address source(pkt->data.fields.source);
  756. const SharedPtr<Peer> peer(RR->topology->get(tPtr,source));
  757. try {
  758. // Check for trusted paths or unencrypted HELLOs (HELLO is the only packet sent in the clear)
  759. const uint8_t c = Protocol::packetCipher(pkt->data.fields);
  760. bool trusted = false;
  761. if (c == ZT_PROTO_CIPHER_SUITE__NONE) {
  762. // If this is marked as a packet via a trusted path, check source address and path ID.
  763. // Obviously if no trusted paths are configured this always returns false and such
  764. // packets are dropped on the floor.
  765. const uint64_t tpid = Utils::ntoh(pkt->data.fields.mac); // the MAC is the trusted path ID on these packets
  766. if (RR->topology->shouldInboundPathBeTrusted(path->address(),tpid)) {
  767. trusted = true;
  768. } else {
  769. if (peer)
  770. RR->t->incomingPacketDropped(tPtr,idBE,0,peer->identity(),path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  771. return true;
  772. }
  773. } else if ((c == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE)&&(pkt->data.fields.verb == Protocol::VERB_HELLO)) {
  774. // Only HELLO is allowed in the clear, but the MAC is still checked in _doHELLO().
  775. return _doHELLO(*this,RR,tPtr,false);
  776. }
  777. if (!peer) {
  778. RR->sw->requestWhois(tPtr,RR->node->now(),source);
  779. return false;
  780. }
  781. if (!trusted) {
  782. if (!dearmor(peer->key())) {
  783. RR->t->incomingPacketDropped(tPtr,idBE,0,peer->identity(),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_MAC_FAILED);
  784. return true;
  785. }
  786. }
  787. if (!uncompress()) {
  788. RR->t->incomingPacketDropped(tPtr,idBE,0,peer->identity(),path->address(),hops,Protocol::VERB_NOP,ZT_TRACE_PACKET_DROP_REASON_INVALID_COMPRESSED_DATA);
  789. return true;
  790. }
  791. const Protocol::Verb verb = (Protocol::Verb)pkt->data.fields.verb;
  792. bool r = true;
  793. switch(verb) {
  794. default: // ignore unknown verbs, but if they pass auth check they are "received" and considered NOPs by peer->receive()
  795. RR->t->incomingPacketDropped(tPtr,idBE,0,peer->identity(),path->address(),hops,verb,ZT_TRACE_PACKET_DROP_REASON_UNRECOGNIZED_VERB);
  796. // fall through
  797. case Protocol::VERB_NOP:
  798. peer->received(tPtr,path,hops,idBE,size,Protocol::VERB_NOP,0,Protocol::VERB_NOP,0);
  799. break;
  800. case Protocol::VERB_HELLO: r = _doHELLO(*this,RR,tPtr,true); break;
  801. case Protocol::VERB_ERROR: r = _doERROR(*this,RR,tPtr,peer); break;
  802. case Protocol::VERB_OK: r = _doOK(*this,RR,tPtr,peer); break;
  803. case Protocol::VERB_WHOIS: r = _doWHOIS(*this,RR,tPtr,peer); break;
  804. case Protocol::VERB_RENDEZVOUS: r = _doRENDEZVOUS(*this,RR,tPtr,peer); break;
  805. case Protocol::VERB_FRAME: r = _doFRAME(*this,RR,tPtr,peer); break;
  806. case Protocol::VERB_EXT_FRAME: r = _doEXT_FRAME(*this,RR,tPtr,peer); break;
  807. case Protocol::VERB_ECHO: r = _doECHO(*this,RR,tPtr,peer); break;
  808. case Protocol::VERB_NETWORK_CREDENTIALS: r = _doNETWORK_CREDENTIALS(*this,RR,tPtr,peer); break;
  809. case Protocol::VERB_NETWORK_CONFIG_REQUEST: r = _doNETWORK_CONFIG_REQUEST(*this,RR,tPtr,peer); break;
  810. case Protocol::VERB_NETWORK_CONFIG: r = _doNETWORK_CONFIG(*this,RR,tPtr,peer); break;
  811. case Protocol::VERB_MULTICAST_GATHER: r = _doMULTICAST_GATHER(*this,RR,tPtr,peer); break;
  812. case Protocol::VERB_PUSH_DIRECT_PATHS: r = _doPUSH_DIRECT_PATHS(*this,RR,tPtr,peer); break;
  813. case Protocol::VERB_USER_MESSAGE: r = _doUSER_MESSAGE(*this,RR,tPtr,peer); break;
  814. }
  815. return r;
  816. } catch (int ztExcCode) {
  817. } catch ( ... ) {}
  818. if (peer)
  819. RR->t->incomingPacketDropped(tPtr,idBE,0,peer->identity(),path->address(),hops,Protocol::VERB_HELLO,ZT_TRACE_PACKET_DROP_REASON_UNSPECIFIED);
  820. return true;
  821. }
  822. } // namespace ZeroTier