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) 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
} }