control.go 7.6 KB

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