| 1 |
//go:build windows |
| 2 |
// +build windows |
| 3 |
|
| 4 |
/* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved. |
| 7 |
*/ |
| 8 |
|
| 9 |
//NOTE: This file was forked from https://git.zx2c4.com/wireguard-go/tree/tun/tun_windows.go?id=851efb1bb65555e0f765a3361c8eb5ac47435b19 |
| 10 |
// Mainly to shed functionality we won't be using and to fix names that display in the system |
| 11 |
|
| 12 |
package wintun |
| 13 |
|
| 14 |
import ( |
| 15 |
"errors" |
| 16 |
"fmt" |
| 17 |
"os" |
| 18 |
"sync" |
| 19 |
"sync/atomic" |
| 20 |
"time" |
| 21 |
_ "unsafe" |
| 22 |
|
| 23 |
"golang.org/x/sys/windows" |
| 24 |
|
| 25 |
"golang.zx2c4.com/wintun" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
rateMeasurementGranularity = uint64((time.Second / 2) / time.Nanosecond) |
| 30 |
spinloopRateThreshold = 800000000 / 8 // 800mbps |
| 31 |
spinloopDuration = uint64(time.Millisecond / 80 / time.Nanosecond) // ~1gbit/s |
| 32 |
) |
| 33 |
|
| 34 |
type rateJuggler struct { |
| 35 |
current uint64 |
| 36 |
nextByteCount uint64 |
| 37 |
nextStartTime int64 |
| 38 |
changing int32 |
| 39 |
} |
| 40 |
|
| 41 |
type NativeTun struct { |
| 42 |
wt *wintun.Adapter |
| 43 |
name string |
| 44 |
handle windows.Handle |
| 45 |
rate rateJuggler |
| 46 |
session wintun.Session |
| 47 |
readWait windows.Handle |
| 48 |
running sync.WaitGroup |
| 49 |
closeOnce sync.Once |
| 50 |
close int32 |
| 51 |
} |
| 52 |
|
| 53 |
var WintunTunnelType = "Nebula" |
| 54 |
var WintunStaticRequestedGUID *windows.GUID |
| 55 |
|
| 56 |
//go:linkname procyield runtime.procyield |
| 57 |
func procyield(cycles uint32) |
| 58 |
|
| 59 |
//go:linkname nanotime runtime.nanotime |
| 60 |
func nanotime() int64 |
| 61 |
|
| 62 |
// CreateTUN creates a Wintun interface with the given name. Should a Wintun |
| 63 |
// interface with the same name exist, it is reused. |
| 64 |
func CreateTUN(ifname string, mtu int) (Device, error) { |
| 65 |
return CreateTUNWithRequestedGUID(ifname, WintunStaticRequestedGUID, mtu) |
| 66 |
} |
| 67 |
|
| 68 |
// CreateTUNWithRequestedGUID creates a Wintun interface with the given name and |
| 69 |
// a requested GUID. Should a Wintun interface with the same name exist, it is reused. |
| 70 |
func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID, mtu int) (Device, error) { |
| 71 |
wt, err := wintun.CreateAdapter(ifname, WintunTunnelType, requestedGUID) |
| 72 |
if err != nil { |
| 73 |
return nil, fmt.Errorf("Error creating interface: %w", err) |
| 74 |
} |
| 75 |
|
| 76 |
tun := &NativeTun{ |
| 77 |
wt: wt, |
| 78 |
name: ifname, |
| 79 |
handle: windows.InvalidHandle, |
| 80 |
} |
| 81 |
|
| 82 |
tun.session, err = wt.StartSession(0x800000) // Ring capacity, 8 MiB |
| 83 |
if err != nil { |
| 84 |
tun.wt.Close() |
| 85 |
return nil, fmt.Errorf("Error starting session: %w", err) |
| 86 |
} |
| 87 |
tun.readWait = tun.session.ReadWaitEvent() |
| 88 |
return tun, nil |
| 89 |
} |
| 90 |
|
| 91 |
func (tun *NativeTun) Name() (string, error) { |
| 92 |
return tun.name, nil |
| 93 |
} |
| 94 |
|
| 95 |
func (tun *NativeTun) File() *os.File { |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
func (tun *NativeTun) Close() error { |
| 100 |
var err error |
| 101 |
tun.closeOnce.Do(func() { |
| 102 |
atomic.StoreInt32(&tun.close, 1) |
| 103 |
windows.SetEvent(tun.readWait) |
| 104 |
tun.running.Wait() |
| 105 |
tun.session.End() |
| 106 |
if tun.wt != nil { |
| 107 |
tun.wt.Close() |
| 108 |
} |
| 109 |
}) |
| 110 |
return err |
| 111 |
} |
| 112 |
|
| 113 |
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking. |
| 114 |
|
| 115 |
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { |
| 116 |
tun.running.Add(1) |
| 117 |
defer tun.running.Done() |
| 118 |
retry: |
| 119 |
if atomic.LoadInt32(&tun.close) == 1 { |
| 120 |
return 0, os.ErrClosed |
| 121 |
} |
| 122 |
start := nanotime() |
| 123 |
shouldSpin := atomic.LoadUint64(&tun.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&tun.rate.nextStartTime)) <= rateMeasurementGranularity*2 |
| 124 |
for { |
| 125 |
if atomic.LoadInt32(&tun.close) == 1 { |
| 126 |
return 0, os.ErrClosed |
| 127 |
} |
| 128 |
packet, err := tun.session.ReceivePacket() |
| 129 |
switch err { |
| 130 |
case nil: |
| 131 |
packetSize := len(packet) |
| 132 |
copy(buff[offset:], packet) |
| 133 |
tun.session.ReleaseReceivePacket(packet) |
| 134 |
tun.rate.update(uint64(packetSize)) |
| 135 |
return packetSize, nil |
| 136 |
case windows.ERROR_NO_MORE_ITEMS: |
| 137 |
if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration { |
| 138 |
windows.WaitForSingleObject(tun.readWait, windows.INFINITE) |
| 139 |
goto retry |
| 140 |
} |
| 141 |
procyield(1) |
| 142 |
continue |
| 143 |
case windows.ERROR_HANDLE_EOF: |
| 144 |
return 0, os.ErrClosed |
| 145 |
case windows.ERROR_INVALID_DATA: |
| 146 |
return 0, errors.New("Send ring corrupt") |
| 147 |
} |
| 148 |
return 0, fmt.Errorf("Read failed: %w", err) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func (tun *NativeTun) Flush() error { |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) { |
| 157 |
tun.running.Add(1) |
| 158 |
defer tun.running.Done() |
| 159 |
if atomic.LoadInt32(&tun.close) == 1 { |
| 160 |
return 0, os.ErrClosed |
| 161 |
} |
| 162 |
|
| 163 |
packetSize := len(buff) - offset |
| 164 |
tun.rate.update(uint64(packetSize)) |
| 165 |
|
| 166 |
packet, err := tun.session.AllocateSendPacket(packetSize) |
| 167 |
if err == nil { |
| 168 |
copy(packet, buff[offset:]) |
| 169 |
tun.session.SendPacket(packet) |
| 170 |
return packetSize, nil |
| 171 |
} |
| 172 |
switch err { |
| 173 |
case windows.ERROR_HANDLE_EOF: |
| 174 |
return 0, os.ErrClosed |
| 175 |
case windows.ERROR_BUFFER_OVERFLOW: |
| 176 |
return 0, nil // Dropping when ring is full. |
| 177 |
} |
| 178 |
return 0, fmt.Errorf("Write failed: %w", err) |
| 179 |
} |
| 180 |
|
| 181 |
// LUID returns Windows interface instance ID. |
| 182 |
func (tun *NativeTun) LUID() uint64 { |
| 183 |
tun.running.Add(1) |
| 184 |
defer tun.running.Done() |
| 185 |
if atomic.LoadInt32(&tun.close) == 1 { |
| 186 |
return 0 |
| 187 |
} |
| 188 |
return tun.wt.LUID() |
| 189 |
} |
| 190 |
|
| 191 |
// RunningVersion returns the running version of the Wintun driver. |
| 192 |
func (tun *NativeTun) RunningVersion() (version uint32, err error) { |
| 193 |
return wintun.RunningVersion() |
| 194 |
} |
| 195 |
|
| 196 |
func (rate *rateJuggler) update(packetLen uint64) { |
| 197 |
now := nanotime() |
| 198 |
total := atomic.AddUint64(&rate.nextByteCount, packetLen) |
| 199 |
period := uint64(now - atomic.LoadInt64(&rate.nextStartTime)) |
| 200 |
if period >= rateMeasurementGranularity { |
| 201 |
if !atomic.CompareAndSwapInt32(&rate.changing, 0, 1) { |
| 202 |
return |
| 203 |
} |
| 204 |
atomic.StoreInt64(&rate.nextStartTime, now) |
| 205 |
atomic.StoreUint64(&rate.current, total*uint64(time.Second/time.Nanosecond)/period) |
| 206 |
atomic.StoreUint64(&rate.nextByteCount, 0) |
| 207 |
atomic.StoreInt32(&rate.changing, 0) |
| 208 |
} |
| 209 |
} |