-
Notifications
You must be signed in to change notification settings - Fork 1
/
tun-win32.c
196 lines (156 loc) · 4.42 KB
/
tun-win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* tun-win32.c
*
* Copyright (c) 2001 Dug Song <[email protected]>
*
* $Id: tun-win32.c,v 1.1 2002/02/25 06:21:59 dugsong Exp $
*/
#include "config.h"
#include <dnet.h>
#include <event.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tun.h"
#define USER_DEVICE_DIR "\\\\.\\"
#define NETCARD_REG_KEY_2000 "SYSTEM\\CurrentControlSet\\Control\\Class" \
"\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
#if 0
#define NETCARD_REG_KEY "SOFTWARE\\Microsoft\\Windows NT" \
"\\CurrentVersion\\NetworkCards"
#endif
struct tun {
arp_t *arp;
intf_t *intf;
route_t *route;
struct arp_entry arpent;
struct intf_entry ifent;
struct route_entry rtent;
struct addr dst;
HANDLE handle;
struct event ev;
tun_handler callback;
void *arg;
};
static int
reg_query_compare(HKEY key, char *name, char *text)
{
char value[512];
u_long size;
size = sizeof(value);
if (RegQueryValueEx(key, name, 0, 0, value, &size) != ERROR_SUCCESS)
return (-1);
return (strcmp(text, value));
}
tun_t *
tun_open(struct addr *src, struct addr *dst, int mtu)
{
HKEY Key, AdapterKey;
FILETIME ltime;
tun_t *tun;
ip_addr_t mask;
u_long i, len;
char device[64], AdapterId[512], Value[512];
if ((tun = calloc(1, sizeof(*tun))) == NULL)
return (NULL);
if ((tun->arp = arp_open()) == NULL ||
(tun->intf = intf_open()) == NULL ||
(tun->route = route_open()) == NULL)
return (tun_close(tun));
tun->ifent.intf_len = sizeof(tun->ifent);
strlcpy(tun->ifent.intf_name, "DKW Heavy Industries VPN Adapter.",
sizeof(tun->ifent.intf_name));
if (intf_get(tun->intf, &tun->ifent) < 0 ||
tun->ifent.intf_addr.addr_type != ADDR_TYPE_IP)
return (tun_close(tun));
/* Add fake tunnel gateway. */
tun->arpent.arp_pa = tun->ifent.intf_addr;
addr_btom(tun->ifent.intf_addr.addr_bits, &mask, sizeof(mask));
tun->arpent.arp_pa.addr_ip &= mask;
tun->arpent.arp_pa.addr_ip |= htonl(1);
tun->arpent.arp_pa.addr_bits = IP_ADDR_BITS;
addr_aton("0:d:e:a:d:0", &tun->arpent.arp_ha);
if (arp_add(tun->arp, &tun->arpent) < 0)
return (tun_close(tun));
/* Add route for destination thru tunnel. */
tun->rtent.route_dst = *dst;
tun->rtent.route_gw = tun->arpent.arp_pa;
if (route_add(tun->route, &tun->rtent) < 0)
return (tun_close(tun));
tun->dst = *dst;
tun->handle = INVALID_HANDLE_VALUE;
/* Open CIPE device for reading. */
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETCARD_REG_KEY_2000,
0, KEY_READ, &Key) != ERROR_SUCCESS)
return (tun_close(tun));
for (i = 0; i < 666; i++) {
len = sizeof(AdapterId);
if (RegEnumKeyEx(Key, i, AdapterId, &len,
0, 0, 0, <ime) != ERROR_SUCCESS)
break;
if (RegOpenKeyEx(Key, AdapterId, 0, KEY_READ,
&AdapterKey) != ERROR_SUCCESS)
break;
len = sizeof(Value);
if (reg_query_compare(AdapterKey,
"Manufacturer", "DKWHeavyIndustries") == 0 &&
reg_query_compare(AdapterKey,
"ProductName", "CIPE") == 0 &&
RegQueryValueEx(AdapterKey, "NetCfgInstanceId",
0, 0, Value, &len) == ERROR_SUCCESS) {
snprintf(device, sizeof(device),
USER_DEVICE_DIR "%s.tap", Value);
tun->handle = CreateFile(device, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED|FILE_ATTRIBUTE_SYSTEM, 0);
RegCloseKey(AdapterKey);
break;
}
RegCloseKey(AdapterKey);
}
RegCloseKey(Key);
if (tun->handle == INVALID_HANDLE_VALUE)
return (tun_close(tun));
return (tun);
}
static void
_tun_recv(int fd, short event, void *arg)
{
struct event_iov *eio = (struct event_iov *)fd;
tun_t *tun = (tun_t *)arg;
struct ip_hdr *ip;
ip = (struct ip_hdr *)(eio->buf + ETH_HDR_LEN);
if (eio->len > ETH_HDR_LEN + IP_HDR_LEN &&
ip->ip_dst == tun->dst.addr_ip) {
(*tun->callback)(eio->buf + ETH_HDR_LEN,
eio->len - ETH_HDR_LEN, tun->arg);
}
event_add(&tun->ev, NULL);
}
int
tun_register(tun_t *tun, tun_handler callback, void *arg)
{
tun->callback = callback;
tun->arg = arg;
event_set(&tun->ev, (int)tun->handle, EV_READ, _tun_recv, tun);
event_add(&tun->ev, NULL);
return (0);
}
tun_t *
tun_close(tun_t *tun)
{
if (event_initialized(&tun->ev))
event_del(&tun->ev);
if (tun->handle != INVALID_HANDLE_VALUE)
CloseHandle(tun->handle);
if (tun->dst.addr_type == ADDR_TYPE_IP) {
route_delete(tun->route, &tun->rtent);
arp_delete(tun->arp, &tun->arpent);
}
if (tun->route != NULL)
route_close(tun->route);
if (tun->arp != NULL)
arp_close(tun->arp);
free(tun);
return (NULL);
}