1 | /* |
1 | /* |
2 | device-cygwin.C -- Stub for Cygwin environment |
2 | device-cygwin.C -- Stub for Cygwin environment |
3 | Copyright (C) 2003 Marc Lehmann <ocg@goof.com> |
3 | Copyright (C) 2003-2004 Marc Lehmann <ocg@goof.com> |
4 | Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>, |
4 | Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>, |
5 | 2002-2003 Guus Sliepen <guus@sliepen.eu.org> |
5 | 2002-2003 Guus Sliepen <guus@sliepen.eu.org> |
6 | |
6 | |
7 | This program is free software; you can redistribute it and/or modify |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
… | |
… | |
28 | // a newer driver is available as part of the openvpn package: |
28 | // a newer driver is available as part of the openvpn package: |
29 | // http://openvpn.sf.net/ |
29 | // http://openvpn.sf.net/ |
30 | |
30 | |
31 | #include "config.h" |
31 | #include "config.h" |
32 | |
32 | |
33 | #include <stdio.h> |
33 | #include <cstdio> |
|
|
34 | #include <cstring> |
|
|
35 | #include <cstdlib> |
34 | #include <errno.h> |
36 | #include <errno.h> |
35 | #include <sys/types.h> |
37 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
39 | #include <fcntl.h> |
38 | #include <unistd.h> |
40 | #include <unistd.h> |
39 | #include <syslog.h> |
|
|
40 | #include <cstring> |
|
|
41 | |
41 | |
42 | #include "conf.h" |
42 | #include "conf.h" |
43 | #include "util.h" |
43 | #include "util.h" |
44 | |
44 | |
|
|
45 | #include <io.h> |
45 | #include <w32api/windows.h> |
46 | #include <w32api/windows.h> |
46 | #include <w32api/winioctl.h> |
47 | #include <w32api/winioctl.h> |
47 | |
48 | |
48 | #define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
49 | #define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
49 | |
50 | |
… | |
… | |
51 | #define USERDEVICEDIR "\\??\\" |
52 | #define USERDEVICEDIR "\\??\\" |
52 | #define TAPSUFFIX ".tap" |
53 | #define TAPSUFFIX ".tap" |
53 | |
54 | |
54 | #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) |
55 | #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) |
55 | |
56 | |
56 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
57 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
57 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
58 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
58 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
59 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
59 | |
60 | #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE(7, METHOD_BUFFERED) |
60 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
|
|
61 | static mac my_mac; |
|
|
62 | |
61 | |
63 | static const char * |
62 | static const char * |
64 | wstrerror (int err) |
63 | wstrerror (int err) |
65 | { |
64 | { |
66 | static char buf[1024]; |
65 | static char buf[1024]; |
… | |
… | |
70 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL)) |
69 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL)) |
71 | { |
70 | { |
72 | strncpy (buf, _("(unable to format errormessage)"), sizeof (buf)); |
71 | strncpy (buf, _("(unable to format errormessage)"), sizeof (buf)); |
73 | }; |
72 | }; |
74 | |
73 | |
|
|
74 | char *nl; |
75 | if ((char *newline = strchr (buf, '\r'))) |
75 | if ((nl = strchr (buf, '\r'))) |
76 | *newline = '\0'; |
76 | *nl = '\0'; |
77 | |
77 | |
78 | return buf; |
78 | return buf; |
|
|
79 | } |
|
|
80 | |
|
|
81 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
|
|
82 | static mac local_mac; |
|
|
83 | static tap_packet *rcv_pkt; |
|
|
84 | static int iopipe[2]; |
|
|
85 | static HANDLE pipe_handle, send_event, thread; |
|
|
86 | |
|
|
87 | static DWORD WINAPI |
|
|
88 | read_thread(void *) |
|
|
89 | { |
|
|
90 | static OVERLAPPED overlapped; |
|
|
91 | static DWORD dlen; |
|
|
92 | static u32 len; |
|
|
93 | static u8 data[MAX_MTU]; |
|
|
94 | |
|
|
95 | overlapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
|
|
96 | |
|
|
97 | for (;;) |
|
|
98 | { |
|
|
99 | if (!ReadFile (device_handle, data, MAX_MTU, &dlen, &overlapped)) |
|
|
100 | { |
|
|
101 | if (GetLastError () == ERROR_IO_PENDING) |
|
|
102 | GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE); |
|
|
103 | else |
|
|
104 | { |
|
|
105 | slog (L_ERR, "WIN32 TAP: ReadFile returned error: %s", wstrerror (GetLastError ())); |
|
|
106 | exit (EXIT_FAILURE); |
|
|
107 | } |
|
|
108 | } |
|
|
109 | |
|
|
110 | if (dlen > 0) |
|
|
111 | { |
|
|
112 | len = dlen; |
|
|
113 | WriteFile (pipe_handle, &len, sizeof (len), &dlen, NULL); |
|
|
114 | WriteFile (pipe_handle, data, len, &dlen, NULL); |
|
|
115 | } |
|
|
116 | } |
79 | } |
117 | } |
80 | |
118 | |
81 | const char * |
119 | const char * |
82 | tap_device::info () |
120 | tap_device::info () |
83 | { |
121 | { |
… | |
… | |
89 | HKEY key, key2; |
127 | HKEY key, key2; |
90 | int i; |
128 | int i; |
91 | |
129 | |
92 | char regpath[1024]; |
130 | char regpath[1024]; |
93 | char adapterid[1024]; |
131 | char adapterid[1024]; |
94 | char adaptername[1024]; |
132 | BYTE adaptername[1024]; |
95 | char tapname[1024]; |
133 | char tapname[1024]; |
96 | long len; |
134 | DWORD len; |
97 | |
135 | |
98 | bool found = false; |
136 | bool found = false; |
99 | |
137 | |
100 | int sock, err; |
138 | int sock, err; |
101 | |
139 | |
102 | /* Open registry and look for network adapters */ |
140 | /* Open registry and look for network adapters */ |
103 | |
141 | |
104 | if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) |
142 | if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) |
105 | { |
143 | { |
106 | slog (L_ERR, _("Unable to read registry: %s"), |
144 | slog (L_ERR, _("WIN32 TAP: unable to read registry: %s"), |
107 | wstrerror (GetLastError ())); |
145 | wstrerror (GetLastError ())); |
108 | return false; |
146 | exit (EXIT_FAILURE); |
109 | } |
147 | } |
110 | |
148 | |
111 | for (i = 0;; i++) |
149 | for (i = 0;; i++) |
112 | { |
150 | { |
113 | len = sizeof (adapterid); |
151 | len = sizeof (adapterid); |
… | |
… | |
153 | |
191 | |
154 | RegCloseKey (key); |
192 | RegCloseKey (key); |
155 | |
193 | |
156 | if (!found) |
194 | if (!found) |
157 | { |
195 | { |
158 | slog (L_ERR, _("No Windows tap device found!")); |
196 | slog (L_ERR, _("WIN32 TAP: no windows tap device found!")); |
159 | exit (1); |
197 | exit (EXIT_FAILURE); |
160 | } |
198 | } |
161 | |
|
|
162 | strcpy (ifrname, adaptername); |
|
|
163 | |
199 | |
164 | /* Try to open the corresponding tap device */ |
200 | /* Try to open the corresponding tap device */ |
165 | |
201 | |
166 | if (device_handle == INVALID_HANDLE_VALUE) |
202 | if (device_handle == INVALID_HANDLE_VALUE) |
167 | { |
203 | { |
168 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device); |
204 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); |
169 | device_handle = |
205 | device_handle = |
170 | CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
206 | CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
171 | OPEN_EXISTING, |
207 | OPEN_EXISTING, |
172 | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
208 | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
173 | } |
209 | } |
174 | |
210 | |
175 | if (device_handle == INVALID_HANDLE_VALUE) |
211 | if (device_handle == INVALID_HANDLE_VALUE) |
176 | { |
212 | { |
177 | slog (L_ERR, _("%s (%s) is not a usable Windows tap device: %s"), |
213 | slog (L_ERR, _("WIN32 TAP: %s is not a usable windows tap device %s: %s"), |
178 | device, iface, wstrerror (GetLastError ())); |
214 | adaptername, tapname, wstrerror (GetLastError ())); |
179 | exit (1); |
215 | exit (EXIT_FAILURE); |
180 | } |
216 | } |
181 | |
217 | |
182 | fd = cygwin_attach_handle_to_fd (tapname, -1, device_handle, 1, GENERIC_WRITE | GENERIC_READ); |
218 | strcpy (ifrname, (char *)tapname); |
183 | |
219 | |
184 | /* Get MAC address from tap device */ |
220 | /* Get MAC address from tap device */ |
185 | |
221 | |
186 | if (!DeviceIoControl |
222 | if (!DeviceIoControl (device_handle, TAP_IOCTL_GET_MAC, |
187 | (device_handle, TAP_IOCTL_GET_MAC, &mac, sizeof (mac), &mac, sizeof (mac), &len, 0)) |
223 | &local_mac, sizeof (local_mac), &local_mac, sizeof (local_mac), |
|
|
224 | &len, 0)) |
188 | { |
225 | { |
189 | slog (L_ERR, |
226 | slog (L_ERR, |
190 | _("Could not get MAC address from Windows tap device %s (%s): %s"), |
227 | _("WIN32 TAP: could not get MAC address from windows tap device %s: %s"), |
191 | device, iface, wstrerror (GetLastError ())); |
228 | adaptername, wstrerror (GetLastError ())); |
192 | exit (1); |
229 | exit (EXIT_FAILURE); |
193 | } |
230 | } |
|
|
231 | |
|
|
232 | pipe (iopipe); |
|
|
233 | fd = iopipe[0]; |
|
|
234 | pipe_handle = (HANDLE) get_osfhandle (iopipe[1]); |
|
|
235 | |
|
|
236 | send_event = CreateEvent (NULL, FALSE, FALSE, NULL); |
|
|
237 | |
|
|
238 | thread = CreateThread (NULL, 0, read_thread, NULL, 0, NULL); |
|
|
239 | |
|
|
240 | /* try to set driver media status to 'connected' */ |
|
|
241 | ULONG status = TRUE; |
|
|
242 | DeviceIoControl (device_handle, TAP_IOCTL_SET_MEDIA_STATUS, |
|
|
243 | &status, sizeof (status), |
|
|
244 | &status, sizeof (status), &len, NULL); |
|
|
245 | // ignore error here on purpose |
194 | } |
246 | } |
195 | |
247 | |
196 | tap_device::~tap_device () |
248 | tap_device::~tap_device () |
197 | { |
249 | { |
198 | close (fd); |
250 | close (iopipe[0]); |
|
|
251 | close (iopipe[1]); |
|
|
252 | CloseHandle (device_handle); |
|
|
253 | CloseHandle (send_event); |
199 | } |
254 | } |
200 | |
255 | |
201 | tap_packet * |
256 | tap_packet * |
202 | tap_device::recv () |
257 | tap_device::recv () |
203 | { |
258 | { |
204 | tap_packet *pkt = new tap_packet; |
259 | tap_packet *pkt = new tap_packet; |
205 | |
260 | |
206 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
261 | if (sizeof (u32) != read (iopipe[0], &pkt->len, sizeof (u32))) |
207 | |
|
|
208 | if (pkt->len <= 0) |
|
|
209 | { |
262 | { |
210 | slog (L_ERR, _("error while reading from %s %s: %s"), |
263 | slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt length")); |
211 | info (), conf.ifname, strerror (errno)); |
264 | delete pkt; |
212 | free (pkt); |
|
|
213 | return 0; |
265 | return 0; |
214 | } |
266 | } |
215 | |
267 | |
|
|
268 | if (pkt->len != read (iopipe[0], &((*pkt)[0]), pkt->len)) |
|
|
269 | { |
|
|
270 | slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt")); |
|
|
271 | delete pkt; |
|
|
272 | return 0; |
|
|
273 | } |
|
|
274 | |
216 | id2mac (THISNODE->id, &((*pkt)[6])); |
275 | id2mac (THISNODE->id, &((*pkt)[6])); |
217 | |
276 | |
218 | if (pkt->is_arp ()) |
277 | if (pkt->is_arp ()) |
219 | { |
278 | { |
220 | if ((*pkt)[22] == 0x08) |
279 | if (!memcmp (&(*pkt)[22], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[22])); |
221 | id2mac (THISNODE->id, &((*pkt)[22])); |
280 | if (!memcmp (&(*pkt)[32], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[32])); |
222 | if ((*pkt)[32] == 0x08) |
|
|
223 | id2mac (THISNODE->id, &((*pkt)[32])); |
|
|
224 | } |
281 | } |
225 | |
282 | |
226 | return pkt; |
283 | return pkt; |
227 | } |
284 | } |
228 | |
285 | |
229 | void |
286 | void |
230 | tap_device::send (tap_packet * pkt) |
287 | tap_device::send (tap_packet * pkt) |
231 | { |
288 | { |
232 | (*pkt)[6] = 0x08; |
289 | memcpy (&(*pkt)[6], &local_mac, sizeof (mac)); |
233 | (*pkt)[7] = 0x00; |
|
|
234 | (*pkt)[8] = 0x58; |
|
|
235 | (*pkt)[9] = 0x00; |
|
|
236 | (*pkt)[10] = 0x00; |
|
|
237 | (*pkt)[11] = 0x01; |
|
|
238 | |
290 | |
239 | if (pkt->is_arp ()) |
291 | if (pkt->is_arp ()) |
240 | { |
292 | { |
241 | if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) |
293 | if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) |
242 | memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); |
294 | memcpy (&(*pkt)[22], &local_mac, sizeof (mac)); |
243 | |
295 | |
244 | if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) |
296 | if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) |
245 | memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); |
297 | memcpy (&(*pkt)[32], &local_mac, sizeof (mac)); |
246 | } |
298 | } |
247 | |
299 | |
248 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
300 | DWORD dlen; |
249 | slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, |
301 | OVERLAPPED overlapped; |
250 | strerror (errno)); |
302 | overlapped.hEvent = send_event; |
251 | } |
|
|
252 | |
303 | |
253 | #if 0 |
304 | if (!WriteFile (device_handle, &((*pkt)[0]), pkt->len, &dlen, &overlapped)) |
254 | |
|
|
255 | slog (L_DEBUG, _indent: Standard input:377: Error:Stmt nesting error. |
|
|
256 | ("Tap reader running")); |
|
|
257 | |
|
|
258 | /* Read from tap device and send to parent */ |
|
|
259 | |
|
|
260 | overlapped.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL); |
|
|
261 | |
|
|
262 | for indent |
|
|
263 | : Standard input: 320: Error:Stmt nesting error.(;; |
|
|
264 | ) |
|
|
265 | { |
|
|
266 | overlapped.Offset = 0; |
|
|
267 | overlapped.OffsetHigh = 0; |
|
|
268 | ResetEvent (overlapped.hEvent); |
|
|
269 | |
|
|
270 | status = ReadFile (device_handle, buf, sizeof (buf), &len, &overlapped); |
|
|
271 | |
|
|
272 | if (!status) |
|
|
273 | { |
305 | { |
274 | if (GetLastError () == ERROR_IO_PENDING) |
306 | if (GetLastError () == ERROR_IO_PENDING) |
275 | { |
|
|
276 | WaitForSingleObject (overlapped.hEvent, INFINITE); |
|
|
277 | if (!GetOverlappedResult (device_handle, &overlapped, &len, FALSE)) |
307 | GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE); |
278 | continue; |
|
|
279 | } |
|
|
280 | else |
308 | else |
281 | { |
309 | slog (L_ERR, _("WIN32 TAP: can't write to %s %s: %s"), info (), conf.ifname, |
282 | slog (L_ERR, _("Error while reading from %s %s: %s"), |
310 | wstrerror (GetLastError ())); |
283 | device_info, device, strerror (errno)); |
|
|
284 | return -1; |
|
|
285 | } |
|
|
286 | } |
|
|
287 | |
|
|
288 | if (send (sock, buf, len, 0) <= 0) |
|
|
289 | return -1; |
|
|
290 | } |
|
|
291 | } |
|
|
292 | |
|
|
293 | void |
|
|
294 | close_device (void) |
|
|
295 | { |
|
|
296 | cp (); |
|
|
297 | |
|
|
298 | CloseHandle (device_handle); |
|
|
299 | } |
|
|
300 | |
|
|
301 | bool |
|
|
302 | read_packet (vpn_packet_t * packet) |
|
|
303 | { |
|
|
304 | int lenin; |
|
|
305 | |
|
|
306 | cp (); |
|
|
307 | |
|
|
308 | if ((lenin = recv (device_fd, packet->data, MTU, 0)) <= 0) |
|
|
309 | { |
311 | } |
310 | slog (L_ERR, _("Error while reading from %s %s: %s"), device_info, |
|
|
311 | device, strerror (errno)); |
|
|
312 | return false; |
|
|
313 | } |
|
|
314 | |
|
|
315 | packet->len = lenin; |
|
|
316 | |
|
|
317 | device_total_in += packet->len; |
|
|
318 | |
|
|
319 | ifdebug (TRAFFIC) slog (L_DEBUG, _("Read packet of %d bytes from %s"), |
|
|
320 | packet->len, device_info); |
|
|
321 | |
|
|
322 | return true; |
|
|
323 | } |
312 | } |
324 | |
|
|
325 | bool |
|
|
326 | write_packet (vpn_packet_t * packet) |
|
|
327 | { |
|
|
328 | long lenout; |
|
|
329 | OVERLAPPED overlapped = { 0 }; |
|
|
330 | |
|
|
331 | cp (); |
|
|
332 | |
|
|
333 | ifdebug (TRAFFIC) slog (L_DEBUG, _("Writing packet of %d bytes to %s"), |
|
|
334 | packet->len, device_info); |
|
|
335 | |
|
|
336 | if (!WriteFile |
|
|
337 | (device_handle, packet->data, packet->len, &lenout, &overlapped)) |
|
|
338 | { |
|
|
339 | slog (L_ERR, _("Error while writing to %s %s: %s"), device_info, |
|
|
340 | device, wstrerror (GetLastError ())); |
|
|
341 | return false; |
|
|
342 | } |
|
|
343 | |
|
|
344 | device_total_out += packet->len; |
|
|
345 | |
|
|
346 | return true; |
|
|
347 | } |
|
|
348 | |
|
|
349 | void |
|
|
350 | dump_device_stats (void) |
|
|
351 | { |
|
|
352 | cp (); |
|
|
353 | |
|
|
354 | slog (L_DEBUG, _("Statistics for %s %s:"), device_info, device); |
|
|
355 | slog (L_DEBUG, _(" total bytes in: %10d"), device_total_in); |
|
|
356 | slog (L_DEBUG, _(" total bytes out: %10d"), device_total_out); |
|
|
357 | } |
|
|
358 | #endif |
|
|