wgengine.go 3.8 KB

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