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-2009 OpenVPN Technologies, Inc. <sales@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
30 #include "tap-win32/common.h"
43 /* time constants for --ip-win32 adaptive */
44 #define IPW32_SET_ADAPTIVE_DELAY_WINDOW 300
45 #define IPW32_SET_ADAPTIVE_TRY_NETSH 20
47 struct tuntap_options
{
48 /* --ip-win32 options */
49 bool ip_win32_defined
;
51 # define IPW32_SET_MANUAL 0 /* "--ip-win32 manual" */
52 # define IPW32_SET_NETSH 1 /* "--ip-win32 netsh" */
53 # define IPW32_SET_IPAPI 2 /* "--ip-win32 ipapi" */
54 # define IPW32_SET_DHCP_MASQ 3 /* "--ip-win32 dynamic" */
55 # define IPW32_SET_ADAPTIVE 4 /* "--ip-win32 adaptive" */
56 # define IPW32_SET_N 5
59 /* --ip-win32 dynamic options */
60 bool dhcp_masq_custom_offset
;
64 /* --tap-sleep option */
67 /* --dhcp-option options */
71 const char *domain
; /* DOMAIN (15) */
73 const char *netbios_scope
; /* NBS (47) */
75 int netbios_node_type
; /* NBT 1,2,4,8 (46) */
77 #define N_DHCP_ADDR 4 /* Max # of addresses allowed for
81 in_addr_t dns
[N_DHCP_ADDR
];
85 in_addr_t wins
[N_DHCP_ADDR
];
89 in_addr_t ntp
[N_DHCP_ADDR
];
93 in_addr_t nbdd
[N_DHCP_ADDR
];
96 /* DISABLE_NBT (43, Vendor option 001) */
100 bool dhcp_pre_release
;
106 struct tuntap_options
{
112 struct tuntap_options
{
113 int dummy
; /* not used */
119 * Define a TUN/TAP dev.
124 # define TUNNEL_TYPE(tt) ((tt) ? ((tt)->type) : DEV_TYPE_UNDEF)
125 int type
; /* DEV_TYPE_x as defined in proto.h */
127 # define TUNNEL_TOPOLOGY(tt) ((tt) ? ((tt)->topology) : TOP_UNDEF)
128 int topology
; /* one of the TOP_x values */
130 bool did_ifconfig_setup
;
135 struct tuntap_options options
; /* options set on command line */
137 char *actual_name
; /* actual name of TUN/TAP dev, usually including unit number */
139 /* number of TX buffers */
142 /* ifconfig parameters */
144 in_addr_t remote_netmask
;
149 struct overlapped_io reads
;
150 struct overlapped_io writes
;
151 struct rw_handle rw_handle
;
153 /* used for setting interface address via IP Helper API
154 or DHCP masquerade */
155 bool ipapi_context_defined
;
157 ULONG ipapi_instance
;
158 in_addr_t adapter_netmask
;
160 /* Windows adapter index for TAP-Win32 adapter,
166 int fd
; /* file descriptor for TUN/TAP dev */
169 #ifdef TARGET_SOLARIS
173 /* used for printing status info only */
174 unsigned int rwflags_debug
;
176 /* Some TUN/TAP drivers like to be ioctled for mtu
182 tuntap_defined (const struct tuntap
*tt
)
185 return tt
&& tt
->hand
!= NULL
;
187 return tt
&& tt
->fd
>= 0;
192 * Function prototypes
195 void clear_tuntap (struct tuntap
*tuntap
);
197 void open_tun (const char *dev
, const char *dev_type
, const char *dev_node
,
198 bool ipv6
, struct tuntap
*tt
);
200 void close_tun (struct tuntap
*tt
);
202 int write_tun (struct tuntap
* tt
, uint8_t *buf
, int len
);
204 int read_tun (struct tuntap
* tt
, uint8_t *buf
, int len
);
206 void tuncfg (const char *dev
, const char *dev_type
, const char *dev_node
,
207 bool ipv6
, int persist_mode
, const char *username
,
208 const char *groupname
, const struct tuntap_options
*options
);
210 const char *guess_tuntap_dev (const char *dev
,
211 const char *dev_type
,
212 const char *dev_node
,
213 struct gc_arena
*gc
);
215 struct tuntap
*init_tun (const char *dev
, /* --dev option */
216 const char *dev_type
, /* --dev-type option */
217 int topology
, /* one of the TOP_x values */
218 const char *ifconfig_local_parm
, /* --ifconfig parm 1 */
219 const char *ifconfig_remote_netmask_parm
, /* --ifconfig parm 2 */
220 in_addr_t local_public
,
221 in_addr_t remote_public
,
222 const bool strict_warn
,
225 void init_tun_post (struct tuntap
*tt
,
226 const struct frame
*frame
,
227 const struct tuntap_options
*options
);
229 void do_ifconfig (struct tuntap
*tt
,
230 const char *actual
, /* actual device name */
232 const struct env_set
*es
);
234 bool is_dev_type (const char *dev
, const char *dev_type
, const char *match_type
);
235 int dev_type_enum (const char *dev
, const char *dev_type
);
236 const char *dev_type_string (const char *dev
, const char *dev_type
);
238 const char *ifconfig_options_string (const struct tuntap
* tt
, bool remote
, bool disable
, struct gc_arena
*gc
);
240 bool is_tun_p2p (const struct tuntap
*tt
);
242 void check_subnet_conflict (const in_addr_t ip
,
243 const in_addr_t netmask
,
246 void warn_on_use_of_common_subnets (void);
253 tun_adjust_frame_parameters (struct frame
* frame
, int size
)
255 frame_add_to_extra_tun (frame
, size
);
259 * Should ifconfig be called before or after
263 #define IFCONFIG_BEFORE_TUN_OPEN 0
264 #define IFCONFIG_AFTER_TUN_OPEN 1
266 #define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
271 #if defined(TARGET_LINUX)
272 return IFCONFIG_AFTER_TUN_OPEN
;
273 #elif defined(TARGET_SOLARIS)
274 return IFCONFIG_AFTER_TUN_OPEN
;
275 #elif defined(TARGET_OPENBSD)
276 return IFCONFIG_BEFORE_TUN_OPEN
;
277 #elif defined(TARGET_DARWIN)
278 return IFCONFIG_AFTER_TUN_OPEN
;
279 #elif defined(TARGET_NETBSD)
280 return IFCONFIG_AFTER_TUN_OPEN
;
282 return IFCONFIG_BEFORE_TUN_OPEN
;
284 return IFCONFIG_DEFAULT
;
290 #define TUN_PASS_BUFFER
295 struct tap_reg
*next
;
302 struct panel_reg
*next
;
305 int ascii2ipset (const char* name
);
306 const char *ipset2ascii (int index
);
307 const char *ipset2ascii_all (struct gc_arena
*gc
);
309 void verify_255_255_255_252 (in_addr_t local
, in_addr_t remote
);
311 const IP_ADAPTER_INFO
*get_adapter_info_list (struct gc_arena
*gc
);
312 const IP_ADAPTER_INFO
*get_tun_adapter (const struct tuntap
*tt
, const IP_ADAPTER_INFO
*list
);
314 const IP_ADAPTER_INFO
*get_adapter_info (DWORD index
, struct gc_arena
*gc
);
315 const IP_PER_ADAPTER_INFO
*get_per_adapter_info (const DWORD index
, struct gc_arena
*gc
);
316 const IP_ADAPTER_INFO
*get_adapter (const IP_ADAPTER_INFO
*ai
, DWORD index
);
318 bool is_adapter_up (const struct tuntap
*tt
, const IP_ADAPTER_INFO
*list
);
319 bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO
*ai
, const in_addr_t ip
, in_addr_t
*highest_netmask
);
321 DWORD
adapter_index_of_ip (const IP_ADAPTER_INFO
*list
,
326 void show_tap_win32_adapters (int msglev
, int warnlev
);
327 void show_adapters (int msglev
);
329 void tap_allow_nonadmin_access (const char *dev_node
);
331 void show_valid_win32_tun_subnets (void);
332 const char *tap_win32_getinfo (const struct tuntap
*tt
, struct gc_arena
*gc
);
333 void tun_show_debug (struct tuntap
*tt
);
335 bool dhcp_release_by_adapter_index(const DWORD adapter_index
);
336 bool dhcp_renew_by_adapter_index (const DWORD adapter_index
);
338 void tun_standby_init (struct tuntap
*tt
);
339 bool tun_standby (struct tuntap
*tt
);
341 int tun_read_queue (struct tuntap
*tt
, int maxsize
);
342 int tun_write_queue (struct tuntap
*tt
, struct buffer
*buf
);
343 int tun_finalize (HANDLE h
, struct overlapped_io
*io
, struct buffer
*buf
);
346 tuntap_stop (int status
)
349 * This corresponds to the STATUS_NO_SUCH_DEVICE
350 * error in tapdrvr.c.
354 return openvpn_errno () == ERROR_FILE_NOT_FOUND
;
360 tun_write_win32 (struct tuntap
*tt
, struct buffer
*buf
)
364 if (overlapped_io_active (&tt
->writes
))
366 status
= tun_finalize (tt
->hand
, &tt
->writes
, NULL
);
368 err
= GetLastError ();
370 tun_write_queue (tt
, buf
);
381 read_tun_buffered (struct tuntap
*tt
, struct buffer
*buf
, int maxsize
)
383 return tun_finalize (tt
->hand
, &tt
->reads
, buf
);
387 write_tun_buffered (struct tuntap
*tt
, struct buffer
*buf
)
389 return tun_write_win32 (tt
, buf
);
395 tuntap_stop (int status
)
401 tun_standby_init (struct tuntap
*tt
)
406 tun_standby (struct tuntap
*tt
)
414 * TUN/TAP I/O wait functions
417 static inline event_t
418 tun_event_handle (const struct tuntap
*tt
)
421 return &tt
->rw_handle
;
427 static inline unsigned int
428 tun_set (struct tuntap
*tt
,
429 struct event_set
*es
,
430 unsigned int rwflags
,
432 unsigned int *persistent
)
434 if (tuntap_defined (tt
))
436 /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
437 if (!persistent
|| *persistent
!= rwflags
)
439 event_ctl (es
, tun_event_handle (tt
), rwflags
, arg
);
441 *persistent
= rwflags
;
444 if (rwflags
& EVENT_READ
)
445 tun_read_queue (tt
, 0);
447 tt
->rwflags_debug
= rwflags
;
452 const char *tun_stat (const struct tuntap
*tt
, unsigned int rwflags
, struct gc_arena
*gc
);