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-2023 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 <winioctl.h>
29 
30 #include "mtu.h"
31 #include "openvpn-msg.h"
32 #include "argv.h"
33 #include "win32-util.h"
34 
35 /* location of executables */
36 #define SYS_PATH_ENV_VAR_NAME "SystemRoot" /* environmental variable name that normally contains the system path */
37 #define NETSH_PATH_SUFFIX "\\system32\\netsh.exe"
38 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
39 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
40 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
41 #define WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.exe"
42 
43 /*
44  * Win32-specific OpenVPN code, targeted at the mingw
45  * development environment.
46  */
47 
48 /* MSVC headers do not define this macro, so do it here */
49 #ifndef IN6_ARE_ADDR_EQUAL
50 #define IN6_ARE_ADDR_EQUAL(a, b) \
51  (memcmp((const void *)(a), (const void *)(b), sizeof(struct in6_addr)) == 0)
52 #endif
53 
54 void init_win32(void);
55 
56 void uninit_win32(void);
57 
58 void set_pause_exit_win32(void);
59 
61 {
62  SECURITY_ATTRIBUTES sa;
63  SECURITY_DESCRIPTOR sd;
64 };
65 
66 #define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
67 
68 /*
69  * Save old window title.
70  */
72 {
73  bool saved;
74  char old_window_title[256];
75 };
76 
77 struct rw_handle {
78  HANDLE read;
79  HANDLE write;
80 };
81 
82 /*
83  * Event-based notification of incoming TCP connections
84  */
85 
86 #define NE32_PERSIST_EVENT (1<<0)
87 #define NE32_WRITE_EVENT (1<<1)
88 
89 static inline bool
90 defined_net_event_win32(const struct rw_handle *event)
91 {
92  return event->read != NULL;
93 }
94 
95 void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags);
96 
98 
99 void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
100 
101 /*
102  * A stateful variant of the net_event_win32 functions above
103  */
104 
106 {
110 };
111 
112 void net_event_win32_init(struct net_event_win32 *ne);
113 
114 void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
115 
116 void net_event_win32_reset(struct net_event_win32 *ne);
117 
119 
120 void net_event_win32_stop(struct net_event_win32 *ne);
121 
122 void net_event_win32_close(struct net_event_win32 *ne);
123 
124 static inline bool
126 {
127  return defined_net_event_win32(&ne->handle);
128 }
129 
130 static inline struct rw_handle *
132 {
133  return &ne->handle;
134 }
135 
136 static inline long
138 {
139  return ne->event_mask;
140 }
141 
142 static inline void
144 {
145  ne->event_mask &= ~selected_events;
146 }
147 
148 /*
149  * Signal handling
150  */
151 struct win32_signal {
152 #define WSO_MODE_UNDEF 0
153 #define WSO_MODE_SERVICE 1
154 #define WSO_MODE_CONSOLE 2
155  int mode;
156  struct rw_handle in;
159 };
160 
161 extern struct win32_signal win32_signal; /* static/global */
162 extern struct window_title window_title; /* static/global */
163 
164 void win32_signal_clear(struct win32_signal *ws);
165 
166 /* win32_signal_open startup type */
167 #define WSO_NOFORCE 0
168 #define WSO_FORCE_SERVICE 1
169 #define WSO_FORCE_CONSOLE 2
170 
171 void win32_signal_open(struct win32_signal *ws,
172  int force, /* set to WSO force parm */
173  const char *exit_event_name,
174  bool exit_event_initial_state);
175 
176 void win32_signal_close(struct win32_signal *ws);
177 
178 int win32_signal_get(struct win32_signal *ws);
179 
180 void win32_pause(struct win32_signal *ws);
181 
182 bool win32_service_interrupt(struct win32_signal *ws);
183 
184 /*
185  * Set the text on the window title bar
186  */
187 
188 void window_title_clear(struct window_title *wt);
189 
190 void window_title_save(struct window_title *wt);
191 
192 void window_title_restore(const struct window_title *wt);
193 
194 void window_title_generate(const char *title);
195 
196 /*
197  * We try to do all Win32 I/O using overlapped
198  * (i.e. asynchronous) I/O for a performance win.
199  */
201 #define IOSTATE_INITIAL 0
202 #define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
203 #define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
204  int iostate;
205  OVERLAPPED overlapped;
206  DWORD size;
207  DWORD flags;
208  int status;
210  union {
211  struct sockaddr_in addr;
212  struct sockaddr_in6 addr6;
213  };
214  int addrlen;
215  struct buffer buf_init;
216  struct buffer buf;
217 };
218 
219 void overlapped_io_init(struct overlapped_io *o,
220  const struct frame *frame,
221  BOOL event_state);
222 
223 void overlapped_io_close(struct overlapped_io *o);
224 
225 static inline bool
227 {
229 }
230 
231 char *overlapped_io_state_ascii(const struct overlapped_io *o);
232 
233 /*
234  * Use to control access to resources that only one
235  * OpenVPN process on a given machine can access at
236  * a given time.
237  */
238 
239 struct semaphore
240 {
241  const char *name;
242  bool locked;
243  HANDLE hand;
244 };
245 
246 void semaphore_clear(struct semaphore *s);
247 
248 void semaphore_open(struct semaphore *s, const char *name);
249 
250 bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
251 
252 void semaphore_release(struct semaphore *s);
253 
254 void semaphore_close(struct semaphore *s);
255 
256 /*
257  * Special global semaphore used to protect network
258  * shell commands from simultaneous instantiation.
259  *
260  * It seems you can't run more than one instance
261  * of netsh on the same machine at the same time.
262  */
263 
264 extern struct semaphore netcmd_semaphore;
265 void netcmd_semaphore_init(void);
266 
267 void netcmd_semaphore_close(void);
268 
269 void netcmd_semaphore_lock(void);
270 
271 void netcmd_semaphore_release(void);
272 
273 /* Set Win32 security attributes structure to allow all access */
275 
276 /* add constant environmental variables needed by Windows */
277 struct env_set;
278 
279 /* get and set the current windows system path */
280 void set_win_sys_path(const char *newpath, struct env_set *es);
281 
282 void set_win_sys_path_via_env(struct env_set *es);
283 
284 char *get_win_sys_path(void);
285 
286 /* call self in a subprocess */
287 void fork_to_self(const char *cmdline);
288 
289 bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel);
290 
291 bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
292 
293 #define WIN_XP 0
294 #define WIN_VISTA 1
295 #define WIN_7 2
296 #define WIN_8 3
297 #define WIN_8_1 4
298 #define WIN_10 5
299 
300 int win32_version_info(void);
301 
302 /*
303  * String representation of Windows version number and name, see
304  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
305  */
306 const char *win32_version_string(struct gc_arena *gc, bool add_name);
307 
308 /*
309  * Send the |size| bytes in buffer |data| to the interactive service |pipe|
310  * and read the result in |ack|. Returns false on communication error.
311  * The string in |context| is used to prefix error messages.
312  */
313 bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
314  ack_message_t *ack, const char *context);
315 
316 /*
317  * Attempt to simulate fork/execve on Windows
318  */
319 int
320 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
321 
322 /*
323  * openvpn_swprintf() is currently only used by Windows code paths
324  * and when enabled for all platforms it will currently break older
325  * OpenBSD versions lacking vswprintf(3) support in their libc.
326  */
327 bool
328 openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...);
329 
330 /* Sleep that can be interrupted by signals and exit event */
331 void win32_sleep(const int n);
332 
333 #endif /* ifndef OPENVPN_WIN32_H */
334 #endif /* ifdef _WIN32 */
overlapped_io_active
static bool overlapped_io_active(struct overlapped_io *o)
Definition: win32.h:226
get_win_sys_path
char * get_win_sys_path(void)
Definition: win32.c:1110
netcmd_semaphore_release
void netcmd_semaphore_release(void)
Definition: win32.c:871
rw_handle::read
HANDLE read
Definition: win32.h:78
security_attributes
Definition: win32.h:60
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:355
overlapped_io::buf
struct buffer buf
Definition: win32.h:216
win32_signal::in
struct rw_handle in
Definition: win32.h:156
netcmd_semaphore_init
void netcmd_semaphore_init(void)
Definition: win32.c:843
net_event_win32_close
void net_event_win32_close(struct net_event_win32 *ne)
Definition: win32.c:372
net_event_win32::sd
socket_descriptor_t sd
Definition: win32.h:108
reset_net_event_win32
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition: win32.c:259
window_title::saved
bool saved
Definition: win32.h:73
overlapped_io::addr_defined
bool addr_defined
Definition: win32.h:209
win32_signal_clear
void win32_signal_clear(struct win32_signal *ws)
Definition: win32.c:444
argv
Definition: argv.h:35
window_title_save
void window_title_save(struct window_title *wt)
Definition: win32.c:701
semaphore_release
void semaphore_release(struct semaphore *s)
Definition: win32.c:807
netcmd_semaphore_close
void netcmd_semaphore_close(void)
Definition: win32.c:849
overlapped_io_close
void overlapped_io_close(struct overlapped_io *o)
Definition: win32.c:186
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:214
win32_signal_close
void win32_signal_close(struct win32_signal *ws)
Definition: win32.c:602
fork_to_self
void fork_to_self(const char *cmdline)
Definition: win32.c:1070
overlapped_io::addr
struct sockaddr_in addr
Definition: win32.h:211
net_event_win32_stop
void net_event_win32_stop(struct net_event_win32 *ne)
Definition: win32.c:361
ack_message_t
Definition: openvpn-msg.h:116
semaphore
Definition: win32.h:239
close_net_event_win32
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:274
window_title
Definition: win32.h:71
netcmd_semaphore_lock
void netcmd_semaphore_lock(void)
Definition: win32.c:855
overlapped_io::flags
DWORD flags
Definition: win32.h:207
frame
Packet geometry parameters.
Definition: mtu.h:98
IOSTATE_IMMEDIATE_RETURN
#define IOSTATE_IMMEDIATE_RETURN
Definition: win32.h:203
net_event_win32
Definition: win32.h:105
net_event_win32_defined
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition: win32.h:125
set_win_sys_path_via_env
void set_win_sys_path_via_env(struct env_set *es)
Definition: win32.c:1125
win32_service_interrupt
bool win32_service_interrupt(struct win32_signal *ws)
Definition: win32.c:622
set_pause_exit_win32
void set_pause_exit_win32(void)
Definition: win32.c:143
win32_signal
Definition: win32.h:151
mtu.h
overlapped_io::overlapped
OVERLAPPED overlapped
Definition: win32.h:205
semaphore::locked
bool locked
Definition: win32.h:242
semaphore_clear
void semaphore_clear(struct semaphore *s)
Definition: win32.c:743
window_title::old_window_title
char old_window_title[256]
Definition: win32.h:74
defined_net_event_win32
static bool defined_net_event_win32(const struct rw_handle *event)
Definition: win32.h:90
argv.h
semaphore::hand
HANDLE hand
Definition: win32.h:243
init_win32
void init_win32(void)
Definition: win32.c:106
win32_pause
void win32_pause(struct win32_signal *ws)
Definition: win32.c:680
net_event_win32_get_event_mask
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition: win32.h:137
openvpn_swprintf
bool openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format,...)
Definition: win32.c:1486
overlapped_io::addr6
struct sockaddr_in6 addr6
Definition: win32.h:212
window_title_clear
void window_title_clear(struct window_title *wt)
Definition: win32.c:695
net_event_win32::event_mask
long event_mask
Definition: win32.h:109
security_attributes::sd
SECURITY_DESCRIPTOR sd
Definition: win32.h:63
overlapped_io::status
int status
Definition: win32.h:208
openvpn_execve
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition: win32.c:998
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
win_wfp_block_dns
bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1195
win32_sleep
void win32_sleep(const int n)
Definition: win32.c:1566
overlapped_io::iostate
int iostate
Definition: win32.h:204
win_wfp_uninit
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1245
net_event_win32_start
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition: win32.c:328
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:220
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:450
env_set
Definition: env_set.h:42
window_title_restore
void window_title_restore(const struct window_title *wt)
Definition: win32.c:718
semaphore_open
void semaphore_open(struct semaphore *s, const char *name)
Definition: win32.c:749
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:143
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:149
overlapped_io::buf_init
struct buffer buf_init
Definition: win32.h:215
net_event_win32_reset_write
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition: win32.c:337
net_event_win32_get_event
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition: win32.h:131
overlapped_io_state_ascii
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition: win32.c:199
uninit_win32
void uninit_win32(void)
Definition: win32.c:119
rw_handle::write
HANDLE write
Definition: win32.h:79
security_attributes::sa
SECURITY_ATTRIBUTES sa
Definition: win32.h:62
rw_handle
Definition: win32.h:77
openvpn-msg.h
window_title_generate
void window_title_generate(const char *title)
Definition: win32.c:727
semaphore_lock
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition: win32.c:773
net_event_win32_init
void net_event_win32_init(struct net_event_win32 *ne)
Definition: win32.c:321
net_event_win32::handle
struct rw_handle handle
Definition: win32.h:107
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:1465
netcmd_semaphore
struct semaphore netcmd_semaphore
Definition: win32.c:92
win32_signal::console_mode_save
DWORD console_mode_save
Definition: win32.h:157
win32_version_info
int win32_version_info(void)
Definition: win32.c:1272
semaphore::name
const char * name
Definition: win32.h:241
win32_signal::mode
int mode
Definition: win32.h:155
win32_version_string
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1409
win32_signal_get
int win32_signal_get(struct win32_signal *ws)
Definition: win32.c:636
semaphore_close
void semaphore_close(struct semaphore *s)
Definition: win32.c:823
set_win_sys_path
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition: win32.c:1117
win32-util.h
overlapped_io::size
DWORD size
Definition: win32.h:206
overlapped_io
Definition: win32.h:200
win32_signal::console_mode_save_defined
bool console_mode_save_defined
Definition: win32.h:158
IOSTATE_QUEUED
#define IOSTATE_QUEUED
Definition: win32.h:202
overlapped_io_init
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition: win32.c:168