2 device.c -- Interaction with Windows tap driver in a MinGW environment
3 Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2002-2003 Guus Sliepen <guus@sliepen.eu.org>
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.
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.
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.
20 $Id: device.c,v 1.1.2.9 2003/08/02 21:01:50 guus Exp $
35 #define NETCARD_REG_KEY_2000 "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
36 #define NETCARD_REG_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards"
37 #define REG_SERVICE_KEY "SYSTEM\\CurrentControlSet\\Services"
38 #define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
40 #define USERMODEDEVICEDIR "\\\\.\\"
41 #define SYSDEVICEDIR "\\Device\\"
42 #define USERDEVICEDIR "\\??\\"
43 #define TAPSUFFIX ".tap"
45 #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS)
47 #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED)
48 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED)
49 #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED)
51 /* FIXME: This only works for Windows 2000 */
55 HANDLE device_handle = INVALID_HANDLE_VALUE;
58 char *device_info = NULL;
60 int device_total_in = 0;
61 int device_total_out = 0;
63 DWORD WINAPI tapreader(void *bla) {
64 int sock, err, status;
66 struct addrinfo hint = {
67 .ai_family = AF_UNSPEC,
68 .ai_socktype = SOCK_DGRAM,
69 .ai_protocol = IPPROTO_UDP,
74 OVERLAPPED overlapped;
76 /* Open a socket to the parent process */
78 err = getaddrinfo(NULL, "12345", &hint, &ai);
81 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
85 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
90 logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
94 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
95 logger(LOG_ERR, _("System call `%s' failed: %s"), "connect", strerror(errno));
99 logger(LOG_DEBUG, _("Tap reader running"));
101 /* Read from tap device and send to parent */
103 overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
106 overlapped.Offset = 0;
107 overlapped.OffsetHigh = 0;
108 ResetEvent(overlapped.hEvent);
110 status = ReadFile(device_handle, buf, sizeof(buf), &len, &overlapped);
113 if(GetLastError() == ERROR_IO_PENDING) {
114 WaitForSingleObject(overlapped.hEvent, INFINITE);
115 if(!GetOverlappedResult(device_handle, &overlapped, &len, FALSE))
118 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
119 device, strerror(errno));
124 if(send(sock, buf, len, 0) <= 0)
129 bool setup_device(void)
135 char adapterid[1024];
136 char adaptername[1024];
146 struct addrinfo hint = {
147 .ai_family = AF_UNSPEC,
148 .ai_socktype = SOCK_DGRAM,
149 .ai_protocol = IPPROTO_UDP,
150 .ai_flags = AI_PASSIVE,
155 get_config_string(lookup_config(config_tree, "Device"), &device);
156 get_config_string(lookup_config(config_tree, "Interface"), &iface);
158 /* Open registry and look for network adapters */
160 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) {
161 logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError()));
166 len = sizeof(adapterid);
167 if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL))
170 /* Find out more about this adapter */
172 snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", REG_CONTROL_NET, adapterid);
174 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2))
177 len = sizeof(adaptername);
178 RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len);
183 if(!strcmp(device, adapterid)) {
191 if(!strcmp(iface, adaptername)) {
198 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
199 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
200 if(device_handle != INVALID_HANDLE_VALUE) {
209 logger(LOG_ERR, _("No Windows tap device found!"));
214 device = xstrdup(adapterid);
217 iface = xstrdup(adaptername);
219 /* Try to open the corresponding tap device */
221 if(device_handle == INVALID_HANDLE_VALUE) {
222 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
223 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
226 if(device_handle == INVALID_HANDLE_VALUE) {
227 logger(LOG_ERR, _("%s (%s) is no a usable Windows tap device!"), device, iface);
231 /* Get MAC address from tap device */
233 if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
234 logger(LOG_ERR, _("Could not get MAC address from Windows tap device!"));
238 if(routing_mode == RMODE_ROUTER) {
242 /* Create a listening socket */
244 err = getaddrinfo(NULL, "12345", &hint, &ai);
247 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
251 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
254 logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
258 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
259 logger(LOG_ERR, _("System call `%s' failed: %s"), "bind", strerror(errno));
265 if(listen(sock, 1)) {
266 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
270 /* Start the tap reader */
272 thread = CreateThread(NULL, 0, tapreader, NULL, 0, NULL);
275 logger(LOG_ERR, _("System call `%s' failed: %s"), "CreateThread", winerror(GetLastError()));
279 /* Wait for the tap reader to connect back to us */
281 if((device_fd = accept(sock, NULL, 0)) == -1) {
282 logger(LOG_ERR, _("System call `%s' failed: %s"), "accept", strerror(errno));
288 device_info = _("Windows tap device");
290 logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
295 void close_device(void)
299 CloseHandle(device_handle);
302 bool read_packet(vpn_packet_t *packet)
308 if((lenin = recv(device_fd, packet->data, MTU, 0)) <= 0) {
309 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
310 device, strerror(errno));
316 device_total_in += packet->len;
318 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
324 bool write_packet(vpn_packet_t *packet)
327 OVERLAPPED overlapped = {0};
331 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
332 packet->len, device_info);
334 if(!WriteFile(device_handle, packet->data, packet->len, &lenout, &overlapped)) {
335 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, winerror(GetLastError()));
339 device_total_out += packet->len;
344 void dump_device_stats(void)
348 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
349 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
350 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);