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