2021-10-21 15:24:11 -06:00
|
|
|
//go:build !e2e_testing
|
2021-03-31 09:26:35 -06:00
|
|
|
// +build !e2e_testing
|
|
|
|
|
2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-11-08 11:36:31 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2024-03-28 14:17:28 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ *net.IPNet) (Device, error) {
|
2020-06-30 12:48:58 -06:00
|
|
|
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTun(c *config.C, l *logrus.Logger, cidr *net.IPNet, multiqueue bool) (Device, error) {
|
2021-11-08 11:36:31 -07:00
|
|
|
useWintun := true
|
2021-11-12 09:47:36 -07:00
|
|
|
if err := checkWinTunExists(); err != nil {
|
2021-11-08 11:36:31 -07:00
|
|
|
l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver")
|
|
|
|
useWintun = false
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:36:31 -07:00
|
|
|
if useWintun {
|
2024-03-28 14:17:28 -06:00
|
|
|
device, err := newWinTun(c, l, cidr, multiqueue)
|
2021-11-08 11:36:31 -07:00
|
|
|
if err != nil {
|
2021-11-12 09:47:36 -07:00
|
|
|
return nil, fmt.Errorf("create Wintun interface failed, %w", err)
|
2020-02-26 13:23:16 -07:00
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
return device, nil
|
2020-02-26 13:23:16 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
device, err := newWaterTun(c, l, cidr, multiqueue)
|
2021-11-12 09:47:36 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create wintap driver failed, %w", err)
|
|
|
|
}
|
|
|
|
return device, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:36:31 -07:00
|
|
|
func checkWinTunExists() error {
|
|
|
|
myPath, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-28 06:53:16 -06:00
|
|
|
|
2021-11-08 11:36:31 -07:00
|
|
|
arch := runtime.GOARCH
|
|
|
|
switch arch {
|
|
|
|
case "386":
|
|
|
|
//NOTE: wintun bundles 386 as x86
|
|
|
|
arch = "x86"
|
|
|
|
}
|
2020-07-28 06:53:16 -06:00
|
|
|
|
2021-11-08 11:36:31 -07:00
|
|
|
_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
|
2019-11-19 10:00:20 -07:00
|
|
|
return err
|
|
|
|
}
|