userspace.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package wgengine
  4. import (
  5. "bufio"
  6. "context"
  7. crand "crypto/rand"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "net/netip"
  12. "reflect"
  13. "runtime"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/tailscale/wireguard-go/device"
  18. "github.com/tailscale/wireguard-go/tun"
  19. "golang.org/x/exp/maps"
  20. "tailscale.com/control/controlclient"
  21. "tailscale.com/envknob"
  22. "tailscale.com/health"
  23. "tailscale.com/ipn/ipnstate"
  24. "tailscale.com/net/dns"
  25. "tailscale.com/net/dns/resolver"
  26. "tailscale.com/net/flowtrack"
  27. "tailscale.com/net/interfaces"
  28. "tailscale.com/net/packet"
  29. "tailscale.com/net/sockstats"
  30. "tailscale.com/net/tsaddr"
  31. "tailscale.com/net/tsdial"
  32. "tailscale.com/net/tshttpproxy"
  33. "tailscale.com/net/tstun"
  34. "tailscale.com/syncs"
  35. "tailscale.com/tailcfg"
  36. "tailscale.com/tstime/mono"
  37. "tailscale.com/types/dnstype"
  38. "tailscale.com/types/ipproto"
  39. "tailscale.com/types/key"
  40. "tailscale.com/types/logger"
  41. "tailscale.com/types/netmap"
  42. "tailscale.com/util/clientmetric"
  43. "tailscale.com/util/deephash"
  44. "tailscale.com/util/mak"
  45. "tailscale.com/wgengine/capture"
  46. "tailscale.com/wgengine/filter"
  47. "tailscale.com/wgengine/magicsock"
  48. "tailscale.com/wgengine/monitor"
  49. "tailscale.com/wgengine/netlog"
  50. "tailscale.com/wgengine/router"
  51. "tailscale.com/wgengine/wgcfg"
  52. "tailscale.com/wgengine/wgint"
  53. "tailscale.com/wgengine/wglog"
  54. )
  55. // Lazy wireguard-go configuration parameters.
  56. const (
  57. // lazyPeerIdleThreshold is the idle duration after
  58. // which we remove a peer from the wireguard configuration.
  59. // (This includes peers that have never been idle, which
  60. // effectively have infinite idleness)
  61. lazyPeerIdleThreshold = 5 * time.Minute
  62. // packetSendTimeUpdateFrequency controls how often we record
  63. // the time that we wrote a packet to an IP address.
  64. packetSendTimeUpdateFrequency = 10 * time.Second
  65. // packetSendRecheckWireguardThreshold controls how long we can go
  66. // between packet sends to an IP before checking to see
  67. // whether this IP address needs to be added back to the
  68. // WireGuard peer oconfig.
  69. packetSendRecheckWireguardThreshold = 1 * time.Minute
  70. )
  71. // statusPollInterval is how often we ask wireguard-go for its engine
  72. // status (as long as there's activity). See docs on its use below.
  73. const statusPollInterval = 1 * time.Minute
  74. // networkLoggerUploadTimeout is the maximum timeout to wait when
  75. // shutting down the network logger as it uploads the last network log messages.
  76. const networkLoggerUploadTimeout = 5 * time.Second
  77. type userspaceEngine struct {
  78. logf logger.Logf
  79. wgLogger *wglog.Logger //a wireguard-go logging wrapper
  80. reqCh chan struct{}
  81. waitCh chan struct{} // chan is closed when first Close call completes; contrast with closing bool
  82. timeNow func() mono.Time
  83. tundev *tstun.Wrapper
  84. wgdev *device.Device
  85. router router.Router
  86. confListenPort uint16 // original conf.ListenPort
  87. dns *dns.Manager
  88. magicConn *magicsock.Conn
  89. linkMon *monitor.Mon
  90. linkMonOwned bool // whether we created linkMon (and thus need to close it)
  91. linkMonUnregister func() // unsubscribes from changes; used regardless of linkMonOwned
  92. birdClient BIRDClient // or nil
  93. testMaybeReconfigHook func() // for tests; if non-nil, fires if maybeReconfigWireguardLocked called
  94. // isLocalAddr reports the whether an IP is assigned to the local
  95. // tunnel interface. It's used to reflect local packets
  96. // incorrectly sent to us.
  97. isLocalAddr syncs.AtomicValue[func(netip.Addr) bool]
  98. // isDNSIPOverTailscale reports the whether a DNS resolver's IP
  99. // is being routed over Tailscale.
  100. isDNSIPOverTailscale syncs.AtomicValue[func(netip.Addr) bool]
  101. wgLock sync.Mutex // serializes all wgdev operations; see lock order comment below
  102. lastCfgFull wgcfg.Config
  103. lastNMinPeers int
  104. lastRouterSig deephash.Sum // of router.Config
  105. lastEngineSigFull deephash.Sum // of full wireguard config
  106. lastEngineSigTrim deephash.Sum // of trimmed wireguard config
  107. lastDNSConfig *dns.Config
  108. lastIsSubnetRouter bool // was the node a primary subnet router in the last run.
  109. recvActivityAt map[key.NodePublic]mono.Time
  110. trimmedNodes map[key.NodePublic]bool // set of node keys of peers currently excluded from wireguard config
  111. sentActivityAt map[netip.Addr]*mono.Time // value is accessed atomically
  112. destIPActivityFuncs map[netip.Addr]func()
  113. statusBufioReader *bufio.Reader // reusable for UAPI
  114. lastStatusPollTime mono.Time // last time we polled the engine status
  115. mu sync.Mutex // guards following; see lock order comment below
  116. netMap *netmap.NetworkMap // or nil
  117. closing bool // Close was called (even if we're still closing)
  118. statusCallback StatusCallback
  119. peerSequence []key.NodePublic
  120. endpoints []tailcfg.Endpoint
  121. pendOpen map[flowtrack.Tuple]*pendingOpenFlow // see pendopen.go
  122. networkMapCallbacks map[*someHandle]NetworkMapCallback
  123. tsIPByIPPort map[netip.AddrPort]netip.Addr // allows registration of IP:ports as belonging to a certain Tailscale IP for whois lookups
  124. // pongCallback is the map of response handlers waiting for disco or TSMP
  125. // pong callbacks. The map key is a random slice of bytes.
  126. pongCallback map[[8]byte]func(packet.TSMPPongReply)
  127. // icmpEchoResponseCallback is the map of reponse handlers waiting for ICMP
  128. // echo responses. The map key is a random uint32 that is the little endian
  129. // value of the ICMP identifer and sequence number concatenated.
  130. icmpEchoResponseCallback map[uint32]func()
  131. // networkLogger logs statistics about network connections.
  132. networkLogger netlog.Logger
  133. // Lock ordering: magicsock.Conn.mu, wgLock, then mu.
  134. }
  135. // InternalsGetter is implemented by Engines that can export their internals.
  136. type InternalsGetter interface {
  137. GetInternals() (_ *tstun.Wrapper, _ *magicsock.Conn, _ *dns.Manager, ok bool)
  138. }
  139. func (e *userspaceEngine) GetInternals() (_ *tstun.Wrapper, _ *magicsock.Conn, _ *dns.Manager, ok bool) {
  140. return e.tundev, e.magicConn, e.dns, true
  141. }
  142. // ResolvingEngine is implemented by Engines that have DNS resolvers.
  143. type ResolvingEngine interface {
  144. GetResolver() (_ *resolver.Resolver, ok bool)
  145. }
  146. var (
  147. _ ResolvingEngine = (*userspaceEngine)(nil)
  148. _ ResolvingEngine = (*watchdogEngine)(nil)
  149. )
  150. func (e *userspaceEngine) GetResolver() (r *resolver.Resolver, ok bool) {
  151. return e.dns.Resolver(), true
  152. }
  153. // BIRDClient handles communication with the BIRD Internet Routing Daemon.
  154. type BIRDClient interface {
  155. EnableProtocol(proto string) error
  156. DisableProtocol(proto string) error
  157. Close() error
  158. }
  159. // Config is the engine configuration.
  160. type Config struct {
  161. // Tun is the device used by the Engine to exchange packets with
  162. // the OS.
  163. // If nil, a fake Device that does nothing is used.
  164. Tun tun.Device
  165. // IsTAP is whether Tun is actually a TAP (Layer 2) device that'll
  166. // require ethernet headers.
  167. IsTAP bool
  168. // Router interfaces the Engine to the OS network stack.
  169. // If nil, a fake Router that does nothing is used.
  170. Router router.Router
  171. // DNS interfaces the Engine to the OS DNS resolver configuration.
  172. // If nil, a fake OSConfigurator that does nothing is used.
  173. DNS dns.OSConfigurator
  174. // LinkMonitor optionally provides an existing link monitor to re-use.
  175. // If nil, a new link monitor is created.
  176. LinkMonitor *monitor.Mon
  177. // Dialer is the dialer to use for outbound connections.
  178. // If nil, a new Dialer is created
  179. Dialer *tsdial.Dialer
  180. // ListenPort is the port on which the engine will listen.
  181. // If zero, a port is automatically selected.
  182. ListenPort uint16
  183. // RespondToPing determines whether this engine should internally
  184. // reply to ICMP pings, without involving the OS.
  185. // Used in "fake" mode for development.
  186. RespondToPing bool
  187. // BIRDClient, if non-nil, will be used to configure BIRD whenever
  188. // this node is a primary subnet router.
  189. BIRDClient BIRDClient
  190. }
  191. func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) {
  192. logf("Starting userspace WireGuard engine (with fake TUN device)")
  193. return NewUserspaceEngine(logf, Config{
  194. ListenPort: listenPort,
  195. RespondToPing: true,
  196. })
  197. }
  198. // NetstackRouterType is a gross cross-package init-time registration
  199. // from netstack to here, informing this package of netstack's router
  200. // type.
  201. var NetstackRouterType reflect.Type
  202. // IsNetstackRouter reports whether e is either fully netstack based
  203. // (without TUN) or is at least using netstack for routing.
  204. func IsNetstackRouter(e Engine) bool {
  205. switch e := e.(type) {
  206. case *userspaceEngine:
  207. if reflect.TypeOf(e.router) == NetstackRouterType {
  208. return true
  209. }
  210. case *watchdogEngine:
  211. return IsNetstackRouter(e.wrap)
  212. }
  213. return IsNetstack(e)
  214. }
  215. // IsNetstack reports whether e is a netstack-based TUN-free engine.
  216. func IsNetstack(e Engine) bool {
  217. ig, ok := e.(InternalsGetter)
  218. if !ok {
  219. return false
  220. }
  221. tw, _, _, ok := ig.GetInternals()
  222. if !ok {
  223. return false
  224. }
  225. name, err := tw.Name()
  226. return err == nil && name == "FakeTUN"
  227. }
  228. // NewUserspaceEngine creates the named tun device and returns a
  229. // Tailscale Engine running on it.
  230. func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) {
  231. var closePool closeOnErrorPool
  232. defer closePool.closeAllIfError(&reterr)
  233. if conf.Tun == nil {
  234. logf("[v1] using fake (no-op) tun device")
  235. conf.Tun = tstun.NewFake()
  236. }
  237. if conf.Router == nil {
  238. logf("[v1] using fake (no-op) OS network configurator")
  239. conf.Router = router.NewFake(logf)
  240. }
  241. if conf.DNS == nil {
  242. logf("[v1] using fake (no-op) DNS configurator")
  243. d, err := dns.NewNoopManager()
  244. if err != nil {
  245. return nil, err
  246. }
  247. conf.DNS = d
  248. }
  249. if conf.Dialer == nil {
  250. conf.Dialer = &tsdial.Dialer{Logf: logf}
  251. }
  252. var tsTUNDev *tstun.Wrapper
  253. if conf.IsTAP {
  254. tsTUNDev = tstun.WrapTAP(logf, conf.Tun)
  255. } else {
  256. tsTUNDev = tstun.Wrap(logf, conf.Tun)
  257. }
  258. closePool.add(tsTUNDev)
  259. e := &userspaceEngine{
  260. timeNow: mono.Now,
  261. logf: logf,
  262. reqCh: make(chan struct{}, 1),
  263. waitCh: make(chan struct{}),
  264. tundev: tsTUNDev,
  265. router: conf.Router,
  266. confListenPort: conf.ListenPort,
  267. birdClient: conf.BIRDClient,
  268. }
  269. if e.birdClient != nil {
  270. // Disable the protocol at start time.
  271. if err := e.birdClient.DisableProtocol("tailscale"); err != nil {
  272. return nil, err
  273. }
  274. }
  275. e.isLocalAddr.Store(tsaddr.NewContainsIPFunc(nil))
  276. e.isDNSIPOverTailscale.Store(tsaddr.NewContainsIPFunc(nil))
  277. if conf.LinkMonitor != nil {
  278. e.linkMon = conf.LinkMonitor
  279. } else {
  280. mon, err := monitor.New(logf)
  281. if err != nil {
  282. return nil, err
  283. }
  284. closePool.add(mon)
  285. e.linkMon = mon
  286. e.linkMonOwned = true
  287. }
  288. tunName, _ := conf.Tun.Name()
  289. conf.Dialer.SetTUNName(tunName)
  290. conf.Dialer.SetLinkMonitor(e.linkMon)
  291. e.dns = dns.NewManager(logf, conf.DNS, e.linkMon, conf.Dialer, fwdDNSLinkSelector{e, tunName})
  292. // TODO: there's probably a better place for this
  293. sockstats.SetLinkMonitor(e.linkMon)
  294. logf("link state: %+v", e.linkMon.InterfaceState())
  295. unregisterMonWatch := e.linkMon.RegisterChangeCallback(func(changed bool, st *interfaces.State) {
  296. tshttpproxy.InvalidateCache()
  297. e.linkChange(changed, st)
  298. })
  299. closePool.addFunc(unregisterMonWatch)
  300. e.linkMonUnregister = unregisterMonWatch
  301. endpointsFn := func(endpoints []tailcfg.Endpoint) {
  302. e.mu.Lock()
  303. e.endpoints = append(e.endpoints[:0], endpoints...)
  304. e.mu.Unlock()
  305. e.RequestStatus()
  306. }
  307. magicsockOpts := magicsock.Options{
  308. Logf: logf,
  309. Port: conf.ListenPort,
  310. EndpointsFunc: endpointsFn,
  311. DERPActiveFunc: e.RequestStatus,
  312. IdleFunc: e.tundev.IdleDuration,
  313. NoteRecvActivity: e.noteRecvActivity,
  314. LinkMonitor: e.linkMon,
  315. }
  316. var err error
  317. e.magicConn, err = magicsock.NewConn(magicsockOpts)
  318. if err != nil {
  319. return nil, fmt.Errorf("wgengine: %v", err)
  320. }
  321. closePool.add(e.magicConn)
  322. e.magicConn.SetNetworkUp(e.linkMon.InterfaceState().AnyInterfaceUp())
  323. tsTUNDev.SetDiscoKey(e.magicConn.DiscoPublicKey())
  324. if conf.RespondToPing {
  325. e.tundev.PostFilterPacketInboundFromWireGaurd = echoRespondToAll
  326. }
  327. e.tundev.PreFilterPacketOutboundToWireGuardEngineIntercept = e.handleLocalPackets
  328. if envknob.BoolDefaultTrue("TS_DEBUG_CONNECT_FAILURES") {
  329. if e.tundev.PreFilterPacketInboundFromWireGuard != nil {
  330. return nil, errors.New("unexpected PreFilterIn already set")
  331. }
  332. e.tundev.PreFilterPacketInboundFromWireGuard = e.trackOpenPreFilterIn
  333. if e.tundev.PostFilterPacketOutboundToWireGuard != nil {
  334. return nil, errors.New("unexpected PostFilterOut already set")
  335. }
  336. e.tundev.PostFilterPacketOutboundToWireGuard = e.trackOpenPostFilterOut
  337. }
  338. e.wgLogger = wglog.NewLogger(logf)
  339. e.tundev.OnTSMPPongReceived = func(pong packet.TSMPPongReply) {
  340. e.mu.Lock()
  341. defer e.mu.Unlock()
  342. cb := e.pongCallback[pong.Data]
  343. e.logf("wgengine: got TSMP pong %02x, peerAPIPort=%v; cb=%v", pong.Data, pong.PeerAPIPort, cb != nil)
  344. if cb != nil {
  345. go cb(pong)
  346. }
  347. }
  348. e.tundev.OnICMPEchoResponseReceived = func(p *packet.Parsed) bool {
  349. idSeq := p.EchoIDSeq()
  350. e.mu.Lock()
  351. defer e.mu.Unlock()
  352. cb := e.icmpEchoResponseCallback[idSeq]
  353. if cb == nil {
  354. // We didn't swallow it, so let it flow to the host.
  355. return false
  356. }
  357. e.logf("wgengine: got diagnostic ICMP response %02x", idSeq)
  358. go cb()
  359. return true
  360. }
  361. // wgdev takes ownership of tundev, will close it when closed.
  362. e.logf("Creating WireGuard device...")
  363. e.wgdev = wgcfg.NewDevice(e.tundev, e.magicConn.Bind(), e.wgLogger.DeviceLogger)
  364. closePool.addFunc(e.wgdev.Close)
  365. closePool.addFunc(func() {
  366. if err := e.magicConn.Close(); err != nil {
  367. e.logf("error closing magicconn: %v", err)
  368. }
  369. })
  370. go func() {
  371. up := false
  372. for event := range e.tundev.EventsUpDown() {
  373. if event&tun.EventUp != 0 && !up {
  374. e.logf("external route: up")
  375. e.RequestStatus()
  376. up = true
  377. }
  378. if event&tun.EventDown != 0 && up {
  379. e.logf("external route: down")
  380. e.RequestStatus()
  381. up = false
  382. }
  383. }
  384. }()
  385. e.logf("Bringing WireGuard device up...")
  386. if err := e.wgdev.Up(); err != nil {
  387. return nil, fmt.Errorf("wgdev.Up: %w", err)
  388. }
  389. e.logf("Bringing router up...")
  390. if err := e.router.Up(); err != nil {
  391. return nil, fmt.Errorf("router.Up: %w", err)
  392. }
  393. // It's a little pointless to apply no-op settings here (they
  394. // should already be empty?), but it at least exercises the
  395. // router implementation early on.
  396. e.logf("Clearing router settings...")
  397. if err := e.router.Set(nil); err != nil {
  398. return nil, fmt.Errorf("router.Set(nil): %w", err)
  399. }
  400. e.logf("Starting link monitor...")
  401. e.linkMon.Start()
  402. e.logf("Engine created.")
  403. return e, nil
  404. }
  405. // echoRespondToAll is an inbound post-filter responding to all echo requests.
  406. func echoRespondToAll(p *packet.Parsed, t *tstun.Wrapper) filter.Response {
  407. if p.IsEchoRequest() {
  408. header := p.ICMP4Header()
  409. header.ToResponse()
  410. outp := packet.Generate(&header, p.Payload())
  411. t.InjectOutbound(outp)
  412. // We already responded to it, but it's not an error.
  413. // Proceed with regular delivery. (Since this code is only
  414. // used in fake mode, regular delivery just means throwing
  415. // it away. If this ever gets run in non-fake mode, you'll
  416. // get double responses to pings, which is an indicator you
  417. // shouldn't be doing that I guess.)
  418. return filter.Accept
  419. }
  420. return filter.Accept
  421. }
  422. // handleLocalPackets inspects packets coming from the local network
  423. // stack, and intercepts any packets that should be handled by
  424. // tailscaled directly. Other packets are allowed to proceed into the
  425. // main ACL filter.
  426. func (e *userspaceEngine) handleLocalPackets(p *packet.Parsed, t *tstun.Wrapper) filter.Response {
  427. if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
  428. isLocalAddr, ok := e.isLocalAddr.LoadOk()
  429. if !ok {
  430. e.logf("[unexpected] e.isLocalAddr was nil, can't check for loopback packet")
  431. } else if isLocalAddr(p.Dst.Addr()) {
  432. // macOS NetworkExtension directs packets destined to the
  433. // tunnel's local IP address into the tunnel, instead of
  434. // looping back within the kernel network stack. We have to
  435. // notice that an outbound packet is actually destined for
  436. // ourselves, and loop it back into macOS.
  437. t.InjectInboundCopy(p.Buffer())
  438. metricReflectToOS.Add(1)
  439. return filter.Drop
  440. }
  441. }
  442. return filter.Accept
  443. }
  444. var debugTrimWireguard = envknob.RegisterOptBool("TS_DEBUG_TRIM_WIREGUARD")
  445. // forceFullWireguardConfig reports whether we should give wireguard our full
  446. // network map, even for inactive peers.
  447. //
  448. // TODO(bradfitz): remove this at some point. We had a TODO to do it before 1.0
  449. // but it's still there as of 1.30. Really we should not do this wireguard lazy
  450. // peer config at all and just fix wireguard-go to not have so much extra memory
  451. // usage per peer. That would simplify a lot of Tailscale code. OTOH, we have 50
  452. // MB of memory on iOS now instead of 15 MB, so the other option is to just give
  453. // up on lazy wireguard config and blow the memory and hope for the best on iOS.
  454. // That's sad too. Or we get rid of these knobs (lazy wireguard config has been
  455. // stable!) but I'm worried that a future regression would be easier to debug
  456. // with these knobs in place.
  457. func forceFullWireguardConfig(numPeers int) bool {
  458. // Did the user explicitly enable trimming via the environment variable knob?
  459. if b, ok := debugTrimWireguard().Get(); ok {
  460. return !b
  461. }
  462. if opt := controlclient.TrimWGConfig(); opt != "" {
  463. return !opt.EqualBool(true)
  464. }
  465. return false
  466. }
  467. // isTrimmablePeer reports whether p is a peer that we can trim out of the
  468. // network map.
  469. //
  470. // For implementation simplicity, we can only trim peers that have
  471. // only non-subnet AllowedIPs (an IPv4 /32 or IPv6 /128), which is the
  472. // common case for most peers. Subnet router nodes will just always be
  473. // created in the wireguard-go config.
  474. func isTrimmablePeer(p *wgcfg.Peer, numPeers int) bool {
  475. if forceFullWireguardConfig(numPeers) {
  476. return false
  477. }
  478. // AllowedIPs must all be single IPs, not subnets.
  479. for _, aip := range p.AllowedIPs {
  480. if !aip.IsSingleIP() {
  481. return false
  482. }
  483. }
  484. return true
  485. }
  486. // noteRecvActivity is called by magicsock when a packet has been
  487. // received for the peer with node key nk. Magicsock calls this no
  488. // more than every 10 seconds for a given peer.
  489. func (e *userspaceEngine) noteRecvActivity(nk key.NodePublic) {
  490. e.wgLock.Lock()
  491. defer e.wgLock.Unlock()
  492. if _, ok := e.recvActivityAt[nk]; !ok {
  493. // Not a trimmable peer we care about tracking. (See isTrimmablePeer)
  494. if e.trimmedNodes[nk] {
  495. e.logf("wgengine: [unexpected] noteReceiveActivity called on idle node %v that's not in recvActivityAt", nk.ShortString())
  496. }
  497. return
  498. }
  499. now := e.timeNow()
  500. e.recvActivityAt[nk] = now
  501. // As long as there's activity, periodically poll the engine to get
  502. // stats for the far away side effect of
  503. // ipn/ipnlocal.LocalBackend.parseWgStatusLocked to log activity, for
  504. // use in various admin dashboards.
  505. // This particularly matters on platforms without a connected GUI, as
  506. // the GUIs generally poll this enough to cause that logging. But
  507. // tailscaled alone did not, hence this.
  508. if e.lastStatusPollTime.IsZero() || now.Sub(e.lastStatusPollTime) >= statusPollInterval {
  509. e.lastStatusPollTime = now
  510. go e.RequestStatus()
  511. }
  512. // If the last activity time jumped a bunch (say, at least
  513. // half the idle timeout) then see if we need to reprogram
  514. // WireGuard. This could probably be just
  515. // lazyPeerIdleThreshold without the divide by 2, but
  516. // maybeReconfigWireguardLocked is cheap enough to call every
  517. // couple minutes (just not on every packet).
  518. if e.trimmedNodes[nk] {
  519. e.logf("wgengine: idle peer %v now active, reconfiguring WireGuard", nk.ShortString())
  520. e.maybeReconfigWireguardLocked(nil)
  521. }
  522. }
  523. // isActiveSinceLocked reports whether the peer identified by (nk, ip)
  524. // has had a packet sent to or received from it since t.
  525. //
  526. // e.wgLock must be held.
  527. func (e *userspaceEngine) isActiveSinceLocked(nk key.NodePublic, ip netip.Addr, t mono.Time) bool {
  528. if e.recvActivityAt[nk].After(t) {
  529. return true
  530. }
  531. timePtr, ok := e.sentActivityAt[ip]
  532. if !ok {
  533. return false
  534. }
  535. return timePtr.LoadAtomic().After(t)
  536. }
  537. // discoChanged are the set of peers whose disco keys have changed, implying they've restarted.
  538. // If a peer is in this set and was previously in the live wireguard config,
  539. // it needs to be first removed and then re-added to flush out its wireguard session key.
  540. // If discoChanged is nil or empty, this extra removal step isn't done.
  541. //
  542. // e.wgLock must be held.
  543. func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[key.NodePublic]bool) error {
  544. if hook := e.testMaybeReconfigHook; hook != nil {
  545. hook()
  546. return nil
  547. }
  548. full := e.lastCfgFull
  549. e.wgLogger.SetPeers(full.Peers)
  550. // Compute a minimal config to pass to wireguard-go
  551. // based on the full config. Prune off all the peers
  552. // and only add the active ones back.
  553. min := full
  554. min.Peers = make([]wgcfg.Peer, 0, e.lastNMinPeers)
  555. // We'll only keep a peer around if it's been active in
  556. // the past 5 minutes. That's more than WireGuard's key
  557. // rotation time anyway so it's no harm if we remove it
  558. // later if it's been inactive.
  559. activeCutoff := e.timeNow().Add(-lazyPeerIdleThreshold)
  560. // Not all peers can be trimmed from the network map (see
  561. // isTrimmablePeer). For those that are trimmable, keep track of
  562. // their NodeKey and Tailscale IPs. These are the ones we'll need
  563. // to install tracking hooks for to watch their send/receive
  564. // activity.
  565. trackNodes := make([]key.NodePublic, 0, len(full.Peers))
  566. trackIPs := make([]netip.Addr, 0, len(full.Peers))
  567. // Don't re-alloc the map; the Go compiler optimizes map clears as of
  568. // Go 1.11, so we can re-use the existing + allocated map.
  569. if e.trimmedNodes != nil {
  570. maps.Clear(e.trimmedNodes)
  571. } else {
  572. e.trimmedNodes = make(map[key.NodePublic]bool)
  573. }
  574. needRemoveStep := false
  575. for i := range full.Peers {
  576. p := &full.Peers[i]
  577. nk := p.PublicKey
  578. if !isTrimmablePeer(p, len(full.Peers)) {
  579. min.Peers = append(min.Peers, *p)
  580. if discoChanged[nk] {
  581. needRemoveStep = true
  582. }
  583. continue
  584. }
  585. trackNodes = append(trackNodes, nk)
  586. recentlyActive := false
  587. for _, cidr := range p.AllowedIPs {
  588. trackIPs = append(trackIPs, cidr.Addr())
  589. recentlyActive = recentlyActive || e.isActiveSinceLocked(nk, cidr.Addr(), activeCutoff)
  590. }
  591. if recentlyActive {
  592. min.Peers = append(min.Peers, *p)
  593. if discoChanged[nk] {
  594. needRemoveStep = true
  595. }
  596. } else {
  597. e.trimmedNodes[nk] = true
  598. }
  599. }
  600. e.lastNMinPeers = len(min.Peers)
  601. if changed := deephash.Update(&e.lastEngineSigTrim, &struct {
  602. WGConfig *wgcfg.Config
  603. TrimmedNodes map[key.NodePublic]bool
  604. TrackNodes []key.NodePublic
  605. TrackIPs []netip.Addr
  606. }{&min, e.trimmedNodes, trackNodes, trackIPs}); !changed {
  607. return nil
  608. }
  609. e.updateActivityMapsLocked(trackNodes, trackIPs)
  610. if needRemoveStep {
  611. minner := min
  612. minner.Peers = nil
  613. numRemove := 0
  614. for _, p := range min.Peers {
  615. if discoChanged[p.PublicKey] {
  616. numRemove++
  617. continue
  618. }
  619. minner.Peers = append(minner.Peers, p)
  620. }
  621. if numRemove > 0 {
  622. e.logf("wgengine: Reconfig: removing session keys for %d peers", numRemove)
  623. if err := wgcfg.ReconfigDevice(e.wgdev, &minner, e.logf); err != nil {
  624. e.logf("wgdev.Reconfig: %v", err)
  625. return err
  626. }
  627. }
  628. }
  629. e.logf("wgengine: Reconfig: configuring userspace WireGuard config (with %d/%d peers)", len(min.Peers), len(full.Peers))
  630. if err := wgcfg.ReconfigDevice(e.wgdev, &min, e.logf); err != nil {
  631. e.logf("wgdev.Reconfig: %v", err)
  632. return err
  633. }
  634. return nil
  635. }
  636. // updateActivityMapsLocked updates the data structures used for tracking the activity
  637. // of wireguard peers that we might add/remove dynamically from the real config
  638. // as given to wireguard-go.
  639. //
  640. // e.wgLock must be held.
  641. func (e *userspaceEngine) updateActivityMapsLocked(trackNodes []key.NodePublic, trackIPs []netip.Addr) {
  642. // Generate the new map of which nodekeys we want to track
  643. // receive times for.
  644. mr := map[key.NodePublic]mono.Time{} // TODO: only recreate this if set of keys changed
  645. for _, nk := range trackNodes {
  646. // Preserve old times in the new map, but also
  647. // populate map entries for new trackNodes values with
  648. // time.Time{} zero values. (Only entries in this map
  649. // are tracked, so the Time zero values allow it to be
  650. // tracked later)
  651. mr[nk] = e.recvActivityAt[nk]
  652. }
  653. e.recvActivityAt = mr
  654. oldTime := e.sentActivityAt
  655. e.sentActivityAt = make(map[netip.Addr]*mono.Time, len(oldTime))
  656. oldFunc := e.destIPActivityFuncs
  657. e.destIPActivityFuncs = make(map[netip.Addr]func(), len(oldFunc))
  658. updateFn := func(timePtr *mono.Time) func() {
  659. return func() {
  660. now := e.timeNow()
  661. old := timePtr.LoadAtomic()
  662. // How long's it been since we last sent a packet?
  663. elapsed := now.Sub(old)
  664. if old == 0 {
  665. // For our first packet, old is 0, which has indeterminate meaning.
  666. // Set elapsed to a big number (four score and seven years).
  667. elapsed = 762642 * time.Hour
  668. }
  669. if elapsed >= packetSendTimeUpdateFrequency {
  670. timePtr.StoreAtomic(now)
  671. }
  672. // On a big jump, assume we might no longer be in the wireguard
  673. // config and go check.
  674. if elapsed >= packetSendRecheckWireguardThreshold {
  675. e.wgLock.Lock()
  676. defer e.wgLock.Unlock()
  677. e.maybeReconfigWireguardLocked(nil)
  678. }
  679. }
  680. }
  681. for _, ip := range trackIPs {
  682. timePtr := oldTime[ip]
  683. if timePtr == nil {
  684. timePtr = new(mono.Time)
  685. }
  686. e.sentActivityAt[ip] = timePtr
  687. fn := oldFunc[ip]
  688. if fn == nil {
  689. fn = updateFn(timePtr)
  690. }
  691. e.destIPActivityFuncs[ip] = fn
  692. }
  693. e.tundev.SetDestIPActivityFuncs(e.destIPActivityFuncs)
  694. }
  695. // hasOverlap checks if there is a IPPrefix which is common amongst the two
  696. // provided slices.
  697. func hasOverlap(aips, rips []netip.Prefix) bool {
  698. for _, aip := range aips {
  699. for _, rip := range rips {
  700. if aip == rip {
  701. return true
  702. }
  703. }
  704. }
  705. return false
  706. }
  707. func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, dnsCfg *dns.Config, debug *tailcfg.Debug) error {
  708. if routerCfg == nil {
  709. panic("routerCfg must not be nil")
  710. }
  711. if dnsCfg == nil {
  712. panic("dnsCfg must not be nil")
  713. }
  714. e.isLocalAddr.Store(tsaddr.NewContainsIPFunc(routerCfg.LocalAddrs))
  715. e.wgLock.Lock()
  716. defer e.wgLock.Unlock()
  717. e.tundev.SetWGConfig(cfg)
  718. e.lastDNSConfig = dnsCfg
  719. peerSet := make(map[key.NodePublic]struct{}, len(cfg.Peers))
  720. e.mu.Lock()
  721. e.peerSequence = e.peerSequence[:0]
  722. for _, p := range cfg.Peers {
  723. e.peerSequence = append(e.peerSequence, p.PublicKey)
  724. peerSet[p.PublicKey] = struct{}{}
  725. }
  726. nm := e.netMap
  727. e.mu.Unlock()
  728. listenPort := e.confListenPort
  729. if debug != nil && debug.RandomizeClientPort {
  730. listenPort = 0
  731. }
  732. isSubnetRouter := false
  733. if e.birdClient != nil && nm != nil && nm.SelfNode != nil {
  734. isSubnetRouter = hasOverlap(nm.SelfNode.PrimaryRoutes, nm.Hostinfo.RoutableIPs)
  735. e.logf("[v1] Reconfig: hasOverlap(%v, %v) = %v; isSubnetRouter=%v lastIsSubnetRouter=%v",
  736. nm.SelfNode.PrimaryRoutes, nm.Hostinfo.RoutableIPs,
  737. isSubnetRouter, isSubnetRouter, e.lastIsSubnetRouter)
  738. }
  739. isSubnetRouterChanged := isSubnetRouter != e.lastIsSubnetRouter
  740. engineChanged := deephash.Update(&e.lastEngineSigFull, cfg)
  741. routerChanged := deephash.Update(&e.lastRouterSig, &struct {
  742. RouterConfig *router.Config
  743. DNSConfig *dns.Config
  744. }{routerCfg, dnsCfg})
  745. if !engineChanged && !routerChanged && listenPort == e.magicConn.LocalPort() && !isSubnetRouterChanged {
  746. return ErrNoChanges
  747. }
  748. newLogIDs := cfg.NetworkLogging
  749. oldLogIDs := e.lastCfgFull.NetworkLogging
  750. netLogIDsNowValid := !newLogIDs.NodeID.IsZero() && !newLogIDs.DomainID.IsZero()
  751. netLogIDsWasValid := !oldLogIDs.NodeID.IsZero() && !oldLogIDs.DomainID.IsZero()
  752. netLogIDsChanged := netLogIDsNowValid && netLogIDsWasValid && newLogIDs != oldLogIDs
  753. netLogRunning := netLogIDsNowValid && !routerCfg.Equal(&router.Config{})
  754. if envknob.NoLogsNoSupport() {
  755. netLogRunning = false
  756. }
  757. // TODO(bradfitz,danderson): maybe delete this isDNSIPOverTailscale
  758. // field and delete the resolver.ForwardLinkSelector hook and
  759. // instead have ipnlocal populate a map of DNS IP => linkName and
  760. // put that in the *dns.Config instead, and plumb it down to the
  761. // dns.Manager. Maybe also with isLocalAddr above.
  762. e.isDNSIPOverTailscale.Store(tsaddr.NewContainsIPFunc(dnsIPsOverTailscale(dnsCfg, routerCfg)))
  763. // See if any peers have changed disco keys, which means they've restarted.
  764. // If so, we need to update the wireguard-go/device.Device in two phases:
  765. // once without the node which has restarted, to clear its wireguard session key,
  766. // and a second time with it.
  767. discoChanged := make(map[key.NodePublic]bool)
  768. {
  769. prevEP := make(map[key.NodePublic]key.DiscoPublic)
  770. for i := range e.lastCfgFull.Peers {
  771. if p := &e.lastCfgFull.Peers[i]; !p.DiscoKey.IsZero() {
  772. prevEP[p.PublicKey] = p.DiscoKey
  773. }
  774. }
  775. for i := range cfg.Peers {
  776. p := &cfg.Peers[i]
  777. if p.DiscoKey.IsZero() {
  778. continue
  779. }
  780. pub := p.PublicKey
  781. if old, ok := prevEP[pub]; ok && old != p.DiscoKey {
  782. discoChanged[pub] = true
  783. e.logf("wgengine: Reconfig: %s changed from %q to %q", pub.ShortString(), old, p.DiscoKey)
  784. }
  785. }
  786. }
  787. e.lastCfgFull = *cfg.Clone()
  788. // Tell magicsock about the new (or initial) private key
  789. // (which is needed by DERP) before wgdev gets it, as wgdev
  790. // will start trying to handshake, which we want to be able to
  791. // go over DERP.
  792. if err := e.magicConn.SetPrivateKey(cfg.PrivateKey); err != nil {
  793. e.logf("wgengine: Reconfig: SetPrivateKey: %v", err)
  794. }
  795. e.magicConn.UpdatePeers(peerSet)
  796. e.magicConn.SetPreferredPort(listenPort)
  797. if err := e.maybeReconfigWireguardLocked(discoChanged); err != nil {
  798. return err
  799. }
  800. // Shutdown the network logger because the IDs changed.
  801. // Let it be started back up by subsequent logic.
  802. if netLogIDsChanged && e.networkLogger.Running() {
  803. e.logf("wgengine: Reconfig: shutting down network logger")
  804. ctx, cancel := context.WithTimeout(context.Background(), networkLoggerUploadTimeout)
  805. defer cancel()
  806. if err := e.networkLogger.Shutdown(ctx); err != nil {
  807. e.logf("wgengine: Reconfig: error shutting down network logger: %v", err)
  808. }
  809. }
  810. // Startup the network logger.
  811. // Do this before configuring the router so that we capture initial packets.
  812. if netLogRunning && !e.networkLogger.Running() {
  813. nid := cfg.NetworkLogging.NodeID
  814. tid := cfg.NetworkLogging.DomainID
  815. e.logf("wgengine: Reconfig: starting up network logger (node:%s tailnet:%s)", nid.Public(), tid.Public())
  816. if err := e.networkLogger.Startup(cfg.NodeID, nid, tid, e.tundev, e.magicConn); err != nil {
  817. e.logf("wgengine: Reconfig: error starting up network logger: %v", err)
  818. }
  819. e.networkLogger.ReconfigRoutes(routerCfg)
  820. }
  821. if routerChanged {
  822. e.logf("wgengine: Reconfig: configuring router")
  823. e.networkLogger.ReconfigRoutes(routerCfg)
  824. err := e.router.Set(routerCfg)
  825. health.SetRouterHealth(err)
  826. if err != nil {
  827. return err
  828. }
  829. // Keep DNS configuration after router configuration, as some
  830. // DNS managers refuse to apply settings if the device has no
  831. // assigned address.
  832. e.logf("wgengine: Reconfig: configuring DNS")
  833. err = e.dns.Set(*dnsCfg)
  834. health.SetDNSHealth(err)
  835. if err != nil {
  836. return err
  837. }
  838. }
  839. // Shutdown the network logger.
  840. // Do this after configuring the router so that we capture final packets.
  841. // This attempts to flush out any log messages and may block.
  842. if !netLogRunning && e.networkLogger.Running() {
  843. e.logf("wgengine: Reconfig: shutting down network logger")
  844. ctx, cancel := context.WithTimeout(context.Background(), networkLoggerUploadTimeout)
  845. defer cancel()
  846. if err := e.networkLogger.Shutdown(ctx); err != nil {
  847. e.logf("wgengine: Reconfig: error shutting down network logger: %v", err)
  848. }
  849. }
  850. if isSubnetRouterChanged && e.birdClient != nil {
  851. e.logf("wgengine: Reconfig: configuring BIRD")
  852. var err error
  853. if isSubnetRouter {
  854. err = e.birdClient.EnableProtocol("tailscale")
  855. } else {
  856. err = e.birdClient.DisableProtocol("tailscale")
  857. }
  858. if err != nil {
  859. // Log but don't fail here.
  860. e.logf("wgengine: error configuring BIRD: %v", err)
  861. } else {
  862. e.lastIsSubnetRouter = isSubnetRouter
  863. }
  864. }
  865. e.logf("[v1] wgengine: Reconfig done")
  866. return nil
  867. }
  868. func (e *userspaceEngine) GetFilter() *filter.Filter {
  869. return e.tundev.GetFilter()
  870. }
  871. func (e *userspaceEngine) SetFilter(filt *filter.Filter) {
  872. e.tundev.SetFilter(filt)
  873. }
  874. func (e *userspaceEngine) SetStatusCallback(cb StatusCallback) {
  875. e.mu.Lock()
  876. defer e.mu.Unlock()
  877. e.statusCallback = cb
  878. }
  879. func (e *userspaceEngine) getStatusCallback() StatusCallback {
  880. e.mu.Lock()
  881. defer e.mu.Unlock()
  882. return e.statusCallback
  883. }
  884. var singleNewline = []byte{'\n'}
  885. var ErrEngineClosing = errors.New("engine closing; no status")
  886. func (e *userspaceEngine) getPeerStatusLite(pk key.NodePublic) (status ipnstate.PeerStatusLite, ok bool) {
  887. e.wgLock.Lock()
  888. if e.wgdev == nil {
  889. e.wgLock.Unlock()
  890. return status, false
  891. }
  892. peer := e.wgdev.LookupPeer(pk.Raw32())
  893. e.wgLock.Unlock()
  894. if peer == nil {
  895. return status, false
  896. }
  897. status.NodeKey = pk
  898. status.RxBytes = int64(wgint.PeerRxBytes(peer))
  899. status.TxBytes = int64(wgint.PeerTxBytes(peer))
  900. status.LastHandshake = time.Unix(0, wgint.PeerLastHandshakeNano(peer))
  901. return status, true
  902. }
  903. func (e *userspaceEngine) getStatus() (*Status, error) {
  904. // Grab derpConns before acquiring wgLock to not violate lock ordering;
  905. // the DERPs method acquires magicsock.Conn.mu.
  906. // (See comment in userspaceEngine's declaration.)
  907. derpConns := e.magicConn.DERPs()
  908. e.mu.Lock()
  909. closing := e.closing
  910. peerKeys := make([]key.NodePublic, len(e.peerSequence))
  911. copy(peerKeys, e.peerSequence)
  912. localAddrs := append([]tailcfg.Endpoint(nil), e.endpoints...)
  913. e.mu.Unlock()
  914. if closing {
  915. return nil, ErrEngineClosing
  916. }
  917. peers := make([]ipnstate.PeerStatusLite, 0, len(peerKeys))
  918. for _, key := range peerKeys {
  919. if status, found := e.getPeerStatusLite(key); found {
  920. peers = append(peers, status)
  921. }
  922. }
  923. return &Status{
  924. AsOf: time.Now(),
  925. LocalAddrs: localAddrs,
  926. Peers: peers,
  927. DERPs: derpConns,
  928. }, nil
  929. }
  930. func (e *userspaceEngine) RequestStatus() {
  931. // This is slightly tricky. e.getStatus() can theoretically get
  932. // blocked inside wireguard for a while, and RequestStatus() is
  933. // sometimes called from a goroutine, so we don't want a lot of
  934. // them hanging around. On the other hand, requesting multiple
  935. // status updates simultaneously is pointless anyway; they will
  936. // all say the same thing.
  937. // Enqueue at most one request. If one is in progress already, this
  938. // adds one more to the queue. If one has been requested but not
  939. // started, it is a no-op.
  940. select {
  941. case e.reqCh <- struct{}{}:
  942. default:
  943. }
  944. // Dequeue at most one request. Another thread may have already
  945. // dequeued the request we enqueued above, which is fine, since the
  946. // information is guaranteed to be at least as recent as the current
  947. // call to RequestStatus().
  948. select {
  949. case <-e.reqCh:
  950. s, err := e.getStatus()
  951. if s == nil && err == nil {
  952. e.logf("[unexpected] RequestStatus: both s and err are nil")
  953. return
  954. }
  955. if cb := e.getStatusCallback(); cb != nil {
  956. cb(s, err)
  957. }
  958. default:
  959. }
  960. }
  961. func (e *userspaceEngine) Close() {
  962. e.mu.Lock()
  963. if e.closing {
  964. e.mu.Unlock()
  965. return
  966. }
  967. e.closing = true
  968. e.mu.Unlock()
  969. r := bufio.NewReader(strings.NewReader(""))
  970. e.wgdev.IpcSetOperation(r)
  971. e.magicConn.Close()
  972. e.linkMonUnregister()
  973. if e.linkMonOwned {
  974. e.linkMon.Close()
  975. }
  976. e.dns.Down()
  977. e.router.Close()
  978. e.wgdev.Close()
  979. e.tundev.Close()
  980. if e.birdClient != nil {
  981. e.birdClient.DisableProtocol("tailscale")
  982. e.birdClient.Close()
  983. }
  984. close(e.waitCh)
  985. ctx, cancel := context.WithTimeout(context.Background(), networkLoggerUploadTimeout)
  986. defer cancel()
  987. if err := e.networkLogger.Shutdown(ctx); err != nil {
  988. e.logf("wgengine: Close: error shutting down network logger: %v", err)
  989. }
  990. }
  991. func (e *userspaceEngine) Wait() {
  992. <-e.waitCh
  993. }
  994. func (e *userspaceEngine) GetLinkMonitor() *monitor.Mon {
  995. return e.linkMon
  996. }
  997. // LinkChange signals a network change event. It's currently
  998. // (2021-03-03) only called on Android. On other platforms, linkMon
  999. // generates link change events for us.
  1000. func (e *userspaceEngine) LinkChange(_ bool) {
  1001. e.linkMon.InjectEvent()
  1002. }
  1003. func (e *userspaceEngine) linkChange(changed bool, cur *interfaces.State) {
  1004. up := cur.AnyInterfaceUp()
  1005. if !up {
  1006. e.logf("LinkChange: all links down; pausing: %v", cur)
  1007. } else if changed {
  1008. e.logf("LinkChange: major, rebinding. New state: %v", cur)
  1009. } else {
  1010. e.logf("[v1] LinkChange: minor")
  1011. }
  1012. health.SetAnyInterfaceUp(up)
  1013. e.magicConn.SetNetworkUp(up)
  1014. if !up || changed {
  1015. if err := e.dns.FlushCaches(); err != nil {
  1016. e.logf("wgengine: dns flush failed after major link change: %v", err)
  1017. }
  1018. }
  1019. // Hacky workaround for Linux DNS issue 2458: on
  1020. // suspend/resume or whenever NetworkManager is started, it
  1021. // nukes all systemd-resolved configs. So reapply our DNS
  1022. // config on major link change.
  1023. if changed {
  1024. switch runtime.GOOS {
  1025. case "linux", "android", "ios", "darwin":
  1026. e.wgLock.Lock()
  1027. dnsCfg := e.lastDNSConfig
  1028. e.wgLock.Unlock()
  1029. if dnsCfg != nil {
  1030. if err := e.dns.Set(*dnsCfg); err != nil {
  1031. e.logf("wgengine: error setting DNS config after major link change: %v", err)
  1032. } else {
  1033. e.logf("wgengine: set DNS config again after major link change")
  1034. }
  1035. }
  1036. }
  1037. }
  1038. why := "link-change-minor"
  1039. if changed {
  1040. why = "link-change-major"
  1041. metricNumMajorChanges.Add(1)
  1042. e.magicConn.Rebind()
  1043. } else {
  1044. metricNumMinorChanges.Add(1)
  1045. }
  1046. e.magicConn.ReSTUN(why)
  1047. }
  1048. func (e *userspaceEngine) AddNetworkMapCallback(cb NetworkMapCallback) func() {
  1049. e.mu.Lock()
  1050. defer e.mu.Unlock()
  1051. if e.networkMapCallbacks == nil {
  1052. e.networkMapCallbacks = make(map[*someHandle]NetworkMapCallback)
  1053. }
  1054. h := new(someHandle)
  1055. e.networkMapCallbacks[h] = cb
  1056. return func() {
  1057. e.mu.Lock()
  1058. defer e.mu.Unlock()
  1059. delete(e.networkMapCallbacks, h)
  1060. }
  1061. }
  1062. func (e *userspaceEngine) SetNetInfoCallback(cb NetInfoCallback) {
  1063. e.magicConn.SetNetInfoCallback(cb)
  1064. }
  1065. func (e *userspaceEngine) SetDERPMap(dm *tailcfg.DERPMap) {
  1066. e.magicConn.SetDERPMap(dm)
  1067. }
  1068. func (e *userspaceEngine) SetNetworkMap(nm *netmap.NetworkMap) {
  1069. e.magicConn.SetNetworkMap(nm)
  1070. e.mu.Lock()
  1071. e.netMap = nm
  1072. callbacks := make([]NetworkMapCallback, 0, 4)
  1073. for _, fn := range e.networkMapCallbacks {
  1074. callbacks = append(callbacks, fn)
  1075. }
  1076. e.mu.Unlock()
  1077. for _, fn := range callbacks {
  1078. fn(nm)
  1079. }
  1080. }
  1081. func (e *userspaceEngine) DiscoPublicKey() key.DiscoPublic {
  1082. return e.magicConn.DiscoPublicKey()
  1083. }
  1084. func (e *userspaceEngine) UpdateStatus(sb *ipnstate.StatusBuilder) {
  1085. st, err := e.getStatus()
  1086. if err != nil {
  1087. e.logf("wgengine: getStatus: %v", err)
  1088. return
  1089. }
  1090. if sb.WantPeers {
  1091. for _, ps := range st.Peers {
  1092. sb.AddPeer(ps.NodeKey, &ipnstate.PeerStatus{
  1093. RxBytes: int64(ps.RxBytes),
  1094. TxBytes: int64(ps.TxBytes),
  1095. LastHandshake: ps.LastHandshake,
  1096. InEngine: true,
  1097. })
  1098. }
  1099. }
  1100. e.magicConn.UpdateStatus(sb)
  1101. }
  1102. func (e *userspaceEngine) Ping(ip netip.Addr, pingType tailcfg.PingType, cb func(*ipnstate.PingResult)) {
  1103. res := &ipnstate.PingResult{IP: ip.String()}
  1104. pip, ok := e.PeerForIP(ip)
  1105. if !ok {
  1106. e.logf("ping(%v): no matching peer", ip)
  1107. res.Err = "no matching peer"
  1108. cb(res)
  1109. return
  1110. }
  1111. if pip.IsSelf {
  1112. res.Err = fmt.Sprintf("%v is local Tailscale IP", ip)
  1113. res.IsLocalIP = true
  1114. cb(res)
  1115. return
  1116. }
  1117. peer := pip.Node
  1118. e.logf("ping(%v): sending %v ping to %v %v ...", ip, pingType, peer.Key.ShortString(), peer.ComputedName)
  1119. switch pingType {
  1120. case "disco":
  1121. e.magicConn.Ping(peer, res, cb)
  1122. case "TSMP":
  1123. e.sendTSMPPing(ip, peer, res, cb)
  1124. case "ICMP":
  1125. e.sendICMPEchoRequest(ip, peer, res, cb)
  1126. }
  1127. }
  1128. func (e *userspaceEngine) mySelfIPMatchingFamily(dst netip.Addr) (src netip.Addr, err error) {
  1129. e.mu.Lock()
  1130. defer e.mu.Unlock()
  1131. if e.netMap == nil {
  1132. return netip.Addr{}, errors.New("no netmap")
  1133. }
  1134. for _, a := range e.netMap.Addresses {
  1135. if a.IsSingleIP() && a.Addr().BitLen() == dst.BitLen() {
  1136. return a.Addr(), nil
  1137. }
  1138. }
  1139. if len(e.netMap.Addresses) == 0 {
  1140. return netip.Addr{}, errors.New("no self address in netmap")
  1141. }
  1142. return netip.Addr{}, errors.New("no self address in netmap matching address family")
  1143. }
  1144. func (e *userspaceEngine) sendICMPEchoRequest(destIP netip.Addr, peer *tailcfg.Node, res *ipnstate.PingResult, cb func(*ipnstate.PingResult)) {
  1145. srcIP, err := e.mySelfIPMatchingFamily(destIP)
  1146. if err != nil {
  1147. res.Err = err.Error()
  1148. cb(res)
  1149. return
  1150. }
  1151. var icmph packet.Header
  1152. if srcIP.Is4() {
  1153. icmph = packet.ICMP4Header{
  1154. IP4Header: packet.IP4Header{
  1155. IPProto: ipproto.ICMPv4,
  1156. Src: srcIP,
  1157. Dst: destIP,
  1158. },
  1159. Type: packet.ICMP4EchoRequest,
  1160. Code: packet.ICMP4NoCode,
  1161. }
  1162. } else {
  1163. icmph = packet.ICMP6Header{
  1164. IP6Header: packet.IP6Header{
  1165. IPProto: ipproto.ICMPv6,
  1166. Src: srcIP,
  1167. Dst: destIP,
  1168. },
  1169. Type: packet.ICMP6EchoRequest,
  1170. Code: packet.ICMP6NoCode,
  1171. }
  1172. }
  1173. idSeq, payload := packet.ICMPEchoPayload(nil)
  1174. expireTimer := time.AfterFunc(10*time.Second, func() {
  1175. e.setICMPEchoResponseCallback(idSeq, nil)
  1176. })
  1177. t0 := time.Now()
  1178. e.setICMPEchoResponseCallback(idSeq, func() {
  1179. expireTimer.Stop()
  1180. d := time.Since(t0)
  1181. res.LatencySeconds = d.Seconds()
  1182. res.NodeIP = destIP.String()
  1183. res.NodeName = peer.ComputedName
  1184. cb(res)
  1185. })
  1186. icmpPing := packet.Generate(icmph, payload)
  1187. e.tundev.InjectOutbound(icmpPing)
  1188. }
  1189. func (e *userspaceEngine) sendTSMPPing(ip netip.Addr, peer *tailcfg.Node, res *ipnstate.PingResult, cb func(*ipnstate.PingResult)) {
  1190. srcIP, err := e.mySelfIPMatchingFamily(ip)
  1191. if err != nil {
  1192. res.Err = err.Error()
  1193. cb(res)
  1194. return
  1195. }
  1196. var iph packet.Header
  1197. if srcIP.Is4() {
  1198. iph = packet.IP4Header{
  1199. IPProto: ipproto.TSMP,
  1200. Src: srcIP,
  1201. Dst: ip,
  1202. }
  1203. } else {
  1204. iph = packet.IP6Header{
  1205. IPProto: ipproto.TSMP,
  1206. Src: srcIP,
  1207. Dst: ip,
  1208. }
  1209. }
  1210. var data [8]byte
  1211. crand.Read(data[:])
  1212. expireTimer := time.AfterFunc(10*time.Second, func() {
  1213. e.setTSMPPongCallback(data, nil)
  1214. })
  1215. t0 := time.Now()
  1216. e.setTSMPPongCallback(data, func(pong packet.TSMPPongReply) {
  1217. expireTimer.Stop()
  1218. d := time.Since(t0)
  1219. res.LatencySeconds = d.Seconds()
  1220. res.NodeIP = ip.String()
  1221. res.NodeName = peer.ComputedName
  1222. res.PeerAPIPort = pong.PeerAPIPort
  1223. cb(res)
  1224. })
  1225. var tsmpPayload [9]byte
  1226. tsmpPayload[0] = byte(packet.TSMPTypePing)
  1227. copy(tsmpPayload[1:], data[:])
  1228. tsmpPing := packet.Generate(iph, tsmpPayload[:])
  1229. e.tundev.InjectOutbound(tsmpPing)
  1230. }
  1231. func (e *userspaceEngine) setTSMPPongCallback(data [8]byte, cb func(packet.TSMPPongReply)) {
  1232. e.mu.Lock()
  1233. defer e.mu.Unlock()
  1234. if e.pongCallback == nil {
  1235. e.pongCallback = map[[8]byte]func(packet.TSMPPongReply){}
  1236. }
  1237. if cb == nil {
  1238. delete(e.pongCallback, data)
  1239. } else {
  1240. e.pongCallback[data] = cb
  1241. }
  1242. }
  1243. func (e *userspaceEngine) setICMPEchoResponseCallback(idSeq uint32, cb func()) {
  1244. e.mu.Lock()
  1245. defer e.mu.Unlock()
  1246. if cb == nil {
  1247. delete(e.icmpEchoResponseCallback, idSeq)
  1248. } else {
  1249. mak.Set(&e.icmpEchoResponseCallback, idSeq, cb)
  1250. }
  1251. }
  1252. func (e *userspaceEngine) RegisterIPPortIdentity(ipport netip.AddrPort, tsIP netip.Addr) {
  1253. e.mu.Lock()
  1254. defer e.mu.Unlock()
  1255. if e.tsIPByIPPort == nil {
  1256. e.tsIPByIPPort = make(map[netip.AddrPort]netip.Addr)
  1257. }
  1258. e.tsIPByIPPort[ipport] = tsIP
  1259. }
  1260. func (e *userspaceEngine) UnregisterIPPortIdentity(ipport netip.AddrPort) {
  1261. e.mu.Lock()
  1262. defer e.mu.Unlock()
  1263. if e.tsIPByIPPort == nil {
  1264. return
  1265. }
  1266. delete(e.tsIPByIPPort, ipport)
  1267. }
  1268. var whoIsSleeps = [...]time.Duration{
  1269. 0,
  1270. 10 * time.Millisecond,
  1271. 20 * time.Millisecond,
  1272. 50 * time.Millisecond,
  1273. 100 * time.Millisecond,
  1274. }
  1275. func (e *userspaceEngine) WhoIsIPPort(ipport netip.AddrPort) (tsIP netip.Addr, ok bool) {
  1276. // We currently have a registration race,
  1277. // https://github.com/tailscale/tailscale/issues/1616,
  1278. // so loop a few times for now waiting for the registration
  1279. // to appear.
  1280. // TODO(bradfitz,namansood): remove this once #1616 is fixed.
  1281. for _, d := range whoIsSleeps {
  1282. time.Sleep(d)
  1283. e.mu.Lock()
  1284. tsIP, ok = e.tsIPByIPPort[ipport]
  1285. e.mu.Unlock()
  1286. if ok {
  1287. return tsIP, true
  1288. }
  1289. }
  1290. return tsIP, false
  1291. }
  1292. // PeerForIP returns the Node in the wireguard config
  1293. // that's responsible for handling the given IP address.
  1294. //
  1295. // If none is found in the wireguard config but one is found in
  1296. // the netmap, it's described in an error.
  1297. //
  1298. // peerForIP acquires both e.mu and e.wgLock, but neither at the same
  1299. // time.
  1300. func (e *userspaceEngine) PeerForIP(ip netip.Addr) (ret PeerForIP, ok bool) {
  1301. e.mu.Lock()
  1302. nm := e.netMap
  1303. e.mu.Unlock()
  1304. if nm == nil {
  1305. return ret, false
  1306. }
  1307. // Check for exact matches before looking for subnet matches.
  1308. // TODO(bradfitz): add maps for these. on NetworkMap?
  1309. for _, p := range nm.Peers {
  1310. for _, a := range p.Addresses {
  1311. if a.Addr() == ip && a.IsSingleIP() && tsaddr.IsTailscaleIP(ip) {
  1312. return PeerForIP{Node: p, Route: a}, true
  1313. }
  1314. }
  1315. }
  1316. for _, a := range nm.Addresses {
  1317. if a.Addr() == ip && a.IsSingleIP() && tsaddr.IsTailscaleIP(ip) {
  1318. return PeerForIP{Node: nm.SelfNode, IsSelf: true, Route: a}, true
  1319. }
  1320. }
  1321. e.wgLock.Lock()
  1322. defer e.wgLock.Unlock()
  1323. // TODO(bradfitz): this is O(n peers). Add ART to netaddr?
  1324. var best netip.Prefix
  1325. var bestKey key.NodePublic
  1326. for _, p := range e.lastCfgFull.Peers {
  1327. for _, cidr := range p.AllowedIPs {
  1328. if !cidr.Contains(ip) {
  1329. continue
  1330. }
  1331. if !best.IsValid() || cidr.Bits() > best.Bits() {
  1332. best = cidr
  1333. bestKey = p.PublicKey
  1334. }
  1335. }
  1336. }
  1337. // And another pass. Probably better than allocating a map per peerForIP
  1338. // call. But TODO(bradfitz): add a lookup map to netmap.NetworkMap.
  1339. if !bestKey.IsZero() {
  1340. for _, p := range nm.Peers {
  1341. if p.Key == bestKey {
  1342. return PeerForIP{Node: p, Route: best}, true
  1343. }
  1344. }
  1345. }
  1346. return ret, false
  1347. }
  1348. type closeOnErrorPool []func()
  1349. func (p *closeOnErrorPool) add(c io.Closer) { *p = append(*p, func() { c.Close() }) }
  1350. func (p *closeOnErrorPool) addFunc(fn func()) { *p = append(*p, fn) }
  1351. func (p closeOnErrorPool) closeAllIfError(errp *error) {
  1352. if *errp != nil {
  1353. for _, closeFn := range p {
  1354. closeFn()
  1355. }
  1356. }
  1357. }
  1358. // ipInPrefixes reports whether ip is in any of pp.
  1359. func ipInPrefixes(ip netip.Addr, pp []netip.Prefix) bool {
  1360. for _, p := range pp {
  1361. if p.Contains(ip) {
  1362. return true
  1363. }
  1364. }
  1365. return false
  1366. }
  1367. // dnsIPsOverTailscale returns the IPPrefixes of DNS resolver IPs that are
  1368. // routed over Tailscale. The returned value does not contain duplicates is
  1369. // not necessarily sorted.
  1370. func dnsIPsOverTailscale(dnsCfg *dns.Config, routerCfg *router.Config) (ret []netip.Prefix) {
  1371. m := map[netip.Addr]bool{}
  1372. add := func(resolvers []*dnstype.Resolver) {
  1373. for _, r := range resolvers {
  1374. ip, err := netip.ParseAddr(r.Addr)
  1375. if err != nil {
  1376. if ipp, err := netip.ParseAddrPort(r.Addr); err == nil {
  1377. ip = ipp.Addr()
  1378. } else {
  1379. continue
  1380. }
  1381. }
  1382. if ipInPrefixes(ip, routerCfg.Routes) && !ipInPrefixes(ip, routerCfg.LocalRoutes) {
  1383. m[ip] = true
  1384. }
  1385. }
  1386. }
  1387. add(dnsCfg.DefaultResolvers)
  1388. for _, resolvers := range dnsCfg.Routes {
  1389. add(resolvers)
  1390. }
  1391. ret = make([]netip.Prefix, 0, len(m))
  1392. for ip := range m {
  1393. ret = append(ret, netip.PrefixFrom(ip, ip.BitLen()))
  1394. }
  1395. return ret
  1396. }
  1397. // fwdDNSLinkSelector is userspaceEngine's resolver.ForwardLinkSelector, to pick
  1398. // which network interface to send DNS queries out of.
  1399. type fwdDNSLinkSelector struct {
  1400. ue *userspaceEngine
  1401. tunName string
  1402. }
  1403. func (ls fwdDNSLinkSelector) PickLink(ip netip.Addr) (linkName string) {
  1404. if ls.ue.isDNSIPOverTailscale.Load()(ip) {
  1405. return ls.tunName
  1406. }
  1407. return ""
  1408. }
  1409. var (
  1410. metricMagicDNSPacketIn = clientmetric.NewCounter("magicdns_packet_in") // for 100.100.100.100
  1411. metricReflectToOS = clientmetric.NewCounter("packet_reflect_to_os")
  1412. metricNumMajorChanges = clientmetric.NewCounter("wgengine_major_changes")
  1413. metricNumMinorChanges = clientmetric.NewCounter("wgengine_minor_changes")
  1414. )
  1415. func (e *userspaceEngine) InstallCaptureHook(cb capture.Callback) {
  1416. e.tundev.InstallCaptureHook(cb)
  1417. e.magicConn.InstallCaptureHook(cb)
  1418. }