ManagedRoute.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #include "../node/Constants.hpp"
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifdef __WINDOWS__
  32. #include <WinSock2.h>
  33. #include <Windows.h>
  34. #include <netioapi.h>
  35. #include <IPHlpApi.h>
  36. #endif
  37. #ifdef __UNIX_LIKE__
  38. #include <unistd.h>
  39. #include <sys/param.h>
  40. #include <sys/socket.h>
  41. #include <sys/types.h>
  42. #include <sys/wait.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <net/route.h>
  46. #include <net/if.h>
  47. #ifdef __BSD__
  48. #include <net/if_dl.h>
  49. #include <sys/sysctl.h>
  50. #endif
  51. #include <ifaddrs.h>
  52. #endif
  53. #include <vector>
  54. #include <algorithm>
  55. #include <utility>
  56. #include "ManagedRoute.hpp"
  57. #define ZT_BSD_ROUTE_CMD "/sbin/route"
  58. #define ZT_LINUX_IP_COMMAND "/sbin/ip"
  59. #define ZT_LINUX_IP_COMMAND_2 "/usr/sbin/ip"
  60. namespace ZeroTier {
  61. namespace {
  62. // 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
  63. // If the target is already maximally-specific, 'right' will be unchanged and 'left' will be 't'
  64. static void _forkTarget(const InetAddress &t,InetAddress &left,InetAddress &right)
  65. {
  66. const unsigned int bits = t.netmaskBits() + 1;
  67. left = t;
  68. if (t.ss_family == AF_INET) {
  69. if (bits <= 32) {
  70. left.setPort(bits);
  71. right = t;
  72. reinterpret_cast<struct sockaddr_in *>(&right)->sin_addr.s_addr ^= Utils::hton((uint32_t)(1 << (32 - bits)));
  73. right.setPort(bits);
  74. } else {
  75. right.zero();
  76. }
  77. } else if (t.ss_family == AF_INET6) {
  78. if (bits <= 128) {
  79. left.setPort(bits);
  80. right = t;
  81. uint8_t *b = reinterpret_cast<uint8_t *>(reinterpret_cast<struct sockaddr_in6 *>(&right)->sin6_addr.s6_addr);
  82. b[bits / 8] ^= 1 << (8 - (bits % 8));
  83. right.setPort(bits);
  84. } else {
  85. right.zero();
  86. }
  87. }
  88. }
  89. struct _RTE
  90. {
  91. InetAddress target;
  92. InetAddress via;
  93. char device[128];
  94. int metric;
  95. bool ifscope;
  96. };
  97. #ifdef __BSD__ // ------------------------------------------------------------
  98. #define ZT_ROUTING_SUPPORT_FOUND 1
  99. static std::vector<_RTE> _getRTEs(const InetAddress &target,bool contains)
  100. {
  101. std::vector<_RTE> rtes;
  102. int mib[6];
  103. size_t needed;
  104. mib[0] = CTL_NET;
  105. mib[1] = PF_ROUTE;
  106. mib[2] = 0;
  107. mib[3] = 0;
  108. mib[4] = NET_RT_DUMP;
  109. mib[5] = 0;
  110. if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
  111. if (needed <= 0)
  112. return rtes;
  113. char *buf = (char *)::malloc(needed);
  114. if (buf) {
  115. if (!sysctl(mib,6,buf,&needed,NULL,0)) {
  116. struct rt_msghdr *rtm;
  117. for(char *next=buf,*end=buf+needed;next<end;) {
  118. rtm = (struct rt_msghdr *)next;
  119. char *saptr = (char *)(rtm + 1);
  120. char *saend = next + rtm->rtm_msglen;
  121. InetAddress sa_t,sa_v;
  122. int deviceIndex = -9999;
  123. 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)) {
  124. int which = 0;
  125. while (saptr < saend) {
  126. struct sockaddr *sa = (struct sockaddr *)saptr;
  127. unsigned int salen = sa->sa_len;
  128. if (!salen)
  129. break;
  130. // Skip missing fields in rtm_addrs bit field
  131. while ((rtm->rtm_addrs & 1) == 0) {
  132. rtm->rtm_addrs >>= 1;
  133. ++which;
  134. if (which > 6)
  135. break;
  136. }
  137. if (which > 6)
  138. break;
  139. rtm->rtm_addrs >>= 1;
  140. switch(which++) {
  141. case 0:
  142. //printf("RTA_DST\n");
  143. if (sa->sa_family == AF_INET6) {
  144. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  145. if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) {
  146. // 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.
  147. unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff);
  148. sin6->sin6_addr.s6_addr[2] = 0;
  149. sin6->sin6_addr.s6_addr[3] = 0;
  150. if (!sin6->sin6_scope_id)
  151. sin6->sin6_scope_id = interfaceIndex;
  152. }
  153. }
  154. sa_t = *sa;
  155. break;
  156. case 1:
  157. //printf("RTA_GATEWAY\n");
  158. switch(sa->sa_family) {
  159. case AF_LINK:
  160. deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index;
  161. break;
  162. case AF_INET:
  163. case AF_INET6:
  164. sa_v = *sa;
  165. break;
  166. }
  167. break;
  168. case 2: {
  169. //printf("RTA_NETMASK\n");
  170. if (sa_t.ss_family == AF_INET6) {
  171. salen = sizeof(struct sockaddr_in6);
  172. unsigned int bits = 0;
  173. for(int i=0;i<16;++i) {
  174. unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i];
  175. if (c == 0xff)
  176. bits += 8;
  177. else break;
  178. }
  179. sa_t.setPort(bits);
  180. } else if (sa_t.ss_family == AF_INET) {
  181. salen = sizeof(struct sockaddr_in);
  182. sa_t.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr));
  183. }
  184. } break;
  185. /*
  186. case 3:
  187. //printf("RTA_GENMASK\n");
  188. break;
  189. case 4:
  190. //printf("RTA_IFP\n");
  191. break;
  192. case 5:
  193. //printf("RTA_IFA\n");
  194. break;
  195. case 6:
  196. //printf("RTA_AUTHOR\n");
  197. break;
  198. */
  199. }
  200. saptr += salen;
  201. }
  202. if (((contains)&&(sa_t.containsAddress(target)))||(sa_t == target)) {
  203. rtes.push_back(_RTE());
  204. rtes.back().target = sa_t;
  205. rtes.back().via = sa_v;
  206. if (deviceIndex >= 0) {
  207. if_indextoname(deviceIndex,rtes.back().device);
  208. } else {
  209. rtes.back().device[0] = (char)0;
  210. }
  211. rtes.back().metric = ((int)rtm->rtm_rmx.rmx_hopcount < 0) ? 0 : (int)rtm->rtm_rmx.rmx_hopcount;
  212. }
  213. }
  214. next = saend;
  215. }
  216. }
  217. ::free(buf);
  218. }
  219. }
  220. return rtes;
  221. }
  222. static void _routeCmd(const char *op,const InetAddress &target,const InetAddress &via,const char *ifscope,const char *localInterface)
  223. {
  224. long p = (long)fork();
  225. if (p > 0) {
  226. int exitcode = -1;
  227. ::waitpid(p,&exitcode,0);
  228. } else if (p == 0) {
  229. ::close(STDOUT_FILENO);
  230. ::close(STDERR_FILENO);
  231. char ttmp[64];
  232. char iptmp[64];
  233. if (via) {
  234. if ((ifscope)&&(ifscope[0])) {
  235. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,"-ifscope",ifscope,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString(ttmp),via.toIpString(iptmp),(const char *)0);
  236. } else {
  237. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString(ttmp),via.toIpString(iptmp),(const char *)0);
  238. }
  239. } else if ((localInterface)&&(localInterface[0])) {
  240. if ((ifscope)&&(ifscope[0])) {
  241. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,"-ifscope",ifscope,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString(ttmp),"-interface",localInterface,(const char *)0);
  242. } else {
  243. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,op,((target.ss_family == AF_INET6) ? "-inet6" : "-inet"),target.toString(ttmp),"-interface",localInterface,(const char *)0);
  244. }
  245. }
  246. ::_exit(-1);
  247. }
  248. }
  249. #endif // __BSD__ ------------------------------------------------------------
  250. #ifdef __LINUX__ // ----------------------------------------------------------
  251. #define ZT_ROUTING_SUPPORT_FOUND 1
  252. static void _routeCmd(const char *op,const InetAddress &target,const InetAddress &via,const char *localInterface)
  253. {
  254. long p = (long)fork();
  255. if (p > 0) {
  256. int exitcode = -1;
  257. ::waitpid(p,&exitcode,0);
  258. } else if (p == 0) {
  259. ::close(STDOUT_FILENO);
  260. ::close(STDERR_FILENO);
  261. if (via) {
  262. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"via",via.toIpString().c_str(),(const char *)0);
  263. ::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"via",via.toIpString().c_str(),(const char *)0);
  264. } else if ((localInterface)&&(localInterface[0])) {
  265. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"dev",localInterface,(const char *)0);
  266. ::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"dev",localInterface,(const char *)0);
  267. }
  268. ::_exit(-1);
  269. }
  270. }
  271. #endif // __LINUX__ ----------------------------------------------------------
  272. #ifdef __WINDOWS__ // --------------------------------------------------------
  273. #define ZT_ROUTING_SUPPORT_FOUND 1
  274. static bool _winRoute(bool del,const NET_LUID &interfaceLuid,const NET_IFINDEX &interfaceIndex,const InetAddress &target,const InetAddress &via)
  275. {
  276. MIB_IPFORWARD_ROW2 rtrow;
  277. InitializeIpForwardEntry(&rtrow);
  278. rtrow.InterfaceLuid.Value = interfaceLuid.Value;
  279. rtrow.InterfaceIndex = interfaceIndex;
  280. if (target.ss_family == AF_INET) {
  281. rtrow.DestinationPrefix.Prefix.si_family = AF_INET;
  282. rtrow.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  283. rtrow.DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr = reinterpret_cast<const struct sockaddr_in *>(&target)->sin_addr.S_un.S_addr;
  284. if (via.ss_family == AF_INET) {
  285. rtrow.NextHop.si_family = AF_INET;
  286. rtrow.NextHop.Ipv4.sin_family = AF_INET;
  287. rtrow.NextHop.Ipv4.sin_addr.S_un.S_addr = reinterpret_cast<const struct sockaddr_in *>(&via)->sin_addr.S_un.S_addr;
  288. }
  289. } else if (target.ss_family == AF_INET6) {
  290. rtrow.DestinationPrefix.Prefix.si_family = AF_INET6;
  291. rtrow.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  292. memcpy(rtrow.DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,reinterpret_cast<const struct sockaddr_in6 *>(&target)->sin6_addr.u.Byte,16);
  293. if (via.ss_family == AF_INET6) {
  294. rtrow.NextHop.si_family = AF_INET6;
  295. rtrow.NextHop.Ipv6.sin6_family = AF_INET6;
  296. memcpy(rtrow.NextHop.Ipv6.sin6_addr.u.Byte,reinterpret_cast<const struct sockaddr_in6 *>(&via)->sin6_addr.u.Byte,16);
  297. }
  298. } else {
  299. return false;
  300. }
  301. rtrow.DestinationPrefix.PrefixLength = target.netmaskBits();
  302. rtrow.SitePrefixLength = rtrow.DestinationPrefix.PrefixLength;
  303. rtrow.ValidLifetime = 0xffffffff;
  304. rtrow.PreferredLifetime = 0xffffffff;
  305. rtrow.Metric = -1;
  306. rtrow.Protocol = MIB_IPPROTO_NETMGMT;
  307. rtrow.Loopback = FALSE;
  308. rtrow.AutoconfigureAddress = FALSE;
  309. rtrow.Publish = FALSE;
  310. rtrow.Immortal = FALSE;
  311. rtrow.Age = 0;
  312. rtrow.Origin = NlroManual;
  313. if (del) {
  314. return (DeleteIpForwardEntry2(&rtrow) == NO_ERROR);
  315. } else {
  316. NTSTATUS r = CreateIpForwardEntry2(&rtrow);
  317. if (r == NO_ERROR) {
  318. return true;
  319. } else if (r == ERROR_OBJECT_ALREADY_EXISTS) {
  320. return (SetIpForwardEntry2(&rtrow) == NO_ERROR);
  321. } else {
  322. return false;
  323. }
  324. }
  325. }
  326. static bool _winHasRoute(const NET_LUID &interfaceLuid, const NET_IFINDEX &interfaceIndex, const InetAddress &target, const InetAddress &via)
  327. {
  328. MIB_IPFORWARD_ROW2 rtrow;
  329. InitializeIpForwardEntry(&rtrow);
  330. rtrow.InterfaceLuid.Value = interfaceLuid.Value;
  331. rtrow.InterfaceIndex = interfaceIndex;
  332. if (target.ss_family == AF_INET) {
  333. rtrow.DestinationPrefix.Prefix.si_family = AF_INET;
  334. rtrow.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  335. rtrow.DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr = reinterpret_cast<const struct sockaddr_in *>(&target)->sin_addr.S_un.S_addr;
  336. if (via.ss_family == AF_INET) {
  337. rtrow.NextHop.si_family = AF_INET;
  338. rtrow.NextHop.Ipv4.sin_family = AF_INET;
  339. rtrow.NextHop.Ipv4.sin_addr.S_un.S_addr = reinterpret_cast<const struct sockaddr_in *>(&via)->sin_addr.S_un.S_addr;
  340. }
  341. } else if (target.ss_family == AF_INET6) {
  342. rtrow.DestinationPrefix.Prefix.si_family = AF_INET6;
  343. rtrow.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  344. memcpy(rtrow.DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte, reinterpret_cast<const struct sockaddr_in6 *>(&target)->sin6_addr.u.Byte, 16);
  345. if (via.ss_family == AF_INET6) {
  346. rtrow.NextHop.si_family = AF_INET6;
  347. rtrow.NextHop.Ipv6.sin6_family = AF_INET6;
  348. memcpy(rtrow.NextHop.Ipv6.sin6_addr.u.Byte, reinterpret_cast<const struct sockaddr_in6 *>(&via)->sin6_addr.u.Byte, 16);
  349. }
  350. } else {
  351. return false;
  352. }
  353. rtrow.DestinationPrefix.PrefixLength = target.netmaskBits();
  354. rtrow.SitePrefixLength = rtrow.DestinationPrefix.PrefixLength;
  355. return (GetIpForwardEntry2(&rtrow) == NO_ERROR);
  356. }
  357. #endif // __WINDOWS__ --------------------------------------------------------
  358. #ifndef ZT_ROUTING_SUPPORT_FOUND
  359. #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."
  360. #endif
  361. } // anonymous namespace
  362. /* Linux NOTE: for default route override, some Linux distributions will
  363. * require a change to the rp_filter parameter. A value of '1' will prevent
  364. * default route override from working properly.
  365. *
  366. * sudo sysctl -w net.ipv4.conf.all.rp_filter=2
  367. *
  368. * Add to /etc/sysctl.conf or /etc/sysctl.d/... to make permanent.
  369. *
  370. * This is true of CentOS/RHEL 6+ and possibly others. This is because
  371. * Linux default route override implies asymmetric routes, which then
  372. * trigger Linux's "martian packet" filter. */
  373. bool ManagedRoute::sync()
  374. {
  375. #ifdef __WINDOWS__
  376. NET_LUID interfaceLuid;
  377. interfaceLuid.Value = (ULONG64)Utils::hexStrToU64(_device); // on Windows we use the hex LUID as the "interface name" for ManagedRoute
  378. NET_IFINDEX interfaceIndex = -1;
  379. if (ConvertInterfaceLuidToIndex(&interfaceLuid,&interfaceIndex) != NO_ERROR)
  380. return false;
  381. #endif
  382. InetAddress leftt,rightt;
  383. if (_target.netmaskBits() == 0) // bifurcate only the default route
  384. _forkTarget(_target,leftt,rightt);
  385. else leftt = _target;
  386. #ifdef __BSD__ // ------------------------------------------------------------
  387. // Find lowest metric system route that this route should override (if any)
  388. InetAddress newSystemVia;
  389. char newSystemDevice[128];
  390. newSystemDevice[0] = (char)0;
  391. int systemMetric = 9999999;
  392. std::vector<_RTE> rtes(_getRTEs(_target,false));
  393. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  394. if (r->via) {
  395. if ( ((!newSystemVia)||(r->metric < systemMetric)) && (strcmp(r->device,_device) != 0) ) {
  396. newSystemVia = r->via;
  397. Utils::scopy(newSystemDevice,sizeof(newSystemDevice),r->device);
  398. systemMetric = r->metric;
  399. }
  400. }
  401. }
  402. // Get device corresponding to route if we don't have that already
  403. if ((newSystemVia)&&(!newSystemDevice[0])) {
  404. rtes = _getRTEs(newSystemVia,true);
  405. for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
  406. if ( (r->device[0]) && (strcmp(r->device,_device) != 0) ) {
  407. Utils::scopy(newSystemDevice,sizeof(newSystemDevice),r->device);
  408. break;
  409. }
  410. }
  411. }
  412. if (!newSystemDevice[0])
  413. newSystemVia.zero();
  414. // Shadow system route if it exists, also delete any obsolete shadows
  415. // and replace them with the new state. sync() is called periodically to
  416. // allow us to do that if underlying connectivity changes.
  417. if ((_systemVia != newSystemVia)||(strcmp(_systemDevice,newSystemDevice) != 0)) {
  418. if (_systemVia) {
  419. _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
  420. if (rightt)
  421. _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
  422. }
  423. _systemVia = newSystemVia;
  424. Utils::scopy(_systemDevice,sizeof(_systemDevice),newSystemDevice);
  425. if (_systemVia) {
  426. _routeCmd("add",leftt,_systemVia,_systemDevice,(const char *)0);
  427. _routeCmd("change",leftt,_systemVia,_systemDevice,(const char *)0);
  428. if (rightt) {
  429. _routeCmd("add",rightt,_systemVia,_systemDevice,(const char *)0);
  430. _routeCmd("change",rightt,_systemVia,_systemDevice,(const char *)0);
  431. }
  432. }
  433. }
  434. if (!_applied.count(leftt)) {
  435. _applied[leftt] = false; // not ifscoped
  436. _routeCmd("add",leftt,_via,(const char *)0,(_via) ? (const char *)0 : _device);
  437. _routeCmd("change",leftt,_via,(const char *)0,(_via) ? (const char *)0 : _device);
  438. }
  439. if ((rightt)&&(!_applied.count(rightt))) {
  440. _applied[rightt] = false; // not ifscoped
  441. _routeCmd("add",rightt,_via,(const char *)0,(_via) ? (const char *)0 : _device);
  442. _routeCmd("change",rightt,_via,(const char *)0,(_via) ? (const char *)0 : _device);
  443. }
  444. #endif // __BSD__ ------------------------------------------------------------
  445. #ifdef __LINUX__ // ----------------------------------------------------------
  446. if (!_applied.count(leftt)) {
  447. _applied[leftt] = false; // boolean unused
  448. _routeCmd("replace",leftt,_via,(_via) ? (const char *)0 : _device);
  449. }
  450. if ((rightt)&&(!_applied.count(rightt))) {
  451. _applied[rightt] = false; // boolean unused
  452. _routeCmd("replace",rightt,_via,(_via) ? (const char *)0 : _device);
  453. }
  454. #endif // __LINUX__ ----------------------------------------------------------
  455. #ifdef __WINDOWS__ // --------------------------------------------------------
  456. if ( (!_applied.count(leftt)) || (!_winHasRoute(interfaceLuid,interfaceIndex,leftt,_via)) ) {
  457. _applied[leftt] = false; // boolean unused
  458. _winRoute(false,interfaceLuid,interfaceIndex,leftt,_via);
  459. }
  460. if ( (rightt) && ( (!_applied.count(rightt)) || (!_winHasRoute(interfaceLuid,interfaceIndex,rightt,_via)) ) ) {
  461. _applied[rightt] = false; // boolean unused
  462. _winRoute(false,interfaceLuid,interfaceIndex,rightt,_via);
  463. }
  464. #endif // __WINDOWS__ --------------------------------------------------------
  465. return true;
  466. }
  467. void ManagedRoute::remove()
  468. {
  469. #ifdef __WINDOWS__
  470. NET_LUID interfaceLuid;
  471. interfaceLuid.Value = (ULONG64)Utils::hexStrToU64(_device); // on Windows we use the hex LUID as the "interface name" for ManagedRoute
  472. NET_IFINDEX interfaceIndex = -1;
  473. if (ConvertInterfaceLuidToIndex(&interfaceLuid,&interfaceIndex) != NO_ERROR)
  474. return;
  475. #endif
  476. #ifdef __BSD__
  477. if (_systemVia) {
  478. InetAddress leftt,rightt;
  479. _forkTarget(_target,leftt,rightt);
  480. _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
  481. if (rightt)
  482. _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
  483. }
  484. #endif // __BSD__ ------------------------------------------------------------
  485. for(std::map<InetAddress,bool>::iterator r(_applied.begin());r!=_applied.end();++r) {
  486. #ifdef __BSD__ // ------------------------------------------------------------
  487. _routeCmd("delete",r->first,_via,r->second ? _device : (const char *)0,(_via) ? (const char *)0 : _device);
  488. #endif // __BSD__ ------------------------------------------------------------
  489. #ifdef __LINUX__ // ----------------------------------------------------------
  490. _routeCmd("del",r->first,_via,(_via) ? (const char *)0 : _device);
  491. #endif // __LINUX__ ----------------------------------------------------------
  492. #ifdef __WINDOWS__ // --------------------------------------------------------
  493. _winRoute(true,interfaceLuid,interfaceIndex,r->first,_via);
  494. #endif // __WINDOWS__ --------------------------------------------------------
  495. }
  496. _target.zero();
  497. _via.zero();
  498. _systemVia.zero();
  499. _device[0] = (char)0;
  500. _systemDevice[0] = (char)0;
  501. _applied.clear();
  502. }
  503. } // namespace ZeroTier