nebula/overlay/tun_water_windows.go

209 lines
4.7 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"
2024-03-28 14:17:28 -06:00
"sync/atomic"
2021-11-08 11:36:31 -07:00
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/cidr"
2024-03-28 14:17:28 -06:00
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/iputil"
2024-03-28 14:17:28 -06:00
"github.com/slackhq/nebula/util"
2021-11-08 11:36:31 -07:00
"github.com/songgao/water"
)
type waterTun struct {
Device string
cidr *net.IPNet
MTU int
2024-03-28 14:17:28 -06:00
Routes atomic.Pointer[[]Route]
routeTree atomic.Pointer[cidr.Tree4[iputil.VpnIp]]
l *logrus.Logger
f *net.Interface
2021-11-08 11:36:31 -07:00
*water.Interface
}
2024-03-28 14:17:28 -06:00
func newWaterTun(c *config.C, l *logrus.Logger, cidr *net.IPNet, _ bool) (*waterTun, error) {
// NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
t := &waterTun{
cidr: cidr,
MTU: c.GetInt("tun.mtu", DefaultMTU),
l: l,
}
err := t.reload(c, true)
if err != nil {
return nil, err
}
2024-03-28 14:17:28 -06:00
c.RegisterReloadCallback(func(c *config.C) {
err := t.reload(c, false)
if err != nil {
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
}
})
return t, nil
2021-11-08 11:36:31 -07:00
}
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)
}
2024-03-28 14:17:28 -06:00
t.f, 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
}
2024-03-28 14:17:28 -06:00
err = t.addRoutes(false)
if err != nil {
return err
}
return nil
}
func (t *waterTun) reload(c *config.C, initial bool) error {
change, routes, err := getAllRoutesFromConfig(c, t.cidr, initial)
if err != nil {
return err
}
if !initial && !change {
return nil
}
routeTree, err := makeRouteTree(t.l, routes, false)
if err != nil {
return err
}
// Teach nebula how to handle the routes before establishing them in the system table
oldRoutes := t.Routes.Swap(&routes)
t.routeTree.Store(routeTree)
if !initial {
// Remove first, if the system removes a wanted route hopefully it will be re-added next
t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
// Ensure any routes we actually want are installed
err = t.addRoutes(true)
if err != nil {
// Catch any stray logs
util.LogWithContextIfNeeded("Failed to set routes", err, t.l)
} else {
for _, r := range findRemovedRoutes(routes, *oldRoutes) {
t.l.WithField("route", r).Info("Removed route")
}
}
}
return nil
}
func (t *waterTun) addRoutes(logErrors bool) error {
// Path routes
routes := *t.Routes.Load()
for _, r := range routes {
if r.Via == nil || !r.Install {
// We don't allow route MTUs so only install routes with a via
continue
}
2024-03-28 14:17:28 -06:00
err := exec.Command(
"C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(t.f.Index), "METRIC", strconv.Itoa(r.Metric),
2021-11-08 11:36:31 -07:00
).Run()
2024-03-28 14:17:28 -06:00
2021-11-08 11:36:31 -07:00
if err != nil {
2024-03-28 14:17:28 -06:00
retErr := util.NewContextualError("Failed to add route", map[string]interface{}{"route": r}, err)
if logErrors {
retErr.Log(t.l)
} else {
return retErr
}
} else {
t.l.WithField("route", r).Info("Added route")
2021-11-08 11:36:31 -07:00
}
}
return nil
}
2024-03-28 14:17:28 -06:00
func (t *waterTun) removeRoutes(routes []Route) {
for _, r := range routes {
if !r.Install {
continue
}
err := exec.Command(
"C:\\Windows\\System32\\route.exe", "delete", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(t.f.Index), "METRIC", strconv.Itoa(r.Metric),
).Run()
if err != nil {
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
} else {
t.l.WithField("route", r).Info("Removed route")
}
}
}
func (t *waterTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
2024-03-28 14:17:28 -06:00
_, r := t.routeTree.Load().MostSpecificContains(ip)
return r
}
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")
}