tun bsd fix
This commit is contained in:
1 parent
ea9d6d8870
commit
cad523f1d0
1 file changed
+207
-91
+207
-91
@@ -1,8 +1,6 @@
|
||||
// tun_freebsd.c - FreeBSD TUN/TAP implementation
|
||||
// Uses /dev/tun device with ioctl
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
|
||||
#include "tun_if.h"
|
||||
#include "../lib/debug_config.h"
|
||||
#include <stdio.h>
|
||||
@@ -17,10 +15,11 @@
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if_tun.h> // Для TUNSIFINFO, TUNSIFMODE, TUNSIFHEAD
|
||||
#include <libutil.h> // Для devname()
|
||||
#include "../lib/platform_compat.h"
|
||||
#include <errno.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Helper for interface ioctl operations
|
||||
static int if_ioctl(const char *ifname, unsigned long request, struct ifreq *ifr) {
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
@@ -28,169 +27,268 @@ static int if_ioctl(const char *ifname, unsigned long request, struct ifreq *ifr
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to create ioctl socket: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(ifr->ifr_name, ifname, IFNAMSIZ - 1);
|
||||
ifr->ifr_name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
int ret = ioctl(sock, request, ifr);
|
||||
if (ret < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ioctl failed for %s (request=0x%lx): %s", ifname, request, strerror(errno));
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Parse IP address and CIDR mask
|
||||
static int parse_ip_mask(const char *ip_addr, struct in_addr *ip, struct in_addr *netmask) {
|
||||
if (!ip_addr || !ip || !netmask) return -1;
|
||||
|
||||
char ip_str[INET_ADDRSTRLEN];
|
||||
char *slash = strchr(ip_addr, '/');
|
||||
if (!slash) return -1;
|
||||
|
||||
size_t ip_len = slash - ip_addr;
|
||||
if (ip_len >= sizeof(ip_str)) return -1;
|
||||
strncpy(ip_str, ip_addr, ip_len);
|
||||
ip_str[ip_len] = '\0';
|
||||
|
||||
if (inet_pton(AF_INET, ip_str, ip) <= 0) return -1;
|
||||
|
||||
char *endptr;
|
||||
long cidr = strtol(slash + 1, &endptr, 10);
|
||||
if (*endptr != '\0' || cidr < 0 || cidr > 32) return -1;
|
||||
|
||||
uint32_t mask = (cidr == 0) ? 0 : ~((1U << (32 - cidr)) - 1);
|
||||
netmask->s_addr = htonl(mask);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Create TUN device (FreeBSD uses /dev/tun)
|
||||
// Create TUN device using cloning device /dev/tun
|
||||
static int create_tun_device(char *ifname, size_t ifname_len) {
|
||||
struct ifreq ifr;
|
||||
int fd;
|
||||
|
||||
// FreeBSD uses /dev/tun instead of /dev/net/tun
|
||||
// Открываем cloning device /dev/tun
|
||||
fd = open("/dev/tun", O_RDWR);
|
||||
if (fd < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to open /dev/tun: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
// Note: FreeBSD doesn't use IFF_TUN/IFF_NO_PI flags like Linux
|
||||
|
||||
if (ifname && ifname_len > 0 && ifname[0] != '\0') {
|
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Opened cloning device /dev/tun (fd=%d)", fd);
|
||||
// Получаем имя созданного интерфейса через devname()
|
||||
// devname() возвращает имя устройства по major/minor номерам
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "fstat failed: %s", strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// On FreeBSD, interface name is set via ifname buffer after open
|
||||
if (ifname && ifname_len > 0) {
|
||||
// FreeBSD returns the interface name in the ifr_name after successful open
|
||||
// We use the provided name as-is for tunnel interfaces (tun0, tun1, etc.)
|
||||
strncpy(ifname, ifname, ifname_len - 1);
|
||||
ifname[ifname_len - 1] = '\0';
|
||||
const char *dev_name = devname(st.st_rdev, S_IFCHR);
|
||||
if (!dev_name) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "devname failed: %s", strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// dev_name будет в формате "tunN" или "/dev/tunN"
|
||||
const char *base_name = dev_name;
|
||||
if (strncmp(dev_name, "/dev/", 5) == 0) {
|
||||
base_name = dev_name + 5;
|
||||
}
|
||||
strncpy(ifname, base_name, ifname_len - 1);
|
||||
ifname[ifname_len - 1] = '\0';
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Created TUN device: %s", ifname);
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Set IP address on TUN interface (using ifconfig - more reliable on FreeBSD)
|
||||
// Configure TUN device mode (POINTOPOINT + MULTICAST)
|
||||
static int tun_set_mode(int fd, const char *ifname) {
|
||||
int mode = IFF_POINTOPOINT | IFF_MULTICAST;
|
||||
if (ioctl(fd, TUNSIFMODE, &mode) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "TUNSIFMODE failed for %s: %s", ifname, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set TUNSIFMODE (POINTOPOINT | MULTICAST) on %s", ifname);
|
||||
return 0;
|
||||
}
|
||||
// Enable TUNSIFHEAD mode (multi-af, 4-byte header with address family)
|
||||
static int tun_set_head(int fd, const char *ifname) {
|
||||
int head = 1;
|
||||
if (ioctl(fd, TUNSIFHEAD, &head) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "TUNSIFHEAD failed for %s: %s", ifname, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set TUNSIFHEAD on %s", ifname);
|
||||
return 0;
|
||||
}
|
||||
// Set IP address on TUN interface using ifconfig
|
||||
// FreeBSD требует отдельно IP и netmask для TUN
|
||||
// For point-to-point, require destination address, set to same as local for /32
|
||||
static int tun_set_ip(const char *ifname, const char *ip_addr) {
|
||||
if (!ifname || !ip_addr) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Use ifconfig command - more reliable on FreeBSD for TUN interfaces
|
||||
char cmd[256];
|
||||
snprintf(cmd, sizeof(cmd), "ifconfig %s %s up 2>/dev/null", ifname, ip_addr);
|
||||
|
||||
// Парсим IP и CIDR
|
||||
char ip_str[INET_ADDRSTRLEN] = {0};
|
||||
int cidr = -1;
|
||||
char *slash = strchr(ip_addr, '/');
|
||||
if (slash) {
|
||||
size_t ip_len = slash - ip_addr;
|
||||
if (ip_len >= sizeof(ip_str)) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "IP address too long");
|
||||
return -1;
|
||||
}
|
||||
strncpy(ip_str, ip_addr, ip_len);
|
||||
ip_str[ip_len] = '\0';
|
||||
char *endptr;
|
||||
cidr = (int)strtol(slash + 1, &endptr, 10);
|
||||
if (*endptr != '\0' || cidr < 0 || cidr > 32) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Invalid CIDR: %s", slash + 1);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
strncpy(ip_str, ip_addr, sizeof(ip_str) - 1);
|
||||
cidr = 32; // По умолчанию /32 для POINTOPOINT
|
||||
}
|
||||
// Вычисляем netmask из CIDR
|
||||
struct in_addr netmask;
|
||||
if (cidr == 0) {
|
||||
netmask.s_addr = 0;
|
||||
} else {
|
||||
netmask.s_addr = htonl(~((1U << (32 - cidr)) - 1));
|
||||
}
|
||||
char mask_str[INET_ADDRSTRLEN];
|
||||
if (inet_ntop(AF_INET, &netmask, mask_str, sizeof(mask_str)) == NULL) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "inet_ntop failed for netmask");
|
||||
return -1;
|
||||
}
|
||||
// Формируем команду ifconfig с destination address (same as local for /32)
|
||||
char cmd[512];
|
||||
snprintf(cmd, sizeof(cmd), "ifconfig %s inet %s %s netmask %s up",
|
||||
ifname, ip_str, ip_str, mask_str);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Executing: %s", cmd);
|
||||
int ret = system(cmd);
|
||||
if (ret != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ifconfig failed for %s: %s", ifname, ip_addr);
|
||||
return -1;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ifconfig failed for %s with IP %s netmask %s",
|
||||
ifname, ip_str, mask_str);
|
||||
// Fallback: пробуем через ioctl напрямую
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Trying ioctl fallback...");
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "socket failed: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
|
||||
// Устанавливаем адрес
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
sin->sin_family = AF_INET;
|
||||
if (inet_pton(AF_INET, ip_str, &sin->sin_addr) <= 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "inet_pton failed for %s", ip_str);
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "SIOCSIFADDR failed: %s", strerror(errno));
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
// Устанавливаем destination address (same as local)
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
|
||||
struct sockaddr_in *sin_dst = (struct sockaddr_in *)&ifr.ifr_dstaddr;
|
||||
sin_dst->sin_family = AF_INET;
|
||||
sin_dst->sin_addr = sin->sin_addr; // same as local
|
||||
if (ioctl(sock, SIOCSIFDSTADDR, &ifr) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "SIOCSIFDSTADDR failed: %s", strerror(errno));
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
// Устанавливаем netmask через ifr_addr (union в ifreq)
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
|
||||
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
sin->sin_family = AF_INET;
|
||||
sin->sin_addr = netmask;
|
||||
if (ioctl(sock, SIOCSIFNETMASK, &ifr) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "SIOCSIFNETMASK failed: %s", strerror(errno));
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
// Поднимаем интерфейс
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
|
||||
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "SIOCGIFFLAGS failed: %s", strerror(errno));
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
|
||||
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "SIOCSIFFLAGS failed: %s", strerror(errno));
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
close(sock);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Successfully configured %s via ioctl", ifname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set IP %s on %s via ifconfig", ip_addr, ifname);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set IP %s/%d on %s", ip_str, cidr, ifname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Bring TUN interface up (using ifconfig - more reliable on FreeBSD)
|
||||
static int tun_set_up(const char *ifname) {
|
||||
if (!ifname) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Use ifconfig to bring up the interface
|
||||
char cmd[256];
|
||||
snprintf(cmd, sizeof(cmd), "ifconfig %s up 2>/dev/null", ifname);
|
||||
|
||||
int ret = system(cmd);
|
||||
if (ret != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ifconfig %s up failed", ifname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set MTU on TUN interface
|
||||
static int tun_set_mtu(const char *ifname, int mtu) {
|
||||
if (!ifname || mtu <= 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
ifr.ifr_mtu = mtu;
|
||||
|
||||
if (if_ioctl(ifname, SIOCSIFMTU, &ifr) < 0) return -1;
|
||||
|
||||
if (if_ioctl(ifname, SIOCSIFMTU, &ifr) < 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_TUN, "Failed to set MTU %d on %s: %s", mtu, ifname, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set MTU %d on %s", mtu, ifname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Platform-specific initialization for FreeBSD
|
||||
int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str, int mtu) {
|
||||
(void)ifname; // ifname is stored in tun->ifname by caller
|
||||
|
||||
(void)ifname; // FreeBSD выделяет имя автоматически через cloning device
|
||||
// Create TUN device
|
||||
tun->fd = create_tun_device(tun->ifname, sizeof(tun->ifname));
|
||||
if (tun->fd < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to create TUN device");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Configure IP address using ifconfig (also brings up the interface)
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN interface created: %s (fd=%d)", tun->ifname, tun->fd);
|
||||
// Устанавливаем режим POINTOPOINT (должно быть ДО поднятия интерфейса)
|
||||
if (tun_set_mode(tun->fd, tun->ifname) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN mode on %s", tun->ifname);
|
||||
close(tun->fd);
|
||||
tun->fd = -1;
|
||||
return -1;
|
||||
}
|
||||
// Включаем TUNSIFHEAD для multi-af режима
|
||||
if (tun_set_head(tun->fd, tun->ifname) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN head mode on %s", tun->ifname);
|
||||
close(tun->fd);
|
||||
tun->fd = -1;
|
||||
return -1;
|
||||
}
|
||||
// Configure IP address (это также поднимает интерфейс)
|
||||
if (tun_set_ip(tun->ifname, ip_str) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN IP: %s", ip_str);
|
||||
close(tun->fd);
|
||||
tun->fd = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set MTU
|
||||
if (tun_set_mtu(tun->ifname, mtu) < 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_TUN, "Failed to set MTU %d on %s: %s", mtu, tun->ifname, strerror(errno));
|
||||
DEBUG_WARN(DEBUG_CATEGORY_TUN, "Failed to set MTU %d on %s", mtu, tun->ifname);
|
||||
}
|
||||
|
||||
// Получаем ifindex
|
||||
tun->ifindex = if_nametoindex(tun->ifname);
|
||||
if (tun->ifindex == 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to get ifindex for %s", tun->ifname);
|
||||
} else {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Interface %s has index %d", tun->ifname, tun->ifindex);
|
||||
}
|
||||
|
||||
// Set non-blocking mode
|
||||
int flags = fcntl(tun->fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
fcntl(tun->fd, F_SETFL, flags | O_NONBLOCK);
|
||||
if (fcntl(tun->fd, F_SETFL, O_NONBLOCK) < 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_TUN, "Failed to set non-blocking mode: %s", strerror(errno));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Platform-specific cleanup for FreeBSD
|
||||
void tun_platform_cleanup(struct tun_if* tun) {
|
||||
if (tun->fd >= 0) {
|
||||
@@ -198,24 +296,42 @@ void tun_platform_cleanup(struct tun_if* tun) {
|
||||
tun->fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Platform-specific read for FreeBSD
|
||||
// Возвращает данные без 4-байтового заголовка (TUNSIFHEAD обрабатывается ядром)
|
||||
ssize_t tun_platform_read(struct tun_if* tun, uint8_t* buf, size_t len) {
|
||||
ssize_t nread = read(tun->fd, buf, len);
|
||||
if (nread < 0 && errno == EINTR) {
|
||||
return 0; // Interrupted, not an error
|
||||
uint32_t family;
|
||||
struct iovec iv[2];
|
||||
// Читаем family + данные
|
||||
iv[0].iov_base = &family;
|
||||
iv[0].iov_len = sizeof(family);
|
||||
iv[1].iov_base = buf;
|
||||
iv[1].iov_len = len;
|
||||
ssize_t nread = readv(tun->fd, iv, 2);
|
||||
if (nread < 0) {
|
||||
if (errno == EINTR) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return nread;
|
||||
// Возвращаем только размер данных (без family)
|
||||
if (nread <= (ssize_t)sizeof(family)) {
|
||||
return 0;
|
||||
}
|
||||
return nread - sizeof(family);
|
||||
}
|
||||
|
||||
// Platform-specific write for FreeBSD
|
||||
// Добавляет 4-байтовый заголовок с family перед записью
|
||||
ssize_t tun_platform_write(struct tun_if* tun, const uint8_t* buf, size_t len) {
|
||||
return write(tun->fd, buf, len);
|
||||
uint32_t family = htonl(AF_INET); // Предполагаем IPv4, можно определять по заголовку пакета
|
||||
struct iovec iv[2];
|
||||
iv[0].iov_base = &family;
|
||||
iv[0].iov_len = sizeof(family);
|
||||
iv[1].iov_base = (void*)buf;
|
||||
iv[1].iov_len = len;
|
||||
return writev(tun->fd, iv, 2);
|
||||
}
|
||||
|
||||
// Get poll fd for uasync
|
||||
int tun_platform_get_poll_fd(struct tun_if* tun) {
|
||||
return tun->fd;
|
||||
}
|
||||
|
||||
#endif // __FreeBSD__
|
||||
Reference in new issue
Block a user