Skip to content

Commit 6005c57

Browse files
kayruszx2c4
authored andcommitted
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 <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 82f3e9e commit 6005c57

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

tun/tun_freebsd.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -346,22 +346,24 @@ func CreateTUN(name string, mtu int) (Device, error) {
346346
return nil, fmt.Errorf("Unable to set nd6 flags for %s: %w", assignedName, errno)
347347
}
348348

349-
// Rename the interface
350-
var newnp [unix.IFNAMSIZ]byte
351-
copy(newnp[:], name)
352-
var ifr ifreq_ptr
353-
copy(ifr.Name[:], assignedName)
354-
ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
355-
_, _, errno = unix.Syscall(
356-
unix.SYS_IOCTL,
357-
uintptr(confd),
358-
uintptr(unix.SIOCSIFNAME),
359-
uintptr(unsafe.Pointer(&ifr)),
360-
)
361-
if errno != 0 {
362-
tunFile.Close()
363-
tunDestroy(assignedName)
364-
return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
349+
if name != "" {
350+
// Rename the interface
351+
var newnp [unix.IFNAMSIZ]byte
352+
copy(newnp[:], name)
353+
var ifr ifreq_ptr
354+
copy(ifr.Name[:], assignedName)
355+
ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
356+
_, _, errno = unix.Syscall(
357+
unix.SYS_IOCTL,
358+
uintptr(confd),
359+
uintptr(unix.SIOCSIFNAME),
360+
uintptr(unsafe.Pointer(&ifr)),
361+
)
362+
if errno != 0 {
363+
tunFile.Close()
364+
tunDestroy(assignedName)
365+
return nil, fmt.Errorf("Failed to rename %s to %s: %w", assignedName, name, errno)
366+
}
365367
}
366368

367369
return CreateTUNFromFile(tunFile, mtu)

0 commit comments

Comments
 (0)