hostmap.go 22 KB

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