+ if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
+ logger(LOG_ERR, "Only tap devices support switch mode!");
+ return false;
+ }
+
+ // Open the device
+
+ switch(device_type) {
+#ifdef ENABLE_TUNEMU
+ case DEVICE_TYPE_TUNEMU: {
+ char dynamic_name[256] = "";
+ device_fd = tunemu_open(dynamic_name);
+ }
+ break;
+#endif
+#ifdef HAVE_NET_IF_UTUN_H
+ case DEVICE_TYPE_UTUN:
+ return setup_utun();
+#endif
+ default:
+ device_fd = open(device, O_RDWR | O_NONBLOCK);
+ }
+
+ if(device_fd < 0) {
+ logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
+ return false;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(device_fd, F_SETFD, FD_CLOEXEC);
+#endif
+
+ // Guess what the corresponding interface is called
+
+ char *realname = NULL;
+
+#if defined(HAVE_FDEVNAME)
+ realname = fdevname(device_fd);
+#elif defined(HAVE_DEVNAME)
+ struct stat buf;
+ if(!fstat(device_fd, &buf))
+ realname = devname(buf.st_rdev, S_IFCHR);
+#endif
+
+ if(!realname)
+ realname = device;
+
+ 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
+