SelfAwareness.cpp 6.9 KB

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