Correct errno return for getsockopt(), by Thayne Harbaugh.
[qemu/qemu_0_9_1_stable.git] / tap-win32.c
blobbfec14578a51c23286215d3f53c018372c60d67e
1 /*
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
5 * James Yonan.
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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "qemu-common.h"
30 #include "net.h"
31 #include "sysemu.h"
32 #include <stdio.h>
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
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>
41 #undef PCIBus
43 //=============
44 // TAP IOCTLs
45 //=============
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)
60 //=================
61 // Registry keys
62 //=================
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 1
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;
95 } tun_buffer_t;
97 typedef struct tap_win32_overlapped {
98 HANDLE handle;
99 HANDLE read_event;
100 HANDLE write_event;
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);
125 buffer->next = NULL;
126 return buffer;
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;
143 // Non-blocking call
144 result = WaitForSingleObject(overlapped->output_queue_semaphore, timeout);
146 switch (result)
148 // The semaphore object was signaled.
149 case WAIT_OBJECT_0:
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);
160 break;
162 // Semaphore was nonsignaled, so a time-out occurred.
163 case WAIT_TIMEOUT:
164 // Cannot open another window.
165 break;
168 return buffer;
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;
182 } else {
183 buffer->next = NULL;
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)
196 HKEY netcard_key;
197 LONG status;
198 DWORD len;
199 int i = 0;
201 status = RegOpenKeyEx(
202 HKEY_LOCAL_MACHINE,
203 ADAPTER_KEY,
205 KEY_READ,
206 &netcard_key);
208 if (status != ERROR_SUCCESS) {
209 return FALSE;
212 for (;;) {
213 char enum_name[256];
214 char unit_string[256];
215 HKEY unit_key;
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];
220 DWORD data_type;
222 len = sizeof (enum_name);
223 status = RegEnumKeyEx(
224 netcard_key,
226 enum_name,
227 &len,
228 NULL,
229 NULL,
230 NULL,
231 NULL);
233 if (status == ERROR_NO_MORE_ITEMS)
234 break;
235 else if (status != ERROR_SUCCESS) {
236 return FALSE;
239 snprintf (unit_string, sizeof(unit_string), "%s\\%s",
240 ADAPTER_KEY, enum_name);
242 status = RegOpenKeyEx(
243 HKEY_LOCAL_MACHINE,
244 unit_string,
246 KEY_READ,
247 &unit_key);
249 if (status != ERROR_SUCCESS) {
250 return FALSE;
251 } else {
252 len = sizeof (component_id);
253 status = RegQueryValueEx(
254 unit_key,
255 component_id_string,
256 NULL,
257 &data_type,
258 component_id,
259 &len);
261 if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
262 len = sizeof (net_cfg_instance_id);
263 status = RegQueryValueEx(
264 unit_key,
265 net_cfg_instance_id_string,
266 NULL,
267 &data_type,
268 net_cfg_instance_id,
269 &len);
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);
276 return TRUE;
280 RegCloseKey (unit_key);
282 ++i;
285 RegCloseKey (netcard_key);
286 return FALSE;
289 static int get_device_guid(
290 char *name,
291 int name_size,
292 char *actual_name,
293 int actual_name_size)
295 LONG status;
296 HKEY control_net_key;
297 DWORD len;
298 int i = 0;
299 int stop = 0;
301 status = RegOpenKeyEx(
302 HKEY_LOCAL_MACHINE,
303 NETWORK_CONNECTIONS_KEY,
305 KEY_READ,
306 &control_net_key);
308 if (status != ERROR_SUCCESS) {
309 return -1;
312 while (!stop)
314 char enum_name[256];
315 char connection_string[256];
316 HKEY connection_key;
317 char name_data[256];
318 DWORD name_type;
319 const char name_string[] = "Name";
321 len = sizeof (enum_name);
322 status = RegEnumKeyEx(
323 control_net_key,
325 enum_name,
326 &len,
327 NULL,
328 NULL,
329 NULL,
330 NULL);
332 if (status == ERROR_NO_MORE_ITEMS)
333 break;
334 else if (status != ERROR_SUCCESS) {
335 return -1;
338 snprintf(connection_string,
339 sizeof(connection_string),
340 "%s\\%s\\Connection",
341 NETWORK_CONNECTIONS_KEY, enum_name);
343 status = RegOpenKeyEx(
344 HKEY_LOCAL_MACHINE,
345 connection_string,
347 KEY_READ,
348 &connection_key);
350 if (status == ERROR_SUCCESS) {
351 len = sizeof (name_data);
352 status = RegQueryValueEx(
353 connection_key,
354 name_string,
355 NULL,
356 &name_type,
357 name_data,
358 &len);
360 if (status != ERROR_SUCCESS || name_type != REG_SZ) {
361 return -1;
363 else {
364 if (is_tap_win32_dev(enum_name)) {
365 snprintf(name, name_size, "%s", enum_name);
366 if (actual_name) {
367 if (strcmp(actual_name, "") != 0) {
368 if (strcmp(name_data, actual_name) != 0) {
369 RegCloseKey (connection_key);
370 ++i;
371 continue;
374 else {
375 snprintf(actual_name, actual_name_size, "%s", name_data);
378 stop = 1;
382 RegCloseKey (connection_key);
384 ++i;
387 RegCloseKey (control_net_key);
389 if (stop == 0)
390 return -1;
392 return 0;
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
424 0, // initial count
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;
445 unsigned index;
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;
462 BOOL result;
463 DWORD error;
465 result = GetOverlappedResult( overlapped->handle, &overlapped->write_overlapped,
466 &write_size, FALSE);
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);
474 if (!result) {
475 switch (error = GetLastError())
477 case ERROR_IO_PENDING:
478 #ifndef TUN_ASYNCHRONOUS_WRITES
479 WaitForSingleObject(overlapped->write_event, INFINITE);
480 #endif
481 break;
482 default:
483 return -1;
487 return 0;
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;
494 BOOL result;
495 DWORD dwError;
496 tun_buffer_t* buffer = get_buffer_from_free_list(overlapped);
499 for (;;) {
500 result = ReadFile(overlapped->handle,
501 buffer->buffer,
502 sizeof(buffer->buffer),
503 &read_size,
504 &overlapped->read_overlapped);
505 if (!result) {
506 dwError = GetLastError();
507 if (dwError == ERROR_IO_PENDING) {
508 WaitForSingleObject(overlapped->read_event, INFINITE);
509 result = GetOverlappedResult( overlapped->handle, &overlapped->read_overlapped,
510 &read_size, FALSE);
511 if (!result) {
512 #if DEBUG_TAP_WIN32
513 LPVOID lpBuffer;
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 );
520 #endif
522 } else {
523 #if DEBUG_TAP_WIN32
524 LPVOID lpBuffer;
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 );
530 #endif
534 if(read_size > 0) {
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);
542 return 0;
545 static int tap_win32_read(tap_win32_overlapped_t *overlapped,
546 uint8_t **pbuf, int max_size)
548 int size = 0;
550 tun_buffer_t* buffer = get_buffer_from_output_queue_immediate(overlapped);
552 if(buffer != NULL) {
553 *pbuf = buffer->buffer;
554 size = (int)buffer->read_size;
555 if(size > max_size) {
556 size = max_size;
560 return size;
563 static void tap_win32_free_buffer(tap_win32_overlapped_t *overlapped,
564 char* pbuf)
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];
575 int rc;
576 HANDLE handle;
577 BOOL bret;
578 char name_buffer[0x100] = {0, };
579 struct {
580 unsigned long major;
581 unsigned long minor;
582 unsigned long debug;
583 } version;
584 LONG version_len;
585 DWORD idThread;
586 HANDLE hThread;
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));
592 if (rc)
593 return -1;
595 snprintf (device_path, sizeof(device_path), "%s%s%s",
596 USERMODEDEVICEDIR,
597 device_guid,
598 TAPSUFFIX);
600 handle = CreateFile (
601 device_path,
602 GENERIC_READ | GENERIC_WRITE,
605 OPEN_EXISTING,
606 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
607 0 );
609 if (handle == INVALID_HANDLE_VALUE) {
610 return -1;
613 bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION,
614 &version, sizeof (version),
615 &version, sizeof (version), &version_len, NULL);
617 if (bret == FALSE) {
618 CloseHandle(handle);
619 return -1;
622 if (!tap_win32_set_status(handle, TRUE)) {
623 return -1;
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);
632 return 0;
635 /********************************************/
637 typedef struct TAPState {
638 VLANClientState *vc;
639 tap_win32_overlapped_t *handle;
640 } TAPState;
642 static void tap_receive(void *opaque, const uint8_t *buf, int size)
644 TAPState *s = opaque;
646 tap_win32_write(s->handle, buf, size);
649 static void tap_win32_send(void *opaque)
651 TAPState *s = opaque;
652 uint8_t *buf;
653 int max_size = 4096;
654 int size;
656 size = tap_win32_read(s->handle, &buf, max_size);
657 if (size > 0) {
658 qemu_send_packet(s->vc, buf, size);
659 tap_win32_free_buffer(s->handle, buf);
663 int tap_win32_init(VLANState *vlan, const char *ifname)
665 TAPState *s;
667 s = qemu_mallocz(sizeof(TAPState));
668 if (!s)
669 return -1;
670 if (tap_win32_open(&s->handle, ifname) < 0) {
671 printf("tap: Could not open '%s'\n", ifname);
672 return -1;
675 s->vc = qemu_new_vlan_client(vlan, 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);
681 return 0;