2019-02-04 17:29:52 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2019-02-04 17:29:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package tun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-05-03 00:11:59 +02:00
|
|
|
"fmt"
|
2020-11-25 12:54:26 +01:00
|
|
|
"log"
|
2019-02-04 17:29:52 +01:00
|
|
|
"os"
|
2021-02-18 23:53:22 +01:00
|
|
|
"sync"
|
2019-07-11 10:35:47 +02:00
|
|
|
"sync/atomic"
|
2019-03-20 21:45:40 +01:00
|
|
|
"time"
|
2020-10-24 22:40:46 +02:00
|
|
|
_ "unsafe"
|
2019-02-04 17:29:52 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-07-05 07:54:25 +02:00
|
|
|
|
2019-02-19 18:49:24 +01:00
|
|
|
"golang.zx2c4.com/wireguard/tun/wintun"
|
2019-02-04 17:29:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-08-18 11:49:37 +02:00
|
|
|
rateMeasurementGranularity = uint64((time.Second / 2) / time.Nanosecond)
|
|
|
|
spinloopRateThreshold = 800000000 / 8 // 800mbps
|
|
|
|
spinloopDuration = uint64(time.Millisecond / 80 / time.Nanosecond) // ~1gbit/s
|
2019-02-04 17:29:52 +01:00
|
|
|
)
|
|
|
|
|
2019-08-18 11:49:37 +02:00
|
|
|
type rateJuggler struct {
|
|
|
|
current uint64
|
|
|
|
nextByteCount uint64
|
|
|
|
nextStartTime int64
|
|
|
|
changing int32
|
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
type NativeTun struct {
|
2020-07-22 09:15:49 +02:00
|
|
|
wt *wintun.Adapter
|
2019-07-19 13:51:56 +02:00
|
|
|
handle windows.Handle
|
2019-08-18 11:49:37 +02:00
|
|
|
rate rateJuggler
|
2020-10-24 22:40:46 +02:00
|
|
|
session wintun.Session
|
|
|
|
readWait windows.Handle
|
2021-05-07 09:17:35 +02:00
|
|
|
events chan Event
|
2021-05-07 09:26:24 +02:00
|
|
|
running sync.WaitGroup
|
2021-02-18 23:53:22 +01:00
|
|
|
closeOnce sync.Once
|
2021-05-07 09:26:24 +02:00
|
|
|
close int32
|
2021-05-07 09:17:35 +02:00
|
|
|
forcedMTU int
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 14:09:16 +01:00
|
|
|
var WintunPool, _ = wintun.MakePool("WireGuard")
|
|
|
|
var WintunStaticRequestedGUID *windows.GUID
|
2019-08-29 18:00:44 +02:00
|
|
|
|
2019-08-18 11:49:37 +02:00
|
|
|
//go:linkname procyield runtime.procyield
|
|
|
|
func procyield(cycles uint32)
|
|
|
|
|
|
|
|
//go:linkname nanotime runtime.nanotime
|
|
|
|
func nanotime() int64
|
|
|
|
|
2019-03-04 14:27:16 +01:00
|
|
|
//
|
2019-08-29 20:20:40 +02:00
|
|
|
// CreateTUN creates a Wintun interface with the given name. Should a Wintun
|
|
|
|
// interface with the same name exist, it is reused.
|
2019-03-04 14:27:16 +01:00
|
|
|
//
|
2019-04-20 09:28:06 +02:00
|
|
|
func CreateTUN(ifname string, mtu int) (Device, error) {
|
2020-12-22 14:09:16 +01:00
|
|
|
return CreateTUNWithRequestedGUID(ifname, WintunStaticRequestedGUID, mtu)
|
2019-06-09 19:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-08-29 20:20:40 +02:00
|
|
|
// CreateTUNWithRequestedGUID creates a Wintun interface with the given name and
|
|
|
|
// a requested GUID. Should a Wintun interface with the same name exist, it is reused.
|
2019-06-09 19:20:17 +02:00
|
|
|
//
|
2019-04-20 09:28:06 +02:00
|
|
|
func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID, mtu int) (Device, error) {
|
2019-05-03 00:11:59 +02:00
|
|
|
var err error
|
2020-07-22 09:15:49 +02:00
|
|
|
var wt *wintun.Adapter
|
2019-05-03 00:11:59 +02:00
|
|
|
|
2019-05-09 10:11:15 +02:00
|
|
|
// Does an interface with this name already exist?
|
2020-07-22 09:15:49 +02:00
|
|
|
wt, err = WintunPool.OpenAdapter(ifname)
|
2019-05-17 16:06:02 +02:00
|
|
|
if err == nil {
|
|
|
|
// If so, we delete it, in case it has weird residual configuration.
|
2020-07-22 09:15:49 +02:00
|
|
|
_, err = wt.Delete(true)
|
2019-02-06 22:30:14 +01:00
|
|
|
if err != nil {
|
2020-11-07 21:56:32 +01:00
|
|
|
return nil, fmt.Errorf("Error deleting already existing interface: %w", err)
|
2019-02-06 22:30:14 +01:00
|
|
|
}
|
2019-02-07 22:02:51 +01:00
|
|
|
}
|
2020-11-25 12:54:26 +01:00
|
|
|
wt, rebootRequired, err := WintunPool.CreateAdapter(ifname, requestedGUID)
|
2019-03-31 10:17:11 +02:00
|
|
|
if err != nil {
|
2020-11-07 21:56:32 +01:00
|
|
|
return nil, fmt.Errorf("Error creating interface: %w", err)
|
2019-03-31 10:17:11 +02:00
|
|
|
}
|
2020-11-25 12:54:26 +01:00
|
|
|
if rebootRequired {
|
|
|
|
log.Println("Windows indicated a reboot is required.")
|
|
|
|
}
|
2019-02-07 22:02:51 +01:00
|
|
|
|
2019-04-20 09:28:06 +02:00
|
|
|
forcedMTU := 1420
|
|
|
|
if mtu > 0 {
|
|
|
|
forcedMTU = mtu
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
tun := &NativeTun{
|
2019-03-18 09:42:00 +01:00
|
|
|
wt: wt,
|
2019-07-23 11:45:48 +02:00
|
|
|
handle: windows.InvalidHandle,
|
2019-06-10 23:33:40 +02:00
|
|
|
events: make(chan Event, 10),
|
2019-04-20 09:28:06 +02:00
|
|
|
forcedMTU: forcedMTU,
|
2019-07-11 10:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 22:40:46 +02:00
|
|
|
tun.session, err = wt.StartSession(0x800000) // Ring capacity, 8 MiB
|
2019-07-11 10:35:47 +02:00
|
|
|
if err != nil {
|
2020-11-25 12:37:02 +01:00
|
|
|
tun.wt.Delete(false)
|
2020-10-24 22:40:46 +02:00
|
|
|
close(tun.events)
|
2020-11-07 21:56:32 +01:00
|
|
|
return nil, fmt.Errorf("Error starting session: %w", err)
|
2019-07-11 10:35:47 +02:00
|
|
|
}
|
2020-10-24 22:40:46 +02:00
|
|
|
tun.readWait = tun.session.ReadWaitEvent()
|
2019-07-19 13:51:56 +02:00
|
|
|
return tun, nil
|
2019-02-20 13:12:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Name() (string, error) {
|
2021-05-07 09:26:24 +02:00
|
|
|
tun.running.Add(1)
|
|
|
|
defer tun.running.Done()
|
|
|
|
if atomic.LoadInt32(&tun.close) == 1 {
|
2021-04-27 04:22:45 +02:00
|
|
|
return "", os.ErrClosed
|
|
|
|
}
|
2019-08-29 20:20:40 +02:00
|
|
|
return tun.wt.Name()
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) File() *os.File {
|
2019-02-04 17:29:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 23:33:40 +02:00
|
|
|
func (tun *NativeTun) Events() chan Event {
|
2019-02-04 17:29:52 +01:00
|
|
|
return tun.events
|
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Close() error {
|
2019-07-19 13:51:56 +02:00
|
|
|
var err error
|
2021-02-18 23:53:22 +01:00
|
|
|
tun.closeOnce.Do(func() {
|
2021-05-07 09:26:24 +02:00
|
|
|
atomic.StoreInt32(&tun.close, 1)
|
|
|
|
windows.SetEvent(tun.readWait)
|
|
|
|
tun.running.Wait()
|
2021-02-18 23:53:22 +01:00
|
|
|
tun.session.End()
|
|
|
|
if tun.wt != nil {
|
|
|
|
_, err = tun.wt.Delete(false)
|
|
|
|
}
|
|
|
|
close(tun.events)
|
|
|
|
})
|
2019-07-11 10:35:47 +02:00
|
|
|
return err
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) MTU() (int, error) {
|
2019-05-16 10:33:47 +02:00
|
|
|
return tun.forcedMTU, nil
|
2019-03-13 09:52:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 23:00:15 +02:00
|
|
|
// TODO: This is a temporary hack. We really need to be monitoring the interface in real time and adapting to MTU changes.
|
2019-05-16 10:33:47 +02:00
|
|
|
func (tun *NativeTun) ForceMTU(mtu int) {
|
2021-05-05 11:42:45 +02:00
|
|
|
update := tun.forcedMTU != mtu
|
2019-05-16 10:33:47 +02:00
|
|
|
tun.forcedMTU = mtu
|
2021-05-05 11:42:45 +02:00
|
|
|
if update {
|
|
|
|
tun.events <- EventMTUUpdate
|
|
|
|
}
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
2021-05-07 09:26:24 +02:00
|
|
|
tun.running.Add(1)
|
|
|
|
defer tun.running.Done()
|
2019-07-19 13:51:56 +02:00
|
|
|
retry:
|
2021-05-07 09:26:24 +02:00
|
|
|
if atomic.LoadInt32(&tun.close) == 1 {
|
2021-04-27 04:22:45 +02:00
|
|
|
return 0, os.ErrClosed
|
2019-02-08 14:31:05 +01:00
|
|
|
}
|
2019-08-18 11:49:37 +02:00
|
|
|
start := nanotime()
|
|
|
|
shouldSpin := atomic.LoadUint64(&tun.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&tun.rate.nextStartTime)) <= rateMeasurementGranularity*2
|
2019-07-19 13:51:56 +02:00
|
|
|
for {
|
2021-05-07 09:26:24 +02:00
|
|
|
if atomic.LoadInt32(&tun.close) == 1 {
|
2019-07-19 13:51:56 +02:00
|
|
|
return 0, os.ErrClosed
|
2019-02-08 14:31:05 +01:00
|
|
|
}
|
2020-10-24 22:40:46 +02:00
|
|
|
packet, err := tun.session.ReceivePacket()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
packetSize := len(packet)
|
|
|
|
copy(buff[offset:], packet)
|
|
|
|
tun.session.ReleaseReceivePacket(packet)
|
|
|
|
tun.rate.update(uint64(packetSize))
|
|
|
|
return packetSize, nil
|
|
|
|
case windows.ERROR_NO_MORE_ITEMS:
|
|
|
|
if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration {
|
|
|
|
windows.WaitForSingleObject(tun.readWait, windows.INFINITE)
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
procyield(1)
|
|
|
|
continue
|
|
|
|
case windows.ERROR_HANDLE_EOF:
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
case windows.ERROR_INVALID_DATA:
|
|
|
|
return 0, errors.New("Send ring corrupt")
|
2019-05-31 15:40:08 +02:00
|
|
|
}
|
2020-11-07 21:56:32 +01:00
|
|
|
return 0, fmt.Errorf("Read failed: %w", err)
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
}
|
2019-02-07 04:08:05 +01:00
|
|
|
|
2019-03-21 21:43:04 +01:00
|
|
|
func (tun *NativeTun) Flush() error {
|
2019-07-11 10:35:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-21 21:43:04 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
2021-05-07 09:26:24 +02:00
|
|
|
tun.running.Add(1)
|
|
|
|
defer tun.running.Done()
|
|
|
|
if atomic.LoadInt32(&tun.close) == 1 {
|
2019-07-19 13:51:56 +02:00
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-20 13:12:08 +01:00
|
|
|
|
2020-10-24 22:40:46 +02:00
|
|
|
packetSize := len(buff) - offset
|
2019-08-18 11:49:37 +02:00
|
|
|
tun.rate.update(uint64(packetSize))
|
2019-02-04 17:29:52 +01:00
|
|
|
|
2020-10-24 22:40:46 +02:00
|
|
|
packet, err := tun.session.AllocateSendPacket(packetSize)
|
|
|
|
if err == nil {
|
|
|
|
copy(packet, buff[offset:])
|
|
|
|
tun.session.SendPacket(packet)
|
|
|
|
return packetSize, nil
|
2019-07-19 13:51:56 +02:00
|
|
|
}
|
2020-10-24 22:40:46 +02:00
|
|
|
switch err {
|
|
|
|
case windows.ERROR_HANDLE_EOF:
|
2019-07-19 13:51:56 +02:00
|
|
|
return 0, os.ErrClosed
|
2020-10-24 22:40:46 +02:00
|
|
|
case windows.ERROR_BUFFER_OVERFLOW:
|
2019-07-19 13:51:56 +02:00
|
|
|
return 0, nil // Dropping when ring is full.
|
|
|
|
}
|
2020-11-07 21:56:32 +01:00
|
|
|
return 0, fmt.Errorf("Write failed: %w", err)
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
2019-03-01 00:11:12 +01:00
|
|
|
|
2019-08-29 20:20:40 +02:00
|
|
|
// LUID returns Windows interface instance ID.
|
2019-05-10 21:30:23 +02:00
|
|
|
func (tun *NativeTun) LUID() uint64 {
|
2021-05-07 09:26:24 +02:00
|
|
|
tun.running.Add(1)
|
|
|
|
defer tun.running.Done()
|
|
|
|
if atomic.LoadInt32(&tun.close) == 1 {
|
2021-04-27 04:22:45 +02:00
|
|
|
return 0
|
|
|
|
}
|
2019-05-17 14:26:46 +02:00
|
|
|
return tun.wt.LUID()
|
2019-05-10 21:30:23 +02:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
|
2020-07-22 09:15:49 +02:00
|
|
|
// RunningVersion returns the running version of the Wintun driver.
|
|
|
|
func (tun *NativeTun) RunningVersion() (version uint32, err error) {
|
|
|
|
return wintun.RunningVersion()
|
2019-10-08 09:58:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:49:37 +02:00
|
|
|
func (rate *rateJuggler) update(packetLen uint64) {
|
|
|
|
now := nanotime()
|
|
|
|
total := atomic.AddUint64(&rate.nextByteCount, packetLen)
|
|
|
|
period := uint64(now - atomic.LoadInt64(&rate.nextStartTime))
|
|
|
|
if period >= rateMeasurementGranularity {
|
|
|
|
if !atomic.CompareAndSwapInt32(&rate.changing, 0, 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
atomic.StoreInt64(&rate.nextStartTime, now)
|
|
|
|
atomic.StoreUint64(&rate.current, total*uint64(time.Second/time.Nanosecond)/period)
|
|
|
|
atomic.StoreUint64(&rate.nextByteCount, 0)
|
|
|
|
atomic.StoreInt32(&rate.changing, 0)
|
|
|
|
}
|
|
|
|
}
|