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