hostmap.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. package nebula
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "sync"
  8. "time"
  9. "github.com/rcrowley/go-metrics"
  10. "github.com/sirupsen/logrus"
  11. "github.com/slackhq/nebula/cert"
  12. )
  13. //const ProbeLen = 100
  14. const PromoteEvery = 1000
  15. const MaxRemotes = 10
  16. // How long we should prevent roaming back to the previous IP.
  17. // This helps prevent flapping due to packets already in flight
  18. const RoamingSuppressSeconds = 2
  19. type HostMap struct {
  20. sync.RWMutex //Because we concurrently read and write to our maps
  21. name string
  22. Indexes map[uint32]*HostInfo
  23. RemoteIndexes map[uint32]*HostInfo
  24. Hosts map[uint32]*HostInfo
  25. preferredRanges []*net.IPNet
  26. vpnCIDR *net.IPNet
  27. defaultRoute uint32
  28. unsafeRoutes *CIDRTree
  29. metricsEnabled bool
  30. }
  31. type HostInfo struct {
  32. remote *udpAddr
  33. Remotes []*HostInfoDest
  34. promoteCounter uint32
  35. ConnectionState *ConnectionState
  36. handshakeStart time.Time
  37. HandshakeReady bool
  38. HandshakeCounter int
  39. HandshakeComplete bool
  40. HandshakePacket map[uint8][]byte
  41. packetStore []*cachedPacket
  42. remoteIndexId uint32
  43. localIndexId uint32
  44. hostId uint32
  45. recvError int
  46. remoteCidr *CIDRTree
  47. lastRoam time.Time
  48. lastRoamRemote *udpAddr
  49. }
  50. type cachedPacket struct {
  51. messageType NebulaMessageType
  52. messageSubType NebulaMessageSubType
  53. callback packetCallback
  54. packet []byte
  55. }
  56. type packetCallback func(t NebulaMessageType, st NebulaMessageSubType, h *HostInfo, p, nb, out []byte)
  57. type HostInfoDest struct {
  58. addr *udpAddr
  59. //probes [ProbeLen]bool
  60. probeCounter int
  61. }
  62. type Probe struct {
  63. Addr *net.UDPAddr
  64. Counter int
  65. }
  66. func NewHostMap(name string, vpnCIDR *net.IPNet, preferredRanges []*net.IPNet) *HostMap {
  67. h := map[uint32]*HostInfo{}
  68. i := map[uint32]*HostInfo{}
  69. r := map[uint32]*HostInfo{}
  70. m := HostMap{
  71. name: name,
  72. Indexes: i,
  73. RemoteIndexes: r,
  74. Hosts: h,
  75. preferredRanges: preferredRanges,
  76. vpnCIDR: vpnCIDR,
  77. defaultRoute: 0,
  78. unsafeRoutes: NewCIDRTree(),
  79. }
  80. return &m
  81. }
  82. // UpdateStats takes a name and reports host and index counts to the stats collection system
  83. func (hm *HostMap) EmitStats(name string) {
  84. hm.RLock()
  85. hostLen := len(hm.Hosts)
  86. indexLen := len(hm.Indexes)
  87. remoteIndexLen := len(hm.RemoteIndexes)
  88. hm.RUnlock()
  89. metrics.GetOrRegisterGauge("hostmap."+name+".hosts", nil).Update(int64(hostLen))
  90. metrics.GetOrRegisterGauge("hostmap."+name+".indexes", nil).Update(int64(indexLen))
  91. metrics.GetOrRegisterGauge("hostmap."+name+".remoteIndexes", nil).Update(int64(remoteIndexLen))
  92. }
  93. func (hm *HostMap) GetIndexByVpnIP(vpnIP uint32) (uint32, error) {
  94. hm.RLock()
  95. if i, ok := hm.Hosts[vpnIP]; ok {
  96. index := i.localIndexId
  97. hm.RUnlock()
  98. return index, nil
  99. }
  100. hm.RUnlock()
  101. return 0, errors.New("vpn IP not found")
  102. }
  103. func (hm *HostMap) Add(ip uint32, hostinfo *HostInfo) {
  104. hm.Lock()
  105. hm.Hosts[ip] = hostinfo
  106. hm.Unlock()
  107. }
  108. func (hm *HostMap) AddVpnIP(vpnIP uint32) *HostInfo {
  109. h := &HostInfo{}
  110. hm.RLock()
  111. if _, ok := hm.Hosts[vpnIP]; !ok {
  112. hm.RUnlock()
  113. h = &HostInfo{
  114. Remotes: []*HostInfoDest{},
  115. promoteCounter: 0,
  116. hostId: vpnIP,
  117. HandshakePacket: make(map[uint8][]byte, 0),
  118. }
  119. hm.Lock()
  120. hm.Hosts[vpnIP] = h
  121. hm.Unlock()
  122. return h
  123. } else {
  124. h = hm.Hosts[vpnIP]
  125. hm.RUnlock()
  126. return h
  127. }
  128. }
  129. func (hm *HostMap) DeleteVpnIP(vpnIP uint32) {
  130. hm.Lock()
  131. delete(hm.Hosts, vpnIP)
  132. if len(hm.Hosts) == 0 {
  133. hm.Hosts = map[uint32]*HostInfo{}
  134. }
  135. hm.Unlock()
  136. if l.Level >= logrus.DebugLevel {
  137. l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(vpnIP), "mapTotalSize": len(hm.Hosts)}).
  138. Debug("Hostmap vpnIp deleted")
  139. }
  140. }
  141. func (hm *HostMap) AddIndex(index uint32, ci *ConnectionState) (*HostInfo, error) {
  142. hm.Lock()
  143. if _, ok := hm.Indexes[index]; !ok {
  144. h := &HostInfo{
  145. ConnectionState: ci,
  146. Remotes: []*HostInfoDest{},
  147. localIndexId: index,
  148. HandshakePacket: make(map[uint8][]byte, 0),
  149. }
  150. hm.Indexes[index] = h
  151. l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes),
  152. "hostinfo": m{"existing": false, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  153. Debug("Hostmap index added")
  154. hm.Unlock()
  155. return h, nil
  156. }
  157. hm.Unlock()
  158. return nil, fmt.Errorf("refusing to overwrite existing index: %d", index)
  159. }
  160. func (hm *HostMap) AddIndexHostInfo(index uint32, h *HostInfo) {
  161. hm.Lock()
  162. h.localIndexId = index
  163. hm.Indexes[index] = h
  164. hm.Unlock()
  165. if l.Level > logrus.DebugLevel {
  166. l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes),
  167. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  168. Debug("Hostmap index added")
  169. }
  170. }
  171. // Only used by pendingHostMap when the remote index is not initially known
  172. func (hm *HostMap) addRemoteIndexHostInfo(index uint32, h *HostInfo) {
  173. hm.Lock()
  174. h.remoteIndexId = index
  175. hm.RemoteIndexes[index] = h
  176. hm.Unlock()
  177. if l.Level > logrus.DebugLevel {
  178. l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes),
  179. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  180. Debug("Hostmap remoteIndex added")
  181. }
  182. }
  183. func (hm *HostMap) AddVpnIPHostInfo(vpnIP uint32, h *HostInfo) {
  184. hm.Lock()
  185. h.hostId = vpnIP
  186. hm.Hosts[vpnIP] = h
  187. hm.Indexes[h.localIndexId] = h
  188. hm.RemoteIndexes[h.remoteIndexId] = h
  189. hm.Unlock()
  190. if l.Level > logrus.DebugLevel {
  191. l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(vpnIP), "mapTotalSize": len(hm.Hosts),
  192. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  193. Debug("Hostmap vpnIp added")
  194. }
  195. }
  196. // This is only called in pendingHostmap, to cleanup an inbound handshake
  197. func (hm *HostMap) DeleteIndex(index uint32) {
  198. hm.Lock()
  199. hostinfo, ok := hm.Indexes[index]
  200. if ok {
  201. delete(hm.Indexes, index)
  202. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  203. // Check if we have an entry under hostId that matches the same hostinfo
  204. // instance. Clean it up as well if we do.
  205. var hostinfo2 *HostInfo
  206. hostinfo2, ok = hm.Hosts[hostinfo.hostId]
  207. if ok && hostinfo2 == hostinfo {
  208. delete(hm.Hosts, hostinfo.hostId)
  209. }
  210. }
  211. hm.Unlock()
  212. if l.Level >= logrus.DebugLevel {
  213. l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes)}).
  214. Debug("Hostmap index deleted")
  215. }
  216. }
  217. // This is used to cleanup on recv_error
  218. func (hm *HostMap) DeleteReverseIndex(index uint32) {
  219. hm.Lock()
  220. hostinfo, ok := hm.RemoteIndexes[index]
  221. if ok {
  222. delete(hm.Indexes, hostinfo.localIndexId)
  223. delete(hm.RemoteIndexes, index)
  224. // Check if we have an entry under hostId that matches the same hostinfo
  225. // instance. Clean it up as well if we do (they might not match in pendingHostmap)
  226. var hostinfo2 *HostInfo
  227. hostinfo2, ok = hm.Hosts[hostinfo.hostId]
  228. if ok && hostinfo2 == hostinfo {
  229. delete(hm.Hosts, hostinfo.hostId)
  230. }
  231. }
  232. hm.Unlock()
  233. if l.Level >= logrus.DebugLevel {
  234. l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes)}).
  235. Debug("Hostmap remote index deleted")
  236. }
  237. }
  238. func (hm *HostMap) DeleteHostInfo(hostinfo *HostInfo) {
  239. hm.Lock()
  240. // Check if this same hostId is in the hostmap with a different instance.
  241. // This could happen if we have an entry in the pending hostmap with different
  242. // index values than the one in the main hostmap.
  243. hostinfo2, ok := hm.Hosts[hostinfo.hostId]
  244. if ok && hostinfo2 != hostinfo {
  245. delete(hm.Hosts, hostinfo2.hostId)
  246. delete(hm.Indexes, hostinfo2.localIndexId)
  247. delete(hm.RemoteIndexes, hostinfo2.remoteIndexId)
  248. }
  249. delete(hm.Hosts, hostinfo.hostId)
  250. if len(hm.Hosts) == 0 {
  251. hm.Hosts = map[uint32]*HostInfo{}
  252. }
  253. delete(hm.Indexes, hostinfo.localIndexId)
  254. if len(hm.Indexes) == 0 {
  255. hm.Indexes = map[uint32]*HostInfo{}
  256. }
  257. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  258. if len(hm.RemoteIndexes) == 0 {
  259. hm.RemoteIndexes = map[uint32]*HostInfo{}
  260. }
  261. hm.Unlock()
  262. if l.Level >= logrus.DebugLevel {
  263. l.WithField("hostMap", m{"mapName": hm.name, "mapTotalSize": len(hm.Hosts),
  264. "vpnIp": IntIp(hostinfo.hostId), "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId}).
  265. Debug("Hostmap hostInfo deleted")
  266. }
  267. }
  268. func (hm *HostMap) QueryIndex(index uint32) (*HostInfo, error) {
  269. //TODO: we probably just want ot return bool instead of error, or at least a static error
  270. hm.RLock()
  271. if h, ok := hm.Indexes[index]; ok {
  272. hm.RUnlock()
  273. return h, nil
  274. } else {
  275. hm.RUnlock()
  276. return nil, errors.New("unable to find index")
  277. }
  278. }
  279. func (hm *HostMap) QueryReverseIndex(index uint32) (*HostInfo, error) {
  280. hm.RLock()
  281. if h, ok := hm.RemoteIndexes[index]; ok {
  282. hm.RUnlock()
  283. return h, nil
  284. } else {
  285. hm.RUnlock()
  286. return nil, fmt.Errorf("unable to find reverse index or connectionstate nil in %s hostmap", hm.name)
  287. }
  288. }
  289. func (hm *HostMap) AddRemote(vpnIp uint32, remote *udpAddr) *HostInfo {
  290. hm.Lock()
  291. i, v := hm.Hosts[vpnIp]
  292. if v {
  293. i.AddRemote(*remote)
  294. } else {
  295. i = &HostInfo{
  296. Remotes: []*HostInfoDest{NewHostInfoDest(remote)},
  297. promoteCounter: 0,
  298. hostId: vpnIp,
  299. HandshakePacket: make(map[uint8][]byte, 0),
  300. }
  301. i.remote = i.Remotes[0].addr
  302. hm.Hosts[vpnIp] = i
  303. l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(vpnIp), "udpAddr": remote, "mapTotalSize": len(hm.Hosts)}).
  304. Debug("Hostmap remote ip added")
  305. }
  306. i.ForcePromoteBest(hm.preferredRanges)
  307. hm.Unlock()
  308. return i
  309. }
  310. func (hm *HostMap) QueryVpnIP(vpnIp uint32) (*HostInfo, error) {
  311. return hm.queryVpnIP(vpnIp, nil)
  312. }
  313. // PromoteBestQueryVpnIP will attempt to lazily switch to the best remote every
  314. // `PromoteEvery` calls to this function for a given host.
  315. func (hm *HostMap) PromoteBestQueryVpnIP(vpnIp uint32, ifce *Interface) (*HostInfo, error) {
  316. return hm.queryVpnIP(vpnIp, ifce)
  317. }
  318. func (hm *HostMap) queryVpnIP(vpnIp uint32, promoteIfce *Interface) (*HostInfo, error) {
  319. hm.RLock()
  320. if h, ok := hm.Hosts[vpnIp]; ok {
  321. if promoteIfce != nil {
  322. h.TryPromoteBest(hm.preferredRanges, promoteIfce)
  323. }
  324. //fmt.Println(h.remote)
  325. hm.RUnlock()
  326. return h, nil
  327. } else {
  328. //return &net.UDPAddr{}, nil, errors.New("Unable to find host")
  329. hm.RUnlock()
  330. /*
  331. if lightHouse != nil {
  332. lightHouse.Query(vpnIp)
  333. return nil, errors.New("Unable to find host")
  334. }
  335. */
  336. return nil, errors.New("unable to find host")
  337. }
  338. }
  339. func (hm *HostMap) queryUnsafeRoute(ip uint32) uint32 {
  340. r := hm.unsafeRoutes.MostSpecificContains(ip)
  341. if r != nil {
  342. return r.(uint32)
  343. } else {
  344. return 0
  345. }
  346. }
  347. func (hm *HostMap) CheckHandshakeCompleteIP(vpnIP uint32) bool {
  348. hm.RLock()
  349. if i, ok := hm.Hosts[vpnIP]; ok {
  350. if i == nil {
  351. hm.RUnlock()
  352. return false
  353. }
  354. complete := i.HandshakeComplete
  355. hm.RUnlock()
  356. return complete
  357. }
  358. hm.RUnlock()
  359. return false
  360. }
  361. func (hm *HostMap) CheckHandshakeCompleteIndex(index uint32) bool {
  362. hm.RLock()
  363. if i, ok := hm.Indexes[index]; ok {
  364. if i == nil {
  365. hm.RUnlock()
  366. return false
  367. }
  368. complete := i.HandshakeComplete
  369. hm.RUnlock()
  370. return complete
  371. }
  372. hm.RUnlock()
  373. return false
  374. }
  375. func (hm *HostMap) ClearRemotes(vpnIP uint32) {
  376. hm.Lock()
  377. i := hm.Hosts[vpnIP]
  378. if i == nil {
  379. hm.Unlock()
  380. return
  381. }
  382. i.remote = nil
  383. i.Remotes = nil
  384. hm.Unlock()
  385. }
  386. func (hm *HostMap) SetDefaultRoute(ip uint32) {
  387. hm.defaultRoute = ip
  388. }
  389. func (hm *HostMap) PunchList() []*udpAddr {
  390. var list []*udpAddr
  391. hm.RLock()
  392. for _, v := range hm.Hosts {
  393. for _, r := range v.Remotes {
  394. list = append(list, r.addr)
  395. }
  396. // if h, ok := hm.Hosts[vpnIp]; ok {
  397. // hm.Hosts[vpnIp].PromoteBest(hm.preferredRanges, false)
  398. //fmt.Println(h.remote)
  399. // }
  400. }
  401. hm.RUnlock()
  402. return list
  403. }
  404. func (hm *HostMap) Punchy(conn *udpConn) {
  405. var metricsTxPunchy metrics.Counter
  406. if hm.metricsEnabled {
  407. metricsTxPunchy = metrics.GetOrRegisterCounter("messages.tx.punchy", nil)
  408. } else {
  409. metricsTxPunchy = metrics.NilCounter{}
  410. }
  411. for {
  412. for _, addr := range hm.PunchList() {
  413. metricsTxPunchy.Inc(1)
  414. conn.WriteTo([]byte{1}, addr)
  415. }
  416. time.Sleep(time.Second * 30)
  417. }
  418. }
  419. func (hm *HostMap) addUnsafeRoutes(routes *[]route) {
  420. for _, r := range *routes {
  421. l.WithField("route", r.route).WithField("via", r.via).Warn("Adding UNSAFE Route")
  422. hm.unsafeRoutes.AddCIDR(r.route, ip2int(*r.via))
  423. }
  424. }
  425. func (i *HostInfo) MarshalJSON() ([]byte, error) {
  426. return json.Marshal(m{
  427. "remote": i.remote,
  428. "remotes": i.Remotes,
  429. "promote_counter": i.promoteCounter,
  430. "connection_state": i.ConnectionState,
  431. "handshake_start": i.handshakeStart,
  432. "handshake_ready": i.HandshakeReady,
  433. "handshake_counter": i.HandshakeCounter,
  434. "handshake_complete": i.HandshakeComplete,
  435. "handshake_packet": i.HandshakePacket,
  436. "packet_store": i.packetStore,
  437. "remote_index": i.remoteIndexId,
  438. "local_index": i.localIndexId,
  439. "host_id": int2ip(i.hostId),
  440. "receive_errors": i.recvError,
  441. "last_roam": i.lastRoam,
  442. "last_roam_remote": i.lastRoamRemote,
  443. })
  444. }
  445. func (i *HostInfo) BindConnectionState(cs *ConnectionState) {
  446. i.ConnectionState = cs
  447. }
  448. func (i *HostInfo) TryPromoteBest(preferredRanges []*net.IPNet, ifce *Interface) {
  449. if i.remote == nil {
  450. i.ForcePromoteBest(preferredRanges)
  451. return
  452. }
  453. i.promoteCounter++
  454. if i.promoteCounter%PromoteEvery == 0 {
  455. // return early if we are already on a preferred remote
  456. rIP := udp2ip(i.remote)
  457. for _, l := range preferredRanges {
  458. if l.Contains(rIP) {
  459. return
  460. }
  461. }
  462. // We re-query the lighthouse periodically while sending packets, so
  463. // check for new remotes in our local lighthouse cache
  464. ips := ifce.lightHouse.QueryCache(i.hostId)
  465. for _, ip := range ips {
  466. i.AddRemote(ip)
  467. }
  468. best, preferred := i.getBestRemote(preferredRanges)
  469. if preferred && !best.Equals(i.remote) {
  470. // Try to send a test packet to that host, this should
  471. // cause it to detect a roaming event and switch remotes
  472. ifce.send(test, testRequest, i.ConnectionState, i, best, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  473. }
  474. }
  475. }
  476. func (i *HostInfo) ForcePromoteBest(preferredRanges []*net.IPNet) {
  477. best, _ := i.getBestRemote(preferredRanges)
  478. if best != nil {
  479. i.remote = best
  480. }
  481. }
  482. func (i *HostInfo) getBestRemote(preferredRanges []*net.IPNet) (best *udpAddr, preferred bool) {
  483. if len(i.Remotes) > 0 {
  484. for _, r := range i.Remotes {
  485. rIP := udp2ip(r.addr)
  486. for _, l := range preferredRanges {
  487. if l.Contains(rIP) {
  488. return r.addr, true
  489. }
  490. }
  491. if best == nil || !PrivateIP(rIP) {
  492. best = r.addr
  493. }
  494. /*
  495. for _, r := range i.Remotes {
  496. // Must have > 80% probe success to be considered.
  497. //fmt.Println("GRADE:", r.addr.IP, r.Grade())
  498. if r.Grade() > float64(.8) {
  499. if localToMe.Contains(r.addr.IP) == true {
  500. best = r.addr
  501. break
  502. //i.remote = i.Remotes[c].addr
  503. } else {
  504. //}
  505. }
  506. */
  507. }
  508. return best, false
  509. }
  510. return nil, false
  511. }
  512. // rotateRemote will move remote to the next ip in the list of remote ips for this host
  513. // This is different than PromoteBest in that what is algorithmically best may not actually work.
  514. // Only known use case is when sending a stage 0 handshake.
  515. // It may be better to just send stage 0 handshakes to all known ips and sort it out in the receiver.
  516. func (i *HostInfo) rotateRemote() {
  517. // We have 0, can't rotate
  518. if len(i.Remotes) < 1 {
  519. return
  520. }
  521. if i.remote == nil {
  522. i.remote = i.Remotes[0].addr
  523. return
  524. }
  525. // We want to look at all but the very last entry since that is handled at the end
  526. for x := 0; x < len(i.Remotes)-1; x++ {
  527. // Find our current position and move to the next one in the list
  528. if i.Remotes[x].addr.Equals(i.remote) {
  529. i.remote = i.Remotes[x+1].addr
  530. return
  531. }
  532. }
  533. // Our current position was likely the last in the list, start over at 0
  534. i.remote = i.Remotes[0].addr
  535. }
  536. func (i *HostInfo) cachePacket(t NebulaMessageType, st NebulaMessageSubType, packet []byte, f packetCallback) {
  537. //TODO: return the error so we can log with more context
  538. if len(i.packetStore) < 100 {
  539. tempPacket := make([]byte, len(packet))
  540. copy(tempPacket, packet)
  541. //l.WithField("trace", string(debug.Stack())).Error("Caching packet", tempPacket)
  542. i.packetStore = append(i.packetStore, &cachedPacket{t, st, f, tempPacket})
  543. i.logger().
  544. WithField("length", len(i.packetStore)).
  545. WithField("stored", true).
  546. Debugf("Packet store")
  547. } else if l.Level >= logrus.DebugLevel {
  548. i.logger().
  549. WithField("length", len(i.packetStore)).
  550. WithField("stored", false).
  551. Debugf("Packet store")
  552. }
  553. }
  554. // handshakeComplete will set the connection as ready to communicate, as well as flush any stored packets
  555. func (i *HostInfo) handshakeComplete() {
  556. //TODO: I'm not certain the distinction between handshake complete and ConnectionState being ready matters because:
  557. //TODO: HandshakeComplete means send stored packets and ConnectionState.ready means we are ready to send
  558. //TODO: if the transition from HandhsakeComplete to ConnectionState.ready happens all within this function they are identical
  559. i.ConnectionState.queueLock.Lock()
  560. i.HandshakeComplete = true
  561. //TODO: this should be managed by the handshake state machine to set it based on how many handshake were seen.
  562. // Clamping it to 2 gets us out of the woods for now
  563. *i.ConnectionState.messageCounter = 2
  564. i.logger().Debugf("Sending %d stored packets", len(i.packetStore))
  565. nb := make([]byte, 12, 12)
  566. out := make([]byte, mtu)
  567. for _, cp := range i.packetStore {
  568. cp.callback(cp.messageType, cp.messageSubType, i, cp.packet, nb, out)
  569. }
  570. i.packetStore = make([]*cachedPacket, 0)
  571. i.ConnectionState.ready = true
  572. i.ConnectionState.queueLock.Unlock()
  573. i.ConnectionState.certState = nil
  574. }
  575. func (i *HostInfo) RemoteUDPAddrs() []*udpAddr {
  576. var addrs []*udpAddr
  577. for _, r := range i.Remotes {
  578. addrs = append(addrs, r.addr)
  579. }
  580. return addrs
  581. }
  582. func (i *HostInfo) GetCert() *cert.NebulaCertificate {
  583. if i.ConnectionState != nil {
  584. return i.ConnectionState.peerCert
  585. }
  586. return nil
  587. }
  588. func (i *HostInfo) AddRemote(r udpAddr) *udpAddr {
  589. remote := &r
  590. //add := true
  591. for _, r := range i.Remotes {
  592. if r.addr.Equals(remote) {
  593. return r.addr
  594. //add = false
  595. }
  596. }
  597. // Trim this down if necessary
  598. if len(i.Remotes) > MaxRemotes {
  599. i.Remotes = i.Remotes[len(i.Remotes)-MaxRemotes:]
  600. }
  601. i.Remotes = append(i.Remotes, NewHostInfoDest(remote))
  602. return remote
  603. //l.Debugf("Added remote %s for vpn ip", remote)
  604. }
  605. func (i *HostInfo) SetRemote(remote udpAddr) {
  606. i.remote = i.AddRemote(remote)
  607. }
  608. func (i *HostInfo) ClearRemotes() {
  609. i.remote = nil
  610. i.Remotes = []*HostInfoDest{}
  611. }
  612. func (i *HostInfo) ClearConnectionState() {
  613. i.ConnectionState = nil
  614. }
  615. func (i *HostInfo) RecvErrorExceeded() bool {
  616. if i.recvError < 3 {
  617. i.recvError += 1
  618. return false
  619. }
  620. return true
  621. }
  622. func (i *HostInfo) CreateRemoteCIDR(c *cert.NebulaCertificate) {
  623. if len(c.Details.Ips) == 1 && len(c.Details.Subnets) == 0 {
  624. // Simple case, no CIDRTree needed
  625. return
  626. }
  627. remoteCidr := NewCIDRTree()
  628. for _, ip := range c.Details.Ips {
  629. remoteCidr.AddCIDR(&net.IPNet{IP: ip.IP, Mask: net.IPMask{255, 255, 255, 255}}, struct{}{})
  630. }
  631. for _, n := range c.Details.Subnets {
  632. remoteCidr.AddCIDR(n, struct{}{})
  633. }
  634. i.remoteCidr = remoteCidr
  635. }
  636. func (i *HostInfo) logger() *logrus.Entry {
  637. if i == nil {
  638. return logrus.NewEntry(l)
  639. }
  640. li := l.WithField("vpnIp", IntIp(i.hostId))
  641. if connState := i.ConnectionState; connState != nil {
  642. if peerCert := connState.peerCert; peerCert != nil {
  643. li = li.WithField("certName", peerCert.Details.Name)
  644. }
  645. }
  646. return li
  647. }
  648. //########################
  649. func NewHostInfoDest(addr *udpAddr) *HostInfoDest {
  650. i := &HostInfoDest{
  651. addr: addr,
  652. }
  653. return i
  654. }
  655. func (hid *HostInfoDest) MarshalJSON() ([]byte, error) {
  656. return json.Marshal(m{
  657. "address": hid.addr,
  658. "probe_count": hid.probeCounter,
  659. })
  660. }
  661. /*
  662. func (hm *HostMap) DebugRemotes(vpnIp uint32) string {
  663. s := "\n"
  664. for _, h := range hm.Hosts {
  665. for _, r := range h.Remotes {
  666. s += fmt.Sprintf("%s : %d ## %v\n", r.addr.IP.String(), r.addr.Port, r.probes)
  667. }
  668. }
  669. return s
  670. }
  671. func (d *HostInfoDest) Grade() float64 {
  672. c1 := ProbeLen
  673. for n := len(d.probes) - 1; n >= 0; n-- {
  674. if d.probes[n] == true {
  675. c1 -= 1
  676. }
  677. }
  678. return float64(c1) / float64(ProbeLen)
  679. }
  680. func (d *HostInfoDest) Grade() (float64, float64, float64) {
  681. c1 := ProbeLen
  682. c2 := ProbeLen / 2
  683. c2c := ProbeLen - ProbeLen/2
  684. c3 := ProbeLen / 5
  685. c3c := ProbeLen - ProbeLen/5
  686. for n := len(d.probes) - 1; n >= 0; n-- {
  687. if d.probes[n] == true {
  688. c1 -= 1
  689. if n >= c2c {
  690. c2 -= 1
  691. if n >= c3c {
  692. c3 -= 1
  693. }
  694. }
  695. }
  696. //if n >= d {
  697. }
  698. return float64(c3) / float64(ProbeLen/5), float64(c2) / float64(ProbeLen/2), float64(c1) / float64(ProbeLen)
  699. //return float64(c1) / float64(ProbeLen), float64(c2) / float64(ProbeLen/2), float64(c3) / float64(ProbeLen/5)
  700. }
  701. func (i *HostInfo) HandleReply(addr *net.UDPAddr, counter int) {
  702. for _, r := range i.Remotes {
  703. if r.addr.IP.Equal(addr.IP) && r.addr.Port == addr.Port {
  704. r.ProbeReceived(counter)
  705. }
  706. }
  707. }
  708. func (i *HostInfo) Probes() []*Probe {
  709. p := []*Probe{}
  710. for _, d := range i.Remotes {
  711. p = append(p, &Probe{Addr: d.addr, Counter: d.Probe()})
  712. }
  713. return p
  714. }
  715. func (d *HostInfoDest) Probe() int {
  716. //d.probes = append(d.probes, true)
  717. d.probeCounter++
  718. d.probes[d.probeCounter%ProbeLen] = true
  719. return d.probeCounter
  720. //return d.probeCounter
  721. }
  722. func (d *HostInfoDest) ProbeReceived(probeCount int) {
  723. if probeCount >= (d.probeCounter - ProbeLen) {
  724. //fmt.Println("PROBE WORKED", probeCount)
  725. //fmt.Println(d.addr, d.Grade())
  726. d.probes[probeCount%ProbeLen] = false
  727. }
  728. }
  729. */
  730. // Utility functions
  731. func localIps(allowList *AllowList) *[]net.IP {
  732. //FIXME: This function is pretty garbage
  733. var ips []net.IP
  734. ifaces, _ := net.Interfaces()
  735. for _, i := range ifaces {
  736. allow := allowList.AllowName(i.Name)
  737. l.WithField("interfaceName", i.Name).WithField("allow", allow).Debug("localAllowList.AllowName")
  738. if !allow {
  739. continue
  740. }
  741. addrs, _ := i.Addrs()
  742. for _, addr := range addrs {
  743. var ip net.IP
  744. switch v := addr.(type) {
  745. case *net.IPNet:
  746. //continue
  747. ip = v.IP
  748. case *net.IPAddr:
  749. ip = v.IP
  750. }
  751. if ip.To4() != nil && ip.IsLoopback() == false {
  752. allow := allowList.Allow(ip2int(ip))
  753. l.WithField("localIp", ip).WithField("allow", allow).Debug("localAllowList.Allow")
  754. if !allow {
  755. continue
  756. }
  757. ips = append(ips, ip)
  758. }
  759. }
  760. }
  761. return &ips
  762. }
  763. func PrivateIP(ip net.IP) bool {
  764. private := false
  765. _, private24BitBlock, _ := net.ParseCIDR("10.0.0.0/8")
  766. _, private20BitBlock, _ := net.ParseCIDR("172.16.0.0/12")
  767. _, private16BitBlock, _ := net.ParseCIDR("192.168.0.0/16")
  768. private = private24BitBlock.Contains(ip) || private20BitBlock.Contains(ip) || private16BitBlock.Contains(ip)
  769. return private
  770. }