Use strrchr() insteaad of rindex().
[oweals/tinc.git] / 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 int device_total_in = 0;
51 static int device_total_out = 0;
52
53 bool setup_device(void) {
54         struct ifreq ifr;
55
56         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
57                 device = xstrdup(DEFAULT_DEVICE);
58
59         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
60 #ifdef HAVE_LINUX_IF_TUN_H
61                 if (netname != NULL)
62                         iface = xstrdup(netname);
63 #else
64                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
65 #endif
66         device_fd = open(device, O_RDWR | O_NONBLOCK);
67
68         if(device_fd < 0) {
69                 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
70                 return false;
71         }
72
73 #ifdef HAVE_LINUX_IF_TUN_H
74         /* Ok now check if this is an old ethertap or a new tun/tap thingie */
75
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)";
81         } else {
82                 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
83                 device_type = DEVICE_TYPE_TAP;
84                 device_info = "Linux tun/tap device (tap mode)";
85         }
86
87         if(iface)
88                 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
89
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);
99         } else
100 #endif
101         {
102                 if(routing_mode == RMODE_ROUTER)
103                         overwrite_mac = true;
104                 device_info = "Linux ethertap device";
105                 device_type = DEVICE_TYPE_ETHERTAP;
106                 if(iface)
107                         free(iface);
108                 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
109         }
110
111         logger(LOG_INFO, "%s is a %s", device, device_info);
112
113         return true;
114 }
115
116 void close_device(void) {
117         close(device_fd);
118
119         free(device);
120         free(iface);
121 }
122
123 bool read_packet(vpn_packet_t *packet) {
124         int lenin;
125         
126         switch(device_type) {
127                 case DEVICE_TYPE_TUN:
128                         lenin = read(device_fd, packet->data + 10, MTU - 10);
129
130                         if(lenin <= 0) {
131                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
132                                            device_info, device, strerror(errno));
133                                 return false;
134                         }
135
136                         packet->len = lenin + 10;
137                         break;
138                 case DEVICE_TYPE_TAP:
139                         lenin = read(device_fd, packet->data, MTU);
140
141                         if(lenin <= 0) {
142                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
143                                            device_info, device, strerror(errno));
144                                 return false;
145                         }
146
147                         packet->len = lenin;
148                         break;
149                 case DEVICE_TYPE_ETHERTAP:
150                         lenin = read(device_fd, packet->data - 2, MTU + 2);
151
152                         if(lenin <= 0) {
153                                 logger(LOG_ERR, "Error while reading from %s %s: %s",
154                                            device_info, device, strerror(errno));
155                                 return false;
156                         }
157
158                         packet->len = lenin - 2;
159                         break;
160         }
161
162         device_total_in += packet->len;
163
164         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
165                            device_info);
166
167         return true;
168 }
169
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);
173
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,
179                                            strerror(errno));
180                                 return false;
181                         }
182                         break;
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,
186                                            strerror(errno));
187                                 return false;
188                         }
189                         break;
190                 case DEVICE_TYPE_ETHERTAP:
191                         *(short int *)(packet->data - 2) = packet->len;
192
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,
195                                            strerror(errno));
196                                 return false;
197                         }
198                         break;
199         }
200
201         device_total_out += packet->len;
202
203         return true;
204 }
205
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);
210 }