1 /*
2     device.c -- Interaction with Linux ethertap and tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2014 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 "../utils.h"
36 #include "../xalloc.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 = NULL;
47 char *iface = NULL;
48 static char *type = NULL;
49 static char ifrname[IFNAMSIZ];
50 static const char *device_info;
51 
52 static uint64_t device_total_in = 0;
53 static uint64_t device_total_out = 0;
54 
setup_device(void)55 static bool setup_device(void) {
56 	struct ifreq ifr;
57 	bool t1q = false;
58 
59 	if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
60 		device = xstrdup(DEFAULT_DEVICE);
61 	}
62 
63 	if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
64 #ifdef HAVE_LINUX_IF_TUN_H
65 		if(netname != NULL) {
66 			iface = xstrdup(netname);
67 		}
68 
69 #else
70 		iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
71 #endif
72 	device_fd = open(device, O_RDWR | O_NONBLOCK);
73 
74 	if(device_fd < 0) {
75 		logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
76 		return false;
77 	}
78 
79 #ifdef FD_CLOEXEC
80 	fcntl(device_fd, F_SETFD, FD_CLOEXEC);
81 #endif
82 
83 #ifdef HAVE_LINUX_IF_TUN_H
84 	/* Ok now check if this is an old ethertap or a new tun/tap thingie */
85 
86 	memset(&ifr, 0, sizeof(ifr));
87 
88 	get_config_string(lookup_config(config_tree, "DeviceType"), &type);
89 
90 	if(type && strcasecmp(type, "tun") && strcasecmp(type, "tap")) {
91 		logger(LOG_ERR, "Unknown device type %s!", type);
92 		return false;
93 	}
94 
95 	if((type && !strcasecmp(type, "tun")) || (!type && routing_mode == RMODE_ROUTER)) {
96 		ifr.ifr_flags = IFF_TUN;
97 		device_type = DEVICE_TYPE_TUN;
98 		device_info = "Linux tun/tap device (tun mode)";
99 	} else {
100 		if(routing_mode == RMODE_ROUTER) {
101 			overwrite_mac = true;
102 		}
103 
104 		ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
105 		device_type = DEVICE_TYPE_TAP;
106 		device_info = "Linux tun/tap device (tap mode)";
107 	}
108 
109 #ifdef IFF_ONE_QUEUE
110 
111 	/* Set IFF_ONE_QUEUE flag... */
112 	if(get_config_bool(lookup_config(config_tree, "IffOneQueue"), &t1q) && t1q) {
113 		ifr.ifr_flags |= IFF_ONE_QUEUE;
114 	}
115 
116 #endif
117 
118 	if(iface) {
119 		strncpy(ifr.ifr_name, iface, IFNAMSIZ);
120 		ifr.ifr_name[IFNAMSIZ - 1] = 0;
121 	}
122 
123 	if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
124 		strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
125 		ifrname[IFNAMSIZ - 1] = 0;
126 		free(iface);
127 		iface = xstrdup(ifrname);
128 	} else if(errno == EPERM || errno == EBUSY) {
129 		logger(LOG_ERR, "Error while trying to configure %s: %s", device, strerror(errno));
130 		return false;
131 	} else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
132 		logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
133 		strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
134 		ifrname[IFNAMSIZ - 1] = 0;
135 		free(iface);
136 		iface = xstrdup(ifrname);
137 	} else
138 #endif
139 	{
140 		if(routing_mode == RMODE_ROUTER) {
141 			overwrite_mac = true;
142 		}
143 
144 		device_info = "Linux ethertap device";
145 		device_type = DEVICE_TYPE_ETHERTAP;
146 		free(iface);
147 		iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
148 	}
149 
150 	if(overwrite_mac && !ioctl(device_fd, SIOCGIFHWADDR, &ifr)) {
151 		memcpy(mymac.x, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
152 	}
153 
154 	logger(LOG_INFO, "%s is a %s", device, device_info);
155 
156 	return true;
157 }
158 
close_device(void)159 static void close_device(void) {
160 	close(device_fd);
161 
162 	free(type);
163 	free(device);
164 	free(iface);
165 }
166 
read_packet(vpn_packet_t * packet)167 static bool read_packet(vpn_packet_t *packet) {
168 	int lenin;
169 
170 	switch(device_type) {
171 	case DEVICE_TYPE_TUN:
172 		lenin = read(device_fd, packet->data + 10, MTU - 10);
173 
174 		if(lenin <= 0) {
175 			logger(LOG_ERR, "Error while reading from %s %s: %s",
176 			       device_info, device, strerror(errno));
177 			return false;
178 		}
179 
180 		memset(packet->data, 0, 12);
181 		packet->len = lenin + 10;
182 		break;
183 
184 	case DEVICE_TYPE_TAP:
185 		lenin = read(device_fd, packet->data, MTU);
186 
187 		if(lenin <= 0) {
188 			logger(LOG_ERR, "Error while reading from %s %s: %s",
189 			       device_info, device, strerror(errno));
190 			return false;
191 		}
192 
193 		packet->len = lenin;
194 		break;
195 
196 	case DEVICE_TYPE_ETHERTAP:
197 		lenin = read(device_fd, packet->data - 2, MTU + 2);
198 
199 		if(lenin <= 0) {
200 			logger(LOG_ERR, "Error while reading from %s %s: %s",
201 			       device_info, device, strerror(errno));
202 			return false;
203 		}
204 
205 		packet->len = lenin - 2;
206 		break;
207 	}
208 
209 	device_total_in += packet->len;
210 
211 	ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
212 	                        device_info);
213 
214 	return true;
215 }
216 
write_packet(vpn_packet_t * packet)217 static bool write_packet(vpn_packet_t *packet) {
218 	ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
219 	                        packet->len, device_info);
220 
221 	switch(device_type) {
222 	case DEVICE_TYPE_TUN:
223 		packet->data[10] = packet->data[11] = 0;
224 
225 		if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
226 			logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
227 			       strerror(errno));
228 			return false;
229 		}
230 
231 		break;
232 
233 	case DEVICE_TYPE_TAP:
234 		if(write(device_fd, packet->data, packet->len) < 0) {
235 			logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
236 			       strerror(errno));
237 			return false;
238 		}
239 
240 		break;
241 
242 	case DEVICE_TYPE_ETHERTAP:
243 		memcpy(packet->data - 2, &packet->len, 2);
244 
245 		if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
246 			logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
247 			       strerror(errno));
248 			return false;
249 		}
250 
251 		break;
252 	}
253 
254 	device_total_out += packet->len;
255 
256 	return true;
257 }
258 
dump_device_stats(void)259 static void dump_device_stats(void) {
260 	logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
261 	logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
262 	logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
263 }
264 
265 const devops_t os_devops = {
266 	.setup = setup_device,
267 	.close = close_device,
268 	.read = read_packet,
269 	.write = write_packet,
270 	.dump_stats = dump_device_stats,
271 };
272