direct.go 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package controlclient
  4. import (
  5. "bufio"
  6. "bytes"
  7. "context"
  8. "crypto/ed25519"
  9. "encoding/base64"
  10. "encoding/binary"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "log"
  16. "net/http"
  17. "net/http/httptest"
  18. "net/netip"
  19. "net/url"
  20. "os"
  21. "reflect"
  22. "runtime"
  23. "slices"
  24. "strings"
  25. "sync"
  26. "time"
  27. "go4.org/mem"
  28. "tailscale.com/control/controlknobs"
  29. "tailscale.com/envknob"
  30. "tailscale.com/health"
  31. "tailscale.com/hostinfo"
  32. "tailscale.com/ipn/ipnstate"
  33. "tailscale.com/logtail"
  34. "tailscale.com/net/dnscache"
  35. "tailscale.com/net/dnsfallback"
  36. "tailscale.com/net/interfaces"
  37. "tailscale.com/net/netmon"
  38. "tailscale.com/net/netutil"
  39. "tailscale.com/net/tlsdial"
  40. "tailscale.com/net/tsdial"
  41. "tailscale.com/net/tshttpproxy"
  42. "tailscale.com/smallzstd"
  43. "tailscale.com/tailcfg"
  44. "tailscale.com/tka"
  45. "tailscale.com/tstime"
  46. "tailscale.com/types/key"
  47. "tailscale.com/types/logger"
  48. "tailscale.com/types/netmap"
  49. "tailscale.com/types/persist"
  50. "tailscale.com/types/ptr"
  51. "tailscale.com/types/tkatype"
  52. "tailscale.com/util/clientmetric"
  53. "tailscale.com/util/multierr"
  54. "tailscale.com/util/singleflight"
  55. "tailscale.com/util/syspolicy"
  56. "tailscale.com/util/systemd"
  57. )
  58. // Direct is the client that connects to a tailcontrol server for a node.
  59. type Direct struct {
  60. httpc *http.Client // HTTP client used to talk to tailcontrol
  61. dialer *tsdial.Dialer
  62. dnsCache *dnscache.Resolver
  63. controlKnobs *controlknobs.Knobs // always non-nil
  64. serverURL string // URL of the tailcontrol server
  65. clock tstime.Clock
  66. logf logger.Logf
  67. netMon *netmon.Monitor // or nil
  68. discoPubKey key.DiscoPublic
  69. getMachinePrivKey func() (key.MachinePrivate, error)
  70. debugFlags []string
  71. skipIPForwardingCheck bool
  72. pinger Pinger
  73. popBrowser func(url string) // or nil
  74. c2nHandler http.Handler // or nil
  75. onClientVersion func(*tailcfg.ClientVersion) // or nil
  76. onControlTime func(time.Time) // or nil
  77. onTailnetDefaultAutoUpdate func(bool) // or nil
  78. dialPlan ControlDialPlanner // can be nil
  79. mu sync.Mutex // mutex guards the following fields
  80. serverKey key.MachinePublic // original ("legacy") nacl crypto_box-based public key
  81. serverNoiseKey key.MachinePublic
  82. sfGroup singleflight.Group[struct{}, *NoiseClient] // protects noiseClient creation.
  83. noiseClient *NoiseClient
  84. persist persist.PersistView
  85. authKey string
  86. tryingNewKey key.NodePrivate
  87. expiry time.Time // or zero value if none/unknown
  88. hostinfo *tailcfg.Hostinfo // always non-nil
  89. netinfo *tailcfg.NetInfo
  90. endpoints []tailcfg.Endpoint
  91. tkaHead string
  92. lastPingURL string // last PingRequest.URL received, for dup suppression
  93. }
  94. // Observer is implemented by users of the control client (such as LocalBackend)
  95. // to get notified of changes in the control client's status.
  96. type Observer interface {
  97. // SetControlClientStatus is called when the client has a new status to
  98. // report. The Client is provided to allow the Observer to track which
  99. // Client is reporting the status, allowing it to ignore stale status
  100. // reports from previous Clients.
  101. SetControlClientStatus(Client, Status)
  102. }
  103. type Options struct {
  104. Persist persist.Persist // initial persistent data
  105. GetMachinePrivateKey func() (key.MachinePrivate, error) // returns the machine key to use
  106. ServerURL string // URL of the tailcontrol server
  107. AuthKey string // optional node auth key for auto registration
  108. Clock tstime.Clock
  109. Hostinfo *tailcfg.Hostinfo // non-nil passes ownership, nil means to use default using os.Hostname, etc
  110. DiscoPublicKey key.DiscoPublic
  111. Logf logger.Logf
  112. HTTPTestClient *http.Client // optional HTTP client to use (for tests only)
  113. NoiseTestClient *http.Client // optional HTTP client to use for noise RPCs (tests only)
  114. DebugFlags []string // debug settings to send to control
  115. NetMon *netmon.Monitor // optional network monitor
  116. PopBrowserURL func(url string) // optional func to open browser
  117. OnClientVersion func(*tailcfg.ClientVersion) // optional func to inform GUI of client version status
  118. OnControlTime func(time.Time) // optional func to notify callers of new time from control
  119. OnTailnetDefaultAutoUpdate func(bool) // optional func to inform GUI of default auto-update setting for the tailnet
  120. Dialer *tsdial.Dialer // non-nil
  121. C2NHandler http.Handler // or nil
  122. ControlKnobs *controlknobs.Knobs // or nil to ignore
  123. // Observer is called when there's a change in status to report
  124. // from the control client.
  125. Observer Observer
  126. // SkipIPForwardingCheck declares that the host's IP
  127. // forwarding works and should not be double-checked by the
  128. // controlclient package.
  129. SkipIPForwardingCheck bool
  130. // Pinger optionally specifies the Pinger to use to satisfy
  131. // MapResponse.PingRequest queries from the control plane.
  132. // If nil, PingRequest queries are not answered.
  133. Pinger Pinger
  134. // DialPlan contains and stores a previous dial plan that we received
  135. // from the control server; if nil, we fall back to using DNS.
  136. //
  137. // If we receive a new DialPlan from the server, this value will be
  138. // updated.
  139. DialPlan ControlDialPlanner
  140. }
  141. // ControlDialPlanner is the interface optionally supplied when creating a
  142. // control client to control exactly how TCP connections to the control plane
  143. // are dialed.
  144. //
  145. // It is usually implemented by an atomic.Pointer.
  146. type ControlDialPlanner interface {
  147. // Load returns the current plan for how to connect to control.
  148. //
  149. // The returned plan can be nil. If so, connections should be made by
  150. // resolving the control URL using DNS.
  151. Load() *tailcfg.ControlDialPlan
  152. // Store updates the dial plan with new directions from the control
  153. // server.
  154. //
  155. // The dial plan can span multiple connections to the control server.
  156. // That is, a dial plan received when connected over Wi-Fi is still
  157. // valid for a subsequent connection over LTE after a network switch.
  158. Store(*tailcfg.ControlDialPlan)
  159. }
  160. // Pinger is the LocalBackend.Ping method.
  161. type Pinger interface {
  162. // Ping is a request to do a ping with the peer handling the given IP.
  163. Ping(ctx context.Context, ip netip.Addr, pingType tailcfg.PingType, size int) (*ipnstate.PingResult, error)
  164. }
  165. type Decompressor interface {
  166. DecodeAll(input, dst []byte) ([]byte, error)
  167. Close()
  168. }
  169. // NetmapUpdater is the interface needed by the controlclient to enact change in
  170. // the world as a function of updates received from the network.
  171. type NetmapUpdater interface {
  172. UpdateFullNetmap(*netmap.NetworkMap)
  173. // TODO(bradfitz): add methods to do fine-grained updates, mutating just
  174. // parts of peers, without implementations of NetmapUpdater needing to do
  175. // the diff themselves between the previous full & next full network maps.
  176. }
  177. // NetmapDeltaUpdater is an optional interface that can be implemented by
  178. // NetmapUpdater implementations to receive delta updates from the controlclient
  179. // rather than just full updates.
  180. type NetmapDeltaUpdater interface {
  181. // UpdateNetmapDelta is called with discrete changes to the network map.
  182. //
  183. // The ok result is whether the implementation was able to apply the
  184. // mutations. It might return false if its internal state doesn't
  185. // support applying them or a NetmapUpdater it's wrapping doesn't
  186. // implement the NetmapDeltaUpdater optional method.
  187. UpdateNetmapDelta([]netmap.NodeMutation) (ok bool)
  188. }
  189. // NewDirect returns a new Direct client.
  190. func NewDirect(opts Options) (*Direct, error) {
  191. if opts.ServerURL == "" {
  192. return nil, errors.New("controlclient.New: no server URL specified")
  193. }
  194. if opts.GetMachinePrivateKey == nil {
  195. return nil, errors.New("controlclient.New: no GetMachinePrivateKey specified")
  196. }
  197. if opts.ControlKnobs == nil {
  198. opts.ControlKnobs = &controlknobs.Knobs{}
  199. }
  200. opts.ServerURL = strings.TrimRight(opts.ServerURL, "/")
  201. serverURL, err := url.Parse(opts.ServerURL)
  202. if err != nil {
  203. return nil, err
  204. }
  205. if opts.Clock == nil {
  206. opts.Clock = tstime.StdClock{}
  207. }
  208. if opts.Logf == nil {
  209. // TODO(apenwarr): remove this default and fail instead.
  210. // TODO(bradfitz): ... but then it shouldn't be in Options.
  211. opts.Logf = log.Printf
  212. }
  213. dnsCache := &dnscache.Resolver{
  214. Forward: dnscache.Get().Forward, // use default cache's forwarder
  215. UseLastGood: true,
  216. LookupIPFallback: dnsfallback.MakeLookupFunc(opts.Logf, opts.NetMon),
  217. Logf: opts.Logf,
  218. NetMon: opts.NetMon,
  219. }
  220. httpc := opts.HTTPTestClient
  221. if httpc == nil && runtime.GOOS == "js" {
  222. // In js/wasm, net/http.Transport (as of Go 1.18) will
  223. // only use the browser's Fetch API if you're using
  224. // the DefaultClient (or a client without dial hooks
  225. // etc set).
  226. httpc = http.DefaultClient
  227. }
  228. if httpc == nil {
  229. tr := http.DefaultTransport.(*http.Transport).Clone()
  230. tr.Proxy = tshttpproxy.ProxyFromEnvironment
  231. tshttpproxy.SetTransportGetProxyConnectHeader(tr)
  232. tr.TLSClientConfig = tlsdial.Config(serverURL.Hostname(), tr.TLSClientConfig)
  233. tr.DialContext = dnscache.Dialer(opts.Dialer.SystemDial, dnsCache)
  234. tr.DialTLSContext = dnscache.TLSDialer(opts.Dialer.SystemDial, dnsCache, tr.TLSClientConfig)
  235. tr.ForceAttemptHTTP2 = true
  236. // Disable implicit gzip compression; the various
  237. // handlers (register, map, set-dns, etc) do their own
  238. // zstd compression per naclbox.
  239. tr.DisableCompression = true
  240. httpc = &http.Client{Transport: tr}
  241. }
  242. c := &Direct{
  243. httpc: httpc,
  244. controlKnobs: opts.ControlKnobs,
  245. getMachinePrivKey: opts.GetMachinePrivateKey,
  246. serverURL: opts.ServerURL,
  247. clock: opts.Clock,
  248. logf: opts.Logf,
  249. persist: opts.Persist.View(),
  250. authKey: opts.AuthKey,
  251. discoPubKey: opts.DiscoPublicKey,
  252. debugFlags: opts.DebugFlags,
  253. netMon: opts.NetMon,
  254. skipIPForwardingCheck: opts.SkipIPForwardingCheck,
  255. pinger: opts.Pinger,
  256. popBrowser: opts.PopBrowserURL,
  257. onClientVersion: opts.OnClientVersion,
  258. onTailnetDefaultAutoUpdate: opts.OnTailnetDefaultAutoUpdate,
  259. onControlTime: opts.OnControlTime,
  260. c2nHandler: opts.C2NHandler,
  261. dialer: opts.Dialer,
  262. dnsCache: dnsCache,
  263. dialPlan: opts.DialPlan,
  264. }
  265. if opts.Hostinfo == nil {
  266. c.SetHostinfo(hostinfo.New())
  267. } else {
  268. c.SetHostinfo(opts.Hostinfo)
  269. if ni := opts.Hostinfo.NetInfo; ni != nil {
  270. c.SetNetInfo(ni)
  271. }
  272. }
  273. if opts.NoiseTestClient != nil {
  274. c.noiseClient = &NoiseClient{
  275. Client: opts.NoiseTestClient,
  276. }
  277. c.serverNoiseKey = key.NewMachine().Public() // prevent early error before hitting test client
  278. }
  279. return c, nil
  280. }
  281. // Close closes the underlying Noise connection(s).
  282. func (c *Direct) Close() error {
  283. c.mu.Lock()
  284. defer c.mu.Unlock()
  285. if c.noiseClient != nil {
  286. if err := c.noiseClient.Close(); err != nil {
  287. return err
  288. }
  289. }
  290. c.noiseClient = nil
  291. return nil
  292. }
  293. // SetHostinfo clones the provided Hostinfo and remembers it for the
  294. // next update. It reports whether the Hostinfo has changed.
  295. func (c *Direct) SetHostinfo(hi *tailcfg.Hostinfo) bool {
  296. if hi == nil {
  297. panic("nil Hostinfo")
  298. }
  299. hi = ptr.To(*hi)
  300. hi.NetInfo = nil
  301. c.mu.Lock()
  302. defer c.mu.Unlock()
  303. if hi.Equal(c.hostinfo) {
  304. return false
  305. }
  306. c.hostinfo = hi.Clone()
  307. j, _ := json.Marshal(c.hostinfo)
  308. c.logf("[v1] HostInfo: %s", j)
  309. return true
  310. }
  311. // SetNetInfo clones the provided NetInfo and remembers it for the
  312. // next update. It reports whether the NetInfo has changed.
  313. func (c *Direct) SetNetInfo(ni *tailcfg.NetInfo) bool {
  314. if ni == nil {
  315. panic("nil NetInfo")
  316. }
  317. c.mu.Lock()
  318. defer c.mu.Unlock()
  319. if reflect.DeepEqual(ni, c.netinfo) {
  320. return false
  321. }
  322. c.netinfo = ni.Clone()
  323. c.logf("NetInfo: %v", ni)
  324. return true
  325. }
  326. // SetNetInfo stores a new TKA head value for next update.
  327. // It reports whether the TKA head changed.
  328. func (c *Direct) SetTKAHead(tkaHead string) bool {
  329. c.mu.Lock()
  330. defer c.mu.Unlock()
  331. if tkaHead == c.tkaHead {
  332. return false
  333. }
  334. c.tkaHead = tkaHead
  335. c.logf("tkaHead: %v", tkaHead)
  336. return true
  337. }
  338. func (c *Direct) GetPersist() persist.PersistView {
  339. c.mu.Lock()
  340. defer c.mu.Unlock()
  341. return c.persist
  342. }
  343. func (c *Direct) TryLogout(ctx context.Context) error {
  344. c.logf("[v1] direct.TryLogout()")
  345. mustRegen, newURL, _, err := c.doLogin(ctx, loginOpt{Logout: true})
  346. c.logf("[v1] TryLogout control response: mustRegen=%v, newURL=%v, err=%v", mustRegen, newURL, err)
  347. c.mu.Lock()
  348. c.persist = new(persist.Persist).View()
  349. c.mu.Unlock()
  350. return err
  351. }
  352. func (c *Direct) TryLogin(ctx context.Context, t *tailcfg.Oauth2Token, flags LoginFlags) (url string, err error) {
  353. c.logf("[v1] direct.TryLogin(token=%v, flags=%v)", t != nil, flags)
  354. return c.doLoginOrRegen(ctx, loginOpt{Token: t, Flags: flags})
  355. }
  356. // WaitLoginURL sits in a long poll waiting for the user to authenticate at url.
  357. //
  358. // On success, newURL and err will both be nil.
  359. func (c *Direct) WaitLoginURL(ctx context.Context, url string) (newURL string, err error) {
  360. c.logf("[v1] direct.WaitLoginURL")
  361. return c.doLoginOrRegen(ctx, loginOpt{URL: url})
  362. }
  363. func (c *Direct) doLoginOrRegen(ctx context.Context, opt loginOpt) (newURL string, err error) {
  364. mustRegen, url, oldNodeKeySignature, err := c.doLogin(ctx, opt)
  365. if err != nil {
  366. return url, err
  367. }
  368. if mustRegen {
  369. opt.Regen = true
  370. opt.OldNodeKeySignature = oldNodeKeySignature
  371. _, url, _, err = c.doLogin(ctx, opt)
  372. }
  373. return url, err
  374. }
  375. // SetExpirySooner attempts to shorten the expiry to the specified time.
  376. func (c *Direct) SetExpirySooner(ctx context.Context, expiry time.Time) error {
  377. c.logf("[v1] direct.SetExpirySooner()")
  378. newURL, err := c.doLoginOrRegen(ctx, loginOpt{Expiry: &expiry})
  379. c.logf("[v1] SetExpirySooner control response: newURL=%v, err=%v", newURL, err)
  380. return err
  381. }
  382. type loginOpt struct {
  383. Token *tailcfg.Oauth2Token
  384. Flags LoginFlags
  385. Regen bool // generate a new nodekey, can be overridden in doLogin
  386. URL string
  387. Logout bool // set the expiry to the far past, expiring the node
  388. // Expiry, if non-nil, attempts to set the node expiry to the
  389. // specified time and cannot be used to extend the expiry.
  390. // It is ignored if Logout is set since Logout works by setting a
  391. // expiry time in the far past.
  392. Expiry *time.Time
  393. // OldNodeKeySignature indicates the former NodeKeySignature
  394. // that must be resigned for the new node-key.
  395. OldNodeKeySignature tkatype.MarshaledSignature
  396. }
  397. // httpClient provides a common interface for the noiseClient and
  398. // the NaCl box http.Client.
  399. type httpClient interface {
  400. Do(req *http.Request) (*http.Response, error)
  401. }
  402. // hostInfoLocked returns a Clone of c.hostinfo and c.netinfo.
  403. // It must only be called with c.mu held.
  404. func (c *Direct) hostInfoLocked() *tailcfg.Hostinfo {
  405. hi := c.hostinfo.Clone()
  406. hi.NetInfo = c.netinfo.Clone()
  407. return hi
  408. }
  409. func (c *Direct) doLogin(ctx context.Context, opt loginOpt) (mustRegen bool, newURL string, nks tkatype.MarshaledSignature, err error) {
  410. c.mu.Lock()
  411. persist := c.persist.AsStruct()
  412. tryingNewKey := c.tryingNewKey
  413. serverKey := c.serverKey
  414. serverNoiseKey := c.serverNoiseKey
  415. authKey, isWrapped, wrappedSig, wrappedKey := decodeWrappedAuthkey(c.authKey, c.logf)
  416. hi := c.hostInfoLocked()
  417. backendLogID := hi.BackendLogID
  418. expired := !c.expiry.IsZero() && c.expiry.Before(c.clock.Now())
  419. c.mu.Unlock()
  420. machinePrivKey, err := c.getMachinePrivKey()
  421. if err != nil {
  422. return false, "", nil, fmt.Errorf("getMachinePrivKey: %w", err)
  423. }
  424. if machinePrivKey.IsZero() {
  425. return false, "", nil, errors.New("getMachinePrivKey returned zero key")
  426. }
  427. regen := opt.Regen
  428. if opt.Logout {
  429. c.logf("logging out...")
  430. } else {
  431. if expired {
  432. c.logf("Old key expired -> regen=true")
  433. systemd.Status("key expired; run 'tailscale up' to authenticate")
  434. regen = true
  435. }
  436. if (opt.Flags & LoginInteractive) != 0 {
  437. c.logf("LoginInteractive -> regen=true")
  438. regen = true
  439. }
  440. }
  441. c.logf("doLogin(regen=%v, hasUrl=%v)", regen, opt.URL != "")
  442. if serverKey.IsZero() {
  443. keys, err := loadServerPubKeys(ctx, c.httpc, c.serverURL)
  444. if err != nil {
  445. return regen, opt.URL, nil, err
  446. }
  447. c.logf("control server key from %s: ts2021=%s, legacy=%v", c.serverURL, keys.PublicKey.ShortString(), keys.LegacyPublicKey.ShortString())
  448. c.mu.Lock()
  449. c.serverKey = keys.LegacyPublicKey
  450. c.serverNoiseKey = keys.PublicKey
  451. c.mu.Unlock()
  452. serverKey = keys.LegacyPublicKey
  453. serverNoiseKey = keys.PublicKey
  454. // For servers supporting the Noise transport,
  455. // proactively shut down our TLS TCP connection.
  456. // We're not going to need it and it's nicer to the
  457. // server.
  458. if !serverNoiseKey.IsZero() {
  459. c.httpc.CloseIdleConnections()
  460. }
  461. }
  462. var oldNodeKey key.NodePublic
  463. switch {
  464. case opt.Logout:
  465. tryingNewKey = persist.PrivateNodeKey
  466. case opt.URL != "":
  467. // Nothing.
  468. case regen || persist.PrivateNodeKey.IsZero():
  469. c.logf("Generating a new nodekey.")
  470. persist.OldPrivateNodeKey = persist.PrivateNodeKey
  471. tryingNewKey = key.NewNode()
  472. default:
  473. // Try refreshing the current key first
  474. tryingNewKey = persist.PrivateNodeKey
  475. }
  476. if !persist.OldPrivateNodeKey.IsZero() {
  477. oldNodeKey = persist.OldPrivateNodeKey.Public()
  478. }
  479. if persist.NetworkLockKey.IsZero() {
  480. persist.NetworkLockKey = key.NewNLPrivate()
  481. }
  482. nlPub := persist.NetworkLockKey.Public()
  483. if tryingNewKey.IsZero() {
  484. if opt.Logout {
  485. return false, "", nil, errors.New("no nodekey to log out")
  486. }
  487. log.Fatalf("tryingNewKey is empty, give up")
  488. }
  489. var nodeKeySignature tkatype.MarshaledSignature
  490. if !oldNodeKey.IsZero() && opt.OldNodeKeySignature != nil {
  491. if nodeKeySignature, err = resignNKS(persist.NetworkLockKey, tryingNewKey.Public(), opt.OldNodeKeySignature); err != nil {
  492. c.logf("Failed re-signing node-key signature: %v", err)
  493. }
  494. } else if isWrapped {
  495. // We were given a wrapped pre-auth key, which means that in addition
  496. // to being a regular pre-auth key there was a suffix with information to
  497. // generate a tailnet-lock signature.
  498. nk, err := tryingNewKey.Public().MarshalBinary()
  499. if err != nil {
  500. return false, "", nil, fmt.Errorf("marshalling node-key: %w", err)
  501. }
  502. sig := &tka.NodeKeySignature{
  503. SigKind: tka.SigRotation,
  504. Pubkey: nk,
  505. Nested: wrappedSig,
  506. }
  507. sigHash := sig.SigHash()
  508. sig.Signature = ed25519.Sign(wrappedKey, sigHash[:])
  509. nodeKeySignature = sig.Serialize()
  510. }
  511. if backendLogID == "" {
  512. err = errors.New("hostinfo: BackendLogID missing")
  513. return regen, opt.URL, nil, err
  514. }
  515. tailnet, err := syspolicy.GetString(syspolicy.Tailnet, "")
  516. if err != nil {
  517. c.logf("unable to provide Tailnet field in register request. err: %v", err)
  518. }
  519. now := c.clock.Now().Round(time.Second)
  520. request := tailcfg.RegisterRequest{
  521. Version: 1,
  522. OldNodeKey: oldNodeKey,
  523. NodeKey: tryingNewKey.Public(),
  524. NLKey: nlPub,
  525. Hostinfo: hi,
  526. Followup: opt.URL,
  527. Timestamp: &now,
  528. Ephemeral: (opt.Flags & LoginEphemeral) != 0,
  529. NodeKeySignature: nodeKeySignature,
  530. Tailnet: tailnet,
  531. }
  532. if opt.Logout {
  533. request.Expiry = time.Unix(123, 0) // far in the past
  534. } else if opt.Expiry != nil {
  535. request.Expiry = *opt.Expiry
  536. }
  537. c.logf("RegisterReq: onode=%v node=%v fup=%v nks=%v",
  538. request.OldNodeKey.ShortString(),
  539. request.NodeKey.ShortString(), opt.URL != "", len(nodeKeySignature) > 0)
  540. request.Auth.Oauth2Token = opt.Token
  541. request.Auth.Provider = persist.Provider
  542. request.Auth.LoginName = persist.UserProfile.LoginName
  543. request.Auth.AuthKey = authKey
  544. err = signRegisterRequest(&request, c.serverURL, c.serverKey, machinePrivKey.Public())
  545. if err != nil {
  546. // If signing failed, clear all related fields
  547. request.SignatureType = tailcfg.SignatureNone
  548. request.Timestamp = nil
  549. request.DeviceCert = nil
  550. request.Signature = nil
  551. // Don't log the common error types. Signatures are not usually enabled,
  552. // so these are expected.
  553. if !errors.Is(err, errCertificateNotConfigured) && !errors.Is(err, errNoCertStore) {
  554. c.logf("RegisterReq sign error: %v", err)
  555. }
  556. }
  557. if debugRegister() {
  558. j, _ := json.MarshalIndent(request, "", "\t")
  559. c.logf("RegisterRequest: %s", j)
  560. }
  561. // URL and httpc are protocol specific.
  562. var url string
  563. var httpc httpClient
  564. if serverNoiseKey.IsZero() {
  565. httpc = c.httpc
  566. url = fmt.Sprintf("%s/machine/%s", c.serverURL, machinePrivKey.Public().UntypedHexString())
  567. } else {
  568. request.Version = tailcfg.CurrentCapabilityVersion
  569. httpc, err = c.getNoiseClient()
  570. if err != nil {
  571. return regen, opt.URL, nil, fmt.Errorf("getNoiseClient: %w", err)
  572. }
  573. url = fmt.Sprintf("%s/machine/register", c.serverURL)
  574. url = strings.Replace(url, "http:", "https:", 1)
  575. }
  576. bodyData, err := encode(request, serverKey, serverNoiseKey, machinePrivKey)
  577. if err != nil {
  578. return regen, opt.URL, nil, err
  579. }
  580. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(bodyData))
  581. if err != nil {
  582. return regen, opt.URL, nil, err
  583. }
  584. addLBHeader(req, request.OldNodeKey)
  585. addLBHeader(req, request.NodeKey)
  586. res, err := httpc.Do(req)
  587. if err != nil {
  588. return regen, opt.URL, nil, fmt.Errorf("register request: %w", err)
  589. }
  590. if res.StatusCode != 200 {
  591. msg, _ := io.ReadAll(res.Body)
  592. res.Body.Close()
  593. return regen, opt.URL, nil, fmt.Errorf("register request: http %d: %.200s",
  594. res.StatusCode, strings.TrimSpace(string(msg)))
  595. }
  596. resp := tailcfg.RegisterResponse{}
  597. if err := decode(res, &resp, serverKey, serverNoiseKey, machinePrivKey); err != nil {
  598. c.logf("error decoding RegisterResponse with server key %s and machine key %s: %v", serverKey, machinePrivKey.Public(), err)
  599. return regen, opt.URL, nil, fmt.Errorf("register request: %v", err)
  600. }
  601. if debugRegister() {
  602. j, _ := json.MarshalIndent(resp, "", "\t")
  603. c.logf("RegisterResponse: %s", j)
  604. }
  605. // Log without PII:
  606. c.logf("RegisterReq: got response; nodeKeyExpired=%v, machineAuthorized=%v; authURL=%v",
  607. resp.NodeKeyExpired, resp.MachineAuthorized, resp.AuthURL != "")
  608. if resp.Error != "" {
  609. return false, "", nil, UserVisibleError(resp.Error)
  610. }
  611. if len(resp.NodeKeySignature) > 0 {
  612. return true, "", resp.NodeKeySignature, nil
  613. }
  614. if resp.NodeKeyExpired {
  615. if regen {
  616. return true, "", nil, fmt.Errorf("weird: regen=true but server says NodeKeyExpired: %v", request.NodeKey)
  617. }
  618. c.logf("server reports new node key %v has expired",
  619. request.NodeKey.ShortString())
  620. return true, "", nil, nil
  621. }
  622. if resp.Login.Provider != "" {
  623. persist.Provider = resp.Login.Provider
  624. }
  625. persist.UserProfile = tailcfg.UserProfile{
  626. ID: resp.User.ID,
  627. DisplayName: resp.Login.DisplayName,
  628. ProfilePicURL: resp.Login.ProfilePicURL,
  629. LoginName: resp.Login.LoginName,
  630. }
  631. // TODO(crawshaw): RegisterResponse should be able to mechanically
  632. // communicate some extra instructions from the server:
  633. // - new node key required
  634. // - machine key no longer supported
  635. // - user is disabled
  636. if resp.AuthURL != "" {
  637. c.logf("AuthURL is %v", resp.AuthURL)
  638. } else {
  639. c.logf("[v1] No AuthURL")
  640. }
  641. c.mu.Lock()
  642. if resp.AuthURL == "" {
  643. // key rotation is complete
  644. persist.PrivateNodeKey = tryingNewKey
  645. } else {
  646. // save it for the retry-with-URL
  647. c.tryingNewKey = tryingNewKey
  648. }
  649. c.persist = persist.View()
  650. c.mu.Unlock()
  651. if ctx.Err() != nil {
  652. return regen, "", nil, ctx.Err()
  653. }
  654. return false, resp.AuthURL, nil, nil
  655. }
  656. // resignNKS re-signs a node-key signature for a new node-key.
  657. //
  658. // This only matters on network-locked tailnets, because node-key signatures are
  659. // how other nodes know that a node-key is authentic. When the node-key is
  660. // rotated then the existing signature becomes invalid, so this function is
  661. // responsible for generating a new wrapping signature to certify the new node-key.
  662. //
  663. // The signature itself is a SigRotation signature, which embeds the old signature
  664. // and certifies the new node-key as a replacement for the old by signing the new
  665. // signature with RotationPubkey (which is the node's own network-lock key).
  666. func resignNKS(priv key.NLPrivate, nodeKey key.NodePublic, oldNKS tkatype.MarshaledSignature) (tkatype.MarshaledSignature, error) {
  667. var oldSig tka.NodeKeySignature
  668. if err := oldSig.Unserialize(oldNKS); err != nil {
  669. return nil, fmt.Errorf("decoding NKS: %w", err)
  670. }
  671. nk, err := nodeKey.MarshalBinary()
  672. if err != nil {
  673. return nil, fmt.Errorf("marshalling node-key: %w", err)
  674. }
  675. if bytes.Equal(nk, oldSig.Pubkey) {
  676. // The old signature is valid for the node-key we are using, so just
  677. // use it verbatim.
  678. return oldNKS, nil
  679. }
  680. newSig := tka.NodeKeySignature{
  681. SigKind: tka.SigRotation,
  682. Pubkey: nk,
  683. Nested: &oldSig,
  684. }
  685. if newSig.Signature, err = priv.SignNKS(newSig.SigHash()); err != nil {
  686. return nil, fmt.Errorf("signing NKS: %w", err)
  687. }
  688. return newSig.Serialize(), nil
  689. }
  690. // newEndpoints acquires c.mu and sets the local port and endpoints and reports
  691. // whether they've changed.
  692. //
  693. // It does not retain the provided slice.
  694. func (c *Direct) newEndpoints(endpoints []tailcfg.Endpoint) (changed bool) {
  695. c.mu.Lock()
  696. defer c.mu.Unlock()
  697. // Nothing new?
  698. if slices.Equal(c.endpoints, endpoints) {
  699. return false // unchanged
  700. }
  701. c.logf("[v2] client.newEndpoints(%v)", endpoints)
  702. c.endpoints = slices.Clone(endpoints)
  703. return true // changed
  704. }
  705. // SetEndpoints updates the list of locally advertised endpoints.
  706. // It won't be replicated to the server until a *fresh* call to PollNetMap().
  707. // You don't need to restart PollNetMap if we return changed==false.
  708. func (c *Direct) SetEndpoints(endpoints []tailcfg.Endpoint) (changed bool) {
  709. // (no log message on function entry, because it clutters the logs
  710. // if endpoints haven't changed. newEndpoints() will log it.)
  711. return c.newEndpoints(endpoints)
  712. }
  713. // PollNetMap makes a /map request to download the network map, calling
  714. // NetmapUpdater on each update from the control plane.
  715. //
  716. // It always returns a non-nil error describing the reason for the failure or
  717. // why the request ended.
  718. func (c *Direct) PollNetMap(ctx context.Context, nu NetmapUpdater) error {
  719. return c.sendMapRequest(ctx, true, nu)
  720. }
  721. type rememberLastNetmapUpdater struct {
  722. last *netmap.NetworkMap
  723. }
  724. func (nu *rememberLastNetmapUpdater) UpdateFullNetmap(nm *netmap.NetworkMap) {
  725. nu.last = nm
  726. }
  727. // FetchNetMapForTest fetches the netmap once.
  728. func (c *Direct) FetchNetMapForTest(ctx context.Context) (*netmap.NetworkMap, error) {
  729. var nu rememberLastNetmapUpdater
  730. err := c.sendMapRequest(ctx, false, &nu)
  731. if err == nil && nu.last == nil {
  732. return nil, errors.New("[unexpected] sendMapRequest success without callback")
  733. }
  734. return nu.last, err
  735. }
  736. // SendUpdate makes a /map request to update the server of our latest state, but
  737. // does not fetch anything. It returns an error if the server did not return a
  738. // successful 200 OK response.
  739. func (c *Direct) SendUpdate(ctx context.Context) error {
  740. return c.sendMapRequest(ctx, false, nil)
  741. }
  742. // If we go more than watchdogTimeout without hearing from the server,
  743. // end the long poll. We should be receiving a keep alive ping
  744. // every minute.
  745. const watchdogTimeout = 120 * time.Second
  746. // sendMapRequest makes a /map request to download the network map, calling cb
  747. // with each new netmap. If isStreaming, it will poll forever and only returns
  748. // if the context expires or the server returns an error/closes the connection
  749. // and as such always returns a non-nil error.
  750. //
  751. // If nu is nil, OmitPeers will be set to true.
  752. func (c *Direct) sendMapRequest(ctx context.Context, isStreaming bool, nu NetmapUpdater) error {
  753. if isStreaming && nu == nil {
  754. panic("cb must be non-nil if isStreaming is true")
  755. }
  756. metricMapRequests.Add(1)
  757. metricMapRequestsActive.Add(1)
  758. defer metricMapRequestsActive.Add(-1)
  759. if isStreaming {
  760. metricMapRequestsPoll.Add(1)
  761. } else {
  762. metricMapRequestsLite.Add(1)
  763. }
  764. c.mu.Lock()
  765. persist := c.persist
  766. serverURL := c.serverURL
  767. serverKey := c.serverKey
  768. serverNoiseKey := c.serverNoiseKey
  769. hi := c.hostInfoLocked()
  770. backendLogID := hi.BackendLogID
  771. var epStrs []string
  772. var eps []netip.AddrPort
  773. var epTypes []tailcfg.EndpointType
  774. for _, ep := range c.endpoints {
  775. eps = append(eps, ep.Addr)
  776. epStrs = append(epStrs, ep.Addr.String())
  777. epTypes = append(epTypes, ep.Type)
  778. }
  779. c.mu.Unlock()
  780. machinePrivKey, err := c.getMachinePrivKey()
  781. if err != nil {
  782. return fmt.Errorf("getMachinePrivKey: %w", err)
  783. }
  784. if machinePrivKey.IsZero() {
  785. return errors.New("getMachinePrivKey returned zero key")
  786. }
  787. if persist.PrivateNodeKey().IsZero() {
  788. return errors.New("privateNodeKey is zero")
  789. }
  790. if backendLogID == "" {
  791. return errors.New("hostinfo: BackendLogID missing")
  792. }
  793. c.logf("[v1] PollNetMap: stream=%v ep=%v", isStreaming, epStrs)
  794. vlogf := logger.Discard
  795. if DevKnob.DumpNetMaps() {
  796. // TODO(bradfitz): update this to use "[v2]" prefix perhaps? but we don't
  797. // want to upload it always.
  798. vlogf = c.logf
  799. }
  800. nodeKey := persist.PublicNodeKey()
  801. request := &tailcfg.MapRequest{
  802. Version: tailcfg.CurrentCapabilityVersion,
  803. KeepAlive: true,
  804. NodeKey: nodeKey,
  805. DiscoKey: c.discoPubKey,
  806. Endpoints: eps,
  807. EndpointTypes: epTypes,
  808. Stream: isStreaming,
  809. Hostinfo: hi,
  810. DebugFlags: c.debugFlags,
  811. OmitPeers: nu == nil,
  812. TKAHead: c.tkaHead,
  813. }
  814. var extraDebugFlags []string
  815. if hi != nil && c.netMon != nil && !c.skipIPForwardingCheck &&
  816. ipForwardingBroken(hi.RoutableIPs, c.netMon.InterfaceState()) {
  817. extraDebugFlags = append(extraDebugFlags, "warn-ip-forwarding-off")
  818. }
  819. if health.RouterHealth() != nil {
  820. extraDebugFlags = append(extraDebugFlags, "warn-router-unhealthy")
  821. }
  822. extraDebugFlags = health.AppendWarnableDebugFlags(extraDebugFlags)
  823. if hostinfo.DisabledEtcAptSource() {
  824. extraDebugFlags = append(extraDebugFlags, "warn-etc-apt-source-disabled")
  825. }
  826. if len(extraDebugFlags) > 0 {
  827. old := request.DebugFlags
  828. request.DebugFlags = append(old[:len(old):len(old)], extraDebugFlags...)
  829. }
  830. request.Compress = "zstd"
  831. bodyData, err := encode(request, serverKey, serverNoiseKey, machinePrivKey)
  832. if err != nil {
  833. vlogf("netmap: encode: %v", err)
  834. return err
  835. }
  836. ctx, cancel := context.WithCancel(ctx)
  837. defer cancel()
  838. machinePubKey := machinePrivKey.Public()
  839. t0 := c.clock.Now()
  840. // Url and httpc are protocol specific.
  841. var url string
  842. var httpc httpClient
  843. if serverNoiseKey.IsZero() {
  844. httpc = c.httpc
  845. url = fmt.Sprintf("%s/machine/%s/map", serverURL, machinePubKey.UntypedHexString())
  846. } else {
  847. httpc, err = c.getNoiseClient()
  848. if err != nil {
  849. return fmt.Errorf("getNoiseClient: %w", err)
  850. }
  851. url = fmt.Sprintf("%s/machine/map", serverURL)
  852. url = strings.Replace(url, "http:", "https:", 1)
  853. }
  854. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(bodyData))
  855. if err != nil {
  856. return err
  857. }
  858. addLBHeader(req, nodeKey)
  859. res, err := httpc.Do(req)
  860. if err != nil {
  861. vlogf("netmap: Do: %v", err)
  862. return err
  863. }
  864. vlogf("netmap: Do = %v after %v", res.StatusCode, time.Since(t0).Round(time.Millisecond))
  865. if res.StatusCode != 200 {
  866. msg, _ := io.ReadAll(res.Body)
  867. res.Body.Close()
  868. return fmt.Errorf("initial fetch failed %d: %.200s",
  869. res.StatusCode, strings.TrimSpace(string(msg)))
  870. }
  871. defer res.Body.Close()
  872. health.NoteMapRequestHeard(request)
  873. if nu == nil {
  874. io.Copy(io.Discard, res.Body)
  875. return nil
  876. }
  877. sess := newMapSession(persist.PrivateNodeKey(), nu, c.controlKnobs)
  878. defer sess.Close()
  879. sess.cancel = cancel
  880. sess.logf = c.logf
  881. sess.vlogf = vlogf
  882. sess.altClock = c.clock
  883. sess.machinePubKey = machinePubKey
  884. sess.onDebug = c.handleDebugMessage
  885. sess.onSelfNodeChanged = func(nm *netmap.NetworkMap) {
  886. c.mu.Lock()
  887. defer c.mu.Unlock()
  888. // If we are the ones who last updated persist, then we can update it
  889. // again. Otherwise, we should not touch it. Also, it's only worth
  890. // change it if the Node info changed.
  891. if persist == c.persist {
  892. newPersist := persist.AsStruct()
  893. newPersist.NodeID = nm.SelfNode.StableID()
  894. newPersist.UserProfile = nm.UserProfiles[nm.User()]
  895. c.persist = newPersist.View()
  896. persist = c.persist
  897. }
  898. c.expiry = nm.Expiry
  899. }
  900. // Create a watchdog timer that breaks the connection if we don't receive a
  901. // MapResponse from the network at least once every two minutes. The
  902. // watchdog timer is stopped every time we receive a MapResponse (so it
  903. // doesn't run when we're processing a MapResponse message, including any
  904. // long-running requested operations like Debug.Sleep) and is reset whenever
  905. // we go back to blocking on network reads.
  906. watchdogTimer, watchdogTimedOut := c.clock.NewTimer(watchdogTimeout)
  907. defer watchdogTimer.Stop()
  908. go func() {
  909. select {
  910. case <-ctx.Done():
  911. vlogf("netmap: ending timeout goroutine")
  912. return
  913. case <-watchdogTimedOut:
  914. c.logf("map response long-poll timed out!")
  915. cancel()
  916. return
  917. }
  918. }()
  919. // gotNonKeepAliveMessage is whether we've yet received a MapResponse message without
  920. // KeepAlive set.
  921. var gotNonKeepAliveMessage bool
  922. // If allowStream, then the server will use an HTTP long poll to
  923. // return incremental results. There is always one response right
  924. // away, followed by a delay, and eventually others.
  925. // If !allowStream, it'll still send the first result in exactly
  926. // the same format before just closing the connection.
  927. // We can use this same read loop either way.
  928. var msg []byte
  929. for mapResIdx := 0; mapResIdx == 0 || isStreaming; mapResIdx++ {
  930. watchdogTimer.Reset(watchdogTimeout)
  931. vlogf("netmap: starting size read after %v (poll %v)", time.Since(t0).Round(time.Millisecond), mapResIdx)
  932. var siz [4]byte
  933. if _, err := io.ReadFull(res.Body, siz[:]); err != nil {
  934. vlogf("netmap: size read error after %v: %v", time.Since(t0).Round(time.Millisecond), err)
  935. return err
  936. }
  937. size := binary.LittleEndian.Uint32(siz[:])
  938. vlogf("netmap: read size %v after %v", size, time.Since(t0).Round(time.Millisecond))
  939. msg = append(msg[:0], make([]byte, size)...)
  940. if _, err := io.ReadFull(res.Body, msg); err != nil {
  941. vlogf("netmap: body read error: %v", err)
  942. return err
  943. }
  944. vlogf("netmap: read body after %v", time.Since(t0).Round(time.Millisecond))
  945. var resp tailcfg.MapResponse
  946. if err := c.decodeMsg(msg, &resp, machinePrivKey); err != nil {
  947. vlogf("netmap: decode error: %v", err)
  948. return err
  949. }
  950. watchdogTimer.Stop()
  951. metricMapResponseMessages.Add(1)
  952. if isStreaming {
  953. health.GotStreamedMapResponse()
  954. }
  955. if pr := resp.PingRequest; pr != nil && c.isUniquePingRequest(pr) {
  956. metricMapResponsePings.Add(1)
  957. go c.answerPing(pr)
  958. }
  959. if u := resp.PopBrowserURL; u != "" && u != sess.lastPopBrowserURL {
  960. sess.lastPopBrowserURL = u
  961. if c.popBrowser != nil {
  962. c.logf("netmap: control says to open URL %v; opening...", u)
  963. c.popBrowser(u)
  964. } else {
  965. c.logf("netmap: control says to open URL %v; no popBrowser func", u)
  966. }
  967. }
  968. if resp.ClientVersion != nil && c.onClientVersion != nil {
  969. c.onClientVersion(resp.ClientVersion)
  970. }
  971. if resp.ControlTime != nil && !resp.ControlTime.IsZero() {
  972. c.logf.JSON(1, "controltime", resp.ControlTime.UTC())
  973. if c.onControlTime != nil {
  974. c.onControlTime(*resp.ControlTime)
  975. }
  976. }
  977. if resp.KeepAlive {
  978. vlogf("netmap: got keep-alive")
  979. } else {
  980. vlogf("netmap: got new map")
  981. }
  982. if resp.ControlDialPlan != nil {
  983. if c.dialPlan != nil {
  984. c.logf("netmap: got new dial plan from control")
  985. c.dialPlan.Store(resp.ControlDialPlan)
  986. } else {
  987. c.logf("netmap: [unexpected] new dial plan; nowhere to store it")
  988. }
  989. }
  990. if resp.KeepAlive {
  991. metricMapResponseKeepAlives.Add(1)
  992. continue
  993. }
  994. if au, ok := resp.DefaultAutoUpdate.Get(); ok {
  995. if c.onTailnetDefaultAutoUpdate != nil {
  996. c.onTailnetDefaultAutoUpdate(au)
  997. }
  998. }
  999. metricMapResponseMap.Add(1)
  1000. if gotNonKeepAliveMessage {
  1001. // If we've already seen a non-keep-alive message, this is a delta update.
  1002. metricMapResponseMapDelta.Add(1)
  1003. } else if resp.Node == nil {
  1004. // The very first non-keep-alive message should have Node populated.
  1005. c.logf("initial MapResponse lacked Node")
  1006. return errors.New("initial MapResponse lacked node")
  1007. }
  1008. gotNonKeepAliveMessage = true
  1009. if err := sess.HandleNonKeepAliveMapResponse(ctx, &resp); err != nil {
  1010. return err
  1011. }
  1012. }
  1013. if ctx.Err() != nil {
  1014. return ctx.Err()
  1015. }
  1016. return nil
  1017. }
  1018. func (c *Direct) handleDebugMessage(ctx context.Context, debug *tailcfg.Debug) error {
  1019. if code := debug.Exit; code != nil {
  1020. c.logf("exiting process with status %v per controlplane", *code)
  1021. os.Exit(*code)
  1022. }
  1023. if debug.DisableLogTail {
  1024. logtail.Disable()
  1025. envknob.SetNoLogsNoSupport()
  1026. }
  1027. if sleep := time.Duration(debug.SleepSeconds * float64(time.Second)); sleep > 0 {
  1028. if err := sleepAsRequested(ctx, c.logf, sleep, c.clock); err != nil {
  1029. return err
  1030. }
  1031. }
  1032. return nil
  1033. }
  1034. // initDisplayNames mutates any tailcfg.Nodes in resp to populate their display names,
  1035. // calling InitDisplayNames on each.
  1036. //
  1037. // The magicDNSSuffix used is based on selfNode.
  1038. func initDisplayNames(selfNode tailcfg.NodeView, resp *tailcfg.MapResponse) {
  1039. if resp.Node == nil && len(resp.Peers) == 0 && len(resp.PeersChanged) == 0 {
  1040. // Fast path for a common case (delta updates). No need to compute
  1041. // magicDNSSuffix.
  1042. return
  1043. }
  1044. magicDNSSuffix := netmap.MagicDNSSuffixOfNodeName(selfNode.Name())
  1045. if resp.Node != nil {
  1046. resp.Node.InitDisplayNames(magicDNSSuffix)
  1047. }
  1048. for _, n := range resp.Peers {
  1049. n.InitDisplayNames(magicDNSSuffix)
  1050. }
  1051. for _, n := range resp.PeersChanged {
  1052. n.InitDisplayNames(magicDNSSuffix)
  1053. }
  1054. }
  1055. // decode JSON decodes the res.Body into v. If serverNoiseKey is not specified,
  1056. // it uses the serverKey and mkey to decode the message from the NaCl-crypto-box.
  1057. func decode(res *http.Response, v any, serverKey, serverNoiseKey key.MachinePublic, mkey key.MachinePrivate) error {
  1058. defer res.Body.Close()
  1059. msg, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
  1060. if err != nil {
  1061. return err
  1062. }
  1063. if res.StatusCode != 200 {
  1064. return fmt.Errorf("%d: %v", res.StatusCode, string(msg))
  1065. }
  1066. if !serverNoiseKey.IsZero() {
  1067. return json.Unmarshal(msg, v)
  1068. }
  1069. return decodeMsg(msg, v, serverKey, mkey)
  1070. }
  1071. var (
  1072. debugMap = envknob.RegisterBool("TS_DEBUG_MAP")
  1073. debugRegister = envknob.RegisterBool("TS_DEBUG_REGISTER")
  1074. )
  1075. var jsonEscapedZero = []byte(`\u0000`)
  1076. // decodeMsg is responsible for uncompressing msg and unmarshaling into v.
  1077. // If c.serverNoiseKey is not specified, it uses the c.serverKey and mkey
  1078. // to first the decrypt msg from the NaCl-crypto-box.
  1079. func (c *Direct) decodeMsg(msg []byte, v any, mkey key.MachinePrivate) error {
  1080. c.mu.Lock()
  1081. serverKey := c.serverKey
  1082. serverNoiseKey := c.serverNoiseKey
  1083. c.mu.Unlock()
  1084. var decrypted []byte
  1085. if serverNoiseKey.IsZero() {
  1086. var ok bool
  1087. decrypted, ok = mkey.OpenFrom(serverKey, msg)
  1088. if !ok {
  1089. return errors.New("cannot decrypt response")
  1090. }
  1091. } else {
  1092. decrypted = msg
  1093. }
  1094. decoder, err := smallzstd.NewDecoder(nil)
  1095. if err != nil {
  1096. return err
  1097. }
  1098. defer decoder.Close()
  1099. b, err := decoder.DecodeAll(decrypted, nil)
  1100. if err != nil {
  1101. return err
  1102. }
  1103. if debugMap() {
  1104. var buf bytes.Buffer
  1105. json.Indent(&buf, b, "", " ")
  1106. log.Printf("MapResponse: %s", buf.Bytes())
  1107. }
  1108. if bytes.Contains(b, jsonEscapedZero) {
  1109. log.Printf("[unexpected] zero byte in controlclient.Direct.decodeMsg into %T: %q", v, b)
  1110. }
  1111. if err := json.Unmarshal(b, v); err != nil {
  1112. return fmt.Errorf("response: %v", err)
  1113. }
  1114. return nil
  1115. }
  1116. func decodeMsg(msg []byte, v any, serverKey key.MachinePublic, machinePrivKey key.MachinePrivate) error {
  1117. decrypted, ok := machinePrivKey.OpenFrom(serverKey, msg)
  1118. if !ok {
  1119. return errors.New("cannot decrypt response")
  1120. }
  1121. if bytes.Contains(decrypted, jsonEscapedZero) {
  1122. log.Printf("[unexpected] zero byte in controlclient decodeMsg into %T: %q", v, decrypted)
  1123. }
  1124. if err := json.Unmarshal(decrypted, v); err != nil {
  1125. return fmt.Errorf("response: %v", err)
  1126. }
  1127. return nil
  1128. }
  1129. // encode JSON encodes v. If serverNoiseKey is not specified, it uses the serverKey and mkey to
  1130. // seal the message into a NaCl-crypto-box.
  1131. func encode(v any, serverKey, serverNoiseKey key.MachinePublic, mkey key.MachinePrivate) ([]byte, error) {
  1132. b, err := json.Marshal(v)
  1133. if err != nil {
  1134. return nil, err
  1135. }
  1136. if debugMap() {
  1137. if _, ok := v.(*tailcfg.MapRequest); ok {
  1138. log.Printf("MapRequest: %s", b)
  1139. }
  1140. }
  1141. if !serverNoiseKey.IsZero() {
  1142. return b, nil
  1143. }
  1144. return mkey.SealTo(serverKey, b), nil
  1145. }
  1146. func loadServerPubKeys(ctx context.Context, httpc *http.Client, serverURL string) (*tailcfg.OverTLSPublicKeyResponse, error) {
  1147. keyURL := fmt.Sprintf("%v/key?v=%d", serverURL, tailcfg.CurrentCapabilityVersion)
  1148. req, err := http.NewRequestWithContext(ctx, "GET", keyURL, nil)
  1149. if err != nil {
  1150. return nil, fmt.Errorf("create control key request: %v", err)
  1151. }
  1152. res, err := httpc.Do(req)
  1153. if err != nil {
  1154. return nil, fmt.Errorf("fetch control key: %v", err)
  1155. }
  1156. defer res.Body.Close()
  1157. b, err := io.ReadAll(io.LimitReader(res.Body, 64<<10))
  1158. if err != nil {
  1159. return nil, fmt.Errorf("fetch control key response: %v", err)
  1160. }
  1161. if res.StatusCode != 200 {
  1162. return nil, fmt.Errorf("fetch control key: %d", res.StatusCode)
  1163. }
  1164. var out tailcfg.OverTLSPublicKeyResponse
  1165. jsonErr := json.Unmarshal(b, &out)
  1166. if jsonErr == nil {
  1167. return &out, nil
  1168. }
  1169. // Some old control servers might not be updated to send the new format.
  1170. // Accept the old pre-JSON format too.
  1171. out = tailcfg.OverTLSPublicKeyResponse{}
  1172. k, err := key.ParseMachinePublicUntyped(mem.B(b))
  1173. if err != nil {
  1174. return nil, multierr.New(jsonErr, err)
  1175. }
  1176. out.LegacyPublicKey = k
  1177. return &out, nil
  1178. }
  1179. // DevKnob contains temporary internal-only debug knobs.
  1180. // They're unexported to not draw attention to them.
  1181. var DevKnob = initDevKnob()
  1182. type devKnobs struct {
  1183. DumpNetMaps func() bool
  1184. ForceProxyDNS func() bool
  1185. StripEndpoints func() bool // strip endpoints from control (only use disco messages)
  1186. StripCaps func() bool // strip all local node's control-provided capabilities
  1187. }
  1188. func initDevKnob() devKnobs {
  1189. return devKnobs{
  1190. DumpNetMaps: envknob.RegisterBool("TS_DEBUG_NETMAP"),
  1191. ForceProxyDNS: envknob.RegisterBool("TS_DEBUG_PROXY_DNS"),
  1192. StripEndpoints: envknob.RegisterBool("TS_DEBUG_STRIP_ENDPOINTS"),
  1193. StripCaps: envknob.RegisterBool("TS_DEBUG_STRIP_CAPS"),
  1194. }
  1195. }
  1196. var clock tstime.Clock = tstime.StdClock{}
  1197. // ipForwardingBroken reports whether the system's IP forwarding is disabled
  1198. // and will definitely not work for the routes provided.
  1199. //
  1200. // It should not return false positives.
  1201. //
  1202. // TODO(bradfitz): Change controlclient.Options.SkipIPForwardingCheck into a
  1203. // func([]netip.Prefix) error signature instead.
  1204. func ipForwardingBroken(routes []netip.Prefix, state *interfaces.State) bool {
  1205. warn, err := netutil.CheckIPForwarding(routes, state)
  1206. if err != nil {
  1207. // Oh well, we tried. This is just for debugging.
  1208. // We don't want false positives.
  1209. // TODO: maybe we want a different warning for inability to check?
  1210. return false
  1211. }
  1212. return warn != nil
  1213. }
  1214. // isUniquePingRequest reports whether pr contains a new PingRequest.URL
  1215. // not already handled, noting its value when returning true.
  1216. func (c *Direct) isUniquePingRequest(pr *tailcfg.PingRequest) bool {
  1217. if pr == nil || pr.URL == "" {
  1218. // Bogus.
  1219. return false
  1220. }
  1221. c.mu.Lock()
  1222. defer c.mu.Unlock()
  1223. if pr.URL == c.lastPingURL {
  1224. return false
  1225. }
  1226. c.lastPingURL = pr.URL
  1227. return true
  1228. }
  1229. func (c *Direct) answerPing(pr *tailcfg.PingRequest) {
  1230. httpc := c.httpc
  1231. useNoise := pr.URLIsNoise || pr.Types == "c2n" && c.noiseConfigured()
  1232. if useNoise {
  1233. nc, err := c.getNoiseClient()
  1234. if err != nil {
  1235. c.logf("failed to get noise client for ping request: %v", err)
  1236. return
  1237. }
  1238. httpc = nc.Client
  1239. }
  1240. if pr.URL == "" {
  1241. c.logf("invalid PingRequest with no URL")
  1242. return
  1243. }
  1244. switch pr.Types {
  1245. case "":
  1246. answerHeadPing(c.logf, httpc, pr)
  1247. return
  1248. case "c2n":
  1249. if !useNoise && !envknob.Bool("TS_DEBUG_PERMIT_HTTP_C2N") {
  1250. c.logf("refusing to answer c2n ping without noise")
  1251. return
  1252. }
  1253. answerC2NPing(c.logf, c.c2nHandler, httpc, pr)
  1254. return
  1255. }
  1256. for _, t := range strings.Split(pr.Types, ",") {
  1257. switch pt := tailcfg.PingType(t); pt {
  1258. case tailcfg.PingTSMP, tailcfg.PingDisco, tailcfg.PingICMP, tailcfg.PingPeerAPI:
  1259. go doPingerPing(c.logf, httpc, pr, c.pinger, pt)
  1260. default:
  1261. c.logf("unsupported ping request type: %q", t)
  1262. }
  1263. }
  1264. }
  1265. func answerHeadPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest) {
  1266. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  1267. defer cancel()
  1268. req, err := http.NewRequestWithContext(ctx, "HEAD", pr.URL, nil)
  1269. if err != nil {
  1270. logf("answerHeadPing: NewRequestWithContext: %v", err)
  1271. return
  1272. }
  1273. if pr.Log {
  1274. logf("answerHeadPing: sending HEAD ping to %v ...", pr.URL)
  1275. }
  1276. t0 := clock.Now()
  1277. _, err = c.Do(req)
  1278. d := clock.Since(t0).Round(time.Millisecond)
  1279. if err != nil {
  1280. logf("answerHeadPing error: %v to %v (after %v)", err, pr.URL, d)
  1281. } else if pr.Log {
  1282. logf("answerHeadPing complete to %v (after %v)", pr.URL, d)
  1283. }
  1284. }
  1285. func answerC2NPing(logf logger.Logf, c2nHandler http.Handler, c *http.Client, pr *tailcfg.PingRequest) {
  1286. if c2nHandler == nil {
  1287. logf("answerC2NPing: c2nHandler not defined")
  1288. return
  1289. }
  1290. hreq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(pr.Payload)))
  1291. if err != nil {
  1292. logf("answerC2NPing: ReadRequest: %v", err)
  1293. return
  1294. }
  1295. if pr.Log {
  1296. logf("answerC2NPing: got c2n request for %v ...", hreq.RequestURI)
  1297. }
  1298. handlerTimeout := time.Minute
  1299. if v := hreq.Header.Get("C2n-Handler-Timeout"); v != "" {
  1300. handlerTimeout, _ = time.ParseDuration(v)
  1301. }
  1302. handlerCtx, cancel := context.WithTimeout(context.Background(), handlerTimeout)
  1303. defer cancel()
  1304. hreq = hreq.WithContext(handlerCtx)
  1305. rec := httptest.NewRecorder()
  1306. c2nHandler.ServeHTTP(rec, hreq)
  1307. cancel()
  1308. c2nResBuf := new(bytes.Buffer)
  1309. rec.Result().Write(c2nResBuf)
  1310. replyCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
  1311. defer cancel()
  1312. req, err := http.NewRequestWithContext(replyCtx, "POST", pr.URL, c2nResBuf)
  1313. if err != nil {
  1314. logf("answerC2NPing: NewRequestWithContext: %v", err)
  1315. return
  1316. }
  1317. if pr.Log {
  1318. logf("answerC2NPing: sending POST ping to %v ...", pr.URL)
  1319. }
  1320. t0 := clock.Now()
  1321. _, err = c.Do(req)
  1322. d := time.Since(t0).Round(time.Millisecond)
  1323. if err != nil {
  1324. logf("answerC2NPing error: %v to %v (after %v)", err, pr.URL, d)
  1325. } else if pr.Log {
  1326. logf("answerC2NPing complete to %v (after %v)", pr.URL, d)
  1327. }
  1328. }
  1329. // sleepAsRequest implements the sleep for a tailcfg.Debug message requesting
  1330. // that the client sleep. The complication is that while we're sleeping (if for
  1331. // a long time), we need to periodically reset the watchdog timer before it
  1332. // expires.
  1333. func sleepAsRequested(ctx context.Context, logf logger.Logf, d time.Duration, clock tstime.Clock) error {
  1334. const maxSleep = 5 * time.Minute
  1335. if d > maxSleep {
  1336. logf("sleeping for %v, capped from server-requested %v ...", maxSleep, d)
  1337. d = maxSleep
  1338. } else {
  1339. logf("sleeping for server-requested %v ...", d)
  1340. }
  1341. timer, timerChannel := clock.NewTimer(d)
  1342. defer timer.Stop()
  1343. select {
  1344. case <-ctx.Done():
  1345. return ctx.Err()
  1346. case <-timerChannel:
  1347. return nil
  1348. }
  1349. }
  1350. // getNoiseClient returns the noise client, creating one if one doesn't exist.
  1351. func (c *Direct) getNoiseClient() (*NoiseClient, error) {
  1352. c.mu.Lock()
  1353. serverNoiseKey := c.serverNoiseKey
  1354. nc := c.noiseClient
  1355. c.mu.Unlock()
  1356. if serverNoiseKey.IsZero() {
  1357. return nil, errors.New("zero serverNoiseKey")
  1358. }
  1359. if nc != nil {
  1360. return nc, nil
  1361. }
  1362. var dp func() *tailcfg.ControlDialPlan
  1363. if c.dialPlan != nil {
  1364. dp = c.dialPlan.Load
  1365. }
  1366. nc, err, _ := c.sfGroup.Do(struct{}{}, func() (*NoiseClient, error) {
  1367. k, err := c.getMachinePrivKey()
  1368. if err != nil {
  1369. return nil, err
  1370. }
  1371. c.logf("[v1] creating new noise client")
  1372. nc, err := NewNoiseClient(NoiseOpts{
  1373. PrivKey: k,
  1374. ServerPubKey: serverNoiseKey,
  1375. ServerURL: c.serverURL,
  1376. Dialer: c.dialer,
  1377. DNSCache: c.dnsCache,
  1378. Logf: c.logf,
  1379. NetMon: c.netMon,
  1380. DialPlan: dp,
  1381. })
  1382. if err != nil {
  1383. return nil, err
  1384. }
  1385. c.mu.Lock()
  1386. defer c.mu.Unlock()
  1387. c.noiseClient = nc
  1388. return nc, nil
  1389. })
  1390. if err != nil {
  1391. return nil, err
  1392. }
  1393. return nc, nil
  1394. }
  1395. // setDNSNoise sends the SetDNSRequest request to the control plane server over Noise,
  1396. // requesting a DNS record be created or updated.
  1397. func (c *Direct) setDNSNoise(ctx context.Context, req *tailcfg.SetDNSRequest) error {
  1398. newReq := *req
  1399. newReq.Version = tailcfg.CurrentCapabilityVersion
  1400. nc, err := c.getNoiseClient()
  1401. if err != nil {
  1402. return err
  1403. }
  1404. res, err := nc.post(ctx, "/machine/set-dns", newReq.NodeKey, &newReq)
  1405. if err != nil {
  1406. return err
  1407. }
  1408. defer res.Body.Close()
  1409. if res.StatusCode != 200 {
  1410. msg, _ := io.ReadAll(res.Body)
  1411. return fmt.Errorf("set-dns response: %v, %.200s", res.Status, strings.TrimSpace(string(msg)))
  1412. }
  1413. var setDNSRes tailcfg.SetDNSResponse
  1414. if err := json.NewDecoder(res.Body).Decode(&setDNSRes); err != nil {
  1415. c.logf("error decoding SetDNSResponse: %v", err)
  1416. return fmt.Errorf("set-dns-response: %w", err)
  1417. }
  1418. return nil
  1419. }
  1420. // noiseConfigured reports whether the client can communicate with Control
  1421. // over Noise.
  1422. func (c *Direct) noiseConfigured() bool {
  1423. c.mu.Lock()
  1424. defer c.mu.Unlock()
  1425. return !c.serverNoiseKey.IsZero()
  1426. }
  1427. // SetDNS sends the SetDNSRequest request to the control plane server,
  1428. // requesting a DNS record be created or updated.
  1429. func (c *Direct) SetDNS(ctx context.Context, req *tailcfg.SetDNSRequest) (err error) {
  1430. metricSetDNS.Add(1)
  1431. defer func() {
  1432. if err != nil {
  1433. metricSetDNSError.Add(1)
  1434. }
  1435. }()
  1436. if c.noiseConfigured() {
  1437. return c.setDNSNoise(ctx, req)
  1438. }
  1439. c.mu.Lock()
  1440. serverKey := c.serverKey
  1441. c.mu.Unlock()
  1442. if serverKey.IsZero() {
  1443. return errors.New("zero serverKey")
  1444. }
  1445. machinePrivKey, err := c.getMachinePrivKey()
  1446. if err != nil {
  1447. return fmt.Errorf("getMachinePrivKey: %w", err)
  1448. }
  1449. if machinePrivKey.IsZero() {
  1450. return errors.New("getMachinePrivKey returned zero key")
  1451. }
  1452. // TODO(maisem): dedupe this codepath from SetDNSNoise.
  1453. var serverNoiseKey key.MachinePublic
  1454. bodyData, err := encode(req, serverKey, serverNoiseKey, machinePrivKey)
  1455. if err != nil {
  1456. return err
  1457. }
  1458. body := bytes.NewReader(bodyData)
  1459. u := fmt.Sprintf("%s/machine/%s/set-dns", c.serverURL, machinePrivKey.Public().UntypedHexString())
  1460. hreq, err := http.NewRequestWithContext(ctx, "POST", u, body)
  1461. if err != nil {
  1462. return err
  1463. }
  1464. res, err := c.httpc.Do(hreq)
  1465. if err != nil {
  1466. return err
  1467. }
  1468. defer res.Body.Close()
  1469. if res.StatusCode != 200 {
  1470. msg, _ := io.ReadAll(res.Body)
  1471. return fmt.Errorf("set-dns response: %v, %.200s", res.Status, strings.TrimSpace(string(msg)))
  1472. }
  1473. var setDNSRes tailcfg.SetDNSResponse
  1474. if err := decode(res, &setDNSRes, serverKey, serverNoiseKey, machinePrivKey); err != nil {
  1475. c.logf("error decoding SetDNSResponse with server key %s and machine key %s: %v", serverKey, machinePrivKey.Public(), err)
  1476. return fmt.Errorf("set-dns-response: %w", err)
  1477. }
  1478. return nil
  1479. }
  1480. func (c *Direct) DoNoiseRequest(req *http.Request) (*http.Response, error) {
  1481. nc, err := c.getNoiseClient()
  1482. if err != nil {
  1483. return nil, err
  1484. }
  1485. return nc.Do(req)
  1486. }
  1487. // GetSingleUseNoiseRoundTripper returns a RoundTripper that can be only be used
  1488. // once (and must be used once) to make a single HTTP request over the noise
  1489. // channel to the coordination server.
  1490. //
  1491. // In addition to the RoundTripper, it returns the HTTP/2 channel's early noise
  1492. // payload, if any.
  1493. func (c *Direct) GetSingleUseNoiseRoundTripper(ctx context.Context) (http.RoundTripper, *tailcfg.EarlyNoise, error) {
  1494. nc, err := c.getNoiseClient()
  1495. if err != nil {
  1496. return nil, nil, err
  1497. }
  1498. return nc.GetSingleUseRoundTripper(ctx)
  1499. }
  1500. // doPingerPing sends a Ping to pr.IP using pinger, and sends an http request back to
  1501. // pr.URL with ping response data.
  1502. func doPingerPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, pinger Pinger, pingType tailcfg.PingType) {
  1503. if pr.URL == "" || !pr.IP.IsValid() || pinger == nil {
  1504. logf("invalid ping request: missing url, ip or pinger")
  1505. return
  1506. }
  1507. start := clock.Now()
  1508. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  1509. defer cancel()
  1510. res, err := pinger.Ping(ctx, pr.IP, pingType, 0)
  1511. if err != nil {
  1512. d := time.Since(start).Round(time.Millisecond)
  1513. logf("doPingerPing: ping error of type %q to %v after %v: %v", pingType, pr.IP, d, err)
  1514. return
  1515. }
  1516. postPingResult(start, logf, c, pr, res.ToPingResponse(pingType))
  1517. }
  1518. func postPingResult(start time.Time, logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, res *tailcfg.PingResponse) error {
  1519. duration := time.Since(start)
  1520. if pr.Log {
  1521. if res.Err == "" {
  1522. logf("ping to %v completed in %v. pinger.Ping took %v seconds", pr.IP, res.LatencySeconds, duration)
  1523. } else {
  1524. logf("ping to %v failed after %v: %v", pr.IP, duration, res.Err)
  1525. }
  1526. }
  1527. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  1528. defer cancel()
  1529. jsonPingRes, err := json.Marshal(res)
  1530. if err != nil {
  1531. return err
  1532. }
  1533. // Send the results of the Ping, back to control URL.
  1534. req, err := http.NewRequestWithContext(ctx, "POST", pr.URL, bytes.NewReader(jsonPingRes))
  1535. if err != nil {
  1536. return fmt.Errorf("http.NewRequestWithContext(%q): %w", pr.URL, err)
  1537. }
  1538. if pr.Log {
  1539. logf("postPingResult: sending ping results to %v ...", pr.URL)
  1540. }
  1541. t0 := clock.Now()
  1542. _, err = c.Do(req)
  1543. d := time.Since(t0).Round(time.Millisecond)
  1544. if err != nil {
  1545. return fmt.Errorf("postPingResult error: %w to %v (after %v)", err, pr.URL, d)
  1546. } else if pr.Log {
  1547. logf("postPingResult complete to %v (after %v)", pr.URL, d)
  1548. }
  1549. return nil
  1550. }
  1551. // ReportHealthChange reports to the control plane a change to this node's
  1552. // health.
  1553. func (c *Direct) ReportHealthChange(sys health.Subsystem, sysErr error) {
  1554. if sys == health.SysOverall {
  1555. // We don't report these. These include things like the network is down
  1556. // (in which case we can't report anyway) or the user wanted things
  1557. // stopped, as opposed to the more unexpected failure types in the other
  1558. // subsystems.
  1559. return
  1560. }
  1561. np, err := c.getNoiseClient()
  1562. if err != nil {
  1563. // Don't report errors to control if the server doesn't support noise.
  1564. return
  1565. }
  1566. nodeKey := c.GetPersist().PublicNodeKey()
  1567. req := &tailcfg.HealthChangeRequest{
  1568. Subsys: string(sys),
  1569. NodeKey: nodeKey,
  1570. }
  1571. if sysErr != nil {
  1572. req.Error = sysErr.Error()
  1573. }
  1574. // Best effort, no logging:
  1575. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  1576. defer cancel()
  1577. res, err := np.post(ctx, "/machine/update-health", nodeKey, req)
  1578. if err != nil {
  1579. return
  1580. }
  1581. res.Body.Close()
  1582. }
  1583. // decodeWrappedAuthkey separates wrapping information from an authkey, if any.
  1584. // In all cases the authkey is returned, sans wrapping information if any.
  1585. //
  1586. // If the authkey is wrapped, isWrapped returns true, along with the wrapping signature
  1587. // and private key.
  1588. func decodeWrappedAuthkey(key string, logf logger.Logf) (authKey string, isWrapped bool, sig *tka.NodeKeySignature, priv ed25519.PrivateKey) {
  1589. authKey, suffix, found := strings.Cut(key, "--TL")
  1590. if !found {
  1591. return key, false, nil, nil
  1592. }
  1593. sigBytes, privBytes, found := strings.Cut(suffix, "-")
  1594. if !found {
  1595. logf("decoding wrapped auth-key: did not find delimiter")
  1596. return key, false, nil, nil
  1597. }
  1598. rawSig, err := base64.RawStdEncoding.DecodeString(sigBytes)
  1599. if err != nil {
  1600. logf("decoding wrapped auth-key: signature decode: %v", err)
  1601. return key, false, nil, nil
  1602. }
  1603. rawPriv, err := base64.RawStdEncoding.DecodeString(privBytes)
  1604. if err != nil {
  1605. logf("decoding wrapped auth-key: priv decode: %v", err)
  1606. return key, false, nil, nil
  1607. }
  1608. sig = new(tka.NodeKeySignature)
  1609. if err := sig.Unserialize([]byte(rawSig)); err != nil {
  1610. logf("decoding wrapped auth-key: signature: %v", err)
  1611. return key, false, nil, nil
  1612. }
  1613. priv = ed25519.PrivateKey(rawPriv)
  1614. return authKey, true, sig, priv
  1615. }
  1616. func addLBHeader(req *http.Request, nodeKey key.NodePublic) {
  1617. if !nodeKey.IsZero() {
  1618. req.Header.Add(tailcfg.LBHeader, nodeKey.String())
  1619. }
  1620. }
  1621. var (
  1622. metricMapRequestsActive = clientmetric.NewGauge("controlclient_map_requests_active")
  1623. metricMapRequests = clientmetric.NewCounter("controlclient_map_requests")
  1624. metricMapRequestsLite = clientmetric.NewCounter("controlclient_map_requests_lite")
  1625. metricMapRequestsPoll = clientmetric.NewCounter("controlclient_map_requests_poll")
  1626. metricMapResponseMessages = clientmetric.NewCounter("controlclient_map_response_message") // any message type
  1627. metricMapResponsePings = clientmetric.NewCounter("controlclient_map_response_ping")
  1628. metricMapResponseKeepAlives = clientmetric.NewCounter("controlclient_map_response_keepalive")
  1629. metricMapResponseMap = clientmetric.NewCounter("controlclient_map_response_map") // any non-keepalive map response
  1630. metricMapResponseMapDelta = clientmetric.NewCounter("controlclient_map_response_map_delta") // 2nd+ non-keepalive map response
  1631. metricSetDNS = clientmetric.NewCounter("controlclient_setdns")
  1632. metricSetDNSError = clientmetric.NewCounter("controlclient_setdns_error")
  1633. )