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-2025 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 WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.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};
67
68#define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
69
70/*
71 * Save old window title.
72 */
74{
75 bool saved;
77};
78
80{
81 HANDLE read;
82 HANDLE write;
83};
84
85/*
86 * Event-based notification of incoming TCP connections
87 */
88
89#define NE32_PERSIST_EVENT (1 << 0)
90#define NE32_WRITE_EVENT (1 << 1)
91
92static inline bool
94{
95 return event->read != NULL;
96}
97
98void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd,
99 unsigned int flags);
100
102
103void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
104
105/*
106 * A stateful variant of the net_event_win32 functions above
107 */
108
115
117
118void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
119
121
123
125
127
128static inline bool
130{
131 return defined_net_event_win32(&ne->handle);
132}
133
134static inline struct rw_handle *
136{
137 return &ne->handle;
138}
139
140static inline long
142{
143 return ne->event_mask;
144}
145
146static inline void
148{
149 ne->event_mask &= ~selected_events;
150}
151
152/*
153 * Signal handling
154 */
156{
157#define WSO_MODE_UNDEF 0
158#define WSO_MODE_SERVICE 1
159#define WSO_MODE_CONSOLE 2
160 int mode;
161 struct rw_handle in;
164};
165
166extern struct win32_signal win32_signal; /* static/global */
167extern struct window_title window_title; /* static/global */
168
169void win32_signal_clear(struct win32_signal *ws);
170
171/* win32_signal_open startup type */
172#define WSO_NOFORCE 0
173#define WSO_FORCE_SERVICE 1
174#define WSO_FORCE_CONSOLE 2
175
176void win32_signal_open(struct win32_signal *ws, int force, /* set to WSO force parm */
177 const char *exit_event_name, bool exit_event_initial_state);
178
179void win32_signal_close(struct win32_signal *ws);
180
181int win32_signal_get(struct win32_signal *ws);
182
183void win32_pause(struct win32_signal *ws);
184
186
187/*
188 * Set the text on the window title bar
189 */
190
191void window_title_clear(struct window_title *wt);
192
193void window_title_save(struct window_title *wt);
194
195void window_title_restore(const struct window_title *wt);
196
197void window_title_generate(const char *title);
198
199/*
200 * We try to do all Win32 I/O using overlapped
201 * (i.e. asynchronous) I/O for a performance win.
202 */
204{
205#define IOSTATE_INITIAL 0
206#define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
207#define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
209 OVERLAPPED overlapped;
210 DWORD size;
211 DWORD flags;
214 union
215 {
216 struct sockaddr_in addr;
217 struct sockaddr_in6 addr6;
218 };
221 struct buffer buf;
222};
223
224void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state);
225
227
228static inline bool
230{
231 return o->iostate == IOSTATE_QUEUED || o->iostate == IOSTATE_IMMEDIATE_RETURN;
232}
233
234char *overlapped_io_state_ascii(const struct overlapped_io *o);
235
236/*
237 * Use to control access to resources that only one
238 * OpenVPN process on a given machine can access at
239 * a given time.
240 */
241
243{
244 const char *name;
245 bool locked;
246 HANDLE hand;
247};
248
249void semaphore_clear(struct semaphore *s);
250
251void semaphore_open(struct semaphore *s, const char *name);
252
253bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
254
255void semaphore_release(struct semaphore *s);
256
257void semaphore_close(struct semaphore *s);
258
259/*
260 * Special global semaphore used to protect network
261 * shell commands from simultaneous instantiation.
262 *
263 * It seems you can't run more than one instance
264 * of netsh on the same machine at the same time.
265 */
266
267extern struct semaphore netcmd_semaphore;
268void netcmd_semaphore_init(void);
269
270void netcmd_semaphore_close(void);
271
272void netcmd_semaphore_lock(void);
273
274void netcmd_semaphore_release(void);
275
276/* Set Win32 security attributes structure to allow all access */
278
279/* add constant environmental variables needed by Windows */
280struct env_set;
281
282/* get and set the current windows system path */
283void set_win_sys_path(const char *newpath, struct env_set *es);
284
286
287char *get_win_sys_path(void);
288
289/* call self in a subprocess */
290void fork_to_self(const char *cmdline);
291
292bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only);
293
294bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
295
302const char *win32_version_string(struct gc_arena *gc);
303
304/*
305 * Send the |size| bytes in buffer |data| to the interactive service |pipe|
306 * and read the result in |ack|. Returns false on communication error.
307 * The string in |context| is used to prefix error messages.
308 */
309bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack,
310 const char *context);
311
312/*
313 * Attempt to simulate fork/execve on Windows
314 */
315int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
316
317/* Sleep that can be interrupted by signals and exit event */
318void win32_sleep(const int n);
319
328bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size);
329
343bool plugin_in_trusted_dir(const WCHAR *plugin_path);
344
353bool protect_buffer_win32(char *buf, size_t len);
354
363bool unprotect_buffer_win32(char *buf, size_t len);
364
365#endif /* ifndef OPENVPN_WIN32_H */
366#endif /* ifdef _WIN32 */
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Contains all state information for one tunnel.
Definition openvpn.h:474
Packet geometry parameters.
Definition mtu.h:103
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
socket_descriptor_t sd
Definition win32.h:112
long event_mask
Definition win32.h:113
struct rw_handle handle
Definition win32.h:111
DWORD flags
Definition win32.h:211
struct buffer buf
Definition win32.h:221
DWORD size
Definition win32.h:210
OVERLAPPED overlapped
Definition win32.h:209
struct buffer buf_init
Definition win32.h:220
int addrlen
Definition win32.h:219
bool addr_defined
Definition win32.h:213
int iostate
Definition win32.h:208
struct sockaddr_in6 addr6
Definition win32.h:217
struct sockaddr_in addr
Definition win32.h:216
HANDLE write
Definition win32.h:82
HANDLE read
Definition win32.h:81
SECURITY_ATTRIBUTES sa
Definition win32.h:64
SECURITY_DESCRIPTOR sd
Definition win32.h:65
const char * name
Definition win32.h:244
bool locked
Definition win32.h:245
HANDLE hand
Definition win32.h:246
DWORD console_mode_save
Definition win32.h:162
struct rw_handle in
Definition win32.h:161
bool console_mode_save_defined
Definition win32.h:163
bool saved
Definition win32.h:75
char old_window_title[256]
Definition win32.h:76
SOCKET socket_descriptor_t
Definition syshead.h:440
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:154
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition win32.h:129
static bool defined_net_event_win32(const struct rw_handle *event)
Definition win32.h:93
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:219
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:323
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:374
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition win32.c:452
void uninit_win32(void)
Definition win32.c:120
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition win32.c:169
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition win32.c:1202
void window_title_generate(const char *title)
Definition win32.c:723
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:207
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition win32.c:1115
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:274
void window_title_clear(struct window_title *wt)
Definition win32.c:691
bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
Fetches a registry value for OpenVPN registry key.
Definition win32.c:1441
void window_title_save(struct window_title *wt)
Definition win32.c:697
void semaphore_clear(struct semaphore *s)
Definition win32.c:740
bool unprotect_buffer_win32(char *buf, size_t len)
Decrypt a previously encrypted region of memory using CryptUnProtectMemory() with access restricted t...
Definition win32.c:1618
bool protect_buffer_win32(char *buf, size_t len)
Encrypt a region of memory using CryptProtectMemory() with access restricted to the current process.
Definition win32.c:1600
bool plugin_in_trusted_dir(const WCHAR *plugin_path)
Checks if a plugin is located in a trusted directory.
Definition win32.c:1545
void semaphore_close(struct semaphore *s)
Definition win32.c:819
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:229
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:339
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition win32.c:770
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:330
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition win32.c:1251
void fork_to_self(const char *cmdline)
Definition win32.c:1067
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:198
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition win32.h:141
#define IOSTATE_QUEUED
Definition win32.h:206
bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context)
Definition win32.c:1422
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:185
void netcmd_semaphore_release(void)
Definition win32.c:867
void win32_sleep(const int n)
Definition win32.c:1502
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition win32.c:994
void semaphore_release(struct semaphore *s)
Definition win32.c:804
void init_win32(void)
Definition win32.c:107
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1123
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:259
void window_title_restore(const struct window_title *wt)
Definition win32.c:714
struct semaphore netcmd_semaphore
Definition win32.c:94
char * get_win_sys_path(void)
Definition win32.c:1108
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:619
void win32_pause(struct win32_signal *ws)
Definition win32.c:676
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition win32.h:147
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition win32.h:135
void netcmd_semaphore_init(void)
Definition win32.c:839
void netcmd_semaphore_lock(void)
Definition win32.c:851
void win32_signal_close(struct win32_signal *ws)
Definition win32.c:599
const char * win32_version_string(struct gc_arena *gc)
Get Windows version string with architecture info.
Definition win32.c:1380
void win32_signal_clear(struct win32_signal *ws)
Definition win32.c:446
int win32_signal_get(struct win32_signal *ws)
Definition win32.c:632
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:357
void netcmd_semaphore_close(void)
Definition win32.c:845
void set_pause_exit_win32(void)
Definition win32.c:144
bool init_security_attributes_allow_all(struct security_attributes *obj)
Definition win32.c:150
void semaphore_open(struct semaphore *s, const char *name)
Definition win32.c:746
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:363