NodeConfig.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <memory>
  32. #include <string>
  33. #include <openssl/sha.h>
  34. #include "NodeConfig.hpp"
  35. #include "RuntimeEnvironment.hpp"
  36. #include "Defaults.hpp"
  37. #include "Utils.hpp"
  38. #include "Logger.hpp"
  39. #include "Topology.hpp"
  40. #include "Demarc.hpp"
  41. #include "InetAddress.hpp"
  42. #include "Peer.hpp"
  43. #include "Salsa20.hpp"
  44. #include "HMAC.hpp"
  45. namespace ZeroTier {
  46. NodeConfig::NodeConfig(const RuntimeEnvironment *renv,const char *authToken)
  47. throw(std::runtime_error) :
  48. _r(renv),
  49. _authToken(authToken),
  50. _controlSocket(true,ZT_CONTROL_UDP_PORT,false,&_CBcontrolPacketHandler,this)
  51. {
  52. SHA256_CTX sha;
  53. SHA256_Init(&sha);
  54. SHA256_Update(&sha,_authToken.data(),_authToken.length());
  55. SHA256_Final(_keys,&sha); // first 32 bytes of keys[]: Salsa20 key
  56. SHA256_Init(&sha);
  57. SHA256_Update(&sha,_keys,32);
  58. SHA256_Update(&sha,_authToken.data(),_authToken.length());
  59. SHA256_Final(_keys + 32,&sha); // second 32 bytes of keys[]: HMAC key
  60. }
  61. NodeConfig::~NodeConfig()
  62. {
  63. }
  64. void NodeConfig::whackAllTaps()
  65. {
  66. std::vector< SharedPtr<Network> > nwlist;
  67. Mutex::Lock _l(_networks_m);
  68. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  69. n->second->tap().whack();
  70. }
  71. // Macro used in execute()
  72. #undef _P
  73. #define _P(f,...) { r.push_back(std::string()); Utils::stdsprintf(r.back(),(f),##__VA_ARGS__); }
  74. // Used with Topology::eachPeer to dump peer stats
  75. class _DumpPeerStatistics
  76. {
  77. public:
  78. _DumpPeerStatistics(std::vector<std::string> &out) :
  79. r(out),
  80. _now(Utils::now())
  81. {
  82. }
  83. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  84. {
  85. InetAddress v4(p->ipv4ActivePath(_now));
  86. InetAddress v6(p->ipv6ActivePath(_now));
  87. _P("200 listpeers %s %s %s %u",
  88. p->address().toString().c_str(),
  89. ((v4) ? v4.toString().c_str() : "(none)"),
  90. ((v6) ? v6.toString().c_str() : "(none)"),
  91. (((v4)||(v6)) ? p->latency() : 0));
  92. }
  93. private:
  94. std::vector<std::string> &r;
  95. uint64_t _now;
  96. };
  97. std::vector<std::string> NodeConfig::execute(const char *command)
  98. {
  99. std::vector<std::string> r;
  100. std::vector<std::string> cmd(Utils::split(command,"\r\n \t","\\","'"));
  101. //
  102. // Not coincidentally, response type codes correspond with HTTP
  103. // status codes.
  104. //
  105. if ((cmd.empty())||(cmd[0] == "help")) {
  106. _P("200 help help");
  107. _P("200 help listpeers");
  108. _P("200 help listnetworks");
  109. _P("200 help join <network ID> [<network invitation code>]");
  110. _P("200 help leave <network ID>");
  111. } else if (cmd[0] == "listpeers") {
  112. _r->topology->eachPeer(_DumpPeerStatistics(r));
  113. } else if (cmd[0] == "listnetworks") {
  114. Mutex::Lock _l(_networks_m);
  115. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator nw(_networks.begin());nw!=_networks.end();++nw) {
  116. _P("200 listnetworks %llu %s %s",
  117. nw->first,
  118. nw->second->tap().deviceName().c_str(),
  119. (nw->second->open() ? "public" : "private"));
  120. }
  121. } else if (cmd[0] == "join") {
  122. _P("404 join Not implemented yet.");
  123. } else if (cmd[0] == "leave") {
  124. _P("404 leave Not implemented yet.");
  125. } else {
  126. _P("404 %s No such command. Use 'help' for help.",cmd[0].c_str());
  127. }
  128. return r;
  129. }
  130. void NodeConfig::_CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
  131. {
  132. char hmacKey[32];
  133. char hmac[32];
  134. char buf[131072];
  135. NodeConfig *nc = (NodeConfig *)arg;
  136. const RuntimeEnvironment *_r = nc->_r;
  137. try {
  138. // Minimum length
  139. if (len < 28)
  140. return;
  141. if (len >= sizeof(buf)) // only up to len - 28 bytes are used on receive/decrypt
  142. return;
  143. // Compare first 16 bytes of HMAC, which is after IV in packet
  144. memcpy(hmacKey,nc->_keys + 32,32);
  145. *((uint64_t *)hmacKey) ^= *((const uint64_t *)data); // include IV in HMAC
  146. HMAC::sha256(hmacKey,32,((const unsigned char *)data) + 28,len - 28,hmac);
  147. if (memcmp(hmac,((const unsigned char *)data) + 8,16))
  148. return;
  149. // Decrypt payload if we passed HMAC
  150. Salsa20 s20(nc->_keys,256,data); // first 64 bits of data are IV
  151. s20.decrypt(((const unsigned char *)data) + 28,buf,len - 28);
  152. // Null-terminate string for execute()
  153. buf[len - 28] = (char)0;
  154. // Execute command
  155. std::vector<std::string> r(nc->execute(buf));
  156. // Result packet contains a series of null-terminated results
  157. unsigned int resultLen = 28;
  158. for(std::vector<std::string>::iterator i(r.begin());i!=r.end();++i) {
  159. if ((resultLen + i->length() + 1) >= sizeof(buf))
  160. return; // result too long
  161. memcpy(buf + resultLen,i->c_str(),i->length() + 1);
  162. resultLen += i->length() + 1;
  163. }
  164. // Generate result packet IV
  165. Utils::getSecureRandom(buf,8);
  166. // Generate result packet HMAC
  167. memcpy(hmacKey,nc->_keys + 32,32);
  168. *((uint64_t *)hmacKey) ^= *((const uint64_t *)buf); // include IV in HMAC
  169. HMAC::sha256(hmacKey,32,((const unsigned char *)buf) + 28,resultLen - 28,hmac);
  170. memcpy(buf + 8,hmac,16);
  171. // Copy arbitrary tag from original packet
  172. memcpy(buf + 24,((const unsigned char *)data) + 24,4);
  173. // Send encrypted result back to requester
  174. sock->send(remoteAddr,buf,resultLen,-1);
  175. } catch ( ... ) {
  176. TRACE("unexpected exception parsing control packet or generating response");
  177. }
  178. }
  179. } // namespace ZeroTier