2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2016 Guus Sliepen <guus@tinc-vpn.org>
5 2009 Grzegorz Dymarek <gregd72002@googlemail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "../system.h"
25 #include "../device.h"
26 #include "../logger.h"
30 #include "../xalloc.h"
36 #define DEFAULT_TUN_DEVICE "/dev/tun0"
37 #define DEFAULT_TAP_DEVICE "/dev/tap0"
39 typedef enum device_type {
41 DEVICE_TYPE_TUNIFHEAD,
51 static char *device_info = NULL;
52 static uint64_t device_total_in = 0;
53 static uint64_t device_total_out = 0;
54 #if defined(ENABLE_TUNEMU)
55 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
56 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
57 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
59 static device_type_t device_type = DEVICE_TYPE_TUN;
62 static bool setup_device(void) {
63 // Find out which device file to open
65 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
66 if(routing_mode == RMODE_ROUTER)
67 device = xstrdup(DEFAULT_TUN_DEVICE);
69 device = xstrdup(DEFAULT_TAP_DEVICE);
72 // Find out if it's supposed to be a tun or a tap device
76 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
77 if(!strcasecmp(type, "tun"))
80 else if(!strcasecmp(type, "tunemu"))
81 device_type = DEVICE_TYPE_TUNEMU;
83 else if(!strcasecmp(type, "tunnohead"))
84 device_type = DEVICE_TYPE_TUN;
85 else if(!strcasecmp(type, "tunifhead"))
86 device_type = DEVICE_TYPE_TUNIFHEAD;
87 else if(!strcasecmp(type, "tap"))
88 device_type = DEVICE_TYPE_TAP;
90 logger(LOG_ERR, "Unknown device type %s!", type);
94 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
95 device_type = DEVICE_TYPE_TAP;
100 switch(device_type) {
102 case DEVICE_TYPE_TUNEMU: {
103 char dynamic_name[256] = "";
104 device_fd = tunemu_open(dynamic_name);
109 device_fd = open(device, O_RDWR | O_NONBLOCK);
113 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
118 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
121 // Guess what the corresponding interface is called
125 #if defined(HAVE_FDEVNAME)
126 realname = fdevname(device_fd) ? : device;
127 #elif defined(HAVE_DEVNAME)
129 if(!fstat(device_fd, &buf))
130 realname = devname(buf.st_rdev, S_IFCHR) ? : device;
135 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
136 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
137 else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname))
138 logger(LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
140 // Configure the device as best as we can
142 switch(device_type) {
144 device_type = DEVICE_TYPE_TUN;
145 case DEVICE_TYPE_TUN:
149 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
150 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
155 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
157 const int mode = IFF_BROADCAST | IFF_MULTICAST;
158 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
162 device_info = "Generic BSD tun device";
164 case DEVICE_TYPE_TUNIFHEAD:
168 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
169 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
174 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
176 const int mode = IFF_BROADCAST | IFF_MULTICAST;
177 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
181 device_info = "Generic BSD tun device";
183 case DEVICE_TYPE_TAP:
184 if(routing_mode == RMODE_ROUTER)
185 overwrite_mac = true;
186 device_info = "Generic BSD tap device";
190 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
193 iface = xstrdup(ifr.ifr_name);
200 case DEVICE_TYPE_TUNEMU:
201 device_info = "BSD tunemu device";
208 ioctl(device_fd, SIOCGIFADDR, mymac.x);
211 logger(LOG_INFO, "%s is a %s", device, device_info);
216 static void close_device(void) {
217 switch(device_type) {
219 case DEVICE_TYPE_TUNEMU:
220 tunemu_close(device_fd);
231 static bool read_packet(vpn_packet_t *packet) {
234 switch(device_type) {
235 case DEVICE_TYPE_TUN:
237 case DEVICE_TYPE_TUNEMU:
238 if(device_type == DEVICE_TYPE_TUNEMU)
239 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
242 lenin = read(device_fd, packet->data + 14, MTU - 14);
245 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
246 device, strerror(errno));
250 switch(packet->data[14] >> 4) {
252 packet->data[12] = 0x08;
253 packet->data[13] = 0x00;
256 packet->data[12] = 0x86;
257 packet->data[13] = 0xDD;
260 ifdebug(TRAFFIC) logger(LOG_ERR,
261 "Unknown IP version %d while reading packet from %s %s",
262 packet->data[14] >> 4, device_info, device);
266 memset(packet->data, 0, 12);
267 packet->len = lenin + 14;
270 case DEVICE_TYPE_TUNIFHEAD: {
272 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
274 if((lenin = readv(device_fd, vector, 2)) <= 0) {
275 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
276 device, strerror(errno));
280 switch (ntohl(type)) {
282 packet->data[12] = 0x08;
283 packet->data[13] = 0x00;
287 packet->data[12] = 0x86;
288 packet->data[13] = 0xDD;
292 ifdebug(TRAFFIC) logger(LOG_ERR,
293 "Unknown address family %x while reading packet from %s %s",
294 ntohl(type), device_info, device);
298 memset(packet->data, 0, 12);
299 packet->len = lenin + 10;
303 case DEVICE_TYPE_TAP:
304 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
305 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
306 device, strerror(errno));
317 device_total_in += packet->len;
319 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
320 packet->len, device_info);
325 static bool write_packet(vpn_packet_t *packet) {
326 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
327 packet->len, device_info);
329 switch(device_type) {
330 case DEVICE_TYPE_TUN:
331 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
332 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
333 device, strerror(errno));
338 case DEVICE_TYPE_TUNIFHEAD: {
340 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
343 af = (packet->data[12] << 8) + packet->data[13];
347 type = htonl(AF_INET);
350 type = htonl(AF_INET6);
353 ifdebug(TRAFFIC) logger(LOG_ERR,
354 "Unknown address family %x while writing packet to %s %s",
355 af, device_info, device);
359 if(writev(device_fd, vector, 2) < 0) {
360 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
367 case DEVICE_TYPE_TAP:
368 if(write(device_fd, packet->data, packet->len) < 0) {
369 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
370 device, strerror(errno));
376 case DEVICE_TYPE_TUNEMU:
377 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
378 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
379 device, strerror(errno));
389 device_total_out += packet->len;
394 static void dump_device_stats(void) {
395 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
396 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
397 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
400 const devops_t os_devops = {
401 .setup = setup_device,
402 .close = close_device,
404 .write = write_packet,
405 .dump_stats = dump_device_stats,