Skip to content

Commit

Permalink
Use SyscallConn() instead of Fd() to persist TUN/TAP interface
Browse files Browse the repository at this point in the history
os.File.Fd() puts back the file descriptor in blocking mode which is
pretty annoying as SetDeadline will stop working
  • Loading branch information
montag451 committed Sep 23, 2021
1 parent d5e9ae8 commit d6fe711
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,9 +1260,26 @@ func (h *Handle) linkModify(link Link, flags int) error {

}

control := func(file *os.File, f func(fd uintptr)) error {
name := file.Name()
conn, err := file.SyscallConn()
if err != nil {
return fmt.Errorf("SyscallConn() failed on %s: %v", name, err)
}
if err := conn.Control(f); err != nil {
return fmt.Errorf("Failed to get file descriptor for %s: %v", name, err)
}
return nil
}

// only persist interface if NonPersist is NOT set
if !tuntap.NonPersist {
_, _, errno := unix.Syscall(unix.SYS_IOCTL, fds[0].Fd(), uintptr(unix.TUNSETPERSIST), 1)
var errno syscall.Errno
if err := control(fds[0], func(fd uintptr) {
_, _, errno = unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TUNSETPERSIST), 1)
}); err != nil {
return err
}
if errno != 0 {
cleanupFds(fds)
return fmt.Errorf("Tuntap IOCTL TUNSETPERSIST failed, errno %v", errno)
Expand All @@ -1279,7 +1296,10 @@ func (h *Handle) linkModify(link Link, flags int) error {
// un-persist (e.g. allow the interface to be removed) the tuntap
// should not hurt if not set prior, condition might be not needed
if !tuntap.NonPersist {
_, _, _ = unix.Syscall(unix.SYS_IOCTL, fds[0].Fd(), uintptr(unix.TUNSETPERSIST), 0)
// ignore error
_ = control(fds[0], func(fd uintptr) {
_, _, _ = unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TUNSETPERSIST), 0)
})
}
cleanupFds(fds)
return err
Expand Down

0 comments on commit d6fe711

Please sign in to comment.