-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscalls_darwin.go
547 lines (465 loc) · 13.1 KB
/
syscalls_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package water
import (
"errors"
"fmt"
"io"
"math"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
"unsafe"
"github.com/Doridian/gopacket/bsdbpf"
)
const appleUTUNCtl = "com.apple.net.utun_control"
/*
* From ioctl.h:
* #define IOCPARM_MASK 0x1fff // parameter length, at most 13 bits
* ...
* #define IOC_OUT 0x40000000 // copy out parameters
* #define IOC_IN 0x80000000 // copy in parameters
* #define IOC_INOUT (IOC_IN|IOC_OUT)
* ...
* #define _IOC(inout,group,num,len) \
* (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num))
* ...
* #define _IOWR(g,n,t) _IOC(IOC_INOUT, (g), (n), sizeof(t))
*
* From kern_control.h:
* #define CTLIOCGINFO _IOWR('N', 3, struct ctl_info) // get id from name
*
*/
const appleCTLIOCGINFO = (0x40000000 | 0x80000000) | ((100 & 0x1fff) << 16) | uint32(byte('N'))<<8 | 3
/*
* struct sockaddr_ctl {
* u_char sc_len; // depends on size of bundle ID string
* u_char sc_family; // AF_SYSTEM
* u_int16_t ss_sysaddr; // AF_SYS_KERNCONTROL
* u_int32_t sc_id; // Controller unique identifier
* u_int32_t sc_unit; // Developer private unit number
* u_int32_t sc_reserved[5];
* };
*/
type sockaddrCtl struct {
scLen uint8
scFamily uint8
ssSysaddr uint16
scID uint32
scUnit uint32
scReserved [5]uint32
}
var sockaddrCtlSize uintptr = 32
func openDev(config Config) (ifce *Interface, err error) {
if config.Driver == MacOSDriverTunTapOSX {
return openDevTunTapOSX(config)
}
if config.Driver == MacOSDriverSystem {
return openDevSystem(config)
}
return nil, errors.New("unrecognized driver")
}
// openDevSystem opens tun device on system
func openDevSystem(config Config) (ifce *Interface, err error) {
if config.DeviceType == TUN {
return openDevTunSystem(config)
}
if config.DeviceType == TAP {
return openDevTapSystem(config)
}
return nil, errors.New("unrecognized type")
}
type sockaddrNdrv struct {
sndLen uint8
sndFamily uint8
sndName [16]byte
}
type ifaceCloser struct {
ifaces []string
additional []io.Closer
}
func (c *ifaceCloser) Close() error {
var err error
for _, add := range c.additional {
newErr := add.Close()
if err == nil {
err = newErr
}
}
for _, iface := range c.ifaces {
newErr := exec.Command("ifconfig", iface, "destroy").Run()
if err == nil {
err = newErr
}
}
return nil
}
type bpfReader struct {
bpfCapture *bsdbpf.BPFSniffer
}
func (b *bpfReader) Read(p []byte) (n int, err error) {
pkt, _, err := b.bpfCapture.ReadPacketData()
if err != nil {
return 0, err
}
if len(pkt) > len(p) {
copy(p, pkt[:len(p)])
return len(p), nil
}
copy(p, pkt)
return len(pkt), nil
}
func (b *bpfReader) Close() error {
return b.bpfCapture.Close()
}
var _ io.ReadCloser = (*bpfReader)(nil)
func checkIfaceNameWithPrefix(name string, prefix string, allowBlank bool) (int, error) {
errInvalid := fmt.Errorf("interface name must be %s[0-9]+", prefix)
if name == "" {
if allowBlank {
return -1, nil
}
return -1, errInvalid
}
if !strings.HasPrefix(name, prefix) {
return -1, errInvalid
}
ifIndex, err := strconv.Atoi(name[len(prefix):])
if err != nil || ifIndex < 0 || ifIndex > math.MaxUint32-1 {
return -1, errInvalid
}
return ifIndex, nil
}
func openDevTapSystem(config Config) (ifce *Interface, err error) {
_, err = checkIfaceNameWithPrefix(config.Name, "feth", true)
if err != nil {
return
}
_, err = checkIfaceNameWithPrefix(config.TAPInjectorName, "feth", true)
if err != nil {
return
}
if config.Name != "" && config.Name == config.TAPInjectorName {
return nil, errors.New("Name must not be the same as TAPInjectorName")
}
ifaceOSName := config.Name
ifaceInjectorName := config.TAPInjectorName
closer := &ifaceCloser{
ifaces: []string{},
additional: []io.Closer{},
}
if ifaceOSName == "" {
ifaceOSName = FindLowestNetworkInterfaceByPrefix("feth")
}
err = exec.Command("ifconfig", ifaceOSName, "create").Run()
if err != nil {
closer.Close()
return nil, err
}
closer.ifaces = append(closer.ifaces, ifaceOSName)
if ifaceInjectorName == "" {
ifaceInjectorName = FindLowestNetworkInterfaceByPrefix("feth")
}
err = exec.Command("ifconfig", ifaceInjectorName, "create").Run()
if err != nil {
closer.Close()
return nil, err
}
closer.ifaces = append(closer.ifaces, ifaceInjectorName)
err = exec.Command("ifconfig", ifaceOSName, "peer", ifaceInjectorName).Run()
if err != nil {
closer.Close()
return nil, err
}
// AF_NDRV = 27
injectFd, err := syscall.Socket(27, syscall.SOCK_RAW, 0)
if err != nil {
closer.Close()
return nil, err
}
injectHdl := os.NewFile(uintptr(injectFd), string(ifaceInjectorName[:]))
closer.additional = append(closer.additional, injectHdl)
sockaddr := &sockaddrNdrv{
sndFamily: 27,
sndLen: 1 + 1 + 16,
}
copy(sockaddr.sndName[:], []byte(ifaceInjectorName))
_, _, errno := syscall.Syscall(syscall.SYS_BIND, uintptr(injectFd), uintptr(unsafe.Pointer(sockaddr)), uintptr(sockaddr.sndLen))
if errno != 0 {
closer.Close()
return nil, fmt.Errorf("bind error = %d", errno)
}
_, _, errno = syscall.Syscall(syscall.SYS_CONNECT, uintptr(injectFd), uintptr(unsafe.Pointer(sockaddr)), uintptr(sockaddr.sndLen))
if errno != 0 {
closer.Close()
return nil, fmt.Errorf("connect error = %d", errno)
}
bpfCapture, err := bsdbpf.NewBPFSniffer(
ifaceInjectorName,
&bsdbpf.Options{
ReadBufLen: 32767,
Timeout: nil,
Promisc: true,
Immediate: true,
PreserveLinkAddr: true,
SeeSent: false,
},
)
if err != nil {
closer.Close()
return nil, err
}
bpfReader := &bpfReader{bpfCapture: bpfCapture}
closer.additional = append(closer.additional, bpfReader)
return &Interface{
isTAP: config.DeviceType == TAP,
ReadWriteCloser: &tapReadCloser{
ifaceInjectR: bpfReader,
ifaceInjectW: injectHdl,
closer: closer,
},
secondaryName: ifaceInjectorName,
name: ifaceOSName,
}, nil
}
func openDevTunSystem(config Config) (ifce *Interface, err error) {
ifIndex := -1
if config.Name != "" {
const utunPrefix = "utun"
if !strings.HasPrefix(config.Name, utunPrefix) {
return nil, fmt.Errorf("Interface name must be utun[0-9]+")
}
ifIndex, err = strconv.Atoi(config.Name[len(utunPrefix):])
if err != nil || ifIndex < 0 || ifIndex > math.MaxUint32-1 {
return nil, fmt.Errorf("Interface name must be utun[0-9]+")
}
}
var fd int
// Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ...
//
// In sys/socket.h:
// #define PF_SYSTEM AF_SYSTEM
//
// In sys/sys_domain.h:
// #define SYSPROTO_CONTROL 2 /* kernel control protocol */
if fd, err = syscall.Socket(syscall.AF_SYSTEM, syscall.SOCK_DGRAM, 2); err != nil {
return nil, fmt.Errorf("error in syscall.Socket: %v", err)
}
var ctlInfo = &struct {
ctlID uint32
ctlName [96]byte
}{}
copy(ctlInfo.ctlName[:], []byte(appleUTUNCtl))
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(appleCTLIOCGINFO), uintptr(unsafe.Pointer(ctlInfo))); errno != 0 {
err = errno
return nil, fmt.Errorf("error in syscall.Syscall(syscall.SYS_IOCTL, ...): %v", err)
}
addrP := unsafe.Pointer(&sockaddrCtl{
scLen: uint8(sockaddrCtlSize),
scFamily: syscall.AF_SYSTEM,
/* #define AF_SYS_CONTROL 2 */
ssSysaddr: 2,
scID: ctlInfo.ctlID,
scUnit: uint32(ifIndex) + 1,
})
if _, _, errno := syscall.RawSyscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(addrP), uintptr(sockaddrCtlSize)); errno != 0 {
err = errno
return nil, fmt.Errorf("error in syscall.RawSyscall(syscall.SYS_CONNECT, ...): %v", err)
}
var ifName struct {
name [16]byte
}
ifNameSize := uintptr(16)
if _, _, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT, uintptr(fd),
2, /* #define SYSPROTO_CONTROL 2 */
2, /* #define UTUN_OPT_IFNAME 2 */
uintptr(unsafe.Pointer(&ifName)),
uintptr(unsafe.Pointer(&ifNameSize)), 0); errno != 0 {
err = errno
return nil, fmt.Errorf("error in syscall.Syscall6(syscall.SYS_GETSOCKOPT, ...): %v", err)
}
if err = setNonBlock(fd); err != nil {
return nil, fmt.Errorf("setting non-blocking error")
}
return &Interface{
isTAP: false,
name: string(ifName.name[:ifNameSize-1 /* -1 is for \0 */]),
ReadWriteCloser: &tunReadCloser{
f: os.NewFile(uintptr(fd), string(ifName.name[:])),
},
}, nil
}
// openDevTunTapOSX opens tun / tap device, assuming tuntaposx is installed
func openDevTunTapOSX(config Config) (ifce *Interface, err error) {
var fd int
var socketFD int
if config.DeviceType == TAP && !strings.HasPrefix(config.Name, "tap") {
return nil, errors.New("device name does not start with tap when creating a tap device")
}
if config.DeviceType == TUN && !strings.HasPrefix(config.Name, "tun") {
return nil, errors.New("device name does not start with tun when creating a tun device")
}
if config.DeviceType != TAP && config.DeviceType != TUN {
return nil, errors.New("unsupported DeviceType")
}
if len(config.Name) >= 15 {
return nil, errors.New("device name is too long")
}
if fd, err = syscall.Open(
"/dev/"+config.Name, os.O_RDWR|syscall.O_NONBLOCK, 0); err != nil {
return nil, err
}
// Note that we are not setting NONBLOCK on the fd itself since it breaks tuntaposx
// see https://sourceforge.net/p/tuntaposx/bugs/6/
// create socket so we can do SIO ioctls, we are not using it afterwards
if socketFD, err = syscall.Socket(syscall.AF_SYSTEM, syscall.SOCK_DGRAM, 2); err != nil {
return nil, fmt.Errorf("error in syscall.Socket: %v", err)
}
var ifReq = &struct {
ifName [16]byte
ifruFlags int16
pad [16]byte
}{}
copy(ifReq.ifName[:], []byte(config.Name))
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(socketFD), uintptr(syscall.SIOCGIFFLAGS), uintptr(unsafe.Pointer(ifReq))); errno != 0 {
err = errno
return nil, fmt.Errorf("error in syscall.Syscall(syscall.SYS_IOCTL, ...): %v", err)
}
ifReq.ifruFlags |= syscall.IFF_RUNNING | syscall.IFF_UP
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(socketFD), uintptr(syscall.SIOCSIFFLAGS), uintptr(unsafe.Pointer(ifReq))); errno != 0 {
err = errno
return nil, fmt.Errorf("error in syscall.Syscall(syscall.SYS_IOCTL, ...): %v", err)
}
syscall.Close(socketFD)
return &Interface{
isTAP: config.DeviceType == TAP,
ReadWriteCloser: os.NewFile(uintptr(fd), "tun"),
name: config.Name,
}, nil
}
// tunReadCloser is a hack to work around the first 4 bytes "packet
// information" because there doesn't seem to be an IFF_NO_PI for darwin.
type tunReadCloser struct {
f io.ReadWriteCloser
rMu sync.Mutex
rBuf []byte
wMu sync.Mutex
wBuf []byte
}
var _ io.ReadWriteCloser = (*tunReadCloser)(nil)
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]
// Determine the IP Family for the NULL L2 Header
ipVer := from[0] >> 4
if ipVer == 4 {
t.wBuf[3] = syscall.AF_INET
} else if ipVer == 6 {
t.wBuf[3] = syscall.AF_INET6
} else {
return 0, errors.New("unable to determine IP version from packet")
}
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 setNonBlock(fd int) error {
return syscall.SetNonblock(fd, true)
}
type tapReadCloser struct {
closer io.Closer
ifaceInjectW io.WriteCloser
ifaceInjectR io.ReadCloser
rMu sync.Mutex
wMu sync.Mutex
}
var _ io.ReadWriteCloser = (*tapReadCloser)(nil)
func (t *tapReadCloser) Read(p []byte) (n int, err error) {
t.rMu.Lock()
defer t.rMu.Unlock()
for {
n, err = t.ifaceInjectR.Read(p)
if err == syscall.EINTR {
continue
}
return
}
}
func (t *tapReadCloser) Write(p []byte) (n int, err error) {
t.wMu.Lock()
defer t.wMu.Unlock()
return t.ifaceInjectW.Write(p)
}
func (t *tapReadCloser) Close() error {
err1 := t.ifaceInjectR.Close()
err2 := t.ifaceInjectW.Close()
err3 := t.closer.Close()
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
if err3 != nil {
return err3
}
return nil
}
const maxMTUSysctl = "net.link.fake.max_mtu"
var maxMTUChangeLock sync.Mutex
var maxMTUSet uint32 = 0
func EnsureMTUAdjust(mtu uint32) error {
if maxMTUSet >= mtu {
return nil
}
maxMTUChangeLock.Lock()
defer maxMTUChangeLock.Unlock()
maxMtu, err := syscall.SysctlUint32(maxMTUSysctl)
if err != nil {
return err
}
maxMTUSet = maxMtu
if maxMTUSet >= mtu {
return nil
}
err = exec.Command("sysctl", fmt.Sprintf("%s=%d", maxMTUSysctl, mtu)).Run()
if err == nil {
maxMTUSet = mtu
}
return err
}
func (ifce *Interface) SetMTU(mtu int) error {
if ifce.secondaryName != "" {
err := EnsureMTUAdjust(uint32(mtu))
if err != nil {
return err
}
err = exec.Command("ifconfig", ifce.secondaryName, "mtu", fmt.Sprintf("%d", mtu)).Run()
if err != nil {
return err
}
}
return exec.Command("ifconfig", ifce.name, "mtu", fmt.Sprintf("%d", mtu)).Run()
}