Advertisement
Guest User

Untitled

a guest
May 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 184.67 KB | None | 0 0
  1. /*
  2. * OpenVPN -- An application to securely tunnel IP networks
  3. * over a single TCP/UDP port, with support for SSL/TLS-based
  4. * session authentication and key exchange,
  5. * packet encryption, packet authentication, and
  6. * packet compression.
  7. *
  8. * Copyright (C) 2002-2018 OpenVPN Inc <[email protected]>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23.  
  24. /*
  25. * Support routines for configuring and accessing TUN/TAP
  26. * virtual network adapters.
  27. *
  28. * This file is based on the TUN/TAP driver interface routines
  29. * from VTun by Maxim Krasnyansky <[email protected]>.
  30. */
  31.  
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #elif defined(_MSC_VER)
  35. #include "config-msvc.h"
  36. #endif
  37.  
  38. #include "syshead.h"
  39.  
  40. #include "tun.h"
  41. #include "fdmisc.h"
  42. #include "common.h"
  43. #include "misc.h"
  44. #include "socket.h"
  45. #include "manage.h"
  46. #include "route.h"
  47. #include "win32.h"
  48. #include "block_dns.h"
  49.  
  50. #include "memdbg.h"
  51.  
  52. #ifdef _WIN32
  53. #include "openvpn-msg.h"
  54. #endif
  55.  
  56. #include <string.h>
  57.  
  58. #ifdef _WIN32
  59.  
  60. /* #define SIMULATE_DHCP_FAILED */ /* simulate bad DHCP negotiation */
  61.  
  62. #define NI_TEST_FIRST (1<<0)
  63. #define NI_IP_NETMASK (1<<1)
  64. #define NI_OPTIONS (1<<2)
  65.  
  66. static void netsh_ifconfig(const struct tuntap_options *to,
  67. const char *flex_name,
  68. const in_addr_t ip,
  69. const in_addr_t netmask,
  70. const unsigned int flags);
  71.  
  72. static void netsh_set_dns6_servers(const struct in6_addr *addr_list,
  73. const int addr_len,
  74. const char *flex_name);
  75.  
  76. static void netsh_command(const struct argv *a, int n, int msglevel);
  77.  
  78. static const char *netsh_get_id(const char *dev_node, struct gc_arena *gc);
  79.  
  80. static DWORD get_adapter_index_flexible(const char *name);
  81.  
  82. static bool
  83. do_address_service(const bool add, const short family, const struct tuntap *tt)
  84. {
  85. DWORD len;
  86. bool ret = false;
  87. ack_message_t ack;
  88. struct gc_arena gc = gc_new();
  89. HANDLE pipe = tt->options.msg_channel;
  90.  
  91. address_message_t addr = {
  92. .header = {
  93. (add ? msg_add_address : msg_del_address),
  94. sizeof(address_message_t),
  95. 0
  96. },
  97. .family = family,
  98. .iface = { .index = tt->adapter_index, .name = "" }
  99. };
  100.  
  101. if (addr.iface.index == TUN_ADAPTER_INDEX_INVALID)
  102. {
  103. strncpy(addr.iface.name, tt->actual_name, sizeof(addr.iface.name));
  104. addr.iface.name[sizeof(addr.iface.name) - 1] = '\0';
  105. }
  106.  
  107. if (addr.family == AF_INET)
  108. {
  109. addr.address.ipv4.s_addr = tt->local;
  110. addr.prefix_len = 32;
  111. }
  112. else
  113. {
  114. addr.address.ipv6 = tt->local_ipv6;
  115. addr.prefix_len = tt->netbits_ipv6;
  116. }
  117.  
  118. if (!WriteFile(pipe, &addr, sizeof(addr), &len, NULL)
  119. || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL))
  120. {
  121. msg(M_WARN, "TUN: could not talk to service: %s [%lu]",
  122. strerror_win32(GetLastError(), &gc), GetLastError());
  123. goto out;
  124. }
  125.  
  126. if (ack.error_number != NO_ERROR)
  127. {
  128. msg(M_WARN, "TUN: %s address failed using service: %s [status=%u if_index=%d]",
  129. (add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc),
  130. ack.error_number, addr.iface.index);
  131. goto out;
  132. }
  133.  
  134. ret = true;
  135.  
  136. out:
  137. gc_free(&gc);
  138. return ret;
  139. }
  140.  
  141. static bool
  142. do_dns6_service(bool add, const struct tuntap *tt)
  143. {
  144. DWORD len;
  145. bool ret = false;
  146. ack_message_t ack;
  147. struct gc_arena gc = gc_new();
  148. HANDLE pipe = tt->options.msg_channel;
  149. int addr_len = add ? tt->options.dns6_len : 0;
  150.  
  151. if (addr_len == 0 && add) /* no addresses to add */
  152. {
  153. return true;
  154. }
  155.  
  156. dns_cfg_message_t dns = {
  157. .header = {
  158. (add ? msg_add_dns_cfg : msg_del_dns_cfg),
  159. sizeof(dns_cfg_message_t),
  160. 0
  161. },
  162. .iface = { .index = tt->adapter_index, .name = "" },
  163. .domains = "",
  164. .family = AF_INET6,
  165. .addr_len = addr_len
  166. };
  167.  
  168. /* interface name is required */
  169. strncpy(dns.iface.name, tt->actual_name, sizeof(dns.iface.name));
  170. dns.iface.name[sizeof(dns.iface.name) - 1] = '\0';
  171.  
  172. if (addr_len > _countof(dns.addr))
  173. {
  174. addr_len = _countof(dns.addr);
  175. dns.addr_len = addr_len;
  176. msg(M_WARN, "Number of IPv6 DNS addresses sent to service truncated to %d",
  177. addr_len);
  178. }
  179.  
  180. for (int i = 0; i < addr_len; ++i)
  181. {
  182. dns.addr[i].ipv6 = tt->options.dns6[i];
  183. }
  184.  
  185. msg(D_LOW, "%s IPv6 dns servers on '%s' (if_index = %d) using service",
  186. (add ? "Setting" : "Deleting"), dns.iface.name, dns.iface.index);
  187.  
  188. if (!WriteFile(pipe, &dns, sizeof(dns), &len, NULL)
  189. || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL))
  190. {
  191. msg(M_WARN, "TUN: could not talk to service: %s [%lu]",
  192. strerror_win32(GetLastError(), &gc), GetLastError());
  193. goto out;
  194. }
  195.  
  196. if (ack.error_number != NO_ERROR)
  197. {
  198. msg(M_WARN, "TUN: %s IPv6 dns failed using service: %s [status=%u if_name=%s]",
  199. (add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc),
  200. ack.error_number, dns.iface.name);
  201. goto out;
  202. }
  203.  
  204. msg(M_INFO, "IPv6 dns servers %s using service", (add ? "set" : "deleted"));
  205. ret = true;
  206.  
  207. out:
  208. gc_free(&gc);
  209. return ret;
  210. }
  211.  
  212. #endif /* ifdef _WIN32 */
  213.  
  214. #ifdef TARGET_SOLARIS
  215. static void solaris_error_close(struct tuntap *tt, const struct env_set *es, const char *actual, bool unplumb_inet6);
  216.  
  217. #include <stropts.h>
  218. #endif
  219.  
  220. #if defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H
  221. #include <sys/kern_control.h>
  222. #include <net/if_utun.h>
  223. #include <sys/sys_domain.h>
  224. #endif
  225.  
  226. static void clear_tuntap(struct tuntap *tuntap);
  227.  
  228. bool
  229. is_dev_type(const char *dev, const char *dev_type, const char *match_type)
  230. {
  231. ASSERT(match_type);
  232. if (!dev)
  233. {
  234. return false;
  235. }
  236. if (dev_type)
  237. {
  238. return !strcmp(dev_type, match_type);
  239. }
  240. else
  241. {
  242. return !strncmp(dev, match_type, strlen(match_type));
  243. }
  244. }
  245.  
  246. int
  247. dev_type_enum(const char *dev, const char *dev_type)
  248. {
  249. if (is_dev_type(dev, dev_type, "tun"))
  250. {
  251. return DEV_TYPE_TUN;
  252. }
  253. else if (is_dev_type(dev, dev_type, "tap"))
  254. {
  255. return DEV_TYPE_TAP;
  256. }
  257. else if (is_dev_type(dev, dev_type, "null"))
  258. {
  259. return DEV_TYPE_NULL;
  260. }
  261. else
  262. {
  263. return DEV_TYPE_UNDEF;
  264. }
  265. }
  266.  
  267. const char *
  268. dev_type_string(const char *dev, const char *dev_type)
  269. {
  270. switch (dev_type_enum(dev, dev_type))
  271. {
  272. case DEV_TYPE_TUN:
  273. return "tun";
  274.  
  275. case DEV_TYPE_TAP:
  276. return "tap";
  277.  
  278. case DEV_TYPE_NULL:
  279. return "null";
  280.  
  281. default:
  282. return "[unknown-dev-type]";
  283. }
  284. }
  285.  
  286. /*
  287. * Try to predict the actual TUN/TAP device instance name,
  288. * before the device is actually opened.
  289. */
  290. const char *
  291. guess_tuntap_dev(const char *dev,
  292. const char *dev_type,
  293. const char *dev_node,
  294. struct gc_arena *gc)
  295. {
  296. #ifdef _WIN32
  297. const int dt = dev_type_enum(dev, dev_type);
  298. if (dt == DEV_TYPE_TUN || dt == DEV_TYPE_TAP)
  299. {
  300. return netsh_get_id(dev_node, gc);
  301. }
  302. #endif
  303.  
  304. /* default case */
  305. return dev;
  306. }
  307.  
  308.  
  309. /* --ifconfig-nowarn disables some options sanity checking */
  310. static const char ifconfig_warn_how_to_silence[] = "(silence this warning with --ifconfig-nowarn)";
  311.  
  312. /*
  313. * If !tun, make sure ifconfig_remote_netmask looks
  314. * like a netmask.
  315. *
  316. * If tun, make sure ifconfig_remote_netmask looks
  317. * like an IPv4 address.
  318. */
  319. static void
  320. ifconfig_sanity_check(bool tun, in_addr_t addr, int topology)
  321. {
  322. struct gc_arena gc = gc_new();
  323. const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
  324. if (tun)
  325. {
  326. if (looks_like_netmask && (topology == TOP_NET30 || topology == TOP_P2P))
  327. {
  328. msg(M_WARN, "WARNING: Since you are using --dev tun with a point-to-point topology, the second argument to --ifconfig must be an IP address. You are using something (%s) that looks more like a netmask. %s",
  329. print_in_addr_t(addr, 0, &gc),
  330. ifconfig_warn_how_to_silence);
  331. }
  332. }
  333. else /* tap */
  334. {
  335. if (!looks_like_netmask)
  336. {
  337. msg(M_WARN, "WARNING: Since you are using --dev tap, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
  338. ifconfig_warn_how_to_silence);
  339. }
  340. }
  341. gc_free(&gc);
  342. }
  343.  
  344. /*
  345. * For TAP-style devices, generate a broadcast address.
  346. */
  347. static in_addr_t
  348. generate_ifconfig_broadcast_addr(in_addr_t local,
  349. in_addr_t netmask)
  350. {
  351. return local | ~netmask;
  352. }
  353.  
  354. /*
  355. * Check that --local and --remote addresses do not
  356. * clash with ifconfig addresses or subnet.
  357. */
  358. static void
  359. check_addr_clash(const char *name,
  360. int type,
  361. in_addr_t public,
  362. in_addr_t local,
  363. in_addr_t remote_netmask)
  364. {
  365. struct gc_arena gc = gc_new();
  366. #if 0
  367. msg(M_INFO, "CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
  368. type,
  369. print_in_addr_t(public, 0, &gc),
  370. print_in_addr_t(local, 0, &gc),
  371. print_in_addr_t(remote_netmask, 0, &gc));
  372. #endif
  373.  
  374. if (public)
  375. {
  376. if (type == DEV_TYPE_TUN)
  377. {
  378. const in_addr_t test_netmask = 0xFFFFFF00;
  379. const in_addr_t public_net = public & test_netmask;
  380. const in_addr_t local_net = local & test_netmask;
  381. const in_addr_t remote_net = remote_netmask & test_netmask;
  382.  
  383. if (public == local || public == remote_netmask)
  384. {
  385. msg(M_WARN,
  386. "WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
  387. name,
  388. print_in_addr_t(public, 0, &gc),
  389. print_in_addr_t(local, 0, &gc),
  390. print_in_addr_t(remote_netmask, 0, &gc),
  391. ifconfig_warn_how_to_silence);
  392. }
  393.  
  394. if (public_net == local_net || public_net == remote_net)
  395. {
  396. msg(M_WARN,
  397. "WARNING: potential conflict between --%s address [%s] and --ifconfig address pair [%s, %s] -- this is a warning only that is triggered when local/remote addresses exist within the same /24 subnet as --ifconfig endpoints. %s",
  398. name,
  399. print_in_addr_t(public, 0, &gc),
  400. print_in_addr_t(local, 0, &gc),
  401. print_in_addr_t(remote_netmask, 0, &gc),
  402. ifconfig_warn_how_to_silence);
  403. }
  404. }
  405. else if (type == DEV_TYPE_TAP)
  406. {
  407. const in_addr_t public_network = public & remote_netmask;
  408. const in_addr_t virtual_network = local & remote_netmask;
  409. if (public_network == virtual_network)
  410. {
  411. msg(M_WARN,
  412. "WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
  413. name,
  414. print_in_addr_t(public, 0, &gc),
  415. print_in_addr_t(local, 0, &gc),
  416. print_in_addr_t(remote_netmask, 0, &gc),
  417. ifconfig_warn_how_to_silence);
  418. }
  419. }
  420. }
  421. gc_free(&gc);
  422. }
  423.  
  424. /*
  425. * Issue a warning if ip/netmask (on the virtual IP network) conflicts with
  426. * the settings on the local LAN. This is designed to flag issues where
  427. * (for example) the OpenVPN server LAN is running on 192.168.1.x, but then
  428. * an OpenVPN client tries to connect from a public location that is also running
  429. * off of a router set to 192.168.1.x.
  430. */
  431. void
  432. check_subnet_conflict(const in_addr_t ip,
  433. const in_addr_t netmask,
  434. const char *prefix)
  435. {
  436. #if 0 /* too many false positives */
  437. struct gc_arena gc = gc_new();
  438. in_addr_t lan_gw = 0;
  439. in_addr_t lan_netmask = 0;
  440.  
  441. if (get_default_gateway(&lan_gw, &lan_netmask) && lan_netmask)
  442. {
  443. const in_addr_t lan_network = lan_gw & lan_netmask;
  444. const in_addr_t network = ip & netmask;
  445.  
  446. /* do the two subnets defined by network/netmask and lan_network/lan_netmask intersect? */
  447. if ((network & lan_netmask) == lan_network
  448. || (lan_network & netmask) == network)
  449. {
  450. msg(M_WARN, "WARNING: potential %s subnet conflict between local LAN [%s/%s] and remote VPN [%s/%s]",
  451. prefix,
  452. print_in_addr_t(lan_network, 0, &gc),
  453. print_in_addr_t(lan_netmask, 0, &gc),
  454. print_in_addr_t(network, 0, &gc),
  455. print_in_addr_t(netmask, 0, &gc));
  456. }
  457. }
  458. gc_free(&gc);
  459. #endif /* if 0 */
  460. }
  461.  
  462. void
  463. warn_on_use_of_common_subnets(void)
  464. {
  465. struct gc_arena gc = gc_new();
  466. struct route_gateway_info rgi;
  467. const int needed = (RGI_ADDR_DEFINED|RGI_NETMASK_DEFINED);
  468.  
  469. get_default_gateway(&rgi);
  470. if ((rgi.flags & needed) == needed)
  471. {
  472. const in_addr_t lan_network = rgi.gateway.addr & rgi.gateway.netmask;
  473. if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
  474. {
  475. msg(M_WARN, "NOTE: your local LAN uses the extremely common subnet address 192.168.0.x or 192.168.1.x. Be aware that this might create routing conflicts if you connect to the VPN server from public locations such as internet cafes that use the same subnet.");
  476. }
  477. }
  478. gc_free(&gc);
  479. }
  480.  
  481. /*
  482. * Return a string to be used for options compatibility check
  483. * between peers.
  484. */
  485. const char *
  486. ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
  487. {
  488. struct buffer out = alloc_buf_gc(256, gc);
  489. if (tt->did_ifconfig_setup && !disable)
  490. {
  491. if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
  492. {
  493. buf_printf(&out, "%s %s",
  494. print_in_addr_t(tt->local & tt->remote_netmask, 0, gc),
  495. print_in_addr_t(tt->remote_netmask, 0, gc));
  496. }
  497. else if (tt->type == DEV_TYPE_TUN)
  498. {
  499. const char *l, *r;
  500. if (remote)
  501. {
  502. r = print_in_addr_t(tt->local, 0, gc);
  503. l = print_in_addr_t(tt->remote_netmask, 0, gc);
  504. }
  505. else
  506. {
  507. l = print_in_addr_t(tt->local, 0, gc);
  508. r = print_in_addr_t(tt->remote_netmask, 0, gc);
  509. }
  510. buf_printf(&out, "%s %s", r, l);
  511. }
  512. else
  513. {
  514. buf_printf(&out, "[undef]");
  515. }
  516. }
  517. return BSTR(&out);
  518. }
  519.  
  520. /*
  521. * Return a status string describing wait state.
  522. */
  523. const char *
  524. tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
  525. {
  526. struct buffer out = alloc_buf_gc(64, gc);
  527. if (tt)
  528. {
  529. if (rwflags & EVENT_READ)
  530. {
  531. buf_printf(&out, "T%s",
  532. (tt->rwflags_debug & EVENT_READ) ? "R" : "r");
  533. #ifdef _WIN32
  534. buf_printf(&out, "%s",
  535. overlapped_io_state_ascii(&tt->reads));
  536. #endif
  537. }
  538. if (rwflags & EVENT_WRITE)
  539. {
  540. buf_printf(&out, "T%s",
  541. (tt->rwflags_debug & EVENT_WRITE) ? "W" : "w");
  542. #ifdef _WIN32
  543. buf_printf(&out, "%s",
  544. overlapped_io_state_ascii(&tt->writes));
  545. #endif
  546. }
  547. }
  548. else
  549. {
  550. buf_printf(&out, "T?");
  551. }
  552. return BSTR(&out);
  553. }
  554.  
  555. /*
  556. * Return true for point-to-point topology, false for subnet topology
  557. */
  558. bool
  559. is_tun_p2p(const struct tuntap *tt)
  560. {
  561. bool tun = false;
  562.  
  563. if (tt->type == DEV_TYPE_TAP
  564. || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  565. || tt->type == DEV_TYPE_NULL )
  566. {
  567. tun = false;
  568. }
  569. else if (tt->type == DEV_TYPE_TUN)
  570. {
  571. tun = true;
  572. }
  573. else
  574. {
  575. msg(M_FATAL, "Error: problem with tun vs. tap setting"); /* JYFIXME -- needs to be caught earlier, in init_tun? */
  576.  
  577. }
  578. return tun;
  579. }
  580.  
  581. /*
  582. * Set the ifconfig_* environment variables, both for IPv4 and IPv6
  583. */
  584. void
  585. do_ifconfig_setenv(const struct tuntap *tt, struct env_set *es)
  586. {
  587. struct gc_arena gc = gc_new();
  588. const char *ifconfig_local = print_in_addr_t(tt->local, 0, &gc);
  589. const char *ifconfig_remote_netmask = print_in_addr_t(tt->remote_netmask, 0, &gc);
  590.  
  591. /*
  592. * Set environmental variables with ifconfig parameters.
  593. */
  594. if (tt->did_ifconfig_setup)
  595. {
  596. bool tun = is_tun_p2p(tt);
  597.  
  598. setenv_str(es, "ifconfig_local", ifconfig_local);
  599. if (tun)
  600. {
  601. setenv_str(es, "ifconfig_remote", ifconfig_remote_netmask);
  602. }
  603. else
  604. {
  605. const char *ifconfig_broadcast = print_in_addr_t(tt->broadcast, 0, &gc);
  606. setenv_str(es, "ifconfig_netmask", ifconfig_remote_netmask);
  607. setenv_str(es, "ifconfig_broadcast", ifconfig_broadcast);
  608. }
  609. }
  610.  
  611. if (tt->did_ifconfig_ipv6_setup)
  612. {
  613. const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
  614. const char *ifconfig_ipv6_remote = print_in6_addr(tt->remote_ipv6, 0, &gc);
  615.  
  616. setenv_str(es, "ifconfig_ipv6_local", ifconfig_ipv6_local);
  617. setenv_int(es, "ifconfig_ipv6_netbits", tt->netbits_ipv6);
  618. setenv_str(es, "ifconfig_ipv6_remote", ifconfig_ipv6_remote);
  619. }
  620.  
  621. gc_free(&gc);
  622. }
  623.  
  624. /*
  625. * Init tun/tap object.
  626. *
  627. * Set up tuntap structure for ifconfig,
  628. * but don't execute yet.
  629. */
  630. struct tuntap *
  631. init_tun(const char *dev, /* --dev option */
  632. const char *dev_type, /* --dev-type option */
  633. int topology, /* one of the TOP_x values */
  634. const char *ifconfig_local_parm, /* --ifconfig parm 1 */
  635. const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
  636. const char *ifconfig_ipv6_local_parm, /* --ifconfig parm 1 IPv6 */
  637. int ifconfig_ipv6_netbits_parm,
  638. const char *ifconfig_ipv6_remote_parm, /* --ifconfig parm 2 IPv6 */
  639. struct addrinfo *local_public,
  640. struct addrinfo *remote_public,
  641. const bool strict_warn,
  642. struct env_set *es)
  643. {
  644. struct gc_arena gc = gc_new();
  645. struct tuntap *tt;
  646.  
  647. ALLOC_OBJ(tt, struct tuntap);
  648. clear_tuntap(tt);
  649.  
  650. tt->type = dev_type_enum(dev, dev_type);
  651. tt->topology = topology;
  652.  
  653. if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
  654. {
  655. bool tun = false;
  656.  
  657. /*
  658. * We only handle TUN/TAP devices here, not --dev null devices.
  659. */
  660. tun = is_tun_p2p(tt);
  661.  
  662. /*
  663. * Convert arguments to binary IPv4 addresses.
  664. */
  665.  
  666. tt->local = getaddr(
  667. GETADDR_RESOLVE
  668. | GETADDR_HOST_ORDER
  669. | GETADDR_FATAL_ON_SIGNAL
  670. | GETADDR_FATAL,
  671. ifconfig_local_parm,
  672. 0,
  673. NULL,
  674. NULL);
  675.  
  676. tt->remote_netmask = getaddr(
  677. (tun ? GETADDR_RESOLVE : 0)
  678. | GETADDR_HOST_ORDER
  679. | GETADDR_FATAL_ON_SIGNAL
  680. | GETADDR_FATAL,
  681. ifconfig_remote_netmask_parm,
  682. 0,
  683. NULL,
  684. NULL);
  685.  
  686. /*
  687. * Look for common errors in --ifconfig parms
  688. */
  689. if (strict_warn)
  690. {
  691. struct addrinfo *curele;
  692. ifconfig_sanity_check(tt->type == DEV_TYPE_TUN, tt->remote_netmask, tt->topology);
  693.  
  694. /*
  695. * If local_public or remote_public addresses are defined,
  696. * make sure they do not clash with our virtual subnet.
  697. */
  698.  
  699. for (curele = local_public; curele; curele = curele->ai_next)
  700. {
  701. if (curele->ai_family == AF_INET)
  702. {
  703. check_addr_clash("local",
  704. tt->type,
  705. ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
  706. tt->local,
  707. tt->remote_netmask);
  708. }
  709. }
  710.  
  711. for (curele = remote_public; curele; curele = curele->ai_next)
  712. {
  713. if (curele->ai_family == AF_INET)
  714. {
  715. check_addr_clash("remote",
  716. tt->type,
  717. ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
  718. tt->local,
  719. tt->remote_netmask);
  720. }
  721. }
  722.  
  723. if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
  724. {
  725. check_subnet_conflict(tt->local, tt->remote_netmask, "TUN/TAP adapter");
  726. }
  727. else if (tt->type == DEV_TYPE_TUN)
  728. {
  729. check_subnet_conflict(tt->local, IPV4_NETMASK_HOST, "TUN/TAP adapter");
  730. }
  731. }
  732.  
  733. /*
  734. * If TAP-style interface, generate broadcast address.
  735. */
  736. if (!tun)
  737. {
  738. tt->broadcast = generate_ifconfig_broadcast_addr(tt->local, tt->remote_netmask);
  739. }
  740.  
  741. #ifdef _WIN32
  742. /*
  743. * Make sure that both ifconfig addresses are part of the
  744. * same .252 subnet.
  745. */
  746. if (tun)
  747. {
  748. verify_255_255_255_252(tt->local, tt->remote_netmask);
  749. tt->adapter_netmask = ~3;
  750. }
  751. else
  752. {
  753. tt->adapter_netmask = tt->remote_netmask;
  754. }
  755. #endif
  756.  
  757. tt->did_ifconfig_setup = true;
  758. }
  759.  
  760. if (ifconfig_ipv6_local_parm && ifconfig_ipv6_remote_parm)
  761. {
  762.  
  763. /*
  764. * Convert arguments to binary IPv6 addresses.
  765. */
  766.  
  767. if (inet_pton( AF_INET6, ifconfig_ipv6_local_parm, &tt->local_ipv6 ) != 1
  768. || inet_pton( AF_INET6, ifconfig_ipv6_remote_parm, &tt->remote_ipv6 ) != 1)
  769. {
  770. msg( M_FATAL, "init_tun: problem converting IPv6 ifconfig addresses %s and %s to binary", ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
  771. }
  772. tt->netbits_ipv6 = ifconfig_ipv6_netbits_parm;
  773.  
  774. tt->did_ifconfig_ipv6_setup = true;
  775. }
  776.  
  777. /*
  778. * Set environmental variables with ifconfig parameters.
  779. */
  780. if (es)
  781. {
  782. do_ifconfig_setenv(tt, es);
  783. }
  784.  
  785. gc_free(&gc);
  786. return tt;
  787. }
  788.  
  789. /*
  790. * Platform specific tun initializations
  791. */
  792. void
  793. init_tun_post(struct tuntap *tt,
  794. const struct frame *frame,
  795. const struct tuntap_options *options)
  796. {
  797. tt->options = *options;
  798. #ifdef _WIN32
  799. overlapped_io_init(&tt->reads, frame, FALSE, true);
  800. overlapped_io_init(&tt->writes, frame, TRUE, true);
  801. tt->rw_handle.read = tt->reads.overlapped.hEvent;
  802. tt->rw_handle.write = tt->writes.overlapped.hEvent;
  803. tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
  804. #endif
  805. }
  806.  
  807. #if defined(_WIN32) \
  808. || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
  809.  
  810. /* some of the platforms will auto-add a "network route" pointing
  811. * to the interface on "ifconfig tunX 2001:db8::1/64", others need
  812. * an extra call to "route add..."
  813. * -> helper function to simplify code below
  814. */
  815. void
  816. add_route_connected_v6_net(struct tuntap *tt,
  817. const struct env_set *es)
  818. {
  819. struct route_ipv6 r6;
  820.  
  821. CLEAR(r6);
  822. r6.network = tt->local_ipv6;
  823. r6.netbits = tt->netbits_ipv6;
  824. r6.gateway = tt->local_ipv6;
  825. r6.metric = 0; /* connected route */
  826. r6.flags = RT_DEFINED | RT_METRIC_DEFINED;
  827. add_route_ipv6(&r6, tt, 0, es);
  828. }
  829.  
  830. void
  831. delete_route_connected_v6_net(struct tuntap *tt,
  832. const struct env_set *es)
  833. {
  834. struct route_ipv6 r6;
  835.  
  836. CLEAR(r6);
  837. r6.network = tt->local_ipv6;
  838. r6.netbits = tt->netbits_ipv6;
  839. r6.gateway = tt->local_ipv6;
  840. r6.metric = 0; /* connected route */
  841. r6.flags = RT_DEFINED | RT_ADDED | RT_METRIC_DEFINED;
  842. route_ipv6_clear_host_bits(&r6);
  843. delete_route_ipv6(&r6, tt, 0, es);
  844. }
  845. #endif /* if defined(_WIN32) || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) */
  846.  
  847. #if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
  848. || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
  849. /* we can't use true subnet mode on tun on all platforms, as that
  850. * conflicts with IPv6 (wants to use ND then, which we don't do),
  851. * but the OSes want "a remote address that is different from ours"
  852. * - so we construct one, normally the first in the subnet, but if
  853. * this is the same as ours, use the second one.
  854. * The actual address does not matter at all, as the tun interface
  855. * is still point to point and no layer 2 resolution is done...
  856. */
  857.  
  858. in_addr_t
  859. create_arbitrary_remote( struct tuntap *tt )
  860. {
  861. in_addr_t remote;
  862.  
  863. remote = (tt->local & tt->remote_netmask) +1;
  864.  
  865. if (remote == tt->local)
  866. {
  867. remote++;
  868. }
  869.  
  870. return remote;
  871. }
  872. #endif
  873.  
  874. /* execute the ifconfig command through the shell */
  875. void
  876. do_ifconfig(struct tuntap *tt,
  877. const char *actual, /* actual device name */
  878. int tun_mtu,
  879. const struct env_set *es)
  880. {
  881. struct gc_arena gc = gc_new();
  882.  
  883. if (tt->did_ifconfig_setup)
  884. {
  885. bool tun = false;
  886. const char *ifconfig_local = NULL;
  887. const char *ifconfig_remote_netmask = NULL;
  888. const char *ifconfig_broadcast = NULL;
  889. const char *ifconfig_ipv6_local = NULL;
  890. bool do_ipv6 = false;
  891. struct argv argv = argv_new();
  892.  
  893. msg( D_LOW, "do_ifconfig, tt->did_ifconfig_ipv6_setup=%d",
  894. tt->did_ifconfig_ipv6_setup );
  895.  
  896. /*
  897. * We only handle TUN/TAP devices here, not --dev null devices.
  898. */
  899. tun = is_tun_p2p(tt);
  900.  
  901. /*
  902. * Set ifconfig parameters
  903. */
  904. ifconfig_local = print_in_addr_t(tt->local, 0, &gc);
  905. ifconfig_remote_netmask = print_in_addr_t(tt->remote_netmask, 0, &gc);
  906.  
  907. if (tt->did_ifconfig_ipv6_setup)
  908. {
  909. ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
  910. do_ipv6 = true;
  911. }
  912.  
  913. /*
  914. * If TAP-style device, generate broadcast address.
  915. */
  916. if (!tun)
  917. {
  918. ifconfig_broadcast = print_in_addr_t(tt->broadcast, 0, &gc);
  919. }
  920.  
  921. #ifdef ENABLE_MANAGEMENT
  922. if (management)
  923. {
  924. management_set_state(management,
  925. OPENVPN_STATE_ASSIGN_IP,
  926. NULL,
  927. &tt->local,
  928. &tt->local_ipv6,
  929. NULL,
  930. NULL);
  931. }
  932. #endif
  933.  
  934.  
  935. #if defined(TARGET_LINUX)
  936. #ifdef ENABLE_IPROUTE
  937. /*
  938. * Set the MTU for the device
  939. */
  940. argv_printf(&argv,
  941. "%s link set dev %s up mtu %d",
  942. iproute_path,
  943. actual,
  944. tun_mtu
  945. );
  946. argv_msg(M_INFO, &argv);
  947. openvpn_execve_check(&argv, es, S_FATAL, "Linux ip link set failed");
  948.  
  949. if (tun)
  950. {
  951.  
  952. /*
  953. * Set the address for the device
  954. */
  955. argv_printf(&argv,
  956. "%s addr add dev %s local %s peer %s",
  957. iproute_path,
  958. actual,
  959. ifconfig_local,
  960. ifconfig_remote_netmask
  961. );
  962. argv_msg(M_INFO, &argv);
  963. openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed");
  964. }
  965. else
  966. {
  967. argv_printf(&argv,
  968. "%s addr add dev %s %s/%d broadcast %s",
  969. iproute_path,
  970. actual,
  971. ifconfig_local,
  972. netmask_to_netbits2(tt->remote_netmask),
  973. ifconfig_broadcast
  974. );
  975. argv_msg(M_INFO, &argv);
  976. openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed");
  977. }
  978. if (do_ipv6)
  979. {
  980. argv_printf( &argv,
  981. "%s -6 addr add %s/%d dev %s",
  982. iproute_path,
  983. ifconfig_ipv6_local,
  984. tt->netbits_ipv6,
  985. actual
  986. );
  987. argv_msg(M_INFO, &argv);
  988. openvpn_execve_check(&argv, es, S_FATAL, "Linux ip -6 addr add failed");
  989. }
  990. tt->did_ifconfig = true;
  991. #else /* ifdef ENABLE_IPROUTE */
  992. if (tun)
  993. {
  994. argv_printf(&argv,
  995. "%s %s %s pointopoint %s mtu %d",
  996. IFCONFIG_PATH,
  997. actual,
  998. ifconfig_local,
  999. ifconfig_remote_netmask,
  1000. tun_mtu
  1001. );
  1002. }
  1003. else
  1004. {
  1005. argv_printf(&argv,
  1006. "%s %s %s netmask %s mtu %d broadcast %s",
  1007. IFCONFIG_PATH,
  1008. actual,
  1009. ifconfig_local,
  1010. ifconfig_remote_netmask,
  1011. tun_mtu,
  1012. ifconfig_broadcast
  1013. );
  1014. }
  1015. argv_msg(M_INFO, &argv);
  1016. openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig failed");
  1017. if (do_ipv6)
  1018. {
  1019. argv_printf(&argv,
  1020. "%s %s add %s/%d",
  1021. IFCONFIG_PATH,
  1022. actual,
  1023. ifconfig_ipv6_local,
  1024. tt->netbits_ipv6
  1025. );
  1026. argv_msg(M_INFO, &argv);
  1027. openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig inet6 failed");
  1028. }
  1029. tt->did_ifconfig = true;
  1030.  
  1031. #endif /*ENABLE_IPROUTE*/
  1032. #elif defined(TARGET_ANDROID)
  1033.  
  1034. if (do_ipv6)
  1035. {
  1036. struct buffer out6 = alloc_buf_gc(64, &gc);
  1037. buf_printf(&out6, "%s/%d", ifconfig_ipv6_local,tt->netbits_ipv6);
  1038. management_android_control(management, "IFCONFIG6",buf_bptr(&out6));
  1039. }
  1040.  
  1041. struct buffer out = alloc_buf_gc(64, &gc);
  1042.  
  1043. char *top;
  1044. switch (tt->topology)
  1045. {
  1046. case TOP_NET30:
  1047. top = "net30";
  1048. break;
  1049.  
  1050. case TOP_P2P:
  1051. top = "p2p";
  1052. break;
  1053.  
  1054. case TOP_SUBNET:
  1055. top = "subnet";
  1056. break;
  1057.  
  1058. default:
  1059. top = "undef";
  1060. }
  1061.  
  1062. buf_printf(&out, "%s %s %d %s", ifconfig_local, ifconfig_remote_netmask, tun_mtu, top);
  1063. management_android_control(management, "IFCONFIG", buf_bptr(&out));
  1064.  
  1065. #elif defined(TARGET_SOLARIS)
  1066. /* Solaris 2.6 (and 7?) cannot set all parameters in one go...
  1067. * example:
  1068. * ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 up
  1069. * ifconfig tun2 netmask 255.255.255.255
  1070. */
  1071. if (tun)
  1072. {
  1073. argv_printf(&argv,
  1074. "%s %s %s %s mtu %d up",
  1075. IFCONFIG_PATH,
  1076. actual,
  1077. ifconfig_local,
  1078. ifconfig_remote_netmask,
  1079. tun_mtu
  1080. );
  1081.  
  1082. argv_msg(M_INFO, &argv);
  1083. if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig phase-1 failed"))
  1084. {
  1085. solaris_error_close(tt, es, actual, false);
  1086. }
  1087.  
  1088. argv_printf(&argv,
  1089. "%s %s netmask 255.255.255.255",
  1090. IFCONFIG_PATH,
  1091. actual
  1092. );
  1093. }
  1094. else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1095. {
  1096. argv_printf(&argv,
  1097. "%s %s %s %s netmask %s mtu %d up",
  1098. IFCONFIG_PATH,
  1099. actual,
  1100. ifconfig_local,
  1101. ifconfig_local,
  1102. ifconfig_remote_netmask,
  1103. tun_mtu
  1104. );
  1105. }
  1106. else
  1107. {
  1108. argv_printf(&argv,
  1109. " %s %s %s netmask %s broadcast + up",
  1110. IFCONFIG_PATH,
  1111. actual,
  1112. ifconfig_local,
  1113. ifconfig_remote_netmask
  1114. );
  1115. }
  1116.  
  1117. argv_msg(M_INFO, &argv);
  1118. if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig phase-2 failed"))
  1119. {
  1120. solaris_error_close(tt, es, actual, false);
  1121. }
  1122.  
  1123. if (do_ipv6)
  1124. {
  1125. argv_printf(&argv, "%s %s inet6 unplumb",
  1126. IFCONFIG_PATH, actual );
  1127. argv_msg(M_INFO, &argv);
  1128. openvpn_execve_check(&argv, es, 0, NULL);
  1129.  
  1130. if (tt->type == DEV_TYPE_TUN)
  1131. {
  1132. const char *ifconfig_ipv6_remote =
  1133. print_in6_addr(tt->remote_ipv6, 0, &gc);
  1134.  
  1135. argv_printf(&argv,
  1136. "%s %s inet6 plumb %s/%d %s up",
  1137. IFCONFIG_PATH,
  1138. actual,
  1139. ifconfig_ipv6_local,
  1140. tt->netbits_ipv6,
  1141. ifconfig_ipv6_remote
  1142. );
  1143. }
  1144. else /* tap mode */
  1145. {
  1146. /* base IPv6 tap interface needs to be brought up first
  1147. */
  1148. argv_printf(&argv, "%s %s inet6 plumb up",
  1149. IFCONFIG_PATH, actual );
  1150. argv_msg(M_INFO, &argv);
  1151. if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig IPv6 (prepare) failed"))
  1152. {
  1153. solaris_error_close(tt, es, actual, true);
  1154. }
  1155.  
  1156. /* we might need to do "ifconfig %s inet6 auto-dhcp drop"
  1157. * after the system has noticed the interface and fired up
  1158. * the DHCPv6 client - but this takes quite a while, and the
  1159. * server will ignore the DHCPv6 packets anyway. So we don't.
  1160. */
  1161.  
  1162. /* static IPv6 addresses need to go to a subinterface (tap0:1)
  1163. */
  1164. argv_printf(&argv,
  1165. "%s %s inet6 addif %s/%d up",
  1166. IFCONFIG_PATH, actual,
  1167. ifconfig_ipv6_local, tt->netbits_ipv6 );
  1168. }
  1169. argv_msg(M_INFO, &argv);
  1170. if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig IPv6 failed"))
  1171. {
  1172. solaris_error_close(tt, es, actual, true);
  1173. }
  1174. }
  1175.  
  1176. if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1177. {
  1178. /* Add a network route for the local tun interface */
  1179. struct route_ipv4 r;
  1180. CLEAR(r);
  1181. r.flags = RT_DEFINED | RT_METRIC_DEFINED;
  1182. r.network = tt->local & tt->remote_netmask;
  1183. r.netmask = tt->remote_netmask;
  1184. r.gateway = tt->local;
  1185. r.metric = 0;
  1186. add_route(&r, tt, 0, NULL, es);
  1187. }
  1188.  
  1189. tt->did_ifconfig = true;
  1190.  
  1191. #elif defined(TARGET_OPENBSD)
  1192.  
  1193. in_addr_t remote_end; /* for "virtual" subnet topology */
  1194.  
  1195. /*
  1196. * On OpenBSD, tun interfaces are persistent if created with
  1197. * "ifconfig tunX create", and auto-destroyed if created by
  1198. * opening "/dev/tunX" (so we just use the /dev/tunX)
  1199. */
  1200.  
  1201. /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
  1202. if (tun)
  1203. {
  1204. argv_printf(&argv,
  1205. "%s %s %s %s mtu %d netmask 255.255.255.255 up -link0",
  1206. IFCONFIG_PATH,
  1207. actual,
  1208. ifconfig_local,
  1209. ifconfig_remote_netmask,
  1210. tun_mtu
  1211. );
  1212. }
  1213. else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1214. {
  1215. remote_end = create_arbitrary_remote( tt );
  1216. argv_printf(&argv,
  1217. "%s %s %s %s mtu %d netmask %s up -link0",
  1218. IFCONFIG_PATH,
  1219. actual,
  1220. ifconfig_local,
  1221. print_in_addr_t(remote_end, 0, &gc),
  1222. tun_mtu,
  1223. ifconfig_remote_netmask
  1224. );
  1225. }
  1226. else
  1227. {
  1228. argv_printf(&argv,
  1229. "%s %s %s netmask %s mtu %d broadcast %s link0",
  1230. IFCONFIG_PATH,
  1231. actual,
  1232. ifconfig_local,
  1233. ifconfig_remote_netmask,
  1234. tun_mtu,
  1235. ifconfig_broadcast
  1236. );
  1237. }
  1238. argv_msg(M_INFO, &argv);
  1239. openvpn_execve_check(&argv, es, S_FATAL, "OpenBSD ifconfig failed");
  1240.  
  1241. /* Add a network route for the local tun interface */
  1242. if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1243. {
  1244. struct route_ipv4 r;
  1245. CLEAR(r);
  1246. r.flags = RT_DEFINED;
  1247. r.network = tt->local & tt->remote_netmask;
  1248. r.netmask = tt->remote_netmask;
  1249. r.gateway = remote_end;
  1250. add_route(&r, tt, 0, NULL, es);
  1251. }
  1252.  
  1253. if (do_ipv6)
  1254. {
  1255. argv_printf(&argv,
  1256. "%s %s inet6 %s/%d",
  1257. IFCONFIG_PATH,
  1258. actual,
  1259. ifconfig_ipv6_local,
  1260. tt->netbits_ipv6
  1261. );
  1262. argv_msg(M_INFO, &argv);
  1263. openvpn_execve_check(&argv, es, S_FATAL, "OpenBSD ifconfig inet6 failed");
  1264.  
  1265. /* and, hooray, we explicitely need to add a route... */
  1266. add_route_connected_v6_net(tt, es);
  1267. }
  1268. tt->did_ifconfig = true;
  1269.  
  1270. #elif defined(TARGET_NETBSD)
  1271.  
  1272. in_addr_t remote_end; /* for "virtual" subnet topology */
  1273.  
  1274. if (tun)
  1275. {
  1276. argv_printf(&argv,
  1277. "%s %s %s %s mtu %d netmask 255.255.255.255 up",
  1278. IFCONFIG_PATH,
  1279. actual,
  1280. ifconfig_local,
  1281. ifconfig_remote_netmask,
  1282. tun_mtu
  1283. );
  1284. }
  1285. else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1286. {
  1287. remote_end = create_arbitrary_remote( tt );
  1288. argv_printf(&argv,
  1289. "%s %s %s %s mtu %d netmask %s up",
  1290. IFCONFIG_PATH,
  1291. actual,
  1292. ifconfig_local,
  1293. print_in_addr_t(remote_end, 0, &gc),
  1294. tun_mtu,
  1295. ifconfig_remote_netmask
  1296. );
  1297. }
  1298. else
  1299. {
  1300. /*
  1301. * NetBSD has distinct tun and tap devices
  1302. * so we don't need the "link0" extra parameter to specify we want to do
  1303. * tunneling at the ethernet level
  1304. */
  1305. argv_printf(&argv,
  1306. "%s %s %s netmask %s mtu %d broadcast %s",
  1307. IFCONFIG_PATH,
  1308. actual,
  1309. ifconfig_local,
  1310. ifconfig_remote_netmask,
  1311. tun_mtu,
  1312. ifconfig_broadcast
  1313. );
  1314. }
  1315. argv_msg(M_INFO, &argv);
  1316. openvpn_execve_check(&argv, es, S_FATAL, "NetBSD ifconfig failed");
  1317.  
  1318. /* Add a network route for the local tun interface */
  1319. if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1320. {
  1321. struct route_ipv4 r;
  1322. CLEAR(r);
  1323. r.flags = RT_DEFINED;
  1324. r.network = tt->local & tt->remote_netmask;
  1325. r.netmask = tt->remote_netmask;
  1326. r.gateway = remote_end;
  1327. add_route(&r, tt, 0, NULL, es);
  1328. }
  1329.  
  1330. if (do_ipv6)
  1331. {
  1332. argv_printf(&argv,
  1333. "%s %s inet6 %s/%d",
  1334. IFCONFIG_PATH,
  1335. actual,
  1336. ifconfig_ipv6_local,
  1337. tt->netbits_ipv6
  1338. );
  1339. argv_msg(M_INFO, &argv);
  1340. openvpn_execve_check(&argv, es, S_FATAL, "NetBSD ifconfig inet6 failed");
  1341.  
  1342. /* and, hooray, we explicitely need to add a route... */
  1343. add_route_connected_v6_net(tt, es);
  1344. }
  1345. tt->did_ifconfig = true;
  1346.  
  1347. #elif defined(TARGET_DARWIN)
  1348. /*
  1349. * Darwin (i.e. Mac OS X) seems to exhibit similar behaviour to OpenBSD...
  1350. */
  1351.  
  1352. argv_printf(&argv,
  1353. "%s %s delete",
  1354. IFCONFIG_PATH,
  1355. actual);
  1356. argv_msg(M_INFO, &argv);
  1357. openvpn_execve_check(&argv, es, 0, NULL);
  1358. msg(M_INFO, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
  1359.  
  1360.  
  1361. /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
  1362. if (tun)
  1363. {
  1364. argv_printf(&argv,
  1365. "%s %s %s %s mtu %d netmask 255.255.255.255 up",
  1366. IFCONFIG_PATH,
  1367. actual,
  1368. ifconfig_local,
  1369. ifconfig_remote_netmask,
  1370. tun_mtu
  1371. );
  1372. }
  1373. else
  1374. {
  1375. if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1376. {
  1377. argv_printf(&argv,
  1378. "%s %s %s %s netmask %s mtu %d up",
  1379. IFCONFIG_PATH,
  1380. actual,
  1381. ifconfig_local,
  1382. ifconfig_local,
  1383. ifconfig_remote_netmask,
  1384. tun_mtu
  1385. );
  1386. }
  1387. else
  1388. {
  1389. argv_printf(&argv,
  1390. "%s %s %s netmask %s mtu %d up",
  1391. IFCONFIG_PATH,
  1392. actual,
  1393. ifconfig_local,
  1394. ifconfig_remote_netmask,
  1395. tun_mtu
  1396. );
  1397. }
  1398. }
  1399.  
  1400. argv_msg(M_INFO, &argv);
  1401. openvpn_execve_check(&argv, es, S_FATAL, "Mac OS X ifconfig failed");
  1402. tt->did_ifconfig = true;
  1403.  
  1404. /* Add a network route for the local tun interface */
  1405. if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1406. {
  1407. struct route_ipv4 r;
  1408. CLEAR(r);
  1409. r.flags = RT_DEFINED;
  1410. r.network = tt->local & tt->remote_netmask;
  1411. r.netmask = tt->remote_netmask;
  1412. r.gateway = tt->local;
  1413. add_route(&r, tt, 0, NULL, es);
  1414. }
  1415.  
  1416. if (do_ipv6)
  1417. {
  1418. argv_printf(&argv,
  1419. "%s %s inet6 %s/%d",
  1420. IFCONFIG_PATH,
  1421. actual,
  1422. ifconfig_ipv6_local,
  1423. tt->netbits_ipv6
  1424. );
  1425. argv_msg(M_INFO, &argv);
  1426. openvpn_execve_check(&argv, es, S_FATAL, "MacOS X ifconfig inet6 failed");
  1427.  
  1428. /* and, hooray, we explicitely need to add a route... */
  1429. add_route_connected_v6_net(tt, es);
  1430. }
  1431.  
  1432. #elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
  1433.  
  1434. in_addr_t remote_end; /* for "virtual" subnet topology */
  1435.  
  1436. /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
  1437. if (tun)
  1438. {
  1439. argv_printf(&argv,
  1440. "%s %s %s %s mtu %d netmask 255.255.255.255 up",
  1441. IFCONFIG_PATH,
  1442. actual,
  1443. ifconfig_local,
  1444. ifconfig_remote_netmask,
  1445. tun_mtu
  1446. );
  1447. }
  1448. else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1449. {
  1450. remote_end = create_arbitrary_remote( tt );
  1451. argv_printf(&argv,
  1452. "%s %s %s %s mtu %d netmask %s up",
  1453. IFCONFIG_PATH,
  1454. actual,
  1455. ifconfig_local,
  1456. print_in_addr_t(remote_end, 0, &gc),
  1457. tun_mtu,
  1458. ifconfig_remote_netmask
  1459. );
  1460. }
  1461. else
  1462. {
  1463. argv_printf(&argv,
  1464. "%s %s %s netmask %s mtu %d up",
  1465. IFCONFIG_PATH,
  1466. actual,
  1467. ifconfig_local,
  1468. ifconfig_remote_netmask,
  1469. tun_mtu
  1470. );
  1471. }
  1472.  
  1473. argv_msg(M_INFO, &argv);
  1474. openvpn_execve_check(&argv, es, S_FATAL, "FreeBSD ifconfig failed");
  1475. tt->did_ifconfig = true;
  1476.  
  1477. /* Add a network route for the local tun interface */
  1478. if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
  1479. {
  1480. struct route_ipv4 r;
  1481. CLEAR(r);
  1482. r.flags = RT_DEFINED;
  1483. r.network = tt->local & tt->remote_netmask;
  1484. r.netmask = tt->remote_netmask;
  1485. r.gateway = remote_end;
  1486. add_route(&r, tt, 0, NULL, es);
  1487. }
  1488.  
  1489. if (do_ipv6)
  1490. {
  1491. argv_printf(&argv,
  1492. "%s %s inet6 %s/%d",
  1493. IFCONFIG_PATH,
  1494. actual,
  1495. ifconfig_ipv6_local,
  1496. tt->netbits_ipv6
  1497. );
  1498. argv_msg(M_INFO, &argv);
  1499. openvpn_execve_check(&argv, es, S_FATAL, "FreeBSD ifconfig inet6 failed");
  1500. }
  1501.  
  1502. #elif defined(TARGET_AIX)
  1503. {
  1504. /* AIX ifconfig will complain if it can't find ODM path in env */
  1505. struct env_set *aix_es = env_set_create(NULL);
  1506. env_set_add( aix_es, "ODMDIR=/etc/objrepos" );
  1507.  
  1508. if (tun)
  1509. {
  1510. msg(M_FATAL, "no tun support on AIX (canthappen)");
  1511. }
  1512.  
  1513. /* example: ifconfig tap0 172.30.1.1 netmask 255.255.254.0 up */
  1514. argv_printf(&argv,
  1515. "%s %s %s netmask %s mtu %d up",
  1516. IFCONFIG_PATH,
  1517. actual,
  1518. ifconfig_local,
  1519. ifconfig_remote_netmask,
  1520. tun_mtu
  1521. );
  1522.  
  1523. argv_msg(M_INFO, &argv);
  1524. openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig failed");
  1525. tt->did_ifconfig = true;
  1526.  
  1527. if (do_ipv6)
  1528. {
  1529. argv_printf(&argv,
  1530. "%s %s inet6 %s/%d",
  1531. IFCONFIG_PATH,
  1532. actual,
  1533. ifconfig_ipv6_local,
  1534. tt->netbits_ipv6
  1535. );
  1536. argv_msg(M_INFO, &argv);
  1537. openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig inet6 failed");
  1538. }
  1539. env_set_destroy(aix_es);
  1540. }
  1541. #elif defined (_WIN32)
  1542. {
  1543. ASSERT(actual != NULL);
  1544.  
  1545. switch (tt->options.ip_win32_type)
  1546. {
  1547. case IPW32_SET_MANUAL:
  1548. msg(M_INFO, "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
  1549. actual,
  1550. ifconfig_local,
  1551. print_in_addr_t(tt->adapter_netmask, 0, &gc));
  1552. break;
  1553.  
  1554. case IPW32_SET_NETSH:
  1555. netsh_ifconfig(&tt->options,
  1556. actual,
  1557. tt->local,
  1558. tt->adapter_netmask,
  1559. NI_IP_NETMASK|NI_OPTIONS);
  1560.  
  1561. break;
  1562. }
  1563. tt->did_ifconfig = true;
  1564. }
  1565.  
  1566. if (do_ipv6)
  1567. {
  1568. if (tt->options.ip_win32_type == IPW32_SET_MANUAL)
  1569. {
  1570. msg(M_INFO, "******** NOTE: Please manually set the v6 IP of '%s' to %s (if it is not already set)",
  1571. actual,
  1572. ifconfig_ipv6_local);
  1573. }
  1574. else if (tt->options.msg_channel)
  1575. {
  1576. do_address_service(true, AF_INET6, tt);
  1577. do_dns6_service(true, tt);
  1578. }
  1579. else
  1580. {
  1581. /* example: netsh interface ipv6 set address interface=42 2001:608:8003::d store=active */
  1582. char iface[64];
  1583. openvpn_snprintf(iface, sizeof(iface), "interface=%lu", tt->adapter_index );
  1584. argv_printf(&argv,
  1585. "%s%sc interface ipv6 set address %s %s store=active",
  1586. get_win_sys_path(),
  1587. NETSH_PATH_SUFFIX,
  1588. iface,
  1589. ifconfig_ipv6_local );
  1590. netsh_command(&argv, 4, M_FATAL);
  1591. /* set ipv6 dns servers if any are specified */
  1592. netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len, actual);
  1593. }
  1594.  
  1595. /* explicit route needed */
  1596. if (tt->options.ip_win32_type != IPW32_SET_MANUAL)
  1597. {
  1598. add_route_connected_v6_net(tt, es);
  1599. }
  1600. }
  1601. #else /* if defined(TARGET_LINUX) */
  1602. msg(M_FATAL, "Sorry, but I don't know how to do 'ifconfig' commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");
  1603. #endif /* if defined(TARGET_LINUX) */
  1604. argv_reset(&argv);
  1605. }
  1606. gc_free(&gc);
  1607. }
  1608.  
  1609. static void
  1610. clear_tuntap(struct tuntap *tuntap)
  1611. {
  1612. CLEAR(*tuntap);
  1613. #ifdef _WIN32
  1614. tuntap->hand = NULL;
  1615. #else
  1616. tuntap->fd = -1;
  1617. #endif
  1618. #ifdef TARGET_SOLARIS
  1619. tuntap->ip_fd = -1;
  1620. #endif
  1621. }
  1622.  
  1623. static void
  1624. open_null(struct tuntap *tt)
  1625. {
  1626. tt->actual_name = string_alloc("null", NULL);
  1627. }
  1628.  
  1629.  
  1630. #if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H)
  1631.  
  1632. /*
  1633. * OpenBSD and Mac OS X when using utun
  1634. * have a slightly incompatible TUN device from
  1635. * the rest of the world, in that it prepends a
  1636. * uint32 to the beginning of the IP header
  1637. * to designate the protocol (why not just
  1638. * look at the version field in the IP header to
  1639. * determine v4 or v6?).
  1640. *
  1641. * We strip off this field on reads and
  1642. * put it back on writes.
  1643. *
  1644. * I have not tested TAP devices on OpenBSD,
  1645. * but I have conditionalized the special
  1646. * TUN handling code described above to
  1647. * go away for TAP devices.
  1648. */
  1649.  
  1650. #include <netinet/ip.h>
  1651. #include <sys/uio.h>
  1652.  
  1653. static inline int
  1654. header_modify_read_write_return(int len)
  1655. {
  1656. if (len > 0)
  1657. {
  1658. return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
  1659. }
  1660. else
  1661. {
  1662. return len;
  1663. }
  1664. }
  1665.  
  1666. int
  1667. write_tun_header(struct tuntap *tt, uint8_t *buf, int len)
  1668. {
  1669. if (tt->type == DEV_TYPE_TUN)
  1670. {
  1671. u_int32_t type;
  1672. struct iovec iv[2];
  1673. struct openvpn_iphdr *iph;
  1674.  
  1675. iph = (struct openvpn_iphdr *) buf;
  1676.  
  1677. if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
  1678. {
  1679. type = htonl(AF_INET6);
  1680. }
  1681. else
  1682. {
  1683. type = htonl(AF_INET);
  1684. }
  1685.  
  1686. iv[0].iov_base = &type;
  1687. iv[0].iov_len = sizeof(type);
  1688. iv[1].iov_base = buf;
  1689. iv[1].iov_len = len;
  1690.  
  1691. return header_modify_read_write_return(writev(tt->fd, iv, 2));
  1692. }
  1693. else
  1694. {
  1695. return write(tt->fd, buf, len);
  1696. }
  1697. }
  1698.  
  1699. int
  1700. read_tun_header(struct tuntap *tt, uint8_t *buf, int len)
  1701. {
  1702. if (tt->type == DEV_TYPE_TUN)
  1703. {
  1704. u_int32_t type;
  1705. struct iovec iv[2];
  1706.  
  1707. iv[0].iov_base = &type;
  1708. iv[0].iov_len = sizeof(type);
  1709. iv[1].iov_base = buf;
  1710. iv[1].iov_len = len;
  1711.  
  1712. return header_modify_read_write_return(readv(tt->fd, iv, 2));
  1713. }
  1714. else
  1715. {
  1716. return read(tt->fd, buf, len);
  1717. }
  1718. }
  1719. #endif /* if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H) */
  1720.  
  1721.  
  1722. #if !(defined(_WIN32) || defined(TARGET_LINUX))
  1723. static void
  1724. open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
  1725. bool dynamic, struct tuntap *tt)
  1726. {
  1727. char tunname[256];
  1728. char dynamic_name[256];
  1729. bool dynamic_opened = false;
  1730.  
  1731. if (tt->type == DEV_TYPE_NULL)
  1732. {
  1733. open_null(tt);
  1734. }
  1735. else
  1736. {
  1737. /*
  1738. * --dev-node specified, so open an explicit device node
  1739. */
  1740. if (dev_node)
  1741. {
  1742. openvpn_snprintf(tunname, sizeof(tunname), "%s", dev_node);
  1743. }
  1744. else
  1745. {
  1746. /*
  1747. * dynamic open is indicated by --dev specified without
  1748. * explicit unit number. Try opening /dev/[dev]n
  1749. * where n = [0, 255].
  1750. */
  1751. #ifdef TARGET_NETBSD
  1752. /* on NetBSD, tap (but not tun) devices are opened by
  1753. * opening /dev/tap and then querying the system about the
  1754. * actual device name (tap0, tap1, ...) assigned
  1755. */
  1756. if (dynamic && strcmp( dev, "tap" ) == 0)
  1757. {
  1758. struct ifreq ifr;
  1759. if ((tt->fd = open( "/dev/tap", O_RDWR)) < 0)
  1760. {
  1761. msg(M_FATAL, "Cannot allocate NetBSD TAP dev dynamically");
  1762. }
  1763. if (ioctl( tt->fd, TAPGIFNAME, (void *)&ifr ) < 0)
  1764. {
  1765. msg(M_FATAL, "Cannot query NetBSD TAP device name");
  1766. }
  1767. CLEAR(dynamic_name);
  1768. strncpy( dynamic_name, ifr.ifr_name, sizeof(dynamic_name)-1 );
  1769. dynamic_opened = true;
  1770. openvpn_snprintf(tunname, sizeof(tunname), "/dev/%s", dynamic_name );
  1771. }
  1772. else
  1773. #endif
  1774.  
  1775. if (dynamic && !has_digit((unsigned char *)dev))
  1776. {
  1777. int i;
  1778. for (i = 0; i < 256; ++i)
  1779. {
  1780. openvpn_snprintf(tunname, sizeof(tunname),
  1781. "/dev/%s%d", dev, i);
  1782. openvpn_snprintf(dynamic_name, sizeof(dynamic_name),
  1783. "%s%d", dev, i);
  1784. if ((tt->fd = open(tunname, O_RDWR)) > 0)
  1785. {
  1786. dynamic_opened = true;
  1787. break;
  1788. }
  1789. msg(D_READ_WRITE | M_ERRNO, "Tried opening %s (failed)", tunname);
  1790. }
  1791. if (!dynamic_opened)
  1792. {
  1793. msg(M_FATAL, "Cannot allocate TUN/TAP dev dynamically");
  1794. }
  1795. }
  1796. /*
  1797. * explicit unit number specified
  1798. */
  1799. else
  1800. {
  1801. openvpn_snprintf(tunname, sizeof(tunname), "/dev/%s", dev);
  1802. }
  1803. }
  1804.  
  1805. if (!dynamic_opened)
  1806. {
  1807. /* has named device existed before? if so, don't destroy at end */
  1808. if (if_nametoindex( dev ) > 0)
  1809. {
  1810. msg(M_INFO, "TUN/TAP device %s exists previously, keep at program end", dev );
  1811. tt->persistent_if = true;
  1812. }
  1813.  
  1814. if ((tt->fd = open(tunname, O_RDWR)) < 0)
  1815. {
  1816. msg(M_ERR, "Cannot open TUN/TAP dev %s", tunname);
  1817. }
  1818. }
  1819.  
  1820. set_nonblock(tt->fd);
  1821. set_cloexec(tt->fd); /* don't pass fd to scripts */
  1822. msg(M_INFO, "TUN/TAP device %s opened", tunname);
  1823.  
  1824. /* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
  1825. tt->actual_name = string_alloc(dynamic_opened ? dynamic_name : dev, NULL);
  1826. }
  1827. }
  1828. #endif /* !_WIN32 && !TARGET_LINUX */
  1829.  
  1830. #if !defined(_WIN32)
  1831. static void
  1832. close_tun_generic(struct tuntap *tt)
  1833. {
  1834. if (tt->fd >= 0)
  1835. {
  1836. close(tt->fd);
  1837. }
  1838. if (tt->actual_name)
  1839. {
  1840. free(tt->actual_name);
  1841. }
  1842. clear_tuntap(tt);
  1843. }
  1844. #endif /* !_WIN32 */
  1845.  
  1846. #if defined (TARGET_ANDROID)
  1847. void
  1848. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  1849. {
  1850. #define ANDROID_TUNNAME "vpnservice-tun"
  1851. struct user_pass up;
  1852. struct gc_arena gc = gc_new();
  1853. bool opentun;
  1854.  
  1855. int oldtunfd = tt->fd;
  1856.  
  1857. /* Prefer IPv6 DNS servers,
  1858. * Android will use the DNS server in the order we specify*/
  1859. for (int i = 0; i < tt->options.dns6_len; i++)
  1860. {
  1861. management_android_control(management, "DNS6SERVER",
  1862. print_in6_addr(tt->options.dns6[i], 0, &gc));
  1863. }
  1864.  
  1865. for (int i = 0; i < tt->options.dns_len; i++)
  1866. {
  1867. management_android_control(management, "DNSSERVER",
  1868. print_in_addr_t(tt->options.dns[i], 0, &gc));
  1869. }
  1870.  
  1871. if (tt->options.domain)
  1872. {
  1873. management_android_control(management, "DNSDOMAIN", tt->options.domain);
  1874. }
  1875.  
  1876. int android_method = managment_android_persisttun_action(management);
  1877.  
  1878. /* Android 4.4 workaround */
  1879. if (oldtunfd >=0 && android_method == ANDROID_OPEN_AFTER_CLOSE)
  1880. {
  1881. close(oldtunfd);
  1882. management_sleep(2);
  1883. }
  1884.  
  1885. if (oldtunfd >=0 && android_method == ANDROID_KEEP_OLD_TUN)
  1886. {
  1887. /* keep the old fd */
  1888. opentun = true;
  1889. }
  1890. else
  1891. {
  1892. opentun = management_android_control(management, "OPENTUN", dev);
  1893. /* Pick up the fd from management interface after calling the
  1894. * OPENTUN command */
  1895. tt->fd = management->connection.lastfdreceived;
  1896. management->connection.lastfdreceived = -1;
  1897. }
  1898.  
  1899. if (oldtunfd>=0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
  1900. {
  1901. close(oldtunfd);
  1902. }
  1903.  
  1904. /* Set the actual name to a dummy name */
  1905. tt->actual_name = string_alloc(ANDROID_TUNNAME, NULL);
  1906.  
  1907. if ((tt->fd < 0) || !opentun)
  1908. {
  1909. msg(M_ERR, "ERROR: Cannot open TUN");
  1910. }
  1911.  
  1912. gc_free(&gc);
  1913. }
  1914.  
  1915. void
  1916. close_tun(struct tuntap *tt)
  1917. {
  1918. if (tt)
  1919. {
  1920. close_tun_generic(tt);
  1921. free(tt);
  1922. }
  1923. }
  1924.  
  1925. int
  1926. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  1927. {
  1928. return write(tt->fd, buf, len);
  1929. }
  1930.  
  1931. int
  1932. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  1933. {
  1934. return read(tt->fd, buf, len);
  1935. }
  1936.  
  1937. #elif defined(TARGET_LINUX)
  1938.  
  1939. #ifndef HAVE_LINUX_SOCKIOS_H
  1940. #error header file linux/sockios.h required
  1941. #endif
  1942.  
  1943. #if !PEDANTIC
  1944.  
  1945. void
  1946. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  1947. {
  1948. struct ifreq ifr;
  1949.  
  1950. /*
  1951. * We handle --dev null specially, we do not open /dev/null for this.
  1952. */
  1953. if (tt->type == DEV_TYPE_NULL)
  1954. {
  1955. open_null(tt);
  1956. }
  1957. else
  1958. {
  1959. /*
  1960. * Process --dev-node
  1961. */
  1962. const char *node = dev_node;
  1963. if (!node)
  1964. {
  1965. node = "/dev/net/tun";
  1966. }
  1967.  
  1968. /*
  1969. * Open the interface
  1970. */
  1971. if ((tt->fd = open(node, O_RDWR)) < 0)
  1972. {
  1973. msg(M_ERR, "ERROR: Cannot open TUN/TAP dev %s", node);
  1974. }
  1975.  
  1976. /*
  1977. * Process --tun-ipv6
  1978. */
  1979. CLEAR(ifr);
  1980. ifr.ifr_flags = IFF_NO_PI;
  1981.  
  1982. #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
  1983. ifr.ifr_flags |= IFF_ONE_QUEUE;
  1984. #endif
  1985.  
  1986. /*
  1987. * Figure out if tun or tap device
  1988. */
  1989. if (tt->type == DEV_TYPE_TUN)
  1990. {
  1991. ifr.ifr_flags |= IFF_TUN;
  1992. }
  1993. else if (tt->type == DEV_TYPE_TAP)
  1994. {
  1995. ifr.ifr_flags |= IFF_TAP;
  1996. }
  1997. else
  1998. {
  1999. msg(M_FATAL, "I don't recognize device %s as a tun or tap device",
  2000. dev);
  2001. }
  2002.  
  2003. /*
  2004. * Set an explicit name, if --dev is not tun or tap
  2005. */
  2006. if (strcmp(dev, "tun") && strcmp(dev, "tap"))
  2007. {
  2008. strncpynt(ifr.ifr_name, dev, IFNAMSIZ);
  2009. }
  2010.  
  2011. /*
  2012. * Use special ioctl that configures tun/tap device with the parms
  2013. * we set in ifr
  2014. */
  2015. if (ioctl(tt->fd, TUNSETIFF, (void *) &ifr) < 0)
  2016. {
  2017. msg(M_ERR, "ERROR: Cannot ioctl TUNSETIFF %s", dev);
  2018. }
  2019.  
  2020. msg(M_INFO, "TUN/TAP device %s opened", ifr.ifr_name);
  2021.  
  2022. /*
  2023. * Try making the TX send queue bigger
  2024. */
  2025. #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
  2026. if (tt->options.txqueuelen)
  2027. {
  2028. struct ifreq netifr;
  2029. int ctl_fd;
  2030.  
  2031. if ((ctl_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
  2032. {
  2033. CLEAR(netifr);
  2034. strncpynt(netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
  2035. netifr.ifr_qlen = tt->options.txqueuelen;
  2036. if (ioctl(ctl_fd, SIOCSIFTXQLEN, (void *) &netifr) >= 0)
  2037. {
  2038. msg(D_OSBUF, "TUN/TAP TX queue length set to %d", tt->options.txqueuelen);
  2039. }
  2040. else
  2041. {
  2042. msg(M_WARN | M_ERRNO, "Note: Cannot set tx queue length on %s", ifr.ifr_name);
  2043. }
  2044. close(ctl_fd);
  2045. }
  2046. else
  2047. {
  2048. msg(M_WARN | M_ERRNO, "Note: Cannot open control socket on %s", ifr.ifr_name);
  2049. }
  2050. }
  2051. #endif /* if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN) */
  2052.  
  2053. set_nonblock(tt->fd);
  2054. set_cloexec(tt->fd);
  2055. tt->actual_name = string_alloc(ifr.ifr_name, NULL);
  2056. }
  2057. return;
  2058. }
  2059.  
  2060. #else /* if !PEDANTIC */
  2061.  
  2062. void
  2063. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2064. {
  2065. ASSERT(0);
  2066. }
  2067.  
  2068. #endif /* !PENDANTIC */
  2069.  
  2070. #ifdef ENABLE_FEATURE_TUN_PERSIST
  2071.  
  2072. void
  2073. tuncfg(const char *dev, const char *dev_type, const char *dev_node, int persist_mode, const char *username, const char *groupname, const struct tuntap_options *options)
  2074. {
  2075. struct tuntap *tt;
  2076.  
  2077. ALLOC_OBJ(tt, struct tuntap);
  2078. clear_tuntap(tt);
  2079. tt->type = dev_type_enum(dev, dev_type);
  2080. tt->options = *options;
  2081. open_tun(dev, dev_type, dev_node, tt);
  2082. if (ioctl(tt->fd, TUNSETPERSIST, persist_mode) < 0)
  2083. {
  2084. msg(M_ERR, "Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
  2085. }
  2086. if (username != NULL)
  2087. {
  2088. struct platform_state_user platform_state_user;
  2089.  
  2090. if (!platform_user_get(username, &platform_state_user))
  2091. {
  2092. msg(M_ERR, "Cannot get user entry for %s", username);
  2093. }
  2094. else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.pw->pw_uid) < 0)
  2095. {
  2096. msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
  2097. }
  2098. }
  2099. if (groupname != NULL)
  2100. {
  2101. struct platform_state_group platform_state_group;
  2102.  
  2103. if (!platform_group_get(groupname, &platform_state_group))
  2104. {
  2105. msg(M_ERR, "Cannot get group entry for %s", groupname);
  2106. }
  2107. else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gr->gr_gid) < 0)
  2108. {
  2109. msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", groupname, dev);
  2110. }
  2111. }
  2112. close_tun(tt);
  2113. msg(M_INFO, "Persist state set to: %s", (persist_mode ? "ON" : "OFF"));
  2114. }
  2115.  
  2116. #endif /* ENABLE_FEATURE_TUN_PERSIST */
  2117.  
  2118. void
  2119. close_tun(struct tuntap *tt)
  2120. {
  2121. if (tt)
  2122. {
  2123. if (tt->type != DEV_TYPE_NULL && tt->did_ifconfig)
  2124. {
  2125. struct argv argv = argv_new();
  2126. struct gc_arena gc = gc_new();
  2127.  
  2128. #ifdef ENABLE_IPROUTE
  2129. if (is_tun_p2p(tt))
  2130. {
  2131. argv_printf(&argv,
  2132. "%s addr del dev %s local %s peer %s",
  2133. iproute_path,
  2134. tt->actual_name,
  2135. print_in_addr_t(tt->local, 0, &gc),
  2136. print_in_addr_t(tt->remote_netmask, 0, &gc)
  2137. );
  2138. }
  2139. else
  2140. {
  2141. argv_printf(&argv,
  2142. "%s addr del dev %s %s/%d",
  2143. iproute_path,
  2144. tt->actual_name,
  2145. print_in_addr_t(tt->local, 0, &gc),
  2146. netmask_to_netbits2(tt->remote_netmask)
  2147. );
  2148. }
  2149. #else /* ifdef ENABLE_IPROUTE */
  2150. argv_printf(&argv,
  2151. "%s %s 0.0.0.0",
  2152. IFCONFIG_PATH,
  2153. tt->actual_name
  2154. );
  2155. #endif /* ifdef ENABLE_IPROUTE */
  2156.  
  2157. argv_msg(M_INFO, &argv);
  2158. openvpn_execve_check(&argv, NULL, 0, "Linux ip addr del failed");
  2159.  
  2160. if (tt->did_ifconfig_ipv6_setup)
  2161. {
  2162. const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
  2163.  
  2164. #ifdef ENABLE_IPROUTE
  2165. argv_printf(&argv, "%s -6 addr del %s/%d dev %s",
  2166. iproute_path,
  2167. ifconfig_ipv6_local,
  2168. tt->netbits_ipv6,
  2169. tt->actual_name
  2170. );
  2171. argv_msg(M_INFO, &argv);
  2172. openvpn_execve_check(&argv, NULL, 0, "Linux ip -6 addr del failed");
  2173. #else /* ifdef ENABLE_IPROUTE */
  2174. argv_printf(&argv,
  2175. "%s %s del %s/%d",
  2176. IFCONFIG_PATH,
  2177. tt->actual_name,
  2178. ifconfig_ipv6_local,
  2179. tt->netbits_ipv6
  2180. );
  2181. argv_msg(M_INFO, &argv);
  2182. openvpn_execve_check(&argv, NULL, 0, "Linux ifconfig inet6 del failed");
  2183. #endif
  2184. }
  2185.  
  2186. argv_reset(&argv);
  2187. gc_free(&gc);
  2188. }
  2189. close_tun_generic(tt);
  2190. free(tt);
  2191. }
  2192. }
  2193.  
  2194. int
  2195. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2196. {
  2197. return write(tt->fd, buf, len);
  2198. }
  2199.  
  2200. int
  2201. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2202. {
  2203. return read(tt->fd, buf, len);
  2204. }
  2205.  
  2206. #elif defined(TARGET_SOLARIS)
  2207.  
  2208. #ifndef TUNNEWPPA
  2209. #error I need the symbol TUNNEWPPA from net/if_tun.h
  2210. #endif
  2211.  
  2212. void
  2213. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2214. {
  2215. int if_fd, ip_muxid, arp_muxid, arp_fd, ppa = -1;
  2216. struct lifreq ifr;
  2217. const char *ptr;
  2218. const char *ip_node, *arp_node;
  2219. const char *dev_tuntap_type;
  2220. int link_type;
  2221. bool is_tun;
  2222. struct strioctl strioc_if, strioc_ppa;
  2223.  
  2224. /* improved generic TUN/TAP driver from
  2225. * http://www.whiteboard.ne.jp/~admin2/tuntap/
  2226. * has IPv6 support
  2227. */
  2228. CLEAR(ifr);
  2229.  
  2230. if (tt->type == DEV_TYPE_NULL)
  2231. {
  2232. open_null(tt);
  2233. return;
  2234. }
  2235.  
  2236. if (tt->type == DEV_TYPE_TUN)
  2237. {
  2238. ip_node = "/dev/udp";
  2239. if (!dev_node)
  2240. {
  2241. dev_node = "/dev/tun";
  2242. }
  2243. dev_tuntap_type = "tun";
  2244. link_type = I_PLINK;
  2245. is_tun = true;
  2246. }
  2247. else if (tt->type == DEV_TYPE_TAP)
  2248. {
  2249. ip_node = "/dev/udp";
  2250. if (!dev_node)
  2251. {
  2252. dev_node = "/dev/tap";
  2253. }
  2254. arp_node = dev_node;
  2255. dev_tuntap_type = "tap";
  2256. link_type = I_PLINK; /* was: I_LINK */
  2257. is_tun = false;
  2258. }
  2259. else
  2260. {
  2261. msg(M_FATAL, "I don't recognize device %s as a tun or tap device",
  2262. dev);
  2263. }
  2264.  
  2265. if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
  2266. {
  2267. msg(M_ERR, "Can't open %s", ip_node);
  2268. }
  2269.  
  2270. if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
  2271. {
  2272. msg(M_ERR, "Can't open %s", dev_node);
  2273. }
  2274.  
  2275. /* get unit number */
  2276. if (*dev)
  2277. {
  2278. ptr = dev;
  2279. while (*ptr && !isdigit((int) *ptr))
  2280. {
  2281. ptr++;
  2282. }
  2283. ppa = atoi(ptr);
  2284. }
  2285.  
  2286. /* Assign a new PPA and get its unit number. */
  2287. strioc_ppa.ic_cmd = TUNNEWPPA;
  2288. strioc_ppa.ic_timout = 0;
  2289. strioc_ppa.ic_len = sizeof(ppa);
  2290. strioc_ppa.ic_dp = (char *)&ppa;
  2291.  
  2292. if (*ptr == '\0') /* no number given, try dynamic */
  2293. {
  2294. bool found_one = false;
  2295. while (!found_one && ppa < 64)
  2296. {
  2297. int new_ppa = ioctl(tt->fd, I_STR, &strioc_ppa);
  2298. if (new_ppa >= 0)
  2299. {
  2300. msg( M_INFO, "open_tun: got dynamic interface '%s%d'", dev_tuntap_type, new_ppa );
  2301. ppa = new_ppa;
  2302. found_one = true;
  2303. break;
  2304. }
  2305. if (errno != EEXIST)
  2306. {
  2307. msg(M_ERR, "open_tun: unexpected error trying to find free %s interface", dev_tuntap_type );
  2308. }
  2309. ppa++;
  2310. }
  2311. if (!found_one)
  2312. {
  2313. msg(M_ERR, "open_tun: could not find free %s interface, give up.", dev_tuntap_type );
  2314. }
  2315. }
  2316. else /* try this particular one */
  2317. {
  2318. if ((ppa = ioctl(tt->fd, I_STR, &strioc_ppa)) < 0)
  2319. {
  2320. msg(M_ERR, "Can't assign PPA for new interface (%s%d)", dev_tuntap_type, ppa );
  2321. }
  2322. }
  2323.  
  2324. if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
  2325. {
  2326. msg(M_ERR, "Can't open %s (2)", dev_node);
  2327. }
  2328.  
  2329. if (ioctl(if_fd, I_PUSH, "ip") < 0)
  2330. {
  2331. msg(M_ERR, "Can't push IP module");
  2332. }
  2333.  
  2334. if (tt->type == DEV_TYPE_TUN)
  2335. {
  2336. /* Assign ppa according to the unit number returned by tun device */
  2337. if (ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0)
  2338. {
  2339. msg(M_ERR, "Can't set PPA %d", ppa);
  2340. }
  2341. }
  2342.  
  2343. tt->actual_name = (char *) malloc(32);
  2344. check_malloc_return(tt->actual_name);
  2345.  
  2346. openvpn_snprintf(tt->actual_name, 32, "%s%d", dev_tuntap_type, ppa);
  2347.  
  2348. if (tt->type == DEV_TYPE_TAP)
  2349. {
  2350. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
  2351. {
  2352. msg(M_ERR, "Can't get flags\n");
  2353. }
  2354. strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
  2355. ifr.lifr_ppa = ppa;
  2356. /* Assign ppa according to the unit number returned by tun device */
  2357. if (ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0)
  2358. {
  2359. msg(M_ERR, "Can't set PPA %d", ppa);
  2360. }
  2361. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
  2362. {
  2363. msg(M_ERR, "Can't get flags\n");
  2364. }
  2365. /* Push arp module to if_fd */
  2366. if (ioctl(if_fd, I_PUSH, "arp") < 0)
  2367. {
  2368. msg(M_ERR, "Can't push ARP module");
  2369. }
  2370.  
  2371. /* Pop any modules on the stream */
  2372. while (true)
  2373. {
  2374. if (ioctl(tt->ip_fd, I_POP, NULL) < 0)
  2375. {
  2376. break;
  2377. }
  2378. }
  2379. /* Push arp module to ip_fd */
  2380. if (ioctl(tt->ip_fd, I_PUSH, "arp") < 0)
  2381. {
  2382. msg(M_ERR, "Can't push ARP module\n");
  2383. }
  2384.  
  2385. /* Open arp_fd */
  2386. if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
  2387. {
  2388. msg(M_ERR, "Can't open %s\n", arp_node);
  2389. }
  2390. /* Push arp module to arp_fd */
  2391. if (ioctl(arp_fd, I_PUSH, "arp") < 0)
  2392. {
  2393. msg(M_ERR, "Can't push ARP module\n");
  2394. }
  2395.  
  2396. /* Set ifname to arp */
  2397. strioc_if.ic_cmd = SIOCSLIFNAME;
  2398. strioc_if.ic_timout = 0;
  2399. strioc_if.ic_len = sizeof(ifr);
  2400. strioc_if.ic_dp = (char *)&ifr;
  2401. if (ioctl(arp_fd, I_STR, &strioc_if) < 0)
  2402. {
  2403. msg(M_ERR, "Can't set ifname to arp\n");
  2404. }
  2405. }
  2406.  
  2407. if ((ip_muxid = ioctl(tt->ip_fd, link_type, if_fd)) < 0)
  2408. {
  2409. msg(M_ERR, "Can't link %s device to IP", dev_tuntap_type);
  2410. }
  2411.  
  2412. if (tt->type == DEV_TYPE_TAP)
  2413. {
  2414. if ((arp_muxid = ioctl(tt->ip_fd, link_type, arp_fd)) < 0)
  2415. {
  2416. msg(M_ERR, "Can't link %s device to ARP", dev_tuntap_type);
  2417. }
  2418. close(arp_fd);
  2419. }
  2420.  
  2421. CLEAR(ifr);
  2422. strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
  2423. ifr.lifr_ip_muxid = ip_muxid;
  2424. if (tt->type == DEV_TYPE_TAP)
  2425. {
  2426. ifr.lifr_arp_muxid = arp_muxid;
  2427. }
  2428.  
  2429. if (ioctl(tt->ip_fd, SIOCSLIFMUXID, &ifr) < 0)
  2430. {
  2431. if (tt->type == DEV_TYPE_TAP)
  2432. {
  2433. ioctl(tt->ip_fd, I_PUNLINK, arp_muxid);
  2434. }
  2435. ioctl(tt->ip_fd, I_PUNLINK, ip_muxid);
  2436. msg(M_ERR, "Can't set multiplexor id");
  2437. }
  2438.  
  2439. set_nonblock(tt->fd);
  2440. set_cloexec(tt->fd);
  2441. set_cloexec(tt->ip_fd);
  2442.  
  2443. msg(M_INFO, "TUN/TAP device %s opened", tt->actual_name);
  2444. }
  2445.  
  2446. static void
  2447. solaris_close_tun(struct tuntap *tt)
  2448. {
  2449. if (tt)
  2450. {
  2451. /* IPv6 interfaces need to be 'manually' de-configured */
  2452. if (tt->did_ifconfig_ipv6_setup)
  2453. {
  2454. struct argv argv = argv_new();
  2455. argv_printf( &argv, "%s %s inet6 unplumb",
  2456. IFCONFIG_PATH, tt->actual_name );
  2457. argv_msg(M_INFO, &argv);
  2458. openvpn_execve_check(&argv, NULL, 0, "Solaris ifconfig inet6 unplumb failed");
  2459. argv_reset(&argv);
  2460. }
  2461.  
  2462. if (tt->ip_fd >= 0)
  2463. {
  2464. struct lifreq ifr;
  2465. CLEAR(ifr);
  2466. strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
  2467.  
  2468. if (ioctl(tt->ip_fd, SIOCGLIFFLAGS, &ifr) < 0)
  2469. {
  2470. msg(M_WARN | M_ERRNO, "Can't get iface flags");
  2471. }
  2472.  
  2473. if (ioctl(tt->ip_fd, SIOCGLIFMUXID, &ifr) < 0)
  2474. {
  2475. msg(M_WARN | M_ERRNO, "Can't get multiplexor id");
  2476. }
  2477.  
  2478. if (tt->type == DEV_TYPE_TAP)
  2479. {
  2480. if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_arp_muxid) < 0)
  2481. {
  2482. msg(M_WARN | M_ERRNO, "Can't unlink interface(arp)");
  2483. }
  2484. }
  2485.  
  2486. if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_ip_muxid) < 0)
  2487. {
  2488. msg(M_WARN | M_ERRNO, "Can't unlink interface(ip)");
  2489. }
  2490.  
  2491. close(tt->ip_fd);
  2492. tt->ip_fd = -1;
  2493. }
  2494.  
  2495. if (tt->fd >= 0)
  2496. {
  2497. close(tt->fd);
  2498. tt->fd = -1;
  2499. }
  2500. }
  2501. }
  2502.  
  2503. /*
  2504. * Close TUN device.
  2505. */
  2506. void
  2507. close_tun(struct tuntap *tt)
  2508. {
  2509. if (tt)
  2510. {
  2511. solaris_close_tun(tt);
  2512.  
  2513. if (tt->actual_name)
  2514. {
  2515. free(tt->actual_name);
  2516. }
  2517.  
  2518. clear_tuntap(tt);
  2519. free(tt);
  2520. }
  2521. }
  2522.  
  2523. static void
  2524. solaris_error_close(struct tuntap *tt, const struct env_set *es,
  2525. const char *actual, bool unplumb_inet6 )
  2526. {
  2527. struct argv argv = argv_new();
  2528.  
  2529. if (unplumb_inet6)
  2530. {
  2531. argv_printf( &argv, "%s %s inet6 unplumb",
  2532. IFCONFIG_PATH, actual );
  2533. argv_msg(M_INFO, &argv);
  2534. openvpn_execve_check(&argv, es, 0, "Solaris ifconfig inet6 unplumb failed");
  2535. }
  2536.  
  2537. argv_printf(&argv,
  2538. "%s %s unplumb",
  2539. IFCONFIG_PATH,
  2540. actual);
  2541.  
  2542. argv_msg(M_INFO, &argv);
  2543. openvpn_execve_check(&argv, es, 0, "Solaris ifconfig unplumb failed");
  2544. close_tun(tt);
  2545. msg(M_FATAL, "Solaris ifconfig failed");
  2546. argv_reset(&argv);
  2547. }
  2548.  
  2549. int
  2550. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2551. {
  2552. struct strbuf sbuf;
  2553. sbuf.len = len;
  2554. sbuf.buf = (char *)buf;
  2555. return putmsg(tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
  2556. }
  2557.  
  2558. int
  2559. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2560. {
  2561. struct strbuf sbuf;
  2562. int f = 0;
  2563.  
  2564. sbuf.maxlen = len;
  2565. sbuf.buf = (char *)buf;
  2566. return getmsg(tt->fd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
  2567. }
  2568.  
  2569. #elif defined(TARGET_OPENBSD)
  2570.  
  2571. void
  2572. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2573. {
  2574. open_tun_generic(dev, dev_type, dev_node, true, tt);
  2575.  
  2576. /* Enable multicast on the interface */
  2577. if (tt->fd >= 0)
  2578. {
  2579. struct tuninfo info;
  2580.  
  2581. if (ioctl(tt->fd, TUNGIFINFO, &info) < 0)
  2582. {
  2583. msg(M_WARN | M_ERRNO, "Can't get interface info");
  2584. }
  2585.  
  2586. #ifdef IFF_MULTICAST /* openbsd 4.x doesn't have this */
  2587. info.flags |= IFF_MULTICAST;
  2588. #endif
  2589.  
  2590. if (ioctl(tt->fd, TUNSIFINFO, &info) < 0)
  2591. {
  2592. msg(M_WARN | M_ERRNO, "Can't set interface info");
  2593. }
  2594. }
  2595. }
  2596.  
  2597. /* tun(4): "If the device was created by opening /dev/tunN, it will be
  2598. * automatically destroyed. Devices created via ifconfig(8) are
  2599. * only marked as not running and traffic will be dropped
  2600. * returning EHOSTDOWN."
  2601. * --> no special handling should be needed - *but* OpenBSD is misbehaving
  2602. * here: if the interface was put in tap mode ("ifconfig tunN link0"), it
  2603. * *will* stay around, and needs to be cleaned up manually
  2604. */
  2605.  
  2606. void
  2607. close_tun(struct tuntap *tt)
  2608. {
  2609. /* only *TAP* devices need destroying, tun devices auto-self-destruct
  2610. */
  2611. if (tt && (tt->type == DEV_TYPE_TUN || tt->persistent_if ) )
  2612. {
  2613. close_tun_generic(tt);
  2614. free(tt);
  2615. }
  2616. else if (tt)
  2617. {
  2618. struct gc_arena gc = gc_new();
  2619. struct argv argv = argv_new();
  2620.  
  2621. /* setup command, close tun dev (clears tt->actual_name!), run command
  2622. */
  2623.  
  2624. argv_printf(&argv, "%s %s destroy",
  2625. IFCONFIG_PATH, tt->actual_name);
  2626.  
  2627. close_tun_generic(tt);
  2628.  
  2629. argv_msg(M_INFO, &argv);
  2630. openvpn_execve_check(&argv, NULL, 0, "OpenBSD 'destroy tun interface' failed (non-critical)");
  2631.  
  2632. free(tt);
  2633. }
  2634. }
  2635.  
  2636. int
  2637. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2638. {
  2639. return write_tun_header(tt, buf, len);
  2640. }
  2641.  
  2642. int
  2643. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2644. {
  2645. return read_tun_header(tt, buf, len);
  2646. }
  2647.  
  2648. #elif defined(TARGET_NETBSD)
  2649.  
  2650. /*
  2651. * NetBSD before 4.0 does not support IPv6 on tun out of the box,
  2652. * but there exists a patch (sys/net/if_tun.c, 1.79->1.80, see PR 32944).
  2653. *
  2654. * NetBSD 4.0 and up do, but we need to put the tun interface into
  2655. * "multi_af" mode, which will prepend the address family to all packets
  2656. * (same as OpenBSD and FreeBSD). If this is not enabled, the kernel
  2657. * silently drops all IPv6 packets on output and gets confused on input.
  2658. *
  2659. * On earlier versions, multi_af is not available at all, so we have
  2660. * two different NetBSD code variants here :-(
  2661. *
  2662. */
  2663.  
  2664. void
  2665. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2666. {
  2667. open_tun_generic(dev, dev_type, dev_node, true, tt);
  2668.  
  2669. if (tt->fd >= 0)
  2670. {
  2671. int i = IFF_POINTOPOINT|IFF_MULTICAST;
  2672. ioctl(tt->fd, TUNSIFMODE, &i); /* multicast on */
  2673. i = 0;
  2674. ioctl(tt->fd, TUNSLMODE, &i); /* link layer mode off */
  2675.  
  2676. if (tt->type == DEV_TYPE_TUN)
  2677. {
  2678. i = 1;
  2679. if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0) /* multi-af mode on */
  2680. {
  2681. msg(M_WARN | M_ERRNO, "ioctl(TUNSIFHEAD)");
  2682. }
  2683. }
  2684. }
  2685. }
  2686.  
  2687. /* the current way OpenVPN handles tun devices on NetBSD leads to
  2688. * lingering tunX interfaces after close -> for a full cleanup, they
  2689. * need to be explicitely destroyed
  2690. */
  2691. void
  2692. close_tun(struct tuntap *tt)
  2693. {
  2694. /* only tun devices need destroying, tap devices auto-self-destruct
  2695. */
  2696. if (tt && ( tt->type != DEV_TYPE_TUN || tt->persistent_if ) )
  2697. {
  2698. close_tun_generic(tt);
  2699. free(tt);
  2700. }
  2701. else if (tt)
  2702. {
  2703. struct gc_arena gc = gc_new();
  2704. struct argv argv = argv_new();
  2705.  
  2706. /* setup command, close tun dev (clears tt->actual_name!), run command
  2707. */
  2708.  
  2709. argv_printf(&argv, "%s %s destroy",
  2710. IFCONFIG_PATH, tt->actual_name);
  2711.  
  2712. close_tun_generic(tt);
  2713.  
  2714. argv_msg(M_INFO, &argv);
  2715. openvpn_execve_check(&argv, NULL, 0, "NetBSD 'destroy tun interface' failed (non-critical)");
  2716.  
  2717. free(tt);
  2718. }
  2719. }
  2720.  
  2721. static inline int
  2722. netbsd_modify_read_write_return(int len)
  2723. {
  2724. if (len > 0)
  2725. {
  2726. return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
  2727. }
  2728. else
  2729. {
  2730. return len;
  2731. }
  2732. }
  2733.  
  2734. int
  2735. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2736. {
  2737. if (tt->type == DEV_TYPE_TUN)
  2738. {
  2739. u_int32_t type;
  2740. struct iovec iv[2];
  2741. struct openvpn_iphdr *iph;
  2742.  
  2743. iph = (struct openvpn_iphdr *) buf;
  2744.  
  2745. if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
  2746. {
  2747. type = htonl(AF_INET6);
  2748. }
  2749. else
  2750. {
  2751. type = htonl(AF_INET);
  2752. }
  2753.  
  2754. iv[0].iov_base = (char *)&type;
  2755. iv[0].iov_len = sizeof(type);
  2756. iv[1].iov_base = buf;
  2757. iv[1].iov_len = len;
  2758.  
  2759. return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
  2760. }
  2761. else
  2762. {
  2763. return write(tt->fd, buf, len);
  2764. }
  2765. }
  2766.  
  2767. int
  2768. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2769. {
  2770. if (tt->type == DEV_TYPE_TUN)
  2771. {
  2772. u_int32_t type;
  2773. struct iovec iv[2];
  2774.  
  2775. iv[0].iov_base = (char *)&type;
  2776. iv[0].iov_len = sizeof(type);
  2777. iv[1].iov_base = buf;
  2778. iv[1].iov_len = len;
  2779.  
  2780. return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
  2781. }
  2782. else
  2783. {
  2784. return read(tt->fd, buf, len);
  2785. }
  2786. }
  2787.  
  2788. #elif defined(TARGET_FREEBSD)
  2789.  
  2790. static inline int
  2791. freebsd_modify_read_write_return(int len)
  2792. {
  2793. if (len > 0)
  2794. {
  2795. return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
  2796. }
  2797. else
  2798. {
  2799. return len;
  2800. }
  2801. }
  2802.  
  2803. void
  2804. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2805. {
  2806. open_tun_generic(dev, dev_type, dev_node, true, tt);
  2807.  
  2808. if (tt->fd >= 0 && tt->type == DEV_TYPE_TUN)
  2809. {
  2810. int i = IFF_POINTOPOINT | IFF_MULTICAST;
  2811.  
  2812. if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
  2813. {
  2814. msg(M_WARN | M_ERRNO, "ioctl(TUNSIFMODE)");
  2815. }
  2816. i = 1;
  2817. if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
  2818. {
  2819. msg(M_WARN | M_ERRNO, "ioctl(TUNSIFHEAD)");
  2820. }
  2821. }
  2822. }
  2823.  
  2824. /* tun(4): "These network interfaces persist until the if_tun.ko module is
  2825. * unloaded, or until removed with the ifconfig(8) command."
  2826. * (verified for FreeBSD 6.3, 7.4, 8.2 and 9, same for tap(4))
  2827. *
  2828. * so, to avoid lingering tun/tap interfaces after OpenVPN quits,
  2829. * we need to call "ifconfig ... destroy" for cleanup
  2830. */
  2831. void
  2832. close_tun(struct tuntap *tt)
  2833. {
  2834. if (tt && tt->persistent_if) /* keep pre-existing if around */
  2835. {
  2836. close_tun_generic(tt);
  2837. free(tt);
  2838. }
  2839. else if (tt) /* close and destroy */
  2840. {
  2841. struct argv argv = argv_new();
  2842.  
  2843. /* setup command, close tun dev (clears tt->actual_name!), run command
  2844. */
  2845.  
  2846. argv_printf(&argv, "%s %s destroy",
  2847. IFCONFIG_PATH, tt->actual_name);
  2848.  
  2849. close_tun_generic(tt);
  2850.  
  2851. argv_msg(M_INFO, &argv);
  2852. openvpn_execve_check(&argv, NULL, 0, "FreeBSD 'destroy tun interface' failed (non-critical)");
  2853.  
  2854. free(tt);
  2855. }
  2856. }
  2857.  
  2858. int
  2859. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2860. {
  2861. if (tt->type == DEV_TYPE_TUN)
  2862. {
  2863. u_int32_t type;
  2864. struct iovec iv[2];
  2865. struct ip *iph;
  2866.  
  2867. iph = (struct ip *) buf;
  2868.  
  2869. if (iph->ip_v == 6)
  2870. {
  2871. type = htonl(AF_INET6);
  2872. }
  2873. else
  2874. {
  2875. type = htonl(AF_INET);
  2876. }
  2877.  
  2878. iv[0].iov_base = (char *)&type;
  2879. iv[0].iov_len = sizeof(type);
  2880. iv[1].iov_base = buf;
  2881. iv[1].iov_len = len;
  2882.  
  2883. return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
  2884. }
  2885. else
  2886. {
  2887. return write(tt->fd, buf, len);
  2888. }
  2889. }
  2890.  
  2891. int
  2892. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2893. {
  2894. if (tt->type == DEV_TYPE_TUN)
  2895. {
  2896. u_int32_t type;
  2897. struct iovec iv[2];
  2898.  
  2899. iv[0].iov_base = (char *)&type;
  2900. iv[0].iov_len = sizeof(type);
  2901. iv[1].iov_base = buf;
  2902. iv[1].iov_len = len;
  2903.  
  2904. return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
  2905. }
  2906. else
  2907. {
  2908. return read(tt->fd, buf, len);
  2909. }
  2910. }
  2911.  
  2912. #elif defined(TARGET_DRAGONFLY)
  2913.  
  2914. static inline int
  2915. dragonfly_modify_read_write_return(int len)
  2916. {
  2917. if (len > 0)
  2918. {
  2919. return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
  2920. }
  2921. else
  2922. {
  2923. return len;
  2924. }
  2925. }
  2926.  
  2927. void
  2928. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  2929. {
  2930. open_tun_generic(dev, dev_type, dev_node, true, tt);
  2931.  
  2932. if (tt->fd >= 0)
  2933. {
  2934. int i = 0;
  2935.  
  2936. /* Disable extended modes */
  2937. ioctl(tt->fd, TUNSLMODE, &i);
  2938. i = 1;
  2939. ioctl(tt->fd, TUNSIFHEAD, &i);
  2940. }
  2941. }
  2942.  
  2943. void
  2944. close_tun(struct tuntap *tt)
  2945. {
  2946. if (tt)
  2947. {
  2948. close_tun_generic(tt);
  2949. free(tt);
  2950. }
  2951. }
  2952.  
  2953. int
  2954. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  2955. {
  2956. if (tt->type == DEV_TYPE_TUN)
  2957. {
  2958. u_int32_t type;
  2959. struct iovec iv[2];
  2960. struct ip *iph;
  2961.  
  2962. iph = (struct ip *) buf;
  2963.  
  2964. if (iph->ip_v == 6)
  2965. {
  2966. type = htonl(AF_INET6);
  2967. }
  2968. else
  2969. {
  2970. type = htonl(AF_INET);
  2971. }
  2972.  
  2973. iv[0].iov_base = (char *)&type;
  2974. iv[0].iov_len = sizeof(type);
  2975. iv[1].iov_base = buf;
  2976. iv[1].iov_len = len;
  2977.  
  2978. return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
  2979. }
  2980. else
  2981. {
  2982. return write(tt->fd, buf, len);
  2983. }
  2984. }
  2985.  
  2986. int
  2987. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  2988. {
  2989. if (tt->type == DEV_TYPE_TUN)
  2990. {
  2991. u_int32_t type;
  2992. struct iovec iv[2];
  2993.  
  2994. iv[0].iov_base = (char *)&type;
  2995. iv[0].iov_len = sizeof(type);
  2996. iv[1].iov_base = buf;
  2997. iv[1].iov_len = len;
  2998.  
  2999. return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
  3000. }
  3001. else
  3002. {
  3003. return read(tt->fd, buf, len);
  3004. }
  3005. }
  3006.  
  3007. #elif defined(TARGET_DARWIN)
  3008.  
  3009. /* Darwin (MacOS X) is mostly "just use the generic stuff", but there
  3010. * is always one caveat...:
  3011. *
  3012. * If IPv6 is configured, and the tun device is closed, the IPv6 address
  3013. * configured to the tun interface changes to a lingering /128 route
  3014. * pointing to lo0. Need to unconfigure... (observed on 10.5)
  3015. */
  3016.  
  3017. /*
  3018. * utun is the native Darwin tun driver present since at least 10.7
  3019. * Thanks goes to Jonathan Levin for providing an example how to utun
  3020. * (http://newosxbook.com/src.jl?tree=listings&file=17-15-utun.c)
  3021. */
  3022.  
  3023. #ifdef HAVE_NET_IF_UTUN_H
  3024.  
  3025. /* Helper functions that tries to open utun device
  3026. * return -2 on early initialization failures (utun not supported
  3027. * at all (old OS X) and -1 on initlization failure of utun
  3028. * device (utun works but utunX is already used */
  3029. static
  3030. int
  3031. utun_open_helper(struct ctl_info ctlInfo, int utunnum)
  3032. {
  3033. struct sockaddr_ctl sc;
  3034. int fd;
  3035.  
  3036. fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
  3037.  
  3038. if (fd < 0)
  3039. {
  3040. msg(M_INFO | M_ERRNO, "Opening utun (socket(SYSPROTO_CONTROL))");
  3041. return -2;
  3042. }
  3043.  
  3044. if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
  3045. {
  3046. close(fd);
  3047. msg(M_INFO | M_ERRNO, "Opening utun (ioctl(CTLIOCGINFO))");
  3048. return -2;
  3049. }
  3050.  
  3051.  
  3052. sc.sc_id = ctlInfo.ctl_id;
  3053. sc.sc_len = sizeof(sc);
  3054. sc.sc_family = AF_SYSTEM;
  3055. sc.ss_sysaddr = AF_SYS_CONTROL;
  3056.  
  3057. sc.sc_unit = utunnum+1;
  3058.  
  3059.  
  3060. /* If the connect is successful, a utun%d device will be created, where "%d"
  3061. * is (sc.sc_unit - 1) */
  3062.  
  3063. if (connect(fd, (struct sockaddr *)&sc, sizeof(sc)) < 0)
  3064. {
  3065. msg(M_INFO | M_ERRNO, "Opening utun (connect(AF_SYS_CONTROL))");
  3066. close(fd);
  3067. return -1;
  3068. }
  3069.  
  3070. set_nonblock(fd);
  3071. set_cloexec(fd); /* don't pass fd to scripts */
  3072.  
  3073. return fd;
  3074. }
  3075.  
  3076. void
  3077. open_darwin_utun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  3078. {
  3079. struct ctl_info ctlInfo;
  3080. int fd;
  3081. char utunname[20];
  3082. int utunnum = -1;
  3083. socklen_t utunname_len = sizeof(utunname);
  3084.  
  3085. /* dev_node is simply utun, do the normal dynamic utun
  3086. * otherwise try to parse the utun number */
  3087. if (dev_node && (strcmp("utun", dev_node) != 0 ))
  3088. {
  3089. if (sscanf(dev_node, "utun%d", &utunnum) != 1)
  3090. {
  3091. msg(M_FATAL, "Cannot parse 'dev-node %s' please use 'dev-node utunX'"
  3092. "to use a utun device number X", dev_node);
  3093. }
  3094. }
  3095.  
  3096.  
  3097.  
  3098. CLEAR(ctlInfo);
  3099. if (strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME, sizeof(ctlInfo.ctl_name)) >=
  3100. sizeof(ctlInfo.ctl_name))
  3101. {
  3102. msg(M_ERR, "Opening utun: UTUN_CONTROL_NAME too long");
  3103. }
  3104.  
  3105. /* try to open first available utun device if no specific utun is requested */
  3106. if (utunnum == -1)
  3107. {
  3108. for (utunnum = 0; utunnum<255; utunnum++)
  3109. {
  3110. fd = utun_open_helper(ctlInfo, utunnum);
  3111. /* Break if the fd is valid,
  3112. * or if early initalization failed (-2) */
  3113. if (fd !=-1)
  3114. {
  3115. break;
  3116. }
  3117. }
  3118. }
  3119. else
  3120. {
  3121. fd = utun_open_helper(ctlInfo, utunnum);
  3122. }
  3123.  
  3124. /* opening an utun device failed */
  3125. tt->fd = fd;
  3126.  
  3127. if (fd < 0)
  3128. {
  3129. return;
  3130. }
  3131.  
  3132. /* Retrieve the assigned interface name. */
  3133. if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len))
  3134. {
  3135. msg(M_ERR | M_ERRNO, "Error retrieving utun interface name");
  3136. }
  3137.  
  3138. tt->actual_name = string_alloc(utunname, NULL);
  3139.  
  3140. msg(M_INFO, "Opened utun device %s", utunname);
  3141. tt->is_utun = true;
  3142. }
  3143.  
  3144. #endif /* ifdef HAVE_NET_IF_UTUN_H */
  3145.  
  3146. void
  3147. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  3148. {
  3149. #ifdef HAVE_NET_IF_UTUN_H
  3150. /* If dev_node does not start start with utun assume regular tun/tap */
  3151. if ((!dev_node && tt->type==DEV_TYPE_TUN)
  3152. || (dev_node && !strncmp(dev_node, "utun", 4)))
  3153. {
  3154.  
  3155. /* Check if user has specific dev_type tap and forced utun with
  3156. * dev-node utun */
  3157. if (tt->type!=DEV_TYPE_TUN)
  3158. {
  3159. msg(M_FATAL, "Cannot use utun devices with --dev-type %s",
  3160. dev_type_string(dev, dev_type));
  3161. }
  3162.  
  3163. /* Try utun first and fall back to normal tun if utun fails
  3164. * and dev_node is not specified */
  3165. open_darwin_utun(dev, dev_type, dev_node, tt);
  3166.  
  3167. if (!tt->is_utun)
  3168. {
  3169. if (!dev_node)
  3170. {
  3171. /* No explicit utun and utun failed, try the generic way) */
  3172. msg(M_INFO, "Failed to open utun device. Falling back to /dev/tun device");
  3173. open_tun_generic(dev, dev_type, NULL, true, tt);
  3174. }
  3175. else
  3176. {
  3177. /* Specific utun device or generic utun request with no tun
  3178. * fall back failed, consider this a fatal failure */
  3179. msg(M_FATAL, "Cannot open utun device");
  3180. }
  3181. }
  3182. }
  3183. else
  3184. #endif /* ifdef HAVE_NET_IF_UTUN_H */
  3185. {
  3186.  
  3187. /* Use plain dev-node tun to select /dev/tun style
  3188. * Unset dev_node variable prior to passing to open_tun_generic to
  3189. * let open_tun_generic pick the first available tun device */
  3190.  
  3191. if (dev_node && strcmp(dev_node, "tun")==0)
  3192. {
  3193. dev_node = NULL;
  3194. }
  3195.  
  3196. open_tun_generic(dev, dev_type, dev_node, true, tt);
  3197. }
  3198. }
  3199.  
  3200. void
  3201. close_tun(struct tuntap *tt)
  3202. {
  3203. if (tt)
  3204. {
  3205. struct gc_arena gc = gc_new();
  3206. struct argv argv = argv_new();
  3207.  
  3208. if (tt->did_ifconfig_ipv6_setup)
  3209. {
  3210. const char *ifconfig_ipv6_local =
  3211. print_in6_addr(tt->local_ipv6, 0, &gc);
  3212.  
  3213. argv_printf(&argv, "%s delete -inet6 %s",
  3214. ROUTE_PATH, ifconfig_ipv6_local );
  3215. argv_msg(M_INFO, &argv);
  3216. openvpn_execve_check(&argv, NULL, 0, "MacOS X 'remove inet6 route' failed (non-critical)");
  3217. }
  3218.  
  3219. close_tun_generic(tt);
  3220. free(tt);
  3221. argv_reset(&argv);
  3222. gc_free(&gc);
  3223. }
  3224. }
  3225.  
  3226. int
  3227. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  3228. {
  3229. #ifdef HAVE_NET_IF_UTUN_H
  3230. if (tt->is_utun)
  3231. {
  3232. return write_tun_header(tt, buf, len);
  3233. }
  3234. else
  3235. #endif
  3236. return write(tt->fd, buf, len);
  3237. }
  3238.  
  3239. int
  3240. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  3241. {
  3242. #ifdef HAVE_NET_IF_UTUN_H
  3243. if (tt->is_utun)
  3244. {
  3245. return read_tun_header(tt, buf, len);
  3246. }
  3247. else
  3248. #endif
  3249. return read(tt->fd, buf, len);
  3250. }
  3251.  
  3252. #elif defined(TARGET_AIX)
  3253.  
  3254. void
  3255. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  3256. {
  3257. char tunname[256];
  3258. char dynamic_name[20];
  3259. const char *p;
  3260.  
  3261. if (tt->type == DEV_TYPE_NULL)
  3262. {
  3263. open_null(tt);
  3264. return;
  3265. }
  3266.  
  3267. if (tt->type == DEV_TYPE_TUN)
  3268. {
  3269. msg(M_FATAL, "no support for 'tun' devices on AIX" );
  3270. }
  3271.  
  3272. if (strncmp( dev, "tap", 3 ) != 0 || dev_node)
  3273. {
  3274. msg(M_FATAL, "'--dev %s' and/or '--dev-node' not supported on AIX, use '--dev tap0', 'tap1', etc.", dev );
  3275. }
  3276.  
  3277. if (strcmp( dev, "tap" ) == 0) /* find first free tap dev */
  3278. { /* (= no /dev/tapN node) */
  3279. int i;
  3280. for (i = 0; i<99; i++)
  3281. {
  3282. openvpn_snprintf(tunname, sizeof(tunname), "/dev/tap%d", i);
  3283. if (access( tunname, F_OK ) < 0 && errno == ENOENT)
  3284. {
  3285. break;
  3286. }
  3287. }
  3288. if (i >= 99)
  3289. {
  3290. msg( M_FATAL, "cannot find unused tap device" );
  3291. }
  3292.  
  3293. openvpn_snprintf( dynamic_name, sizeof(dynamic_name), "tap%d", i );
  3294. dev = dynamic_name;
  3295. }
  3296. else /* name given, sanity check */
  3297. {
  3298. /* ensure that dev name is "tap+<digits>" *only* */
  3299. p = &dev[3];
  3300. while (isdigit(*p) )
  3301. {
  3302. p++;
  3303. }
  3304. if (*p != '\0')
  3305. {
  3306. msg( M_FATAL, "TAP device name must be '--dev tapNNNN'" );
  3307. }
  3308.  
  3309. openvpn_snprintf(tunname, sizeof(tunname), "/dev/%s", dev);
  3310. }
  3311.  
  3312. /* pre-existing device?
  3313. */
  3314. if (access( tunname, F_OK ) < 0 && errno == ENOENT)
  3315. {
  3316.  
  3317. /* tunnel device must be created with 'ifconfig tapN create'
  3318. */
  3319. struct argv argv = argv_new();
  3320. struct env_set *es = env_set_create(NULL);
  3321. argv_printf(&argv, "%s %s create", IFCONFIG_PATH, dev);
  3322. argv_msg(M_INFO, &argv);
  3323. env_set_add( es, "ODMDIR=/etc/objrepos" );
  3324. openvpn_execve_check(&argv, es, S_FATAL, "AIX 'create tun interface' failed");
  3325. env_set_destroy(es);
  3326. }
  3327. else
  3328. {
  3329. /* we didn't make it, we're not going to break it */
  3330. tt->persistent_if = TRUE;
  3331. }
  3332.  
  3333. if ((tt->fd = open(tunname, O_RDWR)) < 0)
  3334. {
  3335. msg(M_ERR, "Cannot open TAP device '%s'", tunname);
  3336. }
  3337.  
  3338. set_nonblock(tt->fd);
  3339. set_cloexec(tt->fd); /* don't pass fd to scripts */
  3340. msg(M_INFO, "TUN/TAP device %s opened", tunname);
  3341.  
  3342. /* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
  3343. tt->actual_name = string_alloc(dev, NULL);
  3344. }
  3345.  
  3346. /* tap devices need to be manually destroyed on AIX
  3347. */
  3348. void
  3349. close_tun(struct tuntap *tt)
  3350. {
  3351. struct gc_arena gc = gc_new();
  3352. struct argv argv = argv_new();
  3353. struct env_set *es = env_set_create(NULL);
  3354.  
  3355. if (!tt)
  3356. {
  3357. return;
  3358. }
  3359.  
  3360. /* persistent devices need IP address unconfig, others need destroyal
  3361. */
  3362. if (tt->persistent_if)
  3363. {
  3364. argv_printf(&argv, "%s %s 0.0.0.0 down",
  3365. IFCONFIG_PATH, tt->actual_name);
  3366. }
  3367. else
  3368. {
  3369. argv_printf(&argv, "%s %s destroy",
  3370. IFCONFIG_PATH, tt->actual_name);
  3371. }
  3372.  
  3373. close_tun_generic(tt);
  3374. argv_msg(M_INFO, &argv);
  3375. env_set_add( es, "ODMDIR=/etc/objrepos" );
  3376. openvpn_execve_check(&argv, es, 0, "AIX 'destroy tap interface' failed (non-critical)");
  3377.  
  3378. free(tt);
  3379. env_set_destroy(es);
  3380. }
  3381.  
  3382. int
  3383. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  3384. {
  3385. return write(tt->fd, buf, len);
  3386. }
  3387.  
  3388. int
  3389. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  3390. {
  3391. return read(tt->fd, buf, len);
  3392. }
  3393.  
  3394. #elif defined(_WIN32)
  3395.  
  3396. int
  3397. tun_read_queue(struct tuntap *tt, int maxsize)
  3398. {
  3399. if (tt->reads.iostate == IOSTATE_INITIAL)
  3400. {
  3401. DWORD len;
  3402. BOOL status;
  3403. int err;
  3404.  
  3405. /* reset buf to its initial state */
  3406. tt->reads.buf = tt->reads.buf_init;
  3407.  
  3408. len = maxsize ? maxsize : BLEN(&tt->reads.buf);
  3409. ASSERT(len <= BLEN(&tt->reads.buf));
  3410.  
  3411. /* the overlapped read will signal this event on I/O completion */
  3412. ASSERT(ResetEvent(tt->reads.overlapped.hEvent));
  3413.  
  3414. status = ReadFile(
  3415. tt->hand,
  3416. BPTR(&tt->reads.buf),
  3417. len,
  3418. &tt->reads.size,
  3419. &tt->reads.overlapped
  3420. );
  3421.  
  3422. if (status) /* operation completed immediately? */
  3423. {
  3424. /* since we got an immediate return, we must signal the event object ourselves */
  3425. ASSERT(SetEvent(tt->reads.overlapped.hEvent));
  3426.  
  3427. tt->reads.iostate = IOSTATE_IMMEDIATE_RETURN;
  3428. tt->reads.status = 0;
  3429.  
  3430. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Read immediate return [%d,%d]",
  3431. (int) len,
  3432. (int) tt->reads.size);
  3433. }
  3434. else
  3435. {
  3436. err = GetLastError();
  3437. if (err == ERROR_IO_PENDING) /* operation queued? */
  3438. {
  3439. tt->reads.iostate = IOSTATE_QUEUED;
  3440. tt->reads.status = err;
  3441. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Read queued [%d]",
  3442. (int) len);
  3443. }
  3444. else /* error occurred */
  3445. {
  3446. struct gc_arena gc = gc_new();
  3447. ASSERT(SetEvent(tt->reads.overlapped.hEvent));
  3448. tt->reads.iostate = IOSTATE_IMMEDIATE_RETURN;
  3449. tt->reads.status = err;
  3450. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Read error [%d] : %s",
  3451. (int) len,
  3452. strerror_win32(status, &gc));
  3453. gc_free(&gc);
  3454. }
  3455. }
  3456. }
  3457. return tt->reads.iostate;
  3458. }
  3459.  
  3460. int
  3461. tun_write_queue(struct tuntap *tt, struct buffer *buf)
  3462. {
  3463. if (tt->writes.iostate == IOSTATE_INITIAL)
  3464. {
  3465. BOOL status;
  3466. int err;
  3467.  
  3468. /* make a private copy of buf */
  3469. tt->writes.buf = tt->writes.buf_init;
  3470. tt->writes.buf.len = 0;
  3471. ASSERT(buf_copy(&tt->writes.buf, buf));
  3472.  
  3473. /* the overlapped write will signal this event on I/O completion */
  3474. ASSERT(ResetEvent(tt->writes.overlapped.hEvent));
  3475.  
  3476. status = WriteFile(
  3477. tt->hand,
  3478. BPTR(&tt->writes.buf),
  3479. BLEN(&tt->writes.buf),
  3480. &tt->writes.size,
  3481. &tt->writes.overlapped
  3482. );
  3483.  
  3484. if (status) /* operation completed immediately? */
  3485. {
  3486. tt->writes.iostate = IOSTATE_IMMEDIATE_RETURN;
  3487.  
  3488. /* since we got an immediate return, we must signal the event object ourselves */
  3489. ASSERT(SetEvent(tt->writes.overlapped.hEvent));
  3490.  
  3491. tt->writes.status = 0;
  3492.  
  3493. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Write immediate return [%d,%d]",
  3494. BLEN(&tt->writes.buf),
  3495. (int) tt->writes.size);
  3496. }
  3497. else
  3498. {
  3499. err = GetLastError();
  3500. if (err == ERROR_IO_PENDING) /* operation queued? */
  3501. {
  3502. tt->writes.iostate = IOSTATE_QUEUED;
  3503. tt->writes.status = err;
  3504. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Write queued [%d]",
  3505. BLEN(&tt->writes.buf));
  3506. }
  3507. else /* error occurred */
  3508. {
  3509. struct gc_arena gc = gc_new();
  3510. ASSERT(SetEvent(tt->writes.overlapped.hEvent));
  3511. tt->writes.iostate = IOSTATE_IMMEDIATE_RETURN;
  3512. tt->writes.status = err;
  3513. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Write error [%d] : %s",
  3514. BLEN(&tt->writes.buf),
  3515. strerror_win32(err, &gc));
  3516. gc_free(&gc);
  3517. }
  3518. }
  3519. }
  3520. return tt->writes.iostate;
  3521. }
  3522.  
  3523. int
  3524. tun_finalize(
  3525. HANDLE h,
  3526. struct overlapped_io *io,
  3527. struct buffer *buf)
  3528. {
  3529. int ret = -1;
  3530. BOOL status;
  3531.  
  3532. switch (io->iostate)
  3533. {
  3534. case IOSTATE_QUEUED:
  3535. status = GetOverlappedResult(
  3536. h,
  3537. &io->overlapped,
  3538. &io->size,
  3539. FALSE
  3540. );
  3541. if (status)
  3542. {
  3543. /* successful return for a queued operation */
  3544. if (buf)
  3545. {
  3546. *buf = io->buf;
  3547. }
  3548. ret = io->size;
  3549. io->iostate = IOSTATE_INITIAL;
  3550. ASSERT(ResetEvent(io->overlapped.hEvent));
  3551. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion success [%d]", ret);
  3552. }
  3553. else
  3554. {
  3555. /* error during a queued operation */
  3556. ret = -1;
  3557. if (GetLastError() != ERROR_IO_INCOMPLETE)
  3558. {
  3559. /* if no error (i.e. just not finished yet),
  3560. * then DON'T execute this code */
  3561. io->iostate = IOSTATE_INITIAL;
  3562. ASSERT(ResetEvent(io->overlapped.hEvent));
  3563. msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion error");
  3564. }
  3565. }
  3566. break;
  3567.  
  3568. case IOSTATE_IMMEDIATE_RETURN:
  3569. io->iostate = IOSTATE_INITIAL;
  3570. ASSERT(ResetEvent(io->overlapped.hEvent));
  3571. if (io->status)
  3572. {
  3573. /* error return for a non-queued operation */
  3574. SetLastError(io->status);
  3575. ret = -1;
  3576. msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion non-queued error");
  3577. }
  3578. else
  3579. {
  3580. /* successful return for a non-queued operation */
  3581. if (buf)
  3582. {
  3583. *buf = io->buf;
  3584. }
  3585. ret = io->size;
  3586. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion non-queued success [%d]", ret);
  3587. }
  3588. break;
  3589.  
  3590. case IOSTATE_INITIAL: /* were we called without proper queueing? */
  3591. SetLastError(ERROR_INVALID_FUNCTION);
  3592. ret = -1;
  3593. dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion BAD STATE");
  3594. break;
  3595.  
  3596. default:
  3597. ASSERT(0);
  3598. }
  3599.  
  3600. if (buf)
  3601. {
  3602. buf->len = ret;
  3603. }
  3604. return ret;
  3605. }
  3606.  
  3607. const struct tap_reg *
  3608. get_tap_reg(struct gc_arena *gc)
  3609. {
  3610. HKEY adapter_key;
  3611. LONG status;
  3612. DWORD len;
  3613. struct tap_reg *first = NULL;
  3614. struct tap_reg *last = NULL;
  3615. int i = 0;
  3616.  
  3617. status = RegOpenKeyEx(
  3618. HKEY_LOCAL_MACHINE,
  3619. ADAPTER_KEY,
  3620. 0,
  3621. KEY_READ,
  3622. &adapter_key);
  3623.  
  3624. if (status != ERROR_SUCCESS)
  3625. {
  3626. msg(M_FATAL, "Error opening registry key: %s", ADAPTER_KEY);
  3627. }
  3628.  
  3629. while (true)
  3630. {
  3631. char enum_name[256];
  3632. char unit_string[256];
  3633. HKEY unit_key;
  3634. char component_id_string[] = "ComponentId";
  3635. char component_id[256];
  3636. char net_cfg_instance_id_string[] = "NetCfgInstanceId";
  3637. char net_cfg_instance_id[256];
  3638. DWORD data_type;
  3639.  
  3640. len = sizeof(enum_name);
  3641. status = RegEnumKeyEx(
  3642. adapter_key,
  3643. i,
  3644. enum_name,
  3645. &len,
  3646. NULL,
  3647. NULL,
  3648. NULL,
  3649. NULL);
  3650. if (status == ERROR_NO_MORE_ITEMS)
  3651. {
  3652. break;
  3653. }
  3654. else if (status != ERROR_SUCCESS)
  3655. {
  3656. msg(M_FATAL, "Error enumerating registry subkeys of key: %s",
  3657. ADAPTER_KEY);
  3658. }
  3659.  
  3660. openvpn_snprintf(unit_string, sizeof(unit_string), "%s\\%s",
  3661. ADAPTER_KEY, enum_name);
  3662.  
  3663. status = RegOpenKeyEx(
  3664. HKEY_LOCAL_MACHINE,
  3665. unit_string,
  3666. 0,
  3667. KEY_READ,
  3668. &unit_key);
  3669.  
  3670. if (status != ERROR_SUCCESS)
  3671. {
  3672. dmsg(D_REGISTRY, "Error opening registry key: %s", unit_string);
  3673. }
  3674. else
  3675. {
  3676. len = sizeof(component_id);
  3677. status = RegQueryValueEx(
  3678. unit_key,
  3679. component_id_string,
  3680. NULL,
  3681. &data_type,
  3682. component_id,
  3683. &len);
  3684.  
  3685. if (status != ERROR_SUCCESS || data_type != REG_SZ)
  3686. {
  3687. dmsg(D_REGISTRY, "Error opening registry key: %s\\%s",
  3688. unit_string, component_id_string);
  3689. }
  3690. else
  3691. {
  3692. len = sizeof(net_cfg_instance_id);
  3693. status = RegQueryValueEx(
  3694. unit_key,
  3695. net_cfg_instance_id_string,
  3696. NULL,
  3697. &data_type,
  3698. net_cfg_instance_id,
  3699. &len);
  3700.  
  3701. if (status == ERROR_SUCCESS && data_type == REG_SZ)
  3702. {
  3703. if (!strcmp(component_id, TAP_WIN_COMPONENT_ID) ||
  3704. !strcmp(component_id, "root\\" TAP_WIN_COMPONENT_ID))
  3705. {
  3706. struct tap_reg *reg;
  3707. ALLOC_OBJ_CLEAR_GC(reg, struct tap_reg, gc);
  3708. reg->guid = string_alloc(net_cfg_instance_id, gc);
  3709.  
  3710. /* link into return list */
  3711. if (!first)
  3712. {
  3713. first = reg;
  3714. }
  3715. if (last)
  3716. {
  3717. last->next = reg;
  3718. }
  3719. last = reg;
  3720. }
  3721. }
  3722. }
  3723. RegCloseKey(unit_key);
  3724. }
  3725. ++i;
  3726. }
  3727.  
  3728. RegCloseKey(adapter_key);
  3729. return first;
  3730. }
  3731.  
  3732. const struct panel_reg *
  3733. get_panel_reg(struct gc_arena *gc)
  3734. {
  3735. LONG status;
  3736. HKEY network_connections_key;
  3737. DWORD len;
  3738. struct panel_reg *first = NULL;
  3739. struct panel_reg *last = NULL;
  3740. int i = 0;
  3741.  
  3742. status = RegOpenKeyEx(
  3743. HKEY_LOCAL_MACHINE,
  3744. NETWORK_CONNECTIONS_KEY,
  3745. 0,
  3746. KEY_READ,
  3747. &network_connections_key);
  3748.  
  3749. if (status != ERROR_SUCCESS)
  3750. {
  3751. msg(M_FATAL, "Error opening registry key: %s", NETWORK_CONNECTIONS_KEY);
  3752. }
  3753.  
  3754. while (true)
  3755. {
  3756. char enum_name[256];
  3757. char connection_string[256];
  3758. HKEY connection_key;
  3759. WCHAR name_data[256];
  3760. DWORD name_type;
  3761. const WCHAR name_string[] = L"Name";
  3762.  
  3763. len = sizeof(enum_name);
  3764. status = RegEnumKeyEx(
  3765. network_connections_key,
  3766. i,
  3767. enum_name,
  3768. &len,
  3769. NULL,
  3770. NULL,
  3771. NULL,
  3772. NULL);
  3773. if (status == ERROR_NO_MORE_ITEMS)
  3774. {
  3775. break;
  3776. }
  3777. else if (status != ERROR_SUCCESS)
  3778. {
  3779. msg(M_FATAL, "Error enumerating registry subkeys of key: %s",
  3780. NETWORK_CONNECTIONS_KEY);
  3781. }
  3782.  
  3783. openvpn_snprintf(connection_string, sizeof(connection_string),
  3784. "%s\\%s\\Connection",
  3785. NETWORK_CONNECTIONS_KEY, enum_name);
  3786.  
  3787. status = RegOpenKeyEx(
  3788. HKEY_LOCAL_MACHINE,
  3789. connection_string,
  3790. 0,
  3791. KEY_READ,
  3792. &connection_key);
  3793.  
  3794. if (status != ERROR_SUCCESS)
  3795. {
  3796. dmsg(D_REGISTRY, "Error opening registry key: %s", connection_string);
  3797. }
  3798. else
  3799. {
  3800. len = sizeof(name_data);
  3801. status = RegQueryValueExW(
  3802. connection_key,
  3803. name_string,
  3804. NULL,
  3805. &name_type,
  3806. (LPBYTE) name_data,
  3807. &len);
  3808.  
  3809. if (status != ERROR_SUCCESS || name_type != REG_SZ)
  3810. {
  3811. dmsg(D_REGISTRY, "Error opening registry key: %s\\%s\\%ls",
  3812. NETWORK_CONNECTIONS_KEY, connection_string, name_string);
  3813. }
  3814. else
  3815. {
  3816. int n;
  3817. LPSTR name;
  3818. struct panel_reg *reg;
  3819.  
  3820. ALLOC_OBJ_CLEAR_GC(reg, struct panel_reg, gc);
  3821. n = WideCharToMultiByte(CP_UTF8, 0, name_data, -1, NULL, 0, NULL, NULL);
  3822. name = gc_malloc(n, false, gc);
  3823. WideCharToMultiByte(CP_UTF8, 0, name_data, -1, name, n, NULL, NULL);
  3824. reg->name = name;
  3825. reg->guid = string_alloc(enum_name, gc);
  3826.  
  3827. /* link into return list */
  3828. if (!first)
  3829. {
  3830. first = reg;
  3831. }
  3832. if (last)
  3833. {
  3834. last->next = reg;
  3835. }
  3836. last = reg;
  3837. }
  3838. RegCloseKey(connection_key);
  3839. }
  3840. ++i;
  3841. }
  3842.  
  3843. RegCloseKey(network_connections_key);
  3844.  
  3845. return first;
  3846. }
  3847.  
  3848. /*
  3849. * Check that two addresses are part of the same 255.255.255.252 subnet.
  3850. */
  3851. void
  3852. verify_255_255_255_252(in_addr_t local, in_addr_t remote)
  3853. {
  3854. struct gc_arena gc = gc_new();
  3855. const unsigned int mask = 3;
  3856. const char *err = NULL;
  3857.  
  3858. if (local == remote)
  3859. {
  3860. err = "must be different";
  3861. goto error;
  3862. }
  3863. if ((local & (~mask)) != (remote & (~mask)))
  3864. {
  3865. err = "must exist within the same 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
  3866. goto error;
  3867. }
  3868. if ((local & mask) == 0
  3869. || (local & mask) == 3
  3870. || (remote & mask) == 0
  3871. || (remote & mask) == 3)
  3872. {
  3873. err = "cannot use the first or last address within a given 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
  3874. goto error;
  3875. }
  3876.  
  3877. gc_free(&gc);
  3878. return;
  3879.  
  3880. error:
  3881. msg(M_FATAL, "There is a problem in your selection of --ifconfig endpoints [local=%s, remote=%s]. The local and remote VPN endpoints %s. Try '" PACKAGE " --show-valid-subnets' option for more info.",
  3882. print_in_addr_t(local, 0, &gc),
  3883. print_in_addr_t(remote, 0, &gc),
  3884. err);
  3885. gc_free(&gc);
  3886. }
  3887.  
  3888. void
  3889. show_valid_win32_tun_subnets(void)
  3890. {
  3891. int i;
  3892. int col = 0;
  3893.  
  3894. printf("On Windows, point-to-point IP support (i.e. --dev tun)\n");
  3895. printf("is emulated by the TAP-Windows driver. The major limitation\n");
  3896. printf("imposed by this approach is that the --ifconfig local and\n");
  3897. printf("remote endpoints must be part of the same 255.255.255.252\n");
  3898. printf("subnet. The following list shows examples of endpoint\n");
  3899. printf("pairs which satisfy this requirement. Only the final\n");
  3900. printf("component of the IP address pairs is at issue.\n\n");
  3901. printf("As an example, the following option would be correct:\n");
  3902. printf(" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
  3903. printf(" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
  3904. printf("because [5,6] is part of the below list.\n\n");
  3905.  
  3906. for (i = 0; i < 256; i += 4)
  3907. {
  3908. printf("[%3d,%3d] ", i+1, i+2);
  3909. if (++col > 4)
  3910. {
  3911. col = 0;
  3912. printf("\n");
  3913. }
  3914. }
  3915. if (col)
  3916. {
  3917. printf("\n");
  3918. }
  3919. }
  3920.  
  3921. void
  3922. show_tap_win_adapters(int msglev, int warnlev)
  3923. {
  3924. struct gc_arena gc = gc_new();
  3925.  
  3926. bool warn_panel_null = false;
  3927. bool warn_panel_dup = false;
  3928. bool warn_tap_dup = false;
  3929.  
  3930. int links;
  3931.  
  3932. const struct tap_reg *tr;
  3933. const struct tap_reg *tr1;
  3934. const struct panel_reg *pr;
  3935.  
  3936. const struct tap_reg *tap_reg = get_tap_reg(&gc);
  3937. const struct panel_reg *panel_reg = get_panel_reg(&gc);
  3938.  
  3939. msg(msglev, "Available TAP-WIN32 adapters [name, GUID]:");
  3940.  
  3941. /* loop through each TAP-Windows adapter registry entry */
  3942. for (tr = tap_reg; tr != NULL; tr = tr->next)
  3943. {
  3944. links = 0;
  3945.  
  3946. /* loop through each network connections entry in the control panel */
  3947. for (pr = panel_reg; pr != NULL; pr = pr->next)
  3948. {
  3949. if (!strcmp(tr->guid, pr->guid))
  3950. {
  3951. msg(msglev, "'%s' %s", pr->name, tr->guid);
  3952. ++links;
  3953. }
  3954. }
  3955.  
  3956. if (links > 1)
  3957. {
  3958. warn_panel_dup = true;
  3959. }
  3960. else if (links == 0)
  3961. {
  3962. /* a TAP adapter exists without a link from the network
  3963. * connections control panel */
  3964. warn_panel_null = true;
  3965. msg(msglev, "[NULL] %s", tr->guid);
  3966. }
  3967. }
  3968.  
  3969. /* check for TAP-Windows adapter duplicated GUIDs */
  3970. for (tr = tap_reg; tr != NULL; tr = tr->next)
  3971. {
  3972. for (tr1 = tap_reg; tr1 != NULL; tr1 = tr1->next)
  3973. {
  3974. if (tr != tr1 && !strcmp(tr->guid, tr1->guid))
  3975. {
  3976. warn_tap_dup = true;
  3977. }
  3978. }
  3979. }
  3980.  
  3981. /* warn on registry inconsistencies */
  3982. if (warn_tap_dup)
  3983. {
  3984. msg(warnlev, "WARNING: Some TAP-Windows adapters have duplicate GUIDs");
  3985. }
  3986.  
  3987. if (warn_panel_dup)
  3988. {
  3989. msg(warnlev, "WARNING: Some TAP-Windows adapters have duplicate links from the Network Connections control panel");
  3990. }
  3991.  
  3992. if (warn_panel_null)
  3993. {
  3994. msg(warnlev, "WARNING: Some TAP-Windows adapters have no link from the Network Connections control panel");
  3995. }
  3996.  
  3997. gc_free(&gc);
  3998. }
  3999.  
  4000. /*
  4001. * Confirm that GUID is a TAP-Windows adapter.
  4002. */
  4003. static bool
  4004. is_tap_win(const char *guid, const struct tap_reg *tap_reg)
  4005. {
  4006. const struct tap_reg *tr;
  4007.  
  4008. for (tr = tap_reg; tr != NULL; tr = tr->next)
  4009. {
  4010. if (guid && !strcmp(tr->guid, guid))
  4011. {
  4012. return true;
  4013. }
  4014. }
  4015.  
  4016. return false;
  4017. }
  4018.  
  4019. static const char *
  4020. guid_to_name(const char *guid, const struct panel_reg *panel_reg)
  4021. {
  4022. const struct panel_reg *pr;
  4023.  
  4024. for (pr = panel_reg; pr != NULL; pr = pr->next)
  4025. {
  4026. if (guid && !strcmp(pr->guid, guid))
  4027. {
  4028. return pr->name;
  4029. }
  4030. }
  4031.  
  4032. return NULL;
  4033. }
  4034.  
  4035. static const char *
  4036. name_to_guid(const char *name, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg)
  4037. {
  4038. const struct panel_reg *pr;
  4039.  
  4040. for (pr = panel_reg; pr != NULL; pr = pr->next)
  4041. {
  4042. if (name && !strcmp(pr->name, name) && is_tap_win(pr->guid, tap_reg))
  4043. {
  4044. return pr->guid;
  4045. }
  4046. }
  4047.  
  4048. return NULL;
  4049. }
  4050.  
  4051. static void
  4052. at_least_one_tap_win(const struct tap_reg *tap_reg)
  4053. {
  4054. if (!tap_reg)
  4055. {
  4056. msg(M_FATAL, "There are no TAP-Windows adapters on this system. You should be able to create a TAP-Windows adapter by going to Start -> All Programs -> TAP-Windows -> Utilities -> Add a new TAP-Windows virtual ethernet adapter.");
  4057. }
  4058. }
  4059.  
  4060. /*
  4061. * Get an adapter GUID and optional actual_name from the
  4062. * registry for the TAP device # = device_number.
  4063. */
  4064. static const char *
  4065. get_unspecified_device_guid(const int device_number,
  4066. char *actual_name,
  4067. int actual_name_size,
  4068. const struct tap_reg *tap_reg_src,
  4069. const struct panel_reg *panel_reg_src,
  4070. struct gc_arena *gc)
  4071. {
  4072. const struct tap_reg *tap_reg = tap_reg_src;
  4073. struct buffer ret = clear_buf();
  4074. struct buffer actual = clear_buf();
  4075. int i;
  4076.  
  4077. ASSERT(device_number >= 0);
  4078.  
  4079. /* Make sure we have at least one TAP adapter */
  4080. if (!tap_reg)
  4081. {
  4082. return NULL;
  4083. }
  4084.  
  4085. /* The actual_name output buffer may be NULL */
  4086. if (actual_name)
  4087. {
  4088. ASSERT(actual_name_size > 0);
  4089. buf_set_write(&actual, actual_name, actual_name_size);
  4090. }
  4091.  
  4092. /* Move on to specified device number */
  4093. for (i = 0; i < device_number; i++)
  4094. {
  4095. tap_reg = tap_reg->next;
  4096. if (!tap_reg)
  4097. {
  4098. return NULL;
  4099. }
  4100. }
  4101.  
  4102. /* Save Network Panel name (if exists) in actual_name */
  4103. if (actual_name)
  4104. {
  4105. const char *act = guid_to_name(tap_reg->guid, panel_reg_src);
  4106. if (act)
  4107. {
  4108. buf_printf(&actual, "%s", act);
  4109. }
  4110. else
  4111. {
  4112. buf_printf(&actual, "%s", tap_reg->guid);
  4113. }
  4114. }
  4115.  
  4116. /* Save GUID for return value */
  4117. ret = alloc_buf_gc(256, gc);
  4118. buf_printf(&ret, "%s", tap_reg->guid);
  4119. return BSTR(&ret);
  4120. }
  4121.  
  4122. /*
  4123. * Lookup a --dev-node adapter name in the registry
  4124. * returning the GUID and optional actual_name.
  4125. */
  4126. static const char *
  4127. get_device_guid(const char *name,
  4128. char *actual_name,
  4129. int actual_name_size,
  4130. const struct tap_reg *tap_reg,
  4131. const struct panel_reg *panel_reg,
  4132. struct gc_arena *gc)
  4133. {
  4134. struct buffer ret = alloc_buf_gc(256, gc);
  4135. struct buffer actual = clear_buf();
  4136.  
  4137. /* Make sure we have at least one TAP adapter */
  4138. if (!tap_reg)
  4139. {
  4140. return NULL;
  4141. }
  4142.  
  4143. /* The actual_name output buffer may be NULL */
  4144. if (actual_name)
  4145. {
  4146. ASSERT(actual_name_size > 0);
  4147. buf_set_write(&actual, actual_name, actual_name_size);
  4148. }
  4149.  
  4150. /* Check if GUID was explicitly specified as --dev-node parameter */
  4151. if (is_tap_win(name, tap_reg))
  4152. {
  4153. const char *act = guid_to_name(name, panel_reg);
  4154. buf_printf(&ret, "%s", name);
  4155. if (act)
  4156. {
  4157. buf_printf(&actual, "%s", act);
  4158. }
  4159. else
  4160. {
  4161. buf_printf(&actual, "%s", name);
  4162. }
  4163. return BSTR(&ret);
  4164. }
  4165.  
  4166. /* Lookup TAP adapter in network connections list */
  4167. {
  4168. const char *guid = name_to_guid(name, tap_reg, panel_reg);
  4169. if (guid)
  4170. {
  4171. buf_printf(&actual, "%s", name);
  4172. buf_printf(&ret, "%s", guid);
  4173. return BSTR(&ret);
  4174. }
  4175. }
  4176.  
  4177. return NULL;
  4178. }
  4179.  
  4180. /*
  4181. * Get adapter info list
  4182. */
  4183. const IP_ADAPTER_INFO *
  4184. get_adapter_info_list(struct gc_arena *gc)
  4185. {
  4186. ULONG size = 0;
  4187. IP_ADAPTER_INFO *pi = NULL;
  4188. DWORD status;
  4189.  
  4190. if ((status = GetAdaptersInfo(NULL, &size)) != ERROR_BUFFER_OVERFLOW)
  4191. {
  4192. msg(M_INFO, "GetAdaptersInfo #1 failed (status=%u) : %s",
  4193. (unsigned int)status,
  4194. strerror_win32(status, gc));
  4195. }
  4196. else
  4197. {
  4198. pi = (PIP_ADAPTER_INFO) gc_malloc(size, false, gc);
  4199. if ((status = GetAdaptersInfo(pi, &size)) != NO_ERROR)
  4200. {
  4201. msg(M_INFO, "GetAdaptersInfo #2 failed (status=%u) : %s",
  4202. (unsigned int)status,
  4203. strerror_win32(status, gc));
  4204. pi = NULL;
  4205. }
  4206. }
  4207. return pi;
  4208. }
  4209.  
  4210. const IP_PER_ADAPTER_INFO *
  4211. get_per_adapter_info(const DWORD index, struct gc_arena *gc)
  4212. {
  4213. ULONG size = 0;
  4214. IP_PER_ADAPTER_INFO *pi = NULL;
  4215. DWORD status;
  4216.  
  4217. if (index != TUN_ADAPTER_INDEX_INVALID)
  4218. {
  4219. if ((status = GetPerAdapterInfo(index, NULL, &size)) != ERROR_BUFFER_OVERFLOW)
  4220. {
  4221. msg(M_INFO, "GetPerAdapterInfo #1 failed (status=%u) : %s",
  4222. (unsigned int)status,
  4223. strerror_win32(status, gc));
  4224. }
  4225. else
  4226. {
  4227. pi = (PIP_PER_ADAPTER_INFO) gc_malloc(size, false, gc);
  4228. if ((status = GetPerAdapterInfo((ULONG)index, pi, &size)) == ERROR_SUCCESS)
  4229. {
  4230. return pi;
  4231. }
  4232. else
  4233. {
  4234. msg(M_INFO, "GetPerAdapterInfo #2 failed (status=%u) : %s",
  4235. (unsigned int)status,
  4236. strerror_win32(status, gc));
  4237. }
  4238. }
  4239. }
  4240. return pi;
  4241. }
  4242.  
  4243. static const IP_INTERFACE_INFO *
  4244. get_interface_info_list(struct gc_arena *gc)
  4245. {
  4246. ULONG size = 0;
  4247. IP_INTERFACE_INFO *ii = NULL;
  4248. DWORD status;
  4249.  
  4250. if ((status = GetInterfaceInfo(NULL, &size)) != ERROR_INSUFFICIENT_BUFFER)
  4251. {
  4252. msg(M_INFO, "GetInterfaceInfo #1 failed (status=%u) : %s",
  4253. (unsigned int)status,
  4254. strerror_win32(status, gc));
  4255. }
  4256. else
  4257. {
  4258. ii = (PIP_INTERFACE_INFO) gc_malloc(size, false, gc);
  4259. if ((status = GetInterfaceInfo(ii, &size)) == NO_ERROR)
  4260. {
  4261. return ii;
  4262. }
  4263. else
  4264. {
  4265. msg(M_INFO, "GetInterfaceInfo #2 failed (status=%u) : %s",
  4266. (unsigned int)status,
  4267. strerror_win32(status, gc));
  4268. }
  4269. }
  4270. return ii;
  4271. }
  4272.  
  4273. static const IP_ADAPTER_INDEX_MAP *
  4274. get_interface_info(DWORD index, struct gc_arena *gc)
  4275. {
  4276. const IP_INTERFACE_INFO *list = get_interface_info_list(gc);
  4277. if (list)
  4278. {
  4279. int i;
  4280. for (i = 0; i < list->NumAdapters; ++i)
  4281. {
  4282. const IP_ADAPTER_INDEX_MAP *inter = &list->Adapter[i];
  4283. if (index == inter->Index)
  4284. {
  4285. return inter;
  4286. }
  4287. }
  4288. }
  4289. return NULL;
  4290. }
  4291.  
  4292. /*
  4293. * Given an adapter index, return a pointer to the
  4294. * IP_ADAPTER_INFO structure for that adapter.
  4295. */
  4296.  
  4297. const IP_ADAPTER_INFO *
  4298. get_adapter(const IP_ADAPTER_INFO *ai, DWORD index)
  4299. {
  4300. if (ai && index != TUN_ADAPTER_INDEX_INVALID)
  4301. {
  4302. const IP_ADAPTER_INFO *a;
  4303.  
  4304. /* find index in the linked list */
  4305. for (a = ai; a != NULL; a = a->Next)
  4306. {
  4307. if (a->Index == index)
  4308. {
  4309. return a;
  4310. }
  4311. }
  4312. }
  4313. return NULL;
  4314. }
  4315.  
  4316. const IP_ADAPTER_INFO *
  4317. get_adapter_info(DWORD index, struct gc_arena *gc)
  4318. {
  4319. return get_adapter(get_adapter_info_list(gc), index);
  4320. }
  4321.  
  4322. static int
  4323. get_adapter_n_ip_netmask(const IP_ADAPTER_INFO *ai)
  4324. {
  4325. if (ai)
  4326. {
  4327. int n = 0;
  4328. const IP_ADDR_STRING *ip = &ai->IpAddressList;
  4329.  
  4330. while (ip)
  4331. {
  4332. ++n;
  4333. ip = ip->Next;
  4334. }
  4335. return n;
  4336. }
  4337. else
  4338. {
  4339. return 0;
  4340. }
  4341. }
  4342.  
  4343. static bool
  4344. get_adapter_ip_netmask(const IP_ADAPTER_INFO *ai, const int n, in_addr_t *ip, in_addr_t *netmask)
  4345. {
  4346. bool ret = false;
  4347. *ip = 0;
  4348. *netmask = 0;
  4349.  
  4350. if (ai)
  4351. {
  4352. const IP_ADDR_STRING *iplist = &ai->IpAddressList;
  4353. int i = 0;
  4354.  
  4355. while (iplist)
  4356. {
  4357. if (i == n)
  4358. {
  4359. break;
  4360. }
  4361. ++i;
  4362. iplist = iplist->Next;
  4363. }
  4364.  
  4365. if (iplist)
  4366. {
  4367. const unsigned int getaddr_flags = GETADDR_HOST_ORDER;
  4368. const char *ip_str = iplist->IpAddress.String;
  4369. const char *netmask_str = iplist->IpMask.String;
  4370. bool succeed1 = false;
  4371. bool succeed2 = false;
  4372.  
  4373. if (ip_str && netmask_str && strlen(ip_str) && strlen(netmask_str))
  4374. {
  4375. *ip = getaddr(getaddr_flags, ip_str, 0, &succeed1, NULL);
  4376. *netmask = getaddr(getaddr_flags, netmask_str, 0, &succeed2, NULL);
  4377. ret = (succeed1 == true && succeed2 == true);
  4378. }
  4379. }
  4380. }
  4381.  
  4382. return ret;
  4383. }
  4384.  
  4385. static bool
  4386. test_adapter_ip_netmask(const IP_ADAPTER_INFO *ai, const in_addr_t ip, const in_addr_t netmask)
  4387. {
  4388. if (ai)
  4389. {
  4390. in_addr_t ip_adapter = 0;
  4391. in_addr_t netmask_adapter = 0;
  4392. const bool status = get_adapter_ip_netmask(ai, 0, &ip_adapter, &netmask_adapter);
  4393. return (status && ip_adapter == ip && netmask_adapter == netmask);
  4394. }
  4395. else
  4396. {
  4397. return false;
  4398. }
  4399. }
  4400.  
  4401. const IP_ADAPTER_INFO *
  4402. get_tun_adapter(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
  4403. {
  4404. if (list && tt)
  4405. {
  4406. return get_adapter(list, tt->adapter_index);
  4407. }
  4408. else
  4409. {
  4410. return NULL;
  4411. }
  4412. }
  4413.  
  4414. bool
  4415. is_adapter_up(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
  4416. {
  4417. int i;
  4418. bool ret = false;
  4419.  
  4420. const IP_ADAPTER_INFO *ai = get_tun_adapter(tt, list);
  4421.  
  4422. if (ai)
  4423. {
  4424. const int n = get_adapter_n_ip_netmask(ai);
  4425.  
  4426. /* loop once for every IP/netmask assigned to adapter */
  4427. for (i = 0; i < n; ++i)
  4428. {
  4429. in_addr_t ip, netmask;
  4430. if (get_adapter_ip_netmask(ai, i, &ip, &netmask))
  4431. {
  4432. if (tt->local && tt->adapter_netmask)
  4433. {
  4434. /* wait for our --ifconfig parms to match the actual adapter parms */
  4435. if (tt->local == ip && tt->adapter_netmask == netmask)
  4436. {
  4437. ret = true;
  4438. }
  4439. }
  4440. else
  4441. {
  4442. /* --ifconfig was not defined, maybe using a real DHCP server */
  4443. if (ip && netmask)
  4444. {
  4445. ret = true;
  4446. }
  4447. }
  4448. }
  4449. }
  4450. }
  4451. else
  4452. {
  4453. ret = true; /* this can occur when TAP adapter is bridged */
  4454.  
  4455. }
  4456. return ret;
  4457. }
  4458.  
  4459. bool
  4460. is_ip_in_adapter_subnet(const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask)
  4461. {
  4462. int i;
  4463. bool ret = false;
  4464.  
  4465. if (highest_netmask)
  4466. {
  4467. *highest_netmask = 0;
  4468. }
  4469.  
  4470. if (ai)
  4471. {
  4472. const int n = get_adapter_n_ip_netmask(ai);
  4473. for (i = 0; i < n; ++i)
  4474. {
  4475. in_addr_t adapter_ip, adapter_netmask;
  4476. if (get_adapter_ip_netmask(ai, i, &adapter_ip, &adapter_netmask))
  4477. {
  4478. if (adapter_ip && adapter_netmask && (ip & adapter_netmask) == (adapter_ip & adapter_netmask))
  4479. {
  4480. if (highest_netmask && adapter_netmask > *highest_netmask)
  4481. {
  4482. *highest_netmask = adapter_netmask;
  4483. }
  4484. ret = true;
  4485. }
  4486. }
  4487. }
  4488. }
  4489. return ret;
  4490. }
  4491.  
  4492. DWORD
  4493. adapter_index_of_ip(const IP_ADAPTER_INFO *list,
  4494. const in_addr_t ip,
  4495. int *count,
  4496. in_addr_t *netmask)
  4497. {
  4498. struct gc_arena gc = gc_new();
  4499. DWORD ret = TUN_ADAPTER_INDEX_INVALID;
  4500. in_addr_t highest_netmask = 0;
  4501. int lowest_metric = INT_MAX;
  4502. bool first = true;
  4503.  
  4504. if (count)
  4505. {
  4506. *count = 0;
  4507. }
  4508.  
  4509. while (list)
  4510. {
  4511. in_addr_t hn;
  4512.  
  4513. if (is_ip_in_adapter_subnet(list, ip, &hn))
  4514. {
  4515. int metric = get_interface_metric(list->Index, AF_INET, NULL);
  4516. if (first || hn > highest_netmask)
  4517. {
  4518. highest_netmask = hn;
  4519. if (metric >= 0)
  4520. {
  4521. lowest_metric = metric;
  4522. }
  4523. if (count)
  4524. {
  4525. *count = 1;
  4526. }
  4527. ret = list->Index;
  4528. first = false;
  4529. }
  4530. else if (hn == highest_netmask)
  4531. {
  4532. if (count)
  4533. {
  4534. ++*count;
  4535. }
  4536. if (metric >= 0 && metric < lowest_metric)
  4537. {
  4538. ret = list->Index;
  4539. lowest_metric = metric;
  4540. }
  4541. }
  4542. }
  4543. list = list->Next;
  4544. }
  4545.  
  4546. dmsg(D_ROUTE_DEBUG, "DEBUG: IP Locate: ip=%s nm=%s index=%d count=%d metric=%d",
  4547. print_in_addr_t(ip, 0, &gc),
  4548. print_in_addr_t(highest_netmask, 0, &gc),
  4549. (int)ret,
  4550. count ? *count : -1,
  4551. lowest_metric);
  4552.  
  4553. if (ret == TUN_ADAPTER_INDEX_INVALID && count)
  4554. {
  4555. *count = 0;
  4556. }
  4557.  
  4558. if (netmask)
  4559. {
  4560. *netmask = highest_netmask;
  4561. }
  4562.  
  4563. gc_free(&gc);
  4564. return ret;
  4565. }
  4566.  
  4567. /*
  4568. * Given an adapter index, return true if the adapter
  4569. * is DHCP disabled.
  4570. */
  4571.  
  4572. #define DHCP_STATUS_UNDEF 0
  4573. #define DHCP_STATUS_ENABLED 1
  4574. #define DHCP_STATUS_DISABLED 2
  4575.  
  4576. static int
  4577. dhcp_status(DWORD index)
  4578. {
  4579. struct gc_arena gc = gc_new();
  4580. int ret = DHCP_STATUS_UNDEF;
  4581. if (index != TUN_ADAPTER_INDEX_INVALID)
  4582. {
  4583. const IP_ADAPTER_INFO *ai = get_adapter_info(index, &gc);
  4584.  
  4585. if (ai)
  4586. {
  4587. if (ai->DhcpEnabled)
  4588. {
  4589. ret = DHCP_STATUS_ENABLED;
  4590. }
  4591. else
  4592. {
  4593. ret = DHCP_STATUS_DISABLED;
  4594. }
  4595. }
  4596. }
  4597. gc_free(&gc);
  4598. return ret;
  4599. }
  4600.  
  4601. /*
  4602. * Delete all temporary address/netmask pairs which were added
  4603. * to adapter (given by index) by previous calls to AddIPAddress.
  4604. */
  4605. static void
  4606. delete_temp_addresses(DWORD index)
  4607. {
  4608. struct gc_arena gc = gc_new();
  4609. const IP_ADAPTER_INFO *a = get_adapter_info(index, &gc);
  4610.  
  4611. if (a)
  4612. {
  4613. const IP_ADDR_STRING *ip = &a->IpAddressList;
  4614. while (ip)
  4615. {
  4616. DWORD status;
  4617. const DWORD context = ip->Context;
  4618.  
  4619. if ((status = DeleteIPAddress((ULONG) context)) == NO_ERROR)
  4620. {
  4621. msg(M_INFO, "Successfully deleted previously set dynamic IP/netmask: %s/%s",
  4622. ip->IpAddress.String,
  4623. ip->IpMask.String);
  4624. }
  4625. else
  4626. {
  4627. const char *empty = "0.0.0.0";
  4628. if (strcmp(ip->IpAddress.String, empty)
  4629. || strcmp(ip->IpMask.String, empty))
  4630. {
  4631. msg(M_INFO, "NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%u)",
  4632. ip->IpAddress.String,
  4633. ip->IpMask.String,
  4634. (unsigned int)status);
  4635. }
  4636. }
  4637. ip = ip->Next;
  4638. }
  4639. }
  4640. gc_free(&gc);
  4641. }
  4642.  
  4643. /*
  4644. * Get interface index for use with IP Helper API functions.
  4645. */
  4646. static DWORD
  4647. get_adapter_index_method_1(const char *guid)
  4648. {
  4649. DWORD index;
  4650. ULONG aindex;
  4651. wchar_t wbuf[256];
  4652. swprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid);
  4653. wbuf [SIZE(wbuf) - 1] = 0;
  4654. if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
  4655. {
  4656. index = TUN_ADAPTER_INDEX_INVALID;
  4657. }
  4658. else
  4659. {
  4660. index = (DWORD)aindex;
  4661. }
  4662. return index;
  4663. }
  4664.  
  4665. static DWORD
  4666. get_adapter_index_method_2(const char *guid)
  4667. {
  4668. struct gc_arena gc = gc_new();
  4669. DWORD index = TUN_ADAPTER_INDEX_INVALID;
  4670.  
  4671. const IP_ADAPTER_INFO *list = get_adapter_info_list(&gc);
  4672.  
  4673. while (list)
  4674. {
  4675. if (!strcmp(guid, list->AdapterName))
  4676. {
  4677. index = list->Index;
  4678. break;
  4679. }
  4680. list = list->Next;
  4681. }
  4682.  
  4683. gc_free(&gc);
  4684. return index;
  4685. }
  4686.  
  4687. static DWORD
  4688. get_adapter_index(const char *guid)
  4689. {
  4690. DWORD index;
  4691. index = get_adapter_index_method_1(guid);
  4692. if (index == TUN_ADAPTER_INDEX_INVALID)
  4693. {
  4694. index = get_adapter_index_method_2(guid);
  4695. }
  4696. if (index == TUN_ADAPTER_INDEX_INVALID)
  4697. {
  4698. msg(M_INFO, "NOTE: could not get adapter index for %s", guid);
  4699. }
  4700. return index;
  4701. }
  4702.  
  4703. static DWORD
  4704. get_adapter_index_flexible(const char *name) /* actual name or GUID */
  4705. {
  4706. struct gc_arena gc = gc_new();
  4707. DWORD index;
  4708. index = get_adapter_index_method_1(name);
  4709. if (index == TUN_ADAPTER_INDEX_INVALID)
  4710. {
  4711. index = get_adapter_index_method_2(name);
  4712. }
  4713. if (index == TUN_ADAPTER_INDEX_INVALID)
  4714. {
  4715. const struct tap_reg *tap_reg = get_tap_reg(&gc);
  4716. const struct panel_reg *panel_reg = get_panel_reg(&gc);
  4717. const char *guid = name_to_guid(name, tap_reg, panel_reg);
  4718. index = get_adapter_index_method_1(guid);
  4719. if (index == TUN_ADAPTER_INDEX_INVALID)
  4720. {
  4721. index = get_adapter_index_method_2(guid);
  4722. }
  4723. }
  4724. if (index == TUN_ADAPTER_INDEX_INVALID)
  4725. {
  4726. msg(M_INFO, "NOTE: could not get adapter index for name/GUID '%s'", name);
  4727. }
  4728. gc_free(&gc);
  4729. return index;
  4730. }
  4731.  
  4732. /*
  4733. * Return a string representing a PIP_ADDR_STRING
  4734. */
  4735. static const char *
  4736. format_ip_addr_string(const IP_ADDR_STRING *ip, struct gc_arena *gc)
  4737. {
  4738. struct buffer out = alloc_buf_gc(256, gc);
  4739. while (ip)
  4740. {
  4741. buf_printf(&out, "%s", ip->IpAddress.String);
  4742. if (strlen(ip->IpMask.String))
  4743. {
  4744. buf_printf(&out, "/");
  4745. buf_printf(&out, "%s", ip->IpMask.String);
  4746. }
  4747. buf_printf(&out, " ");
  4748. ip = ip->Next;
  4749. }
  4750. return BSTR(&out);
  4751. }
  4752.  
  4753. /*
  4754. * Show info for a single adapter
  4755. */
  4756. static void
  4757. show_adapter(int msglev, const IP_ADAPTER_INFO *a, struct gc_arena *gc)
  4758. {
  4759. msg(msglev, "%s", a->Description);
  4760. msg(msglev, " Index = %d", (int)a->Index);
  4761. msg(msglev, " GUID = %s", a->AdapterName);
  4762. msg(msglev, " IP = %s", format_ip_addr_string(&a->IpAddressList, gc));
  4763. msg(msglev, " MAC = %s", format_hex_ex(a->Address, a->AddressLength, 0, 1, ":", gc));
  4764. msg(msglev, " GATEWAY = %s", format_ip_addr_string(&a->GatewayList, gc));
  4765. if (a->DhcpEnabled)
  4766. {
  4767. msg(msglev, " DHCP SERV = %s", format_ip_addr_string(&a->DhcpServer, gc));
  4768. msg(msglev, " DHCP LEASE OBTAINED = %s", time_string(a->LeaseObtained, 0, false, gc));
  4769. msg(msglev, " DHCP LEASE EXPIRES = %s", time_string(a->LeaseExpires, 0, false, gc));
  4770. }
  4771. if (a->HaveWins)
  4772. {
  4773. msg(msglev, " PRI WINS = %s", format_ip_addr_string(&a->PrimaryWinsServer, gc));
  4774. msg(msglev, " SEC WINS = %s", format_ip_addr_string(&a->SecondaryWinsServer, gc));
  4775. }
  4776.  
  4777. {
  4778. const IP_PER_ADAPTER_INFO *pai = get_per_adapter_info(a->Index, gc);
  4779. if (pai)
  4780. {
  4781. msg(msglev, " DNS SERV = %s", format_ip_addr_string(&pai->DnsServerList, gc));
  4782. }
  4783. }
  4784. }
  4785.  
  4786. /*
  4787. * Show current adapter list
  4788. */
  4789. void
  4790. show_adapters(int msglev)
  4791. {
  4792. struct gc_arena gc = gc_new();
  4793. const IP_ADAPTER_INFO *ai = get_adapter_info_list(&gc);
  4794.  
  4795. msg(msglev, "SYSTEM ADAPTER LIST");
  4796. if (ai)
  4797. {
  4798. const IP_ADAPTER_INFO *a;
  4799.  
  4800. /* find index in the linked list */
  4801. for (a = ai; a != NULL; a = a->Next)
  4802. {
  4803. show_adapter(msglev, a, &gc);
  4804. }
  4805. }
  4806. gc_free(&gc);
  4807. }
  4808.  
  4809. /*
  4810. * Set a particular TAP-Windows adapter (or all of them if
  4811. * adapter_name == NULL) to allow it to be opened from
  4812. * a non-admin account. This setting will only persist
  4813. * for the lifetime of the device object.
  4814. */
  4815.  
  4816. static void
  4817. tap_allow_nonadmin_access_handle(const char *device_path, HANDLE hand)
  4818. {
  4819. struct security_attributes sa;
  4820. BOOL status;
  4821.  
  4822. if (!init_security_attributes_allow_all(&sa))
  4823. {
  4824. msg(M_ERR, "Error: init SA failed");
  4825. }
  4826.  
  4827. status = SetKernelObjectSecurity(hand, DACL_SECURITY_INFORMATION, &sa.sd);
  4828. if (!status)
  4829. {
  4830. msg(M_ERRNO, "Error: SetKernelObjectSecurity failed on %s", device_path);
  4831. }
  4832. else
  4833. {
  4834. msg(M_INFO|M_NOPREFIX, "TAP-Windows device: %s [Non-admin access allowed]", device_path);
  4835. }
  4836. }
  4837.  
  4838. void
  4839. tap_allow_nonadmin_access(const char *dev_node)
  4840. {
  4841. struct gc_arena gc = gc_new();
  4842. const struct tap_reg *tap_reg = get_tap_reg(&gc);
  4843. const struct panel_reg *panel_reg = get_panel_reg(&gc);
  4844. const char *device_guid = NULL;
  4845. HANDLE hand;
  4846. char actual_buffer[256];
  4847. char device_path[256];
  4848.  
  4849. at_least_one_tap_win(tap_reg);
  4850.  
  4851. if (dev_node)
  4852. {
  4853. /* Get the device GUID for the device specified with --dev-node. */
  4854. device_guid = get_device_guid(dev_node, actual_buffer, sizeof(actual_buffer), tap_reg, panel_reg, &gc);
  4855.  
  4856. if (!device_guid)
  4857. {
  4858. msg(M_FATAL, "TAP-Windows adapter '%s' not found", dev_node);
  4859. }
  4860.  
  4861. /* Open Windows TAP-Windows adapter */
  4862. openvpn_snprintf(device_path, sizeof(device_path), "%s%s%s",
  4863. USERMODEDEVICEDIR,
  4864. device_guid,
  4865. TAP_WIN_SUFFIX);
  4866.  
  4867. hand = CreateFile(
  4868. device_path,
  4869. MAXIMUM_ALLOWED,
  4870. 0, /* was: FILE_SHARE_READ */
  4871. 0,
  4872. OPEN_EXISTING,
  4873. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  4874. 0
  4875. );
  4876.  
  4877. if (hand == INVALID_HANDLE_VALUE)
  4878. {
  4879. msg(M_ERR, "CreateFile failed on TAP device: %s", device_path);
  4880. }
  4881.  
  4882. tap_allow_nonadmin_access_handle(device_path, hand);
  4883. CloseHandle(hand);
  4884. }
  4885. else
  4886. {
  4887. int device_number = 0;
  4888.  
  4889. /* Try opening all TAP devices */
  4890. while (true)
  4891. {
  4892. device_guid = get_unspecified_device_guid(device_number,
  4893. actual_buffer,
  4894. sizeof(actual_buffer),
  4895. tap_reg,
  4896. panel_reg,
  4897. &gc);
  4898.  
  4899. if (!device_guid)
  4900. {
  4901. break;
  4902. }
  4903.  
  4904. /* Open Windows TAP-Windows adapter */
  4905. openvpn_snprintf(device_path, sizeof(device_path), "%s%s%s",
  4906. USERMODEDEVICEDIR,
  4907. device_guid,
  4908. TAP_WIN_SUFFIX);
  4909.  
  4910. hand = CreateFile(
  4911. device_path,
  4912. MAXIMUM_ALLOWED,
  4913. 0, /* was: FILE_SHARE_READ */
  4914. 0,
  4915. OPEN_EXISTING,
  4916. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  4917. 0
  4918. );
  4919.  
  4920. if (hand == INVALID_HANDLE_VALUE)
  4921. {
  4922. msg(M_WARN, "CreateFile failed on TAP device: %s", device_path);
  4923. }
  4924. else
  4925. {
  4926. tap_allow_nonadmin_access_handle(device_path, hand);
  4927. CloseHandle(hand);
  4928. }
  4929.  
  4930. device_number++;
  4931. }
  4932. }
  4933. gc_free(&gc);
  4934. }
  4935.  
  4936. /*
  4937. * DHCP release/renewal
  4938. */
  4939. bool
  4940. dhcp_release_by_adapter_index(const DWORD adapter_index)
  4941. {
  4942. struct gc_arena gc = gc_new();
  4943. bool ret = false;
  4944. const IP_ADAPTER_INDEX_MAP *inter = get_interface_info(adapter_index, &gc);
  4945.  
  4946. if (inter)
  4947. {
  4948. DWORD status = IpReleaseAddress((IP_ADAPTER_INDEX_MAP *)inter);
  4949. if (status == NO_ERROR)
  4950. {
  4951. msg(D_TUNTAP_INFO, "TAP: DHCP address released");
  4952. ret = true;
  4953. }
  4954. else
  4955. {
  4956. msg(M_WARN, "NOTE: Release of DHCP-assigned IP address lease on TAP-Windows adapter failed: %s (code=%u)",
  4957. strerror_win32(status, &gc),
  4958. (unsigned int)status);
  4959. }
  4960. }
  4961.  
  4962. gc_free(&gc);
  4963. return ret;
  4964. }
  4965.  
  4966. static bool
  4967. dhcp_release(const struct tuntap *tt)
  4968. {
  4969. if (tt && tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ && tt->adapter_index != TUN_ADAPTER_INDEX_INVALID)
  4970. {
  4971. return dhcp_release_by_adapter_index(tt->adapter_index);
  4972. }
  4973. else
  4974. {
  4975. return false;
  4976. }
  4977. }
  4978.  
  4979. bool
  4980. dhcp_renew_by_adapter_index(const DWORD adapter_index)
  4981. {
  4982. struct gc_arena gc = gc_new();
  4983. bool ret = false;
  4984. const IP_ADAPTER_INDEX_MAP *inter = get_interface_info(adapter_index, &gc);
  4985.  
  4986. if (inter)
  4987. {
  4988. DWORD status = IpRenewAddress((IP_ADAPTER_INDEX_MAP *)inter);
  4989. if (status == NO_ERROR)
  4990. {
  4991. msg(D_TUNTAP_INFO, "TAP: DHCP address renewal succeeded");
  4992. ret = true;
  4993. }
  4994. else
  4995. {
  4996. msg(M_WARN, "WARNING: Failed to renew DHCP IP address lease on TAP-Windows adapter: %s (code=%u)",
  4997. strerror_win32(status, &gc),
  4998. (unsigned int)status);
  4999. }
  5000. }
  5001. gc_free(&gc);
  5002. return ret;
  5003. }
  5004.  
  5005. static bool
  5006. dhcp_renew(const struct tuntap *tt)
  5007. {
  5008. if (tt && tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ && tt->adapter_index != TUN_ADAPTER_INDEX_INVALID)
  5009. {
  5010. return dhcp_renew_by_adapter_index(tt->adapter_index);
  5011. }
  5012. else
  5013. {
  5014. return false;
  5015. }
  5016. }
  5017.  
  5018. /*
  5019. * netsh functions
  5020. */
  5021.  
  5022. static void
  5023. netsh_command(const struct argv *a, int n, int msglevel)
  5024. {
  5025. int i;
  5026. for (i = 0; i < n; ++i)
  5027. {
  5028. bool status;
  5029. management_sleep(1);
  5030. netcmd_semaphore_lock();
  5031. argv_msg_prefix(M_INFO, a, "NETSH");
  5032. status = openvpn_execve_check(a, NULL, 0, "ERROR: netsh command failed");
  5033. netcmd_semaphore_release();
  5034. if (status)
  5035. {
  5036. return;
  5037. }
  5038. management_sleep(4);
  5039. }
  5040. msg(msglevel, "NETSH: command failed");
  5041. }
  5042.  
  5043. void
  5044. ipconfig_register_dns(const struct env_set *es)
  5045. {
  5046. struct argv argv = argv_new();
  5047. const char err[] = "ERROR: Windows ipconfig command failed";
  5048.  
  5049. msg(D_TUNTAP_INFO, "Start ipconfig commands for register-dns...");
  5050. netcmd_semaphore_lock();
  5051.  
  5052. argv_printf(&argv, "%s%sc /flushdns",
  5053. get_win_sys_path(),
  5054. WIN_IPCONFIG_PATH_SUFFIX);
  5055. argv_msg(D_TUNTAP_INFO, &argv);
  5056. openvpn_execve_check(&argv, es, 0, err);
  5057. argv_reset(&argv);
  5058.  
  5059. argv_printf(&argv, "%s%sc /registerdns",
  5060. get_win_sys_path(),
  5061. WIN_IPCONFIG_PATH_SUFFIX);
  5062. argv_msg(D_TUNTAP_INFO, &argv);
  5063. openvpn_execve_check(&argv, es, 0, err);
  5064. argv_reset(&argv);
  5065.  
  5066. netcmd_semaphore_release();
  5067. msg(D_TUNTAP_INFO, "End ipconfig commands for register-dns...");
  5068. }
  5069.  
  5070. void
  5071. ip_addr_string_to_array(in_addr_t *dest, int *dest_len, const IP_ADDR_STRING *src)
  5072. {
  5073. int i = 0;
  5074. while (src)
  5075. {
  5076. const unsigned int getaddr_flags = GETADDR_HOST_ORDER;
  5077. const char *ip_str = src->IpAddress.String;
  5078. in_addr_t ip = 0;
  5079. bool succeed = false;
  5080.  
  5081. if (i >= *dest_len)
  5082. {
  5083. break;
  5084. }
  5085. if (!ip_str || !strlen(ip_str))
  5086. {
  5087. break;
  5088. }
  5089.  
  5090. ip = getaddr(getaddr_flags, ip_str, 0, &succeed, NULL);
  5091. if (!succeed)
  5092. {
  5093. break;
  5094. }
  5095. dest[i++] = ip;
  5096.  
  5097. src = src->Next;
  5098. }
  5099. *dest_len = i;
  5100.  
  5101. #if 0
  5102. {
  5103. struct gc_arena gc = gc_new();
  5104. msg(M_INFO, "ip_addr_string_to_array [%d]", *dest_len);
  5105. for (i = 0; i < *dest_len; ++i)
  5106. {
  5107. msg(M_INFO, "%s", print_in_addr_t(dest[i], 0, &gc));
  5108. }
  5109. gc_free(&gc);
  5110. }
  5111. #endif
  5112. }
  5113.  
  5114. static bool
  5115. ip_addr_one_to_one(const in_addr_t *a1, const int a1len, const IP_ADDR_STRING *ias)
  5116. {
  5117. in_addr_t a2[8];
  5118. int a2len = SIZE(a2);
  5119. int i;
  5120.  
  5121. ip_addr_string_to_array(a2, &a2len, ias);
  5122. /*msg (M_INFO, "a1len=%d a2len=%d", a1len, a2len);*/
  5123. if (a1len != a2len)
  5124. {
  5125. return false;
  5126. }
  5127.  
  5128. for (i = 0; i < a1len; ++i)
  5129. {
  5130. if (a1[i] != a2[i])
  5131. {
  5132. return false;
  5133. }
  5134. }
  5135. return true;
  5136. }
  5137.  
  5138. static bool
  5139. ip_addr_member_of(const in_addr_t addr, const IP_ADDR_STRING *ias)
  5140. {
  5141. in_addr_t aa[8];
  5142. int len = SIZE(aa);
  5143. int i;
  5144.  
  5145. ip_addr_string_to_array(aa, &len, ias);
  5146. for (i = 0; i < len; ++i)
  5147. {
  5148. if (addr == aa[i])
  5149. {
  5150. return true;
  5151. }
  5152. }
  5153. return false;
  5154. }
  5155.  
  5156. /**
  5157. * Set the ipv6 dns servers on the specified interface.
  5158. * The list of dns servers currently set on the interface
  5159. * are cleared first.
  5160. * No action is taken if number of addresses (addr_len) < 1.
  5161. */
  5162. static void
  5163. netsh_set_dns6_servers(const struct in6_addr *addr_list,
  5164. const int addr_len,
  5165. const char *flex_name)
  5166. {
  5167. struct gc_arena gc = gc_new();
  5168. struct argv argv = argv_new();
  5169.  
  5170. for (int i = 0; i < addr_len; ++i)
  5171. {
  5172. const char *fmt = (i == 0) ?
  5173. "%s%sc interface ipv6 set dns %s static %s"
  5174. : "%s%sc interface ipv6 add dns %s %s";
  5175. argv_printf(&argv, fmt, get_win_sys_path(),
  5176. NETSH_PATH_SUFFIX, flex_name,
  5177. print_in6_addr(addr_list[i], 0, &gc));
  5178.  
  5179. /* disable slow address validation on Windows 7 and higher */
  5180. if (win32_version_info() >= WIN_7)
  5181. {
  5182. argv_printf_cat(&argv, "%s", "validate=no");
  5183. }
  5184.  
  5185. /* Treat errors while adding as non-fatal as we do not check for duplicates */
  5186. netsh_command(&argv, 1, (i==0) ? M_FATAL : M_NONFATAL);
  5187. }
  5188.  
  5189. argv_reset(&argv);
  5190. gc_free(&gc);
  5191. }
  5192.  
  5193. static void
  5194. netsh_ifconfig_options(const char *type,
  5195. const in_addr_t *addr_list,
  5196. const int addr_len,
  5197. const IP_ADDR_STRING *current,
  5198. const char *flex_name,
  5199. const bool test_first)
  5200. {
  5201. struct gc_arena gc = gc_new();
  5202. struct argv argv = argv_new();
  5203. bool delete_first = false;
  5204.  
  5205. /* first check if we should delete existing DNS/WINS settings from TAP interface */
  5206. if (test_first)
  5207. {
  5208. if (!ip_addr_one_to_one(addr_list, addr_len, current))
  5209. {
  5210. delete_first = true;
  5211. }
  5212. }
  5213. else
  5214. {
  5215. delete_first = true;
  5216. }
  5217.  
  5218. /* delete existing DNS/WINS settings from TAP interface */
  5219. if (delete_first)
  5220. {
  5221. argv_printf(&argv, "%s%sc interface ip delete %s %s all",
  5222. get_win_sys_path(),
  5223. NETSH_PATH_SUFFIX,
  5224. type,
  5225. flex_name);
  5226. netsh_command(&argv, 2, M_FATAL);
  5227. }
  5228.  
  5229. /* add new DNS/WINS settings to TAP interface */
  5230. {
  5231. int count = 0;
  5232. int i;
  5233. for (i = 0; i < addr_len; ++i)
  5234. {
  5235. if (delete_first || !test_first || !ip_addr_member_of(addr_list[i], current))
  5236. {
  5237. const char *fmt = count ?
  5238. "%s%sc interface ip add %s %s %s"
  5239. : "%s%sc interface ip set %s %s static %s";
  5240.  
  5241. argv_printf(&argv, fmt,
  5242. get_win_sys_path(),
  5243. NETSH_PATH_SUFFIX,
  5244. type,
  5245. flex_name,
  5246. print_in_addr_t(addr_list[i], 0, &gc));
  5247. netsh_command(&argv, 2, M_FATAL);
  5248.  
  5249. ++count;
  5250. }
  5251. else
  5252. {
  5253. msg(M_INFO, "NETSH: \"%s\" %s %s [already set]",
  5254. flex_name,
  5255. type,
  5256. print_in_addr_t(addr_list[i], 0, &gc));
  5257. }
  5258. }
  5259. }
  5260.  
  5261. argv_reset(&argv);
  5262. gc_free(&gc);
  5263. }
  5264.  
  5265. static void
  5266. init_ip_addr_string2(IP_ADDR_STRING *dest, const IP_ADDR_STRING *src1, const IP_ADDR_STRING *src2)
  5267. {
  5268. CLEAR(dest[0]);
  5269. CLEAR(dest[1]);
  5270. if (src1)
  5271. {
  5272. dest[0] = *src1;
  5273. dest[0].Next = NULL;
  5274. }
  5275. if (src2)
  5276. {
  5277. dest[1] = *src2;
  5278. dest[0].Next = &dest[1];
  5279. dest[1].Next = NULL;
  5280. }
  5281. }
  5282.  
  5283. static void
  5284. netsh_ifconfig(const struct tuntap_options *to,
  5285. const char *flex_name,
  5286. const in_addr_t ip,
  5287. const in_addr_t netmask,
  5288. const unsigned int flags)
  5289. {
  5290. struct gc_arena gc = gc_new();
  5291. struct argv argv = argv_new();
  5292. const IP_ADAPTER_INFO *ai = NULL;
  5293. const IP_PER_ADAPTER_INFO *pai = NULL;
  5294.  
  5295. if (flags & NI_TEST_FIRST)
  5296. {
  5297. const IP_ADAPTER_INFO *list = get_adapter_info_list(&gc);
  5298. const int index = get_adapter_index_flexible(flex_name);
  5299. ai = get_adapter(list, index);
  5300. pai = get_per_adapter_info(index, &gc);
  5301. }
  5302.  
  5303. if (flags & NI_IP_NETMASK)
  5304. {
  5305. if (test_adapter_ip_netmask(ai, ip, netmask))
  5306. {
  5307. msg(M_INFO, "NETSH: \"%s\" %s/%s [already set]",
  5308. flex_name,
  5309. print_in_addr_t(ip, 0, &gc),
  5310. print_in_addr_t(netmask, 0, &gc));
  5311. }
  5312. else
  5313. {
  5314. /* example: netsh interface ip set address my-tap static 10.3.0.1 255.255.255.0 */
  5315. argv_printf(&argv, "%s%sc interface ip set address %s static %s %s",
  5316. get_win_sys_path(),
  5317. NETSH_PATH_SUFFIX,
  5318. flex_name,
  5319. print_in_addr_t(ip, 0, &gc),
  5320. print_in_addr_t(netmask, 0, &gc));
  5321.  
  5322. netsh_command(&argv, 4, M_FATAL);
  5323. }
  5324. }
  5325.  
  5326. /* set WINS/DNS options */
  5327. if (flags & NI_OPTIONS)
  5328. {
  5329. IP_ADDR_STRING wins[2];
  5330. CLEAR(wins[0]);
  5331. CLEAR(wins[1]);
  5332.  
  5333. netsh_ifconfig_options("dns",
  5334. to->dns,
  5335. to->dns_len,
  5336. pai ? &pai->DnsServerList : NULL,
  5337. flex_name,
  5338. BOOL_CAST(flags & NI_TEST_FIRST));
  5339. if (ai && ai->HaveWins)
  5340. {
  5341. init_ip_addr_string2(wins, &ai->PrimaryWinsServer, &ai->SecondaryWinsServer);
  5342. }
  5343.  
  5344. netsh_ifconfig_options("wins",
  5345. to->wins,
  5346. to->wins_len,
  5347. ai ? wins : NULL,
  5348. flex_name,
  5349. BOOL_CAST(flags & NI_TEST_FIRST));
  5350. }
  5351.  
  5352. argv_reset(&argv);
  5353. gc_free(&gc);
  5354. }
  5355.  
  5356. static void
  5357. netsh_enable_dhcp(const char *actual_name)
  5358. {
  5359. struct argv argv = argv_new();
  5360.  
  5361. /* example: netsh interface ip set address my-tap dhcp */
  5362. argv_printf(&argv,
  5363. "%s%sc interface ip set address %s dhcp",
  5364. get_win_sys_path(),
  5365. NETSH_PATH_SUFFIX,
  5366. actual_name);
  5367.  
  5368. netsh_command(&argv, 4, M_FATAL);
  5369.  
  5370. argv_reset(&argv);
  5371. }
  5372.  
  5373. /* Enable dhcp on tap adapter using iservice */
  5374. static bool
  5375. service_enable_dhcp(const struct tuntap *tt)
  5376. {
  5377. DWORD len;
  5378. bool ret = false;
  5379. ack_message_t ack;
  5380. struct gc_arena gc = gc_new();
  5381. HANDLE pipe = tt->options.msg_channel;
  5382.  
  5383. enable_dhcp_message_t dhcp = {
  5384. .header = {
  5385. msg_enable_dhcp,
  5386. sizeof(enable_dhcp_message_t),
  5387. 0
  5388. },
  5389. .iface = { .index = tt->adapter_index, .name = "" }
  5390. };
  5391.  
  5392. if (!WriteFile(pipe, &dhcp, sizeof(dhcp), &len, NULL)
  5393. || !ReadFile(pipe, &ack, sizeof(ack), &len, NULL))
  5394. {
  5395. msg(M_WARN, "Enable_dhcp: could not talk to service: %s [%lu]",
  5396. strerror_win32(GetLastError(), &gc), GetLastError());
  5397. goto out;
  5398. }
  5399.  
  5400. if (ack.error_number != NO_ERROR)
  5401. {
  5402. msg(M_NONFATAL, "TUN: enabling dhcp using service failed: %s [status=%u if_index=%d]",
  5403. strerror_win32(ack.error_number, &gc), ack.error_number, dhcp.iface.index);
  5404. }
  5405. else
  5406. {
  5407. msg(M_INFO, "DHCP enabled on interface %d using service", dhcp.iface.index);
  5408. ret = true;
  5409. }
  5410.  
  5411. out:
  5412. gc_free(&gc);
  5413. return ret;
  5414. }
  5415.  
  5416. /*
  5417. * Return a TAP name for netsh commands.
  5418. */
  5419. static const char *
  5420. netsh_get_id(const char *dev_node, struct gc_arena *gc)
  5421. {
  5422. const struct tap_reg *tap_reg = get_tap_reg(gc);
  5423. const struct panel_reg *panel_reg = get_panel_reg(gc);
  5424. struct buffer actual = alloc_buf_gc(256, gc);
  5425. const char *guid;
  5426.  
  5427. at_least_one_tap_win(tap_reg);
  5428.  
  5429. if (dev_node)
  5430. {
  5431. guid = get_device_guid(dev_node, BPTR(&actual), BCAP(&actual), tap_reg, panel_reg, gc);
  5432. }
  5433. else
  5434. {
  5435. guid = get_unspecified_device_guid(0, BPTR(&actual), BCAP(&actual), tap_reg, panel_reg, gc);
  5436.  
  5437. if (get_unspecified_device_guid(1, NULL, 0, tap_reg, panel_reg, gc)) /* ambiguous if more than one TAP-Windows adapter */
  5438. {
  5439. guid = NULL;
  5440. }
  5441. }
  5442.  
  5443. if (!guid)
  5444. {
  5445. return "NULL"; /* not found */
  5446. }
  5447. else if (strcmp(BPTR(&actual), "NULL"))
  5448. {
  5449. return BPTR(&actual); /* control panel name */
  5450. }
  5451. else
  5452. {
  5453. return guid; /* no control panel name, return GUID instead */
  5454. }
  5455. }
  5456.  
  5457. /*
  5458. * Called iteratively on TAP-Windows wait-for-initialization polling loop
  5459. */
  5460. void
  5461. tun_standby_init(struct tuntap *tt)
  5462. {
  5463. tt->standby_iter = 0;
  5464. }
  5465.  
  5466. bool
  5467. tun_standby(struct tuntap *tt)
  5468. {
  5469. bool ret = true;
  5470. ++tt->standby_iter;
  5471. if (tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
  5472. {
  5473. if (tt->standby_iter == IPW32_SET_ADAPTIVE_TRY_NETSH)
  5474. {
  5475. msg(M_INFO, "NOTE: now trying netsh (this may take some time)");
  5476. netsh_ifconfig(&tt->options,
  5477. tt->actual_name,
  5478. tt->local,
  5479. tt->adapter_netmask,
  5480. NI_TEST_FIRST|NI_IP_NETMASK|NI_OPTIONS);
  5481. }
  5482. else if (tt->standby_iter >= IPW32_SET_ADAPTIVE_TRY_NETSH*2)
  5483. {
  5484. ret = false;
  5485. }
  5486. }
  5487. return ret;
  5488. }
  5489.  
  5490. /*
  5491. * Convert DHCP options from the command line / config file
  5492. * into a raw DHCP-format options string.
  5493. */
  5494.  
  5495. static void
  5496. write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *error)
  5497. {
  5498. if (!buf_safe(buf, 3))
  5499. {
  5500. *error = true;
  5501. msg(M_WARN, "write_dhcp_u8: buffer overflow building DHCP options");
  5502. return;
  5503. }
  5504. buf_write_u8(buf, type);
  5505. buf_write_u8(buf, 1);
  5506. buf_write_u8(buf, data);
  5507. }
  5508.  
  5509. static void
  5510. write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *data, const unsigned int len, bool *error)
  5511. {
  5512. if (len > 0)
  5513. {
  5514. int i;
  5515. const int size = len * sizeof(uint32_t);
  5516.  
  5517. if (!buf_safe(buf, 2 + size))
  5518. {
  5519. *error = true;
  5520. msg(M_WARN, "write_dhcp_u32_array: buffer overflow building DHCP options");
  5521. return;
  5522. }
  5523. if (size < 1 || size > 255)
  5524. {
  5525. *error = true;
  5526. msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size);
  5527. return;
  5528. }
  5529. buf_write_u8(buf, type);
  5530. buf_write_u8(buf, size);
  5531. for (i = 0; i < len; ++i)
  5532. {
  5533. buf_write_u32(buf, data[i]);
  5534. }
  5535. }
  5536. }
  5537.  
  5538. static void
  5539. write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *error)
  5540. {
  5541. const int len = strlen(str);
  5542. if (!buf_safe(buf, 2 + len))
  5543. {
  5544. *error = true;
  5545. msg(M_WARN, "write_dhcp_str: buffer overflow building DHCP options");
  5546. return;
  5547. }
  5548. if (len < 1 || len > 255)
  5549. {
  5550. *error = true;
  5551. msg(M_WARN, "write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str);
  5552. return;
  5553. }
  5554. buf_write_u8(buf, type);
  5555. buf_write_u8(buf, len);
  5556. buf_write(buf, str, len);
  5557. }
  5558.  
  5559. static bool
  5560. build_dhcp_options_string(struct buffer *buf, const struct tuntap_options *o)
  5561. {
  5562. bool error = false;
  5563. if (o->domain)
  5564. {
  5565. write_dhcp_str(buf, 15, o->domain, &error);
  5566. }
  5567.  
  5568. if (o->netbios_scope)
  5569. {
  5570. write_dhcp_str(buf, 47, o->netbios_scope, &error);
  5571. }
  5572.  
  5573. if (o->netbios_node_type)
  5574. {
  5575. write_dhcp_u8(buf, 46, o->netbios_node_type, &error);
  5576. }
  5577.  
  5578. write_dhcp_u32_array(buf, 6, (uint32_t *)o->dns, o->dns_len, &error);
  5579. write_dhcp_u32_array(buf, 44, (uint32_t *)o->wins, o->wins_len, &error);
  5580. write_dhcp_u32_array(buf, 42, (uint32_t *)o->ntp, o->ntp_len, &error);
  5581. write_dhcp_u32_array(buf, 45, (uint32_t *)o->nbdd, o->nbdd_len, &error);
  5582.  
  5583. /* the MS DHCP server option 'Disable Netbios-over-TCP/IP
  5584. * is implemented as vendor option 001, value 002.
  5585. * A value of 001 means 'leave NBT alone' which is the default */
  5586. if (o->disable_nbt)
  5587. {
  5588. if (!buf_safe(buf, 8))
  5589. {
  5590. msg(M_WARN, "build_dhcp_options_string: buffer overflow building DHCP options");
  5591. return false;
  5592. }
  5593. buf_write_u8(buf, 43);
  5594. buf_write_u8(buf, 6);/* total length field */
  5595. buf_write_u8(buf, 0x001);
  5596. buf_write_u8(buf, 4);/* length of the vendor specified field */
  5597. buf_write_u32(buf, 0x002);
  5598. }
  5599. return !error;
  5600. }
  5601.  
  5602. static void
  5603. fork_dhcp_action(struct tuntap *tt)
  5604. {
  5605. if (tt->options.dhcp_pre_release || tt->options.dhcp_renew)
  5606. {
  5607. struct gc_arena gc = gc_new();
  5608. struct buffer cmd = alloc_buf_gc(256, &gc);
  5609. const int verb = 3;
  5610. const int pre_sleep = 1;
  5611.  
  5612. buf_printf(&cmd, "openvpn --verb %d --tap-sleep %d", verb, pre_sleep);
  5613. if (tt->options.dhcp_pre_release)
  5614. {
  5615. buf_printf(&cmd, " --dhcp-pre-release");
  5616. }
  5617. if (tt->options.dhcp_renew)
  5618. {
  5619. buf_printf(&cmd, " --dhcp-renew");
  5620. }
  5621. buf_printf(&cmd, " --dhcp-internal %u", (unsigned int)tt->adapter_index);
  5622.  
  5623. fork_to_self(BSTR(&cmd));
  5624. gc_free(&gc);
  5625. }
  5626. }
  5627.  
  5628. static void
  5629. register_dns_service(const struct tuntap *tt)
  5630. {
  5631. DWORD len;
  5632. HANDLE msg_channel = tt->options.msg_channel;
  5633. ack_message_t ack;
  5634. struct gc_arena gc = gc_new();
  5635.  
  5636. message_header_t rdns = { msg_register_dns, sizeof(message_header_t), 0 };
  5637.  
  5638. if (!WriteFile(msg_channel, &rdns, sizeof(rdns), &len, NULL)
  5639. || !ReadFile(msg_channel, &ack, sizeof(ack), &len, NULL))
  5640. {
  5641. msg(M_WARN, "Register_dns: could not talk to service: %s [status=0x%lx]",
  5642. strerror_win32(GetLastError(), &gc), GetLastError());
  5643. }
  5644.  
  5645. else if (ack.error_number != NO_ERROR)
  5646. {
  5647. msg(M_WARN, "Register_dns failed using service: %s [status=0x%x]",
  5648. strerror_win32(ack.error_number, &gc), ack.error_number);
  5649. }
  5650.  
  5651. else
  5652. {
  5653. msg(M_INFO, "Register_dns request sent to the service");
  5654. }
  5655.  
  5656. gc_free(&gc);
  5657. }
  5658.  
  5659. void
  5660. fork_register_dns_action(struct tuntap *tt)
  5661. {
  5662. if (tt && tt->options.register_dns && tt->options.msg_channel)
  5663. {
  5664. register_dns_service(tt);
  5665. }
  5666. else if (tt && tt->options.register_dns)
  5667. {
  5668. struct gc_arena gc = gc_new();
  5669. struct buffer cmd = alloc_buf_gc(256, &gc);
  5670. const int verb = 3;
  5671.  
  5672. buf_printf(&cmd, "openvpn --verb %d --register-dns --rdns-internal", verb);
  5673. fork_to_self(BSTR(&cmd));
  5674. gc_free(&gc);
  5675. }
  5676. }
  5677.  
  5678. static uint32_t
  5679. dhcp_masq_addr(const in_addr_t local, const in_addr_t netmask, const int offset)
  5680. {
  5681. struct gc_arena gc = gc_new();
  5682. in_addr_t dsa; /* DHCP server addr */
  5683.  
  5684. if (offset < 0)
  5685. {
  5686. dsa = (local | (~netmask)) + offset;
  5687. }
  5688. else
  5689. {
  5690. dsa = (local & netmask) + offset;
  5691. }
  5692.  
  5693. if (dsa == local)
  5694. {
  5695. msg(M_FATAL, "ERROR: There is a clash between the --ifconfig local address and the internal DHCP server address -- both are set to %s -- please use the --ip-win32 dynamic option to choose a different free address from the --ifconfig subnet for the internal DHCP server", print_in_addr_t(dsa, 0, &gc));
  5696. }
  5697.  
  5698. if ((local & netmask) != (dsa & netmask))
  5699. {
  5700. msg(M_FATAL, "ERROR: --ip-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
  5701. }
  5702.  
  5703. gc_free(&gc);
  5704. return htonl(dsa);
  5705. }
  5706.  
  5707. void
  5708. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  5709. {
  5710. struct gc_arena gc = gc_new();
  5711. char device_path[256];
  5712. const char *device_guid = NULL;
  5713. DWORD len;
  5714. bool dhcp_masq = false;
  5715. bool dhcp_masq_post = false;
  5716.  
  5717. /*netcmd_semaphore_lock ();*/
  5718.  
  5719. msg( M_INFO, "open_tun");
  5720.  
  5721. if (tt->type == DEV_TYPE_NULL)
  5722. {
  5723. open_null(tt);
  5724. gc_free(&gc);
  5725. return;
  5726. }
  5727. else if (tt->type == DEV_TYPE_TAP || tt->type == DEV_TYPE_TUN)
  5728. {
  5729. }
  5730. else
  5731. {
  5732. msg(M_FATAL|M_NOPREFIX, "Unknown virtual device type: '%s'", dev);
  5733. }
  5734.  
  5735. /*
  5736. * Lookup the device name in the registry, using the --dev-node high level name.
  5737. */
  5738. {
  5739. const struct tap_reg *tap_reg = get_tap_reg(&gc);
  5740. const struct panel_reg *panel_reg = get_panel_reg(&gc);
  5741. char actual_buffer[256];
  5742.  
  5743. at_least_one_tap_win(tap_reg);
  5744.  
  5745. if (dev_node)
  5746. {
  5747. /* Get the device GUID for the device specified with --dev-node. */
  5748. device_guid = get_device_guid(dev_node, actual_buffer, sizeof(actual_buffer), tap_reg, panel_reg, &gc);
  5749.  
  5750. if (!device_guid)
  5751. {
  5752. msg(M_FATAL, "TAP-Windows adapter '%s' not found", dev_node);
  5753. }
  5754.  
  5755. /* Open Windows TAP-Windows adapter */
  5756. openvpn_snprintf(device_path, sizeof(device_path), "%s%s%s",
  5757. USERMODEDEVICEDIR,
  5758. device_guid,
  5759. TAP_WIN_SUFFIX);
  5760.  
  5761. tt->hand = CreateFile(
  5762. device_path,
  5763. GENERIC_READ | GENERIC_WRITE,
  5764. 0, /* was: FILE_SHARE_READ */
  5765. 0,
  5766. OPEN_EXISTING,
  5767. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  5768. 0
  5769. );
  5770.  
  5771. if (tt->hand == INVALID_HANDLE_VALUE)
  5772. {
  5773. msg(M_ERR, "CreateFile failed on TAP device: %s", device_path);
  5774. }
  5775. }
  5776. else
  5777. {
  5778. int device_number = 0;
  5779.  
  5780. /* Try opening all TAP devices until we find one available */
  5781. while (true)
  5782. {
  5783. device_guid = get_unspecified_device_guid(device_number,
  5784. actual_buffer,
  5785. sizeof(actual_buffer),
  5786. tap_reg,
  5787. panel_reg,
  5788. &gc);
  5789.  
  5790. if (!device_guid)
  5791. {
  5792. msg(M_FATAL, "All TAP-Windows adapters on this system are currently in use.");
  5793. }
  5794.  
  5795. /* Open Windows TAP-Windows adapter */
  5796. openvpn_snprintf(device_path, sizeof(device_path), "%s%s%s",
  5797. USERMODEDEVICEDIR,
  5798. device_guid,
  5799. TAP_WIN_SUFFIX);
  5800.  
  5801. tt->hand = CreateFile(
  5802. device_path,
  5803. GENERIC_READ | GENERIC_WRITE,
  5804. 0, /* was: FILE_SHARE_READ */
  5805. 0,
  5806. OPEN_EXISTING,
  5807. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  5808. 0
  5809. );
  5810.  
  5811. if (tt->hand == INVALID_HANDLE_VALUE)
  5812. {
  5813. msg(D_TUNTAP_INFO, "CreateFile failed on TAP device: %s", device_path);
  5814. }
  5815. else
  5816. {
  5817. break;
  5818. }
  5819.  
  5820. device_number++;
  5821. }
  5822. }
  5823.  
  5824. /* translate high-level device name into a device instance
  5825. * GUID using the registry */
  5826. tt->actual_name = string_alloc(actual_buffer, NULL);
  5827. }
  5828.  
  5829. msg(M_INFO, "TAP-WIN32 device [%s] opened: %s", tt->actual_name, device_path);
  5830. tt->adapter_index = get_adapter_index(device_guid);
  5831.  
  5832. /* get driver version info */
  5833. {
  5834. ULONG info[3];
  5835. CLEAR(info);
  5836. if (DeviceIoControl(tt->hand, TAP_WIN_IOCTL_GET_VERSION,
  5837. &info, sizeof(info),
  5838. &info, sizeof(info), &len, NULL))
  5839. {
  5840. msg(D_TUNTAP_INFO, "TAP-Windows Driver Version %d.%d %s",
  5841. (int) info[0],
  5842. (int) info[1],
  5843. (info[2] ? "(DEBUG)" : ""));
  5844.  
  5845. }
  5846. if (!(info[0] == TAP_WIN_MIN_MAJOR && info[1] >= TAP_WIN_MIN_MINOR))
  5847. {
  5848. msg(M_FATAL, "ERROR: This version of " PACKAGE_NAME " requires a TAP-Windows driver that is at least version %d.%d -- If you recently upgraded your " PACKAGE_NAME " distribution, a reboot is probably required at this point to get Windows to see the new driver.",
  5849. TAP_WIN_MIN_MAJOR,
  5850. TAP_WIN_MIN_MINOR);
  5851. }
  5852.  
  5853. /* usage of numeric constants is ugly, but this is really tied to
  5854. * *this* version of the driver
  5855. */
  5856. if (tt->type == DEV_TYPE_TUN
  5857. && info[0] == 9 && info[1] < 8)
  5858. {
  5859. msg( M_INFO, "WARNING: Tap-Win32 driver version %d.%d does not support IPv6 in TUN mode. IPv6 will not work. Upgrade your Tap-Win32 driver.", (int) info[0], (int) info[1] );
  5860. }
  5861.  
  5862. /* tap driver 9.8 (2.2.0 and 2.2.1 release) is buggy
  5863. */
  5864. if (tt->type == DEV_TYPE_TUN
  5865. && info[0] == 9 && info[1] == 8)
  5866. {
  5867. msg( M_FATAL, "ERROR: Tap-Win32 driver version %d.%d is buggy regarding small IPv4 packets in TUN mode. Upgrade your Tap-Win32 driver.", (int) info[0], (int) info[1] );
  5868. }
  5869. }
  5870.  
  5871. /* get driver MTU */
  5872. {
  5873. ULONG mtu;
  5874. if (DeviceIoControl(tt->hand, TAP_WIN_IOCTL_GET_MTU,
  5875. &mtu, sizeof(mtu),
  5876. &mtu, sizeof(mtu), &len, NULL))
  5877. {
  5878. tt->post_open_mtu = (int) mtu;
  5879. msg(D_MTU_INFO, "TAP-Windows MTU=%d", (int) mtu);
  5880. }
  5881. }
  5882.  
  5883. /*
  5884. * Preliminaries for setting TAP-Windows adapter TCP/IP
  5885. * properties via --ip-win32 dynamic or --ip-win32 adaptive.
  5886. */
  5887. if (tt->did_ifconfig_setup)
  5888. {
  5889. if (tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ)
  5890. {
  5891. /*
  5892. * If adapter is set to non-DHCP, set to DHCP mode.
  5893. */
  5894. if (dhcp_status(tt->adapter_index) == DHCP_STATUS_DISABLED)
  5895. {
  5896. /* try using the service if available, else directly execute netsh */
  5897. if (tt->options.msg_channel)
  5898. {
  5899. service_enable_dhcp(tt);
  5900. }
  5901. else
  5902. {
  5903. netsh_enable_dhcp(tt->actual_name);
  5904. }
  5905. }
  5906. dhcp_masq = true;
  5907. dhcp_masq_post = true;
  5908. }
  5909. else if (tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
  5910. {
  5911. /*
  5912. * If adapter is set to non-DHCP, use netsh right away.
  5913. */
  5914. if (dhcp_status(tt->adapter_index) != DHCP_STATUS_ENABLED)
  5915. {
  5916. netsh_ifconfig(&tt->options,
  5917. tt->actual_name,
  5918. tt->local,
  5919. tt->adapter_netmask,
  5920. NI_TEST_FIRST|NI_IP_NETMASK|NI_OPTIONS);
  5921. }
  5922. else
  5923. {
  5924. dhcp_masq = true;
  5925. }
  5926. }
  5927. }
  5928.  
  5929. /* set point-to-point mode if TUN device */
  5930.  
  5931. if (tt->type == DEV_TYPE_TUN)
  5932. {
  5933. if (!tt->did_ifconfig_setup)
  5934. {
  5935. msg(M_FATAL, "ERROR: --dev tun also requires --ifconfig");
  5936. }
  5937.  
  5938. if (tt->topology == TOP_SUBNET)
  5939. {
  5940. in_addr_t ep[3];
  5941. BOOL status;
  5942.  
  5943. ep[0] = htonl(tt->local);
  5944. ep[1] = htonl(tt->local & tt->remote_netmask);
  5945. ep[2] = htonl(tt->remote_netmask);
  5946.  
  5947. status = DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_TUN,
  5948. ep, sizeof(ep),
  5949. ep, sizeof(ep), &len, NULL);
  5950.  
  5951. msg(status ? M_INFO : M_FATAL, "Set TAP-Windows TUN subnet mode network/local/netmask = %s/%s/%s [%s]",
  5952. print_in_addr_t(ep[1], IA_NET_ORDER, &gc),
  5953. print_in_addr_t(ep[0], IA_NET_ORDER, &gc),
  5954. print_in_addr_t(ep[2], IA_NET_ORDER, &gc),
  5955. status ? "SUCCEEDED" : "FAILED");
  5956.  
  5957. }
  5958. else
  5959. {
  5960.  
  5961. in_addr_t ep[2];
  5962. ep[0] = htonl(tt->local);
  5963. ep[1] = htonl(tt->remote_netmask);
  5964.  
  5965. if (!DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT,
  5966. ep, sizeof(ep),
  5967. ep, sizeof(ep), &len, NULL))
  5968. {
  5969. msg(M_FATAL, "ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
  5970. }
  5971. }
  5972. }
  5973.  
  5974. /* should we tell the TAP-Windows driver to masquerade as a DHCP server as a means
  5975. * of setting the adapter address? */
  5976. if (dhcp_masq)
  5977. {
  5978. uint32_t ep[4];
  5979.  
  5980. /* We will answer DHCP requests with a reply to set IP/subnet to these values */
  5981. ep[0] = htonl(tt->local);
  5982. ep[1] = htonl(tt->adapter_netmask);
  5983.  
  5984. /* At what IP address should the DHCP server masquerade at? */
  5985. if (tt->type == DEV_TYPE_TUN)
  5986. {
  5987. if (tt->topology == TOP_SUBNET)
  5988. {
  5989. if (tt->options.dhcp_masq_custom_offset)
  5990. {
  5991. ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, tt->options.dhcp_masq_offset);
  5992. }
  5993. else
  5994. {
  5995. ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
  5996. }
  5997. }
  5998. else
  5999. {
  6000. ep[2] = htonl(tt->remote_netmask);
  6001. }
  6002. }
  6003. else
  6004. {
  6005. ASSERT(tt->type == DEV_TYPE_TAP);
  6006. ep[2] = dhcp_masq_addr(tt->local, tt->adapter_netmask, tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset : 0);
  6007. }
  6008.  
  6009. /* lease time in seconds */
  6010. ep[3] = (uint32_t) tt->options.dhcp_lease_time;
  6011.  
  6012. ASSERT(ep[3] > 0);
  6013.  
  6014. #ifndef SIMULATE_DHCP_FAILED /* this code is disabled to simulate bad DHCP negotiation */
  6015. if (!DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_DHCP_MASQ,
  6016. ep, sizeof(ep),
  6017. ep, sizeof(ep), &len, NULL))
  6018. {
  6019. msg(M_FATAL, "ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set TAP_WIN_IOCTL_CONFIG_DHCP_MASQ mode");
  6020. }
  6021.  
  6022. msg(M_INFO, "Notified TAP-Windows driver to set a DHCP IP/netmask of %s/%s on interface %s [DHCP-serv: %s, lease-time: %d]",
  6023. print_in_addr_t(tt->local, 0, &gc),
  6024. print_in_addr_t(tt->adapter_netmask, 0, &gc),
  6025. device_guid,
  6026. print_in_addr_t(ep[2], IA_NET_ORDER, &gc),
  6027. ep[3]
  6028. );
  6029.  
  6030. /* user-supplied DHCP options capability */
  6031. if (tt->options.dhcp_options)
  6032. {
  6033. struct buffer buf = alloc_buf(256);
  6034. if (build_dhcp_options_string(&buf, &tt->options))
  6035. {
  6036. msg(D_DHCP_OPT, "DHCP option string: %s", format_hex(BPTR(&buf), BLEN(&buf), 0, &gc));
  6037. if (!DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT,
  6038. BPTR(&buf), BLEN(&buf),
  6039. BPTR(&buf), BLEN(&buf), &len, NULL))
  6040. {
  6041. msg(M_FATAL, "ERROR: The TAP-Windows driver rejected a TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
  6042. }
  6043. }
  6044. else
  6045. {
  6046. msg(M_WARN, "DHCP option string not set due to error");
  6047. }
  6048. free_buf(&buf);
  6049. }
  6050. #endif /* ifndef SIMULATE_DHCP_FAILED */
  6051. }
  6052.  
  6053. /* set driver media status to 'connected' */
  6054. {
  6055. ULONG status = TRUE;
  6056. if (!DeviceIoControl(tt->hand, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
  6057. &status, sizeof(status),
  6058. &status, sizeof(status), &len, NULL))
  6059. {
  6060. msg(M_WARN, "WARNING: The TAP-Windows driver rejected a TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
  6061. }
  6062. }
  6063.  
  6064. /* possible wait for adapter to come up */
  6065. {
  6066. int s = tt->options.tap_sleep;
  6067. if (s > 0)
  6068. {
  6069. msg(M_INFO, "Sleeping for %d seconds...", s);
  6070. management_sleep(s);
  6071. }
  6072. }
  6073.  
  6074. /* possibly use IP Helper API to set IP address on adapter */
  6075. {
  6076. const DWORD index = tt->adapter_index;
  6077.  
  6078. /* flush arp cache */
  6079. if (index != TUN_ADAPTER_INDEX_INVALID)
  6080. {
  6081. DWORD status = -1;
  6082.  
  6083. if (tt->options.msg_channel)
  6084. {
  6085. ack_message_t ack;
  6086. flush_neighbors_message_t msg = {
  6087. .header = {
  6088. msg_flush_neighbors,
  6089. sizeof(flush_neighbors_message_t),
  6090. 0
  6091. },
  6092. .family = AF_INET,
  6093. .iface = { .index = index, .name = "" }
  6094. };
  6095.  
  6096. if (!WriteFile(tt->options.msg_channel, &msg, sizeof(msg), &len, NULL)
  6097. || !ReadFile(tt->options.msg_channel, &ack, sizeof(ack), &len, NULL))
  6098. {
  6099. msg(M_WARN, "TUN: could not talk to service: %s [%lu]",
  6100. strerror_win32(GetLastError(), &gc), GetLastError());
  6101. }
  6102.  
  6103. status = ack.error_number;
  6104. }
  6105. else
  6106. {
  6107. status = FlushIpNetTable(index);
  6108. }
  6109.  
  6110. if (status == NO_ERROR)
  6111. {
  6112. msg(M_INFO, "Successful ARP Flush on interface [%u] %s",
  6113. (unsigned int)index,
  6114. device_guid);
  6115. }
  6116. else if (status != -1)
  6117. {
  6118. msg(D_TUNTAP_INFO, "NOTE: FlushIpNetTable failed on interface [%u] %s (status=%u) : %s",
  6119. (unsigned int)index,
  6120. device_guid,
  6121. (unsigned int)status,
  6122. strerror_win32(status, &gc));
  6123. }
  6124. }
  6125.  
  6126. /*
  6127. * If the TAP-Windows driver is masquerading as a DHCP server
  6128. * make sure the TCP/IP properties for the adapter are
  6129. * set correctly.
  6130. */
  6131. if (dhcp_masq_post)
  6132. {
  6133. /* check dhcp enable status */
  6134. if (dhcp_status(index) == DHCP_STATUS_DISABLED)
  6135. {
  6136. msg(M_WARN, "WARNING: You have selected '--ip-win32 dynamic', which will not work unless the TAP-Windows TCP/IP properties are set to 'Obtain an IP address automatically'");
  6137. }
  6138.  
  6139. /* force an explicit DHCP lease renewal on TAP adapter? */
  6140. if (tt->options.dhcp_pre_release)
  6141. {
  6142. dhcp_release(tt);
  6143. }
  6144. if (tt->options.dhcp_renew)
  6145. {
  6146. dhcp_renew(tt);
  6147. }
  6148. }
  6149. else
  6150. {
  6151. fork_dhcp_action(tt);
  6152. }
  6153.  
  6154. if (tt->did_ifconfig_setup && tt->options.ip_win32_type == IPW32_SET_IPAPI)
  6155. {
  6156. DWORD status;
  6157. const char *error_suffix = "I am having trouble using the Windows 'IP helper API' to automatically set the IP address -- consider using other --ip-win32 methods (not 'ipapi')";
  6158.  
  6159. /* couldn't get adapter index */
  6160. if (index == TUN_ADAPTER_INDEX_INVALID)
  6161. {
  6162. msg(M_FATAL, "ERROR: unable to get adapter index for interface %s -- %s",
  6163. device_guid,
  6164. error_suffix);
  6165. }
  6166.  
  6167. /* check dhcp enable status */
  6168. if (dhcp_status(index) == DHCP_STATUS_DISABLED)
  6169. {
  6170. msg(M_WARN, "NOTE: You have selected (explicitly or by default) '--ip-win32 ipapi', which has a better chance of working correctly if the TAP-Windows TCP/IP properties are set to 'Obtain an IP address automatically'");
  6171. }
  6172.  
  6173. /* delete previously added IP addresses which were not
  6174. * correctly deleted */
  6175. delete_temp_addresses(index);
  6176.  
  6177. /* add a new IP address */
  6178. if ((status = AddIPAddress(htonl(tt->local),
  6179. htonl(tt->adapter_netmask),
  6180. index,
  6181. &tt->ipapi_context,
  6182. &tt->ipapi_instance)) == NO_ERROR)
  6183. {
  6184. msg(M_INFO, "Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
  6185. print_in_addr_t(tt->local, 0, &gc),
  6186. print_in_addr_t(tt->adapter_netmask, 0, &gc),
  6187. device_guid
  6188. );
  6189. }
  6190. else
  6191. {
  6192. msg(M_FATAL, "ERROR: AddIPAddress %s/%s failed on interface %s, index=%d, status=%u (windows error: '%s') -- %s",
  6193. print_in_addr_t(tt->local, 0, &gc),
  6194. print_in_addr_t(tt->adapter_netmask, 0, &gc),
  6195. device_guid,
  6196. (int)index,
  6197. (unsigned int)status,
  6198. strerror_win32(status, &gc),
  6199. error_suffix);
  6200. }
  6201. tt->ipapi_context_defined = true;
  6202. }
  6203. }
  6204. /*netcmd_semaphore_release ();*/
  6205. gc_free(&gc);
  6206. }
  6207.  
  6208. const char *
  6209. tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
  6210. {
  6211. if (tt && tt->hand != NULL)
  6212. {
  6213. struct buffer out = alloc_buf_gc(256, gc);
  6214. DWORD len;
  6215. if (DeviceIoControl(tt->hand, TAP_WIN_IOCTL_GET_INFO,
  6216. BSTR(&out), BCAP(&out),
  6217. BSTR(&out), BCAP(&out),
  6218. &len, NULL))
  6219. {
  6220. return BSTR(&out);
  6221. }
  6222. }
  6223. return NULL;
  6224. }
  6225.  
  6226. void
  6227. tun_show_debug(struct tuntap *tt)
  6228. {
  6229. if (tt && tt->hand != NULL)
  6230. {
  6231. struct buffer out = alloc_buf(1024);
  6232. DWORD len;
  6233. while (DeviceIoControl(tt->hand, TAP_WIN_IOCTL_GET_LOG_LINE,
  6234. BSTR(&out), BCAP(&out),
  6235. BSTR(&out), BCAP(&out),
  6236. &len, NULL))
  6237. {
  6238. msg(D_TAP_WIN_DEBUG, "TAP-Windows: %s", BSTR(&out));
  6239. }
  6240. free_buf(&out);
  6241. }
  6242. }
  6243.  
  6244. void
  6245. close_tun(struct tuntap *tt)
  6246. {
  6247. struct gc_arena gc = gc_new();
  6248.  
  6249. if (tt)
  6250. {
  6251. if (tt->did_ifconfig_ipv6_setup)
  6252. {
  6253. /* remove route pointing to interface */
  6254. delete_route_connected_v6_net(tt, NULL);
  6255.  
  6256. if (tt->options.msg_channel)
  6257. {
  6258. do_address_service(false, AF_INET6, tt);
  6259. if (tt->options.dns6_len > 0)
  6260. {
  6261. do_dns6_service(false, tt);
  6262. }
  6263. }
  6264. else
  6265. {
  6266. const char *ifconfig_ipv6_local;
  6267. struct argv argv = argv_new();
  6268.  
  6269. /* "store=active" is needed in Windows 8(.1) to delete the
  6270. * address we added (pointed out by Cedric Tabary).
  6271. */
  6272.  
  6273. /* netsh interface ipv6 delete address \"%s\" %s */
  6274. ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
  6275. argv_printf(&argv,
  6276. "%s%sc interface ipv6 delete address %s %s store=active",
  6277. get_win_sys_path(),
  6278. NETSH_PATH_SUFFIX,
  6279. tt->actual_name,
  6280. ifconfig_ipv6_local);
  6281.  
  6282. netsh_command(&argv, 1, M_WARN);
  6283.  
  6284. /* delete ipv6 dns servers if any were set */
  6285. if (tt->options.dns6_len > 0)
  6286. {
  6287. argv_printf(&argv,
  6288. "%s%sc interface ipv6 delete dns %s all",
  6289. get_win_sys_path(),
  6290. NETSH_PATH_SUFFIX,
  6291. tt->actual_name);
  6292. netsh_command(&argv, 1, M_WARN);
  6293. }
  6294. argv_reset(&argv);
  6295. }
  6296. }
  6297. #if 1
  6298. if (tt->ipapi_context_defined)
  6299. {
  6300. DWORD status;
  6301. if ((status = DeleteIPAddress(tt->ipapi_context)) != NO_ERROR)
  6302. {
  6303. msg(M_WARN, "Warning: DeleteIPAddress[%u] failed on TAP-Windows adapter, status=%u : %s",
  6304. (unsigned int)tt->ipapi_context,
  6305. (unsigned int)status,
  6306. strerror_win32(status, &gc));
  6307. }
  6308. }
  6309. #endif
  6310.  
  6311. dhcp_release(tt);
  6312.  
  6313. if (tt->hand != NULL)
  6314. {
  6315. dmsg(D_WIN32_IO_LOW, "Attempting CancelIO on TAP-Windows adapter");
  6316. if (!CancelIo(tt->hand))
  6317. {
  6318. msg(M_WARN | M_ERRNO, "Warning: CancelIO failed on TAP-Windows adapter");
  6319. }
  6320. }
  6321.  
  6322. dmsg(D_WIN32_IO_LOW, "Attempting close of overlapped read event on TAP-Windows adapter");
  6323. overlapped_io_close(&tt->reads);
  6324.  
  6325. dmsg(D_WIN32_IO_LOW, "Attempting close of overlapped write event on TAP-Windows adapter");
  6326. overlapped_io_close(&tt->writes);
  6327.  
  6328. if (tt->hand != NULL)
  6329. {
  6330. dmsg(D_WIN32_IO_LOW, "Attempting CloseHandle on TAP-Windows adapter");
  6331. if (!CloseHandle(tt->hand))
  6332. {
  6333. msg(M_WARN | M_ERRNO, "Warning: CloseHandle failed on TAP-Windows adapter");
  6334. }
  6335. }
  6336.  
  6337. if (tt->actual_name)
  6338. {
  6339. free(tt->actual_name);
  6340. }
  6341.  
  6342. clear_tuntap(tt);
  6343. free(tt);
  6344. }
  6345. gc_free(&gc);
  6346. }
  6347.  
  6348. /*
  6349. * Convert --ip-win32 constants between index and ascii form.
  6350. */
  6351.  
  6352. struct ipset_names {
  6353. const char *short_form;
  6354. };
  6355.  
  6356. /* Indexed by IPW32_SET_x */
  6357. static const struct ipset_names ipset_names[] = {
  6358. {"manual"},
  6359. {"netsh"},
  6360. {"ipapi"},
  6361. {"dynamic"},
  6362. {"adaptive"}
  6363. };
  6364.  
  6365. int
  6366. ascii2ipset(const char *name)
  6367. {
  6368. int i;
  6369. ASSERT(IPW32_SET_N == SIZE(ipset_names));
  6370. for (i = 0; i < IPW32_SET_N; ++i)
  6371. {
  6372. if (!strcmp(name, ipset_names[i].short_form))
  6373. {
  6374. return i;
  6375. }
  6376. }
  6377. return -1;
  6378. }
  6379.  
  6380. const char *
  6381. ipset2ascii(int index)
  6382. {
  6383. ASSERT(IPW32_SET_N == SIZE(ipset_names));
  6384. if (index < 0 || index >= IPW32_SET_N)
  6385. {
  6386. return "[unknown --ip-win32 type]";
  6387. }
  6388. else
  6389. {
  6390. return ipset_names[index].short_form;
  6391. }
  6392. }
  6393.  
  6394. const char *
  6395. ipset2ascii_all(struct gc_arena *gc)
  6396. {
  6397. struct buffer out = alloc_buf_gc(256, gc);
  6398. int i;
  6399.  
  6400. ASSERT(IPW32_SET_N == SIZE(ipset_names));
  6401. for (i = 0; i < IPW32_SET_N; ++i)
  6402. {
  6403. if (i)
  6404. {
  6405. buf_printf(&out, " ");
  6406. }
  6407. buf_printf(&out, "[%s]", ipset2ascii(i));
  6408. }
  6409. return BSTR(&out);
  6410. }
  6411.  
  6412. #else /* generic */
  6413.  
  6414. void
  6415. open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
  6416. {
  6417. open_tun_generic(dev, dev_type, dev_node, true, tt);
  6418. }
  6419.  
  6420. void
  6421. close_tun(struct tuntap *tt)
  6422. {
  6423. if (tt)
  6424. {
  6425. close_tun_generic(tt);
  6426. free(tt);
  6427. }
  6428. }
  6429.  
  6430. int
  6431. write_tun(struct tuntap *tt, uint8_t *buf, int len)
  6432. {
  6433. return write(tt->fd, buf, len);
  6434. }
  6435.  
  6436. int
  6437. read_tun(struct tuntap *tt, uint8_t *buf, int len)
  6438. {
  6439. return read(tt->fd, buf, len);
  6440. }
  6441.  
  6442. #endif /* if defined (TARGET_ANDROID) */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement