nebula/overlay/tun_water_windows.go

127 lines
2.8 KiB
Go
Raw Normal View History

2021-11-11 15:37:29 -07:00
package overlay
2021-11-08 11:36:31 -07:00
import (
"fmt"
"io"
"net"
"os/exec"
"strconv"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/cidr"
"github.com/slackhq/nebula/iputil"
2021-11-08 11:36:31 -07:00
"github.com/songgao/water"
)
type waterTun struct {
Device string
cidr *net.IPNet
MTU int
Routes []Route
routeTree *cidr.Tree4
2021-11-08 11:36:31 -07:00
*water.Interface
}
func newWaterTun(l *logrus.Logger, cidr *net.IPNet, defaultMTU int, routes []Route) (*waterTun, error) {
routeTree, err := makeRouteTree(l, routes, false)
if err != nil {
return nil, err
}
2021-11-08 11:36:31 -07:00
// NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
return &waterTun{
cidr: cidr,
MTU: defaultMTU,
Routes: routes,
routeTree: routeTree,
2021-11-08 11:36:31 -07:00
}, nil
}
func (t *waterTun) Activate() error {
2021-11-08 11:36:31 -07:00
var err error
t.Interface, err = water.New(water.Config{
2021-11-08 11:36:31 -07:00
DeviceType: water.TUN,
PlatformSpecificParams: water.PlatformSpecificParams{
ComponentID: "tap0901",
Network: t.cidr.String(),
2021-11-08 11:36:31 -07:00
},
})
if err != nil {
return fmt.Errorf("activate failed: %v", err)
2021-11-08 11:36:31 -07:00
}
t.Device = t.Interface.Name()
2021-11-08 11:36:31 -07:00
// TODO use syscalls instead of exec.Command
err = exec.Command(
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address",
fmt.Sprintf("name=%s", t.Device),
2021-11-08 11:36:31 -07:00
"source=static",
fmt.Sprintf("addr=%s", t.cidr.IP),
fmt.Sprintf("mask=%s", net.IP(t.cidr.Mask)),
2021-11-08 11:36:31 -07:00
"gateway=none",
).Run()
if err != nil {
return fmt.Errorf("failed to run 'netsh' to set address: %s", err)
}
err = exec.Command(
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface",
t.Device,
fmt.Sprintf("mtu=%d", t.MTU),
2021-11-08 11:36:31 -07:00
).Run()
if err != nil {
return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
}
iface, err := net.InterfaceByName(t.Device)
2021-11-08 11:36:31 -07:00
if err != nil {
return fmt.Errorf("failed to find interface named %s: %v", t.Device, err)
2021-11-08 11:36:31 -07:00
}
for _, r := range t.Routes {
if r.Via == nil || !r.Install {
// We don't allow route MTUs so only install routes with a via
continue
}
2021-11-08 11:36:31 -07:00
err = exec.Command(
2021-11-11 15:37:29 -07:00
"C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric),
2021-11-08 11:36:31 -07:00
).Run()
if err != nil {
2021-11-11 15:37:29 -07:00
return fmt.Errorf("failed to add the unsafe_route %s: %v", r.Cidr.String(), err)
2021-11-08 11:36:31 -07:00
}
}
return nil
}
func (t *waterTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
r := t.routeTree.MostSpecificContains(ip)
if r != nil {
return r.(iputil.VpnIp)
}
return 0
}
func (t *waterTun) Cidr() *net.IPNet {
return t.cidr
2021-11-08 11:36:31 -07:00
}
func (t *waterTun) Name() string {
return t.Device
2021-11-08 11:36:31 -07:00
}
func (t *waterTun) Close() error {
if t.Interface == nil {
2021-11-08 11:36:31 -07:00
return nil
}
return t.Interface.Close()
2021-11-08 11:36:31 -07:00
}
func (t *waterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
2021-11-08 11:36:31 -07:00
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
}