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
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Support routines for configuring and accessing TUN/TAP
27 * virtual network adapters.
29 * This file is based on the TUN/TAP driver interface routines
30 * from VTun by Maxim Krasnyansky <max_mk@yahoo.com>.
34 #include "config-win32.h"
51 static void solaris_error_close (struct tuntap
*tt
, const struct env_set
*es
, const char *actual
);
55 is_dev_type (const char *dev
, const char *dev_type
, const char *match_type
)
61 return !strcmp (dev_type
, match_type
);
63 return !strncmp (dev
, match_type
, strlen (match_type
));
67 dev_type_enum (const char *dev
, const char *dev_type
)
69 if (is_dev_type (dev
, dev_type
, "tun"))
71 else if (is_dev_type (dev
, dev_type
, "tap"))
73 else if (is_dev_type (dev
, dev_type
, "null"))
76 return DEV_TYPE_UNDEF
;
80 dev_type_string (const char *dev
, const char *dev_type
)
82 switch (dev_type_enum (dev
, dev_type
))
91 return "[unknown-dev-type]";
96 dev_component_in_dev_node (const char *dev_node
)
99 const int dirsep
= OS_SPECIFIC_DIRSEP
;
103 ret
= strrchr (dev_node
, dirsep
);
115 * Try to predict the actual TUN/TAP device instance name,
116 * before the device is actually opened.
119 guess_tuntap_dev (const char *dev
,
120 const char *dev_type
,
121 const char *dev_node
,
125 const int dt
= dev_type_enum (dev
, dev_type
);
126 if (dt
== DEV_TYPE_TUN
|| dt
== DEV_TYPE_TAP
)
128 return get_netsh_id (dev_node
, gc
);
137 * Called by the open_tun function of OSes to check if we
138 * explicitly support IPv6.
140 * In this context, explicit means that the OS expects us to
141 * do something special to the tun socket in order to support
142 * IPv6, i.e. it is not transparent.
144 * ipv6_explicitly_supported should be set to false if we don't
145 * have any explicit IPv6 code in the tun device handler.
147 * If ipv6_explicitly_supported is true, then we have explicit
148 * OS-specific tun dev code for handling IPv6. If so, tt->ipv6
149 * is set according to the --tun-ipv6 command line option.
152 ipv6_support (bool ipv6
, bool ipv6_explicitly_supported
, struct tuntap
* tt
)
155 if (ipv6_explicitly_supported
)
158 msg (M_WARN
, "NOTE: explicit support for IPv6 tun devices is not provided for this OS");
161 /* --ifconfig-nowarn disables some options sanity checking */
162 static const char ifconfig_warn_how_to_silence
[] = "(silence this warning with --ifconfig-nowarn)";
165 * If !tun, make sure ifconfig_remote_netmask looks
168 * If tun, make sure ifconfig_remote_netmask looks
169 * like an IPv4 address.
172 ifconfig_sanity_check (bool tun
, in_addr_t addr
)
174 struct gc_arena gc
= gc_new ();
175 const bool looks_like_netmask
= ((addr
& 0xFF000000) == 0xFF000000);
178 if (looks_like_netmask
)
179 msg (M_WARN
, "WARNING: Since you are using --dev tun, the second argument to --ifconfig must be an IP address. You are using something (%s) that looks more like a netmask. %s",
180 print_in_addr_t (addr
, 0, &gc
),
181 ifconfig_warn_how_to_silence
);
185 if (!looks_like_netmask
)
186 msg (M_WARN
, "WARNING: Since you are using --dev tap, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
187 ifconfig_warn_how_to_silence
);
193 * For TAP-style devices, generate a broadcast address.
196 generate_ifconfig_broadcast_addr (in_addr_t local
,
199 return local
| ~netmask
;
203 * Check that --local and --remote addresses do not
204 * clash with ifconfig addresses or subnet.
207 check_addr_clash (const char *name
,
211 in_addr_t remote_netmask
)
213 struct gc_arena gc
= gc_new ();
215 msg (M_INFO
, "CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
217 print_in_addr_t (public, 0, &gc
),
218 print_in_addr_t (local
, 0, &gc
),
219 print_in_addr_t (remote_netmask
, 0, &gc
));
224 if (type
== DEV_TYPE_TUN
)
226 const in_addr_t test_netmask
= 0xFFFFFF00;
227 const in_addr_t public_net
= public & test_netmask
;
228 const in_addr_t local_net
= local
& test_netmask
;
229 const in_addr_t remote_net
= remote_netmask
& test_netmask
;
231 if (public == local
|| public == remote_netmask
)
233 "WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
235 print_in_addr_t (public, 0, &gc
),
236 print_in_addr_t (local
, 0, &gc
),
237 print_in_addr_t (remote_netmask
, 0, &gc
),
238 ifconfig_warn_how_to_silence
);
240 if (public_net
== local_net
|| public_net
== remote_net
)
242 "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",
244 print_in_addr_t (public, 0, &gc
),
245 print_in_addr_t (local
, 0, &gc
),
246 print_in_addr_t (remote_netmask
, 0, &gc
),
247 ifconfig_warn_how_to_silence
);
249 else if (type
== DEV_TYPE_TAP
)
251 const in_addr_t public_network
= public & remote_netmask
;
252 const in_addr_t virtual_network
= local
& remote_netmask
;
253 if (public_network
== virtual_network
)
255 "WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
257 print_in_addr_t (public, 0, &gc
),
258 print_in_addr_t (local
, 0, &gc
),
259 print_in_addr_t (remote_netmask
, 0, &gc
),
260 ifconfig_warn_how_to_silence
);
267 * Complain if --dev tap and --ifconfig is used on an OS for which
268 * we don't have a custom tap ifconfig template below.
273 msg (M_FATAL
, "Sorry but you cannot use --dev tap and --ifconfig together on this OS because I have not yet been programmed to understand the appropriate ifconfig syntax to use for TAP-style devices on this OS. Your best alternative is to use an --up script and do the ifconfig command manually.");
277 * Return a string to be used for options compatibility check
281 ifconfig_options_string (const struct tuntap
* tt
, bool remote
, bool disable
, struct gc_arena
*gc
)
283 struct buffer out
= alloc_buf_gc (256, gc
);
284 if (tt
->did_ifconfig_setup
&& !disable
)
286 if (tt
->type
== DEV_TYPE_TUN
)
291 r
= print_in_addr_t (tt
->local
, 0, gc
);
292 l
= print_in_addr_t (tt
->remote_netmask
, 0, gc
);
296 l
= print_in_addr_t (tt
->local
, 0, gc
);
297 r
= print_in_addr_t (tt
->remote_netmask
, 0, gc
);
299 buf_printf (&out
, "%s %s", r
, l
);
301 else if (tt
->type
== DEV_TYPE_TAP
)
303 buf_printf (&out
, "%s %s",
304 print_in_addr_t (tt
->local
& tt
->remote_netmask
, 0, gc
),
305 print_in_addr_t (tt
->remote_netmask
, 0, gc
));
308 buf_printf (&out
, "[undef]");
314 * Return a status string describing wait state.
317 tun_stat (const struct tuntap
*tt
, unsigned int rwflags
, struct gc_arena
*gc
)
319 struct buffer out
= alloc_buf_gc (64, gc
);
322 if (rwflags
& EVENT_READ
)
324 buf_printf (&out
, "T%s",
325 (tt
->rwflags_debug
& EVENT_READ
) ? "R" : "r");
327 buf_printf (&out
, "%s",
328 overlapped_io_state_ascii (&tt
->reads
));
331 if (rwflags
& EVENT_WRITE
)
333 buf_printf (&out
, "T%s",
334 (tt
->rwflags_debug
& EVENT_WRITE
) ? "W" : "w");
336 buf_printf (&out
, "%s",
337 overlapped_io_state_ascii (&tt
->writes
));
343 buf_printf (&out
, "T?");
349 * Init tun/tap object.
351 * Set up tuntap structure for ifconfig,
352 * but don't execute yet.
355 init_tun (const char *dev
, /* --dev option */
356 const char *dev_type
, /* --dev-type option */
357 const char *ifconfig_local_parm
, /* --ifconfig parm 1 */
358 const char *ifconfig_remote_netmask_parm
, /* --ifconfig parm 2 */
359 in_addr_t local_public
,
360 in_addr_t remote_public
,
361 const bool strict_warn
,
364 struct gc_arena gc
= gc_new ();
367 ALLOC_OBJ (tt
, struct tuntap
);
370 tt
->type
= dev_type_enum (dev
, dev_type
);
372 if (ifconfig_local_parm
&& ifconfig_remote_netmask_parm
)
375 const char *ifconfig_local
= NULL
;
376 const char *ifconfig_remote_netmask
= NULL
;
377 const char *ifconfig_broadcast
= NULL
;
380 * We only handle TUN/TAP devices here, not --dev null devices.
382 if (tt
->type
== DEV_TYPE_TUN
)
384 else if (tt
->type
== DEV_TYPE_TAP
)
387 msg (M_FATAL
, "'%s' is not a TUN/TAP device. The --ifconfig option works only for TUN/TAP devices.", dev
);
390 * Convert arguments to binary IPv4 addresses.
393 tt
->local
= getaddr (
396 | GETADDR_FATAL_ON_SIGNAL
403 tt
->remote_netmask
= getaddr (
404 (tun
? GETADDR_RESOLVE
: 0)
406 | GETADDR_FATAL_ON_SIGNAL
408 ifconfig_remote_netmask_parm
,
414 * Look for common errors in --ifconfig parms
418 ifconfig_sanity_check (tun
, tt
->remote_netmask
);
421 * If local_public or remote_public addresses are defined,
422 * make sure they do not clash with our virtual subnet.
425 check_addr_clash ("local",
431 check_addr_clash ("remote",
439 * Set ifconfig parameters
441 ifconfig_local
= print_in_addr_t (tt
->local
, 0, &gc
);
442 ifconfig_remote_netmask
= print_in_addr_t (tt
->remote_netmask
, 0, &gc
);
445 * If TAP-style interface, generate broadcast address.
449 tt
->broadcast
= generate_ifconfig_broadcast_addr (tt
->local
, tt
->remote_netmask
);
450 ifconfig_broadcast
= print_in_addr_t (tt
->broadcast
, 0, &gc
);
454 * Set environmental variables with ifconfig parameters.
458 setenv_str (es
, "ifconfig_local", ifconfig_local
);
461 setenv_str (es
, "ifconfig_remote", ifconfig_remote_netmask
);
465 setenv_str (es
, "ifconfig_netmask", ifconfig_remote_netmask
);
466 setenv_str (es
, "ifconfig_broadcast", ifconfig_broadcast
);
470 tt
->did_ifconfig_setup
= true;
477 * Platform specific tun initializations
480 init_tun_post (struct tuntap
*tt
,
481 const struct frame
*frame
,
482 const struct tuntap_options
*options
)
484 tt
->options
= *options
;
486 overlapped_io_init (&tt
->reads
, frame
, FALSE
, true);
487 overlapped_io_init (&tt
->writes
, frame
, TRUE
, true);
488 tt
->rw_handle
.read
= tt
->reads
.overlapped
.hEvent
;
489 tt
->rw_handle
.write
= tt
->writes
.overlapped
.hEvent
;
490 tt
->adapter_index
= ~0;
494 /* execute the ifconfig command through the shell */
496 do_ifconfig (struct tuntap
*tt
,
497 const char *actual
, /* actual device name */
499 const struct env_set
*es
)
501 struct gc_arena gc
= gc_new ();
503 if (tt
->did_ifconfig_setup
)
506 const char *ifconfig_local
= NULL
;
507 const char *ifconfig_remote_netmask
= NULL
;
508 const char *ifconfig_broadcast
= NULL
;
509 char command_line
[256];
512 * We only handle TUN/TAP devices here, not --dev null devices.
514 if (tt
->type
== DEV_TYPE_TUN
)
516 else if (tt
->type
== DEV_TYPE_TAP
)
519 ASSERT (0); /* should have been caught in init_tun */
522 * Set ifconfig parameters
524 ifconfig_local
= print_in_addr_t (tt
->local
, 0, &gc
);
525 ifconfig_remote_netmask
= print_in_addr_t (tt
->remote_netmask
, 0, &gc
);
528 * If TAP-style device, generate broadcast address.
531 ifconfig_broadcast
= print_in_addr_t (tt
->broadcast
, 0, &gc
);
533 #ifdef ENABLE_MANAGEMENT
536 management_set_state (management
,
537 OPENVPN_STATE_ASSIGN_IP
,
544 #if defined(TARGET_LINUX)
545 #ifdef CONFIG_FEATURE_IPROUTE
547 * Set the MTU for the device
549 openvpn_snprintf (command_line
, sizeof (command_line
),
550 IPROUTE_PATH
" link set dev %s up mtu %d",
554 msg (M_INFO
, "%s", command_line
);
555 system_check (command_line
, es
, S_FATAL
, "Linux ip link set failed");
560 * Set the address for the device
562 openvpn_snprintf (command_line
, sizeof (command_line
),
563 IPROUTE_PATH
" addr add dev %s local %s peer %s",
566 ifconfig_remote_netmask
568 msg (M_INFO
, "%s", command_line
);
569 system_check (command_line
, es
, S_FATAL
, "Linux ip addr add failed");
571 openvpn_snprintf (command_line
, sizeof (command_line
),
572 IPROUTE_PATH
" addr add dev %s %s/%d broadcast %s",
575 count_netmask_bits(ifconfig_remote_netmask
),
578 msg (M_INFO
, "%s", command_line
);
579 system_check (command_line
, es
, S_FATAL
, "Linux ip addr add failed");
581 tt
->did_ifconfig
= true;
584 openvpn_snprintf (command_line
, sizeof (command_line
),
585 IFCONFIG_PATH
" %s %s pointopoint %s mtu %d",
588 ifconfig_remote_netmask
,
592 openvpn_snprintf (command_line
, sizeof (command_line
),
593 IFCONFIG_PATH
" %s %s netmask %s mtu %d broadcast %s",
596 ifconfig_remote_netmask
,
600 msg (M_INFO
, "%s", command_line
);
601 system_check (command_line
, es
, S_FATAL
, "Linux ifconfig failed");
602 tt
->did_ifconfig
= true;
604 #endif /*CONFIG_FEATURE_IPROUTE*/
605 #elif defined(TARGET_SOLARIS)
607 /* Solaris 2.6 (and 7?) cannot set all parameters in one go...
609 * ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 up
610 * ifconfig tun2 netmask 255.255.255.255
614 openvpn_snprintf (command_line
, sizeof (command_line
),
615 IFCONFIG_PATH
" %s %s %s mtu %d up",
618 ifconfig_remote_netmask
,
622 msg (M_INFO
, "%s", command_line
);
623 if (!system_check (command_line
, es
, 0, "Solaris ifconfig phase-1 failed"))
624 solaris_error_close (tt
, es
, actual
);
626 openvpn_snprintf (command_line
, sizeof (command_line
),
627 IFCONFIG_PATH
" %s netmask 255.255.255.255",
634 msg (M_INFO
, "%s", command_line
);
635 if (!system_check (command_line
, es
, 0, "Solaris ifconfig phase-2 failed"))
636 solaris_error_close (tt
, es
, actual
);
638 tt
->did_ifconfig
= true;
640 #elif defined(TARGET_OPENBSD)
643 * OpenBSD tun devices appear to be persistent by default. It seems in order
644 * to make this work correctly, we need to delete the previous instance
645 * (if it exists), and re-ifconfig. Let me know if you know a better way.
648 openvpn_snprintf (command_line
, sizeof (command_line
),
649 IFCONFIG_PATH
" %s destroy",
651 msg (M_INFO
, "%s", command_line
);
652 system_check (command_line
, es
, 0, NULL
);
653 openvpn_snprintf (command_line
, sizeof (command_line
),
654 IFCONFIG_PATH
" %s create",
656 msg (M_INFO
, "%s", command_line
);
657 system_check (command_line
, es
, 0, NULL
);
658 msg (M_INFO
, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
660 /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
662 openvpn_snprintf (command_line
, sizeof (command_line
),
663 IFCONFIG_PATH
" %s %s %s mtu %d netmask 255.255.255.255 up",
666 ifconfig_remote_netmask
,
670 openvpn_snprintf (command_line
, sizeof (command_line
),
671 IFCONFIG_PATH
" %s %s netmask %s mtu %d broadcast %s link0",
674 ifconfig_remote_netmask
,
678 msg (M_INFO
, "%s", command_line
);
679 system_check (command_line
, es
, S_FATAL
, "OpenBSD ifconfig failed");
680 tt
->did_ifconfig
= true;
682 #elif defined(TARGET_NETBSD)
685 openvpn_snprintf (command_line
, sizeof (command_line
),
686 IFCONFIG_PATH
" %s %s %s mtu %d netmask 255.255.255.255 up",
689 ifconfig_remote_netmask
,
694 * NetBSD has distinct tun and tap devices
695 * so we don't need the "link0" extra parameter to specify we want to do
696 * tunneling at the ethernet level
698 openvpn_snprintf (command_line
, sizeof (command_line
),
699 IFCONFIG_PATH
" %s %s netmask %s mtu %d broadcast %s",
702 ifconfig_remote_netmask
,
706 msg (M_INFO
, "%s", command_line
);
707 system_check (command_line
, es
, S_FATAL
, "NetBSD ifconfig failed");
708 tt
->did_ifconfig
= true;
710 #elif defined(TARGET_DARWIN)
713 * Darwin (i.e. Mac OS X) seems to exhibit similar behaviour to OpenBSD...
716 openvpn_snprintf (command_line
, sizeof (command_line
),
717 IFCONFIG_PATH
" %s delete",
719 msg (M_INFO
, "%s", command_line
);
720 system_check (command_line
, es
, 0, NULL
);
721 msg (M_INFO
, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
724 /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
726 openvpn_snprintf (command_line
, sizeof (command_line
),
727 IFCONFIG_PATH
" %s %s %s mtu %d netmask 255.255.255.255 up",
730 ifconfig_remote_netmask
,
734 openvpn_snprintf (command_line
, sizeof (command_line
),
735 IFCONFIG_PATH
" %s %s netmask %s mtu %d up",
738 ifconfig_remote_netmask
,
742 msg (M_INFO
, "%s", command_line
);
743 system_check (command_line
, es
, S_FATAL
, "Mac OS X ifconfig failed");
744 tt
->did_ifconfig
= true;
746 #elif defined(TARGET_FREEBSD)
748 /* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
750 openvpn_snprintf (command_line
, sizeof (command_line
),
751 IFCONFIG_PATH
" %s %s %s mtu %d netmask 255.255.255.255 up",
754 ifconfig_remote_netmask
,
758 openvpn_snprintf (command_line
, sizeof (command_line
),
759 IFCONFIG_PATH
" %s %s netmask %s mtu %d up",
762 ifconfig_remote_netmask
,
766 msg (M_INFO
, "%s", command_line
);
767 system_check (command_line
, es
, S_FATAL
, "FreeBSD ifconfig failed");
768 tt
->did_ifconfig
= true;
770 #elif defined (WIN32)
775 * Make sure that both ifconfig addresses are part of the
780 verify_255_255_255_252 (tt
->local
, tt
->remote_netmask
);
781 tt
->adapter_netmask
= ~3;
782 netmask
= print_in_addr_t (tt
->adapter_netmask
, 0, &gc
);
786 netmask
= ifconfig_remote_netmask
;
787 tt
->adapter_netmask
= tt
->remote_netmask
;
790 /* example: netsh interface ip set address my-tap static 10.3.0.1 255.255.255.0 */
791 openvpn_snprintf (command_line
, sizeof (command_line
),
792 "netsh interface ip set address \"%s\" static %s %s",
797 switch (tt
->options
.ip_win32_type
)
799 case IPW32_SET_MANUAL
:
800 msg (M_INFO
, "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
805 case IPW32_SET_NETSH
:
806 if (!strcmp (actual
, "NULL"))
807 msg (M_FATAL
, "Error: When using --ip-win32 netsh, if you have more than one TAP-Win32 adapter, you must also specify --dev-node");
808 netcmd_semaphore_lock ();
809 msg (M_INFO
, "%s", command_line
);
810 system_check (command_line
, es
, S_FATAL
, "ERROR: netsh command failed");
811 netcmd_semaphore_release ();
814 tt
->did_ifconfig
= true;
818 msg (M_FATAL
, "Sorry, but I don't know how to do 'ifconfig' commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");
825 clear_tuntap (struct tuntap
*tuntap
)
833 #ifdef TARGET_SOLARIS
836 tuntap
->ipv6
= false;
840 open_null (struct tuntap
*tt
)
842 tt
->actual_name
= string_alloc ("null", NULL
);
847 open_tun_generic (const char *dev
, const char *dev_type
, const char *dev_node
,
848 bool ipv6
, bool ipv6_explicitly_supported
, bool dynamic
,
852 char dynamic_name
[256];
853 bool dynamic_opened
= false;
855 ipv6_support (ipv6
, ipv6_explicitly_supported
, tt
);
857 if (tt
->type
== DEV_TYPE_NULL
)
864 * --dev-node specified, so open an explicit device node
868 openvpn_snprintf (tunname
, sizeof (tunname
), "%s", dev_node
);
873 * dynamic open is indicated by --dev specified without
874 * explicit unit number. Try opening /dev/[dev]n
875 * where n = [0, 255].
877 if (dynamic
&& !has_digit(dev
))
880 for (i
= 0; i
< 256; ++i
)
882 openvpn_snprintf (tunname
, sizeof (tunname
),
883 "/dev/%s%d", dev
, i
);
884 openvpn_snprintf (dynamic_name
, sizeof (dynamic_name
),
886 if ((tt
->fd
= open (tunname
, O_RDWR
)) > 0)
888 dynamic_opened
= true;
891 msg (D_READ_WRITE
| M_ERRNO
, "Tried opening %s (failed)", tunname
);
894 msg (M_FATAL
, "Cannot allocate TUN/TAP dev dynamically");
897 * explicit unit number specified
901 openvpn_snprintf (tunname
, sizeof (tunname
), "/dev/%s", dev
);
907 if ((tt
->fd
= open (tunname
, O_RDWR
)) < 0)
908 msg (M_ERR
, "Cannot open TUN/TAP dev %s", tunname
);
911 set_nonblock (tt
->fd
);
912 set_cloexec (tt
->fd
); /* don't pass fd to scripts */
913 msg (M_INFO
, "TUN/TAP device %s opened", tunname
);
915 /* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
916 tt
->actual_name
= string_alloc (dynamic_opened
? dynamic_name
: dev
, NULL
);
921 close_tun_generic (struct tuntap
*tt
)
926 free (tt
->actual_name
);
932 #if defined(TARGET_LINUX)
934 #ifdef HAVE_LINUX_IF_TUN_H /* New driver support */
936 #ifndef HAVE_LINUX_SOCKIOS_H
937 #error header file linux/sockios.h required
940 #if defined(HAVE_TUN_PI) && defined(HAVE_IPHDR) && defined(HAVE_IOVEC) && defined(ETH_P_IPV6) && defined(ETH_P_IP) && defined(HAVE_READV) && defined(HAVE_WRITEV)
942 /* #warning IPv6 ON */
945 /* #warning IPv6 OFF */
951 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
956 * Set tt->ipv6 to true if
957 * (a) we have the capability of supporting --tun-ipv6, and
958 * (b) --tun-ipv6 was specified.
960 ipv6_support (ipv6
, LINUX_IPV6
, tt
);
963 * We handle --dev null specially, we do not open /dev/null for this.
965 if (tt
->type
== DEV_TYPE_NULL
)
974 const char *node
= dev_node
;
976 node
= "/dev/net/tun";
981 if ((tt
->fd
= open (node
, O_RDWR
)) < 0)
983 msg (M_WARN
| M_ERRNO
, "Note: Cannot open TUN/TAP dev %s", node
);
984 goto linux_2_2_fallback
;
992 ifr
.ifr_flags
= IFF_NO_PI
;
994 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
995 ifr
.ifr_flags
|= IFF_ONE_QUEUE
;
999 * Figure out if tun or tap device
1001 if (tt
->type
== DEV_TYPE_TUN
)
1003 ifr
.ifr_flags
|= IFF_TUN
;
1005 else if (tt
->type
== DEV_TYPE_TAP
)
1007 ifr
.ifr_flags
|= IFF_TAP
;
1011 msg (M_FATAL
, "I don't recognize device %s as a tun or tap device",
1016 * Set an explicit name, if --dev is not tun or tap
1018 if (strcmp(dev
, "tun") && strcmp(dev
, "tap"))
1019 strncpynt (ifr
.ifr_name
, dev
, IFNAMSIZ
);
1022 * Use special ioctl that configures tun/tap device with the parms
1025 if (ioctl (tt
->fd
, TUNSETIFF
, (void *) &ifr
) < 0)
1027 msg (M_WARN
| M_ERRNO
, "Note: Cannot ioctl TUNSETIFF %s", dev
);
1028 goto linux_2_2_fallback
;
1031 msg (M_INFO
, "TUN/TAP device %s opened", ifr
.ifr_name
);
1034 * Try making the TX send queue bigger
1036 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
1038 struct ifreq netifr
;
1041 if ((ctl_fd
= socket (AF_INET
, SOCK_DGRAM
, 0)) >= 0)
1044 strncpynt (netifr
.ifr_name
, ifr
.ifr_name
, IFNAMSIZ
);
1045 netifr
.ifr_qlen
= tt
->options
.txqueuelen
;
1046 if (ioctl (ctl_fd
, SIOCSIFTXQLEN
, (void *) &netifr
) >= 0)
1047 msg (D_OSBUF
, "TUN/TAP TX queue length set to %d", tt
->options
.txqueuelen
);
1049 msg (M_WARN
| M_ERRNO
, "Note: Cannot set tx queue length on %s", ifr
.ifr_name
);
1054 msg (M_WARN
| M_ERRNO
, "Note: Cannot open control socket on %s", ifr
.ifr_name
);
1059 set_nonblock (tt
->fd
);
1060 set_cloexec (tt
->fd
);
1061 tt
->actual_name
= string_alloc (ifr
.ifr_name
, NULL
);
1066 msg (M_INFO
, "Note: Attempting fallback to kernel 2.2 TUN/TAP interface");
1072 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, false, true, tt
);
1078 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1088 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1090 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, false, true, tt
);
1093 #endif /* HAVE_LINUX_IF_TUN_H */
1095 #ifdef TUNSETPERSIST
1098 tuncfg (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, int persist_mode
)
1102 ALLOC_OBJ (tt
, struct tuntap
);
1104 tt
->type
= dev_type_enum (dev
, dev_type
);
1105 open_tun (dev
, dev_type
, dev_node
, ipv6
, tt
);
1106 if (ioctl (tt
->fd
, TUNSETPERSIST
, persist_mode
) < 0)
1107 msg (M_ERR
, "Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode
, dev
);
1109 msg (M_INFO
, "Persist state set to: %s", (persist_mode
? "ON" : "OFF"));
1112 #endif /* TUNSETPERSIST */
1115 close_tun (struct tuntap
*tt
)
1119 close_tun_generic (tt
);
1125 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1132 struct iovec vect
[2];
1135 iph
= (struct iphdr
*)buf
;
1139 if(iph
->version
== 6)
1140 pi
.proto
= htons(ETH_P_IPV6
);
1142 pi
.proto
= htons(ETH_P_IP
);
1144 vect
[0].iov_len
= sizeof(pi
);
1145 vect
[0].iov_base
= &pi
;
1146 vect
[1].iov_len
= len
;
1147 vect
[1].iov_base
= buf
;
1149 ret
= writev(tt
->fd
, vect
, 2);
1150 return(ret
- sizeof(pi
));
1154 return write (tt
->fd
, buf
, len
);
1158 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1163 struct iovec vect
[2];
1167 vect
[0].iov_len
= sizeof(pi
);
1168 vect
[0].iov_base
= &pi
;
1169 vect
[1].iov_len
= len
;
1170 vect
[1].iov_base
= buf
;
1172 ret
= readv(tt
->fd
, vect
, 2);
1173 return(ret
- sizeof(pi
));
1177 return read (tt
->fd
, buf
, len
);
1180 #elif defined(TARGET_SOLARIS)
1183 #error I need the symbol TUNNEWPPA from net/if_tun.h
1187 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1189 int if_fd
, muxid
, ppa
= -1;
1192 const char *ip_node
;
1193 const char *dev_tuntap_type
;
1197 ipv6_support (ipv6
, false, tt
);
1199 if (tt
->type
== DEV_TYPE_NULL
)
1205 if (tt
->type
== DEV_TYPE_TUN
)
1207 ip_node
= "/dev/udp";
1209 dev_node
= "/dev/tun";
1210 dev_tuntap_type
= "tun";
1211 link_type
= I_PLINK
;
1214 else if (tt
->type
== DEV_TYPE_TAP
)
1216 ip_node
= "/dev/ip";
1218 dev_node
= "/dev/tap";
1219 dev_tuntap_type
= "tap";
1220 link_type
= I_PLINK
; /* was: I_LINK */
1225 msg (M_FATAL
, "I don't recognize device %s as a tun or tap device",
1229 /* get unit number */
1233 while (*ptr
&& !isdigit ((int) *ptr
))
1238 if ((tt
->ip_fd
= open (ip_node
, O_RDWR
, 0)) < 0)
1239 msg (M_ERR
, "Can't open %s", ip_node
);
1241 if ((tt
->fd
= open (dev_node
, O_RDWR
, 0)) < 0)
1242 msg (M_ERR
, "Can't open %s", dev_node
);
1244 /* Assign a new PPA and get its unit number. */
1245 if ((ppa
= ioctl (tt
->fd
, TUNNEWPPA
, ppa
)) < 0)
1246 msg (M_ERR
, "Can't assign new interface");
1248 if ((if_fd
= open (dev_node
, O_RDWR
, 0)) < 0)
1249 msg (M_ERR
, "Can't open %s (2)", dev_node
);
1251 if (ioctl (if_fd
, I_PUSH
, "ip") < 0)
1252 msg (M_ERR
, "Can't push IP module");
1254 /* Assign ppa according to the unit number returned by tun device */
1255 if (ioctl (if_fd
, IF_UNITSEL
, (char *) &ppa
) < 0)
1256 msg (M_ERR
, "Can't set PPA %d", ppa
);
1258 if ((muxid
= ioctl (tt
->ip_fd
, link_type
, if_fd
)) < 0)
1259 msg (M_ERR
, "Can't link %s device to IP", dev_tuntap_type
);
1263 tt
->actual_name
= (char *) malloc (32);
1264 check_malloc_return (tt
->actual_name
);
1266 openvpn_snprintf (tt
->actual_name
, 32, "%s%d", dev_tuntap_type
, ppa
);
1269 strncpynt (ifr
.ifr_name
, tt
->actual_name
, sizeof (ifr
.ifr_name
));
1270 ifr
.ifr_ip_muxid
= muxid
;
1272 if (ioctl (tt
->ip_fd
, SIOCSIFMUXID
, &ifr
) < 0)
1274 ioctl (tt
->ip_fd
, I_PUNLINK
, muxid
);
1275 msg (M_ERR
, "Can't set multiplexor id");
1278 set_nonblock (tt
->fd
);
1279 set_cloexec (tt
->fd
);
1280 set_cloexec (tt
->ip_fd
);
1282 msg (M_INFO
, "TUN/TAP device %s opened", tt
->actual_name
);
1286 solaris_close_tun (struct tuntap
*tt
)
1294 strncpynt (ifr
.ifr_name
, tt
->actual_name
, sizeof (ifr
.ifr_name
));
1296 if (ioctl (tt
->ip_fd
, SIOCGIFFLAGS
, &ifr
) < 0)
1297 msg (M_WARN
| M_ERRNO
, "Can't get iface flags");
1299 if (ioctl (tt
->ip_fd
, SIOCGIFMUXID
, &ifr
) < 0)
1300 msg (M_WARN
| M_ERRNO
, "Can't get multiplexor id");
1302 if (ioctl (tt
->ip_fd
, I_PUNLINK
, ifr
.ifr_ip_muxid
) < 0)
1303 msg (M_WARN
| M_ERRNO
, "Can't unlink interface");
1321 close_tun (struct tuntap
*tt
)
1325 solaris_close_tun (tt
);
1327 if (tt
->actual_name
)
1328 free (tt
->actual_name
);
1336 solaris_error_close (struct tuntap
*tt
, const struct env_set
*es
, const char *actual
)
1338 char command_line
[256];
1340 openvpn_snprintf (command_line
, sizeof (command_line
),
1341 IFCONFIG_PATH
" %s unplumb",
1344 msg (M_INFO
, "%s", command_line
);
1345 system_check (command_line
, es
, 0, "Solaris ifconfig unplumb failed");
1347 msg (M_FATAL
, "Solaris ifconfig failed");
1351 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1355 sbuf
.buf
= (char *)buf
;
1356 return putmsg (tt
->fd
, NULL
, &sbuf
, 0) >= 0 ? sbuf
.len
: -1;
1360 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1366 sbuf
.buf
= (char *)buf
;
1367 return getmsg (tt
->fd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1370 #elif defined(TARGET_OPENBSD)
1372 #if !defined(HAVE_READV) || !defined(HAVE_WRITEV)
1373 #error openbsd build requires readv & writev library functions
1377 * OpenBSD has a slightly incompatible TUN device from
1378 * the rest of the world, in that it prepends a
1379 * uint32 to the beginning of the IP header
1380 * to designate the protocol (why not just
1381 * look at the version field in the IP header to
1382 * determine v4 or v6?).
1384 * We strip off this field on reads and
1385 * put it back on writes.
1387 * I have not tested TAP devices on OpenBSD,
1388 * but I have conditionalized the special
1389 * TUN handling code described above to
1390 * go away for TAP devices.
1394 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1396 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, true, true, tt
);
1398 /* Enable multicast on the interface */
1401 struct tuninfo info
;
1403 if (ioctl (tt
->fd
, TUNGIFINFO
, &info
) < 0) {
1404 msg (M_WARN
| M_ERRNO
, "Can't get interface info: %s",
1408 info
.flags
|= IFF_MULTICAST
;
1410 if (ioctl (tt
->fd
, TUNSIFINFO
, &info
) < 0) {
1411 msg (M_WARN
| M_ERRNO
, "Can't set interface info: %s",
1418 close_tun (struct tuntap
* tt
)
1422 close_tun_generic (tt
);
1428 openbsd_modify_read_write_return (int len
)
1431 return len
> sizeof (u_int32_t
) ? len
- sizeof (u_int32_t
) : 0;
1437 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1439 if (tt
->type
== DEV_TYPE_TUN
)
1445 iph
= (struct ip
*) buf
;
1447 if (tt
->ipv6
&& iph
->ip_v
== 6)
1448 type
= htonl (AF_INET6
);
1450 type
= htonl (AF_INET
);
1452 iv
[0].iov_base
= &type
;
1453 iv
[0].iov_len
= sizeof (type
);
1454 iv
[1].iov_base
= buf
;
1455 iv
[1].iov_len
= len
;
1457 return openbsd_modify_read_write_return (writev (tt
->fd
, iv
, 2));
1460 return write (tt
->fd
, buf
, len
);
1464 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1466 if (tt
->type
== DEV_TYPE_TUN
)
1471 iv
[0].iov_base
= &type
;
1472 iv
[0].iov_len
= sizeof (type
);
1473 iv
[1].iov_base
= buf
;
1474 iv
[1].iov_len
= len
;
1476 return openbsd_modify_read_write_return (readv (tt
->fd
, iv
, 2));
1479 return read (tt
->fd
, buf
, len
);
1482 #elif defined(TARGET_NETBSD)
1485 * NetBSD does not support IPv6 on tun out of the box,
1486 * but there exists a patch. When this patch is applied,
1487 * only two things are left to openvpn:
1488 * 1. Activate multicasting (this has already been done
1489 * before by the kernel, but we make sure that nobody
1490 * has deactivated multicasting inbetween.
1491 * 2. Deactivate "link layer mode" (otherwise NetBSD
1492 * prepends the address family to the packet, and we
1493 * would run into the same trouble as with OpenBSD.
1497 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1499 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, true, true, tt
);
1502 int i
= IFF_POINTOPOINT
|IFF_MULTICAST
;
1503 ioctl (tt
->fd
, TUNSIFMODE
, &i
); /* multicast on */
1505 ioctl (tt
->fd
, TUNSLMODE
, &i
); /* link layer mode off */
1510 close_tun (struct tuntap
*tt
)
1514 close_tun_generic (tt
);
1520 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1522 return write (tt
->fd
, buf
, len
);
1526 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1528 return read (tt
->fd
, buf
, len
);
1531 #elif defined(TARGET_FREEBSD)
1534 freebsd_modify_read_write_return (int len
)
1537 return len
> sizeof (u_int32_t
) ? len
- sizeof (u_int32_t
) : 0;
1543 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
1545 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, true, true, tt
);
1551 /* Disable extended modes */
1552 ioctl (tt
->fd
, TUNSLMODE
, &i
);
1554 ioctl (tt
->fd
, TUNSIFHEAD
, &i
);
1559 close_tun (struct tuntap
*tt
)
1563 close_tun_generic (tt
);
1569 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1571 if (tt
->type
== DEV_TYPE_TUN
)
1577 iph
= (struct ip
*) buf
;
1579 if (tt
->ipv6
&& iph
->ip_v
== 6)
1580 type
= htonl (AF_INET6
);
1582 type
= htonl (AF_INET
);
1584 iv
[0].iov_base
= (char *)&type
;
1585 iv
[0].iov_len
= sizeof (type
);
1586 iv
[1].iov_base
= buf
;
1587 iv
[1].iov_len
= len
;
1589 return freebsd_modify_read_write_return (writev (tt
->fd
, iv
, 2));
1592 return write (tt
->fd
, buf
, len
);
1596 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
1598 if (tt
->type
== DEV_TYPE_TUN
)
1603 iv
[0].iov_base
= (char *)&type
;
1604 iv
[0].iov_len
= sizeof (type
);
1605 iv
[1].iov_base
= buf
;
1606 iv
[1].iov_len
= len
;
1608 return freebsd_modify_read_write_return (readv (tt
->fd
, iv
, 2));
1611 return read (tt
->fd
, buf
, len
);
1614 #elif defined(WIN32)
1617 tun_read_queue (struct tuntap
*tt
, int maxsize
)
1619 if (tt
->reads
.iostate
== IOSTATE_INITIAL
)
1625 /* reset buf to its initial state */
1626 tt
->reads
.buf
= tt
->reads
.buf_init
;
1628 len
= maxsize
? maxsize
: BLEN (&tt
->reads
.buf
);
1629 ASSERT (len
<= BLEN (&tt
->reads
.buf
));
1631 /* the overlapped read will signal this event on I/O completion */
1632 ASSERT (ResetEvent (tt
->reads
.overlapped
.hEvent
));
1636 BPTR (&tt
->reads
.buf
),
1639 &tt
->reads
.overlapped
1642 if (status
) /* operation completed immediately? */
1644 /* since we got an immediate return, we must signal the event object ourselves */
1645 ASSERT (SetEvent (tt
->reads
.overlapped
.hEvent
));
1647 tt
->reads
.iostate
= IOSTATE_IMMEDIATE_RETURN
;
1648 tt
->reads
.status
= 0;
1650 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Read immediate return [%d,%d]",
1652 (int) tt
->reads
.size
);
1656 err
= GetLastError ();
1657 if (err
== ERROR_IO_PENDING
) /* operation queued? */
1659 tt
->reads
.iostate
= IOSTATE_QUEUED
;
1660 tt
->reads
.status
= err
;
1661 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Read queued [%d]",
1664 else /* error occurred */
1666 struct gc_arena gc
= gc_new ();
1667 ASSERT (SetEvent (tt
->reads
.overlapped
.hEvent
));
1668 tt
->reads
.iostate
= IOSTATE_IMMEDIATE_RETURN
;
1669 tt
->reads
.status
= err
;
1670 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Read error [%d] : %s",
1672 strerror_win32 (status
, &gc
));
1677 return tt
->reads
.iostate
;
1681 tun_write_queue (struct tuntap
*tt
, struct buffer
*buf
)
1683 if (tt
->writes
.iostate
== IOSTATE_INITIAL
)
1688 /* make a private copy of buf */
1689 tt
->writes
.buf
= tt
->writes
.buf_init
;
1690 tt
->writes
.buf
.len
= 0;
1691 ASSERT (buf_copy (&tt
->writes
.buf
, buf
));
1693 /* the overlapped write will signal this event on I/O completion */
1694 ASSERT (ResetEvent (tt
->writes
.overlapped
.hEvent
));
1698 BPTR (&tt
->writes
.buf
),
1699 BLEN (&tt
->writes
.buf
),
1701 &tt
->writes
.overlapped
1704 if (status
) /* operation completed immediately? */
1706 tt
->writes
.iostate
= IOSTATE_IMMEDIATE_RETURN
;
1708 /* since we got an immediate return, we must signal the event object ourselves */
1709 ASSERT (SetEvent (tt
->writes
.overlapped
.hEvent
));
1711 tt
->writes
.status
= 0;
1713 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Write immediate return [%d,%d]",
1714 BLEN (&tt
->writes
.buf
),
1715 (int) tt
->writes
.size
);
1719 err
= GetLastError ();
1720 if (err
== ERROR_IO_PENDING
) /* operation queued? */
1722 tt
->writes
.iostate
= IOSTATE_QUEUED
;
1723 tt
->writes
.status
= err
;
1724 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Write queued [%d]",
1725 BLEN (&tt
->writes
.buf
));
1727 else /* error occurred */
1729 struct gc_arena gc
= gc_new ();
1730 ASSERT (SetEvent (tt
->writes
.overlapped
.hEvent
));
1731 tt
->writes
.iostate
= IOSTATE_IMMEDIATE_RETURN
;
1732 tt
->writes
.status
= err
;
1733 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Write error [%d] : %s",
1734 BLEN (&tt
->writes
.buf
),
1735 strerror_win32 (err
, &gc
));
1740 return tt
->writes
.iostate
;
1746 struct overlapped_io
*io
,
1752 switch (io
->iostate
)
1754 case IOSTATE_QUEUED
:
1755 status
= GetOverlappedResult(
1763 /* successful return for a queued operation */
1767 io
->iostate
= IOSTATE_INITIAL
;
1768 ASSERT (ResetEvent (io
->overlapped
.hEvent
));
1769 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Completion success [%d]", ret
);
1773 /* error during a queued operation */
1775 if (GetLastError() != ERROR_IO_INCOMPLETE
)
1777 /* if no error (i.e. just not finished yet),
1778 then DON'T execute this code */
1779 io
->iostate
= IOSTATE_INITIAL
;
1780 ASSERT (ResetEvent (io
->overlapped
.hEvent
));
1781 msg (D_WIN32_IO
| M_ERRNO
, "WIN32 I/O: TAP Completion error");
1786 case IOSTATE_IMMEDIATE_RETURN
:
1787 io
->iostate
= IOSTATE_INITIAL
;
1788 ASSERT (ResetEvent (io
->overlapped
.hEvent
));
1791 /* error return for a non-queued operation */
1792 SetLastError (io
->status
);
1794 msg (D_WIN32_IO
| M_ERRNO
, "WIN32 I/O: TAP Completion non-queued error");
1798 /* successful return for a non-queued operation */
1802 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Completion non-queued success [%d]", ret
);
1806 case IOSTATE_INITIAL
: /* were we called without proper queueing? */
1807 SetLastError (ERROR_INVALID_FUNCTION
);
1809 dmsg (D_WIN32_IO
, "WIN32 I/O: TAP Completion BAD STATE");
1821 const struct tap_reg
*
1822 get_tap_reg (struct gc_arena
*gc
)
1827 struct tap_reg
*first
= NULL
;
1828 struct tap_reg
*last
= NULL
;
1831 status
= RegOpenKeyEx(
1838 if (status
!= ERROR_SUCCESS
)
1839 msg (M_FATAL
, "Error opening registry key: %s", ADAPTER_KEY
);
1843 char enum_name
[256];
1844 char unit_string
[256];
1846 char component_id_string
[] = "ComponentId";
1847 char component_id
[256];
1848 char net_cfg_instance_id_string
[] = "NetCfgInstanceId";
1849 char net_cfg_instance_id
[256];
1852 len
= sizeof (enum_name
);
1853 status
= RegEnumKeyEx(
1862 if (status
== ERROR_NO_MORE_ITEMS
)
1864 else if (status
!= ERROR_SUCCESS
)
1865 msg (M_FATAL
, "Error enumerating registry subkeys of key: %s",
1868 openvpn_snprintf (unit_string
, sizeof(unit_string
), "%s\\%s",
1869 ADAPTER_KEY
, enum_name
);
1871 status
= RegOpenKeyEx(
1878 if (status
!= ERROR_SUCCESS
)
1879 dmsg (D_REGISTRY
, "Error opening registry key: %s", unit_string
);
1882 len
= sizeof (component_id
);
1883 status
= RegQueryValueEx(
1885 component_id_string
,
1891 if (status
!= ERROR_SUCCESS
|| data_type
!= REG_SZ
)
1892 dmsg (D_REGISTRY
, "Error opening registry key: %s\\%s",
1893 unit_string
, component_id_string
);
1896 len
= sizeof (net_cfg_instance_id
);
1897 status
= RegQueryValueEx(
1899 net_cfg_instance_id_string
,
1902 net_cfg_instance_id
,
1905 if (status
== ERROR_SUCCESS
&& data_type
== REG_SZ
)
1907 if (!strcmp (component_id
, TAP_COMPONENT_ID
))
1909 struct tap_reg
*reg
;
1910 ALLOC_OBJ_CLEAR_GC (reg
, struct tap_reg
, gc
);
1911 reg
->guid
= string_alloc (net_cfg_instance_id
, gc
);
1913 /* link into return list */
1922 RegCloseKey (unit_key
);
1927 RegCloseKey (adapter_key
);
1931 const struct panel_reg
*
1932 get_panel_reg (struct gc_arena
*gc
)
1935 HKEY network_connections_key
;
1937 struct panel_reg
*first
= NULL
;
1938 struct panel_reg
*last
= NULL
;
1941 status
= RegOpenKeyEx(
1943 NETWORK_CONNECTIONS_KEY
,
1946 &network_connections_key
);
1948 if (status
!= ERROR_SUCCESS
)
1949 msg (M_FATAL
, "Error opening registry key: %s", NETWORK_CONNECTIONS_KEY
);
1953 char enum_name
[256];
1954 char connection_string
[256];
1955 HKEY connection_key
;
1956 char name_data
[256];
1958 const char name_string
[] = "Name";
1960 len
= sizeof (enum_name
);
1961 status
= RegEnumKeyEx(
1962 network_connections_key
,
1970 if (status
== ERROR_NO_MORE_ITEMS
)
1972 else if (status
!= ERROR_SUCCESS
)
1973 msg (M_FATAL
, "Error enumerating registry subkeys of key: %s",
1974 NETWORK_CONNECTIONS_KEY
);
1976 openvpn_snprintf (connection_string
, sizeof(connection_string
),
1977 "%s\\%s\\Connection",
1978 NETWORK_CONNECTIONS_KEY
, enum_name
);
1980 status
= RegOpenKeyEx(
1987 if (status
!= ERROR_SUCCESS
)
1988 dmsg (D_REGISTRY
, "Error opening registry key: %s", connection_string
);
1991 len
= sizeof (name_data
);
1992 status
= RegQueryValueEx(
2000 if (status
!= ERROR_SUCCESS
|| name_type
!= REG_SZ
)
2001 dmsg (D_REGISTRY
, "Error opening registry key: %s\\%s\\%s",
2002 NETWORK_CONNECTIONS_KEY
, connection_string
, name_string
);
2005 struct panel_reg
*reg
;
2007 ALLOC_OBJ_CLEAR_GC (reg
, struct panel_reg
, gc
);
2008 reg
->name
= string_alloc (name_data
, gc
);
2009 reg
->guid
= string_alloc (enum_name
, gc
);
2011 /* link into return list */
2018 RegCloseKey (connection_key
);
2023 RegCloseKey (network_connections_key
);
2029 * Check that two addresses are part of the same 255.255.255.252 subnet.
2032 verify_255_255_255_252 (in_addr_t local
, in_addr_t remote
)
2034 struct gc_arena gc
= gc_new ();
2035 const unsigned int mask
= 3;
2036 const char *err
= NULL
;
2038 if (local
== remote
)
2040 err
= "must be different";
2043 if ((local
& (~mask
)) != (remote
& (~mask
)))
2045 err
= "must exist within the same 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
2048 if ((local
& mask
) == 0
2049 || (local
& mask
) == 3
2050 || (remote
& mask
) == 0
2051 || (remote
& mask
) == 3)
2053 err
= "cannot use the first or last address within a given 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
2061 msg (M_FATAL
, "There is a problem in your selection of --ifconfig endpoints [local=%s, remote=%s]. The local and remote VPN endpoints %s. Try '" PACKAGE
" --show-valid-subnets' option for more info.",
2062 print_in_addr_t (local
, 0, &gc
),
2063 print_in_addr_t (remote
, 0, &gc
),
2068 void show_valid_win32_tun_subnets (void)
2073 printf ("On Windows, point-to-point IP support (i.e. --dev tun)\n");
2074 printf ("is emulated by the TAP-Win32 driver. The major limitation\n");
2075 printf ("imposed by this approach is that the --ifconfig local and\n");
2076 printf ("remote endpoints must be part of the same 255.255.255.252\n");
2077 printf ("subnet. The following list shows examples of endpoint\n");
2078 printf ("pairs which satisfy this requirement. Only the final\n");
2079 printf ("component of the IP address pairs is at issue.\n\n");
2080 printf ("As an example, the following option would be correct:\n");
2081 printf (" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
2082 printf (" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
2083 printf ("because [5,6] is part of the below list.\n\n");
2085 for (i
= 0; i
< 256; i
+= 4)
2087 printf("[%3d,%3d] ", i
+1, i
+2);
2099 show_tap_win32_adapters (int msglev
, int warnlev
)
2101 struct gc_arena gc
= gc_new ();
2103 bool warn_panel_null
= false;
2104 bool warn_panel_dup
= false;
2105 bool warn_tap_dup
= false;
2109 const struct tap_reg
*tr
;
2110 const struct tap_reg
*tr1
;
2111 const struct panel_reg
*pr
;
2113 const struct tap_reg
*tap_reg
= get_tap_reg (&gc
);
2114 const struct panel_reg
*panel_reg
= get_panel_reg (&gc
);
2116 msg (msglev
, "Available TAP-WIN32 adapters [name, GUID]:");
2118 /* loop through each TAP-Win32 adapter registry entry */
2119 for (tr
= tap_reg
; tr
!= NULL
; tr
= tr
->next
)
2123 /* loop through each network connections entry in the control panel */
2124 for (pr
= panel_reg
; pr
!= NULL
; pr
= pr
->next
)
2126 if (!strcmp (tr
->guid
, pr
->guid
))
2128 msg (msglev
, "'%s' %s", pr
->name
, tr
->guid
);
2135 warn_panel_dup
= true;
2137 else if (links
== 0)
2139 /* a TAP adapter exists without a link from the network
2140 connections control panel */
2141 warn_panel_null
= true;
2142 msg (msglev
, "[NULL] %s", tr
->guid
);
2146 /* check for TAP-Win32 adapter duplicated GUIDs */
2147 for (tr
= tap_reg
; tr
!= NULL
; tr
= tr
->next
)
2149 for (tr1
= tap_reg
; tr1
!= NULL
; tr1
= tr1
->next
)
2151 if (tr
!= tr1
&& !strcmp (tr
->guid
, tr1
->guid
))
2152 warn_tap_dup
= true;
2156 /* warn on registry inconsistencies */
2158 msg (warnlev
, "WARNING: Some TAP-Win32 adapters have duplicate GUIDs");
2161 msg (warnlev
, "WARNING: Some TAP-Win32 adapters have duplicate links from the Network Connections control panel");
2163 if (warn_panel_null
)
2164 msg (warnlev
, "WARNING: Some TAP-Win32 adapters have no link from the Network Connections control panel");
2170 * Confirm that GUID is a TAP-Win32 adapter.
2173 is_tap_win32 (const char *guid
, const struct tap_reg
*tap_reg
)
2175 const struct tap_reg
*tr
;
2177 for (tr
= tap_reg
; tr
!= NULL
; tr
= tr
->next
)
2179 if (guid
&& !strcmp (tr
->guid
, guid
))
2187 guid_to_name (const char *guid
, const struct panel_reg
*panel_reg
)
2189 const struct panel_reg
*pr
;
2191 for (pr
= panel_reg
; pr
!= NULL
; pr
= pr
->next
)
2193 if (guid
&& !strcmp (pr
->guid
, guid
))
2201 name_to_guid (const char *name
, const struct tap_reg
*tap_reg
, const struct panel_reg
*panel_reg
)
2203 const struct panel_reg
*pr
;
2205 for (pr
= panel_reg
; pr
!= NULL
; pr
= pr
->next
)
2207 if (name
&& !strcmp (pr
->name
, name
) && is_tap_win32 (pr
->guid
, tap_reg
))
2215 at_least_one_tap_win32 (const struct tap_reg
*tap_reg
)
2218 msg (M_FATAL
, "There are no TAP-Win32 adapters on this system. You should be able to create a TAP-Win32 adapter by going to Start -> All Programs -> " PACKAGE_NAME
" -> Add a new TAP-Win32 virtual ethernet adapter.");
2222 * Get an adapter GUID and optional actual_name from the
2223 * registry for the TAP device # = device_number.
2226 get_unspecified_device_guid (const int device_number
,
2228 int actual_name_size
,
2229 const struct tap_reg
*tap_reg_src
,
2230 const struct panel_reg
*panel_reg_src
,
2231 struct gc_arena
*gc
)
2233 const struct tap_reg
*tap_reg
= tap_reg_src
;
2234 struct buffer ret
= clear_buf ();
2235 struct buffer actual
= clear_buf ();
2238 ASSERT (device_number
>= 0);
2240 /* Make sure we have at least one TAP adapter */
2244 /* The actual_name output buffer may be NULL */
2247 ASSERT (actual_name_size
> 0);
2248 buf_set_write (&actual
, actual_name
, actual_name_size
);
2251 /* Move on to specified device number */
2252 for (i
= 0; i
< device_number
; i
++)
2254 tap_reg
= tap_reg
->next
;
2259 /* Save Network Panel name (if exists) in actual_name */
2262 const char *act
= guid_to_name (tap_reg
->guid
, panel_reg_src
);
2264 buf_printf (&actual
, "%s", act
);
2266 buf_printf (&actual
, "NULL");
2269 /* Save GUID for return value */
2270 ret
= alloc_buf_gc (256, gc
);
2271 buf_printf (&ret
, "%s", tap_reg
->guid
);
2276 * Lookup a --dev-node adapter name in the registry
2277 * returning the GUID and optional actual_name.
2280 get_device_guid (const char *name
,
2282 int actual_name_size
,
2283 const struct tap_reg
*tap_reg
,
2284 const struct panel_reg
*panel_reg
,
2285 struct gc_arena
*gc
)
2287 struct buffer ret
= alloc_buf_gc (256, gc
);
2288 struct buffer actual
= clear_buf ();
2290 /* Make sure we have at least one TAP adapter */
2294 /* The actual_name output buffer may be NULL */
2297 ASSERT (actual_name_size
> 0);
2298 buf_set_write (&actual
, actual_name
, actual_name_size
);
2301 /* Check if GUID was explicitly specified as --dev-node parameter */
2302 if (is_tap_win32 (name
, tap_reg
))
2304 const char *act
= guid_to_name (name
, panel_reg
);
2305 buf_printf (&ret
, "%s", name
);
2307 buf_printf (&actual
, "%s", act
);
2309 buf_printf (&actual
, "NULL");
2313 /* Lookup TAP adapter in network connections list */
2315 const char *guid
= name_to_guid (name
, tap_reg
, panel_reg
);
2318 buf_printf (&actual
, "%s", name
);
2319 buf_printf (&ret
, "%s", guid
);
2328 * Return a TAP name for netsh commands.
2331 get_netsh_id (const char *dev_node
, struct gc_arena
*gc
)
2333 const struct tap_reg
*tap_reg
= get_tap_reg (gc
);
2334 const struct panel_reg
*panel_reg
= get_panel_reg (gc
);
2335 struct buffer actual
= alloc_buf_gc (256, gc
);
2338 at_least_one_tap_win32 (tap_reg
);
2342 guid
= get_device_guid (dev_node
, BPTR (&actual
), BCAP (&actual
), tap_reg
, panel_reg
, gc
);
2346 guid
= get_unspecified_device_guid (0, BPTR (&actual
), BCAP (&actual
), tap_reg
, panel_reg
, gc
);
2348 if (get_unspecified_device_guid (1, NULL
, 0, tap_reg
, panel_reg
, gc
)) /* ambiguous if more than one TAP-Win32 adapter */
2353 return "NULL"; /* not found */
2354 else if (strcmp (BPTR (&actual
), "NULL"))
2355 return BPTR (&actual
); /* control panel name */
2357 return guid
; /* no control panel name, return GUID instead */
2361 * Get adapter info list
2363 const IP_ADAPTER_INFO
*
2364 get_adapter_info_list (struct gc_arena
*gc
)
2367 IP_ADAPTER_INFO
*pi
= NULL
;
2370 if ((status
= GetAdaptersInfo (NULL
, &size
)) != ERROR_BUFFER_OVERFLOW
)
2372 msg (M_INFO
, "GetAdaptersInfo #1 failed (status=%u) : %s",
2373 (unsigned int)status
,
2374 strerror_win32 (status
, gc
));
2378 pi
= (PIP_ADAPTER_INFO
) gc_malloc (size
, false, gc
);
2379 if ((status
= GetAdaptersInfo (pi
, &size
)) == NO_ERROR
)
2383 msg (M_INFO
, "GetAdaptersInfo #2 failed (status=%u) : %s",
2384 (unsigned int)status
,
2385 strerror_win32 (status
, gc
));
2391 static const IP_INTERFACE_INFO
*
2392 get_interface_info_list (struct gc_arena
*gc
)
2395 IP_INTERFACE_INFO
*ii
= NULL
;
2398 if ((status
= GetInterfaceInfo (NULL
, &size
)) != ERROR_INSUFFICIENT_BUFFER
)
2400 msg (M_INFO
, "GetInterfaceInfo #1 failed (status=%u) : %s",
2401 (unsigned int)status
,
2402 strerror_win32 (status
, gc
));
2406 ii
= (PIP_INTERFACE_INFO
) gc_malloc (size
, false, gc
);
2407 if ((status
= GetInterfaceInfo (ii
, &size
)) == NO_ERROR
)
2411 msg (M_INFO
, "GetInterfaceInfo #2 failed (status=%u) : %s",
2412 (unsigned int)status
,
2413 strerror_win32 (status
, gc
));
2419 static const IP_ADAPTER_INDEX_MAP
*
2420 get_interface_info (DWORD index
, struct gc_arena
*gc
)
2422 const IP_INTERFACE_INFO
*list
= get_interface_info_list (gc
);
2426 for (i
= 0; i
< list
->NumAdapters
; ++i
)
2428 const IP_ADAPTER_INDEX_MAP
*inter
= &list
->Adapter
[i
];
2429 if (index
== inter
->Index
)
2437 * Given an adapter index, return a pointer to the
2438 * IP_ADAPTER_INFO structure for that adapter.
2441 static const IP_ADAPTER_INFO
*
2442 get_adapter (const IP_ADAPTER_INFO
*ai
, DWORD index
)
2444 if (ai
&& index
!= (DWORD
)~0)
2446 const IP_ADAPTER_INFO
*a
;
2448 /* find index in the linked list */
2449 for (a
= ai
; a
!= NULL
; a
= a
->Next
)
2451 if (a
->Index
== index
)
2458 static const IP_ADAPTER_INFO
*
2459 get_adapter_info (DWORD index
, struct gc_arena
*gc
)
2461 return get_adapter (get_adapter_info_list (gc
), index
);
2465 get_adapter_n_ip_netmask (const IP_ADAPTER_INFO
*ai
)
2470 const IP_ADDR_STRING
*ip
= &ai
->IpAddressList
;
2484 get_adapter_ip_netmask (const IP_ADAPTER_INFO
*ai
, const int n
, in_addr_t
*ip
, in_addr_t
*netmask
)
2492 const IP_ADDR_STRING
*iplist
= &ai
->IpAddressList
;
2500 iplist
= iplist
->Next
;
2505 const unsigned int getaddr_flags
= GETADDR_HOST_ORDER
;
2506 const char *ip_str
= iplist
->IpAddress
.String
;
2507 const char *netmask_str
= iplist
->IpMask
.String
;
2508 bool succeed1
= false;
2509 bool succeed2
= false;
2511 if (ip_str
&& netmask_str
&& strlen (ip_str
) && strlen (netmask_str
))
2513 *ip
= getaddr (getaddr_flags
, ip_str
, 0, &succeed1
, NULL
);
2514 *netmask
= getaddr (getaddr_flags
, netmask_str
, 0, &succeed2
, NULL
);
2515 ret
= (succeed1
== true && succeed2
== true);
2523 const IP_ADAPTER_INFO
*
2524 get_tun_adapter (const struct tuntap
*tt
, const IP_ADAPTER_INFO
*list
)
2527 return get_adapter (list
, tt
->adapter_index
);
2533 is_adapter_up (const struct tuntap
*tt
, const IP_ADAPTER_INFO
*list
)
2538 const IP_ADAPTER_INFO
*ai
= get_tun_adapter (tt
, list
);
2542 const int n
= get_adapter_n_ip_netmask (ai
);
2544 /* loop once for every IP/netmask assigned to adapter */
2545 for (i
= 0; i
< n
; ++i
)
2547 in_addr_t ip
, netmask
;
2548 if (get_adapter_ip_netmask (ai
, i
, &ip
, &netmask
))
2550 if (tt
->local
&& tt
->adapter_netmask
)
2552 /* wait for our --ifconfig parms to match the actual adapter parms */
2553 if (tt
->local
== ip
&& tt
->adapter_netmask
== netmask
)
2558 /* --ifconfig was not defined, maybe using a real DHCP server */
2566 ret
= true; /* this can occur when TAP adapter is bridged */
2572 is_ip_in_adapter_subnet (const IP_ADAPTER_INFO
*ai
, const in_addr_t ip
, in_addr_t
*highest_netmask
)
2577 if (highest_netmask
)
2578 *highest_netmask
= 0;
2582 const int n
= get_adapter_n_ip_netmask (ai
);
2583 for (i
= 0; i
< n
; ++i
)
2585 in_addr_t adapter_ip
, adapter_netmask
;
2586 if (get_adapter_ip_netmask (ai
, i
, &adapter_ip
, &adapter_netmask
))
2588 if (adapter_ip
&& adapter_netmask
&& (ip
& adapter_netmask
) == (adapter_ip
& adapter_netmask
))
2590 if (highest_netmask
&& adapter_netmask
> *highest_netmask
)
2591 *highest_netmask
= adapter_netmask
;
2601 adapter_index_of_ip (const IP_ADAPTER_INFO
*list
, const in_addr_t ip
, int *count
)
2603 struct gc_arena gc
= gc_new ();
2605 in_addr_t highest_netmask
= 0;
2615 if (is_ip_in_adapter_subnet (list
, ip
, &hn
))
2617 if (first
|| hn
> highest_netmask
)
2619 highest_netmask
= hn
;
2625 else if (hn
== highest_netmask
)
2634 dmsg (D_ROUTE_DEBUG
, "DEBUG: IP Locate: ip=%s nm=%s index=%d count=%d",
2635 print_in_addr_t (ip
, 0, &gc
),
2636 print_in_addr_t (highest_netmask
, 0, &gc
),
2638 count
? *count
: -1);
2640 if (ret
== ~0 && count
)
2648 * Given an adapter index, return true if the adapter
2652 dhcp_disabled (DWORD index
)
2654 struct gc_arena gc
= gc_new ();
2655 const IP_ADAPTER_INFO
*ai
= get_adapter_info (index
, &gc
);
2658 if (ai
&& !ai
->DhcpEnabled
)
2666 * Delete all temporary address/netmask pairs which were added
2667 * to adapter (given by index) by previous calls to AddIPAddress.
2670 delete_temp_addresses (DWORD index
)
2672 struct gc_arena gc
= gc_new ();
2673 const IP_ADAPTER_INFO
*a
= get_adapter_info (index
, &gc
);
2677 const IP_ADDR_STRING
*ip
= &a
->IpAddressList
;
2681 const DWORD context
= ip
->Context
;
2683 if ((status
= DeleteIPAddress ((ULONG
) context
)) == NO_ERROR
)
2685 msg (M_INFO
, "Successfully deleted previously set dynamic IP/netmask: %s/%s",
2686 ip
->IpAddress
.String
,
2691 const char *empty
= "0.0.0.0";
2692 if (strcmp (ip
->IpAddress
.String
, empty
)
2693 || strcmp (ip
->IpMask
.String
, empty
))
2694 msg (M_INFO
, "NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%u)",
2695 ip
->IpAddress
.String
,
2697 (unsigned int)status
);
2706 * Get interface index for use with IP Helper API functions.
2709 get_interface_index (const char *guid
)
2711 struct gc_arena gc
= gc_new ();
2715 snwprintf (wbuf
, SIZE (wbuf
), L
"\\DEVICE\\TCPIP_%S", guid
);
2716 wbuf
[SIZE(wbuf
) - 1] = 0;
2717 if ((status
= GetAdapterIndex (wbuf
, &index
)) != NO_ERROR
)
2719 msg (M_INFO
, "NOTE: could not get adapter index for %S, status=%u : %s",
2721 (unsigned int)status
,
2722 strerror_win32 (status
, &gc
));
2734 * Return a string representing a PIP_ADDR_STRING
2737 format_ip_addr_string (const IP_ADDR_STRING
*ip
, struct gc_arena
*gc
)
2739 struct buffer out
= alloc_buf_gc (256, gc
);
2742 buf_printf (&out
, "%s", ip
->IpAddress
.String
);
2743 if (strlen (ip
->IpMask
.String
))
2745 buf_printf (&out
, "/");
2746 buf_printf (&out
, "%s", ip
->IpMask
.String
);
2748 buf_printf (&out
, " ");
2755 * Show info for a single adapter
2758 show_adapter (int msglev
, const IP_ADAPTER_INFO
*a
, struct gc_arena
*gc
)
2760 msg (msglev
, "%s", a
->Description
);
2761 msg (msglev
, " Index = %d", (int)a
->Index
);
2762 msg (msglev
, " GUID = %s", a
->AdapterName
);
2763 msg (msglev
, " IP = %s", format_ip_addr_string (&a
->IpAddressList
, gc
));
2764 msg (msglev
, " MAC = %s", format_hex_ex (a
->Address
, a
->AddressLength
, 0, 1, ":", gc
));
2765 msg (msglev
, " GATEWAY = %s", format_ip_addr_string (&a
->GatewayList
, gc
));
2768 msg (msglev
, " DHCP SERV = %s", format_ip_addr_string (&a
->DhcpServer
, gc
));
2769 msg (msglev
, " DHCP LEASE OBTAINED = %s", time_string (a
->LeaseObtained
, 0, false, gc
));
2770 msg (msglev
, " DHCP LEASE EXPIRES = %s", time_string (a
->LeaseExpires
, 0, false, gc
));
2774 msg (msglev
, " PRI WINS = %s", format_ip_addr_string (&a
->PrimaryWinsServer
, gc
));
2775 msg (msglev
, " SEC WINS = %s", format_ip_addr_string (&a
->SecondaryWinsServer
, gc
));
2780 * Show current adapter list
2783 show_adapters (int msglev
)
2785 struct gc_arena gc
= gc_new ();
2786 const IP_ADAPTER_INFO
*ai
= get_adapter_info_list (&gc
);
2788 msg (msglev
, "SYSTEM ADAPTER LIST");
2791 const IP_ADAPTER_INFO
*a
;
2793 /* find index in the linked list */
2794 for (a
= ai
; a
!= NULL
; a
= a
->Next
)
2796 show_adapter (msglev
, a
, &gc
);
2803 * DHCP release/renewal
2807 dhcp_release (const struct tuntap
*tt
)
2809 struct gc_arena gc
= gc_new ();
2811 if (tt
&& tt
->options
.ip_win32_type
== IPW32_SET_DHCP_MASQ
&& tt
->adapter_index
!= ~0)
2813 const IP_ADAPTER_INDEX_MAP
*inter
= get_interface_info (tt
->adapter_index
, &gc
);
2816 DWORD status
= IpReleaseAddress ((IP_ADAPTER_INDEX_MAP
*)inter
);
2817 if (status
== NO_ERROR
)
2819 msg (D_TUNTAP_INFO
, "TAP: DHCP address released");
2823 msg (M_WARN
, "NOTE: Release of DHCP-assigned IP address lease on TAP-Win32 adapter failed: %s (code=%u)",
2824 strerror_win32 (status
, &gc
),
2825 (unsigned int)status
);
2833 dhcp_renew (const struct tuntap
*tt
)
2835 struct gc_arena gc
= gc_new ();
2837 if (tt
&& tt
->options
.ip_win32_type
== IPW32_SET_DHCP_MASQ
&& tt
->adapter_index
!= ~0)
2839 const IP_ADAPTER_INDEX_MAP
*inter
= get_interface_info (tt
->adapter_index
, &gc
);
2842 DWORD status
= IpRenewAddress ((IP_ADAPTER_INDEX_MAP
*)inter
);
2843 if (status
== NO_ERROR
)
2845 msg (D_TUNTAP_INFO
, "TAP: DHCP address renewal succeeded");
2849 msg (M_WARN
, "WARNING: Failed to renew DHCP IP address lease on TAP-Win32 adapter: %s (code=%u)",
2850 strerror_win32 (status
, &gc
),
2851 (unsigned int)status
);
2859 * Convert DHCP options from the command line / config file
2860 * into a raw DHCP-format options string.
2864 write_dhcp_u8 (struct buffer
*buf
, const int type
, const int data
)
2866 if (!buf_safe (buf
, 3))
2867 msg (M_FATAL
, "write_dhcp_u8: buffer overflow building DHCP options");
2868 buf_write_u8 (buf
, type
);
2869 buf_write_u8 (buf
, 1);
2870 buf_write_u8 (buf
, data
);
2874 write_dhcp_u32_array (struct buffer
*buf
, const int type
, const uint32_t *data
, const unsigned int len
)
2879 const int size
= len
* sizeof (uint32_t);
2881 if (!buf_safe (buf
, 2 + size
))
2882 msg (M_FATAL
, "write_dhcp_u32_array: buffer overflow building DHCP options");
2883 if (size
< 1 || size
> 255)
2884 msg (M_FATAL
, "write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size
);
2885 buf_write_u8 (buf
, type
);
2886 buf_write_u8 (buf
, size
);
2887 for (i
= 0; i
< len
; ++i
)
2888 buf_write_u32 (buf
, data
[i
]);
2893 write_dhcp_str (struct buffer
*buf
, const int type
, const char *str
)
2895 const int len
= strlen (str
);
2896 if (!buf_safe (buf
, 2 + len
))
2897 msg (M_FATAL
, "write_dhcp_str: buffer overflow building DHCP options");
2898 if (len
< 1 || len
> 255)
2899 msg (M_FATAL
, "write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str
);
2900 buf_write_u8 (buf
, type
);
2901 buf_write_u8 (buf
, len
);
2902 buf_write (buf
, str
, len
);
2906 build_dhcp_options_string (struct buffer
*buf
, const struct tuntap_options
*o
)
2909 write_dhcp_str (buf
, 15, o
->domain
);
2911 if (o
->netbios_scope
)
2912 write_dhcp_str (buf
, 47, o
->netbios_scope
);
2914 if (o
->netbios_node_type
)
2915 write_dhcp_u8 (buf
, 46, o
->netbios_node_type
);
2917 write_dhcp_u32_array (buf
, 6, (uint32_t*)o
->dns
, o
->dns_len
);
2918 write_dhcp_u32_array (buf
, 44, (uint32_t*)o
->wins
, o
->wins_len
);
2919 write_dhcp_u32_array (buf
, 42, (uint32_t*)o
->ntp
, o
->ntp_len
);
2920 write_dhcp_u32_array (buf
, 45, (uint32_t*)o
->nbdd
, o
->nbdd_len
);
2922 /* the MS DHCP server option 'Disable Netbios-over-TCP/IP
2923 is implemented as vendor option 001, value 002.
2924 A value of 001 means 'leave NBT alone' which is the default */
2927 buf_write_u8 (buf
, 43);
2928 buf_write_u8 (buf
, 6); /* total length field */
2929 buf_write_u8 (buf
, 0x001);
2930 buf_write_u8 (buf
, 4); /* length of the vendor specified field */
2931 buf_write_u32 (buf
, 0x002);
2936 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
2938 struct gc_arena gc
= gc_new ();
2939 char device_path
[256];
2940 const char *device_guid
= NULL
;
2943 /*netcmd_semaphore_lock ();*/
2945 ipv6_support (ipv6
, false, tt
);
2947 if (tt
->type
== DEV_TYPE_NULL
)
2953 else if (tt
->type
== DEV_TYPE_TAP
|| tt
->type
== DEV_TYPE_TUN
)
2959 msg (M_FATAL
|M_NOPREFIX
, "Unknown virtual device type: '%s'", dev
);
2963 * Lookup the device name in the registry, using the --dev-node high level name.
2966 const struct tap_reg
*tap_reg
= get_tap_reg (&gc
);
2967 const struct panel_reg
*panel_reg
= get_panel_reg (&gc
);
2968 char guid_buffer
[256];
2970 at_least_one_tap_win32 (tap_reg
);
2974 /* Get the device GUID for the device specified with --dev-node. */
2975 device_guid
= get_device_guid (dev_node
, guid_buffer
, sizeof (guid_buffer
), tap_reg
, panel_reg
, &gc
);
2978 msg (M_FATAL
, "TAP-Win32 adapter '%s' not found", dev_node
);
2980 /* Open Windows TAP-Win32 adapter */
2981 openvpn_snprintf (device_path
, sizeof(device_path
), "%s%s%s",
2986 tt
->hand
= CreateFile (
2988 GENERIC_READ
| GENERIC_WRITE
,
2989 0, /* was: FILE_SHARE_READ */
2992 FILE_ATTRIBUTE_SYSTEM
| FILE_FLAG_OVERLAPPED
,
2996 if (tt
->hand
== INVALID_HANDLE_VALUE
)
2997 msg (M_ERR
, "CreateFile failed on TAP device: %s", device_path
);
3001 int device_number
= 0;
3003 /* Try opening all TAP devices until we find one available */
3006 device_guid
= get_unspecified_device_guid (device_number
,
3008 sizeof (guid_buffer
),
3014 msg (M_FATAL
, "All TAP-Win32 adapters on this system are currently in use.");
3016 /* Open Windows TAP-Win32 adapter */
3017 openvpn_snprintf (device_path
, sizeof(device_path
), "%s%s%s",
3022 tt
->hand
= CreateFile (
3024 GENERIC_READ
| GENERIC_WRITE
,
3025 0, /* was: FILE_SHARE_READ */
3028 FILE_ATTRIBUTE_SYSTEM
| FILE_FLAG_OVERLAPPED
,
3032 if (tt
->hand
== INVALID_HANDLE_VALUE
)
3033 msg (D_TUNTAP_INFO
, "CreateFile failed on TAP device: %s", device_path
);
3041 /* translate high-level device name into a device instance
3042 GUID using the registry */
3043 tt
->actual_name
= string_alloc (guid_buffer
, NULL
);
3046 msg (M_INFO
, "TAP-WIN32 device [%s] opened: %s", tt
->actual_name
, device_path
);
3048 /* get driver version info */
3052 if (DeviceIoControl (tt
->hand
, TAP_IOCTL_GET_VERSION
,
3053 &info
, sizeof (info
),
3054 &info
, sizeof (info
), &len
, NULL
))
3056 msg (D_TUNTAP_INFO
, "TAP-Win32 Driver Version %d.%d %s",
3059 (info
[2] ? "(DEBUG)" : ""));
3062 if ( !(info
[0] > TAP_WIN32_MIN_MAJOR
3063 || (info
[0] == TAP_WIN32_MIN_MAJOR
&& info
[1] >= TAP_WIN32_MIN_MINOR
)) )
3064 msg (M_FATAL
, "ERROR: This version of " PACKAGE_NAME
" requires a TAP-Win32 driver that is at least version %d.%d -- If you recently upgraded your " PACKAGE_NAME
" distribution, a reboot is probably required at this point to get Windows to see the new driver.",
3065 TAP_WIN32_MIN_MAJOR
,
3066 TAP_WIN32_MIN_MINOR
);
3069 /* get driver MTU */
3072 if (DeviceIoControl (tt
->hand
, TAP_IOCTL_GET_MTU
,
3074 &mtu
, sizeof (mtu
), &len
, NULL
))
3076 tt
->post_open_mtu
= (int) mtu
;
3077 msg (D_MTU_INFO
, "TAP-Win32 MTU=%d", (int) mtu
);
3081 /* set point-to-point mode if TUN device */
3083 if (tt
->type
== DEV_TYPE_TUN
)
3086 ep
[0] = htonl (tt
->local
);
3087 ep
[1] = htonl (tt
->remote_netmask
);
3088 if (!tt
->did_ifconfig_setup
)
3090 msg (M_FATAL
, "ERROR: --dev tun also requires --ifconfig");
3092 if (!DeviceIoControl (tt
->hand
, TAP_IOCTL_CONFIG_POINT_TO_POINT
,
3094 ep
, sizeof (ep
), &len
, NULL
))
3095 msg (M_FATAL
, "ERROR: The TAP-Win32 driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
3098 /* should we tell the TAP-Win32 driver to masquerade as a DHCP server as a means
3099 of setting the adapter address? */
3100 if (tt
->did_ifconfig_setup
&& tt
->options
.ip_win32_type
== IPW32_SET_DHCP_MASQ
)
3104 /* We will answer DHCP requests with a reply to set IP/subnet to these values */
3105 ep
[0] = htonl (tt
->local
);
3106 ep
[1] = htonl (tt
->adapter_netmask
);
3108 /* At what IP address should the DHCP server masquerade at? */
3109 if (tt
->type
== DEV_TYPE_TUN
)
3111 ep
[2] = htonl (tt
->remote_netmask
);
3112 if (tt
->options
.dhcp_masq_custom_offset
)
3113 msg (M_WARN
, "WARNING: because you are using '--dev tun' mode, the '--ip-win32 dynamic [offset]' option is ignoring the offset parameter");
3117 in_addr_t dsa
; /* DHCP server addr */
3119 ASSERT (tt
->type
== DEV_TYPE_TAP
);
3121 if (tt
->options
.dhcp_masq_offset
< 0)
3122 dsa
= (tt
->local
| (~tt
->adapter_netmask
)) + tt
->options
.dhcp_masq_offset
;
3124 dsa
= (tt
->local
& tt
->adapter_netmask
) + tt
->options
.dhcp_masq_offset
;
3126 if (dsa
== tt
->local
)
3127 msg (M_FATAL
, "ERROR: There is a clash between the --ifconfig local address and the internal DHCP server address -- both are set to %s -- please use the --ip-win32 dynamic option to choose a different free address from the --ifconfig subnet for the internal DHCP server", print_in_addr_t (dsa
, 0, &gc
));
3129 if ((tt
->local
& tt
->adapter_netmask
) != (dsa
& tt
->adapter_netmask
))
3130 msg (M_FATAL
, "ERROR: --tap-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
3132 ep
[2] = htonl (dsa
);
3135 /* lease time in seconds */
3136 ep
[3] = (uint32_t) tt
->options
.dhcp_lease_time
;
3140 if (!DeviceIoControl (tt
->hand
, TAP_IOCTL_CONFIG_DHCP_MASQ
,
3142 ep
, sizeof (ep
), &len
, NULL
))
3143 msg (M_FATAL
, "ERROR: The TAP-Win32 driver rejected a DeviceIoControl call to set TAP_IOCTL_CONFIG_DHCP_MASQ mode");
3145 msg (M_INFO
, "Notified TAP-Win32 driver to set a DHCP IP/netmask of %s/%s on interface %s [DHCP-serv: %s, lease-time: %d]",
3146 print_in_addr_t (tt
->local
, 0, &gc
),
3147 print_in_addr_t (tt
->adapter_netmask
, 0, &gc
),
3149 print_in_addr_t (ep
[2], IA_NET_ORDER
, &gc
),
3153 /* user-supplied DHCP options capability */
3154 if (tt
->options
.dhcp_options
)
3156 struct buffer buf
= alloc_buf (256);
3157 build_dhcp_options_string (&buf
, &tt
->options
);
3158 msg (D_DHCP_OPT
, "DHCP option string: %s", format_hex (BPTR (&buf
), BLEN (&buf
), 0, &gc
));
3159 if (!DeviceIoControl (tt
->hand
, TAP_IOCTL_CONFIG_DHCP_SET_OPT
,
3160 BPTR (&buf
), BLEN (&buf
),
3161 BPTR (&buf
), BLEN (&buf
), &len
, NULL
))
3162 msg (M_FATAL
, "ERROR: The TAP-Win32 driver rejected a TAP_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
3167 /* set driver media status to 'connected' */
3169 ULONG status
= TRUE
;
3170 if (!DeviceIoControl (tt
->hand
, TAP_IOCTL_SET_MEDIA_STATUS
,
3171 &status
, sizeof (status
),
3172 &status
, sizeof (status
), &len
, NULL
))
3173 msg (M_WARN
, "WARNING: The TAP-Win32 driver rejected a TAP_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
3176 /* possible wait for adapter to come up */
3178 int s
= tt
->options
.tap_sleep
;
3181 msg (M_INFO
, "Sleeping for %d seconds...", s
);
3186 /* possibly use IP Helper API to set IP address on adapter */
3188 DWORD index
= get_interface_index (device_guid
);
3189 tt
->adapter_index
= index
;
3191 /* flush arp cache */
3192 if (index
!= (DWORD
)~0)
3196 if ((status
= FlushIpNetTable (index
)) == NO_ERROR
)
3197 msg (M_INFO
, "Successful ARP Flush on interface [%u] %s",
3198 (unsigned int)index
,
3201 msg (M_WARN
, "NOTE: FlushIpNetTable failed on interface [%u] %s (status=%u) : %s",
3202 (unsigned int)index
,
3204 (unsigned int)status
,
3205 strerror_win32 (status
, &gc
));
3209 * If the TAP-Win32 driver is masquerading as a DHCP server
3210 * make sure the TCP/IP properties for the adapter are
3213 if (tt
->did_ifconfig_setup
&& tt
->options
.ip_win32_type
== IPW32_SET_DHCP_MASQ
)
3215 /* check dhcp enable status */
3216 if (dhcp_disabled (index
))
3217 msg (M_WARN
, "WARNING: You have selected '--ip-win32 dynamic', which will not work unless the TAP-Win32 TCP/IP properties are set to 'Obtain an IP address automatically'");
3219 /* force an explicit DHCP lease renewal on TAP adapter? */
3220 if (tt
->options
.dhcp_pre_release
)
3222 if (tt
->options
.dhcp_renew
)
3226 if (tt
->did_ifconfig_setup
&& tt
->options
.ip_win32_type
== IPW32_SET_IPAPI
)
3229 const char *error_suffix
= "I am having trouble using the Windows 'IP helper API' to automatically set the IP address -- consider using other --ip-win32 methods (not 'ipapi')";
3231 /* couldn't get adapter index */
3232 if (index
== (DWORD
)~0)
3234 msg (M_FATAL
, "ERROR: unable to get adapter index for interface %s -- %s",
3239 /* check dhcp enable status */
3240 if (dhcp_disabled (index
))
3241 msg (M_WARN
, "NOTE: You have selected (explicitly or by default) '--ip-win32 ipapi', which has a better chance of working correctly if the TAP-Win32 TCP/IP properties are set to 'Obtain an IP address automatically'");
3243 /* delete previously added IP addresses which were not
3244 correctly deleted */
3245 delete_temp_addresses (index
);
3247 /* add a new IP address */
3248 if ((status
= AddIPAddress (htonl(tt
->local
),
3249 htonl(tt
->adapter_netmask
),
3252 &tt
->ipapi_instance
)) == NO_ERROR
)
3253 msg (M_INFO
, "Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
3254 print_in_addr_t (tt
->local
, 0, &gc
),
3255 print_in_addr_t (tt
->adapter_netmask
, 0, &gc
),
3259 msg (M_FATAL
, "ERROR: AddIPAddress %s/%s failed on interface %s, index=%d, status=%u (windows error: '%s') -- %s",
3260 print_in_addr_t (tt
->local
, 0, &gc
),
3261 print_in_addr_t (tt
->adapter_netmask
, 0, &gc
),
3264 (unsigned int)status
,
3265 strerror_win32 (status
, &gc
),
3267 tt
->ipapi_context_defined
= true;
3270 /*netcmd_semaphore_release ();*/
3275 tap_win32_getinfo (const struct tuntap
*tt
, struct gc_arena
*gc
)
3277 if (tt
&& tt
->hand
!= NULL
)
3279 struct buffer out
= alloc_buf_gc (256, gc
);
3281 if (DeviceIoControl (tt
->hand
, TAP_IOCTL_GET_INFO
,
3282 BSTR (&out
), BCAP (&out
),
3283 BSTR (&out
), BCAP (&out
),
3293 tun_show_debug (struct tuntap
*tt
)
3295 if (tt
&& tt
->hand
!= NULL
)
3297 struct buffer out
= alloc_buf (1024);
3299 while (DeviceIoControl (tt
->hand
, TAP_IOCTL_GET_LOG_LINE
,
3300 BSTR (&out
), BCAP (&out
),
3301 BSTR (&out
), BCAP (&out
),
3304 msg (D_TAP_WIN32_DEBUG
, "TAP-Win32: %s", BSTR (&out
));
3311 close_tun (struct tuntap
*tt
)
3313 struct gc_arena gc
= gc_new ();
3318 if (tt
->ipapi_context_defined
)
3321 if ((status
= DeleteIPAddress (tt
->ipapi_context
)) != NO_ERROR
)
3323 msg (M_WARN
, "Warning: DeleteIPAddress[%u] failed on TAP-Win32 adapter, status=%u : %s",
3324 (unsigned int)tt
->ipapi_context
,
3325 (unsigned int)status
,
3326 strerror_win32 (status
, &gc
));
3331 if (tt
->options
.dhcp_release
)
3334 if (tt
->hand
!= NULL
)
3336 dmsg (D_WIN32_IO_LOW
, "Attempting CancelIO on TAP-Win32 adapter");
3337 if (!CancelIo (tt
->hand
))
3338 msg (M_WARN
| M_ERRNO
, "Warning: CancelIO failed on TAP-Win32 adapter");
3341 dmsg (D_WIN32_IO_LOW
, "Attempting close of overlapped read event on TAP-Win32 adapter");
3342 overlapped_io_close (&tt
->reads
);
3344 dmsg (D_WIN32_IO_LOW
, "Attempting close of overlapped write event on TAP-Win32 adapter");
3345 overlapped_io_close (&tt
->writes
);
3347 if (tt
->hand
!= NULL
)
3349 dmsg (D_WIN32_IO_LOW
, "Attempting CloseHandle on TAP-Win32 adapter");
3350 if (!CloseHandle (tt
->hand
))
3351 msg (M_WARN
| M_ERRNO
, "Warning: CloseHandle failed on TAP-Win32 adapter");
3354 if (tt
->actual_name
)
3355 free (tt
->actual_name
);
3364 * Convert --ip-win32 constants between index and ascii form.
3367 struct ipset_names
{
3368 const char *short_form
;
3371 /* Indexed by IPW32_SET_x */
3372 static const struct ipset_names ipset_names
[] = {
3380 ascii2ipset (const char* name
)
3383 ASSERT (IPW32_SET_N
== SIZE (ipset_names
));
3384 for (i
= 0; i
< IPW32_SET_N
; ++i
)
3385 if (!strcmp (name
, ipset_names
[i
].short_form
))
3391 ipset2ascii (int index
)
3393 ASSERT (IPW32_SET_N
== SIZE (ipset_names
));
3394 if (index
< 0 || index
>= IPW32_SET_N
)
3395 return "[unknown --ip-win32 type]";
3397 return ipset_names
[index
].short_form
;
3401 ipset2ascii_all (struct gc_arena
*gc
)
3403 struct buffer out
= alloc_buf_gc (256, gc
);
3406 ASSERT (IPW32_SET_N
== SIZE (ipset_names
));
3407 for (i
= 0; i
< IPW32_SET_N
; ++i
)
3410 buf_printf(&out
, " ");
3411 buf_printf(&out
, "[%s]", ipset2ascii(i
));
3419 open_tun (const char *dev
, const char *dev_type
, const char *dev_node
, bool ipv6
, struct tuntap
*tt
)
3421 open_tun_generic (dev
, dev_type
, dev_node
, ipv6
, false, true, tt
);
3425 close_tun (struct tuntap
* tt
)
3429 close_tun_generic (tt
);
3435 write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
3437 return write (tt
->fd
, buf
, len
);
3441 read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
)
3443 return read (tt
->fd
, buf
, len
);