2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-04-30 20:20:09 +00:00
|
|
|
package router
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-29 03:19:01 +00:00
|
|
|
"context"
|
2020-07-31 20:27:09 +00:00
|
|
|
"fmt"
|
2020-09-08 20:10:20 +00:00
|
|
|
"os/exec"
|
2020-09-16 21:41:28 +00:00
|
|
|
"sync"
|
2020-09-08 20:10:20 +00:00
|
|
|
"syscall"
|
2020-09-23 22:27:30 +00:00
|
|
|
"time"
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-09-26 02:11:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2020-10-29 03:19:01 +00:00
|
|
|
"tailscale.com/logtail/backoff"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-07-31 20:27:09 +00:00
|
|
|
"tailscale.com/wgengine/router/dns"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type winRouter struct {
|
|
|
|
logf func(fmt string, args ...interface{})
|
|
|
|
tunname string
|
|
|
|
nativeTun *tun.NativeTun
|
2020-02-14 23:03:25 +00:00
|
|
|
wgdev *device.Device
|
2020-02-05 22:16:58 +00:00
|
|
|
routeChangeCallback *winipcfg.RouteChangeCallback
|
2020-07-31 20:27:09 +00:00
|
|
|
dns *dns.Manager
|
2020-10-29 03:19:01 +00:00
|
|
|
firewall *firewallTweaker
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, wgdev *device.Device, tundev tun.Device) (Router, error) {
|
2020-02-14 23:03:25 +00:00
|
|
|
tunname, err := tundev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
|
|
|
nativeTun := tundev.(*tun.NativeTun)
|
2020-12-04 13:56:34 +00:00
|
|
|
luid := winipcfg.LUID(nativeTun.LUID())
|
|
|
|
guid, err := luid.GUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
mconfig := dns.ManagerConfig{
|
2020-11-06 19:42:34 +00:00
|
|
|
Logf: logf,
|
2020-12-04 13:56:34 +00:00
|
|
|
InterfaceName: guid.String(),
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 23:03:25 +00:00
|
|
|
return &winRouter{
|
2020-02-05 22:16:58 +00:00
|
|
|
logf: logf,
|
2020-02-14 23:03:25 +00:00
|
|
|
wgdev: wgdev,
|
2020-02-05 22:16:58 +00:00
|
|
|
tunname: tunname,
|
2020-07-31 20:27:09 +00:00
|
|
|
nativeTun: nativeTun,
|
|
|
|
dns: dns.NewManager(mconfig),
|
2020-10-29 03:19:01 +00:00
|
|
|
firewall: &firewallTweaker{logf: logger.WithPrefix(logf, "firewall: ")},
|
2020-02-14 23:03:25 +00:00
|
|
|
}, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *winRouter) Up() error {
|
2020-10-29 03:19:01 +00:00
|
|
|
r.firewall.clear()
|
2020-09-08 20:10:20 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
var err error
|
2020-09-23 22:27:30 +00:00
|
|
|
t0 := time.Now()
|
2020-09-10 18:40:02 +00:00
|
|
|
r.routeChangeCallback, err = monitorDefaultRoutes(r.nativeTun)
|
2020-09-23 22:27:30 +00:00
|
|
|
d := time.Since(t0).Round(time.Millisecond)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-23 22:27:30 +00:00
|
|
|
return fmt.Errorf("monitorDefaultRoutes, after %v: %v", d, err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-09-23 22:27:30 +00:00
|
|
|
r.logf("monitorDefaultRoutes done after %v", d)
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
func (r *winRouter) Set(cfg *Config) error {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &shutdownConfig
|
|
|
|
}
|
|
|
|
|
2020-10-29 03:19:01 +00:00
|
|
|
var localAddrs []string
|
|
|
|
for _, la := range cfg.LocalAddrs {
|
|
|
|
localAddrs = append(localAddrs, la.String())
|
2020-09-08 20:10:20 +00:00
|
|
|
}
|
2020-10-29 03:19:01 +00:00
|
|
|
r.firewall.set(localAddrs)
|
2020-09-08 20:10:20 +00:00
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
err := configureInterface(cfg, r.nativeTun)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-01 20:27:42 +00:00
|
|
|
r.logf("ConfigureInterface: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
|
|
|
if err := r.dns.Set(cfg.DNS); err != nil {
|
|
|
|
return fmt.Errorf("dns set: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:16:05 +00:00
|
|
|
func (r *winRouter) Close() error {
|
2020-10-29 03:19:01 +00:00
|
|
|
r.firewall.clear()
|
2020-09-08 20:10:20 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
if err := r.dns.Down(); err != nil {
|
|
|
|
return fmt.Errorf("dns down: %w", err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if r.routeChangeCallback != nil {
|
|
|
|
r.routeChangeCallback.Unregister()
|
|
|
|
}
|
2020-09-10 18:40:02 +00:00
|
|
|
|
2020-02-13 00:16:05 +00:00
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
func cleanup(logf logger.Logf, interfaceName string) {
|
2020-07-31 20:27:09 +00:00
|
|
|
// Nothing to do here.
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2020-10-29 03:19:01 +00:00
|
|
|
|
|
|
|
// firewallTweaker changes the Windows firewall. Normally this wouldn't be so complicated,
|
|
|
|
// but it can be REALLY SLOW to change the Windows firewall for reasons not understood.
|
|
|
|
// Like 4 minutes slow. But usually it's tens of milliseconds.
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/785.
|
|
|
|
// So this tracks the desired state and runs the actual adjusting code asynchrounsly.
|
|
|
|
type firewallTweaker struct {
|
|
|
|
logf logger.Logf
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
running bool // doAsyncSet goroutine is running
|
|
|
|
known bool // firewall is in known state (in lastVal)
|
|
|
|
want []string // next value we want, or "" to delete the firewall rule
|
|
|
|
lastVal []string // last set value, if known
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *firewallTweaker) clear() { ft.set(nil) }
|
|
|
|
|
|
|
|
// set takes the IPv4 and/or IPv6 CIDRs to allow; an empty slice
|
|
|
|
// removes the firwall rules.
|
|
|
|
//
|
|
|
|
// set takes ownership of the slice.
|
|
|
|
func (ft *firewallTweaker) set(cidrs []string) {
|
|
|
|
ft.mu.Lock()
|
|
|
|
defer ft.mu.Unlock()
|
|
|
|
|
|
|
|
if len(cidrs) == 0 {
|
|
|
|
ft.logf("marking for removal")
|
|
|
|
} else {
|
|
|
|
ft.logf("marking allowed %v", cidrs)
|
|
|
|
}
|
|
|
|
ft.want = cidrs
|
|
|
|
if ft.running {
|
|
|
|
// The doAsyncSet goroutine will check ft.want
|
|
|
|
// before returning.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ft.logf("starting netsh goroutine")
|
|
|
|
ft.running = true
|
|
|
|
go ft.doAsyncSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *firewallTweaker) runFirewall(args ...string) (time.Duration, error) {
|
|
|
|
t0 := time.Now()
|
|
|
|
args = append([]string{"advfirewall", "firewall"}, args...)
|
|
|
|
cmd := exec.Command("netsh", args...)
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
|
|
err := cmd.Run()
|
|
|
|
return time.Since(t0).Round(time.Millisecond), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *firewallTweaker) doAsyncSet() {
|
|
|
|
bo := backoff.NewBackoff("win-firewall", ft.logf, time.Minute)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ft.mu.Lock()
|
|
|
|
for { // invariant: ft.mu must be locked when beginning this block
|
|
|
|
val := ft.want
|
|
|
|
if ft.known && strsEqual(ft.lastVal, val) {
|
|
|
|
ft.running = false
|
|
|
|
ft.logf("ending netsh goroutine")
|
|
|
|
ft.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
needClear := !ft.known || len(ft.lastVal) > 0 || len(val) == 0
|
|
|
|
ft.mu.Unlock()
|
|
|
|
|
|
|
|
if needClear {
|
|
|
|
ft.logf("clearing Tailscale-In firewall rules...")
|
|
|
|
// We ignore the error here, because netsh returns an error for
|
|
|
|
// deleting something that doesn't match.
|
|
|
|
// TODO(bradfitz): care? That'd involve querying it before/after to see
|
|
|
|
// whether it was necessary/worked. But the output format is localized,
|
|
|
|
// so can't rely on parsing English. Maybe need to use OLE, not netsh.exe?
|
|
|
|
d, _ := ft.runFirewall("delete", "rule", "name=Tailscale-In", "dir=in")
|
|
|
|
ft.logf("cleared Tailscale-In firewall rules in %v", d)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
for _, cidr := range val {
|
|
|
|
ft.logf("adding Tailscale-In rule to allow %v ...", cidr)
|
|
|
|
var d time.Duration
|
|
|
|
d, err = ft.runFirewall("add", "rule", "name=Tailscale-In", "dir=in", "action=allow", "localip="+cidr, "profile=private", "enable=yes")
|
|
|
|
if err != nil {
|
|
|
|
ft.logf("error adding Tailscale-In rule to allow %v: %v", cidr, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ft.logf("added Tailscale-In rule to allow %v in %v", cidr, d)
|
|
|
|
}
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
|
|
|
|
ft.mu.Lock()
|
|
|
|
ft.lastVal = val
|
|
|
|
ft.known = (err == nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strsEqual(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|