|
|
|
|
@ -96,36 +96,24 @@ static int create_tun_device(char *ifname, size_t ifname_len) {
|
|
|
|
|
return fd; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Set IP address on TUN interface
|
|
|
|
|
// Set IP address on TUN interface (using ifconfig - more reliable on FreeBSD)
|
|
|
|
|
static int tun_set_ip(const char *ifname, const char *ip_addr) { |
|
|
|
|
if (!ifname || !ip_addr) { |
|
|
|
|
errno = EINVAL; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct in_addr ip, netmask; |
|
|
|
|
if (parse_ip_mask(ip_addr, &ip, &netmask) < 0) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid IP address format: %s", ip_addr); |
|
|
|
|
errno = EINVAL; |
|
|
|
|
// 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); |
|
|
|
|
|
|
|
|
|
int ret = system(cmd); |
|
|
|
|
if (ret != 0) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ifconfig failed for %s: %s", ifname, ip_addr); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct ifreq ifr; |
|
|
|
|
memset(&ifr, 0, sizeof(ifr)); |
|
|
|
|
|
|
|
|
|
// Set IP address
|
|
|
|
|
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr; |
|
|
|
|
addr->sin_family = AF_INET; |
|
|
|
|
addr->sin_addr = ip; |
|
|
|
|
if (if_ioctl(ifname, SIOCSIFADDR, &ifr) < 0) return -1; |
|
|
|
|
|
|
|
|
|
// Set netmask (FreeBSD uses ifr_addr for netmask too)
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr)); |
|
|
|
|
addr = (struct sockaddr_in *)&ifr.ifr_addr; |
|
|
|
|
addr->sin_family = AF_INET; |
|
|
|
|
addr->sin_addr = netmask; |
|
|
|
|
if (if_ioctl(ifname, SIOCSIFNETMASK, &ifr) < 0) return -1; |
|
|
|
|
|
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Set IP %s on %s via ifconfig", ip_addr, ifname); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -176,7 +164,15 @@ int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str
|
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Configure IP address
|
|
|
|
|
// 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
|
|
|
|
|
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); |
|
|
|
|
@ -188,14 +184,6 @@ int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str
|
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Bring interface up
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tun->ifindex = if_nametoindex(tun->ifname); |
|
|
|
|
if (tun->ifindex == 0) { |
|
|
|
|
|