Topology.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c)2019 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: 2026-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 "Topology.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Node.hpp"
  17. #include "Network.hpp"
  18. #include "NetworkConfig.hpp"
  19. #include "Buffer.hpp"
  20. #include "Switch.hpp"
  21. namespace ZeroTier {
  22. #define ZT_DEFAULT_WORLD_LENGTH 570
  23. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x7e,0xe9,0x57,0x60,0xcd,0xb8,0xb3,0x88,0xa4,0x69,0x22,0x14,0x91,0xaa,0x9a,0xcd,0x66,0xcc,0x76,0x4c,0xde,0xfd,0x56,0x03,0x9f,0x10,0x67,0xae,0x15,0xe6,0x9c,0x6f,0xb4,0x2d,0x7b,0x55,0x33,0x0e,0x3f,0xda,0xac,0x52,0x9c,0x07,0x92,0xfd,0x73,0x40,0xa6,0xaa,0x21,0xab,0xa8,0xa4,0x89,0xfd,0xae,0xa4,0x4a,0x39,0xbf,0x2d,0x00,0x65,0x9a,0xc9,0xc8,0x18,0xeb,0x36,0x00,0x92,0x76,0x37,0xef,0x4d,0x14,0x04,0xa4,0x4d,0x54,0x46,0x84,0x85,0x13,0x79,0x75,0x1f,0xaa,0x79,0xb4,0xc4,0xea,0x85,0x04,0x01,0x75,0xea,0x06,0x58,0x60,0x48,0x24,0x02,0xe1,0xeb,0x34,0x20,0x52,0x00,0x0e,0x62,0x90,0x06,0x1a,0x9b,0xe0,0xcd,0x29,0x3c,0x8b,0x55,0xf1,0xc3,0xd2,0x52,0x48,0x08,0xaf,0xc5,0x49,0x22,0x08,0x0e,0x35,0x39,0xa7,0x5a,0xdd,0xc3,0xce,0xf0,0xf6,0xad,0x26,0x0d,0x58,0x82,0x93,0xbb,0x77,0x86,0xe7,0x1e,0xfa,0x4b,0x90,0x57,0xda,0xd9,0x86,0x7a,0xfe,0x12,0xdd,0x04,0xca,0xfe,0x9e,0xfe,0xb9,0x00,0xcc,0xde,0xf7,0x6b,0xc7,0xb9,0x7d,0xed,0x90,0x4e,0xab,0xc5,0xdf,0x09,0x88,0x6d,0x9c,0x15,0x14,0xa6,0x10,0x03,0x6c,0xb9,0x13,0x9c,0xc2,0x14,0x00,0x1a,0x29,0x58,0x97,0x8e,0xfc,0xec,0x15,0x71,0x2d,0xd3,0x94,0x8c,0x6e,0x6b,0x3a,0x8e,0x89,0x3d,0xf0,0x1f,0xf4,0x93,0xd1,0xf8,0xd9,0x80,0x6a,0x86,0x0c,0x54,0x20,0x57,0x1b,0xf0,0x00,0x02,0x04,0x68,0xc2,0x08,0x86,0x27,0x09,0x06,0x26,0x05,0x98,0x80,0x02,0x00,0x12,0x00,0x00,0x30,0x05,0x71,0x0e,0x34,0x00,0x51,0x27,0x09,0x77,0x8c,0xde,0x71,0x90,0x00,0x3f,0x66,0x81,0xa9,0x9e,0x5a,0xd1,0x89,0x5e,0x9f,0xba,0x33,0xe6,0x21,0x2d,0x44,0x54,0xe1,0x68,0xbc,0xec,0x71,0x12,0x10,0x1b,0xf0,0x00,0x95,0x6e,0xd8,0xe9,0x2e,0x42,0x89,0x2c,0xb6,0xf2,0xec,0x41,0x08,0x81,0xa8,0x4a,0xb1,0x9d,0xa5,0x0e,0x12,0x87,0xba,0x3d,0x92,0x6c,0x3a,0x1f,0x75,0x5c,0xcc,0xf2,0x99,0xa1,0x20,0x70,0x55,0x00,0x02,0x04,0x67,0xc3,0x67,0x42,0x27,0x09,0x06,0x26,0x05,0x98,0x80,0x04,0x00,0x00,0xc3,0x02,0x54,0xf2,0xbc,0xa1,0xf7,0x00,0x19,0x27,0x09,0x62,0xf8,0x65,0xae,0x71,0x00,0xe2,0x07,0x6c,0x57,0xde,0x87,0x0e,0x62,0x88,0xd7,0xd5,0xe7,0x40,0x44,0x08,0xb1,0x54,0x5e,0xfc,0xa3,0x7d,0x67,0xf7,0x7b,0x87,0xe9,0xe5,0x41,0x68,0xc2,0x5d,0x3e,0xf1,0xa9,0xab,0xf2,0x90,0x5e,0xa5,0xe7,0x85,0xc0,0x1d,0xff,0x23,0x88,0x7a,0xd4,0x23,0x2d,0x95,0xc7,0xa8,0xfd,0x2c,0x27,0x11,0x1a,0x72,0xbd,0x15,0x93,0x22,0xdc,0x00,0x02,0x04,0x32,0x07,0xfc,0x8a,0x27,0x09,0x06,0x20,0x01,0x49,0xf0,0xd0,0xdb,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x27,0x09,0xca,0xfe,0x04,0xeb,0xa9,0x00,0x6c,0x6a,0x9d,0x1d,0xea,0x55,0xc1,0x61,0x6b,0xfe,0x2a,0x2b,0x8f,0x0f,0xf9,0xa8,0xca,0xca,0xf7,0x03,0x74,0xfb,0x1f,0x39,0xe3,0xbe,0xf8,0x1c,0xbf,0xeb,0xef,0x17,0xb7,0x22,0x82,0x68,0xa0,0xa2,0xa2,0x9d,0x34,0x88,0xc7,0x52,0x56,0x5c,0x6c,0x96,0x5c,0xbd,0x65,0x06,0xec,0x24,0x39,0x7c,0xc8,0xa5,0xd9,0xd1,0x52,0x85,0xa8,0x7f,0x00,0x02,0x04,0x54,0x11,0x35,0x9b,0x27,0x09,0x06,0x2a,0x02,0x6e,0xa0,0xd4,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x99,0x93,0x27,0x09};
  24. Topology::Topology(const RuntimeEnvironment *renv,void *tPtr) :
  25. RR(renv),
  26. _numConfiguredPhysicalPaths(0),
  27. _amUpstream(false)
  28. {
  29. uint8_t tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  30. uint64_t idtmp[2];
  31. idtmp[0] = 0;
  32. idtmp[1] = 0;
  33. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PLANET,idtmp,tmp,sizeof(tmp));
  34. if (n > 0) {
  35. try {
  36. World cachedPlanet;
  37. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n),0);
  38. addWorld(tPtr,cachedPlanet,false);
  39. } catch ( ... ) {} // ignore invalid cached planets
  40. }
  41. World defaultPlanet;
  42. {
  43. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  44. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  45. }
  46. addWorld(tPtr,defaultPlanet,false);
  47. }
  48. Topology::~Topology()
  49. {
  50. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  51. Address *a = (Address *)0;
  52. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  53. while (i.next(a,p)) {
  54. _savePeer((void *)0,*p);
  55. }
  56. }
  57. SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
  58. {
  59. SharedPtr<Peer> np;
  60. {
  61. Mutex::Lock _l(_peers_m);
  62. SharedPtr<Peer> &hp = _peers[peer->address()];
  63. if (!hp) {
  64. hp = peer;
  65. }
  66. np = hp;
  67. }
  68. return np;
  69. }
  70. SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
  71. {
  72. if (zta == RR->identity.address()) {
  73. return SharedPtr<Peer>();
  74. }
  75. {
  76. Mutex::Lock _l(_peers_m);
  77. const SharedPtr<Peer> *const ap = _peers.get(zta);
  78. if (ap) {
  79. return *ap;
  80. }
  81. }
  82. try {
  83. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  84. uint64_t idbuf[2];
  85. idbuf[0] = zta.toInt();
  86. idbuf[1] = 0;
  87. int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  88. if (len > 0) {
  89. buf.setSize(len);
  90. Mutex::Lock _l(_peers_m);
  91. SharedPtr<Peer> &ap = _peers[zta];
  92. if (ap) {
  93. return ap;
  94. }
  95. ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
  96. if (!ap) {
  97. _peers.erase(zta);
  98. }
  99. return SharedPtr<Peer>();
  100. }
  101. } catch ( ... ) {} // ignore invalid identities or other strange failures
  102. return SharedPtr<Peer>();
  103. }
  104. Identity Topology::getIdentity(void *tPtr,const Address &zta)
  105. {
  106. if (zta == RR->identity.address()) {
  107. return RR->identity;
  108. } else {
  109. Mutex::Lock _l(_peers_m);
  110. const SharedPtr<Peer> *const ap = _peers.get(zta);
  111. if (ap) {
  112. return (*ap)->identity();
  113. }
  114. }
  115. return Identity();
  116. }
  117. SharedPtr<Peer> Topology::getUpstreamPeer()
  118. {
  119. const int64_t now = RR->node->now();
  120. unsigned int bestq = ~((unsigned int)0);
  121. const SharedPtr<Peer> *best = (const SharedPtr<Peer> *)0;
  122. Mutex::Lock _l2(_peers_m);
  123. Mutex::Lock _l1(_upstreams_m);
  124. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  125. const SharedPtr<Peer> *p = _peers.get(*a);
  126. if (p) {
  127. const unsigned int q = (*p)->relayQuality(now);
  128. if (q <= bestq) {
  129. bestq = q;
  130. best = p;
  131. }
  132. }
  133. }
  134. if (!best) {
  135. return SharedPtr<Peer>();
  136. }
  137. return *best;
  138. }
  139. bool Topology::isUpstream(const Identity &id) const
  140. {
  141. Mutex::Lock _l(_upstreams_m);
  142. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  143. }
  144. bool Topology::shouldAcceptWorldUpdateFrom(const Address &addr) const
  145. {
  146. Mutex::Lock _l(_upstreams_m);
  147. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),addr) != _upstreamAddresses.end()) {
  148. return true;
  149. }
  150. for(std::vector< std::pair< uint64_t,Address> >::const_iterator s(_moonSeeds.begin());s!=_moonSeeds.end();++s) {
  151. if (s->second == addr) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. ZT_PeerRole Topology::role(const Address &ztaddr) const
  158. {
  159. Mutex::Lock _l(_upstreams_m);
  160. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  161. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  162. if (i->identity.address() == ztaddr) {
  163. return ZT_PEER_ROLE_PLANET;
  164. }
  165. }
  166. return ZT_PEER_ROLE_MOON;
  167. }
  168. return ZT_PEER_ROLE_LEAF;
  169. }
  170. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  171. {
  172. Mutex::Lock _l(_upstreams_m);
  173. // For roots the only permitted addresses are those defined. This adds just a little
  174. // bit of extra security against spoofing, replaying, etc.
  175. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  176. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  177. if (r->identity.address() == ztaddr) {
  178. if (r->stableEndpoints.empty()) {
  179. return false; // no stable endpoints specified, so allow dynamic paths
  180. }
  181. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  182. if (ipaddr.ipsEqual(*e)) {
  183. return false;
  184. }
  185. }
  186. }
  187. }
  188. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  189. for(std::vector<World::Root>::const_iterator r(m->roots().begin());r!=m->roots().end();++r) {
  190. if (r->identity.address() == ztaddr) {
  191. if (r->stableEndpoints.empty()) {
  192. return false; // no stable endpoints specified, so allow dynamic paths
  193. }
  194. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  195. if (ipaddr.ipsEqual(*e)) {
  196. return false;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. return true;
  203. }
  204. return false;
  205. }
  206. bool Topology::addWorld(void *tPtr,const World &newWorld,bool alwaysAcceptNew)
  207. {
  208. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON)) {
  209. return false;
  210. }
  211. Mutex::Lock _l2(_peers_m);
  212. Mutex::Lock _l1(_upstreams_m);
  213. World *existing = (World *)0;
  214. switch(newWorld.type()) {
  215. case World::TYPE_PLANET:
  216. existing = &_planet;
  217. break;
  218. case World::TYPE_MOON:
  219. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  220. if (m->id() == newWorld.id()) {
  221. existing = &(*m);
  222. break;
  223. }
  224. }
  225. break;
  226. default:
  227. return false;
  228. }
  229. if (existing) {
  230. if (existing->shouldBeReplacedBy(newWorld)) {
  231. *existing = newWorld;
  232. } else {
  233. return false;
  234. }
  235. } else if (newWorld.type() == World::TYPE_MOON) {
  236. if (alwaysAcceptNew) {
  237. _moons.push_back(newWorld);
  238. existing = &(_moons.back());
  239. } else {
  240. for(std::vector< std::pair<uint64_t,Address> >::iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  241. if (m->first == newWorld.id()) {
  242. for(std::vector<World::Root>::const_iterator r(newWorld.roots().begin());r!=newWorld.roots().end();++r) {
  243. if (r->identity.address() == m->second) {
  244. _moonSeeds.erase(m);
  245. _moons.push_back(newWorld);
  246. existing = &(_moons.back());
  247. break;
  248. }
  249. }
  250. if (existing) {
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. if (!existing) {
  257. return false;
  258. }
  259. } else {
  260. return false;
  261. }
  262. try {
  263. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  264. existing->serialize(sbuf,false);
  265. uint64_t idtmp[2];
  266. idtmp[0] = existing->id();
  267. idtmp[1] = 0;
  268. RR->node->stateObjectPut(tPtr,(existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON,idtmp,sbuf.data(),sbuf.size());
  269. } catch ( ... ) {}
  270. _memoizeUpstreams(tPtr);
  271. return true;
  272. }
  273. void Topology::addMoon(void *tPtr,const uint64_t id,const Address &seed)
  274. {
  275. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  276. uint64_t idtmp[2];
  277. idtmp[0] = id;
  278. idtmp[1] = 0;
  279. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_MOON,idtmp,tmp,sizeof(tmp));
  280. if (n > 0) {
  281. try {
  282. World w;
  283. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n));
  284. if ((w.type() == World::TYPE_MOON)&&(w.id() == id)) {
  285. addWorld(tPtr,w,true);
  286. return;
  287. }
  288. } catch ( ... ) {}
  289. }
  290. if (seed) {
  291. Mutex::Lock _l(_upstreams_m);
  292. if (std::find(_moonSeeds.begin(),_moonSeeds.end(),std::pair<uint64_t,Address>(id,seed)) == _moonSeeds.end()) {
  293. _moonSeeds.push_back(std::pair<uint64_t,Address>(id,seed));
  294. }
  295. }
  296. }
  297. void Topology::removeMoon(void *tPtr,const uint64_t id)
  298. {
  299. Mutex::Lock _l2(_peers_m);
  300. Mutex::Lock _l1(_upstreams_m);
  301. std::vector<World> nm;
  302. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  303. if (m->id() != id) {
  304. nm.push_back(*m);
  305. } else {
  306. uint64_t idtmp[2];
  307. idtmp[0] = id;
  308. idtmp[1] = 0;
  309. RR->node->stateObjectDelete(tPtr,ZT_STATE_OBJECT_MOON,idtmp);
  310. }
  311. }
  312. _moons.swap(nm);
  313. std::vector< std::pair<uint64_t,Address> > cm;
  314. for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  315. if (m->first != id) {
  316. cm.push_back(*m);
  317. }
  318. }
  319. _moonSeeds.swap(cm);
  320. _memoizeUpstreams(tPtr);
  321. }
  322. void Topology::doPeriodicTasks(void *tPtr,int64_t now)
  323. {
  324. {
  325. Mutex::Lock _l1(_peers_m);
  326. Mutex::Lock _l2(_upstreams_m);
  327. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  328. Address *a = (Address *)0;
  329. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  330. while (i.next(a,p)) {
  331. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) {
  332. _savePeer(tPtr,*p);
  333. _peers.erase(*a);
  334. }
  335. }
  336. }
  337. {
  338. Mutex::Lock _l(_paths_m);
  339. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  340. Path::HashKey *k = (Path::HashKey *)0;
  341. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  342. while (i.next(k,p)) {
  343. if (p->references() <= 1) {
  344. _paths.erase(*k);
  345. }
  346. }
  347. }
  348. }
  349. void Topology::_memoizeUpstreams(void *tPtr)
  350. {
  351. // assumes _upstreams_m and _peers_m are locked
  352. _upstreamAddresses.clear();
  353. _amUpstream = false;
  354. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  355. const Identity &id = i->identity;
  356. if (id == RR->identity) {
  357. _amUpstream = true;
  358. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) == _upstreamAddresses.end()) {
  359. _upstreamAddresses.push_back(id.address());
  360. SharedPtr<Peer> &hp = _peers[id.address()];
  361. if (!hp) {
  362. hp = new Peer(RR,RR->identity,id);
  363. }
  364. }
  365. }
  366. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  367. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  368. if (i->identity == RR->identity) {
  369. _amUpstream = true;
  370. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  371. _upstreamAddresses.push_back(i->identity.address());
  372. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  373. if (!hp) {
  374. hp = new Peer(RR,RR->identity,i->identity);
  375. }
  376. }
  377. }
  378. }
  379. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  380. }
  381. void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
  382. {
  383. try {
  384. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  385. peer->serializeForCache(buf);
  386. uint64_t tmpid[2];
  387. tmpid[0] = peer->address().toInt();
  388. tmpid[1] = 0;
  389. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
  390. } catch ( ... ) {} // sanity check, discard invalid entries
  391. }
  392. } // namespace ZeroTier