hostmap.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "github.com/rcrowley/go-metrics"
  10. "github.com/sirupsen/logrus"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/slackhq/nebula/cidr"
  13. "github.com/slackhq/nebula/header"
  14. "github.com/slackhq/nebula/iputil"
  15. "github.com/slackhq/nebula/udp"
  16. )
  17. // const ProbeLen = 100
  18. const PromoteEvery = 1000
  19. const ReQueryEvery = 5000
  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. Established
  30. )
  31. const (
  32. Unknowntype = iota
  33. ForwardingType
  34. TerminalType
  35. )
  36. type Relay struct {
  37. Type int
  38. State int
  39. LocalIndex uint32
  40. RemoteIndex uint32
  41. PeerIp iputil.VpnIp
  42. }
  43. type HostMap struct {
  44. sync.RWMutex //Because we concurrently read and write to our maps
  45. name string
  46. Indexes map[uint32]*HostInfo
  47. Relays map[uint32]*HostInfo // Maps a Relay IDX to a Relay HostInfo object
  48. RemoteIndexes map[uint32]*HostInfo
  49. Hosts map[iputil.VpnIp]*HostInfo
  50. preferredRanges []*net.IPNet
  51. vpnCIDR *net.IPNet
  52. metricsEnabled bool
  53. l *logrus.Logger
  54. }
  55. // For synchronization, treat the pointed-to Relay struct as immutable. To edit the Relay
  56. // struct, make a copy of an existing value, edit the fileds in the copy, and
  57. // then store a pointer to the new copy in both realyForBy* maps.
  58. type RelayState struct {
  59. sync.RWMutex
  60. relays map[iputil.VpnIp]struct{} // Set of VpnIp's of Hosts to use as relays to access this peer
  61. relayForByIp map[iputil.VpnIp]*Relay // Maps VpnIps of peers for which this HostInfo is a relay to some Relay info
  62. relayForByIdx map[uint32]*Relay // Maps a local index to some Relay info
  63. }
  64. func (rs *RelayState) DeleteRelay(ip iputil.VpnIp) {
  65. rs.Lock()
  66. defer rs.Unlock()
  67. delete(rs.relays, ip)
  68. }
  69. func (rs *RelayState) GetRelayForByIp(ip iputil.VpnIp) (*Relay, bool) {
  70. rs.RLock()
  71. defer rs.RUnlock()
  72. r, ok := rs.relayForByIp[ip]
  73. return r, ok
  74. }
  75. func (rs *RelayState) InsertRelayTo(ip iputil.VpnIp) {
  76. rs.Lock()
  77. defer rs.Unlock()
  78. rs.relays[ip] = struct{}{}
  79. }
  80. func (rs *RelayState) CopyRelayIps() []iputil.VpnIp {
  81. rs.RLock()
  82. defer rs.RUnlock()
  83. ret := make([]iputil.VpnIp, 0, len(rs.relays))
  84. for ip := range rs.relays {
  85. ret = append(ret, ip)
  86. }
  87. return ret
  88. }
  89. func (rs *RelayState) CopyRelayForIps() []iputil.VpnIp {
  90. rs.RLock()
  91. defer rs.RUnlock()
  92. currentRelays := make([]iputil.VpnIp, 0, len(rs.relayForByIp))
  93. for relayIp := range rs.relayForByIp {
  94. currentRelays = append(currentRelays, relayIp)
  95. }
  96. return currentRelays
  97. }
  98. func (rs *RelayState) CopyRelayForIdxs() []uint32 {
  99. rs.RLock()
  100. defer rs.RUnlock()
  101. ret := make([]uint32, 0, len(rs.relayForByIdx))
  102. for i := range rs.relayForByIdx {
  103. ret = append(ret, i)
  104. }
  105. return ret
  106. }
  107. func (rs *RelayState) RemoveRelay(localIdx uint32) (iputil.VpnIp, bool) {
  108. rs.Lock()
  109. defer rs.Unlock()
  110. r, ok := rs.relayForByIdx[localIdx]
  111. if !ok {
  112. return iputil.VpnIp(0), false
  113. }
  114. delete(rs.relayForByIdx, localIdx)
  115. delete(rs.relayForByIp, r.PeerIp)
  116. return r.PeerIp, true
  117. }
  118. func (rs *RelayState) CompleteRelayByIP(vpnIp iputil.VpnIp, remoteIdx uint32) bool {
  119. rs.Lock()
  120. defer rs.Unlock()
  121. r, ok := rs.relayForByIp[vpnIp]
  122. if !ok {
  123. return false
  124. }
  125. newRelay := *r
  126. newRelay.State = Established
  127. newRelay.RemoteIndex = remoteIdx
  128. rs.relayForByIdx[r.LocalIndex] = &newRelay
  129. rs.relayForByIp[r.PeerIp] = &newRelay
  130. return true
  131. }
  132. func (rs *RelayState) CompleteRelayByIdx(localIdx uint32, remoteIdx uint32) (*Relay, bool) {
  133. rs.Lock()
  134. defer rs.Unlock()
  135. r, ok := rs.relayForByIdx[localIdx]
  136. if !ok {
  137. return nil, false
  138. }
  139. newRelay := *r
  140. newRelay.State = Established
  141. newRelay.RemoteIndex = remoteIdx
  142. rs.relayForByIdx[r.LocalIndex] = &newRelay
  143. rs.relayForByIp[r.PeerIp] = &newRelay
  144. return &newRelay, true
  145. }
  146. func (rs *RelayState) QueryRelayForByIp(vpnIp iputil.VpnIp) (*Relay, bool) {
  147. rs.RLock()
  148. defer rs.RUnlock()
  149. r, ok := rs.relayForByIp[vpnIp]
  150. return r, ok
  151. }
  152. func (rs *RelayState) QueryRelayForByIdx(idx uint32) (*Relay, bool) {
  153. rs.RLock()
  154. defer rs.RUnlock()
  155. r, ok := rs.relayForByIdx[idx]
  156. return r, ok
  157. }
  158. func (rs *RelayState) InsertRelay(ip iputil.VpnIp, idx uint32, r *Relay) {
  159. rs.Lock()
  160. defer rs.Unlock()
  161. rs.relayForByIp[ip] = r
  162. rs.relayForByIdx[idx] = r
  163. }
  164. type HostInfo struct {
  165. sync.RWMutex
  166. remote *udp.Addr
  167. remotes *RemoteList
  168. promoteCounter atomic.Uint32
  169. multiportTx bool
  170. multiportRx bool
  171. ConnectionState *ConnectionState
  172. handshakeStart time.Time //todo: this an entry in the handshake manager
  173. HandshakeReady bool //todo: being in the manager means you are ready
  174. HandshakeCounter int //todo: another handshake manager entry
  175. HandshakeLastRemotes []*udp.Addr //todo: another handshake manager entry, which remotes we sent to last time
  176. HandshakeComplete bool //todo: this should go away in favor of ConnectionState.ready
  177. HandshakePacket map[uint8][]byte //todo: this is other handshake manager entry
  178. packetStore []*cachedPacket //todo: this is other handshake manager entry
  179. remoteIndexId uint32
  180. localIndexId uint32
  181. vpnIp iputil.VpnIp
  182. recvError int
  183. remoteCidr *cidr.Tree4
  184. relayState RelayState
  185. // lastRebindCount is the other side of Interface.rebindCount, if these values don't match then we need to ask LH
  186. // for a punch from the remote end of this tunnel. The goal being to prime their conntrack for our traffic just like
  187. // with a handshake
  188. lastRebindCount int8
  189. // lastHandshakeTime records the time the remote side told us about at the stage when the handshake was completed locally
  190. // Stage 1 packet will contain it if I am a responder, stage 2 packet if I am an initiator
  191. // This is used to avoid an attack where a handshake packet is replayed after some time
  192. lastHandshakeTime uint64
  193. lastRoam time.Time
  194. lastRoamRemote *udp.Addr
  195. // Used to track other hostinfos for this vpn ip since only 1 can be primary
  196. // Synchronised via hostmap lock and not the hostinfo lock.
  197. next, prev *HostInfo
  198. }
  199. type ViaSender struct {
  200. relayHI *HostInfo // relayHI is the host info object of the relay
  201. remoteIdx uint32 // remoteIdx is the index included in the header of the received packet
  202. relay *Relay // relay contains the rest of the relay information, including the PeerIP of the host trying to communicate with us.
  203. }
  204. type cachedPacket struct {
  205. messageType header.MessageType
  206. messageSubType header.MessageSubType
  207. callback packetCallback
  208. packet []byte
  209. }
  210. type packetCallback func(t header.MessageType, st header.MessageSubType, h *HostInfo, p, nb, out []byte)
  211. type cachedPacketMetrics struct {
  212. sent metrics.Counter
  213. dropped metrics.Counter
  214. }
  215. func NewHostMap(l *logrus.Logger, name string, vpnCIDR *net.IPNet, preferredRanges []*net.IPNet) *HostMap {
  216. h := map[iputil.VpnIp]*HostInfo{}
  217. i := map[uint32]*HostInfo{}
  218. r := map[uint32]*HostInfo{}
  219. relays := map[uint32]*HostInfo{}
  220. m := HostMap{
  221. name: name,
  222. Indexes: i,
  223. Relays: relays,
  224. RemoteIndexes: r,
  225. Hosts: h,
  226. preferredRanges: preferredRanges,
  227. vpnCIDR: vpnCIDR,
  228. l: l,
  229. }
  230. return &m
  231. }
  232. // UpdateStats takes a name and reports host and index counts to the stats collection system
  233. func (hm *HostMap) EmitStats(name string) {
  234. hm.RLock()
  235. hostLen := len(hm.Hosts)
  236. indexLen := len(hm.Indexes)
  237. remoteIndexLen := len(hm.RemoteIndexes)
  238. relaysLen := len(hm.Relays)
  239. hm.RUnlock()
  240. metrics.GetOrRegisterGauge("hostmap."+name+".hosts", nil).Update(int64(hostLen))
  241. metrics.GetOrRegisterGauge("hostmap."+name+".indexes", nil).Update(int64(indexLen))
  242. metrics.GetOrRegisterGauge("hostmap."+name+".remoteIndexes", nil).Update(int64(remoteIndexLen))
  243. metrics.GetOrRegisterGauge("hostmap."+name+".relayIndexes", nil).Update(int64(relaysLen))
  244. }
  245. func (hm *HostMap) RemoveRelay(localIdx uint32) {
  246. hm.Lock()
  247. hiRelay, ok := hm.Relays[localIdx]
  248. if !ok {
  249. hm.Unlock()
  250. return
  251. }
  252. delete(hm.Relays, localIdx)
  253. hm.Unlock()
  254. ip, ok := hiRelay.relayState.RemoveRelay(localIdx)
  255. if !ok {
  256. return
  257. }
  258. hiPeer, err := hm.QueryVpnIp(ip)
  259. if err != nil {
  260. return
  261. }
  262. var otherPeerIdx uint32
  263. hiPeer.relayState.DeleteRelay(hiRelay.vpnIp)
  264. relay, ok := hiPeer.relayState.GetRelayForByIp(hiRelay.vpnIp)
  265. if ok {
  266. otherPeerIdx = relay.LocalIndex
  267. }
  268. // I am a relaying host. I need to remove the other relay, too.
  269. hm.RemoveRelay(otherPeerIdx)
  270. }
  271. func (hm *HostMap) GetIndexByVpnIp(vpnIp iputil.VpnIp) (uint32, error) {
  272. hm.RLock()
  273. if i, ok := hm.Hosts[vpnIp]; ok {
  274. index := i.localIndexId
  275. hm.RUnlock()
  276. return index, nil
  277. }
  278. hm.RUnlock()
  279. return 0, errors.New("vpn IP not found")
  280. }
  281. func (hm *HostMap) Add(ip iputil.VpnIp, hostinfo *HostInfo) {
  282. hm.Lock()
  283. hm.Hosts[ip] = hostinfo
  284. hm.Unlock()
  285. }
  286. func (hm *HostMap) AddVpnIp(vpnIp iputil.VpnIp, init func(hostinfo *HostInfo)) (hostinfo *HostInfo, created bool) {
  287. hm.RLock()
  288. if h, ok := hm.Hosts[vpnIp]; !ok {
  289. hm.RUnlock()
  290. h = &HostInfo{
  291. vpnIp: vpnIp,
  292. HandshakePacket: make(map[uint8][]byte, 0),
  293. relayState: RelayState{
  294. relays: map[iputil.VpnIp]struct{}{},
  295. relayForByIp: map[iputil.VpnIp]*Relay{},
  296. relayForByIdx: map[uint32]*Relay{},
  297. },
  298. }
  299. if init != nil {
  300. init(h)
  301. }
  302. hm.Lock()
  303. hm.Hosts[vpnIp] = h
  304. hm.Unlock()
  305. return h, true
  306. } else {
  307. hm.RUnlock()
  308. return h, false
  309. }
  310. }
  311. // Only used by pendingHostMap when the remote index is not initially known
  312. func (hm *HostMap) addRemoteIndexHostInfo(index uint32, h *HostInfo) {
  313. hm.Lock()
  314. h.remoteIndexId = index
  315. hm.RemoteIndexes[index] = h
  316. hm.Unlock()
  317. if hm.l.Level > logrus.DebugLevel {
  318. hm.l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes),
  319. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": h.vpnIp}}).
  320. Debug("Hostmap remoteIndex added")
  321. }
  322. }
  323. // DeleteReverseIndex is used to clean up on recv_error
  324. // This function should only ever be called on the pending hostmap
  325. func (hm *HostMap) DeleteReverseIndex(index uint32) {
  326. hm.Lock()
  327. hostinfo, ok := hm.RemoteIndexes[index]
  328. if ok {
  329. delete(hm.Indexes, hostinfo.localIndexId)
  330. delete(hm.RemoteIndexes, index)
  331. // Check if we have an entry under hostId that matches the same hostinfo
  332. // instance. Clean it up as well if we do (they might not match in pendingHostmap)
  333. var hostinfo2 *HostInfo
  334. hostinfo2, ok = hm.Hosts[hostinfo.vpnIp]
  335. if ok && hostinfo2 == hostinfo {
  336. delete(hm.Hosts, hostinfo.vpnIp)
  337. }
  338. }
  339. hm.Unlock()
  340. if hm.l.Level >= logrus.DebugLevel {
  341. hm.l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes)}).
  342. Debug("Hostmap remote index deleted")
  343. }
  344. }
  345. // DeleteHostInfo will fully unlink the hostinfo and return true if it was the final hostinfo for this vpn ip
  346. func (hm *HostMap) DeleteHostInfo(hostinfo *HostInfo) bool {
  347. // Delete the host itself, ensuring it's not modified anymore
  348. hm.Lock()
  349. // If we have a previous or next hostinfo then we are not the last one for this vpn ip
  350. final := (hostinfo.next == nil && hostinfo.prev == nil)
  351. hm.unlockedDeleteHostInfo(hostinfo)
  352. hm.Unlock()
  353. // And tear down all the relays going through this host, if final
  354. for _, localIdx := range hostinfo.relayState.CopyRelayForIdxs() {
  355. hm.RemoveRelay(localIdx)
  356. }
  357. if final {
  358. // And tear down the relays this deleted hostInfo was using to be reached
  359. teardownRelayIdx := []uint32{}
  360. for _, relayIp := range hostinfo.relayState.CopyRelayIps() {
  361. relayHostInfo, err := hm.QueryVpnIp(relayIp)
  362. if err != nil {
  363. hm.l.WithError(err).WithField("relay", relayIp).Info("Missing relay host in hostmap")
  364. } else {
  365. if r, ok := relayHostInfo.relayState.QueryRelayForByIp(hostinfo.vpnIp); ok {
  366. teardownRelayIdx = append(teardownRelayIdx, r.LocalIndex)
  367. }
  368. }
  369. }
  370. for _, localIdx := range teardownRelayIdx {
  371. hm.RemoveRelay(localIdx)
  372. }
  373. }
  374. return final
  375. }
  376. func (hm *HostMap) DeleteRelayIdx(localIdx uint32) {
  377. hm.Lock()
  378. defer hm.Unlock()
  379. delete(hm.RemoteIndexes, localIdx)
  380. }
  381. func (hm *HostMap) MakePrimary(hostinfo *HostInfo) {
  382. hm.Lock()
  383. defer hm.Unlock()
  384. hm.unlockedMakePrimary(hostinfo)
  385. }
  386. func (hm *HostMap) unlockedMakePrimary(hostinfo *HostInfo) {
  387. oldHostinfo := hm.Hosts[hostinfo.vpnIp]
  388. if oldHostinfo == hostinfo {
  389. return
  390. }
  391. if hostinfo.prev != nil {
  392. hostinfo.prev.next = hostinfo.next
  393. }
  394. if hostinfo.next != nil {
  395. hostinfo.next.prev = hostinfo.prev
  396. }
  397. hm.Hosts[hostinfo.vpnIp] = hostinfo
  398. if oldHostinfo == nil {
  399. return
  400. }
  401. hostinfo.next = oldHostinfo
  402. oldHostinfo.prev = hostinfo
  403. hostinfo.prev = nil
  404. }
  405. func (hm *HostMap) unlockedDeleteHostInfo(hostinfo *HostInfo) {
  406. primary, ok := hm.Hosts[hostinfo.vpnIp]
  407. if ok && primary == hostinfo {
  408. // The vpnIp pointer points to the same hostinfo as the local index id, we can remove it
  409. delete(hm.Hosts, hostinfo.vpnIp)
  410. if len(hm.Hosts) == 0 {
  411. hm.Hosts = map[iputil.VpnIp]*HostInfo{}
  412. }
  413. if hostinfo.next != nil {
  414. // We had more than 1 hostinfo at this vpnip, promote the next in the list to primary
  415. hm.Hosts[hostinfo.vpnIp] = hostinfo.next
  416. // It is primary, there is no previous hostinfo now
  417. hostinfo.next.prev = nil
  418. }
  419. } else {
  420. // Relink if we were in the middle of multiple hostinfos for this vpn ip
  421. if hostinfo.prev != nil {
  422. hostinfo.prev.next = hostinfo.next
  423. }
  424. if hostinfo.next != nil {
  425. hostinfo.next.prev = hostinfo.prev
  426. }
  427. }
  428. hostinfo.next = nil
  429. hostinfo.prev = nil
  430. // The remote index uses index ids outside our control so lets make sure we are only removing
  431. // the remote index pointer here if it points to the hostinfo we are deleting
  432. hostinfo2, ok := hm.RemoteIndexes[hostinfo.remoteIndexId]
  433. if ok && hostinfo2 == hostinfo {
  434. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  435. if len(hm.RemoteIndexes) == 0 {
  436. hm.RemoteIndexes = map[uint32]*HostInfo{}
  437. }
  438. }
  439. delete(hm.Indexes, hostinfo.localIndexId)
  440. if len(hm.Indexes) == 0 {
  441. hm.Indexes = map[uint32]*HostInfo{}
  442. }
  443. if hm.l.Level >= logrus.DebugLevel {
  444. hm.l.WithField("hostMap", m{"mapName": hm.name, "mapTotalSize": len(hm.Hosts),
  445. "vpnIp": hostinfo.vpnIp, "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId}).
  446. Debug("Hostmap hostInfo deleted")
  447. }
  448. }
  449. func (hm *HostMap) QueryIndex(index uint32) (*HostInfo, error) {
  450. //TODO: we probably just want to return bool instead of error, or at least a static error
  451. hm.RLock()
  452. if h, ok := hm.Indexes[index]; ok {
  453. hm.RUnlock()
  454. return h, nil
  455. } else {
  456. hm.RUnlock()
  457. return nil, errors.New("unable to find index")
  458. }
  459. }
  460. // Retrieves a HostInfo by Index. Returns whether the HostInfo is primary at time of query.
  461. // This helper exists so that the hostinfo.prev pointer can be read while the hostmap lock is held.
  462. func (hm *HostMap) QueryIndexIsPrimary(index uint32) (*HostInfo, bool, error) {
  463. //TODO: we probably just want to return bool instead of error, or at least a static error
  464. hm.RLock()
  465. if h, ok := hm.Indexes[index]; ok {
  466. hm.RUnlock()
  467. return h, h.prev == nil, nil
  468. } else {
  469. hm.RUnlock()
  470. return nil, false, errors.New("unable to find index")
  471. }
  472. }
  473. func (hm *HostMap) QueryRelayIndex(index uint32) (*HostInfo, error) {
  474. //TODO: we probably just want to return bool instead of error, or at least a static error
  475. hm.RLock()
  476. if h, ok := hm.Relays[index]; ok {
  477. hm.RUnlock()
  478. return h, nil
  479. } else {
  480. hm.RUnlock()
  481. return nil, errors.New("unable to find index")
  482. }
  483. }
  484. func (hm *HostMap) QueryReverseIndex(index uint32) (*HostInfo, error) {
  485. hm.RLock()
  486. if h, ok := hm.RemoteIndexes[index]; ok {
  487. hm.RUnlock()
  488. return h, nil
  489. } else {
  490. hm.RUnlock()
  491. return nil, fmt.Errorf("unable to find reverse index or connectionstate nil in %s hostmap", hm.name)
  492. }
  493. }
  494. func (hm *HostMap) QueryVpnIp(vpnIp iputil.VpnIp) (*HostInfo, error) {
  495. return hm.queryVpnIp(vpnIp, nil)
  496. }
  497. // PromoteBestQueryVpnIp will attempt to lazily switch to the best remote every
  498. // `PromoteEvery` calls to this function for a given host.
  499. func (hm *HostMap) PromoteBestQueryVpnIp(vpnIp iputil.VpnIp, ifce *Interface) (*HostInfo, error) {
  500. return hm.queryVpnIp(vpnIp, ifce)
  501. }
  502. func (hm *HostMap) queryVpnIp(vpnIp iputil.VpnIp, promoteIfce *Interface) (*HostInfo, error) {
  503. hm.RLock()
  504. if h, ok := hm.Hosts[vpnIp]; ok {
  505. hm.RUnlock()
  506. // Do not attempt promotion if you are a lighthouse
  507. if promoteIfce != nil && !promoteIfce.lightHouse.amLighthouse {
  508. h.TryPromoteBest(hm.preferredRanges, promoteIfce)
  509. }
  510. return h, nil
  511. }
  512. hm.RUnlock()
  513. return nil, errors.New("unable to find host")
  514. }
  515. // unlockedAddHostInfo assumes you have a write-lock and will add a hostinfo object to the hostmap Indexes and RemoteIndexes maps.
  516. // If an entry exists for the Hosts table (vpnIp -> hostinfo) then the provided hostinfo will be made primary
  517. func (hm *HostMap) unlockedAddHostInfo(hostinfo *HostInfo, f *Interface) {
  518. if f.serveDns {
  519. remoteCert := hostinfo.ConnectionState.peerCert
  520. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  521. }
  522. existing := hm.Hosts[hostinfo.vpnIp]
  523. hm.Hosts[hostinfo.vpnIp] = hostinfo
  524. if existing != nil {
  525. hostinfo.next = existing
  526. existing.prev = hostinfo
  527. }
  528. hm.Indexes[hostinfo.localIndexId] = hostinfo
  529. hm.RemoteIndexes[hostinfo.remoteIndexId] = hostinfo
  530. if hm.l.Level >= logrus.DebugLevel {
  531. hm.l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": hostinfo.vpnIp, "mapTotalSize": len(hm.Hosts),
  532. "hostinfo": m{"existing": true, "localIndexId": hostinfo.localIndexId, "hostId": hostinfo.vpnIp}}).
  533. Debug("Hostmap vpnIp added")
  534. }
  535. i := 1
  536. check := hostinfo
  537. for check != nil {
  538. if i > MaxHostInfosPerVpnIp {
  539. hm.unlockedDeleteHostInfo(check)
  540. }
  541. check = check.next
  542. i++
  543. }
  544. }
  545. // TryPromoteBest handles re-querying lighthouses and probing for better paths
  546. // NOTE: It is an error to call this if you are a lighthouse since they should not roam clients!
  547. func (i *HostInfo) TryPromoteBest(preferredRanges []*net.IPNet, ifce *Interface) {
  548. c := i.promoteCounter.Add(1)
  549. if c%PromoteEvery == 0 {
  550. // The lock here is currently protecting i.remote access
  551. i.RLock()
  552. remote := i.remote
  553. i.RUnlock()
  554. // return early if we are already on a preferred remote
  555. if remote != nil {
  556. rIP := remote.IP
  557. for _, l := range preferredRanges {
  558. if l.Contains(rIP) {
  559. return
  560. }
  561. }
  562. }
  563. i.remotes.ForEach(preferredRanges, func(addr *udp.Addr, preferred bool) {
  564. if remote != nil && (addr == nil || !preferred) {
  565. return
  566. }
  567. // Try to send a test packet to that host, this should
  568. // cause it to detect a roaming event and switch remotes
  569. ifce.sendTo(header.Test, header.TestRequest, i.ConnectionState, i, addr, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  570. })
  571. }
  572. // Re query our lighthouses for new remotes occasionally
  573. if c%ReQueryEvery == 0 && ifce.lightHouse != nil {
  574. ifce.lightHouse.QueryServer(i.vpnIp, ifce)
  575. }
  576. }
  577. func (i *HostInfo) cachePacket(l *logrus.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
  578. //TODO: return the error so we can log with more context
  579. if len(i.packetStore) < 100 {
  580. tempPacket := make([]byte, len(packet))
  581. copy(tempPacket, packet)
  582. //l.WithField("trace", string(debug.Stack())).Error("Caching packet", tempPacket)
  583. i.packetStore = append(i.packetStore, &cachedPacket{t, st, f, tempPacket})
  584. if l.Level >= logrus.DebugLevel {
  585. i.logger(l).
  586. WithField("length", len(i.packetStore)).
  587. WithField("stored", true).
  588. Debugf("Packet store")
  589. }
  590. } else if l.Level >= logrus.DebugLevel {
  591. m.dropped.Inc(1)
  592. i.logger(l).
  593. WithField("length", len(i.packetStore)).
  594. WithField("stored", false).
  595. Debugf("Packet store")
  596. }
  597. }
  598. // handshakeComplete will set the connection as ready to communicate, as well as flush any stored packets
  599. func (i *HostInfo) handshakeComplete(l *logrus.Logger, m *cachedPacketMetrics) {
  600. //TODO: I'm not certain the distinction between handshake complete and ConnectionState being ready matters because:
  601. //TODO: HandshakeComplete means send stored packets and ConnectionState.ready means we are ready to send
  602. //TODO: if the transition from HandhsakeComplete to ConnectionState.ready happens all within this function they are identical
  603. i.ConnectionState.queueLock.Lock()
  604. i.HandshakeComplete = true
  605. //TODO: this should be managed by the handshake state machine to set it based on how many handshake were seen.
  606. // Clamping it to 2 gets us out of the woods for now
  607. i.ConnectionState.messageCounter.Store(2)
  608. if l.Level >= logrus.DebugLevel {
  609. i.logger(l).Debugf("Sending %d stored packets", len(i.packetStore))
  610. }
  611. if len(i.packetStore) > 0 {
  612. nb := make([]byte, 12, 12)
  613. out := make([]byte, mtu)
  614. for _, cp := range i.packetStore {
  615. cp.callback(cp.messageType, cp.messageSubType, i, cp.packet, nb, out)
  616. }
  617. m.sent.Inc(int64(len(i.packetStore)))
  618. }
  619. i.remotes.ResetBlockedRemotes()
  620. i.packetStore = make([]*cachedPacket, 0)
  621. i.ConnectionState.ready = true
  622. i.ConnectionState.queueLock.Unlock()
  623. i.ConnectionState.certState = nil
  624. }
  625. func (i *HostInfo) GetCert() *cert.NebulaCertificate {
  626. if i.ConnectionState != nil {
  627. return i.ConnectionState.peerCert
  628. }
  629. return nil
  630. }
  631. func (i *HostInfo) SetRemote(remote *udp.Addr) {
  632. // We copy here because we likely got this remote from a source that reuses the object
  633. if !i.remote.Equals(remote) {
  634. i.remote = remote.Copy()
  635. i.remotes.LearnRemote(i.vpnIp, remote.Copy())
  636. }
  637. }
  638. // SetRemoteIfPreferred returns true if the remote was changed. The lastRoam
  639. // time on the HostInfo will also be updated.
  640. func (i *HostInfo) SetRemoteIfPreferred(hm *HostMap, newRemote *udp.Addr) bool {
  641. if newRemote == nil {
  642. // relays have nil udp Addrs
  643. return false
  644. }
  645. currentRemote := i.remote
  646. if currentRemote == nil {
  647. i.SetRemote(newRemote)
  648. return true
  649. }
  650. // NOTE: We do this loop here instead of calling `isPreferred` in
  651. // remote_list.go so that we only have to loop over preferredRanges once.
  652. newIsPreferred := false
  653. for _, l := range hm.preferredRanges {
  654. // return early if we are already on a preferred remote
  655. if l.Contains(currentRemote.IP) {
  656. return false
  657. }
  658. if l.Contains(newRemote.IP) {
  659. newIsPreferred = true
  660. }
  661. }
  662. if newIsPreferred {
  663. // Consider this a roaming event
  664. i.lastRoam = time.Now()
  665. i.lastRoamRemote = currentRemote.Copy()
  666. i.SetRemote(newRemote)
  667. return true
  668. }
  669. return false
  670. }
  671. func (i *HostInfo) RecvErrorExceeded() bool {
  672. if i.recvError < 3 {
  673. i.recvError += 1
  674. return false
  675. }
  676. return true
  677. }
  678. func (i *HostInfo) CreateRemoteCIDR(c *cert.NebulaCertificate) {
  679. if len(c.Details.Ips) == 1 && len(c.Details.Subnets) == 0 {
  680. // Simple case, no CIDRTree needed
  681. return
  682. }
  683. remoteCidr := cidr.NewTree4()
  684. for _, ip := range c.Details.Ips {
  685. remoteCidr.AddCIDR(&net.IPNet{IP: ip.IP, Mask: net.IPMask{255, 255, 255, 255}}, struct{}{})
  686. }
  687. for _, n := range c.Details.Subnets {
  688. remoteCidr.AddCIDR(n, struct{}{})
  689. }
  690. i.remoteCidr = remoteCidr
  691. }
  692. func (i *HostInfo) logger(l *logrus.Logger) *logrus.Entry {
  693. if i == nil {
  694. return logrus.NewEntry(l)
  695. }
  696. li := l.WithField("vpnIp", i.vpnIp).
  697. WithField("localIndex", i.localIndexId).
  698. WithField("remoteIndex", i.remoteIndexId)
  699. if connState := i.ConnectionState; connState != nil {
  700. if peerCert := connState.peerCert; peerCert != nil {
  701. li = li.WithField("certName", peerCert.Details.Name)
  702. }
  703. }
  704. return li
  705. }
  706. // Utility functions
  707. func localIps(l *logrus.Logger, allowList *LocalAllowList) *[]net.IP {
  708. //FIXME: This function is pretty garbage
  709. var ips []net.IP
  710. ifaces, _ := net.Interfaces()
  711. for _, i := range ifaces {
  712. allow := allowList.AllowName(i.Name)
  713. if l.Level >= logrus.TraceLevel {
  714. l.WithField("interfaceName", i.Name).WithField("allow", allow).Trace("localAllowList.AllowName")
  715. }
  716. if !allow {
  717. continue
  718. }
  719. addrs, _ := i.Addrs()
  720. for _, addr := range addrs {
  721. var ip net.IP
  722. switch v := addr.(type) {
  723. case *net.IPNet:
  724. //continue
  725. ip = v.IP
  726. case *net.IPAddr:
  727. ip = v.IP
  728. }
  729. //TODO: Filtering out link local for now, this is probably the most correct thing
  730. //TODO: Would be nice to filter out SLAAC MAC based ips as well
  731. if ip.IsLoopback() == false && !ip.IsLinkLocalUnicast() {
  732. allow := allowList.Allow(ip)
  733. if l.Level >= logrus.TraceLevel {
  734. l.WithField("localIp", ip).WithField("allow", allow).Trace("localAllowList.Allow")
  735. }
  736. if !allow {
  737. continue
  738. }
  739. ips = append(ips, ip)
  740. }
  741. }
  742. }
  743. return &ips
  744. }