nebula/tun_windows.go

76 lines
1.8 KiB
Go
Raw Normal View History

2021-10-21 23:24:11 +02:00
//go:build !e2e_testing
2021-03-31 17:26:35 +02:00
// +build !e2e_testing
2019-11-19 18:00:20 +01:00
package nebula
import (
"fmt"
"io"
2019-11-19 18:00:20 +01:00
"net"
2021-11-08 19:36:31 +01:00
"os"
"path/filepath"
"runtime"
"syscall"
2019-11-19 18:00:20 +01:00
2021-03-26 15:46:30 +01:00
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/overlay"
2019-11-19 18:00:20 +01:00
)
type Tun struct {
overlay.Device
}
2021-03-26 15:46:30 +01:00
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int) (ifce *Tun, err error) {
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
}
2021-03-26 15:46:30 +01:00
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
2019-11-19 18:00:20 +01:00
if len(routes) > 0 {
return nil, fmt.Errorf("route MTU not supported in Windows")
2019-11-19 18:00:20 +01:00
}
2021-11-08 19:36:31 +01:00
useWintun := true
if err = checkWinTunExists(); err != nil {
l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver")
useWintun = false
2019-11-19 18:00:20 +01:00
}
var inside overlay.Device
2021-11-08 19:36:31 +01:00
if useWintun {
inside, err = newWinTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen)
if err != nil {
2021-11-08 19:36:31 +01:00
return nil, fmt.Errorf("Create Wintun interface failed, %w", err)
}
} else {
inside, err = newWindowsWaterTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen)
if err != nil {
return nil, fmt.Errorf("Create wintap driver failed, %w", err)
}
}
2021-11-08 19:36:31 +01:00
return &Tun{
Device: inside,
2021-11-08 19:36:31 +01:00
}, nil
2019-11-19 18:00:20 +01:00
}
2021-11-08 19:36:31 +01:00
func checkWinTunExists() error {
myPath, err := os.Executable()
if err != nil {
return err
}
2021-11-08 19:36:31 +01:00
arch := runtime.GOARCH
switch arch {
case "386":
//NOTE: wintun bundles 386 as x86
arch = "x86"
}
2021-11-08 19:36:31 +01:00
_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
2019-11-19 18:00:20 +01:00
return err
}
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
}