control.go 4.6 KB

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