hostmap.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "log/slog"
  8. "net"
  9. "net/netip"
  10. "slices"
  11. "sync"
  12. "sync/atomic"
  13. "time"
  14. "github.com/gaissmai/bart"
  15. "github.com/rcrowley/go-metrics"
  16. "github.com/slackhq/nebula/cert"
  17. "github.com/slackhq/nebula/config"
  18. "github.com/slackhq/nebula/header"
  19. "github.com/slackhq/nebula/logging"
  20. )
  21. const defaultPromoteEvery = 1000 // Count of packets sent before we try moving a tunnel to a preferred underlay ip address
  22. const defaultReQueryEvery = 5000 // Count of packets sent before re-querying a hostinfo to the lighthouse
  23. const defaultReQueryWait = time.Minute // Minimum amount of seconds to wait before re-querying a hostinfo the lighthouse. Evaluated every ReQueryEvery
  24. const MaxRemotes = 10
  25. // MaxHostInfosPerVpnIp is the max number of hostinfos we will track for a given vpn ip
  26. // 5 allows for an initial handshake and each host pair re-handshaking twice
  27. const MaxHostInfosPerVpnIp = 5
  28. // How long we should prevent roaming back to the previous IP.
  29. // This helps prevent flapping due to packets already in flight
  30. const RoamingSuppressSeconds = 2
  31. const (
  32. Requested = iota
  33. PeerRequested
  34. Established
  35. Disestablished
  36. )
  37. const (
  38. Unknowntype = iota
  39. ForwardingType
  40. TerminalType
  41. )
  42. type Relay struct {
  43. Type int
  44. State int
  45. LocalIndex uint32
  46. RemoteIndex uint32
  47. PeerAddr netip.Addr
  48. }
  49. type HostMap struct {
  50. sync.RWMutex //Because we concurrently read and write to our maps
  51. Indexes map[uint32]*HostInfo
  52. Relays map[uint32]*HostInfo // Maps a Relay IDX to a Relay HostInfo object
  53. RemoteIndexes map[uint32]*HostInfo
  54. Hosts map[netip.Addr]*HostInfo
  55. preferredRanges atomic.Pointer[[]netip.Prefix]
  56. l *slog.Logger
  57. }
  58. // For synchronization, treat the pointed-to Relay struct as immutable. To edit the Relay
  59. // struct, make a copy of an existing value, edit the fileds in the copy, and
  60. // then store a pointer to the new copy in both realyForBy* maps.
  61. type RelayState struct {
  62. sync.RWMutex
  63. relays []netip.Addr // Ordered set of VpnAddrs of Hosts to use as relays to access this peer
  64. // For data race avoidance, the contents of a *Relay are treated immutably. To update a *Relay, copy the existing data,
  65. // modify what needs to be updated, and store the new modified copy in the relayForByIp and relayForByIdx maps (with
  66. // the RelayState Lock held)
  67. relayForByAddr map[netip.Addr]*Relay // Maps vpnAddr of peers for which this HostInfo is a relay to some Relay info
  68. relayForByIdx map[uint32]*Relay // Maps a local index to some Relay info
  69. }
  70. func (rs *RelayState) DeleteRelay(ip netip.Addr) {
  71. rs.Lock()
  72. defer rs.Unlock()
  73. for idx, val := range rs.relays {
  74. if val == ip {
  75. rs.relays = append(rs.relays[:idx], rs.relays[idx+1:]...)
  76. return
  77. }
  78. }
  79. }
  80. func (rs *RelayState) UpdateRelayForByIpState(vpnIp netip.Addr, state int) {
  81. rs.Lock()
  82. defer rs.Unlock()
  83. if r, ok := rs.relayForByAddr[vpnIp]; ok {
  84. newRelay := *r
  85. newRelay.State = state
  86. rs.relayForByAddr[newRelay.PeerAddr] = &newRelay
  87. rs.relayForByIdx[newRelay.LocalIndex] = &newRelay
  88. }
  89. }
  90. func (rs *RelayState) UpdateRelayForByIdxState(idx uint32, state int) {
  91. rs.Lock()
  92. defer rs.Unlock()
  93. if r, ok := rs.relayForByIdx[idx]; ok {
  94. newRelay := *r
  95. newRelay.State = state
  96. rs.relayForByAddr[newRelay.PeerAddr] = &newRelay
  97. rs.relayForByIdx[newRelay.LocalIndex] = &newRelay
  98. }
  99. }
  100. func (rs *RelayState) CopyAllRelayFor() []*Relay {
  101. rs.RLock()
  102. defer rs.RUnlock()
  103. ret := make([]*Relay, 0, len(rs.relayForByIdx))
  104. for _, r := range rs.relayForByIdx {
  105. ret = append(ret, r)
  106. }
  107. return ret
  108. }
  109. func (rs *RelayState) GetRelayForByAddr(addr netip.Addr) (*Relay, bool) {
  110. rs.RLock()
  111. defer rs.RUnlock()
  112. r, ok := rs.relayForByAddr[addr]
  113. return r, ok
  114. }
  115. func (rs *RelayState) InsertRelayTo(ip netip.Addr) {
  116. rs.Lock()
  117. defer rs.Unlock()
  118. if !slices.Contains(rs.relays, ip) {
  119. rs.relays = append(rs.relays, ip)
  120. }
  121. }
  122. func (rs *RelayState) CopyRelayIps() []netip.Addr {
  123. rs.RLock()
  124. defer rs.RUnlock()
  125. ret := make([]netip.Addr, len(rs.relays))
  126. copy(ret, rs.relays)
  127. return ret
  128. }
  129. func (rs *RelayState) CopyRelayForIps() []netip.Addr {
  130. rs.RLock()
  131. defer rs.RUnlock()
  132. currentRelays := make([]netip.Addr, 0, len(rs.relayForByAddr))
  133. for relayIp := range rs.relayForByAddr {
  134. currentRelays = append(currentRelays, relayIp)
  135. }
  136. return currentRelays
  137. }
  138. func (rs *RelayState) CopyRelayForIdxs() []uint32 {
  139. rs.RLock()
  140. defer rs.RUnlock()
  141. ret := make([]uint32, 0, len(rs.relayForByIdx))
  142. for i := range rs.relayForByIdx {
  143. ret = append(ret, i)
  144. }
  145. return ret
  146. }
  147. func (rs *RelayState) CompleteRelayByIP(vpnIp netip.Addr, remoteIdx uint32) bool {
  148. rs.Lock()
  149. defer rs.Unlock()
  150. r, ok := rs.relayForByAddr[vpnIp]
  151. if !ok {
  152. return false
  153. }
  154. newRelay := *r
  155. newRelay.State = Established
  156. newRelay.RemoteIndex = remoteIdx
  157. rs.relayForByIdx[r.LocalIndex] = &newRelay
  158. rs.relayForByAddr[r.PeerAddr] = &newRelay
  159. return true
  160. }
  161. func (rs *RelayState) CompleteRelayByIdx(localIdx uint32, remoteIdx uint32) (*Relay, bool) {
  162. rs.Lock()
  163. defer rs.Unlock()
  164. r, ok := rs.relayForByIdx[localIdx]
  165. if !ok {
  166. return nil, false
  167. }
  168. newRelay := *r
  169. newRelay.State = Established
  170. newRelay.RemoteIndex = remoteIdx
  171. rs.relayForByIdx[r.LocalIndex] = &newRelay
  172. rs.relayForByAddr[r.PeerAddr] = &newRelay
  173. return &newRelay, true
  174. }
  175. func (rs *RelayState) QueryRelayForByIp(vpnIp netip.Addr) (*Relay, bool) {
  176. rs.RLock()
  177. defer rs.RUnlock()
  178. r, ok := rs.relayForByAddr[vpnIp]
  179. return r, ok
  180. }
  181. func (rs *RelayState) QueryRelayForByIdx(idx uint32) (*Relay, bool) {
  182. rs.RLock()
  183. defer rs.RUnlock()
  184. r, ok := rs.relayForByIdx[idx]
  185. return r, ok
  186. }
  187. func (rs *RelayState) InsertRelay(ip netip.Addr, idx uint32, r *Relay) {
  188. rs.Lock()
  189. defer rs.Unlock()
  190. rs.relayForByAddr[ip] = r
  191. rs.relayForByIdx[idx] = r
  192. }
  193. type NetworkType uint8
  194. const (
  195. NetworkTypeUnknown NetworkType = iota
  196. // NetworkTypeVPN is a network that overlaps one or more of the vpnNetworks in our certificate
  197. NetworkTypeVPN
  198. // NetworkTypeVPNPeer is a network that does not overlap one of our networks
  199. NetworkTypeVPNPeer
  200. // NetworkTypeUnsafe is a network from Certificate.UnsafeNetworks()
  201. NetworkTypeUnsafe
  202. )
  203. type HostInfo struct {
  204. remote netip.AddrPort
  205. remotes *RemoteList
  206. promoteCounter atomic.Uint32
  207. ConnectionState *ConnectionState
  208. remoteIndexId uint32
  209. localIndexId uint32
  210. // vpnAddrs is a list of vpn addresses assigned to this host that are within our own vpn networks
  211. // The host may have other vpn addresses that are outside our
  212. // vpn networks but were removed because they are not usable
  213. vpnAddrs []netip.Addr
  214. // networks is a combination of specific vpn addresses (not prefixes!) and full unsafe networks assigned to this host.
  215. networks *bart.Table[NetworkType]
  216. relayState RelayState
  217. // HandshakePacket records the packets used to create this hostinfo
  218. // We need these to avoid replayed handshake packets creating new hostinfos which causes churn
  219. HandshakePacket map[uint8][]byte
  220. // nextLHQuery is the earliest we can ask the lighthouse for new information.
  221. // This is used to limit lighthouse re-queries in chatty clients
  222. nextLHQuery atomic.Int64
  223. // lastRebindCount is the other side of Interface.rebindCount, if these values don't match then we need to ask LH
  224. // for a punch from the remote end of this tunnel. The goal being to prime their conntrack for our traffic just like
  225. // with a handshake
  226. lastRebindCount int8
  227. // lastHandshakeTime records the time the remote side told us about at the stage when the handshake was completed locally
  228. // Stage 1 packet will contain it if I am a responder, stage 2 packet if I am an initiator
  229. // This is used to avoid an attack where a handshake packet is replayed after some time
  230. lastHandshakeTime uint64
  231. lastRoam time.Time
  232. lastRoamRemote netip.AddrPort
  233. // Used to track other hostinfos for this vpn ip since only 1 can be primary
  234. // Synchronised via hostmap lock and not the hostinfo lock.
  235. next, prev *HostInfo
  236. //TODO: in, out, and others might benefit from being an atomic.Int32. We could collapse connectionManager pendingDeletion, relayUsed, and in/out into this 1 thing
  237. in, out, pendingDeletion atomic.Bool
  238. // lastUsed tracks the last time ConnectionManager checked the tunnel and it was in use.
  239. // This value will be behind against actual tunnel utilization in the hot path.
  240. // This should only be used by the ConnectionManagers ticker routine.
  241. lastUsed time.Time
  242. }
  243. type ViaSender struct {
  244. UdpAddr netip.AddrPort
  245. relayHI *HostInfo // relayHI is the host info object of the relay
  246. remoteIdx uint32 // remoteIdx is the index included in the header of the received packet
  247. relay *Relay // relay contains the rest of the relay information, including the PeerIP of the host trying to communicate with us.
  248. IsRelayed bool // IsRelayed is true if the packet was sent through a relay
  249. }
  250. func (v ViaSender) String() string {
  251. if v.IsRelayed {
  252. return fmt.Sprintf("%s (relayed)", v.UdpAddr)
  253. }
  254. return v.UdpAddr.String()
  255. }
  256. func (v ViaSender) MarshalJSON() ([]byte, error) {
  257. if v.IsRelayed {
  258. return json.Marshal(m{"relay": v.UdpAddr})
  259. }
  260. return json.Marshal(m{"direct": v.UdpAddr})
  261. }
  262. type cachedPacket struct {
  263. messageType header.MessageType
  264. messageSubType header.MessageSubType
  265. callback packetCallback
  266. packet []byte
  267. }
  268. type packetCallback func(t header.MessageType, st header.MessageSubType, h *HostInfo, p, nb, out []byte)
  269. type cachedPacketMetrics struct {
  270. sent metrics.Counter
  271. dropped metrics.Counter
  272. }
  273. func NewHostMapFromConfig(l *slog.Logger, c *config.C) *HostMap {
  274. hm := newHostMap(l)
  275. hm.reload(c, true)
  276. c.RegisterReloadCallback(func(c *config.C) {
  277. hm.reload(c, false)
  278. })
  279. l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
  280. return hm
  281. }
  282. func newHostMap(l *slog.Logger) *HostMap {
  283. return &HostMap{
  284. Indexes: map[uint32]*HostInfo{},
  285. Relays: map[uint32]*HostInfo{},
  286. RemoteIndexes: map[uint32]*HostInfo{},
  287. Hosts: map[netip.Addr]*HostInfo{},
  288. l: l,
  289. }
  290. }
  291. func (hm *HostMap) reload(c *config.C, initial bool) {
  292. if initial || c.HasChanged("preferred_ranges") {
  293. var preferredRanges []netip.Prefix
  294. rawPreferredRanges := c.GetStringSlice("preferred_ranges", []string{})
  295. for _, rawPreferredRange := range rawPreferredRanges {
  296. preferredRange, err := netip.ParsePrefix(rawPreferredRange)
  297. if err != nil {
  298. hm.l.Warn("Failed to parse preferred ranges, ignoring",
  299. "error", err,
  300. "range", rawPreferredRanges,
  301. )
  302. continue
  303. }
  304. preferredRanges = append(preferredRanges, preferredRange)
  305. }
  306. oldRanges := hm.preferredRanges.Swap(&preferredRanges)
  307. if !initial {
  308. hm.l.Info("preferred_ranges changed",
  309. "oldPreferredRanges", *oldRanges,
  310. "newPreferredRanges", preferredRanges,
  311. )
  312. }
  313. }
  314. }
  315. // EmitStats reports host, index, and relay counts to the stats collection system
  316. func (hm *HostMap) EmitStats() {
  317. hm.RLock()
  318. hostLen := len(hm.Hosts)
  319. indexLen := len(hm.Indexes)
  320. remoteIndexLen := len(hm.RemoteIndexes)
  321. relaysLen := len(hm.Relays)
  322. hm.RUnlock()
  323. metrics.GetOrRegisterGauge("hostmap.main.hosts", nil).Update(int64(hostLen))
  324. metrics.GetOrRegisterGauge("hostmap.main.indexes", nil).Update(int64(indexLen))
  325. metrics.GetOrRegisterGauge("hostmap.main.remoteIndexes", nil).Update(int64(remoteIndexLen))
  326. metrics.GetOrRegisterGauge("hostmap.main.relayIndexes", nil).Update(int64(relaysLen))
  327. }
  328. // DeleteHostInfo will fully unlink the hostinfo and return true if it was the final hostinfo for this vpn ip
  329. func (hm *HostMap) DeleteHostInfo(hostinfo *HostInfo) bool {
  330. // Delete the host itself, ensuring it's not modified anymore
  331. hm.Lock()
  332. // If we have a previous or next hostinfo then we are not the last one for this vpn ip
  333. final := (hostinfo.next == nil && hostinfo.prev == nil)
  334. hm.unlockedDeleteHostInfo(hostinfo)
  335. hm.Unlock()
  336. return final
  337. }
  338. func (hm *HostMap) MakePrimary(hostinfo *HostInfo) {
  339. hm.Lock()
  340. defer hm.Unlock()
  341. hm.unlockedMakePrimary(hostinfo)
  342. }
  343. func (hm *HostMap) unlockedMakePrimary(hostinfo *HostInfo) {
  344. // Get the current primary, if it exists
  345. oldHostinfo := hm.Hosts[hostinfo.vpnAddrs[0]]
  346. // Every address in the hostinfo gets elevated to primary
  347. for _, vpnAddr := range hostinfo.vpnAddrs {
  348. //NOTE: It is possible that we leave a dangling hostinfo here but connection manager works on
  349. // indexes so it should be fine.
  350. hm.Hosts[vpnAddr] = hostinfo
  351. }
  352. // If we are already primary then we won't bother re-linking
  353. if oldHostinfo == hostinfo {
  354. return
  355. }
  356. // Unlink this hostinfo
  357. if hostinfo.prev != nil {
  358. hostinfo.prev.next = hostinfo.next
  359. }
  360. if hostinfo.next != nil {
  361. hostinfo.next.prev = hostinfo.prev
  362. }
  363. // If there wasn't a previous primary then clear out any links
  364. if oldHostinfo == nil {
  365. hostinfo.next = nil
  366. hostinfo.prev = nil
  367. return
  368. }
  369. // Relink the hostinfo as primary
  370. hostinfo.next = oldHostinfo
  371. oldHostinfo.prev = hostinfo
  372. hostinfo.prev = nil
  373. }
  374. func (hm *HostMap) unlockedDeleteHostInfo(hostinfo *HostInfo) {
  375. for _, addr := range hostinfo.vpnAddrs {
  376. h := hm.Hosts[addr]
  377. for h != nil {
  378. if h == hostinfo {
  379. hm.unlockedInnerDeleteHostInfo(h, addr)
  380. }
  381. h = h.next
  382. }
  383. }
  384. }
  385. func (hm *HostMap) unlockedInnerDeleteHostInfo(hostinfo *HostInfo, addr netip.Addr) {
  386. primary, ok := hm.Hosts[addr]
  387. isLastHostinfo := hostinfo.next == nil && hostinfo.prev == nil
  388. if ok && primary == hostinfo {
  389. // The vpn addr pointer points to the same hostinfo as the local index id, we can remove it
  390. delete(hm.Hosts, addr)
  391. if len(hm.Hosts) == 0 {
  392. hm.Hosts = map[netip.Addr]*HostInfo{}
  393. }
  394. if hostinfo.next != nil {
  395. // We had more than 1 hostinfo at this vpn addr, promote the next in the list to primary
  396. hm.Hosts[addr] = hostinfo.next
  397. // It is primary, there is no previous hostinfo now
  398. hostinfo.next.prev = nil
  399. }
  400. } else {
  401. // Relink if we were in the middle of multiple hostinfos for this vpn addr
  402. if hostinfo.prev != nil {
  403. hostinfo.prev.next = hostinfo.next
  404. }
  405. if hostinfo.next != nil {
  406. hostinfo.next.prev = hostinfo.prev
  407. }
  408. }
  409. hostinfo.next = nil
  410. hostinfo.prev = nil
  411. // The remote index uses index ids outside our control so lets make sure we are only removing
  412. // the remote index pointer here if it points to the hostinfo we are deleting
  413. hostinfo2, ok := hm.RemoteIndexes[hostinfo.remoteIndexId]
  414. if ok && hostinfo2 == hostinfo {
  415. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  416. if len(hm.RemoteIndexes) == 0 {
  417. hm.RemoteIndexes = map[uint32]*HostInfo{}
  418. }
  419. }
  420. delete(hm.Indexes, hostinfo.localIndexId)
  421. if len(hm.Indexes) == 0 {
  422. hm.Indexes = map[uint32]*HostInfo{}
  423. }
  424. if hm.l.Enabled(context.Background(), slog.LevelDebug) {
  425. hm.l.Debug("Hostmap hostInfo deleted",
  426. "hostMap", m{"mapTotalSize": len(hm.Hosts),
  427. "vpnAddrs": hostinfo.vpnAddrs, "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId},
  428. )
  429. }
  430. if isLastHostinfo {
  431. // I have lost connectivity to my peers. My relay tunnel is likely broken. Mark the next
  432. // hops as 'Requested' so that new relay tunnels are created in the future.
  433. hm.unlockedDisestablishVpnAddrRelayFor(hostinfo)
  434. }
  435. // Clean up any local relay indexes for which I am acting as a relay hop
  436. for _, localRelayIdx := range hostinfo.relayState.CopyRelayForIdxs() {
  437. delete(hm.Relays, localRelayIdx)
  438. }
  439. }
  440. func (hm *HostMap) QueryIndex(index uint32) *HostInfo {
  441. hm.RLock()
  442. if h, ok := hm.Indexes[index]; ok {
  443. hm.RUnlock()
  444. return h
  445. } else {
  446. hm.RUnlock()
  447. return nil
  448. }
  449. }
  450. func (hm *HostMap) QueryRelayIndex(index uint32) *HostInfo {
  451. hm.RLock()
  452. if h, ok := hm.Relays[index]; ok {
  453. hm.RUnlock()
  454. return h
  455. } else {
  456. hm.RUnlock()
  457. return nil
  458. }
  459. }
  460. func (hm *HostMap) QueryReverseIndex(index uint32) *HostInfo {
  461. hm.RLock()
  462. if h, ok := hm.RemoteIndexes[index]; ok {
  463. hm.RUnlock()
  464. return h
  465. } else {
  466. hm.RUnlock()
  467. return nil
  468. }
  469. }
  470. func (hm *HostMap) QueryVpnAddr(vpnIp netip.Addr) *HostInfo {
  471. return hm.queryVpnAddr(vpnIp, nil)
  472. }
  473. func (hm *HostMap) QueryVpnAddrsRelayFor(targetIps []netip.Addr, relayHostIp netip.Addr) (*HostInfo, *Relay, error) {
  474. hm.RLock()
  475. defer hm.RUnlock()
  476. h, ok := hm.Hosts[relayHostIp]
  477. if !ok {
  478. return nil, nil, errors.New("unable to find host")
  479. }
  480. for h != nil {
  481. for _, targetIp := range targetIps {
  482. r, ok := h.relayState.QueryRelayForByIp(targetIp)
  483. if ok && r.State == Established {
  484. return h, r, nil
  485. }
  486. }
  487. h = h.next
  488. }
  489. return nil, nil, errors.New("unable to find host with relay")
  490. }
  491. func (hm *HostMap) unlockedDisestablishVpnAddrRelayFor(hi *HostInfo) {
  492. for _, relayHostIp := range hi.relayState.CopyRelayIps() {
  493. if h, ok := hm.Hosts[relayHostIp]; ok {
  494. for h != nil {
  495. h.relayState.UpdateRelayForByIpState(hi.vpnAddrs[0], Disestablished)
  496. h = h.next
  497. }
  498. }
  499. }
  500. for _, rs := range hi.relayState.CopyAllRelayFor() {
  501. if rs.Type == ForwardingType {
  502. if h, ok := hm.Hosts[rs.PeerAddr]; ok {
  503. for h != nil {
  504. h.relayState.UpdateRelayForByIpState(hi.vpnAddrs[0], Disestablished)
  505. h = h.next
  506. }
  507. }
  508. }
  509. }
  510. }
  511. func (hm *HostMap) queryVpnAddr(vpnIp netip.Addr, promoteIfce *Interface) *HostInfo {
  512. hm.RLock()
  513. if h, ok := hm.Hosts[vpnIp]; ok {
  514. hm.RUnlock()
  515. // Do not attempt promotion if you are a lighthouse
  516. if promoteIfce != nil && !promoteIfce.lightHouse.amLighthouse {
  517. h.TryPromoteBest(hm.GetPreferredRanges(), promoteIfce)
  518. }
  519. return h
  520. }
  521. hm.RUnlock()
  522. return nil
  523. }
  524. // unlockedAddHostInfo assumes you have a write-lock and will add a hostinfo object to the hostmap Indexes and RemoteIndexes maps.
  525. // If an entry exists for the Hosts table (vpnIp -> hostinfo) then the provided hostinfo will be made primary
  526. func (hm *HostMap) unlockedAddHostInfo(hostinfo *HostInfo, f *Interface) {
  527. if f.dnsServer != nil {
  528. remoteCert := hostinfo.ConnectionState.peerCert
  529. f.dnsServer.Add(remoteCert.Certificate.Name()+".", hostinfo.vpnAddrs)
  530. }
  531. for _, addr := range hostinfo.vpnAddrs {
  532. hm.unlockedInnerAddHostInfo(addr, hostinfo, f)
  533. }
  534. hm.Indexes[hostinfo.localIndexId] = hostinfo
  535. hm.RemoteIndexes[hostinfo.remoteIndexId] = hostinfo
  536. hostinfo.out.Store(true)
  537. if f.connectionManager != nil { // f.connectionManager is only nil in some unit tests
  538. f.connectionManager.trafficTimer.Add(hostinfo.localIndexId, f.connectionManager.checkInterval)
  539. }
  540. if hm.l.Enabled(context.Background(), slog.LevelDebug) {
  541. hm.l.Debug("Hostmap vpnIp added",
  542. "hostMap", m{"vpnAddrs": hostinfo.vpnAddrs, "mapTotalSize": len(hm.Hosts),
  543. "hostinfo": m{"existing": true, "localIndexId": hostinfo.localIndexId, "vpnAddrs": hostinfo.vpnAddrs}},
  544. )
  545. }
  546. }
  547. func (hm *HostMap) unlockedInnerAddHostInfo(vpnAddr netip.Addr, hostinfo *HostInfo, f *Interface) {
  548. existing := hm.Hosts[vpnAddr]
  549. hm.Hosts[vpnAddr] = hostinfo
  550. if existing != nil && existing != hostinfo {
  551. hostinfo.next = existing
  552. existing.prev = hostinfo
  553. }
  554. i := 1
  555. check := hostinfo
  556. for check != nil {
  557. if i > MaxHostInfosPerVpnIp {
  558. hm.unlockedDeleteHostInfo(check)
  559. }
  560. check = check.next
  561. i++
  562. }
  563. }
  564. func (hm *HostMap) GetPreferredRanges() []netip.Prefix {
  565. //NOTE: if preferredRanges is ever not stored before a load this will fail to dereference a nil pointer
  566. return *hm.preferredRanges.Load()
  567. }
  568. func (hm *HostMap) ForEachVpnAddr(f controlEach) {
  569. hm.RLock()
  570. defer hm.RUnlock()
  571. for _, v := range hm.Hosts {
  572. f(v)
  573. }
  574. }
  575. func (hm *HostMap) ForEachIndex(f controlEach) {
  576. hm.RLock()
  577. defer hm.RUnlock()
  578. for _, v := range hm.Indexes {
  579. f(v)
  580. }
  581. }
  582. // TryPromoteBest handles re-querying lighthouses and probing for better paths
  583. // NOTE: It is an error to call this if you are a lighthouse since they should not roam clients!
  584. func (i *HostInfo) TryPromoteBest(preferredRanges []netip.Prefix, ifce *Interface) {
  585. c := i.promoteCounter.Add(1)
  586. if c%ifce.tryPromoteEvery.Load() == 0 {
  587. remote := i.remote
  588. // return early if we are already on a preferred remote
  589. if remote.IsValid() {
  590. rIP := remote.Addr()
  591. for _, l := range preferredRanges {
  592. if l.Contains(rIP) {
  593. return
  594. }
  595. }
  596. }
  597. i.remotes.ForEach(preferredRanges, func(addr netip.AddrPort, preferred bool) {
  598. if remote.IsValid() && (!addr.IsValid() || !preferred) {
  599. return
  600. }
  601. // Try to send a test packet to that host, this should
  602. // cause it to detect a roaming event and switch remotes
  603. ifce.sendTo(header.Test, header.TestRequest, i.ConnectionState, i, addr, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  604. })
  605. }
  606. // Re query our lighthouses for new remotes occasionally
  607. if c%ifce.reQueryEvery.Load() == 0 && ifce.lightHouse != nil {
  608. now := time.Now().UnixNano()
  609. if now < i.nextLHQuery.Load() {
  610. return
  611. }
  612. i.nextLHQuery.Store(now + ifce.reQueryWait.Load())
  613. ifce.lightHouse.QueryServer(i.vpnAddrs[0])
  614. }
  615. }
  616. func (i *HostInfo) GetCert() *cert.CachedCertificate {
  617. if i.ConnectionState != nil {
  618. return i.ConnectionState.peerCert
  619. }
  620. return nil
  621. }
  622. // TODO: Maybe use ViaSender here?
  623. func (i *HostInfo) SetRemote(remote netip.AddrPort) {
  624. // We copy here because we likely got this remote from a source that reuses the object
  625. if i.remote != remote {
  626. i.remote = remote
  627. i.remotes.LearnRemote(i.vpnAddrs[0], remote)
  628. }
  629. }
  630. // SetRemoteIfPreferred returns true if the remote was changed. The lastRoam
  631. // time on the HostInfo will also be updated.
  632. func (i *HostInfo) SetRemoteIfPreferred(hm *HostMap, via ViaSender) bool {
  633. if via.IsRelayed {
  634. return false
  635. }
  636. currentRemote := i.remote
  637. if !currentRemote.IsValid() {
  638. i.SetRemote(via.UdpAddr)
  639. return true
  640. }
  641. // NOTE: We do this loop here instead of calling `isPreferred` in
  642. // remote_list.go so that we only have to loop over preferredRanges once.
  643. newIsPreferred := false
  644. for _, l := range hm.GetPreferredRanges() {
  645. // return early if we are already on a preferred remote
  646. if l.Contains(currentRemote.Addr()) {
  647. return false
  648. }
  649. if l.Contains(via.UdpAddr.Addr()) {
  650. newIsPreferred = true
  651. }
  652. }
  653. if newIsPreferred {
  654. // Consider this a roaming event
  655. i.lastRoam = time.Now()
  656. i.lastRoamRemote = currentRemote
  657. i.SetRemote(via.UdpAddr)
  658. return true
  659. }
  660. return false
  661. }
  662. // buildNetworks fills in the networks field of HostInfo. It accepts a cert.Certificate so you never ever mix the network types up.
  663. func (i *HostInfo) buildNetworks(myVpnNetworksTable *bart.Lite, c cert.Certificate) {
  664. if len(c.Networks()) == 1 && len(c.UnsafeNetworks()) == 0 {
  665. if myVpnNetworksTable.Contains(c.Networks()[0].Addr()) {
  666. return // Simple case, no BART needed
  667. }
  668. }
  669. i.networks = new(bart.Table[NetworkType])
  670. for _, network := range c.Networks() {
  671. nprefix := netip.PrefixFrom(network.Addr(), network.Addr().BitLen())
  672. if myVpnNetworksTable.Contains(network.Addr()) {
  673. i.networks.Insert(nprefix, NetworkTypeVPN)
  674. } else {
  675. i.networks.Insert(nprefix, NetworkTypeVPNPeer)
  676. }
  677. }
  678. for _, network := range c.UnsafeNetworks() {
  679. i.networks.Insert(network, NetworkTypeUnsafe)
  680. }
  681. }
  682. // logger returns a derived slog.Logger with per-hostinfo fields pre-bound.
  683. func (i *HostInfo) logger(l *slog.Logger) *slog.Logger {
  684. if i == nil {
  685. return l
  686. }
  687. li := l.With(
  688. "vpnAddrs", i.vpnAddrs,
  689. "localIndex", i.localIndexId,
  690. "remoteIndex", i.remoteIndexId,
  691. )
  692. if connState := i.ConnectionState; connState != nil {
  693. if peerCert := connState.peerCert; peerCert != nil {
  694. li = li.With("certName", peerCert.Certificate.Name())
  695. }
  696. }
  697. return li
  698. }
  699. // Utility functions
  700. func localAddrs(l *slog.Logger, allowList *LocalAllowList) []netip.Addr {
  701. //FIXME: This function is pretty garbage
  702. var finalAddrs []netip.Addr
  703. ifaces, _ := net.Interfaces()
  704. for _, i := range ifaces {
  705. allow := allowList.AllowName(i.Name)
  706. if l.Enabled(context.Background(), logging.LevelTrace) {
  707. l.Log(context.Background(), logging.LevelTrace, "localAllowList.AllowName",
  708. "interfaceName", i.Name,
  709. "allow", allow,
  710. )
  711. }
  712. if !allow {
  713. continue
  714. }
  715. addrs, _ := i.Addrs()
  716. for _, rawAddr := range addrs {
  717. var addr netip.Addr
  718. switch v := rawAddr.(type) {
  719. case *net.IPNet:
  720. //continue
  721. addr, _ = netip.AddrFromSlice(v.IP)
  722. case *net.IPAddr:
  723. addr, _ = netip.AddrFromSlice(v.IP)
  724. }
  725. if !addr.IsValid() {
  726. if l.Enabled(context.Background(), slog.LevelDebug) {
  727. l.Debug("addr was invalid", "localAddr", rawAddr)
  728. }
  729. continue
  730. }
  731. addr = addr.Unmap()
  732. if addr.IsLoopback() == false && addr.IsLinkLocalUnicast() == false {
  733. isAllowed := allowList.Allow(addr)
  734. if l.Enabled(context.Background(), logging.LevelTrace) {
  735. l.Log(context.Background(), logging.LevelTrace, "localAllowList.Allow",
  736. "localAddr", addr,
  737. "allowed", isAllowed,
  738. )
  739. }
  740. if !isAllowed {
  741. continue
  742. }
  743. finalAddrs = append(finalAddrs, addr)
  744. }
  745. }
  746. }
  747. return finalAddrs
  748. }