control.go 9.0 KB

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