tun: freebsd: allow empty names

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

Signed-off-by: Kay Diam <kay.diam@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Kay Diam 2021-03-07 17:21:31 +01:00 committed by Jason A. Donenfeld
parent 82f3e9e2af
commit 6005c573e2

View File

@ -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) return nil, fmt.Errorf("Unable to set nd6 flags for %s: %w", assignedName, errno)
} }
// Rename the interface if name != "" {
var newnp [unix.IFNAMSIZ]byte // Rename the interface
copy(newnp[:], name) var newnp [unix.IFNAMSIZ]byte
var ifr ifreq_ptr copy(newnp[:], name)
copy(ifr.Name[:], assignedName) var ifr ifreq_ptr
ifr.Data = uintptr(unsafe.Pointer(&newnp[0])) copy(ifr.Name[:], assignedName)
_, _, errno = unix.Syscall( ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
unix.SYS_IOCTL, _, _, errno = unix.Syscall(
uintptr(confd), unix.SYS_IOCTL,
uintptr(unix.SIOCSIFNAME), uintptr(confd),
uintptr(unsafe.Pointer(&ifr)), uintptr(unix.SIOCSIFNAME),
) uintptr(unsafe.Pointer(&ifr)),
if errno != 0 { )
tunFile.Close() if errno != 0 {
tunDestroy(assignedName) tunFile.Close()
return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno) tunDestroy(assignedName)
return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
}
} }
return CreateTUNFromFile(tunFile, mtu) return CreateTUNFromFile(tunFile, mtu)