-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarwin.go
260 lines (220 loc) · 5.87 KB
/
darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//go:build darwin
package swiftunnel
import (
"errors"
"fmt"
"github.com/XenonCommunity/swiftunnel/swiftypes"
"math"
"os"
"strconv"
"strings"
"sync"
"syscall"
"unsafe"
)
type SwiftInterface struct {
*tunReadCloser
name string
AdapterType swiftypes.AdapterType
}
const (
appleUTUNCtl = "com.apple.net.utun_control"
appleCTLIOCGINFO = (0x40000000 | 0x80000000) | ((100 & 0x1fff) << 16) | uint32(byte('N'))<<8 | 3
sockaddrCtlSize = 32
maxAdapterNameLen = 15
)
type sockaddrCtl struct {
scLen uint8
scFamily uint8
ssSysaddr uint16
scID uint32
scUnit uint32
scReserved [5]uint32
}
func openDevSystem(config Config) (*SwiftInterface, error) {
if config.AdapterType != swiftypes.AdapterTypeTUN {
return nil, errors.New("only TUN is supported for SystemDriver; use TunTapOSXDriver for TAP")
}
ifIndex, err := parseUtunIndex(config.AdapterName)
if err != nil {
return nil, err
}
fd, err := syscall.Socket(syscall.AF_SYSTEM, syscall.SOCK_DGRAM, 2)
if err != nil {
return nil, fmt.Errorf("error creating socket: %w", err)
}
defer func(fd int) {
_ = syscall.Close(fd)
}(fd)
ctlInfo := &struct {
ctlID uint32
ctlName [96]byte
}{}
copy(ctlInfo.ctlName[:], appleUTUNCtl)
if err := ioctl(uintptr(fd), uintptr(appleCTLIOCGINFO), uintptr(unsafe.Pointer(ctlInfo))); err != nil {
return nil, fmt.Errorf("error in ioctl call: %w", err)
}
sockAddr := &sockaddrCtl{
scLen: sockaddrCtlSize,
scFamily: syscall.AF_SYSTEM,
ssSysaddr: 2,
scID: ctlInfo.ctlID,
scUnit: uint32(ifIndex) + 1,
}
if err := connect(fd, sockAddr); err != nil {
return nil, fmt.Errorf("error connecting to socket: %w", err)
}
ifName, err := getIfName(fd)
if err != nil {
return nil, fmt.Errorf("error getting interface name: %w", err)
}
return &SwiftInterface{
name: ifName,
AdapterType: config.AdapterType,
tunReadCloser: &tunReadCloser{
f: os.NewFile(uintptr(fd), ifName),
},
}, nil
}
func parseUtunIndex(name string) (int, error) {
const utunPrefix = "utun"
if !strings.HasPrefix(name, utunPrefix) {
return -1, errors.New("interface name must be in the format utun[0-9]+")
}
index, err := strconv.Atoi(name[len(utunPrefix):])
if err != nil || index < 0 || index > math.MaxUint32-1 {
return -1, fmt.Errorf("invalid interface index in name: %w", err)
}
return index, nil
}
func openDevTunTapOSX(config Config) (*SwiftInterface, error) {
if len(config.AdapterName) >= maxAdapterNameLen {
return nil, errors.New("device name is too long")
}
prefix := map[swiftypes.AdapterType]string{
swiftypes.AdapterTypeTUN: "tun",
swiftypes.AdapterTypeTAP: "tap",
}
if expected, ok := prefix[config.AdapterType]; !ok || !strings.HasPrefix(config.AdapterName, expected) {
return nil, fmt.Errorf("device name must start with %s", expected)
}
fd, err := syscall.Open("/dev/"+config.AdapterName, os.O_RDWR|syscall.O_NONBLOCK, 0)
if err != nil {
return nil, fmt.Errorf("error opening device: %w", err)
}
if err := setIfUp(uintptr(fd), config.AdapterName); err != nil {
_ = syscall.Close(fd)
return nil, err
}
return &SwiftInterface{
name: config.AdapterName,
AdapterType: config.AdapterType,
tunReadCloser: &tunReadCloser{
f: os.NewFile(uintptr(fd), config.AdapterName),
},
}, nil
}
func connect(fd int, addr *sockaddrCtl) error {
_, _, errno := syscall.RawSyscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(unsafe.Pointer(addr)), sockaddrCtlSize)
if errno != 0 {
return errno
}
return nil
}
func getIfName(fd int) (string, error) {
var ifName [16]byte
ifNameSize := uintptr(len(ifName))
_, _, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT, uintptr(fd), 2, 2, uintptr(unsafe.Pointer(&ifName)), uintptr(unsafe.Pointer(&ifNameSize)), 0)
if errno != 0 {
return "", errno
}
return string(ifName[:ifNameSize-1]), nil
}
func setIfUp(fd uintptr, ifName string) error {
var ifReq = struct {
ifName [16]byte
ifruFlags int16
pad [16]byte
}{}
copy(ifReq.ifName[:], ifName)
ifReq.ifruFlags |= syscall.IFF_RUNNING | syscall.IFF_UP
return ioctl(fd, syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifReq)))
}
type tunReadCloser struct {
f *os.File
rMu sync.Mutex
rBuf []byte
wMu sync.Mutex
wBuf []byte
}
func (t *tunReadCloser) Read(to []byte) (int, error) {
t.rMu.Lock()
defer t.rMu.Unlock()
if cap(t.rBuf) < len(to)+4 {
t.rBuf = make([]byte, len(to)+4)
}
t.rBuf = t.rBuf[:len(to)+4]
n, err := t.f.Read(t.rBuf)
copy(to, t.rBuf[4:])
return n - 4, err
}
func (t *tunReadCloser) Write(from []byte) (int, error) {
if len(from) == 0 {
return 0, syscall.EIO
}
t.wMu.Lock()
defer t.wMu.Unlock()
if cap(t.wBuf) < len(from)+4 {
t.wBuf = make([]byte, len(from)+4)
}
t.wBuf = t.wBuf[:len(from)+4]
switch ipVer := from[0] >> 4; ipVer {
case 4:
t.wBuf[3] = syscall.AF_INET
case 6:
t.wBuf[3] = syscall.AF_INET6
default:
return 0, errors.New("invalid IP version")
}
copy(t.wBuf[4:], from)
n, err := t.f.Write(t.wBuf)
return n - 4, err
}
func (t *tunReadCloser) Close() error {
return t.f.Close()
}
func (a *SwiftInterface) GetFD() *os.File {
return a.tunReadCloser.f
}
func NewSwiftInterface(config Config) (*SwiftInterface, error) {
switch config.DriverType {
case DriverTypeTunTapOSX:
tapOSX, err := openDevTunTapOSX(config)
if config.UnicastConfig == nil {
if err = tapOSX.SetUnicastIpAddressEntry(config.UnicastConfig); err != nil {
return nil, err
}
}
if config.MTU > 0 {
if err = tapOSX.SetMTU(config.MTU); err != nil {
return nil, err
}
}
return tapOSX, err
case DriverTypeSystem:
system, err := openDevSystem(config)
if config.UnicastConfig == nil {
if err = system.SetUnicastIpAddressEntry(config.UnicastConfig); err != nil {
return nil, err
}
}
if config.MTU > 0 {
if err = system.SetMTU(config.MTU); err != nil {
return nil, err
}
}
return system, err
default:
return nil, errors.New("unrecognized driver")
}
}