control.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. }
  59. // ListHostmap returns details about the actual or pending (handshaking) hostmap
  60. func (c *Control) ListHostmap(pendingMap bool) []ControlHostInfo {
  61. var hm *HostMap
  62. if pendingMap {
  63. hm = c.f.handshakeManager.pendingHostMap
  64. } else {
  65. hm = c.f.hostMap
  66. }
  67. hm.RLock()
  68. hosts := make([]ControlHostInfo, len(hm.Hosts))
  69. i := 0
  70. for _, v := range hm.Hosts {
  71. hosts[i] = copyHostInfo(v)
  72. i++
  73. }
  74. hm.RUnlock()
  75. return hosts
  76. }
  77. // GetHostInfoByVpnIP returns a single tunnels hostInfo, or nil if not found
  78. func (c *Control) GetHostInfoByVpnIP(vpnIP uint32, pending bool) *ControlHostInfo {
  79. var hm *HostMap
  80. if pending {
  81. hm = c.f.handshakeManager.pendingHostMap
  82. } else {
  83. hm = c.f.hostMap
  84. }
  85. h, err := hm.QueryVpnIP(vpnIP)
  86. if err != nil {
  87. return nil
  88. }
  89. ch := copyHostInfo(h)
  90. return &ch
  91. }
  92. // SetRemoteForTunnel forces a tunnel to use a specific remote
  93. func (c *Control) SetRemoteForTunnel(vpnIP uint32, addr udpAddr) *ControlHostInfo {
  94. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  95. if err != nil {
  96. return nil
  97. }
  98. hostInfo.SetRemote(addr.Copy())
  99. ch := copyHostInfo(hostInfo)
  100. return &ch
  101. }
  102. // CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
  103. func (c *Control) CloseTunnel(vpnIP uint32, localOnly bool) bool {
  104. hostInfo, err := c.f.hostMap.QueryVpnIP(vpnIP)
  105. if err != nil {
  106. return false
  107. }
  108. if !localOnly {
  109. c.f.send(
  110. closeTunnel,
  111. 0,
  112. hostInfo.ConnectionState,
  113. hostInfo,
  114. hostInfo.remote,
  115. []byte{},
  116. make([]byte, 12, 12),
  117. make([]byte, mtu),
  118. )
  119. }
  120. c.f.closeTunnel(hostInfo)
  121. return true
  122. }
  123. func copyHostInfo(h *HostInfo) ControlHostInfo {
  124. addrs := h.RemoteUDPAddrs()
  125. chi := ControlHostInfo{
  126. VpnIP: int2ip(h.hostId),
  127. LocalIndex: h.localIndexId,
  128. RemoteIndex: h.remoteIndexId,
  129. RemoteAddrs: make([]udpAddr, len(addrs), len(addrs)),
  130. CachedPackets: len(h.packetStore),
  131. MessageCounter: *h.ConnectionState.messageCounter,
  132. }
  133. if c := h.GetCert(); c != nil {
  134. chi.Cert = c.Copy()
  135. }
  136. if h.remote != nil {
  137. chi.CurrentRemote = *h.remote
  138. }
  139. for i, addr := range addrs {
  140. chi.RemoteAddrs[i] = addr.Copy()
  141. }
  142. return chi
  143. }