X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fbsd%2Fdevice.c;h=3e64ba92d1f3d118958eba5f801e053d82d79745;hb=d917c8cb6b69475d568ccbe82389b9f2b3eb5e80;hp=f8a559686f75b7b55a2b7b1b1309450a7761a264;hpb=40c28589328a2aa96c2ce1419c5d90616c758b3d;p=tinc diff --git a/src/bsd/device.c b/src/bsd/device.c index f8a55968..3e64ba92 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -33,7 +33,12 @@ #include "bsd/tunemu.h" #endif -#define DEFAULT_DEVICE "/dev/tun0" +#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, @@ -61,15 +66,19 @@ static device_type_t device_type = DEVICE_TYPE_TUN; static bool setup_device(void) { char *type; - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) - device = xstrdup(DEFAULT_DEVICE); + if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(routing_mode == RMODE_ROUTER) + device = xstrdup(DEFAULT_TUN_DEVICE); + else + device = xstrdup(DEFAULT_TAP_DEVICE); + } if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device); if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { if(!strcasecmp(type, "tun")) - /* use default */; + /* use default */; #ifdef HAVE_TUNEMU else if(!strcasecmp(type, "tunemu")) device_type = DEVICE_TYPE_TUNEMU; @@ -93,7 +102,7 @@ static bool setup_device(void) { #ifdef HAVE_TUNEMU case DEVICE_TYPE_TUNEMU: { char dynamic_name[256] = ""; - device_fd = tunemu_open(dynamic_name); + device_fd = tunemu_open(dynamic_name); } break; #endif @@ -115,7 +124,7 @@ static bool setup_device(void) { device_type = DEVICE_TYPE_TUN; case DEVICE_TYPE_TUN: #ifdef TUNSIFHEAD - { + { const int zero = 0; if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) { logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno)); @@ -164,12 +173,12 @@ static bool setup_device(void) { iface = xstrdup(ifr.ifr_name); } } - + #endif break; #ifdef HAVE_TUNEMU case DEVICE_TYPE_TUNEMU: - device_info = "BSD tunemu device"; + device_info = "BSD tunemu device"; break; #endif } @@ -229,6 +238,7 @@ static bool read_packet(vpn_packet_t *packet) { return false; } + memset(packet->data, 0, 12); packet->len = inlen + 14; break; @@ -260,6 +270,7 @@ static bool read_packet(vpn_packet_t *packet) { return false; } + memset(packet->data, 0, 12); packet->len = inlen + 10; break; } @@ -277,7 +288,7 @@ static bool read_packet(vpn_packet_t *packet) { default: return false; } - + device_total_in += packet->len; logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", @@ -303,7 +314,7 @@ static bool write_packet(vpn_packet_t *packet) { u_int32_t type; struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}}; int af; - + af = (packet->data[12] << 8) + packet->data[13]; switch (af) { @@ -327,7 +338,7 @@ static bool write_packet(vpn_packet_t *packet) { } break; } - + case DEVICE_TYPE_TAP: if(write(device_fd, packet->data, packet->len) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,