Use devname() if available to support devfs cloning on BSD.
[tinc] / src / bsd / device.c
index efb80a8..70e0b0b 100644 (file)
@@ -1,7 +1,7 @@
 /*
     device.c -- Interaction BSD tun/tap device
     Copyright (C) 2001-2005 Ivo Timmermans,
-                  2001-2012 Guus Sliepen <guus@tinc-vpn.org>
+                  2001-2016 Guus Sliepen <guus@tinc-vpn.org>
                   2009      Grzegorz Dymarek <gregd72002@googlemail.com>
 
     This program is free software; you can redistribute it and/or modify
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#include "system.h"
+#include "../system.h"
 
-#include "conf.h"
-#include "device.h"
-#include "logger.h"
-#include "net.h"
-#include "route.h"
-#include "utils.h"
-#include "xalloc.h"
+#include "../conf.h"
+#include "../device.h"
+#include "../logger.h"
+#include "../net.h"
+#include "../route.h"
+#include "../utils.h"
+#include "../xalloc.h"
 
 #ifdef ENABLE_TUNEMU
-#include "bsd/tunemu.h"
+#include "tunemu.h"
 #endif
 
 #define DEFAULT_TUN_DEVICE "/dev/tun0"
-#if defined(HAVE_FREEBSD) || defined(HAVE_NETBSD)
 #define DEFAULT_TAP_DEVICE "/dev/tap0"
-#else
-#define DEFAULT_TAP_DEVICE "/dev/tun0"
-#endif
 
 typedef enum device_type {
        DEVICE_TYPE_TUN,
@@ -64,7 +60,7 @@ static device_type_t device_type = DEVICE_TYPE_TUN;
 #endif
 
 static bool setup_device(void) {
-       char *type;
+       // Find out which device file to open
 
        if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
                if(routing_mode == RMODE_ROUTER)
@@ -73,8 +69,9 @@ static bool setup_device(void) {
                        device = xstrdup(DEFAULT_TAP_DEVICE);
        }
 
-       if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
-               iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
+       // Find out if it's supposed to be a tun or a tap device
+
+       char *type;
 
        if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
                if(!strcasecmp(type, "tun"))
@@ -98,6 +95,8 @@ static bool setup_device(void) {
                        device_type = DEVICE_TYPE_TAP;
        }
 
+       // Open the device
+
        switch(device_type) {
 #ifdef ENABLE_TUNEMU
                case DEVICE_TYPE_TUNEMU: {
@@ -119,6 +118,27 @@ static bool setup_device(void) {
        fcntl(device_fd, F_SETFD, FD_CLOEXEC);
 #endif
 
+       // Guess what the corresponding interface is called
+
+       char *realname;
+
+#if defined(HAVE_FDEVNAME)
+       realname = fdevname(device_fd) ? : device;
+#elif defined(HAVE_DEVNAME)
+       struct stat buf;
+       if(!fstat(device_fd, &buf))
+               realname = devname(buf.st_rdev, S_IFCHR) ? : device;
+#else
+       realname = device;
+#endif
+
+       if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
+               iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
+       else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname))
+               logger(LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
+
+       // Configure the device as best as we can
+
        switch(device_type) {
                default:
                        device_type = DEVICE_TYPE_TUN;