handshake_manager.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. package nebula
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/rand"
  6. "encoding/binary"
  7. "errors"
  8. "net/netip"
  9. "sync"
  10. "time"
  11. "github.com/rcrowley/go-metrics"
  12. "github.com/sirupsen/logrus"
  13. "github.com/slackhq/nebula/header"
  14. "github.com/slackhq/nebula/udp"
  15. "golang.org/x/exp/slices"
  16. )
  17. const (
  18. DefaultHandshakeTryInterval = time.Millisecond * 100
  19. DefaultHandshakeRetries = 10
  20. DefaultHandshakeTriggerBuffer = 64
  21. DefaultUseRelays = true
  22. )
  23. var (
  24. defaultHandshakeConfig = HandshakeConfig{
  25. tryInterval: DefaultHandshakeTryInterval,
  26. retries: DefaultHandshakeRetries,
  27. triggerBuffer: DefaultHandshakeTriggerBuffer,
  28. useRelays: DefaultUseRelays,
  29. }
  30. )
  31. type HandshakeConfig struct {
  32. tryInterval time.Duration
  33. retries int64
  34. triggerBuffer int
  35. useRelays bool
  36. messageMetrics *MessageMetrics
  37. }
  38. type HandshakeManager struct {
  39. // Mutex for interacting with the vpnIps and indexes maps
  40. sync.RWMutex
  41. vpnIps map[netip.Addr]*HandshakeHostInfo
  42. indexes map[uint32]*HandshakeHostInfo
  43. mainHostMap *HostMap
  44. lightHouse *LightHouse
  45. outside udp.Conn
  46. config HandshakeConfig
  47. OutboundHandshakeTimer *LockingTimerWheel[netip.Addr]
  48. messageMetrics *MessageMetrics
  49. metricInitiated metrics.Counter
  50. metricTimedOut metrics.Counter
  51. f *Interface
  52. l *logrus.Logger
  53. // can be used to trigger outbound handshake for the given vpnIp
  54. trigger chan netip.Addr
  55. }
  56. type HandshakeHostInfo struct {
  57. sync.Mutex
  58. startTime time.Time // Time that we first started trying with this handshake
  59. ready bool // Is the handshake ready
  60. counter int64 // How many attempts have we made so far
  61. lastRemotes []netip.AddrPort // Remotes that we sent to during the previous attempt
  62. packetStore []*cachedPacket // A set of packets to be transmitted once the handshake completes
  63. hostinfo *HostInfo
  64. }
  65. func (hh *HandshakeHostInfo) cachePacket(l *logrus.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
  66. if len(hh.packetStore) < 100 {
  67. tempPacket := make([]byte, len(packet))
  68. copy(tempPacket, packet)
  69. hh.packetStore = append(hh.packetStore, &cachedPacket{t, st, f, tempPacket})
  70. if l.Level >= logrus.DebugLevel {
  71. hh.hostinfo.logger(l).
  72. WithField("length", len(hh.packetStore)).
  73. WithField("stored", true).
  74. Debugf("Packet store")
  75. }
  76. } else {
  77. m.dropped.Inc(1)
  78. if l.Level >= logrus.DebugLevel {
  79. hh.hostinfo.logger(l).
  80. WithField("length", len(hh.packetStore)).
  81. WithField("stored", false).
  82. Debugf("Packet store")
  83. }
  84. }
  85. }
  86. func NewHandshakeManager(l *logrus.Logger, mainHostMap *HostMap, lightHouse *LightHouse, outside udp.Conn, config HandshakeConfig) *HandshakeManager {
  87. return &HandshakeManager{
  88. vpnIps: map[netip.Addr]*HandshakeHostInfo{},
  89. indexes: map[uint32]*HandshakeHostInfo{},
  90. mainHostMap: mainHostMap,
  91. lightHouse: lightHouse,
  92. outside: outside,
  93. config: config,
  94. trigger: make(chan netip.Addr, config.triggerBuffer),
  95. OutboundHandshakeTimer: NewLockingTimerWheel[netip.Addr](config.tryInterval, hsTimeout(config.retries, config.tryInterval)),
  96. messageMetrics: config.messageMetrics,
  97. metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
  98. metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
  99. l: l,
  100. }
  101. }
  102. func (c *HandshakeManager) Run(ctx context.Context) {
  103. clockSource := time.NewTicker(c.config.tryInterval)
  104. defer clockSource.Stop()
  105. for {
  106. select {
  107. case <-ctx.Done():
  108. return
  109. case vpnIP := <-c.trigger:
  110. c.handleOutbound(vpnIP, true)
  111. case now := <-clockSource.C:
  112. c.NextOutboundHandshakeTimerTick(now)
  113. }
  114. }
  115. }
  116. func (hm *HandshakeManager) HandleIncoming(addr netip.AddrPort, via *ViaSender, packet []byte, h *header.H) {
  117. // First remote allow list check before we know the vpnIp
  118. if addr.IsValid() {
  119. if !hm.lightHouse.GetRemoteAllowList().AllowUnknownVpnIp(addr.Addr()) {
  120. hm.l.WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  121. return
  122. }
  123. }
  124. switch h.Subtype {
  125. case header.HandshakeIXPSK0:
  126. switch h.MessageCounter {
  127. case 1:
  128. ixHandshakeStage1(hm.f, addr, via, packet, h)
  129. case 2:
  130. newHostinfo := hm.queryIndex(h.RemoteIndex)
  131. tearDown := ixHandshakeStage2(hm.f, addr, via, newHostinfo, packet, h)
  132. if tearDown && newHostinfo != nil {
  133. hm.DeleteHostInfo(newHostinfo.hostinfo)
  134. }
  135. }
  136. }
  137. }
  138. func (c *HandshakeManager) NextOutboundHandshakeTimerTick(now time.Time) {
  139. c.OutboundHandshakeTimer.Advance(now)
  140. for {
  141. vpnIp, has := c.OutboundHandshakeTimer.Purge()
  142. if !has {
  143. break
  144. }
  145. c.handleOutbound(vpnIp, false)
  146. }
  147. }
  148. func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered bool) {
  149. hh := hm.queryVpnIp(vpnIp)
  150. if hh == nil {
  151. return
  152. }
  153. hh.Lock()
  154. defer hh.Unlock()
  155. hostinfo := hh.hostinfo
  156. // If we are out of time, clean up
  157. if hh.counter >= hm.config.retries {
  158. hh.hostinfo.logger(hm.l).WithField("udpAddrs", hh.hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())).
  159. WithField("initiatorIndex", hh.hostinfo.localIndexId).
  160. WithField("remoteIndex", hh.hostinfo.remoteIndexId).
  161. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  162. WithField("durationNs", time.Since(hh.startTime).Nanoseconds()).
  163. Info("Handshake timed out")
  164. hm.metricTimedOut.Inc(1)
  165. hm.DeleteHostInfo(hostinfo)
  166. return
  167. }
  168. // Increment the counter to increase our delay, linear backoff
  169. hh.counter++
  170. // Check if we have a handshake packet to transmit yet
  171. if !hh.ready {
  172. if !ixHandshakeStage0(hm.f, hh) {
  173. hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
  174. return
  175. }
  176. }
  177. // Get a remotes object if we don't already have one.
  178. // This is mainly to protect us as this should never be the case
  179. // NB ^ This comment doesn't jive. It's how the thing gets initialized.
  180. // It's the common path. Should it update every time, in case a future LH query/queries give us more info?
  181. if hostinfo.remotes == nil {
  182. hostinfo.remotes = hm.lightHouse.QueryCache(vpnIp)
  183. }
  184. remotes := hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())
  185. remotesHaveChanged := !slices.Equal(remotes, hh.lastRemotes)
  186. // We only care about a lighthouse trigger if we have new remotes to send to.
  187. // This is a very specific optimization for a fast lighthouse reply.
  188. if lighthouseTriggered && !remotesHaveChanged {
  189. // If we didn't return here a lighthouse could cause us to aggressively send handshakes
  190. return
  191. }
  192. hh.lastRemotes = remotes
  193. // TODO: this will generate a load of queries for hosts with only 1 ip
  194. // (such as ones registered to the lighthouse with only a private IP)
  195. // So we only do it one time after attempting 5 handshakes already.
  196. if len(remotes) <= 1 && hh.counter == 5 {
  197. // If we only have 1 remote it is highly likely our query raced with the other host registered within the lighthouse
  198. // Our vpnIp here has a tunnel with a lighthouse but has yet to send a host update packet there so we only know about
  199. // the learned public ip for them. Query again to short circuit the promotion counter
  200. hm.lightHouse.QueryServer(vpnIp)
  201. }
  202. // Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
  203. var sentTo []netip.AddrPort
  204. hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr netip.AddrPort, _ bool) {
  205. hm.messageMetrics.Tx(header.Handshake, header.MessageSubType(hostinfo.HandshakePacket[0][1]), 1)
  206. err := hm.outside.WriteTo(hostinfo.HandshakePacket[0], addr)
  207. if err != nil {
  208. hostinfo.logger(hm.l).WithField("udpAddr", addr).
  209. WithField("initiatorIndex", hostinfo.localIndexId).
  210. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  211. WithError(err).Error("Failed to send handshake message")
  212. } else {
  213. sentTo = append(sentTo, addr)
  214. }
  215. })
  216. // Don't be too noisy or confusing if we fail to send a handshake - if we don't get through we'll eventually log a timeout,
  217. // so only log when the list of remotes has changed
  218. if remotesHaveChanged {
  219. hostinfo.logger(hm.l).WithField("udpAddrs", sentTo).
  220. WithField("initiatorIndex", hostinfo.localIndexId).
  221. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  222. Info("Handshake message sent")
  223. } else if hm.l.IsLevelEnabled(logrus.DebugLevel) {
  224. hostinfo.logger(hm.l).WithField("udpAddrs", sentTo).
  225. WithField("initiatorIndex", hostinfo.localIndexId).
  226. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  227. Debug("Handshake message sent")
  228. }
  229. if hm.config.useRelays && len(hostinfo.remotes.relays) > 0 {
  230. hostinfo.logger(hm.l).WithField("relays", hostinfo.remotes.relays).Info("Attempt to relay through hosts")
  231. // Send a RelayRequest to all known Relay IP's
  232. for _, relay := range hostinfo.remotes.relays {
  233. // Don't relay to myself, and don't relay through the host I'm trying to connect to
  234. if relay == vpnIp || relay == hm.lightHouse.myVpnNet.Addr() {
  235. continue
  236. }
  237. relayHostInfo := hm.mainHostMap.QueryVpnIp(relay)
  238. if relayHostInfo == nil || !relayHostInfo.remote.IsValid() {
  239. hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Establish tunnel to relay target")
  240. hm.f.Handshake(relay)
  241. continue
  242. }
  243. // Check the relay HostInfo to see if we already established a relay through it
  244. existingRelay, ok := relayHostInfo.relayState.QueryRelayForByIp(vpnIp)
  245. if !ok {
  246. // No relays exist or requested yet.
  247. if relayHostInfo.remote.IsValid() {
  248. idx, err := AddRelay(hm.l, relayHostInfo, hm.mainHostMap, vpnIp, nil, TerminalType, Requested)
  249. if err != nil {
  250. hostinfo.logger(hm.l).WithField("relay", relay.String()).WithError(err).Info("Failed to add relay to hostmap")
  251. }
  252. //TODO: IPV6-WORK
  253. myVpnIpB := hm.f.myVpnNet.Addr().As4()
  254. theirVpnIpB := vpnIp.As4()
  255. m := NebulaControl{
  256. Type: NebulaControl_CreateRelayRequest,
  257. InitiatorRelayIndex: idx,
  258. RelayFromIp: binary.BigEndian.Uint32(myVpnIpB[:]),
  259. RelayToIp: binary.BigEndian.Uint32(theirVpnIpB[:]),
  260. }
  261. msg, err := m.Marshal()
  262. if err != nil {
  263. hostinfo.logger(hm.l).
  264. WithError(err).
  265. Error("Failed to marshal Control message to create relay")
  266. } else {
  267. hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
  268. hm.l.WithFields(logrus.Fields{
  269. "relayFrom": hm.f.myVpnNet.Addr(),
  270. "relayTo": vpnIp,
  271. "initiatorRelayIndex": idx,
  272. "relay": relay}).
  273. Info("send CreateRelayRequest")
  274. }
  275. }
  276. continue
  277. }
  278. switch existingRelay.State {
  279. case Established:
  280. hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Send handshake via relay")
  281. hm.f.SendVia(relayHostInfo, existingRelay, hostinfo.HandshakePacket[0], make([]byte, 12), make([]byte, mtu), false)
  282. case Disestablished:
  283. // Mark this relay as 'requested'
  284. relayHostInfo.relayState.UpdateRelayForByIpState(vpnIp, Requested)
  285. fallthrough
  286. case Requested:
  287. hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Re-send CreateRelay request")
  288. // Re-send the CreateRelay request, in case the previous one was lost.
  289. relayFrom := hm.f.myVpnNet.Addr().As4()
  290. relayTo := vpnIp.As4()
  291. m := NebulaControl{
  292. Type: NebulaControl_CreateRelayRequest,
  293. InitiatorRelayIndex: existingRelay.LocalIndex,
  294. RelayFromIp: binary.BigEndian.Uint32(relayFrom[:]),
  295. RelayToIp: binary.BigEndian.Uint32(relayTo[:]),
  296. }
  297. msg, err := m.Marshal()
  298. if err != nil {
  299. hostinfo.logger(hm.l).
  300. WithError(err).
  301. Error("Failed to marshal Control message to create relay")
  302. } else {
  303. // This must send over the hostinfo, not over hm.Hosts[ip]
  304. hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
  305. hm.l.WithFields(logrus.Fields{
  306. "relayFrom": hm.f.myVpnNet,
  307. "relayTo": vpnIp,
  308. "initiatorRelayIndex": existingRelay.LocalIndex,
  309. "relay": relay}).
  310. Info("send CreateRelayRequest")
  311. }
  312. case PeerRequested:
  313. // PeerRequested only occurs in Forwarding relays, not Terminal relays, and this is a Terminal relay case.
  314. fallthrough
  315. default:
  316. hostinfo.logger(hm.l).
  317. WithField("vpnIp", vpnIp).
  318. WithField("state", existingRelay.State).
  319. WithField("relay", relay).
  320. Errorf("Relay unexpected state")
  321. }
  322. }
  323. }
  324. // If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
  325. if !lighthouseTriggered {
  326. hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
  327. }
  328. }
  329. // GetOrHandshake will try to find a hostinfo with a fully formed tunnel or start a new handshake if one is not present
  330. // The 2nd argument will be true if the hostinfo is ready to transmit traffic
  331. func (hm *HandshakeManager) GetOrHandshake(vpnIp netip.Addr, cacheCb func(*HandshakeHostInfo)) (*HostInfo, bool) {
  332. hm.mainHostMap.RLock()
  333. h, ok := hm.mainHostMap.Hosts[vpnIp]
  334. hm.mainHostMap.RUnlock()
  335. if ok {
  336. // Do not attempt promotion if you are a lighthouse
  337. if !hm.lightHouse.amLighthouse {
  338. h.TryPromoteBest(hm.mainHostMap.GetPreferredRanges(), hm.f)
  339. }
  340. return h, true
  341. }
  342. return hm.StartHandshake(vpnIp, cacheCb), false
  343. }
  344. // StartHandshake will ensure a handshake is currently being attempted for the provided vpn ip
  345. func (hm *HandshakeManager) StartHandshake(vpnIp netip.Addr, cacheCb func(*HandshakeHostInfo)) *HostInfo {
  346. hm.Lock()
  347. if hh, ok := hm.vpnIps[vpnIp]; ok {
  348. // We are already trying to handshake with this vpn ip
  349. if cacheCb != nil {
  350. cacheCb(hh)
  351. }
  352. hm.Unlock()
  353. return hh.hostinfo
  354. }
  355. hostinfo := &HostInfo{
  356. vpnIp: vpnIp,
  357. HandshakePacket: make(map[uint8][]byte, 0),
  358. relayState: RelayState{
  359. relays: map[netip.Addr]struct{}{},
  360. relayForByIp: map[netip.Addr]*Relay{},
  361. relayForByIdx: map[uint32]*Relay{},
  362. },
  363. }
  364. hh := &HandshakeHostInfo{
  365. hostinfo: hostinfo,
  366. startTime: time.Now(),
  367. }
  368. hm.vpnIps[vpnIp] = hh
  369. hm.metricInitiated.Inc(1)
  370. hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval)
  371. if cacheCb != nil {
  372. cacheCb(hh)
  373. }
  374. // If this is a static host, we don't need to wait for the HostQueryReply
  375. // We can trigger the handshake right now
  376. _, doTrigger := hm.lightHouse.GetStaticHostList()[vpnIp]
  377. if !doTrigger {
  378. // Add any calculated remotes, and trigger early handshake if one found
  379. doTrigger = hm.lightHouse.addCalculatedRemotes(vpnIp)
  380. }
  381. if doTrigger {
  382. select {
  383. case hm.trigger <- vpnIp:
  384. default:
  385. }
  386. }
  387. hm.Unlock()
  388. hm.lightHouse.QueryServer(vpnIp)
  389. return hostinfo
  390. }
  391. var (
  392. ErrExistingHostInfo = errors.New("existing hostinfo")
  393. ErrAlreadySeen = errors.New("already seen")
  394. ErrLocalIndexCollision = errors.New("local index collision")
  395. )
  396. // CheckAndComplete checks for any conflicts in the main and pending hostmap
  397. // before adding hostinfo to main. If err is nil, it was added. Otherwise err will be:
  398. //
  399. // ErrAlreadySeen if we already have an entry in the hostmap that has seen the
  400. // exact same handshake packet
  401. //
  402. // ErrExistingHostInfo if we already have an entry in the hostmap for this
  403. // VpnIp and the new handshake was older than the one we currently have
  404. //
  405. // ErrLocalIndexCollision if we already have an entry in the main or pending
  406. // hostmap for the hostinfo.localIndexId.
  407. func (c *HandshakeManager) CheckAndComplete(hostinfo *HostInfo, handshakePacket uint8, f *Interface) (*HostInfo, error) {
  408. c.mainHostMap.Lock()
  409. defer c.mainHostMap.Unlock()
  410. c.Lock()
  411. defer c.Unlock()
  412. // Check if we already have a tunnel with this vpn ip
  413. existingHostInfo, found := c.mainHostMap.Hosts[hostinfo.vpnIp]
  414. if found && existingHostInfo != nil {
  415. testHostInfo := existingHostInfo
  416. for testHostInfo != nil {
  417. // Is it just a delayed handshake packet?
  418. if bytes.Equal(hostinfo.HandshakePacket[handshakePacket], testHostInfo.HandshakePacket[handshakePacket]) {
  419. return testHostInfo, ErrAlreadySeen
  420. }
  421. testHostInfo = testHostInfo.next
  422. }
  423. // Is this a newer handshake?
  424. if existingHostInfo.lastHandshakeTime >= hostinfo.lastHandshakeTime && !existingHostInfo.ConnectionState.initiator {
  425. return existingHostInfo, ErrExistingHostInfo
  426. }
  427. existingHostInfo.logger(c.l).Info("Taking new handshake")
  428. }
  429. existingIndex, found := c.mainHostMap.Indexes[hostinfo.localIndexId]
  430. if found {
  431. // We have a collision, but for a different hostinfo
  432. return existingIndex, ErrLocalIndexCollision
  433. }
  434. existingPendingIndex, found := c.indexes[hostinfo.localIndexId]
  435. if found && existingPendingIndex.hostinfo != hostinfo {
  436. // We have a collision, but for a different hostinfo
  437. return existingPendingIndex.hostinfo, ErrLocalIndexCollision
  438. }
  439. existingRemoteIndex, found := c.mainHostMap.RemoteIndexes[hostinfo.remoteIndexId]
  440. if found && existingRemoteIndex != nil && existingRemoteIndex.vpnIp != hostinfo.vpnIp {
  441. // We have a collision, but this can happen since we can't control
  442. // the remote ID. Just log about the situation as a note.
  443. hostinfo.logger(c.l).
  444. WithField("remoteIndex", hostinfo.remoteIndexId).WithField("collision", existingRemoteIndex.vpnIp).
  445. Info("New host shadows existing host remoteIndex")
  446. }
  447. c.mainHostMap.unlockedAddHostInfo(hostinfo, f)
  448. return existingHostInfo, nil
  449. }
  450. // Complete is a simpler version of CheckAndComplete when we already know we
  451. // won't have a localIndexId collision because we already have an entry in the
  452. // pendingHostMap. An existing hostinfo is returned if there was one.
  453. func (hm *HandshakeManager) Complete(hostinfo *HostInfo, f *Interface) {
  454. hm.mainHostMap.Lock()
  455. defer hm.mainHostMap.Unlock()
  456. hm.Lock()
  457. defer hm.Unlock()
  458. existingRemoteIndex, found := hm.mainHostMap.RemoteIndexes[hostinfo.remoteIndexId]
  459. if found && existingRemoteIndex != nil {
  460. // We have a collision, but this can happen since we can't control
  461. // the remote ID. Just log about the situation as a note.
  462. hostinfo.logger(hm.l).
  463. WithField("remoteIndex", hostinfo.remoteIndexId).WithField("collision", existingRemoteIndex.vpnIp).
  464. Info("New host shadows existing host remoteIndex")
  465. }
  466. // We need to remove from the pending hostmap first to avoid undoing work when after to the main hostmap.
  467. hm.unlockedDeleteHostInfo(hostinfo)
  468. hm.mainHostMap.unlockedAddHostInfo(hostinfo, f)
  469. }
  470. // allocateIndex generates a unique localIndexId for this HostInfo
  471. // and adds it to the pendingHostMap. Will error if we are unable to generate
  472. // a unique localIndexId
  473. func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) error {
  474. hm.mainHostMap.RLock()
  475. defer hm.mainHostMap.RUnlock()
  476. hm.Lock()
  477. defer hm.Unlock()
  478. for i := 0; i < 32; i++ {
  479. index, err := generateIndex(hm.l)
  480. if err != nil {
  481. return err
  482. }
  483. _, inPending := hm.indexes[index]
  484. _, inMain := hm.mainHostMap.Indexes[index]
  485. if !inMain && !inPending {
  486. hh.hostinfo.localIndexId = index
  487. hm.indexes[index] = hh
  488. return nil
  489. }
  490. }
  491. return errors.New("failed to generate unique localIndexId")
  492. }
  493. func (c *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
  494. c.Lock()
  495. defer c.Unlock()
  496. c.unlockedDeleteHostInfo(hostinfo)
  497. }
  498. func (c *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
  499. delete(c.vpnIps, hostinfo.vpnIp)
  500. if len(c.vpnIps) == 0 {
  501. c.vpnIps = map[netip.Addr]*HandshakeHostInfo{}
  502. }
  503. delete(c.indexes, hostinfo.localIndexId)
  504. if len(c.vpnIps) == 0 {
  505. c.indexes = map[uint32]*HandshakeHostInfo{}
  506. }
  507. if c.l.Level >= logrus.DebugLevel {
  508. c.l.WithField("hostMap", m{"mapTotalSize": len(c.vpnIps),
  509. "vpnIp": hostinfo.vpnIp, "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId}).
  510. Debug("Pending hostmap hostInfo deleted")
  511. }
  512. }
  513. func (hm *HandshakeManager) QueryVpnIp(vpnIp netip.Addr) *HostInfo {
  514. hh := hm.queryVpnIp(vpnIp)
  515. if hh != nil {
  516. return hh.hostinfo
  517. }
  518. return nil
  519. }
  520. func (hm *HandshakeManager) queryVpnIp(vpnIp netip.Addr) *HandshakeHostInfo {
  521. hm.RLock()
  522. defer hm.RUnlock()
  523. return hm.vpnIps[vpnIp]
  524. }
  525. func (hm *HandshakeManager) QueryIndex(index uint32) *HostInfo {
  526. hh := hm.queryIndex(index)
  527. if hh != nil {
  528. return hh.hostinfo
  529. }
  530. return nil
  531. }
  532. func (hm *HandshakeManager) queryIndex(index uint32) *HandshakeHostInfo {
  533. hm.RLock()
  534. defer hm.RUnlock()
  535. return hm.indexes[index]
  536. }
  537. func (c *HandshakeManager) GetPreferredRanges() []netip.Prefix {
  538. return c.mainHostMap.GetPreferredRanges()
  539. }
  540. func (c *HandshakeManager) ForEachVpnIp(f controlEach) {
  541. c.RLock()
  542. defer c.RUnlock()
  543. for _, v := range c.vpnIps {
  544. f(v.hostinfo)
  545. }
  546. }
  547. func (c *HandshakeManager) ForEachIndex(f controlEach) {
  548. c.RLock()
  549. defer c.RUnlock()
  550. for _, v := range c.indexes {
  551. f(v.hostinfo)
  552. }
  553. }
  554. func (c *HandshakeManager) EmitStats() {
  555. c.RLock()
  556. hostLen := len(c.vpnIps)
  557. indexLen := len(c.indexes)
  558. c.RUnlock()
  559. metrics.GetOrRegisterGauge("hostmap.pending.hosts", nil).Update(int64(hostLen))
  560. metrics.GetOrRegisterGauge("hostmap.pending.indexes", nil).Update(int64(indexLen))
  561. c.mainHostMap.EmitStats()
  562. }
  563. // Utility functions below
  564. func generateIndex(l *logrus.Logger) (uint32, error) {
  565. b := make([]byte, 4)
  566. // Let zero mean we don't know the ID, so don't generate zero
  567. var index uint32
  568. for index == 0 {
  569. _, err := rand.Read(b)
  570. if err != nil {
  571. l.Errorln(err)
  572. return 0, err
  573. }
  574. index = binary.BigEndian.Uint32(b)
  575. }
  576. if l.Level >= logrus.DebugLevel {
  577. l.WithField("index", index).
  578. Debug("Generated index")
  579. }
  580. return index, nil
  581. }
  582. func hsTimeout(tries int64, interval time.Duration) time.Duration {
  583. return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
  584. }