Switch.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <algorithm>
  29. #include <utility>
  30. #include <stdexcept>
  31. #include "../include/ZeroTierOne.h"
  32. #include "Constants.hpp"
  33. #include "RuntimeEnvironment.hpp"
  34. #include "Switch.hpp"
  35. #include "Node.hpp"
  36. #include "InetAddress.hpp"
  37. #include "Topology.hpp"
  38. #include "Peer.hpp"
  39. #include "SelfAwareness.hpp"
  40. #include "Packet.hpp"
  41. #include "Trace.hpp"
  42. namespace ZeroTier {
  43. Switch::Switch(const RuntimeEnvironment *renv) :
  44. RR(renv),
  45. _lastBeaconResponse(0),
  46. _lastCheckedQueues(0),
  47. _lastUniteAttempt(8) // only really used on root servers and upstreams, and it'll grow there just fine
  48. {
  49. }
  50. void Switch::onRemotePacket(void *tPtr,const int64_t localSocket,const InetAddress &fromAddr,const void *data,unsigned int len)
  51. {
  52. try {
  53. const int64_t now = RR->node->now();
  54. const SharedPtr<Path> path(RR->topology->getPath(localSocket,fromAddr));
  55. path->received(now);
  56. if (len == 13) {
  57. /* LEGACY: before VERB_PUSH_DIRECT_PATHS, peers used broadcast
  58. * announcements on the LAN to solve the 'same network problem.' We
  59. * no longer send these, but we'll listen for them for a while to
  60. * locate peers with versions <1.0.4. */
  61. const Address beaconAddr(reinterpret_cast<const char *>(data) + 8,5);
  62. if (beaconAddr == RR->identity.address())
  63. return;
  64. if (!RR->node->shouldUsePathForZeroTierTraffic(tPtr,beaconAddr,localSocket,fromAddr))
  65. return;
  66. const SharedPtr<Peer> peer(RR->topology->get(beaconAddr));
  67. if (peer) { // we'll only respond to beacons from known peers
  68. if ((now - _lastBeaconResponse) >= 2500) { // limit rate of responses
  69. _lastBeaconResponse = now;
  70. Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NOP);
  71. outp.armor(peer->key(),true);
  72. path->send(RR,tPtr,outp.data(),outp.size(),now);
  73. }
  74. }
  75. } else if (len > ZT_PROTO_MIN_FRAGMENT_LENGTH) { // SECURITY: min length check is important since we do some C-style stuff below!
  76. if (reinterpret_cast<const uint8_t *>(data)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] == ZT_PACKET_FRAGMENT_INDICATOR) {
  77. // Handle fragment ----------------------------------------------------
  78. Packet::Fragment fragment(data,len);
  79. const Address destination(fragment.destination());
  80. if (destination != RR->identity.address()) {
  81. if (fragment.hops() < ZT_RELAY_MAX_HOPS) {
  82. fragment.incrementHops();
  83. // Note: we don't bother initiating NAT-t for fragments, since heads will set that off.
  84. // It wouldn't hurt anything, just redundant and unnecessary.
  85. SharedPtr<Peer> relayTo = RR->topology->get(destination);
  86. if ((!relayTo)||(!relayTo->sendDirect(tPtr,fragment.data(),fragment.size(),now,false))) {
  87. // Don't know peer or no direct path -- so relay via someone upstream
  88. relayTo = RR->topology->findRelayTo(now,destination);
  89. if (relayTo)
  90. relayTo->sendDirect(tPtr,fragment.data(),fragment.size(),now,true);
  91. }
  92. }
  93. } else {
  94. // Fragment looks like ours
  95. const uint64_t fragmentPacketId = fragment.packetId();
  96. const unsigned int fragmentNumber = fragment.fragmentNumber();
  97. const unsigned int totalFragments = fragment.totalFragments();
  98. if ((totalFragments <= ZT_MAX_PACKET_FRAGMENTS)&&(fragmentNumber < ZT_MAX_PACKET_FRAGMENTS)&&(fragmentNumber > 0)&&(totalFragments > 1)) {
  99. // Fragment appears basically sane. Its fragment number must be
  100. // 1 or more, since a Packet with fragmented bit set is fragment 0.
  101. // Total fragments must be more than 1, otherwise why are we
  102. // seeing a Packet::Fragment?
  103. RXQueueEntry *const rq = _findRXQueueEntry(fragmentPacketId);
  104. Mutex::Lock rql(rq->lock);
  105. if (rq->packetId != fragmentPacketId) {
  106. // No packet found, so we received a fragment without its head.
  107. rq->timestamp = now;
  108. rq->packetId = fragmentPacketId;
  109. rq->frags[fragmentNumber - 1] = fragment;
  110. rq->totalFragments = totalFragments; // total fragment count is known
  111. rq->haveFragments = 1 << fragmentNumber; // we have only this fragment
  112. rq->complete = false;
  113. } else if (!(rq->haveFragments & (1 << fragmentNumber))) {
  114. // We have other fragments and maybe the head, so add this one and check
  115. rq->frags[fragmentNumber - 1] = fragment;
  116. rq->totalFragments = totalFragments;
  117. if (Utils::countBits(rq->haveFragments |= (1 << fragmentNumber)) == totalFragments) {
  118. // We have all fragments -- assemble and process full Packet
  119. for(unsigned int f=1;f<totalFragments;++f)
  120. rq->frag0.append(rq->frags[f - 1].payload(),rq->frags[f - 1].payloadLength());
  121. if (rq->frag0.tryDecode(RR,tPtr)) {
  122. rq->timestamp = 0; // packet decoded, free entry
  123. } else {
  124. rq->complete = true; // set complete flag but leave entry since it probably needs WHOIS or something
  125. }
  126. }
  127. } // else this is a duplicate fragment, ignore
  128. }
  129. }
  130. // --------------------------------------------------------------------
  131. } else if (len >= ZT_PROTO_MIN_PACKET_LENGTH) { // min length check is important!
  132. // Handle packet head -------------------------------------------------
  133. const Address destination(reinterpret_cast<const uint8_t *>(data) + 8,ZT_ADDRESS_LENGTH);
  134. const Address source(reinterpret_cast<const uint8_t *>(data) + 13,ZT_ADDRESS_LENGTH);
  135. if (source == RR->identity.address())
  136. return;
  137. if (destination != RR->identity.address()) {
  138. Packet packet(data,len);
  139. if (packet.hops() < ZT_RELAY_MAX_HOPS) {
  140. packet.incrementHops();
  141. SharedPtr<Peer> relayTo = RR->topology->get(destination);
  142. if ((relayTo)&&(relayTo->sendDirect(tPtr,packet.data(),packet.size(),now,false))) {
  143. if ((source != RR->identity.address())&&(_shouldUnite(now,source,destination))) {
  144. const SharedPtr<Peer> sourcePeer(RR->topology->get(source));
  145. if (sourcePeer)
  146. relayTo->introduce(tPtr,now,sourcePeer);
  147. }
  148. } else {
  149. relayTo = RR->topology->findRelayTo(now,destination);
  150. if ((relayTo)&&(relayTo->address() != source)) {
  151. if (relayTo->sendDirect(tPtr,packet.data(),packet.size(),now,true)) {
  152. const SharedPtr<Peer> sourcePeer(RR->topology->get(source));
  153. if (sourcePeer)
  154. relayTo->introduce(tPtr,now,sourcePeer);
  155. }
  156. }
  157. }
  158. }
  159. } else if ((reinterpret_cast<const uint8_t *>(data)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0) {
  160. // Packet is the head of a fragmented packet series
  161. const uint64_t packetId = (
  162. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[0]) << 56) |
  163. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[1]) << 48) |
  164. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[2]) << 40) |
  165. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[3]) << 32) |
  166. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[4]) << 24) |
  167. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[5]) << 16) |
  168. (((uint64_t)reinterpret_cast<const uint8_t *>(data)[6]) << 8) |
  169. ((uint64_t)reinterpret_cast<const uint8_t *>(data)[7])
  170. );
  171. RXQueueEntry *const rq = _findRXQueueEntry(packetId);
  172. Mutex::Lock rql(rq->lock);
  173. if (rq->packetId != packetId) {
  174. // If we have no other fragments yet, create an entry and save the head
  175. rq->timestamp = now;
  176. rq->packetId = packetId;
  177. rq->frag0.init(data,len,path,now);
  178. rq->totalFragments = 0;
  179. rq->haveFragments = 1;
  180. rq->complete = false;
  181. } else if (!(rq->haveFragments & 1)) {
  182. // If we have other fragments but no head, see if we are complete with the head
  183. if ((rq->totalFragments > 1)&&(Utils::countBits(rq->haveFragments |= 1) == rq->totalFragments)) {
  184. // We have all fragments -- assemble and process full Packet
  185. rq->frag0.init(data,len,path,now);
  186. for(unsigned int f=1;f<rq->totalFragments;++f)
  187. rq->frag0.append(rq->frags[f - 1].payload(),rq->frags[f - 1].payloadLength());
  188. if (rq->frag0.tryDecode(RR,tPtr)) {
  189. rq->timestamp = 0; // packet decoded, free entry
  190. } else {
  191. rq->complete = true; // set complete flag but leave entry since it probably needs WHOIS or something
  192. }
  193. } else {
  194. // Still waiting on more fragments, but keep the head
  195. rq->frag0.init(data,len,path,now);
  196. }
  197. } // else this is a duplicate head, ignore
  198. } else {
  199. // Packet is unfragmented, so just process it
  200. IncomingPacket packet(data,len,path,now);
  201. if (!packet.tryDecode(RR,tPtr)) {
  202. RXQueueEntry *const rq = _nextRXQueueEntry();
  203. Mutex::Lock rql(rq->lock);
  204. rq->timestamp = now;
  205. rq->packetId = packet.packetId();
  206. rq->frag0 = packet;
  207. rq->totalFragments = 1;
  208. rq->haveFragments = 1;
  209. rq->complete = true;
  210. }
  211. }
  212. // --------------------------------------------------------------------
  213. }
  214. }
  215. } catch ( ... ) {} // sanity check, should be caught elsewhere
  216. }
  217. 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)
  218. {
  219. if (!network->hasConfig())
  220. return;
  221. // Check if this packet is from someone other than the tap -- i.e. bridged in
  222. bool fromBridged;
  223. if ((fromBridged = (from != network->mac()))) {
  224. if (!network->config().permitsBridging(RR->identity.address())) {
  225. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"not a bridge");
  226. return;
  227. }
  228. }
  229. uint8_t qosBucket = ZT_QOS_DEFAULT_BUCKET;
  230. if (to.isMulticast()) {
  231. MulticastGroup multicastGroup(to,0);
  232. if (to.isBroadcast()) {
  233. 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)) ) {
  234. /* IPv4 ARP is one of the few special cases that we impose upon what is
  235. * otherwise a straightforward Ethernet switch emulation. Vanilla ARP
  236. * is dumb old broadcast and simply doesn't scale. ZeroTier multicast
  237. * groups have an additional field called ADI (additional distinguishing
  238. * information) which was added specifically for ARP though it could
  239. * be used for other things too. We then take ARP broadcasts and turn
  240. * them into multicasts by stuffing the IP address being queried into
  241. * the 32-bit ADI field. In practice this uses our multicast pub/sub
  242. * system to implement a kind of extended/distributed ARP table. */
  243. multicastGroup = MulticastGroup::deriveMulticastGroupForAddressResolution(InetAddress(((const unsigned char *)data) + 24,4,0));
  244. } else if (!network->config().enableBroadcast()) {
  245. // Don't transmit broadcasts if this network doesn't want them
  246. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"broadcast disabled");
  247. return;
  248. }
  249. } else if ((etherType == ZT_ETHERTYPE_IPV6)&&(len >= (40 + 8 + 16))) {
  250. // IPv6 NDP emulation for certain very special patterns of private IPv6 addresses -- if enabled
  251. if ((network->config().ndpEmulation())&&(reinterpret_cast<const uint8_t *>(data)[6] == 0x3a)&&(reinterpret_cast<const uint8_t *>(data)[40] == 0x87)) { // ICMPv6 neighbor solicitation
  252. Address v6EmbeddedAddress;
  253. const uint8_t *const pkt6 = reinterpret_cast<const uint8_t *>(data) + 40 + 8;
  254. const uint8_t *my6 = (const uint8_t *)0;
  255. // ZT-RFC4193 address: fdNN:NNNN:NNNN:NNNN:NN99:93DD:DDDD:DDDD / 88 (one /128 per actual host)
  256. // ZT-6PLANE address: fcXX:XXXX:XXDD:DDDD:DDDD:####:####:#### / 40 (one /80 per actual host)
  257. // (XX - lower 32 bits of network ID XORed with higher 32 bits)
  258. // For these to work, we must have a ZT-managed address assigned in one of the
  259. // above formats, and the query must match its prefix.
  260. for(unsigned int sipk=0;sipk<network->config().staticIpCount;++sipk) {
  261. const InetAddress *const sip = &(network->config().staticIps[sipk]);
  262. if (sip->ss_family == AF_INET6) {
  263. my6 = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_addr.s6_addr);
  264. const unsigned int sipNetmaskBits = Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_port);
  265. if ((sipNetmaskBits == 88)&&(my6[0] == 0xfd)&&(my6[9] == 0x99)&&(my6[10] == 0x93)) { // ZT-RFC4193 /88 ???
  266. unsigned int ptr = 0;
  267. while (ptr != 11) {
  268. if (pkt6[ptr] != my6[ptr])
  269. break;
  270. ++ptr;
  271. }
  272. if (ptr == 11) { // prefix match!
  273. v6EmbeddedAddress.setTo(pkt6 + ptr,5);
  274. break;
  275. }
  276. } else if (sipNetmaskBits == 40) { // ZT-6PLANE /40 ???
  277. const uint32_t nwid32 = (uint32_t)((network->id() ^ (network->id() >> 32)) & 0xffffffff);
  278. if ( (my6[0] == 0xfc) && (my6[1] == (uint8_t)((nwid32 >> 24) & 0xff)) && (my6[2] == (uint8_t)((nwid32 >> 16) & 0xff)) && (my6[3] == (uint8_t)((nwid32 >> 8) & 0xff)) && (my6[4] == (uint8_t)(nwid32 & 0xff))) {
  279. unsigned int ptr = 0;
  280. while (ptr != 5) {
  281. if (pkt6[ptr] != my6[ptr])
  282. break;
  283. ++ptr;
  284. }
  285. if (ptr == 5) { // prefix match!
  286. v6EmbeddedAddress.setTo(pkt6 + ptr,5);
  287. break;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. if ((v6EmbeddedAddress)&&(v6EmbeddedAddress != RR->identity.address())) {
  294. const MAC peerMac(v6EmbeddedAddress,network->id());
  295. uint8_t adv[72];
  296. adv[0] = 0x60; adv[1] = 0x00; adv[2] = 0x00; adv[3] = 0x00;
  297. adv[4] = 0x00; adv[5] = 0x20;
  298. adv[6] = 0x3a; adv[7] = 0xff;
  299. for(int i=0;i<16;++i) adv[8 + i] = pkt6[i];
  300. for(int i=0;i<16;++i) adv[24 + i] = my6[i];
  301. adv[40] = 0x88; adv[41] = 0x00;
  302. adv[42] = 0x00; adv[43] = 0x00; // future home of checksum
  303. adv[44] = 0x60; adv[45] = 0x00; adv[46] = 0x00; adv[47] = 0x00;
  304. for(int i=0;i<16;++i) adv[48 + i] = pkt6[i];
  305. adv[64] = 0x02; adv[65] = 0x01;
  306. adv[66] = peerMac[0]; adv[67] = peerMac[1]; adv[68] = peerMac[2]; adv[69] = peerMac[3]; adv[70] = peerMac[4]; adv[71] = peerMac[5];
  307. uint16_t pseudo_[36];
  308. uint8_t *const pseudo = reinterpret_cast<uint8_t *>(pseudo_);
  309. for(int i=0;i<32;++i) pseudo[i] = adv[8 + i];
  310. pseudo[32] = 0x00; pseudo[33] = 0x00; pseudo[34] = 0x00; pseudo[35] = 0x20;
  311. pseudo[36] = 0x00; pseudo[37] = 0x00; pseudo[38] = 0x00; pseudo[39] = 0x3a;
  312. for(int i=0;i<32;++i) pseudo[40 + i] = adv[40 + i];
  313. uint32_t checksum = 0;
  314. for(int i=0;i<36;++i) checksum += Utils::hton(pseudo_[i]);
  315. while ((checksum >> 16)) checksum = (checksum & 0xffff) + (checksum >> 16);
  316. checksum = ~checksum;
  317. adv[42] = (checksum >> 8) & 0xff;
  318. adv[43] = checksum & 0xff;
  319. RR->node->putFrame(tPtr,network->id(),network->userPtr(),peerMac,from,ZT_ETHERTYPE_IPV6,0,adv,72);
  320. return; // NDP emulation done. We have forged a "fake" reply, so no need to send actual NDP query.
  321. } // else no NDP emulation
  322. } // else no NDP emulation
  323. }
  324. // Check this after NDP emulation, since that has to be allowed in exactly this case
  325. if (network->config().multicastLimit == 0) {
  326. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"multicast disabled");
  327. return;
  328. }
  329. /* Learn multicast groups for bridged-in hosts.
  330. * Note that some OSes, most notably Linux, do this for you by learning
  331. * multicast addresses on bridge interfaces and subscribing each slave.
  332. * But in that case this does no harm, as the sets are just merged. */
  333. if (fromBridged)
  334. network->learnBridgedMulticastGroup(tPtr,multicastGroup,RR->node->now());
  335. // First pass sets noTee to false, but noTee is set to true in OutboundMulticast to prevent duplicates.
  336. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),Address(),from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  337. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"filter blocked");
  338. return;
  339. }
  340. RR->mc->send(
  341. tPtr,
  342. RR->node->now(),
  343. network,
  344. Address(),
  345. multicastGroup,
  346. (fromBridged) ? from : MAC(),
  347. etherType,
  348. data,
  349. len);
  350. } else if (to == network->mac()) {
  351. // Destination is this node, so just reinject it
  352. RR->node->putFrame(tPtr,network->id(),network->userPtr(),from,to,etherType,vlanId,data,len);
  353. } else if (to[0] == MAC::firstOctetForNetwork(network->id())) {
  354. // Destination is another ZeroTier peer on the same network
  355. Address toZT(to.toAddress(network->id())); // since in-network MACs are derived from addresses and network IDs, we can reverse this
  356. SharedPtr<Peer> toPeer(RR->topology->get(toZT));
  357. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),toZT,from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  358. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"filter blocked");
  359. return;
  360. }
  361. network->pushCredentialsIfNeeded(tPtr,toZT,RR->node->now());
  362. if (fromBridged) {
  363. Packet outp(toZT,RR->identity.address(),Packet::VERB_EXT_FRAME);
  364. outp.append(network->id());
  365. outp.append((unsigned char)0x00);
  366. to.appendTo(outp);
  367. from.appendTo(outp);
  368. outp.append((uint16_t)etherType);
  369. outp.append(data,len);
  370. if (!network->config().disableCompression())
  371. outp.compress();
  372. aqm_enqueue(tPtr,network,outp,true,qosBucket);
  373. } else {
  374. Packet outp(toZT,RR->identity.address(),Packet::VERB_FRAME);
  375. outp.append(network->id());
  376. outp.append((uint16_t)etherType);
  377. outp.append(data,len);
  378. if (!network->config().disableCompression())
  379. outp.compress();
  380. aqm_enqueue(tPtr,network,outp,true,qosBucket);
  381. }
  382. } else {
  383. // Destination is bridged behind a remote peer
  384. // We filter with a NULL destination ZeroTier address first. Filtrations
  385. // for each ZT destination are also done below. This is the same rationale
  386. // and design as for multicast.
  387. if (!network->filterOutgoingPacket(tPtr,false,RR->identity.address(),Address(),from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  388. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"filter blocked");
  389. return;
  390. }
  391. Address bridges[ZT_MAX_BRIDGE_SPAM];
  392. unsigned int numBridges = 0;
  393. /* Create an array of up to ZT_MAX_BRIDGE_SPAM recipients for this bridged frame. */
  394. bridges[0] = network->findBridgeTo(to);
  395. std::vector<Address> activeBridges(network->config().activeBridges());
  396. if ((bridges[0])&&(bridges[0] != RR->identity.address())&&(network->config().permitsBridging(bridges[0]))) {
  397. /* We have a known bridge route for this MAC, send it there. */
  398. ++numBridges;
  399. } else if (!activeBridges.empty()) {
  400. /* If there is no known route, spam to up to ZT_MAX_BRIDGE_SPAM active
  401. * bridges. If someone responds, we'll learn the route. */
  402. std::vector<Address>::const_iterator ab(activeBridges.begin());
  403. if (activeBridges.size() <= ZT_MAX_BRIDGE_SPAM) {
  404. // If there are <= ZT_MAX_BRIDGE_SPAM active bridges, spam them all
  405. while (ab != activeBridges.end()) {
  406. bridges[numBridges++] = *ab;
  407. ++ab;
  408. }
  409. } else {
  410. // Otherwise pick a random set of them
  411. while (numBridges < ZT_MAX_BRIDGE_SPAM) {
  412. if (ab == activeBridges.end())
  413. ab = activeBridges.begin();
  414. if (((unsigned long)Utils::random() % (unsigned long)activeBridges.size()) == 0) {
  415. bridges[numBridges++] = *ab;
  416. ++ab;
  417. } else ++ab;
  418. }
  419. }
  420. }
  421. for(unsigned int b=0;b<numBridges;++b) {
  422. if (network->filterOutgoingPacket(tPtr,true,RR->identity.address(),bridges[b],from,to,(const uint8_t *)data,len,etherType,vlanId,qosBucket)) {
  423. Packet outp(bridges[b],RR->identity.address(),Packet::VERB_EXT_FRAME);
  424. outp.append(network->id());
  425. outp.append((uint8_t)0x00);
  426. to.appendTo(outp);
  427. from.appendTo(outp);
  428. outp.append((uint16_t)etherType);
  429. outp.append(data,len);
  430. if (!network->config().disableCompression())
  431. outp.compress();
  432. aqm_enqueue(tPtr,network,outp,true,qosBucket);
  433. } else {
  434. RR->t->outgoingNetworkFrameDropped(tPtr,network,from,to,etherType,vlanId,len,"filter blocked (bridge replication)");
  435. }
  436. }
  437. }
  438. }
  439. void Switch::aqm_enqueue(void *tPtr, const SharedPtr<Network> &network, Packet &packet,bool encrypt,int qosBucket)
  440. {
  441. if(!network->qosEnabled()) {
  442. send(tPtr, packet, encrypt);
  443. return;
  444. }
  445. NetworkQoSControlBlock *nqcb = _netQueueControlBlock[network->id()];
  446. if (!nqcb) {
  447. // DEBUG_INFO("creating network QoS control block (NQCB) for network %llx", network->id());
  448. nqcb = new NetworkQoSControlBlock();
  449. _netQueueControlBlock[network->id()] = nqcb;
  450. // Initialize ZT_QOS_NUM_BUCKETS queues and place them in the INACTIVE list
  451. // These queues will be shuffled between the new/old/inactive lists by the enqueue/dequeue algorithm
  452. for (int i=0; i<ZT_QOS_NUM_BUCKETS; i++) {
  453. nqcb->inactiveQueues.push_back(new ManagedQueue(i));
  454. }
  455. }
  456. if (packet.verb() != Packet::VERB_FRAME && packet.verb() != Packet::VERB_EXT_FRAME) {
  457. // DEBUG_INFO("skipping, no QoS for this packet, verb=%x", packet.verb());
  458. // just send packet normally, no QoS for ZT protocol traffic
  459. send(tPtr, packet, encrypt);
  460. }
  461. _aqm_m.lock();
  462. // Enqueue packet and move queue to appropriate list
  463. const Address dest(packet.destination());
  464. TXQueueEntry *txEntry = new TXQueueEntry(dest,RR->node->now(),packet,encrypt);
  465. ManagedQueue *selectedQueue = nullptr;
  466. for (size_t i=0; i<ZT_QOS_NUM_BUCKETS; i++) {
  467. if (i < nqcb->oldQueues.size()) { // search old queues first (I think this is best since old would imply most recent usage of the queue)
  468. if (nqcb->oldQueues[i]->id == qosBucket) {
  469. selectedQueue = nqcb->oldQueues[i];
  470. }
  471. } if (i < nqcb->newQueues.size()) { // search new queues (this would imply not often-used queues)
  472. if (nqcb->newQueues[i]->id == qosBucket) {
  473. selectedQueue = nqcb->newQueues[i];
  474. }
  475. } if (i < nqcb->inactiveQueues.size()) { // search inactive queues
  476. if (nqcb->inactiveQueues[i]->id == qosBucket) {
  477. selectedQueue = nqcb->inactiveQueues[i];
  478. // move queue to end of NEW queue list
  479. selectedQueue->byteCredit = ZT_QOS_QUANTUM;
  480. // DEBUG_INFO("moving q=%p from INACTIVE to NEW list", selectedQueue);
  481. nqcb->newQueues.push_back(selectedQueue);
  482. nqcb->inactiveQueues.erase(nqcb->inactiveQueues.begin() + i);
  483. }
  484. }
  485. }
  486. if (!selectedQueue) {
  487. return;
  488. }
  489. selectedQueue->q.push_back(txEntry);
  490. selectedQueue->byteLength+=txEntry->packet.payloadLength();
  491. nqcb->_currEnqueuedPackets++;
  492. // DEBUG_INFO("nq=%2lu, oq=%2lu, iq=%2lu, nqcb.size()=%3d, bucket=%2d, q=%p", nqcb->newQueues.size(), nqcb->oldQueues.size(), nqcb->inactiveQueues.size(), nqcb->_currEnqueuedPackets, qosBucket, selectedQueue);
  493. // Drop a packet if necessary
  494. ManagedQueue *selectedQueueToDropFrom = nullptr;
  495. if (nqcb->_currEnqueuedPackets > ZT_QOS_MAX_ENQUEUED_PACKETS)
  496. {
  497. // DEBUG_INFO("too many enqueued packets (%d), finding packet to drop", nqcb->_currEnqueuedPackets);
  498. int maxQueueLength = 0;
  499. for (size_t i=0; i<ZT_QOS_NUM_BUCKETS; i++) {
  500. if (i < nqcb->oldQueues.size()) {
  501. if (nqcb->oldQueues[i]->byteLength > maxQueueLength) {
  502. maxQueueLength = nqcb->oldQueues[i]->byteLength;
  503. selectedQueueToDropFrom = nqcb->oldQueues[i];
  504. }
  505. } if (i < nqcb->newQueues.size()) {
  506. if (nqcb->newQueues[i]->byteLength > maxQueueLength) {
  507. maxQueueLength = nqcb->newQueues[i]->byteLength;
  508. selectedQueueToDropFrom = nqcb->newQueues[i];
  509. }
  510. } if (i < nqcb->inactiveQueues.size()) {
  511. if (nqcb->inactiveQueues[i]->byteLength > maxQueueLength) {
  512. maxQueueLength = nqcb->inactiveQueues[i]->byteLength;
  513. selectedQueueToDropFrom = nqcb->inactiveQueues[i];
  514. }
  515. }
  516. }
  517. if (selectedQueueToDropFrom) {
  518. // DEBUG_INFO("dropping packet from head of largest queue (%d payload bytes)", maxQueueLength);
  519. int sizeOfDroppedPacket = selectedQueueToDropFrom->q.front()->packet.payloadLength();
  520. delete selectedQueueToDropFrom->q.front();
  521. selectedQueueToDropFrom->q.pop_front();
  522. selectedQueueToDropFrom->byteLength-=sizeOfDroppedPacket;
  523. nqcb->_currEnqueuedPackets--;
  524. }
  525. }
  526. _aqm_m.unlock();
  527. aqm_dequeue(tPtr);
  528. }
  529. uint64_t Switch::control_law(uint64_t t, int count)
  530. {
  531. return (uint64_t)(t + ZT_QOS_INTERVAL / sqrt(count));
  532. }
  533. Switch::dqr Switch::dodequeue(ManagedQueue *q, uint64_t now)
  534. {
  535. dqr r;
  536. r.ok_to_drop = false;
  537. r.p = q->q.front();
  538. if (r.p == NULL) {
  539. q->first_above_time = 0;
  540. return r;
  541. }
  542. uint64_t sojourn_time = now - r.p->creationTime;
  543. if (sojourn_time < ZT_QOS_TARGET || q->byteLength <= ZT_DEFAULT_MTU) {
  544. // went below - stay below for at least interval
  545. q->first_above_time = 0;
  546. } else {
  547. if (q->first_above_time == 0) {
  548. // just went above from below. if still above at
  549. // first_above_time, will say it's ok to drop.
  550. q->first_above_time = now + ZT_QOS_INTERVAL;
  551. } else if (now >= q->first_above_time) {
  552. r.ok_to_drop = true;
  553. }
  554. }
  555. return r;
  556. }
  557. Switch::TXQueueEntry * Switch::CoDelDequeue(ManagedQueue *q, bool isNew, uint64_t now)
  558. {
  559. dqr r = dodequeue(q, now);
  560. if (q->dropping) {
  561. if (!r.ok_to_drop) {
  562. q->dropping = false;
  563. }
  564. while (now >= q->drop_next && q->dropping) {
  565. q->q.pop_front(); // drop
  566. r = dodequeue(q, now);
  567. if (!r.ok_to_drop) {
  568. // leave dropping state
  569. q->dropping = false;
  570. } else {
  571. ++(q->count);
  572. // schedule the next drop.
  573. q->drop_next = control_law(q->drop_next, q->count);
  574. }
  575. }
  576. } else if (r.ok_to_drop) {
  577. q->q.pop_front(); // drop
  578. r = dodequeue(q, now);
  579. q->dropping = true;
  580. q->count = (q->count > 2 && now - q->drop_next < 8*ZT_QOS_INTERVAL)?
  581. q->count - 2 : 1;
  582. q->drop_next = control_law(now, q->count);
  583. }
  584. return r.p;
  585. }
  586. void Switch::aqm_dequeue(void *tPtr)
  587. {
  588. // Cycle through network-specific QoS control blocks
  589. for(std::map<uint64_t,NetworkQoSControlBlock*>::iterator nqcb(_netQueueControlBlock.begin());nqcb!=_netQueueControlBlock.end();) {
  590. if (!(*nqcb).second->_currEnqueuedPackets) {
  591. return;
  592. }
  593. uint64_t now = RR->node->now();
  594. TXQueueEntry *entryToEmit = nullptr;
  595. std::vector<ManagedQueue*> *currQueues = &((*nqcb).second->newQueues);
  596. std::vector<ManagedQueue*> *oldQueues = &((*nqcb).second->oldQueues);
  597. std::vector<ManagedQueue*> *inactiveQueues = &((*nqcb).second->inactiveQueues);
  598. _aqm_m.lock();
  599. // Attempt dequeue from queues in NEW list
  600. bool examiningNewQueues = true;
  601. while (currQueues->size()) {
  602. ManagedQueue *queueAtFrontOfList = currQueues->front();
  603. if (queueAtFrontOfList->byteCredit < 0) {
  604. queueAtFrontOfList->byteCredit += ZT_QOS_QUANTUM;
  605. // Move to list of OLD queues
  606. // DEBUG_INFO("moving q=%p from NEW to OLD list", queueAtFrontOfList);
  607. oldQueues->push_back(queueAtFrontOfList);
  608. currQueues->erase(currQueues->begin());
  609. } else {
  610. entryToEmit = CoDelDequeue(queueAtFrontOfList, examiningNewQueues, now);
  611. if (!entryToEmit) {
  612. // Move to end of list of OLD queues
  613. // DEBUG_INFO("moving q=%p from NEW to OLD list", queueAtFrontOfList);
  614. oldQueues->push_back(queueAtFrontOfList);
  615. currQueues->erase(currQueues->begin());
  616. }
  617. else {
  618. int len = entryToEmit->packet.payloadLength();
  619. queueAtFrontOfList->byteLength -= len;
  620. queueAtFrontOfList->byteCredit -= len;
  621. // Send the packet!
  622. queueAtFrontOfList->q.pop_front();
  623. send(tPtr, entryToEmit->packet, entryToEmit->encrypt);
  624. (*nqcb).second->_currEnqueuedPackets--;
  625. }
  626. if (queueAtFrontOfList) {
  627. //DEBUG_INFO("dequeuing from q=%p, len=%lu in NEW list (byteCredit=%d)", queueAtFrontOfList, queueAtFrontOfList->q.size(), queueAtFrontOfList->byteCredit);
  628. }
  629. break;
  630. }
  631. }
  632. // Attempt dequeue from queues in OLD list
  633. examiningNewQueues = false;
  634. currQueues = &((*nqcb).second->oldQueues);
  635. while (currQueues->size()) {
  636. ManagedQueue *queueAtFrontOfList = currQueues->front();
  637. if (queueAtFrontOfList->byteCredit < 0) {
  638. queueAtFrontOfList->byteCredit += ZT_QOS_QUANTUM;
  639. oldQueues->push_back(queueAtFrontOfList);
  640. currQueues->erase(currQueues->begin());
  641. } else {
  642. entryToEmit = CoDelDequeue(queueAtFrontOfList, examiningNewQueues, now);
  643. if (!entryToEmit) {
  644. //DEBUG_INFO("moving q=%p from OLD to INACTIVE list", queueAtFrontOfList);
  645. // Move to inactive list of queues
  646. inactiveQueues->push_back(queueAtFrontOfList);
  647. currQueues->erase(currQueues->begin());
  648. }
  649. else {
  650. int len = entryToEmit->packet.payloadLength();
  651. queueAtFrontOfList->byteLength -= len;
  652. queueAtFrontOfList->byteCredit -= len;
  653. queueAtFrontOfList->q.pop_front();
  654. send(tPtr, entryToEmit->packet, entryToEmit->encrypt);
  655. (*nqcb).second->_currEnqueuedPackets--;
  656. }
  657. if (queueAtFrontOfList) {
  658. //DEBUG_INFO("dequeuing from q=%p, len=%lu in OLD list (byteCredit=%d)", queueAtFrontOfList, queueAtFrontOfList->q.size(), queueAtFrontOfList->byteCredit);
  659. }
  660. break;
  661. }
  662. }
  663. nqcb++;
  664. _aqm_m.unlock();
  665. }
  666. }
  667. void Switch::removeNetworkQoSControlBlock(uint64_t nwid)
  668. {
  669. NetworkQoSControlBlock *nq = _netQueueControlBlock[nwid];
  670. if (nq) {
  671. _netQueueControlBlock.erase(nwid);
  672. delete nq;
  673. nq = NULL;
  674. }
  675. }
  676. void Switch::send(void *tPtr,Packet &packet,bool encrypt)
  677. {
  678. const Address dest(packet.destination());
  679. if (dest == RR->identity.address())
  680. return;
  681. if (!_trySend(tPtr,packet,encrypt)) {
  682. {
  683. Mutex::Lock _l(_txQueue_m);
  684. if (_txQueue.size() >= ZT_TX_QUEUE_SIZE) {
  685. _txQueue.pop_front();
  686. }
  687. _txQueue.push_back(TXQueueEntry(dest,RR->node->now(),packet,encrypt));
  688. }
  689. if (!RR->topology->get(dest))
  690. requestWhois(tPtr,RR->node->now(),dest);
  691. }
  692. }
  693. void Switch::requestWhois(void *tPtr,const int64_t now,const Address &addr)
  694. {
  695. if (addr == RR->identity.address())
  696. return;
  697. {
  698. Mutex::Lock _l(_lastSentWhoisRequest_m);
  699. int64_t &last = _lastSentWhoisRequest[addr];
  700. if ((now - last) < ZT_WHOIS_RETRY_DELAY)
  701. return;
  702. else last = now;
  703. }
  704. const SharedPtr<Peer> root(RR->topology->root(now));
  705. if (root) {
  706. Packet outp(root->address(),RR->identity.address(),Packet::VERB_WHOIS);
  707. addr.appendTo(outp);
  708. RR->node->expectReplyTo(outp.packetId());
  709. root->sendDirect(tPtr,outp.data(),outp.size(),now,true);
  710. }
  711. }
  712. void Switch::doAnythingWaitingForPeer(void *tPtr,const SharedPtr<Peer> &peer)
  713. {
  714. {
  715. Mutex::Lock _l(_lastSentWhoisRequest_m);
  716. _lastSentWhoisRequest.erase(peer->address());
  717. }
  718. const int64_t now = RR->node->now();
  719. for(unsigned int ptr=0;ptr<ZT_RX_QUEUE_SIZE;++ptr) {
  720. RXQueueEntry *const rq = &(_rxQueue[ptr]);
  721. Mutex::Lock rql(rq->lock);
  722. if ((rq->timestamp)&&(rq->complete)) {
  723. if ((rq->frag0.tryDecode(RR,tPtr))||((now - rq->timestamp) > ZT_RECEIVE_QUEUE_TIMEOUT))
  724. rq->timestamp = 0;
  725. }
  726. }
  727. {
  728. Mutex::Lock _l(_txQueue_m);
  729. for(std::list< TXQueueEntry >::iterator txi(_txQueue.begin());txi!=_txQueue.end();) {
  730. if (txi->dest == peer->address()) {
  731. if (_trySend(tPtr,txi->packet,txi->encrypt)) {
  732. _txQueue.erase(txi++);
  733. } else {
  734. ++txi;
  735. }
  736. } else {
  737. ++txi;
  738. }
  739. }
  740. }
  741. }
  742. unsigned long Switch::doTimerTasks(void *tPtr,int64_t now)
  743. {
  744. const uint64_t timeSinceLastCheck = now - _lastCheckedQueues;
  745. if (timeSinceLastCheck < ZT_WHOIS_RETRY_DELAY)
  746. return (unsigned long)(ZT_WHOIS_RETRY_DELAY - timeSinceLastCheck);
  747. _lastCheckedQueues = now;
  748. std::vector<Address> needWhois;
  749. {
  750. Mutex::Lock _l(_txQueue_m);
  751. for(std::list< TXQueueEntry >::iterator txi(_txQueue.begin());txi!=_txQueue.end();) {
  752. if (_trySend(tPtr,txi->packet,txi->encrypt)) {
  753. _txQueue.erase(txi++);
  754. } else if ((now - txi->creationTime) > ZT_TRANSMIT_QUEUE_TIMEOUT) {
  755. _txQueue.erase(txi++);
  756. } else {
  757. if (!RR->topology->get(txi->dest))
  758. needWhois.push_back(txi->dest);
  759. ++txi;
  760. }
  761. }
  762. }
  763. for(std::vector<Address>::const_iterator i(needWhois.begin());i!=needWhois.end();++i)
  764. requestWhois(tPtr,now,*i);
  765. for(unsigned int ptr=0;ptr<ZT_RX_QUEUE_SIZE;++ptr) {
  766. RXQueueEntry *const rq = &(_rxQueue[ptr]);
  767. Mutex::Lock rql(rq->lock);
  768. if ((rq->timestamp)&&(rq->complete)) {
  769. if ((rq->frag0.tryDecode(RR,tPtr))||((now - rq->timestamp) > ZT_RECEIVE_QUEUE_TIMEOUT)) {
  770. rq->timestamp = 0;
  771. } else {
  772. const Address src(rq->frag0.source());
  773. if (!RR->topology->get(src))
  774. requestWhois(tPtr,now,src);
  775. }
  776. }
  777. }
  778. {
  779. Mutex::Lock _l(_lastUniteAttempt_m);
  780. Hashtable< _LastUniteKey,uint64_t >::Iterator i(_lastUniteAttempt);
  781. _LastUniteKey *k = (_LastUniteKey *)0;
  782. uint64_t *v = (uint64_t *)0;
  783. while (i.next(k,v)) {
  784. if ((now - *v) >= (ZT_MIN_UNITE_INTERVAL * 8))
  785. _lastUniteAttempt.erase(*k);
  786. }
  787. }
  788. {
  789. Mutex::Lock _l(_lastSentWhoisRequest_m);
  790. Hashtable< Address,int64_t >::Iterator i(_lastSentWhoisRequest);
  791. Address *a = (Address *)0;
  792. int64_t *ts = (int64_t *)0;
  793. while (i.next(a,ts)) {
  794. if ((now - *ts) > (ZT_WHOIS_RETRY_DELAY * 2))
  795. _lastSentWhoisRequest.erase(*a);
  796. }
  797. }
  798. return ZT_WHOIS_RETRY_DELAY;
  799. }
  800. bool Switch::_shouldUnite(const int64_t now,const Address &source,const Address &destination)
  801. {
  802. Mutex::Lock _l(_lastUniteAttempt_m);
  803. uint64_t &ts = _lastUniteAttempt[_LastUniteKey(source,destination)];
  804. if ((now - ts) >= ZT_MIN_UNITE_INTERVAL) {
  805. ts = now;
  806. return true;
  807. }
  808. return false;
  809. }
  810. bool Switch::_trySend(void *tPtr,Packet &packet,bool encrypt)
  811. {
  812. SharedPtr<Path> viaPath;
  813. const int64_t now = RR->node->now();
  814. const Address destination(packet.destination());
  815. const SharedPtr<Peer> peer(RR->topology->get(destination));
  816. if (peer) {
  817. viaPath = peer->getAppropriatePath(now,false);
  818. if (!viaPath) {
  819. if (peer->rateGateTryStaticPath(now)) {
  820. InetAddress tryAddr;
  821. bool gotPath = RR->node->externalPathLookup(tPtr,peer->address(),AF_INET6,tryAddr);
  822. if ((gotPath)&&(tryAddr)) {
  823. peer->sendHELLO(tPtr,-1,tryAddr,now);
  824. } else {
  825. gotPath = RR->node->externalPathLookup(tPtr,peer->address(),AF_INET,tryAddr);
  826. if ((gotPath)&&(tryAddr))
  827. peer->sendHELLO(tPtr,-1,tryAddr,now);
  828. }
  829. }
  830. const SharedPtr<Peer> relay(RR->topology->findRelayTo(now,destination));
  831. if (relay) {
  832. viaPath = relay->getAppropriatePath(now,true);
  833. if (!viaPath)
  834. return false;
  835. }
  836. return false;
  837. }
  838. } else {
  839. return false;
  840. }
  841. unsigned int mtu = ZT_DEFAULT_PHYSMTU;
  842. uint64_t trustedPathId = 0;
  843. RR->topology->getOutboundPathInfo(viaPath->address(),mtu,trustedPathId);
  844. unsigned int chunkSize = std::min(packet.size(),mtu);
  845. packet.setFragmented(chunkSize < packet.size());
  846. peer->recordOutgoingPacket(viaPath, packet.packetId(), packet.payloadLength(), packet.verb(), now);
  847. if (trustedPathId) {
  848. packet.setTrusted(trustedPathId);
  849. } else {
  850. packet.armor(peer->key(),encrypt);
  851. }
  852. if (viaPath->send(RR,tPtr,packet.data(),chunkSize,now)) {
  853. if (chunkSize < packet.size()) {
  854. // Too big for one packet, fragment the rest
  855. unsigned int fragStart = chunkSize;
  856. unsigned int remaining = packet.size() - chunkSize;
  857. unsigned int fragsRemaining = (remaining / (mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH));
  858. if ((fragsRemaining * (mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH)) < remaining)
  859. ++fragsRemaining;
  860. const unsigned int totalFragments = fragsRemaining + 1;
  861. for(unsigned int fno=1;fno<totalFragments;++fno) {
  862. chunkSize = std::min(remaining,(unsigned int)(mtu - ZT_PROTO_MIN_FRAGMENT_LENGTH));
  863. Packet::Fragment frag(packet,fragStart,chunkSize,fno,totalFragments);
  864. viaPath->send(RR,tPtr,frag.data(),frag.size(),now);
  865. fragStart += chunkSize;
  866. remaining -= chunkSize;
  867. }
  868. }
  869. }
  870. return true;
  871. }
  872. } // namespace ZeroTier