control.go 7.7 KB

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