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