SelfAwareness.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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 <string.h>
  29. #include <set>
  30. #include <vector>
  31. #include "Constants.hpp"
  32. #include "SelfAwareness.hpp"
  33. #include "RuntimeEnvironment.hpp"
  34. #include "Node.hpp"
  35. #include "Topology.hpp"
  36. #include "Packet.hpp"
  37. #include "Peer.hpp"
  38. #include "Switch.hpp"
  39. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  40. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 600000
  41. namespace ZeroTier {
  42. class _ResetWithinScope
  43. {
  44. public:
  45. _ResetWithinScope(void *tPtr,uint64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  46. _now(now),
  47. _tPtr(tPtr),
  48. _family(inetAddressFamily),
  49. _scope(scope) {}
  50. inline void operator()(Topology &t,const SharedPtr<Peer> &p) { p->resetWithinScope(_tPtr,_scope,_family,_now); }
  51. private:
  52. uint64_t _now;
  53. void *_tPtr;
  54. int _family;
  55. InetAddress::IpScope _scope;
  56. };
  57. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  58. RR(renv),
  59. _phy(128)
  60. {
  61. }
  62. void SelfAwareness::iam(void *tPtr,const Address &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now)
  63. {
  64. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  65. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  66. return;
  67. Mutex::Lock _l(_phy_m);
  68. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  69. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  70. // Changes to external surface reported by trusted peers causes path reset in this scope
  71. TRACE("physical address %s for scope %u as seen from %s(%s) differs from %s, resetting paths in scope",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),entry.mySurface.toString().c_str());
  72. entry.mySurface = myPhysicalAddress;
  73. entry.ts = now;
  74. entry.trusted = trusted;
  75. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  76. // due to multiple reports of endpoint change.
  77. // Don't use 'entry' after this since hash table gets modified.
  78. {
  79. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  80. PhySurfaceKey *k = (PhySurfaceKey *)0;
  81. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  82. while (i.next(k,e)) {
  83. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  84. _phy.erase(*k);
  85. }
  86. }
  87. // Reset all paths within this scope and address family
  88. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.ss_family,(InetAddress::IpScope)scope);
  89. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  90. } else {
  91. // Otherwise just update DB to use to determine external surface info
  92. entry.mySurface = myPhysicalAddress;
  93. entry.ts = now;
  94. entry.trusted = trusted;
  95. }
  96. }
  97. void SelfAwareness::clean(uint64_t now)
  98. {
  99. Mutex::Lock _l(_phy_m);
  100. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  101. PhySurfaceKey *k = (PhySurfaceKey *)0;
  102. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  103. while (i.next(k,e)) {
  104. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  105. _phy.erase(*k);
  106. }
  107. }
  108. std::vector<InetAddress> SelfAwareness::getSymmetricNatPredictions()
  109. {
  110. /* This is based on ideas and strategies found here:
  111. * https://tools.ietf.org/html/draft-takeda-symmetric-nat-traversal-00
  112. *
  113. * For each IP address reported by a trusted (upstream) peer, we find
  114. * the external port most recently reported by ANY peer for that IP.
  115. *
  116. * We only do any of this for global IPv4 addresses since private IPs
  117. * and IPv6 are not going to have symmetric NAT.
  118. *
  119. * SECURITY NOTE:
  120. *
  121. * We never use IPs reported by non-trusted peers, since this could lead
  122. * to a minor vulnerability whereby a peer could poison our cache with
  123. * bad external surface reports via OK(HELLO) and then possibly coax us
  124. * into suggesting their IP to other peers via PUSH_DIRECT_PATHS. This
  125. * in turn could allow them to MITM flows.
  126. *
  127. * Since flows are encrypted and authenticated they could not actually
  128. * read or modify traffic, but they could gather meta-data for forensics
  129. * purpsoes or use this as a DOS attack vector. */
  130. std::map< uint32_t,std::pair<uint64_t,unsigned int> > maxPortByIp;
  131. InetAddress theOneTrueSurface;
  132. bool symmetric = false;
  133. {
  134. Mutex::Lock _l(_phy_m);
  135. { // First get IPs from only trusted peers, and perform basic NAT type characterization
  136. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  137. PhySurfaceKey *k = (PhySurfaceKey *)0;
  138. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  139. while (i.next(k,e)) {
  140. if ((e->trusted)&&(e->mySurface.ss_family == AF_INET)&&(e->mySurface.ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  141. if (!theOneTrueSurface)
  142. theOneTrueSurface = e->mySurface;
  143. else if (theOneTrueSurface != e->mySurface)
  144. symmetric = true;
  145. maxPortByIp[reinterpret_cast<const struct sockaddr_in *>(&(e->mySurface))->sin_addr.s_addr] = std::pair<uint64_t,unsigned int>(e->ts,e->mySurface.port());
  146. }
  147. }
  148. }
  149. { // Then find max port per IP from a trusted peer
  150. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  151. PhySurfaceKey *k = (PhySurfaceKey *)0;
  152. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  153. while (i.next(k,e)) {
  154. if ((e->mySurface.ss_family == AF_INET)&&(e->mySurface.ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  155. std::map< uint32_t,std::pair<uint64_t,unsigned int> >::iterator mp(maxPortByIp.find(reinterpret_cast<const struct sockaddr_in *>(&(e->mySurface))->sin_addr.s_addr));
  156. if ((mp != maxPortByIp.end())&&(mp->second.first < e->ts)) {
  157. mp->second.first = e->ts;
  158. mp->second.second = e->mySurface.port();
  159. }
  160. }
  161. }
  162. }
  163. }
  164. if (symmetric) {
  165. std::vector<InetAddress> r;
  166. for(unsigned int k=1;k<=3;++k) {
  167. for(std::map< uint32_t,std::pair<uint64_t,unsigned int> >::iterator i(maxPortByIp.begin());i!=maxPortByIp.end();++i) {
  168. unsigned int p = i->second.second + k;
  169. if (p > 65535) p -= 64511;
  170. InetAddress pred(&(i->first),4,p);
  171. if (std::find(r.begin(),r.end(),pred) == r.end())
  172. r.push_back(pred);
  173. }
  174. }
  175. return r;
  176. }
  177. return std::vector<InetAddress>();
  178. }
  179. } // namespace ZeroTier