root.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "../node/Constants.hpp"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. #include <sys/time.h>
  38. #include <sys/un.h>
  39. #include <sys/ioctl.h>
  40. #include <arpa/inet.h>
  41. #include <netinet/in.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/ip6.h>
  44. #include <netinet/tcp.h>
  45. #include "../node/Packet.hpp"
  46. #include "../node/Utils.hpp"
  47. #include "../node/Address.hpp"
  48. #include "../node/Identity.hpp"
  49. #include "../node/InetAddress.hpp"
  50. #include "../node/Mutex.hpp"
  51. #include "../node/SharedPtr.hpp"
  52. #include "../osdep/OSUtils.hpp"
  53. #include <string>
  54. #include <thread>
  55. #include <map>
  56. #include <set>
  57. #include <vector>
  58. #include <iostream>
  59. #include <unordered_map>
  60. #include <vector>
  61. #include <atomic>
  62. #include <mutex>
  63. using namespace ZeroTier;
  64. struct PeerInfo
  65. {
  66. Identity id;
  67. uint8_t key[32];
  68. InetAddress ip4,ip6;
  69. int64_t lastReceive;
  70. AtomicCounter __refCount;
  71. ZT_ALWAYS_INLINE ~PeerInfo() { Utils::burn(key,sizeof(key)); }
  72. };
  73. struct IdentityHasher { ZT_ALWAYS_INLINE std::size_t operator()(const Identity &id) const { return (std::size_t)id.hashCode(); } };
  74. struct AddressHasher { ZT_ALWAYS_INLINE std::size_t operator()(const Address &a) const { return (std::size_t)a.toInt(); } };
  75. struct InetAddressHasher { ZT_ALWAYS_INLINE std::size_t operator()(const InetAddress &ip) const { return (std::size_t)ip.hashCode(); } };
  76. static Identity self;
  77. static std::atomic_bool run;
  78. static std::unordered_map< Identity,SharedPtr<PeerInfo>,IdentityHasher > peersByIdentity;
  79. static std::unordered_map< Address,std::set< SharedPtr<PeerInfo> >,AddressHasher > peersByVirtAddr;
  80. static std::unordered_map< InetAddress,std::set< SharedPtr<PeerInfo> >,InetAddressHasher > peersByPhysAddr;
  81. static std::mutex peersByIdentity_l;
  82. static std::mutex peersByVirtAddr_l;
  83. static std::mutex peersByPhysAddr_l;
  84. static void handlePacket(const InetAddress *const ip,const Packet *const inpkt)
  85. {
  86. Packet pkt(*inpkt);
  87. char ipstr[128],ipstr2[128],astr[32];
  88. const bool fragment = pkt[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] == ZT_PACKET_FRAGMENT_INDICATOR;
  89. // See if this is destined for us and isn't a fragment / fragmented. (No packets
  90. // understood by the root are fragments/fragmented.)
  91. if ((!fragment)&&(!pkt.fragmented())&&(pkt.destination() == self.address())) {
  92. SharedPtr<PeerInfo> peer;
  93. // If this is an un-encrypted HELLO, either learn a new peer or verify
  94. // that this is a peer we already know.
  95. if ((pkt.cipher() == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE)&&(pkt.verb() == Packet::VERB_HELLO)) {
  96. Identity id;
  97. if (id.deserialize(pkt,ZT_PROTO_VERB_HELLO_IDX_IDENTITY)) {
  98. {
  99. std::lock_guard<std::mutex> pbi_l(peersByIdentity_l);
  100. auto pById = peersByIdentity.find(id);
  101. if (pById != peersByIdentity.end()) {
  102. peer = pById->second;
  103. }
  104. }
  105. if (peer) {
  106. if (!pkt.dearmor(peer->key)) {
  107. printf("%s HELLO rejected: packet authentication failed" ZT_EOL_S,ip->toString(ipstr));
  108. return;
  109. }
  110. } else {
  111. peer.set(new PeerInfo);
  112. if (id.agree(self,peer->key)) {
  113. if (pkt.dearmor(peer->key)) {
  114. if (id.locallyValidate()) {
  115. peer->id = id;
  116. {
  117. std::lock_guard<std::mutex> pbi_l(peersByIdentity_l);
  118. peersByIdentity.emplace(id,peer);
  119. }
  120. } else {
  121. printf("%s HELLO rejected: invalid identity (locallyValidate() failed)" ZT_EOL_S,ip->toString(ipstr));
  122. return;
  123. }
  124. } else {
  125. printf("%s HELLO rejected: packet authentication failed" ZT_EOL_S,ip->toString(ipstr));
  126. return;
  127. }
  128. } else {
  129. printf("%s HELLO rejected: key agreement failed" ZT_EOL_S,ip->toString(ipstr));
  130. return;
  131. }
  132. }
  133. }
  134. }
  135. // If it wasn't a HELLO, check to see if any known identities for the sender's
  136. // short ZT address successfully decrypt the packet.
  137. if (!peer) {
  138. std::lock_guard<std::mutex> pbv_l(peersByVirtAddr_l);
  139. auto peers = peersByVirtAddr.find(pkt.source());
  140. if (peers != peersByVirtAddr.end()) {
  141. for(auto p=peers->second.begin();p!=peers->second.end();++p) {
  142. if (pkt.dearmor((*p)->key)) {
  143. peer = (*p);
  144. break;
  145. } else {
  146. pkt = *inpkt; // dearmor() destroys contents of pkt
  147. }
  148. }
  149. }
  150. }
  151. // If we found the peer, update IP and/or time.
  152. if (peer) {
  153. InetAddress *const peerIp = (ip->ss_family == AF_INET) ? &(peer->ip4) : &(peer->ip6);
  154. if (*peerIp != ip) {
  155. std::lock_guard<std::mutex> pbp_l(peersByPhysAddr_l);
  156. if (*peerIp) {
  157. auto prev = peersByPhysAddr.find(*peerIp);
  158. if (prev != peersByPhysAddr.end()) {
  159. prev->second.erase(peer);
  160. if (prev->second.empty())
  161. peersByPhysAddr.erase(prev);
  162. }
  163. }
  164. *peerIp = ip;
  165. peersByPhysAddr[ip].emplace(peer);
  166. }
  167. peer->lastReceive = OSUtils::now();
  168. printf("%s has %s" ZT_EOL_S,ip->toString(ipstr),pkt.source().toString(astr));
  169. return;
  170. }
  171. }
  172. std::vector<InetAddress> toAddrs;
  173. {
  174. std::lock_guard<std::mutex> pbv_l(peersByVirtAddr_l);
  175. auto peers = peersByVirtAddr.find(inpkt->destination());
  176. if (peers != peersByVirtAddr.end()) {
  177. for(auto p=peers->second.begin();p!=peers->second.end();++p) {
  178. if ((*p)->ip6)
  179. toAddrs.push_back((*p)->ip6);
  180. else if ((*p)->ip4)
  181. toAddrs.push_back((*p)->ip4);
  182. }
  183. }
  184. }
  185. if (toAddrs.empty()) {
  186. printf("%s not forwarding to %s: no destinations found" ZT_EOL_S,ip->toString(ipstr),pkt.destination().toString(astr));
  187. return;
  188. }
  189. if (fragment) {
  190. if (reinterpret_cast<Packet::Fragment *>(&pkt)->incrementHops() >= ZT_PROTO_MAX_HOPS) {
  191. printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),pkt.destination().toString(astr));
  192. return;
  193. }
  194. } else {
  195. if (pkt.incrementHops() >= ZT_PROTO_MAX_HOPS) {
  196. printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),pkt.destination().toString(astr));
  197. return;
  198. }
  199. }
  200. for(auto i=toAddrs.begin();i!=toAddrs.end();++i) {
  201. printf("%s -> %s for %s" ZT_EOL_S,ip->toString(ipstr),i->toString(ipstr2),pkt.destination().toString(astr));
  202. }
  203. }
  204. static int bindSocket(struct sockaddr *bindAddr)
  205. {
  206. int s = socket(bindAddr->sa_family,SOCK_DGRAM,0);
  207. if (s < 0) {
  208. close(s);
  209. return -1;
  210. }
  211. int f = 131072;
  212. setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)&f,sizeof(f));
  213. f = 131072;
  214. setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)&f,sizeof(f));
  215. if (bindAddr->sa_family == AF_INET6) {
  216. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  217. #ifdef IPV6_MTU_DISCOVER
  218. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  219. #endif
  220. #ifdef IPV6_DONTFRAG
  221. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,&f,sizeof(f));
  222. #endif
  223. }
  224. f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  225. f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEPORT,(void *)&f,sizeof(f));
  226. f = 1; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&f,sizeof(f));
  227. #ifdef IP_DONTFRAG
  228. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  229. #endif
  230. #ifdef IP_MTU_DISCOVER
  231. f = IP_PMTUDISC_DONT; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  232. #endif
  233. #ifdef SO_NO_CHECK
  234. if (bindAddr->sa_family == AF_INET) {
  235. f = 1; setsockopt(s,SOL_SOCKET,SO_NO_CHECK,(void *)&f,sizeof(f));
  236. }
  237. #endif
  238. if (bind(s,bindAddr,(bindAddr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))) {
  239. close(s);
  240. return -1;
  241. }
  242. return s;
  243. }
  244. int main(int argc,char **argv)
  245. {
  246. if (argc != 2) {
  247. printf("Usage: zerotier-root <identity.secret> [<port>]" ZT_EOL_S);
  248. return 1;
  249. }
  250. std::string myIdStr;
  251. if (!OSUtils::readFile(argv[1],myIdStr)) {
  252. printf("FATAL: cannot read identity.secret at %s" ZT_EOL_S,argv[1]);
  253. return 1;
  254. }
  255. if (!self.fromString(myIdStr.c_str())) {
  256. printf("FATAL: cannot read identity.secret at %s (invalid identity)" ZT_EOL_S,argv[1]);
  257. return 1;
  258. }
  259. if (!self.hasPrivate()) {
  260. printf("FATAL: cannot read identity.secret at %s (missing secret key)" ZT_EOL_S,argv[1]);
  261. return 1;
  262. }
  263. unsigned int ncores = std::thread::hardware_concurrency();
  264. if (ncores == 0) ncores = 1;
  265. run = true;
  266. std::vector<int> sockets;
  267. std::vector<std::thread> threads;
  268. for(unsigned int tn=0;tn<ncores;++tn) {
  269. struct sockaddr_in6 in6;
  270. memset(&in6,0,sizeof(in6));
  271. in6.sin6_family = AF_INET6;
  272. in6.sin6_port = htons(ZT_DEFAULT_PORT);
  273. const int s6 = bindSocket((struct sockaddr *)&in6);
  274. if (s6 < 0) {
  275. std::cout << "ERROR: unable to bind to port " << ZT_DEFAULT_PORT << ZT_EOL_S;
  276. exit(1);
  277. }
  278. struct sockaddr_in in4;
  279. memset(&in4,0,sizeof(in4));
  280. in4.sin_family = AF_INET;
  281. in4.sin_port = htons(ZT_DEFAULT_PORT);
  282. const int s4 = bindSocket((struct sockaddr *)&in4);
  283. if (s4 < 0) {
  284. std::cout << "ERROR: unable to bind to port " << ZT_DEFAULT_PORT << ZT_EOL_S;
  285. exit(1);
  286. }
  287. sockets.push_back(s6);
  288. sockets.push_back(s4);
  289. threads.push_back(std::thread([s6]() {
  290. struct sockaddr_in6 in6;
  291. Packet pkt;
  292. memset(&in6,0,sizeof(in6));
  293. for(;;) {
  294. socklen_t sl = sizeof(in6);
  295. const int pl = (int)recvfrom(s6,pkt.unsafeData(),pkt.capacity(),0,(struct sockaddr *)&in6,&sl);
  296. if (pl > 0) {
  297. try {
  298. pkt.setSize((unsigned int)pl);
  299. handlePacket(reinterpret_cast<const InetAddress *>(&in6),&pkt);
  300. } catch ( ... ) {
  301. }
  302. } else {
  303. break;
  304. }
  305. }
  306. }));
  307. threads.push_back(std::thread([s4]() {
  308. struct sockaddr_in in4;
  309. Packet pkt;
  310. memset(&in4,0,sizeof(in4));
  311. for(;;) {
  312. socklen_t sl = sizeof(in4);
  313. const int pl = (int)recvfrom(s4,pkt.unsafeData(),pkt.capacity(),0,(struct sockaddr *)&in4,&sl);
  314. if (pl > 0) {
  315. try {
  316. pkt.setSize((unsigned int)pl);
  317. handlePacket(reinterpret_cast<const InetAddress *>(&in4),&pkt);
  318. } catch ( ... ) {
  319. }
  320. } else {
  321. break;
  322. }
  323. }
  324. }));
  325. }
  326. while (run) {
  327. sleep(1);
  328. }
  329. return 0;
  330. }