hostmap.go 24 KB

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