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-03-14 03:53:58 +00:00
"context"
2022-11-02 03:37:13 +00:00
"encoding/base64"
2022-11-07 23:32:53 +00:00
"encoding/json"
2020-02-03 18:35:52 +00:00
"errors"
2020-02-05 22:16:58 +00:00
"fmt"
2021-03-30 19:56:00 +00:00
"io"
2021-03-26 20:44:55 +00:00
"net"
2021-04-07 15:39:08 +00:00
"net/http"
2022-07-25 03:08:42 +00:00
"net/netip"
2022-11-02 03:37:13 +00:00
"net/url"
2020-09-17 14:59:55 +00:00
"os"
2021-04-17 04:01:29 +00:00
"os/user"
2021-03-29 17:42:33 +00:00
"path/filepath"
2020-10-21 19:55:03 +00:00
"runtime"
2021-04-08 21:54:25 +00:00
"sort"
2021-03-26 20:44:55 +00:00
"strconv"
2020-02-05 22:16:58 +00:00
"strings"
"sync"
2021-04-02 15:21:40 +00:00
"sync/atomic"
2020-02-05 22:16:58 +00:00
"time"
2022-11-07 23:32:53 +00:00
"go4.org/mem"
2022-07-25 03:08:42 +00:00
"go4.org/netipx"
2022-10-04 03:39:45 +00:00
"golang.org/x/exp/slices"
2021-04-13 15:13:46 +00:00
"tailscale.com/client/tailscale/apitype"
2020-02-05 22:16:58 +00:00
"tailscale.com/control/controlclient"
2022-09-26 17:07:28 +00:00
"tailscale.com/doctor"
"tailscale.com/doctor/routetable"
2022-01-24 18:52:57 +00:00
"tailscale.com/envknob"
2021-02-25 05:29:51 +00:00
"tailscale.com/health"
2022-11-11 17:43:49 +00:00
"tailscale.com/health/healthmsg"
2021-08-20 17:34:13 +00:00
"tailscale.com/hostinfo"
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"
2021-03-25 21:50:21 +00:00
"tailscale.com/net/dns"
2022-09-05 18:36:30 +00:00
"tailscale.com/net/dnsfallback"
2020-08-28 04:25:17 +00:00
"tailscale.com/net/interfaces"
2022-03-28 17:24:11 +00:00
"tailscale.com/net/netutil"
2020-07-31 20:27:09 +00:00
"tailscale.com/net/tsaddr"
2021-12-01 04:39:12 +00:00
"tailscale.com/net/tsdial"
2021-03-29 17:42:33 +00:00
"tailscale.com/paths"
2020-02-05 22:16:58 +00:00
"tailscale.com/portlist"
2022-08-04 17:43:49 +00:00
"tailscale.com/syncs"
2020-02-05 22:16:58 +00:00
"tailscale.com/tailcfg"
2022-08-01 22:46:41 +00:00
"tailscale.com/tka"
2021-08-05 21:05:24 +00:00
"tailscale.com/types/dnstype"
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"
2021-05-27 17:07:17 +00:00
"tailscale.com/types/preftype"
2022-02-22 17:52:49 +00:00
"tailscale.com/types/views"
2021-07-03 04:30:29 +00:00
"tailscale.com/util/deephash"
2021-04-09 22:24:47 +00:00
"tailscale.com/util/dnsname"
2022-10-04 03:39:45 +00:00
"tailscale.com/util/mak"
2021-11-02 21:30:48 +00:00
"tailscale.com/util/multierr"
2021-04-22 07:25:00 +00:00
"tailscale.com/util/osshare"
2020-11-24 23:35:04 +00:00
"tailscale.com/util/systemd"
2022-11-07 23:32:53 +00:00
"tailscale.com/util/uniq"
2020-02-05 22:16:58 +00:00
"tailscale.com/version"
2021-05-27 17:07:17 +00:00
"tailscale.com/version/distro"
2020-02-05 22:16:58 +00:00
"tailscale.com/wgengine"
"tailscale.com/wgengine/filter"
2021-12-28 21:39:04 +00:00
"tailscale.com/wgengine/magicsock"
2020-05-11 21:02:12 +00:00
"tailscale.com/wgengine/router"
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 {
2022-01-24 18:52:57 +00:00
if e := envknob . String ( "TS_DEBUG_CONTROL_FLAGS" ) ; e != "" {
2020-10-20 17:40:12 +00:00
return strings . Split ( e , "," )
}
return nil
}
2020-10-19 22:56:59 +00:00
2022-04-15 20:19:13 +00:00
// SSHServer is the interface of the conditionally linked ssh/tailssh.server.
type SSHServer interface {
HandleSSHConn ( net . Conn ) error
2022-04-17 18:49:56 +00:00
// OnPolicyChange is called when the SSH access policy changes,
// so that existing sessions can be re-evaluated for validity
// and closed if they'd no longer be accepted.
OnPolicyChange ( )
2022-05-28 11:33:46 +00:00
// Shutdown is called when tailscaled is shutting down.
Shutdown ( )
2022-04-15 20:19:13 +00:00
}
type newSSHServerFunc func ( logger . Logf , * LocalBackend ) ( SSHServer , error )
var newSSHServer newSSHServerFunc // or nil
// RegisterNewSSHServer lets the conditionally linked ssh/tailssh package register itself.
func RegisterNewSSHServer ( fn newSSHServerFunc ) {
newSSHServer = fn
}
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-16 05:20:48 +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
2022-11-09 05:58:10 +00:00
pm * profileManager
2021-03-16 05:20:48 +00:00
store ipn . StateStore
2021-12-01 04:39:12 +00:00
dialer * tsdial . Dialer // non-nil
2021-03-16 05:20:48 +00:00
backendLogID string
unregisterLinkMon func ( )
unregisterHealthWatch 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 )
2021-11-03 16:03:11 +00:00
varRoot string // or empty if SetVarRoot never called
2022-08-04 04:51:02 +00:00
sshAtomicBool atomic . Bool
2022-05-30 09:06:46 +00:00
shutdownCalled bool // if Shutdown has been called
2020-05-19 02:32:20 +00:00
2022-11-09 05:58:10 +00:00
// lastProfileID tracks the last profile we've seen from the ProfileManager.
// It's used to detect when the user has changed their profile.
lastProfileID ipn . ProfileID
2022-11-07 23:32:53 +00:00
filterAtomic atomic . Pointer [ filter . Filter ]
containsViaIPFuncAtomic syncs . AtomicValue [ func ( netip . Addr ) bool ]
shouldInterceptTCPPortAtomic syncs . AtomicValue [ func ( uint16 ) bool ]
2021-11-25 17:43:39 +00:00
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
2022-04-17 21:49:16 +00:00
filterHash deephash . Sum
2021-04-07 15:39:08 +00:00
httpTestClient * http . Client // for controlclient. nil by default, used by tests.
2021-04-30 14:23:22 +00:00
ccGen clientGen // function for producing controlclient; lazily populated
2022-05-30 09:06:46 +00:00
sshServer SSHServer // or nil, initialized lazily.
2021-02-04 21:12:42 +00:00
notify func ( ipn . Notify )
2021-04-30 03:27:00 +00:00
cc controlclient . Client
2022-06-20 01:00:50 +00:00
ccAuto * controlclient . Auto // if cc is of type *controlclient.Auto
2020-11-02 17:52:59 +00:00
inServerMode bool
2021-09-03 20:17:46 +00:00
machinePrivKey key . MachinePrivate
2022-08-01 22:46:41 +00:00
nlPrivKey key . NLPrivate
2022-08-26 16:45:16 +00:00
tka * tkaState
2021-02-04 21:12:42 +00:00
state ipn . State
2021-04-22 07:25:00 +00:00
capFileSharing bool // whether netMap contains the file sharing capability
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-03-25 22:38:40 +00:00
netMap * netmap . NetworkMap
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
nodeByAddr map [ netip . Addr ] * tailcfg . Node
2021-03-25 22:38:40 +00:00
activeLogin string // last logged LoginName from netMap
engineStatus ipn . EngineStatus
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
endpoints [ ] tailcfg . Endpoint
2021-03-25 22:38:40 +00:00
blocked bool
2021-11-04 19:19:00 +00:00
keyExpired bool
2021-04-21 20:37:36 +00:00
authURL string // cleared on Notify
authURLSticky string // not cleared on Notify
2021-03-25 22:38:40 +00:00
interact bool
2022-09-01 16:27:06 +00:00
egg bool
2021-03-25 22:38:40 +00:00
prevIfState * interfaces . State
2021-03-30 18:19:42 +00:00
peerAPIServer * peerAPIServer // or nil
2021-03-25 22:38:40 +00:00
peerAPIListeners [ ] * peerAPIListener
2022-02-18 20:55:22 +00:00
loginFlags controlclient . LoginFlags
2021-04-08 21:54:25 +00:00
incomingFiles map [ * incomingFile ] bool
2022-03-27 03:12:12 +00:00
lastStatusTime time . Time // status.AsOf value of the last processed status update
2021-04-12 21:05:44 +00:00
// directFileRoot, if non-empty, means to write received files
// directly to this directory, without staging them in an
// intermediate buffered directory for "pick-up" later. If
// empty, the files are received in a daemon-owned location
// and the localapi is used to enumerate, download, and delete
// them. This is used on macOS where the GUI lifetime is the
// same as the Network Extension lifetime and we can thus avoid
// double-copying files by writing them to the right location
// immediately.
2022-07-17 17:05:36 +00:00
// It's also used on several NAS platforms (Synology, TrueNAS, etc)
// but in that case DoFinalRename is also set true, which moves the
// *.partial file to its final name on completion.
2021-12-06 20:24:25 +00:00
directFileRoot string
2022-07-17 17:05:36 +00:00
directFileDoFinalRename bool // false on macOS, true on several NAS platforms
2022-10-04 03:39:45 +00:00
componentLogUntil map [ string ] componentLogState
2020-02-05 22:16:58 +00:00
2022-11-07 23:32:53 +00:00
// ServeConfig fields. (also guarded by mu)
2022-11-09 21:15:59 +00:00
lastServeConfJSON mem . RO // last JSON that was parsed into serveConfig
serveConfig ipn . ServeConfigView // or !Valid if none
2022-11-07 23:32:53 +00:00
2022-11-12 02:46:26 +00:00
serveListeners map [ netip . AddrPort ] * serveListener // addrPort => serveListener
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
2022-09-23 17:06:55 +00:00
// dialPlan is any dial plan that we've received from the control
// server during a previous connection; it is cleared on logout.
dialPlan atomic . Pointer [ tailcfg . ControlDialPlan ]
2022-10-06 18:09:13 +00:00
// tkaSyncLock is used to make tkaSyncIfNeeded an exclusive
// section. This is needed to stop two map-responses in quick succession
// from racing each other through TKA sync logic / RPCs.
//
// tkaSyncLock MUST be taken before mu (or inversely, mu must not be held
// at the moment that tkaSyncLock is taken).
tkaSyncLock sync . Mutex
2020-02-05 22:16:58 +00:00
}
2021-04-30 14:23:22 +00:00
// clientGen is a func that creates a control plane client.
// It's the type used by LocalBackend.SetControlClientGetterForTesting.
2021-04-30 03:27:00 +00:00
type clientGen func ( controlclient . Options ) ( controlclient . Client , error )
2020-02-03 18:35:52 +00:00
// NewLocalBackend returns a new LocalBackend that is ready to run,
// but is not actually running.
2021-12-01 04:39:12 +00:00
//
// If dialer is nil, a new one is made.
2022-11-09 05:58:10 +00:00
func NewLocalBackend ( logf logger . Logf , logid string , store ipn . StateStore , stateKey ipn . StateKey , dialer * tsdial . Dialer , e wgengine . Engine , loginFlags controlclient . LoginFlags ) ( * LocalBackend , error ) {
2020-02-05 22:16:58 +00:00
if e == nil {
2021-12-01 04:39:12 +00:00
panic ( "ipn.NewLocalBackend: engine must not be nil" )
}
2022-02-14 16:32:23 +00:00
2022-11-09 05:58:10 +00:00
pm , err := newProfileManager ( store , logf , stateKey )
if err != nil {
return nil , err
}
2022-02-14 16:32:23 +00:00
hi := hostinfo . New ( )
2022-02-18 04:41:49 +00:00
logf . JSON ( 1 , "Hostinfo" , hi )
2022-02-13 04:42:38 +00:00
envknob . LogCurrent ( logf )
2021-12-01 04:39:12 +00:00
if dialer == nil {
2022-09-30 21:15:17 +00:00
dialer = & tsdial . Dialer { Logf : logf }
2020-02-05 22:16:58 +00:00
}
2021-04-22 07:25:00 +00:00
osshare . SetFileSharingEnabled ( false , logf )
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 ,
2022-11-09 05:58:10 +00:00
pm : pm ,
store : pm . Store ( ) ,
2021-12-01 04:39:12 +00:00
dialer : dialer ,
2020-10-14 21:07:40 +00:00
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 { } ) ,
2022-02-18 20:55:22 +00:00
loginFlags : loginFlags ,
2020-02-05 22:16:58 +00:00
}
2021-12-01 04:39:12 +00:00
2021-11-25 17:43:39 +00:00
// Default filter blocks everything and logs nothing, until Start() is called.
2022-07-25 03:08:42 +00:00
b . setFilter ( filter . NewAllowNone ( logf , & netipx . IPSet { } ) )
2021-11-25 17:43:39 +00:00
2022-11-07 23:32:53 +00:00
b . setTCPPortsIntercepted ( nil )
2020-02-05 22:16:58 +00:00
b . statusChanged = sync . NewCond ( & b . statusLock )
2021-07-19 18:07:42 +00:00
b . e . SetStatusCallback ( b . setWgengineStatus )
2020-02-05 22:16:58 +00:00
2021-03-02 04:45:30 +00:00
linkMon := e . GetLinkMonitor ( )
2021-03-26 04:41:37 +00:00
b . prevIfState = linkMon . InterfaceState ( )
2021-03-02 04:45:30 +00:00
// 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 )
2021-03-16 05:20:48 +00:00
b . unregisterHealthWatch = health . RegisterWatcher ( b . onHealthChange )
2021-03-29 22:17:05 +00:00
wiredPeerAPIPort := false
if ig , ok := e . ( wgengine . InternalsGetter ) ; ok {
2022-04-14 21:55:23 +00:00
if tunWrap , _ , _ , ok := ig . GetInternals ( ) ; ok {
2022-01-31 17:20:22 +00:00
tunWrap . PeerAPIPort = b . GetPeerAPIPort
2021-03-29 22:17:05 +00:00
wiredPeerAPIPort = true
}
}
if ! wiredPeerAPIPort {
b . logf ( "[unexpected] failed to wire up peer API port for engine %T" , e )
}
2022-10-04 03:39:45 +00:00
for _ , component := range debuggableComponents {
key := componentStateKey ( component )
2022-11-09 05:58:10 +00:00
if ut , err := ipn . ReadStoreInt ( pm . Store ( ) , key ) ; err == nil {
2022-10-04 03:39:45 +00:00
if until := time . Unix ( ut , 0 ) ; until . After ( time . Now ( ) ) {
// conditional to avoid log spam at start when off
b . SetComponentDebugLogging ( component , until )
}
}
}
2020-02-25 15:36:32 +00:00
return b , nil
2020-02-05 22:16:58 +00:00
}
2022-10-04 03:39:45 +00:00
type componentLogState struct {
until time . Time
timer * time . Timer // if non-nil, the AfterFunc to disable it
}
var debuggableComponents = [ ] string {
"magicsock" ,
}
func componentStateKey ( component string ) ipn . StateKey {
return ipn . StateKey ( "_debug_" + component + "_until" )
}
// SetComponentDebugLogging sets component's debug logging enabled until the until time.
// If until is in the past, the component's debug logging is disabled.
//
// The following components are recognized:
//
// - magicsock
func ( b * LocalBackend ) SetComponentDebugLogging ( component string , until time . Time ) error {
b . mu . Lock ( )
defer b . mu . Unlock ( )
var setEnabled func ( bool )
switch component {
case "magicsock" :
mc , err := b . magicConn ( )
if err != nil {
return err
}
setEnabled = mc . SetDebugLoggingEnabled
}
if setEnabled == nil || ! slices . Contains ( debuggableComponents , component ) {
return fmt . Errorf ( "unknown component %q" , component )
}
timeUnixOrZero := func ( t time . Time ) int64 {
if t . IsZero ( ) {
return 0
}
return t . Unix ( )
}
ipn . PutStoreInt ( b . store , componentStateKey ( component ) , timeUnixOrZero ( until ) )
now := time . Now ( )
on := now . Before ( until )
setEnabled ( on )
var onFor time . Duration
if on {
onFor = until . Sub ( now )
b . logf ( "debugging logging for component %q enabled for %v (until %v)" , component , onFor . Round ( time . Second ) , until . UTC ( ) . Format ( time . RFC3339 ) )
} else {
b . logf ( "debugging logging for component %q disabled" , component )
}
if oldSt , ok := b . componentLogUntil [ component ] ; ok && oldSt . timer != nil {
oldSt . timer . Stop ( )
}
newSt := componentLogState { until : until }
if on {
newSt . timer = time . AfterFunc ( onFor , func ( ) {
// Turn off logging after the timer fires, as long as the state is
// unchanged when the timer actually fires.
b . mu . Lock ( )
defer b . mu . Unlock ( )
if ls := b . componentLogUntil [ component ] ; ls . until == until {
setEnabled ( false )
b . logf ( "debugging logging for component %q disabled (by timer)" , component )
}
} )
}
mak . Set ( & b . componentLogUntil , component , newSt )
return nil
}
2022-10-15 17:31:35 +00:00
// GetComponentDebugLogging gets the time that component's debug logging is
// enabled until, or the zero time if component's time is not currently
// enabled.
func ( b * LocalBackend ) GetComponentDebugLogging ( component string ) time . Time {
b . mu . Lock ( )
defer b . mu . Unlock ( )
now := time . Now ( )
ls := b . componentLogUntil [ component ]
if ls . until . IsZero ( ) || ls . until . Before ( now ) {
return time . Time { }
}
return ls . until
}
2021-12-01 04:39:12 +00:00
// Dialer returns the backend's dialer.
func ( b * LocalBackend ) Dialer ( ) * tsdial . Dialer {
return b . dialer
}
2021-04-12 21:05:44 +00:00
// SetDirectFileRoot sets the directory to download files to directly,
// without buffering them through an intermediate daemon-owned
// tailcfg.UserID-specific directory.
//
// This must be called before the LocalBackend starts being used.
func ( b * LocalBackend ) SetDirectFileRoot ( dir string ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
b . directFileRoot = dir
}
2021-12-06 20:24:25 +00:00
// SetDirectFileDoFinalRename sets whether the peerapi file server should rename
// a received "name.partial" file to "name" when the download is complete.
//
// This only applies when SetDirectFileRoot is non-empty.
// The default is false.
func ( b * LocalBackend ) SetDirectFileDoFinalRename ( v bool ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
b . directFileDoFinalRename = v
}
2021-07-16 22:21:00 +00:00
// b.mu must be held.
func ( b * LocalBackend ) maybePauseControlClientLocked ( ) {
if b . cc == nil {
return
}
networkUp := b . prevIfState . AnyInterfaceUp ( )
b . cc . SetPaused ( ( b . state == ipn . Stopped && b . netMap != nil ) || ! networkUp )
}
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
2021-07-16 22:21:00 +00:00
b . maybePauseControlClientLocked ( )
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.
2022-11-09 05:58:10 +00:00
b . updateFilterLocked ( b . netMap , b . pm . CurrentPrefs ( ) )
2021-03-30 20:49:08 +00:00
2021-06-14 15:59:09 +00:00
if peerAPIListenAsync && b . netMap != nil && b . state == ipn . Running {
2021-03-30 20:49:08 +00:00
want := len ( b . netMap . Addresses )
if len ( b . peerAPIListeners ) < want {
2021-07-06 16:07:18 +00:00
b . logf ( "linkChange: peerAPIListeners too low; trying again" )
2021-03-30 20:49:08 +00:00
go b . initPeerAPIListener ( )
}
}
2020-08-28 04:25:17 +00:00
}
2021-03-16 05:20:48 +00:00
func ( b * LocalBackend ) onHealthChange ( sys health . Subsystem , err error ) {
if err == nil {
b . logf ( "health(%q): ok" , sys )
} else {
b . logf ( "health(%q): error: %v" , sys , err )
}
}
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 ( )
2022-07-19 03:36:14 +00:00
if b . shutdownCalled {
b . mu . Unlock ( )
return
}
2022-04-25 18:02:59 +00:00
b . shutdownCalled = true
2022-07-19 03:36:14 +00:00
if b . loginFlags & controlclient . LoginEphemeral != 0 {
b . mu . Unlock ( )
ctx , cancel := context . WithTimeout ( b . ctx , 5 * time . Second )
defer cancel ( )
b . LogoutSync ( ctx ) // best effort
b . mu . Lock ( )
}
2021-04-08 04:12:16 +00:00
cc := b . cc
2022-05-28 11:33:46 +00:00
if b . sshServer != nil {
b . sshServer . Shutdown ( )
2022-05-30 09:06:46 +00:00
b . sshServer = nil
2022-05-28 11:33:46 +00:00
}
2022-04-21 02:15:43 +00:00
b . closePeerAPIListenersLocked ( )
2020-05-19 02:32:20 +00:00
b . mu . Unlock ( )
2020-05-21 20:30:20 +00:00
2021-03-01 20:56:03 +00:00
b . unregisterLinkMon ( )
2021-03-16 05:20:48 +00:00
b . unregisterHealthWatch ( )
2021-04-08 04:12:16 +00:00
if cc != nil {
cc . Shutdown ( )
2020-05-19 02:32:20 +00:00
}
2020-05-21 20:30:20 +00:00
b . ctxCancel ( )
2020-02-05 22:16:58 +00:00
b . e . Close ( )
b . e . Wait ( )
}
2022-10-24 00:15:04 +00:00
func stripKeysFromPrefs ( p ipn . PrefsView ) ipn . PrefsView {
if ! p . Valid ( ) || p . Persist ( ) == nil {
return p
}
p2 := p . AsStruct ( )
p2 . Persist . LegacyFrontendPrivateMachineKey = key . MachinePrivate { }
p2 . Persist . PrivateNodeKey = key . NodePrivate { }
p2 . Persist . OldPrivateNodeKey = key . NodePrivate { }
return p2 . View ( )
}
2021-04-07 15:27:35 +00:00
// Prefs returns a copy of b's current prefs, with any private keys removed.
2022-10-24 00:15:04 +00:00
func ( b * LocalBackend ) Prefs ( ) ipn . PrefsView {
2021-04-07 15:27:35 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
return stripKeysFromPrefs ( b . pm . CurrentPrefs ( ) )
2021-04-07 15:27:35 +00:00
}
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 ( )
}
2021-03-19 04:07:58 +00:00
// StatusWithoutPeers is like Status but omits any details
// of peers.
func ( b * LocalBackend ) StatusWithoutPeers ( ) * ipnstate . Status {
sb := new ( ipnstate . StatusBuilder )
b . updateStatus ( sb , nil )
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 )
2021-03-19 04:07:58 +00:00
b . updateStatus ( sb , b . populatePeerStatusLocked )
}
2020-03-26 05:57:46 +00:00
2021-03-19 04:07:58 +00:00
// updateStatus populates sb with status.
//
// extraLocked, if non-nil, is called while b.mu is still held.
func ( b * LocalBackend ) updateStatus ( sb * ipnstate . StatusBuilder , extraLocked func ( * ipnstate . StatusBuilder ) ) {
2020-03-26 05:57:46 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2021-03-25 22:38:40 +00:00
sb . MutateStatus ( func ( s * ipnstate . Status ) {
s . Version = version . Long
s . BackendState = b . state . String ( )
2021-04-21 20:37:36 +00:00
s . AuthURL = b . authURLSticky
2021-09-02 02:27:22 +00:00
if err := health . OverallError ( ) ; err != nil {
switch e := err . ( type ) {
2021-11-02 21:30:48 +00:00
case multierr . Error :
for _ , err := range e . Errors ( ) {
2021-09-02 02:27:22 +00:00
s . Health = append ( s . Health , err . Error ( ) )
}
default :
s . Health = append ( s . Health , err . Error ( ) )
}
}
2022-06-17 19:09:23 +00:00
if m := b . sshOnButUnusableHealthCheckMessageLocked ( ) ; m != "" {
s . Health = append ( s . Health , m )
}
2021-03-25 22:38:40 +00:00
if b . netMap != nil {
2021-06-15 19:12:15 +00:00
s . CertDomains = append ( [ ] string ( nil ) , b . netMap . DNS . CertDomains ... )
2022-02-15 17:36:01 +00:00
s . MagicDNSSuffix = b . netMap . MagicDNSSuffix ( )
if s . CurrentTailnet == nil {
s . CurrentTailnet = & ipnstate . TailnetStatus { }
}
s . CurrentTailnet . MagicDNSSuffix = b . netMap . MagicDNSSuffix ( )
s . CurrentTailnet . MagicDNSEnabled = b . netMap . DNS . Proxied
s . CurrentTailnet . Name = b . netMap . Domain
2022-11-11 17:43:49 +00:00
if prefs := b . pm . CurrentPrefs ( ) ; prefs . Valid ( ) {
if ! prefs . RouteAll ( ) && b . netMap . AnyPeersAdvertiseRoutes ( ) {
s . Health = append ( s . Health , healthmsg . WarnAcceptRoutesOff )
}
if ! prefs . ExitNodeID ( ) . IsZero ( ) {
if exitPeer , ok := b . netMap . PeerWithStableID ( prefs . ExitNodeID ( ) ) ; ok {
var online = false
if exitPeer . Online != nil {
online = * exitPeer . Online
}
s . ExitNodeStatus = & ipnstate . ExitNodeStatus {
ID : prefs . ExitNodeID ( ) ,
Online : online ,
TailscaleIPs : exitPeer . Addresses ,
}
2022-06-07 19:31:10 +00:00
}
}
}
2021-03-25 22:38:40 +00:00
}
} )
sb . MutateSelfStatus ( func ( ss * ipnstate . PeerStatus ) {
2021-12-16 16:06:32 +00:00
ss . Online = health . GetInPollNetMap ( )
2021-11-26 01:11:01 +00:00
if b . netMap != nil {
2022-10-28 16:39:01 +00:00
ss . InNetworkMap = true
2021-11-26 01:11:01 +00:00
ss . HostName = b . netMap . Hostinfo . Hostname
ss . DNSName = b . netMap . Name
ss . UserID = b . netMap . User
if sn := b . netMap . SelfNode ; sn != nil {
2022-10-28 16:39:01 +00:00
peerStatusFromNode ( ss , sn )
2021-11-26 01:11:01 +00:00
if c := sn . Capabilities ; len ( c ) > 0 {
ss . Capabilities = append ( [ ] string ( nil ) , c ... )
}
}
} else {
ss . HostName , _ = os . Hostname ( )
2021-05-07 14:27:14 +00:00
}
2021-03-25 22:38:40 +00:00
for _ , pln := range b . peerAPIListeners {
2021-03-26 20:44:55 +00:00
ss . PeerAPIURL = append ( ss . PeerAPIURL , pln . urlStr )
2021-03-25 22:38:40 +00:00
}
} )
2020-03-26 05:57:46 +00:00
// TODO: hostinfo, and its networkinfo
// TODO: EngineStatus copy (and deprecate it?)
2021-03-19 04:07:58 +00:00
if extraLocked != nil {
extraLocked ( sb )
}
}
func ( b * LocalBackend ) populatePeerStatusLocked ( sb * ipnstate . StatusBuilder ) {
if b . netMap == nil {
return
}
for id , up := range b . netMap . UserProfiles {
sb . AddUser ( id , up )
}
2022-11-09 05:58:10 +00:00
exitNodeID := b . pm . CurrentPrefs ( ) . ExitNodeID ( )
2021-03-19 04:07:58 +00:00
for _ , p := range b . netMap . Peers {
var lastSeen time . Time
if p . LastSeen != nil {
lastSeen = * p . LastSeen
2020-03-26 05:57:46 +00:00
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var tailscaleIPs = make ( [ ] netip . Addr , 0 , len ( p . Addresses ) )
2021-03-19 04:07:58 +00:00
for _ , addr := range p . Addresses {
2022-07-25 03:08:42 +00:00
if addr . IsSingleIP ( ) && tsaddr . IsTailscaleIP ( addr . Addr ( ) ) {
tailscaleIPs = append ( tailscaleIPs , addr . Addr ( ) )
2020-03-26 05:57:46 +00:00
}
}
2022-10-28 16:39:01 +00:00
ps := & ipnstate . PeerStatus {
InNetworkMap : true ,
UserID : p . User ,
TailscaleIPs : tailscaleIPs ,
HostName : p . Hostinfo . Hostname ( ) ,
DNSName : p . Name ,
OS : p . Hostinfo . OS ( ) ,
KeepAlive : p . KeepAlive ,
LastSeen : lastSeen ,
Online : p . Online != nil && * p . Online ,
ShareeNode : p . Hostinfo . ShareeNode ( ) ,
2022-11-09 05:58:10 +00:00
ExitNode : p . StableID != "" && p . StableID == exitNodeID ,
2022-10-28 16:39:01 +00:00
SSH_HostKeys : p . Hostinfo . SSH_HostKeys ( ) . AsSlice ( ) ,
2022-02-22 17:52:49 +00:00
}
2022-10-28 16:39:01 +00:00
peerStatusFromNode ( ps , p )
sb . AddPeer ( p . Key , ps )
}
}
// peerStatusFromNode copies fields that exist in the Node struct for
// current node and peers into the provided PeerStatus.
func peerStatusFromNode ( ps * ipnstate . PeerStatus , n * tailcfg . Node ) {
ps . ID = n . StableID
ps . Created = n . Created
ps . ExitNodeOption = tsaddr . ContainsExitRoutes ( n . AllowedIPs )
if n . Tags != nil {
v := views . SliceOf ( n . Tags )
ps . Tags = & v
}
if n . PrimaryRoutes != nil {
v := views . IPPrefixSliceOf ( n . PrimaryRoutes )
ps . PrimaryRoutes = & v
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-03-15 21:59:35 +00:00
// WhoIs reports the node and user who owns the node with the given IP:port.
// If the IP address is a Tailscale IP, the provided port may be 0.
2021-01-28 23:29:17 +00:00
// If ok == true, n and u are valid.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) WhoIs ( ipp netip . AddrPort ) ( n * tailcfg . Node , u tailcfg . UserProfile , ok bool ) {
2021-01-28 23:29:17 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-07-25 03:08:42 +00:00
n , ok = b . nodeByAddr [ ipp . Addr ( ) ]
2021-01-28 23:29:17 +00:00
if ! ok {
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var ip netip . Addr
2021-05-15 01:07:28 +00:00
if ipp . Port ( ) != 0 {
2021-03-15 21:59:35 +00:00
ip , ok = b . e . WhoIsIPPort ( ipp )
}
if ! ok {
return nil , u , false
}
n , ok = b . nodeByAddr [ ip ]
if ! ok {
return nil , u , false
}
2021-01-28 23:29:17 +00:00
}
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
}
2022-03-18 18:48:40 +00:00
// PeerCaps returns the capabilities that remote src IP has to
// ths current node.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) PeerCaps ( src netip . Addr ) [ ] string {
2022-03-18 18:48:40 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-09-19 17:36:07 +00:00
return b . peerCapsLocked ( src )
}
func ( b * LocalBackend ) peerCapsLocked ( src netip . Addr ) [ ] string {
2022-03-18 18:48:40 +00:00
if b . netMap == nil {
return nil
}
2022-08-04 04:31:40 +00:00
filt := b . filterAtomic . Load ( )
if filt == nil {
2022-03-18 18:48:40 +00:00
return nil
}
for _ , a := range b . netMap . Addresses {
if ! a . IsSingleIP ( ) {
continue
}
2022-09-19 17:36:07 +00:00
dst := a . Addr ( )
if dst . BitLen ( ) == src . BitLen ( ) { // match on family
return filt . AppendCaps ( nil , src , dst )
2022-03-18 18:48:40 +00:00
}
}
return nil
}
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.
2021-10-26 17:19:35 +00:00
if st . Err != nil {
2020-07-29 01:47:23 +00:00
// TODO(crawshaw): display in the UI.
2021-10-26 17:19:35 +00:00
if errors . Is ( st . Err , io . EOF ) {
2020-12-21 18:58:06 +00:00
b . logf ( "[v1] Received error: EOF" )
2021-11-03 22:42:40 +00:00
return
}
b . logf ( "Received error: %v" , st . Err )
var uerr controlclient . UserVisibleError
if errors . As ( st . Err , & uerr ) {
s := uerr . UserVisibleError ( )
b . send ( ipn . Notify { ErrMessage : & s } )
2020-12-21 18:58:06 +00:00
}
2020-07-29 01:47:23 +00:00
return
}
2021-05-06 03:28:29 +00:00
b . mu . Lock ( )
wasBlocked := b . blocked
2021-11-04 19:19:00 +00:00
keyExpiryExtended := false
if st . NetMap != nil {
wasExpired := b . keyExpired
isExpired := ! st . NetMap . Expiry . IsZero ( ) && st . NetMap . Expiry . Before ( time . Now ( ) )
if wasExpired && ! isExpired {
keyExpiryExtended = true
}
b . keyExpired = isExpired
}
2021-05-06 03:28:29 +00:00
b . mu . Unlock ( )
2021-11-04 19:19:00 +00:00
if keyExpiryExtended && wasBlocked {
// Key extended, unblock the engine
b . blockEngineUpdates ( false )
}
2021-05-06 03:28:29 +00:00
if st . LoginFinished != nil && wasBlocked {
2020-06-15 23:04:12 +00:00
// 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 ( )
ipnlocal: don't assume NeedsLogin immediately after StartLogout().
Previously, there was no server round trip required to log out, so when
you asked ipnlocal to Logout(), it could clear the netmap immediately
and switch to NeedsLogin state.
In v1.8, we added a true Logout operation. ipn.Logout() would trigger
an async cc.StartLogout() and *also* immediately switch to NeedsLogin.
Unfortunately, some frontends would see NeedsLogin and immediately
trigger a new StartInteractiveLogin() operation, before the
controlclient auth state machine actually acted on the Logout command,
thus accidentally invalidating the entire logout operation, retaining
the netmap, and violating the user's expectations.
Instead, add a new LogoutFinished signal from controlclient
(paralleling LoginFinished) and, upon starting a logout, don't update
the ipn state machine until it's received.
Updates: #1918 (BUG-2)
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-20 06:46:57 +00:00
if st . LogoutFinished != nil {
// Since we're logged out now, our netmap cache is invalid.
// Since st.NetMap==nil means "netmap is unchanged", there is
// no other way to represent this change.
b . setNetMapLocked ( nil )
2021-12-15 23:55:02 +00:00
b . e . SetNetworkMap ( new ( netmap . NetworkMap ) )
ipnlocal: don't assume NeedsLogin immediately after StartLogout().
Previously, there was no server round trip required to log out, so when
you asked ipnlocal to Logout(), it could clear the netmap immediately
and switch to NeedsLogin state.
In v1.8, we added a true Logout operation. ipn.Logout() would trigger
an async cc.StartLogout() and *also* immediately switch to NeedsLogin.
Unfortunately, some frontends would see NeedsLogin and immediately
trigger a new StartInteractiveLogin() operation, before the
controlclient auth state machine actually acted on the Logout command,
thus accidentally invalidating the entire logout operation, retaining
the netmap, and violating the user's expectations.
Instead, add a new LogoutFinished signal from controlclient
(paralleling LoginFinished) and, upon starting a logout, don't update
the ipn state machine until it's received.
Updates: #1918 (BUG-2)
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-20 06:46:57 +00:00
}
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( ) . AsStruct ( )
2020-07-29 01:47:23 +00:00
netMap := b . netMap
interact := b . interact
ipn{,/ipnlocal}, cmd/tailscale/cli: don't check pref reverts on initial up
The ipn.NewPrefs func returns a populated ipn.Prefs for historical
reasons. It's not used or as important as it once was, but it hasn't
yet been removed. Meanwhile, it contains some default values that are
used on some platforms. Notably, for this bug (#1725), Windows/Mac use
its Prefs.RouteAll true value (to accept subnets), but Linux users
have always gotten a "false" value for that, because that's what
cmd/tailscale's CLI default flag is _for all operating systems_. That
meant that "tailscale up" was rightfully reporting that the user was
changing an implicit setting: RouteAll was changing from true with
false with the user explicitly saying so.
An obvious fix might be to change ipn.NewPrefs to return
Prefs.RouteAll == false on some platforms, but the logic is
complicated by darwin: we want RouteAll true on windows, android, ios,
and the GUI mac app, but not the CLI tailscaled-on-macOS mode. But
even if we used build tags (e.g. the "redo" build tag) to determine
what the default is, that then means we have duplicated and differing
"defaults" between both the CLI up flags and ipn.NewPrefs. Furthering
that complication didn't seem like a good idea.
So, changing the NewPrefs defaults is too invasive at this stage of
the release, as is removing the NewPrefs func entirely.
Instead, tweak slightly the semantics of the ipn.Prefs.ControlURL
field. This now defines that a ControlURL of the empty string means
both "we're uninitialized" and also "just use the default".
Then, once we have the "empty-string-means-unintialized" semantics,
use that to suppress "tailscale up"'s recent implicit-setting-revert
checking safety net, if we've never initialized Tailscale yet.
And update/add tests.
Fixes #1725
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-18 05:50:58 +00:00
if prefs . ControlURL == "" {
// Once we get a message from the control plane, set
// our ControlURL pref explicitly. This causes a
// future "tailscale up" to start checking for
// implicit setting reverts, which it doesn't do when
// ControlURL is blank.
prefs . ControlURL = prefs . ControlURLOrDefault ( )
prefsChanged = true
}
2022-11-10 14:43:59 +00:00
if st . Persist != nil && st . Persist . Valid ( ) {
if ! prefs . Persist . View ( ) . Equals ( * st . Persist ) {
2020-07-29 01:47:23 +00:00
prefsChanged = true
2022-11-10 14:43:59 +00:00
prefs . Persist = st . Persist . AsStruct ( )
2020-07-29 01:47:23 +00:00
}
}
if st . URL != "" {
b . authURL = st . URL
2021-04-21 20:37:36 +00:00
b . authURLSticky = st . URL
2020-07-29 01:47:23 +00:00
}
2021-05-06 03:28:29 +00:00
if wasBlocked && st . LoginFinished != nil {
// Interactive login finished successfully (URL visited).
// After an interactive login, the user always wants
// WantRunning.
2022-10-23 17:07:10 +00:00
if ! prefs . WantRunning || prefs . LoggedOut {
2020-07-29 01:47:23 +00:00
prefsChanged = true
}
2022-10-23 17:07:10 +00:00
prefs . WantRunning = true
prefs . LoggedOut = false
2020-07-29 01:47:23 +00:00
}
2022-10-25 18:01:33 +00:00
if findExitNodeIDLocked ( prefs , st . NetMap ) {
prefsChanged = true
}
2020-07-29 01:47:23 +00:00
// Prefs will be written out; this is not safe unless locked or cloned.
2022-04-17 21:49:16 +00:00
if st . NetMap != nil {
2022-10-25 18:01:33 +00:00
b . mu . Unlock ( ) // respect locking rules for tkaSyncIfNeeded
2022-11-11 06:29:03 +00:00
if err := b . tkaSyncIfNeeded ( st . NetMap , prefs . View ( ) ) ; err != nil {
2022-10-25 18:01:33 +00:00
b . logf ( "[v1] TKA sync error: %v" , err )
}
b . mu . Lock ( )
2022-10-27 20:40:31 +00:00
if b . tka != nil {
head , err := b . tka . authority . Head ( ) . MarshalText ( )
if err != nil {
b . logf ( "[v1] error marshalling tka head: %v" , err )
} else {
b . cc . SetTKAHead ( string ( head ) )
}
} else {
b . cc . SetTKAHead ( "" )
}
2022-10-25 18:01:33 +00:00
if ! envknob . TKASkipSignatureCheck ( ) {
b . tkaFilterNetmapLocked ( st . NetMap )
}
b . setNetMapLocked ( st . NetMap )
2022-11-09 05:58:10 +00:00
b . updateFilterLocked ( st . NetMap , prefs . View ( ) )
2022-04-17 21:49:16 +00:00
}
2020-06-15 23:04:12 +00:00
2020-07-29 01:47:23 +00:00
if prefsChanged {
2022-11-09 05:58:10 +00:00
if err := b . pm . SetPrefs ( prefs . View ( ) ) ; err != nil {
b . logf ( "Failed to save new controlclient state: %v" , err )
2020-06-15 23:04:12 +00:00
}
2022-11-09 05:58:10 +00:00
}
b . mu . Unlock ( )
2022-11-09 23:28:36 +00:00
2022-11-09 05:58:10 +00:00
// Now complete the lock-free parts of what we started while locked.
if prefsChanged {
2022-11-03 22:46:17 +00:00
p := prefs . View ( )
b . send ( ipn . Notify { Prefs : & p } )
2020-06-15 23:04:12 +00:00
}
2022-11-09 05:58:10 +00:00
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 {
2022-02-13 00:22:33 +00:00
b . logf ( "[v1] netmap diff:\n%v" , diff )
2020-06-15 23:04:12 +00:00
}
}
2020-07-29 01:47:23 +00:00
b . e . SetNetworkMap ( st . NetMap )
2020-09-18 14:44:01 +00:00
b . e . SetDERPMap ( st . NetMap . DERPMap )
2020-07-29 01:47:23 +00:00
2022-09-05 18:36:30 +00:00
// Update our cached DERP map
dnsfallback . UpdateCache ( st . NetMap . DERPMap )
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
}
2022-10-23 17:07:10 +00:00
// findExitNodeIDLocked updates prefs to reference an exit node by ID, rather
// than by IP. It returns whether prefs was mutated.
func findExitNodeIDLocked ( prefs * ipn . Prefs , nm * netmap . NetworkMap ) ( prefsChanged bool ) {
2021-12-01 23:33:14 +00:00
if nm == nil {
// No netmap, can't resolve anything.
return false
}
2021-01-21 01:24:16 +00:00
// If we have a desired IP on file, try to find the corresponding
// node.
2022-10-23 17:07:10 +00:00
if ! prefs . ExitNodeIP . IsValid ( ) {
2021-02-25 04:05:23 +00:00
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.
2022-10-23 17:07:10 +00:00
if prefs . ExitNodeID != "" {
prefs . ExitNodeID = ""
2021-02-25 04:05:23 +00:00
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 {
2022-10-23 17:07:10 +00:00
if ! addr . IsSingleIP ( ) || addr . Addr ( ) != 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.
2022-10-23 17:07:10 +00:00
prefs . ExitNodeID = peer . StableID
prefs . ExitNodeIP = netip . Addr { }
2021-02-25 04:05:23 +00:00
return true
2021-01-21 01:24:16 +00:00
}
}
2022-10-23 17:07:10 +00:00
return prefsChanged
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 )
2021-09-15 19:59:47 +00:00
b . broadcastStatusChanged ( )
2020-06-15 23:04:12 +00:00
return
}
if s == nil {
b . logf ( "[unexpected] non-error wgengine update with status=nil: %v" , s )
2021-09-15 19:59:47 +00:00
b . broadcastStatusChanged ( )
2020-06-15 23:04:12 +00:00
return
}
b . mu . Lock ( )
2022-03-27 03:12:12 +00:00
if s . AsOf . Before ( b . lastStatusTime ) {
// Don't process a status update that is older than the one we have
// already processed. (corp#2579)
b . mu . Unlock ( )
return
}
b . lastStatusTime = s . AsOf
2020-10-29 22:26:10 +00:00
es := b . parseWgStatusLocked ( s )
2021-04-08 04:12:16 +00:00
cc := b . cc
2020-06-15 23:04:12 +00:00
b . engineStatus = es
2021-09-15 19:10:15 +00:00
needUpdateEndpoints := ! endpointsEqual ( s . LocalAddrs , b . endpoints )
if needUpdateEndpoints {
b . endpoints = append ( [ ] tailcfg . Endpoint { } , s . LocalAddrs ... )
}
2020-06-15 23:04:12 +00:00
b . mu . Unlock ( )
2021-04-08 04:12:16 +00:00
if cc != nil {
2021-09-15 19:10:15 +00:00
if needUpdateEndpoints {
2022-06-19 23:31:54 +00:00
cc . UpdateEndpoints ( s . LocalAddrs )
2021-09-15 19:10:15 +00:00
}
2021-07-19 18:07:42 +00:00
b . stateMachine ( )
2020-06-15 23:04:12 +00:00
}
2021-09-15 19:59:47 +00:00
b . broadcastStatusChanged ( )
b . send ( ipn . Notify { Engine : & es } )
}
2021-09-16 19:56:43 +00:00
2021-09-15 19:59:47 +00:00
func ( b * LocalBackend ) broadcastStatusChanged ( ) {
// The sync.Cond docs say: "It is allowed but not required for the caller to hold c.L during the call."
// In this particular case, we must acquire b.statusLock. Otherwise we might broadcast before
// the waiter (in requestEngineStatusAndWait) starts to wait, in which case
// the waiter can get stuck indefinitely. See PR 2865.
2021-09-16 19:56:43 +00:00
b . statusLock . Lock ( )
2020-06-15 23:04:12 +00:00
b . statusChanged . Broadcast ( )
2021-09-16 19:56:43 +00:00
b . statusLock . Unlock ( )
2020-06-15 23:04:12 +00:00
}
2021-09-15 19:10:15 +00:00
func endpointsEqual ( x , y [ ] tailcfg . Endpoint ) bool {
if len ( x ) != len ( y ) {
return false
}
for i := range x {
if x [ i ] != y [ i ] {
return false
}
}
return true
}
2021-04-07 05:11:50 +00:00
func ( b * LocalBackend ) SetNotifyCallback ( notify func ( ipn . Notify ) ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
b . notify = notify
}
2021-04-07 15:39:08 +00:00
// SetHTTPTestClient sets an alternate HTTP client to use with
// connections to the coordination server. It exists for
// testing. Using nil means to use the default.
func ( b * LocalBackend ) SetHTTPTestClient ( c * http . Client ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
b . httpTestClient = c
}
2021-04-30 14:23:22 +00:00
// SetControlClientGetterForTesting sets the func that creates a
// control plane client. It can be called at most once, before Start.
func ( b * LocalBackend ) SetControlClientGetterForTesting ( newControlClient func ( controlclient . Options ) ( controlclient . Client , error ) ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . ccGen != nil {
panic ( "invalid use of SetControlClientGetterForTesting after Start" )
}
b . ccGen = newControlClient
}
func ( b * LocalBackend ) getNewControlClientFunc ( ) clientGen {
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . ccGen == nil {
// Initialize it rather than just returning the
// default to make any future call to
// SetControlClientGetterForTesting panic.
b . ccGen = func ( opts controlclient . Options ) ( controlclient . Client , error ) {
return controlclient . New ( opts )
}
}
return b . ccGen
}
2021-04-23 17:26:25 +00:00
// startIsNoopLocked reports whether a Start call on this LocalBackend
// with the provided Start Options would be a useless no-op.
//
2022-08-02 16:33:46 +00:00
// TODO(apenwarr): we shouldn't need this. The state machine is now
// nearly clean enough where it can accept a new connection while in
// any state, not just Running, and on any platform. We'd want to add
// a few more tests to state_test.go to ensure this continues to work
// as expected.
ipnlocal: don't assume NeedsLogin immediately after StartLogout().
Previously, there was no server round trip required to log out, so when
you asked ipnlocal to Logout(), it could clear the netmap immediately
and switch to NeedsLogin state.
In v1.8, we added a true Logout operation. ipn.Logout() would trigger
an async cc.StartLogout() and *also* immediately switch to NeedsLogin.
Unfortunately, some frontends would see NeedsLogin and immediately
trigger a new StartInteractiveLogin() operation, before the
controlclient auth state machine actually acted on the Logout command,
thus accidentally invalidating the entire logout operation, retaining
the netmap, and violating the user's expectations.
Instead, add a new LogoutFinished signal from controlclient
(paralleling LoginFinished) and, upon starting a logout, don't update
the ipn state machine until it's received.
Updates: #1918 (BUG-2)
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-20 06:46:57 +00:00
//
2021-04-23 17:26:25 +00:00
// b.mu must be held.
func ( b * LocalBackend ) startIsNoopLocked ( opts ipn . Options ) bool {
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
// Options has 5 fields; check all of them:
2021-04-23 17:26:25 +00:00
// * FrontendLogID
// * StateKey
// * Prefs
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
// * UpdatePrefs
2021-04-23 17:26:25 +00:00
// * AuthKey
return b . state == ipn . Running &&
b . hostinfo != nil &&
b . hostinfo . FrontendLogID == opts . FrontendLogID &&
2022-11-09 05:58:10 +00:00
opts . LegacyMigrationPrefs == nil &&
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
opts . UpdatePrefs == nil &&
2021-04-23 17:26:25 +00:00
opts . AuthKey == ""
}
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 {
2022-11-09 05:58:10 +00:00
if opts . LegacyMigrationPrefs == nil && ! b . pm . CurrentPrefs ( ) . Valid ( ) {
return errors . New ( "no prefs provided" )
2020-02-03 23:58:40 +00:00
}
2022-11-09 05:58:10 +00:00
if opts . LegacyMigrationPrefs != nil {
b . logf ( "Start: %v" , opts . LegacyMigrationPrefs . 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
2021-04-23 17:26:25 +00:00
b . mu . Lock ( )
2022-11-09 05:58:10 +00:00
profileID := b . pm . CurrentProfile ( ) . ID
2021-04-23 17:26:25 +00:00
// The iOS client sends a "Start" whenever its UI screen comes
// up, just because it wants a netmap. That should be fixed,
// but meanwhile we can make Start cheaper here for such a
// case and not restart the world (which takes a few seconds).
// Instead, just send a notify with the state that iOS needs.
2022-11-09 05:58:10 +00:00
if b . startIsNoopLocked ( opts ) && profileID == b . lastProfileID {
2021-04-23 17:26:25 +00:00
b . logf ( "Start: already running; sending notify" )
nm := b . netMap
state := b . state
b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
p := b . pm . CurrentPrefs ( )
2021-04-23 17:26:25 +00:00
b . send ( ipn . Notify {
State : & state ,
NetMap : nm ,
2022-11-03 22:46:17 +00:00
Prefs : & p ,
2021-04-23 17:26:25 +00:00
LoginFinished : new ( empty . Message ) ,
} )
return nil
}
2021-08-20 17:34:13 +00:00
hostinfo := hostinfo . New ( )
2020-06-15 23:04:12 +00:00
hostinfo . BackendLogID = b . backendLogID
hostinfo . FrontendLogID = opts . FrontendLogID
2022-08-30 19:30:55 +00:00
hostinfo . Userspace . Set ( wgengine . IsNetstack ( b . e ) )
hostinfo . UserspaceRouter . Set ( wgengine . IsNetstackRouter ( b . e ) )
2020-02-05 22:16:58 +00:00
2021-04-08 04:12:16 +00:00
if b . cc != nil {
2020-02-25 20:30:28 +00:00
// 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.
2021-04-30 13:14:56 +00:00
b . mu . Unlock ( )
2021-04-08 04:12:16 +00:00
b . cc . Shutdown ( )
2021-04-30 13:14:56 +00:00
b . mu . Lock ( )
2020-02-25 20:30:28 +00:00
}
2021-04-07 15:39:08 +00:00
httpTestClient := b . httpTestClient
2020-02-25 20:30:28 +00:00
2020-06-15 23:04:12 +00:00
if b . hostinfo != nil {
2022-05-03 22:07:30 +00:00
hostinfo . Services = b . hostinfo . Services // keep any previous services
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
2022-11-09 05:58:10 +00:00
if err := b . migrateStateLocked ( opts . LegacyMigrationPrefs ) ; 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
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
if opts . UpdatePrefs != nil {
2022-11-09 05:58:10 +00:00
oldPrefs := b . pm . CurrentPrefs ( )
newPrefs := opts . UpdatePrefs . Clone ( )
newPrefs . Persist = oldPrefs . Persist ( )
pv := newPrefs . View ( )
if err := b . pm . SetPrefs ( pv ) ; err != nil {
b . logf ( "failed to save UpdatePrefs state: %v" , err )
2021-07-13 21:03:05 +00:00
}
2022-11-09 05:58:10 +00:00
b . setAtomicValuesFromPrefs ( pv )
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
}
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
wantRunning := prefs . WantRunning ( )
2021-04-02 15:21:40 +00:00
if wantRunning {
if err := b . initMachineKeyLocked ( ) ; err != nil {
return fmt . Errorf ( "initMachineKeyLocked: %w" , err )
}
}
2022-08-01 22:46:41 +00:00
if err := b . initNLKeyLocked ( ) ; err != nil {
return fmt . Errorf ( "initNLKeyLocked: %w" , err )
}
2021-04-02 15:21:40 +00:00
2022-11-09 05:58:10 +00:00
loggedOut := prefs . LoggedOut ( )
2021-04-30 07:56:11 +00:00
2022-11-09 05:58:10 +00:00
b . inServerMode = prefs . ForceDaemon ( )
b . serverURL = prefs . ControlURLOrDefault ( )
2020-11-04 18:24:33 +00:00
if b . inServerMode || runtime . GOOS == "windows" {
b . logf ( "Start: serverMode=%v" , b . inServerMode )
}
2022-11-09 05:58:10 +00:00
b . applyPrefsToHostinfo ( hostinfo , prefs )
2020-02-18 03:33:01 +00:00
2020-10-27 19:51:48 +00:00
b . setNetMapLocked ( nil )
2022-11-09 05:58:10 +00:00
persistv := prefs . Persist ( )
if persistv == nil {
persistv = new ( persist . Persist )
}
2022-10-23 17:07:10 +00:00
b . updateFilterLocked ( nil , ipn . PrefsView { } )
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
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 :
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] got initial portlist info in %v" , time . Since ( t0 ) . Round ( time . Millisecond ) )
2020-10-14 21:07:40 +00:00
timer . Stop ( )
case <- timer . C :
b . logf ( "timeout waiting for initial portlist" )
}
} )
}
2022-08-09 17:16:34 +00:00
discoPublic := b . e . DiscoPublicKey ( )
2020-06-19 19:06:49 +00:00
2020-02-05 22:16:58 +00:00
var err error
2021-04-14 16:37:54 +00:00
isNetstack := wgengine . IsNetstackRouter ( b . e )
debugFlags := controlDebugFlags
if isNetstack {
debugFlags = append ( [ ] string { "netstack" } , debugFlags ... )
}
ipnlocal: accept a new opts.UpdatePrefs field.
This is needed because the original opts.Prefs field was at some point
subverted for use in frontend->backend state migration for backward
compatibility on some platforms. We still need that feature, but we
also need the feature of providing the full set of prefs from
`tailscale up`, *not* including overwriting the prefs.Persist keys, so
we can't use the original field from `tailscale up`.
`tailscale up` had attempted to compensate for that by doing SetPrefs()
before Start(), but that violates the ipn.Backend contract, which says
you should call Start() before anything else (that's why it's called
Start()). As a result, doing SetPrefs({ControlURL=...,
WantRunning=true}) would cause a connection to the *previous* control
server (because WantRunning=true), and then connect to the *new*
control server only after running Start().
This problem may have been avoided before, but only by pure luck.
It turned out to be relatively harmless since the connection to the old
control server was immediately closed and replaced anyway, but it
created a race condition that could have caused spurious notifications
or rejected keys if the server responded quickly.
As already covered by existing TODOs, a better fix would be to have
Start() get out of the business of state migration altogether. But
we're approaching a release so I want to make the minimum possible fix.
Fixes #1840.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2021-05-04 08:26:07 +00:00
// TODO(apenwarr): The only way to change the ServerURL is to
// re-run b.Start(), because this is the only place we create a
// new controlclient. SetPrefs() allows you to overwrite ServerURL,
// but it won't take effect until the next Start().
2021-04-30 14:23:22 +00:00
cc , err := b . getNewControlClientFunc ( ) ( controlclient . Options {
2021-04-02 15:21:40 +00:00
GetMachinePrivateKey : b . createGetMachinePrivateKeyFunc ( ) ,
2022-10-13 17:43:00 +00:00
GetNLPrivateKey : b . createGetNLPrivateKeyFunc ( ) ,
2021-04-02 15:21:40 +00:00
Logf : logger . WithPrefix ( b . logf , "control: " ) ,
Persist : * persistv ,
ServerURL : b . serverURL ,
AuthKey : opts . AuthKey ,
Hostinfo : hostinfo ,
KeepAlive : true ,
NewDecompressor : b . newDecompressor ,
2021-04-07 15:39:08 +00:00
HTTPTestClient : httpTestClient ,
2021-04-02 15:21:40 +00:00
DiscoPublicKey : discoPublic ,
2021-04-14 16:37:54 +00:00
DebugFlags : debugFlags ,
2021-04-02 15:21:40 +00:00
LinkMonitor : b . e . GetLinkMonitor ( ) ,
2022-06-06 20:07:14 +00:00
Pinger : b ,
2022-03-21 21:10:25 +00:00
PopBrowserURL : b . tellClientToBrowseToURL ,
2022-04-27 18:57:59 +00:00
Dialer : b . Dialer ( ) ,
2022-06-20 01:14:45 +00:00
Status : b . setClientStatus ,
2022-08-27 19:55:41 +00:00
C2NHandler : http . HandlerFunc ( b . handleC2N ) ,
2022-09-23 17:06:55 +00:00
DialPlan : & b . dialPlan , // pointer because it can't be copied
2021-03-31 18:55:21 +00:00
2021-09-15 23:26:38 +00:00
// Don't warn about broken Linux IP forwarding when
2021-03-31 18:55:21 +00:00
// netstack is being used.
2021-04-14 16:37:54 +00:00
SkipIPForwardingCheck : isNetstack ,
2020-02-05 22:16:58 +00:00
} )
if err != nil {
return err
}
b . mu . Lock ( )
2021-04-08 04:17:33 +00:00
b . cc = cc
2022-06-20 01:00:50 +00:00
b . ccAuto , _ = cc . ( * controlclient . Auto )
2020-02-28 20:12:49 +00:00
endpoints := b . endpoints
2022-10-27 20:40:31 +00:00
var tkaHead string
if b . tka != nil {
head , err := b . tka . authority . Head ( ) . MarshalText ( )
if err != nil {
b . mu . Unlock ( )
return fmt . Errorf ( "marshalling tka head: %w" , err )
}
tkaHead = string ( head )
}
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2020-02-28 20:12:49 +00:00
if endpoints != nil {
2022-06-19 23:31:54 +00:00
cc . UpdateEndpoints ( endpoints )
2020-02-05 22:16:58 +00:00
}
2022-10-27 20:40:31 +00:00
cc . SetTKAHead ( tkaHead )
2020-02-05 22:16:58 +00:00
2020-05-19 02:32:20 +00:00
b . e . SetNetInfoCallback ( b . setNetInfo )
2020-03-24 05:16:16 +00:00
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 } )
2022-11-03 22:46:17 +00:00
b . send ( ipn . Notify { Prefs : & prefs } )
2020-02-05 22:16:58 +00:00
2021-04-30 09:27:37 +00:00
if ! loggedOut && b . hasNodeKey ( ) {
// Even if !WantRunning, we should verify our key, if there
// is one. If you want tailscaled to be completely idle,
// use logout instead.
2021-04-08 04:17:33 +00:00
cc . Login ( nil , controlclient . LoginDefault )
2021-04-02 15:21:40 +00:00
}
2021-04-30 09:27:37 +00:00
b . stateMachine ( )
2020-02-05 22:16:58 +00:00
return nil
}
2022-04-17 21:49:16 +00:00
// updateFilterLocked updates the packet filter in wgengine based on the
2020-05-19 02:32:20 +00:00
// given netMap and user preferences.
2022-04-17 21:49:16 +00:00
//
// b.mu must be held.
2022-10-23 17:07:10 +00:00
func ( b * LocalBackend ) updateFilterLocked ( netMap * netmap . NetworkMap , prefs ipn . PrefsView ) {
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
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
addrs [ ] netip . Prefix
2020-11-10 05:33:41 +00:00
packetFilter [ ] filter . Match
2022-07-25 03:08:42 +00:00
localNetsB netipx . IPSetBuilder
logNetsB netipx . IPSetBuilder
2022-10-23 17:07:10 +00:00
shieldsUp = ! prefs . Valid ( ) || prefs . ShieldsUp ( ) // Be conservative when not ready
2020-07-31 20:03:00 +00:00
)
2021-03-10 00:10:30 +00:00
// Log traffic for Tailscale IPs.
logNetsB . AddPrefix ( tsaddr . CGNATRange ( ) )
logNetsB . AddPrefix ( tsaddr . TailscaleULARange ( ) )
logNetsB . RemovePrefix ( tsaddr . ChromeOSVMRange ( ) )
2020-07-31 20:03:00 +00:00
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
2022-11-02 20:13:26 +00:00
if packetFilterPermitsUnlockedNodes ( netMap . Peers , packetFilter ) {
err := errors . New ( "server sent invalid packet filter permitting traffic to unlocked nodes; rejecting all packets for safety" )
health . SetValidUnsignedNodes ( err )
packetFilter = nil
} else {
health . SetValidUnsignedNodes ( nil )
}
2020-05-22 02:41:18 +00:00
}
2022-10-23 17:07:10 +00:00
if prefs . Valid ( ) {
ar := prefs . AdvertiseRoutes ( )
for i := 0 ; i < ar . Len ( ) ; i ++ {
r := ar . At ( i )
2021-05-15 01:07:28 +00:00
if r . Bits ( ) == 0 {
2021-02-23 04:43:35 +00:00
// 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.
2021-12-16 19:15:45 +00:00
localInterfaceRoutes , hostIPs , err := interfaceRoutes ( )
if err != nil {
b . logf ( "getting local interface routes: %v" , err )
continue
}
s , err := shrinkDefaultRoute ( r , localInterfaceRoutes , hostIPs )
2021-02-23 04:43:35 +00:00
if err != nil {
b . logf ( "computing default route filter: %v" , err )
continue
}
localNetsB . AddSet ( s )
} else {
localNetsB . AddPrefix ( r )
2021-03-10 00:10:30 +00:00
// When advertising a non-default route, we assume
// this is a corporate subnet that should be present
// in the audit logs.
logNetsB . AddPrefix ( r )
2021-02-23 04:43:35 +00:00
}
2021-02-22 22:34:15 +00:00
}
2020-07-29 01:47:23 +00:00
}
2021-06-02 16:04:37 +00:00
localNets , _ := localNetsB . IPSet ( )
logNets , _ := logNetsB . IPSet ( )
2022-04-18 23:17:10 +00:00
var sshPol tailcfg . SSHPolicy
if haveNetmap && netMap . SSHPolicy != nil {
sshPol = * netMap . SSHPolicy
}
2020-07-29 01:47:23 +00:00
2022-08-15 18:22:28 +00:00
changed := deephash . Update ( & b . filterHash , & struct {
HaveNetmap bool
Addrs [ ] netip . Prefix
FilterMatch [ ] filter . Match
LocalNets [ ] netipx . IPRange
LogNets [ ] netipx . IPRange
ShieldsUp bool
SSHPolicy tailcfg . SSHPolicy
} { haveNetmap , addrs , packetFilter , localNets . Ranges ( ) , logNets . Ranges ( ) , shieldsUp , sshPol } )
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 {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] netmap packet filter: (not ready yet)" )
2021-11-25 17:43:39 +00:00
b . setFilter ( filter . NewAllowNone ( b . logf , logNets ) )
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 {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] netmap packet filter: (shields up)" )
2021-11-25 17:43:39 +00:00
b . setFilter ( filter . NewShieldsUpFilter ( localNets , logNets , oldFilter , b . logf ) )
2020-08-01 02:07:14 +00:00
} else {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] netmap packet filter: %v filters" , len ( packetFilter ) )
2021-11-25 17:43:39 +00:00
b . setFilter ( filter . New ( packetFilter , localNets , logNets , oldFilter , b . logf ) )
2020-05-22 02:41:18 +00:00
}
2022-04-17 18:49:56 +00:00
if b . sshServer != nil {
go b . sshServer . OnPolicyChange ( )
}
2020-07-29 01:47:23 +00:00
}
2022-11-02 20:13:26 +00:00
// packetFilterPermitsUnlockedNodes reports any peer in peers with the
// UnsignedPeerAPIOnly bool set true has any of its allowed IPs in the packet
// filter.
//
// If this reports true, the packet filter is invalid (the server is either broken
// or malicious) and should be ignored for safety.
func packetFilterPermitsUnlockedNodes ( peers [ ] * tailcfg . Node , packetFilter [ ] filter . Match ) bool {
var b netipx . IPSetBuilder
var numUnlocked int
for _ , p := range peers {
if ! p . UnsignedPeerAPIOnly {
continue
}
numUnlocked ++
for _ , a := range p . AllowedIPs { // not only addresses!
b . AddPrefix ( a )
}
}
if numUnlocked == 0 {
return false
}
s , err := b . IPSet ( )
if err != nil {
// Shouldn't happen, but if it does, fail closed.
return true
}
for _ , m := range packetFilter {
for _ , r := range m . Srcs {
if ! s . OverlapsPrefix ( r ) {
continue
}
if len ( m . Dsts ) != 0 {
return true
}
}
}
return false
}
2021-11-25 17:43:39 +00:00
func ( b * LocalBackend ) setFilter ( f * filter . Filter ) {
b . filterAtomic . Store ( f )
b . e . SetFilter ( f )
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var removeFromDefaultRoute = [ ] netip . Prefix {
2021-02-23 04:43:35 +00:00
// RFC1918 LAN ranges
2022-07-26 03:55:44 +00:00
netip . MustParsePrefix ( "192.168.0.0/16" ) ,
netip . MustParsePrefix ( "172.16.0.0/12" ) ,
netip . MustParsePrefix ( "10.0.0.0/8" ) ,
2021-03-18 00:04:32 +00:00
// IPv4 link-local
2022-07-26 03:55:44 +00:00
netip . MustParsePrefix ( "169.254.0.0/16" ) ,
2021-03-18 00:04:32 +00:00
// IPv4 multicast
2022-07-26 03:55:44 +00:00
netip . MustParsePrefix ( "224.0.0.0/4" ) ,
2021-02-23 04:43:35 +00:00
// Tailscale IPv4 range
tsaddr . CGNATRange ( ) ,
// IPv6 Link-local addresses
2022-07-26 03:55:44 +00:00
netip . MustParsePrefix ( "fe80::/10" ) ,
2021-03-18 00:04:32 +00:00
// IPv6 multicast
2022-07-26 03:55:44 +00:00
netip . MustParsePrefix ( "ff00::/8" ) ,
2021-02-23 04:43:35 +00:00
// Tailscale IPv6 range
tsaddr . TailscaleULARange ( ) ,
}
2021-07-02 14:15:28 +00:00
// internalAndExternalInterfaces splits interface routes into "internal"
// and "external" sets. Internal routes are those of virtual ethernet
// network interfaces used by guest VMs and containers, such as WSL and
// Docker.
//
// Given that "internal" routes don't leave the device, we choose to
// trust them more, allowing access to them when an Exit Node is enabled.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func internalAndExternalInterfaces ( ) ( internal , external [ ] netip . Prefix , err error ) {
2021-10-14 20:40:06 +00:00
il , err := interfaces . GetList ( )
if err != nil {
return nil , nil , err
}
return internalAndExternalInterfacesFrom ( il , runtime . GOOS )
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func internalAndExternalInterfacesFrom ( il interfaces . List , goos string ) ( internal , external [ ] netip . Prefix , err error ) {
2021-10-14 20:40:06 +00:00
// We use an IPSetBuilder here to canonicalize the prefixes
// and to remove any duplicate entries.
2022-07-25 03:08:42 +00:00
var internalBuilder , externalBuilder netipx . IPSetBuilder
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
if err := il . ForeachInterfaceAddress ( func ( iface interfaces . Interface , pfx netip . Prefix ) {
2022-07-25 03:08:42 +00:00
if tsaddr . IsTailscaleIP ( pfx . Addr ( ) ) {
2021-07-02 14:15:28 +00:00
return
}
if pfx . IsSingleIP ( ) {
return
}
2021-10-14 20:09:06 +00:00
if iface . IsLoopback ( ) {
2021-10-14 20:40:06 +00:00
internalBuilder . AddPrefix ( pfx )
2021-10-14 20:09:06 +00:00
return
}
2021-10-14 20:40:06 +00:00
if goos == "windows" {
2021-07-02 14:15:28 +00:00
// Windows Hyper-V prefixes all MAC addresses with 00:15:5d.
// https://docs.microsoft.com/en-us/troubleshoot/windows-server/virtualization/default-limit-256-dynamic-mac-addresses
//
// This includes WSL2 vEthernet.
// Importantly: by default WSL2 /etc/resolv.conf points to
// a stub resolver running on the host vEthernet IP.
// So enabling exit nodes with the default tailnet
// configuration breaks WSL2 DNS without this.
mac := iface . Interface . HardwareAddr
if len ( mac ) == 6 && mac [ 0 ] == 0x00 && mac [ 1 ] == 0x15 && mac [ 2 ] == 0x5d {
2021-10-14 20:40:06 +00:00
internalBuilder . AddPrefix ( pfx )
2021-07-02 14:15:28 +00:00
return
}
}
2021-10-14 20:40:06 +00:00
externalBuilder . AddPrefix ( pfx )
2021-07-02 14:15:28 +00:00
} ) ; err != nil {
return nil , nil , err
}
2021-10-14 20:40:06 +00:00
iSet , err := internalBuilder . IPSet ( )
if err != nil {
return nil , nil , err
}
eSet , err := externalBuilder . IPSet ( )
if err != nil {
return nil , nil , err
}
2021-07-02 14:15:28 +00:00
2021-10-14 20:40:06 +00:00
return iSet . Prefixes ( ) , eSet . Prefixes ( ) , nil
2021-07-02 14:15:28 +00:00
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func interfaceRoutes ( ) ( ips * netipx . IPSet , hostIPs [ ] netip . Addr , err error ) {
2022-07-25 03:08:42 +00:00
var b netipx . IPSetBuilder
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
if err := interfaces . ForeachInterfaceAddress ( func ( _ interfaces . Interface , pfx netip . Prefix ) {
2022-07-25 03:08:42 +00:00
if tsaddr . IsTailscaleIP ( pfx . Addr ( ) ) {
2021-02-23 04:43:35 +00:00
return
}
if pfx . IsSingleIP ( ) {
return
}
2022-07-25 03:08:42 +00:00
hostIPs = append ( hostIPs , pfx . Addr ( ) )
2021-04-08 22:56:51 +00:00
b . AddPrefix ( pfx )
} ) ; err != nil {
return nil , nil , err
}
2021-06-02 16:04:37 +00:00
ipSet , _ := b . IPSet ( )
return ipSet , hostIPs , nil
2021-04-08 22:56:51 +00:00
}
// shrinkDefaultRoute returns an IPSet representing the IPs in route,
2021-12-16 19:15:45 +00:00
// minus those in removeFromDefaultRoute and localInterfaceRoutes,
// plus the IPs in hostIPs.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func shrinkDefaultRoute ( route netip . Prefix , localInterfaceRoutes * netipx . IPSet , hostIPs [ ] netip . Addr ) ( * netipx . IPSet , error ) {
2022-07-25 03:08:42 +00:00
var b netipx . IPSetBuilder
2021-04-08 22:56:51 +00:00
// Add the default route.
b . AddPrefix ( route )
// Remove the local interface routes.
2021-12-16 19:15:45 +00:00
b . RemoveSet ( localInterfaceRoutes )
2021-03-18 00:04:32 +00:00
// Having removed all the LAN subnets, re-add the hosts's own
// IPs. It's fine for clients to connect to an exit node's public
// IP address, just not the attached subnet.
//
// Truly forbidden subnets (in removeFromDefaultRoute) will still
// be stripped back out by the next step.
for _ , ip := range hostIPs {
if route . Contains ( ip ) {
b . Add ( ip )
}
}
2021-02-23 04:43:35 +00:00
for _ , pfx := range removeFromDefaultRoute {
b . RemovePrefix ( pfx )
}
2021-06-02 16:04:37 +00:00
return b . IPSet ( )
2021-02-23 04:43:35 +00:00
}
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).
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func dnsCIDRsEqual ( newAddr , oldAddr [ ] netip . Prefix ) 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-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 {
2022-10-22 05:39:59 +00:00
ports , ok := <- b . portpoll . Updates ( )
2020-03-14 03:53:58 +00:00
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 ( )
2021-03-30 18:19:42 +00:00
notifyFunc := b . notify
2021-04-09 14:57:32 +00:00
apiSrv := b . peerAPIServer
2020-02-25 20:30:28 +00:00
b . mu . Unlock ( )
2021-03-30 18:19:42 +00:00
if notifyFunc == nil {
return
}
2021-12-01 23:00:23 +00:00
if apiSrv . hasFilesWaiting ( ) {
2021-04-09 14:57:32 +00:00
n . FilesWaiting = & empty . Message { }
}
2021-03-30 18:19:42 +00:00
n . Version = version . Long
2021-04-08 21:54:25 +00:00
notifyFunc ( n )
}
func ( b * LocalBackend ) sendFileNotify ( ) {
var n ipn . Notify
b . mu . Lock ( )
notifyFunc := b . notify
apiSrv := b . peerAPIServer
if notifyFunc == nil || apiSrv == nil {
b . mu . Unlock ( )
return
}
2021-04-09 14:57:32 +00:00
// Make sure we always set n.IncomingFiles non-nil so it gets encoded
// in JSON to clients. They distinguish between empty and non-nil
// to know whether a Notify should be able about files.
n . IncomingFiles = make ( [ ] ipn . PartialFile , 0 )
2021-04-08 21:54:25 +00:00
for f := range b . incomingFiles {
n . IncomingFiles = append ( n . IncomingFiles , f . PartialFile ( ) )
}
b . mu . Unlock ( )
sort . Slice ( n . IncomingFiles , func ( i , j int ) bool {
return n . IncomingFiles [ i ] . Started . Before ( n . IncomingFiles [ j ] . Started )
} )
b . send ( 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
2021-04-21 20:37:36 +00:00
b . authURL = "" // but NOT clearing authURLSticky
2020-02-05 22:16:58 +00:00
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 ( )
2022-03-21 21:10:25 +00:00
b . tellClientToBrowseToURL ( url )
2021-02-04 21:12:42 +00:00
if b . State ( ) == ipn . Running {
b . enterState ( ipn . Starting )
2020-02-05 22:16:58 +00:00
}
}
2022-03-21 21:10:25 +00:00
func ( b * LocalBackend ) tellClientToBrowseToURL ( url string ) {
if url != "" {
b . send ( ipn . Notify { BrowseToURL : & url } )
}
}
2021-04-02 15:21:40 +00:00
// For testing lazy machine key generation.
2022-09-14 19:49:39 +00:00
var panicOnMachineKeyGeneration = envknob . RegisterBool ( "TS_DEBUG_PANIC_MACHINE_KEY" )
2021-04-02 15:21:40 +00:00
2021-09-03 20:17:46 +00:00
func ( b * LocalBackend ) createGetMachinePrivateKeyFunc ( ) func ( ) ( key . MachinePrivate , error ) {
2022-08-04 17:43:49 +00:00
var cache syncs . AtomicValue [ key . MachinePrivate ]
2021-09-03 20:17:46 +00:00
return func ( ) ( key . MachinePrivate , error ) {
2022-09-14 19:49:39 +00:00
if panicOnMachineKeyGeneration ( ) {
2021-04-02 15:21:40 +00:00
panic ( "machine key generated" )
}
2022-08-04 17:43:49 +00:00
if v , ok := cache . LoadOk ( ) ; ok {
2021-04-02 15:21:40 +00:00
return v , nil
}
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-08-04 17:43:49 +00:00
if v , ok := cache . LoadOk ( ) ; ok {
2021-04-02 15:21:40 +00:00
return v , nil
}
if err := b . initMachineKeyLocked ( ) ; err != nil {
2021-09-03 20:17:46 +00:00
return key . MachinePrivate { } , err
2021-04-02 15:21:40 +00:00
}
cache . Store ( b . machinePrivKey )
return b . machinePrivKey , nil
}
}
2022-10-13 17:43:00 +00:00
func ( b * LocalBackend ) createGetNLPrivateKeyFunc ( ) func ( ) ( key . NLPrivate , error ) {
var cache syncs . AtomicValue [ key . NLPrivate ]
return func ( ) ( key . NLPrivate , error ) {
2022-08-01 22:46:41 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-08-04 17:43:49 +00:00
if v , ok := cache . LoadOk ( ) ; ok {
2022-08-01 22:46:41 +00:00
return v , nil
}
2022-10-13 17:43:00 +00:00
priv := b . nlPrivKey
cache . Store ( priv )
return priv , nil
2022-08-01 22:46:41 +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 ) {
2020-09-28 22:28:26 +00:00
if ! b . machinePrivKey . IsZero ( ) {
// Already set.
return nil
}
2021-09-03 20:17:46 +00:00
var legacyMachineKey key . MachinePrivate
2022-11-09 05:58:10 +00:00
if p := b . pm . CurrentPrefs ( ) . Persist ( ) ; p != nil {
legacyMachineKey = p . LegacyFrontendPrivateMachineKey
2020-09-28 22:28:26 +00:00
}
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
}
2021-09-03 20:17:46 +00:00
if ! legacyMachineKey . IsZero ( ) && ! legacyMachineKey . Equal ( b . machinePrivKey ) {
2020-09-28 22:28:26 +00:00
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 ( ) {
b . machinePrivKey = legacyMachineKey
} else {
b . logf ( "generating new machine key" )
2021-09-03 20:17:46 +00:00
b . machinePrivKey = key . NewMachine ( )
2020-09-28 22:28:26 +00:00
}
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
}
2022-08-01 22:46:41 +00:00
// initNLKeyLocked is called to initialize b.nlPrivKey.
//
// b.prefs must already be initialized.
2022-10-23 17:07:10 +00:00
//
2022-08-01 22:46:41 +00:00
// b.stateKey should be set too, but just for nicer log messages.
// b.mu must be held.
func ( b * LocalBackend ) initNLKeyLocked ( ) ( err error ) {
if ! b . nlPrivKey . IsZero ( ) {
// Already set.
return nil
}
keyText , err := b . store . ReadState ( ipn . NLKeyStateKey )
if err == nil {
if err := b . nlPrivKey . UnmarshalText ( keyText ) ; err != nil {
return fmt . Errorf ( "invalid key in %s key of %v: %w" , ipn . NLKeyStateKey , b . store , err )
}
if b . nlPrivKey . IsZero ( ) {
return fmt . Errorf ( "invalid zero key stored in %v key of %v" , ipn . NLKeyStateKey , b . store )
}
return nil
}
if err != ipn . ErrStateNotExist {
return fmt . Errorf ( "error reading %v key of %v: %w" , ipn . NLKeyStateKey , b . store , err )
}
// If we didn't find one already on disk, generate a new one.
b . logf ( "generating new network-lock key" )
b . nlPrivKey = key . NewNLPrivate ( )
keyText , _ = b . nlPrivKey . MarshalText ( )
if err := b . store . WriteState ( ipn . NLKeyStateKey , keyText ) ; err != nil {
b . logf ( "error writing network-lock key to store: %v" , err )
return err
}
b . logf ( "network-lock key written to store" )
return nil
}
2022-11-09 05:58:10 +00:00
// migrateStateLocked migrates state from the frontend to the backend.
// It is a no-op if prefs is nil
// b.mu must be held.
func ( b * LocalBackend ) migrateStateLocked ( prefs * ipn . Prefs ) ( err error ) {
if prefs == nil && ! b . pm . CurrentPrefs ( ) . Valid ( ) {
return fmt . Errorf ( "no prefs provided and no current profile" )
2020-02-14 00:38:36 +00:00
}
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 ( ) )
2022-11-09 05:58:10 +00:00
if err := b . pm . SetPrefs ( prefs . View ( ) ) ; err != nil {
2020-02-03 18:35:52 +00:00
return fmt . Errorf ( "store.WriteState: %v" , err )
}
}
2022-11-09 05:58:10 +00:00
b . setAtomicValuesFromPrefs ( b . pm . CurrentPrefs ( ) )
2021-08-26 21:50:55 +00:00
2020-02-03 18:35:52 +00:00
return nil
}
2022-11-07 23:32:53 +00:00
// setTCPPortsIntercepted populates b.shouldInterceptTCPPortAtomic with an
// efficient func for ShouldInterceptTCPPort to use, which is called on every
// incoming packet.
func ( b * LocalBackend ) setTCPPortsIntercepted ( ports [ ] uint16 ) {
slices . Sort ( ports )
uniq . ModifySlice ( & ports )
var f func ( uint16 ) bool
switch len ( ports ) {
case 0 :
f = func ( uint16 ) bool { return false }
case 1 :
f = func ( p uint16 ) bool { return ports [ 0 ] == p }
case 2 :
f = func ( p uint16 ) bool { return ports [ 0 ] == p || ports [ 1 ] == p }
case 3 :
f = func ( p uint16 ) bool { return ports [ 0 ] == p || ports [ 1 ] == p || ports [ 2 ] == p }
default :
if len ( ports ) > 16 {
m := map [ uint16 ] bool { }
for _ , p := range ports {
m [ p ] = true
}
f = func ( p uint16 ) bool { return m [ p ] }
} else {
f = func ( p uint16 ) bool {
for _ , x := range ports {
if p == x {
return true
}
}
return false
}
}
}
b . shouldInterceptTCPPortAtomic . Store ( f )
}
2022-11-09 05:58:10 +00:00
// setAtomicValuesFromPrefs populates sshAtomicBool, containsViaIPFuncAtomic
// and shouldInterceptTCPPortAtomic from the prefs p, which may be !Valid().
2022-10-23 17:07:10 +00:00
func ( b * LocalBackend ) setAtomicValuesFromPrefs ( p ipn . PrefsView ) {
b . sshAtomicBool . Store ( p . Valid ( ) && p . RunSSH ( ) && envknob . CanSSHD ( ) )
2022-03-30 15:47:16 +00:00
2022-10-23 17:07:10 +00:00
if ! p . Valid ( ) {
2022-03-30 15:47:16 +00:00
b . containsViaIPFuncAtomic . Store ( tsaddr . NewContainsIPFunc ( nil ) )
2022-11-07 23:32:53 +00:00
b . setTCPPortsIntercepted ( nil )
2022-03-30 15:47:16 +00:00
} else {
2022-10-23 17:07:10 +00:00
b . containsViaIPFuncAtomic . Store ( tsaddr . NewContainsIPFunc ( p . AdvertiseRoutes ( ) . Filter ( tsaddr . IsViaPrefix ) ) )
2022-11-09 05:58:10 +00:00
b . setTCPPortsInterceptedFromNetmapAndPrefsLocked ( p )
2022-03-30 15:47:16 +00:00
}
}
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-07-13 20:13:11 +00:00
// Login implements Backend.
2022-02-18 20:55:22 +00:00
// As of 2022-02-17, this is only exists for tests.
2021-03-19 17:21:33 +00:00
func ( b * LocalBackend ) Login ( token * tailcfg . Oauth2Token ) {
2020-07-13 20:13:11 +00:00
b . mu . Lock ( )
b . assertClientLocked ( )
2021-04-08 04:12:16 +00:00
cc := b . cc
2020-07-13 20:13:11 +00:00
b . mu . Unlock ( )
2022-02-18 20:55:22 +00:00
cc . Login ( token , b . loginFlags | controlclient . LoginInteractive )
2020-07-13 20:13:11 +00:00
}
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
2021-04-08 04:12:16 +00:00
cc := b . cc
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 {
2022-02-18 20:55:22 +00:00
cc . Login ( nil , b . loginFlags | controlclient . LoginInteractive )
2020-02-05 22:16:58 +00:00
}
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) Ping ( ctx context . Context , ip netip . Addr , pingType tailcfg . PingType ) ( * ipnstate . PingResult , error ) {
2022-05-28 04:34:36 +00:00
if pingType == tailcfg . PingPeerAPI {
t0 := time . Now ( )
node , base , err := b . pingPeerAPI ( ctx , ip )
if err != nil && ctx . Err ( ) != nil {
return nil , ctx . Err ( )
}
d := time . Since ( t0 )
pr := & ipnstate . PingResult {
IP : ip . String ( ) ,
NodeIP : ip . String ( ) ,
LatencySeconds : d . Seconds ( ) ,
PeerAPIURL : base ,
}
if err != nil {
pr . Err = err . Error ( )
}
if node != nil {
pr . NodeName = node . Name
}
return pr , nil
}
2022-05-03 21:16:34 +00:00
ch := make ( chan * ipnstate . PingResult , 1 )
2022-04-22 01:49:01 +00:00
b . e . Ping ( ip , pingType , func ( pr * ipnstate . PingResult ) {
2022-05-03 21:16:34 +00:00
select {
case ch <- pr :
default :
}
2020-08-09 21:49:42 +00:00
} )
2022-05-03 21:16:34 +00:00
select {
case pr := <- ch :
return pr , nil
case <- ctx . Done ( ) :
return nil , ctx . Err ( )
}
2020-08-09 21:49:42 +00:00
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) pingPeerAPI ( ctx context . Context , ip netip . Addr ) ( peer * tailcfg . Node , peerBase string , err error ) {
2022-05-28 04:34:36 +00:00
ctx , cancel := context . WithTimeout ( ctx , 10 * time . Second )
defer cancel ( )
nm := b . NetMap ( )
if nm == nil {
return nil , "" , errors . New ( "no netmap" )
}
peer , ok := nm . PeerByTailscaleIP ( ip )
if ! ok {
return nil , "" , fmt . Errorf ( "no peer found with Tailscale IP %v" , ip )
}
base := peerAPIBase ( nm , peer )
if base == "" {
return nil , "" , fmt . Errorf ( "no peer API base found for peer %v (%v)" , peer . ID , ip )
}
outReq , err := http . NewRequestWithContext ( ctx , "HEAD" , base , nil )
if err != nil {
return nil , "" , err
}
tr := b . Dialer ( ) . PeerAPITransport ( )
res , err := tr . RoundTrip ( outReq )
if err != nil {
return nil , "" , err
}
defer res . Body . Close ( ) // but unnecessary on HEAD responses
if res . StatusCode != http . StatusOK {
return nil , "" , fmt . Errorf ( "HTTP status %v" , res . Status )
}
return peer , base , nil
}
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-11-02 01:40:39 +00:00
ret . LivePeers = map [ key . NodePublic ] 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 ++
2021-11-02 01:40:39 +00:00
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 ( )
2022-11-09 05:58:10 +00:00
p := b . pm . CurrentPrefs ( )
if ! p . Valid ( ) || b . netMap == nil {
2021-01-11 22:24:32 +00:00
return false // default to safest setting
2020-05-19 02:32:20 +00:00
}
2022-11-09 05:58:10 +00:00
return ! p . 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 ( )
2022-11-09 05:58:10 +00:00
b . pm . SetCurrentUser ( uid )
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 . Unlock ( )
}
2022-04-18 16:37:23 +00:00
func ( b * LocalBackend ) CheckPrefs ( p * ipn . Prefs ) error {
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . checkPrefsLocked ( p )
}
func ( b * LocalBackend ) checkPrefsLocked ( p * ipn . Prefs ) error {
2022-06-17 19:09:23 +00:00
var errs [ ] error
2022-04-18 16:37:23 +00:00
if p . Hostname == "badhostname.tailscale." {
// Keep this one just for testing.
2022-06-17 19:09:23 +00:00
errs = append ( errs , errors . New ( "bad hostname [test]" ) )
2022-04-18 16:37:23 +00:00
}
2022-06-17 19:09:23 +00:00
if err := b . checkSSHPrefsLocked ( p ) ; err != nil {
errs = append ( errs , err )
}
return multierr . New ( errs ... )
}
func ( b * LocalBackend ) checkSSHPrefsLocked ( p * ipn . Prefs ) error {
if ! p . RunSSH {
return nil
}
switch runtime . GOOS {
case "linux" :
if distro . Get ( ) == distro . Synology && ! envknob . UseWIPCode ( ) {
return errors . New ( "The Tailscale SSH server does not run on Synology." )
2022-04-18 16:37:23 +00:00
}
2022-07-24 01:38:42 +00:00
if distro . Get ( ) == distro . QNAP && ! envknob . UseWIPCode ( ) {
return errors . New ( "The Tailscale SSH server does not run on QNAP." )
}
2022-06-17 19:09:23 +00:00
// otherwise okay
case "darwin" :
// okay only in tailscaled mode for now.
if version . IsSandboxedMacOS ( ) {
return errors . New ( "The Tailscale SSH server does not run in sandboxed Tailscale GUI builds." )
2022-04-18 16:37:23 +00:00
}
2022-06-17 19:09:23 +00:00
if ! envknob . UseWIPCode ( ) {
return errors . New ( "The Tailscale SSH server is disabled on macOS tailscaled by default. To try, set env TAILSCALE_USE_WIP_CODE=1" )
}
2022-11-01 00:49:37 +00:00
case "freebsd" :
2022-06-17 19:09:23 +00:00
default :
return errors . New ( "The Tailscale SSH server is not supported on " + runtime . GOOS )
}
2022-09-14 19:49:39 +00:00
if ! envknob . CanSSHD ( ) {
2022-06-17 19:09:23 +00:00
return errors . New ( "The Tailscale SSH server has been administratively disabled." )
}
if envknob . SSHIgnoreTailnetPolicy ( ) || envknob . SSHPolicyFile ( ) != "" {
return nil
}
if b . netMap != nil {
if ! hasCapability ( b . netMap , tailcfg . CapabilitySSH ) {
if b . isDefaultServerLocked ( ) {
return errors . New ( "Unable to enable local Tailscale SSH server; not enabled on Tailnet. See https://tailscale.com/s/ssh" )
}
return errors . New ( "Unable to enable local Tailscale SSH server; not enabled on Tailnet." )
2022-04-20 18:22:54 +00:00
}
2022-04-18 16:37:23 +00:00
}
return nil
}
2022-06-17 19:09:23 +00:00
func ( b * LocalBackend ) sshOnButUnusableHealthCheckMessageLocked ( ) ( healthMessage string ) {
2022-11-09 05:58:10 +00:00
if p := b . pm . CurrentPrefs ( ) ; ! p . Valid ( ) || ! p . RunSSH ( ) {
2022-06-17 19:09:23 +00:00
return ""
}
if envknob . SSHIgnoreTailnetPolicy ( ) || envknob . SSHPolicyFile ( ) != "" {
return "development SSH policy in use"
}
nm := b . netMap
if nm == nil {
return ""
}
if nm . SSHPolicy != nil && len ( nm . SSHPolicy . Rules ) > 0 {
return ""
}
isDefault := b . isDefaultServerLocked ( )
isAdmin := hasCapability ( nm , tailcfg . CapabilityAdmin )
if ! isAdmin {
2022-11-11 17:43:49 +00:00
return healthmsg . TailscaleSSHOnBut + "access controls don't allow anyone to access this device. Ask your admin to update your tailnet's ACLs to allow access."
2022-06-17 19:09:23 +00:00
}
if ! isDefault {
2022-11-11 17:43:49 +00:00
return healthmsg . TailscaleSSHOnBut + "access controls don't allow anyone to access this device. Update your tailnet's ACLs to allow access."
2022-06-17 19:09:23 +00:00
}
2022-11-11 17:43:49 +00:00
return healthmsg . TailscaleSSHOnBut + "access controls don't allow anyone to access this device. Update your tailnet's ACLs at https://tailscale.com/s/ssh-policy"
2022-06-17 19:09:23 +00:00
}
func ( b * LocalBackend ) isDefaultServerLocked ( ) bool {
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
if ! prefs . Valid ( ) {
2022-06-17 19:09:23 +00:00
return true // assume true until set otherwise
}
2022-11-09 05:58:10 +00:00
return prefs . ControlURLOrDefault ( ) == ipn . DefaultControlURL
2022-06-17 19:09:23 +00:00
}
2022-10-23 17:07:10 +00:00
func ( b * LocalBackend ) EditPrefs ( mp * ipn . MaskedPrefs ) ( ipn . PrefsView , error ) {
2020-08-11 02:42:04 +00:00
b . mu . Lock ( )
2022-09-01 16:27:06 +00:00
if mp . EggSet {
mp . EggSet = false
b . egg = true
go b . doSetHostinfoFilterServices ( b . hostinfo . Clone ( ) )
}
2022-11-09 05:58:10 +00:00
p0 := b . pm . CurrentPrefs ( )
p1 := b . pm . CurrentPrefs ( ) . AsStruct ( )
2021-04-01 04:35:21 +00:00
p1 . ApplyEdits ( mp )
2022-04-18 16:37:23 +00:00
if err := b . checkPrefsLocked ( p1 ) ; err != nil {
b . mu . Unlock ( )
b . logf ( "EditPrefs check error: %v" , err )
2022-10-23 17:07:10 +00:00
return ipn . PrefsView { } , err
2022-04-18 16:37:23 +00:00
}
2022-09-14 19:49:39 +00:00
if p1 . RunSSH && ! envknob . CanSSHD ( ) {
2022-03-23 20:52:29 +00:00
b . mu . Unlock ( )
b . logf ( "EditPrefs requests SSH, but disabled by envknob; returning error" )
2022-10-23 17:07:10 +00:00
return ipn . PrefsView { } , errors . New ( "Tailscale SSH server administratively disabled." )
2022-03-23 20:52:29 +00:00
}
2022-10-23 17:07:10 +00:00
if p1 . View ( ) . Equals ( p0 ) {
2021-04-01 04:35:21 +00:00
b . mu . Unlock ( )
2022-10-24 00:15:04 +00:00
return stripKeysFromPrefs ( p0 ) , nil
2020-08-11 02:42:04 +00:00
}
2021-04-01 04:35:21 +00:00
b . logf ( "EditPrefs: %v" , mp . Pretty ( ) )
2022-10-24 00:15:04 +00:00
newPrefs := b . setPrefsLockedOnEntry ( "EditPrefs" , p1 ) // does a b.mu.Unlock
2021-04-11 04:56:18 +00:00
2021-04-30 08:29:22 +00:00
// Note: don't perform any actions for the new prefs here. Not
// every prefs change goes through EditPrefs. Put your actions
// in setPrefsLocksOnEntry instead.
2022-10-24 00:15:04 +00:00
// This should return the public prefs, not the private ones.
return stripKeysFromPrefs ( newPrefs ) , nil
2020-08-11 02:42:04 +00:00
}
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 ( )
2021-04-01 04:35:21 +00:00
b . setPrefsLockedOnEntry ( "SetPrefs" , newp )
}
2020-07-29 01:47:23 +00:00
2021-04-01 04:35:21 +00:00
// setPrefsLockedOnEntry requires b.mu be held to call it, but it
2021-12-01 23:33:14 +00:00
// unlocks b.mu when done. newp ownership passes to this function.
2022-10-24 00:15:04 +00:00
// It returns a readonly copy of the new prefs.
func ( b * LocalBackend ) setPrefsLockedOnEntry ( caller string , newp * ipn . Prefs ) ipn . PrefsView {
2020-07-29 01:47:23 +00:00
netMap := b . netMap
2022-11-09 05:58:10 +00:00
b . setAtomicValuesFromPrefs ( newp . View ( ) )
2022-10-23 17:07:10 +00:00
2022-11-09 05:58:10 +00:00
oldp := b . pm . CurrentPrefs ( )
if oldp . Valid ( ) {
newp . Persist = oldp . Persist ( ) . Clone ( ) // caller isn't allowed to override this
}
2021-12-01 23:33:14 +00:00
// findExitNodeIDLocked returns whether it updated b.prefs, but
// everything in this function treats b.prefs as completely new
// anyway. No-op if no exit node resolution is needed.
2022-10-23 17:07:10 +00:00
findExitNodeIDLocked ( newp , netMap )
2020-07-29 01:47:23 +00:00
// We do this to avoid holding the lock while doing everything else.
2022-11-09 05:58:10 +00:00
b . inServerMode = newp . ForceDaemon
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 ( )
2022-11-09 05:58:10 +00:00
b . applyPrefsToHostinfo ( newHi , newp . View ( ) )
2020-06-15 23:04:12 +00:00
b . hostinfo = newHi
2020-07-08 23:49:02 +00:00
hostInfoChanged := ! oldHi . Equal ( newHi )
2021-04-30 08:29:22 +00:00
cc := b . cc
2020-07-29 01:47:23 +00:00
2022-04-17 21:49:16 +00:00
// [GRINDER STATS LINE] - please don't remove (used for log parsing)
if caller == "SetPrefs" {
2022-11-09 05:58:10 +00:00
b . logf ( "SetPrefs: %v" , newp . Pretty ( ) )
2022-04-17 21:49:16 +00:00
}
2022-11-09 05:58:10 +00:00
b . updateFilterLocked ( netMap , newp . View ( ) )
2022-04-17 21:49:16 +00:00
2022-11-09 05:58:10 +00:00
if oldp . ShouldSSHBeRunning ( ) && ! newp . ShouldSSHBeRunning ( ) {
2022-05-30 09:06:46 +00:00
if b . sshServer != nil {
go b . sshServer . Shutdown ( )
b . sshServer = nil
}
}
2020-10-27 19:51:48 +00:00
if netMap != nil {
2022-11-09 05:58:10 +00:00
up := netMap . UserProfiles [ netMap . User ]
if login := up . LoginName ; login != "" {
if newp . Persist == nil {
2020-10-27 19:51:48 +00:00
b . logf ( "active login: %s" , login )
2022-11-09 05:58:10 +00:00
} else {
if newp . Persist . LoginName != login {
b . logf ( "active login: %q (changed from %q)" , login , newp . Persist . LoginName )
newp . Persist . LoginName = login
}
newp . Persist . UserProfile = up
2020-10-27 19:51:48 +00:00
}
}
}
2020-04-10 15:42:34 +00:00
2022-11-09 05:58:10 +00:00
prefs := newp . View ( )
if err := b . pm . SetPrefs ( prefs ) ; err != nil {
b . logf ( "failed to save new controlclient state: %v" , err )
}
b . lastProfileID = b . pm . CurrentProfile ( ) . ID
b . mu . Unlock ( )
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-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
2022-11-09 05:58:10 +00:00
if ! oldp . WantRunning ( ) && newp . WantRunning {
2021-04-30 08:29:22 +00:00
b . logf ( "transitioning to running; doing Login..." )
cc . Login ( nil , controlclient . LoginDefault )
}
2022-11-09 05:58:10 +00:00
if oldp . WantRunning ( ) != newp . WantRunning {
2020-02-05 22:16:58 +00:00
b . stateMachine ( )
} else {
b . authReconfig ( )
}
2022-11-03 22:46:17 +00:00
b . send ( ipn . Notify { Prefs : & prefs } )
2022-10-24 00:15:04 +00:00
return prefs
2020-02-05 22:16:58 +00:00
}
2022-01-31 17:20:22 +00:00
// GetPeerAPIPort returns the port number for the peerapi server
// running on the provided IP.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) GetPeerAPIPort ( ip netip . Addr ) ( port uint16 , ok bool ) {
2021-03-29 22:17:05 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
for _ , pln := range b . peerAPIListeners {
2021-04-07 18:32:53 +00:00
if pln . ip == ip {
2021-04-02 05:04:46 +00:00
return uint16 ( pln . port ) , true
2021-03-29 22:17:05 +00:00
}
}
return 0 , false
}
2022-01-31 17:20:22 +00:00
// ServePeerAPIConnection serves an already-accepted connection c.
//
// The remote parameter is the remote address.
2022-09-25 18:29:55 +00:00
// The local parameter is the local address (either a Tailscale IPv4
2022-01-31 17:20:22 +00:00
// or IPv6 IP and the peerapi port for that address).
//
// The connection will be closed by ServePeerAPIConnection.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) ServePeerAPIConnection ( remote , local netip . AddrPort , c net . Conn ) {
2022-01-31 17:20:22 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
for _ , pln := range b . peerAPIListeners {
2022-07-25 03:08:42 +00:00
if pln . ip == local . Addr ( ) {
2022-01-31 17:20:22 +00:00
go pln . ServeConn ( remote , c )
return
}
}
b . logf ( "[unexpected] no peerAPI listener found for %v" , local )
c . Close ( )
return
}
2021-03-29 19:49:13 +00:00
func ( b * LocalBackend ) peerAPIServicesLocked ( ) ( ret [ ] tailcfg . Service ) {
for _ , pln := range b . peerAPIListeners {
2021-11-30 01:19:17 +00:00
proto := tailcfg . PeerAPI4
2021-03-29 19:49:13 +00:00
if pln . ip . Is6 ( ) {
2021-11-30 01:19:17 +00:00
proto = tailcfg . PeerAPI6
2021-03-29 19:49:13 +00:00
}
ret = append ( ret , tailcfg . Service {
Proto : proto ,
2021-04-02 05:04:46 +00:00
Port : uint16 ( pln . port ) ,
2021-03-29 19:49:13 +00:00
} )
}
2021-11-30 01:19:17 +00:00
switch runtime . GOOS {
2021-12-09 20:01:19 +00:00
case "linux" , "freebsd" , "openbsd" , "illumos" , "darwin" , "windows" :
2021-11-30 01:19:17 +00:00
// These are the platforms currently supported by
// net/dns/resolver/tsdns.go:Resolver.HandleExitNodeDNSQuery.
ret = append ( ret , tailcfg . Service {
Proto : tailcfg . PeerAPIDNS ,
Port : 1 , // version
} )
}
2021-03-29 19:49:13 +00:00
return ret
}
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 ) {
2021-04-09 19:10:52 +00:00
if hi == nil {
b . logf ( "[unexpected] doSetHostinfoFilterServices with nil hostinfo" )
return
}
2020-04-29 09:23:29 +00:00
b . mu . Lock ( )
2021-04-08 04:12:16 +00:00
cc := b . cc
2021-03-29 19:49:13 +00:00
if cc == nil {
// Control client isn't up yet.
b . mu . Unlock ( )
return
}
peerAPIServices := b . peerAPIServicesLocked ( )
2022-09-01 16:27:06 +00:00
if b . egg {
2022-10-18 04:28:32 +00:00
peerAPIServices = append ( peerAPIServices , tailcfg . Service { Proto : "egg" , Port : 1 } )
2022-09-01 16:27:06 +00:00
}
2020-04-29 09:23:29 +00:00
b . mu . Unlock ( )
2021-03-29 19:49:13 +00:00
// Make a shallow copy of hostinfo so we can mutate
// at the Service field.
hi2 := * hi // shallow copy
if ! b . shouldUploadServices ( ) {
hi2 . Services = [ ] tailcfg . Service { }
2020-04-29 09:23:29 +00:00
}
2021-03-29 19:49:13 +00:00
// Don't mutate hi.Service's underlying array. Append to
// the slice with no free capacity.
c := len ( hi2 . Services )
hi2 . Services = append ( hi2 . Services [ : c : c ] , peerAPIServices ... )
cc . SetHostinfo ( & hi2 )
2020-04-29 09:23:29 +00:00
}
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
}
2021-11-04 19:19:00 +00:00
func ( b * LocalBackend ) isEngineBlocked ( ) bool {
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . blocked
}
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
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
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 {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] authReconfig: blocked, skipping." )
2020-02-05 22:16:58 +00:00
return
}
if nm == nil {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] authReconfig: netmap not yet valid. Skipping." )
2020-02-05 22:16:58 +00:00
return
}
2022-10-23 17:07:10 +00:00
if ! prefs . WantRunning ( ) {
2022-02-12 16:05:24 +00:00
b . logf ( "[v1] 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
2022-10-23 17:07:10 +00:00
if prefs . RouteAll ( ) {
2021-02-05 23:44:46 +00:00
flags |= netmap . AllowSubnetRoutes
2020-02-05 22:16:58 +00:00
}
2022-10-23 17:07:10 +00:00
if prefs . 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-12-03 16:33:05 +00:00
// Keep the dialer updated about whether we're supposed to use
// an exit node's DNS server (so SOCKS5/HTTP outgoing dials
// can use it for name resolution)
2022-10-23 17:07:10 +00:00
if dohURL , ok := exitNodeCanProxyDNS ( nm , prefs . ExitNodeID ( ) ) ; ok {
2021-12-03 16:33:05 +00:00
b . dialer . SetExitDNSDoH ( dohURL )
} else {
b . dialer . SetExitDNSDoH ( "" )
}
2022-10-23 17:07:10 +00:00
cfg , err := nmcfg . WGCfg ( nm , b . logf , flags , prefs . 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
2022-06-29 20:25:45 +00:00
oneCGNATRoute := shouldUseOneCGNATRoute ( nm , b . logf , version . OS ( ) )
2022-04-14 21:40:17 +00:00
rcfg := b . routerConfig ( cfg , prefs , oneCGNATRoute )
2022-06-30 02:32:41 +00:00
dcfg := dnsConfigForNetmap ( nm , prefs , b . logf , version . OS ( ) )
2021-09-07 21:38:26 +00:00
err = b . e . Reconfig ( cfg , rcfg , dcfg , nm . Debug )
if err == wgengine . ErrNoChanges {
return
}
2022-10-28 22:21:24 +00:00
b . logf ( "[v1] authReconfig: ra=%v dns=%v 0x%02x: %v" , prefs . RouteAll ( ) , prefs . CorpDNS ( ) , flags , err )
2021-09-07 21:38:26 +00:00
b . initPeerAPIListener ( )
}
2020-07-31 20:27:09 +00:00
2022-06-29 20:25:45 +00:00
// shouldUseOneCGNATRoute reports whether we should prefer to make one big
// CGNAT /10 route rather than a /32 per peer.
//
// The versionOS is a Tailscale-style version ("iOS", "macOS") and not
// a runtime.GOOS.
func shouldUseOneCGNATRoute ( nm * netmap . NetworkMap , logf logger . Logf , versionOS string ) bool {
// Explicit enabling or disabling always take precedence.
if nm . Debug != nil {
if v , ok := nm . Debug . OneCGNATRoute . Get ( ) ; ok {
logf ( "[v1] shouldUseOneCGNATRoute: explicit=%v" , v )
return v
}
}
// Also prefer to do this on the Mac, so that we don't need to constantly
// update the network extension configuration (which is disruptive to
// Chrome, see https://github.com/tailscale/tailscale/issues/3102). Only
// use fine-grained routes if another interfaces is also using the CGNAT
// IP range.
if versionOS == "macOS" {
hasCGNATInterface , err := interfaces . HasCGNATInterface ( )
if err != nil {
logf ( "shouldUseOneCGNATRoute: Could not determine if any interfaces use CGNAT: %v" , err )
return false
}
logf ( "[v1] shouldUseOneCGNATRoute: macOS automatic=%v" , ! hasCGNATInterface )
if ! hasCGNATInterface {
return true
}
}
return false
}
2021-09-08 00:58:04 +00:00
// dnsConfigForNetmap returns a *dns.Config for the given netmap,
ipn/ipnlocal, net/dns*, util/cloudenv: specialize DNS config on Google Cloud
This does three things:
* If you're on GCP, it adds a *.internal DNS split route to the
metadata server, so we never break GCP DNS names. This lets people
have some Tailscale nodes on GCP and some not (e.g. laptops at home)
without having to add a Tailnet-wide *.internal DNS route.
If you already have such a route, though, it won't overwrite it.
* If the 100.100.100.100 DNS forwarder has nowhere to forward to,
it forwards it to the GCP metadata IP, which forwards to 8.8.8.8.
This means there are never errNoUpstreams ("upstream nameservers not set")
errors on GCP due to e.g. mangled /etc/resolv.conf (GCP default VMs
don't have systemd-resolved, so it's likely a DNS supremacy fight)
* makes the DNS fallback mechanism use the GCP metadata IP as a
fallback before our hosted HTTP-based fallbacks
I created a default GCP VM from their web wizard. It has no
systemd-resolved.
I then made its /etc/resolv.conf be empty and deleted its GCP
hostnames in /etc/hosts.
I then logged in to a tailnet with no global DNS settings.
With this, tailscaled writes /etc/resolv.conf (direct mode, as no
systemd-resolved) and sets it to 100.100.100.100, which then has
regular DNS via the metadata IP and *.internal DNS via the metadata IP
as well. If the tailnet configures explicit DNS servers, those are used
instead, except for *.internal.
This also adds a new util/cloudenv package based on version/distro
where the cloud type is only detected once. We'll likely expand it in
the future for other clouds, doing variants of this change for other
popular cloud environments.
Fixes #4911
RELNOTES=Google Cloud DNS improvements
Change-Id: I19f3c2075983669b2b2c0f29a548da8de373c7cf
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-06-29 20:19:34 +00:00
// prefs, client OS version, and cloud hosting environment.
2021-09-08 00:58:04 +00:00
//
// The versionOS is a Tailscale-style version ("iOS", "macOS") and not
// a runtime.GOOS.
2022-10-23 17:07:10 +00:00
func dnsConfigForNetmap ( nm * netmap . NetworkMap , prefs ipn . PrefsView , logf logger . Logf , versionOS string ) * dns . Config {
2021-09-07 21:38:26 +00:00
dcfg := & dns . Config {
2022-05-03 21:41:58 +00:00
Routes : map [ dnsname . FQDN ] [ ] * dnstype . Resolver { } ,
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
Hosts : map [ dnsname . FQDN ] [ ] netip . Addr { } ,
2021-05-21 03:22:21 +00:00
}
2021-04-02 07:34:32 +00:00
2021-10-04 14:54:16 +00:00
// selfV6Only is whether we only have IPv6 addresses ourselves.
selfV6Only := tsaddr . PrefixesContainsFunc ( nm . Addresses , tsaddr . PrefixIs6 ) &&
! tsaddr . PrefixesContainsFunc ( nm . Addresses , tsaddr . PrefixIs4 )
2022-01-04 21:33:08 +00:00
dcfg . OnlyIPv6 = selfV6Only
2021-10-04 14:54:16 +00:00
2021-05-17 22:18:25 +00:00
// Populate MagicDNS records. We do this unconditionally so that
// quad-100 can always respond to MagicDNS queries, even if the OS
// isn't configured to make MagicDNS resolution truly
// magic. Details in
// https://github.com/tailscale/tailscale/issues/1886.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
set := func ( name string , addrs [ ] netip . Prefix ) {
2021-05-17 22:18:25 +00:00
if len ( addrs ) == 0 || name == "" {
return
}
fqdn , err := dnsname . ToFQDN ( name )
if err != nil {
return // TODO: propagate error?
}
2021-10-04 14:54:16 +00:00
have4 := tsaddr . PrefixesContainsFunc ( addrs , tsaddr . PrefixIs4 )
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var ips [ ] netip . Addr
2021-05-17 22:18:25 +00:00
for _ , addr := range addrs {
2021-10-04 14:54:16 +00:00
if selfV6Only {
2022-07-25 03:08:42 +00:00
if addr . Addr ( ) . Is6 ( ) {
ips = append ( ips , addr . Addr ( ) )
2021-10-04 14:54:16 +00:00
}
continue
}
// If this node has an IPv4 address, then
// remove peers' IPv6 addresses for now, as we
// don't guarantee that the peer node actually
// can speak IPv6 correctly.
2021-05-17 22:18:25 +00:00
//
// https://github.com/tailscale/tailscale/issues/1152
// tracks adding the right capability reporting to
// enable AAAA in MagicDNS.
2022-07-25 03:08:42 +00:00
if addr . Addr ( ) . Is6 ( ) && have4 {
2021-05-17 22:18:25 +00:00
continue
}
2022-07-25 03:08:42 +00:00
ips = append ( ips , addr . Addr ( ) )
2021-05-17 22:18:25 +00:00
}
dcfg . Hosts [ fqdn ] = ips
}
set ( nm . Name , nm . Addresses )
for _ , peer := range nm . Peers {
set ( peer . Name , peer . Addresses )
}
2021-06-16 17:58:25 +00:00
for _ , rec := range nm . DNS . ExtraRecords {
switch rec . Type {
case "" , "A" , "AAAA" :
// Treat these all the same for now: infer from the value
default :
// TODO: more
continue
}
2022-07-26 03:55:44 +00:00
ip , err := netip . ParseAddr ( rec . Value )
2021-06-16 17:58:25 +00:00
if err != nil {
// Ignore.
continue
}
fqdn , err := dnsname . ToFQDN ( rec . Name )
if err != nil {
continue
}
dcfg . Hosts [ fqdn ] = append ( dcfg . Hosts [ fqdn ] , ip )
}
2021-05-17 22:18:25 +00:00
2022-10-23 17:07:10 +00:00
if ! prefs . CorpDNS ( ) {
2021-09-07 21:56:45 +00:00
return dcfg
}
2021-11-30 23:19:18 +00:00
for _ , dom := range nm . DNS . Domains {
fqdn , err := dnsname . ToFQDN ( dom )
if err != nil {
logf ( "[unexpected] non-FQDN search domain %q" , dom )
}
dcfg . SearchDomains = append ( dcfg . SearchDomains , fqdn )
}
if nm . DNS . Proxied { // actually means "enable MagicDNS"
for _ , dom := range magicDNSRootDomains ( nm ) {
dcfg . Routes [ dom ] = nil // resolve internally with dcfg.Hosts
}
}
2022-05-03 21:41:58 +00:00
addDefault := func ( resolvers [ ] * dnstype . Resolver ) {
2021-09-07 21:56:45 +00:00
for _ , r := range resolvers {
2022-04-19 04:58:00 +00:00
dcfg . DefaultResolvers = append ( dcfg . DefaultResolvers , r )
2021-04-08 08:35:14 +00:00
}
2021-09-07 21:56:45 +00:00
}
2021-04-20 04:58:26 +00:00
2021-11-30 23:19:18 +00:00
// If we're using an exit node and that exit node is new enough (1.19.x+)
// to run a DoH DNS proxy, then send all our DNS traffic through it.
2022-10-23 17:07:10 +00:00
if dohURL , ok := exitNodeCanProxyDNS ( nm , prefs . ExitNodeID ( ) ) ; ok {
2022-05-03 21:41:58 +00:00
addDefault ( [ ] * dnstype . Resolver { { Addr : dohURL } } )
2021-11-30 23:19:18 +00:00
return dcfg
}
2021-09-07 21:56:45 +00:00
addDefault ( nm . DNS . Resolvers )
for suffix , resolvers := range nm . DNS . Routes {
fqdn , err := dnsname . ToFQDN ( suffix )
if err != nil {
logf ( "[unexpected] non-FQDN route suffix %q" , suffix )
}
2021-08-25 16:07:21 +00:00
2021-09-07 21:56:45 +00:00
// Create map entry even if len(resolvers) == 0; Issue 2706.
// This lets the control plane send ExtraRecords for which we
// can authoritatively answer "name not exists" for when the
// control plane also sends this explicit but empty route
// making it as something we handle.
//
// While we're already populating it, might as well size the
// slice appropriately.
2022-05-03 21:41:58 +00:00
dcfg . Routes [ fqdn ] = make ( [ ] * dnstype . Resolver , 0 , len ( resolvers ) )
2021-08-25 16:07:21 +00:00
2021-09-07 21:56:45 +00:00
for _ , r := range resolvers {
2022-04-19 04:58:00 +00:00
dcfg . Routes [ fqdn ] = append ( dcfg . Routes [ fqdn ] , r )
2021-04-09 22:24:47 +00:00
}
2021-09-07 21:56:45 +00:00
}
2021-04-20 04:58:26 +00:00
2021-09-07 21:56:45 +00:00
// Set FallbackResolvers as the default resolvers in the
// scenarios that can't handle a purely split-DNS config. See
// https://github.com/tailscale/tailscale/issues/1743 for
// details.
switch {
case len ( dcfg . DefaultResolvers ) != 0 :
// Default resolvers already set.
2022-10-23 17:07:10 +00:00
case ! prefs . ExitNodeID ( ) . IsZero ( ) :
2021-09-07 21:56:45 +00:00
// When using exit nodes, it's very likely the LAN
// resolvers will become unreachable. So, force use of the
// fallback resolvers until we implement DNS forwarding to
// exit nodes.
//
// This is especially important on Apple OSes, where
// adding the default route to the tunnel interface makes
// it "primary", and we MUST provide VPN-sourced DNS
// settings or we break all DNS resolution.
//
// https://github.com/tailscale/tailscale/issues/1713
addDefault ( nm . DNS . FallbackResolvers )
case len ( dcfg . Routes ) == 0 :
// No settings requiring split DNS, no problem.
2021-04-01 08:33:58 +00:00
}
2021-09-07 21:56:45 +00:00
2021-09-07 21:38:26 +00:00
return dcfg
2021-03-25 22:38:40 +00:00
}
2022-08-01 22:46:41 +00:00
// SetTailnetKeyAuthority sets the key authority which should be
// used for locked tailnets.
//
// It should only be called before the LocalBackend is used.
2022-08-26 16:45:16 +00:00
func ( b * LocalBackend ) SetTailnetKeyAuthority ( a * tka . Authority , storage * tka . FS ) {
b . tka = & tkaState {
authority : a ,
storage : storage ,
}
2022-08-01 22:46:41 +00:00
}
2021-11-03 16:03:11 +00:00
// SetVarRoot sets the root directory of Tailscale's writable
// storage area . (e.g. "/var/lib/tailscale")
//
// It should only be called before the LocalBackend is used.
func ( b * LocalBackend ) SetVarRoot ( dir string ) {
b . varRoot = dir
}
2021-09-27 20:31:40 +00:00
// TailscaleVarRoot returns the root directory of Tailscale's writable
2021-03-31 21:08:32 +00:00
// storage area. (e.g. "/var/lib/tailscale")
2021-09-27 20:31:40 +00:00
//
// It returns an empty string if there's no configured or discovered
// location.
func ( b * LocalBackend ) TailscaleVarRoot ( ) string {
2021-11-03 16:03:11 +00:00
if b . varRoot != "" {
return b . varRoot
}
2021-05-20 12:33:49 +00:00
switch runtime . GOOS {
2022-01-06 15:43:24 +00:00
case "ios" , "android" , "darwin" :
2022-08-04 17:43:49 +00:00
return paths . AppSharedDir . Load ( )
2021-03-31 21:08:32 +00:00
}
2021-11-03 16:03:11 +00:00
return ""
2021-03-31 21:08:32 +00:00
}
2021-04-12 21:05:44 +00:00
func ( b * LocalBackend ) fileRootLocked ( uid tailcfg . UserID ) string {
if v := b . directFileRoot ; v != "" {
return v
}
2021-09-27 20:31:40 +00:00
varRoot := b . TailscaleVarRoot ( )
2021-04-12 21:05:44 +00:00
if varRoot == "" {
2021-12-01 23:00:23 +00:00
b . logf ( "Taildrop disabled; no state directory" )
2021-04-12 21:05:44 +00:00
return ""
}
baseDir := fmt . Sprintf ( "%s-uid-%d" ,
strings . ReplaceAll ( b . activeLogin , "@" , "-" ) ,
uid )
dir := filepath . Join ( varRoot , "files" , baseDir )
if err := os . MkdirAll ( dir , 0700 ) ; err != nil {
2021-12-01 23:00:23 +00:00
b . logf ( "Taildrop disabled; error making directory: %v" , err )
2021-04-12 21:05:44 +00:00
return ""
}
return dir
}
2021-04-23 18:45:48 +00:00
// closePeerAPIListenersLocked closes any existing peer API listeners
// and clears out the peer API server state.
//
// It does not kick off any Hostinfo update with new services.
//
// b.mu must be held.
func ( b * LocalBackend ) closePeerAPIListenersLocked ( ) {
b . peerAPIServer = nil
for _ , pln := range b . peerAPIListeners {
pln . Close ( )
}
b . peerAPIListeners = nil
}
2021-06-14 15:59:09 +00:00
// peerAPIListenAsync is whether the operating system requires that we
// retry listening on the peerAPI ip/port for whatever reason.
//
// On Windows, see Issue 1620.
// On Android, see Issue 1960.
const peerAPIListenAsync = runtime . GOOS == "windows" || runtime . GOOS == "android"
2021-03-25 22:38:40 +00:00
func ( b * LocalBackend ) initPeerAPIListener ( ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-04-25 18:02:59 +00:00
if b . shutdownCalled {
return
}
2021-03-25 22:38:40 +00:00
2021-06-03 16:39:30 +00:00
if b . netMap == nil {
// We're called from authReconfig which checks that
// netMap is non-nil, but if a concurrent Logout,
// ResetForClientDisconnect, or Start happens when its
// mutex was released, the netMap could be
// nil'ed out (Issue 1996). Bail out early here if so.
return
}
2021-04-09 18:13:42 +00:00
if len ( b . netMap . Addresses ) == len ( b . peerAPIListeners ) {
allSame := true
for i , pln := range b . peerAPIListeners {
2022-07-25 03:08:42 +00:00
if pln . ip != b . netMap . Addresses [ i ] . Addr ( ) {
2021-04-09 18:13:42 +00:00
allSame = false
break
}
}
if allSame {
// Nothing to do.
return
}
}
2021-04-23 18:45:48 +00:00
b . closePeerAPIListenersLocked ( )
2021-03-25 22:38:40 +00:00
2021-03-29 17:42:33 +00:00
selfNode := b . netMap . SelfNode
if len ( b . netMap . Addresses ) == 0 || selfNode == nil {
return
}
2021-04-12 21:05:44 +00:00
fileRoot := b . fileRootLocked ( selfNode . User )
if fileRoot == "" {
2021-12-01 23:00:23 +00:00
b . logf ( "peerapi starting without Taildrop directory configured" )
2021-03-27 04:24:02 +00:00
}
2021-03-29 17:42:33 +00:00
ps := & peerAPIServer {
2021-12-06 20:24:25 +00:00
b : b ,
rootDir : fileRoot ,
selfNode : selfNode ,
directFileMode : b . directFileRoot != "" ,
directFileDoFinalRename : b . directFileDoFinalRename ,
2021-03-29 17:42:33 +00:00
}
2021-11-23 05:45:34 +00:00
if re , ok := b . e . ( wgengine . ResolvingEngine ) ; ok {
if r , ok := re . GetResolver ( ) ; ok {
ps . resolver = r
}
}
2021-03-30 18:19:42 +00:00
b . peerAPIServer = ps
2021-03-29 17:42:33 +00:00
2021-03-30 16:54:52 +00:00
isNetstack := wgengine . IsNetstack ( b . e )
for i , a := range b . netMap . Addresses {
var ln net . Listener
var err error
skipListen := i > 0 && isNetstack
if ! skipListen {
2022-07-25 03:08:42 +00:00
ln , err = ps . listen ( a . Addr ( ) , b . prevIfState )
2021-03-30 16:54:52 +00:00
if err != nil {
2021-06-14 15:59:09 +00:00
if peerAPIListenAsync {
// Expected. But we fix it later in linkChange
2021-04-27 16:58:05 +00:00
// ("peerAPIListeners too low").
continue
}
2022-07-25 03:08:42 +00:00
b . logf ( "[unexpected] peerapi listen(%q) error: %v" , a . Addr ( ) , err )
2021-03-30 16:54:52 +00:00
continue
}
2021-03-25 22:38:40 +00:00
}
pln := & peerAPIListener {
2021-03-29 17:42:33 +00:00
ps : ps ,
2022-07-25 03:08:42 +00:00
ip : a . Addr ( ) ,
2021-03-30 16:54:52 +00:00
ln : ln , // nil for 2nd+ on netstack
2021-03-29 17:42:33 +00:00
lb : b ,
2021-03-25 22:38:40 +00:00
}
2021-03-30 16:54:52 +00:00
if skipListen {
2021-04-02 05:04:46 +00:00
pln . port = b . peerAPIListeners [ 0 ] . port
2021-03-30 16:54:52 +00:00
} else {
2021-04-02 05:04:46 +00:00
pln . port = ln . Addr ( ) . ( * net . TCPAddr ) . Port
2021-03-30 16:54:52 +00:00
}
2022-07-25 03:08:42 +00:00
pln . urlStr = "http://" + net . JoinHostPort ( a . Addr ( ) . String ( ) , strconv . Itoa ( pln . port ) )
2021-03-30 16:54:52 +00:00
b . logf ( "peerapi: serving on %s" , pln . urlStr )
2021-03-25 22:38:40 +00:00
go pln . serve ( )
b . peerAPIListeners = append ( b . peerAPIListeners , pln )
}
2021-04-09 19:10:52 +00:00
2021-04-09 20:23:34 +00:00
go b . doSetHostinfoFilterServices ( b . hostinfo . Clone ( ) )
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.
2021-04-09 22:24:47 +00:00
func magicDNSRootDomains ( nm * netmap . NetworkMap ) [ ] dnsname . FQDN {
2021-01-10 20:03:01 +00:00
if v := nm . MagicDNSSuffix ( ) ; v != "" {
2021-04-09 22:24:47 +00:00
fqdn , err := dnsname . ToFQDN ( v )
if err != nil {
// TODO: propagate error
return nil
}
2021-04-21 01:05:17 +00:00
ret := [ ] dnsname . FQDN {
fqdn ,
dnsname . FQDN ( "0.e.1.a.c.5.1.1.a.7.d.f.ip6.arpa." ) ,
}
for i := 64 ; i <= 127 ; i ++ {
fqdn , err = dnsname . ToFQDN ( fmt . Sprintf ( "%d.100.in-addr.arpa." , i ) )
if err != nil {
// TODO: propagate error
continue
}
ret = append ( ret , fqdn )
}
return ret
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 (
2022-07-26 03:55:44 +00:00
ipv4Default = netip . MustParsePrefix ( "0.0.0.0/0" )
ipv6Default = netip . MustParsePrefix ( "::/0" )
2021-01-21 01:24:16 +00:00
)
2021-03-04 20:04:31 +00:00
// peerRoutes returns the routerConfig.Routes to access peers.
// If there are over cgnatThreshold CGNAT routes, one big CGNAT route
// is used instead.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func peerRoutes ( peers [ ] wgcfg . Peer , cgnatThreshold int ) ( routes [ ] netip . Prefix ) {
2021-03-04 20:04:31 +00:00
tsULA := tsaddr . TailscaleULARange ( )
cgNAT := tsaddr . CGNATRange ( )
var didULA bool
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var cgNATIPs [ ] netip . Prefix
2021-03-04 20:04:31 +00:00
for _ , peer := range peers {
for _ , aip := range peer . AllowedIPs {
aip = unmapIPPrefix ( aip )
// Only add the Tailscale IPv6 ULA once, if we see anybody using part of it.
2022-07-25 03:08:42 +00:00
if aip . Addr ( ) . Is6 ( ) && aip . IsSingleIP ( ) && tsULA . Contains ( aip . Addr ( ) ) {
2021-03-04 20:04:31 +00:00
if ! didULA {
didULA = true
routes = append ( routes , tsULA )
}
continue
}
2022-07-25 03:08:42 +00:00
if aip . IsSingleIP ( ) && cgNAT . Contains ( aip . Addr ( ) ) {
2021-03-04 20:04:31 +00:00
cgNATIPs = append ( cgNATIPs , aip )
} else {
routes = append ( routes , aip )
}
}
}
if len ( cgNATIPs ) > cgnatThreshold {
// Probably the hello server. Just append one big route.
routes = append ( routes , cgNAT )
} else {
routes = append ( routes , cgNATIPs ... )
}
2022-04-13 22:41:04 +00:00
sort . Slice ( routes , func ( i , j int ) bool {
return ipPrefixLess ( routes [ i ] , routes [ j ] )
} )
2021-03-04 20:04:31 +00:00
return routes
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ipPrefixLess ( ri , rj netip . Prefix ) bool {
2022-07-25 03:08:42 +00:00
if ri . Addr ( ) == rj . Addr ( ) {
2022-04-13 22:41:04 +00:00
return ri . Bits ( ) < rj . Bits ( )
}
2022-07-25 03:08:42 +00:00
return ri . Addr ( ) . Less ( rj . Addr ( ) )
2022-04-13 22:41:04 +00:00
}
2020-07-31 20:27:09 +00:00
// routerConfig produces a router.Config from a wireguard config and IPN prefs.
2022-10-23 17:07:10 +00:00
func ( b * LocalBackend ) routerConfig ( cfg * wgcfg . Config , prefs ipn . PrefsView , oneCGNATRoute bool ) * router . Config {
2022-04-14 21:40:17 +00:00
singleRouteThreshold := 10_000
if oneCGNATRoute {
singleRouteThreshold = 1
}
2020-05-12 07:08:52 +00:00
rs := & router . Config {
2020-12-24 20:33:55 +00:00
LocalAddrs : unmapIPPrefixes ( cfg . Addresses ) ,
2022-10-23 17:07:10 +00:00
SubnetRoutes : unmapIPPrefixes ( prefs . AdvertiseRoutes ( ) . AsSlice ( ) ) ,
SNATSubnetRoutes : ! prefs . NoSNAT ( ) ,
NetfilterMode : prefs . NetfilterMode ( ) ,
2022-04-14 21:40:17 +00:00
Routes : peerRoutes ( cfg . Peers , singleRouteThreshold ) ,
2020-05-11 21:02:12 +00:00
}
2021-05-27 17:07:17 +00:00
if distro . Get ( ) == distro . Synology {
// Issue 1995: we don't use iptables on Synology.
rs . NetfilterMode = preftype . NetfilterOff
}
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.
2022-10-23 17:07:10 +00:00
if prefs . ExitNodeID ( ) != "" || prefs . ExitNodeIP ( ) . IsValid ( ) {
2021-01-21 01:24:16 +00:00
var default4 , default6 bool
for _ , route := range rs . Routes {
2021-04-08 22:56:51 +00:00
switch route {
case ipv4Default :
2021-01-21 01:24:16 +00:00
default4 = true
2021-04-08 22:56:51 +00:00
case ipv6Default :
2021-01-21 01:24:16 +00:00
default6 = true
}
if default4 && default6 {
break
}
}
if ! default4 {
rs . Routes = append ( rs . Routes , ipv4Default )
}
if ! default6 {
rs . Routes = append ( rs . Routes , ipv6Default )
}
2021-07-02 14:15:28 +00:00
internalIPs , externalIPs , err := internalAndExternalInterfaces ( )
if err != nil {
b . logf ( "failed to discover interface ips: %v" , err )
}
2021-06-16 15:53:08 +00:00
if runtime . GOOS == "linux" || runtime . GOOS == "darwin" || runtime . GOOS == "windows" {
2021-07-02 14:15:28 +00:00
rs . LocalRoutes = internalIPs // unconditionally allow access to guest VM networks
2022-10-23 17:07:10 +00:00
if prefs . ExitNodeAllowLANAccess ( ) {
2021-07-02 14:15:28 +00:00
rs . LocalRoutes = append ( rs . LocalRoutes , externalIPs ... )
2021-04-19 23:03:03 +00:00
} else {
// Explicitly add routes to the local network so that we do not
// leak any traffic.
2021-07-02 14:15:28 +00:00
rs . Routes = append ( rs . Routes , externalIPs ... )
2021-04-19 23:03:03 +00:00
}
2022-08-05 17:32:11 +00:00
b . logf ( "allowing exit node access to local IPs: %v" , rs . LocalRoutes )
2021-04-08 22:56:51 +00:00
}
2021-01-21 01:24:16 +00:00
}
2022-01-04 21:33:08 +00:00
if tsaddr . PrefixesContainsFunc ( rs . LocalAddrs , tsaddr . PrefixIs4 ) {
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
rs . Routes = append ( rs . Routes , netip . PrefixFrom ( tsaddr . TailscaleServiceIP ( ) , 32 ) )
2022-01-04 21:33:08 +00:00
}
2020-06-08 22:19:26 +00:00
2020-05-11 21:02:12 +00:00
return rs
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func unmapIPPrefix ( ipp netip . Prefix ) netip . Prefix {
2022-07-25 03:08:42 +00:00
return netip . PrefixFrom ( ipp . Addr ( ) . Unmap ( ) , ipp . Bits ( ) )
2021-03-04 20:04:31 +00:00
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func unmapIPPrefixes ( ippsList ... [ ] netip . Prefix ) ( ret [ ] netip . Prefix ) {
2020-12-24 20:33:55 +00:00
for _ , ipps := range ippsList {
for _ , ipp := range ipps {
2021-03-04 20:04:31 +00:00
ret = append ( ret , unmapIPPrefix ( ipp ) )
2020-05-11 21:02:12 +00:00
}
}
return ret
}
2022-02-17 23:00:41 +00:00
// Warning: b.mu might be held. Currently (2022-02-17) both callers hold it.
2022-10-23 17:07:10 +00:00
func ( b * LocalBackend ) applyPrefsToHostinfo ( hi * tailcfg . Hostinfo , prefs ipn . PrefsView ) {
if h := prefs . Hostname ( ) ; h != "" {
2020-07-24 21:05:04 +00:00
hi . Hostname = h
}
2022-10-23 17:07:10 +00:00
hi . RoutableIPs = prefs . AdvertiseRoutes ( ) . AsSlice ( )
hi . RequestTags = prefs . AdvertiseTags ( ) . AsSlice ( )
hi . ShieldsUp = prefs . ShieldsUp ( )
2022-02-17 23:00:41 +00:00
var sshHostKeys [ ] string
2022-10-23 17:07:10 +00:00
if prefs . RunSSH ( ) && envknob . CanSSHD ( ) {
2022-02-17 23:00:41 +00:00
// TODO(bradfitz): this is called with b.mu held. Not ideal.
// If the filesystem gets wedged or something we could block for
// a long time. But probably fine.
sshHostKeys = b . getSSHHostKeyPublicStrings ( )
}
hi . SSH_HostKeys = sshHostKeys
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 ( )
2022-11-09 05:58:10 +00:00
b . enterStateLockedOnEntry ( newState )
}
// enterStateLockedOnEntry is like enterState but requires b.mu be held to call
// it, but it unlocks b.mu when done.
func ( b * LocalBackend ) enterStateLockedOnEntry ( newState ipn . State ) {
2021-04-23 18:45:48 +00:00
oldState := b . state
2020-06-19 17:43:55 +00:00
b . state = newState
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
2021-04-30 10:08:26 +00:00
netMap := b . netMap
2020-11-24 23:35:04 +00:00
activeLogin := b . activeLogin
authURL := b . authURL
2021-04-21 20:37:36 +00:00
if newState == ipn . Running {
b . authURL = ""
b . authURLSticky = ""
2021-04-23 18:45:48 +00:00
} else if oldState == ipn . Running {
// Transitioning away from running.
b . closePeerAPIListenersLocked ( )
2021-04-21 20:37:36 +00:00
}
2021-07-16 22:21:00 +00:00
b . maybePauseControlClientLocked ( )
2020-02-05 22:16:58 +00:00
b . mu . Unlock ( )
2022-06-03 17:52:07 +00:00
// prefs may change irrespective of state; WantRunning should be explicitly
// set before potential early return even if the state is unchanged.
2022-11-09 05:58:10 +00:00
health . SetIPNState ( newState . String ( ) , prefs . Valid ( ) && prefs . WantRunning ( ) )
2021-04-23 18:45:48 +00:00
if oldState == newState {
2020-02-05 22:16:58 +00:00
return
}
2021-05-06 03:28:29 +00:00
b . logf ( "Switching ipn state %v -> %v (WantRunning=%v, nm=%v)" ,
2022-11-09 05:58:10 +00:00
oldState , newState , prefs . WantRunning ( ) , netMap != nil )
2021-04-12 17:36:19 +00:00
b . send ( ipn . Notify { State : & newState } )
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 :
2021-06-22 20:13:59 +00:00
err := b . e . Reconfig ( & wgcfg . Config { } , & router . Config { } , & dns . Config { } , nil )
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
2021-07-01 19:20:19 +00:00
for _ , addr := range netMap . Addresses {
2022-07-25 03:08:42 +00:00
addrs = append ( addrs , addr . Addr ( ) . String ( ) )
2020-11-24 23:35:04 +00:00
}
systemd . Status ( "Connected; %s; %s" , activeLogin , strings . Join ( addrs , " " ) )
2022-11-09 05:58:10 +00:00
case ipn . NoState :
// Do nothing.
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
}
}
2021-04-30 09:27:37 +00:00
func ( b * LocalBackend ) hasNodeKey ( ) bool {
// we can't use b.Prefs(), because it strips the keys, oops!
b . mu . Lock ( )
2022-10-23 17:07:10 +00:00
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
p := b . pm . CurrentPrefs ( )
return p . Valid ( ) && p . Persist ( ) != nil && ! p . Persist ( ) . PrivateNodeKey . IsZero ( )
2021-04-30 09:27:37 +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 (
2022-11-09 05:58:10 +00:00
cc = b . cc
netMap = b . netMap
state = b . state
blocked = b . blocked
st = b . engineStatus
keyExpired = b . keyExpired
wantRunning = false
loggedOut = false
2020-02-29 02:34:56 +00:00
)
2022-11-09 05:58:10 +00:00
if p := b . pm . CurrentPrefs ( ) ; p . Valid ( ) {
wantRunning = p . WantRunning ( )
loggedOut = p . LoggedOut ( )
}
2020-02-28 19:39:13 +00:00
b . mu . Unlock ( )
2020-05-19 02:32:20 +00:00
switch {
2021-05-06 03:28:29 +00:00
case ! wantRunning && ! loggedOut && ! blocked && b . hasNodeKey ( ) :
2021-04-30 07:56:11 +00:00
return ipn . Stopped
2020-05-19 02:32:20 +00:00
case netMap == nil :
2021-04-30 07:56:11 +00:00
if cc . AuthCantContinue ( ) || loggedOut {
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
2021-09-15 19:49:09 +00:00
}
switch state {
case ipn . Stopped :
2021-04-30 08:29:22 +00:00
// If we were already in the Stopped state, then
// we can assume auth is in good shape (or we would
// have been in NeedsLogin), so transition to Starting
// right away.
return ipn . Starting
2021-09-15 19:49:09 +00:00
case ipn . NoState :
2021-04-30 08:29:22 +00:00
// Our first time connecting to control, and we
// don't know if we'll NeedsLogin or not yet.
// UIs should print "Loading..." in this state.
return ipn . NoState
2021-09-15 19:49:09 +00:00
case ipn . Starting , ipn . Running , ipn . NeedsLogin :
2021-04-30 08:29:22 +00:00
return state
2021-09-15 19:49:09 +00:00
default :
2021-04-30 08:29:22 +00:00
b . logf ( "unexpected no-netmap state transition for %v" , state )
2020-02-05 22:16:58 +00:00
return state
}
2020-05-19 02:32:20 +00:00
case ! wantRunning :
2021-02-04 21:12:42 +00:00
return ipn . Stopped
2021-11-04 19:19:00 +00:00
case keyExpired :
// NetMap must be non-nil for us to get here.
// The node key expired, need to relogin.
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 :
2021-09-15 19:43:09 +00:00
if 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
// 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.
//
2022-09-25 18:29:55 +00:00
// TODO(apenwarr): use a channel or something to prevent reentrancy?
2022-08-02 16:33:46 +00:00
// Or maybe just call the state machine from fewer places.
2020-02-05 22:16:58 +00:00
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..." )
2021-06-22 20:13:59 +00:00
b . e . Reconfig ( & wgcfg . Config { } , & router . Config { } , & dns . Config { } , nil )
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 ( )
}
2021-04-20 18:56:36 +00:00
// ResetForClientDisconnect resets the backend for GUI clients running
// in interactive (non-headless) mode. This is currently used only by
// Windows. This causes all state to be cleared, lest an unrelated user
// connect to tailscaled next. But it does not trigger a logout; we
// don't want to the user to have to reauthenticate in the future
// when they restart the GUI.
func ( b * LocalBackend ) ResetForClientDisconnect ( ) {
defer b . enterState ( ipn . Stopped )
b . mu . Lock ( )
defer b . mu . Unlock ( )
b . logf ( "LocalBackend.ResetForClientDisconnect" )
if b . cc != nil {
go b . cc . Shutdown ( )
b . cc = nil
}
b . setNetMapLocked ( nil )
2022-11-09 05:58:10 +00:00
b . pm . Reset ( )
2021-11-04 19:19:00 +00:00
b . keyExpired = false
2021-04-20 18:56:36 +00:00
b . authURL = ""
2021-04-21 20:37:36 +00:00
b . authURLSticky = ""
2021-04-20 18:56:36 +00:00
b . activeLogin = ""
2022-11-09 05:58:10 +00:00
b . setAtomicValuesFromPrefs ( ipn . PrefsView { } )
2022-11-07 23:32:53 +00:00
b . setTCPPortsIntercepted ( nil )
2021-04-20 18:56:36 +00:00
}
2022-09-14 19:49:39 +00:00
func ( b * LocalBackend ) ShouldRunSSH ( ) bool { return b . sshAtomicBool . Load ( ) && envknob . CanSSHD ( ) }
2021-08-26 21:50:55 +00:00
2022-09-25 18:29:55 +00:00
// ShouldHandleViaIP reports whether ip is an IPv6 address in the
2022-03-30 15:47:16 +00:00
// Tailscale ULA's v6 "via" range embedding an IPv4 address to be forwarded to
// by Tailscale.
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func ( b * LocalBackend ) ShouldHandleViaIP ( ip netip . Addr ) bool {
2022-08-04 17:43:49 +00:00
if f , ok := b . containsViaIPFuncAtomic . LoadOk ( ) ; ok {
2022-03-30 15:47:16 +00:00
return f ( ip )
}
return false
}
2021-04-08 04:17:33 +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.
2020-02-05 22:16:58 +00:00
func ( b * LocalBackend ) Logout ( ) {
2021-04-08 04:06:31 +00:00
b . logout ( context . Background ( ) , false )
}
func ( b * LocalBackend ) LogoutSync ( ctx context . Context ) error {
return b . logout ( ctx , true )
}
func ( b * LocalBackend ) logout ( ctx context . Context , sync bool ) error {
2020-02-28 19:39:13 +00:00
b . mu . Lock ( )
2021-04-08 04:12:16 +00:00
cc := b . cc
2020-02-28 19:39:13 +00:00
b . mu . Unlock ( )
2021-04-08 04:06:31 +00:00
b . EditPrefs ( & ipn . MaskedPrefs {
WantRunningSet : true ,
2021-04-30 07:56:11 +00:00
LoggedOutSet : true ,
Prefs : ipn . Prefs { WantRunning : false , LoggedOut : true } ,
2021-04-08 04:06:31 +00:00
} )
2022-09-23 17:06:55 +00:00
// Clear any previous dial plan(s), if set.
b . dialPlan . Store ( nil )
2021-04-08 04:12:16 +00:00
if cc == nil {
2020-09-11 22:10:29 +00:00
// 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.
2021-04-08 04:06:31 +00:00
return errors . New ( "no controlclient" )
2020-09-11 22:10:29 +00:00
}
2021-04-08 04:06:31 +00:00
var err error
if sync {
err = cc . Logout ( ctx )
} else {
cc . StartLogout ( )
}
2020-02-05 22:16:58 +00:00
2020-02-28 19:39:13 +00:00
b . stateMachine ( )
2021-04-08 04:06:31 +00:00
return err
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 ( ) {
2021-04-08 04:12:16 +00:00
if b . cc == nil {
panic ( "LocalBackend.assertClient: b.cc == nil" )
2020-02-05 22:16:58 +00:00
}
}
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 ( )
2021-04-08 04:12:16 +00:00
cc := b . cc
2020-03-04 06:21:56 +00:00
b . mu . Unlock ( )
2020-02-25 22:05:17 +00:00
2021-04-08 04:12:16 +00:00
if cc == nil {
2020-03-04 06:21:56 +00:00
return
2020-02-25 22:05:17 +00:00
}
2021-04-08 04:12:16 +00:00
cc . SetNetInfo ( ni )
2020-02-25 22:05:17 +00:00
}
2020-05-27 19:23:17 +00:00
2021-04-22 07:25:00 +00:00
func hasCapability ( nm * netmap . NetworkMap , cap string ) bool {
if nm != nil && nm . SelfNode != nil {
for _ , c := range nm . SelfNode . Capabilities {
if c == cap {
return true
}
}
}
return false
}
2021-02-05 23:44:46 +00:00
func ( b * LocalBackend ) setNetMapLocked ( nm * netmap . NetworkMap ) {
2021-12-02 19:10:35 +00:00
b . dialer . SetNetMap ( nm )
2020-10-27 19:51:48 +00:00
var login string
if nm != nil {
2022-11-09 05:58:10 +00:00
up := nm . UserProfiles [ nm . User ]
login = up . LoginName
2020-10-27 19:51:48 +00:00
if login == "" {
login = "<missing-profile>"
}
2022-11-09 05:58:10 +00:00
if cp := b . pm . CurrentProfile ( ) ; cp . ID != "" && cp . UserProfile . ID == 0 {
// Migration to profiles: we didn't use to persist
// the UserProfile, so if we don't have one, fill it
// in from the NetworkMap.
prefs := b . pm . CurrentPrefs ( ) . AsStruct ( )
prefs . Persist . UserProfile = up
b . pm . SetPrefs ( prefs . View ( ) )
}
2020-10-27 19:51:48 +00:00
}
b . netMap = nm
if login != b . activeLogin {
b . logf ( "active login: %v" , login )
b . activeLogin = login
}
2021-07-16 22:21:00 +00:00
b . maybePauseControlClientLocked ( )
2021-01-28 23:29:17 +00:00
2021-09-18 19:59:55 +00:00
if nm != nil {
health . SetControlHealth ( nm . ControlHealth )
} else {
health . SetControlHealth ( nil )
}
2021-04-22 07:25:00 +00:00
// Determine if file sharing is enabled
fs := hasCapability ( nm , tailcfg . CapabilityFileSharing )
if fs != b . capFileSharing {
osshare . SetFileSharingEnabled ( fs , b . logf )
}
b . capFileSharing = fs
2022-11-09 05:58:10 +00:00
b . setTCPPortsInterceptedFromNetmapAndPrefsLocked ( b . pm . CurrentPrefs ( ) )
2021-01-28 23:29:17 +00:00
if nm == nil {
b . nodeByAddr = nil
return
}
// Update the nodeByAddr index.
if b . nodeByAddr == nil {
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
b . nodeByAddr = map [ netip . Addr ] * tailcfg . Node { }
2021-01-28 23:29:17 +00:00
}
// 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 ( ) {
2022-07-25 03:08:42 +00:00
b . nodeByAddr [ ipp . Addr ( ) ] = n
2021-01-28 23:29:17 +00:00
}
}
}
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
}
2022-11-07 23:32:53 +00:00
// setTCPPortsInterceptedFromNetmapAndPrefsLocked calls setTCPPortsIntercepted with
// the ports that tailscaled should handle as a function of b.netMap and b.prefs.
//
// b.mu must be held.
2022-11-09 05:58:10 +00:00
func ( b * LocalBackend ) setTCPPortsInterceptedFromNetmapAndPrefsLocked ( prefs ipn . PrefsView ) {
2022-11-07 23:32:53 +00:00
handlePorts := make ( [ ] uint16 , 0 , 4 )
if prefs . Valid ( ) && prefs . RunSSH ( ) && envknob . CanSSHD ( ) {
handlePorts = append ( handlePorts , 22 )
}
nm := b . netMap
if nm != nil && nm . SelfNode != nil {
2022-11-09 05:58:10 +00:00
profileID := b . pm . CurrentProfile ( ) . ID
2022-11-07 23:32:53 +00:00
confKey := ipn . ServeConfigKey ( profileID )
if confj , err := b . store . ReadState ( confKey ) ; err == nil {
if ! b . lastServeConfJSON . Equal ( mem . B ( confj ) ) {
b . lastServeConfJSON = mem . B ( confj )
var conf ipn . ServeConfig
if err := json . Unmarshal ( confj , & conf ) ; err != nil {
b . logf ( "invalid ServeConfig %q in StateStore: %v" , confKey , err )
2022-11-09 21:15:59 +00:00
b . serveConfig = ipn . ServeConfigView { }
} else {
b . serveConfig = conf . View ( )
2022-11-07 23:32:53 +00:00
}
}
2022-11-09 21:15:59 +00:00
if b . serveConfig . Valid ( ) {
2022-11-12 02:46:26 +00:00
servePorts := make ( [ ] uint16 , 0 , 3 )
2022-11-11 03:58:40 +00:00
b . serveConfig . TCP ( ) . Range ( func ( port uint16 , _ ipn . TCPPortHandlerView ) bool {
if port > 0 {
2022-11-12 02:46:26 +00:00
servePorts = append ( servePorts , uint16 ( port ) )
2022-11-09 21:15:59 +00:00
}
return true
} )
2022-11-12 02:46:26 +00:00
handlePorts = append ( handlePorts , servePorts ... )
// don't listen on netmap addresses if we're in userspace mode
if ! wgengine . IsNetstack ( b . e ) {
b . updateServeTCPPortNetMapAddrListenersLocked ( servePorts )
}
2022-11-07 23:32:53 +00:00
}
}
}
b . setTCPPortsIntercepted ( handlePorts )
}
2022-09-21 20:35:22 +00:00
// operatorUserName returns the current pref's OperatorUser's name, or the
// empty string if none.
func ( b * LocalBackend ) operatorUserName ( ) string {
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
if ! prefs . Valid ( ) {
2022-09-21 20:35:22 +00:00
return ""
}
2022-11-09 05:58:10 +00:00
return prefs . OperatorUser ( )
2022-09-21 20:35:22 +00:00
}
2021-04-17 04:01:29 +00:00
// OperatorUserID returns the current pref's OperatorUser's ID (in
// os/user.User.Uid string form), or the empty string if none.
func ( b * LocalBackend ) OperatorUserID ( ) string {
2022-10-23 17:07:10 +00:00
opUserName := b . operatorUserName ( )
2021-04-17 04:01:29 +00:00
if opUserName == "" {
return ""
}
u , err := user . Lookup ( opUserName )
if err != nil {
b . logf ( "error looking up operator %q uid: %v" , opUserName , err )
return ""
}
return u . Uid
}
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.
2021-11-02 01:40:39 +00:00
func ( b * LocalBackend ) TestOnlyPublicKeys ( ) ( machineKey key . MachinePublic , nodeKey key . NodePublic ) {
2020-05-27 19:23:17 +00:00
b . mu . Lock ( )
2020-09-28 22:28:26 +00:00
machinePrivKey := b . machinePrivKey
2022-11-09 05:58:10 +00:00
prefs := b . pm . CurrentPrefs ( )
2020-05-27 19:23:17 +00:00
b . mu . Unlock ( )
2022-10-23 17:07:10 +00:00
if ! prefs . Valid ( ) || machinePrivKey . IsZero ( ) {
2020-05-27 19:23:17 +00:00
return
}
2020-09-28 22:28:26 +00:00
mk := machinePrivKey . Public ( )
2022-10-23 07:48:12 +00:00
nk := prefs . Persist ( ) . PublicNodeKey ( )
2021-11-02 01:40:39 +00:00
return mk , nk
2020-05-27 19:23:17 +00:00
}
2020-10-21 19:55:03 +00:00
2021-04-13 15:13:46 +00:00
func ( b * LocalBackend ) WaitingFiles ( ) ( [ ] apitype . WaitingFile , error ) {
2021-03-30 19:56:00 +00:00
b . mu . Lock ( )
apiSrv := b . peerAPIServer
b . mu . Unlock ( )
return apiSrv . WaitingFiles ( )
}
func ( b * LocalBackend ) DeleteFile ( name string ) error {
b . mu . Lock ( )
apiSrv := b . peerAPIServer
b . mu . Unlock ( )
return apiSrv . DeleteFile ( name )
}
func ( b * LocalBackend ) OpenFile ( name string ) ( rc io . ReadCloser , size int64 , err error ) {
b . mu . Lock ( )
apiSrv := b . peerAPIServer
b . mu . Unlock ( )
return apiSrv . OpenFile ( name )
}
2021-03-31 18:55:21 +00:00
2021-04-16 17:57:46 +00:00
// hasCapFileSharing reports whether the current node has the file
// sharing capability enabled.
func ( b * LocalBackend ) hasCapFileSharing ( ) bool {
b . mu . Lock ( )
defer b . mu . Unlock ( )
2021-04-22 07:25:00 +00:00
return b . capFileSharing
2021-04-16 17:57:46 +00:00
}
2021-04-05 04:35:52 +00:00
// FileTargets lists nodes that the current node can send files to.
2021-04-13 15:13:46 +00:00
func ( b * LocalBackend ) FileTargets ( ) ( [ ] * apitype . FileTarget , error ) {
var ret [ ] * apitype . FileTarget
2021-04-05 04:35:52 +00:00
b . mu . Lock ( )
defer b . mu . Unlock ( )
nm := b . netMap
if b . state != ipn . Running || nm == nil {
2022-06-08 16:11:50 +00:00
return nil , errors . New ( "not connected to the tailnet" )
2021-04-05 04:35:52 +00:00
}
2021-04-29 17:26:53 +00:00
if ! b . capFileSharing {
return nil , errors . New ( "file sharing not enabled by Tailscale admin" )
}
2021-04-05 04:35:52 +00:00
for _ , p := range nm . Peers {
2022-10-08 04:03:36 +00:00
if ! b . peerIsTaildropTargetLocked ( p ) {
2021-04-05 04:35:52 +00:00
continue
}
peerAPI := peerAPIBase ( b . netMap , p )
if peerAPI == "" {
continue
}
2021-04-13 15:13:46 +00:00
ret = append ( ret , & apitype . FileTarget {
2021-04-05 04:35:52 +00:00
Node : p ,
PeerAPIURL : peerAPI ,
} )
}
// TODO: sort a different way than the netmap already is?
return ret , nil
}
2022-10-08 04:03:36 +00:00
// peerIsTaildropTargetLocked reports whether p is a valid Taildrop file
// recipient from this node according to its ownership and the capabilities in
// the netmap.
//
// b.mu must be locked.
func ( b * LocalBackend ) peerIsTaildropTargetLocked ( p * tailcfg . Node ) bool {
if b . netMap == nil || p == nil {
return false
}
if b . netMap . User == p . User {
return true
}
if len ( p . Addresses ) > 0 &&
b . peerHasCapLocked ( p . Addresses [ 0 ] . Addr ( ) , tailcfg . CapabilityFileSharingTarget ) {
// Explicitly noted in the netmap ACL caps as a target.
return true
}
return false
}
2022-09-19 17:36:07 +00:00
func ( b * LocalBackend ) peerHasCapLocked ( addr netip . Addr , wantCap string ) bool {
for _ , hasCap := range b . peerCapsLocked ( addr ) {
if hasCap == wantCap {
return true
}
}
return false
}
2021-06-07 23:03:16 +00:00
// SetDNS adds a DNS record for the given domain name & TXT record
// value.
//
// It's meant for use with dns-01 ACME (LetsEncrypt) challenges.
//
// This is the low-level interface. Other layers will provide more
// friendly options to get HTTPS certs.
func ( b * LocalBackend ) SetDNS ( ctx context . Context , name , value string ) error {
req := & tailcfg . SetDNSRequest {
2022-03-06 17:32:52 +00:00
Version : 1 , // TODO(bradfitz,maisem): use tailcfg.CurrentCapabilityVersion when using the Noise transport
2021-06-07 23:03:16 +00:00
Type : "TXT" ,
Name : name ,
Value : value ,
}
b . mu . Lock ( )
2022-06-20 01:00:50 +00:00
cc := b . ccAuto
2022-11-09 05:58:10 +00:00
if prefs := b . pm . CurrentPrefs ( ) ; prefs . Valid ( ) {
req . NodeKey = prefs . Persist ( ) . PrivateNodeKey . Public ( )
2021-06-07 23:03:16 +00:00
}
b . mu . Unlock ( )
if cc == nil {
return errors . New ( "not connected" )
}
if req . NodeKey . IsZero ( ) {
return errors . New ( "no nodekey" )
}
if name == "" {
return errors . New ( "missing 'name'" )
}
if value == "" {
return errors . New ( "missing 'value'" )
}
return cc . SetDNS ( ctx , req )
}
2021-04-08 21:54:25 +00:00
func ( b * LocalBackend ) registerIncomingFile ( inf * incomingFile , active bool ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . incomingFiles == nil {
b . incomingFiles = make ( map [ * incomingFile ] bool )
}
if active {
b . incomingFiles [ inf ] = true
} else {
delete ( b . incomingFiles , inf )
}
}
2021-04-05 04:35:52 +00:00
// peerAPIBase returns the "http://ip:port" URL base to reach peer's peerAPI.
// It returns the empty string if the peer doesn't support the peerapi
// or there's no matching address family based on the netmap's own addresses.
func peerAPIBase ( nm * netmap . NetworkMap , peer * tailcfg . Node ) string {
2022-02-15 16:19:44 +00:00
if nm == nil || peer == nil || ! peer . Hostinfo . Valid ( ) {
2021-04-05 04:35:52 +00:00
return ""
}
2022-02-15 16:19:44 +00:00
2021-04-05 04:35:52 +00:00
var have4 , have6 bool
for _ , a := range nm . Addresses {
if ! a . IsSingleIP ( ) {
continue
}
switch {
2022-07-25 03:08:42 +00:00
case a . Addr ( ) . Is4 ( ) :
2021-04-05 04:35:52 +00:00
have4 = true
2022-07-25 03:08:42 +00:00
case a . Addr ( ) . Is6 ( ) :
2021-04-05 04:35:52 +00:00
have6 = true
}
}
var p4 , p6 uint16
2022-02-15 16:19:44 +00:00
svcs := peer . Hostinfo . Services ( )
for i , n := 0 , svcs . Len ( ) ; i < n ; i ++ {
s := svcs . At ( i )
2021-04-05 04:35:52 +00:00
switch s . Proto {
2021-11-30 01:19:17 +00:00
case tailcfg . PeerAPI4 :
2021-04-05 04:35:52 +00:00
p4 = s . Port
2021-11-30 01:19:17 +00:00
case tailcfg . PeerAPI6 :
2021-04-05 04:35:52 +00:00
p6 = s . Port
}
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
var ipp netip . AddrPort
2021-04-05 04:35:52 +00:00
switch {
case have4 && p4 != 0 :
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
ipp = netip . AddrPortFrom ( nodeIP ( peer , netip . Addr . Is4 ) , p4 )
2021-04-05 04:35:52 +00:00
case have6 && p6 != 0 :
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
ipp = netip . AddrPortFrom ( nodeIP ( peer , netip . Addr . Is6 ) , p6 )
2021-04-05 04:35:52 +00:00
}
2022-07-25 03:08:42 +00:00
if ! ipp . Addr ( ) . IsValid ( ) {
2021-04-05 04:35:52 +00:00
return ""
}
return fmt . Sprintf ( "http://%v" , ipp )
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
func nodeIP ( n * tailcfg . Node , pred func ( netip . Addr ) bool ) netip . Addr {
2021-04-05 04:35:52 +00:00
for _ , a := range n . Addresses {
2022-07-25 03:08:42 +00:00
if a . IsSingleIP ( ) && pred ( a . Addr ( ) ) {
return a . Addr ( )
2021-04-05 04:35:52 +00:00
}
}
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
return netip . Addr { }
2021-04-05 04:35:52 +00:00
}
2021-03-31 18:55:21 +00:00
func ( b * LocalBackend ) CheckIPForwarding ( ) error {
2021-04-01 16:35:41 +00:00
if wgengine . IsNetstackRouter ( b . e ) {
2021-03-31 18:55:21 +00:00
return nil
}
2021-11-26 00:12:08 +00:00
2022-03-28 17:24:11 +00:00
// TODO: let the caller pass in the ranges.
warn , err := netutil . CheckIPForwarding ( tsaddr . ExitRoutes ( ) , nil )
2021-11-26 00:12:08 +00:00
if err != nil {
2022-03-28 17:24:11 +00:00
return err
2021-03-31 18:55:21 +00:00
}
2022-03-28 17:24:11 +00:00
return warn
2021-03-31 18:55:21 +00:00
}
2021-04-06 20:38:47 +00:00
2021-06-25 18:44:40 +00:00
// DERPMap returns the current DERPMap in use, or nil if not connected.
func ( b * LocalBackend ) DERPMap ( ) * tailcfg . DERPMap {
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . netMap == nil {
return nil
}
return b . netMap . DERPMap
}
2021-11-23 05:45:34 +00:00
// OfferingExitNode reports whether b is currently offering exit node
// access.
func ( b * LocalBackend ) OfferingExitNode ( ) bool {
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
if ! b . pm . CurrentPrefs ( ) . Valid ( ) {
2021-11-23 05:45:34 +00:00
return false
}
var def4 , def6 bool
2022-11-09 05:58:10 +00:00
ar := b . pm . CurrentPrefs ( ) . AdvertiseRoutes ( )
2022-10-23 17:07:10 +00:00
for i := 0 ; i < ar . Len ( ) ; i ++ {
r := ar . At ( i )
2021-11-23 05:45:34 +00:00
if r . Bits ( ) != 0 {
continue
}
2022-07-25 03:08:42 +00:00
if r . Addr ( ) . Is4 ( ) {
2021-11-23 05:45:34 +00:00
def4 = true
2022-07-25 03:08:42 +00:00
} else if r . Addr ( ) . Is6 ( ) {
2021-11-23 05:45:34 +00:00
def6 = true
}
}
return def4 && def6
}
2021-11-29 22:18:09 +00:00
// allowExitNodeDNSProxyToServeName reports whether the Exit Node DNS
// proxy is allowed to serve responses for the provided DNS name.
func ( b * LocalBackend ) allowExitNodeDNSProxyToServeName ( name string ) bool {
b . mu . Lock ( )
defer b . mu . Unlock ( )
nm := b . netMap
if nm == nil {
return false
}
name = strings . ToLower ( name )
for _ , bad := range nm . DNS . ExitNodeFilteredSet {
if bad == "" {
// Invalid, ignore.
continue
}
if bad [ 0 ] == '.' {
// Entries beginning with a dot are suffix matches.
if dnsname . HasSuffix ( name , bad ) {
return false
}
continue
}
// Otherwise entries are exact matches. They're
// guaranteed to be lowercase already.
if name == bad {
return false
}
}
return true
}
2021-11-30 23:19:18 +00:00
2022-03-09 22:42:42 +00:00
// SetExpiry updates the expiry of the current node key to t, as long as it's
// only sooner than the old expiry.
//
// If t is in the past, the key is expired immediately.
// If t is after the current expiry, an error is returned.
func ( b * LocalBackend ) SetExpirySooner ( ctx context . Context , expiry time . Time ) error {
2022-06-20 05:00:22 +00:00
b . mu . Lock ( )
cc := b . ccAuto
b . mu . Unlock ( )
if cc == nil {
return errors . New ( "not running" )
}
return cc . SetExpirySooner ( ctx , expiry )
2022-03-09 22:42:42 +00:00
}
2021-11-30 23:19:18 +00:00
// exitNodeCanProxyDNS reports the DoH base URL ("http://foo/dns-query") without query parameters
// to exitNodeID's DoH service, if available.
//
// If exitNodeID is the zero valid, it returns "", false.
func exitNodeCanProxyDNS ( nm * netmap . NetworkMap , exitNodeID tailcfg . StableNodeID ) ( dohURL string , ok bool ) {
if exitNodeID . IsZero ( ) {
return "" , false
}
for _ , p := range nm . Peers {
if p . StableID != exitNodeID {
continue
}
2022-02-15 16:19:44 +00:00
services := p . Hostinfo . Services ( )
for i , n := 0 , services . Len ( ) ; i < n ; i ++ {
s := services . At ( i )
2021-11-30 23:19:18 +00:00
if s . Proto == tailcfg . PeerAPIDNS && s . Port >= 1 {
return peerAPIBase ( nm , p ) + "/dns-query" , true
}
}
}
return "" , false
}
2021-12-28 21:39:04 +00:00
func ( b * LocalBackend ) DebugRebind ( ) error {
mc , err := b . magicConn ( )
if err != nil {
return err
}
mc . Rebind ( )
return nil
}
func ( b * LocalBackend ) DebugReSTUN ( ) error {
mc , err := b . magicConn ( )
if err != nil {
return err
}
mc . ReSTUN ( "explicit-debug" )
return nil
}
func ( b * LocalBackend ) magicConn ( ) ( * magicsock . Conn , error ) {
ig , ok := b . e . ( wgengine . InternalsGetter )
if ! ok {
return nil , errors . New ( "engine isn't InternalsGetter" )
}
2022-04-14 21:55:23 +00:00
_ , mc , _ , ok := ig . GetInternals ( )
2021-12-28 21:39:04 +00:00
if ! ok {
return nil , errors . New ( "failed to get internals" )
}
return mc , nil
}
2022-03-10 18:28:42 +00:00
2022-11-02 03:37:13 +00:00
type keyProvingNoiseRoundTripper struct {
b * LocalBackend
2022-10-16 20:56:46 +00:00
}
2022-11-02 03:37:13 +00:00
func ( n keyProvingNoiseRoundTripper ) RoundTrip ( req * http . Request ) ( * http . Response , error ) {
b := n . b
var priv key . NodePrivate
b . mu . Lock ( )
cc := b . ccAuto
if nm := b . netMap ; nm != nil {
priv = nm . PrivateKey
}
b . mu . Unlock ( )
if cc == nil {
return nil , errors . New ( "no client" )
}
if priv . IsZero ( ) {
return nil , errors . New ( "no netmap or private key" )
}
rt , ep , err := cc . GetSingleUseNoiseRoundTripper ( req . Context ( ) )
if err != nil {
return nil , err
}
if ep == nil || ep . NodeKeyChallenge . IsZero ( ) {
go rt . RoundTrip ( new ( http . Request ) ) // return our reservation with a bogus request
return nil , errors . New ( "this coordination server does not support API calls over the Noise channel" )
}
// QueryEscape the node key since it has a colon in it.
nk := url . QueryEscape ( priv . Public ( ) . String ( ) )
req . SetBasicAuth ( nk , "" )
// genNodeProofHeaderValue returns the Tailscale-Node-Proof header's value to prove
// to chalPub that we control claimedPrivate.
genNodeProofHeaderValue := func ( claimedPrivate key . NodePrivate , chalPub key . ChallengePublic ) string {
// TODO(bradfitz): cache this somewhere?
box := claimedPrivate . SealToChallenge ( chalPub , [ ] byte ( chalPub . String ( ) ) )
return claimedPrivate . Public ( ) . String ( ) + " " + base64 . StdEncoding . EncodeToString ( box )
}
// And prove we have the private key corresponding to the public key sent
// tin the basic auth username.
req . Header . Set ( "Tailscale-Node-Proof" , genNodeProofHeaderValue ( priv , ep . NodeKeyChallenge ) )
return rt . RoundTrip ( req )
2022-10-16 20:56:46 +00:00
}
2022-11-02 03:37:13 +00:00
// KeyProvingNoiseRoundTripper returns an http.RoundTripper that uses the LocalBackend's
// DoNoiseRequest method and mutates the request to add an authorization header
// to prove the client's nodekey.
func ( b * LocalBackend ) KeyProvingNoiseRoundTripper ( ) http . RoundTripper {
return keyProvingNoiseRoundTripper { b }
2022-10-16 20:56:46 +00:00
}
2022-09-25 18:29:55 +00:00
// DoNoiseRequest sends a request to URL over the control plane
2022-03-10 18:28:42 +00:00
// Noise connection.
func ( b * LocalBackend ) DoNoiseRequest ( req * http . Request ) ( * http . Response , error ) {
b . mu . Lock ( )
2022-06-20 01:00:50 +00:00
cc := b . ccAuto
2022-03-10 18:28:42 +00:00
b . mu . Unlock ( )
if cc == nil {
return nil , errors . New ( "no client" )
}
return cc . DoNoiseRequest ( req )
}
2022-04-15 20:19:13 +00:00
2022-09-21 20:35:22 +00:00
// tailscaleSSHEnabled reports whether Tailscale SSH is currently enabled based
// on prefs. It returns false if there are no prefs set.
func ( b * LocalBackend ) tailscaleSSHEnabled ( ) bool {
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
p := b . pm . CurrentPrefs ( )
return p . Valid ( ) && p . RunSSH ( )
2022-09-21 20:35:22 +00:00
}
2022-05-30 09:06:46 +00:00
func ( b * LocalBackend ) sshServerOrInit ( ) ( _ SSHServer , err error ) {
b . mu . Lock ( )
defer b . mu . Unlock ( )
if b . sshServer != nil {
return b . sshServer , nil
}
if newSSHServer == nil {
return nil , errors . New ( "no SSH server support" )
}
b . sshServer , err = newSSHServer ( b . logf , b )
if err != nil {
return nil , fmt . Errorf ( "newSSHServer: %w" , err )
}
return b . sshServer , nil
}
func ( b * LocalBackend ) HandleSSHConn ( c net . Conn ) ( err error ) {
s , err := b . sshServerOrInit ( )
if err != nil {
return err
2022-04-15 20:19:13 +00:00
}
2022-05-30 09:06:46 +00:00
return s . HandleSSHConn ( c )
2022-04-15 20:19:13 +00:00
}
2022-05-05 21:53:36 +00:00
// HandleQuad100Port80Conn serves http://100.100.100.100/ on port 80 (and
// the equivalent tsaddr.TailscaleServiceIPv6 address).
func ( b * LocalBackend ) HandleQuad100Port80Conn ( c net . Conn ) {
var s http . Server
s . Handler = http . HandlerFunc ( b . handleQuad100Port80Conn )
s . Serve ( netutil . NewOneConnListener ( c , nil ) )
}
func ( b * LocalBackend ) handleQuad100Port80Conn ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "X-Frame-Options" , "DENY" )
w . Header ( ) . Set ( "Content-Security-Policy" , "default-src 'self';" )
if r . Method != "GET" && r . Method != "HEAD" {
http . Error ( w , "method not allowed" , http . StatusMethodNotAllowed )
return
}
b . mu . Lock ( )
defer b . mu . Unlock ( )
io . WriteString ( w , "<h1>Tailscale</h1>\n" )
if b . netMap == nil {
io . WriteString ( w , "No netmap.\n" )
return
}
if len ( b . netMap . Addresses ) == 0 {
io . WriteString ( w , "No local addresses.\n" )
return
}
io . WriteString ( w , "<p>Local addresses:</p><ul>\n" )
for _ , ipp := range b . netMap . Addresses {
2022-07-25 03:08:42 +00:00
fmt . Fprintf ( w , "<li>%v</li>\n" , ipp . Addr ( ) )
2022-05-05 21:53:36 +00:00
}
io . WriteString ( w , "</ul>\n" )
}
2022-09-26 17:07:28 +00:00
func ( b * LocalBackend ) Doctor ( ctx context . Context , logf logger . Logf ) {
var checks [ ] doctor . Check
checks = append ( checks , routetable . Check { } )
// TODO(andrew): more
numChecks := len ( checks )
checks = append ( checks , doctor . CheckFunc ( "numchecks" , func ( _ context . Context , log logger . Logf ) error {
log ( "%d checks" , numChecks )
return nil
} ) )
doctor . RunChecks ( ctx , logf , checks ... )
}
2022-11-07 21:04:10 +00:00
// SetDevStateStore updates the LocalBackend's state storage to the provided values.
//
// It's meant only for development.
func ( b * LocalBackend ) SetDevStateStore ( key , value string ) error {
if b . store == nil {
return errors . New ( "no state store" )
}
err := b . store . WriteState ( ipn . StateKey ( key ) , [ ] byte ( value ) )
b . logf ( "SetDevStateStore(%q, %q) = %v" , key , value , err )
2022-11-07 23:32:53 +00:00
if err != nil {
return err
}
b . mu . Lock ( )
defer b . mu . Unlock ( )
2022-11-09 05:58:10 +00:00
b . setTCPPortsInterceptedFromNetmapAndPrefsLocked ( b . pm . CurrentPrefs ( ) )
2022-11-07 23:32:53 +00:00
return nil
}
// ShouldInterceptTCPPort reports whether the given TCP port number to a
// Tailscale IP (not a subnet router, service IP, etc) should be intercepted by
// Tailscaled and handled in-process.
func ( b * LocalBackend ) ShouldInterceptTCPPort ( port uint16 ) bool {
return b . shouldInterceptTCPPortAtomic . Load ( ) ( port )
}
2022-11-09 05:58:10 +00:00
// SwitchProfile switches to the profile with the given id.
// It will restart the backend on success.
// If the profile is not known, it returns an errProfileNotFound.
func ( b * LocalBackend ) SwitchProfile ( profile ipn . ProfileID ) error {
if b . CurrentProfile ( ) . ID == profile {
return nil
}
b . mu . Lock ( )
if err := b . pm . SwitchProfile ( profile ) ; err != nil {
b . mu . Unlock ( )
return err
}
return b . resetForProfileChangeLockedOnEntry ( )
}
// resetForProfileChangeLockedOnEntry resets the backend for a profile change.
func ( b * LocalBackend ) resetForProfileChangeLockedOnEntry ( ) error {
b . setNetMapLocked ( nil ) // Reset netmap.
// Reset the NetworkMap in the engine
b . e . SetNetworkMap ( new ( netmap . NetworkMap ) )
b . enterStateLockedOnEntry ( ipn . NoState ) // Reset state.
return b . Start ( ipn . Options { } )
}
// DeleteProfile deletes a profile with the given ID.
// If the profile is not known, it is a no-op.
func ( b * LocalBackend ) DeleteProfile ( p ipn . ProfileID ) error {
b . mu . Lock ( )
defer b . mu . Unlock ( )
needToRestart := b . pm . CurrentProfile ( ) . ID == p
if err := b . pm . DeleteProfile ( p ) ; err != nil {
if err == errProfileNotFound {
return nil
}
return err
}
if ! needToRestart {
return nil
}
return b . resetForProfileChangeLockedOnEntry ( )
}
// CurrentProfile returns the current LoginProfile.
// The value may be zero if the profile is not persisted.
func ( b * LocalBackend ) CurrentProfile ( ) ipn . LoginProfile {
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . pm . CurrentProfile ( )
}
// NewProfile creates and switches to the new profile.
func ( b * LocalBackend ) NewProfile ( ) error {
b . mu . Lock ( )
b . pm . NewProfile ( )
return b . resetForProfileChangeLockedOnEntry ( )
}
// ListProfiles returns a list of all LoginProfiles.
func ( b * LocalBackend ) ListProfiles ( ) [ ] ipn . LoginProfile {
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . pm . Profiles ( )
}
// CurrentUser returns the current server mode user ID. It is only non-empty on
// Windows where we have a multi-user system.
func ( b * LocalBackend ) CurrentUser ( ) string {
b . mu . Lock ( )
defer b . mu . Unlock ( )
return b . pm . CurrentUser ( )
}