Introducing the Big Tinc Lock.
[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 "device.h"
32 #include "logger.h"
33 #include "net.h"
34 #include "route.h"
35 #include "threads.h"
36 #include "utils.h"
37 #include "xalloc.h"
38
39 typedef enum device_type_t {
40         DEVICE_TYPE_ETHERTAP,
41         DEVICE_TYPE_TUN,
42         DEVICE_TYPE_TAP,
43 } device_type_t;
44
45 int device_fd = -1;
46 int fd = -1;
47 static device_type_t device_type;
48 char *device = NULL;
49 char *iface = NULL;
50 static char ifrname[IFNAMSIZ];
51 static char *device_info;
52
53 static uint64_t device_total_in = 0;
54 static uint64_t device_total_out = 0;
55
56 thread_t thread;
57
58 static void read_thread(void *arg) {
59         static vpn_packet_t packet;
60         errno = 0;
61
62         while(true) {
63                 if(read_packet(&packet)) {
64                         mutex_lock(&mutex);
65                         route(myself, &packet);
66                         mutex_unlock(&mutex);
67                 } else {
68                         if(errno == EAGAIN || errno == EINTR) {
69                                 errno = 0;
70                                 continue;
71                         }
72
73                         return;
74                 }
75         }       
76 }
77
78 bool setup_device(void) {
79         struct ifreq ifr;
80         bool t1q = false;
81
82         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
83                 device = xstrdup(DEFAULT_DEVICE);
84
85         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
86 #ifdef HAVE_LINUX_IF_TUN_H
87                 if (netname != NULL)
88                         iface = xstrdup(netname);
89 #else
90                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
91 #endif
92         fd = open(device, O_RDWR);
93
94         if(fd < 0) {
95                 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
96                 return false;
97         }
98
99 #ifdef HAVE_LINUX_IF_TUN_H
100         /* Ok now check if this is an old ethertap or a new tun/tap thingie */
101
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)";
107         } else {
108                 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
109                 device_type = DEVICE_TYPE_TAP;
110                 device_info = "Linux tun/tap device (tap mode)";
111         }
112
113 #ifdef IFF_ONE_QUEUE
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;
117 #endif
118
119         if(iface)
120                 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
121
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);
131         } else
132 #endif
133         {
134                 if(routing_mode == RMODE_ROUTER)
135                         overwrite_mac = true;
136                 device_info = "Linux ethertap device";
137                 device_type = DEVICE_TYPE_ETHERTAP;
138                 if(iface)
139                         free(iface);
140                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
141         }
142
143         logger(LOG_INFO, "%s is a %s", device, device_info);
144
145         return thread_create(&thread, read_thread, NULL);
146 }
147
148 void close_device(void) {
149         close(fd);
150         thread_destroy(&thread);
151
152         free(device);
153         free(iface);
154 }
155
156 bool read_packet(vpn_packet_t *packet) {
157         int inlen;
158         
159         switch(device_type) {
160                 case DEVICE_TYPE_TUN:
161                         inlen = read(fd, packet->data + 10, MTU - 10);
162
163                         if(inlen <= 0) {
164                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
165                                            device_info, device, strerror(errno));
166                                 return false;
167                         }
168
169                         packet->len = inlen + 10;
170                         break;
171                 case DEVICE_TYPE_TAP:
172                         inlen = read(fd, packet->data, MTU);
173
174                         if(inlen <= 0) {
175                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
176                                            device_info, device, strerror(errno));
177                                 return false;
178                         }
179
180                         packet->len = inlen;
181                         break;
182                 case DEVICE_TYPE_ETHERTAP:
183                         inlen = read(fd, packet->data - 2, MTU + 2);
184
185                         if(inlen <= 0) {
186                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
187                                            device_info, device, strerror(errno));
188                                 return false;
189                         }
190
191                         packet->len = inlen - 2;
192                         break;
193         }
194
195         device_total_in += packet->len;
196
197         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
198                            device_info);
199
200         return true;
201 }
202
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);
206
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,
212                                            strerror(errno));
213                                 return false;
214                         }
215                         break;
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,
219                                            strerror(errno));
220                                 return false;
221                         }
222                         break;
223                 case DEVICE_TYPE_ETHERTAP:
224                         *(short int *)(packet->data - 2) = packet->len;
225
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,
228                                            strerror(errno));
229                                 return false;
230                         }
231                         break;
232         }
233
234         device_total_out += packet->len;
235
236         return true;
237 }
238
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);
243 }