hostmap.go 24 KB

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