control.go 5.2 KB

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