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 
56 void init_win32(void);
57 
58 void uninit_win32(void);
59 
60 void 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;
76  char old_window_title[256];
77 };
78 
79 struct 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 
91 static inline bool
92 defined_net_event_win32(const struct rw_handle *event)
93 {
94  return event->read != NULL;
95 }
96 
97 void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags);
98 
100 
101 void 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 
108 {
112 };
113 
114 void net_event_win32_init(struct net_event_win32 *ne);
115 
116 void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
117 
118 void net_event_win32_reset(struct net_event_win32 *ne);
119 
121 
122 void net_event_win32_stop(struct net_event_win32 *ne);
123 
124 void net_event_win32_close(struct net_event_win32 *ne);
125 
126 static inline bool
128 {
129  return defined_net_event_win32(&ne->handle);
130 }
131 
132 static inline struct rw_handle *
134 {
135  return &ne->handle;
136 }
137 
138 static inline long
140 {
141  return ne->event_mask;
142 }
143 
144 static inline void
146 {
147  ne->event_mask &= ~selected_events;
148 }
149 
150 /*
151  * Signal handling
152  */
153 struct win32_signal {
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 
163 extern struct win32_signal win32_signal; /* static/global */
164 extern struct window_title window_title; /* static/global */
165 
166 void 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 
173 void 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 
178 void win32_signal_close(struct win32_signal *ws);
179 
180 int win32_signal_get(struct win32_signal *ws);
181 
182 void win32_pause(struct win32_signal *ws);
183 
184 bool win32_service_interrupt(struct win32_signal *ws);
185 
186 /*
187  * Set the text on the window title bar
188  */
189 
190 void window_title_clear(struct window_title *wt);
191 
192 void window_title_save(struct window_title *wt);
193 
194 void window_title_restore(const struct window_title *wt);
195 
196 void 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 */
206  int iostate;
207  OVERLAPPED overlapped;
208  DWORD size;
209  DWORD flags;
210  int status;
212  union {
213  struct sockaddr_in addr;
214  struct sockaddr_in6 addr6;
215  };
216  int addrlen;
217  struct buffer buf_init;
218  struct buffer buf;
219 };
220 
221 void overlapped_io_init(struct overlapped_io *o,
222  const struct frame *frame,
223  BOOL event_state);
224 
225 void overlapped_io_close(struct overlapped_io *o);
226 
227 static inline bool
229 {
231 }
232 
233 char *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 
241 struct semaphore
242 {
243  const char *name;
244  bool locked;
245  HANDLE hand;
246 };
247 
248 void semaphore_clear(struct semaphore *s);
249 
250 void semaphore_open(struct semaphore *s, const char *name);
251 
252 bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
253 
254 void semaphore_release(struct semaphore *s);
255 
256 void 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 
266 extern struct semaphore netcmd_semaphore;
267 void netcmd_semaphore_init(void);
268 
269 void netcmd_semaphore_close(void);
270 
271 void netcmd_semaphore_lock(void);
272 
273 void netcmd_semaphore_release(void);
274 
275 /* Set Win32 security attributes structure to allow all access */
277 
278 /* add constant environmental variables needed by Windows */
279 struct env_set;
280 
281 /* get and set the current windows system path */
282 void set_win_sys_path(const char *newpath, struct env_set *es);
283 
284 void set_win_sys_path_via_env(struct env_set *es);
285 
286 char *get_win_sys_path(void);
287 
288 /* call self in a subprocess */
289 void fork_to_self(const char *cmdline);
290 
291 bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only);
292 
293 bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
294 
295 #define WIN_XP 0
296 #define WIN_VISTA 1
297 #define WIN_7 2
298 #define WIN_8 3
299 #define WIN_8_1 4
300 #define WIN_10 5
301 
302 int win32_version_info(void);
303 
304 /*
305  * String representation of Windows version number and name, see
306  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
307  */
308 const char *win32_version_string(struct gc_arena *gc, bool add_name);
309 
310 /*
311  * Send the |size| bytes in buffer |data| to the interactive service |pipe|
312  * and read the result in |ack|. Returns false on communication error.
313  * The string in |context| is used to prefix error messages.
314  */
315 bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
316  ack_message_t *ack, const char *context);
317 
318 /*
319  * Attempt to simulate fork/execve on Windows
320  */
321 int
322 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
323 
324 /* Sleep that can be interrupted by signals and exit event */
325 void win32_sleep(const int n);
326 
335 bool
336 get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size);
337 
351 bool
352 plugin_in_trusted_dir(const WCHAR *plugin_path);
353 
354 #endif /* ifndef OPENVPN_WIN32_H */
355 #endif /* ifdef _WIN32 */
overlapped_io_active
static bool overlapped_io_active(struct overlapped_io *o)
Definition: win32.h:228
get_win_sys_path
char * get_win_sys_path(void)
Definition: win32.c:1113
netcmd_semaphore_release
void netcmd_semaphore_release(void)
Definition: win32.c:874
rw_handle::read
HANDLE read
Definition: win32.h:80
security_attributes
Definition: win32.h:62
env_set::gc
struct gc_arena * gc
Definition: env_set.h:43
net_event_win32_reset
void net_event_win32_reset(struct net_event_win32 *ne)
Definition: win32.c:358
overlapped_io::buf
struct buffer buf
Definition: win32.h:218
win32_signal::in
struct rw_handle in
Definition: win32.h:158
netcmd_semaphore_init
void netcmd_semaphore_init(void)
Definition: win32.c:846
net_event_win32_close
void net_event_win32_close(struct net_event_win32 *ne)
Definition: win32.c:375
net_event_win32::sd
socket_descriptor_t sd
Definition: win32.h:110
reset_net_event_win32
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition: win32.c:262
window_title::saved
bool saved
Definition: win32.h:75
overlapped_io::addr_defined
bool addr_defined
Definition: win32.h:211
win32_signal_clear
void win32_signal_clear(struct win32_signal *ws)
Definition: win32.c:447
argv
Definition: argv.h:35
window_title_save
void window_title_save(struct window_title *wt)
Definition: win32.c:704
semaphore_release
void semaphore_release(struct semaphore *s)
Definition: win32.c:810
netcmd_semaphore_close
void netcmd_semaphore_close(void)
Definition: win32.c:852
overlapped_io_close
void overlapped_io_close(struct overlapped_io *o)
Definition: win32.c:189
context
Contains all state information for one tunnel.
Definition: openvpn.h:476
es
struct env_set * es
Definition: test_pkcs11.c:133
overlapped_io::addrlen
int addrlen
Definition: win32.h:216
win32_signal_close
void win32_signal_close(struct win32_signal *ws)
Definition: win32.c:605
fork_to_self
void fork_to_self(const char *cmdline)
Definition: win32.c:1073
overlapped_io::addr
struct sockaddr_in addr
Definition: win32.h:213
net_event_win32_stop
void net_event_win32_stop(struct net_event_win32 *ne)
Definition: win32.c:364
ack_message_t
Definition: openvpn-msg.h:124
semaphore
Definition: win32.h:241
close_net_event_win32
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:277
window_title
Definition: win32.h:73
netcmd_semaphore_lock
void netcmd_semaphore_lock(void)
Definition: win32.c:858
overlapped_io::flags
DWORD flags
Definition: win32.h:209
frame
Packet geometry parameters.
Definition: mtu.h:98
IOSTATE_IMMEDIATE_RETURN
#define IOSTATE_IMMEDIATE_RETURN
Definition: win32.h:205
net_event_win32
Definition: win32.h:107
net_event_win32_defined
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition: win32.h:127
set_win_sys_path_via_env
void set_win_sys_path_via_env(struct env_set *es)
Definition: win32.c:1128
win32_service_interrupt
bool win32_service_interrupt(struct win32_signal *ws)
Definition: win32.c:625
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
set_pause_exit_win32
void set_pause_exit_win32(void)
Definition: win32.c:146
win32_signal
Definition: win32.h:153
mtu.h
overlapped_io::overlapped
OVERLAPPED overlapped
Definition: win32.h:207
semaphore::locked
bool locked
Definition: win32.h:244
semaphore_clear
void semaphore_clear(struct semaphore *s)
Definition: win32.c:746
window_title::old_window_title
char old_window_title[256]
Definition: win32.h:76
get_openvpn_reg_value
bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
Fetches a registry value for OpenVPN registry key.
Definition: win32.c:1501
defined_net_event_win32
static bool defined_net_event_win32(const struct rw_handle *event)
Definition: win32.h:92
argv.h
semaphore::hand
HANDLE hand
Definition: win32.h:245
init_win32
void init_win32(void)
Definition: win32.c:109
win32_pause
void win32_pause(struct win32_signal *ws)
Definition: win32.c:683
net_event_win32_get_event_mask
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition: win32.h:139
overlapped_io::addr6
struct sockaddr_in6 addr6
Definition: win32.h:214
window_title_clear
void window_title_clear(struct window_title *wt)
Definition: win32.c:698
net_event_win32::event_mask
long event_mask
Definition: win32.h:111
security_attributes::sd
SECURITY_DESCRIPTOR sd
Definition: win32.h:65
overlapped_io::status
int status
Definition: win32.h:210
win_wfp_block
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition: win32.c:1211
openvpn_execve
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition: win32.c:1001
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
plugin_in_trusted_dir
bool plugin_in_trusted_dir(const WCHAR *plugin_path)
Checks if a plugin is located in a trusted directory.
Definition: win32.c:1607
win32_sleep
void win32_sleep(const int n)
Definition: win32.c:1563
overlapped_io::iostate
int iostate
Definition: win32.h:206
win_wfp_uninit
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1260
net_event_win32_start
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition: win32.c:331
syshead.h
init_net_event_win32
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:223
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
win32_signal_open
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition: win32.c:453
env_set
Definition: env_set.h:42
window_title_restore
void window_title_restore(const struct window_title *wt)
Definition: win32.c:721
semaphore_open
void semaphore_open(struct semaphore *s, const char *name)
Definition: win32.c:752
net_event_win32_clear_selected_events
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition: win32.h:145
socket_descriptor_t
SOCKET socket_descriptor_t
Definition: syshead.h:429
init_security_attributes_allow_all
bool init_security_attributes_allow_all(struct security_attributes *obj)
Definition: win32.c:152
overlapped_io::buf_init
struct buffer buf_init
Definition: win32.h:217
net_event_win32_reset_write
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition: win32.c:340
net_event_win32_get_event
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition: win32.h:133
overlapped_io_state_ascii
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition: win32.c:202
uninit_win32
void uninit_win32(void)
Definition: win32.c:122
rw_handle::write
HANDLE write
Definition: win32.h:81
security_attributes::sa
SECURITY_ATTRIBUTES sa
Definition: win32.h:64
rw_handle
Definition: win32.h:79
openvpn-msg.h
window_title_generate
void window_title_generate(const char *title)
Definition: win32.c:730
semaphore_lock
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition: win32.c:776
net_event_win32_init
void net_event_win32_init(struct net_event_win32 *ne)
Definition: win32.c:324
net_event_win32::handle
struct rw_handle handle
Definition: win32.h:109
send_msg_iservice
bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context)
Definition: win32.c:1480
netcmd_semaphore
struct semaphore netcmd_semaphore
Definition: win32.c:95
win32_signal::console_mode_save
DWORD console_mode_save
Definition: win32.h:159
win32_version_info
int win32_version_info(void)
Definition: win32.c:1287
semaphore::name
const char * name
Definition: win32.h:243
win32_signal::mode
int mode
Definition: win32.h:157
win32_version_string
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1424
win32_signal_get
int win32_signal_get(struct win32_signal *ws)
Definition: win32.c:639
semaphore_close
void semaphore_close(struct semaphore *s)
Definition: win32.c:826
set_win_sys_path
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition: win32.c:1120
win32-util.h
overlapped_io::size
DWORD size
Definition: win32.h:208
overlapped_io
Definition: win32.h:202
win32_signal::console_mode_save_defined
bool console_mode_save_defined
Definition: win32.h:160
IOSTATE_QUEUED
#define IOSTATE_QUEUED
Definition: win32.h:204
overlapped_io_init
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition: win32.c:171