hostmap.go 23 KB

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