wgengine.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package wgengine
  4. import (
  5. "errors"
  6. "net/netip"
  7. "time"
  8. "tailscale.com/ipn/ipnstate"
  9. "tailscale.com/net/dns"
  10. "tailscale.com/tailcfg"
  11. "tailscale.com/types/key"
  12. "tailscale.com/types/netmap"
  13. "tailscale.com/wgengine/capture"
  14. "tailscale.com/wgengine/filter"
  15. "tailscale.com/wgengine/monitor"
  16. "tailscale.com/wgengine/router"
  17. "tailscale.com/wgengine/wgcfg"
  18. )
  19. // Status is the Engine status.
  20. //
  21. // TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
  22. type Status struct {
  23. AsOf time.Time // the time at which the status was calculated
  24. Peers []ipnstate.PeerStatusLite
  25. LocalAddrs []tailcfg.Endpoint // the set of possible endpoints for the magic conn
  26. DERPs int // number of active DERP connections
  27. }
  28. // StatusCallback is the type of status callbacks used by
  29. // Engine.SetStatusCallback.
  30. //
  31. // Exactly one of Status or error is non-nil.
  32. type StatusCallback func(*Status, error)
  33. // NetInfoCallback is the type used by Engine.SetNetInfoCallback.
  34. type NetInfoCallback func(*tailcfg.NetInfo)
  35. // NetworkMapCallback is the type used by callbacks that hook
  36. // into network map updates.
  37. type NetworkMapCallback func(*netmap.NetworkMap)
  38. // someHandle is allocated so its pointer address acts as a unique
  39. // map key handle. (It needs to have non-zero size for Go to guarantee
  40. // the pointer is unique.)
  41. type someHandle struct{ _ byte }
  42. // ErrNoChanges is returned by Engine.Reconfig if no changes were made.
  43. var ErrNoChanges = errors.New("no changes made to Engine config")
  44. // PeerForIP is the type returned by Engine.PeerForIP.
  45. type PeerForIP struct {
  46. // Node is the matched node. It's always non-nil when
  47. // Engine.PeerForIP returns ok==true.
  48. Node *tailcfg.Node
  49. // IsSelf is whether the Node is the local process.
  50. IsSelf bool
  51. // Route is the route that matched the IP provided
  52. // to Engine.PeerForIP.
  53. Route netip.Prefix
  54. }
  55. // Engine is the Tailscale WireGuard engine interface.
  56. type Engine interface {
  57. // Reconfig reconfigures WireGuard and makes sure it's running.
  58. // This also handles setting up any kernel routes.
  59. //
  60. // This is called whenever tailcontrol (the control plane)
  61. // sends an updated network map.
  62. //
  63. // The *tailcfg.Debug parameter can be nil.
  64. //
  65. // The returned error is ErrNoChanges if no changes were made.
  66. Reconfig(*wgcfg.Config, *router.Config, *dns.Config, *tailcfg.Debug) error
  67. // PeerForIP returns the node to which the provided IP routes,
  68. // if any. If none is found, (nil, false) is returned.
  69. PeerForIP(netip.Addr) (_ PeerForIP, ok bool)
  70. // GetFilter returns the current packet filter, if any.
  71. GetFilter() *filter.Filter
  72. // SetFilter updates the packet filter.
  73. SetFilter(*filter.Filter)
  74. // SetStatusCallback sets the function to call when the
  75. // WireGuard status changes.
  76. SetStatusCallback(StatusCallback)
  77. // GetLinkMonitor returns the link monitor.
  78. GetLinkMonitor() *monitor.Mon
  79. // RequestStatus requests a WireGuard status update right
  80. // away, sent to the callback registered via SetStatusCallback.
  81. RequestStatus()
  82. // Close shuts down this wireguard instance, remove any routes
  83. // it added, etc. To bring it up again later, you'll need a
  84. // new Engine.
  85. Close()
  86. // Wait waits until the Engine's Close method is called or the
  87. // engine aborts with an error. You don't have to call this.
  88. // TODO: return an error?
  89. Wait()
  90. // LinkChange informs the engine that the system network
  91. // link has changed.
  92. //
  93. // The isExpensive parameter is not used.
  94. //
  95. // LinkChange should be called whenever something changed with
  96. // the network, no matter how minor.
  97. //
  98. // Deprecated: don't use this method. It was removed shortly
  99. // before the Tailscale 1.6 release when we remembered that
  100. // Android doesn't use the Linux-based link monitor and has
  101. // its own mechanism that uses LinkChange. Android is the only
  102. // caller of this method now. Don't add more.
  103. LinkChange(isExpensive bool)
  104. // SetDERPMap controls which (if any) DERP servers are used.
  105. // If nil, DERP is disabled. It starts disabled until a DERP map
  106. // is configured.
  107. SetDERPMap(*tailcfg.DERPMap)
  108. // SetNetworkMap informs the engine of the latest network map
  109. // from the server. The network map's DERPMap field should be
  110. // ignored as as it might be disabled; get it from SetDERPMap
  111. // instead.
  112. // The network map should only be read from.
  113. SetNetworkMap(*netmap.NetworkMap)
  114. // AddNetworkMapCallback adds a function to a list of callbacks
  115. // that are called when the network map updates. It returns a
  116. // function that when called would remove the function from the
  117. // list of callbacks.
  118. AddNetworkMapCallback(NetworkMapCallback) (removeCallback func())
  119. // SetNetInfoCallback sets the function to call when a
  120. // new NetInfo summary is available.
  121. SetNetInfoCallback(NetInfoCallback)
  122. // DiscoPublicKey gets the public key used for path discovery
  123. // messages.
  124. DiscoPublicKey() key.DiscoPublic
  125. // UpdateStatus populates the network state using the provided
  126. // status builder.
  127. UpdateStatus(*ipnstate.StatusBuilder)
  128. // Ping is a request to start a ping with the peer handling the given IP and
  129. // then call cb with its ping latency & method.
  130. Ping(ip netip.Addr, pingType tailcfg.PingType, cb func(*ipnstate.PingResult))
  131. // RegisterIPPortIdentity registers a given node (identified by its
  132. // Tailscale IP) as temporarily having the given IP:port for whois lookups.
  133. // The IP:port is generally a localhost IP and an ephemeral port, used
  134. // while proxying connections to localhost when tailscaled is running
  135. // in netstack mode.
  136. RegisterIPPortIdentity(netip.AddrPort, netip.Addr)
  137. // UnregisterIPPortIdentity removes a temporary IP:port registration
  138. // made previously by RegisterIPPortIdentity.
  139. UnregisterIPPortIdentity(netip.AddrPort)
  140. // WhoIsIPPort looks up an IP:port in the temporary registrations,
  141. // and returns a matching Tailscale IP, if it exists.
  142. WhoIsIPPort(netip.AddrPort) (netip.Addr, bool)
  143. // InstallCaptureHook registers a function to be called to capture
  144. // packets traversing the data path. The hook can be uninstalled by
  145. // calling this function with a nil value.
  146. InstallCaptureHook(capture.Callback)
  147. }