Topology.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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: 2025-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; idtmp[1] = 0;
  32. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PLANET,idtmp,tmp,sizeof(tmp));
  33. if (n > 0) {
  34. try {
  35. World cachedPlanet;
  36. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n),0);
  37. addWorld(tPtr,cachedPlanet,false);
  38. } catch ( ... ) {} // ignore invalid cached planets
  39. }
  40. World defaultPlanet;
  41. {
  42. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  43. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  44. }
  45. addWorld(tPtr,defaultPlanet,false);
  46. }
  47. Topology::~Topology()
  48. {
  49. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  50. Address *a = (Address *)0;
  51. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  52. while (i.next(a,p))
  53. _savePeer((void *)0,*p);
  54. }
  55. SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
  56. {
  57. SharedPtr<Peer> np;
  58. {
  59. Mutex::Lock _l(_peers_m);
  60. SharedPtr<Peer> &hp = _peers[peer->address()];
  61. if (!hp)
  62. hp = peer;
  63. np = hp;
  64. }
  65. return np;
  66. }
  67. SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
  68. {
  69. if (zta == RR->identity.address())
  70. return SharedPtr<Peer>();
  71. {
  72. Mutex::Lock _l(_peers_m);
  73. const SharedPtr<Peer> *const ap = _peers.get(zta);
  74. if (ap)
  75. return *ap;
  76. }
  77. try {
  78. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  79. uint64_t idbuf[2]; idbuf[0] = zta.toInt(); idbuf[1] = 0;
  80. int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  81. if (len > 0) {
  82. buf.setSize(len);
  83. Mutex::Lock _l(_peers_m);
  84. SharedPtr<Peer> &ap = _peers[zta];
  85. if (ap)
  86. return ap;
  87. ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
  88. if (!ap) {
  89. _peers.erase(zta);
  90. }
  91. return SharedPtr<Peer>();
  92. }
  93. } catch ( ... ) {} // ignore invalid identities or other strange failures
  94. return SharedPtr<Peer>();
  95. }
  96. Identity Topology::getIdentity(void *tPtr,const Address &zta)
  97. {
  98. if (zta == RR->identity.address()) {
  99. return RR->identity;
  100. } else {
  101. Mutex::Lock _l(_peers_m);
  102. const SharedPtr<Peer> *const ap = _peers.get(zta);
  103. if (ap)
  104. return (*ap)->identity();
  105. }
  106. return Identity();
  107. }
  108. SharedPtr<Peer> Topology::getUpstreamPeer()
  109. {
  110. const int64_t now = RR->node->now();
  111. unsigned int bestq = ~((unsigned int)0);
  112. const SharedPtr<Peer> *best = (const SharedPtr<Peer> *)0;
  113. Mutex::Lock _l2(_peers_m);
  114. Mutex::Lock _l1(_upstreams_m);
  115. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  116. const SharedPtr<Peer> *p = _peers.get(*a);
  117. if (p) {
  118. const unsigned int q = (*p)->relayQuality(now);
  119. if (q <= bestq) {
  120. bestq = q;
  121. best = p;
  122. }
  123. }
  124. }
  125. if (!best)
  126. return SharedPtr<Peer>();
  127. return *best;
  128. }
  129. bool Topology::isUpstream(const Identity &id) const
  130. {
  131. Mutex::Lock _l(_upstreams_m);
  132. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  133. }
  134. bool Topology::shouldAcceptWorldUpdateFrom(const Address &addr) const
  135. {
  136. Mutex::Lock _l(_upstreams_m);
  137. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),addr) != _upstreamAddresses.end())
  138. return true;
  139. for(std::vector< std::pair< uint64_t,Address> >::const_iterator s(_moonSeeds.begin());s!=_moonSeeds.end();++s) {
  140. if (s->second == addr)
  141. return true;
  142. }
  143. return false;
  144. }
  145. ZT_PeerRole Topology::role(const Address &ztaddr) const
  146. {
  147. Mutex::Lock _l(_upstreams_m);
  148. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  149. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  150. if (i->identity.address() == ztaddr)
  151. return ZT_PEER_ROLE_PLANET;
  152. }
  153. return ZT_PEER_ROLE_MOON;
  154. }
  155. return ZT_PEER_ROLE_LEAF;
  156. }
  157. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  158. {
  159. Mutex::Lock _l(_upstreams_m);
  160. // For roots the only permitted addresses are those defined. This adds just a little
  161. // bit of extra security against spoofing, replaying, etc.
  162. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  163. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  164. if (r->identity.address() == ztaddr) {
  165. if (r->stableEndpoints.empty())
  166. return false; // no stable endpoints specified, so allow dynamic paths
  167. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  168. if (ipaddr.ipsEqual(*e))
  169. return false;
  170. }
  171. }
  172. }
  173. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  174. for(std::vector<World::Root>::const_iterator r(m->roots().begin());r!=m->roots().end();++r) {
  175. if (r->identity.address() == ztaddr) {
  176. if (r->stableEndpoints.empty())
  177. return false; // no stable endpoints specified, so allow dynamic paths
  178. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  179. if (ipaddr.ipsEqual(*e))
  180. return false;
  181. }
  182. }
  183. }
  184. }
  185. return true;
  186. }
  187. return false;
  188. }
  189. bool Topology::addWorld(void *tPtr,const World &newWorld,bool alwaysAcceptNew)
  190. {
  191. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON))
  192. return false;
  193. Mutex::Lock _l2(_peers_m);
  194. Mutex::Lock _l1(_upstreams_m);
  195. World *existing = (World *)0;
  196. switch(newWorld.type()) {
  197. case World::TYPE_PLANET:
  198. existing = &_planet;
  199. break;
  200. case World::TYPE_MOON:
  201. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  202. if (m->id() == newWorld.id()) {
  203. existing = &(*m);
  204. break;
  205. }
  206. }
  207. break;
  208. default:
  209. return false;
  210. }
  211. if (existing) {
  212. if (existing->shouldBeReplacedBy(newWorld))
  213. *existing = newWorld;
  214. else return false;
  215. } else if (newWorld.type() == World::TYPE_MOON) {
  216. if (alwaysAcceptNew) {
  217. _moons.push_back(newWorld);
  218. existing = &(_moons.back());
  219. } else {
  220. for(std::vector< std::pair<uint64_t,Address> >::iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  221. if (m->first == newWorld.id()) {
  222. for(std::vector<World::Root>::const_iterator r(newWorld.roots().begin());r!=newWorld.roots().end();++r) {
  223. if (r->identity.address() == m->second) {
  224. _moonSeeds.erase(m);
  225. _moons.push_back(newWorld);
  226. existing = &(_moons.back());
  227. break;
  228. }
  229. }
  230. if (existing)
  231. break;
  232. }
  233. }
  234. }
  235. if (!existing)
  236. return false;
  237. } else {
  238. return false;
  239. }
  240. try {
  241. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  242. existing->serialize(sbuf,false);
  243. uint64_t idtmp[2];
  244. idtmp[0] = existing->id(); idtmp[1] = 0;
  245. RR->node->stateObjectPut(tPtr,(existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON,idtmp,sbuf.data(),sbuf.size());
  246. } catch ( ... ) {}
  247. _memoizeUpstreams(tPtr);
  248. return true;
  249. }
  250. void Topology::addMoon(void *tPtr,const uint64_t id,const Address &seed)
  251. {
  252. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  253. uint64_t idtmp[2];
  254. idtmp[0] = id; idtmp[1] = 0;
  255. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_MOON,idtmp,tmp,sizeof(tmp));
  256. if (n > 0) {
  257. try {
  258. World w;
  259. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n));
  260. if ((w.type() == World::TYPE_MOON)&&(w.id() == id)) {
  261. addWorld(tPtr,w,true);
  262. return;
  263. }
  264. } catch ( ... ) {}
  265. }
  266. if (seed) {
  267. Mutex::Lock _l(_upstreams_m);
  268. if (std::find(_moonSeeds.begin(),_moonSeeds.end(),std::pair<uint64_t,Address>(id,seed)) == _moonSeeds.end())
  269. _moonSeeds.push_back(std::pair<uint64_t,Address>(id,seed));
  270. }
  271. }
  272. void Topology::removeMoon(void *tPtr,const uint64_t id)
  273. {
  274. Mutex::Lock _l2(_peers_m);
  275. Mutex::Lock _l1(_upstreams_m);
  276. std::vector<World> nm;
  277. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  278. if (m->id() != id) {
  279. nm.push_back(*m);
  280. } else {
  281. uint64_t idtmp[2];
  282. idtmp[0] = id; idtmp[1] = 0;
  283. RR->node->stateObjectDelete(tPtr,ZT_STATE_OBJECT_MOON,idtmp);
  284. }
  285. }
  286. _moons.swap(nm);
  287. std::vector< std::pair<uint64_t,Address> > cm;
  288. for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  289. if (m->first != id)
  290. cm.push_back(*m);
  291. }
  292. _moonSeeds.swap(cm);
  293. _memoizeUpstreams(tPtr);
  294. }
  295. void Topology::doPeriodicTasks(void *tPtr,int64_t now)
  296. {
  297. {
  298. Mutex::Lock _l1(_peers_m);
  299. Mutex::Lock _l2(_upstreams_m);
  300. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  301. Address *a = (Address *)0;
  302. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  303. while (i.next(a,p)) {
  304. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) {
  305. _savePeer(tPtr,*p);
  306. _peers.erase(*a);
  307. }
  308. }
  309. }
  310. {
  311. Mutex::Lock _l(_paths_m);
  312. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  313. Path::HashKey *k = (Path::HashKey *)0;
  314. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  315. while (i.next(k,p)) {
  316. if (p->references() <= 1)
  317. _paths.erase(*k);
  318. }
  319. }
  320. }
  321. void Topology::_memoizeUpstreams(void *tPtr)
  322. {
  323. // assumes _upstreams_m and _peers_m are locked
  324. _upstreamAddresses.clear();
  325. _amUpstream = false;
  326. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  327. const Identity &id = i->identity;
  328. if (id == RR->identity) {
  329. _amUpstream = true;
  330. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) == _upstreamAddresses.end()) {
  331. _upstreamAddresses.push_back(id.address());
  332. SharedPtr<Peer> &hp = _peers[id.address()];
  333. if (!hp) {
  334. hp = new Peer(RR,RR->identity,id);
  335. }
  336. }
  337. }
  338. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  339. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  340. if (i->identity == RR->identity) {
  341. _amUpstream = true;
  342. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  343. _upstreamAddresses.push_back(i->identity.address());
  344. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  345. if (!hp)
  346. hp = new Peer(RR,RR->identity,i->identity);
  347. }
  348. }
  349. }
  350. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  351. }
  352. void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
  353. {
  354. try {
  355. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  356. peer->serializeForCache(buf);
  357. uint64_t tmpid[2]; tmpid[0] = peer->address().toInt(); tmpid[1] = 0;
  358. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
  359. } catch ( ... ) {} // sanity check, discard invalid entries
  360. }
  361. } // namespace ZeroTier