Skip to contentSkip to conversation

FreeBSD default tun name patch

PATCH In review kayrus 3 participants 4 messages Mar 7, 2021 – Mar 9, 2021

lore
Share & tools
lore

kayrus

author
#0 lore raw

PATCH 1/1 FreeBSD default tun name patch

This change allows to omit the tun interface name setting in FreeBSD. When name is not set, kernel automatically picks up the tun name and index.

---
 tun/tun_freebsd.go | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
tun/tun_freebsd.go +18 -16
index 12b44da..1883aad 100644
@@ -346,22 +346,24 @@ func CreateTUN(name string, mtu int) (Device, error) {
 		return nil, fmt.Errorf("Unable to set nd6 flags for %s: %w", assignedName, errno)
 	}
 
-	// Rename the interface
-	var newnp [unix.IFNAMSIZ]byte
-	copy(newnp[:], name)
-	var ifr ifreq_ptr
-	copy(ifr.Name[:], assignedName)
-	ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
-	_, _, errno = unix.Syscall(
-		unix.SYS_IOCTL,
-		uintptr(confd),
-		uintptr(unix.SIOCSIFNAME),
-		uintptr(unsafe.Pointer(&ifr)),
-	)
-	if errno != 0 {
-		tunFile.Close()
-		tunDestroy(assignedName)
-		return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+	if name != "" {
+		// Rename the interface
+		var newnp [unix.IFNAMSIZ]byte
+		copy(newnp[:], name)
+		var ifr ifreq_ptr
+		copy(ifr.Name[:], assignedName)
+		ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
+		_, _, errno = unix.Syscall(
+			unix.SYS_IOCTL,
+			uintptr(confd),
+			uintptr(unix.SIOCSIFNAME),
+			uintptr(unsafe.Pointer(&ifr)),
+		)
+		if errno != 0 {
+			tunFile.Close()
+			tunDestroy(assignedName)
+			return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+		}
 	}
 
 	return CreateTUNFromFile(tunFile, mtu)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/1 FreeBSD default tun name patch

This change allows to omit the tun interface name setting in FreeBSD. When name is not set, kernel automatically picks up the tun name and index.

---
 tun/tun_freebsd.go | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
tun/tun_freebsd.go +18 -16
index 12b44da..1883aad 100644
@@ -346,22 +346,24 @@ func CreateTUN(name string, mtu int) (Device, error) {
 		return nil, fmt.Errorf("Unable to set nd6 flags for %s: %w", assignedName, errno)
 	}
 
-	// Rename the interface
-	var newnp [unix.IFNAMSIZ]byte
-	copy(newnp[:], name)
-	var ifr ifreq_ptr
-	copy(ifr.Name[:], assignedName)
-	ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
-	_, _, errno = unix.Syscall(
-		unix.SYS_IOCTL,
-		uintptr(confd),
-		uintptr(unix.SIOCSIFNAME),
-		uintptr(unsafe.Pointer(&ifr)),
-	)
-	if errno != 0 {
-		tunFile.Close()
-		tunDestroy(assignedName)
-		return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+	if name != "" {
+		// Rename the interface
+		var newnp [unix.IFNAMSIZ]byte
+		copy(newnp[:], name)
+		var ifr ifreq_ptr
+		copy(ifr.Name[:], assignedName)
+		ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
+		_, _, errno = unix.Syscall(
+			unix.SYS_IOCTL,
+			uintptr(confd),
+			uintptr(unix.SIOCSIFNAME),
+			uintptr(unsafe.Pointer(&ifr)),
+		)
+		if errno != 0 {
+			tunFile.Close()
+			tunDestroy(assignedName)
+			return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+		}
 	}
 
 	return CreateTUNFromFile(tunFile, mtu)
Signature · 1 line
2.25.1
#3 lore raw
In reply to kayrus
Quoted 7 lines · kayrus

This change allows to omit the tun interface name setting in FreeBSD. When name is not set, kernel automatically picks up the tun name and index.

---
 tun/tun_freebsd.go | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

Hi,

I'm a little confused by what's going on here -- folks are wanting to use the assigned name from the tuntap cloner, or is it just a desire to not require that a new name be specified? The latter seems incompatible with how the Linux counterpart works, but perhaps Linux doesn't assign a name/unit when opening /dev/net/tun like we do.

Thanks,

Kyle Evans

Quoted 48 lines
tun/tun_freebsd.go +18 -17
index 12b44da..1883aad 100644
@@ -346,22 +346,24 @@ func CreateTUN(name string, mtu int) (Device, error) {
                return nil, fmt.Errorf("Unable to set nd6 flags for %s: %w", assignedName, errno)
        }
 
-       // Rename the interface
-       var newnp [unix.IFNAMSIZ]byte
-       copy(newnp[:], name)
-       var ifr ifreq_ptr
-       copy(ifr.Name[:], assignedName)
-       ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
-       _, _, errno = unix.Syscall(
-               unix.SYS_IOCTL,
-               uintptr(confd),
-               uintptr(unix.SIOCSIFNAME),
-               uintptr(unsafe.Pointer(&ifr)),
-       )
-       if errno != 0 {
-               tunFile.Close()
-               tunDestroy(assignedName)
-               return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+       if name != "" {
+               // Rename the interface
+               var newnp [unix.IFNAMSIZ]byte
+               copy(newnp[:], name)
+               var ifr ifreq_ptr
+               copy(ifr.Name[:], assignedName)
+               ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
+               _, _, errno = unix.Syscall(
+                       unix.SYS_IOCTL,
+                       uintptr(confd),
+                       uintptr(unix.SIOCSIFNAME),
+                       uintptr(unsafe.Pointer(&ifr)),
+               )
+               if errno != 0 {
+                       tunFile.Close()
+                       tunDestroy(assignedName)
+                       return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
+               }
        }
 
        return CreateTUNFromFile(tunFile, mtu)
--

2.25.1