2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2001-2004 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 $
31 #define DEFAULT_DEVICE "/dev/tun0"
33 typedef enum device_type {
35 DEVICE_TYPE_TUNIFHEAD,
43 static int device_total_in = 0;
44 static int device_total_out = 0;
46 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
48 static device_type_t device_type = DEVICE_TYPE_TUN;
51 bool setup_device(void) {
60 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
61 device = DEFAULT_DEVICE;
63 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
64 iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
66 if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
67 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
71 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
72 if(!strcasecmp(type, "tun"))
74 else if(!strcasecmp(type, "tunnohead"))
75 device_type = DEVICE_TYPE_TUN;
76 else if(!strcasecmp(type, "tunifhead"))
77 device_type = DEVICE_TYPE_TUNIFHEAD;
78 else if(!strcasecmp(type, "tap"))
79 device_type = DEVICE_TYPE_TAP;
81 logger(LOG_ERR, _("Unknown device type %s!"), type);
85 if(strstr(device, "tap"))
86 device_type = DEVICE_TYPE_TAP;
91 device_type = DEVICE_TYPE_TUN;
94 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
95 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
99 device_info = _("Generic BSD tun device");
101 case DEVICE_TYPE_TUNIFHEAD:
103 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
104 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
108 device_info = _("Generic BSD tun device");
110 case DEVICE_TYPE_TAP:
111 if(routing_mode == RMODE_ROUTER)
112 overwrite_mac = true;
113 device_info = _("Generic BSD tap device");
117 logger(LOG_INFO, _("%s is a %s"), device, device_info);
122 void close_device(void) {
128 bool read_packet(vpn_packet_t *packet) {
133 switch(device_type) {
134 case DEVICE_TYPE_TUN:
135 if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
136 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
137 device, strerror(errno));
141 switch(packet->data[14] >> 4) {
143 packet->data[12] = 0x08;
144 packet->data[13] = 0x00;
147 packet->data[12] = 0x86;
148 packet->data[13] = 0xDD;
151 ifdebug(TRAFFIC) logger(LOG_ERR,
152 _ ("Unknown IP version %d while reading packet from %s %s"),
153 packet->data[14] >> 4, device_info, device);
157 packet->len = lenin + 14;
160 case DEVICE_TYPE_TUNIFHEAD: {
162 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
164 if((lenin = readv(device_fd, vector, 2)) <= 0) {
165 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
166 device, strerror(errno));
170 switch (ntohl(type)) {
172 packet->data[12] = 0x08;
173 packet->data[13] = 0x00;
177 packet->data[12] = 0x86;
178 packet->data[13] = 0xDD;
182 ifdebug(TRAFFIC) logger(LOG_ERR,
183 _ ("Unknown address family %x while reading packet from %s %s"),
184 ntohl(type), device_info, device);
188 packet->len = lenin + 10;
192 case DEVICE_TYPE_TAP:
193 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
194 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
195 device, strerror(errno));
206 device_total_in += packet->len;
208 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
209 packet->len, device_info);
214 bool write_packet(vpn_packet_t *packet)
218 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
219 packet->len, device_info);
221 switch(device_type) {
222 case DEVICE_TYPE_TUN:
223 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
224 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
225 device, strerror(errno));
230 case DEVICE_TYPE_TUNIFHEAD: {
232 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
235 af = (packet->data[12] << 8) + packet->data[13];
239 type = htonl(AF_INET);
242 type = htonl(AF_INET6);
245 ifdebug(TRAFFIC) logger(LOG_ERR,
246 _("Unknown address family %x while writing packet to %s %s"),
247 af, device_info, device);
251 if(writev(device_fd, vector, 2) < 0) {
252 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
259 case DEVICE_TYPE_TAP:
260 if(write(device_fd, packet->data, packet->len) < 0) {
261 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
262 device, strerror(errno));
271 device_total_out += packet->len;
276 void dump_device_stats(void)
280 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
281 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
282 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);