Experimental IFF_ONE_QUEUE support for Linux
[tinc] / src / linux / device.c
1 /*
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>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #ifdef HAVE_LINUX_IF_TUN_H
24 #include <linux/if_tun.h>
25 #define DEFAULT_DEVICE "/dev/net/tun"
26 #else
27 #define DEFAULT_DEVICE "/dev/tap0"
28 #endif
29
30 #include "conf.h"
31 #include "logger.h"
32 #include "net.h"
33 #include "route.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 typedef enum device_type_t {
38         DEVICE_TYPE_ETHERTAP,
39         DEVICE_TYPE_TUN,
40         DEVICE_TYPE_TAP,
41 } device_type_t;
42
43 int device_fd = -1;
44 static device_type_t device_type;
45 char *device = NULL;
46 char *iface = NULL;
47 static char ifrname[IFNAMSIZ];
48 static char *device_info;
49
50 static uint64_t device_total_in = 0;
51 static uint64_t device_total_out = 0;
52
53 bool setup_device(void) {
54         struct ifreq ifr;
55         bool t1q = false;
56
57         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
58                 device = xstrdup(DEFAULT_DEVICE);
59
60         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
61 #ifdef HAVE_LINUX_IF_TUN_H
62                 if (netname != NULL)
63                         iface = xstrdup(netname);
64 #else
65                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
66 #endif
67         device_fd = open(device, O_RDWR | O_NONBLOCK);
68
69         if(device_fd < 0) {
70                 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
71                 return false;
72         }
73
74 #ifdef HAVE_LINUX_IF_TUN_H
75         /* Ok now check if this is an old ethertap or a new tun/tap thingie */
76
77         memset(&ifr, 0, sizeof(ifr));
78         if(routing_mode == RMODE_ROUTER) {
79                 ifr.ifr_flags = IFF_TUN;
80                 device_type = DEVICE_TYPE_TUN;
81                 device_info = "Linux tun/tap device (tun mode)";
82         } else {
83                 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
84                 device_type = DEVICE_TYPE_TAP;
85                 device_info = "Linux tun/tap device (tap mode)";
86         }
87
88 #ifdef IFF_ONE_QUEUE
89         /* Set IFF_ONE_QUEUE flag... */
90         if(get_config_bool(lookup_config(config_tree, "IffOneQueue"), &t1q) && t1q)
91                 ifr.ifr_flags |= IFF_ONE_QUEUE;
92 #endif
93
94         if(iface)
95                 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
96
97         if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
98                 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
99                 if(iface) free(iface);
100                 iface = xstrdup(ifrname);
101         } else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
102                 logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
103                 strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
104                 if(iface) free(iface);
105                 iface = xstrdup(ifrname);
106         } else
107 #endif
108         {
109                 if(routing_mode == RMODE_ROUTER)
110                         overwrite_mac = true;
111                 device_info = "Linux ethertap device";
112                 device_type = DEVICE_TYPE_ETHERTAP;
113                 if(iface)
114                         free(iface);
115                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
116         }
117
118         logger(LOG_INFO, "%s is a %s", device, device_info);
119
120         return true;
121 }
122
123 void close_device(void) {
124         close(device_fd);
125
126         free(device);
127         free(iface);
128 }
129
130 bool read_packet(vpn_packet_t *packet) {
131         int lenin;
132         
133         switch(device_type) {
134                 case DEVICE_TYPE_TUN:
135                         lenin = read(device_fd, packet->data + 10, MTU - 10);
136
137                         if(lenin <= 0) {
138                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
139                                            device_info, device, strerror(errno));
140                                 return false;
141                         }
142
143                         packet->len = lenin + 10;
144                         break;
145                 case DEVICE_TYPE_TAP:
146                         lenin = read(device_fd, packet->data, MTU);
147
148                         if(lenin <= 0) {
149                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
150                                            device_info, device, strerror(errno));
151                                 return false;
152                         }
153
154                         packet->len = lenin;
155                         break;
156                 case DEVICE_TYPE_ETHERTAP:
157                         lenin = read(device_fd, packet->data - 2, MTU + 2);
158
159                         if(lenin <= 0) {
160                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
161                                            device_info, device, strerror(errno));
162                                 return false;
163                         }
164
165                         packet->len = lenin - 2;
166                         break;
167         }
168
169         device_total_in += packet->len;
170
171         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
172                            device_info);
173
174         return true;
175 }
176
177 bool write_packet(vpn_packet_t *packet) {
178         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
179                            packet->len, device_info);
180
181         switch(device_type) {
182                 case DEVICE_TYPE_TUN:
183                         packet->data[10] = packet->data[11] = 0;
184                         if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
185                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
186                                            strerror(errno));
187                                 return false;
188                         }
189                         break;
190                 case DEVICE_TYPE_TAP:
191                         if(write(device_fd, packet->data, packet->len) < 0) {
192                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
193                                            strerror(errno));
194                                 return false;
195                         }
196                         break;
197                 case DEVICE_TYPE_ETHERTAP:
198                         *(short int *)(packet->data - 2) = packet->len;
199
200                         if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
201                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
202                                            strerror(errno));
203                                 return false;
204                         }
205                         break;
206         }
207
208         device_total_out += packet->len;
209
210         return true;
211 }
212
213 void dump_device_stats(void) {
214         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
215         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
216         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
217 }