2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2001-2005 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) {
56 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
57 device = DEFAULT_DEVICE;
59 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
60 iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
62 if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
63 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
67 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
68 if(!strcasecmp(type, "tun"))
70 else if(!strcasecmp(type, "tunnohead"))
71 device_type = DEVICE_TYPE_TUN;
72 else if(!strcasecmp(type, "tunifhead"))
73 device_type = DEVICE_TYPE_TUNIFHEAD;
74 else if(!strcasecmp(type, "tap"))
75 device_type = DEVICE_TYPE_TAP;
77 logger(LOG_ERR, _("Unknown device type %s!"), type);
81 if(strstr(device, "tap"))
82 device_type = DEVICE_TYPE_TAP;
87 device_type = DEVICE_TYPE_TUN;
92 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
93 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
98 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
100 const int mode = IFF_BROADCAST | IFF_MULTICAST;
101 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
105 device_info = _("Generic BSD tun device");
107 case DEVICE_TYPE_TUNIFHEAD:
111 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
112 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
117 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
119 const int mode = IFF_BROADCAST | IFF_MULTICAST;
120 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
124 device_info = _("Generic BSD tun device");
126 case DEVICE_TYPE_TAP:
127 if(routing_mode == RMODE_ROUTER)
128 overwrite_mac = true;
129 device_info = _("Generic BSD tap device");
133 logger(LOG_INFO, _("%s is a %s"), device, device_info);
138 void close_device(void) {
144 bool read_packet(vpn_packet_t *packet) {
149 switch(device_type) {
150 case DEVICE_TYPE_TUN:
151 if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
152 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
153 device, strerror(errno));
157 switch(packet->data[14] >> 4) {
159 packet->data[12] = 0x08;
160 packet->data[13] = 0x00;
163 packet->data[12] = 0x86;
164 packet->data[13] = 0xDD;
167 ifdebug(TRAFFIC) logger(LOG_ERR,
168 _ ("Unknown IP version %d while reading packet from %s %s"),
169 packet->data[14] >> 4, device_info, device);
173 packet->len = lenin + 14;
176 case DEVICE_TYPE_TUNIFHEAD: {
178 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
180 if((lenin = readv(device_fd, vector, 2)) <= 0) {
181 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
182 device, strerror(errno));
186 switch (ntohl(type)) {
188 packet->data[12] = 0x08;
189 packet->data[13] = 0x00;
193 packet->data[12] = 0x86;
194 packet->data[13] = 0xDD;
198 ifdebug(TRAFFIC) logger(LOG_ERR,
199 _ ("Unknown address family %x while reading packet from %s %s"),
200 ntohl(type), device_info, device);
204 packet->len = lenin + 10;
208 case DEVICE_TYPE_TAP:
209 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
210 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
211 device, strerror(errno));
222 device_total_in += packet->len;
224 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
225 packet->len, device_info);
230 bool write_packet(vpn_packet_t *packet)
234 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
235 packet->len, device_info);
237 switch(device_type) {
238 case DEVICE_TYPE_TUN:
239 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
240 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
241 device, strerror(errno));
246 case DEVICE_TYPE_TUNIFHEAD: {
248 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
251 af = (packet->data[12] << 8) + packet->data[13];
255 type = htonl(AF_INET);
258 type = htonl(AF_INET6);
261 ifdebug(TRAFFIC) logger(LOG_ERR,
262 _("Unknown address family %x while writing packet to %s %s"),
263 af, device_info, device);
267 if(writev(device_fd, vector, 2) < 0) {
268 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
275 case DEVICE_TYPE_TAP:
276 if(write(device_fd, packet->data, packet->len) < 0) {
277 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
278 device, strerror(errno));
287 device_total_out += packet->len;
292 void dump_device_stats(void)
296 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
297 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
298 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);