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