1 |
/* |
2 |
device.c -- Interaction with Linux ethertap and tun/tap device |
3 |
Copyright (C) 2001-2003 Ivo Timmermans <ivo@o2w.nl>, |
4 |
2001-2003 Guus Sliepen <guus@sliepen.eu.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: device.c,v 1.1.2.20 2003/07/22 20:55:21 guus Exp $ |
21 |
*/ |
22 |
|
23 |
|
24 |
#ifdef HAVE_TUNTAP |
25 |
#ifdef LINUX_IF_TUN_H |
26 |
#include LINUX_IF_TUN_H |
27 |
#else |
28 |
#include <linux/if_tun.h> |
29 |
#endif |
30 |
#define DEFAULT_DEVICE "/dev/net/tun" |
31 |
#else |
32 |
#define DEFAULT_DEVICE "/dev/tap0" |
33 |
#endif |
34 |
|
35 |
|
36 |
typedef enum device_type_t { |
37 |
DEVICE_TYPE_ETHERTAP, |
38 |
DEVICE_TYPE_TUN, |
39 |
DEVICE_TYPE_TAP, |
40 |
} device_type_t; |
41 |
|
42 |
int device_fd = -1; |
43 |
device_type_t device_type; |
44 |
char *device; |
45 |
char *iface; |
46 |
char ifrname[IFNAMSIZ]; |
47 |
char *device_info; |
48 |
|
49 |
int device_total_in = 0; |
50 |
int device_total_out = 0; |
51 |
|
52 |
bool setup_device(void) |
53 |
{ |
54 |
struct ifreq ifr; |
55 |
|
56 |
cp(); |
57 |
|
58 |
if(!get_config_string(lookup_config(config_tree, "Device"), &device)) |
59 |
device = DEFAULT_DEVICE; |
60 |
|
61 |
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) |
62 |
#ifdef HAVE_TUNTAP |
63 |
iface = netname; |
64 |
#else |
65 |
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; |
66 |
#endif |
67 |
device_fd = open(device, O_RDWR | O_NONBLOCK); |
68 |
|
69 |
if(device_fd < 0) { |
70 |
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); |
71 |
return false; |
72 |
} |
73 |
|
74 |
#ifdef HAVE_TUNTAP |
75 |
/* Ok now check if this is an old ethertap or a new tun/tap thingie */ |
76 |
|
77 |
memset(&ifr, 0, sizeof(ifr)); |
78 |
if(routing_mode == RMODE_ROUTER) { |
79 |
ifr.ifr_flags = IFF_TUN; |
80 |
device_type = DEVICE_TYPE_TUN; |
81 |
device_info = _("Linux tun/tap device (tun mode)"); |
82 |
} else { |
83 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
84 |
device_type = DEVICE_TYPE_TAP; |
85 |
device_info = _("Linux tun/tap device (tap mode)"); |
86 |
} |
87 |
|
88 |
if(iface) |
89 |
strncpy(ifr.ifr_name, iface, IFNAMSIZ); |
90 |
|
91 |
if(!ioctl(device_fd, TUNSETIFF, (void *) &ifr)) { |
92 |
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
93 |
iface = ifrname; |
94 |
} else if(!ioctl(device_fd, (('T' << 8) | 202), (void *) &ifr)) { |
95 |
logger(LOG_WARNING, _("Old ioctl() request was needed for %s"), device); |
96 |
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
97 |
iface = ifrname; |
98 |
} else |
99 |
#endif |
100 |
{ |
101 |
if(routing_mode == RMODE_ROUTER) |
102 |
overwrite_mac = true; |
103 |
device_info = _("Linux ethertap device"); |
104 |
device_type = DEVICE_TYPE_ETHERTAP; |
105 |
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; |
106 |
} |
107 |
|
108 |
logger(LOG_INFO, _("%s is a %s"), device, device_info); |
109 |
|
110 |
return true; |
111 |
} |
112 |
|
113 |
void close_device(void) |
114 |
{ |
115 |
cp(); |
116 |
|
117 |
close(device_fd); |
118 |
} |
119 |
|
120 |
bool read_packet(vpn_packet_t *packet) |
121 |
{ |
122 |
int lenin; |
123 |
|
124 |
cp(); |
125 |
|
126 |
switch(device_type) { |
127 |
case DEVICE_TYPE_TUN: |
128 |
lenin = read(device_fd, packet->data + 10, MTU - 10); |
129 |
|
130 |
if(lenin <= 0) { |
131 |
logger(LOG_ERR, _("Error while reading from %s %s: %s"), |
132 |
device_info, device, strerror(errno)); |
133 |
return false; |
134 |
} |
135 |
|
136 |
packet->len = lenin + 10; |
137 |
break; |
138 |
case DEVICE_TYPE_TAP: |
139 |
lenin = read(device_fd, packet->data, MTU); |
140 |
|
141 |
if(lenin <= 0) { |
142 |
logger(LOG_ERR, _("Error while reading from %s %s: %s"), |
143 |
device_info, device, strerror(errno)); |
144 |
return false; |
145 |
} |
146 |
|
147 |
packet->len = lenin; |
148 |
break; |
149 |
case DEVICE_TYPE_ETHERTAP: |
150 |
lenin = read(device_fd, packet->data - 2, MTU + 2); |
151 |
|
152 |
if(lenin <= 0) { |
153 |
logger(LOG_ERR, _("Error while reading from %s %s: %s"), |
154 |
device_info, device, strerror(errno)); |
155 |
return false; |
156 |
} |
157 |
|
158 |
packet->len = lenin - 2; |
159 |
break; |
160 |
} |
161 |
|
162 |
device_total_in += packet->len; |
163 |
|
164 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, |
165 |
device_info); |
166 |
|
167 |
return true; |
168 |
} |
169 |
|
170 |
bool write_packet(vpn_packet_t *packet) |
171 |
{ |
172 |
cp(); |
173 |
|
174 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"), |
175 |
packet->len, device_info); |
176 |
|
177 |
switch(device_type) { |
178 |
case DEVICE_TYPE_TUN: |
179 |
packet->data[10] = packet->data[11] = 0; |
180 |
if(write(device_fd, packet->data + 10, packet->len - 10) < 0) { |
181 |
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, |
182 |
strerror(errno)); |
183 |
return false; |
184 |
} |
185 |
break; |
186 |
case DEVICE_TYPE_TAP: |
187 |
if(write(device_fd, packet->data, packet->len) < 0) { |
188 |
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, |
189 |
strerror(errno)); |
190 |
return false; |
191 |
} |
192 |
break; |
193 |
case DEVICE_TYPE_ETHERTAP: |
194 |
*(short int *)(packet->data - 2) = packet->len; |
195 |
|
196 |
if(write(device_fd, packet->data - 2, packet->len + 2) < 0) { |
197 |
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, |
198 |
strerror(errno)); |
199 |
return false; |
200 |
} |
201 |
break; |
202 |
} |
203 |
|
204 |
device_total_out += packet->len; |
205 |
|
206 |
return true; |
207 |
} |
208 |
|
209 |
void dump_device_stats(void) |
210 |
{ |
211 |
cp(); |
212 |
|
213 |
logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device); |
214 |
logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in); |
215 |
logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out); |
216 |
} |