ManagedRoute.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #include "../node/Constants.hpp"
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifdef __WINDOWS__
  24. #include <WinSock2.h>
  25. #include <Windows.h>
  26. #include <netioapi.h>
  27. #include <IPHlpApi.h>
  28. #endif
  29. #ifdef __UNIX_LIKE__
  30. #include <unistd.h>
  31. #include <sys/param.h>
  32. #include <sys/sysctl.h>
  33. #include <sys/socket.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <net/route.h>
  39. #include <net/if.h>
  40. #include <net/if_dl.h>
  41. #include <ifaddrs.h>
  42. #endif
  43. #include <vector>
  44. #include <algorithm>
  45. #include <utility>
  46. #include "ManagedRoute.hpp"
  47. #define ZT_BSD_ROUTE_CMD "/sbin/route"
  48. #define ZT_LINUX_IP_COMMAND "/sbin/ip"
  49. namespace ZeroTier {
  50. namespace {
  51. // Fork a target into two more specific targets e.g. 0.0.0.0/0 -> 0.0.0.0/1, 128.0.0.0/1
  52. // If the target is already maximally-specific, 'right' will be unchanged and 'left' will be 't'
  53. static void _forkTarget(const InetAddress &t,InetAddress &left,InetAddress &right)
  54. {
  55. const unsigned int bits = t.netmaskBits() + 1;
  56. left = t;
  57. if ((t.ss_family == AF_INET)&&(bits <= 32)) {
  58. left.setPort(bits);
  59. right = t;
  60. reinterpret_cast<struct sockaddr_in *>(&right)->sin_addr.s_addr ^= Utils::hton((uint32_t)(1 << (32 - bits)));
  61. right.setPort(bits);
  62. } else if ((t.ss_family == AF_INET6)&&(bits <= 128)) {
  63. left.setPort(bits);
  64. right = t;
  65. uint8_t *b = reinterpret_cast<uint8_t *>(reinterpret_cast<struct sockaddr_in6 *>(&right)->sin6_addr.s6_addr);
  66. b[bits / 8] ^= 1 << (8 - (bits % 8));
  67. right.setPort(bits);
  68. }
  69. }
  70. #ifdef __BSD__ // ------------------------------------------------------------
  71. #define ZT_ROUTING_SUPPORT_FOUND 1
  72. struct _RTE
  73. {
  74. InetAddress target;
  75. InetAddress via;
  76. char device[128];
  77. int metric;
  78. bool ifscope;
  79. };
  80. static std::vector<_RTE> _getRTEs(const InetAddress &target,bool contains)
  81. {
  82. std::vector<_RTE> rtes;
  83. int mib[6];
  84. size_t needed;
  85. mib[0] = CTL_NET;
  86. mib[1] = PF_ROUTE;
  87. mib[2] = 0;
  88. mib[3] = 0;
  89. mib[4] = NET_RT_DUMP;
  90. mib[5] = 0;
  91. if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
  92. if (needed <= 0)
  93. return rtes;
  94. char *buf = (char *)::malloc(needed);
  95. if (buf) {
  96. if (!sysctl(mib,6,buf,&needed,NULL,0)) {
  97. struct rt_msghdr *rtm;
  98. for(char *next=buf,*end=buf+needed;next<end;) {
  99. rtm = (struct rt_msghdr *)next;
  100. char *saptr = (char *)(rtm + 1);
  101. char *saend = next + rtm->rtm_msglen;
  102. InetAddress sa_t,sa_v;
  103. int deviceIndex = -9999;
  104. if (((rtm->rtm_flags & RTF_LLINFO) == 0)&&((rtm->rtm_flags & RTF_HOST) == 0)&&((rtm->rtm_flags & RTF_UP) != 0)&&((rtm->rtm_flags & RTF_MULTICAST) == 0)) {
  105. int which = 0;
  106. while (saptr < saend) {
  107. struct sockaddr *sa = (struct sockaddr *)saptr;
  108. unsigned int salen = sa->sa_len;
  109. if (!salen)
  110. break;
  111. // Skip missing fields in rtm_addrs bit field
  112. while ((rtm->rtm_addrs & 1) == 0) {
  113. rtm->rtm_addrs >>= 1;
  114. ++which;
  115. if (which > 6)
  116. break;
  117. }
  118. if (which > 6)
  119. break;
  120. rtm->rtm_addrs >>= 1;
  121. switch(which++) {
  122. case 0:
  123. //printf("RTA_DST\n");
  124. if (sa->sa_family == AF_INET6) {
  125. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  126. if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) {
  127. // BSD uses this fucking strange in-band signaling method to encode device scope IDs for IPv6 addresses... probably a holdover from very early versions of the spec.
  128. unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff);
  129. sin6->sin6_addr.s6_addr[2] = 0;
  130. sin6->sin6_addr.s6_addr[3] = 0;
  131. if (!sin6->sin6_scope_id)
  132. sin6->sin6_scope_id = interfaceIndex;
  133. }
  134. }
  135. sa_t = *sa;
  136. break;
  137. case 1:
  138. //printf("RTA_GATEWAY\n");
  139. switch(sa->sa_family) {
  140. case AF_LINK:
  141. deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index;
  142. break;
  143. case AF_INET:
  144. case AF_INET6:
  145. sa_v = *sa;
  146. break;
  147. }
  148. break;
  149. case 2: {
  150. //printf("RTA_NETMASK\n");
  151. if (sa_t.ss_family == AF_INET6) {
  152. salen = sizeof(struct sockaddr_in6);
  153. unsigned int bits = 0;
  154. for(int i=0;i<16;++i) {
  155. unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i];
  156. if (c == 0xff)
  157. bits += 8;
  158. else break;
  159. }
  160. sa_t.setPort(bits);
  161. } else if (sa_t.ss_family == AF_INET) {
  162. salen = sizeof(struct sockaddr_in);
  163. sa_t.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr));
  164. }
  165. } break;
  166. /*
  167. case 3:
  168. //printf("RTA_GENMASK\n");
  169. break;
  170. case 4:
  171. //printf("RTA_IFP\n");
  172. break;
  173. case 5:
  174. //printf("RTA_IFA\n");
  175. break;
  176. case 6:
  177. //printf("RTA_AUTHOR\n");
  178. break;
  179. */
  180. }
  181. saptr += salen;
  182. }
  183. if (((contains)&&(sa_t.containsAddress(target)))||(sa_t == target)) {
  184. rtes.push_back(_RTE());
  185. rtes.back().target = sa_t;
  186. rtes.back().via = sa_v;
  187. if (deviceIndex >= 0) {
  188. if_indextoname(deviceIndex,rtes.back().device);
  189. } else {
  190. rtes.back().device[0] = (char)0;
  191. }
  192. rtes.back().metric = ((int)rtm->rtm_rmx.rmx_hopcount < 0) ? 0 : (int)rtm->rtm_rmx.rmx_hopcount;
  193. }
  194. }
  195. next = saend;
  196. }
  197. }
  198. ::free(buf);
  199. }
  200. }
  201. return rtes;
  202. }
  203. static void _routeCmd(const char *op,const InetAddress &target,const InetAddress &via,const char *ifscope,const char *localInterface)
  204. {
  205. long p = (long)fork();
  206. if (p > 0) {
  207. int exitcode = -1;
  208. ::waitpid(p,&exitcode,0);
  209. } else if (p == 0) {
  210. //::close(STDOUT_FILENO);
  211. //::close(STDERR_FILENO);
  212. if (via) {
  213. if ((ifscope)&&(ifscope[0])) {
  214. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,"-ifscope",ifscope,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString().c_str(),via.toIpString().c_str(),(const char *)0);
  215. } else {
  216. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString().c_str(),via.toIpString().c_str(),(const char *)0);
  217. }
  218. } else if ((localInterface)&&(localInterface[0])) {
  219. if ((ifscope)&&(ifscope[0])) {
  220. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,"-ifscope",ifscope,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString().c_str(),"-interface",localInterface,(const char *)0);
  221. } else {
  222. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString().c_str(),"-interface",localInterface,(const char *)0);
  223. }
  224. }
  225. ::_exit(-1);
  226. }
  227. }
  228. #endif // __BSD__ ------------------------------------------------------------
  229. #ifdef __LINUX__ // ----------------------------------------------------------
  230. #define ZT_ROUTING_SUPPORT_FOUND 1
  231. #endif // __LINUX__ ----------------------------------------------------------
  232. #ifdef __WINDOWS__ // --------------------------------------------------------
  233. #define ZT_ROUTING_SUPPORT_FOUND 1
  234. #endif // __WINDOWS__ --------------------------------------------------------
  235. } // anonymous namespace
  236. bool ManagedRoute::sync()
  237. {
  238. if (this->target.isDefaultRoute()) {
  239. /* In ZeroTier we use a forked-route trick to override the default
  240. * with a more specific one while leaving the original system route
  241. * intact. We also create a shadow more specific route to the
  242. * original gateway that is device-bound so that ZeroTier's device
  243. * bound ports go via the physical Internet link. This has to be
  244. * done *slightly* differently on different platforms. */
  245. InetAddress leftt,rightt;
  246. _forkTarget(this->target,leftt,rightt);
  247. #ifdef __BSD__ // ------------------------------------------------------------
  248. InetAddress systemVia;
  249. char systemDevice[128];
  250. int systemMetric = 9999999;
  251. systemDevice[0] = (char)0;
  252. std::vector<_RTE> rtes(_getRTEs(this->target,false));
  253. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  254. if (r->via) {
  255. if ((!systemVia)||(r->metric < systemMetric)) {
  256. systemVia = r->via;
  257. Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
  258. systemMetric = r->metric;
  259. }
  260. }
  261. }
  262. if (!systemDevice[0]) {
  263. rtes = _getRTEs(systemVia,true);
  264. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  265. if (r->device[0])
  266. Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
  267. }
  268. }
  269. if ((!systemVia)||(!systemDevice[0]))
  270. return false;
  271. _routeCmd("add",leftt,systemVia,systemDevice,(const char *)0);
  272. _routeCmd("change",leftt,systemVia,systemDevice,(const char *)0);
  273. _routeCmd("add",rightt,systemVia,systemDevice,(const char *)0);
  274. _routeCmd("change",rightt,systemVia,systemDevice,(const char *)0);
  275. if (this->via) {
  276. _routeCmd("add",leftt,this->via,(const char *)0,(const char *)0);
  277. _routeCmd("change",leftt,this->via,(const char *)0,(const char *)0);
  278. _routeCmd("add",rightt,this->via,(const char *)0,(const char *)0);
  279. _routeCmd("change",rightt,this->via,(const char *)0,(const char *)0);
  280. } else if ((this->device)&&(this->device[0])) {
  281. _routeCmd("add",leftt,this->via,(const char *)0,this->device);
  282. _routeCmd("change",leftt,this->via,(const char *)0,this->device);
  283. _routeCmd("add",rightt,this->via,(const char *)0,this->device);
  284. _routeCmd("change",rightt,this->via,(const char *)0,this->device);
  285. }
  286. #endif // __BSD__ ------------------------------------------------------------
  287. #ifdef __LINUX__ // ----------------------------------------------------------
  288. #endif // __LINUX__ ----------------------------------------------------------
  289. #ifdef __WINDOWS__ // --------------------------------------------------------
  290. #endif // __WINDOWS__ --------------------------------------------------------
  291. } else {
  292. // TODO
  293. #ifdef __BSD__ // ------------------------------------------------------------
  294. #endif // __BSD__ ------------------------------------------------------------
  295. #ifdef __LINUX__ // ----------------------------------------------------------
  296. #endif // __LINUX__ ----------------------------------------------------------
  297. #ifdef __WINDOWS__ // --------------------------------------------------------
  298. #endif // __WINDOWS__ --------------------------------------------------------
  299. }
  300. return true;
  301. }
  302. void ManagedRoute::remove()
  303. {
  304. if (!this->applied)
  305. return;
  306. if (this->target.isDefaultRoute()) {
  307. /* In ZeroTier we use a forked-route trick to override the default
  308. * with a more specific one while leaving the original system route
  309. * intact. We also create a shadow more specific route to the
  310. * original gateway that is device-bound so that ZeroTier's device
  311. * bound ports go via the physical Internet link. This has to be
  312. * done *slightly* differently on different platforms. */
  313. InetAddress leftt,rightt;
  314. _forkTarget(this->target,leftt,rightt);
  315. #ifdef __BSD__ // ------------------------------------------------------------
  316. InetAddress systemVia;
  317. char systemDevice[128];
  318. int systemMetric = 9999999;
  319. systemDevice[0] = (char)0;
  320. std::vector<_RTE> rtes(_getRTEs(this->target,false));
  321. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  322. if (r->via) {
  323. if ((!systemVia)||(r->metric < systemMetric)) {
  324. systemVia = r->via;
  325. Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
  326. systemMetric = r->metric;
  327. }
  328. }
  329. }
  330. if (!systemDevice[0]) {
  331. rtes = _getRTEs(systemVia,true);
  332. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  333. if (r->device[0])
  334. Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
  335. }
  336. }
  337. if ((!systemVia)||(!systemDevice[0]))
  338. return false;
  339. _routeCmd("delete",leftt,systemVia,systemDevice,(const char *)0);
  340. _routeCmd("delete",rightt,systemVia,systemDevice,(const char *)0);
  341. if (this->via) {
  342. _routeCmd("delete",leftt,this->via,(const char *)0,(const char *)0);
  343. _routeCmd("delete",rightt,this->via,(const char *)0,(const char *)0);
  344. } else if ((this->device)&&(this->device[0])) {
  345. _routeCmd("delete",leftt,this->via,(const char *)0,this->device);
  346. _routeCmd("delete",rightt,this->via,(const char *)0,this->device);
  347. }
  348. #endif // __BSD__ ------------------------------------------------------------
  349. #ifdef __LINUX__ // ----------------------------------------------------------
  350. #endif // __LINUX__ ----------------------------------------------------------
  351. #ifdef __WINDOWS__ // --------------------------------------------------------
  352. #endif // __WINDOWS__ --------------------------------------------------------
  353. } else {
  354. // TODO
  355. #ifdef __BSD__ // ------------------------------------------------------------
  356. #endif // __BSD__ ------------------------------------------------------------
  357. #ifdef __LINUX__ // ----------------------------------------------------------
  358. #endif // __LINUX__ ----------------------------------------------------------
  359. #ifdef __WINDOWS__ // --------------------------------------------------------
  360. #endif // __WINDOWS__ --------------------------------------------------------
  361. }
  362. }
  363. } // namespace ZeroTier
  364. #ifndef ZT_ROUTING_SUPPORT_FOUND
  365. #error ManagedRoute.cpp has no support for managing routes on this platform! You'll need to check and see if one of the existing ones will work and make sure proper defines are set, or write one. Please do a Github pull request if you do this for a new OS!
  366. #endif
  367. /*
  368. int main(int argc,char **argv)
  369. {
  370. ZeroTier::ManagedRoute t;
  371. t.set(ZeroTier::InetAddress("0.0.0.0/0"),ZeroTier::InetAddress("10.6.6.112"),"zt2");
  372. sleep(10000);
  373. }
  374. */