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