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:
Brad Fitzpatrick 2024-11-12 16:53:55 -08:00 committed by Brad Fitzpatrick
parent 799c1978fa
commit 4e883d38c8

View file

@ -269,21 +269,15 @@ func (tun *NativeTun) setMTU(n int) error {
defer unix.Close(fd)
// do ioctl call
var ifr [ifReqSize]byte
copy(ifr[:], name)
*(*uint32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = uint32(n)
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(unix.SIOCSIFMTU),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
return fmt.Errorf("failed to set MTU of TUN device: %w", errno)
req, err := unix.NewIfreq(name)
if err != nil {
return fmt.Errorf("unix.NewIfreq(%q): %w", name, err)
}
req.SetUint32(uint32(n))
err = unix.IoctlIfreq(fd, unix.SIOCSIFMTU, req)
if err != nil {
return fmt.Errorf("failed to set MTU of TUN device %q: %w", name, err)
}
return nil
}