■ ■ ■ ■ ■ ■
core/device/tun/tun_netstack.go
| skipped 3 lines |
4 | 4 | | |
5 | 5 | | import ( |
6 | 6 | | "fmt" |
7 | | - | "unsafe" |
8 | 7 | | |
9 | 8 | | "golang.org/x/sys/unix" |
10 | 9 | | "gvisor.dev/gvisor/pkg/tcpip/link/fdbased" |
| skipped 70 lines |
81 | 80 | | return unix.Close(t.fd) |
82 | 81 | | } |
83 | 82 | | |
84 | | - | // Ref: wireguard tun/tun_linux.go setMTU. |
85 | 83 | | func setMTU(name string, n uint32) error { |
86 | 84 | | // open datagram socket |
87 | 85 | | fd, err := unix.Socket( |
| skipped 7 lines |
95 | 93 | | |
96 | 94 | | defer unix.Close(fd) |
97 | 95 | | |
98 | | - | const ifReqSize = unix.IFNAMSIZ + 64 |
99 | | - | |
100 | | - | // do ioctl call |
101 | | - | var ifr [ifReqSize]byte |
102 | | - | copy(ifr[:], name) |
103 | | - | *(*uint32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = n |
104 | | - | _, _, errno := unix.Syscall( |
105 | | - | unix.SYS_IOCTL, |
106 | | - | uintptr(fd), |
107 | | - | uintptr(unix.SIOCSIFMTU), |
108 | | - | uintptr(unsafe.Pointer(&ifr[0])), |
109 | | - | ) |
110 | | - | |
111 | | - | if errno != 0 { |
112 | | - | return fmt.Errorf("failed to set MTU: %w", errno) |
| 96 | + | ifr, err := unix.NewIfreq(name) |
| 97 | + | if err != nil { |
| 98 | + | return err |
113 | 99 | | } |
114 | | - | |
115 | | - | return nil |
| 100 | + | ifr.SetUint32(n) |
| 101 | + | return unix.IoctlIfreq(fd, unix.SIOCSIFMTU, ifr) |
116 | 102 | | } |
117 | 103 | | |