SelfAwareness.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <cstdlib>
  14. #include <cstring>
  15. #include <set>
  16. #include "Constants.hpp"
  17. #include "SelfAwareness.hpp"
  18. #include "RuntimeEnvironment.hpp"
  19. #include "Topology.hpp"
  20. #include "Peer.hpp"
  21. #include "Switch.hpp"
  22. #include "Trace.hpp"
  23. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  24. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 600000
  25. namespace ZeroTier {
  26. class _ResetWithinScope
  27. {
  28. public:
  29. ZT_ALWAYS_INLINE _ResetWithinScope(void *tPtr,int64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  30. _now(now),
  31. _tPtr(tPtr),
  32. _family(inetAddressFamily),
  33. _scope(scope) {}
  34. ZT_ALWAYS_INLINE void operator()(const SharedPtr<Peer> &p) { p->resetWithinScope(_tPtr,_scope,_family,_now); }
  35. private:
  36. int64_t _now;
  37. void *_tPtr;
  38. int _family;
  39. InetAddress::IpScope _scope;
  40. };
  41. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  42. RR(renv),
  43. _phy(256)
  44. {
  45. }
  46. SelfAwareness::~SelfAwareness()
  47. {
  48. }
  49. void SelfAwareness::iam(void *tPtr,const Address &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  50. {
  51. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  52. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  53. return;
  54. Mutex::Lock l(_phy_l);
  55. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  56. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  57. // Changes to external surface reported by trusted peers causes path reset in this scope
  58. RR->t->resettingPathsInScope(tPtr,reporter,reporterPhysicalAddress,myPhysicalAddress,scope);
  59. entry.mySurface = myPhysicalAddress;
  60. entry.ts = now;
  61. entry.trusted = trusted;
  62. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  63. // due to multiple reports of endpoint change.
  64. // Don't use 'entry' after this since hash table gets modified.
  65. {
  66. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  67. PhySurfaceKey *k = (PhySurfaceKey *)0;
  68. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  69. while (i.next(k,e)) {
  70. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  71. _phy.erase(*k);
  72. }
  73. }
  74. // Reset all paths within this scope and address family
  75. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.ss_family,(InetAddress::IpScope)scope);
  76. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  77. } else {
  78. // Otherwise just update DB to use to determine external surface info
  79. entry.mySurface = myPhysicalAddress;
  80. entry.ts = now;
  81. entry.trusted = trusted;
  82. }
  83. }
  84. void SelfAwareness::clean(int64_t now)
  85. {
  86. Mutex::Lock l(_phy_l);
  87. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  88. PhySurfaceKey *k = nullptr;
  89. PhySurfaceEntry *e = nullptr;
  90. while (i.next(k,e)) {
  91. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  92. _phy.erase(*k);
  93. }
  94. }
  95. bool SelfAwareness::symmetricNat(const int64_t now) const
  96. {
  97. Hashtable< InetAddress,std::pair< std::set<int>,std::set<int64_t> > > ipToPortsAndLocalSockets(16);
  98. {
  99. Mutex::Lock l(_phy_l);
  100. Hashtable<PhySurfaceKey,PhySurfaceEntry>::Iterator i(const_cast<SelfAwareness *>(this)->_phy);
  101. PhySurfaceKey *k = nullptr;
  102. PhySurfaceEntry *e = nullptr;
  103. while (i.next(k,e)) {
  104. if ((now - e->ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) {
  105. std::pair< std::set<int>,std::set<int64_t> > &ii = ipToPortsAndLocalSockets[e->mySurface.ipOnly()];
  106. ii.first.insert(e->mySurface.port());
  107. if (k->receivedOnLocalSocket != -1)
  108. ii.second.insert(k->receivedOnLocalSocket);
  109. }
  110. }
  111. }
  112. Hashtable< InetAddress,std::pair< std::set<int>,std::set<int64_t> > >::Iterator i(ipToPortsAndLocalSockets);
  113. InetAddress *k = nullptr;
  114. std::pair< std::set<int>,std::set<int64_t> > *v = nullptr;
  115. while (i.next(k,v)) {
  116. if (v->first.size() > v->second.size()) // more external ports than local sockets for a given external IP
  117. return true;
  118. }
  119. return false;
  120. }
  121. std::multimap<unsigned long,InetAddress> SelfAwareness::externalAddresses(const int64_t now) const
  122. {
  123. std::multimap<unsigned long,InetAddress> r;
  124. Hashtable<InetAddress,unsigned long> counts(16);
  125. {
  126. Mutex::Lock l(_phy_l);
  127. Hashtable<PhySurfaceKey,PhySurfaceEntry>::Iterator i(const_cast<SelfAwareness *>(this)->_phy);
  128. PhySurfaceKey *k = nullptr;
  129. PhySurfaceEntry *e = nullptr;
  130. while (i.next(k,e)) {
  131. if ((now - e->ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  132. ++counts[e->mySurface];
  133. }
  134. }
  135. Hashtable<InetAddress,unsigned long>::Iterator i(counts);
  136. InetAddress *k = nullptr;
  137. unsigned long *c = nullptr;
  138. while (i.next(k,c))
  139. r.insert(std::pair<unsigned long,InetAddress>(*c,*k));
  140. return r;
  141. }
  142. } // namespace ZeroTier