1
0

handshake_manager.go 23 KB

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