From 696a9bc0a1c7f7d249be8ce27c57a7f8dfc60c87 Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Fri, 2 Jun 2023 18:01:23 +0900 Subject: [PATCH 1/5] Cedar: Don't hardcode prefix for virtual brige destination --- src/Cedar/BridgeUnix.c | 2 +- src/Cedar/Cedar.h | 3 ++- src/Cedar/SM.c | 3 ++- src/Cedar/VLanUnix.c | 8 ++++---- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Cedar/BridgeUnix.c b/src/Cedar/BridgeUnix.c index d5890cba..69fb20f3 100644 --- a/src/Cedar/BridgeUnix.c +++ b/src/Cedar/BridgeUnix.c @@ -321,7 +321,7 @@ TOKEN_LIST *GetEthListLinux(bool enum_normal, bool enum_rawip) { if (IsInListStr(o, name) == false) { - if (StartWith(name, "tap_") == false) + if (StartWith(name, UNIX_VLAN_BRDEST_IFACE_PREFIX"_") == false) { Add(o, CopyStr(name)); } diff --git a/src/Cedar/Cedar.h b/src/Cedar/Cedar.h index 7e3c88e0..5ce8afa0 100644 --- a/src/Cedar/Cedar.h +++ b/src/Cedar/Cedar.h @@ -675,7 +675,8 @@ // ////////////////////////////////////////////////////////////////////// -#define UNIX_VLAN_IFACE_PREFIX "vpn" // Prefix of UNIX virual LAN card interface +#define UNIX_VLAN_CLIENT_IFACE_PREFIX "vpn" // Prefix of UNIX virtual LAN card interface (used for client) +#define UNIX_VLAN_BRDEST_IFACE_PREFIX "tap" // Prefix of UNIX virtual LAN card interface (used for bridge destination) #ifndef UNIX_BSD #define TAP_FILENAME_1 "/dev/net/tun" diff --git a/src/Cedar/SM.c b/src/Cedar/SM.c index ce6d49b7..41c17700 100644 --- a/src/Cedar/SM.c +++ b/src/Cedar/SM.c @@ -7987,7 +7987,8 @@ void SmBridgeDlgOnOk(HWND hWnd, SM_SERVER *s) StrCpy(t.HubName, sizeof(t.HubName), hub); t.TapMode = tapmode; - if (InStrEx(t.DeviceName, UNIX_VLAN_IFACE_PREFIX, false) + if (InStrEx(t.DeviceName, UNIX_VLAN_CLIENT_IFACE_PREFIX, false) + || InStrEx(t.DeviceName, "UNIX_VLAN_BRDEST_IFACE_PREFIX, false) || InStrEx(t.DeviceName, "tun", false) || InStrEx(t.DeviceName, "tap", false)) { diff --git a/src/Cedar/VLanUnix.c b/src/Cedar/VLanUnix.c index 633c4fd9..41d29ed7 100644 --- a/src/Cedar/VLanUnix.c +++ b/src/Cedar/VLanUnix.c @@ -273,7 +273,7 @@ VLAN *NewTap(char *name, char *mac_address, bool create_up) return NULL; } - fd = UnixCreateTapDeviceEx(name, "tap", mac_address, create_up); + fd = UnixCreateTapDeviceEx(name, UNIX_VLAN_BRDEST_IFACE_PREFIX, mac_address, create_up); if (fd == -1) { return NULL; @@ -565,7 +565,7 @@ int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool cre } int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up) { - return UnixCreateTapDeviceEx(name, UNIX_VLAN_IFACE_PREFIX, mac_address, create_up); + return UnixCreateTapDeviceEx(name, UNIX_VLAN_CLIENT_IFACE_PREFIX, mac_address, create_up); } // Close the tap device @@ -701,7 +701,7 @@ bool UnixVLanCreateEx(char *name, char *prefix, UCHAR *mac_address, bool create_ } bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up) { - return UnixVLanCreateEx(name, UNIX_VLAN_IFACE_PREFIX, mac_address, create_up); + return UnixVLanCreateEx(name, UNIX_VLAN_CLIENT_IFACE_PREFIX, mac_address, create_up); } // Set a VLAN up/down @@ -728,7 +728,7 @@ bool UnixVLanSetState(char* name, bool state_up) return false; } - GenerateTunName(name, UNIX_VLAN_IFACE_PREFIX, eth_name, sizeof(eth_name)); + GenerateTunName(name, UNIX_VLAN_CLIENT_IFACE_PREFIX, eth_name, sizeof(eth_name)); Zero(&ifr, sizeof(ifr)); StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name); From 09708bc8cbef153e9526c0a9739b2aac6ac05524 Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Mon, 5 Jun 2023 18:29:22 +0900 Subject: [PATCH 2/5] Cedar/BridgeUnix: make sure to destroy tap device for bridge on FreeBSD Also, rename NewTap/FreeTap to NewBridgeTap/FreeBridgeTap because these functions are used to create/destroy tap device used for bridge destination. --- src/Cedar/BridgeUnix.c | 6 +++--- src/Cedar/VLanUnix.c | 37 +++++++++++++++++++++++++++++-------- src/Cedar/VLanUnix.h | 7 ++++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/Cedar/BridgeUnix.c b/src/Cedar/BridgeUnix.c index 69fb20f3..5c1d0646 100644 --- a/src/Cedar/BridgeUnix.c +++ b/src/Cedar/BridgeUnix.c @@ -506,7 +506,7 @@ ETH *OpenEthLinux(char *name, bool local, bool tapmode, char *tapaddr) { #ifndef NO_VLAN // In tap mode - VLAN *v = NewTap(name, tapaddr, true); + VLAN *v = NewBridgeTap(name, tapaddr, true); if (v == NULL) { return NULL; @@ -1399,7 +1399,7 @@ ETH *OpenEthBSD(char *name, bool local, bool tapmode, char *tapaddr) { #ifndef NO_VLAN // In tap mode - VLAN *v = NewTap(name, tapaddr, true); + VLAN *v = NewBridgeTap(name, tapaddr, true); if (v == NULL) { return NULL; @@ -1475,7 +1475,7 @@ void CloseEth(ETH *e) if (e->Tap != NULL) { #ifndef NO_VLAN - FreeTap(e->Tap); + FreeBridgeTap(e->Tap); #endif // NO_VLAN } diff --git a/src/Cedar/VLanUnix.c b/src/Cedar/VLanUnix.c index 41d29ed7..ae7e93e8 100644 --- a/src/Cedar/VLanUnix.c +++ b/src/Cedar/VLanUnix.c @@ -263,7 +263,7 @@ void FreeVLan(VLAN *v) } // Create a tap -VLAN *NewTap(char *name, char *mac_address, bool create_up) +VLAN *NewBridgeTap(char *name, char *mac_address, bool create_up) { int fd; VLAN *v; @@ -288,7 +288,7 @@ VLAN *NewTap(char *name, char *mac_address, bool create_up) } // Close the tap -void FreeTap(VLAN *v) +void FreeBridgeTap(VLAN *v) { // Validate arguments if (v == NULL) @@ -296,7 +296,11 @@ void FreeTap(VLAN *v) return; } - close(v->fd); + UnixCloseTapDevice(v->fd); +#ifdef UNIX_BSD + UnixDestroyBridgeTapDevice(v->InstanceName); +#endif + FreeVLan(v); } @@ -582,7 +586,7 @@ void UnixCloseTapDevice(int 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) +void UnixDestroyTapDeviceEx(char *name, char *prefix) { #ifdef UNIX_BSD struct ifreq ifr; @@ -590,7 +594,7 @@ void UnixDestroyTapDevice(char *name) int s; Zero(&ifr, sizeof(ifr)); - GenerateTunName(name, UNIX_VLAN_IFACE_PREFIX, eth_name, sizeof(eth_name)); + GenerateTunName(name, prefix, eth_name, sizeof(eth_name)); StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), eth_name); s = socket(AF_INET, SOCK_DGRAM, 0); @@ -604,12 +608,29 @@ void UnixDestroyTapDevice(char *name) #endif // UNIX_BSD } +void UnixDestroyBridgeTapDevice(char *name) +{ +#ifdef UNIX_BSD + UnixDestroyTapDeviceEx(name, UNIX_VLAN_BRDEST_IFACE_PREFIX); +#endif // UNIX_BSD +} + +void UnixDestroyClientTapDevice(char *name) +{ +#ifdef UNIX_BSD + UnixDestroyTapDeviceEx(name, UNIX_VLAN_CLIENT_IFACE_PREFIX); +#endif // UNIX_BSD +} + #else // NO_VLAN -void UnixCloseTapDevice(int fd) +void UnixCloseBridgeTapDevice(int fd) { } +void UnixCloseClientTapDevice(int fd) +{ + void UnixDestroyTapDevice(char *name) { } @@ -809,7 +830,7 @@ void UnixVLanDelete(char *name) { UnixCloseTapDevice(t->fd); #ifdef UNIX_BSD - UnixDestroyTapDevice(t->Name); + UnixDestroyClientTapDevice(t->Name); #endif Delete(unix_vlan, t); Free(t); @@ -858,7 +879,7 @@ void UnixVLanFree() UnixCloseTapDevice(t->fd); #ifdef UNIX_BSD - UnixDestroyTapDevice(t->Name); + UnixDestroyClientTapDevice(t->Name); #endif Free(t); } diff --git a/src/Cedar/VLanUnix.h b/src/Cedar/VLanUnix.h index 714b746a..faac5b3c 100644 --- a/src/Cedar/VLanUnix.h +++ b/src/Cedar/VLanUnix.h @@ -31,9 +31,9 @@ struct VLAN // Function prototype VLAN *NewVLan(char *instance_name, VLAN_PARAM *param); -VLAN *NewTap(char *name, char *mac_address, bool create_up); +VLAN *NewBridgeTap(char *name, char *mac_address, bool create_up); void FreeVLan(VLAN *v); -void FreeTap(VLAN *v); +void FreeBridgeTap(VLAN *v); CANCEL *VLanGetCancel(VLAN *v); bool VLanGetNextPacket(VLAN *v, void **buf, UINT *size); bool VLanPutPacket(VLAN *v, void *buf, UINT size); @@ -60,7 +60,8 @@ struct UNIX_VLAN_LIST int UnixCreateTapDevice(char *name, UCHAR *mac_address, bool create_up); int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up); void UnixCloseTapDevice(int fd); -void UnixDestroyTapDevice(char *name); +void UnixDestroyBridgeTapDevice(char *name); +void UnixDestroyClientTapDevice(char *name); void UnixVLanInit(); void UnixVLanFree(); bool UnixVLanCreate(char *name, UCHAR *mac_address, bool create_up); From 88264842454cb775d41c4400b3370934c43fc382 Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Wed, 14 Jun 2023 20:56:01 +0900 Subject: [PATCH 3/5] Rename macro BRDEST -> BRIDGE for simplicity UNIX_VLAN_BRDEST_IFACE_PREFIX -> UNIX_VLAN_BRIDGE_IFACE_PREFIX --- src/Cedar/BridgeUnix.c | 2 +- src/Cedar/Cedar.h | 2 +- src/Cedar/SM.c | 2 +- src/Cedar/VLanUnix.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cedar/BridgeUnix.c b/src/Cedar/BridgeUnix.c index 5c1d0646..b9c29039 100644 --- a/src/Cedar/BridgeUnix.c +++ b/src/Cedar/BridgeUnix.c @@ -321,7 +321,7 @@ TOKEN_LIST *GetEthListLinux(bool enum_normal, bool enum_rawip) { if (IsInListStr(o, name) == false) { - if (StartWith(name, UNIX_VLAN_BRDEST_IFACE_PREFIX"_") == false) + if (StartWith(name, UNIX_VLAN_BRIDGE_IFACE_PREFIX"_") == false) { Add(o, CopyStr(name)); } diff --git a/src/Cedar/Cedar.h b/src/Cedar/Cedar.h index 5ce8afa0..097b6a4c 100644 --- a/src/Cedar/Cedar.h +++ b/src/Cedar/Cedar.h @@ -676,7 +676,7 @@ ////////////////////////////////////////////////////////////////////// #define UNIX_VLAN_CLIENT_IFACE_PREFIX "vpn" // Prefix of UNIX virtual LAN card interface (used for client) -#define UNIX_VLAN_BRDEST_IFACE_PREFIX "tap" // Prefix of UNIX virtual LAN card interface (used for bridge destination) +#define UNIX_VLAN_BRIDGE_IFACE_PREFIX "tap" // Prefix of UNIX virtual LAN card interface (used for bridge destination) #ifndef UNIX_BSD #define TAP_FILENAME_1 "/dev/net/tun" diff --git a/src/Cedar/SM.c b/src/Cedar/SM.c index 41c17700..b76343f3 100644 --- a/src/Cedar/SM.c +++ b/src/Cedar/SM.c @@ -7988,7 +7988,7 @@ void SmBridgeDlgOnOk(HWND hWnd, SM_SERVER *s) t.TapMode = tapmode; if (InStrEx(t.DeviceName, UNIX_VLAN_CLIENT_IFACE_PREFIX, false) - || InStrEx(t.DeviceName, "UNIX_VLAN_BRDEST_IFACE_PREFIX, false) + || InStrEx(t.DeviceName, "UNIX_VLAN_BRIDGE_IFACE_PREFIX, false) || InStrEx(t.DeviceName, "tun", false) || InStrEx(t.DeviceName, "tap", false)) { diff --git a/src/Cedar/VLanUnix.c b/src/Cedar/VLanUnix.c index ae7e93e8..80174209 100644 --- a/src/Cedar/VLanUnix.c +++ b/src/Cedar/VLanUnix.c @@ -273,7 +273,7 @@ VLAN *NewBridgeTap(char *name, char *mac_address, bool create_up) return NULL; } - fd = UnixCreateTapDeviceEx(name, UNIX_VLAN_BRDEST_IFACE_PREFIX, mac_address, create_up); + fd = UnixCreateTapDeviceEx(name, UNIX_VLAN_BRIDGE_IFACE_PREFIX, mac_address, create_up); if (fd == -1) { return NULL; @@ -611,7 +611,7 @@ void UnixDestroyTapDeviceEx(char *name, char *prefix) void UnixDestroyBridgeTapDevice(char *name) { #ifdef UNIX_BSD - UnixDestroyTapDeviceEx(name, UNIX_VLAN_BRDEST_IFACE_PREFIX); + UnixDestroyTapDeviceEx(name, UNIX_VLAN_BRIDGE_IFACE_PREFIX); #endif // UNIX_BSD } From 6665efb8221f3bc24ddc7e2e4f6817631d09299d Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Thu, 15 Jun 2023 10:17:33 +0900 Subject: [PATCH 4/5] Remove unnecessary quotation to fix build --- src/Cedar/SM.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cedar/SM.c b/src/Cedar/SM.c index b76343f3..b7651940 100644 --- a/src/Cedar/SM.c +++ b/src/Cedar/SM.c @@ -7988,7 +7988,7 @@ void SmBridgeDlgOnOk(HWND hWnd, SM_SERVER *s) t.TapMode = tapmode; if (InStrEx(t.DeviceName, UNIX_VLAN_CLIENT_IFACE_PREFIX, false) - || InStrEx(t.DeviceName, "UNIX_VLAN_BRIDGE_IFACE_PREFIX, false) + || InStrEx(t.DeviceName, UNIX_VLAN_BRIDGE_IFACE_PREFIX, false) || InStrEx(t.DeviceName, "tun", false) || InStrEx(t.DeviceName, "tap", false)) { From 41be858df097eed8bc9a71952bc0eadcdc4ebe63 Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Sat, 17 Jun 2023 01:54:36 +0900 Subject: [PATCH 5/5] Collect garbage at development --- src/Cedar/VLanUnix.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Cedar/VLanUnix.c b/src/Cedar/VLanUnix.c index 80174209..b59fe2e0 100644 --- a/src/Cedar/VLanUnix.c +++ b/src/Cedar/VLanUnix.c @@ -624,17 +624,18 @@ void UnixDestroyClientTapDevice(char *name) #else // NO_VLAN -void UnixCloseBridgeTapDevice(int fd) +void UnixCloseDevice(int fd) { } -void UnixCloseClientTapDevice(int fd) -{ - void UnixDestroyTapDevice(char *name) { } +void UnixDestroyTapDeviceEx(char *name, char *prefix) +{ +} + int UnixCreateTapDeviceEx(char *name, char *prefix, UCHAR *mac_address, bool create_up) { return -1;