Binder.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <set>
  56. #include <atomic>
  57. #include "../node/NonCopyable.hpp"
  58. #include "../node/InetAddress.hpp"
  59. #include "../node/Mutex.hpp"
  60. #include "../node/Utils.hpp"
  61. #include "Phy.hpp"
  62. #include "OSUtils.hpp"
  63. // Period between refreshes of bindings
  64. #define ZT_BINDER_REFRESH_PERIOD 30000
  65. // Max number of bindings
  66. #define ZT_BINDER_MAX_BINDINGS 128
  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() : udpSock((PhySocket *)0),tcpListenSock((PhySocket *)0) {}
  85. PhySocket *udpSock;
  86. PhySocket *tcpListenSock;
  87. InetAddress address;
  88. };
  89. public:
  90. Binder() : _bindingCount(0) {}
  91. /**
  92. * Close all bound ports, should be called on shutdown
  93. *
  94. * @param phy Physical interface
  95. */
  96. template<typename PHY_HANDLER_TYPE>
  97. void closeAll(Phy<PHY_HANDLER_TYPE> &phy)
  98. {
  99. Mutex::Lock _l(_lock);
  100. for(unsigned int b=0,c=_bindingCount;b<c;++b) {
  101. phy.close(_bindings[b].udpSock,false);
  102. phy.close(_bindings[b].tcpListenSock,false);
  103. }
  104. _bindingCount = 0;
  105. }
  106. /**
  107. * Scan local devices and addresses and rebind TCP and UDP
  108. *
  109. * This should be called after wake from sleep, on detected network device
  110. * changes, on startup, or periodically (e.g. every 30-60s).
  111. *
  112. * @param phy Physical interface
  113. * @param ports Ports to bind on all interfaces
  114. * @param ignoreInterfacesByName Ignore these interfaces by name
  115. * @param ignoreInterfacesByNamePrefix Ignore these interfaces by name-prefix (starts-with, e.g. zt ignores zt*)
  116. * @param ignoreInterfacesByAddress Ignore these interfaces by address
  117. * @tparam PHY_HANDLER_TYPE Type for Phy<> template
  118. * @tparam INTERFACE_CHECKER Type for class containing shouldBindInterface() method
  119. */
  120. template<typename PHY_HANDLER_TYPE,typename INTERFACE_CHECKER>
  121. void refresh(Phy<PHY_HANDLER_TYPE> &phy,unsigned int *ports,unsigned int portCount,INTERFACE_CHECKER &ifChecker)
  122. {
  123. std::map<InetAddress,std::string> localIfAddrs;
  124. PhySocket *udps,*tcps;
  125. Mutex::Lock _l(_lock);
  126. #ifdef __WINDOWS__
  127. char aabuf[32768];
  128. ULONG aalen = sizeof(aabuf);
  129. 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) {
  130. PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
  131. while (a) {
  132. PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
  133. while (ua) {
  134. InetAddress ip(ua->Address.lpSockaddr);
  135. if (ifChecker.shouldBindInterface("",ip)) {
  136. switch(ip.ipScope()) {
  137. default: break;
  138. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  139. case InetAddress::IP_SCOPE_GLOBAL:
  140. case InetAddress::IP_SCOPE_SHARED:
  141. case InetAddress::IP_SCOPE_PRIVATE:
  142. for(int x=0;x<(int)portCount;++x) {
  143. ip.setPort(ports[x]);
  144. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string()));
  145. }
  146. break;
  147. }
  148. }
  149. ua = ua->Next;
  150. }
  151. a = a->Next;
  152. }
  153. }
  154. #else // not __WINDOWS__
  155. /* On Linux we use an alternative method if available since getifaddrs()
  156. * gets very slow when there are lots of network namespaces. This won't
  157. * work unless /proc/PID/net/if_inet6 exists and it may not on some
  158. * embedded systems, so revert to getifaddrs() there. */
  159. #ifdef __LINUX__
  160. char fn[256],tmp[256];
  161. std::set<std::string> ifnames;
  162. const unsigned long pid = (unsigned long)getpid();
  163. // Get all device names
  164. OSUtils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/dev",pid);
  165. FILE *procf = fopen(fn,"r");
  166. if (procf) {
  167. while (fgets(tmp,sizeof(tmp),procf)) {
  168. tmp[255] = 0;
  169. char *saveptr = (char *)0;
  170. for(char *f=Utils::stok(tmp," \t\r\n:|",&saveptr);(f);f=Utils::stok((char *)0," \t\r\n:|",&saveptr)) {
  171. if ((strcmp(f,"Inter-") != 0)&&(strcmp(f,"face") != 0)&&(f[0] != 0))
  172. ifnames.insert(f);
  173. break; // we only want the first field
  174. }
  175. }
  176. fclose(procf);
  177. }
  178. // Get IPv6 addresses (and any device names we don't already know)
  179. OSUtils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/if_inet6",pid);
  180. procf = fopen(fn,"r");
  181. if (procf) {
  182. while (fgets(tmp,sizeof(tmp),procf)) {
  183. tmp[255] = 0;
  184. char *saveptr = (char *)0;
  185. unsigned char ipbits[16];
  186. memset(ipbits,0,sizeof(ipbits));
  187. char *devname = (char *)0;
  188. int n = 0;
  189. for(char *f=Utils::stok(tmp," \t\r\n",&saveptr);(f);f=Utils::stok((char *)0," \t\r\n",&saveptr)) {
  190. switch(n++) {
  191. case 0: // IP in hex
  192. Utils::unhex(f,32,ipbits,16);
  193. break;
  194. case 5: // device name
  195. devname = f;
  196. break;
  197. }
  198. }
  199. if (devname) {
  200. ifnames.insert(devname);
  201. InetAddress ip(ipbits,16,0);
  202. if (ifChecker.shouldBindInterface(devname,ip)) {
  203. switch(ip.ipScope()) {
  204. default: break;
  205. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  206. case InetAddress::IP_SCOPE_GLOBAL:
  207. case InetAddress::IP_SCOPE_SHARED:
  208. case InetAddress::IP_SCOPE_PRIVATE:
  209. for(int x=0;x<(int)portCount;++x) {
  210. ip.setPort(ports[x]);
  211. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string(devname)));
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. fclose(procf);
  219. }
  220. // Get IPv4 addresses for each device
  221. if (ifnames.size() > 0) {
  222. const int controlfd = (int)socket(AF_INET,SOCK_DGRAM,0);
  223. struct ifconf configuration;
  224. configuration.ifc_len = 0;
  225. configuration.ifc_buf = nullptr;
  226. if (controlfd < 0) goto ip4_address_error;
  227. if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
  228. configuration.ifc_buf = (char*)malloc(configuration.ifc_len);
  229. if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
  230. for (int i=0; i < (int)(configuration.ifc_len / sizeof(ifreq)); i ++) {
  231. struct ifreq& request = configuration.ifc_req[i];
  232. struct sockaddr* addr = &request.ifr_ifru.ifru_addr;
  233. if (addr->sa_family != AF_INET) continue;
  234. std::string ifname = request.ifr_ifrn.ifrn_name;
  235. // name can either be just interface name or interface name followed by ':' and arbitrary label
  236. if (ifname.find(':') != std::string::npos)
  237. ifname = ifname.substr(0, ifname.find(':'));
  238. InetAddress ip(&(((struct sockaddr_in *)addr)->sin_addr),4,0);
  239. if (ifChecker.shouldBindInterface(ifname.c_str(), ip)) {
  240. switch(ip.ipScope()) {
  241. default: break;
  242. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  243. case InetAddress::IP_SCOPE_GLOBAL:
  244. case InetAddress::IP_SCOPE_SHARED:
  245. case InetAddress::IP_SCOPE_PRIVATE:
  246. for(int x=0;x<(int)portCount;++x) {
  247. ip.setPort(ports[x]);
  248. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,ifname));
  249. }
  250. break;
  251. }
  252. }
  253. }
  254. ip4_address_error:
  255. free(configuration.ifc_buf);
  256. if (controlfd > 0) close(controlfd);
  257. }
  258. const bool gotViaProc = (localIfAddrs.size() > 0);
  259. #else
  260. const bool gotViaProc = false;
  261. #endif
  262. if (!gotViaProc) {
  263. struct ifaddrs *ifatbl = (struct ifaddrs *)0;
  264. struct ifaddrs *ifa;
  265. if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
  266. ifa = ifatbl;
  267. while (ifa) {
  268. if ((ifa->ifa_name)&&(ifa->ifa_addr)) {
  269. InetAddress ip = *(ifa->ifa_addr);
  270. if (ifChecker.shouldBindInterface(ifa->ifa_name,ip)) {
  271. switch(ip.ipScope()) {
  272. default: break;
  273. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  274. case InetAddress::IP_SCOPE_GLOBAL:
  275. case InetAddress::IP_SCOPE_SHARED:
  276. case InetAddress::IP_SCOPE_PRIVATE:
  277. for(int x=0;x<(int)portCount;++x) {
  278. ip.setPort(ports[x]);
  279. localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string(ifa->ifa_name)));
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. ifa = ifa->ifa_next;
  286. }
  287. freeifaddrs(ifatbl);
  288. }
  289. }
  290. #endif
  291. // Default to binding to wildcard if we can't enumerate addresses
  292. if (localIfAddrs.empty()) {
  293. for(int x=0;x<(int)portCount;++x) {
  294. localIfAddrs.insert(std::pair<InetAddress,std::string>(InetAddress((uint32_t)0,ports[x]),std::string()));
  295. 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,ports[x]),std::string()));
  296. }
  297. }
  298. const unsigned int oldBindingCount = _bindingCount;
  299. _bindingCount = 0;
  300. // Save bindings that are still valid, close those that are not
  301. for(unsigned int b=0;b<oldBindingCount;++b) {
  302. if (localIfAddrs.find(_bindings[b].address) != localIfAddrs.end()) {
  303. if (_bindingCount != b)
  304. _bindings[(unsigned int)_bindingCount] = _bindings[b];
  305. ++_bindingCount;
  306. } else {
  307. PhySocket *const udps = _bindings[b].udpSock;
  308. PhySocket *const tcps = _bindings[b].tcpListenSock;
  309. _bindings[b].udpSock = (PhySocket *)0;
  310. _bindings[b].tcpListenSock = (PhySocket *)0;
  311. phy.close(udps,false);
  312. phy.close(tcps,false);
  313. }
  314. }
  315. // Create new bindings for those not already bound
  316. for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
  317. unsigned int bi = 0;
  318. while (bi != _bindingCount) {
  319. if (_bindings[bi].address == ii->first)
  320. break;
  321. ++bi;
  322. }
  323. if (bi == _bindingCount) {
  324. udps = phy.udpBind(reinterpret_cast<const struct sockaddr *>(&(ii->first)),(void *)0,ZT_UDP_DESIRED_BUF_SIZE);
  325. tcps = phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&(ii->first)),(void *)0);
  326. if ((udps)&&(tcps)) {
  327. #ifdef __LINUX__
  328. // Bind Linux sockets to their device so routes tha we manage do not override physical routes (wish all platforms had this!)
  329. if (ii->second.length() > 0) {
  330. char tmp[256];
  331. Utils::scopy(tmp,sizeof(tmp),ii->second.c_str());
  332. int fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(udps);
  333. if (fd >= 0)
  334. setsockopt(fd,SOL_SOCKET,SO_BINDTODEVICE,tmp,strlen(tmp));
  335. fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(tcps);
  336. if (fd >= 0)
  337. setsockopt(fd,SOL_SOCKET,SO_BINDTODEVICE,tmp,strlen(tmp));
  338. }
  339. #endif // __LINUX__
  340. if (_bindingCount < ZT_BINDER_MAX_BINDINGS) {
  341. _bindings[_bindingCount].udpSock = udps;
  342. _bindings[_bindingCount].tcpListenSock = tcps;
  343. _bindings[_bindingCount].address = ii->first;
  344. ++_bindingCount;
  345. }
  346. } else {
  347. phy.close(udps,false);
  348. phy.close(tcps,false);
  349. }
  350. }
  351. }
  352. }
  353. /**
  354. * @return All currently bound local interface addresses
  355. */
  356. inline std::vector<InetAddress> allBoundLocalInterfaceAddresses() const
  357. {
  358. std::vector<InetAddress> aa;
  359. Mutex::Lock _l(_lock);
  360. for(unsigned int b=0,c=_bindingCount;b<c;++b)
  361. aa.push_back(_bindings[b].address);
  362. return aa;
  363. }
  364. /**
  365. * Send from all bound UDP sockets
  366. */
  367. template<typename PHY_HANDLER_TYPE>
  368. inline bool udpSendAll(Phy<PHY_HANDLER_TYPE> &phy,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  369. {
  370. bool r = false;
  371. Mutex::Lock _l(_lock);
  372. for(unsigned int b=0,c=_bindingCount;b<c;++b) {
  373. if (ttl) phy.setIp4UdpTtl(_bindings[b].udpSock,ttl);
  374. if (phy.udpSend(_bindings[b].udpSock,(const struct sockaddr *)addr,data,len)) r = true;
  375. if (ttl) phy.setIp4UdpTtl(_bindings[b].udpSock,255);
  376. }
  377. return r;
  378. }
  379. /**
  380. * @param addr Address to check
  381. * @return True if this is a bound local interface address
  382. */
  383. inline bool isBoundLocalInterfaceAddress(const InetAddress &addr) const
  384. {
  385. Mutex::Lock _l(_lock);
  386. for(unsigned int b=0;b<_bindingCount;++b) {
  387. if (_bindings[b].address == addr)
  388. return true;
  389. }
  390. return false;
  391. }
  392. /**
  393. * Quickly check that a UDP socket is valid
  394. *
  395. * @param udpSock UDP socket to check
  396. * @return True if socket is currently bound/allocated
  397. */
  398. inline bool isUdpSocketValid(PhySocket *const udpSock)
  399. {
  400. for(unsigned int b=0,c=_bindingCount;b<c;++b) {
  401. if (_bindings[b].udpSock == udpSock)
  402. return (b < _bindingCount); // double check atomic which may have changed
  403. }
  404. return false;
  405. }
  406. private:
  407. _Binding _bindings[ZT_BINDER_MAX_BINDINGS];
  408. std::atomic<unsigned int> _bindingCount;
  409. Mutex _lock;
  410. };
  411. } // namespace ZeroTier
  412. #endif