ManagedRoute.cpp 21 KB

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