Switch.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 <cstdio>
  14. #include <algorithm>
  15. #include <utility>
  16. #include "Constants.hpp"
  17. #include "RuntimeEnvironment.hpp"
  18. #include "Switch.hpp"
  19. #include "Node.hpp"
  20. #include "InetAddress.hpp"
  21. #include "Peer.hpp"
  22. #include "SelfAwareness.hpp"
  23. #include "Trace.hpp"
  24. namespace ZeroTier {
  25. Switch::Switch(const RuntimeEnvironment *renv) :
  26. RR(renv),
  27. _lastCheckedQueues(0)
  28. {
  29. }
  30. void Switch::onRemotePacket(void *tPtr,const int64_t localSocket,const InetAddress &fromAddr,const void *data,unsigned int len)
  31. {
  32. try {
  33. const int64_t now = RR->node->now();
  34. const SharedPtr<Path> path(RR->topology->getPath(localSocket,fromAddr));
  35. path->received(now);
  36. if (len > ZT_PROTO_MIN_FRAGMENT_LENGTH) { // SECURITY: min length check is important since we do some C-style stuff below!
  37. if (reinterpret_cast<const uint8_t *>(data)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] == ZT_PACKET_FRAGMENT_INDICATOR) {
  38. // Handle fragment ----------------------------------------------------
  39. Packet::Fragment fragment(data,len);
  40. const Address destination(fragment.destination());
  41. if (destination != RR->identity.address()) {
  42. if (fragment.hops() < ZT_RELAY_MAX_HOPS) {
  43. fragment.incrementHops();
  44. SharedPtr<Peer> relayTo = RR->topology->get(tPtr,destination);
  45. if ((!relayTo)||(!relayTo->sendDirect(tPtr,fragment.data(),fragment.size(),now))) {
  46. relayTo = RR->topology->root();
  47. if (relayTo)
  48. relayTo->sendDirect(tPtr,fragment.data(),fragment.size(),now);
  49. }
  50. }
  51. } else {
  52. // Fragment looks like ours
  53. const uint64_t fragmentPacketId = fragment.packetId();
  54. const unsigned int fragmentNumber = fragment.fragmentNumber();
  55. const unsigned int totalFragments = fragment.totalFragments();
  56. if ((totalFragments <= ZT_MAX_PACKET_FRAGMENTS)&&(fragmentNumber < ZT_MAX_PACKET_FRAGMENTS)&&(fragmentNumber > 0)&&(totalFragments > 1)) {
  57. // Fragment appears basically sane. Its fragment number must be
  58. // 1 or more, since a Packet with fragmented bit set is fragment 0.
  59. // Total fragments must be more than 1, otherwise why are we
  60. // seeing a Packet::Fragment?
  61. RXQueueEntry *const rq = _findRXQueueEntry(fragmentPacketId);
  62. Mutex::Lock rql(rq->lock);
  63. if (rq->packetId != fragmentPacketId) {
  64. // No packet found, so we received a fragment without its head.
  65. rq->timestamp = now;
  66. rq->packetId = fragmentPacketId;
  67. rq->frags[fragmentNumber - 1] = fragment;
  68. rq->totalFragments = totalFragments; // total fragment count is known
  69. rq->haveFragments = 1 << fragmentNumber; // we have only this fragment
  70. rq->complete = false;
  71. } else if (!(rq->haveFragments & (1 << fragmentNumber))) {
  72. // We have other fragments and maybe the head, so add this one and check
  73. rq->frags[fragmentNumber - 1] = fragment;
  74. rq->totalFragments = totalFragments;
  75. if (Utils::countBits(rq->haveFragments |= (1 << fragmentNumber)) == totalFragments) {
  76. // We have all fragments -- assemble and process full Packet
  77. for(unsigned int f=1;f<totalFragments;++f)
  78. rq->frag0.append(rq->frags[f - 1].payload(),rq->frags[f - 1].payloadLength());
  79. if (rq->frag0.tryDecode(RR,tPtr)) {
  80. rq->timestamp = 0; // packet decoded, free entry
  81. } else {
  82. rq->complete = true; // set complete flag but leave entry since it probably needs WHOIS or something
  83. }
  84. }
  85. } // else this is a duplicate fragment, ignore
  86. }
  87. }
  88. // --------------------------------------------------------------------
  89. } else if (len >= ZT_PROTO_MIN_PACKET_LENGTH) { // min length check is important!
  90. // Handle packet head -------------------------------------------------
  91. const Address destination(reinterpret_cast<const uint8_t *>(data) + 8,ZT_ADDRESS_LENGTH);
  92. const Address source(reinterpret_cast<const uint8_t *>(data) + 13,ZT_ADDRESS_LENGTH);
  93. if (source == RR->identity.address())
  94. return;
  95. if (destination != RR->identity.address()) {
  96. // This packet is not for this node, so possibly relay it ----------
  97. Packet packet(data,len);
  98. if (packet.hops() < ZT_RELAY_MAX_HOPS) {
  99. packet.incrementHops();
  100. SharedPtr<Peer> relayTo = RR->topology->get(tPtr,destination);
  101. if ((!relayTo)||(!relayTo->sendDirect(tPtr,packet.data(),packet.size(),now))) {
  102. relayTo = RR->topology->root();
  103. if ((relayTo)&&(relayTo->address() != source))
  104. relayTo->sendDirect(tPtr,packet.data(),packet.size(),now);
  105. }
  106. }
  107. } else if ((reinterpret_cast<const uint8_t *>(data)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0) {
  108. // Packet is the head of a fragmented packet series ----------------
  109. const uint64_t packetId = (
  110. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[0]) << 56U) |
  111. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[1]) << 48U) |
  112. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[2]) << 40U) |
  113. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[3]) << 32U) |
  114. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[4]) << 24U) |
  115. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[5]) << 16U) |
  116. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[6]) << 8U) |
  117. ((uint64_t)reinterpret_cast<const uint8_t *>(data)[7])
  118. );
  119. RXQueueEntry *const rq = _findRXQueueEntry(packetId);
  120. Mutex::Lock rql(rq->lock);
  121. if (rq->packetId != packetId) {
  122. // If we have no other fragments yet, create an entry and save the head
  123. rq->timestamp = now;
  124. rq->packetId = packetId;
  125. rq->frag0.init(data,len,path,now);
  126. rq->totalFragments = 0;
  127. rq->haveFragments = 1;
  128. rq->complete = false;
  129. } else if (!(rq->haveFragments & 1)) {
  130. // Check if packet is complete -----------------------------------
  131. if ((rq->totalFragments > 1)&&(Utils::countBits(rq->haveFragments |= 1) == rq->totalFragments)) {
  132. // We have all fragments -- assemble and process full Packet ---
  133. rq->frag0.init(data,len,path,now);
  134. for(unsigned int f=1;f<rq->totalFragments;++f)
  135. rq->frag0.append(rq->frags[f - 1].payload(),rq->frags[f - 1].payloadLength());
  136. if (rq->frag0.tryDecode(RR,tPtr)) {
  137. rq->timestamp = 0; // packet decoded, free entry
  138. } else {
  139. rq->complete = true; // set complete flag but leave entry since it probably needs WHOIS or something
  140. }
  141. } else {
  142. // Still waiting on more fragments, but keep the head ----------
  143. rq->frag0.init(data,len,path,now);
  144. }
  145. } // else this is a duplicate head, ignore
  146. } else {
  147. // Packet is unfragmented, so just process it ----------------------
  148. IncomingPacket packet(data,len,path,now);
  149. if (!packet.tryDecode(RR,tPtr)) {
  150. RXQueueEntry *const rq = _nextRXQueueEntry();
  151. Mutex::Lock rql(rq->lock);
  152. rq->timestamp = now;
  153. rq->packetId = packet.packetId();
  154. rq->frag0 = packet;
  155. rq->totalFragments = 1;
  156. rq->haveFragments = 1;
  157. rq->complete = true;
  158. }
  159. }
  160. // --------------------------------------------------------------------
  161. }
  162. }
  163. } catch ( ... ) {} // sanity check, should be caught elsewhere
  164. }
  165. void Switch::onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  166. {
  167. if (!network->hasConfig())
  168. return;
  169. // Check if this packet is from someone other than the tap -- i.e. bridged in
  170. bool fromBridged;
  171. if ((fromBridged = (from != network->mac()))) {
  172. if (!network->config().permitsBridging(RR->identity.address())) {
  173. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_BRIDGING_NOT_ALLOWED_LOCAL);
  174. return;
  175. }
  176. }
  177. uint8_t qosBucket = 0;
  178. if (to.isMulticast()) {
  179. MulticastGroup multicastGroup(to,0);
  180. if (to.isBroadcast()) {
  181. if ( (etherType == ZT_ETHERTYPE_ARP) && (len >= 28) && ((((const uint8_t *)data)[2] == 0x08)&&(((const uint8_t *)data)[3] == 0x00)&&(((const uint8_t *)data)[4] == 6)&&(((const uint8_t *)data)[5] == 4)&&(((const uint8_t *)data)[7] == 0x01)) ) {
  182. /* IPv4 ARP is one of the few special cases that we impose upon what is
  183. * otherwise a straightforward Ethernet switch emulation. Vanilla ARP
  184. * is dumb old broadcast and simply doesn't scale. ZeroTier multicast
  185. * groups have an additional field called ADI (additional distinguishing
  186. * information) which was added specifically for ARP though it could
  187. * be used for other things too. We then take ARP broadcasts and turn
  188. * them into multicasts by stuffing the IP address being queried into
  189. * the 32-bit ADI field. In practice this uses our multicast pub/sub
  190. * system to implement a kind of extended/distributed ARP table. */
  191. multicastGroup = MulticastGroup::deriveMulticastGroupForAddressResolution(InetAddress(((const unsigned char *)data) + 24,4,0));
  192. } else if (!network->config().enableBroadcast()) {
  193. // Don't transmit broadcasts if this network doesn't want them
  194. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_BROADCAST_DISABLED);
  195. return;
  196. }
  197. } else if ((etherType == ZT_ETHERTYPE_IPV6)&&(len >= (40 + 8 + 16))) {
  198. // IPv6 NDP emulation for certain very special patterns of private IPv6 addresses -- if enabled
  199. if ((network->config().ndpEmulation())&&(reinterpret_cast<const uint8_t *>(data)[6] == 0x3a)&&(reinterpret_cast<const uint8_t *>(data)[40] == 0x87)) { // ICMPv6 neighbor solicitation
  200. Address v6EmbeddedAddress;
  201. const uint8_t *const pkt6 = reinterpret_cast<const uint8_t *>(data) + 40 + 8;
  202. const uint8_t *my6 = (const uint8_t *)0;
  203. // ZT-RFC4193 address: fdNN:NNNN:NNNN:NNNN:NN99:93DD:DDDD:DDDD / 88 (one /128 per actual host)
  204. // ZT-6PLANE address: fcXX:XXXX:XXDD:DDDD:DDDD:####:####:#### / 40 (one /80 per actual host)
  205. // (XX - lower 32 bits of network ID XORed with higher 32 bits)
  206. // For these to work, we must have a ZT-managed address assigned in one of the
  207. // above formats, and the query must match its prefix.
  208. for(unsigned int sipk=0;sipk<network->config().staticIpCount;++sipk) {
  209. const InetAddress *const sip = &(network->config().staticIps[sipk]);
  210. if (sip->ss_family == AF_INET6) {
  211. my6 = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_addr.s6_addr);
  212. const unsigned int sipNetmaskBits = Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_port);
  213. if ((sipNetmaskBits == 88)&&(my6[0] == 0xfd)&&(my6[9] == 0x99)&&(my6[10] == 0x93)) { // ZT-RFC4193 /88 ???
  214. unsigned int ptr = 0;
  215. while (ptr != 11) {
  216. if (pkt6[ptr] != my6[ptr])
  217. break;
  218. ++ptr;
  219. }
  220. if (ptr == 11) { // prefix match!
  221. v6EmbeddedAddress.setTo(pkt6 + ptr,5);
  222. break;
  223. }
  224. } else if (sipNetmaskBits == 40) { // ZT-6PLANE /40 ???
  225. const uint32_t nwid32 = (uint32_t)((network->id() ^ (network->id() >> 32U)) & 0xffffffffU);
  226. if ( (my6[0] == 0xfc) && (my6[1] == (uint8_t)((nwid32 >> 24U) & 0xffU)) && (my6[2] == (uint8_t)((nwid32 >> 16U) & 0xffU)) && (my6[3] == (uint8_t)((nwid32 >> 8U) & 0xffU)) && (my6[4] == (uint8_t)(nwid32 & 0xffU))) {
  227. unsigned int ptr = 0;
  228. while (ptr != 5) {
  229. if (pkt6[ptr] != my6[ptr])
  230. break;
  231. ++ptr;
  232. }
  233. if (ptr == 5) { // prefix match!
  234. v6EmbeddedAddress.setTo(pkt6 + ptr,5);
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if ((v6EmbeddedAddress)&&(v6EmbeddedAddress != RR->identity.address())) {
  242. const MAC peerMac(v6EmbeddedAddress,network->id());
  243. uint8_t adv[72];
  244. adv[0] = 0x60; adv[1] = 0x00; adv[2] = 0x00; adv[3] = 0x00;
  245. adv[4] = 0x00; adv[5] = 0x20;
  246. adv[6] = 0x3a; adv[7] = 0xff;
  247. for(int i=0;i<16;++i) adv[8 + i] = pkt6[i];
  248. for(int i=0;i<16;++i) adv[24 + i] = my6[i];
  249. adv[40] = 0x88; adv[41] = 0x00;
  250. adv[42] = 0x00; adv[43] = 0x00; // future home of checksum
  251. adv[44] = 0x60; adv[45] = 0x00; adv[46] = 0x00; adv[47] = 0x00;
  252. for(int i=0;i<16;++i) adv[48 + i] = pkt6[i];
  253. adv[64] = 0x02; adv[65] = 0x01;
  254. adv[66] = peerMac[0]; adv[67] = peerMac[1]; adv[68] = peerMac[2]; adv[69] = peerMac[3]; adv[70] = peerMac[4]; adv[71] = peerMac[5];
  255. uint16_t pseudo_[36];
  256. uint8_t *const pseudo = reinterpret_cast<uint8_t *>(pseudo_);
  257. for(int i=0;i<32;++i) pseudo[i] = adv[8 + i];
  258. pseudo[32] = 0x00; pseudo[33] = 0x00; pseudo[34] = 0x00; pseudo[35] = 0x20;
  259. pseudo[36] = 0x00; pseudo[37] = 0x00; pseudo[38] = 0x00; pseudo[39] = 0x3a;
  260. for(int i=0;i<32;++i) pseudo[40 + i] = adv[40 + i];
  261. uint32_t checksum = 0;
  262. for(int i=0;i<36;++i) checksum += Utils::hton(pseudo_[i]);
  263. while ((checksum >> 16U)) checksum = (checksum & 0xffffU) + (checksum >> 16U);
  264. checksum = ~checksum;
  265. adv[42] = (checksum >> 8U) & 0xffU;
  266. adv[43] = checksum & 0xffU;
  267. RR->node->putFrame(tPtr,network->id(),network->userPtr(),peerMac,from,ZT_ETHERTYPE_IPV6,0,adv,72);
  268. return; // NDP emulation done. We have forged a "fake" reply, so no need to send actual NDP query.
  269. } // else no NDP emulation
  270. } // else no NDP emulation
  271. }
  272. // Check this after NDP emulation, since that has to be allowed in exactly this case
  273. if (network->config().multicastLimit == 0) {
  274. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_MULTICAST_DISABLED);
  275. return;
  276. }
  277. /* Learn multicast groups for bridged-in hosts.
  278. * Note that some OSes, most notably Linux, do this for you by learning
  279. * multicast addresses on bridge interfaces and subscribing each slave.
  280. * But in that case this does no harm, as the sets are just merged. */
  281. if (fromBridged)
  282. network->learnBridgedMulticastGroup(tPtr,multicastGroup,RR->node->now());
  283. // First pass sets noTee to false, but noTee is set to true in OutboundMulticast to prevent duplicates.
  284. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),Address(),from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  285. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED);
  286. return;
  287. }
  288. // TODO
  289. /*
  290. RR->mc->send(
  291. tPtr,
  292. RR->node->now(),
  293. network,
  294. Address(),
  295. multicastGroup,
  296. (fromBridged) ? from : MAC(),
  297. etherType,
  298. data,
  299. len);
  300. */
  301. } else if (to == network->mac()) {
  302. // Destination is this node, so just reinject it -------------------------
  303. RR->node->putFrame(tPtr,network->id(),network->userPtr(),from,to,etherType,vlanId,data,len);
  304. } else if (to[0] == MAC::firstOctetForNetwork(network->id())) {
  305. // Destination is another ZeroTier peer on the same network --------------
  306. Address toZT(to.toAddress(network->id())); // since in-network MACs are derived from addresses and network IDs, we can reverse this
  307. SharedPtr<Peer> toPeer(RR->topology->get(tPtr,toZT));
  308. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),toZT,from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  309. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED);
  310. return;
  311. }
  312. network->pushCredentialsIfNeeded(tPtr,toZT,RR->node->now());
  313. if (fromBridged) {
  314. Packet outp(toZT,RR->identity.address(),Packet::VERB_EXT_FRAME);
  315. outp.append(network->id());
  316. outp.append((unsigned char)0x00);
  317. to.appendTo(outp);
  318. from.appendTo(outp);
  319. outp.append((uint16_t)etherType);
  320. outp.append(data,len);
  321. } else {
  322. Packet outp(toZT,RR->identity.address(),Packet::VERB_FRAME);
  323. outp.append(network->id());
  324. outp.append((uint16_t)etherType);
  325. outp.append(data,len);
  326. }
  327. } else {
  328. // Destination is bridged behind a remote peer ---------------------------
  329. // We filter with a NULL destination ZeroTier address first. Filtrations
  330. // for each ZT destination are also done below. This is the same rationale
  331. // and design as for multicast.
  332. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),Address(),from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  333. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED);
  334. return;
  335. }
  336. Address bridges[ZT_MAX_BRIDGE_SPAM];
  337. unsigned int numBridges = 0;
  338. /* Create an array of up to ZT_MAX_BRIDGE_SPAM recipients for this bridged frame. */
  339. bridges[0] = network->findBridgeTo(to);
  340. std::vector<Address> activeBridges;
  341. for(unsigned int i=0;i<network->config().specialistCount;++i) {
  342. if ((network->config().specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
  343. activeBridges.push_back(Address(network->config().specialists[i]));
  344. }
  345. if ((bridges[0])&&(bridges[0] != RR->identity.address())&&(network->config().permitsBridging(bridges[0]))) {
  346. /* We have a known bridge route for this MAC, send it there. */
  347. ++numBridges;
  348. } else if (!activeBridges.empty()) {
  349. /* If there is no known route, spam to up to ZT_MAX_BRIDGE_SPAM active
  350. * bridges. If someone responds, we'll learn the route. */
  351. std::vector<Address>::const_iterator ab(activeBridges.begin());
  352. if (activeBridges.size() <= ZT_MAX_BRIDGE_SPAM) {
  353. // If there are <= ZT_MAX_BRIDGE_SPAM active bridges, spam them all
  354. while (ab != activeBridges.end()) {
  355. bridges[numBridges++] = *ab;
  356. ++ab;
  357. }
  358. } else {
  359. // Otherwise pick a random set of them
  360. while (numBridges < ZT_MAX_BRIDGE_SPAM) {
  361. if (ab == activeBridges.end())
  362. ab = activeBridges.begin();
  363. if (((unsigned long)Utils::random() % (unsigned long)activeBridges.size()) == 0) {
  364. bridges[numBridges++] = *ab;
  365. ++ab;
  366. } else ++ab;
  367. }
  368. }
  369. }
  370. for(unsigned int b=0;b<numBridges;++b) {
  371. if (network->filterOutgoingPacket(tPtr,true,RR->identity.address(),bridges[b],from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  372. Packet outp(bridges[b],RR->identity.address(),Packet::VERB_EXT_FRAME);
  373. outp.append(network->id());
  374. outp.append((uint8_t)0x00);
  375. to.appendTo(outp);
  376. from.appendTo(outp);
  377. outp.append((uint16_t)etherType);
  378. outp.append(data,len);
  379. } else {
  380. RR->t->outgoingNetworkFrameDropped(tPtr,network->id(),from,to,(uint16_t)etherType,(uint16_t)len,(const uint8_t *)data,ZT_TRACE_FRAME_DROP_REASON_FILTER_BLOCKED_AT_BRIDGE_REPLICATION);
  381. }
  382. }
  383. }
  384. }
  385. void Switch::send(void *tPtr,Packet &packet,bool encrypt)
  386. {
  387. const Address dest(packet.destination());
  388. if (dest == RR->identity.address())
  389. return;
  390. if (!_trySend(tPtr,packet,encrypt)) {
  391. {
  392. Mutex::Lock _l(_txQueue_m);
  393. if (_txQueue.size() >= ZT_TX_QUEUE_SIZE) {
  394. _txQueue.pop_front();
  395. }
  396. _txQueue.push_back(TXQueueEntry(dest,RR->node->now(),packet,encrypt));
  397. }
  398. if (!RR->topology->get(tPtr,dest))
  399. requestWhois(tPtr,RR->node->now(),dest);
  400. }
  401. }
  402. void Switch::requestWhois(void *tPtr,const int64_t now,const Address &addr)
  403. {
  404. if (addr == RR->identity.address())
  405. return;
  406. {
  407. Mutex::Lock _l(_lastSentWhoisRequest_m);
  408. int64_t &last = _lastSentWhoisRequest[addr];
  409. if ((now - last) < ZT_WHOIS_RETRY_DELAY)
  410. return;
  411. else last = now;
  412. }
  413. const SharedPtr<Peer> root(RR->topology->root());
  414. if (root) {
  415. Packet outp(root->address(),RR->identity.address(),Packet::VERB_WHOIS);
  416. addr.appendTo(outp);
  417. RR->node->expectReplyTo(outp.packetId());
  418. root->sendDirect(tPtr,outp.data(),outp.size(),now);
  419. }
  420. }
  421. void Switch::doAnythingWaitingForPeer(void *tPtr,const SharedPtr<Peer> &peer)
  422. {
  423. {
  424. Mutex::Lock _l(_lastSentWhoisRequest_m);
  425. _lastSentWhoisRequest.erase(peer->address());
  426. }
  427. const int64_t now = RR->node->now();
  428. for(unsigned int ptr=0;ptr<ZT_RX_QUEUE_SIZE;++ptr) {
  429. RXQueueEntry *const rq = &(_rxQueue[ptr]);
  430. Mutex::Lock rql(rq->lock);
  431. if ((rq->timestamp)&&(rq->complete)) {
  432. if ((rq->frag0.tryDecode(RR,tPtr))||((now - rq->timestamp) > ZT_RECEIVE_QUEUE_TIMEOUT))
  433. rq->timestamp = 0;
  434. }
  435. }
  436. {
  437. Mutex::Lock _l(_txQueue_m);
  438. for(std::list< TXQueueEntry >::iterator txi(_txQueue.begin());txi!=_txQueue.end();) {
  439. if (txi->dest == peer->address()) {
  440. if (_trySend(tPtr,txi->packet,txi->encrypt)) {
  441. _txQueue.erase(txi++);
  442. } else {
  443. ++txi;
  444. }
  445. } else {
  446. ++txi;
  447. }
  448. }
  449. }
  450. }
  451. unsigned long Switch::doTimerTasks(void *tPtr,int64_t now)
  452. {
  453. const uint64_t timeSinceLastCheck = now - _lastCheckedQueues;
  454. if (timeSinceLastCheck < ZT_WHOIS_RETRY_DELAY)
  455. return (unsigned long)(ZT_WHOIS_RETRY_DELAY - timeSinceLastCheck);
  456. _lastCheckedQueues = now;
  457. std::vector<Address> needWhois;
  458. {
  459. Mutex::Lock _l(_txQueue_m);
  460. for(std::list< TXQueueEntry >::iterator txi(_txQueue.begin());txi!=_txQueue.end();) {
  461. if (_trySend(tPtr,txi->packet,txi->encrypt)) {
  462. _txQueue.erase(txi++);
  463. } else if ((now - txi->creationTime) > ZT_TRANSMIT_QUEUE_TIMEOUT) {
  464. _txQueue.erase(txi++);
  465. } else {
  466. if (!RR->topology->get(tPtr,txi->dest))
  467. needWhois.push_back(txi->dest);
  468. ++txi;
  469. }
  470. }
  471. }
  472. for(std::vector<Address>::const_iterator i(needWhois.begin());i!=needWhois.end();++i)
  473. requestWhois(tPtr,now,*i);
  474. for(unsigned int ptr=0;ptr<ZT_RX_QUEUE_SIZE;++ptr) {
  475. RXQueueEntry *const rq = &(_rxQueue[ptr]);
  476. Mutex::Lock rql(rq->lock);
  477. if ((rq->timestamp)&&(rq->complete)) {
  478. if ((rq->frag0.tryDecode(RR,tPtr))||((now - rq->timestamp) > ZT_RECEIVE_QUEUE_TIMEOUT)) {
  479. rq->timestamp = 0;
  480. } else {
  481. const Address src(rq->frag0.source());
  482. if (!RR->topology->get(tPtr,src))
  483. requestWhois(tPtr,now,src);
  484. }
  485. }
  486. }
  487. {
  488. Mutex::Lock _l(_lastSentWhoisRequest_m);
  489. Hashtable< Address,int64_t >::Iterator i(_lastSentWhoisRequest);
  490. Address *a = (Address *)0;
  491. int64_t *ts = (int64_t *)0;
  492. while (i.next(a,ts)) {
  493. if ((now - *ts) > (ZT_WHOIS_RETRY_DELAY * 2))
  494. _lastSentWhoisRequest.erase(*a);
  495. }
  496. }
  497. return ZT_WHOIS_RETRY_DELAY;
  498. }
  499. bool Switch::_trySend(void *tPtr,Packet &packet,bool encrypt)
  500. {
  501. const int64_t now = RR->node->now();
  502. const SharedPtr<Peer> peer(RR->topology->get(tPtr,packet.destination()));
  503. SharedPtr<Path> viaPath;
  504. if (peer) {
  505. viaPath = peer->path(now);
  506. if (!viaPath) {
  507. const SharedPtr<Peer> relay(RR->topology->root());
  508. if (relay) {
  509. viaPath = relay->path(now);
  510. if (!viaPath)
  511. return false;
  512. } else {
  513. return false;
  514. }
  515. }
  516. } else {
  517. return false;
  518. }
  519. unsigned int mtu = ZT_DEFAULT_PHYSMTU;
  520. uint64_t trustedPathId = 0;
  521. RR->topology->getOutboundPathInfo(viaPath->address(),mtu,trustedPathId);
  522. unsigned int chunkSize = std::min(packet.size(),mtu);
  523. packet.setFragmented(chunkSize < packet.size());
  524. if (trustedPathId) {
  525. packet.setTrusted(trustedPathId);
  526. } else {
  527. packet.armor(peer->key(),encrypt);
  528. }
  529. if (viaPath->send(RR,tPtr,packet.data(),chunkSize,now)) {
  530. if (chunkSize < packet.size()) {
  531. // Too big for one packet, fragment the rest
  532. unsigned int fragStart = chunkSize;
  533. unsigned int remaining = packet.size() - chunkSize;
  534. unsigned int fragsRemaining = (remaining / (mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH));
  535. if ((fragsRemaining * (mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH)) < remaining)
  536. ++fragsRemaining;
  537. const unsigned int totalFragments = fragsRemaining + 1;
  538. for(unsigned int fno=1;fno<totalFragments;++fno) {
  539. chunkSize = std::min(remaining,(unsigned int)(mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH));
  540. Packet::Fragment frag(packet,fragStart,chunkSize,fno,totalFragments);
  541. viaPath->send(RR,tPtr,frag.data(),frag.size(),now);
  542. fragStart += chunkSize;
  543. remaining -= chunkSize;
  544. }
  545. }
  546. }
  547. return true;
  548. }
  549. } // namespace ZeroTier