-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapdiag.cpp
464 lines (400 loc) · 9.24 KB
/
tapdiag.cpp
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
// tapdiag.cpp : Hacky utility to interact with OpenVPN TAP driver to toggle some states for testing.
//
#include <Windows.h>
#include <stdio.h>
#include "tap-windows.h"
HANDLE OpenTapDiagDevice(const char* dev_node);
void PrintHelp(const char* programname)
{
printf("%s Usage:\n", programname);
printf(" /enable Add registry key to enable TAP diag device\n");
printf(" /disable Set registry key to disable TAP diag device\n");
printf(" /link:[on|off] Change link state for TAP adapter\n");
printf(" /setq:[on|off|always] Configure adding of 802.1Q priority/vlan headers\n");
}
int main(int argc, const char** argv)
{
// Parse parameters
bool set_link = false;
bool link_on = false;
bool set_enable = false;
bool enable_value = false;
bool set_qos = false;
ULONG qos_value = 0;
for (int i = 1; i < argc; i++)
{
const char* arg = argv[i];
if (arg[0] == '-' || arg[0] == '/')
{
arg++;
if (0 == strcmp(arg, "enable"))
{
set_enable = true;
enable_value = true;
continue;
}
else if (0 == strcmp(arg, "disable"))
{
set_enable = true;
enable_value = true;
continue;
}
else if (0 == strncmp(arg, "link:", 5))
{
const char* part = arg + 5;
if (0 == strcmp(part, "on"))
{
set_link = true;
link_on = true;
continue;
}
else if (0 == strcmp(part, "off"))
{
set_link = true;
link_on = false;
continue;
}
else
{
printf("Unrecognized option: %s\n", arg);
PrintHelp(argv[0]);
return 1;
}
}
else if (0 == strncmp(arg, "setq:", 5))
{
const char* part = arg + 5;
if (0 == strcmp(part, "on"))
{
set_qos = true;
qos_value = TAP_PRIORITY_BEHAVIOR_ENABLED;
continue;
}
else if (0 == strcmp(part, "off"))
{
set_qos = true;
qos_value = TAP_PRIORITY_BEHAVIOR_NOPRIORITY;
continue;
}
else if (0 == strcmp(part, "always"))
{
set_qos = true;
qos_value = TAP_PRIORITY_BEHAVIOR_ADDALWAYS;
continue;
}
else
{
printf("Unrecognized option: %s\n", arg);
PrintHelp(argv[0]);
return 1;
}
}
}
printf("Unrecognized option: %s\n", arg);
PrintHelp(argv[0]);
return 1;
}
if (set_enable)
{
int exitcode = 1;
const char* regPath = "SYSTEM\\CurrentControlSet\\Services\\tap0901";
printf("Setting tapdiag enable flag...\n");
HKEY driverKey = NULL;
LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0, KEY_SET_VALUE, &driverKey);
if (result != ERROR_SUCCESS)
{
printf("Failed to open driver registry key. (%d)\n", result);
}
else
{
DWORD newValue = enable_value ? 1 : 0;
result = RegSetValueEx(driverKey, "TapDiag", 0, REG_DWORD, (BYTE*)&newValue, sizeof(DWORD));
if (result == ERROR_SUCCESS)
{
printf("Successfully set enable flag.\n");
printf("HLKM:%s - TapDiag REG_DWORD = %d\n", regPath, newValue);
printf("\nNote: You must restart the tap driver for this to take effect!\n\n");
exitcode = 0;
}
else
{
printf("Failed to set registry value. (%d)\n", result);
}
RegCloseKey(driverKey);
}
return exitcode;
}
// Open device
HANDLE tap_device = OpenTapDiagDevice(NULL);
if (tap_device == INVALID_HANDLE_VALUE)
{
printf("Failed to open TAP device.\n");
return 1;
}
printf("TAP Device opened.\n");
// Perform action
if (set_link)
{
ULONG mediaStatus = link_on ? 1 : 0;
DWORD bytesReturned = 0;
if (DeviceIoControl(tap_device, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
&mediaStatus, sizeof(mediaStatus),
NULL, 0, &bytesReturned, NULL))
{
printf("Successfully set media status.\n");
}
else
{
printf("Failed to set media status.\n");
return 1;
}
}
if (set_qos)
{
DWORD bytesReturned = 0;
if (DeviceIoControl(tap_device, TAP_WIN_IOCTL_PRIORITY_BEHAVIOR,
&qos_value, sizeof(qos_value),
NULL, 0, &bytesReturned, NULL))
{
printf("Successfully set 802.1Q configuration.\n");
}
else
{
printf("Failed to set 802.1Q configuration\n");
return 1;
}
}
CloseHandle(tap_device);
return 0;
}
#define TAP_WIN_COMPONENT_ID "tap0901"
struct tap_reg
{
const char *guid;
struct tap_reg *next;
};
const char * string_alloc(const char * src_string)
{
size_t len = strlen(src_string);
char* new_string = new char[len + 1];
strncpy_s(new_string, len+1, src_string, len);
return new_string;
}
const struct tap_reg *
get_tap_reg()
{
HKEY adapter_key;
LONG status;
DWORD len;
struct tap_reg *first = NULL;
struct tap_reg *last = NULL;
int i = 0;
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
ADAPTER_KEY,
0,
KEY_READ,
&adapter_key);
if (status != ERROR_SUCCESS)
{
//msg(M_FATAL, "Error opening registry key: %s", ADAPTER_KEY);
}
while (true)
{
char enum_name[256];
char unit_string[256];
HKEY unit_key;
char component_id_string[] = "ComponentId";
char component_id[256];
char net_cfg_instance_id_string[] = "NetCfgInstanceId";
char net_cfg_instance_id[256];
DWORD data_type;
len = sizeof(enum_name);
status = RegEnumKeyEx(
adapter_key,
i,
enum_name,
&len,
NULL,
NULL,
NULL,
NULL);
if (status == ERROR_NO_MORE_ITEMS)
{
break;
}
else if (status != ERROR_SUCCESS)
{
//msg(M_FATAL, "Error enumerating registry subkeys of key: %s",
// ADAPTER_KEY);
}
snprintf(unit_string, sizeof(unit_string), "%s\\%s",
ADAPTER_KEY, enum_name);
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
unit_string,
0,
KEY_READ,
&unit_key);
if (status != ERROR_SUCCESS)
{
//dmsg(D_REGISTRY, "Error opening registry key: %s", unit_string);
}
else
{
len = sizeof(component_id);
status = RegQueryValueEx(
unit_key,
component_id_string,
NULL,
&data_type,
(BYTE*)component_id,
&len);
if (status != ERROR_SUCCESS || data_type != REG_SZ)
{
//dmsg(D_REGISTRY, "Error opening registry key: %s\\%s",
// unit_string, component_id_string);
}
else
{
len = sizeof(net_cfg_instance_id);
status = RegQueryValueEx(
unit_key,
net_cfg_instance_id_string,
NULL,
&data_type,
(BYTE*)net_cfg_instance_id,
&len);
if (status == ERROR_SUCCESS && data_type == REG_SZ)
{
if (!strcmp(component_id, TAP_WIN_COMPONENT_ID))
{
struct tap_reg *reg = new tap_reg;
reg->guid = string_alloc(net_cfg_instance_id);
/* link into return list */
if (!first)
{
first = reg;
}
if (last)
{
last->next = reg;
}
last = reg;
}
}
}
RegCloseKey(unit_key);
}
++i;
}
RegCloseKey(adapter_key);
return first;
}
HANDLE OpenTapDiagDevice(const char* dev_node)
{
HANDLE h = INVALID_HANDLE_VALUE;
char device_path[256];
const char *device_guid = NULL;
/*
* Lookup the device name in the registry, using the --dev-node high level name.
*/
{
const struct tap_reg *tap_reg = get_tap_reg();
//const struct panel_reg *panel_reg = get_panel_reg();
//char actual_buffer[256];
if (tap_reg == NULL)
{
printf("No TAP devices found.\n");
return h;
}
// Don't allow selecting by dev node for now. Just pick the first device.
#if 0
if (dev_node)
{
/* Get the device GUID for the device specified with --dev-node. */
device_guid = get_device_guid(dev_node, actual_buffer, sizeof(actual_buffer), tap_reg, panel_reg, &gc);
if (!device_guid)
{
msg(M_FATAL, "TAP-Windows adapter '%s' not found", dev_node);
}
/* Open Windows TAP-Windows adapter */
snprintf(device_path, sizeof(device_path), "%s%s%s",
USERMODEDEVICEDIR,
device_guid,
TAP_WIN_SUFFIX);
tt->hand = CreateFile(
device_path,
GENERIC_READ | GENERIC_WRITE,
0, /* was: FILE_SHARE_READ */
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (tt->hand == INVALID_HANDLE_VALUE)
{
msg(M_ERR, "CreateFile failed on TAP device: %s", device_path);
}
}
else
#endif
{
int device_number = 0;
const struct tap_reg *tap = tap_reg;
/* Try opening all TAP devices until we find one available */
while (tap != NULL)
{
/* device_guid = get_unspecified_device_guid(device_number,
actual_buffer,
sizeof(actual_buffer),
tap_reg,
panel_reg,
&gc);*/
device_guid = tap_reg->guid;
if (!device_guid)
{
//msg(M_FATAL, "All TAP-Windows adapters on this system are currently in use.");
}
/* Open Windows TAP-Windows adapter */
snprintf(device_path, sizeof(device_path), "%s%s%sdiag",
USERMODEDEVICEDIR,
device_guid,
TAP_WIN_SUFFIX);
printf("Opening device '%s'\n", device_path);
h = CreateFile(
device_path,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (h == INVALID_HANDLE_VALUE)
{
//msg(D_TUNTAP_INFO, "CreateFile failed on TAP device: %s", device_path);
}
else
{
return h;
}
tap = tap->next;
}
}
}
return h;
//{
// ULONG info[3];
// CLEAR(info);
// if (DeviceIoControl(tt->hand, TAP_WIN_IOCTL_GET_VERSION,
// &info, sizeof(info),
// &info, sizeof(info), &len, NULL))
// {
// msg(D_TUNTAP_INFO, "TAP-Windows Driver Version %d.%d %s",
// (int)info[0],
// (int)info[1],
// (info[2] ? "(DEBUG)" : ""));
// }
//}
}