2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2021-11-08 11:36:31 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2024-07-31 09:18:56 -06:00
|
|
|
"net/netip"
|
2021-11-08 11:36:31 -07:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2024-03-28 14:17:28 -06:00
|
|
|
"sync/atomic"
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
"github.com/gaissmai/bart"
|
2021-12-14 09:52:28 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2024-03-28 14:17:28 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/util"
|
2021-11-08 11:36:31 -07:00
|
|
|
"github.com/songgao/water"
|
|
|
|
)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type waterTun struct {
|
2021-11-12 11:47:09 -07:00
|
|
|
Device string
|
2024-07-31 09:18:56 -06:00
|
|
|
cidr netip.Prefix
|
2021-11-12 11:47:09 -07:00
|
|
|
MTU int
|
2024-03-28 14:17:28 -06:00
|
|
|
Routes atomic.Pointer[[]Route]
|
2024-07-31 09:18:56 -06:00
|
|
|
routeTree atomic.Pointer[bart.Table[netip.Addr]]
|
2024-03-28 14:17:28 -06:00
|
|
|
l *logrus.Logger
|
|
|
|
f *net.Interface
|
2021-11-08 11:36:31 -07:00
|
|
|
*water.Interface
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func newWaterTun(c *config.C, l *logrus.Logger, cidr netip.Prefix, _ bool) (*waterTun, error) {
|
2024-03-28 14:17:28 -06:00
|
|
|
// 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)
|
2021-11-12 10:19:28 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *waterTun) Activate() error {
|
2021-11-08 11:36:31 -07:00
|
|
|
var err error
|
2021-11-12 09:47:36 -07:00
|
|
|
t.Interface, err = water.New(water.Config{
|
2021-11-08 11:36:31 -07:00
|
|
|
DeviceType: water.TUN,
|
|
|
|
PlatformSpecificParams: water.PlatformSpecificParams{
|
|
|
|
ComponentID: "tap0901",
|
2021-11-12 11:47:09 -07:00
|
|
|
Network: t.cidr.String(),
|
2021-11-08 11:36:31 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-12 09:47:36 -07:00
|
|
|
return fmt.Errorf("activate failed: %v", err)
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -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",
|
2021-11-12 09:47:36 -07:00
|
|
|
fmt.Sprintf("name=%s", t.Device),
|
2021-11-08 11:36:31 -07:00
|
|
|
"source=static",
|
2024-07-31 09:18:56 -06:00
|
|
|
fmt.Sprintf("addr=%s", t.cidr.Addr()),
|
|
|
|
fmt.Sprintf("mask=%s", net.CIDRMask(t.cidr.Bits(), t.cidr.Addr().BitLen())),
|
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",
|
2021-11-12 09:47:36 -07:00
|
|
|
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 {
|
2021-11-12 09:47:36 -07:00
|
|
|
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 {
|
2024-07-31 09:18:56 -06:00
|
|
|
if !r.Via.IsValid() || !r.Install {
|
2021-11-12 10:19:28 -07:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (t *waterTun) RouteFor(ip netip.Addr) netip.Addr {
|
|
|
|
r, _ := t.routeTree.Load().Lookup(ip)
|
2023-11-02 16:05:08 -06:00
|
|
|
return r
|
2021-11-12 10:19:28 -07:00
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (t *waterTun) Cidr() netip.Prefix {
|
2021-11-12 11:47:09 -07:00
|
|
|
return t.cidr
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *waterTun) Name() string {
|
2021-11-12 09:47:36 -07:00
|
|
|
return t.Device
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *waterTun) Close() error {
|
|
|
|
if t.Interface == nil {
|
2021-11-08 11:36:31 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
return t.Interface.Close()
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -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")
|
|
|
|
}
|