-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscall_linux.go
123 lines (95 loc) · 3.01 KB
/
syscall_linux.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
package tuntap
import (
"os"
"syscall"
"unsafe"
)
func ioctl(a1, a2, a3 uintptr) error {
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, a1, a2, a3); errno != 0 {
return errno
}
return nil
}
func ifaceAddIoctl(req *ifReq, uid, gid int, persist bool) error {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
req.Flags |= syscall.IFF_TUN_EXCL
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: TUNSETIFF", err)
}
if uid != -1 {
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETOWNER), uintptr(uid)); err != nil {
return os.NewSyscallError("ioctl: TUNSETOWNER", err)
}
}
if gid != -1 {
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETGROUP), uintptr(gid)); err != nil {
return os.NewSyscallError("ioctl: TUNSETGROUP", err)
}
}
if persist {
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETPERSIST), 1); err != nil {
return os.NewSyscallError("ioctl: TUNSETPERSIST", err)
}
}
return nil
}
func ifaceDelIoctl(req *ifReq) error {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: TUNSETIFF", err)
}
if err := ioctl(file.Fd(), uintptr(syscall.TUNSETPERSIST), 0); err != nil {
return os.NewSyscallError("ioctl: TUNSETPERSIST", err)
}
return nil
}
func ifaceFeaturesIoctl() (uint16, error) {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return 0, err
}
defer file.Close()
var features uint16
if err := ioctl(file.Fd(), uintptr(syscall.TUNGETFEATURES), uintptr(unsafe.Pointer(&features))); err != nil {
return 0, os.NewSyscallError("ioctl: TUNGETFEATURES", err)
}
return features, nil
}
func ifaceLinkUpIoctl(req *ifReq) error {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
if err != nil {
return os.NewSyscallError("socket failed", err)
}
fd := uintptr(s)
if err := ioctl(fd, uintptr(syscall.SIOCGIFFLAGS), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: SIOCGIFFLAGS", err)
}
req.Flags |= syscall.IFF_UP
if err := ioctl(fd, uintptr(syscall.SIOCSIFFLAGS), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: SIOCSIFFLAGS", err)
}
return nil
}
func ifaceLinkDownIoctl(req *ifReq) error {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
if err != nil {
return os.NewSyscallError("socket failed", err)
}
fd := uintptr(s)
if err := ioctl(fd, uintptr(syscall.SIOCGIFFLAGS), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: SIOCGIFFLAGS", err)
}
req.Flags = req.Flags &^ syscall.IFF_UP
if err := ioctl(fd, uintptr(syscall.SIOCSIFFLAGS), uintptr(unsafe.Pointer(req))); err != nil {
return os.NewSyscallError("ioctl: SIOCSIFFLAGS", err)
}
return nil
}