From ea9d6d8870c862fc3218147f2d072d3d3db3fa49 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sun, 8 Mar 2026 20:52:49 +0300 Subject: [PATCH] 1 --- src/tun_freebsd.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/tun_freebsd.c b/src/tun_freebsd.c index a47407ca..3ee96127 100644 --- a/src/tun_freebsd.c +++ b/src/tun_freebsd.c @@ -117,22 +117,22 @@ static int tun_set_ip(const char *ifname, const char *ip_addr) { return 0; } -// Bring TUN interface up +// Bring TUN interface up (using ifconfig - more reliable on FreeBSD) static int tun_set_up(const char *ifname) { if (!ifname) { errno = EINVAL; return -1; } - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - - // Get current flags - if (if_ioctl(ifname, SIOCGIFFLAGS, &ifr) < 0) return -1; - - // Set UP and RUNNING - ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - if (if_ioctl(ifname, SIOCSIFFLAGS, &ifr) < 0) 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; } @@ -164,15 +164,7 @@ int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str return -1; } - // Bring interface UP first (required before setting IP on FreeBSD) - if (tun_set_up(tun->ifname) < 0) { - DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to bring up TUN interface %s", tun->ifname); - close(tun->fd); - tun->fd = -1; - return -1; - } - - // Configure IP address using ifconfig + // Configure IP address using ifconfig (also brings up the interface) 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);