1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-11-22 09:29:52 +03:00

Merge PR #1859: FreeBSD: Improve client's virtual network interface handling

This commit is contained in:
Davide Beatrici 2023-06-01 09:38:26 +02:00 committed by GitHub
commit 0f689d9dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 14 deletions

View File

@ -675,6 +675,8 @@
// //
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#define UNIX_VLAN_IFACE_PREFIX "vpn" // Prefix of UNIX virual LAN card interface
#ifndef UNIX_BSD #ifndef UNIX_BSD
#define TAP_FILENAME_1 "/dev/net/tun" #define TAP_FILENAME_1 "/dev/net/tun"
#define TAP_FILENAME_2 "/dev/tun" #define TAP_FILENAME_2 "/dev/tun"

View File

@ -6524,9 +6524,7 @@ bool CtConnect(CLIENT *c, RPC_CLIENT_CONNECT *connect)
// Requires account and VLan lists of the CLIENT argument to be already locked // Requires account and VLan lists of the CLIENT argument to be already locked
bool CtVLansDown(CLIENT *c) bool CtVLansDown(CLIENT *c)
{ {
#ifndef UNIX_LINUX #if defined(UNIX_LINUX) || defined(UNIX_BSD)
return true;
#else
int i; int i;
LIST *tmpVLanList; LIST *tmpVLanList;
UNIX_VLAN t, *r; UNIX_VLAN t, *r;
@ -6568,6 +6566,8 @@ bool CtVLansDown(CLIENT *c)
ReleaseList(tmpVLanList); ReleaseList(tmpVLanList);
return result; return result;
#else
return true;
#endif #endif
} }
@ -6575,9 +6575,7 @@ bool CtVLansDown(CLIENT *c)
// Requires VLan list of the CLIENT argument to be already locked // Requires VLan list of the CLIENT argument to be already locked
bool CtVLansUp(CLIENT *c) bool CtVLansUp(CLIENT *c)
{ {
#ifndef UNIX_LINUX #if defined(UNIX_LINUX) || defined(UNIX_BSD)
return true;
#else
int i; int i;
UNIX_VLAN *r; UNIX_VLAN *r;
@ -6591,9 +6589,8 @@ bool CtVLansUp(CLIENT *c)
r = LIST_DATA(c->UnixVLanList, i); r = LIST_DATA(c->UnixVLanList, i);
UnixVLanSetState(r->Name, true); UnixVLanSetState(r->Name, true);
} }
return true;
#endif #endif
return true;
} }
// Get the account information // Get the account information

View File

@ -7987,7 +7987,8 @@ void SmBridgeDlgOnOk(HWND hWnd, SM_SERVER *s)
StrCpy(t.HubName, sizeof(t.HubName), hub); StrCpy(t.HubName, sizeof(t.HubName), hub);
t.TapMode = tapmode; t.TapMode = tapmode;
if (InStrEx(t.DeviceName, "vpn", false) || InStrEx(t.DeviceName, "tun", false) if (InStrEx(t.DeviceName, UNIX_VLAN_IFACE_PREFIX, false)
|| InStrEx(t.DeviceName, "tun", false)
|| InStrEx(t.DeviceName, "tap", false)) || InStrEx(t.DeviceName, "tap", false))
{ {
// Trying to make a local bridge to the VPN device // Trying to make a local bridge to the VPN device

View File

@ -554,7 +554,7 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
} }
int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up) int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up)
{ {
return UnixCreateTapDeviceEx(name, "vpn", mac_address, create_up); return UnixCreateTapDeviceEx(name, UNIX_VLAN_IFACE_PREFIX, mac_address, create_up);
} }
// Close the tap device // Close the tap device
@ -569,12 +569,40 @@ void UnixCloseTapDevice(int fd)
close(fd); close(fd);
} }
// Destroy the tap device (for FreeBSD)
// FreeBSD tap device is still plumbed after closing fd so need to destroy after close
void UnixDestroyTapDevice(char *name)
{
#ifdef UNIX_BSD
struct ifreq ifr;
char eth_name[MAX_SIZE];
int s;
Zero(&ifr, sizeof(ifr));
GenerateTunName(name, UNIX_VLAN_IFACE_PREFIX, eth_name, sizeof(eth_name));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1)
{
return;
}
ioctl(s, SIOCIFDESTROY, &ifr);
close(s);
#endif // UNIX_BSD
}
#else // NO_VLAN #else // NO_VLAN
void UnixCloseTapDevice(int fd) void UnixCloseTapDevice(int fd)
{ {
} }
void UnixDestroyTapDevice(char *name)
{
}
int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up) int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up)
{ {
return -1; return -1;
@ -662,13 +690,13 @@ bool UnixVLanCreateEx(char *name, char *prefix, UCHAR *mac_address, bool create_
} }
bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up) bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up)
{ {
return UnixVLanCreateEx(name, "vpn", mac_address, create_up); return UnixVLanCreateEx(name, UNIX_VLAN_IFACE_PREFIX, mac_address, create_up);
} }
// Set a VLAN up/down // Set a VLAN up/down
bool UnixVLanSetState(char* name, bool state_up) bool UnixVLanSetState(char* name, bool state_up)
{ {
#ifdef UNIX_LINUX #if defined(UNIX_LINUX) || defined(UNIX_BSD)
UNIX_VLAN_LIST *t, tt; UNIX_VLAN_LIST *t, tt;
struct ifreq ifr; struct ifreq ifr;
int s; int s;
@ -689,7 +717,7 @@ bool UnixVLanSetState(char* name, bool state_up)
return false; return false;
} }
GenerateTunName(name, "vpn", eth_name, sizeof(eth_name)); GenerateTunName(name, UNIX_VLAN_IFACE_PREFIX, eth_name, sizeof(eth_name));
Zero(&ifr, sizeof(ifr)); Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name); StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name);
@ -714,7 +742,7 @@ bool UnixVLanSetState(char* name, bool state_up)
close(s); close(s);
} }
UnlockList(unix_vlan); UnlockList(unix_vlan);
#endif // UNIX_LINUX #endif // UNIX_LINUX || UNIX_BSD
return true; return true;
} }
@ -769,6 +797,9 @@ void UnixVLanDelete(char *name)
if (t != NULL) if (t != NULL)
{ {
UnixCloseTapDevice(t->fd); UnixCloseTapDevice(t->fd);
#ifdef UNIX_BSD
UnixDestroyTapDevice(t->Name);
#endif
Delete(unix_vlan, t); Delete(unix_vlan, t);
Free(t); Free(t);
} }
@ -815,6 +846,9 @@ void UnixVLanFree()
UNIX_VLAN_LIST *t = LIST_DATA(unix_vlan, i); UNIX_VLAN_LIST *t = LIST_DATA(unix_vlan, i);
UnixCloseTapDevice(t->fd); UnixCloseTapDevice(t->fd);
#ifdef UNIX_BSD
UnixDestroyTapDevice(t->Name);
#endif
Free(t); Free(t);
} }

View File

@ -60,6 +60,7 @@ struct UNIX_VLAN_LIST
int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up); int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up);
int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up); int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up);
void UnixCloseTapDevice(int fd); void UnixCloseTapDevice(int fd);
void UnixDestroyTapDevice(char *name);
void UnixVLanInit(); void UnixVLanInit();
void UnixVLanFree(); void UnixVLanFree();
bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up); bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up);