control.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. var hm *HostMap
  58. if pendingMap {
  59. hm = c.f.handshakeManager.pendingHostMap
  60. } else {
  61. hm = c.f.hostMap
  62. }
  63. hm.RLock()
  64. hosts := make([]ControlHostInfo, len(hm.Hosts))
  65. i := 0
  66. for _, v := range hm.Hosts {
  67. hosts[i] = copyHostInfo(v)
  68. i++
  69. }
  70. hm.RUnlock()
  71. return hosts
  72. }
  73. // GetHostInfoByVpnIP returns a single tunnels hostInfo, or nil if not found
  74. func (c *Control) GetHostInfoByVpnIP(vpnIP uint32, pending bool) *ControlHostInfo {
  75. var hm *HostMap
  76. if pending {
  77. hm = c.f.handshakeManager.pendingHostMap
  78. } else {
  79. hm = c.f.hostMap
  80. }
  81. h, err := hm.QueryVpnIP(vpnIP)
  82. if err != nil {
  83. return nil
  84. }
  85. ch := copyHostInfo(h)
  86. return &ch
  87. }
  88. // SetRemoteForTunnel forces a tunnel to use a specific remote
  89. func (c *Control) SetRemoteForTunnel(vpnIP uint32, addr udpAddr) *ControlHostInfo {
  90. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  91. if err != nil {
  92. return nil
  93. }
  94. hostInfo.SetRemote(addr.Copy())
  95. ch := copyHostInfo(hostInfo)
  96. return &ch
  97. }
  98. // CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
  99. func (c *Control) CloseTunnel(vpnIP uint32, localOnly bool) bool {
  100. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  101. if err != nil {
  102. return false
  103. }
  104. if !localOnly {
  105. c.f.send(
  106. closeTunnel,
  107. 0,
  108. hostInfo.ConnectionState,
  109. hostInfo,
  110. hostInfo.remote,
  111. []byte{},
  112. make([]byte, 12, 12),
  113. make([]byte, mtu),
  114. )
  115. }
  116. c.f.closeTunnel(hostInfo)
  117. return true
  118. }
  119. // CloseAllTunnels is just like CloseTunnel except it goes through and shuts them all down, optionally you can avoid shutting down lighthouse tunnels
  120. // the int returned is a count of tunnels closed
  121. func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
  122. //TODO: this is probably better as a function in ConnectionManager or HostMap directly
  123. c.f.hostMap.Lock()
  124. for _, h := range c.f.hostMap.Hosts {
  125. if excludeLighthouses {
  126. if _, ok := c.f.lightHouse.lighthouses[h.hostId]; ok {
  127. continue
  128. }
  129. }
  130. if h.ConnectionState.ready {
  131. c.f.send(closeTunnel, 0, h.ConnectionState, h, h.remote, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  132. c.l.WithField("vpnIp", IntIp(h.hostId)).WithField("udpAddr", h.remote).
  133. Debug("Sending close tunnel message")
  134. closed++
  135. }
  136. }
  137. c.f.hostMap.Unlock()
  138. return
  139. }
  140. func copyHostInfo(h *HostInfo) ControlHostInfo {
  141. addrs := h.RemoteUDPAddrs()
  142. chi := ControlHostInfo{
  143. VpnIP: int2ip(h.hostId),
  144. LocalIndex: h.localIndexId,
  145. RemoteIndex: h.remoteIndexId,
  146. RemoteAddrs: make([]*udpAddr, len(addrs), len(addrs)),
  147. CachedPackets: len(h.packetStore),
  148. MessageCounter: atomic.LoadUint64(&h.ConnectionState.atomicMessageCounter),
  149. }
  150. if c := h.GetCert(); c != nil {
  151. chi.Cert = c.Copy()
  152. }
  153. if h.remote != nil {
  154. chi.CurrentRemote = h.remote.Copy()
  155. }
  156. for i, addr := range addrs {
  157. chi.RemoteAddrs[i] = addr.Copy()
  158. }
  159. return chi
  160. }