ManagedRoute.cpp 21 KB

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