root / tap-win32.c @ f5b78d4f
History | View | Annotate | Download (19.9 kB)
1 | 7fb843f8 | bellard | /*
|
---|---|---|---|
2 | 7fb843f8 | bellard | * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
|
3 | 7fb843f8 | bellard | * on Windows. Originally derived from the CIPE-Win32
|
4 | 7fb843f8 | bellard | * project by Damion K. Wilson, with extensive modifications by
|
5 | 7fb843f8 | bellard | * James Yonan.
|
6 | 7fb843f8 | bellard | *
|
7 | 7fb843f8 | bellard | * All source code which derives from the CIPE-Win32 project is
|
8 | 7fb843f8 | bellard | * Copyright (C) Damion K. Wilson, 2003, and is released under the
|
9 | 7fb843f8 | bellard | * GPL version 2 (see below).
|
10 | 7fb843f8 | bellard | *
|
11 | 7fb843f8 | bellard | * All other source code is Copyright (C) James Yonan, 2003-2004,
|
12 | 7fb843f8 | bellard | * and is released under the GPL version 2 (see below).
|
13 | 7fb843f8 | bellard | *
|
14 | 7fb843f8 | bellard | * This program is free software; you can redistribute it and/or modify
|
15 | 7fb843f8 | bellard | * it under the terms of the GNU General Public License as published by
|
16 | 7fb843f8 | bellard | * the Free Software Foundation; either version 2 of the License, or
|
17 | 7fb843f8 | bellard | * (at your option) any later version.
|
18 | 7fb843f8 | bellard | *
|
19 | 7fb843f8 | bellard | * This program is distributed in the hope that it will be useful,
|
20 | 7fb843f8 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 | 7fb843f8 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 | 7fb843f8 | bellard | * GNU General Public License for more details.
|
23 | 7fb843f8 | bellard | *
|
24 | 7fb843f8 | bellard | * You should have received a copy of the GNU General Public License
|
25 | 7fb843f8 | bellard | * along with this program (see the file COPYING included with this
|
26 | 7fb843f8 | bellard | * distribution); if not, write to the Free Software Foundation, Inc.,
|
27 | 7fb843f8 | bellard | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28 | 7fb843f8 | bellard | */
|
29 | 87ecb68b | pbrook | #include "qemu-common.h" |
30 | 87ecb68b | pbrook | #include "net.h" |
31 | 87ecb68b | pbrook | #include "sysemu.h" |
32 | 7fb843f8 | bellard | #include <stdio.h> |
33 | 4fddf62a | ths | #define WIN32_LEAN_AND_MEAN
|
34 | 7fb843f8 | bellard | #include <windows.h> |
35 | 7fb843f8 | bellard | |
36 | 7fb843f8 | bellard | /* NOTE: PCIBus is redefined in winddk.h */
|
37 | 7fb843f8 | bellard | #define PCIBus _PCIBus
|
38 | 7fb843f8 | bellard | #include <ddk/ntapi.h> |
39 | 7fb843f8 | bellard | #include <ddk/winddk.h> |
40 | 7fb843f8 | bellard | #include <ddk/ntddk.h> |
41 | 7fb843f8 | bellard | #undef PCIBus
|
42 | 7fb843f8 | bellard | |
43 | 7fb843f8 | bellard | //=============
|
44 | 7fb843f8 | bellard | // TAP IOCTLs
|
45 | 7fb843f8 | bellard | //=============
|
46 | 7fb843f8 | bellard | |
47 | 7fb843f8 | bellard | #define TAP_CONTROL_CODE(request,method) \
|
48 | 7fb843f8 | bellard | CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS) |
49 | 7fb843f8 | bellard | |
50 | 7fb843f8 | bellard | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED) |
51 | 7fb843f8 | bellard | #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED) |
52 | 7fb843f8 | bellard | #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED) |
53 | 7fb843f8 | bellard | #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED) |
54 | 7fb843f8 | bellard | #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED) |
55 | 7fb843f8 | bellard | #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED) |
56 | 7fb843f8 | bellard | #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED) |
57 | 7fb843f8 | bellard | #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED) |
58 | 7fb843f8 | bellard | #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED) |
59 | 7fb843f8 | bellard | |
60 | 7fb843f8 | bellard | //=================
|
61 | 7fb843f8 | bellard | // Registry keys
|
62 | 7fb843f8 | bellard | //=================
|
63 | 7fb843f8 | bellard | |
64 | 7fb843f8 | bellard | #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
65 | 7fb843f8 | bellard | |
66 | 7fb843f8 | bellard | #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
67 | 7fb843f8 | bellard | |
68 | 7fb843f8 | bellard | //======================
|
69 | 7fb843f8 | bellard | // Filesystem prefixes
|
70 | 7fb843f8 | bellard | //======================
|
71 | 7fb843f8 | bellard | |
72 | 7fb843f8 | bellard | #define USERMODEDEVICEDIR "\\\\.\\Global\\" |
73 | 7fb843f8 | bellard | #define TAPSUFFIX ".tap" |
74 | 7fb843f8 | bellard | |
75 | 7fb843f8 | bellard | |
76 | 7fb843f8 | bellard | //======================
|
77 | 7fb843f8 | bellard | // Compile time configuration
|
78 | 7fb843f8 | bellard | //======================
|
79 | 7fb843f8 | bellard | |
80 | 7fb843f8 | bellard | //#define DEBUG_TAP_WIN32 1
|
81 | 7fb843f8 | bellard | |
82 | 7fb843f8 | bellard | #define TUN_ASYNCHRONOUS_WRITES 1 |
83 | 7fb843f8 | bellard | |
84 | 7fb843f8 | bellard | #define TUN_BUFFER_SIZE 1560 |
85 | 7fb843f8 | bellard | #define TUN_MAX_BUFFER_COUNT 32 |
86 | 7fb843f8 | bellard | |
87 | 7fb843f8 | bellard | /*
|
88 | 7fb843f8 | bellard | * The data member "buffer" must be the first element in the tun_buffer
|
89 | 7fb843f8 | bellard | * structure. See the function, tap_win32_free_buffer.
|
90 | 7fb843f8 | bellard | */
|
91 | 7fb843f8 | bellard | typedef struct tun_buffer_s { |
92 | 7fb843f8 | bellard | unsigned char buffer [TUN_BUFFER_SIZE]; |
93 | 7fb843f8 | bellard | unsigned long read_size; |
94 | 7fb843f8 | bellard | struct tun_buffer_s* next;
|
95 | 7fb843f8 | bellard | } tun_buffer_t; |
96 | 7fb843f8 | bellard | |
97 | 7fb843f8 | bellard | typedef struct tap_win32_overlapped { |
98 | 7fb843f8 | bellard | HANDLE handle; |
99 | 7fb843f8 | bellard | HANDLE read_event; |
100 | 7fb843f8 | bellard | HANDLE write_event; |
101 | 7fb843f8 | bellard | HANDLE output_queue_semaphore; |
102 | 7fb843f8 | bellard | HANDLE free_list_semaphore; |
103 | e6cda8e2 | bellard | HANDLE tap_semaphore; |
104 | 7fb843f8 | bellard | CRITICAL_SECTION output_queue_cs; |
105 | 7fb843f8 | bellard | CRITICAL_SECTION free_list_cs; |
106 | 7fb843f8 | bellard | OVERLAPPED read_overlapped; |
107 | 7fb843f8 | bellard | OVERLAPPED write_overlapped; |
108 | 7fb843f8 | bellard | tun_buffer_t buffers[TUN_MAX_BUFFER_COUNT]; |
109 | 7fb843f8 | bellard | tun_buffer_t* free_list; |
110 | 7fb843f8 | bellard | tun_buffer_t* output_queue_front; |
111 | 7fb843f8 | bellard | tun_buffer_t* output_queue_back; |
112 | 7fb843f8 | bellard | } tap_win32_overlapped_t; |
113 | 7fb843f8 | bellard | |
114 | 7fb843f8 | bellard | static tap_win32_overlapped_t tap_overlapped;
|
115 | 7fb843f8 | bellard | |
116 | 5fafdf24 | ths | static tun_buffer_t* get_buffer_from_free_list(tap_win32_overlapped_t* const overlapped) |
117 | 7fb843f8 | bellard | { |
118 | 7fb843f8 | bellard | tun_buffer_t* buffer = NULL;
|
119 | 7fb843f8 | bellard | WaitForSingleObject(overlapped->free_list_semaphore, INFINITE); |
120 | 7fb843f8 | bellard | EnterCriticalSection(&overlapped->free_list_cs); |
121 | 7fb843f8 | bellard | buffer = overlapped->free_list; |
122 | 7fb843f8 | bellard | // assert(buffer != NULL);
|
123 | 7fb843f8 | bellard | overlapped->free_list = buffer->next; |
124 | 7fb843f8 | bellard | LeaveCriticalSection(&overlapped->free_list_cs); |
125 | 7fb843f8 | bellard | buffer->next = NULL;
|
126 | 7fb843f8 | bellard | return buffer;
|
127 | 7fb843f8 | bellard | } |
128 | 7fb843f8 | bellard | |
129 | 7fb843f8 | bellard | static void put_buffer_on_free_list(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer) |
130 | 7fb843f8 | bellard | { |
131 | 7fb843f8 | bellard | EnterCriticalSection(&overlapped->free_list_cs); |
132 | 7fb843f8 | bellard | buffer->next = overlapped->free_list; |
133 | 7fb843f8 | bellard | overlapped->free_list = buffer; |
134 | 7fb843f8 | bellard | LeaveCriticalSection(&overlapped->free_list_cs); |
135 | 7fb843f8 | bellard | ReleaseSemaphore(overlapped->free_list_semaphore, 1, NULL); |
136 | 7fb843f8 | bellard | } |
137 | 7fb843f8 | bellard | |
138 | 5fafdf24 | ths | static tun_buffer_t* get_buffer_from_output_queue(tap_win32_overlapped_t* const overlapped, const int block) |
139 | 7fb843f8 | bellard | { |
140 | 7fb843f8 | bellard | tun_buffer_t* buffer = NULL;
|
141 | 7fb843f8 | bellard | DWORD result, timeout = block ? INFINITE : 0L;
|
142 | 7fb843f8 | bellard | |
143 | 7fb843f8 | bellard | // Non-blocking call
|
144 | 5fafdf24 | ths | result = WaitForSingleObject(overlapped->output_queue_semaphore, timeout); |
145 | 7fb843f8 | bellard | |
146 | 5fafdf24 | ths | switch (result)
|
147 | 5fafdf24 | ths | { |
148 | 7fb843f8 | bellard | // The semaphore object was signaled.
|
149 | 5fafdf24 | ths | case WAIT_OBJECT_0:
|
150 | 7fb843f8 | bellard | EnterCriticalSection(&overlapped->output_queue_cs); |
151 | 7fb843f8 | bellard | |
152 | 7fb843f8 | bellard | buffer = overlapped->output_queue_front; |
153 | 7fb843f8 | bellard | overlapped->output_queue_front = buffer->next; |
154 | 7fb843f8 | bellard | |
155 | 7fb843f8 | bellard | if(overlapped->output_queue_front == NULL) { |
156 | 7fb843f8 | bellard | overlapped->output_queue_back = NULL;
|
157 | 7fb843f8 | bellard | } |
158 | 7fb843f8 | bellard | |
159 | 7fb843f8 | bellard | LeaveCriticalSection(&overlapped->output_queue_cs); |
160 | 5fafdf24 | ths | break;
|
161 | 7fb843f8 | bellard | |
162 | 7fb843f8 | bellard | // Semaphore was nonsignaled, so a time-out occurred.
|
163 | 5fafdf24 | ths | case WAIT_TIMEOUT:
|
164 | 7fb843f8 | bellard | // Cannot open another window.
|
165 | 5fafdf24 | ths | break;
|
166 | 7fb843f8 | bellard | } |
167 | 7fb843f8 | bellard | |
168 | 7fb843f8 | bellard | return buffer;
|
169 | 7fb843f8 | bellard | } |
170 | 7fb843f8 | bellard | |
171 | 5fafdf24 | ths | static tun_buffer_t* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t* const overlapped) |
172 | 7fb843f8 | bellard | { |
173 | 7fb843f8 | bellard | return get_buffer_from_output_queue(overlapped, 0); |
174 | 7fb843f8 | bellard | } |
175 | 7fb843f8 | bellard | |
176 | 7fb843f8 | bellard | static void put_buffer_on_output_queue(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer) |
177 | 7fb843f8 | bellard | { |
178 | 7fb843f8 | bellard | EnterCriticalSection(&overlapped->output_queue_cs); |
179 | 7fb843f8 | bellard | |
180 | 7fb843f8 | bellard | if(overlapped->output_queue_front == NULL && overlapped->output_queue_back == NULL) { |
181 | 7fb843f8 | bellard | overlapped->output_queue_front = overlapped->output_queue_back = buffer; |
182 | 7fb843f8 | bellard | } else {
|
183 | 7fb843f8 | bellard | buffer->next = NULL;
|
184 | 7fb843f8 | bellard | overlapped->output_queue_back->next = buffer; |
185 | 7fb843f8 | bellard | overlapped->output_queue_back = buffer; |
186 | 7fb843f8 | bellard | } |
187 | 7fb843f8 | bellard | |
188 | 7fb843f8 | bellard | LeaveCriticalSection(&overlapped->output_queue_cs); |
189 | 7fb843f8 | bellard | |
190 | 7fb843f8 | bellard | ReleaseSemaphore(overlapped->output_queue_semaphore, 1, NULL); |
191 | 7fb843f8 | bellard | } |
192 | 7fb843f8 | bellard | |
193 | 7fb843f8 | bellard | |
194 | 7fb843f8 | bellard | static int is_tap_win32_dev(const char *guid) |
195 | 7fb843f8 | bellard | { |
196 | 7fb843f8 | bellard | HKEY netcard_key; |
197 | 7fb843f8 | bellard | LONG status; |
198 | 7fb843f8 | bellard | DWORD len; |
199 | 7fb843f8 | bellard | int i = 0; |
200 | 7fb843f8 | bellard | |
201 | 7fb843f8 | bellard | status = RegOpenKeyEx( |
202 | 7fb843f8 | bellard | HKEY_LOCAL_MACHINE, |
203 | 7fb843f8 | bellard | ADAPTER_KEY, |
204 | 7fb843f8 | bellard | 0,
|
205 | 7fb843f8 | bellard | KEY_READ, |
206 | 7fb843f8 | bellard | &netcard_key); |
207 | 7fb843f8 | bellard | |
208 | 7fb843f8 | bellard | if (status != ERROR_SUCCESS) {
|
209 | 7fb843f8 | bellard | return FALSE;
|
210 | 7fb843f8 | bellard | } |
211 | 7fb843f8 | bellard | |
212 | 7fb843f8 | bellard | for (;;) {
|
213 | 7fb843f8 | bellard | char enum_name[256]; |
214 | 7fb843f8 | bellard | char unit_string[256]; |
215 | 7fb843f8 | bellard | HKEY unit_key; |
216 | 7fb843f8 | bellard | char component_id_string[] = "ComponentId"; |
217 | 7fb843f8 | bellard | char component_id[256]; |
218 | 7fb843f8 | bellard | char net_cfg_instance_id_string[] = "NetCfgInstanceId"; |
219 | 7fb843f8 | bellard | char net_cfg_instance_id[256]; |
220 | 7fb843f8 | bellard | DWORD data_type; |
221 | 7fb843f8 | bellard | |
222 | 7fb843f8 | bellard | len = sizeof (enum_name);
|
223 | 7fb843f8 | bellard | status = RegEnumKeyEx( |
224 | 7fb843f8 | bellard | netcard_key, |
225 | 7fb843f8 | bellard | i, |
226 | 7fb843f8 | bellard | enum_name, |
227 | 7fb843f8 | bellard | &len, |
228 | 7fb843f8 | bellard | NULL,
|
229 | 7fb843f8 | bellard | NULL,
|
230 | 7fb843f8 | bellard | NULL,
|
231 | 7fb843f8 | bellard | NULL);
|
232 | 7fb843f8 | bellard | |
233 | 7fb843f8 | bellard | if (status == ERROR_NO_MORE_ITEMS)
|
234 | 7fb843f8 | bellard | break;
|
235 | 7fb843f8 | bellard | else if (status != ERROR_SUCCESS) { |
236 | 7fb843f8 | bellard | return FALSE;
|
237 | 7fb843f8 | bellard | } |
238 | 7fb843f8 | bellard | |
239 | 7fb843f8 | bellard | snprintf (unit_string, sizeof(unit_string), "%s\\%s", |
240 | 7fb843f8 | bellard | ADAPTER_KEY, enum_name); |
241 | 7fb843f8 | bellard | |
242 | 7fb843f8 | bellard | status = RegOpenKeyEx( |
243 | 7fb843f8 | bellard | HKEY_LOCAL_MACHINE, |
244 | 7fb843f8 | bellard | unit_string, |
245 | 7fb843f8 | bellard | 0,
|
246 | 7fb843f8 | bellard | KEY_READ, |
247 | 7fb843f8 | bellard | &unit_key); |
248 | 7fb843f8 | bellard | |
249 | 7fb843f8 | bellard | if (status != ERROR_SUCCESS) {
|
250 | 7fb843f8 | bellard | return FALSE;
|
251 | 7fb843f8 | bellard | } else {
|
252 | 7fb843f8 | bellard | len = sizeof (component_id);
|
253 | 7fb843f8 | bellard | status = RegQueryValueEx( |
254 | 7fb843f8 | bellard | unit_key, |
255 | 7fb843f8 | bellard | component_id_string, |
256 | 7fb843f8 | bellard | NULL,
|
257 | 7fb843f8 | bellard | &data_type, |
258 | 7fb843f8 | bellard | component_id, |
259 | 7fb843f8 | bellard | &len); |
260 | 7fb843f8 | bellard | |
261 | 7fb843f8 | bellard | if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
|
262 | 7fb843f8 | bellard | len = sizeof (net_cfg_instance_id);
|
263 | 7fb843f8 | bellard | status = RegQueryValueEx( |
264 | 7fb843f8 | bellard | unit_key, |
265 | 7fb843f8 | bellard | net_cfg_instance_id_string, |
266 | 7fb843f8 | bellard | NULL,
|
267 | 7fb843f8 | bellard | &data_type, |
268 | 7fb843f8 | bellard | net_cfg_instance_id, |
269 | 7fb843f8 | bellard | &len); |
270 | 7fb843f8 | bellard | |
271 | 7fb843f8 | bellard | if (status == ERROR_SUCCESS && data_type == REG_SZ) {
|
272 | 7fb843f8 | bellard | if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/ |
273 | 7fb843f8 | bellard | !strcmp (net_cfg_instance_id, guid)) { |
274 | 7fb843f8 | bellard | RegCloseKey (unit_key); |
275 | 7fb843f8 | bellard | RegCloseKey (netcard_key); |
276 | 7fb843f8 | bellard | return TRUE;
|
277 | 7fb843f8 | bellard | } |
278 | 7fb843f8 | bellard | } |
279 | 7fb843f8 | bellard | } |
280 | 7fb843f8 | bellard | RegCloseKey (unit_key); |
281 | 7fb843f8 | bellard | } |
282 | 7fb843f8 | bellard | ++i; |
283 | 7fb843f8 | bellard | } |
284 | 7fb843f8 | bellard | |
285 | 7fb843f8 | bellard | RegCloseKey (netcard_key); |
286 | 7fb843f8 | bellard | return FALSE;
|
287 | 7fb843f8 | bellard | } |
288 | 7fb843f8 | bellard | |
289 | 7fb843f8 | bellard | static int get_device_guid( |
290 | 7fb843f8 | bellard | char *name,
|
291 | 7fb843f8 | bellard | int name_size,
|
292 | 7fb843f8 | bellard | char *actual_name,
|
293 | 7fb843f8 | bellard | int actual_name_size)
|
294 | 7fb843f8 | bellard | { |
295 | 7fb843f8 | bellard | LONG status; |
296 | 7fb843f8 | bellard | HKEY control_net_key; |
297 | 7fb843f8 | bellard | DWORD len; |
298 | 7fb843f8 | bellard | int i = 0; |
299 | 7fb843f8 | bellard | int stop = 0; |
300 | 7fb843f8 | bellard | |
301 | 7fb843f8 | bellard | status = RegOpenKeyEx( |
302 | 7fb843f8 | bellard | HKEY_LOCAL_MACHINE, |
303 | 7fb843f8 | bellard | NETWORK_CONNECTIONS_KEY, |
304 | 7fb843f8 | bellard | 0,
|
305 | 7fb843f8 | bellard | KEY_READ, |
306 | 7fb843f8 | bellard | &control_net_key); |
307 | 7fb843f8 | bellard | |
308 | 7fb843f8 | bellard | if (status != ERROR_SUCCESS) {
|
309 | 7fb843f8 | bellard | return -1; |
310 | 7fb843f8 | bellard | } |
311 | 7fb843f8 | bellard | |
312 | 7fb843f8 | bellard | while (!stop)
|
313 | 7fb843f8 | bellard | { |
314 | 7fb843f8 | bellard | char enum_name[256]; |
315 | 7fb843f8 | bellard | char connection_string[256]; |
316 | 7fb843f8 | bellard | HKEY connection_key; |
317 | 7fb843f8 | bellard | char name_data[256]; |
318 | 7fb843f8 | bellard | DWORD name_type; |
319 | 7fb843f8 | bellard | const char name_string[] = "Name"; |
320 | 7fb843f8 | bellard | |
321 | 7fb843f8 | bellard | len = sizeof (enum_name);
|
322 | 7fb843f8 | bellard | status = RegEnumKeyEx( |
323 | 7fb843f8 | bellard | control_net_key, |
324 | 7fb843f8 | bellard | i, |
325 | 7fb843f8 | bellard | enum_name, |
326 | 7fb843f8 | bellard | &len, |
327 | 7fb843f8 | bellard | NULL,
|
328 | 7fb843f8 | bellard | NULL,
|
329 | 7fb843f8 | bellard | NULL,
|
330 | 7fb843f8 | bellard | NULL);
|
331 | 7fb843f8 | bellard | |
332 | 7fb843f8 | bellard | if (status == ERROR_NO_MORE_ITEMS)
|
333 | 7fb843f8 | bellard | break;
|
334 | 7fb843f8 | bellard | else if (status != ERROR_SUCCESS) { |
335 | 7fb843f8 | bellard | return -1; |
336 | 7fb843f8 | bellard | } |
337 | 7fb843f8 | bellard | |
338 | 5fafdf24 | ths | snprintf(connection_string, |
339 | 7fb843f8 | bellard | sizeof(connection_string),
|
340 | 7fb843f8 | bellard | "%s\\%s\\Connection",
|
341 | 7fb843f8 | bellard | NETWORK_CONNECTIONS_KEY, enum_name); |
342 | 7fb843f8 | bellard | |
343 | 7fb843f8 | bellard | status = RegOpenKeyEx( |
344 | 7fb843f8 | bellard | HKEY_LOCAL_MACHINE, |
345 | 7fb843f8 | bellard | connection_string, |
346 | 7fb843f8 | bellard | 0,
|
347 | 7fb843f8 | bellard | KEY_READ, |
348 | 7fb843f8 | bellard | &connection_key); |
349 | 3b46e624 | ths | |
350 | 7fb843f8 | bellard | if (status == ERROR_SUCCESS) {
|
351 | 7fb843f8 | bellard | len = sizeof (name_data);
|
352 | 7fb843f8 | bellard | status = RegQueryValueEx( |
353 | 7fb843f8 | bellard | connection_key, |
354 | 7fb843f8 | bellard | name_string, |
355 | 7fb843f8 | bellard | NULL,
|
356 | 7fb843f8 | bellard | &name_type, |
357 | 7fb843f8 | bellard | name_data, |
358 | 7fb843f8 | bellard | &len); |
359 | 7fb843f8 | bellard | |
360 | 7fb843f8 | bellard | if (status != ERROR_SUCCESS || name_type != REG_SZ) {
|
361 | 7fb843f8 | bellard | return -1; |
362 | 7fb843f8 | bellard | } |
363 | 7fb843f8 | bellard | else {
|
364 | 7fb843f8 | bellard | if (is_tap_win32_dev(enum_name)) {
|
365 | 7fb843f8 | bellard | snprintf(name, name_size, "%s", enum_name);
|
366 | 7fb843f8 | bellard | if (actual_name) {
|
367 | 7fb843f8 | bellard | if (strcmp(actual_name, "") != 0) { |
368 | 7fb843f8 | bellard | if (strcmp(name_data, actual_name) != 0) { |
369 | 7fb843f8 | bellard | RegCloseKey (connection_key); |
370 | 7fb843f8 | bellard | ++i; |
371 | 7fb843f8 | bellard | continue;
|
372 | 7fb843f8 | bellard | } |
373 | 7fb843f8 | bellard | } |
374 | 7fb843f8 | bellard | else {
|
375 | 7fb843f8 | bellard | snprintf(actual_name, actual_name_size, "%s", name_data);
|
376 | 7fb843f8 | bellard | } |
377 | 7fb843f8 | bellard | } |
378 | 7fb843f8 | bellard | stop = 1;
|
379 | 7fb843f8 | bellard | } |
380 | 7fb843f8 | bellard | } |
381 | 7fb843f8 | bellard | |
382 | 7fb843f8 | bellard | RegCloseKey (connection_key); |
383 | 7fb843f8 | bellard | } |
384 | 7fb843f8 | bellard | ++i; |
385 | 7fb843f8 | bellard | } |
386 | 7fb843f8 | bellard | |
387 | 7fb843f8 | bellard | RegCloseKey (control_net_key); |
388 | 7fb843f8 | bellard | |
389 | 7fb843f8 | bellard | if (stop == 0) |
390 | 7fb843f8 | bellard | return -1; |
391 | 7fb843f8 | bellard | |
392 | 7fb843f8 | bellard | return 0; |
393 | 7fb843f8 | bellard | } |
394 | 7fb843f8 | bellard | |
395 | 7fb843f8 | bellard | static int tap_win32_set_status(HANDLE handle, int status) |
396 | 7fb843f8 | bellard | { |
397 | 7fb843f8 | bellard | unsigned long len = 0; |
398 | 7fb843f8 | bellard | |
399 | 7fb843f8 | bellard | return DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS,
|
400 | 7fb843f8 | bellard | &status, sizeof (status),
|
401 | 7fb843f8 | bellard | &status, sizeof (status), &len, NULL); |
402 | 7fb843f8 | bellard | } |
403 | 7fb843f8 | bellard | |
404 | 7fb843f8 | bellard | static void tap_win32_overlapped_init(tap_win32_overlapped_t* const overlapped, const HANDLE handle) |
405 | 7fb843f8 | bellard | { |
406 | 7fb843f8 | bellard | overlapped->handle = handle; |
407 | 7fb843f8 | bellard | |
408 | 7fb843f8 | bellard | overlapped->read_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
409 | 7fb843f8 | bellard | overlapped->write_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
410 | 7fb843f8 | bellard | |
411 | 7fb843f8 | bellard | overlapped->read_overlapped.Offset = 0;
|
412 | 7fb843f8 | bellard | overlapped->read_overlapped.OffsetHigh = 0;
|
413 | 7fb843f8 | bellard | overlapped->read_overlapped.hEvent = overlapped->read_event; |
414 | 7fb843f8 | bellard | |
415 | 7fb843f8 | bellard | overlapped->write_overlapped.Offset = 0;
|
416 | 7fb843f8 | bellard | overlapped->write_overlapped.OffsetHigh = 0;
|
417 | 7fb843f8 | bellard | overlapped->write_overlapped.hEvent = overlapped->write_event; |
418 | 7fb843f8 | bellard | |
419 | 7fb843f8 | bellard | InitializeCriticalSection(&overlapped->output_queue_cs); |
420 | 7fb843f8 | bellard | InitializeCriticalSection(&overlapped->free_list_cs); |
421 | 7fb843f8 | bellard | |
422 | 5fafdf24 | ths | overlapped->output_queue_semaphore = CreateSemaphore( |
423 | 7fb843f8 | bellard | NULL, // default security attributes |
424 | 7fb843f8 | bellard | 0, // initial count |
425 | 7fb843f8 | bellard | TUN_MAX_BUFFER_COUNT, // maximum count
|
426 | 7fb843f8 | bellard | NULL); // unnamed semaphore |
427 | 7fb843f8 | bellard | |
428 | 7fb843f8 | bellard | if(!overlapped->output_queue_semaphore) {
|
429 | 7fb843f8 | bellard | fprintf(stderr, "error creating output queue semaphore!\n");
|
430 | 7fb843f8 | bellard | } |
431 | 7fb843f8 | bellard | |
432 | 5fafdf24 | ths | overlapped->free_list_semaphore = CreateSemaphore( |
433 | 7fb843f8 | bellard | NULL, // default security attributes |
434 | 7fb843f8 | bellard | TUN_MAX_BUFFER_COUNT, // initial count
|
435 | 7fb843f8 | bellard | TUN_MAX_BUFFER_COUNT, // maximum count
|
436 | 7fb843f8 | bellard | NULL); // unnamed semaphore |
437 | 7fb843f8 | bellard | |
438 | 7fb843f8 | bellard | if(!overlapped->free_list_semaphore) {
|
439 | 7fb843f8 | bellard | fprintf(stderr, "error creating free list semaphore!\n");
|
440 | 7fb843f8 | bellard | } |
441 | 7fb843f8 | bellard | |
442 | 7fb843f8 | bellard | overlapped->free_list = overlapped->output_queue_front = overlapped->output_queue_back = NULL;
|
443 | 7fb843f8 | bellard | |
444 | 7fb843f8 | bellard | { |
445 | 7fb843f8 | bellard | unsigned index;
|
446 | 7fb843f8 | bellard | for(index = 0; index < TUN_MAX_BUFFER_COUNT; index++) { |
447 | 7fb843f8 | bellard | tun_buffer_t* element = &overlapped->buffers[index]; |
448 | 7fb843f8 | bellard | element->next = overlapped->free_list; |
449 | 7fb843f8 | bellard | overlapped->free_list = element; |
450 | 7fb843f8 | bellard | } |
451 | 7fb843f8 | bellard | } |
452 | e6cda8e2 | bellard | /* To count buffers, initially no-signal. */
|
453 | e6cda8e2 | bellard | overlapped->tap_semaphore = CreateSemaphore(NULL, 0, TUN_MAX_BUFFER_COUNT, NULL); |
454 | e6cda8e2 | bellard | if(!overlapped->tap_semaphore)
|
455 | e6cda8e2 | bellard | fprintf(stderr, "error creating tap_semaphore.\n");
|
456 | 7fb843f8 | bellard | } |
457 | 7fb843f8 | bellard | |
458 | 5fafdf24 | ths | static int tap_win32_write(tap_win32_overlapped_t *overlapped, |
459 | 7fb843f8 | bellard | const void *buffer, unsigned long size) |
460 | 7fb843f8 | bellard | { |
461 | 7fb843f8 | bellard | unsigned long write_size; |
462 | 7fb843f8 | bellard | BOOL result; |
463 | 7fb843f8 | bellard | DWORD error; |
464 | 7fb843f8 | bellard | |
465 | 7fb843f8 | bellard | result = GetOverlappedResult( overlapped->handle, &overlapped->write_overlapped, |
466 | 7fb843f8 | bellard | &write_size, FALSE); |
467 | 7fb843f8 | bellard | |
468 | 7fb843f8 | bellard | if (!result && GetLastError() == ERROR_IO_INCOMPLETE)
|
469 | 7fb843f8 | bellard | WaitForSingleObject(overlapped->write_event, INFINITE); |
470 | 7fb843f8 | bellard | |
471 | 7fb843f8 | bellard | result = WriteFile(overlapped->handle, buffer, size, |
472 | 7fb843f8 | bellard | &write_size, &overlapped->write_overlapped); |
473 | 3b46e624 | ths | |
474 | 5fafdf24 | ths | if (!result) {
|
475 | 7fb843f8 | bellard | switch (error = GetLastError())
|
476 | 5fafdf24 | ths | { |
477 | 5fafdf24 | ths | case ERROR_IO_PENDING:
|
478 | 7fb843f8 | bellard | #ifndef TUN_ASYNCHRONOUS_WRITES
|
479 | 7fb843f8 | bellard | WaitForSingleObject(overlapped->write_event, INFINITE); |
480 | 7fb843f8 | bellard | #endif
|
481 | 7fb843f8 | bellard | break;
|
482 | 7fb843f8 | bellard | default:
|
483 | 7fb843f8 | bellard | return -1; |
484 | 5fafdf24 | ths | } |
485 | 7fb843f8 | bellard | } |
486 | 7fb843f8 | bellard | |
487 | 7fb843f8 | bellard | return 0; |
488 | 7fb843f8 | bellard | } |
489 | 7fb843f8 | bellard | |
490 | 7fb843f8 | bellard | static DWORD WINAPI tap_win32_thread_entry(LPVOID param)
|
491 | 7fb843f8 | bellard | { |
492 | 7fb843f8 | bellard | tap_win32_overlapped_t *overlapped = (tap_win32_overlapped_t*)param; |
493 | 7fb843f8 | bellard | unsigned long read_size; |
494 | 7fb843f8 | bellard | BOOL result; |
495 | 7fb843f8 | bellard | DWORD dwError; |
496 | 7fb843f8 | bellard | tun_buffer_t* buffer = get_buffer_from_free_list(overlapped); |
497 | 7fb843f8 | bellard | |
498 | 7fb843f8 | bellard | |
499 | 7fb843f8 | bellard | for (;;) {
|
500 | 7fb843f8 | bellard | result = ReadFile(overlapped->handle, |
501 | 7fb843f8 | bellard | buffer->buffer, |
502 | 7fb843f8 | bellard | sizeof(buffer->buffer),
|
503 | 7fb843f8 | bellard | &read_size, |
504 | 7fb843f8 | bellard | &overlapped->read_overlapped); |
505 | 7fb843f8 | bellard | if (!result) {
|
506 | 7fb843f8 | bellard | dwError = GetLastError(); |
507 | 7fb843f8 | bellard | if (dwError == ERROR_IO_PENDING) {
|
508 | 7fb843f8 | bellard | WaitForSingleObject(overlapped->read_event, INFINITE); |
509 | 7fb843f8 | bellard | result = GetOverlappedResult( overlapped->handle, &overlapped->read_overlapped, |
510 | 7fb843f8 | bellard | &read_size, FALSE); |
511 | 7fb843f8 | bellard | if (!result) {
|
512 | 7fb843f8 | bellard | #if DEBUG_TAP_WIN32
|
513 | 7fb843f8 | bellard | LPVOID lpBuffer; |
514 | 7fb843f8 | bellard | dwError = GetLastError(); |
515 | 7fb843f8 | bellard | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
516 | 7fb843f8 | bellard | NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
517 | 7fb843f8 | bellard | (LPTSTR) & lpBuffer, 0, NULL ); |
518 | 7fb843f8 | bellard | fprintf(stderr, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError, lpBuffer);
|
519 | 7fb843f8 | bellard | LocalFree( lpBuffer ); |
520 | 7fb843f8 | bellard | #endif
|
521 | 7fb843f8 | bellard | } |
522 | 7fb843f8 | bellard | } else {
|
523 | 7fb843f8 | bellard | #if DEBUG_TAP_WIN32
|
524 | 7fb843f8 | bellard | LPVOID lpBuffer; |
525 | 7fb843f8 | bellard | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
526 | 7fb843f8 | bellard | NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
527 | 7fb843f8 | bellard | (LPTSTR) & lpBuffer, 0, NULL ); |
528 | 7fb843f8 | bellard | fprintf(stderr, "Tap-Win32: Error ReadFile %d - %s\n", dwError, lpBuffer);
|
529 | 7fb843f8 | bellard | LocalFree( lpBuffer ); |
530 | 7fb843f8 | bellard | #endif
|
531 | 7fb843f8 | bellard | } |
532 | 7fb843f8 | bellard | } |
533 | 7fb843f8 | bellard | |
534 | 7fb843f8 | bellard | if(read_size > 0) { |
535 | 7fb843f8 | bellard | buffer->read_size = read_size; |
536 | 7fb843f8 | bellard | put_buffer_on_output_queue(overlapped, buffer); |
537 | e6cda8e2 | bellard | ReleaseSemaphore(overlapped->tap_semaphore, 1, NULL); |
538 | 7fb843f8 | bellard | buffer = get_buffer_from_free_list(overlapped); |
539 | 7fb843f8 | bellard | } |
540 | 7fb843f8 | bellard | } |
541 | 7fb843f8 | bellard | |
542 | 7fb843f8 | bellard | return 0; |
543 | 7fb843f8 | bellard | } |
544 | 7fb843f8 | bellard | |
545 | 5fafdf24 | ths | static int tap_win32_read(tap_win32_overlapped_t *overlapped, |
546 | 7fb843f8 | bellard | uint8_t **pbuf, int max_size)
|
547 | 7fb843f8 | bellard | { |
548 | 7fb843f8 | bellard | int size = 0; |
549 | 7fb843f8 | bellard | |
550 | 7fb843f8 | bellard | tun_buffer_t* buffer = get_buffer_from_output_queue_immediate(overlapped); |
551 | 7fb843f8 | bellard | |
552 | 7fb843f8 | bellard | if(buffer != NULL) { |
553 | 7fb843f8 | bellard | *pbuf = buffer->buffer; |
554 | 7fb843f8 | bellard | size = (int)buffer->read_size;
|
555 | 7fb843f8 | bellard | if(size > max_size) {
|
556 | 7fb843f8 | bellard | size = max_size; |
557 | 7fb843f8 | bellard | } |
558 | 7fb843f8 | bellard | } |
559 | 7fb843f8 | bellard | |
560 | 7fb843f8 | bellard | return size;
|
561 | 7fb843f8 | bellard | } |
562 | 7fb843f8 | bellard | |
563 | 5fafdf24 | ths | static void tap_win32_free_buffer(tap_win32_overlapped_t *overlapped, |
564 | 5fafdf24 | ths | char* pbuf)
|
565 | 7fb843f8 | bellard | { |
566 | 7fb843f8 | bellard | tun_buffer_t* buffer = (tun_buffer_t*)pbuf; |
567 | 7fb843f8 | bellard | put_buffer_on_free_list(overlapped, buffer); |
568 | 7fb843f8 | bellard | } |
569 | 7fb843f8 | bellard | |
570 | 5fafdf24 | ths | static int tap_win32_open(tap_win32_overlapped_t **phandle, |
571 | 7fb843f8 | bellard | const char *prefered_name) |
572 | 7fb843f8 | bellard | { |
573 | 7fb843f8 | bellard | char device_path[256]; |
574 | 7fb843f8 | bellard | char device_guid[0x100]; |
575 | 7fb843f8 | bellard | int rc;
|
576 | 7fb843f8 | bellard | HANDLE handle; |
577 | 7fb843f8 | bellard | BOOL bret; |
578 | 7fb843f8 | bellard | char name_buffer[0x100] = {0, }; |
579 | 7fb843f8 | bellard | struct {
|
580 | 7fb843f8 | bellard | unsigned long major; |
581 | 7fb843f8 | bellard | unsigned long minor; |
582 | 7fb843f8 | bellard | unsigned long debug; |
583 | 7fb843f8 | bellard | } version; |
584 | 7fb843f8 | bellard | LONG version_len; |
585 | 7fb843f8 | bellard | DWORD idThread; |
586 | 7fb843f8 | bellard | HANDLE hThread; |
587 | 7fb843f8 | bellard | |
588 | 7fb843f8 | bellard | if (prefered_name != NULL) |
589 | 7fb843f8 | bellard | snprintf(name_buffer, sizeof(name_buffer), "%s", prefered_name); |
590 | 7fb843f8 | bellard | |
591 | 7fb843f8 | bellard | rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer)); |
592 | 7fb843f8 | bellard | if (rc)
|
593 | 7fb843f8 | bellard | return -1; |
594 | 7fb843f8 | bellard | |
595 | 7fb843f8 | bellard | snprintf (device_path, sizeof(device_path), "%s%s%s", |
596 | 7fb843f8 | bellard | USERMODEDEVICEDIR, |
597 | 7fb843f8 | bellard | device_guid, |
598 | 7fb843f8 | bellard | TAPSUFFIX); |
599 | 7fb843f8 | bellard | |
600 | 7fb843f8 | bellard | handle = CreateFile ( |
601 | 7fb843f8 | bellard | device_path, |
602 | 7fb843f8 | bellard | GENERIC_READ | GENERIC_WRITE, |
603 | 7fb843f8 | bellard | 0,
|
604 | 7fb843f8 | bellard | 0,
|
605 | 7fb843f8 | bellard | OPEN_EXISTING, |
606 | 7fb843f8 | bellard | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, |
607 | 7fb843f8 | bellard | 0 );
|
608 | 7fb843f8 | bellard | |
609 | 7fb843f8 | bellard | if (handle == INVALID_HANDLE_VALUE) {
|
610 | 7fb843f8 | bellard | return -1; |
611 | 7fb843f8 | bellard | } |
612 | 7fb843f8 | bellard | |
613 | 7fb843f8 | bellard | bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION, |
614 | 7fb843f8 | bellard | &version, sizeof (version),
|
615 | 7fb843f8 | bellard | &version, sizeof (version), &version_len, NULL); |
616 | 7fb843f8 | bellard | |
617 | 7fb843f8 | bellard | if (bret == FALSE) {
|
618 | 7fb843f8 | bellard | CloseHandle(handle); |
619 | 7fb843f8 | bellard | return -1; |
620 | 7fb843f8 | bellard | } |
621 | 7fb843f8 | bellard | |
622 | 7fb843f8 | bellard | if (!tap_win32_set_status(handle, TRUE)) {
|
623 | 7fb843f8 | bellard | return -1; |
624 | 7fb843f8 | bellard | } |
625 | 7fb843f8 | bellard | |
626 | 7fb843f8 | bellard | tap_win32_overlapped_init(&tap_overlapped, handle); |
627 | 7fb843f8 | bellard | |
628 | 7fb843f8 | bellard | *phandle = &tap_overlapped; |
629 | 7fb843f8 | bellard | |
630 | 7fb843f8 | bellard | hThread = CreateThread(NULL, 0, tap_win32_thread_entry, |
631 | 7fb843f8 | bellard | (LPVOID)&tap_overlapped, 0, &idThread);
|
632 | 7fb843f8 | bellard | return 0; |
633 | 7fb843f8 | bellard | } |
634 | 7fb843f8 | bellard | |
635 | 7fb843f8 | bellard | /********************************************/
|
636 | 7fb843f8 | bellard | |
637 | 7fb843f8 | bellard | typedef struct TAPState { |
638 | 7fb843f8 | bellard | VLANClientState *vc; |
639 | 7fb843f8 | bellard | tap_win32_overlapped_t *handle; |
640 | 7fb843f8 | bellard | } TAPState; |
641 | 7fb843f8 | bellard | |
642 | 7fb843f8 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
643 | 7fb843f8 | bellard | { |
644 | 7fb843f8 | bellard | TAPState *s = opaque; |
645 | 7fb843f8 | bellard | |
646 | 7fb843f8 | bellard | tap_win32_write(s->handle, buf, size); |
647 | 7fb843f8 | bellard | } |
648 | 7fb843f8 | bellard | |
649 | e6cda8e2 | bellard | static void tap_win32_send(void *opaque) |
650 | 7fb843f8 | bellard | { |
651 | e6cda8e2 | bellard | TAPState *s = opaque; |
652 | 7fb843f8 | bellard | uint8_t *buf; |
653 | 7fb843f8 | bellard | int max_size = 4096; |
654 | 7fb843f8 | bellard | int size;
|
655 | 7fb843f8 | bellard | |
656 | 7fb843f8 | bellard | size = tap_win32_read(s->handle, &buf, max_size); |
657 | 7fb843f8 | bellard | if (size > 0) { |
658 | 7fb843f8 | bellard | qemu_send_packet(s->vc, buf, size); |
659 | 7fb843f8 | bellard | tap_win32_free_buffer(s->handle, buf); |
660 | 7fb843f8 | bellard | } |
661 | 7fb843f8 | bellard | } |
662 | 7fb843f8 | bellard | |
663 | 7fb843f8 | bellard | int tap_win32_init(VLANState *vlan, const char *ifname) |
664 | 7fb843f8 | bellard | { |
665 | 7fb843f8 | bellard | TAPState *s; |
666 | 3b46e624 | ths | |
667 | 7fb843f8 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
668 | 7fb843f8 | bellard | if (!s)
|
669 | 7fb843f8 | bellard | return -1; |
670 | 7fb843f8 | bellard | if (tap_win32_open(&s->handle, ifname) < 0) { |
671 | 7fb843f8 | bellard | printf("tap: Could not open '%s'\n", ifname);
|
672 | 7fb843f8 | bellard | return -1; |
673 | 7fb843f8 | bellard | } |
674 | 7fb843f8 | bellard | |
675 | 4081fccf | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
676 | 3b46e624 | ths | |
677 | 7fb843f8 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
678 | 7fb843f8 | bellard | "tap: ifname=%s", ifname);
|
679 | a18e524a | bellard | |
680 | e6cda8e2 | bellard | qemu_add_wait_object(s->handle->tap_semaphore, tap_win32_send, s); |
681 | 7fb843f8 | bellard | return 0; |
682 | 7fb843f8 | bellard | } |