2 device.c -- Interaction with Linux ethertap and 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifdef HAVE_LINUX_IF_TUN_H
24 #include <linux/if_tun.h>
25 #define DEFAULT_DEVICE "/dev/net/tun"
27 #define DEFAULT_DEVICE "/dev/tap0"
39 typedef enum device_type_t {
47 static device_type_t device_type;
50 static char ifrname[IFNAMSIZ];
51 static char *device_info;
53 static uint64_t device_total_in = 0;
54 static uint64_t device_total_out = 0;
58 static void read_thread(void *arg) {
59 static vpn_packet_t packet;
63 if(read_packet(&packet)) {
65 route(myself, &packet);
68 if(errno == EAGAIN || errno == EINTR) {
78 bool setup_device(void) {
82 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
83 device = xstrdup(DEFAULT_DEVICE);
85 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
86 #ifdef HAVE_LINUX_IF_TUN_H
88 iface = xstrdup(netname);
90 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
92 fd = open(device, O_RDWR);
95 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
99 #ifdef HAVE_LINUX_IF_TUN_H
100 /* Ok now check if this is an old ethertap or a new tun/tap thingie */
102 memset(&ifr, 0, sizeof ifr);
103 if(routing_mode == RMODE_ROUTER) {
104 ifr.ifr_flags = IFF_TUN;
105 device_type = DEVICE_TYPE_TUN;
106 device_info = "Linux tun/tap device (tun mode)";
108 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
109 device_type = DEVICE_TYPE_TAP;
110 device_info = "Linux tun/tap device (tap mode)";
114 /* Set IFF_ONE_QUEUE flag... */
115 if(get_config_bool(lookup_config(config_tree, "IffOneQueue"), &t1q) && t1q)
116 ifr.ifr_flags |= IFF_ONE_QUEUE;
120 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
122 if(!ioctl(fd, TUNSETIFF, &ifr)) {
123 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
124 if(iface) free(iface);
125 iface = xstrdup(ifrname);
126 } else if(!ioctl(fd, (('T' << 8) | 202), &ifr)) {
127 logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
128 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
129 if(iface) free(iface);
130 iface = xstrdup(ifrname);
134 if(routing_mode == RMODE_ROUTER)
135 overwrite_mac = true;
136 device_info = "Linux ethertap device";
137 device_type = DEVICE_TYPE_ETHERTAP;
140 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
143 logger(LOG_INFO, "%s is a %s", device, device_info);
145 return thread_create(&thread, read_thread, NULL);
148 void close_device(void) {
150 thread_destroy(&thread);
156 bool read_packet(vpn_packet_t *packet) {
159 switch(device_type) {
160 case DEVICE_TYPE_TUN:
161 inlen = read(fd, packet->data + 10, MTU - 10);
164 logger(LOG_ERR, "Error while reading from %s %s: %s",
165 device_info, device, strerror(errno));
169 packet->len = inlen + 10;
171 case DEVICE_TYPE_TAP:
172 inlen = read(fd, packet->data, MTU);
175 logger(LOG_ERR, "Error while reading from %s %s: %s",
176 device_info, device, strerror(errno));
182 case DEVICE_TYPE_ETHERTAP:
183 inlen = read(fd, packet->data - 2, MTU + 2);
186 logger(LOG_ERR, "Error while reading from %s %s: %s",
187 device_info, device, strerror(errno));
191 packet->len = inlen - 2;
195 device_total_in += packet->len;
197 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
203 bool write_packet(vpn_packet_t *packet) {
204 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
205 packet->len, device_info);
207 switch(device_type) {
208 case DEVICE_TYPE_TUN:
209 packet->data[10] = packet->data[11] = 0;
210 if(write(fd, packet->data + 10, packet->len - 10) < 0) {
211 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
216 case DEVICE_TYPE_TAP:
217 if(write(fd, packet->data, packet->len) < 0) {
218 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
223 case DEVICE_TYPE_ETHERTAP:
224 *(short int *)(packet->data - 2) = packet->len;
226 if(write(fd, packet->data - 2, packet->len + 2) < 0) {
227 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
234 device_total_out += packet->len;
239 void dump_device_stats(void) {
240 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
241 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
242 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);