Network.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. But make it work first, then make
  55. // it fast.
  56. for(const_iterator myField(begin());myField!=end();++myField) {
  57. if (!((myField->first.length() > 1)&&(myField->first[0] == '~'))) { // ~fields are max delta range specs
  58. // If they lack the same field, comparison fails.
  59. const_iterator theirField(mc.find(myField->first));
  60. if (theirField == mc.end())
  61. return false;
  62. const_iterator deltaField(find(_DELTA_PREFIX + myField->first));
  63. if (deltaField == end()) {
  64. // If there is no delta, compare on simple equality
  65. if (myField->second != theirField->second)
  66. return false;
  67. } else {
  68. // Otherwise compare range with max delta. Presence of a dot in delta
  69. // indicates a floating point comparison. Otherwise an integer
  70. // comparison occurs.
  71. if (deltaField->second.find('.') != std::string::npos) {
  72. double my = strtod(myField->second.c_str(),(char **)0);
  73. double their = strtod(theirField->second.c_str(),(char **)0);
  74. double delta = strtod(deltaField->second.c_str(),(char **)0);
  75. if (fabs(my - their) > delta)
  76. return false;
  77. } else {
  78. int64_t my = strtoll(myField->second.c_str(),(char **)0,10);
  79. int64_t their = strtoll(theirField->second.c_str(),(char **)0,10);
  80. int64_t delta = strtoll(deltaField->second.c_str(),(char **)0,10);
  81. if (my > their) {
  82. if ((my - their) > delta)
  83. return false;
  84. } else {
  85. if ((their - my) > delta)
  86. return false;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. Network::Network(const RuntimeEnvironment *renv,uint64_t id)
  95. throw(std::runtime_error) :
  96. _r(renv),
  97. _tap(renv,renv->identity.address().toMAC(),ZT_IF_MTU,&_CBhandleTapData,this),
  98. _id(id),
  99. _lastConfigUpdate(0)
  100. {
  101. }
  102. Network::~Network()
  103. {
  104. }
  105. void Network::setConfiguration(const Network::Config &conf)
  106. {
  107. Mutex::Lock _l(_lock);
  108. if ((conf.networkId() == _id)&&(conf.peerAddress() == _r->identity.address())) { // sanity check
  109. _configuration = conf;
  110. _myCertificate = conf.certificateOfMembership();
  111. _lastConfigUpdate = Utils::now();
  112. }
  113. }
  114. void Network::requestConfiguration()
  115. {
  116. Packet outp(controller(),_r->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
  117. outp.append((uint64_t)_id);
  118. _r->sw->send(outp,true);
  119. }
  120. bool Network::isAllowed(const Address &peer) const
  121. {
  122. // Exceptions can occur if we do not yet have *our* configuration.
  123. try {
  124. Mutex::Lock _l(_lock);
  125. if (_configuration.isOpen())
  126. return true;
  127. std::map<Address,Certificate>::const_iterator pc(_membershipCertificates.find(peer));
  128. if (pc == _membershipCertificates.end())
  129. return false;
  130. return _myCertificate.qualifyMembership(pc->second);
  131. } catch (std::exception &exc) {
  132. TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer.toString().c_str(),exc.what());
  133. } catch ( ... ) {
  134. TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer.toString().c_str());
  135. }
  136. return false;
  137. }
  138. void Network::clean()
  139. {
  140. Mutex::Lock _l(_lock);
  141. if (_configuration.isOpen())
  142. _membershipCertificates.clear();
  143. else {
  144. for(std::map<Address,Certificate>::iterator i=(_membershipCertificates.begin());i!=_membershipCertificates.end();) {
  145. if (_myCertificate.qualifyMembership(i->second))
  146. ++i;
  147. else _membershipCertificates.erase(i++);
  148. }
  149. }
  150. }
  151. void Network::_CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data)
  152. {
  153. const RuntimeEnvironment *_r = ((Network *)arg)->_r;
  154. try {
  155. _r->sw->onLocalEthernet(SharedPtr<Network>((Network *)arg),from,to,etherType,data);
  156. } catch (std::exception &exc) {
  157. TRACE("unexpected exception handling local packet: %s",exc.what());
  158. } catch ( ... ) {
  159. TRACE("unexpected exception handling local packet");
  160. }
  161. }
  162. } // namespace ZeroTier