userspace.go 55 KB

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