SelfAwareness.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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) { p->resetWithinScope(RR,_scope,_now); }
  46. private:
  47. const RuntimeEnvironment *RR;
  48. uint64_t _now;
  49. InetAddress::IpScope _scope;
  50. };
  51. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  52. RR(renv)
  53. {
  54. memset(_lastPhysicalAddress,0,sizeof(_lastPhysicalAddress));
  55. }
  56. SelfAwareness::~SelfAwareness()
  57. {
  58. }
  59. void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted)
  60. {
  61. // This code depends on the numeric values assigned to scopes in InetAddress.hpp
  62. const unsigned int scope = (unsigned int)myPhysicalAddress.ipScope();
  63. if ((scope > 0)&&(scope < (unsigned int)InetAddress::IP_SCOPE_LOOPBACK)) {
  64. if ( (!trusted) && ((scope == (unsigned int)InetAddress::IP_SCOPE_GLOBAL)||(scope != (unsigned int)reporterPhysicalAddress.ipScope())) ) {
  65. /* For now only trusted peers are permitted to inform us of changes to
  66. * our global Internet IP or to changes of NATed IPs. We'll let peers on
  67. * private, shared, or link-local networks inform us of changes as long
  68. * as they too are at the same scope. This discrimination avoids a DoS
  69. * attack in which an attacker could force us to reset our connections. */
  70. return;
  71. } else {
  72. Mutex::Lock _l(_lock);
  73. InetAddress &lastPhy = _lastPhysicalAddress[scope - 1];
  74. if (!lastPhy) {
  75. 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());
  76. lastPhy = myPhysicalAddress;
  77. } else if (lastPhy != myPhysicalAddress) {
  78. 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());
  79. lastPhy = myPhysicalAddress;
  80. _ResetWithinScope rset(RR,RR->node->now(),(InetAddress::IpScope)scope);
  81. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  82. }
  83. }
  84. }
  85. }
  86. } // namespace ZeroTier