SelfAwareness.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "Constants.hpp"
  22. #include "SelfAwareness.hpp"
  23. #include "RuntimeEnvironment.hpp"
  24. #include "Node.hpp"
  25. #include "Topology.hpp"
  26. #include "Packet.hpp"
  27. #include "Peer.hpp"
  28. #include "Switch.hpp"
  29. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  30. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 3600000
  31. namespace ZeroTier {
  32. class _ResetWithinScope
  33. {
  34. public:
  35. _ResetWithinScope(uint64_t now,InetAddress::IpScope scope) :
  36. _now(now),
  37. _scope(scope) {}
  38. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  39. {
  40. if (p->resetWithinScope(_scope,_now))
  41. peersReset.push_back(p);
  42. }
  43. std::vector< SharedPtr<Peer> > peersReset;
  44. private:
  45. uint64_t _now;
  46. InetAddress::IpScope _scope;
  47. };
  48. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  49. RR(renv),
  50. _phy(32)
  51. {
  52. }
  53. SelfAwareness::~SelfAwareness()
  54. {
  55. }
  56. void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now)
  57. {
  58. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  59. // This would be weird, e.g. a public IP talking to 10.0.0.1, so just ignore it.
  60. // If your network is this weird it's probably not reliable information.
  61. if (scope != reporterPhysicalAddress.ipScope())
  62. return;
  63. // Some scopes we ignore, and global scope IPs are only used for this
  64. // mechanism if they come from someone we trust (e.g. a root).
  65. switch(scope) {
  66. case InetAddress::IP_SCOPE_NONE:
  67. case InetAddress::IP_SCOPE_LOOPBACK:
  68. case InetAddress::IP_SCOPE_MULTICAST:
  69. return;
  70. case InetAddress::IP_SCOPE_GLOBAL:
  71. if (!trusted)
  72. return;
  73. break;
  74. default:
  75. break;
  76. }
  77. Mutex::Lock _l(_phy_m);
  78. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,reporterPhysicalAddress,scope)];
  79. if ( ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  80. entry.mySurface = myPhysicalAddress;
  81. entry.ts = now;
  82. 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());
  83. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  84. // due to multiple reports of endpoint change.
  85. // Don't use 'entry' after this since hash table gets modified.
  86. {
  87. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  88. PhySurfaceKey *k = (PhySurfaceKey *)0;
  89. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  90. while (i.next(k,e)) {
  91. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  92. _phy.erase(*k);
  93. }
  94. }
  95. // Reset all paths within this scope
  96. _ResetWithinScope rset(now,(InetAddress::IpScope)scope);
  97. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  98. // Send a NOP to all peers for whom we forgot a path. This will cause direct
  99. // links to be re-established if possible, possibly using a root server or some
  100. // other relay.
  101. for(std::vector< SharedPtr<Peer> >::const_iterator p(rset.peersReset.begin());p!=rset.peersReset.end();++p) {
  102. if ((*p)->activelyTransferringFrames(now)) {
  103. Packet outp((*p)->address(),RR->identity.address(),Packet::VERB_NOP);
  104. RR->sw->send(outp,true,0);
  105. }
  106. }
  107. } else {
  108. entry.mySurface = myPhysicalAddress;
  109. entry.ts = now;
  110. }
  111. }
  112. void SelfAwareness::clean(uint64_t now)
  113. {
  114. Mutex::Lock _l(_phy_m);
  115. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  116. PhySurfaceKey *k = (PhySurfaceKey *)0;
  117. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  118. while (i.next(k,e)) {
  119. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  120. _phy.erase(*k);
  121. }
  122. }
  123. } // namespace ZeroTier