2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2017 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"
31 #include "../xalloc.h"
34 #include "bsd/tunemu.h"
37 #ifdef HAVE_NET_IF_UTUN_H
38 #include <sys/sys_domain.h>
39 #include <sys/kern_control.h>
40 #include <net/if_utun.h>
43 #define DEFAULT_TUN_DEVICE "/dev/tun0"
44 #define DEFAULT_TAP_DEVICE "/dev/tap0"
46 typedef enum device_type {
48 DEVICE_TYPE_TUNIFHEAD,
59 static const char *device_info = "OS X utun device";
60 #if defined(ENABLE_TUNEMU)
61 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
62 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
63 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
65 static device_type_t device_type = DEVICE_TYPE_TUN;
68 #ifdef HAVE_NET_IF_UTUN_H
69 static bool setup_utun(void) {
70 device_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
73 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
77 struct ctl_info info = {};
79 strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
81 if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
82 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
87 char *p = strstr(device, "utun"), *e = NULL;
90 unit = strtol(p + 4, &e, 10);
97 struct sockaddr_ctl sc = {
100 .sc_family = AF_SYSTEM,
101 .ss_sysaddr = AF_SYS_CONTROL,
105 if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
106 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
111 socklen_t len = sizeof(name);
113 if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
114 iface = xstrdup(device);
116 iface = xstrdup(name);
119 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
125 static bool setup_device(void) {
126 get_config_string(lookup_config(config_tree, "Device"), &device);
128 // Find out if it's supposed to be a tun or a tap device
132 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
133 if(!strcasecmp(type, "tun"))
137 else if(!strcasecmp(type, "tunemu")) {
138 device_type = DEVICE_TYPE_TUNEMU;
142 #ifdef HAVE_NET_IF_UTUN_H
143 else if(!strcasecmp(type, "utun")) {
144 device_type = DEVICE_TYPE_UTUN;
148 else if(!strcasecmp(type, "tunnohead")) {
149 device_type = DEVICE_TYPE_TUN;
150 } else if(!strcasecmp(type, "tunifhead")) {
151 device_type = DEVICE_TYPE_TUNIFHEAD;
152 } else if(!strcasecmp(type, "tap")) {
153 device_type = DEVICE_TYPE_TAP;
155 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
159 #ifdef HAVE_NET_IF_UTUN_H
161 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)) {
162 device_type = DEVICE_TYPE_UTUN;
165 if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER) {
166 device_type = DEVICE_TYPE_TAP;
170 if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
171 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
175 // Find out which device file to open
178 if(device_type == DEVICE_TYPE_TAP) {
179 device = xstrdup(DEFAULT_TAP_DEVICE);
181 device = xstrdup(DEFAULT_TUN_DEVICE);
187 switch(device_type) {
190 case DEVICE_TYPE_TUNEMU: {
191 char dynamic_name[256] = "";
192 device_fd = tunemu_open(dynamic_name);
196 #ifdef HAVE_NET_IF_UTUN_H
198 case DEVICE_TYPE_UTUN:
203 device_fd = open(device, O_RDWR | O_NONBLOCK);
207 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
212 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
215 // Guess what the corresponding interface is called
217 char *realname = NULL;
219 #if defined(HAVE_FDEVNAME)
220 realname = fdevname(device_fd);
221 #elif defined(HAVE_DEVNAME)
224 if(!fstat(device_fd, &buf)) {
225 realname = devname(buf.st_rdev, S_IFCHR);
234 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) {
235 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
236 } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) {
237 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
240 // Configure the device as best as we can
242 switch(device_type) {
244 device_type = DEVICE_TYPE_TUN;
246 case DEVICE_TYPE_TUN:
251 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof(zero)) == -1) {
252 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
258 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
260 const int mode = IFF_BROADCAST | IFF_MULTICAST;
261 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
265 device_info = "Generic BSD tun device";
268 case DEVICE_TYPE_TUNIFHEAD:
273 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof(one)) == -1) {
274 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
280 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
282 const int mode = IFF_BROADCAST | IFF_MULTICAST;
283 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
287 device_info = "Generic BSD tun device";
290 case DEVICE_TYPE_TAP:
291 if(routing_mode == RMODE_ROUTER) {
292 overwrite_mac = true;
295 device_info = "Generic BSD tap device";
300 if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) {
305 iface = xstrdup(ifr.ifr_name);
313 case DEVICE_TYPE_TUNEMU:
314 device_info = "BSD tunemu device";
322 ioctl(device_fd, SIOCGIFADDR, mymac.x);
327 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
332 static void close_device(void) {
333 switch(device_type) {
336 case DEVICE_TYPE_TUNEMU:
337 tunemu_close(device_fd);
354 static bool read_packet(vpn_packet_t *packet) {
357 switch(device_type) {
358 case DEVICE_TYPE_TUN:
360 case DEVICE_TYPE_TUNEMU:
361 if(device_type == DEVICE_TYPE_TUNEMU) {
362 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
365 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
368 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
369 device, strerror(errno));
373 switch(DATA(packet)[14] >> 4) {
375 DATA(packet)[12] = 0x08;
376 DATA(packet)[13] = 0x00;
380 DATA(packet)[12] = 0x86;
381 DATA(packet)[13] = 0xDD;
385 logger(DEBUG_TRAFFIC, LOG_ERR,
386 "Unknown IP version %d while reading packet from %s %s",
387 DATA(packet)[14] >> 4, device_info, device);
391 memset(DATA(packet), 0, 12);
392 packet->len = inlen + 14;
395 case DEVICE_TYPE_UTUN:
396 case DEVICE_TYPE_TUNIFHEAD: {
397 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
398 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
399 device, strerror(errno));
403 switch(DATA(packet)[14] >> 4) {
405 DATA(packet)[12] = 0x08;
406 DATA(packet)[13] = 0x00;
410 DATA(packet)[12] = 0x86;
411 DATA(packet)[13] = 0xDD;
415 logger(DEBUG_TRAFFIC, LOG_ERR,
416 "Unknown IP version %d while reading packet from %s %s",
417 DATA(packet)[14] >> 4, device_info, device);
421 memset(DATA(packet), 0, 12);
422 packet->len = inlen + 10;
426 case DEVICE_TYPE_TAP:
427 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
428 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
429 device, strerror(errno));
440 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
441 packet->len, device_info);
446 static bool write_packet(vpn_packet_t *packet) {
447 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
448 packet->len, device_info);
450 switch(device_type) {
451 case DEVICE_TYPE_TUN:
452 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
453 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
454 device, strerror(errno));
460 case DEVICE_TYPE_UTUN:
461 case DEVICE_TYPE_TUNIFHEAD: {
462 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
467 type = htonl(AF_INET);
471 type = htonl(AF_INET6);
475 logger(DEBUG_TRAFFIC, LOG_ERR,
476 "Unknown address family %x while writing packet to %s %s",
477 af, device_info, device);
481 memcpy(DATA(packet) + 10, &type, sizeof(type));
483 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
484 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
492 case DEVICE_TYPE_TAP:
493 if(write(device_fd, DATA(packet), packet->len) < 0) {
494 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
495 device, strerror(errno));
503 case DEVICE_TYPE_TUNEMU:
504 if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
505 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
506 device, strerror(errno));
520 const devops_t os_devops = {
521 .setup = setup_device,
522 .close = close_device,
524 .write = write_packet,