ManagedRoute.cpp 20 KB

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