control.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //TODO: this is probably better as a function in ConnectionManager or HostMap directly
  35. c.f.hostMap.Lock()
  36. for _, h := range c.f.hostMap.Hosts {
  37. if h.ConnectionState.ready {
  38. c.f.send(closeTunnel, 0, h.ConnectionState, h, h.remote, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  39. c.l.WithField("vpnIp", IntIp(h.hostId)).WithField("udpAddr", h.remote).
  40. Debug("Sending close tunnel message")
  41. }
  42. }
  43. c.f.hostMap.Unlock()
  44. c.l.Info("Goodbye")
  45. }
  46. // ShutdownBlock will listen for and block on term and interrupt signals, calling Control.Stop() once signalled
  47. func (c *Control) ShutdownBlock() {
  48. sigChan := make(chan os.Signal)
  49. signal.Notify(sigChan, syscall.SIGTERM)
  50. signal.Notify(sigChan, syscall.SIGINT)
  51. rawSig := <-sigChan
  52. sig := rawSig.String()
  53. c.l.WithField("signal", sig).Info("Caught signal, shutting down")
  54. c.Stop()
  55. }
  56. // RebindUDPServer asks the UDP listener to rebind it's listener. Mainly used on mobile clients when interfaces change
  57. func (c *Control) RebindUDPServer() {
  58. _ = c.f.outside.Rebind()
  59. // Trigger a lighthouse update, useful for mobile clients that should have an update interval of 0
  60. c.f.lightHouse.SendUpdate(c.f)
  61. // Let the main interface know that we rebound so that underlying tunnels know to trigger punches from their remotes
  62. c.f.rebindCount++
  63. }
  64. // ListHostmap returns details about the actual or pending (handshaking) hostmap
  65. func (c *Control) ListHostmap(pendingMap bool) []ControlHostInfo {
  66. var hm *HostMap
  67. if pendingMap {
  68. hm = c.f.handshakeManager.pendingHostMap
  69. } else {
  70. hm = c.f.hostMap
  71. }
  72. hm.RLock()
  73. hosts := make([]ControlHostInfo, len(hm.Hosts))
  74. i := 0
  75. for _, v := range hm.Hosts {
  76. hosts[i] = copyHostInfo(v)
  77. i++
  78. }
  79. hm.RUnlock()
  80. return hosts
  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)
  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)
  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)
  126. return true
  127. }
  128. func copyHostInfo(h *HostInfo) ControlHostInfo {
  129. addrs := h.RemoteUDPAddrs()
  130. chi := ControlHostInfo{
  131. VpnIP: int2ip(h.hostId),
  132. LocalIndex: h.localIndexId,
  133. RemoteIndex: h.remoteIndexId,
  134. RemoteAddrs: make([]udpAddr, len(addrs), len(addrs)),
  135. CachedPackets: len(h.packetStore),
  136. MessageCounter: atomic.LoadUint64(&h.ConnectionState.atomicMessageCounter),
  137. }
  138. if c := h.GetCert(); c != nil {
  139. chi.Cert = c.Copy()
  140. }
  141. if h.remote != nil {
  142. chi.CurrentRemote = *h.remote
  143. }
  144. for i, addr := range addrs {
  145. chi.RemoteAddrs[i] = addr.Copy()
  146. }
  147. return chi
  148. }