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-2010 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-windows.h>
43
#define TUN_ADAPTER_INDEX_INVALID ((DWORD)-1)
45
/* time constants for --ip-win32 adaptive */
46
#define IPW32_SET_ADAPTIVE_DELAY_WINDOW 300
47
#define IPW32_SET_ADAPTIVE_TRY_NETSH 20
49
struct tuntap_options {
50
/* --ip-win32 options */
51
bool ip_win32_defined;
53
# define IPW32_SET_MANUAL 0 /* "--ip-win32 manual" */
54
# define IPW32_SET_NETSH 1 /* "--ip-win32 netsh" */
55
# define IPW32_SET_IPAPI 2 /* "--ip-win32 ipapi" */
56
# define IPW32_SET_DHCP_MASQ 3 /* "--ip-win32 dynamic" */
57
# define IPW32_SET_ADAPTIVE 4 /* "--ip-win32 adaptive" */
58
# define IPW32_SET_N 5
61
/* --ip-win32 dynamic options */
62
bool dhcp_masq_custom_offset;
66
/* --tap-sleep option */
69
/* --dhcp-option options */
73
const char *domain; /* DOMAIN (15) */
75
const char *netbios_scope; /* NBS (47) */
77
int netbios_node_type; /* NBT 1,2,4,8 (46) */
79
#define N_DHCP_ADDR 4 /* Max # of addresses allowed for
83
in_addr_t dns[N_DHCP_ADDR];
87
in_addr_t wins[N_DHCP_ADDR];
91
in_addr_t ntp[N_DHCP_ADDR];
95
in_addr_t nbdd[N_DHCP_ADDR];
98
/* DISABLE_NBT (43, Vendor option 001) */
102
bool dhcp_pre_release;
110
struct tuntap_options {
116
struct tuntap_options {
117
int dummy; /* not used */
123
* Define a TUN/TAP dev.
128
# define TUNNEL_TYPE(tt) ((tt) ? ((tt)->type) : DEV_TYPE_UNDEF)
129
int type; /* DEV_TYPE_x as defined in proto.h */
131
# define TUNNEL_TOPOLOGY(tt) ((tt) ? ((tt)->topology) : TOP_UNDEF)
132
int topology; /* one of the TOP_x values */
134
bool did_ifconfig_setup;
135
bool did_ifconfig_ipv6_setup;
140
bool persistent_if; /* if existed before, keep on program end */
142
struct tuntap_options options; /* options set on command line */
144
char *actual_name; /* actual name of TUN/TAP dev, usually including unit number */
146
/* number of TX buffers */
149
/* ifconfig parameters */
151
in_addr_t remote_netmask;
154
struct in6_addr local_ipv6;
155
struct in6_addr remote_ipv6;
160
struct overlapped_io reads;
161
struct overlapped_io writes;
162
struct rw_handle rw_handle;
164
/* used for setting interface address via IP Helper API
165
or DHCP masquerade */
166
bool ipapi_context_defined;
168
ULONG ipapi_instance;
169
in_addr_t adapter_netmask;
171
/* Windows adapter index for TAP-Windows adapter,
177
int fd; /* file descriptor for TUN/TAP dev */
180
#ifdef TARGET_SOLARIS
184
/* used for printing status info only */
185
unsigned int rwflags_debug;
187
/* Some TUN/TAP drivers like to be ioctled for mtu
193
tuntap_defined (const struct tuntap *tt)
196
return tt && tt->hand != NULL;
198
return tt && tt->fd >= 0;
203
* Function prototypes
206
void open_tun (const char *dev, const char *dev_type, const char *dev_node,
209
void close_tun (struct tuntap *tt);
211
int write_tun (struct tuntap* tt, uint8_t *buf, int len);
213
int read_tun (struct tuntap* tt, uint8_t *buf, int len);
215
void tuncfg (const char *dev, const char *dev_type, const char *dev_node,
216
int persist_mode, const char *username,
217
const char *groupname, const struct tuntap_options *options);
219
const char *guess_tuntap_dev (const char *dev,
220
const char *dev_type,
221
const char *dev_node,
222
struct gc_arena *gc);
224
struct tuntap *init_tun (const char *dev, /* --dev option */
225
const char *dev_type, /* --dev-type option */
226
int topology, /* one of the TOP_x values */
227
const char *ifconfig_local_parm, /* --ifconfig parm 1 */
228
const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
229
const char *ifconfig_ipv6_local_parm, /* --ifconfig parm 1 / IPv6 */
230
int ifconfig_ipv6_netbits_parm, /* --ifconfig parm 1 / bits */
231
const char *ifconfig_ipv6_remote_parm, /* --ifconfig parm 2 / IPv6 */
232
in_addr_t local_public,
233
in_addr_t remote_public,
234
const bool strict_warn,
237
void init_tun_post (struct tuntap *tt,
238
const struct frame *frame,
239
const struct tuntap_options *options);
241
void do_ifconfig (struct tuntap *tt,
242
const char *actual, /* actual device name */
244
const struct env_set *es);
246
bool is_dev_type (const char *dev, const char *dev_type, const char *match_type);
247
int dev_type_enum (const char *dev, const char *dev_type);
248
const char *dev_type_string (const char *dev, const char *dev_type);
250
const char *ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc);
252
bool is_tun_p2p (const struct tuntap *tt);
254
void check_subnet_conflict (const in_addr_t ip,
255
const in_addr_t netmask,
258
void warn_on_use_of_common_subnets (void);
265
tun_adjust_frame_parameters (struct frame* frame, int size)
267
frame_add_to_extra_tun (frame, size);
271
* Should ifconfig be called before or after
275
#define IFCONFIG_BEFORE_TUN_OPEN 0
276
#define IFCONFIG_AFTER_TUN_OPEN 1
278
#define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
283
#if defined(TARGET_LINUX)
284
return IFCONFIG_AFTER_TUN_OPEN;
285
#elif defined(TARGET_SOLARIS)
286
return IFCONFIG_AFTER_TUN_OPEN;
287
#elif defined(TARGET_OPENBSD)
288
return IFCONFIG_AFTER_TUN_OPEN;
289
#elif defined(TARGET_DARWIN)
290
return IFCONFIG_AFTER_TUN_OPEN;
291
#elif defined(TARGET_NETBSD)
292
return IFCONFIG_AFTER_TUN_OPEN;
294
return IFCONFIG_BEFORE_TUN_OPEN;
296
return IFCONFIG_DEFAULT;
302
#define TUN_PASS_BUFFER
307
struct tap_reg *next;
314
struct panel_reg *next;
317
int ascii2ipset (const char* name);
318
const char *ipset2ascii (int index);
319
const char *ipset2ascii_all (struct gc_arena *gc);
321
void verify_255_255_255_252 (in_addr_t local, in_addr_t remote);
323
const IP_ADAPTER_INFO *get_adapter_info_list (struct gc_arena *gc);
324
const IP_ADAPTER_INFO *get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
326
const IP_ADAPTER_INFO *get_adapter_info (DWORD index, struct gc_arena *gc);
327
const IP_PER_ADAPTER_INFO *get_per_adapter_info (const DWORD index, struct gc_arena *gc);
328
const IP_ADAPTER_INFO *get_adapter (const IP_ADAPTER_INFO *ai, DWORD index);
330
bool is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
331
bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask);
333
DWORD adapter_index_of_ip (const IP_ADAPTER_INFO *list,
338
void show_tap_win_adapters (int msglev, int warnlev);
339
void show_adapters (int msglev);
341
void tap_allow_nonadmin_access (const char *dev_node);
343
void show_valid_win32_tun_subnets (void);
344
const char *tap_win_getinfo (const struct tuntap *tt, struct gc_arena *gc);
345
void tun_show_debug (struct tuntap *tt);
347
bool dhcp_release_by_adapter_index(const DWORD adapter_index);
348
bool dhcp_renew_by_adapter_index (const DWORD adapter_index);
350
void fork_register_dns_action (struct tuntap *tt);
351
void ipconfig_register_dns (const struct env_set *es);
353
void tun_standby_init (struct tuntap *tt);
354
bool tun_standby (struct tuntap *tt);
356
int tun_read_queue (struct tuntap *tt, int maxsize);
357
int tun_write_queue (struct tuntap *tt, struct buffer *buf);
358
int tun_finalize (HANDLE h, struct overlapped_io *io, struct buffer *buf);
361
tuntap_stop (int status)
364
* This corresponds to the STATUS_NO_SUCH_DEVICE
365
* error in tapdrvr.c.
369
return openvpn_errno () == ERROR_FILE_NOT_FOUND;
375
tun_write_win32 (struct tuntap *tt, struct buffer *buf)
379
if (overlapped_io_active (&tt->writes))
381
status = tun_finalize (tt->hand, &tt->writes, NULL);
383
err = GetLastError ();
385
tun_write_queue (tt, buf);
396
read_tun_buffered (struct tuntap *tt, struct buffer *buf, int maxsize)
398
return tun_finalize (tt->hand, &tt->reads, buf);
402
write_tun_buffered (struct tuntap *tt, struct buffer *buf)
404
return tun_write_win32 (tt, buf);
410
tuntap_stop (int status)
416
tun_standby_init (struct tuntap *tt)
421
tun_standby (struct tuntap *tt)
429
* TUN/TAP I/O wait functions
432
static inline event_t
433
tun_event_handle (const struct tuntap *tt)
436
return &tt->rw_handle;
442
static inline unsigned int
443
tun_set (struct tuntap *tt,
444
struct event_set *es,
445
unsigned int rwflags,
447
unsigned int *persistent)
449
if (tuntap_defined (tt))
451
/* if persistent is defined, call event_ctl only if rwflags has changed since last call */
452
if (!persistent || *persistent != rwflags)
454
event_ctl (es, tun_event_handle (tt), rwflags, arg);
456
*persistent = rwflags;
459
if (rwflags & EVENT_READ)
460
tun_read_queue (tt, 0);
462
tt->rwflags_debug = rwflags;
467
const char *tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);