aboutsummaryrefslogtreecommitdiffstats
path: root/tun/wintun/ring_windows.go
blob: 8e6b37584d4e8a997a1e2cf65d341df18675a662 (plain) (blame)
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
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package wintun

import (
	"runtime"
	"unsafe"

	"golang.org/x/sys/windows"
)

const (
	PacketAlignment    = 4        // Number of bytes packets are aligned to in rings
	PacketSizeMax      = 0xffff   // Maximum packet size
	PacketCapacity     = 0x800000 // Ring capacity, 8MiB
	PacketTrailingSize = uint32(unsafe.Sizeof(PacketHeader{})) + ((PacketSizeMax + (PacketAlignment - 1)) &^ (PacketAlignment - 1)) - PacketAlignment
	ioctlRegisterRings = (51820 << 16) | (0x970 << 2) | 0 /*METHOD_BUFFERED*/ | (0x3 /*FILE_READ_DATA | FILE_WRITE_DATA*/ << 14)
)

type PacketHeader struct {
	Size uint32
}

type Packet struct {
	PacketHeader
	Data [PacketSizeMax]byte
}

type Ring struct {
	Head      uint32
	Tail      uint32
	Alertable int32
	Data      [PacketCapacity + PacketTrailingSize]byte
}

type RingDescriptor struct {
	Send, Receive struct {
		Size      uint32
		Ring      *Ring
		TailMoved windows.Handle
	}
}

// Wrap returns value modulo ring capacity
func (rb *Ring) Wrap(value uint32) uint32 {
	return value & (PacketCapacity - 1)
}

// Aligns a packet size to PacketAlignment
func PacketAlign(size uint32) uint32 {
	return (size + (PacketAlignment - 1)) &^ (PacketAlignment - 1)
}

func NewRingDescriptor() (descriptor *RingDescriptor, err error) {
	descriptor = new(RingDescriptor)
	allocatedRegion, err := windows.VirtualAlloc(0, unsafe.Sizeof(Ring{})*2, windows.MEM_COMMIT|windows.MEM_RESERVE, windows.PAGE_READWRITE)
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			descriptor.free()
			descriptor = nil
		}
	}()
	descriptor.Send.Size = uint32(unsafe.Sizeof(Ring{}))
	descriptor.Send.Ring = (*Ring)(unsafe.Pointer(allocatedRegion))
	descriptor.Send.TailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
	if err != nil {
		return
	}

	descriptor.Receive.Size = uint32(unsafe.Sizeof(Ring{}))
	descriptor.Receive.Ring = (*Ring)(unsafe.Pointer(allocatedRegion + unsafe.Sizeof(Ring{})))
	descriptor.Receive.TailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
	if err != nil {
		windows.CloseHandle(descriptor.Send.TailMoved)
		return
	}
	runtime.SetFinalizer(descriptor, func(d *RingDescriptor) { d.free() })
	return
}

func (descriptor *RingDescriptor) free() {
	if descriptor.Send.Ring != nil {
		windows.VirtualFree(uintptr(unsafe.Pointer(descriptor.Send.Ring)), 0, windows.MEM_RELEASE)
		descriptor.Send.Ring = nil
		descriptor.Receive.Ring = nil
	}
}

func (descriptor *RingDescriptor) Close() {
	if descriptor.Send.TailMoved != 0 {
		windows.CloseHandle(descriptor.Send.TailMoved)
		descriptor.Send.TailMoved = 0
	}
	if descriptor.Send.TailMoved != 0 {
		windows.CloseHandle(descriptor.Receive.TailMoved)
		descriptor.Receive.TailMoved = 0
	}
}

func (wintun *Interface) Register(descriptor *RingDescriptor) (windows.Handle, error) {
	handle, err := wintun.handle()
	if err != nil {
		return 0, err
	}
	var bytesReturned uint32
	err = windows.DeviceIoControl(handle, ioctlRegisterRings, (*byte)(unsafe.Pointer(descriptor)), uint32(unsafe.Sizeof(*descriptor)), nil, 0, &bytesReturned, nil)
	if err != nil {
		return 0, err
	}
	return handle, nil
}