SelfAwareness.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "Constants.hpp"
  31. #include "SelfAwareness.hpp"
  32. #include "RuntimeEnvironment.hpp"
  33. #include "Node.hpp"
  34. #include "Topology.hpp"
  35. #include "Packet.hpp"
  36. #include "Peer.hpp"
  37. namespace ZeroTier {
  38. class _ResetWithinScope
  39. {
  40. public:
  41. _ResetWithinScope(const RuntimeEnvironment *renv,uint64_t now,InetAddress::IpScope scope) :
  42. RR(renv),
  43. _now(now),
  44. _scope(scope) {}
  45. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  46. {
  47. if (p->resetWithinScope(RR,_scope,_now))
  48. peersReset.push_back(p);
  49. }
  50. std::vector< SharedPtr<Peer> > peersReset;
  51. private:
  52. const RuntimeEnvironment *RR;
  53. uint64_t _now;
  54. InetAddress::IpScope _scope;
  55. };
  56. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  57. RR(renv)
  58. {
  59. memset(_lastPhysicalAddress,0,sizeof(_lastPhysicalAddress));
  60. }
  61. SelfAwareness::~SelfAwareness()
  62. {
  63. }
  64. void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted)
  65. {
  66. // This code depends on the numeric values assigned to scopes in InetAddress.hpp
  67. const unsigned int scope = (unsigned int)myPhysicalAddress.ipScope();
  68. if ((scope > 0)&&(scope < (unsigned int)InetAddress::IP_SCOPE_LOOPBACK)) {
  69. if ( (!trusted) && ((scope == (unsigned int)InetAddress::IP_SCOPE_GLOBAL)||(scope != (unsigned int)reporterPhysicalAddress.ipScope())) ) {
  70. /* For now only trusted peers are permitted to inform us of changes to
  71. * our global Internet IP or to changes of NATed IPs. We'll let peers on
  72. * private, shared, or link-local networks inform us of changes as long
  73. * as they too are at the same scope. This discrimination avoids a DoS
  74. * attack in which an attacker could force us to reset our connections. */
  75. return;
  76. } else {
  77. Mutex::Lock _l(_lock);
  78. InetAddress &lastPhy = _lastPhysicalAddress[scope - 1];
  79. if (!lastPhy) {
  80. TRACE("learned physical address %s for scope %u from reporter %s(%s) (replaced <null>)",myPhysicalAddress.toString().c_str(),scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str());
  81. lastPhy = myPhysicalAddress;
  82. } else if (lastPhy != myPhysicalAddress) {
  83. TRACE("learned physical address %s for scope %u from reporter %s(%s) (replaced %s, resetting within scope)",myPhysicalAddress.toString().c_str(),scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),lastPhy.toString().c_str());
  84. lastPhy = myPhysicalAddress;
  85. uint64_t now = RR->node->now();
  86. _ResetWithinScope rset(RR,now,(InetAddress::IpScope)scope);
  87. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  88. // For all peers for whom we forgot an address, send a packet indirectly if
  89. // they are still considered alive so that we will re-establish direct links.
  90. SharedPtr<Peer> sn(RR->topology->getBestSupernode());
  91. if (sn) {
  92. Path *snp = sn->getBestPath(now);
  93. if (snp) {
  94. for(std::vector< SharedPtr<Peer> >::const_iterator p(rset.peersReset.begin());p!=rset.peersReset.end();++p) {
  95. if ((*p)->alive(now)) {
  96. TRACE("sending indirect NOP to %s via %s(%s) to re-establish link",(*p)->address().toString().c_str(),sn->address().toString().c_str(),snp->address().toString().c_str());
  97. Packet outp((*p)->address(),RR->identity.address(),Packet::VERB_NOP);
  98. outp.armor((*p)->key(),true);
  99. snp->send(RR,outp.data(),outp.size(),now);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. } // namespace ZeroTier