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>, |
|
|
5 | 2002-2003 Guus Sliepen <guus@sliepen.eu.org> |
4 | |
6 | |
5 | 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 |
6 | 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 |
7 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
10 | (at your option) any later version. |
… | |
… | |
21 | // and the default cipedrvr uses a different MAC address than we do, |
23 | // and the default cipedrvr uses a different MAC address than we do, |
22 | // so this module tries to fix mac addresses in packets and arp packets. |
24 | // so this module tries to fix mac addresses in packets and arp packets. |
23 | // this is probably not very fast, but neither is cygwin nor poll. |
25 | // this is probably not very fast, but neither is cygwin nor poll. |
24 | // |
26 | // |
25 | // http://cipe-win32.sourceforge.net/ |
27 | // http://cipe-win32.sourceforge.net/ |
|
|
28 | // a newer driver is available as part of the openvpn package: |
|
|
29 | // http://openvpn.sf.net/ |
26 | |
30 | |
27 | #include "config.h" |
31 | #include "config.h" |
28 | |
32 | |
29 | #include <stdio.h> |
33 | #include <cstdio> |
|
|
34 | #include <cstring> |
|
|
35 | #include <cstdlib> |
30 | #include <errno.h> |
36 | #include <errno.h> |
31 | #include <sys/types.h> |
37 | #include <sys/types.h> |
32 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
33 | #include <fcntl.h> |
39 | #include <fcntl.h> |
34 | #include <unistd.h> |
40 | #include <unistd.h> |
35 | #include <syslog.h> |
|
|
36 | #include <cstring> |
|
|
37 | |
41 | |
38 | #include "conf.h" |
42 | #include "conf.h" |
39 | #include "util.h" |
43 | #include "util.h" |
40 | |
44 | |
|
|
45 | #include <io.h> |
|
|
46 | #include <w32api/windows.h> |
|
|
47 | #include <w32api/winioctl.h> |
|
|
48 | |
|
|
49 | #define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
|
|
50 | |
|
|
51 | #define USERMODEDEVICEDIR "\\\\.\\" |
|
|
52 | #define USERDEVICEDIR "\\??\\" |
|
|
53 | #define TAPSUFFIX ".tap" |
|
|
54 | |
|
|
55 | #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) |
|
|
56 | |
|
|
57 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
|
|
58 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
|
|
59 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
|
|
60 | #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE(7, METHOD_BUFFERED) |
|
|
61 | |
|
|
62 | static const char * |
|
|
63 | wstrerror (int err) |
|
|
64 | { |
|
|
65 | static char buf[1024]; |
|
|
66 | |
|
|
67 | if (!FormatMessage |
|
|
68 | (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, |
|
|
69 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL)) |
|
|
70 | { |
|
|
71 | strncpy (buf, _("(unable to format errormessage)"), sizeof (buf)); |
|
|
72 | }; |
|
|
73 | |
|
|
74 | char *nl; |
|
|
75 | if ((nl = strchr (buf, '\r'))) |
|
|
76 | *nl = '\0'; |
|
|
77 | |
|
|
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 | } |
|
|
117 | } |
|
|
118 | |
|
|
119 | const char * |
|
|
120 | tap_device::info () |
|
|
121 | { |
|
|
122 | return _("cygwin cipe/openvpn tap device"); |
|
|
123 | } |
|
|
124 | |
41 | tap_device::tap_device () |
125 | tap_device::tap_device () |
42 | { |
126 | { |
43 | if ((fd = open (conf.ifname, O_RDWR)) < 0) |
127 | HKEY key, key2; |
44 | { |
128 | int i; |
45 | slog (L_CRIT, _("could not open %s: %s"), conf.ifname, strerror (errno)); |
129 | |
46 | exit (1); |
130 | char regpath[1024]; |
|
|
131 | char adapterid[1024]; |
|
|
132 | BYTE adaptername[1024]; |
|
|
133 | char tapname[1024]; |
|
|
134 | DWORD len; |
|
|
135 | |
|
|
136 | bool found = false; |
|
|
137 | |
|
|
138 | int sock, err; |
|
|
139 | |
|
|
140 | /* Open registry and look for network adapters */ |
|
|
141 | |
|
|
142 | if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) |
47 | } |
143 | { |
|
|
144 | slog (L_ERR, _("WIN32 TAP: unable to read registry: %s"), |
|
|
145 | wstrerror (GetLastError ())); |
|
|
146 | exit (EXIT_FAILURE); |
|
|
147 | } |
|
|
148 | |
|
|
149 | for (i = 0;; i++) |
|
|
150 | { |
|
|
151 | len = sizeof (adapterid); |
|
|
152 | if (RegEnumKeyEx (key, i, adapterid, &len, 0, 0, 0, NULL)) |
|
|
153 | break; |
|
|
154 | |
|
|
155 | /* Find out more about this adapter */ |
|
|
156 | |
|
|
157 | snprintf (regpath, sizeof (regpath), "%s\\%s\\Connection", |
|
|
158 | REG_CONTROL_NET, adapterid); |
|
|
159 | |
|
|
160 | if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
|
|
161 | continue; |
|
|
162 | |
|
|
163 | len = sizeof (adaptername); |
|
|
164 | err = RegQueryValueEx (key2, "Name", 0, 0, adaptername, &len); |
|
|
165 | |
|
|
166 | RegCloseKey (key2); |
|
|
167 | |
|
|
168 | if (err) |
|
|
169 | continue; |
|
|
170 | |
|
|
171 | if (conf.ifname) |
|
|
172 | { |
|
|
173 | if (strcmp (conf.ifname, adapterid)) |
|
|
174 | continue; |
|
|
175 | } |
|
|
176 | else |
|
|
177 | { |
|
|
178 | found = true; |
|
|
179 | break; |
|
|
180 | } |
|
|
181 | |
|
|
182 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); |
|
|
183 | device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
|
|
184 | OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
|
|
185 | if (device_handle != INVALID_HANDLE_VALUE) |
|
|
186 | { |
|
|
187 | found = true; |
|
|
188 | break; |
|
|
189 | } |
|
|
190 | } |
|
|
191 | |
|
|
192 | RegCloseKey (key); |
|
|
193 | |
|
|
194 | if (!found) |
|
|
195 | { |
|
|
196 | slog (L_ERR, _("WIN32 TAP: no windows tap device found!")); |
|
|
197 | exit (EXIT_FAILURE); |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* Try to open the corresponding tap device */ |
|
|
201 | |
|
|
202 | if (device_handle == INVALID_HANDLE_VALUE) |
|
|
203 | { |
|
|
204 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); |
|
|
205 | device_handle = |
|
|
206 | CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
|
|
207 | OPEN_EXISTING, |
|
|
208 | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
|
|
209 | } |
|
|
210 | |
|
|
211 | if (device_handle == INVALID_HANDLE_VALUE) |
|
|
212 | { |
|
|
213 | slog (L_ERR, _("WIN32 TAP: %s is not a usable windows tap device %s: %s"), |
|
|
214 | adaptername, tapname, wstrerror (GetLastError ())); |
|
|
215 | exit (EXIT_FAILURE); |
|
|
216 | } |
|
|
217 | |
|
|
218 | strcpy (ifrname, (char *)tapname); |
|
|
219 | |
|
|
220 | /* Get MAC address from tap device */ |
|
|
221 | |
|
|
222 | if (!DeviceIoControl (device_handle, TAP_IOCTL_GET_MAC, |
|
|
223 | &local_mac, sizeof (local_mac), &local_mac, sizeof (local_mac), |
|
|
224 | &len, 0)) |
|
|
225 | { |
|
|
226 | slog (L_ERR, |
|
|
227 | _("WIN32 TAP: could not get MAC address from windows tap device %s: %s"), |
|
|
228 | adaptername, wstrerror (GetLastError ())); |
|
|
229 | exit (EXIT_FAILURE); |
|
|
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 |
48 | } |
246 | } |
49 | |
247 | |
50 | tap_device::~tap_device () |
248 | tap_device::~tap_device () |
51 | { |
249 | { |
52 | close (fd); |
250 | close (iopipe[0]); |
|
|
251 | close (iopipe[1]); |
|
|
252 | CloseHandle (device_handle); |
|
|
253 | CloseHandle (send_event); |
53 | } |
254 | } |
54 | |
255 | |
55 | tap_packet * |
256 | tap_packet * |
56 | tap_device::recv () |
257 | tap_device::recv () |
57 | { |
258 | { |
58 | tap_packet *pkt = new tap_packet; |
259 | tap_packet *pkt = new tap_packet; |
59 | |
260 | |
60 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
261 | if (sizeof (u32) != read (iopipe[0], &pkt->len, sizeof (u32))) |
61 | |
|
|
62 | if (pkt->len <= 0) |
|
|
63 | { |
262 | { |
64 | slog (L_ERR, _("error while reading from %s %s: %s"), |
263 | slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt length")); |
65 | info (), conf.ifname, strerror (errno)); |
264 | delete pkt; |
66 | free (pkt); |
|
|
67 | return 0; |
265 | return 0; |
68 | } |
266 | } |
69 | |
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 | |
70 | id2mac (THISNODE->id, &((*pkt)[6])); |
275 | id2mac (THISNODE->id, &((*pkt)[6])); |
71 | |
276 | |
72 | if (pkt->is_arp ()) |
277 | if (pkt->is_arp ()) |
73 | { |
278 | { |
74 | if ((*pkt)[22] == 0x08) id2mac (THISNODE->id, &((*pkt)[22])); |
279 | if (!memcmp (&(*pkt)[22], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[22])); |
75 | if ((*pkt)[32] == 0x08) id2mac (THISNODE->id, &((*pkt)[32])); |
280 | if (!memcmp (&(*pkt)[32], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[32])); |
76 | } |
281 | } |
77 | |
282 | |
78 | return pkt; |
283 | return pkt; |
79 | } |
284 | } |
80 | |
285 | |
81 | void |
286 | void |
82 | tap_device::send (tap_packet *pkt) |
287 | tap_device::send (tap_packet * pkt) |
83 | { |
288 | { |
84 | (*pkt)[ 6] = 0x08; (*pkt)[ 7] = 0x00; (*pkt)[ 8] = 0x58; |
289 | memcpy (&(*pkt)[6], &local_mac, sizeof (mac)); |
85 | (*pkt)[ 9] = 0x00; (*pkt)[10] = 0x00; (*pkt)[11] = 0x01; |
|
|
86 | |
290 | |
87 | if (pkt->is_arp ()) |
291 | if (pkt->is_arp ()) |
88 | { |
292 | { |
89 | if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) |
293 | if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) |
90 | memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); |
294 | memcpy (&(*pkt)[22], &local_mac, sizeof (mac)); |
91 | |
295 | |
92 | if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) |
296 | if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) |
93 | memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); |
297 | memcpy (&(*pkt)[32], &local_mac, sizeof (mac)); |
|
|
298 | } |
|
|
299 | |
|
|
300 | DWORD dlen; |
|
|
301 | OVERLAPPED overlapped; |
|
|
302 | overlapped.hEvent = send_event; |
|
|
303 | |
|
|
304 | if (!WriteFile (device_handle, &((*pkt)[0]), pkt->len, &dlen, &overlapped)) |
94 | } |
305 | { |
95 | |
306 | if (GetLastError () == ERROR_IO_PENDING) |
96 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
307 | GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE); |
|
|
308 | else |
97 | slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, |
309 | slog (L_ERR, _("WIN32 TAP: can't write to %s %s: %s"), info (), conf.ifname, |
98 | strerror (errno)); |
310 | wstrerror (GetLastError ())); |
|
|
311 | } |
99 | } |
312 | } |