userspace.go 45 KB

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