tun: use IoctlCtlInfo from golang.org/x/sys/unix on macOS

Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on
macOS and might not be supported in future versions. Switch to use
unix.IoctlCtlInfo to get the kernel control info.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Tobias Klauser 2020-10-27 14:39:34 +01:00 committed by Jason A. Donenfeld
parent 36dc8b6994
commit 8ae09213a7

View file

@ -20,9 +20,6 @@ import (
const utunControlName = "com.apple.net.utun_control" const utunControlName = "com.apple.net.utun_control"
// _CTLIOCGINFO value derived from /usr/include/sys/{kern_control,ioccom}.h
const _CTLIOCGINFO = (0x40000000 | 0x80000000) | ((100 & 0x1fff) << 16) | uint32(byte('N'))<<8 | 3
// sockaddr_ctl specifeid in /usr/include/sys/kern_control.h // sockaddr_ctl specifeid in /usr/include/sys/kern_control.h
type sockaddrCtl struct { type sockaddrCtl struct {
scLen uint8 scLen uint8
@ -130,29 +127,18 @@ func CreateTUN(name string, mtu int) (Device, error) {
return nil, err return nil, err
} }
var ctlInfo = &struct { ctlInfo := &unix.CtlInfo{}
ctlID uint32 copy(ctlInfo.Name[:], []byte(utunControlName))
ctlName [96]byte err = unix.IoctlCtlInfo(fd, ctlInfo)
}{} if err != nil {
return nil, fmt.Errorf("IoctlGetCtlInfo: %w", err)
copy(ctlInfo.ctlName[:], []byte(utunControlName))
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(_CTLIOCGINFO),
uintptr(unsafe.Pointer(ctlInfo)),
)
if errno != 0 {
return nil, fmt.Errorf("_CTLIOCGINFO: %v", errno)
} }
sc := sockaddrCtl{ sc := sockaddrCtl{
scLen: uint8(sockaddrCtlSize), scLen: uint8(sockaddrCtlSize),
scFamily: unix.AF_SYSTEM, scFamily: unix.AF_SYSTEM,
ssSysaddr: 2, ssSysaddr: 2,
scID: ctlInfo.ctlID, scID: ctlInfo.Id,
scUnit: uint32(ifIndex) + 1, scUnit: uint32(ifIndex) + 1,
} }