Skip to content

Commit

Permalink
tun: freebsd: allow empty names
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
kayrus authored and zx2c4 committed Mar 9, 2021
1 parent 5aa17ce commit 6b5293b
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions tun/tun_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6b5293b

Please sign in to comment.