userspace.go 44 KB

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