2 * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
3 * on Windows. Originally derived from the CIPE-Win32
4 * project by Damion K. Wilson, with extensive modifications by
7 * All source code which derives from the CIPE-Win32 project is
8 * Copyright (C) Damion K. Wilson, 2003, and is released under the
9 * GPL version 2 (see below).
11 * All other source code is Copyright (C) James Yonan, 2003-2004,
12 * and is released under the GPL version 2 (see below).
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program (see the file COPYING included with this
26 * distribution); if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "qemu-common.h"
33 #define WIN32_LEAN_AND_MEAN
36 /* NOTE: PCIBus is redefined in winddk.h */
37 #define PCIBus _PCIBus
38 #include <ddk/ntapi.h>
39 #include <ddk/winddk.h>
40 #include <ddk/ntddk.h>
47 #define TAP_CONTROL_CODE(request,method) \
48 CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
50 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
51 #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
52 #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
53 #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
54 #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
55 #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
56 #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
57 #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
58 #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
64 #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
66 #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
68 //======================
69 // Filesystem prefixes
70 //======================
72 #define USERMODEDEVICEDIR "\\\\.\\Global\\"
73 #define TAPSUFFIX ".tap"
76 //======================
77 // Compile time configuration
78 //======================
80 //#define DEBUG_TAP_WIN32
82 #define TUN_ASYNCHRONOUS_WRITES 1
84 #define TUN_BUFFER_SIZE 1560
85 #define TUN_MAX_BUFFER_COUNT 32
88 * The data member "buffer" must be the first element in the tun_buffer
89 * structure. See the function, tap_win32_free_buffer.
91 typedef struct tun_buffer_s
{
92 unsigned char buffer
[TUN_BUFFER_SIZE
];
93 unsigned long read_size
;
94 struct tun_buffer_s
* next
;
97 typedef struct tap_win32_overlapped
{
101 HANDLE output_queue_semaphore
;
102 HANDLE free_list_semaphore
;
103 HANDLE tap_semaphore
;
104 CRITICAL_SECTION output_queue_cs
;
105 CRITICAL_SECTION free_list_cs
;
106 OVERLAPPED read_overlapped
;
107 OVERLAPPED write_overlapped
;
108 tun_buffer_t buffers
[TUN_MAX_BUFFER_COUNT
];
109 tun_buffer_t
* free_list
;
110 tun_buffer_t
* output_queue_front
;
111 tun_buffer_t
* output_queue_back
;
112 } tap_win32_overlapped_t
;
114 static tap_win32_overlapped_t tap_overlapped
;
116 static tun_buffer_t
* get_buffer_from_free_list(tap_win32_overlapped_t
* const overlapped
)
118 tun_buffer_t
* buffer
= NULL
;
119 WaitForSingleObject(overlapped
->free_list_semaphore
, INFINITE
);
120 EnterCriticalSection(&overlapped
->free_list_cs
);
121 buffer
= overlapped
->free_list
;
122 // assert(buffer != NULL);
123 overlapped
->free_list
= buffer
->next
;
124 LeaveCriticalSection(&overlapped
->free_list_cs
);
129 static void put_buffer_on_free_list(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
131 EnterCriticalSection(&overlapped
->free_list_cs
);
132 buffer
->next
= overlapped
->free_list
;
133 overlapped
->free_list
= buffer
;
134 LeaveCriticalSection(&overlapped
->free_list_cs
);
135 ReleaseSemaphore(overlapped
->free_list_semaphore
, 1, NULL
);
138 static tun_buffer_t
* get_buffer_from_output_queue(tap_win32_overlapped_t
* const overlapped
, const int block
)
140 tun_buffer_t
* buffer
= NULL
;
141 DWORD result
, timeout
= block
? INFINITE
: 0L;
144 result
= WaitForSingleObject(overlapped
->output_queue_semaphore
, timeout
);
148 // The semaphore object was signaled.
150 EnterCriticalSection(&overlapped
->output_queue_cs
);
152 buffer
= overlapped
->output_queue_front
;
153 overlapped
->output_queue_front
= buffer
->next
;
155 if(overlapped
->output_queue_front
== NULL
) {
156 overlapped
->output_queue_back
= NULL
;
159 LeaveCriticalSection(&overlapped
->output_queue_cs
);
162 // Semaphore was nonsignaled, so a time-out occurred.
164 // Cannot open another window.
171 static tun_buffer_t
* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t
* const overlapped
)
173 return get_buffer_from_output_queue(overlapped
, 0);
176 static void put_buffer_on_output_queue(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
178 EnterCriticalSection(&overlapped
->output_queue_cs
);
180 if(overlapped
->output_queue_front
== NULL
&& overlapped
->output_queue_back
== NULL
) {
181 overlapped
->output_queue_front
= overlapped
->output_queue_back
= buffer
;
184 overlapped
->output_queue_back
->next
= buffer
;
185 overlapped
->output_queue_back
= buffer
;
188 LeaveCriticalSection(&overlapped
->output_queue_cs
);
190 ReleaseSemaphore(overlapped
->output_queue_semaphore
, 1, NULL
);
194 static int is_tap_win32_dev(const char *guid
)
201 status
= RegOpenKeyEx(
208 if (status
!= ERROR_SUCCESS
) {
214 char unit_string
[256];
216 char component_id_string
[] = "ComponentId";
217 char component_id
[256];
218 char net_cfg_instance_id_string
[] = "NetCfgInstanceId";
219 char net_cfg_instance_id
[256];
222 len
= sizeof (enum_name
);
223 status
= RegEnumKeyEx(
233 if (status
== ERROR_NO_MORE_ITEMS
)
235 else if (status
!= ERROR_SUCCESS
) {
239 snprintf (unit_string
, sizeof(unit_string
), "%s\\%s",
240 ADAPTER_KEY
, enum_name
);
242 status
= RegOpenKeyEx(
249 if (status
!= ERROR_SUCCESS
) {
252 len
= sizeof (component_id
);
253 status
= RegQueryValueEx(
261 if (!(status
!= ERROR_SUCCESS
|| data_type
!= REG_SZ
)) {
262 len
= sizeof (net_cfg_instance_id
);
263 status
= RegQueryValueEx(
265 net_cfg_instance_id_string
,
271 if (status
== ERROR_SUCCESS
&& data_type
== REG_SZ
) {
272 if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
273 !strcmp (net_cfg_instance_id
, guid
)) {
274 RegCloseKey (unit_key
);
275 RegCloseKey (netcard_key
);
280 RegCloseKey (unit_key
);
285 RegCloseKey (netcard_key
);
289 static int get_device_guid(
293 int actual_name_size
)
296 HKEY control_net_key
;
301 status
= RegOpenKeyEx(
303 NETWORK_CONNECTIONS_KEY
,
308 if (status
!= ERROR_SUCCESS
) {
315 char connection_string
[256];
319 const char name_string
[] = "Name";
321 len
= sizeof (enum_name
);
322 status
= RegEnumKeyEx(
332 if (status
== ERROR_NO_MORE_ITEMS
)
334 else if (status
!= ERROR_SUCCESS
) {
338 snprintf(connection_string
,
339 sizeof(connection_string
),
340 "%s\\%s\\Connection",
341 NETWORK_CONNECTIONS_KEY
, enum_name
);
343 status
= RegOpenKeyEx(
350 if (status
== ERROR_SUCCESS
) {
351 len
= sizeof (name_data
);
352 status
= RegQueryValueEx(
360 if (status
!= ERROR_SUCCESS
|| name_type
!= REG_SZ
) {
364 if (is_tap_win32_dev(enum_name
)) {
365 snprintf(name
, name_size
, "%s", enum_name
);
367 if (strcmp(actual_name
, "") != 0) {
368 if (strcmp(name_data
, actual_name
) != 0) {
369 RegCloseKey (connection_key
);
375 snprintf(actual_name
, actual_name_size
, "%s", name_data
);
382 RegCloseKey (connection_key
);
387 RegCloseKey (control_net_key
);
395 static int tap_win32_set_status(HANDLE handle
, int status
)
397 unsigned long len
= 0;
399 return DeviceIoControl(handle
, TAP_IOCTL_SET_MEDIA_STATUS
,
400 &status
, sizeof (status
),
401 &status
, sizeof (status
), &len
, NULL
);
404 static void tap_win32_overlapped_init(tap_win32_overlapped_t
* const overlapped
, const HANDLE handle
)
406 overlapped
->handle
= handle
;
408 overlapped
->read_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
409 overlapped
->write_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
411 overlapped
->read_overlapped
.Offset
= 0;
412 overlapped
->read_overlapped
.OffsetHigh
= 0;
413 overlapped
->read_overlapped
.hEvent
= overlapped
->read_event
;
415 overlapped
->write_overlapped
.Offset
= 0;
416 overlapped
->write_overlapped
.OffsetHigh
= 0;
417 overlapped
->write_overlapped
.hEvent
= overlapped
->write_event
;
419 InitializeCriticalSection(&overlapped
->output_queue_cs
);
420 InitializeCriticalSection(&overlapped
->free_list_cs
);
422 overlapped
->output_queue_semaphore
= CreateSemaphore(
423 NULL
, // default security attributes
425 TUN_MAX_BUFFER_COUNT
, // maximum count
426 NULL
); // unnamed semaphore
428 if(!overlapped
->output_queue_semaphore
) {
429 fprintf(stderr
, "error creating output queue semaphore!\n");
432 overlapped
->free_list_semaphore
= CreateSemaphore(
433 NULL
, // default security attributes
434 TUN_MAX_BUFFER_COUNT
, // initial count
435 TUN_MAX_BUFFER_COUNT
, // maximum count
436 NULL
); // unnamed semaphore
438 if(!overlapped
->free_list_semaphore
) {
439 fprintf(stderr
, "error creating free list semaphore!\n");
442 overlapped
->free_list
= overlapped
->output_queue_front
= overlapped
->output_queue_back
= NULL
;
446 for(index
= 0; index
< TUN_MAX_BUFFER_COUNT
; index
++) {
447 tun_buffer_t
* element
= &overlapped
->buffers
[index
];
448 element
->next
= overlapped
->free_list
;
449 overlapped
->free_list
= element
;
452 /* To count buffers, initially no-signal. */
453 overlapped
->tap_semaphore
= CreateSemaphore(NULL
, 0, TUN_MAX_BUFFER_COUNT
, NULL
);
454 if(!overlapped
->tap_semaphore
)
455 fprintf(stderr
, "error creating tap_semaphore.\n");
458 static int tap_win32_write(tap_win32_overlapped_t
*overlapped
,
459 const void *buffer
, unsigned long size
)
461 unsigned long write_size
;
465 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->write_overlapped
,
468 if (!result
&& GetLastError() == ERROR_IO_INCOMPLETE
)
469 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
471 result
= WriteFile(overlapped
->handle
, buffer
, size
,
472 &write_size
, &overlapped
->write_overlapped
);
475 switch (error
= GetLastError())
477 case ERROR_IO_PENDING
:
478 #ifndef TUN_ASYNCHRONOUS_WRITES
479 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
490 static DWORD WINAPI
tap_win32_thread_entry(LPVOID param
)
492 tap_win32_overlapped_t
*overlapped
= (tap_win32_overlapped_t
*)param
;
493 unsigned long read_size
;
496 tun_buffer_t
* buffer
= get_buffer_from_free_list(overlapped
);
500 result
= ReadFile(overlapped
->handle
,
502 sizeof(buffer
->buffer
),
504 &overlapped
->read_overlapped
);
506 dwError
= GetLastError();
507 if (dwError
== ERROR_IO_PENDING
) {
508 WaitForSingleObject(overlapped
->read_event
, INFINITE
);
509 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->read_overlapped
,
512 #ifdef DEBUG_TAP_WIN32
514 dwError
= GetLastError();
515 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
516 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
517 (LPTSTR
) & lpBuffer
, 0, NULL
);
518 fprintf(stderr
, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError
, lpBuffer
);
519 LocalFree( lpBuffer
);
523 #ifdef DEBUG_TAP_WIN32
525 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
526 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
527 (LPTSTR
) & lpBuffer
, 0, NULL
);
528 fprintf(stderr
, "Tap-Win32: Error ReadFile %d - %s\n", dwError
, lpBuffer
);
529 LocalFree( lpBuffer
);
535 buffer
->read_size
= read_size
;
536 put_buffer_on_output_queue(overlapped
, buffer
);
537 ReleaseSemaphore(overlapped
->tap_semaphore
, 1, NULL
);
538 buffer
= get_buffer_from_free_list(overlapped
);
545 static int tap_win32_read(tap_win32_overlapped_t
*overlapped
,
546 uint8_t **pbuf
, int max_size
)
550 tun_buffer_t
* buffer
= get_buffer_from_output_queue_immediate(overlapped
);
553 *pbuf
= buffer
->buffer
;
554 size
= (int)buffer
->read_size
;
555 if(size
> max_size
) {
563 static void tap_win32_free_buffer(tap_win32_overlapped_t
*overlapped
,
566 tun_buffer_t
* buffer
= (tun_buffer_t
*)pbuf
;
567 put_buffer_on_free_list(overlapped
, buffer
);
570 static int tap_win32_open(tap_win32_overlapped_t
**phandle
,
571 const char *prefered_name
)
573 char device_path
[256];
574 char device_guid
[0x100];
578 char name_buffer
[0x100] = {0, };
588 if (prefered_name
!= NULL
)
589 snprintf(name_buffer
, sizeof(name_buffer
), "%s", prefered_name
);
591 rc
= get_device_guid(device_guid
, sizeof(device_guid
), name_buffer
, sizeof(name_buffer
));
595 snprintf (device_path
, sizeof(device_path
), "%s%s%s",
600 handle
= CreateFile (
602 GENERIC_READ
| GENERIC_WRITE
,
606 FILE_ATTRIBUTE_SYSTEM
| FILE_FLAG_OVERLAPPED
,
609 if (handle
== INVALID_HANDLE_VALUE
) {
613 bret
= DeviceIoControl(handle
, TAP_IOCTL_GET_VERSION
,
614 &version
, sizeof (version
),
615 &version
, sizeof (version
), &version_len
, NULL
);
622 if (!tap_win32_set_status(handle
, TRUE
)) {
626 tap_win32_overlapped_init(&tap_overlapped
, handle
);
628 *phandle
= &tap_overlapped
;
630 hThread
= CreateThread(NULL
, 0, tap_win32_thread_entry
,
631 (LPVOID
)&tap_overlapped
, 0, &idThread
);
635 /********************************************/
637 typedef struct TAPState
{
639 tap_win32_overlapped_t
*handle
;
642 static void tap_cleanup(VLANClientState
*vc
)
644 TAPState
*s
= vc
->opaque
;
646 qemu_del_wait_object(s
->handle
->tap_semaphore
, NULL
, NULL
);
648 /* FIXME: need to kill thread and close file handle:
654 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
656 TAPState
*s
= opaque
;
658 tap_win32_write(s
->handle
, buf
, size
);
661 static void tap_win32_send(void *opaque
)
663 TAPState
*s
= opaque
;
668 size
= tap_win32_read(s
->handle
, &buf
, max_size
);
670 qemu_send_packet(s
->vc
, buf
, size
);
671 tap_win32_free_buffer(s
->handle
, buf
);
675 int tap_win32_init(VLANState
*vlan
, const char *model
,
676 const char *name
, const char *ifname
)
680 s
= qemu_mallocz(sizeof(TAPState
));
683 if (tap_win32_open(&s
->handle
, ifname
) < 0) {
684 printf("tap: Could not open '%s'\n", ifname
);
688 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
,
689 NULL
, tap_cleanup
, s
);
691 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
692 "tap: ifname=%s", ifname
);
694 qemu_add_wait_object(s
->handle
->tap_semaphore
, tap_win32_send
, s
);