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, see <http://www.gnu.org/licenses/>.
31 #include "qemu-common.h"
34 #include "qemu-error.h"
43 #define TAP_CONTROL_CODE(request,method) \
44 CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
46 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
47 #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
48 #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
49 #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
50 #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
51 #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
52 #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
53 #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
54 #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
60 #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
62 #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
64 //======================
65 // Filesystem prefixes
66 //======================
68 #define USERMODEDEVICEDIR "\\\\.\\Global\\"
69 #define TAPSUFFIX ".tap"
72 //======================
73 // Compile time configuration
74 //======================
76 //#define DEBUG_TAP_WIN32
78 #define TUN_ASYNCHRONOUS_WRITES 1
80 #define TUN_BUFFER_SIZE 1560
81 #define TUN_MAX_BUFFER_COUNT 32
84 * The data member "buffer" must be the first element in the tun_buffer
85 * structure. See the function, tap_win32_free_buffer.
87 typedef struct tun_buffer_s {
88 unsigned char buffer [TUN_BUFFER_SIZE];
89 unsigned long read_size;
90 struct tun_buffer_s* next;
93 typedef struct tap_win32_overlapped {
97 HANDLE output_queue_semaphore;
98 HANDLE free_list_semaphore;
100 CRITICAL_SECTION output_queue_cs;
101 CRITICAL_SECTION free_list_cs;
102 OVERLAPPED read_overlapped;
103 OVERLAPPED write_overlapped;
104 tun_buffer_t buffers[TUN_MAX_BUFFER_COUNT];
105 tun_buffer_t* free_list;
106 tun_buffer_t* output_queue_front;
107 tun_buffer_t* output_queue_back;
108 } tap_win32_overlapped_t;
110 static tap_win32_overlapped_t tap_overlapped;
112 static tun_buffer_t* get_buffer_from_free_list(tap_win32_overlapped_t* const overlapped)
114 tun_buffer_t* buffer = NULL;
115 WaitForSingleObject(overlapped->free_list_semaphore, INFINITE);
116 EnterCriticalSection(&overlapped->free_list_cs);
117 buffer = overlapped->free_list;
118 // assert(buffer != NULL);
119 overlapped->free_list = buffer->next;
120 LeaveCriticalSection(&overlapped->free_list_cs);
125 static void put_buffer_on_free_list(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
127 EnterCriticalSection(&overlapped->free_list_cs);
128 buffer->next = overlapped->free_list;
129 overlapped->free_list = buffer;
130 LeaveCriticalSection(&overlapped->free_list_cs);
131 ReleaseSemaphore(overlapped->free_list_semaphore, 1, NULL);
134 static tun_buffer_t* get_buffer_from_output_queue(tap_win32_overlapped_t* const overlapped, const int block)
136 tun_buffer_t* buffer = NULL;
137 DWORD result, timeout = block ? INFINITE : 0L;
140 result = WaitForSingleObject(overlapped->output_queue_semaphore, timeout);
144 // The semaphore object was signaled.
146 EnterCriticalSection(&overlapped->output_queue_cs);
148 buffer = overlapped->output_queue_front;
149 overlapped->output_queue_front = buffer->next;
151 if(overlapped->output_queue_front == NULL) {
152 overlapped->output_queue_back = NULL;
155 LeaveCriticalSection(&overlapped->output_queue_cs);
158 // Semaphore was nonsignaled, so a time-out occurred.
160 // Cannot open another window.
167 static tun_buffer_t* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t* const overlapped)
169 return get_buffer_from_output_queue(overlapped, 0);
172 static void put_buffer_on_output_queue(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
174 EnterCriticalSection(&overlapped->output_queue_cs);
176 if(overlapped->output_queue_front == NULL && overlapped->output_queue_back == NULL) {
177 overlapped->output_queue_front = overlapped->output_queue_back = buffer;
180 overlapped->output_queue_back->next = buffer;
181 overlapped->output_queue_back = buffer;
184 LeaveCriticalSection(&overlapped->output_queue_cs);
186 ReleaseSemaphore(overlapped->output_queue_semaphore, 1, NULL);
190 static int is_tap_win32_dev(const char *guid)
197 status = RegOpenKeyEx(
204 if (status != ERROR_SUCCESS) {
210 char unit_string[256];
212 char component_id_string[] = "ComponentId";
213 char component_id[256];
214 char net_cfg_instance_id_string[] = "NetCfgInstanceId";
215 char net_cfg_instance_id[256];
218 len = sizeof (enum_name);
219 status = RegEnumKeyEx(
229 if (status == ERROR_NO_MORE_ITEMS)
231 else if (status != ERROR_SUCCESS) {
235 snprintf (unit_string, sizeof(unit_string), "%s\\%s",
236 ADAPTER_KEY, enum_name);
238 status = RegOpenKeyEx(
245 if (status != ERROR_SUCCESS) {
248 len = sizeof (component_id);
249 status = RegQueryValueEx(
254 (LPBYTE)component_id,
257 if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
258 len = sizeof (net_cfg_instance_id);
259 status = RegQueryValueEx(
261 net_cfg_instance_id_string,
264 (LPBYTE)net_cfg_instance_id,
267 if (status == ERROR_SUCCESS && data_type == REG_SZ) {
268 if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
269 !strcmp (net_cfg_instance_id, guid)) {
270 RegCloseKey (unit_key);
271 RegCloseKey (netcard_key);
276 RegCloseKey (unit_key);
281 RegCloseKey (netcard_key);
285 static int get_device_guid(
289 int actual_name_size)
292 HKEY control_net_key;
297 status = RegOpenKeyEx(
299 NETWORK_CONNECTIONS_KEY,
304 if (status != ERROR_SUCCESS) {
311 char connection_string[256];
315 const char name_string[] = "Name";
317 len = sizeof (enum_name);
318 status = RegEnumKeyEx(
328 if (status == ERROR_NO_MORE_ITEMS)
330 else if (status != ERROR_SUCCESS) {
334 snprintf(connection_string,
335 sizeof(connection_string),
336 "%s\\%s\\Connection",
337 NETWORK_CONNECTIONS_KEY, enum_name);
339 status = RegOpenKeyEx(
346 if (status == ERROR_SUCCESS) {
347 len = sizeof (name_data);
348 status = RegQueryValueEx(
356 if (status != ERROR_SUCCESS || name_type != REG_SZ) {
360 if (is_tap_win32_dev(enum_name)) {
361 snprintf(name, name_size, "%s", enum_name);
363 if (strcmp(actual_name, "") != 0) {
364 if (strcmp(name_data, actual_name) != 0) {
365 RegCloseKey (connection_key);
371 snprintf(actual_name, actual_name_size, "%s", name_data);
378 RegCloseKey (connection_key);
383 RegCloseKey (control_net_key);
391 static int tap_win32_set_status(HANDLE handle, int status)
393 unsigned long len = 0;
395 return DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS,
396 &status, sizeof (status),
397 &status, sizeof (status), &len, NULL);
400 static void tap_win32_overlapped_init(tap_win32_overlapped_t* const overlapped, const HANDLE handle)
402 overlapped->handle = handle;
404 overlapped->read_event = CreateEvent(NULL, FALSE, FALSE, NULL);
405 overlapped->write_event = CreateEvent(NULL, FALSE, FALSE, NULL);
407 overlapped->read_overlapped.Offset = 0;
408 overlapped->read_overlapped.OffsetHigh = 0;
409 overlapped->read_overlapped.hEvent = overlapped->read_event;
411 overlapped->write_overlapped.Offset = 0;
412 overlapped->write_overlapped.OffsetHigh = 0;
413 overlapped->write_overlapped.hEvent = overlapped->write_event;
415 InitializeCriticalSection(&overlapped->output_queue_cs);
416 InitializeCriticalSection(&overlapped->free_list_cs);
418 overlapped->output_queue_semaphore = CreateSemaphore(
419 NULL, // default security attributes
421 TUN_MAX_BUFFER_COUNT, // maximum count
422 NULL); // unnamed semaphore
424 if(!overlapped->output_queue_semaphore) {
425 fprintf(stderr, "error creating output queue semaphore!\n");
428 overlapped->free_list_semaphore = CreateSemaphore(
429 NULL, // default security attributes
430 TUN_MAX_BUFFER_COUNT, // initial count
431 TUN_MAX_BUFFER_COUNT, // maximum count
432 NULL); // unnamed semaphore
434 if(!overlapped->free_list_semaphore) {
435 fprintf(stderr, "error creating free list semaphore!\n");
438 overlapped->free_list = overlapped->output_queue_front = overlapped->output_queue_back = NULL;
442 for(index = 0; index < TUN_MAX_BUFFER_COUNT; index++) {
443 tun_buffer_t* element = &overlapped->buffers[index];
444 element->next = overlapped->free_list;
445 overlapped->free_list = element;
448 /* To count buffers, initially no-signal. */
449 overlapped->tap_semaphore = CreateSemaphore(NULL, 0, TUN_MAX_BUFFER_COUNT, NULL);
450 if(!overlapped->tap_semaphore)
451 fprintf(stderr, "error creating tap_semaphore.\n");
454 static int tap_win32_write(tap_win32_overlapped_t *overlapped,
455 const void *buffer, unsigned long size)
457 unsigned long write_size;
461 result = GetOverlappedResult( overlapped->handle, &overlapped->write_overlapped,
464 if (!result && GetLastError() == ERROR_IO_INCOMPLETE)
465 WaitForSingleObject(overlapped->write_event, INFINITE);
467 result = WriteFile(overlapped->handle, buffer, size,
468 &write_size, &overlapped->write_overlapped);
471 switch (error = GetLastError())
473 case ERROR_IO_PENDING:
474 #ifndef TUN_ASYNCHRONOUS_WRITES
475 WaitForSingleObject(overlapped->write_event, INFINITE);
486 static DWORD WINAPI tap_win32_thread_entry(LPVOID param)
488 tap_win32_overlapped_t *overlapped = (tap_win32_overlapped_t*)param;
489 unsigned long read_size;
492 tun_buffer_t* buffer = get_buffer_from_free_list(overlapped);
496 result = ReadFile(overlapped->handle,
498 sizeof(buffer->buffer),
500 &overlapped->read_overlapped);
502 dwError = GetLastError();
503 if (dwError == ERROR_IO_PENDING) {
504 WaitForSingleObject(overlapped->read_event, INFINITE);
505 result = GetOverlappedResult( overlapped->handle, &overlapped->read_overlapped,
508 #ifdef DEBUG_TAP_WIN32
510 dwError = GetLastError();
511 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
512 NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
513 (LPTSTR) & lpBuffer, 0, NULL );
514 fprintf(stderr, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError, lpBuffer);
515 LocalFree( lpBuffer );
519 #ifdef DEBUG_TAP_WIN32
521 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
522 NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
523 (LPTSTR) & lpBuffer, 0, NULL );
524 fprintf(stderr, "Tap-Win32: Error ReadFile %d - %s\n", dwError, lpBuffer);
525 LocalFree( lpBuffer );
531 buffer->read_size = read_size;
532 put_buffer_on_output_queue(overlapped, buffer);
533 ReleaseSemaphore(overlapped->tap_semaphore, 1, NULL);
534 buffer = get_buffer_from_free_list(overlapped);
541 static int tap_win32_read(tap_win32_overlapped_t *overlapped,
542 uint8_t **pbuf, int max_size)
546 tun_buffer_t* buffer = get_buffer_from_output_queue_immediate(overlapped);
549 *pbuf = buffer->buffer;
550 size = (int)buffer->read_size;
551 if(size > max_size) {
559 static void tap_win32_free_buffer(tap_win32_overlapped_t *overlapped,
562 tun_buffer_t* buffer = (tun_buffer_t*)pbuf;
563 put_buffer_on_free_list(overlapped, buffer);
566 static int tap_win32_open(tap_win32_overlapped_t **phandle,
567 const char *prefered_name)
569 char device_path[256];
570 char device_guid[0x100];
574 char name_buffer[0x100] = {0, };
583 if (prefered_name != NULL)
584 snprintf(name_buffer, sizeof(name_buffer), "%s", prefered_name);
586 rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer));
590 snprintf (device_path, sizeof(device_path), "%s%s%s",
595 handle = CreateFile (
597 GENERIC_READ | GENERIC_WRITE,
601 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
604 if (handle == INVALID_HANDLE_VALUE) {
608 bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION,
609 &version, sizeof (version),
610 &version, sizeof (version), &version_len, NULL);
617 if (!tap_win32_set_status(handle, TRUE)) {
621 tap_win32_overlapped_init(&tap_overlapped, handle);
623 *phandle = &tap_overlapped;
625 CreateThread(NULL, 0, tap_win32_thread_entry,
626 (LPVOID)&tap_overlapped, 0, &idThread);
630 /********************************************/
632 typedef struct TAPState {
634 tap_win32_overlapped_t *handle;
637 static void tap_cleanup(NetClientState *nc)
639 TAPState *s = DO_UPCAST(TAPState, nc, nc);
641 qemu_del_wait_object(s->handle->tap_semaphore, NULL, NULL);
643 /* FIXME: need to kill thread and close file handle:
648 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
650 TAPState *s = DO_UPCAST(TAPState, nc, nc);
652 return tap_win32_write(s->handle, buf, size);
655 static void tap_win32_send(void *opaque)
657 TAPState *s = opaque;
662 size = tap_win32_read(s->handle, &buf, max_size);
664 qemu_send_packet(&s->nc, buf, size);
665 tap_win32_free_buffer(s->handle, buf);
669 static NetClientInfo net_tap_win32_info = {
670 .type = NET_CLIENT_OPTIONS_KIND_TAP,
671 .size = sizeof(TAPState),
672 .receive = tap_receive,
673 .cleanup = tap_cleanup,
676 static int tap_win32_init(NetClientState *peer, const char *model,
677 const char *name, const char *ifname)
681 tap_win32_overlapped_t *handle;
683 if (tap_win32_open(&handle, ifname) < 0) {
684 printf("tap: Could not open '%s'\n", ifname);
688 nc = qemu_new_net_client(&net_tap_win32_info, peer, model, name);
690 s = DO_UPCAST(TAPState, nc, nc);
692 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
693 "tap: ifname=%s", ifname);
697 qemu_add_wait_object(s->handle->tap_semaphore, tap_win32_send, s);
702 int net_init_tap(const NetClientOptions *opts, const char *name,
703 NetClientState *peer)
705 const NetdevTapOptions *tap;
707 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
710 if (!tap->has_ifname) {
711 error_report("tap: no interface name");
715 if (tap_win32_init(peer, "tap", name, tap->ifname) == -1) {
722 int tap_has_ufo(NetClientState *nc)
727 int tap_has_vnet_hdr(NetClientState *nc)
732 int tap_probe_vnet_hdr_len(int fd, int len)
737 void tap_fd_set_vnet_hdr_len(int fd, int len)
741 void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr)
745 void tap_set_offload(NetClientState *nc, int csum, int tso4,
746 int tso6, int ecn, int ufo)
750 struct vhost_net *tap_get_vhost_net(NetClientState *nc)