Network.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 <stdlib.h>
  28. #include <math.h>
  29. #include <openssl/sha.h>
  30. #include "RuntimeEnvironment.hpp"
  31. #include "NodeConfig.hpp"
  32. #include "Network.hpp"
  33. #include "Switch.hpp"
  34. #include "Packet.hpp"
  35. namespace ZeroTier {
  36. void Network::Certificate::_shaForSignature(unsigned char *dig) const
  37. {
  38. SHA256_CTX sha;
  39. SHA256_Init(&sha);
  40. unsigned char zero = 0;
  41. for(const_iterator i(begin());i!=end();++i) {
  42. SHA256_Update(&sha,&zero,1);
  43. SHA256_Update(&sha,(const unsigned char *)i->first.data(),i->first.length());
  44. SHA256_Update(&sha,&zero,1);
  45. SHA256_Update(&sha,(const unsigned char *)i->second.data(),i->second.length());
  46. SHA256_Update(&sha,&zero,1);
  47. }
  48. SHA256_Final(dig,&sha);
  49. }
  50. static const std::string _DELTA_PREFIX("~");
  51. bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) const
  52. {
  53. // Note: optimization probably needed here, probably via some kind of
  54. // memoization / dynamic programming.
  55. for(const_iterator myField(begin());myField!=end();++myField) {
  56. if (!((myField->first.length() > 1)&&(myField->first[0] == '~'))) { // ~fields are max delta range specs
  57. // If they lack the same field, comparison fails.
  58. const_iterator theirField(mc.find(myField->first));
  59. if (theirField == mc.end())
  60. return false;
  61. const_iterator deltaField(find(_DELTA_PREFIX + myField->first));
  62. if (deltaField == end()) {
  63. // If there is no delta, compare on simple equality
  64. if (myField->second != theirField->second)
  65. return false;
  66. } else {
  67. // Otherwise compare range with max delta. Presence of a dot in delta
  68. // indicates a floating point comparison. Otherwise an integer
  69. // comparison occurs.
  70. if (deltaField->second.find('.') != std::string::npos) {
  71. double my = strtod(myField->second.c_str(),(char **)0);
  72. double their = strtod(theirField->second.c_str(),(char **)0);
  73. double delta = strtod(deltaField->second.c_str(),(char **)0);
  74. if (fabs(my - their) > delta)
  75. return false;
  76. } else {
  77. int64_t my = strtoll(myField->second.c_str(),(char **)0,10);
  78. int64_t their = strtoll(theirField->second.c_str(),(char **)0,10);
  79. int64_t delta = strtoll(deltaField->second.c_str(),(char **)0,10);
  80. if (my > their) {
  81. if ((my - their) > delta)
  82. return false;
  83. } else {
  84. if ((their - my) > delta)
  85. return false;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. Network::Network(const RuntimeEnvironment *renv,uint64_t id)
  94. throw(std::runtime_error) :
  95. _r(renv),
  96. _tap(renv,renv->identity.address().toMAC(),ZT_IF_MTU,&_CBhandleTapData,this),
  97. _lastConfigUpdate(0),
  98. _id(id)
  99. {
  100. }
  101. Network::~Network()
  102. {
  103. }
  104. void Network::setConfiguration(const Network::Config &conf)
  105. {
  106. Mutex::Lock _l(_lock);
  107. if ((conf.networkId() == _id)&&(conf.peerAddress() == _r->identity.address())) { // sanity check
  108. _configuration = conf;
  109. _myCertificate = conf.certificateOfMembership();
  110. _lastConfigUpdate = Utils::now();
  111. }
  112. }
  113. void Network::requestConfiguration()
  114. {
  115. Packet outp(controller(),_r->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
  116. outp.append((uint64_t)_id);
  117. _r->sw->send(outp,true);
  118. }
  119. bool Network::isAllowed(const Address &peer) const
  120. {
  121. // Exceptions can occur if we do not yet have *our* configuration.
  122. try {
  123. Mutex::Lock _l(_lock);
  124. if (_configuration.isOpen())
  125. return true;
  126. std::map<Address,Certificate>::const_iterator pc(_membershipCertificates.find(peer));
  127. if (pc == _membershipCertificates.end())
  128. return false;
  129. return _myCertificate.qualifyMembership(pc->second);
  130. } catch (std::exception &exc) {
  131. TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer.toString().c_str(),exc.what());
  132. return false;
  133. } catch ( ... ) {
  134. TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer.toString().c_str());
  135. return false;
  136. }
  137. }
  138. void Network::clean()
  139. {
  140. Mutex::Lock _l(_lock);
  141. for(std::map<Address,Certificate>::iterator i=(_membershipCertificates.begin());i!=_membershipCertificates.end();) {
  142. if (_myCertificate.qualifyMembership(i->second))
  143. ++i;
  144. else _membershipCertificates.erase(i++);
  145. }
  146. }
  147. void Network::_CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data)
  148. {
  149. const RuntimeEnvironment *_r = ((Network *)arg)->_r;
  150. try {
  151. _r->sw->onLocalEthernet(SharedPtr<Network>((Network *)arg),from,to,etherType,data);
  152. } catch (std::exception &exc) {
  153. TRACE("unexpected exception handling local packet: %s",exc.what());
  154. } catch ( ... ) {
  155. TRACE("unexpected exception handling local packet");
  156. }
  157. }
  158. } // namespace ZeroTier