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 (
|
2021-03-02 00:24:26 +00:00
|
|
|
"bufio"
|
2020-10-29 03:19:01 +00:00
|
|
|
"context"
|
2021-06-16 15:53:08 +00:00
|
|
|
"encoding/json"
|
2020-07-31 20:27:09 +00:00
|
|
|
"fmt"
|
2021-03-02 00:24:26 +00:00
|
|
|
"io"
|
2021-02-10 20:46:29 +00:00
|
|
|
"os"
|
2020-09-08 20:10:20 +00:00
|
|
|
"os/exec"
|
2021-03-02 00:24:26 +00:00
|
|
|
"strings"
|
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
|
|
|
|
2021-03-02 00:24:26 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2021-05-25 19:42:22 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2020-09-26 02:11:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2021-03-02 00:24:26 +00:00
|
|
|
"inet.af/netaddr"
|
2020-10-29 03:19:01 +00:00
|
|
|
"tailscale.com/logtail/backoff"
|
2021-03-25 22:30:05 +00:00
|
|
|
"tailscale.com/net/dns"
|
2021-03-26 00:40:52 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-07-20 20:28:06 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type winRouter struct {
|
|
|
|
logf func(fmt string, args ...interface{})
|
2021-07-20 20:28:06 +00:00
|
|
|
linkMon *monitor.Mon // may be nil
|
2020-02-05 22:16:58 +00:00
|
|
|
nativeTun *tun.NativeTun
|
|
|
|
routeChangeCallback *winipcfg.RouteChangeCallback
|
2020-10-29 03:19:01 +00:00
|
|
|
firewall *firewallTweaker
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 20:28:06 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, tundev tun.Device, linkMon *monitor.Mon) (Router, error) {
|
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-02-14 23:03:25 +00:00
|
|
|
return &winRouter{
|
2020-02-05 22:16:58 +00:00
|
|
|
logf: logf,
|
2021-07-20 20:28:06 +00:00
|
|
|
linkMon: linkMon,
|
2020-07-31 20:27:09 +00:00
|
|
|
nativeTun: nativeTun,
|
2021-03-02 00:24:26 +00:00
|
|
|
firewall: &firewallTweaker{
|
|
|
|
logf: logger.WithPrefix(logf, "firewall: "),
|
|
|
|
tunGUID: *guid,
|
|
|
|
},
|
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
|
|
|
}
|
2021-06-16 15:53:08 +00:00
|
|
|
r.firewall.set(localAddrs, cfg.Routes, cfg.LocalRoutes)
|
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
|
|
|
|
2021-03-10 09:44:38 +00:00
|
|
|
// Flush DNS on router config change to clear cached DNS entries (solves #1430)
|
2021-03-26 00:40:52 +00:00
|
|
|
if err := dns.Flush(); err != nil {
|
|
|
|
r.logf("flushdns error: %v", err)
|
2021-03-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:24:26 +00:00
|
|
|
func hasDefaultRoute(routes []netaddr.IPPrefix) bool {
|
|
|
|
for _, route := range routes {
|
2021-05-15 01:07:28 +00:00
|
|
|
if route.Bits() == 0 {
|
2021-03-02 00:24:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-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 {
|
2021-03-02 00:24:26 +00:00
|
|
|
logf logger.Logf
|
|
|
|
tunGUID windows.GUID
|
|
|
|
|
2021-06-16 15:53:08 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
didProcRule bool
|
|
|
|
running bool // doAsyncSet goroutine is running
|
|
|
|
known bool // firewall is in known state (in lastVal)
|
|
|
|
wantLocal []string // next value we want, or "" to delete the firewall rule
|
|
|
|
lastLocal []string // last set value, if known
|
|
|
|
|
|
|
|
localRoutes []netaddr.IPPrefix
|
|
|
|
lastLocalRoutes []netaddr.IPPrefix
|
|
|
|
|
2021-03-02 00:24:26 +00:00
|
|
|
wantKillswitch bool
|
|
|
|
lastKillswitch bool
|
|
|
|
|
|
|
|
// Only touched by doAsyncSet, so mu doesn't need to be held.
|
|
|
|
|
|
|
|
// fwProc is a subprocess that runs the wireguard-windows firewall
|
|
|
|
// killswitch code. It is only non-nil when the default route
|
|
|
|
// killswitch is active, and may go back and forth between nil and
|
|
|
|
// non-nil any number of times during the process's lifetime.
|
|
|
|
fwProc *exec.Cmd
|
|
|
|
// stop makes fwProc exit when closed.
|
2021-06-16 15:53:08 +00:00
|
|
|
fwProcWriter io.WriteCloser
|
|
|
|
fwProcEncoder *json.Encoder
|
2020-10-29 03:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 15:53:08 +00:00
|
|
|
func (ft *firewallTweaker) clear() { ft.set(nil, nil, nil) }
|
2020-10-29 03:19:01 +00:00
|
|
|
|
2021-03-02 00:24:26 +00:00
|
|
|
// set takes CIDRs to allow, and the routes that point into the Tailscale tun interface.
|
|
|
|
// Empty slices remove firewall rules.
|
2020-10-29 03:19:01 +00:00
|
|
|
//
|
2021-03-02 00:24:26 +00:00
|
|
|
// set takes ownership of cidrs, but not routes.
|
2021-06-16 15:53:08 +00:00
|
|
|
func (ft *firewallTweaker) set(cidrs []string, routes, localRoutes []netaddr.IPPrefix) {
|
2020-10-29 03:19:01 +00:00
|
|
|
ft.mu.Lock()
|
|
|
|
defer ft.mu.Unlock()
|
|
|
|
|
|
|
|
if len(cidrs) == 0 {
|
|
|
|
ft.logf("marking for removal")
|
|
|
|
} else {
|
|
|
|
ft.logf("marking allowed %v", cidrs)
|
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.wantLocal = cidrs
|
2021-06-16 15:53:08 +00:00
|
|
|
ft.localRoutes = localRoutes
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.wantKillswitch = hasDefaultRoute(routes)
|
2020-10-29 03:19:01 +00:00
|
|
|
if ft.running {
|
2021-03-02 00:24:26 +00:00
|
|
|
// The doAsyncSet goroutine will check ft.wantLocal/wantKillswitch
|
2020-10-29 03:19:01 +00:00
|
|
|
// 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
|
2021-03-02 00:24:26 +00:00
|
|
|
val := ft.wantLocal
|
2021-06-16 15:53:08 +00:00
|
|
|
if ft.known && strsEqual(ft.lastLocal, val) && ft.wantKillswitch == ft.lastKillswitch && routesEqual(ft.localRoutes, ft.lastLocalRoutes) {
|
2020-10-29 03:19:01 +00:00
|
|
|
ft.running = false
|
|
|
|
ft.logf("ending netsh goroutine")
|
|
|
|
ft.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
wantKillswitch := ft.wantKillswitch
|
|
|
|
needClear := !ft.known || len(ft.lastLocal) > 0 || len(val) == 0
|
2021-02-10 20:46:29 +00:00
|
|
|
needProcRule := !ft.didProcRule
|
2021-06-16 15:53:08 +00:00
|
|
|
localRoutes := ft.localRoutes
|
2020-10-29 03:19:01 +00:00
|
|
|
ft.mu.Unlock()
|
|
|
|
|
2021-06-16 15:53:08 +00:00
|
|
|
err := ft.doSet(val, wantKillswitch, needClear, needProcRule, localRoutes)
|
|
|
|
if err != nil {
|
|
|
|
ft.logf("set failed: %v", err)
|
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
|
|
|
|
ft.mu.Lock()
|
|
|
|
ft.lastLocal = val
|
2021-06-16 15:53:08 +00:00
|
|
|
ft.lastLocalRoutes = localRoutes
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.lastKillswitch = wantKillswitch
|
|
|
|
ft.known = (err == nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doSet creates and deletes firewall rules to make the system state
|
|
|
|
// match the values of local, killswitch, clear and procRule.
|
|
|
|
//
|
|
|
|
// local is the list of local Tailscale addresses (formatted as CIDR
|
|
|
|
// prefixes) to allow through the Windows firewall.
|
|
|
|
// killswitch, if true, enables the wireguard-windows based internet
|
|
|
|
// killswitch to prevent use of non-Tailscale default routes.
|
|
|
|
// clear, if true, removes all tailscale address firewall rules before
|
|
|
|
// adding local.
|
|
|
|
// procRule, if true, installs a firewall rule that permits the Tailscale
|
|
|
|
// process to dial out as it pleases.
|
|
|
|
//
|
|
|
|
// Must only be invoked from doAsyncSet.
|
2021-06-16 15:53:08 +00:00
|
|
|
func (ft *firewallTweaker) doSet(local []string, killswitch bool, clear bool, procRule bool, allowedRoutes []netaddr.IPPrefix) error {
|
2021-03-02 00:24:26 +00:00
|
|
|
if clear {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if procRule {
|
|
|
|
ft.logf("deleting any prior Tailscale-Process rule...")
|
|
|
|
d, err := ft.runFirewall("delete", "rule", "name=Tailscale-Process", "dir=in") // best effort
|
|
|
|
if err == nil {
|
|
|
|
ft.logf("removed old Tailscale-Process rule in %v", d)
|
2020-10-29 03:19:01 +00:00
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
var exe string
|
|
|
|
exe, err = os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
ft.logf("failed to find Executable for Tailscale-Process rule: %v", err)
|
|
|
|
} else {
|
|
|
|
ft.logf("adding Tailscale-Process rule to allow UDP for %q ...", exe)
|
|
|
|
d, err = ft.runFirewall("add", "rule", "name=Tailscale-Process",
|
|
|
|
"dir=in",
|
|
|
|
"action=allow",
|
|
|
|
"edge=yes",
|
|
|
|
"program="+exe,
|
|
|
|
"protocol=udp",
|
|
|
|
"profile=any",
|
|
|
|
"enable=yes",
|
|
|
|
)
|
2021-02-10 20:46:29 +00:00
|
|
|
if err != nil {
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.logf("error adding Tailscale-Process rule: %v", err)
|
2021-02-10 20:46:29 +00:00
|
|
|
} else {
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.mu.Lock()
|
|
|
|
ft.didProcRule = true
|
|
|
|
ft.mu.Unlock()
|
|
|
|
ft.logf("added Tailscale-Process rule in %v", d)
|
2021-02-10 20:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
}
|
|
|
|
for _, cidr := range local {
|
|
|
|
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)
|
|
|
|
return err
|
2020-10-29 03:19:01 +00:00
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.logf("added Tailscale-In rule to allow %v in %v", cidr, d)
|
|
|
|
}
|
2020-10-29 03:19:01 +00:00
|
|
|
|
2021-06-16 15:53:08 +00:00
|
|
|
if !killswitch {
|
|
|
|
if ft.fwProc != nil {
|
|
|
|
ft.fwProcWriter.Close()
|
|
|
|
ft.fwProcWriter = nil
|
|
|
|
ft.fwProc.Wait()
|
|
|
|
ft.fwProc = nil
|
|
|
|
ft.fwProcEncoder = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ft.fwProc == nil {
|
2021-03-02 00:24:26 +00:00
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
proc := exec.Command(exe, "/firewall", ft.tunGUID.String())
|
2021-06-16 15:53:08 +00:00
|
|
|
in, err := proc.StdinPipe()
|
2021-03-02 00:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-16 15:53:08 +00:00
|
|
|
out, err := proc.StdoutPipe()
|
2021-03-02 00:24:26 +00:00
|
|
|
if err != nil {
|
2021-06-16 15:53:08 +00:00
|
|
|
in.Close()
|
2021-03-02 00:24:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(out io.ReadCloser) {
|
|
|
|
b := bufio.NewReaderSize(out, 1<<10)
|
|
|
|
for {
|
|
|
|
line, err := b.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line != "" {
|
|
|
|
ft.logf("fw-child: %s", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(out)
|
2021-06-16 15:53:08 +00:00
|
|
|
proc.Stderr = proc.Stdout
|
2021-03-02 00:24:26 +00:00
|
|
|
|
|
|
|
if err := proc.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-16 15:53:08 +00:00
|
|
|
ft.fwProcWriter = in
|
2021-03-02 00:24:26 +00:00
|
|
|
ft.fwProc = proc
|
2021-06-16 15:53:08 +00:00
|
|
|
ft.fwProcEncoder = json.NewEncoder(in)
|
2020-10-29 03:19:01 +00:00
|
|
|
}
|
2021-06-16 15:53:08 +00:00
|
|
|
// Note(maisem): when local lan access toggled, we need to inform the
|
|
|
|
// firewall to let the local routes through. The set of routes is passed
|
|
|
|
// in via stdin encoded in json.
|
|
|
|
return ft.fwProcEncoder.Encode(allowedRoutes)
|
|
|
|
}
|
2021-03-02 00:24:26 +00:00
|
|
|
|
2021-06-16 15:53:08 +00:00
|
|
|
func routesEqual(a, b []netaddr.IPPrefix) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Routes are pre-sorted.
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2020-10-29 03:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|