2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2021-11-08 11:36:31 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-04-18 10:12:25 -06:00
|
|
|
"net/netip"
|
2024-03-28 14:17:28 -06:00
|
|
|
"sync/atomic"
|
2021-11-08 11:36:31 -07:00
|
|
|
"unsafe"
|
|
|
|
|
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/slackhq/nebula/wintun"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tunGUIDLabel = "Fixed Nebula Windows GUID v1"
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type winTun 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
|
2021-11-08 11:36:31 -07:00
|
|
|
|
|
|
|
tun *wintun.NativeTun
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateGUIDByDeviceName(name string) (*windows.GUID, error) {
|
|
|
|
// GUID is 128 bit
|
|
|
|
hash := crypto.MD5.New()
|
|
|
|
|
|
|
|
_, err := hash.Write([]byte(tunGUIDLabel))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = hash.Write([]byte(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := hash.Sum(nil)
|
|
|
|
|
|
|
|
return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func newWinTun(c *config.C, l *logrus.Logger, cidr netip.Prefix, _ bool) (*winTun, error) {
|
2024-03-28 14:17:28 -06:00
|
|
|
deviceName := c.GetString("tun.dev", "")
|
2021-11-08 11:36:31 -07:00
|
|
|
guid, err := generateGUIDByDeviceName(deviceName)
|
|
|
|
if err != nil {
|
2021-11-12 09:47:36 -07:00
|
|
|
return nil, fmt.Errorf("generate GUID failed: %w", err)
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
t := &winTun{
|
|
|
|
Device: deviceName,
|
|
|
|
cidr: cidr,
|
|
|
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
|
|
|
l: l,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.reload(c, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:06:43 -06:00
|
|
|
var tunDevice wintun.Device
|
2024-03-28 14:17:28 -06:00
|
|
|
tunDevice, err = wintun.CreateTUNWithRequestedGUID(deviceName, guid, t.MTU)
|
2021-11-08 11:36:31 -07:00
|
|
|
if err != nil {
|
2023-10-16 09:06:43 -06:00
|
|
|
// Windows 10 has an issue with unclean shutdowns not fully cleaning up the wintun device.
|
|
|
|
// Trying a second time resolves the issue.
|
|
|
|
l.WithError(err).Debug("Failed to create wintun device, retrying")
|
2024-03-28 14:17:28 -06:00
|
|
|
tunDevice, err = wintun.CreateTUNWithRequestedGUID(deviceName, guid, t.MTU)
|
2023-10-16 09:06:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create TUN device failed: %w", err)
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
2024-03-28 14:17:28 -06:00
|
|
|
t.tun = tunDevice.(*wintun.NativeTun)
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func (t *winTun) reload(c *config.C, initial bool) error {
|
|
|
|
change, routes, err := getAllRoutesFromConfig(c, t.cidr, initial)
|
2021-11-12 10:19:28 -07:00
|
|
|
if err != nil {
|
2024-03-28 14:17:28 -06:00
|
|
|
return err
|
2021-11-12 10:19:28 -07:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
if !initial && !change {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
routeTree, err := makeRouteTree(t.l, routes, false)
|
2022-04-18 10:12:25 -06:00
|
|
|
if err != nil {
|
2024-03-28 14:17:28 -06:00
|
|
|
return err
|
2022-04-18 10:12:25 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
// Teach nebula how to handle the routes before establishing them in the system table
|
|
|
|
oldRoutes := t.Routes.Swap(&routes)
|
|
|
|
t.routeTree.Store(routeTree)
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
if !initial {
|
|
|
|
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
|
|
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
|
|
if err != nil {
|
|
|
|
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure any routes we actually want are installed
|
|
|
|
err = t.addRoutes(true)
|
|
|
|
if err != nil {
|
|
|
|
// Catch any stray logs
|
|
|
|
util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *winTun) Activate() error {
|
|
|
|
luid := winipcfg.LUID(t.tun.LUID())
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
err := luid.SetIPAddresses([]netip.Prefix{t.cidr})
|
2024-03-28 14:17:28 -06:00
|
|
|
if err != nil {
|
2021-11-08 11:36:31 -07:00
|
|
|
return fmt.Errorf("failed to set address: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
err = t.addRoutes(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *winTun) addRoutes(logErrors bool) error {
|
|
|
|
luid := winipcfg.LUID(t.tun.LUID())
|
|
|
|
routes := *t.Routes.Load()
|
2021-11-08 11:36:31 -07:00
|
|
|
foundDefault4 := false
|
2021-11-12 10:19:28 -07:00
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
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
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
|
|
|
|
// Add our unsafe route
|
2024-07-31 09:18:56 -06:00
|
|
|
err := luid.AddRoute(r.Cidr, r.Via, uint32(r.Metric))
|
2024-03-28 14:17:28 -06:00
|
|
|
if err != nil {
|
|
|
|
retErr := util.NewContextualError("Failed to add route", map[string]interface{}{"route": r}, err)
|
|
|
|
if logErrors {
|
|
|
|
retErr.Log(t.l)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.l.WithField("route", r).Info("Added route")
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
if !foundDefault4 {
|
2024-07-31 09:18:56 -06:00
|
|
|
if r.Cidr.Bits() == 0 && r.Cidr.Addr().BitLen() == 32 {
|
2024-03-28 14:17:28 -06:00
|
|
|
foundDefault4 = true
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ipif, err := luid.IPInterface(windows.AF_INET)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get ip interface: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
ipif.NLMTU = uint32(t.MTU)
|
2021-11-08 11:36:31 -07:00
|
|
|
if foundDefault4 {
|
|
|
|
ipif.UseAutomaticMetric = false
|
|
|
|
ipif.Metric = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ipif.Set(); err != nil {
|
|
|
|
return fmt.Errorf("failed to set ip interface: %w", err)
|
|
|
|
}
|
2024-03-28 14:17:28 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *winTun) removeRoutes(routes []Route) error {
|
|
|
|
luid := winipcfg.LUID(t.tun.LUID())
|
|
|
|
|
|
|
|
for _, r := range routes {
|
|
|
|
if !r.Install {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
err := luid.DeleteRoute(r.Cidr, r.Via)
|
2024-03-28 14:17:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
|
|
|
} else {
|
|
|
|
t.l.WithField("route", r).Info("Removed route")
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (t *winTun) 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 *winTun) 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 *winTun) 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 *winTun) Read(b []byte) (int, error) {
|
|
|
|
return t.tun.Read(b, 0)
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *winTun) Write(b []byte) (int, error) {
|
|
|
|
return t.tun.Write(b, 0)
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-11-08 11:36:31 -07:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *winTun) Close() error {
|
2021-11-08 11:36:31 -07:00
|
|
|
// It seems that the Windows networking stack doesn't like it when we destroy interfaces that have active routes,
|
|
|
|
// so to be certain, just remove everything before destroying.
|
2021-11-12 09:47:36 -07:00
|
|
|
luid := winipcfg.LUID(t.tun.LUID())
|
2021-11-08 11:36:31 -07:00
|
|
|
_ = luid.FlushRoutes(windows.AF_INET)
|
|
|
|
_ = luid.FlushIPAddresses(windows.AF_INET)
|
|
|
|
/* We don't support IPV6 yet
|
|
|
|
_ = luid.FlushRoutes(windows.AF_INET6)
|
|
|
|
_ = luid.FlushIPAddresses(windows.AF_INET6)
|
|
|
|
*/
|
|
|
|
_ = luid.FlushDNS(windows.AF_INET)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
return t.tun.Close()
|
2021-11-08 11:36:31 -07:00
|
|
|
}
|