OpenVPN
win32.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single 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#ifdef _WIN32
24#ifndef OPENVPN_WIN32_H
25#define OPENVPN_WIN32_H
26
27#include <iphlpapi.h>
28#include <ws2tcpip.h>
29
30#include "syshead.h"
31#include "mtu.h"
32#include "openvpn-msg.h"
33#include "argv.h"
34#include "win32-util.h"
35
36/* location of executables */
37#define SYS_PATH_ENV_VAR_NAME \
38 "SystemRoot" /* environmental variable name that normally contains the system path */
39#define NETSH_PATH_SUFFIX "\\system32\\netsh.exe"
40#define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
41#define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
42#define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
43#define POWERSHELL_PATH_SUFFIX "\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
44
45/*
46 * Win32-specific OpenVPN code, targeted at the mingw
47 * development environment.
48 */
49
50/* MSVC headers do not define this macro, so do it here */
51#ifndef IN6_ARE_ADDR_EQUAL
52#define IN6_ARE_ADDR_EQUAL(a, b) \
53 (memcmp((const void *)(a), (const void *)(b), sizeof(struct in6_addr)) == 0)
54#endif
55
56void init_win32(void);
57
58void uninit_win32(void);
59
60void set_pause_exit_win32(void);
61
63{
64 SECURITY_ATTRIBUTES sa;
65 SECURITY_DESCRIPTOR sd;
66 PACL dacl;
67};
68
69#define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
70
71/*
72 * Save old window title.
73 */
75{
76 bool saved;
78};
79
81{
82 HANDLE read;
83 HANDLE write;
84};
85
86/*
87 * Event-based notification of incoming TCP connections
88 */
89
90#define NE32_PERSIST_EVENT (1 << 0)
91#define NE32_WRITE_EVENT (1 << 1)
92
93static inline bool
95{
96 return event->read != NULL;
97}
98
99void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd,
100 unsigned int flags);
101
103
104void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
105
106/*
107 * A stateful variant of the net_event_win32 functions above
108 */
109
116
118
119void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
120
122
124
126
128
129static inline bool
131{
132 return defined_net_event_win32(&ne->handle);
133}
134
135static inline struct rw_handle *
137{
138 return &ne->handle;
139}
140
141static inline long
143{
144 return ne->event_mask;
145}
146
147static inline void
149{
150 ne->event_mask &= ~selected_events;
151}
152
153/*
154 * Signal handling
155 */
157{
158#define WSO_MODE_UNDEF 0
159#define WSO_MODE_SERVICE 1
160#define WSO_MODE_CONSOLE 2
161 int mode;
162 struct rw_handle in;
165};
166
167extern struct win32_signal win32_signal; /* static/global */
168extern struct window_title window_title; /* static/global */
169
170void win32_signal_clear(struct win32_signal *ws);
171
172/* win32_signal_open startup type */
173#define WSO_NOFORCE 0
174#define WSO_FORCE_SERVICE 1
175#define WSO_FORCE_CONSOLE 2
176
177void win32_signal_open(struct win32_signal *ws, int force, /* set to WSO force parm */
178 const char *exit_event_name, bool exit_event_initial_state);
179
180void win32_signal_close(struct win32_signal *ws);
181
182int win32_signal_get(struct win32_signal *ws);
183
184void win32_pause(struct win32_signal *ws);
185
187
188/*
189 * Set the text on the window title bar
190 */
191
192void window_title_clear(struct window_title *wt);
193
194void window_title_save(struct window_title *wt);
195
196void window_title_restore(const struct window_title *wt);
197
198void window_title_generate(const char *title);
199
200/*
201 * We try to do all Win32 I/O using overlapped
202 * (i.e. asynchronous) I/O for a performance win.
203 */
205{
206#define IOSTATE_INITIAL 0
207#define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
208#define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
210 OVERLAPPED overlapped;
211 DWORD size;
212 DWORD flags;
215 union
216 {
217 struct sockaddr_in addr;
218 struct sockaddr_in6 addr6;
219 };
222 struct buffer buf;
223};
224
225void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state);
226
228
229static inline bool
231{
232 return o->iostate == IOSTATE_QUEUED || o->iostate == IOSTATE_IMMEDIATE_RETURN;
233}
234
235char *overlapped_io_state_ascii(const struct overlapped_io *o);
236
237/*
238 * Use to control access to resources that only one
239 * OpenVPN process on a given machine can access at
240 * a given time.
241 */
242
244{
245 const char *name;
246 bool locked;
247 HANDLE hand;
248};
249
250void semaphore_clear(struct semaphore *s);
251
252void semaphore_open(struct semaphore *s, const char *name);
253
254bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
255
256void semaphore_release(struct semaphore *s);
257
258void semaphore_close(struct semaphore *s);
259
260/*
261 * Special global semaphore used to protect network
262 * shell commands from simultaneous instantiation.
263 *
264 * It seems you can't run more than one instance
265 * of netsh on the same machine at the same time.
266 *
267 * Its DACL is restricted to the creating user to prevent an unprivileged
268 * local user from starving it and DoS'ing running instances. This means
269 * different user accounts running OpenVPN directly, not via the interactive
270 * service, will make all but the first user's instances exit.
271 */
272
273extern struct semaphore netcmd_semaphore;
274void netcmd_semaphore_init(void);
275
276void netcmd_semaphore_close(void);
277
278void netcmd_semaphore_lock(void);
279
280void netcmd_semaphore_release(void);
281
282/* Set Win32 security attributes structure to allow all access */
284
285/* add constant environmental variables needed by Windows */
286struct env_set;
287
288/* get and set the current windows system path */
289void set_win_sys_path(const char *newpath, struct env_set *es);
290
292
293char *get_win_sys_path(void);
294
295/* call self in a subprocess */
296void fork_to_self(const char *cmdline);
297
298bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only);
299
300bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
301
308const char *win32_version_string(struct gc_arena *gc);
309
317bool send_msg_iservice(HANDLE pipe, const void *data, DWORD size, ack_message_t *ack,
318 const char *context);
319
320/*
321 * Attempt to simulate fork/execve on Windows
322 */
323int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
324
325/* Sleep that can be interrupted by signals and exit event */
326void win32_sleep(const int n);
327
336bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size);
337
351bool plugin_in_trusted_dir(const WCHAR *plugin_path);
352
361bool protect_buffer_win32(char *buf, DWORD len);
362
371bool unprotect_buffer_win32(char *buf, DWORD len);
372
373#endif /* ifndef OPENVPN_WIN32_H */
374#endif /* ifdef _WIN32 */
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
Contains all state information for one tunnel.
Definition openvpn.h:471
Packet geometry parameters.
Definition mtu.h:113
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
socket_descriptor_t sd
Definition win32.h:113
long event_mask
Definition win32.h:114
struct rw_handle handle
Definition win32.h:112
DWORD flags
Definition win32.h:212
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 addrlen
Definition win32.h:220
bool addr_defined
Definition win32.h:214
int iostate
Definition win32.h:209
struct sockaddr_in6 addr6
Definition win32.h:218
struct sockaddr_in addr
Definition win32.h:217
HANDLE write
Definition win32.h:83
HANDLE read
Definition win32.h:82
SECURITY_ATTRIBUTES sa
Definition win32.h:64
SECURITY_DESCRIPTOR sd
Definition win32.h:65
const char * name
Definition win32.h:245
bool locked
Definition win32.h:246
HANDLE hand
Definition win32.h:247
DWORD console_mode_save
Definition win32.h:163
struct rw_handle in
Definition win32.h:162
bool console_mode_save_defined
Definition win32.h:164
bool saved
Definition win32.h:76
char old_window_title[256]
Definition win32.h:77
SOCKET socket_descriptor_t
Definition syshead.h:445
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:122
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition win32.h:130
static bool defined_net_event_win32(const struct rw_handle *event)
Definition win32.h:94
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:315
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:419
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:470
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition win32.c:548
void uninit_win32(void)
Definition win32.c:122
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition win32.c:265
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition win32.c:1249
void window_title_generate(const char *title)
Definition win32.c:820
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:208
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition win32.c:1162
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:370
void window_title_clear(struct window_title *wt)
Definition win32.c:788
bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
Fetches a registry value for OpenVPN registry key.
Definition win32.c:1488
void window_title_save(struct window_title *wt)
Definition win32.c:794
bool protect_buffer_win32(char *buf, DWORD len)
Encrypt a region of memory using CryptProtectMemory() with access restricted to the current process.
Definition win32.c:1647
void semaphore_clear(struct semaphore *s)
Definition win32.c:837
bool plugin_in_trusted_dir(const WCHAR *plugin_path)
Checks if a plugin is located in a trusted directory.
Definition win32.c:1593
void semaphore_close(struct semaphore *s)
Definition win32.c:917
bool unprotect_buffer_win32(char *buf, DWORD len)
Decrypt a previously encrypted region of memory using CryptUnProtectMemory() with access restricted t...
Definition win32.c:1665
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:230
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:435
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition win32.c:868
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:426
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition win32.c:1298
void fork_to_self(const char *cmdline)
Definition win32.c:1114
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:294
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition win32.h:142
#define IOSTATE_QUEUED
Definition win32.h:207
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:281
void netcmd_semaphore_release(void)
Definition win32.c:965
void win32_sleep(const int n)
Definition win32.c:1549
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition win32.c:1041
void semaphore_release(struct semaphore *s)
Definition win32.c:902
void init_win32(void)
Definition win32.c:109
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1170
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:355
void window_title_restore(const struct window_title *wt)
Definition win32.c:811
struct semaphore netcmd_semaphore
Definition win32.c:96
char * get_win_sys_path(void)
Definition win32.c:1155
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:716
void win32_pause(struct win32_signal *ws)
Definition win32.c:773
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition win32.h:148
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition win32.h:136
void netcmd_semaphore_init(void)
Definition win32.c:937
void netcmd_semaphore_lock(void)
Definition win32.c:949
void win32_signal_close(struct win32_signal *ws)
Definition win32.c:696
const char * win32_version_string(struct gc_arena *gc)
Get Windows version string with architecture info.
Definition win32.c:1427
void win32_signal_clear(struct win32_signal *ws)
Definition win32.c:542
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:1469
int win32_signal_get(struct win32_signal *ws)
Definition win32.c:729
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:453
void netcmd_semaphore_close(void)
Definition win32.c:943
void set_pause_exit_win32(void)
Definition win32.c:146
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:159
void semaphore_open(struct semaphore *s, const char *name)
Definition win32.c:843
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:459