control.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package nebula
  2. import (
  3. "context"
  4. "net/netip"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "github.com/sirupsen/logrus"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/header"
  11. "github.com/slackhq/nebula/overlay"
  12. )
  13. // Every interaction here needs to take extra care to copy memory and not return or use arguments "as is" when touching
  14. // core. This means copying IP objects, slices, de-referencing pointers and taking the actual value, etc
  15. type controlEach func(h *HostInfo)
  16. type controlHostLister interface {
  17. QueryVpnIp(vpnIp netip.Addr) *HostInfo
  18. ForEachIndex(each controlEach)
  19. ForEachVpnIp(each controlEach)
  20. GetPreferredRanges() []netip.Prefix
  21. }
  22. type Control struct {
  23. f *Interface
  24. l *logrus.Logger
  25. ctx context.Context
  26. cancel context.CancelFunc
  27. sshStart func()
  28. statsStart func()
  29. dnsStart func()
  30. lighthouseStart func()
  31. }
  32. type ControlHostInfo struct {
  33. VpnIp netip.Addr `json:"vpnIp"`
  34. LocalIndex uint32 `json:"localIndex"`
  35. RemoteIndex uint32 `json:"remoteIndex"`
  36. RemoteAddrs []netip.AddrPort `json:"remoteAddrs"`
  37. Cert *cert.NebulaCertificate `json:"cert"`
  38. MessageCounter uint64 `json:"messageCounter"`
  39. CurrentRemote netip.AddrPort `json:"currentRemote"`
  40. CurrentRelaysToMe []netip.Addr `json:"currentRelaysToMe"`
  41. CurrentRelaysThroughMe []netip.Addr `json:"currentRelaysThroughMe"`
  42. }
  43. // Start actually runs nebula, this is a nonblocking call. To block use Control.ShutdownBlock()
  44. func (c *Control) Start() {
  45. // Activate the interface
  46. c.f.activate()
  47. // Call all the delayed funcs that waited patiently for the interface to be created.
  48. if c.sshStart != nil {
  49. go c.sshStart()
  50. }
  51. if c.statsStart != nil {
  52. go c.statsStart()
  53. }
  54. if c.dnsStart != nil {
  55. go c.dnsStart()
  56. }
  57. if c.lighthouseStart != nil {
  58. c.lighthouseStart()
  59. }
  60. // Start reading packets.
  61. c.f.run()
  62. }
  63. func (c *Control) Context() context.Context {
  64. return c.ctx
  65. }
  66. // Stop signals nebula to shutdown and close all tunnels, returns after the shutdown is complete
  67. func (c *Control) Stop() {
  68. // Stop the handshakeManager (and other services), to prevent new tunnels from
  69. // being created while we're shutting them all down.
  70. c.cancel()
  71. c.CloseAllTunnels(false)
  72. if err := c.f.Close(); err != nil {
  73. c.l.WithError(err).Error("Close interface failed")
  74. }
  75. c.l.Info("Goodbye")
  76. }
  77. // ShutdownBlock will listen for and block on term and interrupt signals, calling Control.Stop() once signalled
  78. func (c *Control) ShutdownBlock() {
  79. sigChan := make(chan os.Signal, 1)
  80. signal.Notify(sigChan, syscall.SIGTERM)
  81. signal.Notify(sigChan, syscall.SIGINT)
  82. rawSig := <-sigChan
  83. sig := rawSig.String()
  84. c.l.WithField("signal", sig).Info("Caught signal, shutting down")
  85. c.Stop()
  86. }
  87. // RebindUDPServer asks the UDP listener to rebind it's listener. Mainly used on mobile clients when interfaces change
  88. func (c *Control) RebindUDPServer() {
  89. _ = c.f.outside.Rebind()
  90. // Trigger a lighthouse update, useful for mobile clients that should have an update interval of 0
  91. c.f.lightHouse.SendUpdate()
  92. // Let the main interface know that we rebound so that underlying tunnels know to trigger punches from their remotes
  93. c.f.rebindCount++
  94. }
  95. // ListHostmapHosts returns details about the actual or pending (handshaking) hostmap by vpn ip
  96. func (c *Control) ListHostmapHosts(pendingMap bool) []ControlHostInfo {
  97. if pendingMap {
  98. return listHostMapHosts(c.f.handshakeManager)
  99. } else {
  100. return listHostMapHosts(c.f.hostMap)
  101. }
  102. }
  103. // ListHostmapIndexes returns details about the actual or pending (handshaking) hostmap by local index id
  104. func (c *Control) ListHostmapIndexes(pendingMap bool) []ControlHostInfo {
  105. if pendingMap {
  106. return listHostMapIndexes(c.f.handshakeManager)
  107. } else {
  108. return listHostMapIndexes(c.f.hostMap)
  109. }
  110. }
  111. // GetHostInfoByVpnIp returns a single tunnels hostInfo, or nil if not found
  112. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  113. func (c *Control) GetHostInfoByVpnIp(vpnIp netip.Addr, pending bool) *ControlHostInfo {
  114. var hl controlHostLister
  115. if pending {
  116. hl = c.f.handshakeManager
  117. } else {
  118. hl = c.f.hostMap
  119. }
  120. h := hl.QueryVpnIp(vpnIp)
  121. if h == nil {
  122. return nil
  123. }
  124. ch := copyHostInfo(h, c.f.hostMap.GetPreferredRanges())
  125. return &ch
  126. }
  127. // SetRemoteForTunnel forces a tunnel to use a specific remote
  128. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  129. func (c *Control) SetRemoteForTunnel(vpnIp netip.Addr, addr netip.AddrPort) *ControlHostInfo {
  130. hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
  131. if hostInfo == nil {
  132. return nil
  133. }
  134. hostInfo.SetRemote(addr)
  135. ch := copyHostInfo(hostInfo, c.f.hostMap.GetPreferredRanges())
  136. return &ch
  137. }
  138. // CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
  139. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  140. func (c *Control) CloseTunnel(vpnIp netip.Addr, localOnly bool) bool {
  141. hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
  142. if hostInfo == nil {
  143. return false
  144. }
  145. if !localOnly {
  146. c.f.send(
  147. header.CloseTunnel,
  148. 0,
  149. hostInfo.ConnectionState,
  150. hostInfo,
  151. []byte{},
  152. make([]byte, 12, 12),
  153. make([]byte, mtu),
  154. )
  155. }
  156. c.f.closeTunnel(hostInfo)
  157. return true
  158. }
  159. // CloseAllTunnels is just like CloseTunnel except it goes through and shuts them all down, optionally you can avoid shutting down lighthouse tunnels
  160. // the int returned is a count of tunnels closed
  161. func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
  162. //TODO: this is probably better as a function in ConnectionManager or HostMap directly
  163. lighthouses := c.f.lightHouse.GetLighthouses()
  164. shutdown := func(h *HostInfo) {
  165. if excludeLighthouses {
  166. if _, ok := lighthouses[h.vpnIp]; ok {
  167. return
  168. }
  169. }
  170. c.f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  171. c.f.closeTunnel(h)
  172. c.l.WithField("vpnIp", h.vpnIp).WithField("udpAddr", h.remote).
  173. Debug("Sending close tunnel message")
  174. closed++
  175. }
  176. // Learn which hosts are being used as relays, so we can shut them down last.
  177. relayingHosts := map[netip.Addr]*HostInfo{}
  178. // Grab the hostMap lock to access the Relays map
  179. c.f.hostMap.Lock()
  180. for _, relayingHost := range c.f.hostMap.Relays {
  181. relayingHosts[relayingHost.vpnIp] = relayingHost
  182. }
  183. c.f.hostMap.Unlock()
  184. hostInfos := []*HostInfo{}
  185. // Grab the hostMap lock to access the Hosts map
  186. c.f.hostMap.Lock()
  187. for _, relayHost := range c.f.hostMap.Indexes {
  188. if _, ok := relayingHosts[relayHost.vpnIp]; !ok {
  189. hostInfos = append(hostInfos, relayHost)
  190. }
  191. }
  192. c.f.hostMap.Unlock()
  193. for _, h := range hostInfos {
  194. shutdown(h)
  195. }
  196. for _, h := range relayingHosts {
  197. shutdown(h)
  198. }
  199. return
  200. }
  201. func (c *Control) Device() overlay.Device {
  202. return c.f.inside
  203. }
  204. func copyHostInfo(h *HostInfo, preferredRanges []netip.Prefix) ControlHostInfo {
  205. chi := ControlHostInfo{
  206. VpnIp: h.vpnIp,
  207. LocalIndex: h.localIndexId,
  208. RemoteIndex: h.remoteIndexId,
  209. RemoteAddrs: h.remotes.CopyAddrs(preferredRanges),
  210. CurrentRelaysToMe: h.relayState.CopyRelayIps(),
  211. CurrentRelaysThroughMe: h.relayState.CopyRelayForIps(),
  212. CurrentRemote: h.remote,
  213. }
  214. if h.ConnectionState != nil {
  215. chi.MessageCounter = h.ConnectionState.messageCounter.Load()
  216. }
  217. if c := h.GetCert(); c != nil {
  218. chi.Cert = c.Copy()
  219. }
  220. return chi
  221. }
  222. func listHostMapHosts(hl controlHostLister) []ControlHostInfo {
  223. hosts := make([]ControlHostInfo, 0)
  224. pr := hl.GetPreferredRanges()
  225. hl.ForEachVpnIp(func(hostinfo *HostInfo) {
  226. hosts = append(hosts, copyHostInfo(hostinfo, pr))
  227. })
  228. return hosts
  229. }
  230. func listHostMapIndexes(hl controlHostLister) []ControlHostInfo {
  231. hosts := make([]ControlHostInfo, 0)
  232. pr := hl.GetPreferredRanges()
  233. hl.ForEachIndex(func(hostinfo *HostInfo) {
  234. hosts = append(hosts, copyHostInfo(hostinfo, pr))
  235. })
  236. return hosts
  237. }