2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2022 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"
28 #include "../xalloc.h"
34 #ifdef HAVE_NET_IF_UTUN_H
35 #include <sys/sys_domain.h>
36 #include <sys/kern_control.h>
37 #include <net/if_utun.h>
40 #if defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
41 #define DEFAULT_TUN_DEVICE "/dev/tun" // Use the autoclone device
42 #define DEFAULT_TAP_DEVICE "/dev/tap"
44 #define DEFAULT_TUN_DEVICE "/dev/tun0"
45 #define DEFAULT_TAP_DEVICE "/dev/tap0"
48 typedef enum device_type {
50 DEVICE_TYPE_TUNIFHEAD,
61 static const char *device_info = "OS X utun device";
62 #if defined(ENABLE_TUNEMU)
63 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
64 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
65 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
67 static device_type_t device_type = DEVICE_TYPE_TUN;
70 #ifdef HAVE_NET_IF_UTUN_H
71 static bool setup_utun(void) {
72 device_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
75 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
81 memset(&info, 0, sizeof(info));
83 strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
85 if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
86 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
91 char *p = strstr(device, "utun"), *e = NULL;
94 unit = strtol(p + 4, &e, 10);
101 struct sockaddr_ctl sc = {
102 .sc_id = info.ctl_id,
103 .sc_len = sizeof(sc),
104 .sc_family = AF_SYSTEM,
105 .ss_sysaddr = AF_SYS_CONTROL,
109 if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
110 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
115 socklen_t len = sizeof(name);
117 if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
118 iface = xstrdup(device);
120 iface = xstrdup(name);
123 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
129 static bool setup_device(void) {
130 get_config_string(lookup_config(&config_tree, "Device"), &device);
132 // Find out if it's supposed to be a tun or a tap device
136 if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) {
137 if(!strcasecmp(type, "tun"))
141 else if(!strcasecmp(type, "tunemu")) {
142 device_type = DEVICE_TYPE_TUNEMU;
146 #ifdef HAVE_NET_IF_UTUN_H
147 else if(!strcasecmp(type, "utun")) {
148 device_type = DEVICE_TYPE_UTUN;
152 else if(!strcasecmp(type, "tunnohead")) {
153 device_type = DEVICE_TYPE_TUN;
154 } else if(!strcasecmp(type, "tunifhead")) {
155 device_type = DEVICE_TYPE_TUNIFHEAD;
156 } else if(!strcasecmp(type, "tap")) {
157 device_type = DEVICE_TYPE_TAP;
159 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
163 #ifdef HAVE_NET_IF_UTUN_H
165 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)) {
166 device_type = DEVICE_TYPE_UTUN;
169 if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER) {
170 device_type = DEVICE_TYPE_TAP;
174 if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
175 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
179 // Find out which device file to open
182 if(device_type == DEVICE_TYPE_TAP) {
183 device = xstrdup(DEFAULT_TAP_DEVICE);
185 device = xstrdup(DEFAULT_TUN_DEVICE);
191 switch(device_type) {
194 case DEVICE_TYPE_TUNEMU: {
195 char dynamic_name[256] = "";
196 device_fd = tunemu_open(dynamic_name);
200 #ifdef HAVE_NET_IF_UTUN_H
202 case DEVICE_TYPE_UTUN:
207 device_fd = open(device, O_RDWR | O_NONBLOCK);
211 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
216 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
219 // Guess what the corresponding interface is called
221 char *realname = NULL;
223 #if defined(HAVE_FDEVNAME)
224 realname = fdevname(device_fd);
225 #elif defined(HAVE_DEVNAME)
228 if(!fstat(device_fd, &buf)) {
229 realname = devname(buf.st_rdev, S_IFCHR);
238 if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) {
239 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
240 } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) {
241 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
244 // Configure the device as best as we can
246 switch(device_type) {
248 device_type = DEVICE_TYPE_TUN;
250 case DEVICE_TYPE_TUN:
255 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof(zero)) == -1) {
256 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
262 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
264 const int mode = IFF_BROADCAST | IFF_MULTICAST;
265 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
269 device_info = "Generic BSD tun device";
272 case DEVICE_TYPE_TUNIFHEAD:
277 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof(one)) == -1) {
278 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
284 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
286 const int mode = IFF_BROADCAST | IFF_MULTICAST;
287 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
291 device_info = "Generic BSD tun device";
294 case DEVICE_TYPE_TAP:
295 if(routing_mode == RMODE_ROUTER) {
296 overwrite_mac = true;
299 device_info = "Generic BSD tap device";
304 if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) {
306 iface = xstrdup(ifr.ifr_name);
314 case DEVICE_TYPE_TUNEMU:
315 device_info = "BSD tunemu device";
323 ioctl(device_fd, SIOCGIFADDR, mymac.x);
328 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
333 static void close_device(void) {
334 switch(device_type) {
337 case DEVICE_TYPE_TUNEMU:
338 tunemu_close(device_fd);
355 static bool read_packet(vpn_packet_t *packet) {
358 switch(device_type) {
359 case DEVICE_TYPE_TUN:
361 case DEVICE_TYPE_TUNEMU:
362 if(device_type == DEVICE_TYPE_TUNEMU) {
363 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
366 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
369 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
370 device, strerror(errno));
374 switch(DATA(packet)[14] >> 4) {
376 DATA(packet)[12] = 0x08;
377 DATA(packet)[13] = 0x00;
381 DATA(packet)[12] = 0x86;
382 DATA(packet)[13] = 0xDD;
386 logger(DEBUG_TRAFFIC, LOG_ERR,
387 "Unknown IP version %d while reading packet from %s %s",
388 DATA(packet)[14] >> 4, device_info, device);
392 memset(DATA(packet), 0, 12);
393 packet->len = inlen + 14;
396 case DEVICE_TYPE_UTUN:
397 case DEVICE_TYPE_TUNIFHEAD: {
398 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
399 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
400 device, strerror(errno));
404 switch(DATA(packet)[14] >> 4) {
406 DATA(packet)[12] = 0x08;
407 DATA(packet)[13] = 0x00;
411 DATA(packet)[12] = 0x86;
412 DATA(packet)[13] = 0xDD;
416 logger(DEBUG_TRAFFIC, LOG_ERR,
417 "Unknown IP version %d while reading packet from %s %s",
418 DATA(packet)[14] >> 4, device_info, device);
422 memset(DATA(packet), 0, 12);
423 packet->len = inlen + 10;
427 case DEVICE_TYPE_TAP:
428 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
429 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
430 device, strerror(errno));
441 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
442 packet->len, device_info);
447 static bool write_packet(vpn_packet_t *packet) {
448 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
449 packet->len, device_info);
451 switch(device_type) {
452 case DEVICE_TYPE_TUN:
453 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
454 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
455 device, strerror(errno));
461 case DEVICE_TYPE_UTUN:
462 case DEVICE_TYPE_TUNIFHEAD: {
463 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
468 type = htonl(AF_INET);
472 type = htonl(AF_INET6);
476 logger(DEBUG_TRAFFIC, LOG_ERR,
477 "Unknown address family %x while writing packet to %s %s",
478 af, device_info, device);
482 memcpy(DATA(packet) + 10, &type, sizeof(type));
484 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
485 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
493 case DEVICE_TYPE_TAP:
494 if(write(device_fd, DATA(packet), packet->len) < 0) {
495 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
496 device, strerror(errno));
504 case DEVICE_TYPE_TUNEMU:
505 if(tunemu_write(DATA(packet) + 14, packet->len - 14) < 0) {
506 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
507 device, strerror(errno));
521 const devops_t os_devops = {
522 .setup = setup_device,
523 .close = close_device,
525 .write = write_packet,