2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2009 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 $
33 #include "bsd/tunemu.h"
36 #define DEFAULT_DEVICE "/dev/tun0"
38 typedef enum device_type {
40 DEVICE_TYPE_TUNIFHEAD,
50 static char *device_info = NULL;
51 static int device_total_in = 0;
52 static int device_total_out = 0;
54 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
55 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
56 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
58 static device_type_t device_type = DEVICE_TYPE_TUN;
61 bool setup_device(void) {
66 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
67 device = xstrdup(DEFAULT_DEVICE);
69 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
70 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
72 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
73 if(!strcasecmp(type, "tun"))
76 else if(!strcasecmp(type, "tunemu"))
77 device_type = DEVICE_TYPE_TUNEMU;
79 else if(!strcasecmp(type, "tunnohead"))
80 device_type = DEVICE_TYPE_TUN;
81 else if(!strcasecmp(type, "tunifhead"))
82 device_type = DEVICE_TYPE_TUNIFHEAD;
83 else if(!strcasecmp(type, "tap"))
84 device_type = DEVICE_TYPE_TAP;
86 logger(LOG_ERR, _("Unknown device type %s!"), type);
90 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
91 device_type = DEVICE_TYPE_TAP;
96 case DEVICE_TYPE_TUNEMU: {
97 char dynamic_name[256] = "";
98 device_fd = tunemu_open(dynamic_name);
103 device_fd = open(device, O_RDWR | O_NONBLOCK);
107 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
111 switch(device_type) {
113 device_type = DEVICE_TYPE_TUN;
114 case DEVICE_TYPE_TUN:
118 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
119 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
124 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
126 const int mode = IFF_BROADCAST | IFF_MULTICAST;
127 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
131 device_info = _("Generic BSD tun device");
133 case DEVICE_TYPE_TUNIFHEAD:
137 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
138 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
143 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
145 const int mode = IFF_BROADCAST | IFF_MULTICAST;
146 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
150 device_info = _("Generic BSD tun device");
152 case DEVICE_TYPE_TAP:
153 if(routing_mode == RMODE_ROUTER)
154 overwrite_mac = true;
155 device_info = _("Generic BSD tap device");
158 case DEVICE_TYPE_TUNEMU:
159 device_info = _("BSD tunemu device");
164 logger(LOG_INFO, _("%s is a %s"), device, device_info);
169 void close_device(void) {
172 switch(device_type) {
174 case DEVICE_TYPE_TUNEMU:
175 tunemu_close(device_fd);
186 bool read_packet(vpn_packet_t *packet) {
191 switch(device_type) {
192 case DEVICE_TYPE_TUN:
194 case DEVICE_TYPE_TUNEMU:
195 if(device_type == DEVICE_TYPE_TUNEMU)
196 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
199 lenin = read(device_fd, packet->data + 14, MTU - 14);
203 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
204 device, strerror(errno));
208 switch(packet->data[14] >> 4) {
210 packet->data[12] = 0x08;
211 packet->data[13] = 0x00;
214 packet->data[12] = 0x86;
215 packet->data[13] = 0xDD;
218 ifdebug(TRAFFIC) logger(LOG_ERR,
219 _ ("Unknown IP version %d while reading packet from %s %s"),
220 packet->data[14] >> 4, device_info, device);
224 packet->len = lenin + 14;
227 case DEVICE_TYPE_TUNIFHEAD: {
229 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
231 if((lenin = readv(device_fd, vector, 2)) <= 0) {
232 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
233 device, strerror(errno));
237 switch (ntohl(type)) {
239 packet->data[12] = 0x08;
240 packet->data[13] = 0x00;
244 packet->data[12] = 0x86;
245 packet->data[13] = 0xDD;
249 ifdebug(TRAFFIC) logger(LOG_ERR,
250 _ ("Unknown address family %x while reading packet from %s %s"),
251 ntohl(type), device_info, device);
255 packet->len = lenin + 10;
259 case DEVICE_TYPE_TAP:
260 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
261 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
262 device, strerror(errno));
273 device_total_in += packet->len;
275 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
276 packet->len, device_info);
278 logger(LOG_INFO, "E:fd_read");
282 bool write_packet(vpn_packet_t *packet)
286 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
287 packet->len, device_info);
289 switch(device_type) {
290 case DEVICE_TYPE_TUN:
291 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
292 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
293 device, strerror(errno));
298 case DEVICE_TYPE_TUNIFHEAD: {
300 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
303 af = (packet->data[12] << 8) + packet->data[13];
307 type = htonl(AF_INET);
310 type = htonl(AF_INET6);
313 ifdebug(TRAFFIC) logger(LOG_ERR,
314 _("Unknown address family %x while writing packet to %s %s"),
315 af, device_info, device);
319 if(writev(device_fd, vector, 2) < 0) {
320 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
327 case DEVICE_TYPE_TAP:
328 if(write(device_fd, packet->data, packet->len) < 0) {
329 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
330 device, strerror(errno));
336 case DEVICE_TYPE_TUNEMU:
337 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
338 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
339 device, strerror(errno));
349 device_total_out += packet->len;
354 void dump_device_stats(void)
358 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
359 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
360 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);