2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2008 Guus Sliepen <guus@tinc-vpn.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 1398 2004-11-01 15:18:53Z guus $
32 #define DEFAULT_DEVICE "/dev/tun0"
34 typedef enum device_type {
36 DEVICE_TYPE_TUNIFHEAD,
43 static char *device_info = NULL;
44 static int device_total_in = 0;
45 static int device_total_out = 0;
46 #if defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
47 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
49 static device_type_t device_type = DEVICE_TYPE_TUN;
52 bool setup_device(void) {
57 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
58 device = xstrdup(DEFAULT_DEVICE);
60 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
61 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
63 if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
64 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
68 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
69 if(!strcasecmp(type, "tun"))
71 else if(!strcasecmp(type, "tunnohead"))
72 device_type = DEVICE_TYPE_TUN;
73 else if(!strcasecmp(type, "tunifhead"))
74 device_type = DEVICE_TYPE_TUNIFHEAD;
75 else if(!strcasecmp(type, "tap"))
76 device_type = DEVICE_TYPE_TAP;
78 logger(LOG_ERR, _("Unknown device type %s!"), type);
82 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
83 device_type = DEVICE_TYPE_TAP;
88 device_type = DEVICE_TYPE_TUN;
93 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
94 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
99 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
101 const int mode = IFF_BROADCAST | IFF_MULTICAST;
102 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
106 device_info = _("Generic BSD tun device");
108 case DEVICE_TYPE_TUNIFHEAD:
112 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
113 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
118 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
120 const int mode = IFF_BROADCAST | IFF_MULTICAST;
121 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
125 device_info = _("Generic BSD tun device");
127 case DEVICE_TYPE_TAP:
128 if(routing_mode == RMODE_ROUTER)
129 overwrite_mac = true;
130 device_info = _("Generic BSD tap device");
134 logger(LOG_INFO, _("%s is a %s"), device, device_info);
139 void close_device(void) {
145 bool read_packet(vpn_packet_t *packet) {
150 switch(device_type) {
151 case DEVICE_TYPE_TUN:
152 if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
153 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
154 device, strerror(errno));
158 switch(packet->data[14] >> 4) {
160 packet->data[12] = 0x08;
161 packet->data[13] = 0x00;
164 packet->data[12] = 0x86;
165 packet->data[13] = 0xDD;
168 ifdebug(TRAFFIC) logger(LOG_ERR,
169 _ ("Unknown IP version %d while reading packet from %s %s"),
170 packet->data[14] >> 4, device_info, device);
174 packet->len = lenin + 14;
177 case DEVICE_TYPE_TUNIFHEAD: {
179 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
181 if((lenin = readv(device_fd, vector, 2)) <= 0) {
182 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
183 device, strerror(errno));
187 switch (ntohl(type)) {
189 packet->data[12] = 0x08;
190 packet->data[13] = 0x00;
194 packet->data[12] = 0x86;
195 packet->data[13] = 0xDD;
199 ifdebug(TRAFFIC) logger(LOG_ERR,
200 _ ("Unknown address family %x while reading packet from %s %s"),
201 ntohl(type), device_info, device);
205 packet->len = lenin + 10;
209 case DEVICE_TYPE_TAP:
210 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
211 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
212 device, strerror(errno));
223 device_total_in += packet->len;
225 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
226 packet->len, device_info);
231 bool write_packet(vpn_packet_t *packet)
235 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
236 packet->len, device_info);
238 switch(device_type) {
239 case DEVICE_TYPE_TUN:
240 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
241 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
242 device, strerror(errno));
247 case DEVICE_TYPE_TUNIFHEAD: {
249 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
252 af = (packet->data[12] << 8) + packet->data[13];
256 type = htonl(AF_INET);
259 type = htonl(AF_INET6);
262 ifdebug(TRAFFIC) logger(LOG_ERR,
263 _("Unknown address family %x while writing packet to %s %s"),
264 af, device_info, device);
268 if(writev(device_fd, vector, 2) < 0) {
269 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
276 case DEVICE_TYPE_TAP:
277 if(write(device_fd, packet->data, packet->len) < 0) {
278 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
279 device, strerror(errno));
288 device_total_out += packet->len;
293 void dump_device_stats(void)
297 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
298 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
299 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);