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"
35 /* NOTE: PCIBus is redefined in winddk.h */
36 #define PCIBus _PCIBus
37 #include <ddk/ntapi.h>
38 #include <ddk/winddk.h>
39 #include <ddk/ntddk.h>
46 #define TAP_CONTROL_CODE(request,method) \
47 CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
49 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
50 #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
51 #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
52 #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
53 #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
54 #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
55 #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
56 #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
57 #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
63 #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
65 #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
67 //======================
68 // Filesystem prefixes
69 //======================
71 #define USERMODEDEVICEDIR "\\\\.\\Global\\"
72 #define TAPSUFFIX ".tap"
75 //======================
76 // Compile time configuration
77 //======================
79 //#define DEBUG_TAP_WIN32
81 #define TUN_ASYNCHRONOUS_WRITES 1
83 #define TUN_BUFFER_SIZE 1560
84 #define TUN_MAX_BUFFER_COUNT 32
87 * The data member "buffer" must be the first element in the tun_buffer
88 * structure. See the function, tap_win32_free_buffer.
90 typedef struct tun_buffer_s
{
91 unsigned char buffer
[TUN_BUFFER_SIZE
];
92 unsigned long read_size
;
93 struct tun_buffer_s
* next
;
96 typedef struct tap_win32_overlapped
{
100 HANDLE output_queue_semaphore
;
101 HANDLE free_list_semaphore
;
102 HANDLE tap_semaphore
;
103 CRITICAL_SECTION output_queue_cs
;
104 CRITICAL_SECTION free_list_cs
;
105 OVERLAPPED read_overlapped
;
106 OVERLAPPED write_overlapped
;
107 tun_buffer_t buffers
[TUN_MAX_BUFFER_COUNT
];
108 tun_buffer_t
* free_list
;
109 tun_buffer_t
* output_queue_front
;
110 tun_buffer_t
* output_queue_back
;
111 } tap_win32_overlapped_t
;
113 static tap_win32_overlapped_t tap_overlapped
;
115 static tun_buffer_t
* get_buffer_from_free_list(tap_win32_overlapped_t
* const overlapped
)
117 tun_buffer_t
* buffer
= NULL
;
118 WaitForSingleObject(overlapped
->free_list_semaphore
, INFINITE
);
119 EnterCriticalSection(&overlapped
->free_list_cs
);
120 buffer
= overlapped
->free_list
;
121 // assert(buffer != NULL);
122 overlapped
->free_list
= buffer
->next
;
123 LeaveCriticalSection(&overlapped
->free_list_cs
);
128 static void put_buffer_on_free_list(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
130 EnterCriticalSection(&overlapped
->free_list_cs
);
131 buffer
->next
= overlapped
->free_list
;
132 overlapped
->free_list
= buffer
;
133 LeaveCriticalSection(&overlapped
->free_list_cs
);
134 ReleaseSemaphore(overlapped
->free_list_semaphore
, 1, NULL
);
137 static tun_buffer_t
* get_buffer_from_output_queue(tap_win32_overlapped_t
* const overlapped
, const int block
)
139 tun_buffer_t
* buffer
= NULL
;
140 DWORD result
, timeout
= block
? INFINITE
: 0L;
143 result
= WaitForSingleObject(overlapped
->output_queue_semaphore
, timeout
);
147 // The semaphore object was signaled.
149 EnterCriticalSection(&overlapped
->output_queue_cs
);
151 buffer
= overlapped
->output_queue_front
;
152 overlapped
->output_queue_front
= buffer
->next
;
154 if(overlapped
->output_queue_front
== NULL
) {
155 overlapped
->output_queue_back
= NULL
;
158 LeaveCriticalSection(&overlapped
->output_queue_cs
);
161 // Semaphore was nonsignaled, so a time-out occurred.
163 // Cannot open another window.
170 static tun_buffer_t
* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t
* const overlapped
)
172 return get_buffer_from_output_queue(overlapped
, 0);
175 static void put_buffer_on_output_queue(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
177 EnterCriticalSection(&overlapped
->output_queue_cs
);
179 if(overlapped
->output_queue_front
== NULL
&& overlapped
->output_queue_back
== NULL
) {
180 overlapped
->output_queue_front
= overlapped
->output_queue_back
= buffer
;
183 overlapped
->output_queue_back
->next
= buffer
;
184 overlapped
->output_queue_back
= buffer
;
187 LeaveCriticalSection(&overlapped
->output_queue_cs
);
189 ReleaseSemaphore(overlapped
->output_queue_semaphore
, 1, NULL
);
193 static int is_tap_win32_dev(const char *guid
)
200 status
= RegOpenKeyEx(
207 if (status
!= ERROR_SUCCESS
) {
213 char unit_string
[256];
215 char component_id_string
[] = "ComponentId";
216 char component_id
[256];
217 char net_cfg_instance_id_string
[] = "NetCfgInstanceId";
218 char net_cfg_instance_id
[256];
221 len
= sizeof (enum_name
);
222 status
= RegEnumKeyEx(
232 if (status
== ERROR_NO_MORE_ITEMS
)
234 else if (status
!= ERROR_SUCCESS
) {
238 snprintf (unit_string
, sizeof(unit_string
), "%s\\%s",
239 ADAPTER_KEY
, enum_name
);
241 status
= RegOpenKeyEx(
248 if (status
!= ERROR_SUCCESS
) {
251 len
= sizeof (component_id
);
252 status
= RegQueryValueEx(
257 (LPBYTE
)component_id
,
260 if (!(status
!= ERROR_SUCCESS
|| data_type
!= REG_SZ
)) {
261 len
= sizeof (net_cfg_instance_id
);
262 status
= RegQueryValueEx(
264 net_cfg_instance_id_string
,
267 (LPBYTE
)net_cfg_instance_id
,
270 if (status
== ERROR_SUCCESS
&& data_type
== REG_SZ
) {
271 if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
272 !strcmp (net_cfg_instance_id
, guid
)) {
273 RegCloseKey (unit_key
);
274 RegCloseKey (netcard_key
);
279 RegCloseKey (unit_key
);
284 RegCloseKey (netcard_key
);
288 static int get_device_guid(
292 int actual_name_size
)
295 HKEY control_net_key
;
300 status
= RegOpenKeyEx(
302 NETWORK_CONNECTIONS_KEY
,
307 if (status
!= ERROR_SUCCESS
) {
314 char connection_string
[256];
318 const char name_string
[] = "Name";
320 len
= sizeof (enum_name
);
321 status
= RegEnumKeyEx(
331 if (status
== ERROR_NO_MORE_ITEMS
)
333 else if (status
!= ERROR_SUCCESS
) {
337 snprintf(connection_string
,
338 sizeof(connection_string
),
339 "%s\\%s\\Connection",
340 NETWORK_CONNECTIONS_KEY
, enum_name
);
342 status
= RegOpenKeyEx(
349 if (status
== ERROR_SUCCESS
) {
350 len
= sizeof (name_data
);
351 status
= RegQueryValueEx(
359 if (status
!= ERROR_SUCCESS
|| name_type
!= REG_SZ
) {
363 if (is_tap_win32_dev(enum_name
)) {
364 snprintf(name
, name_size
, "%s", enum_name
);
366 if (strcmp(actual_name
, "") != 0) {
367 if (strcmp(name_data
, actual_name
) != 0) {
368 RegCloseKey (connection_key
);
374 snprintf(actual_name
, actual_name_size
, "%s", name_data
);
381 RegCloseKey (connection_key
);
386 RegCloseKey (control_net_key
);
394 static int tap_win32_set_status(HANDLE handle
, int status
)
396 unsigned long len
= 0;
398 return DeviceIoControl(handle
, TAP_IOCTL_SET_MEDIA_STATUS
,
399 &status
, sizeof (status
),
400 &status
, sizeof (status
), &len
, NULL
);
403 static void tap_win32_overlapped_init(tap_win32_overlapped_t
* const overlapped
, const HANDLE handle
)
405 overlapped
->handle
= handle
;
407 overlapped
->read_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
408 overlapped
->write_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
410 overlapped
->read_overlapped
.Offset
= 0;
411 overlapped
->read_overlapped
.OffsetHigh
= 0;
412 overlapped
->read_overlapped
.hEvent
= overlapped
->read_event
;
414 overlapped
->write_overlapped
.Offset
= 0;
415 overlapped
->write_overlapped
.OffsetHigh
= 0;
416 overlapped
->write_overlapped
.hEvent
= overlapped
->write_event
;
418 InitializeCriticalSection(&overlapped
->output_queue_cs
);
419 InitializeCriticalSection(&overlapped
->free_list_cs
);
421 overlapped
->output_queue_semaphore
= CreateSemaphore(
422 NULL
, // default security attributes
424 TUN_MAX_BUFFER_COUNT
, // maximum count
425 NULL
); // unnamed semaphore
427 if(!overlapped
->output_queue_semaphore
) {
428 fprintf(stderr
, "error creating output queue semaphore!\n");
431 overlapped
->free_list_semaphore
= CreateSemaphore(
432 NULL
, // default security attributes
433 TUN_MAX_BUFFER_COUNT
, // initial count
434 TUN_MAX_BUFFER_COUNT
, // maximum count
435 NULL
); // unnamed semaphore
437 if(!overlapped
->free_list_semaphore
) {
438 fprintf(stderr
, "error creating free list semaphore!\n");
441 overlapped
->free_list
= overlapped
->output_queue_front
= overlapped
->output_queue_back
= NULL
;
445 for(index
= 0; index
< TUN_MAX_BUFFER_COUNT
; index
++) {
446 tun_buffer_t
* element
= &overlapped
->buffers
[index
];
447 element
->next
= overlapped
->free_list
;
448 overlapped
->free_list
= element
;
451 /* To count buffers, initially no-signal. */
452 overlapped
->tap_semaphore
= CreateSemaphore(NULL
, 0, TUN_MAX_BUFFER_COUNT
, NULL
);
453 if(!overlapped
->tap_semaphore
)
454 fprintf(stderr
, "error creating tap_semaphore.\n");
457 static int tap_win32_write(tap_win32_overlapped_t
*overlapped
,
458 const void *buffer
, unsigned long size
)
460 unsigned long write_size
;
464 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->write_overlapped
,
467 if (!result
&& GetLastError() == ERROR_IO_INCOMPLETE
)
468 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
470 result
= WriteFile(overlapped
->handle
, buffer
, size
,
471 &write_size
, &overlapped
->write_overlapped
);
474 switch (error
= GetLastError())
476 case ERROR_IO_PENDING
:
477 #ifndef TUN_ASYNCHRONOUS_WRITES
478 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
489 static DWORD WINAPI
tap_win32_thread_entry(LPVOID param
)
491 tap_win32_overlapped_t
*overlapped
= (tap_win32_overlapped_t
*)param
;
492 unsigned long read_size
;
495 tun_buffer_t
* buffer
= get_buffer_from_free_list(overlapped
);
499 result
= ReadFile(overlapped
->handle
,
501 sizeof(buffer
->buffer
),
503 &overlapped
->read_overlapped
);
505 dwError
= GetLastError();
506 if (dwError
== ERROR_IO_PENDING
) {
507 WaitForSingleObject(overlapped
->read_event
, INFINITE
);
508 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->read_overlapped
,
511 #ifdef DEBUG_TAP_WIN32
513 dwError
= GetLastError();
514 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
515 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
516 (LPTSTR
) & lpBuffer
, 0, NULL
);
517 fprintf(stderr
, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError
, lpBuffer
);
518 LocalFree( lpBuffer
);
522 #ifdef DEBUG_TAP_WIN32
524 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
525 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
526 (LPTSTR
) & lpBuffer
, 0, NULL
);
527 fprintf(stderr
, "Tap-Win32: Error ReadFile %d - %s\n", dwError
, lpBuffer
);
528 LocalFree( lpBuffer
);
534 buffer
->read_size
= read_size
;
535 put_buffer_on_output_queue(overlapped
, buffer
);
536 ReleaseSemaphore(overlapped
->tap_semaphore
, 1, NULL
);
537 buffer
= get_buffer_from_free_list(overlapped
);
544 static int tap_win32_read(tap_win32_overlapped_t
*overlapped
,
545 uint8_t **pbuf
, int max_size
)
549 tun_buffer_t
* buffer
= get_buffer_from_output_queue_immediate(overlapped
);
552 *pbuf
= buffer
->buffer
;
553 size
= (int)buffer
->read_size
;
554 if(size
> max_size
) {
562 static void tap_win32_free_buffer(tap_win32_overlapped_t
*overlapped
,
565 tun_buffer_t
* buffer
= (tun_buffer_t
*)pbuf
;
566 put_buffer_on_free_list(overlapped
, buffer
);
569 static int tap_win32_open(tap_win32_overlapped_t
**phandle
,
570 const char *prefered_name
)
572 char device_path
[256];
573 char device_guid
[0x100];
577 char name_buffer
[0x100] = {0, };
587 if (prefered_name
!= NULL
)
588 snprintf(name_buffer
, sizeof(name_buffer
), "%s", prefered_name
);
590 rc
= get_device_guid(device_guid
, sizeof(device_guid
), name_buffer
, sizeof(name_buffer
));
594 snprintf (device_path
, sizeof(device_path
), "%s%s%s",
599 handle
= CreateFile (
601 GENERIC_READ
| GENERIC_WRITE
,
605 FILE_ATTRIBUTE_SYSTEM
| FILE_FLAG_OVERLAPPED
,
608 if (handle
== INVALID_HANDLE_VALUE
) {
612 bret
= DeviceIoControl(handle
, TAP_IOCTL_GET_VERSION
,
613 &version
, sizeof (version
),
614 &version
, sizeof (version
), &version_len
, NULL
);
621 if (!tap_win32_set_status(handle
, TRUE
)) {
625 tap_win32_overlapped_init(&tap_overlapped
, handle
);
627 *phandle
= &tap_overlapped
;
629 hThread
= CreateThread(NULL
, 0, tap_win32_thread_entry
,
630 (LPVOID
)&tap_overlapped
, 0, &idThread
);
634 /********************************************/
636 typedef struct TAPState
{
638 tap_win32_overlapped_t
*handle
;
641 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
643 TAPState
*s
= opaque
;
645 tap_win32_write(s
->handle
, buf
, size
);
648 static void tap_win32_send(void *opaque
)
650 TAPState
*s
= opaque
;
655 size
= tap_win32_read(s
->handle
, &buf
, max_size
);
657 qemu_send_packet(s
->vc
, buf
, size
);
658 tap_win32_free_buffer(s
->handle
, buf
);
662 int tap_win32_init(VLANState
*vlan
, const char *model
,
663 const char *name
, const char *ifname
)
667 s
= qemu_mallocz(sizeof(TAPState
));
670 if (tap_win32_open(&s
->handle
, ifname
) < 0) {
671 printf("tap: Could not open '%s'\n", ifname
);
675 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
677 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
678 "tap: ifname=%s", ifname
);
680 qemu_add_wait_object(s
->handle
->tap_semaphore
, tap_win32_send
, s
);