2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2021 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"
31 #include "bsd/tunemu.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));
79 struct ctl_info info = {};
81 strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
83 if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
84 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
89 char *p = strstr(device, "utun"), *e = NULL;
92 unit = strtol(p + 4, &e, 10);
99 struct sockaddr_ctl sc = {
100 .sc_id = info.ctl_id,
101 .sc_len = sizeof(sc),
102 .sc_family = AF_SYSTEM,
103 .ss_sysaddr = AF_SYS_CONTROL,
107 if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
108 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
113 socklen_t len = sizeof(name);
115 if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
116 iface = xstrdup(device);
118 iface = xstrdup(name);
121 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
127 static bool setup_device(void) {
128 get_config_string(lookup_config(&config_tree, "Device"), &device);
130 // Find out if it's supposed to be a tun or a tap device
134 if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) {
135 if(!strcasecmp(type, "tun"))
139 else if(!strcasecmp(type, "tunemu")) {
140 device_type = DEVICE_TYPE_TUNEMU;
144 #ifdef HAVE_NET_IF_UTUN_H
145 else if(!strcasecmp(type, "utun")) {
146 device_type = DEVICE_TYPE_UTUN;
150 else if(!strcasecmp(type, "tunnohead")) {
151 device_type = DEVICE_TYPE_TUN;
152 } else if(!strcasecmp(type, "tunifhead")) {
153 device_type = DEVICE_TYPE_TUNIFHEAD;
154 } else if(!strcasecmp(type, "tap")) {
155 device_type = DEVICE_TYPE_TAP;
157 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
161 #ifdef HAVE_NET_IF_UTUN_H
163 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)) {
164 device_type = DEVICE_TYPE_UTUN;
167 if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER) {
168 device_type = DEVICE_TYPE_TAP;
172 if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
173 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
177 // Find out which device file to open
180 if(device_type == DEVICE_TYPE_TAP) {
181 device = xstrdup(DEFAULT_TAP_DEVICE);
183 device = xstrdup(DEFAULT_TUN_DEVICE);
189 switch(device_type) {
192 case DEVICE_TYPE_TUNEMU: {
193 char dynamic_name[256] = "";
194 device_fd = tunemu_open(dynamic_name);
198 #ifdef HAVE_NET_IF_UTUN_H
200 case DEVICE_TYPE_UTUN:
205 device_fd = open(device, O_RDWR | O_NONBLOCK);
209 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
214 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
217 // Guess what the corresponding interface is called
219 char *realname = NULL;
221 #if defined(HAVE_FDEVNAME)
222 realname = fdevname(device_fd);
223 #elif defined(HAVE_DEVNAME)
226 if(!fstat(device_fd, &buf)) {
227 realname = devname(buf.st_rdev, S_IFCHR);
236 if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) {
237 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
238 } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) {
239 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
242 // Configure the device as best as we can
244 switch(device_type) {
246 device_type = DEVICE_TYPE_TUN;
248 case DEVICE_TYPE_TUN:
253 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof(zero)) == -1) {
254 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
260 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
262 const int mode = IFF_BROADCAST | IFF_MULTICAST;
263 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
267 device_info = "Generic BSD tun device";
270 case DEVICE_TYPE_TUNIFHEAD:
275 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof(one)) == -1) {
276 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
282 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
284 const int mode = IFF_BROADCAST | IFF_MULTICAST;
285 ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
289 device_info = "Generic BSD tun device";
292 case DEVICE_TYPE_TAP:
293 if(routing_mode == RMODE_ROUTER) {
294 overwrite_mac = true;
297 device_info = "Generic BSD tap device";
302 if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) {
304 iface = xstrdup(ifr.ifr_name);
312 case DEVICE_TYPE_TUNEMU:
313 device_info = "BSD tunemu device";
321 ioctl(device_fd, SIOCGIFADDR, mymac.x);
326 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
331 static void close_device(void) {
332 switch(device_type) {
335 case DEVICE_TYPE_TUNEMU:
336 tunemu_close(device_fd);
353 static bool read_packet(vpn_packet_t *packet) {
356 switch(device_type) {
357 case DEVICE_TYPE_TUN:
359 case DEVICE_TYPE_TUNEMU:
360 if(device_type == DEVICE_TYPE_TUNEMU) {
361 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
364 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
367 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
368 device, strerror(errno));
372 switch(DATA(packet)[14] >> 4) {
374 DATA(packet)[12] = 0x08;
375 DATA(packet)[13] = 0x00;
379 DATA(packet)[12] = 0x86;
380 DATA(packet)[13] = 0xDD;
384 logger(DEBUG_TRAFFIC, LOG_ERR,
385 "Unknown IP version %d while reading packet from %s %s",
386 DATA(packet)[14] >> 4, device_info, device);
390 memset(DATA(packet), 0, 12);
391 packet->len = inlen + 14;
394 case DEVICE_TYPE_UTUN:
395 case DEVICE_TYPE_TUNIFHEAD: {
396 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
397 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
398 device, strerror(errno));
402 switch(DATA(packet)[14] >> 4) {
404 DATA(packet)[12] = 0x08;
405 DATA(packet)[13] = 0x00;
409 DATA(packet)[12] = 0x86;
410 DATA(packet)[13] = 0xDD;
414 logger(DEBUG_TRAFFIC, LOG_ERR,
415 "Unknown IP version %d while reading packet from %s %s",
416 DATA(packet)[14] >> 4, device_info, device);
420 memset(DATA(packet), 0, 12);
421 packet->len = inlen + 10;
425 case DEVICE_TYPE_TAP:
426 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
427 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
428 device, strerror(errno));
439 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
440 packet->len, device_info);
445 static bool write_packet(vpn_packet_t *packet) {
446 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
447 packet->len, device_info);
449 switch(device_type) {
450 case DEVICE_TYPE_TUN:
451 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
452 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
453 device, strerror(errno));
459 case DEVICE_TYPE_UTUN:
460 case DEVICE_TYPE_TUNIFHEAD: {
461 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
466 type = htonl(AF_INET);
470 type = htonl(AF_INET6);
474 logger(DEBUG_TRAFFIC, LOG_ERR,
475 "Unknown address family %x while writing packet to %s %s",
476 af, device_info, device);
480 memcpy(DATA(packet) + 10, &type, sizeof(type));
482 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
483 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
491 case DEVICE_TYPE_TAP:
492 if(write(device_fd, DATA(packet), packet->len) < 0) {
493 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
494 device, strerror(errno));
502 case DEVICE_TYPE_TUNEMU:
503 if(tunemu_write(DATA(packet) + 14, packet->len - 14) < 0) {
504 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
505 device, strerror(errno));
519 const devops_t os_devops = {
520 .setup = setup_device,
521 .close = close_device,
523 .write = write_packet,