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) {
302 iface = xstrdup(ifr.ifr_name);
310 case DEVICE_TYPE_TUNEMU:
311 device_info = "BSD tunemu device";
319 ioctl(device_fd, SIOCGIFADDR, mymac.x);
324 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
329 static void close_device(void) {
330 switch(device_type) {
333 case DEVICE_TYPE_TUNEMU:
334 tunemu_close(device_fd);
351 static bool read_packet(vpn_packet_t *packet) {
354 switch(device_type) {
355 case DEVICE_TYPE_TUN:
357 case DEVICE_TYPE_TUNEMU:
358 if(device_type == DEVICE_TYPE_TUNEMU) {
359 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
362 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
365 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
366 device, strerror(errno));
370 switch(DATA(packet)[14] >> 4) {
372 DATA(packet)[12] = 0x08;
373 DATA(packet)[13] = 0x00;
377 DATA(packet)[12] = 0x86;
378 DATA(packet)[13] = 0xDD;
382 logger(DEBUG_TRAFFIC, LOG_ERR,
383 "Unknown IP version %d while reading packet from %s %s",
384 DATA(packet)[14] >> 4, device_info, device);
388 memset(DATA(packet), 0, 12);
389 packet->len = inlen + 14;
392 case DEVICE_TYPE_UTUN:
393 case DEVICE_TYPE_TUNIFHEAD: {
394 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
395 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
396 device, strerror(errno));
400 switch(DATA(packet)[14] >> 4) {
402 DATA(packet)[12] = 0x08;
403 DATA(packet)[13] = 0x00;
407 DATA(packet)[12] = 0x86;
408 DATA(packet)[13] = 0xDD;
412 logger(DEBUG_TRAFFIC, LOG_ERR,
413 "Unknown IP version %d while reading packet from %s %s",
414 DATA(packet)[14] >> 4, device_info, device);
418 memset(DATA(packet), 0, 12);
419 packet->len = inlen + 10;
423 case DEVICE_TYPE_TAP:
424 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
425 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
426 device, strerror(errno));
437 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
438 packet->len, device_info);
443 static bool write_packet(vpn_packet_t *packet) {
444 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
445 packet->len, device_info);
447 switch(device_type) {
448 case DEVICE_TYPE_TUN:
449 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
450 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
451 device, strerror(errno));
457 case DEVICE_TYPE_UTUN:
458 case DEVICE_TYPE_TUNIFHEAD: {
459 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
464 type = htonl(AF_INET);
468 type = htonl(AF_INET6);
472 logger(DEBUG_TRAFFIC, LOG_ERR,
473 "Unknown address family %x while writing packet to %s %s",
474 af, device_info, device);
478 memcpy(DATA(packet) + 10, &type, sizeof(type));
480 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
481 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
489 case DEVICE_TYPE_TAP:
490 if(write(device_fd, DATA(packet), packet->len) < 0) {
491 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
492 device, strerror(errno));
500 case DEVICE_TYPE_TUNEMU:
501 if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
502 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
503 device, strerror(errno));
517 const devops_t os_devops = {
518 .setup = setup_device,
519 .close = close_device,
521 .write = write_packet,