Binder.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. #ifndef ZT_BINDER_HPP
  27. #define ZT_BINDER_HPP
  28. #include "../node/Constants.hpp"
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #ifdef __WINDOWS__
  34. #include <WinSock2.h>
  35. #include <Windows.h>
  36. #include <ShlObj.h>
  37. #include <netioapi.h>
  38. #include <iphlpapi.h>
  39. #else
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/wait.h>
  43. #include <unistd.h>
  44. #include <ifaddrs.h>
  45. #ifdef __LINUX__
  46. #include <sys/ioctl.h>
  47. #include <net/if.h>
  48. #endif
  49. #endif
  50. #include <string>
  51. #include <vector>
  52. #include <algorithm>
  53. #include <utility>
  54. #include <map>
  55. #include "../node/NonCopyable.hpp"
  56. #include "../node/InetAddress.hpp"
  57. #include "../node/Mutex.hpp"
  58. #include "../node/Utils.hpp"
  59. #include "Phy.hpp"
  60. #include "OSUtils.hpp"
  61. /**
  62. * Period between binder rescans/refreshes
  63. *
  64. * OneService also does this on detected restarts.
  65. */
  66. #define ZT_BINDER_REFRESH_PERIOD 30000
  67. namespace ZeroTier {
  68. /**
  69. * Enumerates local devices and binds to all potential ZeroTier path endpoints
  70. *
  71. * This replaces binding to wildcard (0.0.0.0 and ::0) with explicit binding
  72. * as part of the path to default gateway support. Under the hood it uses
  73. * different queries on different OSes to enumerate devices, and also exposes
  74. * device enumeration and endpoint IP data for use elsewhere.
  75. *
  76. * On OSes that do not support local port enumeration or where this is not
  77. * meaningful, this degrades to binding to wildcard.
  78. */
  79. class Binder : NonCopyable
  80. {
  81. private:
  82. struct _Binding
  83. {
  84. _Binding() :
  85. udpSock((PhySocket *)0),
  86. tcpListenSock((PhySocket *)0),
  87. address() {}
  88. PhySocket *udpSock;
  89. PhySocket *tcpListenSock;
  90. InetAddress address;
  91. };
  92. public:
  93. Binder() {}
  94. /**
  95. * Close all bound ports
  96. *
  97. * This should be called on shutdown. It closes listen sockets and UDP ports
  98. * but not TCP connections from any TCP listen sockets.
  99. *
  100. * @param phy Physical interface
  101. */
  102. template<typename PHY_HANDLER_TYPE>
  103. void closeAll(Phy<PHY_HANDLER_TYPE> &phy)
  104. {
  105. Mutex::Lock _l(_lock);
  106. for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
  107. phy.close(i->udpSock,false);
  108. phy.close(i->tcpListenSock,false);
  109. }
  110. }
  111. /**
  112. * Scan local devices and addresses and rebind TCP and UDP
  113. *
  114. * This should be called after wake from sleep, on detected network device
  115. * changes, on startup, or periodically (e.g. every 30-60s).
  116. *
  117. * @param phy Physical interface
  118. * @param port Port to bind to on all interfaces (TCP and UDP)
  119. * @param ignoreInterfacesByName Ignore these interfaces by name
  120. * @param ignoreInterfacesByNamePrefix Ignore these interfaces by name-prefix (starts-with, e.g. zt ignores zt*)
  121. * @param ignoreInterfacesByAddress Ignore these interfaces by address
  122. * @tparam PHY_HANDLER_TYPE Type for Phy<> template
  123. * @tparam INTERFACE_CHECKER Type for class containing shouldBindInterface() method
  124. */
  125. template<typename PHY_HANDLER_TYPE,typename INTERFACE_CHECKER>
  126. void refresh(Phy<PHY_HANDLER_TYPE> &phy,unsigned int port,INTERFACE_CHECKER &ifChecker)
  127. {
  128. std::map<InetAddress,std::string> localIfAddrs;
  129. PhySocket *udps;
  130. //PhySocket *tcps;
  131. Mutex::Lock _l(_lock);
  132. #ifdef __WINDOWS__
  133. char aabuf[32768];
  134. ULONG aalen = sizeof(aabuf);
  135. if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
  136. PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
  137. while (a) {
  138. PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
  139. while (ua) {
  140. InetAddress ip(ua->Address.lpSockaddr);
  141. if (ifChecker.shouldBindInterface("",ip)) {
  142. switch(ip.ipScope()) {
  143. default: break;
  144. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  145. case InetAddress::IP_SCOPE_GLOBAL:
  146. case InetAddress::IP_SCOPE_SHARED:
  147. case InetAddress::IP_SCOPE_PRIVATE:
  148. ip.setPort(port);
  149. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string()));
  150. break;
  151. }
  152. }
  153. ua = ua->Next;
  154. }
  155. a = a->Next;
  156. }
  157. }
  158. #else // not __WINDOWS__
  159. /* On Linux we use an alternative method if available since getifaddrs()
  160. * gets very slow when there are lots of network namespaces. This won't
  161. * work unless /proc/PID/net/if_inet6 exists and it may not on some
  162. * embedded systems, so revert to getifaddrs() there. */
  163. #ifdef __LINUX__
  164. char fn[256],tmp[256];
  165. std::set<std::string> ifnames;
  166. const unsigned long pid = (unsigned long)getpid();
  167. // Get all device names
  168. Utils::snprintf(fn,sizeof(fn),"/proc/%lu/net/dev",pid);
  169. FILE *procf = fopen(fn,"r");
  170. if (procf) {
  171. while (fgets(tmp,sizeof(tmp),procf)) {
  172. tmp[255] = 0;
  173. char *saveptr = (char *)0;
  174. for(char *f=Utils::stok(tmp," \t\r\n:|",&saveptr);(f);f=Utils::stok((char *)0," \t\r\n:|",&saveptr)) {
  175. if ((strcmp(f,"Inter-") != 0)&&(strcmp(f,"face") != 0)&&(f[0] != 0))
  176. ifnames.insert(f);
  177. break; // we only want the first field
  178. }
  179. }
  180. fclose(procf);
  181. }
  182. // Get IPv6 addresses (and any device names we don't already know)
  183. Utils::snprintf(fn,sizeof(fn),"/proc/%lu/net/if_inet6",pid);
  184. procf = fopen(fn,"r");
  185. if (procf) {
  186. while (fgets(tmp,sizeof(tmp),procf)) {
  187. tmp[255] = 0;
  188. char *saveptr = (char *)0;
  189. unsigned char ipbits[16];
  190. memset(ipbits,0,sizeof(ipbits));
  191. char *devname = (char *)0;
  192. int n = 0;
  193. for(char *f=Utils::stok(tmp," \t\r\n",&saveptr);(f);f=Utils::stok((char *)0," \t\r\n",&saveptr)) {
  194. switch(n++) {
  195. case 0: // IP in hex
  196. Utils::unhex(f,32,ipbits,16);
  197. break;
  198. case 5: // device name
  199. devname = f;
  200. break;
  201. }
  202. }
  203. if (devname) {
  204. ifnames.insert(devname);
  205. InetAddress ip(ipbits,16,0);
  206. if (ifChecker.shouldBindInterface(devname,ip)) {
  207. switch(ip.ipScope()) {
  208. default: break;
  209. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  210. case InetAddress::IP_SCOPE_GLOBAL:
  211. case InetAddress::IP_SCOPE_SHARED:
  212. case InetAddress::IP_SCOPE_PRIVATE:
  213. ip.setPort(port);
  214. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string(devname)));
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. fclose(procf);
  221. }
  222. // Get IPv4 addresses for each device
  223. if (ifnames.size() > 0) {
  224. const int controlfd = (int)socket(AF_INET,SOCK_DGRAM,0);
  225. struct ifconf configuration;
  226. configuration.ifc_len = 0;
  227. configuration.ifc_buf = nullptr;
  228. if (controlfd < 0) goto ip4_address_error;
  229. if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
  230. configuration.ifc_buf = (char*)malloc(configuration.ifc_len);
  231. if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
  232. for (int i=0; i < (int)(configuration.ifc_len / sizeof(ifreq)); i ++) {
  233. struct ifreq& request = configuration.ifc_req[i];
  234. struct sockaddr* addr = &request.ifr_ifru.ifru_addr;
  235. if (addr->sa_family != AF_INET) continue;
  236. std::string ifname = request.ifr_ifrn.ifrn_name;
  237. // name can either be just interface name or interface name followed by ':' and arbitrary label
  238. if (ifname.find(':') != std::string::npos) {
  239. ifname = ifname.substr(0, ifname.find(':'));
  240. }
  241. InetAddress ip(&(((struct sockaddr_in *)addr)->sin_addr),4,0);
  242. if (ifChecker.shouldBindInterface(ifname.c_str(), ip)) {
  243. switch(ip.ipScope()) {
  244. default: break;
  245. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  246. case InetAddress::IP_SCOPE_GLOBAL:
  247. case InetAddress::IP_SCOPE_SHARED:
  248. case InetAddress::IP_SCOPE_PRIVATE:
  249. ip.setPort(port);
  250. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip, ifname));
  251. break;
  252. }
  253. }
  254. }
  255. ip4_address_error:
  256. free(configuration.ifc_buf);
  257. if (controlfd > 0) close(controlfd);
  258. }
  259. const bool gotViaProc = (localIfAddrs.size() > 0);
  260. #else
  261. const bool gotViaProc = false;
  262. #endif
  263. if (!gotViaProc) {
  264. struct ifaddrs *ifatbl = (struct ifaddrs *)0;
  265. struct ifaddrs *ifa;
  266. if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
  267. ifa = ifatbl;
  268. while (ifa) {
  269. if ((ifa->ifa_name)&&(ifa->ifa_addr)) {
  270. InetAddress ip = *(ifa->ifa_addr);
  271. if (ifChecker.shouldBindInterface(ifa->ifa_name,ip)) {
  272. switch(ip.ipScope()) {
  273. default: break;
  274. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  275. case InetAddress::IP_SCOPE_GLOBAL:
  276. case InetAddress::IP_SCOPE_SHARED:
  277. case InetAddress::IP_SCOPE_PRIVATE:
  278. ip.setPort(port);
  279. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string(ifa->ifa_name)));
  280. break;
  281. }
  282. }
  283. }
  284. ifa = ifa->ifa_next;
  285. }
  286. freeifaddrs(ifatbl);
  287. }
  288. }
  289. #endif
  290. // Default to binding to wildcard if we can't enumerate addresses
  291. if (localIfAddrs.empty()) {
  292. localIfAddrs.insert(std::pair<InetAddress,std::string>(InetAddress((uint32_t)0,port),std::string()));
  293. localIfAddrs.insert(std::pair<InetAddress,std::string>(InetAddress((const void *)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",16,port),std::string()));
  294. }
  295. // Close any old bindings to anything that doesn't exist anymore
  296. for(typename std::vector<_Binding>::const_iterator bi(_bindings.begin());bi!=_bindings.end();++bi) {
  297. if (localIfAddrs.find(bi->address) == localIfAddrs.end()) {
  298. phy.close(bi->udpSock,false);
  299. phy.close(bi->tcpListenSock,false);
  300. }
  301. }
  302. std::vector<_Binding> newBindings;
  303. for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
  304. typename std::vector<_Binding>::const_iterator bi(_bindings.begin());
  305. while (bi != _bindings.end()) {
  306. if (bi->address == ii->first) {
  307. newBindings.push_back(*bi);
  308. break;
  309. }
  310. ++bi;
  311. }
  312. if (bi == _bindings.end()) {
  313. udps = phy.udpBind(reinterpret_cast<const struct sockaddr *>(&(ii->first)),(void *)0,ZT_UDP_DESIRED_BUF_SIZE);
  314. if (udps) {
  315. //tcps = phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&ii),(void *)0);
  316. //if (tcps) {
  317. #ifdef __LINUX__
  318. // Bind Linux sockets to their device so routes tha we manage do not override physical routes (wish all platforms had this!)
  319. if (ii->second.length() > 0) {
  320. int fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(udps);
  321. char tmp[256];
  322. Utils::scopy(tmp,sizeof(tmp),ii->second.c_str());
  323. if (fd >= 0) {
  324. if (setsockopt(fd,SOL_SOCKET,SO_BINDTODEVICE,tmp,strlen(tmp)) != 0) {
  325. fprintf(stderr,"WARNING: unable to set SO_BINDTODEVICE to bind %s to %s\n",ii->first.toIpString().c_str(),ii->second.c_str());
  326. }
  327. }
  328. }
  329. #endif // __LINUX__
  330. newBindings.push_back(_Binding());
  331. newBindings.back().udpSock = udps;
  332. //newBindings.back().tcpListenSock = tcps;
  333. newBindings.back().address = ii->first;
  334. //} else {
  335. // phy.close(udps,false);
  336. //}
  337. }
  338. }
  339. }
  340. // Swapping pointers and then letting the old one fall out of scope is faster than copying again
  341. _bindings.swap(newBindings);
  342. }
  343. /**
  344. * Send a UDP packet from the specified local interface, or all
  345. *
  346. * Unfortunately even by examining the routing table there is no ultimately
  347. * robust way to tell where we might reach another host that works in all
  348. * environments. As a result, we send packets with null (wildcard) local
  349. * addresses from *every* bound interface.
  350. *
  351. * These are typically initial HELLOs, path probes, etc., since normal
  352. * conversations will have a local endpoint address. So the cost is low and
  353. * if the peer is not reachable via that route then the packet will go
  354. * nowhere and nothing will happen.
  355. *
  356. * It will of course only send via interface bindings of the same socket
  357. * family. No point in sending V4 via V6 or vice versa.
  358. *
  359. * In any case on most hosts there's only one or two interfaces that we
  360. * will use, so none of this is particularly costly.
  361. *
  362. * @param local Local interface address or null address for 'all'
  363. * @param remote Remote address
  364. * @param data Data to send
  365. * @param len Length of data
  366. * @param v4ttl If non-zero, send this packet with the specified IP TTL (IPv4 only)
  367. */
  368. template<typename PHY_HANDLER_TYPE>
  369. inline bool udpSend(Phy<PHY_HANDLER_TYPE> &phy,const InetAddress &local,const InetAddress &remote,const void *data,unsigned int len,unsigned int v4ttl = 0) const
  370. {
  371. Mutex::Lock _l(_lock);
  372. if (local) {
  373. for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
  374. if (i->address == local) {
  375. if ((v4ttl)&&(local.ss_family == AF_INET))
  376. phy.setIp4UdpTtl(i->udpSock,v4ttl);
  377. const bool result = phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
  378. if ((v4ttl)&&(local.ss_family == AF_INET))
  379. phy.setIp4UdpTtl(i->udpSock,255);
  380. return result;
  381. }
  382. }
  383. return false;
  384. } else {
  385. bool result = false;
  386. for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
  387. if (i->address.ss_family == remote.ss_family) {
  388. if ((v4ttl)&&(remote.ss_family == AF_INET))
  389. phy.setIp4UdpTtl(i->udpSock,v4ttl);
  390. result |= phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
  391. if ((v4ttl)&&(remote.ss_family == AF_INET))
  392. phy.setIp4UdpTtl(i->udpSock,255);
  393. }
  394. }
  395. return result;
  396. }
  397. }
  398. /**
  399. * @return All currently bound local interface addresses
  400. */
  401. inline std::vector<InetAddress> allBoundLocalInterfaceAddresses()
  402. {
  403. Mutex::Lock _l(_lock);
  404. std::vector<InetAddress> aa;
  405. for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i)
  406. aa.push_back(i->address);
  407. return aa;
  408. }
  409. private:
  410. std::vector<_Binding> _bindings;
  411. Mutex _lock;
  412. };
  413. } // namespace ZeroTier
  414. #endif