control.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // GetCertByVpnIp returns the authenticated certificate of the given vpn IP, or nil if not found
  112. func (c *Control) GetCertByVpnIp(vpnIp netip.Addr) *cert.NebulaCertificate {
  113. if c.f.myVpnNet.Addr() == vpnIp {
  114. return c.f.pki.GetCertState().Certificate
  115. }
  116. hi := c.f.hostMap.QueryVpnIp(vpnIp)
  117. if hi == nil {
  118. return nil
  119. }
  120. return hi.GetCert()
  121. }
  122. // CreateTunnel creates a new tunnel to the given vpn ip.
  123. func (c *Control) CreateTunnel(vpnIp netip.Addr) {
  124. c.f.handshakeManager.StartHandshake(vpnIp, nil)
  125. }
  126. // PrintTunnel creates a new tunnel to the given vpn ip.
  127. func (c *Control) PrintTunnel(vpnIp netip.Addr) *ControlHostInfo {
  128. hi := c.f.hostMap.QueryVpnIp(vpnIp)
  129. if hi == nil {
  130. return nil
  131. }
  132. chi := copyHostInfo(hi, c.f.hostMap.GetPreferredRanges())
  133. return &chi
  134. }
  135. // QueryLighthouse queries the lighthouse.
  136. func (c *Control) QueryLighthouse(vpnIp netip.Addr) *CacheMap {
  137. hi := c.f.lightHouse.Query(vpnIp)
  138. if hi == nil {
  139. return nil
  140. }
  141. return hi.CopyCache()
  142. }
  143. // GetHostInfoByVpnIp returns a single tunnels hostInfo, or nil if not found
  144. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  145. func (c *Control) GetHostInfoByVpnIp(vpnIp netip.Addr, pending bool) *ControlHostInfo {
  146. var hl controlHostLister
  147. if pending {
  148. hl = c.f.handshakeManager
  149. } else {
  150. hl = c.f.hostMap
  151. }
  152. h := hl.QueryVpnIp(vpnIp)
  153. if h == nil {
  154. return nil
  155. }
  156. ch := copyHostInfo(h, c.f.hostMap.GetPreferredRanges())
  157. return &ch
  158. }
  159. // SetRemoteForTunnel forces a tunnel to use a specific remote
  160. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  161. func (c *Control) SetRemoteForTunnel(vpnIp netip.Addr, addr netip.AddrPort) *ControlHostInfo {
  162. hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
  163. if hostInfo == nil {
  164. return nil
  165. }
  166. hostInfo.SetRemote(addr)
  167. ch := copyHostInfo(hostInfo, c.f.hostMap.GetPreferredRanges())
  168. return &ch
  169. }
  170. // CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
  171. // Caller should take care to Unmap() any 4in6 addresses prior to calling.
  172. func (c *Control) CloseTunnel(vpnIp netip.Addr, localOnly bool) bool {
  173. hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
  174. if hostInfo == nil {
  175. return false
  176. }
  177. if !localOnly {
  178. c.f.send(
  179. header.CloseTunnel,
  180. 0,
  181. hostInfo.ConnectionState,
  182. hostInfo,
  183. []byte{},
  184. make([]byte, 12, 12),
  185. make([]byte, mtu),
  186. )
  187. }
  188. c.f.closeTunnel(hostInfo)
  189. return true
  190. }
  191. // CloseAllTunnels is just like CloseTunnel except it goes through and shuts them all down, optionally you can avoid shutting down lighthouse tunnels
  192. // the int returned is a count of tunnels closed
  193. func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
  194. //TODO: this is probably better as a function in ConnectionManager or HostMap directly
  195. lighthouses := c.f.lightHouse.GetLighthouses()
  196. shutdown := func(h *HostInfo) {
  197. if excludeLighthouses {
  198. if _, ok := lighthouses[h.vpnIp]; ok {
  199. return
  200. }
  201. }
  202. c.f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  203. c.f.closeTunnel(h)
  204. c.l.WithField("vpnIp", h.vpnIp).WithField("udpAddr", h.remote).
  205. Debug("Sending close tunnel message")
  206. closed++
  207. }
  208. // Learn which hosts are being used as relays, so we can shut them down last.
  209. relayingHosts := map[netip.Addr]*HostInfo{}
  210. // Grab the hostMap lock to access the Relays map
  211. c.f.hostMap.Lock()
  212. for _, relayingHost := range c.f.hostMap.Relays {
  213. relayingHosts[relayingHost.vpnIp] = relayingHost
  214. }
  215. c.f.hostMap.Unlock()
  216. hostInfos := []*HostInfo{}
  217. // Grab the hostMap lock to access the Hosts map
  218. c.f.hostMap.Lock()
  219. for _, relayHost := range c.f.hostMap.Indexes {
  220. if _, ok := relayingHosts[relayHost.vpnIp]; !ok {
  221. hostInfos = append(hostInfos, relayHost)
  222. }
  223. }
  224. c.f.hostMap.Unlock()
  225. for _, h := range hostInfos {
  226. shutdown(h)
  227. }
  228. for _, h := range relayingHosts {
  229. shutdown(h)
  230. }
  231. return
  232. }
  233. func (c *Control) Device() overlay.Device {
  234. return c.f.inside
  235. }
  236. func copyHostInfo(h *HostInfo, preferredRanges []netip.Prefix) ControlHostInfo {
  237. chi := ControlHostInfo{
  238. VpnIp: h.vpnIp,
  239. LocalIndex: h.localIndexId,
  240. RemoteIndex: h.remoteIndexId,
  241. RemoteAddrs: h.remotes.CopyAddrs(preferredRanges),
  242. CurrentRelaysToMe: h.relayState.CopyRelayIps(),
  243. CurrentRelaysThroughMe: h.relayState.CopyRelayForIps(),
  244. CurrentRemote: h.remote,
  245. }
  246. if h.ConnectionState != nil {
  247. chi.MessageCounter = h.ConnectionState.messageCounter.Load()
  248. }
  249. if c := h.GetCert(); c != nil {
  250. chi.Cert = c.Copy()
  251. }
  252. return chi
  253. }
  254. func listHostMapHosts(hl controlHostLister) []ControlHostInfo {
  255. hosts := make([]ControlHostInfo, 0)
  256. pr := hl.GetPreferredRanges()
  257. hl.ForEachVpnIp(func(hostinfo *HostInfo) {
  258. hosts = append(hosts, copyHostInfo(hostinfo, pr))
  259. })
  260. return hosts
  261. }
  262. func listHostMapIndexes(hl controlHostLister) []ControlHostInfo {
  263. hosts := make([]ControlHostInfo, 0)
  264. pr := hl.GetPreferredRanges()
  265. hl.ForEachIndex(func(hostinfo *HostInfo) {
  266. hosts = append(hosts, copyHostInfo(hostinfo, pr))
  267. })
  268. return hosts
  269. }