NodeConfig.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. _controlSocket(true,ZT_CONTROL_UDP_PORT,false,&_CBcontrolPacketHandler,this)
  50. {
  51. SHA256_CTX sha;
  52. SHA256_Init(&sha);
  53. SHA256_Update(&sha,authToken,strlen(authToken));
  54. SHA256_Final(_controlSocketKey,&sha);
  55. }
  56. NodeConfig::~NodeConfig()
  57. {
  58. }
  59. void NodeConfig::whackAllTaps()
  60. {
  61. std::vector< SharedPtr<Network> > nwlist;
  62. Mutex::Lock _l(_networks_m);
  63. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  64. n->second->tap().whack();
  65. }
  66. // Macro used in execute()
  67. #undef _P
  68. #define _P(f,...) { r.push_back(std::string()); Utils::stdsprintf(r.back(),(f),##__VA_ARGS__); }
  69. // Used with Topology::eachPeer to dump peer stats
  70. class _DumpPeerStatistics
  71. {
  72. public:
  73. _DumpPeerStatistics(std::vector<std::string> &out) :
  74. r(out),
  75. _now(Utils::now())
  76. {
  77. }
  78. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  79. {
  80. InetAddress v4(p->ipv4ActivePath(_now));
  81. InetAddress v6(p->ipv6ActivePath(_now));
  82. _P("200 listpeers %s %s %s %u",
  83. p->address().toString().c_str(),
  84. ((v4) ? v4.toString().c_str() : "(none)"),
  85. ((v6) ? v6.toString().c_str() : "(none)"),
  86. (((v4)||(v6)) ? p->latency() : 0));
  87. }
  88. private:
  89. std::vector<std::string> &r;
  90. uint64_t _now;
  91. };
  92. std::vector<std::string> NodeConfig::execute(const char *command)
  93. {
  94. std::vector<std::string> r;
  95. std::vector<std::string> cmd(Utils::split(command,"\r\n \t","\\","'"));
  96. //
  97. // Not coincidentally, response type codes correspond with HTTP
  98. // status codes.
  99. //
  100. if ((cmd.empty())||(cmd[0] == "help")) {
  101. _P("200 help help");
  102. _P("200 help listpeers");
  103. _P("200 help listnetworks");
  104. _P("200 help join <network ID> [<network invitation code>]");
  105. _P("200 help leave <network ID>");
  106. } else if (cmd[0] == "listpeers") {
  107. _r->topology->eachPeer(_DumpPeerStatistics(r));
  108. } else if (cmd[0] == "listnetworks") {
  109. Mutex::Lock _l(_networks_m);
  110. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator nw(_networks.begin());nw!=_networks.end();++nw) {
  111. _P("200 listnetworks %llu %s %s",
  112. nw->first,
  113. nw->second->tap().deviceName().c_str(),
  114. (nw->second->open() ? "public" : "private"));
  115. }
  116. } else if (cmd[0] == "join") {
  117. _P("404 join Not implemented yet.");
  118. } else if (cmd[0] == "leave") {
  119. _P("404 leave Not implemented yet.");
  120. } else {
  121. _P("404 %s No such command. Use 'help' for help.",cmd[0].c_str());
  122. }
  123. return r;
  124. }
  125. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > NodeConfig::encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload)
  126. throw(std::out_of_range)
  127. {
  128. char hmac[32];
  129. char keytmp[32];
  130. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets;
  131. Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> packet;
  132. packet.setSize(16); // HMAC and IV
  133. packet.append((uint32_t)(conversationId & 0xffffffff));
  134. for(unsigned int i=0;i<payload.size();++i) {
  135. packet.append(payload[i]); // will throw if too big
  136. packet.append((unsigned char)0);
  137. if (((i + 1) >= payload.size())||((packet.size() + payload[i + 1].length() + 1) >= packet.capacity())) {
  138. Utils::getSecureRandom(packet.field(8,8),8);
  139. Salsa20 s20(key,256,packet.field(8,8));
  140. s20.encrypt(packet.field(16,packet.size() - 16),packet.field(16,packet.size() - 16),packet.size() - 16);
  141. memcpy(keytmp,key,32);
  142. for(unsigned int i=0;i<32;++i)
  143. keytmp[i] ^= 0x77; // use a different permutation of key for HMAC than for Salsa20
  144. HMAC::sha256(keytmp,32,packet.field(16,packet.size() - 16),packet.size() - 16,hmac);
  145. memcpy(packet.field(0,8),hmac,8);
  146. packets.push_back(packet);
  147. packet.setSize(16); // HMAC and IV
  148. packet.append((uint32_t)(conversationId & 0xffffffff));
  149. }
  150. }
  151. return packets;
  152. }
  153. bool NodeConfig::decodeControlMessagePacket(const void *key,const void *data,unsigned int len,unsigned long &conversationId,std::vector<std::string> &payload)
  154. {
  155. char hmac[32];
  156. char keytmp[32];
  157. try {
  158. if (len < 20)
  159. return false;
  160. Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> packet(data,len);
  161. memcpy(keytmp,key,32);
  162. for(unsigned int i=0;i<32;++i)
  163. keytmp[i] ^= 0x77; // use a different permutation of key for HMAC than for Salsa20
  164. HMAC::sha256(keytmp,32,packet.field(16,packet.size() - 16),packet.size() - 16,hmac);
  165. if (memcmp(packet.field(0,8),hmac,8))
  166. return false;
  167. Salsa20 s20(key,256,packet.field(8,8));
  168. s20.decrypt(packet.field(16,packet.size() - 16),packet.field(16,packet.size() - 16),packet.size() - 16);
  169. conversationId = packet.at<uint32_t>(16);
  170. const char *pl = ((const char *)packet.data()) + 20;
  171. unsigned int pll = packet.size() - 20;
  172. for(unsigned int i=0;i<pll;) {
  173. unsigned int eos = i;
  174. while ((eos < pll)&&(pl[eos]))
  175. ++eos;
  176. if (eos > i) {
  177. payload.push_back(std::string(pl + i,eos - i));
  178. i = eos + 1;
  179. } else break;
  180. }
  181. return true;
  182. } catch ( ... ) {
  183. return false;
  184. }
  185. }
  186. void NodeConfig::_CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
  187. {
  188. }
  189. } // namespace ZeroTier