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