Network.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if (controller() == _r->identity.address())
  102. throw std::runtime_error("configuration error: cannot add a network for which I am the netconf master");
  103. }
  104. Network::~Network()
  105. {
  106. }
  107. void Network::setConfiguration(const Network::Config &conf)
  108. {
  109. Mutex::Lock _l(_lock);
  110. if ((conf.networkId() == _id)&&(conf.peerAddress() == _r->identity.address())) { // sanity check
  111. TRACE("network %.16llx got netconf:\n%s",(unsigned long long)_id,conf.toString().c_str());
  112. _configuration = conf;
  113. _myCertificate = conf.certificateOfMembership();
  114. _lastConfigUpdate = Utils::now();
  115. }
  116. }
  117. void Network::requestConfiguration()
  118. {
  119. if (controller() == _r->identity.address()) {
  120. LOG("unable to request network configuration for network %.16llx: I am the network master, cannot query self",(unsigned long long)_id);
  121. return;
  122. }
  123. TRACE("requesting netconf for network %.16llx from netconf master %s",(unsigned long long)_id,controller().toString().c_str());
  124. Packet outp(controller(),_r->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
  125. outp.append((uint64_t)_id);
  126. _r->sw->send(outp,true);
  127. }
  128. bool Network::isAllowed(const Address &peer) const
  129. {
  130. // Exceptions can occur if we do not yet have *our* configuration.
  131. try {
  132. Mutex::Lock _l(_lock);
  133. if (_configuration.isOpen())
  134. return true;
  135. std::map<Address,Certificate>::const_iterator pc(_membershipCertificates.find(peer));
  136. if (pc == _membershipCertificates.end())
  137. return false;
  138. return _myCertificate.qualifyMembership(pc->second);
  139. } catch (std::exception &exc) {
  140. TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer.toString().c_str(),exc.what());
  141. } catch ( ... ) {
  142. TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer.toString().c_str());
  143. }
  144. return false;
  145. }
  146. void Network::clean()
  147. {
  148. Mutex::Lock _l(_lock);
  149. if (_configuration.isOpen())
  150. _membershipCertificates.clear();
  151. else {
  152. for(std::map<Address,Certificate>::iterator i=(_membershipCertificates.begin());i!=_membershipCertificates.end();) {
  153. if (_myCertificate.qualifyMembership(i->second))
  154. ++i;
  155. else _membershipCertificates.erase(i++);
  156. }
  157. }
  158. }
  159. void Network::_CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data)
  160. {
  161. const RuntimeEnvironment *_r = ((Network *)arg)->_r;
  162. try {
  163. _r->sw->onLocalEthernet(SharedPtr<Network>((Network *)arg),from,to,etherType,data);
  164. } catch (std::exception &exc) {
  165. TRACE("unexpected exception handling local packet: %s",exc.what());
  166. } catch ( ... ) {
  167. TRACE("unexpected exception handling local packet");
  168. }
  169. }
  170. } // namespace ZeroTier