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"
32 #include "clients.h" /* net_init_tap */
34 #include "net/tap.h" /* tap_has_ufo, ... */
35 #include "sysemu/sysemu.h"
36 #include "qemu/error-report.h"
45 #define TAP_CONTROL_CODE(request,method) \
46 CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
48 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
49 #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
50 #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
51 #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
52 #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
53 #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
54 #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
55 #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
56 #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
62 #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
64 #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
66 //======================
67 // Filesystem prefixes
68 //======================
70 #define USERMODEDEVICEDIR "\\\\.\\Global\\"
71 #define TAPSUFFIX ".tap"
74 //======================
75 // Compile time configuration
76 //======================
78 //#define DEBUG_TAP_WIN32
80 #define TUN_ASYNCHRONOUS_WRITES 1
82 #define TUN_BUFFER_SIZE 1560
83 #define TUN_MAX_BUFFER_COUNT 32
86 * The data member "buffer" must be the first element in the tun_buffer
87 * structure. See the function, tap_win32_free_buffer.
89 typedef struct tun_buffer_s {
90 unsigned char buffer [TUN_BUFFER_SIZE];
91 unsigned long read_size;
92 struct tun_buffer_s* next;
95 typedef struct tap_win32_overlapped {
99 HANDLE output_queue_semaphore;
100 HANDLE free_list_semaphore;
101 HANDLE tap_semaphore;
102 CRITICAL_SECTION output_queue_cs;
103 CRITICAL_SECTION free_list_cs;
104 OVERLAPPED read_overlapped;
105 OVERLAPPED write_overlapped;
106 tun_buffer_t buffers[TUN_MAX_BUFFER_COUNT];
107 tun_buffer_t* free_list;
108 tun_buffer_t* output_queue_front;
109 tun_buffer_t* output_queue_back;
110 } tap_win32_overlapped_t;
112 static tap_win32_overlapped_t tap_overlapped;
114 static tun_buffer_t* get_buffer_from_free_list(tap_win32_overlapped_t* const overlapped)
116 tun_buffer_t* buffer = NULL;
117 WaitForSingleObject(overlapped->free_list_semaphore, INFINITE);
118 EnterCriticalSection(&overlapped->free_list_cs);
119 buffer = overlapped->free_list;
120 // assert(buffer != NULL);
121 overlapped->free_list = buffer->next;
122 LeaveCriticalSection(&overlapped->free_list_cs);
127 static void put_buffer_on_free_list(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
129 EnterCriticalSection(&overlapped->free_list_cs);
130 buffer->next = overlapped->free_list;
131 overlapped->free_list = buffer;
132 LeaveCriticalSection(&overlapped->free_list_cs);
133 ReleaseSemaphore(overlapped->free_list_semaphore, 1, NULL);
136 static tun_buffer_t* get_buffer_from_output_queue(tap_win32_overlapped_t* const overlapped, const int block)
138 tun_buffer_t* buffer = NULL;
139 DWORD result, timeout = block ? INFINITE : 0L;
142 result = WaitForSingleObject(overlapped->output_queue_semaphore, timeout);
146 // The semaphore object was signaled.
148 EnterCriticalSection(&overlapped->output_queue_cs);
150 buffer = overlapped->output_queue_front;
151 overlapped->output_queue_front = buffer->next;
153 if(overlapped->output_queue_front == NULL) {
154 overlapped->output_queue_back = NULL;
157 LeaveCriticalSection(&overlapped->output_queue_cs);
160 // Semaphore was nonsignaled, so a time-out occurred.
162 // Cannot open another window.
169 static tun_buffer_t* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t* const overlapped)
171 return get_buffer_from_output_queue(overlapped, 0);
174 static void put_buffer_on_output_queue(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
176 EnterCriticalSection(&overlapped->output_queue_cs);
178 if(overlapped->output_queue_front == NULL && overlapped->output_queue_back == NULL) {
179 overlapped->output_queue_front = overlapped->output_queue_back = buffer;
182 overlapped->output_queue_back->next = buffer;
183 overlapped->output_queue_back = buffer;
186 LeaveCriticalSection(&overlapped->output_queue_cs);
188 ReleaseSemaphore(overlapped->output_queue_semaphore, 1, NULL);
192 static int is_tap_win32_dev(const char *guid)
199 status = RegOpenKeyEx(
206 if (status != ERROR_SUCCESS) {
212 char unit_string[256];
214 char component_id_string[] = "ComponentId";
215 char component_id[256];
216 char net_cfg_instance_id_string[] = "NetCfgInstanceId";
217 char net_cfg_instance_id[256];
220 len = sizeof (enum_name);
221 status = RegEnumKeyEx(
231 if (status == ERROR_NO_MORE_ITEMS)
233 else if (status != ERROR_SUCCESS) {
237 snprintf (unit_string, sizeof(unit_string), "%s\\%s",
238 ADAPTER_KEY, enum_name);
240 status = RegOpenKeyEx(
247 if (status != ERROR_SUCCESS) {
250 len = sizeof (component_id);
251 status = RegQueryValueEx(
256 (LPBYTE)component_id,
259 if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
260 len = sizeof (net_cfg_instance_id);
261 status = RegQueryValueEx(
263 net_cfg_instance_id_string,
266 (LPBYTE)net_cfg_instance_id,
269 if (status == ERROR_SUCCESS && data_type == REG_SZ) {
270 if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
271 !strcmp (net_cfg_instance_id, guid)) {
272 RegCloseKey (unit_key);
273 RegCloseKey (netcard_key);
278 RegCloseKey (unit_key);
283 RegCloseKey (netcard_key);
287 static int get_device_guid(
291 int actual_name_size)
294 HKEY control_net_key;
299 status = RegOpenKeyEx(
301 NETWORK_CONNECTIONS_KEY,
306 if (status != ERROR_SUCCESS) {
313 char connection_string[256];
317 const char name_string[] = "Name";
319 len = sizeof (enum_name);
320 status = RegEnumKeyEx(
330 if (status == ERROR_NO_MORE_ITEMS)
332 else if (status != ERROR_SUCCESS) {
336 snprintf(connection_string,
337 sizeof(connection_string),
338 "%s\\%s\\Connection",
339 NETWORK_CONNECTIONS_KEY, enum_name);
341 status = RegOpenKeyEx(
348 if (status == ERROR_SUCCESS) {
349 len = sizeof (name_data);
350 status = RegQueryValueEx(
358 if (status != ERROR_SUCCESS || name_type != REG_SZ) {
362 if (is_tap_win32_dev(enum_name)) {
363 snprintf(name, name_size, "%s", enum_name);
365 if (strcmp(actual_name, "") != 0) {
366 if (strcmp(name_data, actual_name) != 0) {
367 RegCloseKey (connection_key);
373 snprintf(actual_name, actual_name_size, "%s", name_data);
380 RegCloseKey (connection_key);
385 RegCloseKey (control_net_key);
393 static int tap_win32_set_status(HANDLE handle, int status)
395 unsigned long len = 0;
397 return DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS,
398 &status, sizeof (status),
399 &status, sizeof (status), &len, NULL);
402 static void tap_win32_overlapped_init(tap_win32_overlapped_t* const overlapped, const HANDLE handle)
404 overlapped->handle = handle;
406 overlapped->read_event = CreateEvent(NULL, FALSE, FALSE, NULL);
407 overlapped->write_event = CreateEvent(NULL, FALSE, FALSE, NULL);
409 overlapped->read_overlapped.Offset = 0;
410 overlapped->read_overlapped.OffsetHigh = 0;
411 overlapped->read_overlapped.hEvent = overlapped->read_event;
413 overlapped->write_overlapped.Offset = 0;
414 overlapped->write_overlapped.OffsetHigh = 0;
415 overlapped->write_overlapped.hEvent = overlapped->write_event;
417 InitializeCriticalSection(&overlapped->output_queue_cs);
418 InitializeCriticalSection(&overlapped->free_list_cs);
420 overlapped->output_queue_semaphore = CreateSemaphore(
421 NULL, // default security attributes
423 TUN_MAX_BUFFER_COUNT, // maximum count
424 NULL); // unnamed semaphore
426 if(!overlapped->output_queue_semaphore) {
427 fprintf(stderr, "error creating output queue semaphore!\n");
430 overlapped->free_list_semaphore = CreateSemaphore(
431 NULL, // default security attributes
432 TUN_MAX_BUFFER_COUNT, // initial count
433 TUN_MAX_BUFFER_COUNT, // maximum count
434 NULL); // unnamed semaphore
436 if(!overlapped->free_list_semaphore) {
437 fprintf(stderr, "error creating free list semaphore!\n");
440 overlapped->free_list = overlapped->output_queue_front = overlapped->output_queue_back = NULL;
444 for(index = 0; index < TUN_MAX_BUFFER_COUNT; index++) {
445 tun_buffer_t* element = &overlapped->buffers[index];
446 element->next = overlapped->free_list;
447 overlapped->free_list = element;
450 /* To count buffers, initially no-signal. */
451 overlapped->tap_semaphore = CreateSemaphore(NULL, 0, TUN_MAX_BUFFER_COUNT, NULL);
452 if(!overlapped->tap_semaphore)
453 fprintf(stderr, "error creating tap_semaphore.\n");
456 static int tap_win32_write(tap_win32_overlapped_t *overlapped,
457 const void *buffer, unsigned long size)
459 unsigned long write_size;
463 result = GetOverlappedResult( overlapped->handle, &overlapped->write_overlapped,
466 if (!result && GetLastError() == ERROR_IO_INCOMPLETE)
467 WaitForSingleObject(overlapped->write_event, INFINITE);
469 result = WriteFile(overlapped->handle, buffer, size,
470 &write_size, &overlapped->write_overlapped);
473 switch (error = GetLastError())
475 case ERROR_IO_PENDING:
476 #ifndef TUN_ASYNCHRONOUS_WRITES
477 WaitForSingleObject(overlapped->write_event, INFINITE);
488 static DWORD WINAPI tap_win32_thread_entry(LPVOID param)
490 tap_win32_overlapped_t *overlapped = (tap_win32_overlapped_t*)param;
491 unsigned long read_size;
494 tun_buffer_t* buffer = get_buffer_from_free_list(overlapped);
498 result = ReadFile(overlapped->handle,
500 sizeof(buffer->buffer),
502 &overlapped->read_overlapped);
504 dwError = GetLastError();
505 if (dwError == ERROR_IO_PENDING) {
506 WaitForSingleObject(overlapped->read_event, INFINITE);
507 result = GetOverlappedResult( overlapped->handle, &overlapped->read_overlapped,
510 #ifdef DEBUG_TAP_WIN32
512 dwError = GetLastError();
513 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
514 NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
515 (LPTSTR) & lpBuffer, 0, NULL );
516 fprintf(stderr, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError, lpBuffer);
517 LocalFree( lpBuffer );
521 #ifdef DEBUG_TAP_WIN32
523 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
524 NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
525 (LPTSTR) & lpBuffer, 0, NULL );
526 fprintf(stderr, "Tap-Win32: Error ReadFile %d - %s\n", dwError, lpBuffer);
527 LocalFree( lpBuffer );
533 buffer->read_size = read_size;
534 put_buffer_on_output_queue(overlapped, buffer);
535 ReleaseSemaphore(overlapped->tap_semaphore, 1, NULL);
536 buffer = get_buffer_from_free_list(overlapped);
543 static int tap_win32_read(tap_win32_overlapped_t *overlapped,
544 uint8_t **pbuf, int max_size)
548 tun_buffer_t* buffer = get_buffer_from_output_queue_immediate(overlapped);
551 *pbuf = buffer->buffer;
552 size = (int)buffer->read_size;
553 if(size > max_size) {
561 static void tap_win32_free_buffer(tap_win32_overlapped_t *overlapped,
564 tun_buffer_t* buffer = (tun_buffer_t*)pbuf;
565 put_buffer_on_free_list(overlapped, buffer);
568 static int tap_win32_open(tap_win32_overlapped_t **phandle,
569 const char *preferred_name)
571 char device_path[256];
572 char device_guid[0x100];
576 char name_buffer[0x100] = {0, };
585 if (preferred_name != NULL) {
586 snprintf(name_buffer, sizeof(name_buffer), "%s", preferred_name);
589 rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer));
593 snprintf (device_path, sizeof(device_path), "%s%s%s",
598 handle = CreateFile (
600 GENERIC_READ | GENERIC_WRITE,
604 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
607 if (handle == INVALID_HANDLE_VALUE) {
611 bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION,
612 &version, sizeof (version),
613 &version, sizeof (version), &version_len, NULL);
620 if (!tap_win32_set_status(handle, TRUE)) {
624 tap_win32_overlapped_init(&tap_overlapped, handle);
626 *phandle = &tap_overlapped;
628 CreateThread(NULL, 0, tap_win32_thread_entry,
629 (LPVOID)&tap_overlapped, 0, &idThread);
633 /********************************************/
635 typedef struct TAPState {
637 tap_win32_overlapped_t *handle;
640 static void tap_cleanup(NetClientState *nc)
642 TAPState *s = DO_UPCAST(TAPState, nc, nc);
644 qemu_del_wait_object(s->handle->tap_semaphore, NULL, NULL);
646 /* FIXME: need to kill thread and close file handle:
651 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
653 TAPState *s = DO_UPCAST(TAPState, nc, nc);
655 return tap_win32_write(s->handle, buf, size);
658 static void tap_win32_send(void *opaque)
660 TAPState *s = opaque;
665 size = tap_win32_read(s->handle, &buf, max_size);
667 qemu_send_packet(&s->nc, buf, size);
668 tap_win32_free_buffer(s->handle, buf);
672 static bool tap_has_ufo(NetClientState *nc)
677 static bool tap_has_vnet_hdr(NetClientState *nc)
682 int tap_probe_vnet_hdr_len(int fd, int len)
687 void tap_fd_set_vnet_hdr_len(int fd, int len)
691 int tap_fd_set_vnet_le(int fd, int is_le)
696 int tap_fd_set_vnet_be(int fd, int is_be)
701 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
705 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
706 int tso6, int ecn, int ufo)
710 struct vhost_net *tap_get_vhost_net(NetClientState *nc)
715 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
720 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
725 static NetClientInfo net_tap_win32_info = {
726 .type = NET_CLIENT_OPTIONS_KIND_TAP,
727 .size = sizeof(TAPState),
728 .receive = tap_receive,
729 .cleanup = tap_cleanup,
730 .has_ufo = tap_has_ufo,
731 .has_vnet_hdr = tap_has_vnet_hdr,
732 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
733 .using_vnet_hdr = tap_using_vnet_hdr,
734 .set_offload = tap_set_offload,
735 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
738 static int tap_win32_init(NetClientState *peer, const char *model,
739 const char *name, const char *ifname)
743 tap_win32_overlapped_t *handle;
745 if (tap_win32_open(&handle, ifname) < 0) {
746 printf("tap: Could not open '%s'\n", ifname);
750 nc = qemu_new_net_client(&net_tap_win32_info, peer, model, name);
752 s = DO_UPCAST(TAPState, nc, nc);
754 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
755 "tap: ifname=%s", ifname);
759 qemu_add_wait_object(s->handle->tap_semaphore, tap_win32_send, s);
764 int net_init_tap(const NetClientOptions *opts, const char *name,
765 NetClientState *peer, Error **errp)
767 /* FIXME error_setg(errp, ...) on failure */
768 const NetdevTapOptions *tap;
770 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
773 if (!tap->has_ifname) {
774 error_report("tap: no interface name");
778 if (tap_win32_init(peer, "tap", name, tap->ifname) == -1) {
785 int tap_enable(NetClientState *nc)
790 int tap_disable(NetClientState *nc)