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.
2021-02-04 21:12:42 +00:00
package ipnlocal
2020-02-05 22:16:58 +00:00
import (
2020-09-28 22:28:26 +00:00
"bytes"
2020-03-14 03:53:58 +00:00
"context"
2020-02-03 18:35:52 +00:00
"errors"
2020-02-05 22:16:58 +00:00
"fmt"
2020-09-17 14:59:55 +00:00
"os"
2020-10-21 19:55:03 +00:00
"runtime"
2020-02-05 22:16:58 +00:00
"strings"
"sync"
"time"
2020-07-13 20:13:11 +00:00
"golang.org/x/oauth2"
2020-05-11 21:02:12 +00:00
"inet.af/netaddr"
2020-02-05 22:16:58 +00:00
"tailscale.com/control/controlclient"
2020-07-29 01:47:23 +00:00
"tailscale.com/internal/deepprint"
2021-02-04 21:12:42 +00:00
"tailscale.com/ipn"
2020-03-26 05:57:46 +00:00
"tailscale.com/ipn/ipnstate"
2020-04-01 04:48:33 +00:00
"tailscale.com/ipn/policy"
2020-08-28 04:25:17 +00:00
"tailscale.com/net/interfaces"
2020-07-31 20:27:09 +00:00
"tailscale.com/net/tsaddr"
2020-02-05 22:16:58 +00:00
"tailscale.com/portlist"
"tailscale.com/tailcfg"
2020-02-14 21:09:19 +00:00
"tailscale.com/types/empty"
2020-03-26 05:57:46 +00:00
"tailscale.com/types/key"
2020-02-15 03:23:16 +00:00
"tailscale.com/types/logger"
2021-02-05 23:44:46 +00:00
"tailscale.com/types/netmap"
2021-02-05 23:23:01 +00:00
"tailscale.com/types/persist"
2020-12-30 01:22:56 +00:00
"tailscale.com/types/wgkey"
2020-11-24 23:35:04 +00:00
"tailscale.com/util/systemd"
2020-02-05 22:16:58 +00:00
"tailscale.com/version"
"tailscale.com/wgengine"
"tailscale.com/wgengine/filter"
2020-05-11 21:02:12 +00:00
"tailscale.com/wgengine/router"
2020-07-31 20:27:09 +00:00
"tailscale.com/wgengine/router/dns"
2020-06-08 22:19:26 +00:00
"tailscale.com/wgengine/tsdns"
2021-01-29 20:16:36 +00:00
"tailscale.com/wgengine/wgcfg"
2021-02-05 20:44:43 +00:00
"tailscale.com/wgengine/wgcfg/nmcfg"
2020-02-05 22:16:58 +00:00
)
2020-10-20 17:40:12 +00:00
var controlDebugFlags = getControlDebugFlags ( )
func getControlDebugFlags ( ) [ ] string {
if e := os . Getenv ( "TS_DEBUG_CONTROL_FLAGS" ) ; e != "" {
return strings . Split ( e , "," )
}
return nil
}
2020-10-19 22:56:59 +00:00
2020-05-19 02:32:20 +00:00
// LocalBackend is the glue between the major pieces of the Tailscale
// network software: the cloud control plane (via controlclient), the
// network data plane (via wgengine), and the user-facing UIs and CLIs
// (collectively called "frontends", via LocalBackend's implementation
// of the Backend interface).
//
// LocalBackend implements the overall state machine for the Tailscale
// application. Frontends, controlclient and wgengine can feed events
// into LocalBackend to advance the state machine, and advancing the
// state machine generates events back out to zero or more components.
2020-02-05 22:16:58 +00:00
type LocalBackend struct {
2020-05-19 02:32:20 +00:00
// Elements that are thread-safe or constant after construction.
2021-03-01 20:56:03 +00:00
ctx context . Context // canceled by Close
ctxCancel context . CancelFunc // cancels ctx
logf logger . Logf // general logging
keyLogf logger . Logf // for printing list of peers on change
statsLogf logger . Logf // for printing peers stats on change
e wgengine . Engine
store ipn . StateStore
backendLogID string
unregisterLinkMon func ( )
portpoll * portlist . Poller // may be nil
portpollOnce sync . Once // guards starting readPoller
gotPortPollRes chan struct { } // closed upon first readPoller result
serverURL string // tailcontrol URL
newDecompressor func ( ) ( controlclient . Decompressor , error )
2020-05-19 02:32:20 +00:00
2020-07-29 01:47:23 +00:00
filterHash string
2020-02-05 22:16:58 +00:00
// The mutex protects the following elements.
2020-09-28 22:28:26 +00:00
mu sync . Mutex
2021-02-04 21:12:42 +00:00
notify func ( ipn . Notify )
2020-09-28 22:28:26 +00:00
c * controlclient . Client
2021-02-04 21:12:42 +00:00
stateKey ipn . StateKey // computed in part from user-provided value
userID string // current controlling user ID (for Windows, primarily)
prefs * ipn . Prefs
2020-11-02 17:52:59 +00:00
inServerMode bool
2020-12-30 01:22:56 +00:00
machinePrivKey wgkey . Private
2021-02-04 21:12:42 +00:00
state ipn . State
2020-06-15 23:04:12 +00:00
// hostinfo is mutated in-place while mu is held.
hostinfo * tailcfg . Hostinfo
// netMap is not mutated in-place once set.
2021-02-05 23:44:46 +00:00
netMap * netmap . NetworkMap
2021-01-28 23:29:17 +00:00
nodeByAddr map [ netaddr . IP ] * tailcfg . Node
2020-10-27 19:51:48 +00:00
activeLogin string // last logged LoginName from netMap
2021-02-04 21:12:42 +00:00
engineStatus ipn . EngineStatus
2020-02-28 20:12:49 +00:00
endpoints [ ] string
2020-02-05 22:16:58 +00:00
blocked bool
authURL string
2020-10-27 20:57:10 +00:00
interact bool
2020-10-02 05:03:25 +00:00
prevIfState * interfaces . State
2020-02-05 22:16:58 +00:00
2020-05-19 02:32:20 +00:00
// statusLock must be held before calling statusChanged.Wait() or
2020-02-05 22:16:58 +00:00
// statusChanged.Broadcast().
statusLock sync . Mutex
statusChanged * sync . Cond
}
2020-02-03 18:35:52 +00:00
// NewLocalBackend returns a new LocalBackend that is ready to run,
// but is not actually running.
2021-02-04 21:12:42 +00:00
func NewLocalBackend ( logf logger . Logf , logid string , store ipn . StateStore , e wgengine . Engine ) ( * LocalBackend , error ) {
2020-02-05 22:16:58 +00:00
if e == nil {
panic ( "ipn.NewLocalBackend: wgengine must not be nil" )
}
// Default filter blocks everything, until Start() is called.
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
e . SetFilter ( filter . NewAllowNone ( logf ) )
2020-02-05 22:16:58 +00:00
2020-03-14 03:53:58 +00:00
ctx , cancel := context . WithCancel ( context . Background ( ) )
2020-02-05 22:16:58 +00:00
portpoll , err := portlist . NewPoller ( )
if err != nil {
2020-04-11 15:35:34 +00:00
logf ( "skipping portlist: %s" , err )
2020-02-05 22:16:58 +00:00
}
2020-02-25 15:36:32 +00:00
b := & LocalBackend {
2020-10-14 21:07:40 +00:00
ctx : ctx ,
ctxCancel : cancel ,
logf : logf ,
keyLogf : logger . LogOnChange ( logf , 5 * time . Minute , time . Now ) ,
2020-10-29 22:26:10 +00:00
statsLogf : logger . LogOnChange ( logf , 5 * time . Minute , time . Now ) ,
2020-10-14 21:07:40 +00:00
e : e ,
store : store ,
backendLogID : logid ,
2021-02-04 21:12:42 +00:00
state : ipn . NoState ,
2020-10-14 21:07:40 +00:00
portpoll : portpoll ,
gotPortPollRes : make ( chan struct { } ) ,
2020-02-05 22:16:58 +00:00
}
b . statusChanged = sync . NewCond ( & b . statusLock )
2021-03-02 04:45:30 +00:00
linkMon := e . GetLinkMonitor ( )
// Call our linkChange code once with the current state, and
// then also whenever it changes:
b . linkChange ( false , linkMon . InterfaceState ( ) )
b . unregisterLinkMon = linkMon . RegisterChangeCallback ( b . linkChange )
2020-02-25 15:36:32 +00:00
return b , nil
2020-02-05 22:16:58 +00:00
}
2021-03-02 04:45:30 +00:00
// linkChange is our link monitor callback, called whenever the network changes.
// major is whether ifst is different than earlier.
2020-08-28 04:25:17 +00:00
func ( b * LocalBackend ) linkChange ( major bool , ifst * interfaces . State ) {
2020-10-02 05:03:25 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2020-10-05 22:12:35 +00:00
hadPAC := b . prevIfState . HasPAC ( )
b . prevIfState = ifst
2020-10-06 22:22:46 +00:00
networkUp := ifst . AnyInterfaceUp ( )
if b . c != nil {
2021-02-04 21:12:42 +00:00
go b . c . SetPaused ( b . state == ipn . Stopped || ! networkUp )
2020-10-06 22:22:46 +00:00
}
2020-10-05 22:12:35 +00:00
// If the PAC-ness of the network changed, reconfig wireguard+route to
// add/remove subnets.
if hadPAC != ifst . HasPAC ( ) {
b . logf ( "linkChange: in state %v; PAC changed from %v->%v" , b . state , hadPAC , ifst . HasPAC ( ) )
switch b . state {
2021-02-04 21:12:42 +00:00
case ipn . NoState , ipn . Stopped :
2020-10-05 22:12:35 +00:00
// Do nothing.
default :
go b . authReconfig ( )
2020-10-02 05:03:25 +00:00
}
}
2021-02-23 04:43:35 +00:00
// If the local network configuration has changed, our filter may
// need updating to tweak default routes.
b . updateFilter ( b . netMap , b . prefs )
2020-08-28 04:25:17 +00:00
}
2020-05-19 02:32:20 +00:00
// Shutdown halts the backend and all its sub-components. The backend
// can no longer be used after Shutdown returns.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) Shutdown ( ) {
2020-05-19 02:32:20 +00:00
b . mu . Lock ( )
cli := b . c
b . mu . Unlock ( )
2020-05-21 20:30:20 +00:00
2021-03-01 20:56:03 +00:00
b . unregisterLinkMon ( )
2020-05-19 02:32:20 +00:00
if cli != nil {
cli . Shutdown ( )
}
2020-05-21 20:30:20 +00:00
b . ctxCancel ( )
2020-02-05 22:16:58 +00:00
b . e . Close ( )
b . e . Wait ( )
}
2020-05-19 02:32:20 +00:00
// Status returns the latest status of the backend and its
// sub-components.
2020-03-26 05:57:46 +00:00
func ( b * LocalBackend ) Status ( ) * ipnstate . Status {
sb := new ( ipnstate . StatusBuilder )
b . UpdateStatus ( sb )
return sb . Status ( )
}
2020-05-19 02:32:20 +00:00
// UpdateStatus implements ipnstate.StatusUpdater.
2020-03-26 05:57:46 +00:00
func ( b * LocalBackend ) UpdateStatus ( sb * ipnstate . StatusBuilder ) {
b . e . UpdateStatus ( sb )
b . mu . Lock ( )
defer b . mu . Unlock ( )
2020-08-11 02:42:04 +00:00
sb . SetBackendState ( b . state . String ( ) )
2020-03-26 05:57:46 +00:00
// TODO: hostinfo, and its networkinfo
// TODO: EngineStatus copy (and deprecate it?)
2020-06-15 23:04:12 +00:00
if b . netMap != nil {
2021-01-10 20:03:01 +00:00
sb . SetMagicDNSSuffix ( b . netMap . MagicDNSSuffix ( ) )
2020-09-30 04:39:43 +00:00
for id , up := range b . netMap . UserProfiles {
sb . AddUser ( id , up )
2020-03-26 05:57:46 +00:00
}
2020-06-15 23:04:12 +00:00
for _ , p := range b . netMap . Peers {
2020-03-26 05:57:46 +00:00
var lastSeen time . Time
if p . LastSeen != nil {
lastSeen = * p . LastSeen
}
var tailAddr string
2020-12-16 02:45:13 +00:00
for _ , addr := range p . Addresses {
// The peer struct currently only allows a single
// Tailscale IP address. For compatibility with the
// old display, make sure it's the IPv4 address.
2020-12-24 20:33:55 +00:00
if addr . IP . Is4 ( ) && addr . IsSingleIP ( ) && tsaddr . IsTailscaleIP ( addr . IP ) {
2020-12-16 02:45:13 +00:00
tailAddr = addr . IP . String ( )
break
}
2020-03-26 05:57:46 +00:00
}
sb . AddPeer ( key . Public ( p . Key ) , & ipnstate . PeerStatus {
InNetworkMap : true ,
UserID : p . User ,
TailAddr : tailAddr ,
HostName : p . Hostinfo . Hostname ,
2020-08-27 20:24:59 +00:00
DNSName : p . Name ,
2020-03-26 05:57:46 +00:00
OS : p . Hostinfo . OS ,
KeepAlive : p . KeepAlive ,
Created : p . Created ,
LastSeen : lastSeen ,
2020-12-01 02:05:51 +00:00
ShareeNode : p . Hostinfo . ShareeNode ,
2021-02-05 21:07:48 +00:00
ExitNode : p . StableID != "" && p . StableID == b . prefs . ExitNodeID ,
2020-03-26 05:57:46 +00:00
} )
}
}
2021-01-28 23:29:17 +00:00
}
2020-03-26 05:57:46 +00:00
2021-01-28 23:29:17 +00:00
// WhoIs reports the node and user who owns the node with the given IP.
// If ok == true, n and u are valid.
func ( b * LocalBackend ) WhoIs ( ip netaddr . IP ) ( n * tailcfg . Node , u tailcfg . UserProfile , ok bool ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
n , ok = b . nodeByAddr [ ip ]
if ! ok {
return nil , u , false
}
u , ok = b . netMap . UserProfiles [ n . User ]
if ! ok {
return nil , u , false
}
return n , u , true
2020-03-26 05:57:46 +00:00
}
2020-02-05 22:16:58 +00:00
// SetDecompressor sets a decompression function, which must be a zstd
// reader.
//
// This exists because the iOS/Mac NetworkExtension is very resource
// constrained, and the zstd package is too heavy to fit in the
// constrained RSS limit.
func ( b * LocalBackend ) SetDecompressor ( fn func ( ) ( controlclient . Decompressor , error ) ) {
b . newDecompressor = fn
}
2020-06-15 23:04:12 +00:00
// setClientStatus is the callback invoked by the control client whenever it posts a new status.
// Among other things, this is where we update the netmap, packet filters, DNS and DERP maps.
func ( b * LocalBackend ) setClientStatus ( st controlclient . Status ) {
2020-07-29 01:47:23 +00:00
// The following do not depend on any data for which we need to lock b.
if st . Err != "" {
// TODO(crawshaw): display in the UI.
2020-12-21 18:58:06 +00:00
if st . Err == "EOF" {
b . logf ( "[v1] Received error: EOF" )
} else {
b . logf ( "Received error: %v" , st . Err )
}
2020-07-29 01:47:23 +00:00
return
}
2020-06-15 23:04:12 +00:00
if st . LoginFinished != nil {
// Auth completed, unblock the engine
b . blockEngineUpdates ( false )
b . authReconfig ( )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { LoginFinished : & empty . Message { } } )
2020-06-15 23:04:12 +00:00
}
2020-07-29 01:47:23 +00:00
prefsChanged := false
// Lock b once and do only the things that require locking.
b . mu . Lock ( )
prefs := b . prefs
stateKey := b . stateKey
netMap := b . netMap
interact := b . interact
2020-06-15 23:04:12 +00:00
if st . Persist != nil {
2020-07-30 08:36:06 +00:00
if ! b . prefs . Persist . Equals ( st . Persist ) {
2020-07-29 01:47:23 +00:00
prefsChanged = true
b . prefs . Persist = st . Persist . Clone ( )
}
}
2020-10-21 19:55:03 +00:00
if temporarilySetMachineKeyInPersist ( ) && b . prefs . Persist != nil &&
b . prefs . Persist . LegacyFrontendPrivateMachineKey . IsZero ( ) {
b . prefs . Persist . LegacyFrontendPrivateMachineKey = b . machinePrivKey
prefsChanged = true
}
2020-07-29 01:47:23 +00:00
if st . NetMap != nil {
2021-02-25 05:15:14 +00:00
if b . findExitNodeIDLocked ( st . NetMap ) {
2021-01-21 01:24:16 +00:00
prefsChanged = true
}
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( st . NetMap )
2020-07-29 01:47:23 +00:00
}
if st . URL != "" {
b . authURL = st . URL
}
2021-02-04 21:12:42 +00:00
if b . state == ipn . NeedsLogin {
2020-07-29 01:47:23 +00:00
if ! b . prefs . WantRunning {
prefsChanged = true
}
b . prefs . WantRunning = true
}
// Prefs will be written out; this is not safe unless locked or cloned.
if prefsChanged {
prefs = b . prefs . Clone ( )
}
2020-06-15 23:04:12 +00:00
2020-07-29 01:47:23 +00:00
b . mu . Unlock ( )
2020-06-15 23:04:12 +00:00
2020-07-29 01:47:23 +00:00
// Now complete the lock-free parts of what we started while locked.
if prefsChanged {
2020-06-15 23:04:12 +00:00
if stateKey != "" {
if err := b . store . WriteState ( stateKey , prefs . ToBytes ( ) ) ; err != nil {
b . logf ( "Failed to save new controlclient state: %v" , err )
}
}
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { Prefs : prefs } )
2020-06-15 23:04:12 +00:00
}
if st . NetMap != nil {
2020-07-29 01:47:23 +00:00
if netMap != nil {
diff := st . NetMap . ConciseDiffFrom ( netMap )
2020-06-15 23:04:12 +00:00
if strings . TrimSpace ( diff ) == "" {
2020-12-21 18:58:06 +00:00
b . logf ( "[v1] netmap diff: (none)" )
2020-06-15 23:04:12 +00:00
} else {
b . logf ( "netmap diff:\n%v" , diff )
}
}
2020-07-29 01:47:23 +00:00
b . updateFilter ( st . NetMap , prefs )
b . e . SetNetworkMap ( st . NetMap )
if ! dnsMapsEqual ( st . NetMap , netMap ) {
2020-06-15 23:04:12 +00:00
b . updateDNSMap ( st . NetMap )
}
2020-09-18 14:44:01 +00:00
b . e . SetDERPMap ( st . NetMap . DERPMap )
2020-07-29 01:47:23 +00:00
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { NetMap : st . NetMap } )
2020-06-15 23:04:12 +00:00
}
if st . URL != "" {
b . logf ( "Received auth URL: %.20v..." , st . URL )
2020-10-27 20:57:10 +00:00
if interact {
2020-06-15 23:04:12 +00:00
b . popBrowserAuthNow ( )
}
}
b . stateMachine ( )
2020-07-29 01:47:23 +00:00
// This is currently (2020-07-28) necessary; conditionally disabling it is fragile!
// This is where netmap information gets propagated to router and magicsock.
b . authReconfig ( )
2020-06-15 23:04:12 +00:00
}
2021-02-25 05:15:14 +00:00
// findExitNodeIDLocked updates b.prefs to reference an exit node by ID,
2021-02-25 04:05:23 +00:00
// rather than by IP. It returns whether prefs was mutated.
2021-02-25 05:15:14 +00:00
func ( b * LocalBackend ) findExitNodeIDLocked ( nm * netmap . NetworkMap ) ( prefsChanged bool ) {
2021-01-21 01:24:16 +00:00
// If we have a desired IP on file, try to find the corresponding
// node.
2021-02-25 04:05:23 +00:00
if b . prefs . ExitNodeIP . IsZero ( ) {
return false
}
2021-01-21 01:24:16 +00:00
2021-02-25 04:05:23 +00:00
// IP takes precedence over ID, so if both are set, clear ID.
if b . prefs . ExitNodeID != "" {
b . prefs . ExitNodeID = ""
prefsChanged = true
2021-01-21 01:24:16 +00:00
}
for _ , peer := range nm . Peers {
2021-02-25 04:05:23 +00:00
for _ , addr := range peer . Addresses {
if ! addr . IsSingleIP ( ) || addr . IP != b . prefs . ExitNodeIP {
2021-01-21 01:24:16 +00:00
continue
}
2021-02-25 04:05:23 +00:00
// Found the node being referenced, upgrade prefs to
// reference it directly for next time.
b . prefs . ExitNodeID = peer . StableID
b . prefs . ExitNodeIP = netaddr . IP { }
return true
2021-01-21 01:24:16 +00:00
}
}
2021-02-25 04:05:23 +00:00
return false
2021-01-21 01:24:16 +00:00
}
2020-06-15 23:04:12 +00:00
// setWgengineStatus is the callback by the wireguard engine whenever it posts a new status.
// This updates the endpoints both in the backend and in the control client.
func ( b * LocalBackend ) setWgengineStatus ( s * wgengine . Status , err error ) {
if err != nil {
2021-01-07 04:18:29 +00:00
b . logf ( "wgengine status error: %v" , err )
2020-06-15 23:04:12 +00:00
return
}
if s == nil {
b . logf ( "[unexpected] non-error wgengine update with status=nil: %v" , s )
return
}
b . mu . Lock ( )
2020-10-29 22:26:10 +00:00
es := b . parseWgStatusLocked ( s )
2020-06-15 23:04:12 +00:00
c := b . c
b . engineStatus = es
b . endpoints = append ( [ ] string { } , s . LocalAddrs ... )
b . mu . Unlock ( )
if c != nil {
c . UpdateEndpoints ( 0 , s . LocalAddrs )
}
b . stateMachine ( )
b . statusLock . Lock ( )
b . statusChanged . Broadcast ( )
b . statusLock . Unlock ( )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { Engine : & es } )
2020-06-15 23:04:12 +00:00
}
2020-05-19 02:32:20 +00:00
// Start applies the configuration specified in opts, and starts the
// state machine.
//
// TODO(danderson): this function is trying to do too many things at
// once: it loads state, or imports it, or updates prefs sometimes,
// contains some settings that are one-shot things done by `tailscale
// up` because we had nowhere else to put them, and there's no clear
// guarantee that switching from one user's state to another is
// actually a supported operation (it should be, but it's very unclear
// from the following whether or not that is a safe transition).
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) Start ( opts ipn . Options ) error {
2020-02-03 18:35:52 +00:00
if opts . Prefs == nil && opts . StateKey == "" {
return errors . New ( "no state key or prefs provided" )
2020-02-03 23:58:40 +00:00
}
2020-02-03 18:35:52 +00:00
if opts . Prefs != nil {
2020-04-11 15:35:34 +00:00
b . logf ( "Start: %v" , opts . Prefs . Pretty ( ) )
2020-02-03 18:35:52 +00:00
} else {
2020-04-11 15:35:34 +00:00
b . logf ( "Start" )
2020-02-03 18:35:52 +00:00
}
2020-02-05 22:16:58 +00:00
2020-06-15 23:04:12 +00:00
hostinfo := controlclient . NewHostinfo ( )
hostinfo . BackendLogID = b . backendLogID
hostinfo . FrontendLogID = opts . FrontendLogID
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
2020-02-25 20:30:28 +00:00
if b . c != nil {
// TODO(apenwarr): avoid the need to reinit controlclient.
// This will trigger a full relogin/reconfigure cycle every
// time a Handle reconnects to the backend. Ideally, we
// would send the new Prefs and everything would get back
// into sync with the minimal changes. But that's not how it
// is right now, which is a sign that the code is still too
// complicated.
b . c . Shutdown ( )
}
2020-06-15 23:04:12 +00:00
if b . hostinfo != nil {
hostinfo . Services = b . hostinfo . Services // keep any previous session and netinfo
hostinfo . NetInfo = b . hostinfo . NetInfo
2020-02-25 18:04:20 +00:00
}
2020-06-15 23:04:12 +00:00
b . hostinfo = hostinfo
2021-02-04 21:12:42 +00:00
b . state = ipn . NoState
2020-02-03 18:35:52 +00:00
2020-02-25 20:30:28 +00:00
if err := b . loadStateLocked ( opts . StateKey , opts . Prefs , opts . LegacyConfigPath ) ; err != nil {
2020-02-03 18:35:52 +00:00
b . mu . Unlock ( )
return fmt . Errorf ( "loading requested state: %v" , err )
2020-02-03 23:58:40 +00:00
}
2020-02-03 18:35:52 +00:00
2020-11-03 05:11:20 +00:00
b . inServerMode = b . prefs . ForceDaemon
2020-02-19 05:03:22 +00:00
b . serverURL = b . prefs . ControlURL
2020-06-15 23:04:12 +00:00
hostinfo . RoutableIPs = append ( hostinfo . RoutableIPs , b . prefs . AdvertiseRoutes ... )
hostinfo . RequestTags = append ( hostinfo . RequestTags , b . prefs . AdvertiseTags ... )
2020-11-04 18:24:33 +00:00
if b . inServerMode || runtime . GOOS == "windows" {
b . logf ( "Start: serverMode=%v" , b . inServerMode )
}
2020-07-24 21:05:04 +00:00
applyPrefsToHostinfo ( hostinfo , b . prefs )
2020-02-18 03:33:01 +00:00
2020-02-05 22:16:58 +00:00
b . notify = opts . Notify
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( nil )
2021-02-05 23:23:01 +00:00
persistv := b . prefs . Persist
2020-09-28 22:28:26 +00:00
machinePrivKey := b . machinePrivKey
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-07-29 01:47:23 +00:00
b . updateFilter ( nil , nil )
2020-02-05 22:16:58 +00:00
2020-10-14 21:07:40 +00:00
if b . portpoll != nil {
b . portpollOnce . Do ( func ( ) {
go b . portpoll . Run ( b . ctx )
go b . readPoller ( )
// Give the poller a second to get results to
// prevent it from restarting our map poll
// HTTP request (via doSetHostinfoFilterServices >
// cli.SetHostinfo). In practice this is very quick.
t0 := time . Now ( )
timer := time . NewTimer ( time . Second )
select {
case <- b . gotPortPollRes :
b . logf ( "got initial portlist info in %v" , time . Since ( t0 ) . Round ( time . Millisecond ) )
timer . Stop ( )
case <- timer . C :
b . logf ( "timeout waiting for initial portlist" )
}
} )
}
2020-06-20 17:18:13 +00:00
var discoPublic tailcfg . DiscoKey
2020-06-28 18:53:37 +00:00
if controlclient . Debug . Disco {
2020-07-06 19:10:39 +00:00
discoPublic = b . e . DiscoPublicKey ( )
2020-06-20 17:18:13 +00:00
}
2020-06-19 19:06:49 +00:00
2020-02-05 22:16:58 +00:00
var err error
2021-02-05 23:23:01 +00:00
if persistv == nil {
2020-02-05 22:16:58 +00:00
// let controlclient initialize it
2021-02-05 23:23:01 +00:00
persistv = & persist . Persist { }
2020-02-05 22:16:58 +00:00
}
cli , err := controlclient . New ( controlclient . Options {
2020-09-28 22:28:26 +00:00
MachinePrivateKey : machinePrivKey ,
Logf : logger . WithPrefix ( b . logf , "control: " ) ,
2021-02-05 23:23:01 +00:00
Persist : * persistv ,
2020-09-28 22:28:26 +00:00
ServerURL : b . serverURL ,
AuthKey : opts . AuthKey ,
Hostinfo : hostinfo ,
KeepAlive : true ,
NewDecompressor : b . newDecompressor ,
HTTPTestClient : opts . HTTPTestClient ,
DiscoPublicKey : discoPublic ,
2020-10-19 22:56:59 +00:00
DebugFlags : controlDebugFlags ,
2020-02-05 22:16:58 +00:00
} )
if err != nil {
return err
}
b . mu . Lock ( )
b . c = cli
2020-02-28 20:12:49 +00:00
endpoints := b . endpoints
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-02-28 20:12:49 +00:00
if endpoints != nil {
cli . UpdateEndpoints ( 0 , endpoints )
2020-02-05 22:16:58 +00:00
}
2020-06-15 23:04:12 +00:00
cli . SetStatusFunc ( b . setClientStatus )
b . e . SetStatusCallback ( b . setWgengineStatus )
2020-05-19 02:32:20 +00:00
b . e . SetNetInfoCallback ( b . setNetInfo )
2020-03-24 05:16:16 +00:00
2020-02-29 02:34:56 +00:00
b . mu . Lock ( )
prefs := b . prefs . Clone ( )
2020-10-21 19:55:03 +00:00
if temporarilySetMachineKeyInPersist ( ) && prefs . Persist != nil &&
prefs . Persist . LegacyFrontendPrivateMachineKey . IsZero ( ) {
prefs . Persist . LegacyFrontendPrivateMachineKey = b . machinePrivKey
}
2020-02-29 02:34:56 +00:00
b . mu . Unlock ( )
2020-02-05 22:16:58 +00:00
blid := b . backendLogID
2020-04-11 15:35:34 +00:00
b . logf ( "Backend: logs: be:%v fe:%v" , blid , opts . FrontendLogID )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { BackendLogID : & blid } )
b . send ( ipn . Notify { Prefs : prefs } )
2020-02-05 22:16:58 +00:00
2020-02-03 18:57:34 +00:00
cli . Login ( nil , controlclient . LoginDefault )
2020-02-05 22:16:58 +00:00
return nil
}
2020-05-19 02:32:20 +00:00
// updateFilter updates the packet filter in wgengine based on the
// given netMap and user preferences.
2021-02-05 23:44:46 +00:00
func ( b * LocalBackend ) updateFilter ( netMap * netmap . NetworkMap , prefs * ipn . Prefs ) {
2020-07-31 20:03:00 +00:00
// NOTE(danderson): keep change detection as the first thing in
// this function. Don't try to optimize by returning early, more
// likely than not you'll just end up breaking the change
// detection and end up with the wrong filter installed. This is
// quite hard to debug, so save yourself the trouble.
var (
haveNetmap = netMap != nil
2020-12-24 20:33:55 +00:00
addrs [ ] netaddr . IPPrefix
2020-11-10 05:33:41 +00:00
packetFilter [ ] filter . Match
2021-02-22 22:34:15 +00:00
localNetsB netaddr . IPSetBuilder
2020-07-31 20:03:00 +00:00
shieldsUp = prefs == nil || prefs . ShieldsUp // Be conservative when not ready
)
if haveNetmap {
addrs = netMap . Addresses
2021-02-22 22:34:15 +00:00
for _ , p := range addrs {
localNetsB . AddPrefix ( p )
}
2020-07-31 20:03:00 +00:00
packetFilter = netMap . PacketFilter
2020-05-22 02:41:18 +00:00
}
2020-07-29 01:47:23 +00:00
if prefs != nil {
2021-02-22 22:34:15 +00:00
for _ , r := range prefs . AdvertiseRoutes {
2021-02-23 04:43:35 +00:00
if r . Bits == 0 {
// When offering a default route to the world, we
// filter out locally reachable LANs, so that the
// default route effectively appears to be a "guest
// wifi": you get internet access, but to additionally
// get LAN access the LAN(s) need to be offered
// explicitly as well.
s , err := shrinkDefaultRoute ( r )
if err != nil {
b . logf ( "computing default route filter: %v" , err )
continue
}
localNetsB . AddSet ( s )
} else {
localNetsB . AddPrefix ( r )
}
2021-02-22 22:34:15 +00:00
}
2020-07-29 01:47:23 +00:00
}
2021-02-22 22:34:15 +00:00
localNets := localNetsB . IPSet ( )
2020-07-29 01:47:23 +00:00
2021-02-22 22:34:15 +00:00
changed := deepprint . UpdateHash ( & b . filterHash , haveNetmap , addrs , packetFilter , localNets . Ranges ( ) , shieldsUp )
2020-07-29 01:47:23 +00:00
if ! changed {
return
}
2020-05-22 02:41:18 +00:00
2020-08-01 02:07:14 +00:00
if ! haveNetmap {
2020-07-31 20:03:00 +00:00
b . logf ( "netmap packet filter: (not ready yet)" )
b . e . SetFilter ( filter . NewAllowNone ( b . logf ) )
2020-08-01 02:07:14 +00:00
return
}
2021-01-22 21:39:53 +00:00
oldFilter := b . e . GetFilter ( )
2020-08-01 02:07:14 +00:00
if shieldsUp {
2020-04-29 06:37:35 +00:00
b . logf ( "netmap packet filter: (shields up)" )
2021-01-22 21:39:53 +00:00
b . e . SetFilter ( filter . NewShieldsUpFilter ( localNets , oldFilter , b . logf ) )
2020-08-01 02:07:14 +00:00
} else {
2020-07-31 20:03:00 +00:00
b . logf ( "netmap packet filter: %v" , packetFilter )
2021-01-22 21:39:53 +00:00
b . e . SetFilter ( filter . New ( packetFilter , localNets , oldFilter , b . logf ) )
2020-05-22 02:41:18 +00:00
}
2020-07-29 01:47:23 +00:00
}
2021-02-23 04:43:35 +00:00
var removeFromDefaultRoute = [ ] netaddr . IPPrefix {
// RFC1918 LAN ranges
netaddr . MustParseIPPrefix ( "192.168.0.0/16" ) ,
netaddr . MustParseIPPrefix ( "172.16.0.0/12" ) ,
netaddr . MustParseIPPrefix ( "10.0.0.0/8" ) ,
// Tailscale IPv4 range
tsaddr . CGNATRange ( ) ,
// IPv6 Link-local addresses
netaddr . MustParseIPPrefix ( "fe80::/10" ) ,
// Tailscale IPv6 range
tsaddr . TailscaleULARange ( ) ,
}
// shrinkDefaultRoute returns an IPSet representing the IPs in route,
// minus those in removeFromDefaultRoute and local interface subnets.
func shrinkDefaultRoute ( route netaddr . IPPrefix ) ( * netaddr . IPSet , error ) {
var b netaddr . IPSetBuilder
b . AddPrefix ( route )
err := interfaces . ForeachInterfaceAddress ( func ( _ interfaces . Interface , pfx netaddr . IPPrefix ) {
if tsaddr . IsTailscaleIP ( pfx . IP ) {
return
}
if pfx . IsSingleIP ( ) {
return
}
b . RemovePrefix ( pfx )
} )
if err != nil {
return nil , err
}
for _ , pfx := range removeFromDefaultRoute {
b . RemovePrefix ( pfx )
}
return b . IPSet ( ) , nil
}
2020-07-29 01:47:23 +00:00
// dnsCIDRsEqual determines whether two CIDR lists are equal
// for DNS map construction purposes (that is, only the first entry counts).
2020-12-24 20:33:55 +00:00
func dnsCIDRsEqual ( newAddr , oldAddr [ ] netaddr . IPPrefix ) bool {
2020-07-29 01:47:23 +00:00
if len ( newAddr ) != len ( oldAddr ) {
return false
2020-02-05 22:16:58 +00:00
}
2020-07-29 01:47:23 +00:00
if len ( newAddr ) == 0 || newAddr [ 0 ] == oldAddr [ 0 ] {
return true
}
return false
}
// dnsMapsEqual determines whether the new and the old network map
// induce the same DNS map. It does so without allocating memory,
// at the expense of giving false negatives if peers are reordered.
2021-02-05 23:44:46 +00:00
func dnsMapsEqual ( new , old * netmap . NetworkMap ) bool {
2020-07-29 01:47:23 +00:00
if ( old == nil ) != ( new == nil ) {
return false
}
if old == nil && new == nil {
return true
}
if len ( new . Peers ) != len ( old . Peers ) {
return false
}
if new . Name != old . Name {
return false
}
if ! dnsCIDRsEqual ( new . Addresses , old . Addresses ) {
return false
}
for i , newPeer := range new . Peers {
oldPeer := old . Peers [ i ]
if newPeer . Name != oldPeer . Name {
return false
}
if ! dnsCIDRsEqual ( newPeer . Addresses , oldPeer . Addresses ) {
return false
}
}
return true
2020-02-05 22:16:58 +00:00
}
2020-06-08 22:19:26 +00:00
// updateDNSMap updates the domain map in the DNS resolver in wgengine
// based on the given netMap and user preferences.
2021-02-05 23:44:46 +00:00
func ( b * LocalBackend ) updateDNSMap ( netMap * netmap . NetworkMap ) {
2020-06-08 22:19:26 +00:00
if netMap == nil {
2020-07-29 01:47:23 +00:00
b . logf ( "dns map: (not ready)" )
2020-06-08 22:19:26 +00:00
return
}
2020-07-14 22:48:36 +00:00
2020-07-29 01:47:23 +00:00
nameToIP := make ( map [ string ] netaddr . IP )
2020-12-24 20:33:55 +00:00
set := func ( name string , addrs [ ] netaddr . IPPrefix ) {
2020-09-16 14:54:01 +00:00
if len ( addrs ) == 0 || name == "" {
2020-07-14 22:48:36 +00:00
return
2020-06-08 22:19:26 +00:00
}
2020-12-24 20:33:55 +00:00
nameToIP [ name ] = addrs [ 0 ] . IP
2020-06-08 22:19:26 +00:00
}
2020-07-14 22:48:36 +00:00
for _ , peer := range netMap . Peers {
2020-07-29 01:47:23 +00:00
set ( peer . Name , peer . Addresses )
2020-07-14 22:48:36 +00:00
}
2020-07-29 01:47:23 +00:00
set ( netMap . Name , netMap . Addresses )
2020-07-14 22:48:36 +00:00
2021-01-05 18:37:15 +00:00
dnsMap := tsdns . NewMap ( nameToIP , magicDNSRootDomains ( netMap ) )
2020-07-29 01:47:23 +00:00
// map diff will be logged in tsdns.Resolver.SetMap.
b . e . SetDNSMap ( dnsMap )
2020-06-08 22:19:26 +00:00
}
2020-05-19 02:32:20 +00:00
// readPoller is a goroutine that receives service lists from
// b.portpoll and propagates them into the controlclient's HostInfo.
2020-04-29 09:23:29 +00:00
func ( b * LocalBackend ) readPoller ( ) {
2020-10-14 21:07:40 +00:00
n := 0
2020-02-05 22:16:58 +00:00
for {
2020-03-14 03:53:58 +00:00
ports , ok := <- b . portpoll . C
if ! ok {
return
2020-02-05 22:16:58 +00:00
}
sl := [ ] tailcfg . Service { }
for _ , p := range ports {
s := tailcfg . Service {
2020-04-01 04:48:33 +00:00
Proto : tailcfg . ServiceProto ( p . Proto ) ,
2020-02-05 22:16:58 +00:00
Port : p . Port ,
Description : p . Process ,
}
2020-04-01 04:48:33 +00:00
if policy . IsInterestingService ( s , version . OS ( ) ) {
sl = append ( sl , s )
}
2020-02-05 22:16:58 +00:00
}
b . mu . Lock ( )
2020-06-15 23:04:12 +00:00
if b . hostinfo == nil {
b . hostinfo = new ( tailcfg . Hostinfo )
2020-02-25 19:01:20 +00:00
}
2020-06-15 23:04:12 +00:00
b . hostinfo . Services = sl
hi := b . hostinfo
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-04-29 09:23:29 +00:00
b . doSetHostinfoFilterServices ( hi )
2020-10-14 21:07:40 +00:00
n ++
if n == 1 {
close ( b . gotPortPollRes )
}
2020-02-05 22:16:58 +00:00
}
}
2020-05-19 02:32:20 +00:00
// send delivers n to the connected frontend. If no frontend is
// connected, the notification is dropped without being delivered.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) send ( n ipn . Notify ) {
2020-02-25 20:30:28 +00:00
b . mu . Lock ( )
notify := b . notify
b . mu . Unlock ( )
if notify != nil {
2020-10-27 04:23:58 +00:00
n . Version = version . Long
2020-02-25 20:30:28 +00:00
notify ( n )
2020-07-15 16:27:48 +00:00
} else {
b . logf ( "nil notify callback; dropping %+v" , n )
2020-02-05 22:16:58 +00:00
}
}
2020-05-19 02:32:20 +00:00
// popBrowserAuthNow shuts down the data plane and sends an auth URL
// to the connected frontend, if any.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) popBrowserAuthNow ( ) {
b . mu . Lock ( )
url := b . authURL
2020-10-27 20:57:10 +00:00
b . interact = false
2020-02-05 22:16:58 +00:00
b . authURL = ""
b . mu . Unlock ( )
2020-02-25 22:05:17 +00:00
2020-04-11 15:35:34 +00:00
b . logf ( "popBrowserAuthNow: url=%v" , url != "" )
2020-02-05 22:16:58 +00:00
b . blockEngineUpdates ( true )
b . stopEngineAndWait ( )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { BrowseToURL : & url } )
if b . State ( ) == ipn . Running {
b . enterState ( ipn . Starting )
2020-02-05 22:16:58 +00:00
}
}
2020-09-28 22:28:26 +00:00
// initMachineKeyLocked is called to initialize b.machinePrivKey.
//
// b.prefs must already be initialized.
2020-11-04 17:35:58 +00:00
// b.stateKey should be set too, but just for nicer log messages.
2020-09-28 22:28:26 +00:00
// b.mu must be held.
2020-10-21 19:55:03 +00:00
func ( b * LocalBackend ) initMachineKeyLocked ( ) ( err error ) {
if temporarilySetMachineKeyInPersist ( ) {
defer func ( ) {
if err != nil {
return
}
2020-11-02 16:51:12 +00:00
if b . prefs != nil && b . prefs . Persist != nil {
b . prefs . Persist . LegacyFrontendPrivateMachineKey = b . machinePrivKey
}
2020-10-21 19:55:03 +00:00
} ( )
}
2020-09-28 22:28:26 +00:00
if ! b . machinePrivKey . IsZero ( ) {
// Already set.
return nil
}
2020-12-30 01:22:56 +00:00
var legacyMachineKey wgkey . Private
2020-09-28 22:28:26 +00:00
if b . prefs . Persist != nil {
legacyMachineKey = b . prefs . Persist . LegacyFrontendPrivateMachineKey
}
2021-02-04 21:12:42 +00:00
keyText , err := b . store . ReadState ( ipn . MachineKeyStateKey )
2020-09-28 22:28:26 +00:00
if err == nil {
if err := b . machinePrivKey . UnmarshalText ( keyText ) ; err != nil {
2021-02-04 21:12:42 +00:00
return fmt . Errorf ( "invalid key in %s key of %v: %w" , ipn . MachineKeyStateKey , b . store , err )
2020-09-28 22:28:26 +00:00
}
if b . machinePrivKey . IsZero ( ) {
2021-02-04 21:12:42 +00:00
return fmt . Errorf ( "invalid zero key stored in %v key of %v" , ipn . MachineKeyStateKey , b . store )
2020-09-28 22:28:26 +00:00
}
if ! legacyMachineKey . IsZero ( ) && ! bytes . Equal ( legacyMachineKey [ : ] , b . machinePrivKey [ : ] ) {
b . logf ( "frontend-provided legacy machine key ignored; used value from server state" )
}
return nil
}
2021-02-04 21:12:42 +00:00
if err != ipn . ErrStateNotExist {
return fmt . Errorf ( "error reading %v key of %v: %w" , ipn . MachineKeyStateKey , b . store , err )
2020-09-28 22:28:26 +00:00
}
// If we didn't find one already on disk and the prefs already
// have a legacy machine key, use that. Otherwise generate a
// new one.
if ! legacyMachineKey . IsZero ( ) {
2020-11-04 17:35:58 +00:00
if b . stateKey == "" {
b . logf ( "using frontend-provided legacy machine key" )
} else {
b . logf ( "using legacy machine key from state key %q" , b . stateKey )
}
2020-09-28 22:28:26 +00:00
b . machinePrivKey = legacyMachineKey
} else {
b . logf ( "generating new machine key" )
var err error
2020-12-30 01:22:56 +00:00
b . machinePrivKey , err = wgkey . NewPrivate ( )
2020-09-28 22:28:26 +00:00
if err != nil {
return fmt . Errorf ( "initializing new machine key: %w" , err )
}
}
keyText , _ = b . machinePrivKey . MarshalText ( )
2021-02-04 21:12:42 +00:00
if err := b . store . WriteState ( ipn . MachineKeyStateKey , keyText ) ; err != nil {
2020-09-28 22:28:26 +00:00
b . logf ( "error writing machine key to store: %v" , err )
return err
}
b . logf ( "machine key written to store" )
return nil
}
2020-11-02 17:52:59 +00:00
// writeServerModeStartState stores the ServerModeStartKey value based on the current
// user and prefs. If userID is blank or prefs is blank, no work is done.
//
// b.mu may either be held or not.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) writeServerModeStartState ( userID string , prefs * ipn . Prefs ) {
2020-11-02 17:52:59 +00:00
if userID == "" || prefs == nil {
return
}
if prefs . ForceDaemon {
2021-02-04 21:12:42 +00:00
stateKey := ipn . StateKey ( "user-" + userID )
if err := b . store . WriteState ( ipn . ServerModeStartKey , [ ] byte ( stateKey ) ) ; err != nil {
2020-11-02 17:52:59 +00:00
b . logf ( "WriteState error: %v" , err )
}
// It's important we do this here too, even if it looks
// redundant with the one in the 'if stateKey != ""'
// check block above. That one won't fire in the case
// where the Windows client started up in client mode.
// This happens when we transition into server mode:
if err := b . store . WriteState ( stateKey , prefs . ToBytes ( ) ) ; err != nil {
b . logf ( "WriteState error: %v" , err )
}
} else {
2021-02-04 21:12:42 +00:00
if err := b . store . WriteState ( ipn . ServerModeStartKey , nil ) ; err != nil {
2020-11-02 17:52:59 +00:00
b . logf ( "WriteState error: %v" , err )
}
}
}
2020-05-19 02:32:20 +00:00
// loadStateLocked sets b.prefs and b.stateKey based on a complex
// combination of key, prefs, and legacyPath. b.mu must be held when
// calling.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) loadStateLocked ( key ipn . StateKey , prefs * ipn . Prefs , legacyPath string ) ( err error ) {
2020-02-14 00:38:36 +00:00
if prefs == nil && key == "" {
panic ( "state key and prefs are both unset" )
}
2020-11-04 17:35:58 +00:00
// Optimistically set stateKey (for initMachineKeyLocked's
// logging), but revert it if we return an error so a later SetPrefs
// call can't pick it up if it's bogus.
b . stateKey = key
defer func ( ) {
if err != nil {
b . stateKey = ""
}
} ( )
2020-02-14 00:38:36 +00:00
if key == "" {
2020-09-28 22:28:26 +00:00
// Frontend owns the state, we just need to obey it.
//
// If the frontend (e.g. on Windows) supplied the
// optional/legacy machine key then it's used as the
// value instead of making up a new one.
2020-11-04 18:24:33 +00:00
b . logf ( "using frontend prefs: %s" , prefs . Pretty ( ) )
2020-02-27 20:20:29 +00:00
b . prefs = prefs . Clone ( )
2020-09-28 22:28:26 +00:00
if err := b . initMachineKeyLocked ( ) ; err != nil {
return fmt . Errorf ( "initMachineKeyLocked: %w" , err )
}
2020-11-02 17:52:59 +00:00
b . writeServerModeStartState ( b . userID , b . prefs )
2020-02-14 00:38:36 +00:00
return nil
}
if prefs != nil {
// Backend owns the state, but frontend is trying to migrate
// state into the backend.
2020-11-04 18:24:33 +00:00
b . logf ( "importing frontend prefs into backend store; frontend prefs: %s" , prefs . Pretty ( ) )
2020-02-03 18:35:52 +00:00
if err := b . store . WriteState ( key , prefs . ToBytes ( ) ) ; err != nil {
return fmt . Errorf ( "store.WriteState: %v" , err )
}
}
2020-11-04 18:24:33 +00:00
b . logf ( "using backend prefs" )
2020-02-14 00:38:36 +00:00
bs , err := b . store . ReadState ( key )
if err != nil {
2021-02-04 21:12:42 +00:00
if errors . Is ( err , ipn . ErrStateNotExist ) {
2020-02-20 07:23:34 +00:00
if legacyPath != "" {
2021-02-04 21:12:42 +00:00
b . prefs , err = ipn . LoadPrefs ( legacyPath )
2020-02-20 19:07:00 +00:00
if err != nil {
2020-11-02 16:33:34 +00:00
if ! errors . Is ( err , os . ErrNotExist ) {
b . logf ( "failed to load legacy prefs: %v" , err )
2020-09-17 14:59:55 +00:00
}
2021-02-04 21:12:42 +00:00
b . prefs = ipn . NewPrefs ( )
2020-02-20 19:07:00 +00:00
} else {
2020-11-04 18:24:33 +00:00
b . logf ( "imported prefs from relaynode for %q: %v" , key , b . prefs . Pretty ( ) )
2020-02-20 19:07:00 +00:00
}
2020-02-20 07:23:34 +00:00
} else {
2021-02-04 21:12:42 +00:00
b . prefs = ipn . NewPrefs ( )
2020-11-04 18:24:33 +00:00
b . logf ( "created empty state for %q: %s" , key , b . prefs . Pretty ( ) )
2020-02-20 07:23:34 +00:00
}
2020-09-28 22:28:26 +00:00
if err := b . initMachineKeyLocked ( ) ; err != nil {
return fmt . Errorf ( "initMachineKeyLocked: %w" , err )
}
2020-02-14 00:38:36 +00:00
return nil
}
return fmt . Errorf ( "store.ReadState(%q): %v" , key , err )
}
2021-02-04 21:12:42 +00:00
b . prefs , err = ipn . PrefsFromBytes ( bs , false )
2020-02-14 00:38:36 +00:00
if err != nil {
return fmt . Errorf ( "PrefsFromBytes: %v" , err )
}
2020-11-04 18:24:33 +00:00
b . logf ( "backend prefs for %q: %s" , key , b . prefs . Pretty ( ) )
2020-09-28 22:28:26 +00:00
if err := b . initMachineKeyLocked ( ) ; err != nil {
return fmt . Errorf ( "initMachineKeyLocked: %w" , err )
}
2020-02-03 18:35:52 +00:00
return nil
}
2020-05-19 02:32:20 +00:00
// State returns the backend state machine's current state.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) State ( ) ipn . State {
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . state
}
2020-11-02 17:52:59 +00:00
func ( b * LocalBackend ) InServerMode ( ) bool {
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 21:28:21 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2020-11-02 17:52:59 +00:00
return b . inServerMode
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 21:28:21 +00:00
}
2020-05-19 02:32:20 +00:00
// getEngineStatus returns a copy of b.engineStatus.
2020-03-27 20:26:35 +00:00
//
2020-05-19 02:32:20 +00:00
// TODO(bradfitz): remove this and use Status() throughout.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) getEngineStatus ( ) ipn . EngineStatus {
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . engineStatus
}
2020-07-13 20:13:11 +00:00
// Login implements Backend.
func ( b * LocalBackend ) Login ( token * oauth2 . Token ) {
b . mu . Lock ( )
b . assertClientLocked ( )
c := b . c
b . mu . Unlock ( )
c . Login ( token , controlclient . LoginInteractive )
}
2020-05-19 02:32:20 +00:00
// StartLoginInteractive implements Backend. It requests a new
// interactive login from controlclient, unless such a flow is already
// in progress, in which case StartLoginInteractive attempts to pick
// up the in-progress flow where it left off.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) StartLoginInteractive ( ) {
b . mu . Lock ( )
2020-02-25 20:30:28 +00:00
b . assertClientLocked ( )
2020-10-27 20:57:10 +00:00
b . interact = true
2020-02-05 22:16:58 +00:00
url := b . authURL
2020-02-25 20:30:28 +00:00
c := b . c
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-04-11 15:35:34 +00:00
b . logf ( "StartLoginInteractive: url=%v" , url != "" )
2020-02-05 22:16:58 +00:00
if url != "" {
b . popBrowserAuthNow ( )
} else {
2020-02-25 20:30:28 +00:00
c . Login ( nil , controlclient . LoginInteractive )
2020-02-05 22:16:58 +00:00
}
}
2020-05-19 02:32:20 +00:00
// FakeExpireAfter implements Backend.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) FakeExpireAfter ( x time . Duration ) {
2020-04-11 15:35:34 +00:00
b . logf ( "FakeExpireAfter: %v" , x )
2020-06-15 23:04:12 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . netMap == nil {
return
}
// This function is called very rarely,
// so we prefer to fully copy the netmap over introducing in-place modification here.
mapCopy := * b . netMap
e := mapCopy . Expiry
if e . IsZero ( ) || time . Until ( e ) > x {
mapCopy . Expiry = time . Now ( ) . Add ( x )
2020-02-05 22:16:58 +00:00
}
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( & mapCopy )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { NetMap : b . netMap } )
2020-02-05 22:16:58 +00:00
}
2020-08-09 21:49:42 +00:00
func ( b * LocalBackend ) Ping ( ipStr string ) {
ip , err := netaddr . ParseIP ( ipStr )
if err != nil {
b . logf ( "ignoring Ping request to invalid IP %q" , ipStr )
return
}
b . e . Ping ( ip , func ( pr * ipnstate . PingResult ) {
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { PingResult : pr } )
2020-08-09 21:49:42 +00:00
} )
}
2020-10-29 22:26:10 +00:00
// parseWgStatusLocked returns an EngineStatus based on s.
//
// b.mu must be held; mostly because the caller is about to anyway, and doing so
// gives us slightly better guarantees about the two peers stats lines not
// being intermixed if there are concurrent calls to our caller.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) parseWgStatusLocked ( s * wgengine . Status ) ( ret ipn . EngineStatus ) {
2020-10-29 22:26:10 +00:00
var peerStats , peerKeys strings . Builder
2020-02-05 22:16:58 +00:00
2020-05-29 16:53:04 +00:00
ret . LiveDERPs = s . DERPs
2021-02-04 21:12:42 +00:00
ret . LivePeers = map [ tailcfg . NodeKey ] ipnstate . PeerStatusLite { }
2020-02-05 22:16:58 +00:00
for _ , p := range s . Peers {
2020-05-15 20:13:44 +00:00
if ! p . LastHandshake . IsZero ( ) {
2020-10-29 22:26:10 +00:00
fmt . Fprintf ( & peerStats , "%d/%d " , p . RxBytes , p . TxBytes )
fmt . Fprintf ( & peerKeys , "%s " , p . NodeKey . ShortString ( ) )
2020-05-19 02:32:20 +00:00
ret . NumLive ++
ret . LivePeers [ p . NodeKey ] = p
2020-05-15 20:13:44 +00:00
2020-02-05 22:16:58 +00:00
}
2020-05-19 02:32:20 +00:00
ret . RBytes += p . RxBytes
ret . WBytes += p . TxBytes
2020-05-12 16:14:37 +00:00
}
2020-10-29 22:26:10 +00:00
// [GRINDER STATS LINES] - please don't remove (used for log parsing)
if peerStats . Len ( ) > 0 {
2020-12-21 18:58:06 +00:00
b . keyLogf ( "[v1] peer keys: %s" , strings . TrimSpace ( peerKeys . String ( ) ) )
b . statsLogf ( "[v1] v%v peers: %v" , version . Long , strings . TrimSpace ( peerStats . String ( ) ) )
2020-02-05 22:16:58 +00:00
}
2020-05-19 02:32:20 +00:00
return ret
2020-02-05 22:16:58 +00:00
}
2021-01-11 22:24:32 +00:00
// shouldUploadServices reports whether this node should include services
// in Hostinfo. When the user preferences currently request "shields up"
// mode, all inbound connections are refused, so services are not reported.
// Otherwise, shouldUploadServices respects NetMap.CollectServices.
func ( b * LocalBackend ) shouldUploadServices ( ) bool {
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2021-01-11 22:24:32 +00:00
if b . prefs == nil || b . netMap == nil {
return false // default to safest setting
2020-05-19 02:32:20 +00:00
}
2021-01-11 22:24:32 +00:00
return ! b . prefs . ShieldsUp && b . netMap . CollectServices
2020-02-05 22:16:58 +00:00
}
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 21:28:21 +00:00
func ( b * LocalBackend ) SetCurrentUserID ( uid string ) {
b . mu . Lock ( )
b . userID = uid
b . mu . Unlock ( )
}
2020-08-11 02:42:04 +00:00
func ( b * LocalBackend ) SetWantRunning ( wantRunning bool ) {
b . mu . Lock ( )
new := b . prefs . Clone ( )
b . mu . Unlock ( )
if new . WantRunning == wantRunning {
return
}
new . WantRunning = wantRunning
b . logf ( "SetWantRunning: %v" , wantRunning )
b . SetPrefs ( new )
}
2020-05-19 02:32:20 +00:00
// SetPrefs saves new user preferences and propagates them throughout
// the system. Implements Backend.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) SetPrefs ( newp * ipn . Prefs ) {
2020-10-27 19:33:37 +00:00
if newp == nil {
2020-02-20 19:07:00 +00:00
panic ( "SetPrefs got nil prefs" )
}
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
2020-07-29 01:47:23 +00:00
netMap := b . netMap
stateKey := b . stateKey
2020-10-27 19:33:37 +00:00
oldp := b . prefs
newp . Persist = oldp . Persist // caller isn't allowed to override this
b . prefs = newp
2020-11-02 17:52:59 +00:00
b . inServerMode = newp . ForceDaemon
2020-07-29 01:47:23 +00:00
// We do this to avoid holding the lock while doing everything else.
2020-10-27 19:33:37 +00:00
newp = b . prefs . Clone ( )
2020-07-29 01:47:23 +00:00
2020-06-15 23:04:12 +00:00
oldHi := b . hostinfo
2020-02-27 20:20:29 +00:00
newHi := oldHi . Clone ( )
2020-12-24 20:33:55 +00:00
newHi . RoutableIPs = append ( [ ] netaddr . IPPrefix ( nil ) , b . prefs . AdvertiseRoutes ... )
2020-10-27 19:33:37 +00:00
applyPrefsToHostinfo ( newHi , newp )
2020-06-15 23:04:12 +00:00
b . hostinfo = newHi
2020-07-08 23:49:02 +00:00
hostInfoChanged := ! oldHi . Equal ( newHi )
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 21:28:21 +00:00
userID := b . userID
2020-07-29 01:47:23 +00:00
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-07-29 01:47:23 +00:00
if stateKey != "" {
2020-10-27 19:33:37 +00:00
if err := b . store . WriteState ( stateKey , newp . ToBytes ( ) ) ; err != nil {
2020-07-29 01:47:23 +00:00
b . logf ( "Failed to save new controlclient state: %v" , err )
}
}
2020-11-02 17:52:59 +00:00
b . writeServerModeStartState ( userID , newp )
2020-07-29 01:47:23 +00:00
2020-08-25 18:42:54 +00:00
// [GRINDER STATS LINE] - please don't remove (used for log parsing)
2020-10-27 19:33:37 +00:00
b . logf ( "SetPrefs: %v" , newp . Pretty ( ) )
2020-10-27 19:51:48 +00:00
if netMap != nil {
if login := netMap . UserProfiles [ netMap . User ] . LoginName ; login != "" {
if newp . Persist == nil {
b . logf ( "active login: %s" , login )
} else if newp . Persist . LoginName != login {
// Corp issue 461: sometimes the wrong prefs are
// logged; the frontend isn't always getting
// notified (to update its prefs/persist) on
// account switch. Log this while we figure it
// out.
b . logf ( "active login: %s ([unexpected] corp#461, not %s)" , newp . Persist . LoginName )
}
}
}
2020-04-10 15:42:34 +00:00
2020-10-27 19:33:37 +00:00
if oldp . ShieldsUp != newp . ShieldsUp || hostInfoChanged {
2020-04-29 09:23:29 +00:00
b . doSetHostinfoFilterServices ( newHi )
2020-02-18 03:33:01 +00:00
}
2020-10-27 19:33:37 +00:00
b . updateFilter ( netMap , newp )
2020-06-09 17:09:43 +00:00
2020-09-18 14:44:01 +00:00
if netMap != nil {
2020-07-29 01:47:23 +00:00
b . e . SetDERPMap ( netMap . DERPMap )
2020-06-09 17:09:43 +00:00
}
2020-04-29 06:37:35 +00:00
2020-10-27 19:33:37 +00:00
if oldp . WantRunning != newp . WantRunning {
2020-02-05 22:16:58 +00:00
b . stateMachine ( )
} else {
b . authReconfig ( )
}
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { Prefs : newp } )
2020-02-05 22:16:58 +00:00
}
2020-05-19 02:32:20 +00:00
// doSetHostinfoFilterServices calls SetHostinfo on the controlclient,
// possibly after mangling the given hostinfo.
//
// TODO(danderson): we shouldn't be mangling hostinfo here after
// painstakingly constructing it in twelvety other places.
2020-04-29 09:23:29 +00:00
func ( b * LocalBackend ) doSetHostinfoFilterServices ( hi * tailcfg . Hostinfo ) {
hi2 := * hi
2021-01-11 22:24:32 +00:00
if ! b . shouldUploadServices ( ) {
2020-04-29 09:23:29 +00:00
hi2 . Services = [ ] tailcfg . Service { }
}
b . mu . Lock ( )
cli := b . c
b . mu . Unlock ( )
// b.c might not be started yet
if cli != nil {
cli . SetHostinfo ( & hi2 )
}
}
2020-05-19 02:32:20 +00:00
// NetMap returns the latest cached network map received from
// controlclient, or nil if no network map was received yet.
2021-02-05 23:44:46 +00:00
func ( b * LocalBackend ) NetMap ( ) * netmap . NetworkMap {
2020-06-24 20:55:56 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2020-06-15 23:04:12 +00:00
return b . netMap
2020-02-05 22:16:58 +00:00
}
2020-05-19 02:32:20 +00:00
// blockEngineUpdate sets b.blocked to block, while holding b.mu. Its
// indirect effect is to turn b.authReconfig() into a no-op if block
// is true.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) blockEngineUpdates ( block bool ) {
2020-04-11 15:35:34 +00:00
b . logf ( "blockEngineUpdates(%v)" , block )
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
b . blocked = block
b . mu . Unlock ( )
}
2020-05-19 02:32:20 +00:00
// authReconfig pushes a new configuration into wgengine, if engine
// updates are not currently blocked, based on the cached netmap and
// user prefs.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) authReconfig ( ) {
b . mu . Lock ( )
blocked := b . blocked
uc := b . prefs
2020-06-15 23:04:12 +00:00
nm := b . netMap
2020-10-05 22:12:35 +00:00
hasPAC := b . prevIfState . HasPAC ( )
2020-11-10 18:31:07 +00:00
disableSubnetsIfPAC := nm != nil && nm . Debug != nil && nm . Debug . DisableSubnetsIfPAC . EqualBool ( true )
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
if blocked {
2020-04-11 15:35:34 +00:00
b . logf ( "authReconfig: blocked, skipping." )
2020-02-05 22:16:58 +00:00
return
}
if nm == nil {
2020-04-11 15:35:34 +00:00
b . logf ( "authReconfig: netmap not yet valid. Skipping." )
2020-02-05 22:16:58 +00:00
return
}
if ! uc . WantRunning {
2020-04-11 15:35:34 +00:00
b . logf ( "authReconfig: skipping because !WantRunning." )
2020-02-05 22:16:58 +00:00
return
}
2021-02-05 23:44:46 +00:00
var flags netmap . WGConfigFlags
2020-02-05 22:16:58 +00:00
if uc . RouteAll {
2021-02-05 23:44:46 +00:00
flags |= netmap . AllowSubnetRoutes
2020-02-05 22:16:58 +00:00
}
if uc . AllowSingleHosts {
2021-02-05 23:44:46 +00:00
flags |= netmap . AllowSingleHosts
2020-02-05 22:16:58 +00:00
}
2020-11-10 18:31:07 +00:00
if hasPAC && disableSubnetsIfPAC {
2021-02-05 23:44:46 +00:00
if flags & netmap . AllowSubnetRoutes != 0 {
2020-10-05 22:12:35 +00:00
b . logf ( "authReconfig: have PAC; disabling subnet routes" )
2021-02-05 23:44:46 +00:00
flags &^= netmap . AllowSubnetRoutes
2020-10-05 22:12:35 +00:00
}
}
2020-02-05 22:16:58 +00:00
2021-02-25 04:05:23 +00:00
cfg , err := nmcfg . WGCfg ( nm , b . logf , flags , uc . ExitNodeID )
2020-04-10 14:52:30 +00:00
if err != nil {
2020-05-21 19:51:22 +00:00
b . logf ( "wgcfg: %v" , err )
return
2020-04-10 14:52:30 +00:00
}
2020-02-05 22:16:58 +00:00
2020-07-31 20:27:09 +00:00
rcfg := routerConfig ( cfg , uc )
// If CorpDNS is false, rcfg.DNS remains the zero value.
if uc . CorpDNS {
proxied := nm . DNS . Proxied
2021-01-05 18:37:15 +00:00
if proxied && len ( nm . DNS . Nameservers ) == 0 {
b . logf ( "[unexpected] dns proxied but no nameservers" )
proxied = false
2020-07-31 20:27:09 +00:00
}
rcfg . DNS = dns . Config {
Nameservers : nm . DNS . Nameservers ,
2021-01-05 18:37:15 +00:00
Domains : nm . DNS . Domains ,
2020-07-31 20:27:09 +00:00
PerDomain : nm . DNS . PerDomain ,
Proxied : proxied ,
}
}
err = b . e . Reconfig ( cfg , rcfg )
2020-04-10 15:42:34 +00:00
if err == wgengine . ErrNoChanges {
return
2020-02-05 22:16:58 +00:00
}
2020-12-21 18:58:06 +00:00
b . logf ( "[v1] authReconfig: ra=%v dns=%v 0x%02x: %v" , uc . RouteAll , uc . CorpDNS , flags , err )
2020-02-05 22:16:58 +00:00
}
2021-01-05 18:37:15 +00:00
// magicDNSRootDomains returns the subset of nm.DNS.Domains that are the search domains for MagicDNS.
// Each entry has a trailing period.
2021-02-05 23:44:46 +00:00
func magicDNSRootDomains ( nm * netmap . NetworkMap ) [ ] string {
2021-01-10 20:03:01 +00:00
if v := nm . MagicDNSSuffix ( ) ; v != "" {
return [ ] string { strings . Trim ( v , "." ) + "." }
2020-07-31 20:27:09 +00:00
}
2021-01-10 20:03:01 +00:00
return nil
2020-07-31 20:27:09 +00:00
}
2021-01-21 01:24:16 +00:00
var (
ipv4Default = netaddr . MustParseIPPrefix ( "0.0.0.0/0" )
ipv6Default = netaddr . MustParseIPPrefix ( "::/0" )
)
2020-07-31 20:27:09 +00:00
// routerConfig produces a router.Config from a wireguard config and IPN prefs.
2021-02-04 21:12:42 +00:00
func routerConfig ( cfg * wgcfg . Config , prefs * ipn . Prefs ) * router . Config {
2020-05-12 07:08:52 +00:00
rs := & router . Config {
2020-12-24 20:33:55 +00:00
LocalAddrs : unmapIPPrefixes ( cfg . Addresses ) ,
SubnetRoutes : unmapIPPrefixes ( prefs . AdvertiseRoutes ) ,
2020-05-13 22:35:22 +00:00
SNATSubnetRoutes : ! prefs . NoSNAT ,
2020-05-15 02:07:06 +00:00
NetfilterMode : prefs . NetfilterMode ,
2020-05-11 21:02:12 +00:00
}
for _ , peer := range cfg . Peers {
2020-12-24 20:33:55 +00:00
rs . Routes = append ( rs . Routes , unmapIPPrefixes ( peer . AllowedIPs ) ... )
2020-05-11 21:02:12 +00:00
}
2021-01-21 01:24:16 +00:00
// Sanity check: we expect the control server to program both a v4
// and a v6 default route, if default routing is on. Fill in
// blackhole routes appropriately if we're missing some. This is
// likely to break some functionality, but if the user expressed a
// preference for routing remotely, we want to avoid leaking
// traffic at the expense of functionality.
if prefs . ExitNodeID != "" || ! prefs . ExitNodeIP . IsZero ( ) {
var default4 , default6 bool
for _ , route := range rs . Routes {
if route == ipv4Default {
default4 = true
} else if route == ipv6Default {
default6 = true
}
if default4 && default6 {
break
}
}
if ! default4 {
rs . Routes = append ( rs . Routes , ipv4Default )
}
if ! default6 {
rs . Routes = append ( rs . Routes , ipv6Default )
}
}
2020-06-08 22:19:26 +00:00
rs . Routes = append ( rs . Routes , netaddr . IPPrefix {
2020-07-31 20:27:09 +00:00
IP : tsaddr . TailscaleServiceIP ( ) ,
2020-06-08 22:19:26 +00:00
Bits : 32 ,
} )
2020-05-11 21:02:12 +00:00
return rs
}
2020-12-24 20:33:55 +00:00
func unmapIPPrefixes ( ippsList ... [ ] netaddr . IPPrefix ) ( ret [ ] netaddr . IPPrefix ) {
for _ , ipps := range ippsList {
for _ , ipp := range ipps {
ret = append ( ret , netaddr . IPPrefix { IP : ipp . IP . Unmap ( ) , Bits : ipp . Bits } )
2020-05-11 21:02:12 +00:00
}
}
return ret
}
2021-02-04 21:12:42 +00:00
func applyPrefsToHostinfo ( hi * tailcfg . Hostinfo , prefs * ipn . Prefs ) {
2020-07-24 21:05:04 +00:00
if h := prefs . Hostname ; h != "" {
hi . Hostname = h
}
if v := prefs . OSVersion ; v != "" {
hi . OSVersion = v
}
if m := prefs . DeviceModel ; m != "" {
hi . DeviceModel = m
}
2020-11-24 15:51:13 +00:00
hi . ShieldsUp = prefs . ShieldsUp
2020-07-24 21:05:04 +00:00
}
2020-05-19 02:32:20 +00:00
// enterState transitions the backend into newState, updating internal
// state and propagating events out as needed.
//
// TODO(danderson): while this isn't a lie, exactly, a ton of other
// places twiddle IPN internal state without going through here, so
// really this is more "one of several places in which random things
// happen".
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) enterState ( newState ipn . State ) {
2020-02-05 22:16:58 +00:00
b . mu . Lock ( )
state := b . state
2020-06-19 17:43:55 +00:00
b . state = newState
2020-02-05 22:16:58 +00:00
prefs := b . prefs
2020-02-25 20:30:28 +00:00
notify := b . notify
2020-07-31 16:39:45 +00:00
bc := b . c
2020-10-06 22:22:46 +00:00
networkUp := b . prevIfState . AnyInterfaceUp ( )
2020-11-24 23:35:04 +00:00
activeLogin := b . activeLogin
authURL := b . authURL
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
if state == newState {
return
}
2020-04-11 15:35:34 +00:00
b . logf ( "Switching ipn state %v -> %v (WantRunning=%v)" ,
2020-02-05 22:16:58 +00:00
state , newState , prefs . WantRunning )
2020-02-25 20:30:28 +00:00
if notify != nil {
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { State : & newState } )
2020-02-05 22:16:58 +00:00
}
2020-07-31 16:39:45 +00:00
if bc != nil {
2021-02-04 21:12:42 +00:00
bc . SetPaused ( newState == ipn . Stopped || ! networkUp )
2020-07-31 16:39:45 +00:00
}
2020-02-05 22:16:58 +00:00
switch newState {
2021-02-04 21:12:42 +00:00
case ipn . NeedsLogin :
2020-11-24 23:35:04 +00:00
systemd . Status ( "Needs login: %s" , authURL )
2020-02-05 22:16:58 +00:00
b . blockEngineUpdates ( true )
fallthrough
2021-02-04 21:12:42 +00:00
case ipn . Stopped :
2020-05-31 06:36:57 +00:00
err := b . e . Reconfig ( & wgcfg . Config { } , & router . Config { } )
2020-02-05 22:16:58 +00:00
if err != nil {
2020-04-11 15:35:34 +00:00
b . logf ( "Reconfig(down): %v" , err )
2020-02-05 22:16:58 +00:00
}
2020-11-24 23:35:04 +00:00
if authURL == "" {
systemd . Status ( "Stopped; run 'tailscale up' to log in" )
}
2021-02-04 21:12:42 +00:00
case ipn . Starting , ipn . NeedsMachineAuth :
2020-02-05 22:16:58 +00:00
b . authReconfig ( )
// Needed so that UpdateEndpoints can run
b . e . RequestStatus ( )
2021-02-04 21:12:42 +00:00
case ipn . Running :
2020-11-24 23:35:04 +00:00
var addrs [ ] string
for _ , addr := range b . netMap . Addresses {
addrs = append ( addrs , addr . IP . String ( ) )
}
systemd . Status ( "Connected; %s; %s" , activeLogin , strings . Join ( addrs , " " ) )
2020-02-05 22:16:58 +00:00
default :
2020-04-11 15:35:34 +00:00
b . logf ( "[unexpected] unknown newState %#v" , newState )
2020-02-05 22:16:58 +00:00
}
}
2020-05-19 02:32:20 +00:00
// nextState returns the state the backend seems to be in, based on
// its internal state.
2021-02-04 21:12:42 +00:00
func ( b * LocalBackend ) nextState ( ) ipn . State {
2020-02-28 19:39:13 +00:00
b . mu . Lock ( )
b . assertClientLocked ( )
2020-02-29 02:34:56 +00:00
var (
c = b . c
2020-06-15 23:04:12 +00:00
netMap = b . netMap
2020-02-29 02:34:56 +00:00
state = b . state
wantRunning = b . prefs . WantRunning
)
2020-02-28 19:39:13 +00:00
b . mu . Unlock ( )
2020-05-19 02:32:20 +00:00
switch {
case netMap == nil :
2020-02-25 20:30:28 +00:00
if c . AuthCantContinue ( ) {
2020-02-05 22:16:58 +00:00
// Auth was interrupted or waiting for URL visit,
// so it won't proceed without human help.
2021-02-04 21:12:42 +00:00
return ipn . NeedsLogin
2020-02-05 22:16:58 +00:00
} else {
// Auth or map request needs to finish
return state
}
2020-05-19 02:32:20 +00:00
case ! wantRunning :
2021-02-04 21:12:42 +00:00
return ipn . Stopped
2020-05-19 02:32:20 +00:00
case ! netMap . Expiry . IsZero ( ) && time . Until ( netMap . Expiry ) <= 0 :
2021-02-04 21:12:42 +00:00
return ipn . NeedsLogin
2020-05-19 02:32:20 +00:00
case netMap . MachineStatus != tailcfg . MachineAuthorized :
2020-02-05 22:16:58 +00:00
// TODO(crawshaw): handle tailcfg.MachineInvalid
2021-02-04 21:12:42 +00:00
return ipn . NeedsMachineAuth
case state == ipn . NeedsMachineAuth :
2020-02-05 22:16:58 +00:00
// (if we get here, we know MachineAuthorized == true)
2021-02-04 21:12:42 +00:00
return ipn . Starting
case state == ipn . Starting :
2020-05-19 02:32:20 +00:00
if st := b . getEngineStatus ( ) ; st . NumLive > 0 || st . LiveDERPs > 0 {
2021-02-04 21:12:42 +00:00
return ipn . Running
2020-02-05 22:16:58 +00:00
} else {
return state
}
2021-02-04 21:12:42 +00:00
case state == ipn . Running :
return ipn . Running
2020-05-19 02:32:20 +00:00
default :
2021-02-04 21:12:42 +00:00
return ipn . Starting
2020-02-05 22:16:58 +00:00
}
}
2020-05-19 02:32:20 +00:00
// RequestEngineStatus implements Backend.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) RequestEngineStatus ( ) {
b . e . RequestStatus ( )
}
2020-05-19 02:32:20 +00:00
// RequestStatus implements Backend.
2020-03-27 20:26:35 +00:00
func ( b * LocalBackend ) RequestStatus ( ) {
st := b . Status ( )
2021-02-04 21:12:42 +00:00
b . send ( ipn . Notify { Status : st } )
2020-03-27 20:26:35 +00:00
}
2020-05-19 02:32:20 +00:00
// stateMachine updates the state machine state based on other things
// that have happened. It is invoked from the various callbacks that
// feed events into LocalBackend.
//
2020-02-05 22:16:58 +00:00
// TODO(apenwarr): use a channel or something to prevent re-entrancy?
// Or maybe just call the state machine from fewer places.
func ( b * LocalBackend ) stateMachine ( ) {
b . enterState ( b . nextState ( ) )
}
2020-05-19 02:32:20 +00:00
// stopEngineAndWait deconfigures the local network data plane, and
// waits for it to deliver a status update before returning.
//
// TODO(danderson): this may be racy. We could unblock upon receiving
// a status update that predates the "I've shut down" update.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) stopEngineAndWait ( ) {
2020-04-11 15:35:34 +00:00
b . logf ( "stopEngineAndWait..." )
2020-05-31 06:36:57 +00:00
b . e . Reconfig ( & wgcfg . Config { } , & router . Config { } )
2020-02-05 22:16:58 +00:00
b . requestEngineStatusAndWait ( )
2020-04-11 15:35:34 +00:00
b . logf ( "stopEngineAndWait: done." )
2020-02-05 22:16:58 +00:00
}
// Requests the wgengine status, and does not return until the status
// was delivered (to the usual callback).
func ( b * LocalBackend ) requestEngineStatusAndWait ( ) {
2020-04-11 15:35:34 +00:00
b . logf ( "requestEngineStatusAndWait" )
2020-02-05 22:16:58 +00:00
b . statusLock . Lock ( )
go b . e . RequestStatus ( )
2020-04-11 15:35:34 +00:00
b . logf ( "requestEngineStatusAndWait: waiting..." )
2020-02-05 22:16:58 +00:00
b . statusChanged . Wait ( ) // temporarily releases lock while waiting
2020-04-11 15:35:34 +00:00
b . logf ( "requestEngineStatusAndWait: got status update." )
2020-02-05 22:16:58 +00:00
b . statusLock . Unlock ( )
}
2020-05-19 02:32:20 +00:00
// Logout tells the controlclient that we want to log out, and transitions the local engine to the logged-out state without waiting for controlclient to be in that state.
//
// TODO(danderson): controlclient Logout does nothing useful, and we
// shouldn't be transitioning to a state based on what we believe
// controlclient may have done.
//
2020-02-05 22:16:58 +00:00
// NOTE(apenwarr): No easy way to persist logged-out status.
// Maybe that's for the better; if someone logs out accidentally,
// rebooting will fix it.
func ( b * LocalBackend ) Logout ( ) {
2020-02-28 19:39:13 +00:00
b . mu . Lock ( )
c := b . c
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( nil )
2020-02-28 19:39:13 +00:00
b . mu . Unlock ( )
2020-09-11 22:10:29 +00:00
if c == nil {
// Double Logout can happen via repeated IPN
// connections to ipnserver making it repeatedly
// transition from 1->0 total connections, which on
// Windows by default ("client mode") causes a Logout
// on the transition to zero.
// Previously this crashed when we asserted that c was non-nil
// here.
return
}
2020-02-25 20:30:28 +00:00
c . Logout ( )
2020-02-05 22:16:58 +00:00
2020-02-25 20:30:28 +00:00
b . mu . Lock ( )
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( nil )
2020-02-28 19:39:13 +00:00
b . mu . Unlock ( )
b . stateMachine ( )
2020-02-25 20:30:28 +00:00
}
2020-05-19 02:32:20 +00:00
// assertClientLocked crashes if there is no controlclient in this backend.
2020-02-25 20:30:28 +00:00
func ( b * LocalBackend ) assertClientLocked ( ) {
2020-02-05 22:16:58 +00:00
if b . c == nil {
panic ( "LocalBackend.assertClient: b.c == nil" )
}
}
2020-02-25 22:05:17 +00:00
2020-06-15 23:04:12 +00:00
// setNetInfo sets b.hostinfo.NetInfo to ni, and passes ni along to the
2020-05-19 02:32:20 +00:00
// controlclient, if one exists.
func ( b * LocalBackend ) setNetInfo ( ni * tailcfg . NetInfo ) {
2020-03-04 06:21:56 +00:00
b . mu . Lock ( )
c := b . c
2020-06-15 23:04:12 +00:00
if b . hostinfo != nil {
b . hostinfo . NetInfo = ni . Clone ( )
2020-02-25 22:05:17 +00:00
}
2020-03-04 06:21:56 +00:00
b . mu . Unlock ( )
2020-02-25 22:05:17 +00:00
2020-03-04 06:21:56 +00:00
if c == nil {
return
2020-02-25 22:05:17 +00:00
}
2020-03-04 06:21:56 +00:00
c . SetNetInfo ( ni )
2020-02-25 22:05:17 +00:00
}
2020-05-27 19:23:17 +00:00
2021-02-05 23:44:46 +00:00
func ( b * LocalBackend ) setNetMapLocked ( nm * netmap . NetworkMap ) {
2020-10-27 19:51:48 +00:00
var login string
if nm != nil {
login = nm . UserProfiles [ nm . User ] . LoginName
if login == "" {
login = "<missing-profile>"
}
}
b . netMap = nm
if login != b . activeLogin {
b . logf ( "active login: %v" , login )
b . activeLogin = login
}
2021-01-28 23:29:17 +00:00
if nm == nil {
b . nodeByAddr = nil
return
}
// Update the nodeByAddr index.
if b . nodeByAddr == nil {
b . nodeByAddr = map [ netaddr . IP ] * tailcfg . Node { }
}
// First pass, mark everything unwanted.
for k := range b . nodeByAddr {
b . nodeByAddr [ k ] = nil
}
addNode := func ( n * tailcfg . Node ) {
for _ , ipp := range n . Addresses {
if ipp . IsSingleIP ( ) {
b . nodeByAddr [ ipp . IP ] = n
}
}
}
if nm . SelfNode != nil {
addNode ( nm . SelfNode )
}
for _ , p := range nm . Peers {
addNode ( p )
}
// Third pass, actually delete the unwanted items.
for k , v := range b . nodeByAddr {
if v == nil {
delete ( b . nodeByAddr , k )
}
}
2020-10-27 19:51:48 +00:00
}
2020-05-27 19:23:17 +00:00
// TestOnlyPublicKeys returns the current machine and node public
// keys. Used in tests only to facilitate automated node authorization
// in the test harness.
func ( b * LocalBackend ) TestOnlyPublicKeys ( ) ( machineKey tailcfg . MachineKey , nodeKey tailcfg . NodeKey ) {
b . mu . Lock ( )
prefs := b . prefs
2020-09-28 22:28:26 +00:00
machinePrivKey := b . machinePrivKey
2020-05-27 19:23:17 +00:00
b . mu . Unlock ( )
2020-09-28 22:28:26 +00:00
if prefs == nil || machinePrivKey . IsZero ( ) {
2020-05-27 19:23:17 +00:00
return
}
2020-09-28 22:28:26 +00:00
mk := machinePrivKey . Public ( )
2020-05-27 19:23:17 +00:00
nk := prefs . Persist . PrivateNodeKey . Public ( )
return tailcfg . MachineKey ( mk ) , tailcfg . NodeKey ( nk )
}
2020-10-21 19:55:03 +00:00
// temporarilySetMachineKeyInPersist reports whether we should set
// the machine key in Prefs.Persist.LegacyFrontendPrivateMachineKey
// for the frontend to write out to its preferences for use later.
//
// TODO: remove this in Tailscale 1.3.x (so it effectively always
// returns false). It just exists so users can downgrade from 1.2.x to
// 1.0.x. But eventually we want to stop sending the machine key to
// clients. We can't do that until 1.0.x is no longer supported.
func temporarilySetMachineKeyInPersist ( ) bool {
2020-11-11 17:04:34 +00:00
switch runtime . GOOS {
case "darwin" , "ios" , "android" :
ipn: don't temporarilySetMachineKeyInPersist for Android clients
Without this change, newly installed Android clients crash on startup
with
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x9881b9f8]
goroutine 29 [running]:
tailscale.com/ipn.(*LocalBackend).initMachineKeyLocked.func1(0x50cb1b9c, 0x503c9a00)
/home/elias/proj/tailscale/ipn/local.go:711 +0x2c
tailscale.com/ipn.(*LocalBackend).initMachineKeyLocked(0x503c9a00, 0x0, 0x0)
/home/elias/proj/tailscale/ipn/local.go:736 +0x728
tailscale.com/ipn.(*LocalBackend).loadStateLocked(0x503c9a00, 0x988be40e, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0)
/home/elias/proj/tailscale/ipn/local.go:817 +0x1e8
tailscale.com/ipn.(*LocalBackend).Start(0x503c9a00, 0x0, 0x0, 0x988be40e, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/home/elias/proj/tailscale/ipn/local.go:412 +0x200
main.(*backend).Start(...)
/home/elias/proj/tailscale-android/cmd/tailscale/backend.go:116
main.(*App).runBackend.func3(0x50106340, 0x5000c060, 0x50d9a280)
/home/elias/proj/tailscale-android/cmd/tailscale/main.go:169 +0x90
created by main.(*App).runBackend
/home/elias/proj/tailscale-android/cmd/tailscale/main.go:168 +0x27c
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-10-30 12:30:13 +00:00
// iOS, macOS, Android users can't downgrade anyway.
2020-10-21 19:55:03 +00:00
return false
}
return true
}