fa13433230fb2e1c66e7ccd15829ede8c8e9be2f
[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 {
89         HKEY key, key2;
90         int i;
91
92         char regpath[1024];
93         char adapterid[1024];
94         char adaptername[1024];
95         char tapname[1024];
96         long len;
97         unsigned long status;
98
99         bool found = false;
100
101         int sock, err;
102         HANDLE thread;
103
104         struct addrinfo *ai;
105         struct addrinfo hint = {
106                 .ai_family = AF_UNSPEC,
107                 .ai_socktype = SOCK_STREAM,
108                 .ai_protocol = IPPROTO_TCP,
109                 .ai_flags = 0,
110         };
111
112         cp();
113
114         get_config_string(lookup_config(config_tree, "Device"), &device);
115         get_config_string(lookup_config(config_tree, "Interface"), &iface);
116
117         /* Open registry and look for network adapters */
118
119         if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) {
120                 logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError()));
121                 return false;
122         }
123
124         for (i = 0; ; i++) {
125                 len = sizeof(adapterid);
126                 if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL))
127                         break;
128
129                 /* Find out more about this adapter */
130
131                 snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid);
132
133                 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2))
134                         continue;
135
136                 len = sizeof(adaptername);
137                 err = RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len);
138
139                 RegCloseKey(key2);
140
141                 if(err)
142                         continue;
143
144                 if(device) {
145                         if(!strcmp(device, adapterid)) {
146                                 found = true;
147                                 break;
148                         } else
149                                 continue;
150                 }
151
152                 if(iface) {
153                         if(!strcmp(iface, adaptername)) {
154                                 found = true;
155                                 break;
156                         } else
157                                 continue;
158                 }
159
160                 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
161                 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
162                 if(device_handle != INVALID_HANDLE_VALUE) {
163                         found = true;
164                         break;
165                 }
166         }
167
168         RegCloseKey(key);
169
170         if(!found) {
171                 logger(LOG_ERR, _("No Windows tap device found!"));
172                 return false;
173         }
174
175         if(!device)
176                 device = xstrdup(adapterid);
177
178         if(!iface)
179                 iface = xstrdup(adaptername);
180
181         /* Try to open the corresponding tap device */
182
183         if(device_handle == INVALID_HANDLE_VALUE) {
184                 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
185                 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
186         }
187         
188         if(device_handle == INVALID_HANDLE_VALUE) {
189                 logger(LOG_ERR, _("%s (%s) is not a usable Windows tap device: %s"), device, iface, winerror(GetLastError()));
190                 return false;
191         }
192
193         /* Get MAC address from tap device */
194
195         if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
196                 logger(LOG_ERR, _("Could not get MAC address from Windows tap device %s (%s): %s"), device, iface, winerror(GetLastError()));
197                 return false;
198         }
199
200         if(routing_mode == RMODE_ROUTER) {
201                 overwrite_mac = 1;
202         }
203
204         /* Start the tap reader */
205
206         thread = CreateThread(NULL, 0, tapreader, NULL, 0, NULL);
207
208         if(!thread) {
209                 logger(LOG_ERR, _("System call `%s' failed: %s"), "CreateThread", winerror(GetLastError()));
210                 return false;
211         }
212
213         /* Set media status for newer TAP-Win32 devices */
214
215         status = true;
216         DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL);
217
218         device_info = _("Windows tap device");
219
220         logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
221
222         return true;
223 }
224
225 void close_device(void)
226 {
227         cp();
228
229         CloseHandle(device_handle);
230
231         free(device);
232         free(iface);
233 }
234
235 bool read_packet(vpn_packet_t *packet)
236 {
237         return false;
238 }
239
240 bool write_packet(vpn_packet_t *packet)
241 {
242         long lenout;
243         OVERLAPPED overlapped = {0};
244
245         cp();
246
247         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
248                            packet->len, device_info);
249
250         if(!WriteFile(device_handle, packet->data, packet->len, &lenout, &overlapped)) {
251                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, winerror(GetLastError()));
252                 return false;
253         }
254
255         device_total_out += packet->len;
256
257         return true;
258 }
259
260 void dump_device_stats(void)
261 {
262         cp();
263
264         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
265         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
266         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
267 }