-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscalls_windows.go
391 lines (341 loc) · 9.68 KB
/
syscalls_windows.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
package water
import (
"bytes"
"errors"
"fmt"
"net"
"os/exec"
"sync"
"syscall"
"unsafe"
"golang.org/x/sys/windows/registry"
wintun "golang.zx2c4.com/wireguard/tun"
)
// To use it with windows, you need a tap driver installed on windows.
// https://github.com/OpenVPN/tap-windows6
// or just install OpenVPN
// https://github.com/OpenVPN/openvpn
const (
// tapDriverKey is the location of the TAP driver key.
tapDriverKey = `SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}`
// netConfigKey is the location of the TAP adapter's network config.
netConfigKey = `SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}`
)
var (
errIfceNameNotFound = errors.New("failed to find the name of interface")
// Device Control Codes
tap_win_ioctl_get_mac = tap_control_code(1, 0)
// tap_win_ioctl_get_version = tap_control_code(2, 0)
// tap_win_ioctl_get_mtu = tap_control_code(3, 0)
// tap_win_ioctl_get_info = tap_control_code(4, 0)
// tap_ioctl_config_point_to_point = tap_control_code(5, 0)
tap_ioctl_set_media_status = tap_control_code(6, 0)
// tap_win_ioctl_config_dhcp_masq = tap_control_code(7, 0)
// tap_win_ioctl_get_log_line = tap_control_code(8, 0)
// tap_win_ioctl_config_dhcp_set_opt = tap_control_code(9, 0)
// tap_ioctl_config_tun = tap_control_code(10, 0)
// w32 api
file_device_unknown = uint32(0x00000022)
nCreateEvent,
nResetEvent,
nGetOverlappedResult uintptr
)
func init() {
k32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
panic("LoadLibrary " + err.Error())
}
defer func() {
_ = syscall.FreeLibrary(k32)
}()
nCreateEvent = getProcAddr(k32, "CreateEventW")
nResetEvent = getProcAddr(k32, "ResetEvent")
nGetOverlappedResult = getProcAddr(k32, "GetOverlappedResult")
}
func getProcAddr(lib syscall.Handle, name string) uintptr {
addr, err := syscall.GetProcAddress(lib, name)
if err != nil {
panic(name + " " + err.Error())
}
return addr
}
func resetEvent(h syscall.Handle) error {
r, _, err := syscall.SyscallN(nResetEvent, uintptr(h))
if r == 0 {
return err
}
return nil
}
func getOverlappedResult(h syscall.Handle, overlapped *syscall.Overlapped) (int, error) {
var n int
r, _, err := syscall.SyscallN(nGetOverlappedResult,
uintptr(h),
uintptr(unsafe.Pointer(overlapped)),
uintptr(unsafe.Pointer(&n)), 1)
if r == 0 {
return n, err
}
return n, nil
}
func newOverlapped() (*syscall.Overlapped, error) {
var overlapped syscall.Overlapped
r, _, err := syscall.SyscallN(nCreateEvent, 0, 1, 0, 0)
if r == 0 {
return nil, err
}
overlapped.HEvent = syscall.Handle(r)
return &overlapped, nil
}
type wfile struct {
fd syscall.Handle
rl sync.Mutex
wl sync.Mutex
ro *syscall.Overlapped
wo *syscall.Overlapped
}
func (f *wfile) Close() error {
return syscall.Close(f.fd)
}
func (f *wfile) Write(b []byte) (int, error) {
f.wl.Lock()
defer f.wl.Unlock()
if err := resetEvent(f.wo.HEvent); err != nil {
return 0, err
}
var n uint32
err := syscall.WriteFile(f.fd, b, &n, f.wo)
if err != nil && err != syscall.ERROR_IO_PENDING {
return int(n), err
}
return getOverlappedResult(f.fd, f.wo)
}
func (f *wfile) Read(b []byte) (int, error) {
f.rl.Lock()
defer f.rl.Unlock()
if err := resetEvent(f.ro.HEvent); err != nil {
return 0, err
}
var done uint32
err := syscall.ReadFile(f.fd, b, &done, f.ro)
if err != nil && err != syscall.ERROR_IO_PENDING {
return int(done), err
}
return getOverlappedResult(f.fd, f.ro)
}
func ctl_code(device_type, function, method, access uint32) uint32 {
return (device_type << 16) | (access << 14) | (function << 2) | method
}
func tap_control_code(request, method uint32) uint32 {
return ctl_code(file_device_unknown, request, method, 0)
}
// getdeviceid finds out a TAP device from registry, it *may* requires privileged right to prevent some weird issue.
func getdeviceid(componentID string, interfaceName string) (deviceid string, err error) {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, tapDriverKey, registry.READ)
if err != nil {
return "", errors.New("Failed to open the adapter registry, TAP driver may be not installed" + err.Error())
}
defer k.Close()
// read all subkeys, it should not return an err here
keys, err := k.ReadSubKeyNames(-1)
if err != nil {
return "", err
}
// find the one matched ComponentId
for _, v := range keys {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, tapDriverKey+"\\"+v, registry.READ)
if err != nil {
continue
}
val, _, err := key.GetStringValue("ComponentId")
if err != nil {
key.Close()
continue
}
if val == componentID {
val, _, err = key.GetStringValue("NetCfgInstanceId")
if err != nil {
key.Close()
continue
}
if len(interfaceName) > 0 {
key2 := fmt.Sprintf("%s\\%s\\Connection", netConfigKey, val)
k2, err := registry.OpenKey(registry.LOCAL_MACHINE, key2, registry.READ)
if err != nil {
continue
}
defer k2.Close()
val, _, err := k2.GetStringValue("Name")
if err != nil || val != interfaceName {
continue
}
}
key.Close()
return val, nil
}
key.Close()
}
if len(interfaceName) > 0 {
return "", errors.New("Failed to find the tap device in registry with specified ComponentId '" + componentID + "' and InterfaceName '" + interfaceName + "', TAP driver may be not installed or you may have specified an interface name that doesn't exist")
}
return "", errors.New("Failed to find the tap device in registry with specified ComponentId '" + componentID + "', TAP driver may be not installed")
}
// setStatus is used to bring up or bring down the interface
func setStatus(fd syscall.Handle, status bool) error {
var bytesReturned uint32
rdbbuf := make([]byte, syscall.MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
code := []byte{0x00, 0x00, 0x00, 0x00}
if status {
code[0] = 0x01
}
return syscall.DeviceIoControl(fd, tap_ioctl_set_media_status, &code[0], uint32(4), &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil)
}
func openTap(config Config) (ifce *Interface, err error) {
if config.ComponentID == "" {
config.ComponentID = "root\\tap0901"
}
// find the device in registry.
deviceid, err := getdeviceid(config.PlatformSpecificParams.ComponentID, config.PlatformSpecificParams.InterfaceName)
if err != nil {
return nil, err
}
path := "\\\\.\\Global\\" + deviceid + ".tap"
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}
// type Handle uintptr
file, err := syscall.CreateFile(pathp, syscall.GENERIC_READ|syscall.GENERIC_WRITE, uint32(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE), nil, syscall.OPEN_EXISTING, syscall.FILE_ATTRIBUTE_SYSTEM|syscall.FILE_FLAG_OVERLAPPED, 0)
// if err hanppens, close the interface.
defer func() {
if err != nil {
syscall.Close(file)
}
if err := recover(); err != nil {
syscall.Close(file)
}
}()
if err != nil {
return nil, err
}
var bytesReturned uint32
// find the mac address of tap device, use this to find the name of interface
mac := make([]byte, 6)
err = syscall.DeviceIoControl(file, tap_win_ioctl_get_mac, &mac[0], uint32(len(mac)), &mac[0], uint32(len(mac)), &bytesReturned, nil)
if err != nil {
return nil, err
}
// fd := os.NewFile(uintptr(file), path)
ro, err := newOverlapped()
if err != nil {
return
}
wo, err := newOverlapped()
if err != nil {
return
}
fd := &wfile{fd: file, ro: ro, wo: wo}
ifce = &Interface{isTAP: (config.DeviceType == TAP), ReadWriteCloser: fd}
// bring up device.
if err := setStatus(file, true); err != nil {
return nil, err
}
// find the name of tap interface(u need it to set the ip or other command)
ifces, err := net.Interfaces()
if err != nil {
return
}
for _, v := range ifces {
if len(v.HardwareAddr) < 6 {
continue
}
if bytes.Equal(v.HardwareAddr[:6], mac[:6]) {
ifce.name = v.Name
return
}
}
return nil, errIfceNameNotFound
}
type wintunRWC struct {
ad wintun.Device
rmu sync.Mutex
wmu sync.Mutex
}
func (w *wintunRWC) Close() error {
return w.ad.Close()
}
func (w *wintunRWC) ReadVector(bufs [][]byte, sizes []int) (int, error) {
w.rmu.Lock()
defer w.rmu.Unlock()
return w.ad.Read(bufs, sizes, 0)
}
func (w *wintunRWC) WriteVector(bufs [][]byte) (int, error) {
w.wmu.Lock()
defer w.wmu.Unlock()
return w.ad.Write(bufs, 0)
}
func (w *wintunRWC) Read(b []byte) (int, error) {
lens := []int{0}
res, err := w.ad.Read([][]byte{b}, lens, 0)
if err != nil {
return 0, err
}
if res <= 0 {
return res, nil
}
return lens[0], nil
}
func (w *wintunRWC) Write(b []byte) (int, error) {
res, err := w.WriteVector([][]byte{b})
if err != nil {
return 0, err
}
if res <= 0 {
return res, nil
}
return len(b), nil
}
func (w *wintunRWC) IsVectorNative() bool {
return true
}
// openDev find and open an interface.
func openDev(config Config) (ifce *Interface, err error) {
// TAP
if config.DeviceType == TAP {
return openTap(config)
}
// TUN
var ad wintun.Device
if config.InterfaceName == "" {
config.InterfaceName = "WaterWinTunInterface"
}
if config.ComponentID == "" {
config.ComponentID = "WaterWintun"
}
ad, err = wintun.CreateTUN(config.InterfaceName, 1500)
if err != nil {
return
}
var name string
name, err = ad.Name()
if err != nil {
return
}
wt := &wintunRWC{ad: ad}
return &Interface{VectorReadWrite: wt, ReadWriteCloser: wt, name: name}, nil
}
func (ifce *Interface) SetMTU(mtu int) error {
err := exec.Command("netsh", "interface", "ipv4", "set", "subinterface", ifce.name, fmt.Sprintf("mtu=%d", mtu)).Run()
if err != nil {
return err
}
wtun, ok := ifce.ReadWriteCloser.(*wintunRWC)
if !ok {
return nil // TAP interface
}
ad, ok := wtun.ad.(*wintun.NativeTun)
if !ok {
return errors.New("cannot cast ad to NativeTun")
}
ad.ForceMTU(mtu)
return nil
}