ManagedRoute.cpp 20 KB

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