wgengine.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package wgengine provides the Tailscale WireGuard engine interface.
  4. package wgengine
  5. import (
  6. "errors"
  7. "net/netip"
  8. "time"
  9. "tailscale.com/ipn/ipnstate"
  10. "tailscale.com/net/dns"
  11. "tailscale.com/net/packet"
  12. "tailscale.com/tailcfg"
  13. "tailscale.com/types/key"
  14. "tailscale.com/types/netmap"
  15. "tailscale.com/wgengine/filter"
  16. "tailscale.com/wgengine/router"
  17. "tailscale.com/wgengine/wgcfg"
  18. "tailscale.com/wgengine/wgint"
  19. )
  20. // Status is the Engine status.
  21. //
  22. // TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
  23. type Status struct {
  24. AsOf time.Time // the time at which the status was calculated
  25. Peers []ipnstate.PeerStatusLite
  26. LocalAddrs []tailcfg.Endpoint // the set of possible endpoints for the magic conn
  27. DERPs int // number of active DERP connections
  28. }
  29. // StatusCallback is the type of status callbacks used by
  30. // Engine.SetStatusCallback.
  31. //
  32. // Exactly one of Status or error is non-nil.
  33. type StatusCallback func(*Status, error)
  34. // NetworkMapCallback is the type used by callbacks that hook
  35. // into network map updates.
  36. type NetworkMapCallback func(*netmap.NetworkMap)
  37. // ErrNoChanges is returned by Engine.Reconfig if no changes were made.
  38. var ErrNoChanges = errors.New("no changes made to Engine config")
  39. // PeerForIP is the type returned by Engine.PeerForIP.
  40. type PeerForIP struct {
  41. // Node is the matched node. It's always a valid value when
  42. // Engine.PeerForIP returns ok==true.
  43. Node tailcfg.NodeView
  44. // IsSelf is whether the Node is the local process.
  45. IsSelf bool
  46. // Route is the route that matched the IP provided
  47. // to Engine.PeerForIP.
  48. Route netip.Prefix
  49. }
  50. // Engine is the Tailscale WireGuard engine interface.
  51. type Engine interface {
  52. // Reconfig reconfigures WireGuard and makes sure it's running.
  53. // This also handles setting up any kernel routes.
  54. //
  55. // This is called whenever tailcontrol (the control plane)
  56. // sends an updated network map.
  57. //
  58. // The returned error is ErrNoChanges if no changes were made.
  59. Reconfig(*wgcfg.Config, *router.Config, *dns.Config) error
  60. // ResetAndStop resets the engine to a clean state (like calling Reconfig
  61. // with all pointers to zero values) and waits for it to be fully stopped,
  62. // with no live peers or DERPs.
  63. //
  64. // Unlike Reconfig, it does not return ErrNoChanges.
  65. ResetAndStop() (*Status, error)
  66. // PeerForIP returns the node to which the provided IP routes,
  67. // if any. If none is found, (nil, false) is returned.
  68. PeerForIP(netip.Addr) (_ PeerForIP, ok bool)
  69. // GetFilter returns the current packet filter, if any.
  70. GetFilter() *filter.Filter
  71. // SetFilter updates the packet filter.
  72. SetFilter(*filter.Filter)
  73. // GetJailedFilter returns the current packet filter for jailed nodes,
  74. // if any.
  75. GetJailedFilter() *filter.Filter
  76. // SetJailedFilter updates the packet filter for jailed nodes.
  77. SetJailedFilter(*filter.Filter)
  78. // SetStatusCallback sets the function to call when the
  79. // WireGuard status changes.
  80. SetStatusCallback(StatusCallback)
  81. // RequestStatus requests a WireGuard status update right
  82. // away, sent to the callback registered via SetStatusCallback.
  83. RequestStatus()
  84. // PeerByKey returns the WireGuard status of the provided peer.
  85. // If the peer is not found, ok is false.
  86. PeerByKey(key.NodePublic) (_ wgint.Peer, ok bool)
  87. // Close shuts down this wireguard instance, remove any routes
  88. // it added, etc. To bring it up again later, you'll need a
  89. // new Engine.
  90. Close()
  91. // Done returns a channel that is closed when the Engine's
  92. // Close method is called, the engine aborts with an error,
  93. // or it shuts down due to the closure of the underlying device.
  94. // You don't have to call this.
  95. Done() <-chan struct{}
  96. // SetNetworkMap informs the engine of the latest network map
  97. // from the server. The network map's DERPMap field should be
  98. // ignored as as it might be disabled; get it from SetDERPMap
  99. // instead.
  100. // The network map should only be read from.
  101. SetNetworkMap(*netmap.NetworkMap)
  102. // UpdateStatus populates the network state using the provided
  103. // status builder.
  104. UpdateStatus(*ipnstate.StatusBuilder)
  105. // Ping is a request to start a ping of the given message size to the peer
  106. // handling the given IP, then call cb with its ping latency & method.
  107. //
  108. // If size is zero too small, it is ignored. See tailscale.PingOpts for details.
  109. Ping(ip netip.Addr, pingType tailcfg.PingType, size int, cb func(*ipnstate.PingResult))
  110. // InstallCaptureHook registers a function to be called to capture
  111. // packets traversing the data path. The hook can be uninstalled by
  112. // calling this function with a nil value.
  113. InstallCaptureHook(packet.CaptureCallback)
  114. }