2
0

ManagedRoute.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #ifndef ZT_ROUTING_SUPPORT_FOUND
  236. #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!
  237. #endif
  238. } // anonymous namespace
  239. bool ManagedRoute::sync()
  240. {
  241. if (_target.isDefaultRoute()) {
  242. /* In ZeroTier we use a forked-route trick to override the default
  243. * with a more specific one while leaving the original system route
  244. * intact. We also create a shadow more specific route to the
  245. * original gateway that is device-bound so that ZeroTier's device
  246. * bound ports go via the physical Internet link. This has to be
  247. * done *slightly* differently on different platforms. */
  248. InetAddress leftt,rightt;
  249. _forkTarget(_target,leftt,rightt);
  250. #ifdef __BSD__ // ------------------------------------------------------------
  251. // Get system default route information
  252. InetAddress newSystemVia;
  253. char newSystemDevice[128];
  254. newSystemDevice[0] = (char)0;
  255. int systemMetric = 9999999;
  256. std::vector<_RTE> rtes(_getRTEs(_target,false));
  257. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  258. if (r->via) {
  259. if ((!newSystemVia)||(r->metric < systemMetric)) {
  260. newSystemVia = r->via;
  261. Utils::scopy(_systemDevice,sizeof(_systemDevice),r->device);
  262. systemMetric = r->metric;
  263. }
  264. }
  265. }
  266. if (!newSystemDevice[0]) {
  267. rtes = _getRTEs(newSystemVia,true);
  268. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  269. if (r->device[0]) {
  270. Utils::scopy(newSystemDevice,sizeof(newSystemDevice),r->device);
  271. break;
  272. }
  273. }
  274. }
  275. if ((!newSystemVia)||(!newSystemDevice[0]))
  276. return false;
  277. // If system default route has changed or hasn't been shadowed yet, update shadow
  278. if ((_systemVia != newSystemVia)||(!strcmp(_systemDevice,newSystemDevice))) {
  279. if ((_systemVia)&&(_systemDevice[0])) {
  280. _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
  281. _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
  282. }
  283. _systemVia = newSystemVia;
  284. Utils::scopy(_systemDevice,sizeof(_systemDevice),newSystemDevice);
  285. _routeCmd("add",leftt,_systemVia,_systemDevice,(const char *)0);
  286. _routeCmd("change",leftt,_systemVia,_systemDevice,(const char *)0);
  287. _routeCmd("add",rightt,_systemVia,_systemDevice,(const char *)0);
  288. _routeCmd("change",rightt,_systemVia,_systemDevice,(const char *)0);
  289. }
  290. // Apply overriding routes
  291. if (!_applied) {
  292. if (_via) {
  293. _routeCmd("add",leftt,_via,(const char *)0,(const char *)0);
  294. _routeCmd("change",leftt,_via,(const char *)0,(const char *)0);
  295. _routeCmd("add",rightt,_via,(const char *)0,(const char *)0);
  296. _routeCmd("change",rightt,_via,(const char *)0,(const char *)0);
  297. } else if (_device[0]) {
  298. _routeCmd("add",leftt,_via,(const char *)0,_device);
  299. _routeCmd("change",leftt,_via,(const char *)0,_device);
  300. _routeCmd("add",rightt,_via,(const char *)0,_device);
  301. _routeCmd("change",rightt,_via,(const char *)0,_device);
  302. }
  303. _applied = true;
  304. }
  305. #endif // __BSD__ ------------------------------------------------------------
  306. #ifdef __LINUX__ // ----------------------------------------------------------
  307. #endif // __LINUX__ ----------------------------------------------------------
  308. #ifdef __WINDOWS__ // --------------------------------------------------------
  309. #endif // __WINDOWS__ --------------------------------------------------------
  310. } else {
  311. // TODO
  312. #ifdef __BSD__ // ------------------------------------------------------------
  313. #endif // __BSD__ ------------------------------------------------------------
  314. #ifdef __LINUX__ // ----------------------------------------------------------
  315. #endif // __LINUX__ ----------------------------------------------------------
  316. #ifdef __WINDOWS__ // --------------------------------------------------------
  317. #endif // __WINDOWS__ --------------------------------------------------------
  318. }
  319. return true;
  320. }
  321. void ManagedRoute::remove()
  322. {
  323. if (_applied) {
  324. if (_target.isDefaultRoute()) {
  325. /* In ZeroTier we use a forked-route trick to override the default
  326. * with a more specific one while leaving the original system route
  327. * intact. We also create a shadow more specific route to the
  328. * original gateway that is device-bound so that ZeroTier's device
  329. * bound ports go via the physical Internet link. This has to be
  330. * done *slightly* differently on different platforms. */
  331. InetAddress leftt,rightt;
  332. _forkTarget(_target,leftt,rightt);
  333. #ifdef __BSD__ // ------------------------------------------------------------
  334. if ((_systemVia)&&(_systemDevice[0])) {
  335. _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
  336. _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
  337. }
  338. if (_via) {
  339. _routeCmd("delete",leftt,_via,(const char *)0,(const char *)0);
  340. _routeCmd("delete",rightt,_via,(const char *)0,(const char *)0);
  341. } else if (_device[0]) {
  342. _routeCmd("delete",leftt,_via,(const char *)0,_device);
  343. _routeCmd("delete",rightt,_via,(const char *)0,_device);
  344. }
  345. #endif // __BSD__ ------------------------------------------------------------
  346. #ifdef __LINUX__ // ----------------------------------------------------------
  347. #endif // __LINUX__ ----------------------------------------------------------
  348. #ifdef __WINDOWS__ // --------------------------------------------------------
  349. #endif // __WINDOWS__ --------------------------------------------------------
  350. } else {
  351. // TODO
  352. #ifdef __BSD__ // ------------------------------------------------------------
  353. #endif // __BSD__ ------------------------------------------------------------
  354. #ifdef __LINUX__ // ----------------------------------------------------------
  355. #endif // __LINUX__ ----------------------------------------------------------
  356. #ifdef __WINDOWS__ // --------------------------------------------------------
  357. #endif // __WINDOWS__ --------------------------------------------------------
  358. }
  359. }
  360. _target.zero();
  361. _via.zero();
  362. _systemVia.zero();
  363. _device[0] = (char)0;
  364. _systemDevice[0] = (char)0;
  365. _applied = false;
  366. }
  367. } // namespace ZeroTier
  368. /*
  369. int main(int argc,char **argv)
  370. {
  371. ZeroTier::ManagedRoute t;
  372. t.set(ZeroTier::InetAddress("0.0.0.0/0"),ZeroTier::InetAddress("10.6.6.112"),"zt2");
  373. sleep(10000);
  374. }
  375. */