control.go 7.6 KB

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