1 |
pcg |
1.1 |
/* |
2 |
|
|
device.c -- Interaction with Windows tap driver in a Cygwin environment |
3 |
pcg |
1.2 |
Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>, |
4 |
|
|
2002-2004 Guus Sliepen <guus@tinc-vpn.org> |
5 |
pcg |
1.1 |
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
|
20 |
pcg |
1.2 |
$Id: device.c 1400 2004-11-01 17:02:19Z guus $ |
21 |
pcg |
1.1 |
*/ |
22 |
|
|
|
23 |
|
|
#include <w32api/windows.h> |
24 |
|
|
#include <w32api/winioctl.h> |
25 |
|
|
|
26 |
pcg |
1.2 |
#include "mingw/common.h" |
27 |
pcg |
1.1 |
|
28 |
|
|
int device_fd = -1; |
29 |
|
|
static HANDLE device_handle = INVALID_HANDLE_VALUE; |
30 |
|
|
char *device = NULL; |
31 |
|
|
char *iface = NULL; |
32 |
|
|
char *device_info = NULL; |
33 |
|
|
|
34 |
pcg |
1.2 |
static int device_total_in = 0; |
35 |
|
|
static int device_total_out = 0; |
36 |
pcg |
1.1 |
|
37 |
pcg |
1.2 |
static pid_t reader_pid; |
38 |
|
|
static int sp[2]; |
39 |
pcg |
1.1 |
|
40 |
|
|
bool setup_device(void) |
41 |
|
|
{ |
42 |
|
|
HKEY key, key2; |
43 |
pcg |
1.2 |
int i, err; |
44 |
pcg |
1.1 |
|
45 |
|
|
char regpath[1024]; |
46 |
|
|
char adapterid[1024]; |
47 |
|
|
char adaptername[1024]; |
48 |
|
|
char tapname[1024]; |
49 |
|
|
char gelukt = 0; |
50 |
|
|
long len; |
51 |
|
|
|
52 |
|
|
bool found = false; |
53 |
|
|
|
54 |
|
|
cp(); |
55 |
|
|
|
56 |
|
|
get_config_string(lookup_config(config_tree, "Device"), &device); |
57 |
|
|
get_config_string(lookup_config(config_tree, "Interface"), &iface); |
58 |
|
|
|
59 |
|
|
/* Open registry and look for network adapters */ |
60 |
|
|
|
61 |
pcg |
1.2 |
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) { |
62 |
pcg |
1.1 |
logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError())); |
63 |
|
|
return false; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
for (i = 0; ; i++) { |
67 |
|
|
len = sizeof(adapterid); |
68 |
|
|
if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL)) |
69 |
|
|
break; |
70 |
|
|
|
71 |
|
|
/* Find out more about this adapter */ |
72 |
|
|
|
73 |
pcg |
1.2 |
snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid); |
74 |
pcg |
1.1 |
|
75 |
|
|
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
76 |
|
|
continue; |
77 |
|
|
|
78 |
|
|
len = sizeof(adaptername); |
79 |
|
|
err = RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len); |
80 |
|
|
|
81 |
|
|
RegCloseKey(key2); |
82 |
|
|
|
83 |
|
|
if(err) |
84 |
|
|
continue; |
85 |
|
|
|
86 |
|
|
if(device) { |
87 |
|
|
if(!strcmp(device, adapterid)) { |
88 |
|
|
found = true; |
89 |
|
|
break; |
90 |
|
|
} else |
91 |
|
|
continue; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
if(iface) { |
95 |
|
|
if(!strcmp(iface, adaptername)) { |
96 |
|
|
found = true; |
97 |
|
|
break; |
98 |
|
|
} else |
99 |
|
|
continue; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); |
103 |
|
|
device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0); |
104 |
|
|
if(device_handle != INVALID_HANDLE_VALUE) { |
105 |
|
|
CloseHandle(device_handle); |
106 |
|
|
found = true; |
107 |
|
|
break; |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
RegCloseKey(key); |
112 |
|
|
|
113 |
|
|
if(!found) { |
114 |
|
|
logger(LOG_ERR, _("No Windows tap device found!")); |
115 |
|
|
return false; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
if(!device) |
119 |
|
|
device = xstrdup(adapterid); |
120 |
|
|
|
121 |
|
|
if(!iface) |
122 |
|
|
iface = xstrdup(adaptername); |
123 |
|
|
|
124 |
|
|
snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device); |
125 |
|
|
|
126 |
|
|
/* Now we are going to open this device twice: once for reading and once for writing. |
127 |
|
|
We do this because apparently it isn't possible to check for activity in the select() loop. |
128 |
|
|
Furthermore I don't really know how to do it the "Windows" way. */ |
129 |
|
|
|
130 |
|
|
if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) { |
131 |
|
|
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno)); |
132 |
|
|
return false; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/* The parent opens the tap device for writing. */ |
136 |
|
|
|
137 |
|
|
device_handle = CreateFile(tapname, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM , 0); |
138 |
|
|
|
139 |
|
|
if(device_handle == INVALID_HANDLE_VALUE) { |
140 |
|
|
logger(LOG_ERR, _("Could not open Windows tap device %s (%s) for writing: %s"), device, iface, winerror(GetLastError())); |
141 |
|
|
return false; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
device_fd = sp[0]; |
145 |
|
|
|
146 |
|
|
/* Get MAC address from tap device */ |
147 |
|
|
|
148 |
|
|
if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) { |
149 |
|
|
logger(LOG_ERR, _("Could not get MAC address from Windows tap device %s (%s): %s"), device, iface, winerror(GetLastError())); |
150 |
|
|
return false; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
if(routing_mode == RMODE_ROUTER) { |
154 |
|
|
overwrite_mac = 1; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/* Now we start the child */ |
158 |
|
|
|
159 |
|
|
reader_pid = fork(); |
160 |
|
|
|
161 |
|
|
if(reader_pid == -1) { |
162 |
|
|
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno)); |
163 |
|
|
return false; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
if(!reader_pid) { |
167 |
|
|
/* The child opens the tap device for reading, blocking. |
168 |
|
|
It passes everything it reads to the socket. */ |
169 |
|
|
|
170 |
|
|
char buf[MTU]; |
171 |
|
|
long lenin; |
172 |
|
|
|
173 |
|
|
CloseHandle(device_handle); |
174 |
|
|
|
175 |
|
|
device_handle = CreateFile(tapname, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0); |
176 |
|
|
|
177 |
|
|
if(device_handle == INVALID_HANDLE_VALUE) { |
178 |
|
|
logger(LOG_ERR, _("Could not open Windows tap device %s (%s) for reading: %s"), device, iface, winerror(GetLastError())); |
179 |
|
|
buf[0] = 0; |
180 |
|
|
write(sp[1], buf, 1); |
181 |
|
|
exit(1); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
logger(LOG_DEBUG, _("Tap reader forked and running.")); |
185 |
|
|
|
186 |
|
|
/* Notify success */ |
187 |
|
|
|
188 |
|
|
buf[0] = 1; |
189 |
|
|
write(sp[1], buf, 1); |
190 |
|
|
|
191 |
|
|
/* Pass packets */ |
192 |
|
|
|
193 |
|
|
for(;;) { |
194 |
|
|
ReadFile(device_handle, buf, MTU, &lenin, NULL); |
195 |
|
|
write(sp[1], buf, lenin); |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
read(device_fd, &gelukt, 1); |
200 |
|
|
if(gelukt != 1) { |
201 |
|
|
logger(LOG_DEBUG, _("Tap reader failed!")); |
202 |
|
|
return false; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
device_info = _("Windows tap device"); |
206 |
|
|
|
207 |
|
|
logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info); |
208 |
|
|
|
209 |
|
|
return true; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
void close_device(void) |
213 |
|
|
{ |
214 |
|
|
cp(); |
215 |
|
|
|
216 |
|
|
close(sp[0]); |
217 |
|
|
close(sp[1]); |
218 |
|
|
CloseHandle(device_handle); |
219 |
|
|
|
220 |
|
|
kill(reader_pid, SIGKILL); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
bool read_packet(vpn_packet_t *packet) |
224 |
|
|
{ |
225 |
|
|
int lenin; |
226 |
|
|
|
227 |
|
|
cp(); |
228 |
|
|
|
229 |
|
|
if((lenin = read(sp[0], packet->data, MTU)) <= 0) { |
230 |
|
|
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, |
231 |
|
|
device, strerror(errno)); |
232 |
|
|
return false; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
packet->len = lenin; |
236 |
|
|
|
237 |
|
|
device_total_in += packet->len; |
238 |
|
|
|
239 |
|
|
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, |
240 |
|
|
device_info); |
241 |
|
|
|
242 |
|
|
return true; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
bool write_packet(vpn_packet_t *packet) |
246 |
|
|
{ |
247 |
|
|
long lenout; |
248 |
|
|
|
249 |
|
|
cp(); |
250 |
|
|
|
251 |
|
|
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"), |
252 |
|
|
packet->len, device_info); |
253 |
|
|
|
254 |
|
|
if(!WriteFile (device_handle, packet->data, packet->len, &lenout, NULL)) { |
255 |
|
|
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, winerror(GetLastError())); |
256 |
|
|
return false; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
device_total_out += packet->len; |
260 |
|
|
|
261 |
|
|
return true; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
void dump_device_stats(void) |
265 |
|
|
{ |
266 |
|
|
cp(); |
267 |
|
|
|
268 |
|
|
logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device); |
269 |
|
|
logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in); |
270 |
|
|
logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out); |
271 |
|
|
} |