Merge branch 'master' into 1.1
[tinc] / src / mingw / device.c
1 /*
2     device.c -- Interaction with Windows tap driver in a MinGW 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     $Id$
21 */
22
23 #include "system.h"
24
25 #include <windows.h>
26 #include <winioctl.h>
27
28 #include "conf.h"
29 #include "logger.h"
30 #include "net.h"
31 #include "route.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 #include "mingw/common.h"
36
37 int device_fd = -1;
38 static HANDLE device_handle = INVALID_HANDLE_VALUE;
39 char *device = NULL;
40 char *iface = NULL;
41 static char *device_info = NULL;
42
43 static int device_total_in = 0;
44 static int device_total_out = 0;
45
46 extern char *myport;
47
48 static DWORD WINAPI tapreader(void *bla) {
49         int status;
50         long len;
51         OVERLAPPED overlapped;
52         vpn_packet_t packet;
53
54         logger(LOG_DEBUG, _("Tap reader running"));
55
56         /* Read from tap device and send to parent */
57
58         overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
59
60         for(;;) {
61                 overlapped.Offset = 0;
62                 overlapped.OffsetHigh = 0;
63                 ResetEvent(overlapped.hEvent);
64
65                 status = ReadFile(device_handle, packet.data, MTU, &len, &overlapped);
66
67                 if(!status) {
68                         if(GetLastError() == ERROR_IO_PENDING) {
69                                 WaitForSingleObject(overlapped.hEvent, INFINITE);
70                                 if(!GetOverlappedResult(device_handle, &overlapped, &len, FALSE))
71                                         continue;
72                         } else {
73                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
74                                            device, strerror(errno));
75                                 return -1;
76                         }
77                 }
78
79                 EnterCriticalSection(&mutex);
80                 packet.len = len;
81                 packet.priority = 0;
82                 route(myself, &packet);
83                 LeaveCriticalSection(&mutex);
84         }
85 }
86
87 bool setup_device(void) {
88         HKEY key, key2;
89         int i;
90
91         char regpath[1024];
92         char adapterid[1024];
93         char adaptername[1024];
94         char tapname[1024];
95         long len;
96         unsigned long status;
97
98         bool found = false;
99
100         int sock, err;
101         HANDLE thread;
102
103         struct addrinfo *ai;
104         struct addrinfo hint = {
105                 .ai_family = AF_UNSPEC,
106                 .ai_socktype = SOCK_STREAM,
107                 .ai_protocol = IPPROTO_TCP,
108                 .ai_flags = 0,
109         };
110
111         cp();
112
113         get_config_string(lookup_config(config_tree, "Device"), &device);
114         get_config_string(lookup_config(config_tree, "Interface"), &iface);
115
116         /* Open registry and look for network adapters */
117
118         if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) {
119                 logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError()));
120                 return false;
121         }
122
123         for (i = 0; ; i++) {
124                 len = sizeof adapterid;
125                 if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL))
126                         break;
127
128                 /* Find out more about this adapter */
129
130                 snprintf(regpath, sizeof regpath, "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid);
131
132                 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2))
133                         continue;
134
135                 len = sizeof adaptername;
136                 err = RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len);
137
138                 RegCloseKey(key2);
139
140                 if(err)
141                         continue;
142
143                 if(device) {
144                         if(!strcmp(device, adapterid)) {
145                                 found = true;
146                                 break;
147                         } else
148                                 continue;
149                 }
150
151                 if(iface) {
152                         if(!strcmp(iface, adaptername)) {
153                                 found = true;
154                                 break;
155                         } else
156                                 continue;
157                 }
158
159                 snprintf(tapname, sizeof tapname, USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
160                 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
161                 if(device_handle != INVALID_HANDLE_VALUE) {
162                         found = true;
163                         break;
164                 }
165         }
166
167         RegCloseKey(key);
168
169         if(!found) {
170                 logger(LOG_ERR, _("No Windows tap device found!"));
171                 return false;
172         }
173
174         if(!device)
175                 device = xstrdup(adapterid);
176
177         if(!iface)
178                 iface = xstrdup(adaptername);
179
180         /* Try to open the corresponding tap device */
181
182         if(device_handle == INVALID_HANDLE_VALUE) {
183                 snprintf(tapname, sizeof tapname, USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
184                 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
185         }
186         
187         if(device_handle == INVALID_HANDLE_VALUE) {
188                 logger(LOG_ERR, _("%s (%s) is not a usable Windows tap device: %s"), device, iface, winerror(GetLastError()));
189                 return false;
190         }
191
192         /* Get MAC address from tap device */
193
194         if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof mymac.x, mymac.x, sizeof mymac.x, &len, 0)) {
195                 logger(LOG_ERR, _("Could not get MAC address from Windows tap device %s (%s): %s"), device, iface, winerror(GetLastError()));
196                 return false;
197         }
198
199         if(routing_mode == RMODE_ROUTER) {
200                 overwrite_mac = 1;
201         }
202
203         /* Start the tap reader */
204
205         thread = CreateThread(NULL, 0, tapreader, NULL, 0, NULL);
206
207         if(!thread) {
208                 logger(LOG_ERR, _("System call `%s' failed: %s"), "CreateThread", winerror(GetLastError()));
209                 return false;
210         }
211
212         /* Set media status for newer TAP-Win32 devices */
213
214         status = true;
215         DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof status, &status, sizeof status, &len, NULL);
216
217         device_info = _("Windows tap device");
218
219         logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
220
221         return true;
222 }
223
224 void close_device(void) {
225         cp();
226
227         CloseHandle(device_handle);
228
229         free(device);
230         free(iface);
231 }
232
233 bool read_packet(vpn_packet_t *packet) {
234         return false;
235 }
236
237 bool write_packet(vpn_packet_t *packet) {
238         long outlen;
239         OVERLAPPED overlapped = {0};
240
241         cp();
242
243         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
244                            packet->len, device_info);
245
246         if(!WriteFile(device_handle, packet->data, packet->len, &outlen, &overlapped)) {
247                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, winerror(GetLastError()));
248                 return false;
249         }
250
251         device_total_out += packet->len;
252
253         return true;
254 }
255
256 void dump_device_stats(void) {
257         cp();
258
259         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
260         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
261         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
262 }