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 char *device_info = NULL;
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);
72 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
76 struct ctl_info info = {};
77 strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof info.ctl_name);
79 if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
80 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
85 char *p = strstr(device, "utun"), *e = NULL;
87 unit = strtol(p + 4, &e, 10);
92 struct sockaddr_ctl sc = {
95 .sc_family = AF_SYSTEM,
96 .ss_sysaddr = AF_SYS_CONTROL,
100 if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
101 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
106 socklen_t len = sizeof name;
107 if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
108 iface = xstrdup(device);
110 iface = xstrdup(name);
113 device_info = "OS X utun device";
115 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
121 static bool setup_device(void) {
122 get_config_string(lookup_config(config_tree, "Device"), &device);
124 // Find out if it's supposed to be a tun or a tap device
127 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
128 if(!strcasecmp(type, "tun"))
131 else if(!strcasecmp(type, "tunemu"))
132 device_type = DEVICE_TYPE_TUNEMU;
134 #ifdef HAVE_NET_IF_UTUN_H
135 else if(!strcasecmp(type, "utun"))
136 device_type = DEVICE_TYPE_UTUN;
138 else if(!strcasecmp(type, "tunnohead"))
139 device_type = DEVICE_TYPE_TUN;
140 else if(!strcasecmp(type, "tunifhead"))
141 device_type = DEVICE_TYPE_TUNIFHEAD;
142 else if(!strcasecmp(type, "tap"))
143 device_type = DEVICE_TYPE_TAP;
145 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
149 #ifdef HAVE_NET_IF_UTUN_H
150 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0))
151 device_type = DEVICE_TYPE_UTUN;
154 if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER)
155 device_type = DEVICE_TYPE_TAP;
158 if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
159 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
163 // Find out which device file to open
166 if(device_type == DEVICE_TYPE_TAP)
167 device = xstrdup(DEFAULT_TAP_DEVICE);
169 device = xstrdup(DEFAULT_TUN_DEVICE);
174 switch(device_type) {
176 case DEVICE_TYPE_TUNEMU: {
177 char dynamic_name[256] = "";
178 device_fd = tunemu_open(dynamic_name);
182 #ifdef HAVE_NET_IF_UTUN_H
183 case DEVICE_TYPE_UTUN:
187 device_fd = open(device, O_RDWR | O_NONBLOCK);
191 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
196 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
199 // Guess what the corresponding interface is called
201 char *realname = NULL;
203 #if defined(HAVE_FDEVNAME)
204 realname = fdevname(device_fd);
205 #elif defined(HAVE_DEVNAME)
207 if(!fstat(device_fd, &buf))
208 realname = devname(buf.st_rdev, S_IFCHR);
214 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
215 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
216 else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname))
217 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
219 // Configure the device as best as we can
221 switch(device_type) {
223 device_type = DEVICE_TYPE_TUN;
224 case DEVICE_TYPE_TUN:
228 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
229 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
234 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
236 const int mode = IFF_BROADCAST | IFF_MULTICAST;
237 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
241 device_info = "Generic BSD tun device";
243 case DEVICE_TYPE_TUNIFHEAD:
247 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
248 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
253 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
255 const int mode = IFF_BROADCAST | IFF_MULTICAST;
256 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
260 device_info = "Generic BSD tun device";
262 case DEVICE_TYPE_TAP:
263 if(routing_mode == RMODE_ROUTER)
264 overwrite_mac = true;
265 device_info = "Generic BSD tap device";
269 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
272 iface = xstrdup(ifr.ifr_name);
279 case DEVICE_TYPE_TUNEMU:
280 device_info = "BSD tunemu device";
287 ioctl(device_fd, SIOCGIFADDR, mymac.x);
290 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
295 static void close_device(void) {
296 switch(device_type) {
298 case DEVICE_TYPE_TUNEMU:
299 tunemu_close(device_fd);
307 free(device); device = NULL;
308 free(iface); iface = NULL;
312 static bool read_packet(vpn_packet_t *packet) {
315 switch(device_type) {
316 case DEVICE_TYPE_TUN:
318 case DEVICE_TYPE_TUNEMU:
319 if(device_type == DEVICE_TYPE_TUNEMU)
320 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
323 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
326 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
327 device, strerror(errno));
331 switch(DATA(packet)[14] >> 4) {
333 DATA(packet)[12] = 0x08;
334 DATA(packet)[13] = 0x00;
337 DATA(packet)[12] = 0x86;
338 DATA(packet)[13] = 0xDD;
341 logger(DEBUG_TRAFFIC, LOG_ERR,
342 "Unknown IP version %d while reading packet from %s %s",
343 DATA(packet)[14] >> 4, device_info, device);
347 memset(DATA(packet), 0, 12);
348 packet->len = inlen + 14;
351 case DEVICE_TYPE_UTUN:
352 case DEVICE_TYPE_TUNIFHEAD: {
353 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
354 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
355 device, strerror(errno));
359 switch (DATA(packet)[14] >> 4) {
361 DATA(packet)[12] = 0x08;
362 DATA(packet)[13] = 0x00;
366 DATA(packet)[12] = 0x86;
367 DATA(packet)[13] = 0xDD;
371 logger(DEBUG_TRAFFIC, LOG_ERR,
372 "Unknown IP version %d while reading packet from %s %s",
373 DATA(packet)[14] >> 4, device_info, device);
377 memset(DATA(packet), 0, 12);
378 packet->len = inlen + 10;
382 case DEVICE_TYPE_TAP:
383 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
384 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
385 device, strerror(errno));
396 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
397 packet->len, device_info);
402 static bool write_packet(vpn_packet_t *packet) {
403 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
404 packet->len, device_info);
406 switch(device_type) {
407 case DEVICE_TYPE_TUN:
408 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
409 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
410 device, strerror(errno));
415 case DEVICE_TYPE_UTUN:
416 case DEVICE_TYPE_TUNIFHEAD: {
417 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
422 type = htonl(AF_INET);
425 type = htonl(AF_INET6);
428 logger(DEBUG_TRAFFIC, LOG_ERR,
429 "Unknown address family %x while writing packet to %s %s",
430 af, device_info, device);
434 memcpy(DATA(packet) + 10, &type, sizeof type);
436 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
437 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
444 case DEVICE_TYPE_TAP:
445 if(write(device_fd, DATA(packet), packet->len) < 0) {
446 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
447 device, strerror(errno));
453 case DEVICE_TYPE_TUNEMU:
454 if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
455 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
456 device, strerror(errno));
469 const devops_t os_devops = {
470 .setup = setup_device,
471 .close = close_device,
473 .write = write_packet,