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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifdef _WIN32
25#ifndef OPENVPN_WIN32_H
26#define OPENVPN_WIN32_H
27
28#include <iphlpapi.h>
29#include <ws2tcpip.h>
30
31#include "syshead.h"
32#include "mtu.h"
33#include "openvpn-msg.h"
34#include "argv.h"
35#include "win32-util.h"
36
37/* location of executables */
38#define SYS_PATH_ENV_VAR_NAME "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
79struct rw_handle {
80 HANDLE read;
81 HANDLE write;
82};
83
84/*
85 * Event-based notification of incoming TCP connections
86 */
87
88#define NE32_PERSIST_EVENT (1<<0)
89#define NE32_WRITE_EVENT (1<<1)
90
91static inline bool
93{
94 return event->read != NULL;
95}
96
97void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags);
98
100
101void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
102
103/*
104 * A stateful variant of the net_event_win32 functions above
105 */
106
113
115
116void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
117
119
121
123
125
126static inline bool
128{
129 return defined_net_event_win32(&ne->handle);
130}
131
132static inline struct rw_handle *
134{
135 return &ne->handle;
136}
137
138static inline long
140{
141 return ne->event_mask;
142}
143
144static inline void
146{
147 ne->event_mask &= ~selected_events;
148}
149
150/*
151 * Signal handling
152 */
154#define WSO_MODE_UNDEF 0
155#define WSO_MODE_SERVICE 1
156#define WSO_MODE_CONSOLE 2
157 int mode;
158 struct rw_handle in;
161};
162
163extern struct win32_signal win32_signal; /* static/global */
164extern struct window_title window_title; /* static/global */
165
166void win32_signal_clear(struct win32_signal *ws);
167
168/* win32_signal_open startup type */
169#define WSO_NOFORCE 0
170#define WSO_FORCE_SERVICE 1
171#define WSO_FORCE_CONSOLE 2
172
173void win32_signal_open(struct win32_signal *ws,
174 int force, /* set to WSO force parm */
175 const char *exit_event_name,
176 bool exit_event_initial_state);
177
178void win32_signal_close(struct win32_signal *ws);
179
180int win32_signal_get(struct win32_signal *ws);
181
182void win32_pause(struct win32_signal *ws);
183
185
186/*
187 * Set the text on the window title bar
188 */
189
190void window_title_clear(struct window_title *wt);
191
192void window_title_save(struct window_title *wt);
193
194void window_title_restore(const struct window_title *wt);
195
196void window_title_generate(const char *title);
197
198/*
199 * We try to do all Win32 I/O using overlapped
200 * (i.e. asynchronous) I/O for a performance win.
201 */
203#define IOSTATE_INITIAL 0
204#define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
205#define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
207 OVERLAPPED overlapped;
208 DWORD size;
209 DWORD flags;
212 union {
213 struct sockaddr_in addr;
214 struct sockaddr_in6 addr6;
215 };
218 struct buffer buf;
219};
220
222 const struct frame *frame,
224
226
227static inline bool
229{
230 return o->iostate == IOSTATE_QUEUED || o->iostate == IOSTATE_IMMEDIATE_RETURN;
231}
232
233char *overlapped_io_state_ascii(const struct overlapped_io *o);
234
235/*
236 * Use to control access to resources that only one
237 * OpenVPN process on a given machine can access at
238 * a given time.
239 */
240
242{
243 const char *name;
244 bool locked;
245 HANDLE hand;
246};
247
248void semaphore_clear(struct semaphore *s);
249
250void semaphore_open(struct semaphore *s, const char *name);
251
252bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
253
254void semaphore_release(struct semaphore *s);
255
256void semaphore_close(struct semaphore *s);
257
258/*
259 * Special global semaphore used to protect network
260 * shell commands from simultaneous instantiation.
261 *
262 * It seems you can't run more than one instance
263 * of netsh on the same machine at the same time.
264 */
265
266extern struct semaphore netcmd_semaphore;
267void netcmd_semaphore_init(void);
268
269void netcmd_semaphore_close(void);
270
271void netcmd_semaphore_lock(void);
272
273void netcmd_semaphore_release(void);
274
275/* Set Win32 security attributes structure to allow all access */
277
278/* add constant environmental variables needed by Windows */
279struct env_set;
280
281/* get and set the current windows system path */
282void set_win_sys_path(const char *newpath, struct env_set *es);
283
285
286char *get_win_sys_path(void);
287
288/* call self in a subprocess */
289void fork_to_self(const char *cmdline);
290
291bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only);
292
293bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
294
301const char *win32_version_string(struct gc_arena *gc);
302
303/*
304 * Send the |size| bytes in buffer |data| to the interactive service |pipe|
305 * and read the result in |ack|. Returns false on communication error.
306 * The string in |context| is used to prefix error messages.
307 */
308bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
309 ack_message_t *ack, const char *context);
310
311/*
312 * Attempt to simulate fork/execve on Windows
313 */
314int
315openvpn_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
329get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size);
330
344bool
345plugin_in_trusted_dir(const WCHAR *plugin_path);
346
355bool
356protect_buffer_win32(char *buf, size_t len);
357
366bool
367unprotect_buffer_win32(char *buf, size_t len);
368
369#endif /* ifndef OPENVPN_WIN32_H */
370#endif /* ifdef _WIN32 */
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
Contains all state information for one tunnel.
Definition openvpn.h:476
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
socket_descriptor_t sd
Definition win32.h:110
long event_mask
Definition win32.h:111
struct rw_handle handle
Definition win32.h:109
DWORD flags
Definition win32.h:209
struct buffer buf
Definition win32.h:218
DWORD size
Definition win32.h:208
OVERLAPPED overlapped
Definition win32.h:207
struct buffer buf_init
Definition win32.h:217
int addrlen
Definition win32.h:216
bool addr_defined
Definition win32.h:211
int iostate
Definition win32.h:206
struct sockaddr_in6 addr6
Definition win32.h:214
struct sockaddr_in addr
Definition win32.h:213
HANDLE write
Definition win32.h:81
HANDLE read
Definition win32.h:80
SECURITY_ATTRIBUTES sa
Definition win32.h:64
SECURITY_DESCRIPTOR sd
Definition win32.h:65
const char * name
Definition win32.h:243
bool locked
Definition win32.h:244
HANDLE hand
Definition win32.h:245
DWORD console_mode_save
Definition win32.h:159
struct rw_handle in
Definition win32.h:158
bool console_mode_save_defined
Definition win32.h:160
bool saved
Definition win32.h:75
char old_window_title[256]
Definition win32.h:76
SOCKET socket_descriptor_t
Definition syshead.h:439
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:155
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition win32.h:127
static bool defined_net_event_win32(const struct rw_handle *event)
Definition win32.h:92
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:223
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:324
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:375
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition win32.c:453
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:171
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition win32.c:1211
void window_title_generate(const char *title)
Definition win32.c:730
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:205
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition win32.c:1120
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:277
void window_title_clear(struct window_title *wt)
Definition win32.c:698
bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
Fetches a registry value for OpenVPN registry key.
Definition win32.c:1453
void window_title_save(struct window_title *wt)
Definition win32.c:704
void semaphore_clear(struct semaphore *s)
Definition win32.c:746
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:1632
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:1614
bool plugin_in_trusted_dir(const WCHAR *plugin_path)
Checks if a plugin is located in a trusted directory.
Definition win32.c:1559
void semaphore_close(struct semaphore *s)
Definition win32.c:826
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:228
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:340
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition win32.c:776
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:331
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition win32.c:1260
void fork_to_self(const char *cmdline)
Definition win32.c:1073
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:202
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition win32.h:139
#define IOSTATE_QUEUED
Definition win32.h:204
bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context)
Definition win32.c:1432
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:189
void netcmd_semaphore_release(void)
Definition win32.c:874
void win32_sleep(const int n)
Definition win32.c:1515
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition win32.c:1001
void semaphore_release(struct semaphore *s)
Definition win32.c:810
void init_win32(void)
Definition win32.c:109
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1128
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:262
void window_title_restore(const struct window_title *wt)
Definition win32.c:721
struct semaphore netcmd_semaphore
Definition win32.c:95
char * get_win_sys_path(void)
Definition win32.c:1113
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:625
void win32_pause(struct win32_signal *ws)
Definition win32.c:683
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition win32.h:145
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition win32.h:133
void netcmd_semaphore_init(void)
Definition win32.c:846
void netcmd_semaphore_lock(void)
Definition win32.c:858
void win32_signal_close(struct win32_signal *ws)
Definition win32.c:605
const char * win32_version_string(struct gc_arena *gc)
Get Windows version string with architecture info.
Definition win32.c:1390
void win32_signal_clear(struct win32_signal *ws)
Definition win32.c:447
int win32_signal_get(struct win32_signal *ws)
Definition win32.c:639
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:358
void netcmd_semaphore_close(void)
Definition win32.c:852
void set_pause_exit_win32(void)
Definition win32.c:146
bool init_security_attributes_allow_all(struct security_attributes *obj)
Definition win32.c:152
void semaphore_open(struct semaphore *s, const char *name)
Definition win32.c:752
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:364