hostmap.go 22 KB

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