Peer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Constants.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Trace.hpp"
  16. #include "Peer.hpp"
  17. #include "Topology.hpp"
  18. #include "SelfAwareness.hpp"
  19. #include "InetAddress.hpp"
  20. #include "Protocol.hpp"
  21. #include "Endpoint.hpp"
  22. #include "Expect.hpp"
  23. namespace ZeroTier {
  24. Peer::Peer(const RuntimeEnvironment *renv) :
  25. RR(renv),
  26. m_ephemeralPairTimestamp(0),
  27. m_lastReceive(0),
  28. m_lastSend(0),
  29. m_lastSentHello(),
  30. m_lastWhoisRequestReceived(0),
  31. m_lastEchoRequestReceived(0),
  32. m_lastPrioritizedPaths(0),
  33. m_lastProbeReceived(0),
  34. m_alivePathCount(0),
  35. m_tryQueue(),
  36. m_tryQueuePtr(m_tryQueue.end()),
  37. m_vProto(0),
  38. m_vMajor(0),
  39. m_vMinor(0),
  40. m_vRevision(0)
  41. {
  42. }
  43. Peer::~Peer()
  44. {
  45. Utils::burn(m_helloMacKey,sizeof(m_helloMacKey));
  46. }
  47. bool Peer::init(const Identity &peerIdentity)
  48. {
  49. RWMutex::Lock l(m_lock);
  50. if (m_id) // already initialized sanity check
  51. return false;
  52. m_id = peerIdentity;
  53. uint8_t k[ZT_SYMMETRIC_KEY_SIZE];
  54. if (!RR->identity.agree(peerIdentity,k))
  55. return false;
  56. m_identityKey.set(new SymmetricKey(RR->node->now(),k));
  57. Utils::burn(k,sizeof(k));
  58. m_deriveSecondaryIdentityKeys();
  59. return true;
  60. }
  61. void Peer::received(
  62. void *tPtr,
  63. const SharedPtr<Path> &path,
  64. const unsigned int hops,
  65. const uint64_t packetId,
  66. const unsigned int payloadLength,
  67. const Protocol::Verb verb,
  68. const Protocol::Verb inReVerb)
  69. {
  70. const int64_t now = RR->node->now();
  71. m_lastReceive = now;
  72. m_inMeter.log(now,payloadLength);
  73. if (hops == 0) {
  74. RWMutex::RMaybeWLock l(m_lock);
  75. // If this matches an existing path, skip path learning stuff. For the small number
  76. // of paths a peer will have linear scan is the fastest way to do lookup.
  77. for (unsigned int i=0;i < m_alivePathCount;++i) {
  78. if (m_paths[i] == path)
  79. return;
  80. }
  81. // If we made it here, we don't already know this path.
  82. if (RR->node->shouldUsePathForZeroTierTraffic(tPtr, m_id, path->localSocket(), path->address())) {
  83. // SECURITY: note that if we've made it here we expected this OK, see Expect.hpp.
  84. // There is replay protection in effect for OK responses.
  85. if (verb == Protocol::VERB_OK) {
  86. // If we're learning a new path convert the lock to an exclusive write lock.
  87. l.writing();
  88. // If the path list is full, replace the least recently active path. Otherwise append new path.
  89. unsigned int newPathIdx = 0;
  90. if (m_alivePathCount == ZT_MAX_PEER_NETWORK_PATHS) {
  91. int64_t lastReceiveTimeMax = 0;
  92. for (unsigned int i=0;i<m_alivePathCount;++i) {
  93. if ((m_paths[i]->address().family() == path->address().family()) &&
  94. (m_paths[i]->localSocket() == path->localSocket()) && // TODO: should be localInterface when multipath is integrated
  95. (m_paths[i]->address().ipsEqual2(path->address()))) {
  96. // Replace older path if everything is the same except the port number, since NAT/firewall reboots
  97. // and other wacky stuff can change port number assignments.
  98. m_paths[i] = path;
  99. return;
  100. } else if (m_paths[i]->lastIn() >= lastReceiveTimeMax) {
  101. lastReceiveTimeMax = m_paths[i]->lastIn();
  102. newPathIdx = i;
  103. }
  104. }
  105. } else {
  106. newPathIdx = m_alivePathCount++;
  107. }
  108. InetAddress old;
  109. if (m_paths[newPathIdx])
  110. old = m_paths[newPathIdx]->address();
  111. m_paths[newPathIdx] = path;
  112. // Re-prioritize paths to include the new one.
  113. m_prioritizePaths(now);
  114. RR->t->learnedNewPath(tPtr, 0x582fabdd, packetId, m_id, path->address(), old);
  115. } else {
  116. path->sent(now,hello(tPtr,path->localSocket(),path->address(),now));
  117. RR->t->tryingNewPath(tPtr, 0xb7747ddd, m_id, path->address(), path->address(), packetId, (uint8_t)verb, m_id);
  118. }
  119. }
  120. }
  121. }
  122. void Peer::send(void *tPtr,int64_t now,const void *data,unsigned int len) noexcept
  123. {
  124. SharedPtr<Path> via(this->path(now));
  125. if (via) {
  126. via->send(RR,tPtr,data,len,now);
  127. } else {
  128. const SharedPtr<Peer> root(RR->topology->root());
  129. if ((root)&&(root.ptr() != this)) {
  130. via = root->path(now);
  131. if (via) {
  132. via->send(RR,tPtr,data,len,now);
  133. root->relayed(now,len);
  134. } else {
  135. return;
  136. }
  137. } else {
  138. return;
  139. }
  140. }
  141. sent(now,len);
  142. }
  143. unsigned int Peer::hello(void *tPtr,int64_t localSocket,const InetAddress &atAddress,const int64_t now)
  144. {
  145. Buf outp;
  146. const uint64_t packetId = m_identityKey->nextMessage(RR->identity.address(),m_id.address());
  147. int ii = Protocol::newPacket(outp,packetId,m_id.address(),RR->identity.address(),Protocol::VERB_HELLO);
  148. outp.wI8(ii,ZT_PROTO_VERSION);
  149. outp.wI8(ii,ZEROTIER_VERSION_MAJOR);
  150. outp.wI8(ii,ZEROTIER_VERSION_MINOR);
  151. outp.wI16(ii,ZEROTIER_VERSION_REVISION);
  152. outp.wI64(ii,(uint64_t)now);
  153. outp.wO(ii,RR->identity);
  154. outp.wO(ii,atAddress);
  155. const int ivStart = ii;
  156. outp.wR(ii,12);
  157. // LEGACY: the six reserved bytes after the IV exist for legacy compatibility with v1.x nodes.
  158. // Once those are dead they'll become just reserved bytes for future use as flags etc.
  159. outp.wI32(ii,0); // reserved bytes
  160. void *const legacyMoonCountStart = outp.unsafeData + ii;
  161. outp.wI16(ii,0);
  162. const uint64_t legacySalsaIv = packetId & ZT_CONST_TO_BE_UINT64(0xfffffffffffffff8ULL);
  163. Salsa20(m_identityKey->secret,&legacySalsaIv).crypt12(legacyMoonCountStart,legacyMoonCountStart,2);
  164. const int cryptSectionStart = ii;
  165. FCV<uint8_t,4096> md;
  166. Dictionary::append(md,ZT_PROTO_HELLO_NODE_META_INSTANCE_ID,RR->instanceId);
  167. outp.wI16(ii,(uint16_t)md.size());
  168. outp.wB(ii,md.data(),(unsigned int)md.size());
  169. if (unlikely((ii + ZT_HMACSHA384_LEN) > ZT_BUF_SIZE)) // sanity check: should be impossible
  170. return 0;
  171. AES::CTR ctr(m_helloCipher);
  172. void *const cryptSection = outp.unsafeData + ii;
  173. ctr.init(outp.unsafeData + ivStart,0,cryptSection);
  174. ctr.crypt(cryptSection,ii - cryptSectionStart);
  175. ctr.finish();
  176. HMACSHA384(m_helloMacKey,outp.unsafeData,ii,outp.unsafeData + ii);
  177. ii += ZT_HMACSHA384_LEN;
  178. // LEGACY: we also need Poly1305 for v1.x peers.
  179. uint8_t polyKey[ZT_POLY1305_KEY_SIZE],perPacketKey[ZT_SALSA20_KEY_SIZE];
  180. Protocol::salsa2012DeriveKey(m_identityKey->secret,perPacketKey,outp,ii);
  181. Salsa20(perPacketKey,&packetId).crypt12(Utils::ZERO256,polyKey,sizeof(polyKey));
  182. Poly1305 p1305(polyKey);
  183. p1305.update(outp.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,ii - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START);
  184. uint64_t polyMac[2];
  185. p1305.finish(polyMac);
  186. Utils::storeAsIsEndian<uint64_t>(outp.unsafeData + ZT_PROTO_PACKET_MAC_INDEX,polyMac[0]);
  187. if (likely(RR->node->putPacket(tPtr,localSocket,atAddress,outp.unsafeData,ii)))
  188. return ii;
  189. return 0;
  190. }
  191. void Peer::pulse(void *const tPtr,const int64_t now,const bool isRoot)
  192. {
  193. RWMutex::Lock l(m_lock);
  194. bool needHello = false;
  195. if ( (m_vProto >= 11) && ( ((now - m_ephemeralPairTimestamp) >= (ZT_SYMMETRIC_KEY_TTL / 2)) || ((m_ephemeralKeys[0])&&(m_ephemeralKeys[0]->odometer() >= (ZT_SYMMETRIC_KEY_TTL_MESSAGES / 2))) ) ) {
  196. m_ephemeralPair.generate();
  197. needHello = true;
  198. } else if ((now - m_lastSentHello) >= ZT_PEER_HELLO_INTERVAL) {
  199. needHello = true;
  200. }
  201. if (m_tryQueue.empty()&&(m_alivePathCount == 0)) {
  202. InetAddress addr;
  203. if (RR->node->externalPathLookup(tPtr, m_id, -1, addr)) {
  204. if ((addr)&&(RR->node->shouldUsePathForZeroTierTraffic(tPtr, m_id, -1, addr))) {
  205. RR->t->tryingNewPath(tPtr, 0x84a10000, m_id, addr, InetAddress::NIL, 0, 0, Identity::NIL);
  206. sent(now,m_sendProbe(tPtr,-1,addr,nullptr,0,now));
  207. }
  208. }
  209. }
  210. m_prioritizePaths(now);
  211. if (!m_tryQueue.empty()) {
  212. for(int k=0;k<ZT_NAT_T_MAX_QUEUED_ATTEMPTS_PER_PULSE;++k) {
  213. // This is a global circular pointer that iterates through the list of
  214. // endpoints to attempt.
  215. if (m_tryQueuePtr == m_tryQueue.end()) {
  216. if (m_tryQueue.empty())
  217. break;
  218. m_tryQueuePtr = m_tryQueue.begin();
  219. }
  220. if (likely((now - m_tryQueuePtr->ts) < ZT_PATH_ALIVE_TIMEOUT)) {
  221. if (m_tryQueuePtr->target.isInetAddr()) {
  222. for(unsigned int i=0;i<m_alivePathCount;++i) {
  223. if (m_paths[i]->address().ipsEqual(m_tryQueuePtr->target.ip()))
  224. goto skip_tryQueue_item;
  225. }
  226. if ((m_alivePathCount == 0) && (m_tryQueuePtr->breakSymmetricBFG1024) && (RR->node->natMustDie())) {
  227. // Attempt aggressive NAT traversal if both requested and enabled. This sends a probe
  228. // to all ports under 1024, which assumes that the peer has bound to such a port and
  229. // has attempted to initiate a connection through it. This can traverse a decent number
  230. // of symmetric NATs at the cost of 32KiB per attempt and the potential to trigger IDS
  231. // systems by looking like a port scan (because it is).
  232. uint16_t ports[1023];
  233. for (unsigned int i=0;i<1023;++i)
  234. ports[i] = (uint64_t)(i + 1);
  235. for (unsigned int i=0;i<512;++i) {
  236. const uint64_t rn = Utils::random();
  237. const unsigned int a = (unsigned int)rn % 1023;
  238. const unsigned int b = (unsigned int)(rn >> 32U) % 1023;
  239. if (a != b) {
  240. const uint16_t tmp = ports[a];
  241. ports[a] = ports[b];
  242. ports[b] = tmp;
  243. }
  244. }
  245. sent(now,m_sendProbe(tPtr, -1, m_tryQueuePtr->target.ip(), ports, 1023, now));
  246. } else {
  247. sent(now,m_sendProbe(tPtr, -1, m_tryQueuePtr->target.ip(), nullptr, 0, now));
  248. }
  249. }
  250. }
  251. skip_tryQueue_item:
  252. m_tryQueue.erase(m_tryQueuePtr++);
  253. }
  254. }
  255. // Do keepalive on all currently active paths, sending HELLO to the first
  256. // if needHello is true and sending small keepalives to others.
  257. uint64_t randomJunk = Utils::random();
  258. for(unsigned int i=0;i<m_alivePathCount;++i) {
  259. if (needHello) {
  260. needHello = false;
  261. const unsigned int bytes = hello(tPtr, m_paths[i]->localSocket(), m_paths[i]->address(), now);
  262. m_paths[i]->sent(now, bytes);
  263. sent(now,bytes);
  264. m_lastSentHello = now;
  265. } else if ((now - m_paths[i]->lastOut()) >= ZT_PATH_KEEPALIVE_PERIOD) {
  266. m_paths[i]->send(RR, tPtr, reinterpret_cast<uint8_t *>(&randomJunk) + (i & 7U), 1, now);
  267. sent(now,1);
  268. }
  269. }
  270. // Send a HELLO indirectly if we were not able to send one via any direct path.
  271. if (needHello) {
  272. const SharedPtr<Peer> root(RR->topology->root());
  273. if (root) {
  274. const SharedPtr<Path> via(root->path(now));
  275. if (via) {
  276. const unsigned int bytes = hello(tPtr,via->localSocket(),via->address(),now);
  277. via->sent(now,bytes);
  278. root->relayed(now,bytes);
  279. sent(now,bytes);
  280. m_lastSentHello = now;
  281. }
  282. }
  283. }
  284. }
  285. void Peer::contact(void *tPtr,const int64_t now,const Endpoint &ep,const bool breakSymmetricBFG1024)
  286. {
  287. static uint8_t foo = 0;
  288. RWMutex::Lock l(m_lock);
  289. if (ep.isInetAddr()&&ep.ip().isV4()) {
  290. // For IPv4 addresses we send a tiny packet with a low TTL, which helps to
  291. // traverse some NAT types. It has no effect otherwise. It's important to
  292. // send this right away in case this is a coordinated attempt via RENDEZVOUS.
  293. RR->node->putPacket(tPtr,-1,ep.ip(),&foo,1,2);
  294. ++foo;
  295. }
  296. const bool wasEmpty = m_tryQueue.empty();
  297. if (!wasEmpty) {
  298. for(List<p_TryQueueItem>::iterator i(m_tryQueue.begin());i!=m_tryQueue.end();++i) {
  299. if (i->target == ep) {
  300. i->ts = now;
  301. i->breakSymmetricBFG1024 = breakSymmetricBFG1024;
  302. return;
  303. }
  304. }
  305. }
  306. #ifdef __CPP11__
  307. m_tryQueue.emplace_back(now, ep, breakSymmetricBFG1024);
  308. #else
  309. _tryQueue.push_back(_TryQueueItem(now,ep,breakSymmetricBFG1024));
  310. #endif
  311. if (wasEmpty)
  312. m_tryQueuePtr = m_tryQueue.begin();
  313. }
  314. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  315. {
  316. RWMutex::Lock l(m_lock);
  317. unsigned int pc = 0;
  318. for(unsigned int i=0;i<m_alivePathCount;++i) {
  319. if ((m_paths[i]) && ((m_paths[i]->address().family() == inetAddressFamily) && (m_paths[i]->address().ipScope() == scope))) {
  320. const unsigned int bytes = m_sendProbe(tPtr, m_paths[i]->localSocket(), m_paths[i]->address(), nullptr, 0, now);
  321. m_paths[i]->sent(now, bytes);
  322. sent(now,bytes);
  323. } else if (pc != i) {
  324. m_paths[pc++] = m_paths[i];
  325. }
  326. }
  327. m_alivePathCount = pc;
  328. while (pc < ZT_MAX_PEER_NETWORK_PATHS)
  329. m_paths[pc++].zero();
  330. }
  331. bool Peer::directlyConnected(int64_t now)
  332. {
  333. if ((now - m_lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  334. RWMutex::Lock l(m_lock);
  335. m_prioritizePaths(now);
  336. return m_alivePathCount > 0;
  337. } else {
  338. RWMutex::RLock l(m_lock);
  339. return m_alivePathCount > 0;
  340. }
  341. }
  342. void Peer::getAllPaths(Vector< SharedPtr<Path> > &paths)
  343. {
  344. RWMutex::RLock l(m_lock);
  345. paths.clear();
  346. paths.reserve(m_alivePathCount);
  347. paths.assign(m_paths, m_paths + m_alivePathCount);
  348. }
  349. void Peer::save(void *tPtr) const
  350. {
  351. uint8_t buf[8 + ZT_PEER_MARSHAL_SIZE_MAX];
  352. // Prefix each saved peer with the current timestamp.
  353. Utils::storeBigEndian<uint64_t>(buf,(uint64_t)RR->node->now());
  354. const int len = marshal(buf + 8);
  355. if (len > 0) {
  356. uint64_t id[2];
  357. id[0] = m_id.address().toInt();
  358. id[1] = 0;
  359. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,id,buf,(unsigned int)len + 8);
  360. }
  361. }
  362. int Peer::marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept
  363. {
  364. RWMutex::RLock l(m_lock);
  365. if (!m_identityKey)
  366. return -1;
  367. data[0] = 0; // serialized peer version
  368. // Include our identity's address to detect if this changes and require
  369. // recomputation of m_identityKey.
  370. RR->identity.address().copyTo(data + 1);
  371. // SECURITY: encryption in place is only to protect secrets if they are
  372. // cached to local storage. It's not used over the wire. Dumb ECB is fine
  373. // because secret keys are random and have no structure to reveal.
  374. RR->localCacheSymmetric.encrypt(m_identityKey->secret,data + 6);
  375. RR->localCacheSymmetric.encrypt(m_identityKey->secret + 22,data + 17);
  376. RR->localCacheSymmetric.encrypt(m_identityKey->secret + 38,data + 33);
  377. int p = 54;
  378. int s = m_id.marshal(data + p, false);
  379. if (s < 0)
  380. return -1;
  381. p += s;
  382. if (m_locator) {
  383. data[p++] = 1;
  384. s = m_locator->marshal(data + p);
  385. if (s <= 0)
  386. return s;
  387. p += s;
  388. } else {
  389. data[p++] = 0;
  390. }
  391. Utils::storeBigEndian(data + p,(uint16_t)m_vProto);
  392. p += 2;
  393. Utils::storeBigEndian(data + p,(uint16_t)m_vMajor);
  394. p += 2;
  395. Utils::storeBigEndian(data + p,(uint16_t)m_vMinor);
  396. p += 2;
  397. Utils::storeBigEndian(data + p,(uint16_t)m_vRevision);
  398. p += 2;
  399. data[p++] = 0;
  400. data[p++] = 0;
  401. return p;
  402. }
  403. int Peer::unmarshal(const uint8_t *restrict data,const int len) noexcept
  404. {
  405. RWMutex::Lock l(m_lock);
  406. if ((len <= 54) || (data[0] != 0))
  407. return -1;
  408. m_identityKey.zero();
  409. m_ephemeralKeys[0].zero();
  410. m_ephemeralKeys[1].zero();
  411. if (Address(data + 1) == RR->identity.address()) {
  412. uint8_t k[ZT_SYMMETRIC_KEY_SIZE];
  413. static_assert(ZT_SYMMETRIC_KEY_SIZE == 48,"marshal() and unmarshal() must be revisited if ZT_SYMMETRIC_KEY_SIZE is changed");
  414. RR->localCacheSymmetric.decrypt(data + 1,k);
  415. RR->localCacheSymmetric.decrypt(data + 17,k + 16);
  416. RR->localCacheSymmetric.decrypt(data + 33,k + 32);
  417. m_identityKey.set(new SymmetricKey(RR->node->now(),k));
  418. Utils::burn(k,sizeof(k));
  419. }
  420. int p = 49;
  421. int s = m_id.unmarshal(data + 38, len - 38);
  422. if (s < 0)
  423. return s;
  424. p += s;
  425. if (!m_identityKey) {
  426. uint8_t k[ZT_SYMMETRIC_KEY_SIZE];
  427. if (!RR->identity.agree(m_id,k))
  428. return -1;
  429. m_identityKey.set(new SymmetricKey(RR->node->now(),k));
  430. Utils::burn(k,sizeof(k));
  431. }
  432. if (data[p] == 0) {
  433. ++p;
  434. m_locator.zero();
  435. } else if (data[p] == 1) {
  436. ++p;
  437. Locator *const loc = new Locator();
  438. s = loc->unmarshal(data + p, len - p);
  439. m_locator.set(loc);
  440. if (s < 0)
  441. return s;
  442. p += s;
  443. } else {
  444. return -1;
  445. }
  446. if ((p + 10) > len)
  447. return -1;
  448. m_vProto = Utils::loadBigEndian<uint16_t>(data + p); p += 2;
  449. m_vMajor = Utils::loadBigEndian<uint16_t>(data + p); p += 2;
  450. m_vMinor = Utils::loadBigEndian<uint16_t>(data + p); p += 2;
  451. m_vRevision = Utils::loadBigEndian<uint16_t>(data + p); p += 2;
  452. p += 2 + (int)Utils::loadBigEndian<uint16_t>(data + p);
  453. m_deriveSecondaryIdentityKeys();
  454. return (p > len) ? -1 : p;
  455. }
  456. struct _PathPriorityComparisonOperator
  457. {
  458. ZT_INLINE bool operator()(const SharedPtr<Path> &a,const SharedPtr<Path> &b) const noexcept
  459. {
  460. // Sort in descending order of most recent receive time.
  461. return (a->lastIn() > b->lastIn());
  462. }
  463. };
  464. void Peer::m_prioritizePaths(int64_t now)
  465. {
  466. // assumes _lock is locked for writing
  467. m_lastPrioritizedPaths = now;
  468. if (m_alivePathCount > 0) {
  469. // Sort paths in descending order of priority.
  470. std::sort(m_paths, m_paths + m_alivePathCount, _PathPriorityComparisonOperator());
  471. // Let go of paths that have expired.
  472. for (unsigned int i = 0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  473. if ((!m_paths[i]) || (!m_paths[i]->alive(now))) {
  474. m_alivePathCount = i;
  475. for (;i < ZT_MAX_PEER_NETWORK_PATHS;++i)
  476. m_paths[i].zero();
  477. break;
  478. }
  479. }
  480. }
  481. }
  482. unsigned int Peer::m_sendProbe(void *tPtr,int64_t localSocket,const InetAddress &atAddress,const uint16_t *ports,const unsigned int numPorts,int64_t now)
  483. {
  484. // Assumes m_lock is locked
  485. const SharedPtr<SymmetricKey> k(m_key());
  486. const uint64_t packetId = k->nextMessage(RR->identity.address(),m_id.address());
  487. uint8_t p[ZT_PROTO_MIN_PACKET_LENGTH + 1];
  488. Utils::storeAsIsEndian<uint64_t>(p + ZT_PROTO_PACKET_ID_INDEX,packetId);
  489. m_id.address().copyTo(p + ZT_PROTO_PACKET_DESTINATION_INDEX);
  490. RR->identity.address().copyTo(p + ZT_PROTO_PACKET_SOURCE_INDEX);
  491. p[ZT_PROTO_PACKET_FLAGS_INDEX] = 0;
  492. p[ZT_PROTO_PACKET_VERB_INDEX] = Protocol::VERB_ECHO;
  493. p[ZT_PROTO_PACKET_VERB_INDEX + 1] = 0; // arbitrary payload
  494. Protocol::armor(p,ZT_PROTO_MIN_PACKET_LENGTH + 1,k,cipher());
  495. RR->expect->sending(packetId,now);
  496. if (numPorts > 0) {
  497. InetAddress tmp(atAddress);
  498. for(unsigned int i=0;i<numPorts;++i) {
  499. tmp.setPort(ports[i]);
  500. RR->node->putPacket(tPtr,-1,tmp,p,ZT_PROTO_MIN_PACKET_LENGTH + 1);
  501. }
  502. return ZT_PROTO_MIN_PACKET_LENGTH * numPorts;
  503. } else {
  504. RR->node->putPacket(tPtr,-1,atAddress,p,ZT_PROTO_MIN_PACKET_LENGTH + 1);
  505. return ZT_PROTO_MIN_PACKET_LENGTH;
  506. }
  507. }
  508. void Peer::m_deriveSecondaryIdentityKeys() noexcept
  509. {
  510. uint8_t hk[ZT_SYMMETRIC_KEY_SIZE];
  511. KBKDFHMACSHA384(m_identityKey->secret,ZT_KBKDF_LABEL_HELLO_DICTIONARY_ENCRYPT,0,0,hk);
  512. m_helloCipher.init(hk);
  513. Utils::burn(hk,sizeof(hk));
  514. KBKDFHMACSHA384(m_identityKey->secret,ZT_KBKDF_LABEL_PACKET_HMAC,0,0,m_helloMacKey);
  515. }
  516. } // namespace ZeroTier