wgengine.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package wgengine
  5. import (
  6. "errors"
  7. "inet.af/netaddr"
  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/filter"
  14. "tailscale.com/wgengine/monitor"
  15. "tailscale.com/wgengine/router"
  16. "tailscale.com/wgengine/wgcfg"
  17. )
  18. // Status is the Engine status.
  19. //
  20. // TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
  21. type Status struct {
  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. // NetInfoCallback is the type used by Engine.SetNetInfoCallback.
  32. type NetInfoCallback func(*tailcfg.NetInfo)
  33. // NetworkMapCallback is the type used by callbacks that hook
  34. // into network map updates.
  35. type NetworkMapCallback func(*netmap.NetworkMap)
  36. // someHandle is allocated so its pointer address acts as a unique
  37. // map key handle. (It needs to have non-zero size for Go to guarantee
  38. // the pointer is unique.)
  39. type someHandle struct{ _ byte }
  40. // ErrNoChanges is returned by Engine.Reconfig if no changes were made.
  41. var ErrNoChanges = errors.New("no changes made to Engine config")
  42. // Engine is the Tailscale WireGuard engine interface.
  43. type Engine interface {
  44. // Reconfig reconfigures WireGuard and makes sure it's running.
  45. // This also handles setting up any kernel routes.
  46. //
  47. // This is called whenever tailcontrol (the control plane)
  48. // sends an updated network map.
  49. //
  50. // The *tailcfg.Debug parameter can be nil.
  51. //
  52. // The returned error is ErrNoChanges if no changes were made.
  53. Reconfig(*wgcfg.Config, *router.Config, *dns.Config, *tailcfg.Debug) error
  54. // GetFilter returns the current packet filter, if any.
  55. GetFilter() *filter.Filter
  56. // SetFilter updates the packet filter.
  57. SetFilter(*filter.Filter)
  58. // SetStatusCallback sets the function to call when the
  59. // WireGuard status changes.
  60. SetStatusCallback(StatusCallback)
  61. // GetLinkMonitor returns the link monitor.
  62. GetLinkMonitor() *monitor.Mon
  63. // RequestStatus requests a WireGuard status update right
  64. // away, sent to the callback registered via SetStatusCallback.
  65. RequestStatus()
  66. // Close shuts down this wireguard instance, remove any routes
  67. // it added, etc. To bring it up again later, you'll need a
  68. // new Engine.
  69. Close()
  70. // Wait waits until the Engine's Close method is called or the
  71. // engine aborts with an error. You don't have to call this.
  72. // TODO: return an error?
  73. Wait()
  74. // LinkChange informs the engine that the system network
  75. // link has changed.
  76. //
  77. // The isExpensive parameter is not used.
  78. //
  79. // LinkChange should be called whenever something changed with
  80. // the network, no matter how minor.
  81. //
  82. // Deprecated: don't use this method. It was removed shortly
  83. // before the Tailscale 1.6 release when we remembered that
  84. // Android doesn't use the Linux-based link monitor and has
  85. // its own mechanism that uses LinkChange. Android is the only
  86. // caller of this method now. Don't add more.
  87. LinkChange(isExpensive bool)
  88. // SetDERPMap controls which (if any) DERP servers are used.
  89. // If nil, DERP is disabled. It starts disabled until a DERP map
  90. // is configured.
  91. SetDERPMap(*tailcfg.DERPMap)
  92. // SetNetworkMap informs the engine of the latest network map
  93. // from the server. The network map's DERPMap field should be
  94. // ignored as as it might be disabled; get it from SetDERPMap
  95. // instead.
  96. // The network map should only be read from.
  97. SetNetworkMap(*netmap.NetworkMap)
  98. // AddNetworkMapCallback adds a function to a list of callbacks
  99. // that are called when the network map updates. It returns a
  100. // function that when called would remove the function from the
  101. // list of callbacks.
  102. AddNetworkMapCallback(NetworkMapCallback) (removeCallback func())
  103. // SetNetInfoCallback sets the function to call when a
  104. // new NetInfo summary is available.
  105. SetNetInfoCallback(NetInfoCallback)
  106. // DiscoPublicKey gets the public key used for path discovery
  107. // messages.
  108. DiscoPublicKey() key.DiscoPublic
  109. // UpdateStatus populates the network state using the provided
  110. // status builder.
  111. UpdateStatus(*ipnstate.StatusBuilder)
  112. // Ping is a request to start a discovery ping with the peer handling
  113. // the given IP and then call cb with its ping latency & method.
  114. Ping(ip netaddr.IP, useTSMP bool, cb func(*ipnstate.PingResult))
  115. // RegisterIPPortIdentity registers a given node (identified by its
  116. // Tailscale IP) as temporarily having the given IP:port for whois lookups.
  117. // The IP:port is generally a localhost IP and an ephemeral port, used
  118. // while proxying connections to localhost.
  119. RegisterIPPortIdentity(netaddr.IPPort, netaddr.IP)
  120. // UnregisterIPPortIdentity removes a temporary IP:port registration.
  121. UnregisterIPPortIdentity(netaddr.IPPort)
  122. // WhoIsIPPort looks up an IP:port in the temporary registrations,
  123. // and returns a matching Tailscale IP, if it exists.
  124. WhoIsIPPort(netaddr.IPPort) (netaddr.IP, bool)
  125. }