1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-11-23 01:49:53 +03:00

Merge PR #673: Cedar: custom TAP interface name support for FreeBSD, function refactor

This commit is contained in:
Davide Beatrici 2018-08-28 14:08:03 +02:00 committed by GitHub
commit 3f8fa9d5d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 51 deletions

View File

@ -769,20 +769,14 @@
// //
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#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"
#ifdef UNIX_BSD #else // UNIX_BSD
#ifdef NO_VLAN #define TAP_NAME "tap"
#define TAP_BSD_FILENAME "/dev/tap0" #define TAP_DIR "/dev/"
#else // NO_VLAN #define TAP_MAX (512)
#define TAP_BSD_FILENAME "tap" #endif // UNIX_BSD
#endif // NO_VLAN
#define TAP_BSD_DIR "/dev/"
#define TAP_BSD_NUMBER (16)
#endif // UNIX_MACOS
#define LICENSE_EDITION_VPN3_NO_LICENSE 0 // Without license #define LICENSE_EDITION_VPN3_NO_LICENSE 0 // Without license

View File

@ -443,22 +443,17 @@ void GenerateTunName(char *name, char *prefix, char *tun_name, size_t tun_name_l
// Create a tap device // Create a tap device
int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up) int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up)
{ {
int fd; int fd = -1, s = -1;
char tap_name[MAX_SIZE], tap_path[MAX_SIZE];
struct ifreq ifr; struct ifreq ifr;
char eth_name[MAX_SIZE];
struct sockaddr sa;
char *tap_name = TAP_FILENAME_1;
int s;
#ifdef UNIX_BSD
char tap_bsd_name[256] = TAP_BSD_DIR TAP_BSD_FILENAME;
#endif
// Validate arguments // Validate arguments
if (name == NULL) if (name == NULL)
{ {
return -1; return -1;
} }
GenerateTunName(name, prefix, eth_name, sizeof(eth_name)); GenerateTunName(name, prefix, tap_name, sizeof(tap_name));
// Open the tun / tap // Open the tun / tap
#ifndef UNIX_BSD #ifndef UNIX_BSD
@ -486,21 +481,19 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
{ {
return -1; return -1;
} }
tap_name = TAP_FILENAME_2;
} }
#else // UNIX_BSD #else // UNIX_BSD
{ {
int i; sprintf(tap_path, "%s", TAP_DIR TAP_NAME);
fd = -1; for (int i = 0; i < TAP_MAX; i++) {
for (i = 0; i < TAP_BSD_NUMBER; i++) { sprintf(tap_path + StrLen(TAP_DIR TAP_NAME), "%d", i);
sprintf(tap_bsd_name + strlen(TAP_BSD_DIR TAP_BSD_FILENAME), "%d", i); fd = open(tap_path, O_RDWR);
fd = open(tap_bsd_name, O_RDWR);
if (fd != -1) if (fd != -1)
{ {
tap_name = tap_bsd_name;
break; break;
} }
} }
if (fd == -1) if (fd == -1)
{ {
return -1; return -1;
@ -509,13 +502,12 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
#endif // UNIX_BSD #endif // UNIX_BSD
#ifdef UNIX_LINUX #ifdef UNIX_LINUX
// Create a tap for Linux // Create a TAP device for Linux
// Set the device name // Set the name and the flags
Zero(&ifr, sizeof(ifr)); Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), tap_name);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name);
if (ioctl(fd, TUNSETIFF, &ifr) == -1) if (ioctl(fd, TUNSETIFF, &ifr) == -1)
{ {
@ -524,57 +516,66 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
return -1; return -1;
} }
// MAC address setting
s = socket(AF_INET, SOCK_DGRAM, 0); s = socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1) if (s != -1)
{ {
// Set the MAC address
if (mac_address != NULL) if (mac_address != NULL)
{ {
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), tap_name);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
Copy(&ifr.ifr_addr.sa_data, mac_address, 6); Copy(&ifr.ifr_hwaddr.sa_data, mac_address, ETHER_ADDR_LEN);
ioctl(s, SIOCSIFHWADDR, &ifr); ioctl(s, SIOCSIFHWADDR, &ifr);
} }
Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name);
ioctl(s, SIOCGIFFLAGS, &ifr);
if (create_up) if (create_up)
{ {
Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), tap_name);
ioctl(s, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_UP; ifr.ifr_flags |= IFF_UP;
ioctl(s, SIOCSIFFLAGS, &ifr); ioctl(s, SIOCSIFFLAGS, &ifr);
} }
close(s); close(s);
} }
#endif // UNIX_LINUX
#else // UNIX_LINUX
#ifdef UNIX_BSD #ifdef UNIX_BSD
// MAC address setting // Create a TAP device for BSD
Zero(&ifr, sizeof(ifr));
// Get the current name
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), tap_path + StrLen(TAP_DIR));
s = socket(AF_INET, SOCK_DGRAM, 0); s = socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1) if (s != -1)
{ {
char *bsd_eth_name; // Set the name, if possible
bsd_eth_name = tap_bsd_name + strlen(TAP_BSD_DIR); #ifdef SIOCSIFNAME
ifr.ifr_data = tap_name;
ioctl(s, SIOCSIFNAME, &ifr);
#else // SIOCSIFNAME
StrCpy(tap_name, sizeof(tap_name), ifr.ifr_name);
#endif // SIOCSIFNAME
// Set the MAC address
if (mac_address != NULL) if (mac_address != NULL)
{ {
Zero(&ifr, sizeof(ifr)); Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), bsd_eth_name); StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), tap_name);
ifr.ifr_addr.sa_len = ETHER_ADDR_LEN; ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
ifr.ifr_addr.sa_family = AF_LINK; ifr.ifr_addr.sa_family = AF_LINK;
Copy(&ifr.ifr_addr.sa_data, mac_address, ETHER_ADDR_LEN); Copy(&ifr.ifr_addr.sa_data, mac_address, ETHER_ADDR_LEN);
ioctl(s, SIOCSIFLLADDR, &ifr); ioctl(s, SIOCSIFLLADDR, &ifr);
} }
Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), bsd_eth_name);
ioctl(s, SIOCGIFFLAGS, &ifr);
if (create_up) if (create_up)
{ {
Zero(&ifr, sizeof(ifr));
StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), tap_name);
ioctl(s, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_UP; ifr.ifr_flags |= IFF_UP;
ioctl(s, SIOCSIFFLAGS, &ifr); ioctl(s, SIOCSIFFLAGS, &ifr);
} }
@ -582,8 +583,9 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
close(s); close(s);
} }
#endif // UNIX_BSD #endif // UNIX_BSD
#ifdef UNIX_SOLARIS #ifdef UNIX_SOLARIS
// Create a tap for Solaris // Create a TAP device for Solaris
{ {
int ip_fd; int ip_fd;
int tun_fd; int tun_fd;
@ -647,9 +649,7 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre
close(tun_fd); close(tun_fd);
close(ip_fd); close(ip_fd);
} }
#endif // UNIX_SOLARIS #endif // UNIX_SOLARIS
#endif // UNIX_LINUX
return fd; return fd;
} }