-
Notifications
You must be signed in to change notification settings - Fork 63
/
tuntap-windows.c
470 lines (390 loc) · 12.5 KB
/
tuntap-windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Copyright (c) 2012 Tristan Le Guern <tleguern@bouledef.eu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <strsafe.h>
#include "tuntap.h"
#include "private.h"
/* From OpenVPN tap driver, common.h */
#define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
#define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
#define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
#define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
#define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
#define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_TUN TAP_CONTROL_CODE (10, METHOD_BUFFERED)
/* Windows registry crap */
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
#define NETWORK_ADAPTERS "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
/* From OpenVPN tap driver, proto.h */
typedef unsigned long IPADDR;
/* This one is from Fabien Pichot, in the tNETacle source code */
static LPWSTR
formated_error(LPWSTR pMessage, DWORD m, ...) {
LPWSTR pBuffer = NULL;
va_list args = NULL;
va_start(args, m);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
pMessage,
m,
0,
(LPSTR)&pBuffer,
0,
&args);
va_end(args);
return pBuffer;
}
/* TODO: Rework to be more generic and allow arbitrary key modification (MTU and stuff) */
static char *
reg_query(char *key_name) {
HKEY adapters, adapter;
DWORD i, ret, len;
char *deviceid = NULL;
DWORD sub_keys = 0;
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(key_name), 0, KEY_READ, &adapters);
if (ret != ERROR_SUCCESS) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", ret));
return NULL;
}
ret = RegQueryInfoKey(adapters, NULL, NULL, NULL, &sub_keys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
if (ret != ERROR_SUCCESS) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", ret));
return NULL;
}
if (sub_keys <= 0) {
tuntap_log(TUNTAP_LOG_DEBUG, "Wrong registry key");
return NULL;
}
/* Walk througt all adapters */
for (i = 0; i < sub_keys; i++) {
char new_key[MAX_KEY_LENGTH];
char data[256];
TCHAR key[MAX_KEY_LENGTH];
DWORD keylen = MAX_KEY_LENGTH;
/* Get the adapter key name */
ret = RegEnumKeyEx(adapters, i, key, &keylen, NULL, NULL, NULL, NULL);
if (ret != ERROR_SUCCESS) {
continue;
}
/* Append it to NETWORK_ADAPTERS and open it */
snprintf(new_key, sizeof new_key, "%s\\%s", key_name, key);
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(new_key), 0, KEY_READ, &adapter);
if (ret != ERROR_SUCCESS) {
continue;
}
/* Check its values */
len = sizeof data;
ret = RegQueryValueEx(adapter, "ComponentId", NULL, NULL, (LPBYTE)data, &len);
if (ret != ERROR_SUCCESS) {
/* This value doesn't exist in this adaptater tree */
goto clean;
}
/* If its a tap adapter, its all good */
if (strncmp(data, "tap", 3) == 0 || strncmp(data, "root\\tap", 8) == 0) {
DWORD type;
len = sizeof data;
ret = RegQueryValueEx(adapter, "NetCfgInstanceId", NULL, &type, (LPBYTE)data, &len);
if (ret != ERROR_SUCCESS) {
tuntap_log(TUNTAP_LOG_INFO, (const char *)formated_error(L"%1", ret));
goto clean;
}
deviceid = strdup(data);
break;
}
clean:
RegCloseKey(adapter);
}
RegCloseKey(adapters);
return deviceid;
}
void
tuntap_sys_destroy(struct device *dev) {
(void)dev;
return;
}
int
tuntap_start(struct device *dev, int mode, int tun) {
HANDLE tun_fd;
char *deviceid;
char buf[60];
/* Don't re-initialise a previously started device */
if (dev->tun_fd != TUNFD_INVALID_VALUE) {
return -1;
}
/* Shift the persistence bit */
if (mode & TUNTAP_MODE_PERSIST) {
mode &= ~TUNTAP_MODE_PERSIST;
}
if (mode == TUNTAP_MODE_TUNNEL) {
tuntap_log(TUNTAP_LOG_NOTICE, "Layer 3 tunneling is not implemented");
return -1;
}
else if (mode != TUNTAP_MODE_ETHERNET) {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'mode'");
return -1;
}
deviceid = reg_query(NETWORK_ADAPTERS);
snprintf(buf, sizeof buf, "\\\\.\\Global\\%s.tap", deviceid);
tun_fd = CreateFile(buf, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
if (tun_fd == TUNFD_INVALID_VALUE) {
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
dev->tun_fd = tun_fd;
return 0;
}
void
tuntap_release(struct device *dev) {
(void)CloseHandle(dev->tun_fd);
free(dev);
}
char *
tuntap_get_hwaddr(struct device *dev) {
static unsigned char hwaddr[ETHER_ADDR_LEN];
DWORD len;
if (DeviceIoControl(dev->tun_fd, TAP_IOCTL_GET_MAC, &hwaddr, sizeof(hwaddr), &hwaddr, sizeof(hwaddr), &len, NULL) == 0) {
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return NULL;
} else {
char buf[128];
(void)_snprintf_s(buf, sizeof buf, sizeof buf, "MAC address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
hwaddr[0],hwaddr[1],hwaddr[2],hwaddr[3],hwaddr[4],hwaddr[5]);
tuntap_log(TUNTAP_LOG_DEBUG, buf);
}
return (char *)hwaddr;
}
int
tuntap_set_hwaddr(struct device *dev, const char *hwaddr) {
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_hwaddr()");
return -1;
}
static int
tuntap_sys_set_updown(struct device *dev, ULONG flag) {
DWORD len;
if (DeviceIoControl(dev->tun_fd, TAP_IOCTL_SET_MEDIA_STATUS, &flag, sizeof(flag), &flag, sizeof(flag), &len, NULL) == 0) {
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
} else {
char buf[32];
(void)_snprintf_s(buf, sizeof buf, sizeof buf, "Status: %s", flag ? "Up" : "Down");
tuntap_log(TUNTAP_LOG_DEBUG, buf);
return 0;
}
}
int
tuntap_up(struct device *dev) {
ULONG flag;
flag = 1;
return tuntap_sys_set_updown(dev, flag);
}
int
tuntap_down(struct device *dev) {
ULONG flag;
flag = 0;
return tuntap_sys_set_updown(dev, flag);
}
int
tuntap_get_mtu(struct device *dev) {
ULONG mtu;
DWORD len;
if (DeviceIoControl(dev->tun_fd, TAP_IOCTL_GET_MTU, &mtu, sizeof(mtu), &mtu, sizeof(mtu), &len, NULL) == 0) {
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return 0;
}
int
tuntap_set_mtu(struct device *dev, int mtu) {
(void)dev;
(void)mtu;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_mtu()");
return -1;
}
int
tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask) {
IPADDR psock[4];
DWORD len;
/* Address + Netmask */
psock[0] = s->S_un.S_addr;
psock[1] = mask;
/* DHCP server address (We don't want it) */
psock[2] = 0;
/* DHCP lease time */
psock[3] = 0;
if (DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_MASQ, &psock, sizeof(psock), &psock, sizeof(psock), &len, NULL) == 0) {
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return 0;
}
int
tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s, uint32_t mask) {
(void)dev;
(void)s;
(void)mask;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_sys_set_ipv6()");
return -1;
}
int
tuntap_read(struct device *dev, void *buf, size_t size) {
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
BOOL ok = ReadFile(dev->tun_fd, buf, (DWORD)size, NULL, &overlapped);
int errcode = GetLastError();
if (!ok && errcode != ERROR_IO_PENDING) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
DWORD len;
if (!GetOverlappedResult(dev->tun_fd, &overlapped, &len, TRUE)) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return (int)len;
}
static int
wait_completion_or_timeout(HANDLE fd, OVERLAPPED *overlapped, int timeout_ms)
{
// An operation is pending (either a read or a write),
// so wait until either it completes or the timeout
// triggers. If timeout_ms < 0, then don't set a timeout.
DWORD timeout;
if (timeout_ms < 0)
timeout = INFINITE;
else
timeout = timeout_ms;
DWORD len;
BOOL ok = GetOverlappedResultEx(fd, overlapped, &len, timeout, FALSE);
if (!ok) {
int errcode = GetLastError();
if (errcode == ERROR_IO_INCOMPLETE || errcode == WAIT_TIMEOUT) {
CancelIo(fd);
} else {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
}
return -1;
}
return (int)len;
}
int
tuntap_read_tm(struct device *dev, void *buf, size_t size, int timeout_ms) {
BOOL ok;
DWORD len;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
ok = ReadFile(dev->tun_fd, buf, (DWORD)size, &len, &overlapped);
if (ok)
return (int)len; // Operation resolved immediately
int errcode = GetLastError();
if (errcode != ERROR_IO_PENDING) {
// Couldn't start the read operation
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return wait_completion_or_timeout(dev->tun_fd, &overlapped, timeout_ms);
}
int
tuntap_write(struct device *dev, void *buf, size_t size) {
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
BOOL ok = WriteFile(dev->tun_fd, buf, (DWORD)size, NULL, &overlapped);
int errcode = GetLastError();
if (!ok && errcode != ERROR_IO_PENDING) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
DWORD len;
if (!GetOverlappedResult(dev->tun_fd, &overlapped, &len, TRUE)) {
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return (int)len;
}
int
tuntap_write_tm(struct device *dev, void *buf, size_t size, int timeout_ms) {
int errcode;
BOOL ok;
DWORD len;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
ok = WriteFile(dev->tun_fd, buf, (DWORD)size, &len, &overlapped);
if (ok)
return (int) len; // Write operation completed immediately
errcode = GetLastError();
if (errcode != ERROR_IO_PENDING) {
// Couldn't start write operation at all
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return wait_completion_or_timeout(dev->tun_fd, &overlapped, timeout_ms);
}
int
tuntap_get_readable(struct device *dev) {
(void)dev;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_get_readable()");
return -1;
}
int
tuntap_set_nonblocking(struct device *dev, int set) {
(void)dev;
(void)set;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_nonblocking()");
return -1;
}
int
tuntap_set_debug(struct device *dev, int set) {
(void)dev;
(void)set;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_debug()");
return -1;
}
int
tuntap_set_descr(struct device *dev, const char *descr) {
(void)dev;
(void)descr;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_descr()");
return -1;
}
int
tuntap_set_ifname(struct device *dev, const char *name) {
/* TODO: Check Windows API to know how to rename an interface */
(void)dev;
(void)name;
tuntap_log(TUNTAP_LOG_NOTICE, "Your system does not support tuntap_set_ifname()");
return -1;
}
char*
tuntap_get_descr(struct device* dev) {
(void)dev;
tuntap_log(TUNTAP_LOG_NOTICE,
"Your system does not support tuntap_get_descr()");
return NULL;
}