3
0

connection_manager.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package nebula
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/sirupsen/logrus"
  6. )
  7. // TODO: incount and outcount are intended as a shortcut to locking the mutexes for every single packet
  8. // and something like every 10 packets we could lock, send 10, then unlock for a moment
  9. type connectionManager struct {
  10. hostMap *HostMap
  11. in map[uint32]struct{}
  12. inLock *sync.RWMutex
  13. inCount int
  14. out map[uint32]struct{}
  15. outLock *sync.RWMutex
  16. outCount int
  17. TrafficTimer *SystemTimerWheel
  18. intf *Interface
  19. pendingDeletion map[uint32]int
  20. pendingDeletionLock *sync.RWMutex
  21. pendingDeletionTimer *SystemTimerWheel
  22. checkInterval int
  23. pendingDeletionInterval int
  24. l *logrus.Logger
  25. // I wanted to call one matLock
  26. }
  27. func newConnectionManager(l *logrus.Logger, intf *Interface, checkInterval, pendingDeletionInterval int) *connectionManager {
  28. nc := &connectionManager{
  29. hostMap: intf.hostMap,
  30. in: make(map[uint32]struct{}),
  31. inLock: &sync.RWMutex{},
  32. inCount: 0,
  33. out: make(map[uint32]struct{}),
  34. outLock: &sync.RWMutex{},
  35. outCount: 0,
  36. TrafficTimer: NewSystemTimerWheel(time.Millisecond*500, time.Second*60),
  37. intf: intf,
  38. pendingDeletion: make(map[uint32]int),
  39. pendingDeletionLock: &sync.RWMutex{},
  40. pendingDeletionTimer: NewSystemTimerWheel(time.Millisecond*500, time.Second*60),
  41. checkInterval: checkInterval,
  42. pendingDeletionInterval: pendingDeletionInterval,
  43. l: l,
  44. }
  45. nc.Start()
  46. return nc
  47. }
  48. func (n *connectionManager) In(ip uint32) {
  49. n.inLock.RLock()
  50. // If this already exists, return
  51. if _, ok := n.in[ip]; ok {
  52. n.inLock.RUnlock()
  53. return
  54. }
  55. n.inLock.RUnlock()
  56. n.inLock.Lock()
  57. n.in[ip] = struct{}{}
  58. n.inLock.Unlock()
  59. }
  60. func (n *connectionManager) Out(ip uint32) {
  61. n.outLock.RLock()
  62. // If this already exists, return
  63. if _, ok := n.out[ip]; ok {
  64. n.outLock.RUnlock()
  65. return
  66. }
  67. n.outLock.RUnlock()
  68. n.outLock.Lock()
  69. // double check since we dropped the lock temporarily
  70. if _, ok := n.out[ip]; ok {
  71. n.outLock.Unlock()
  72. return
  73. }
  74. n.out[ip] = struct{}{}
  75. n.AddTrafficWatch(ip, n.checkInterval)
  76. n.outLock.Unlock()
  77. }
  78. func (n *connectionManager) CheckIn(vpnIP uint32) bool {
  79. n.inLock.RLock()
  80. if _, ok := n.in[vpnIP]; ok {
  81. n.inLock.RUnlock()
  82. return true
  83. }
  84. n.inLock.RUnlock()
  85. return false
  86. }
  87. func (n *connectionManager) ClearIP(ip uint32) {
  88. n.inLock.Lock()
  89. n.outLock.Lock()
  90. delete(n.in, ip)
  91. delete(n.out, ip)
  92. n.inLock.Unlock()
  93. n.outLock.Unlock()
  94. }
  95. func (n *connectionManager) ClearPendingDeletion(ip uint32) {
  96. n.pendingDeletionLock.Lock()
  97. delete(n.pendingDeletion, ip)
  98. n.pendingDeletionLock.Unlock()
  99. }
  100. func (n *connectionManager) AddPendingDeletion(ip uint32) {
  101. n.pendingDeletionLock.Lock()
  102. if _, ok := n.pendingDeletion[ip]; ok {
  103. n.pendingDeletion[ip] += 1
  104. } else {
  105. n.pendingDeletion[ip] = 0
  106. }
  107. n.pendingDeletionTimer.Add(ip, time.Second*time.Duration(n.pendingDeletionInterval))
  108. n.pendingDeletionLock.Unlock()
  109. }
  110. func (n *connectionManager) checkPendingDeletion(ip uint32) bool {
  111. n.pendingDeletionLock.RLock()
  112. if _, ok := n.pendingDeletion[ip]; ok {
  113. n.pendingDeletionLock.RUnlock()
  114. return true
  115. }
  116. n.pendingDeletionLock.RUnlock()
  117. return false
  118. }
  119. func (n *connectionManager) AddTrafficWatch(vpnIP uint32, seconds int) {
  120. n.TrafficTimer.Add(vpnIP, time.Second*time.Duration(seconds))
  121. }
  122. func (n *connectionManager) Start() {
  123. go n.Run()
  124. }
  125. func (n *connectionManager) Run() {
  126. clockSource := time.Tick(500 * time.Millisecond)
  127. p := []byte("")
  128. nb := make([]byte, 12, 12)
  129. out := make([]byte, mtu)
  130. for now := range clockSource {
  131. n.HandleMonitorTick(now, p, nb, out)
  132. n.HandleDeletionTick(now)
  133. }
  134. }
  135. func (n *connectionManager) HandleMonitorTick(now time.Time, p, nb, out []byte) {
  136. n.TrafficTimer.advance(now)
  137. for {
  138. ep := n.TrafficTimer.Purge()
  139. if ep == nil {
  140. break
  141. }
  142. vpnIP := ep.(uint32)
  143. // Check for traffic coming back in from this host.
  144. traf := n.CheckIn(vpnIP)
  145. // If we saw incoming packets from this ip, just return
  146. if traf {
  147. if n.l.Level >= logrus.DebugLevel {
  148. n.l.WithField("vpnIp", IntIp(vpnIP)).
  149. WithField("tunnelCheck", m{"state": "alive", "method": "passive"}).
  150. Debug("Tunnel status")
  151. }
  152. n.ClearIP(vpnIP)
  153. n.ClearPendingDeletion(vpnIP)
  154. continue
  155. }
  156. // If we didn't we may need to probe or destroy the conn
  157. hostinfo, err := n.hostMap.QueryVpnIP(vpnIP)
  158. if err != nil {
  159. n.l.Debugf("Not found in hostmap: %s", IntIp(vpnIP))
  160. n.ClearIP(vpnIP)
  161. n.ClearPendingDeletion(vpnIP)
  162. continue
  163. }
  164. hostinfo.logger(n.l).
  165. WithField("tunnelCheck", m{"state": "testing", "method": "active"}).
  166. Debug("Tunnel status")
  167. if hostinfo != nil && hostinfo.ConnectionState != nil {
  168. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  169. n.intf.SendMessageToVpnIp(test, testRequest, vpnIP, p, nb, out)
  170. } else {
  171. hostinfo.logger(n.l).Debugf("Hostinfo sadness: %s", IntIp(vpnIP))
  172. }
  173. n.AddPendingDeletion(vpnIP)
  174. }
  175. }
  176. func (n *connectionManager) HandleDeletionTick(now time.Time) {
  177. n.pendingDeletionTimer.advance(now)
  178. for {
  179. ep := n.pendingDeletionTimer.Purge()
  180. if ep == nil {
  181. break
  182. }
  183. vpnIP := ep.(uint32)
  184. // If we saw incoming packets from this ip, just return
  185. traf := n.CheckIn(vpnIP)
  186. if traf {
  187. n.l.WithField("vpnIp", IntIp(vpnIP)).
  188. WithField("tunnelCheck", m{"state": "alive", "method": "active"}).
  189. Debug("Tunnel status")
  190. n.ClearIP(vpnIP)
  191. n.ClearPendingDeletion(vpnIP)
  192. continue
  193. }
  194. hostinfo, err := n.hostMap.QueryVpnIP(vpnIP)
  195. if err != nil {
  196. n.ClearIP(vpnIP)
  197. n.ClearPendingDeletion(vpnIP)
  198. n.l.Debugf("Not found in hostmap: %s", IntIp(vpnIP))
  199. continue
  200. }
  201. // If it comes around on deletion wheel and hasn't resolved itself, delete
  202. if n.checkPendingDeletion(vpnIP) {
  203. cn := ""
  204. if hostinfo.ConnectionState != nil && hostinfo.ConnectionState.peerCert != nil {
  205. cn = hostinfo.ConnectionState.peerCert.Details.Name
  206. }
  207. hostinfo.logger(n.l).
  208. WithField("tunnelCheck", m{"state": "dead", "method": "active"}).
  209. WithField("certName", cn).
  210. Info("Tunnel status")
  211. n.ClearIP(vpnIP)
  212. n.ClearPendingDeletion(vpnIP)
  213. // TODO: This is only here to let tests work. Should do proper mocking
  214. if n.intf.lightHouse != nil {
  215. n.intf.lightHouse.DeleteVpnIP(vpnIP)
  216. }
  217. n.hostMap.DeleteHostInfo(hostinfo)
  218. } else {
  219. n.ClearIP(vpnIP)
  220. n.ClearPendingDeletion(vpnIP)
  221. }
  222. }
  223. }