control.go 5.5 KB

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