hostmap.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. )
  13. //const ProbeLen = 100
  14. const PromoteEvery = 1000
  15. const ReQueryEvery = 5000
  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. unsafeRoutes *CIDRTree
  29. metricsEnabled bool
  30. l *logrus.Logger
  31. }
  32. type HostInfo struct {
  33. sync.RWMutex
  34. remote *udpAddr
  35. remotes *RemoteList
  36. promoteCounter uint32
  37. ConnectionState *ConnectionState
  38. handshakeStart time.Time //todo: this an entry in the handshake manager
  39. HandshakeReady bool //todo: being in the manager means you are ready
  40. HandshakeCounter int //todo: another handshake manager entry
  41. HandshakeComplete bool //todo: this should go away in favor of ConnectionState.ready
  42. HandshakePacket map[uint8][]byte //todo: this is other handshake manager entry
  43. packetStore []*cachedPacket //todo: this is other handshake manager entry
  44. remoteIndexId uint32
  45. localIndexId uint32
  46. hostId uint32
  47. recvError int
  48. remoteCidr *CIDRTree
  49. // lastRebindCount is the other side of Interface.rebindCount, if these values don't match then we need to ask LH
  50. // for a punch from the remote end of this tunnel. The goal being to prime their conntrack for our traffic just like
  51. // with a handshake
  52. lastRebindCount int8
  53. // lastHandshakeTime records the time the remote side told us about at the stage when the handshake was completed locally
  54. // Stage 1 packet will contain it if I am a responder, stage 2 packet if I am an initiator
  55. // This is used to avoid an attack where a handshake packet is replayed after some time
  56. lastHandshakeTime uint64
  57. lastRoam time.Time
  58. lastRoamRemote *udpAddr
  59. }
  60. type cachedPacket struct {
  61. messageType NebulaMessageType
  62. messageSubType NebulaMessageSubType
  63. callback packetCallback
  64. packet []byte
  65. }
  66. type packetCallback func(t NebulaMessageType, st NebulaMessageSubType, h *HostInfo, p, nb, out []byte)
  67. func NewHostMap(l *logrus.Logger, name string, vpnCIDR *net.IPNet, preferredRanges []*net.IPNet) *HostMap {
  68. h := map[uint32]*HostInfo{}
  69. i := map[uint32]*HostInfo{}
  70. r := map[uint32]*HostInfo{}
  71. m := HostMap{
  72. name: name,
  73. Indexes: i,
  74. RemoteIndexes: r,
  75. Hosts: h,
  76. preferredRanges: preferredRanges,
  77. vpnCIDR: vpnCIDR,
  78. unsafeRoutes: NewCIDRTree(),
  79. l: l,
  80. }
  81. return &m
  82. }
  83. // UpdateStats takes a name and reports host and index counts to the stats collection system
  84. func (hm *HostMap) EmitStats(name string) {
  85. hm.RLock()
  86. hostLen := len(hm.Hosts)
  87. indexLen := len(hm.Indexes)
  88. remoteIndexLen := len(hm.RemoteIndexes)
  89. hm.RUnlock()
  90. metrics.GetOrRegisterGauge("hostmap."+name+".hosts", nil).Update(int64(hostLen))
  91. metrics.GetOrRegisterGauge("hostmap."+name+".indexes", nil).Update(int64(indexLen))
  92. metrics.GetOrRegisterGauge("hostmap."+name+".remoteIndexes", nil).Update(int64(remoteIndexLen))
  93. }
  94. func (hm *HostMap) GetIndexByVpnIP(vpnIP uint32) (uint32, error) {
  95. hm.RLock()
  96. if i, ok := hm.Hosts[vpnIP]; ok {
  97. index := i.localIndexId
  98. hm.RUnlock()
  99. return index, nil
  100. }
  101. hm.RUnlock()
  102. return 0, errors.New("vpn IP not found")
  103. }
  104. func (hm *HostMap) Add(ip uint32, hostinfo *HostInfo) {
  105. hm.Lock()
  106. hm.Hosts[ip] = hostinfo
  107. hm.Unlock()
  108. }
  109. func (hm *HostMap) AddVpnIP(vpnIP uint32) *HostInfo {
  110. h := &HostInfo{}
  111. hm.RLock()
  112. if _, ok := hm.Hosts[vpnIP]; !ok {
  113. hm.RUnlock()
  114. h = &HostInfo{
  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 hm.l.Level >= logrus.DebugLevel {
  137. hm.l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(vpnIP), "mapTotalSize": len(hm.Hosts)}).
  138. Debug("Hostmap vpnIp deleted")
  139. }
  140. }
  141. // Only used by pendingHostMap when the remote index is not initially known
  142. func (hm *HostMap) addRemoteIndexHostInfo(index uint32, h *HostInfo) {
  143. hm.Lock()
  144. h.remoteIndexId = index
  145. hm.RemoteIndexes[index] = h
  146. hm.Unlock()
  147. if hm.l.Level > logrus.DebugLevel {
  148. hm.l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes),
  149. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  150. Debug("Hostmap remoteIndex added")
  151. }
  152. }
  153. func (hm *HostMap) AddVpnIPHostInfo(vpnIP uint32, h *HostInfo) {
  154. hm.Lock()
  155. h.hostId = vpnIP
  156. hm.Hosts[vpnIP] = h
  157. hm.Indexes[h.localIndexId] = h
  158. hm.RemoteIndexes[h.remoteIndexId] = h
  159. hm.Unlock()
  160. if hm.l.Level > logrus.DebugLevel {
  161. hm.l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(vpnIP), "mapTotalSize": len(hm.Hosts),
  162. "hostinfo": m{"existing": true, "localIndexId": h.localIndexId, "hostId": IntIp(h.hostId)}}).
  163. Debug("Hostmap vpnIp added")
  164. }
  165. }
  166. // This is only called in pendingHostmap, to cleanup an inbound handshake
  167. func (hm *HostMap) DeleteIndex(index uint32) {
  168. hm.Lock()
  169. hostinfo, ok := hm.Indexes[index]
  170. if ok {
  171. delete(hm.Indexes, index)
  172. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  173. // Check if we have an entry under hostId that matches the same hostinfo
  174. // instance. Clean it up as well if we do.
  175. hostinfo2, ok := hm.Hosts[hostinfo.hostId]
  176. if ok && hostinfo2 == hostinfo {
  177. delete(hm.Hosts, hostinfo.hostId)
  178. }
  179. }
  180. hm.Unlock()
  181. if hm.l.Level >= logrus.DebugLevel {
  182. hm.l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes)}).
  183. Debug("Hostmap index deleted")
  184. }
  185. }
  186. // This is used to cleanup on recv_error
  187. func (hm *HostMap) DeleteReverseIndex(index uint32) {
  188. hm.Lock()
  189. hostinfo, ok := hm.RemoteIndexes[index]
  190. if ok {
  191. delete(hm.Indexes, hostinfo.localIndexId)
  192. delete(hm.RemoteIndexes, index)
  193. // Check if we have an entry under hostId that matches the same hostinfo
  194. // instance. Clean it up as well if we do (they might not match in pendingHostmap)
  195. var hostinfo2 *HostInfo
  196. hostinfo2, ok = hm.Hosts[hostinfo.hostId]
  197. if ok && hostinfo2 == hostinfo {
  198. delete(hm.Hosts, hostinfo.hostId)
  199. }
  200. }
  201. hm.Unlock()
  202. if hm.l.Level >= logrus.DebugLevel {
  203. hm.l.WithField("hostMap", m{"mapName": hm.name, "indexNumber": index, "mapTotalSize": len(hm.Indexes)}).
  204. Debug("Hostmap remote index deleted")
  205. }
  206. }
  207. func (hm *HostMap) DeleteHostInfo(hostinfo *HostInfo) {
  208. hm.Lock()
  209. defer hm.Unlock()
  210. hm.unlockedDeleteHostInfo(hostinfo)
  211. }
  212. func (hm *HostMap) unlockedDeleteHostInfo(hostinfo *HostInfo) {
  213. // Check if this same hostId is in the hostmap with a different instance.
  214. // This could happen if we have an entry in the pending hostmap with different
  215. // index values than the one in the main hostmap.
  216. hostinfo2, ok := hm.Hosts[hostinfo.hostId]
  217. if ok && hostinfo2 != hostinfo {
  218. delete(hm.Hosts, hostinfo2.hostId)
  219. delete(hm.Indexes, hostinfo2.localIndexId)
  220. delete(hm.RemoteIndexes, hostinfo2.remoteIndexId)
  221. }
  222. delete(hm.Hosts, hostinfo.hostId)
  223. if len(hm.Hosts) == 0 {
  224. hm.Hosts = map[uint32]*HostInfo{}
  225. }
  226. delete(hm.Indexes, hostinfo.localIndexId)
  227. if len(hm.Indexes) == 0 {
  228. hm.Indexes = map[uint32]*HostInfo{}
  229. }
  230. delete(hm.RemoteIndexes, hostinfo.remoteIndexId)
  231. if len(hm.RemoteIndexes) == 0 {
  232. hm.RemoteIndexes = map[uint32]*HostInfo{}
  233. }
  234. if hm.l.Level >= logrus.DebugLevel {
  235. hm.l.WithField("hostMap", m{"mapName": hm.name, "mapTotalSize": len(hm.Hosts),
  236. "vpnIp": IntIp(hostinfo.hostId), "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId}).
  237. Debug("Hostmap hostInfo deleted")
  238. }
  239. }
  240. func (hm *HostMap) QueryIndex(index uint32) (*HostInfo, error) {
  241. //TODO: we probably just want ot return bool instead of error, or at least a static error
  242. hm.RLock()
  243. if h, ok := hm.Indexes[index]; ok {
  244. hm.RUnlock()
  245. return h, nil
  246. } else {
  247. hm.RUnlock()
  248. return nil, errors.New("unable to find index")
  249. }
  250. }
  251. func (hm *HostMap) QueryReverseIndex(index uint32) (*HostInfo, error) {
  252. hm.RLock()
  253. if h, ok := hm.RemoteIndexes[index]; ok {
  254. hm.RUnlock()
  255. return h, nil
  256. } else {
  257. hm.RUnlock()
  258. return nil, fmt.Errorf("unable to find reverse index or connectionstate nil in %s hostmap", hm.name)
  259. }
  260. }
  261. func (hm *HostMap) QueryVpnIP(vpnIp uint32) (*HostInfo, error) {
  262. return hm.queryVpnIP(vpnIp, nil)
  263. }
  264. // PromoteBestQueryVpnIP will attempt to lazily switch to the best remote every
  265. // `PromoteEvery` calls to this function for a given host.
  266. func (hm *HostMap) PromoteBestQueryVpnIP(vpnIp uint32, ifce *Interface) (*HostInfo, error) {
  267. return hm.queryVpnIP(vpnIp, ifce)
  268. }
  269. func (hm *HostMap) queryVpnIP(vpnIp uint32, promoteIfce *Interface) (*HostInfo, error) {
  270. hm.RLock()
  271. if h, ok := hm.Hosts[vpnIp]; ok {
  272. // Do not attempt promotion if you are a lighthouse
  273. if promoteIfce != nil && !promoteIfce.lightHouse.amLighthouse {
  274. h.TryPromoteBest(hm.preferredRanges, promoteIfce)
  275. }
  276. hm.RUnlock()
  277. return h, nil
  278. } else {
  279. //return &net.UDPAddr{}, nil, errors.New("Unable to find host")
  280. hm.RUnlock()
  281. /*
  282. if lightHouse != nil {
  283. lightHouse.Query(vpnIp)
  284. return nil, errors.New("Unable to find host")
  285. }
  286. */
  287. return nil, errors.New("unable to find host")
  288. }
  289. }
  290. func (hm *HostMap) queryUnsafeRoute(ip uint32) uint32 {
  291. r := hm.unsafeRoutes.MostSpecificContains(ip)
  292. if r != nil {
  293. return r.(uint32)
  294. } else {
  295. return 0
  296. }
  297. }
  298. // We already have the hm Lock when this is called, so make sure to not call
  299. // any other methods that might try to grab it again
  300. func (hm *HostMap) addHostInfo(hostinfo *HostInfo, f *Interface) {
  301. if f.serveDns {
  302. remoteCert := hostinfo.ConnectionState.peerCert
  303. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  304. }
  305. hm.Hosts[hostinfo.hostId] = hostinfo
  306. hm.Indexes[hostinfo.localIndexId] = hostinfo
  307. hm.RemoteIndexes[hostinfo.remoteIndexId] = hostinfo
  308. if hm.l.Level >= logrus.DebugLevel {
  309. hm.l.WithField("hostMap", m{"mapName": hm.name, "vpnIp": IntIp(hostinfo.hostId), "mapTotalSize": len(hm.Hosts),
  310. "hostinfo": m{"existing": true, "localIndexId": hostinfo.localIndexId, "hostId": IntIp(hostinfo.hostId)}}).
  311. Debug("Hostmap vpnIp added")
  312. }
  313. }
  314. // punchList assembles a list of all non nil RemoteList pointer entries in this hostmap
  315. // The caller can then do the its work outside of the read lock
  316. func (hm *HostMap) punchList(rl []*RemoteList) []*RemoteList {
  317. hm.RLock()
  318. defer hm.RUnlock()
  319. for _, v := range hm.Hosts {
  320. if v.remotes != nil {
  321. rl = append(rl, v.remotes)
  322. }
  323. }
  324. return rl
  325. }
  326. // Punchy iterates through the result of punchList() to assemble all known addresses and sends a hole punch packet to them
  327. func (hm *HostMap) Punchy(conn *udpConn) {
  328. var metricsTxPunchy metrics.Counter
  329. if hm.metricsEnabled {
  330. metricsTxPunchy = metrics.GetOrRegisterCounter("messages.tx.punchy", nil)
  331. } else {
  332. metricsTxPunchy = metrics.NilCounter{}
  333. }
  334. var remotes []*RemoteList
  335. b := []byte{1}
  336. for {
  337. remotes = hm.punchList(remotes[:0])
  338. for _, rl := range remotes {
  339. //TODO: CopyAddrs generates garbage but ForEach locks for the work here, figure out which way is better
  340. for _, addr := range rl.CopyAddrs(hm.preferredRanges) {
  341. metricsTxPunchy.Inc(1)
  342. conn.WriteTo(b, addr)
  343. }
  344. }
  345. time.Sleep(time.Second * 10)
  346. }
  347. }
  348. func (hm *HostMap) addUnsafeRoutes(routes *[]route) {
  349. for _, r := range *routes {
  350. hm.l.WithField("route", r.route).WithField("via", r.via).Warn("Adding UNSAFE Route")
  351. hm.unsafeRoutes.AddCIDR(r.route, ip2int(*r.via))
  352. }
  353. }
  354. func (i *HostInfo) BindConnectionState(cs *ConnectionState) {
  355. i.ConnectionState = cs
  356. }
  357. // TryPromoteBest handles re-querying lighthouses and probing for better paths
  358. // NOTE: It is an error to call this if you are a lighthouse since they should not roam clients!
  359. func (i *HostInfo) TryPromoteBest(preferredRanges []*net.IPNet, ifce *Interface) {
  360. c := atomic.AddUint32(&i.promoteCounter, 1)
  361. if c%PromoteEvery == 0 {
  362. // return early if we are already on a preferred remote
  363. rIP := i.remote.IP
  364. for _, l := range preferredRanges {
  365. if l.Contains(rIP) {
  366. return
  367. }
  368. }
  369. i.remotes.ForEach(preferredRanges, func(addr *udpAddr, preferred bool) {
  370. if addr == nil || !preferred {
  371. return
  372. }
  373. // Try to send a test packet to that host, this should
  374. // cause it to detect a roaming event and switch remotes
  375. ifce.send(test, testRequest, i.ConnectionState, i, addr, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  376. })
  377. }
  378. // Re query our lighthouses for new remotes occasionally
  379. if c%ReQueryEvery == 0 && ifce.lightHouse != nil {
  380. ifce.lightHouse.QueryServer(i.hostId, ifce)
  381. }
  382. }
  383. func (i *HostInfo) cachePacket(l *logrus.Logger, t NebulaMessageType, st NebulaMessageSubType, packet []byte, f packetCallback) {
  384. //TODO: return the error so we can log with more context
  385. if len(i.packetStore) < 100 {
  386. tempPacket := make([]byte, len(packet))
  387. copy(tempPacket, packet)
  388. //l.WithField("trace", string(debug.Stack())).Error("Caching packet", tempPacket)
  389. i.packetStore = append(i.packetStore, &cachedPacket{t, st, f, tempPacket})
  390. if l.Level >= logrus.DebugLevel {
  391. i.logger(l).
  392. WithField("length", len(i.packetStore)).
  393. WithField("stored", true).
  394. Debugf("Packet store")
  395. }
  396. } else if l.Level >= logrus.DebugLevel {
  397. i.logger(l).
  398. WithField("length", len(i.packetStore)).
  399. WithField("stored", false).
  400. Debugf("Packet store")
  401. }
  402. }
  403. // handshakeComplete will set the connection as ready to communicate, as well as flush any stored packets
  404. func (i *HostInfo) handshakeComplete(l *logrus.Logger) {
  405. //TODO: I'm not certain the distinction between handshake complete and ConnectionState being ready matters because:
  406. //TODO: HandshakeComplete means send stored packets and ConnectionState.ready means we are ready to send
  407. //TODO: if the transition from HandhsakeComplete to ConnectionState.ready happens all within this function they are identical
  408. i.ConnectionState.queueLock.Lock()
  409. i.HandshakeComplete = true
  410. //TODO: this should be managed by the handshake state machine to set it based on how many handshake were seen.
  411. // Clamping it to 2 gets us out of the woods for now
  412. atomic.StoreUint64(&i.ConnectionState.atomicMessageCounter, 2)
  413. if l.Level >= logrus.DebugLevel {
  414. i.logger(l).Debugf("Sending %d stored packets", len(i.packetStore))
  415. }
  416. if len(i.packetStore) > 0 {
  417. nb := make([]byte, 12, 12)
  418. out := make([]byte, mtu)
  419. for _, cp := range i.packetStore {
  420. cp.callback(cp.messageType, cp.messageSubType, i, cp.packet, nb, out)
  421. }
  422. }
  423. i.remotes.ResetBlockedRemotes()
  424. i.packetStore = make([]*cachedPacket, 0)
  425. i.ConnectionState.ready = true
  426. i.ConnectionState.queueLock.Unlock()
  427. i.ConnectionState.certState = nil
  428. }
  429. func (i *HostInfo) GetCert() *cert.NebulaCertificate {
  430. if i.ConnectionState != nil {
  431. return i.ConnectionState.peerCert
  432. }
  433. return nil
  434. }
  435. func (i *HostInfo) SetRemote(remote *udpAddr) {
  436. // We copy here because we likely got this remote from a source that reuses the object
  437. if !i.remote.Equals(remote) {
  438. i.remote = remote.Copy()
  439. i.remotes.LearnRemote(i.hostId, remote.Copy())
  440. }
  441. }
  442. func (i *HostInfo) ClearConnectionState() {
  443. i.ConnectionState = nil
  444. }
  445. func (i *HostInfo) RecvErrorExceeded() bool {
  446. if i.recvError < 3 {
  447. i.recvError += 1
  448. return false
  449. }
  450. return true
  451. }
  452. func (i *HostInfo) CreateRemoteCIDR(c *cert.NebulaCertificate) {
  453. if len(c.Details.Ips) == 1 && len(c.Details.Subnets) == 0 {
  454. // Simple case, no CIDRTree needed
  455. return
  456. }
  457. remoteCidr := NewCIDRTree()
  458. for _, ip := range c.Details.Ips {
  459. remoteCidr.AddCIDR(&net.IPNet{IP: ip.IP, Mask: net.IPMask{255, 255, 255, 255}}, struct{}{})
  460. }
  461. for _, n := range c.Details.Subnets {
  462. remoteCidr.AddCIDR(n, struct{}{})
  463. }
  464. i.remoteCidr = remoteCidr
  465. }
  466. func (i *HostInfo) logger(l *logrus.Logger) *logrus.Entry {
  467. if i == nil {
  468. return logrus.NewEntry(l)
  469. }
  470. li := l.WithField("vpnIp", IntIp(i.hostId))
  471. if connState := i.ConnectionState; connState != nil {
  472. if peerCert := connState.peerCert; peerCert != nil {
  473. li = li.WithField("certName", peerCert.Details.Name)
  474. }
  475. }
  476. return li
  477. }
  478. //########################
  479. /*
  480. func (hm *HostMap) DebugRemotes(vpnIp uint32) string {
  481. s := "\n"
  482. for _, h := range hm.Hosts {
  483. for _, r := range h.Remotes {
  484. s += fmt.Sprintf("%s : %d ## %v\n", r.addr.IP.String(), r.addr.Port, r.probes)
  485. }
  486. }
  487. return s
  488. }
  489. func (i *HostInfo) HandleReply(addr *net.UDPAddr, counter int) {
  490. for _, r := range i.Remotes {
  491. if r.addr.IP.Equal(addr.IP) && r.addr.Port == addr.Port {
  492. r.ProbeReceived(counter)
  493. }
  494. }
  495. }
  496. func (i *HostInfo) Probes() []*Probe {
  497. p := []*Probe{}
  498. for _, d := range i.Remotes {
  499. p = append(p, &Probe{Addr: d.addr, Counter: d.Probe()})
  500. }
  501. return p
  502. }
  503. */
  504. // Utility functions
  505. func localIps(l *logrus.Logger, allowList *AllowList) *[]net.IP {
  506. //FIXME: This function is pretty garbage
  507. var ips []net.IP
  508. ifaces, _ := net.Interfaces()
  509. for _, i := range ifaces {
  510. allow := allowList.AllowName(i.Name)
  511. if l.Level >= logrus.TraceLevel {
  512. l.WithField("interfaceName", i.Name).WithField("allow", allow).Trace("localAllowList.AllowName")
  513. }
  514. if !allow {
  515. continue
  516. }
  517. addrs, _ := i.Addrs()
  518. for _, addr := range addrs {
  519. var ip net.IP
  520. switch v := addr.(type) {
  521. case *net.IPNet:
  522. //continue
  523. ip = v.IP
  524. case *net.IPAddr:
  525. ip = v.IP
  526. }
  527. //TODO: Filtering out link local for now, this is probably the most correct thing
  528. //TODO: Would be nice to filter out SLAAC MAC based ips as well
  529. if ip.IsLoopback() == false && !ip.IsLinkLocalUnicast() {
  530. allow := allowList.Allow(ip)
  531. if l.Level >= logrus.TraceLevel {
  532. l.WithField("localIp", ip).WithField("allow", allow).Trace("localAllowList.Allow")
  533. }
  534. if !allow {
  535. continue
  536. }
  537. ips = append(ips, ip)
  538. }
  539. }
  540. }
  541. return &ips
  542. }