|
| 1 | +package tun |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "unsafe" |
| 7 | + |
| 8 | + "golang.org/x/sys/unix" |
| 9 | +) |
| 10 | + |
| 11 | +// utun configuration constants for macOS |
| 12 | +const ( |
| 13 | + AF_SYSTEM = 32 |
| 14 | + SYSPROTO_CONTROL = 2 |
| 15 | + AF_SYS_CONTROL = 2 |
| 16 | + UTUN_CONTROL_NAME = "com.apple.net.utun_control" |
| 17 | +) |
| 18 | + |
| 19 | +// utunInterface implements Interface for Darwin using AF_SYSTEM. |
| 20 | +type utunInterface struct { |
| 21 | + f *os.File |
| 22 | + name string |
| 23 | +} |
| 24 | + |
| 25 | +func (i *utunInterface) Read(p []byte) (n int, err error) { |
| 26 | + return i.f.Read(p) |
| 27 | +} |
| 28 | + |
| 29 | +func (i *utunInterface) Write(p []byte) (n int, err error) { |
| 30 | + return i.f.Write(p) |
| 31 | +} |
| 32 | + |
| 33 | +func (i *utunInterface) Close() error { |
| 34 | + return i.f.Close() |
| 35 | +} |
| 36 | + |
| 37 | +func (i *utunInterface) Name() string { |
| 38 | + return i.name |
| 39 | +} |
| 40 | + |
| 41 | +// OpenUTUN creates a new utun interface on macOS. |
| 42 | +// If index is 0, the system chooses the first available (utun0, utun1, etc.). |
| 43 | +func OpenUTUN(index int) (Interface, error) { |
| 44 | + fd, err := unix.Socket(AF_SYSTEM, unix.SOCK_DGRAM, SYSPROTO_CONTROL) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("utun: failed to open system socket: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + // 1. Find the control ID for "com.apple.net.utun_control" |
| 50 | + info := struct { |
| 51 | + ctl_id uint32 |
| 52 | + ctl_name [96]byte |
| 53 | + }{} |
| 54 | + copy(info.ctl_name[:], UTUN_CONTROL_NAME) |
| 55 | + |
| 56 | + // CTLIOCGINFO |
| 57 | + err = ioctl(fd, 0xc0644e03, unsafe.Pointer(&info)) |
| 58 | + if err != nil { |
| 59 | + unix.Close(fd) |
| 60 | + return nil, fmt.Errorf("utun: failed to get utun control info: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // 2. Connect to the utun control |
| 64 | + sc := struct { |
| 65 | + sc_len uint8 |
| 66 | + sc_family uint8 |
| 67 | + ss_sysaddr uint16 |
| 68 | + sc_id uint32 |
| 69 | + sc_unit uint32 |
| 70 | + sc_reserved [5]uint32 |
| 71 | + }{ |
| 72 | + sc_len: 32, |
| 73 | + sc_family: AF_SYSTEM, |
| 74 | + ss_sysaddr: AF_SYS_CONTROL, |
| 75 | + sc_id: info.ctl_id, |
| 76 | + sc_unit: uint32(index), // 0 = automatic |
| 77 | + } |
| 78 | + |
| 79 | + err = connect(fd, unsafe.Pointer(&sc), 32) |
| 80 | + if err != nil { |
| 81 | + unix.Close(fd) |
| 82 | + return nil, fmt.Errorf("utun: failed to connect to utun control: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + // 3. Get the interface name (e.g., utun3) |
| 86 | + nameBuf := make([]byte, 64) |
| 87 | + nameLen := uint32(len(nameBuf)) |
| 88 | + // UTUN_OPT_IFNAME (Option 2) |
| 89 | + err = getsockopt(fd, SYSPROTO_CONTROL, 2, unsafe.Pointer(&nameBuf[0]), &nameLen) |
| 90 | + if err != nil { |
| 91 | + unix.Close(fd) |
| 92 | + return nil, fmt.Errorf("utun: failed to get interface name: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + ifname := string(nameBuf[:nameLen-1]) // trim null byte |
| 96 | + return &utunInterface{ |
| 97 | + f: os.NewFile(uintptr(fd), ifname), |
| 98 | + name: ifname, |
| 99 | + }, nil |
| 100 | +} |
| 101 | + |
| 102 | +// Wrapper for unix.Ioctl |
| 103 | +func ioctl(fd int, request uintptr, argp unsafe.Pointer) error { |
| 104 | + _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), request, uintptr(argp)) |
| 105 | + if errno != 0 { |
| 106 | + return errno |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// Wrapper for unix.Connect |
| 112 | +func connect(fd int, addr unsafe.Pointer, len uint32) error { |
| 113 | + _, _, errno := unix.Syscall(unix.SYS_CONNECT, uintptr(fd), uintptr(addr), uintptr(len)) |
| 114 | + if errno != 0 { |
| 115 | + return errno |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// Wrapper for unix.Getsockopt |
| 121 | +func getsockopt(fd int, level, name int, val unsafe.Pointer, len *uint32) error { |
| 122 | + _, _, errno := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(fd), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(len)), 0) |
| 123 | + if errno != 0 { |
| 124 | + return errno |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
0 commit comments