control.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package nebula
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "os/signal"
  7. "sync/atomic"
  8. "syscall"
  9. "github.com/sirupsen/logrus"
  10. "github.com/slackhq/nebula/cert"
  11. )
  12. // Every interaction here needs to take extra care to copy memory and not return or use arguments "as is" when touching
  13. // core. This means copying IP objects, slices, de-referencing pointers and taking the actual value, etc
  14. type Control struct {
  15. f *Interface
  16. l *logrus.Logger
  17. cancel context.CancelFunc
  18. sshStart func()
  19. statsStart func()
  20. dnsStart func()
  21. }
  22. type ControlHostInfo struct {
  23. VpnIP net.IP `json:"vpnIp"`
  24. LocalIndex uint32 `json:"localIndex"`
  25. RemoteIndex uint32 `json:"remoteIndex"`
  26. RemoteAddrs []*udpAddr `json:"remoteAddrs"`
  27. CachedPackets int `json:"cachedPackets"`
  28. Cert *cert.NebulaCertificate `json:"cert"`
  29. MessageCounter uint64 `json:"messageCounter"`
  30. CurrentRemote *udpAddr `json:"currentRemote"`
  31. }
  32. // Start actually runs nebula, this is a nonblocking call. To block use Control.ShutdownBlock()
  33. func (c *Control) Start() {
  34. // Activate the interface
  35. c.f.activate()
  36. // Call all the delayed funcs that waited patiently for the interface to be created.
  37. if c.sshStart != nil {
  38. go c.sshStart()
  39. }
  40. if c.statsStart != nil {
  41. go c.statsStart()
  42. }
  43. if c.dnsStart != nil {
  44. go c.dnsStart()
  45. }
  46. // Start reading packets.
  47. c.f.run()
  48. }
  49. // Stop signals nebula to shutdown, returns after the shutdown is complete
  50. func (c *Control) Stop() {
  51. //TODO: stop tun and udp routines, the lock on hostMap effectively does that though
  52. c.CloseAllTunnels(false)
  53. c.cancel()
  54. c.l.Info("Goodbye")
  55. }
  56. // ShutdownBlock will listen for and block on term and interrupt signals, calling Control.Stop() once signalled
  57. func (c *Control) ShutdownBlock() {
  58. sigChan := make(chan os.Signal)
  59. signal.Notify(sigChan, syscall.SIGTERM)
  60. signal.Notify(sigChan, syscall.SIGINT)
  61. rawSig := <-sigChan
  62. sig := rawSig.String()
  63. c.l.WithField("signal", sig).Info("Caught signal, shutting down")
  64. c.Stop()
  65. }
  66. // RebindUDPServer asks the UDP listener to rebind it's listener. Mainly used on mobile clients when interfaces change
  67. func (c *Control) RebindUDPServer() {
  68. _ = c.f.outside.Rebind()
  69. // Trigger a lighthouse update, useful for mobile clients that should have an update interval of 0
  70. c.f.lightHouse.SendUpdate(c.f)
  71. // Let the main interface know that we rebound so that underlying tunnels know to trigger punches from their remotes
  72. c.f.rebindCount++
  73. }
  74. // ListHostmap returns details about the actual or pending (handshaking) hostmap
  75. func (c *Control) ListHostmap(pendingMap bool) []ControlHostInfo {
  76. if pendingMap {
  77. return listHostMap(c.f.handshakeManager.pendingHostMap)
  78. } else {
  79. return listHostMap(c.f.hostMap)
  80. }
  81. }
  82. // GetHostInfoByVpnIP returns a single tunnels hostInfo, or nil if not found
  83. func (c *Control) GetHostInfoByVpnIP(vpnIP uint32, pending bool) *ControlHostInfo {
  84. var hm *HostMap
  85. if pending {
  86. hm = c.f.handshakeManager.pendingHostMap
  87. } else {
  88. hm = c.f.hostMap
  89. }
  90. h, err := hm.QueryVpnIP(vpnIP)
  91. if err != nil {
  92. return nil
  93. }
  94. ch := copyHostInfo(h, c.f.hostMap.preferredRanges)
  95. return &ch
  96. }
  97. // SetRemoteForTunnel forces a tunnel to use a specific remote
  98. func (c *Control) SetRemoteForTunnel(vpnIP uint32, addr udpAddr) *ControlHostInfo {
  99. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  100. if err != nil {
  101. return nil
  102. }
  103. hostInfo.SetRemote(addr.Copy())
  104. ch := copyHostInfo(hostInfo, c.f.hostMap.preferredRanges)
  105. return &ch
  106. }
  107. // CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
  108. func (c *Control) CloseTunnel(vpnIP uint32, localOnly bool) bool {
  109. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  110. if err != nil {
  111. return false
  112. }
  113. if !localOnly {
  114. c.f.send(
  115. closeTunnel,
  116. 0,
  117. hostInfo.ConnectionState,
  118. hostInfo,
  119. hostInfo.remote,
  120. []byte{},
  121. make([]byte, 12, 12),
  122. make([]byte, mtu),
  123. )
  124. }
  125. c.f.closeTunnel(hostInfo, false)
  126. return true
  127. }
  128. // CloseAllTunnels is just like CloseTunnel except it goes through and shuts them all down, optionally you can avoid shutting down lighthouse tunnels
  129. // the int returned is a count of tunnels closed
  130. func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
  131. //TODO: this is probably better as a function in ConnectionManager or HostMap directly
  132. c.f.hostMap.Lock()
  133. for _, h := range c.f.hostMap.Hosts {
  134. if excludeLighthouses {
  135. if _, ok := c.f.lightHouse.lighthouses[h.hostId]; ok {
  136. continue
  137. }
  138. }
  139. if h.ConnectionState.ready {
  140. c.f.send(closeTunnel, 0, h.ConnectionState, h, h.remote, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  141. c.f.closeTunnel(h, true)
  142. c.l.WithField("vpnIp", IntIp(h.hostId)).WithField("udpAddr", h.remote).
  143. Debug("Sending close tunnel message")
  144. closed++
  145. }
  146. }
  147. c.f.hostMap.Unlock()
  148. return
  149. }
  150. func copyHostInfo(h *HostInfo, preferredRanges []*net.IPNet) ControlHostInfo {
  151. chi := ControlHostInfo{
  152. VpnIP: int2ip(h.hostId),
  153. LocalIndex: h.localIndexId,
  154. RemoteIndex: h.remoteIndexId,
  155. RemoteAddrs: h.remotes.CopyAddrs(preferredRanges),
  156. CachedPackets: len(h.packetStore),
  157. }
  158. if h.ConnectionState != nil {
  159. chi.MessageCounter = atomic.LoadUint64(&h.ConnectionState.atomicMessageCounter)
  160. }
  161. if c := h.GetCert(); c != nil {
  162. chi.Cert = c.Copy()
  163. }
  164. if h.remote != nil {
  165. chi.CurrentRemote = h.remote.Copy()
  166. }
  167. return chi
  168. }
  169. func listHostMap(hm *HostMap) []ControlHostInfo {
  170. hm.RLock()
  171. hosts := make([]ControlHostInfo, len(hm.Hosts))
  172. i := 0
  173. for _, v := range hm.Hosts {
  174. hosts[i] = copyHostInfo(v, hm.preferredRanges)
  175. i++
  176. }
  177. hm.RUnlock()
  178. return hosts
  179. }