tun: use x/sys/unix IoctlIfreq/NewIfreq to set MTU on Linux
The manual struct packing was suspect: https://github.com/tailscale/tailscale/issues/11899 And no need for doing it manually if there's API for it already. Updates tailscale/tailscale#11899 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
799c1978fa
commit
4e883d38c8
1 changed files with 8 additions and 14 deletions
|
|
@ -269,21 +269,15 @@ func (tun *NativeTun) setMTU(n int) error {
|
||||||
|
|
||||||
defer unix.Close(fd)
|
defer unix.Close(fd)
|
||||||
|
|
||||||
// do ioctl call
|
req, err := unix.NewIfreq(name)
|
||||||
var ifr [ifReqSize]byte
|
if err != nil {
|
||||||
copy(ifr[:], name)
|
return fmt.Errorf("unix.NewIfreq(%q): %w", name, err)
|
||||||
*(*uint32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = uint32(n)
|
}
|
||||||
_, _, errno := unix.Syscall(
|
req.SetUint32(uint32(n))
|
||||||
unix.SYS_IOCTL,
|
err = unix.IoctlIfreq(fd, unix.SIOCSIFMTU, req)
|
||||||
uintptr(fd),
|
if err != nil {
|
||||||
uintptr(unix.SIOCSIFMTU),
|
return fmt.Errorf("failed to set MTU of TUN device %q: %w", name, err)
|
||||||
uintptr(unsafe.Pointer(&ifr[0])),
|
|
||||||
)
|
|
||||||
|
|
||||||
if errno != 0 {
|
|
||||||
return fmt.Errorf("failed to set MTU of TUN device: %w", errno)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue