SelfAwareness.cpp 7.4 KB

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