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"
37 typedef enum device_type_t {
44 static device_type_t device_type;
47 static char ifrname[IFNAMSIZ];
48 static char *device_info;
50 static int device_total_in = 0;
51 static int device_total_out = 0;
53 bool setup_device(void) {
56 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
57 device = xstrdup(DEFAULT_DEVICE);
59 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
60 #ifdef HAVE_LINUX_IF_TUN_H
62 iface = xstrdup(netname);
64 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
66 device_fd = open(device, O_RDWR | O_NONBLOCK);
69 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
73 #ifdef HAVE_LINUX_IF_TUN_H
74 /* Ok now check if this is an old ethertap or a new tun/tap thingie */
76 memset(&ifr, 0, sizeof(ifr));
77 if(routing_mode == RMODE_ROUTER) {
78 ifr.ifr_flags = IFF_TUN;
79 device_type = DEVICE_TYPE_TUN;
80 device_info = "Linux tun/tap device (tun mode)";
82 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
83 device_type = DEVICE_TYPE_TAP;
84 device_info = "Linux tun/tap device (tap mode)";
88 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
90 if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
91 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
92 if(iface) free(iface);
93 iface = xstrdup(ifrname);
94 } else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
95 logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
96 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
97 if(iface) free(iface);
98 iface = xstrdup(ifrname);
102 if(routing_mode == RMODE_ROUTER)
103 overwrite_mac = true;
104 device_info = "Linux ethertap device";
105 device_type = DEVICE_TYPE_ETHERTAP;
108 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
111 logger(LOG_INFO, "%s is a %s", device, device_info);
116 void close_device(void) {
123 bool read_packet(vpn_packet_t *packet) {
126 switch(device_type) {
127 case DEVICE_TYPE_TUN:
128 lenin = read(device_fd, packet->data + 10, MTU - 10);
131 logger(LOG_ERR, "Error while reading from %s %s: %s",
132 device_info, device, strerror(errno));
136 packet->len = lenin + 10;
138 case DEVICE_TYPE_TAP:
139 lenin = read(device_fd, packet->data, MTU);
142 logger(LOG_ERR, "Error while reading from %s %s: %s",
143 device_info, device, strerror(errno));
149 case DEVICE_TYPE_ETHERTAP:
150 lenin = read(device_fd, packet->data - 2, MTU + 2);
153 logger(LOG_ERR, "Error while reading from %s %s: %s",
154 device_info, device, strerror(errno));
158 packet->len = lenin - 2;
162 device_total_in += packet->len;
164 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
170 bool write_packet(vpn_packet_t *packet) {
171 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
172 packet->len, device_info);
174 switch(device_type) {
175 case DEVICE_TYPE_TUN:
176 packet->data[10] = packet->data[11] = 0;
177 if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
178 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
183 case DEVICE_TYPE_TAP:
184 if(write(device_fd, packet->data, packet->len) < 0) {
185 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
190 case DEVICE_TYPE_ETHERTAP:
191 *(short int *)(packet->data - 2) = packet->len;
193 if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
194 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
201 device_total_out += packet->len;
206 void dump_device_stats(void) {
207 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
208 logger(LOG_DEBUG, " total bytes in: %10d", device_total_in);
209 logger(LOG_DEBUG, " total bytes out: %10d", device_total_out);