ManagedRoute.cpp 21 KB

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