ManagedRoute.cpp 20 KB

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