Skip to content

Commit 4e883d3

Browse files
committed
tun: use x/sys/unix IoctlIfreq/NewIfreq to set MTU on Linux
The manual struct packing was suspect: tailscale/tailscale#11899 And no need for doing it manually if there's API for it already. Updates tailscale/tailscale#11899 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 799c197 commit 4e883d3

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

tun/tun_linux.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,15 @@ func (tun *NativeTun) setMTU(n int) error {
269269

270270
defer unix.Close(fd)
271271

272-
// do ioctl call
273-
var ifr [ifReqSize]byte
274-
copy(ifr[:], name)
275-
*(*uint32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = uint32(n)
276-
_, _, errno := unix.Syscall(
277-
unix.SYS_IOCTL,
278-
uintptr(fd),
279-
uintptr(unix.SIOCSIFMTU),
280-
uintptr(unsafe.Pointer(&ifr[0])),
281-
)
282-
283-
if errno != 0 {
284-
return fmt.Errorf("failed to set MTU of TUN device: %w", errno)
272+
req, err := unix.NewIfreq(name)
273+
if err != nil {
274+
return fmt.Errorf("unix.NewIfreq(%q): %w", name, err)
275+
}
276+
req.SetUint32(uint32(n))
277+
err = unix.IoctlIfreq(fd, unix.SIOCSIFMTU, req)
278+
if err != nil {
279+
return fmt.Errorf("failed to set MTU of TUN device %q: %w", name, err)
285280
}
286-
287281
return nil
288282
}
289283

0 commit comments

Comments
 (0)