3
0

lighthouse.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/netip"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "github.com/rcrowley/go-metrics"
  13. "github.com/sirupsen/logrus"
  14. "github.com/slackhq/nebula/cidr"
  15. "github.com/slackhq/nebula/config"
  16. "github.com/slackhq/nebula/header"
  17. "github.com/slackhq/nebula/iputil"
  18. "github.com/slackhq/nebula/udp"
  19. "github.com/slackhq/nebula/util"
  20. )
  21. //TODO: if a lighthouse doesn't have an answer, clients AGGRESSIVELY REQUERY.. why? handshake manager and/or getOrHandshake?
  22. //TODO: nodes are roaming lighthouses, this is bad. How are they learning?
  23. var ErrHostNotKnown = errors.New("host not known")
  24. type netIpAndPort struct {
  25. ip net.IP
  26. port uint16
  27. }
  28. type LightHouse struct {
  29. //TODO: We need a timer wheel to kick out vpnIps that haven't reported in a long time
  30. sync.RWMutex //Because we concurrently read and write to our maps
  31. ctx context.Context
  32. amLighthouse bool
  33. myVpnIp iputil.VpnIp
  34. myVpnZeros iputil.VpnIp
  35. myVpnNet *net.IPNet
  36. punchConn udp.Conn
  37. punchy *Punchy
  38. // Local cache of answers from light houses
  39. // map of vpn Ip to answers
  40. addrMap map[iputil.VpnIp]*RemoteList
  41. // filters remote addresses allowed for each host
  42. // - When we are a lighthouse, this filters what addresses we store and
  43. // respond with.
  44. // - When we are not a lighthouse, this filters which addresses we accept
  45. // from lighthouses.
  46. remoteAllowList atomic.Pointer[RemoteAllowList]
  47. // filters local addresses that we advertise to lighthouses
  48. localAllowList atomic.Pointer[LocalAllowList]
  49. // used to trigger the HandshakeManager when we receive HostQueryReply
  50. handshakeTrigger chan<- iputil.VpnIp
  51. // staticList exists to avoid having a bool in each addrMap entry
  52. // since static should be rare
  53. staticList atomic.Pointer[map[iputil.VpnIp]struct{}]
  54. lighthouses atomic.Pointer[map[iputil.VpnIp]struct{}]
  55. interval atomic.Int64
  56. updateCancel context.CancelFunc
  57. ifce EncWriter
  58. nebulaPort uint32 // 32 bits because protobuf does not have a uint16
  59. advertiseAddrs atomic.Pointer[[]netIpAndPort]
  60. // IP's of relays that can be used by peers to access me
  61. relaysForMe atomic.Pointer[[]iputil.VpnIp]
  62. queryChan chan iputil.VpnIp
  63. calculatedRemotes atomic.Pointer[cidr.Tree4[[]*calculatedRemote]] // Maps VpnIp to []*calculatedRemote
  64. metrics *MessageMetrics
  65. metricHolepunchTx metrics.Counter
  66. l *logrus.Logger
  67. }
  68. // NewLightHouseFromConfig will build a Lighthouse struct from the values provided in the config object
  69. // addrMap should be nil unless this is during a config reload
  70. func NewLightHouseFromConfig(ctx context.Context, l *logrus.Logger, c *config.C, myVpnNet *net.IPNet, pc udp.Conn, p *Punchy) (*LightHouse, error) {
  71. amLighthouse := c.GetBool("lighthouse.am_lighthouse", false)
  72. nebulaPort := uint32(c.GetInt("listen.port", 0))
  73. if amLighthouse && nebulaPort == 0 {
  74. return nil, util.NewContextualError("lighthouse.am_lighthouse enabled on node but no port number is set in config", nil, nil)
  75. }
  76. // If port is dynamic, discover it
  77. if nebulaPort == 0 && pc != nil {
  78. uPort, err := pc.LocalAddr()
  79. if err != nil {
  80. return nil, util.NewContextualError("Failed to get listening port", nil, err)
  81. }
  82. nebulaPort = uint32(uPort.Port)
  83. }
  84. ones, _ := myVpnNet.Mask.Size()
  85. h := LightHouse{
  86. ctx: ctx,
  87. amLighthouse: amLighthouse,
  88. myVpnIp: iputil.Ip2VpnIp(myVpnNet.IP),
  89. myVpnZeros: iputil.VpnIp(32 - ones),
  90. myVpnNet: myVpnNet,
  91. addrMap: make(map[iputil.VpnIp]*RemoteList),
  92. nebulaPort: nebulaPort,
  93. punchConn: pc,
  94. punchy: p,
  95. queryChan: make(chan iputil.VpnIp, c.GetUint32("handshakes.query_buffer", 64)),
  96. l: l,
  97. }
  98. lighthouses := make(map[iputil.VpnIp]struct{})
  99. h.lighthouses.Store(&lighthouses)
  100. staticList := make(map[iputil.VpnIp]struct{})
  101. h.staticList.Store(&staticList)
  102. if c.GetBool("stats.lighthouse_metrics", false) {
  103. h.metrics = newLighthouseMetrics()
  104. h.metricHolepunchTx = metrics.GetOrRegisterCounter("messages.tx.holepunch", nil)
  105. } else {
  106. h.metricHolepunchTx = metrics.NilCounter{}
  107. }
  108. err := h.reload(c, true)
  109. if err != nil {
  110. return nil, err
  111. }
  112. c.RegisterReloadCallback(func(c *config.C) {
  113. err := h.reload(c, false)
  114. switch v := err.(type) {
  115. case *util.ContextualError:
  116. v.Log(l)
  117. case error:
  118. l.WithError(err).Error("failed to reload lighthouse")
  119. }
  120. })
  121. h.startQueryWorker()
  122. return &h, nil
  123. }
  124. func (lh *LightHouse) GetStaticHostList() map[iputil.VpnIp]struct{} {
  125. return *lh.staticList.Load()
  126. }
  127. func (lh *LightHouse) GetLighthouses() map[iputil.VpnIp]struct{} {
  128. return *lh.lighthouses.Load()
  129. }
  130. func (lh *LightHouse) GetRemoteAllowList() *RemoteAllowList {
  131. return lh.remoteAllowList.Load()
  132. }
  133. func (lh *LightHouse) GetLocalAllowList() *LocalAllowList {
  134. return lh.localAllowList.Load()
  135. }
  136. func (lh *LightHouse) GetAdvertiseAddrs() []netIpAndPort {
  137. return *lh.advertiseAddrs.Load()
  138. }
  139. func (lh *LightHouse) GetRelaysForMe() []iputil.VpnIp {
  140. return *lh.relaysForMe.Load()
  141. }
  142. func (lh *LightHouse) getCalculatedRemotes() *cidr.Tree4[[]*calculatedRemote] {
  143. return lh.calculatedRemotes.Load()
  144. }
  145. func (lh *LightHouse) GetUpdateInterval() int64 {
  146. return lh.interval.Load()
  147. }
  148. func (lh *LightHouse) reload(c *config.C, initial bool) error {
  149. if initial || c.HasChanged("lighthouse.advertise_addrs") {
  150. rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
  151. advAddrs := make([]netIpAndPort, 0)
  152. for i, rawAddr := range rawAdvAddrs {
  153. fIp, fPort, err := udp.ParseIPAndPort(rawAddr)
  154. if err != nil {
  155. return util.NewContextualError("Unable to parse lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err)
  156. }
  157. if fPort == 0 {
  158. fPort = uint16(lh.nebulaPort)
  159. }
  160. if ip4 := fIp.To4(); ip4 != nil && lh.myVpnNet.Contains(fIp) {
  161. lh.l.WithField("addr", rawAddr).WithField("entry", i+1).
  162. Warn("Ignoring lighthouse.advertise_addrs report because it is within the nebula network range")
  163. continue
  164. }
  165. advAddrs = append(advAddrs, netIpAndPort{ip: fIp, port: fPort})
  166. }
  167. lh.advertiseAddrs.Store(&advAddrs)
  168. if !initial {
  169. lh.l.Info("lighthouse.advertise_addrs has changed")
  170. }
  171. }
  172. if initial || c.HasChanged("lighthouse.interval") {
  173. lh.interval.Store(int64(c.GetInt("lighthouse.interval", 10)))
  174. if !initial {
  175. lh.l.Infof("lighthouse.interval changed to %v", lh.interval.Load())
  176. if lh.updateCancel != nil {
  177. // May not always have a running routine
  178. lh.updateCancel()
  179. }
  180. lh.StartUpdateWorker()
  181. }
  182. }
  183. if initial || c.HasChanged("lighthouse.remote_allow_list") || c.HasChanged("lighthouse.remote_allow_ranges") {
  184. ral, err := NewRemoteAllowListFromConfig(c, "lighthouse.remote_allow_list", "lighthouse.remote_allow_ranges")
  185. if err != nil {
  186. return util.NewContextualError("Invalid lighthouse.remote_allow_list", nil, err)
  187. }
  188. lh.remoteAllowList.Store(ral)
  189. if !initial {
  190. //TODO: a diff will be annoyingly difficult
  191. lh.l.Info("lighthouse.remote_allow_list and/or lighthouse.remote_allow_ranges has changed")
  192. }
  193. }
  194. if initial || c.HasChanged("lighthouse.local_allow_list") {
  195. lal, err := NewLocalAllowListFromConfig(c, "lighthouse.local_allow_list")
  196. if err != nil {
  197. return util.NewContextualError("Invalid lighthouse.local_allow_list", nil, err)
  198. }
  199. lh.localAllowList.Store(lal)
  200. if !initial {
  201. //TODO: a diff will be annoyingly difficult
  202. lh.l.Info("lighthouse.local_allow_list has changed")
  203. }
  204. }
  205. if initial || c.HasChanged("lighthouse.calculated_remotes") {
  206. cr, err := NewCalculatedRemotesFromConfig(c, "lighthouse.calculated_remotes")
  207. if err != nil {
  208. return util.NewContextualError("Invalid lighthouse.calculated_remotes", nil, err)
  209. }
  210. lh.calculatedRemotes.Store(cr)
  211. if !initial {
  212. //TODO: a diff will be annoyingly difficult
  213. lh.l.Info("lighthouse.calculated_remotes has changed")
  214. }
  215. }
  216. //NOTE: many things will get much simpler when we combine static_host_map and lighthouse.hosts in config
  217. if initial || c.HasChanged("static_host_map") || c.HasChanged("static_map.cadence") || c.HasChanged("static_map.network") || c.HasChanged("static_map.lookup_timeout") {
  218. // Clean up. Entries still in the static_host_map will be re-built.
  219. // Entries no longer present must have their (possible) background DNS goroutines stopped.
  220. if existingStaticList := lh.staticList.Load(); existingStaticList != nil {
  221. lh.RLock()
  222. for staticVpnIp := range *existingStaticList {
  223. if am, ok := lh.addrMap[staticVpnIp]; ok && am != nil {
  224. am.hr.Cancel()
  225. }
  226. }
  227. lh.RUnlock()
  228. }
  229. // Build a new list based on current config.
  230. staticList := make(map[iputil.VpnIp]struct{})
  231. err := lh.loadStaticMap(c, lh.myVpnNet, staticList)
  232. if err != nil {
  233. return err
  234. }
  235. lh.staticList.Store(&staticList)
  236. if !initial {
  237. //TODO: we should remove any remote list entries for static hosts that were removed/modified?
  238. if c.HasChanged("static_host_map") {
  239. lh.l.Info("static_host_map has changed")
  240. }
  241. if c.HasChanged("static_map.cadence") {
  242. lh.l.Info("static_map.cadence has changed")
  243. }
  244. if c.HasChanged("static_map.network") {
  245. lh.l.Info("static_map.network has changed")
  246. }
  247. if c.HasChanged("static_map.lookup_timeout") {
  248. lh.l.Info("static_map.lookup_timeout has changed")
  249. }
  250. }
  251. }
  252. if initial || c.HasChanged("lighthouse.hosts") {
  253. lhMap := make(map[iputil.VpnIp]struct{})
  254. err := lh.parseLighthouses(c, lh.myVpnNet, lhMap)
  255. if err != nil {
  256. return err
  257. }
  258. lh.lighthouses.Store(&lhMap)
  259. if !initial {
  260. //NOTE: we are not tearing down existing lighthouse connections because they might be used for non lighthouse traffic
  261. lh.l.Info("lighthouse.hosts has changed")
  262. }
  263. }
  264. if initial || c.HasChanged("relay.relays") {
  265. switch c.GetBool("relay.am_relay", false) {
  266. case true:
  267. // Relays aren't allowed to specify other relays
  268. if len(c.GetStringSlice("relay.relays", nil)) > 0 {
  269. lh.l.Info("Ignoring relays from config because am_relay is true")
  270. }
  271. relaysForMe := []iputil.VpnIp{}
  272. lh.relaysForMe.Store(&relaysForMe)
  273. case false:
  274. relaysForMe := []iputil.VpnIp{}
  275. for _, v := range c.GetStringSlice("relay.relays", nil) {
  276. lh.l.WithField("relay", v).Info("Read relay from config")
  277. configRIP := net.ParseIP(v)
  278. if configRIP != nil {
  279. relaysForMe = append(relaysForMe, iputil.Ip2VpnIp(configRIP))
  280. }
  281. }
  282. lh.relaysForMe.Store(&relaysForMe)
  283. }
  284. }
  285. return nil
  286. }
  287. func (lh *LightHouse) parseLighthouses(c *config.C, tunCidr *net.IPNet, lhMap map[iputil.VpnIp]struct{}) error {
  288. lhs := c.GetStringSlice("lighthouse.hosts", []string{})
  289. if lh.amLighthouse && len(lhs) != 0 {
  290. lh.l.Warn("lighthouse.am_lighthouse enabled on node but upstream lighthouses exist in config")
  291. }
  292. for i, host := range lhs {
  293. ip := net.ParseIP(host)
  294. if ip == nil {
  295. return util.NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, nil)
  296. }
  297. if !tunCidr.Contains(ip) {
  298. return util.NewContextualError("lighthouse host is not in our subnet, invalid", m{"vpnIp": ip, "network": tunCidr.String()}, nil)
  299. }
  300. lhMap[iputil.Ip2VpnIp(ip)] = struct{}{}
  301. }
  302. if !lh.amLighthouse && len(lhMap) == 0 {
  303. lh.l.Warn("No lighthouse.hosts configured, this host will only be able to initiate tunnels with static_host_map entries")
  304. }
  305. staticList := lh.GetStaticHostList()
  306. for lhIP, _ := range lhMap {
  307. if _, ok := staticList[lhIP]; !ok {
  308. return fmt.Errorf("lighthouse %s does not have a static_host_map entry", lhIP)
  309. }
  310. }
  311. return nil
  312. }
  313. func getStaticMapCadence(c *config.C) (time.Duration, error) {
  314. cadence := c.GetString("static_map.cadence", "30s")
  315. d, err := time.ParseDuration(cadence)
  316. if err != nil {
  317. return 0, err
  318. }
  319. return d, nil
  320. }
  321. func getStaticMapLookupTimeout(c *config.C) (time.Duration, error) {
  322. lookupTimeout := c.GetString("static_map.lookup_timeout", "250ms")
  323. d, err := time.ParseDuration(lookupTimeout)
  324. if err != nil {
  325. return 0, err
  326. }
  327. return d, nil
  328. }
  329. func getStaticMapNetwork(c *config.C) (string, error) {
  330. network := c.GetString("static_map.network", "ip4")
  331. if network != "ip" && network != "ip4" && network != "ip6" {
  332. return "", fmt.Errorf("static_map.network must be one of ip, ip4, or ip6")
  333. }
  334. return network, nil
  335. }
  336. func (lh *LightHouse) loadStaticMap(c *config.C, tunCidr *net.IPNet, staticList map[iputil.VpnIp]struct{}) error {
  337. d, err := getStaticMapCadence(c)
  338. if err != nil {
  339. return err
  340. }
  341. network, err := getStaticMapNetwork(c)
  342. if err != nil {
  343. return err
  344. }
  345. lookup_timeout, err := getStaticMapLookupTimeout(c)
  346. if err != nil {
  347. return err
  348. }
  349. shm := c.GetMap("static_host_map", map[interface{}]interface{}{})
  350. i := 0
  351. for k, v := range shm {
  352. rip := net.ParseIP(fmt.Sprintf("%v", k))
  353. if rip == nil {
  354. return util.NewContextualError("Unable to parse static_host_map entry", m{"host": k, "entry": i + 1}, nil)
  355. }
  356. if !tunCidr.Contains(rip) {
  357. return util.NewContextualError("static_host_map key is not in our subnet, invalid", m{"vpnIp": rip, "network": tunCidr.String(), "entry": i + 1}, nil)
  358. }
  359. vpnIp := iputil.Ip2VpnIp(rip)
  360. vals, ok := v.([]interface{})
  361. if !ok {
  362. vals = []interface{}{v}
  363. }
  364. remoteAddrs := []string{}
  365. for _, v := range vals {
  366. remoteAddrs = append(remoteAddrs, fmt.Sprintf("%v", v))
  367. }
  368. err := lh.addStaticRemotes(i, d, network, lookup_timeout, vpnIp, remoteAddrs, staticList)
  369. if err != nil {
  370. return err
  371. }
  372. i++
  373. }
  374. return nil
  375. }
  376. func (lh *LightHouse) Query(ip iputil.VpnIp) *RemoteList {
  377. if !lh.IsLighthouseIP(ip) {
  378. lh.QueryServer(ip)
  379. }
  380. lh.RLock()
  381. if v, ok := lh.addrMap[ip]; ok {
  382. lh.RUnlock()
  383. return v
  384. }
  385. lh.RUnlock()
  386. return nil
  387. }
  388. // QueryServer is asynchronous so no reply should be expected
  389. func (lh *LightHouse) QueryServer(ip iputil.VpnIp) {
  390. // Don't put lighthouse ips in the query channel because we can't query lighthouses about lighthouses
  391. if lh.amLighthouse || lh.IsLighthouseIP(ip) {
  392. return
  393. }
  394. lh.queryChan <- ip
  395. }
  396. func (lh *LightHouse) QueryCache(ip iputil.VpnIp) *RemoteList {
  397. lh.RLock()
  398. if v, ok := lh.addrMap[ip]; ok {
  399. lh.RUnlock()
  400. return v
  401. }
  402. lh.RUnlock()
  403. lh.Lock()
  404. defer lh.Unlock()
  405. // Add an entry if we don't already have one
  406. return lh.unlockedGetRemoteList(ip)
  407. }
  408. // queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing
  409. // details from the remote list. It looks for a hit in the addrMap and a hit in the RemoteList under the owner vpnIp
  410. // If one is found then f() is called with proper locking, f() must return result of n.MarshalTo()
  411. func (lh *LightHouse) queryAndPrepMessage(vpnIp iputil.VpnIp, f func(*cache) (int, error)) (bool, int, error) {
  412. lh.RLock()
  413. // Do we have an entry in the main cache?
  414. if v, ok := lh.addrMap[vpnIp]; ok {
  415. // Swap lh lock for remote list lock
  416. v.RLock()
  417. defer v.RUnlock()
  418. lh.RUnlock()
  419. // vpnIp should also be the owner here since we are a lighthouse.
  420. c := v.cache[vpnIp]
  421. // Make sure we have
  422. if c != nil {
  423. n, err := f(c)
  424. return true, n, err
  425. }
  426. return false, 0, nil
  427. }
  428. lh.RUnlock()
  429. return false, 0, nil
  430. }
  431. func (lh *LightHouse) DeleteVpnIp(vpnIp iputil.VpnIp) {
  432. // First we check the static mapping
  433. // and do nothing if it is there
  434. if _, ok := lh.GetStaticHostList()[vpnIp]; ok {
  435. return
  436. }
  437. lh.Lock()
  438. //l.Debugln(lh.addrMap)
  439. delete(lh.addrMap, vpnIp)
  440. if lh.l.Level >= logrus.DebugLevel {
  441. lh.l.Debugf("deleting %s from lighthouse.", vpnIp)
  442. }
  443. lh.Unlock()
  444. }
  445. // AddStaticRemote adds a static host entry for vpnIp as ourselves as the owner
  446. // We are the owner because we don't want a lighthouse server to advertise for static hosts it was configured with
  447. // And we don't want a lighthouse query reply to interfere with our learned cache if we are a client
  448. // NOTE: this function should not interact with any hot path objects, like lh.staticList, the caller should handle it
  449. func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, timeout time.Duration, vpnIp iputil.VpnIp, toAddrs []string, staticList map[iputil.VpnIp]struct{}) error {
  450. lh.Lock()
  451. am := lh.unlockedGetRemoteList(vpnIp)
  452. am.Lock()
  453. defer am.Unlock()
  454. ctx := lh.ctx
  455. lh.Unlock()
  456. hr, err := NewHostnameResults(ctx, lh.l, d, network, timeout, toAddrs, func() {
  457. // This callback runs whenever the DNS hostname resolver finds a different set of IP's
  458. // in its resolution for hostnames.
  459. am.Lock()
  460. defer am.Unlock()
  461. am.shouldRebuild = true
  462. })
  463. if err != nil {
  464. return util.NewContextualError("Static host address could not be parsed", m{"vpnIp": vpnIp, "entry": i + 1}, err)
  465. }
  466. am.unlockedSetHostnamesResults(hr)
  467. for _, addrPort := range hr.GetIPs() {
  468. switch {
  469. case addrPort.Addr().Is4():
  470. to := NewIp4AndPortFromNetIP(addrPort.Addr(), addrPort.Port())
  471. if !lh.unlockedShouldAddV4(vpnIp, to) {
  472. continue
  473. }
  474. am.unlockedPrependV4(lh.myVpnIp, to)
  475. case addrPort.Addr().Is6():
  476. to := NewIp6AndPortFromNetIP(addrPort.Addr(), addrPort.Port())
  477. if !lh.unlockedShouldAddV6(vpnIp, to) {
  478. continue
  479. }
  480. am.unlockedPrependV6(lh.myVpnIp, to)
  481. }
  482. }
  483. // Mark it as static in the caller provided map
  484. staticList[vpnIp] = struct{}{}
  485. return nil
  486. }
  487. // addCalculatedRemotes adds any calculated remotes based on the
  488. // lighthouse.calculated_remotes configuration. It returns true if any
  489. // calculated remotes were added
  490. func (lh *LightHouse) addCalculatedRemotes(vpnIp iputil.VpnIp) bool {
  491. tree := lh.getCalculatedRemotes()
  492. if tree == nil {
  493. return false
  494. }
  495. ok, calculatedRemotes := tree.MostSpecificContains(vpnIp)
  496. if !ok {
  497. return false
  498. }
  499. var calculated []*Ip4AndPort
  500. for _, cr := range calculatedRemotes {
  501. c := cr.Apply(vpnIp)
  502. if c != nil {
  503. calculated = append(calculated, c)
  504. }
  505. }
  506. lh.Lock()
  507. am := lh.unlockedGetRemoteList(vpnIp)
  508. am.Lock()
  509. defer am.Unlock()
  510. lh.Unlock()
  511. am.unlockedSetV4(lh.myVpnIp, vpnIp, calculated, lh.unlockedShouldAddV4)
  512. return len(calculated) > 0
  513. }
  514. // unlockedGetRemoteList assumes you have the lh lock
  515. func (lh *LightHouse) unlockedGetRemoteList(vpnIp iputil.VpnIp) *RemoteList {
  516. am, ok := lh.addrMap[vpnIp]
  517. if !ok {
  518. am = NewRemoteList(func(a netip.Addr) bool { return lh.shouldAdd(vpnIp, a) })
  519. lh.addrMap[vpnIp] = am
  520. }
  521. return am
  522. }
  523. func (lh *LightHouse) shouldAdd(vpnIp iputil.VpnIp, to netip.Addr) bool {
  524. switch {
  525. case to.Is4():
  526. ipBytes := to.As4()
  527. ip := iputil.Ip2VpnIp(ipBytes[:])
  528. allow := lh.GetRemoteAllowList().AllowIpV4(vpnIp, ip)
  529. if lh.l.Level >= logrus.TraceLevel {
  530. lh.l.WithField("remoteIp", vpnIp).WithField("allow", allow).Trace("remoteAllowList.Allow")
  531. }
  532. if !allow || ipMaskContains(lh.myVpnIp, lh.myVpnZeros, ip) {
  533. return false
  534. }
  535. case to.Is6():
  536. ipBytes := to.As16()
  537. hi := binary.BigEndian.Uint64(ipBytes[:8])
  538. lo := binary.BigEndian.Uint64(ipBytes[8:])
  539. allow := lh.GetRemoteAllowList().AllowIpV6(vpnIp, hi, lo)
  540. if lh.l.Level >= logrus.TraceLevel {
  541. lh.l.WithField("remoteIp", to).WithField("allow", allow).Trace("remoteAllowList.Allow")
  542. }
  543. // We don't check our vpn network here because nebula does not support ipv6 on the inside
  544. if !allow {
  545. return false
  546. }
  547. }
  548. return true
  549. }
  550. // unlockedShouldAddV4 checks if to is allowed by our allow list
  551. func (lh *LightHouse) unlockedShouldAddV4(vpnIp iputil.VpnIp, to *Ip4AndPort) bool {
  552. allow := lh.GetRemoteAllowList().AllowIpV4(vpnIp, iputil.VpnIp(to.Ip))
  553. if lh.l.Level >= logrus.TraceLevel {
  554. lh.l.WithField("remoteIp", vpnIp).WithField("allow", allow).Trace("remoteAllowList.Allow")
  555. }
  556. if !allow || ipMaskContains(lh.myVpnIp, lh.myVpnZeros, iputil.VpnIp(to.Ip)) {
  557. return false
  558. }
  559. return true
  560. }
  561. // unlockedShouldAddV6 checks if to is allowed by our allow list
  562. func (lh *LightHouse) unlockedShouldAddV6(vpnIp iputil.VpnIp, to *Ip6AndPort) bool {
  563. allow := lh.GetRemoteAllowList().AllowIpV6(vpnIp, to.Hi, to.Lo)
  564. if lh.l.Level >= logrus.TraceLevel {
  565. lh.l.WithField("remoteIp", lhIp6ToIp(to)).WithField("allow", allow).Trace("remoteAllowList.Allow")
  566. }
  567. // We don't check our vpn network here because nebula does not support ipv6 on the inside
  568. if !allow {
  569. return false
  570. }
  571. return true
  572. }
  573. func lhIp6ToIp(v *Ip6AndPort) net.IP {
  574. ip := make(net.IP, 16)
  575. binary.BigEndian.PutUint64(ip[:8], v.Hi)
  576. binary.BigEndian.PutUint64(ip[8:], v.Lo)
  577. return ip
  578. }
  579. func (lh *LightHouse) IsLighthouseIP(vpnIp iputil.VpnIp) bool {
  580. if _, ok := lh.GetLighthouses()[vpnIp]; ok {
  581. return true
  582. }
  583. return false
  584. }
  585. func NewLhQueryByInt(VpnIp iputil.VpnIp) *NebulaMeta {
  586. return &NebulaMeta{
  587. Type: NebulaMeta_HostQuery,
  588. Details: &NebulaMetaDetails{
  589. VpnIp: uint32(VpnIp),
  590. },
  591. }
  592. }
  593. func NewIp4AndPort(ip net.IP, port uint32) *Ip4AndPort {
  594. ipp := Ip4AndPort{Port: port}
  595. ipp.Ip = uint32(iputil.Ip2VpnIp(ip))
  596. return &ipp
  597. }
  598. func NewIp4AndPortFromNetIP(ip netip.Addr, port uint16) *Ip4AndPort {
  599. v4Addr := ip.As4()
  600. return &Ip4AndPort{
  601. Ip: binary.BigEndian.Uint32(v4Addr[:]),
  602. Port: uint32(port),
  603. }
  604. }
  605. func NewIp6AndPort(ip net.IP, port uint32) *Ip6AndPort {
  606. return &Ip6AndPort{
  607. Hi: binary.BigEndian.Uint64(ip[:8]),
  608. Lo: binary.BigEndian.Uint64(ip[8:]),
  609. Port: port,
  610. }
  611. }
  612. func NewIp6AndPortFromNetIP(ip netip.Addr, port uint16) *Ip6AndPort {
  613. ip6Addr := ip.As16()
  614. return &Ip6AndPort{
  615. Hi: binary.BigEndian.Uint64(ip6Addr[:8]),
  616. Lo: binary.BigEndian.Uint64(ip6Addr[8:]),
  617. Port: uint32(port),
  618. }
  619. }
  620. func NewUDPAddrFromLH4(ipp *Ip4AndPort) *udp.Addr {
  621. ip := ipp.Ip
  622. return udp.NewAddr(
  623. net.IPv4(byte(ip&0xff000000>>24), byte(ip&0x00ff0000>>16), byte(ip&0x0000ff00>>8), byte(ip&0x000000ff)),
  624. uint16(ipp.Port),
  625. )
  626. }
  627. func NewUDPAddrFromLH6(ipp *Ip6AndPort) *udp.Addr {
  628. return udp.NewAddr(lhIp6ToIp(ipp), uint16(ipp.Port))
  629. }
  630. func (lh *LightHouse) startQueryWorker() {
  631. if lh.amLighthouse {
  632. return
  633. }
  634. go func() {
  635. nb := make([]byte, 12, 12)
  636. out := make([]byte, mtu)
  637. for {
  638. select {
  639. case <-lh.ctx.Done():
  640. return
  641. case ip := <-lh.queryChan:
  642. lh.innerQueryServer(ip, nb, out)
  643. }
  644. }
  645. }()
  646. }
  647. func (lh *LightHouse) innerQueryServer(ip iputil.VpnIp, nb, out []byte) {
  648. if lh.IsLighthouseIP(ip) {
  649. return
  650. }
  651. // Send a query to the lighthouses and hope for the best next time
  652. query, err := NewLhQueryByInt(ip).Marshal()
  653. if err != nil {
  654. lh.l.WithError(err).WithField("vpnIp", ip).Error("Failed to marshal lighthouse query payload")
  655. return
  656. }
  657. lighthouses := lh.GetLighthouses()
  658. lh.metricTx(NebulaMeta_HostQuery, int64(len(lighthouses)))
  659. for n := range lighthouses {
  660. lh.ifce.SendMessageToVpnIp(header.LightHouse, 0, n, query, nb, out)
  661. }
  662. }
  663. func (lh *LightHouse) StartUpdateWorker() {
  664. interval := lh.GetUpdateInterval()
  665. if lh.amLighthouse || interval == 0 {
  666. return
  667. }
  668. clockSource := time.NewTicker(time.Second * time.Duration(interval))
  669. updateCtx, cancel := context.WithCancel(lh.ctx)
  670. lh.updateCancel = cancel
  671. go func() {
  672. defer clockSource.Stop()
  673. for {
  674. lh.SendUpdate()
  675. select {
  676. case <-updateCtx.Done():
  677. return
  678. case <-clockSource.C:
  679. continue
  680. }
  681. }
  682. }()
  683. }
  684. func (lh *LightHouse) SendUpdate() {
  685. var v4 []*Ip4AndPort
  686. var v6 []*Ip6AndPort
  687. for _, e := range lh.GetAdvertiseAddrs() {
  688. if ip := e.ip.To4(); ip != nil {
  689. v4 = append(v4, NewIp4AndPort(e.ip, uint32(e.port)))
  690. } else {
  691. v6 = append(v6, NewIp6AndPort(e.ip, uint32(e.port)))
  692. }
  693. }
  694. lal := lh.GetLocalAllowList()
  695. for _, e := range *localIps(lh.l, lal) {
  696. if ip4 := e.To4(); ip4 != nil && ipMaskContains(lh.myVpnIp, lh.myVpnZeros, iputil.Ip2VpnIp(ip4)) {
  697. continue
  698. }
  699. // Only add IPs that aren't my VPN/tun IP
  700. if ip := e.To4(); ip != nil {
  701. v4 = append(v4, NewIp4AndPort(e, lh.nebulaPort))
  702. } else {
  703. v6 = append(v6, NewIp6AndPort(e, lh.nebulaPort))
  704. }
  705. }
  706. var relays []uint32
  707. for _, r := range lh.GetRelaysForMe() {
  708. relays = append(relays, (uint32)(r))
  709. }
  710. m := &NebulaMeta{
  711. Type: NebulaMeta_HostUpdateNotification,
  712. Details: &NebulaMetaDetails{
  713. VpnIp: uint32(lh.myVpnIp),
  714. Ip4AndPorts: v4,
  715. Ip6AndPorts: v6,
  716. RelayVpnIp: relays,
  717. },
  718. }
  719. lighthouses := lh.GetLighthouses()
  720. lh.metricTx(NebulaMeta_HostUpdateNotification, int64(len(lighthouses)))
  721. nb := make([]byte, 12, 12)
  722. out := make([]byte, mtu)
  723. mm, err := m.Marshal()
  724. if err != nil {
  725. lh.l.WithError(err).Error("Error while marshaling for lighthouse update")
  726. return
  727. }
  728. for vpnIp := range lighthouses {
  729. lh.ifce.SendMessageToVpnIp(header.LightHouse, 0, vpnIp, mm, nb, out)
  730. }
  731. }
  732. type LightHouseHandler struct {
  733. lh *LightHouse
  734. nb []byte
  735. out []byte
  736. pb []byte
  737. meta *NebulaMeta
  738. l *logrus.Logger
  739. }
  740. func (lh *LightHouse) NewRequestHandler() *LightHouseHandler {
  741. lhh := &LightHouseHandler{
  742. lh: lh,
  743. nb: make([]byte, 12, 12),
  744. out: make([]byte, mtu),
  745. l: lh.l,
  746. pb: make([]byte, mtu),
  747. meta: &NebulaMeta{
  748. Details: &NebulaMetaDetails{},
  749. },
  750. }
  751. return lhh
  752. }
  753. func (lh *LightHouse) metricRx(t NebulaMeta_MessageType, i int64) {
  754. lh.metrics.Rx(header.MessageType(t), 0, i)
  755. }
  756. func (lh *LightHouse) metricTx(t NebulaMeta_MessageType, i int64) {
  757. lh.metrics.Tx(header.MessageType(t), 0, i)
  758. }
  759. // This method is similar to Reset(), but it re-uses the pointer structs
  760. // so that we don't have to re-allocate them
  761. func (lhh *LightHouseHandler) resetMeta() *NebulaMeta {
  762. details := lhh.meta.Details
  763. lhh.meta.Reset()
  764. // Keep the array memory around
  765. details.Ip4AndPorts = details.Ip4AndPorts[:0]
  766. details.Ip6AndPorts = details.Ip6AndPorts[:0]
  767. details.RelayVpnIp = details.RelayVpnIp[:0]
  768. lhh.meta.Details = details
  769. return lhh.meta
  770. }
  771. func lhHandleRequest(lhh *LightHouseHandler, f *Interface) udp.LightHouseHandlerFunc {
  772. return func(rAddr *udp.Addr, vpnIp iputil.VpnIp, p []byte) {
  773. lhh.HandleRequest(rAddr, vpnIp, p, f)
  774. }
  775. }
  776. func (lhh *LightHouseHandler) HandleRequest(rAddr *udp.Addr, vpnIp iputil.VpnIp, p []byte, w EncWriter) {
  777. n := lhh.resetMeta()
  778. err := n.Unmarshal(p)
  779. if err != nil {
  780. lhh.l.WithError(err).WithField("vpnIp", vpnIp).WithField("udpAddr", rAddr).
  781. Error("Failed to unmarshal lighthouse packet")
  782. //TODO: send recv_error?
  783. return
  784. }
  785. if n.Details == nil {
  786. lhh.l.WithField("vpnIp", vpnIp).WithField("udpAddr", rAddr).
  787. Error("Invalid lighthouse update")
  788. //TODO: send recv_error?
  789. return
  790. }
  791. lhh.lh.metricRx(n.Type, 1)
  792. switch n.Type {
  793. case NebulaMeta_HostQuery:
  794. lhh.handleHostQuery(n, vpnIp, rAddr, w)
  795. case NebulaMeta_HostQueryReply:
  796. lhh.handleHostQueryReply(n, vpnIp)
  797. case NebulaMeta_HostUpdateNotification:
  798. lhh.handleHostUpdateNotification(n, vpnIp, w)
  799. case NebulaMeta_HostMovedNotification:
  800. case NebulaMeta_HostPunchNotification:
  801. lhh.handleHostPunchNotification(n, vpnIp, w)
  802. case NebulaMeta_HostUpdateNotificationAck:
  803. // noop
  804. }
  805. }
  806. func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, vpnIp iputil.VpnIp, addr *udp.Addr, w EncWriter) {
  807. // Exit if we don't answer queries
  808. if !lhh.lh.amLighthouse {
  809. if lhh.l.Level >= logrus.DebugLevel {
  810. lhh.l.Debugln("I don't answer queries, but received from: ", addr)
  811. }
  812. return
  813. }
  814. //TODO: we can DRY this further
  815. reqVpnIp := n.Details.VpnIp
  816. //TODO: Maybe instead of marshalling into n we marshal into a new `r` to not nuke our current request data
  817. found, ln, err := lhh.lh.queryAndPrepMessage(iputil.VpnIp(n.Details.VpnIp), func(c *cache) (int, error) {
  818. n = lhh.resetMeta()
  819. n.Type = NebulaMeta_HostQueryReply
  820. n.Details.VpnIp = reqVpnIp
  821. lhh.coalesceAnswers(c, n)
  822. return n.MarshalTo(lhh.pb)
  823. })
  824. if !found {
  825. return
  826. }
  827. if err != nil {
  828. lhh.l.WithError(err).WithField("vpnIp", vpnIp).Error("Failed to marshal lighthouse host query reply")
  829. return
  830. }
  831. lhh.lh.metricTx(NebulaMeta_HostQueryReply, 1)
  832. w.SendMessageToVpnIp(header.LightHouse, 0, vpnIp, lhh.pb[:ln], lhh.nb, lhh.out[:0])
  833. // This signals the other side to punch some zero byte udp packets
  834. found, ln, err = lhh.lh.queryAndPrepMessage(vpnIp, func(c *cache) (int, error) {
  835. n = lhh.resetMeta()
  836. n.Type = NebulaMeta_HostPunchNotification
  837. n.Details.VpnIp = uint32(vpnIp)
  838. lhh.coalesceAnswers(c, n)
  839. return n.MarshalTo(lhh.pb)
  840. })
  841. if !found {
  842. return
  843. }
  844. if err != nil {
  845. lhh.l.WithError(err).WithField("vpnIp", vpnIp).Error("Failed to marshal lighthouse host was queried for")
  846. return
  847. }
  848. lhh.lh.metricTx(NebulaMeta_HostPunchNotification, 1)
  849. w.SendMessageToVpnIp(header.LightHouse, 0, iputil.VpnIp(reqVpnIp), lhh.pb[:ln], lhh.nb, lhh.out[:0])
  850. }
  851. func (lhh *LightHouseHandler) coalesceAnswers(c *cache, n *NebulaMeta) {
  852. if c.v4 != nil {
  853. if c.v4.learned != nil {
  854. n.Details.Ip4AndPorts = append(n.Details.Ip4AndPorts, c.v4.learned)
  855. }
  856. if c.v4.reported != nil && len(c.v4.reported) > 0 {
  857. n.Details.Ip4AndPorts = append(n.Details.Ip4AndPorts, c.v4.reported...)
  858. }
  859. }
  860. if c.v6 != nil {
  861. if c.v6.learned != nil {
  862. n.Details.Ip6AndPorts = append(n.Details.Ip6AndPorts, c.v6.learned)
  863. }
  864. if c.v6.reported != nil && len(c.v6.reported) > 0 {
  865. n.Details.Ip6AndPorts = append(n.Details.Ip6AndPorts, c.v6.reported...)
  866. }
  867. }
  868. if c.relay != nil {
  869. n.Details.RelayVpnIp = append(n.Details.RelayVpnIp, c.relay.relay...)
  870. }
  871. }
  872. func (lhh *LightHouseHandler) handleHostQueryReply(n *NebulaMeta, vpnIp iputil.VpnIp) {
  873. if !lhh.lh.IsLighthouseIP(vpnIp) {
  874. return
  875. }
  876. lhh.lh.Lock()
  877. am := lhh.lh.unlockedGetRemoteList(iputil.VpnIp(n.Details.VpnIp))
  878. am.Lock()
  879. lhh.lh.Unlock()
  880. certVpnIp := iputil.VpnIp(n.Details.VpnIp)
  881. am.unlockedSetV4(vpnIp, certVpnIp, n.Details.Ip4AndPorts, lhh.lh.unlockedShouldAddV4)
  882. am.unlockedSetV6(vpnIp, certVpnIp, n.Details.Ip6AndPorts, lhh.lh.unlockedShouldAddV6)
  883. am.unlockedSetRelay(vpnIp, certVpnIp, n.Details.RelayVpnIp)
  884. am.Unlock()
  885. // Non-blocking attempt to trigger, skip if it would block
  886. select {
  887. case lhh.lh.handshakeTrigger <- iputil.VpnIp(n.Details.VpnIp):
  888. default:
  889. }
  890. }
  891. func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, vpnIp iputil.VpnIp, w EncWriter) {
  892. if !lhh.lh.amLighthouse {
  893. if lhh.l.Level >= logrus.DebugLevel {
  894. lhh.l.Debugln("I am not a lighthouse, do not take host updates: ", vpnIp)
  895. }
  896. return
  897. }
  898. //Simple check that the host sent this not someone else
  899. if n.Details.VpnIp != uint32(vpnIp) {
  900. if lhh.l.Level >= logrus.DebugLevel {
  901. lhh.l.WithField("vpnIp", vpnIp).WithField("answer", iputil.VpnIp(n.Details.VpnIp)).Debugln("Host sent invalid update")
  902. }
  903. return
  904. }
  905. lhh.lh.Lock()
  906. am := lhh.lh.unlockedGetRemoteList(vpnIp)
  907. am.Lock()
  908. lhh.lh.Unlock()
  909. certVpnIp := iputil.VpnIp(n.Details.VpnIp)
  910. am.unlockedSetV4(vpnIp, certVpnIp, n.Details.Ip4AndPorts, lhh.lh.unlockedShouldAddV4)
  911. am.unlockedSetV6(vpnIp, certVpnIp, n.Details.Ip6AndPorts, lhh.lh.unlockedShouldAddV6)
  912. am.unlockedSetRelay(vpnIp, certVpnIp, n.Details.RelayVpnIp)
  913. am.Unlock()
  914. n = lhh.resetMeta()
  915. n.Type = NebulaMeta_HostUpdateNotificationAck
  916. n.Details.VpnIp = uint32(vpnIp)
  917. ln, err := n.MarshalTo(lhh.pb)
  918. if err != nil {
  919. lhh.l.WithError(err).WithField("vpnIp", vpnIp).Error("Failed to marshal lighthouse host update ack")
  920. return
  921. }
  922. lhh.lh.metricTx(NebulaMeta_HostUpdateNotificationAck, 1)
  923. w.SendMessageToVpnIp(header.LightHouse, 0, vpnIp, lhh.pb[:ln], lhh.nb, lhh.out[:0])
  924. }
  925. func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, vpnIp iputil.VpnIp, w EncWriter) {
  926. if !lhh.lh.IsLighthouseIP(vpnIp) {
  927. return
  928. }
  929. empty := []byte{0}
  930. punch := func(vpnPeer *udp.Addr) {
  931. if vpnPeer == nil {
  932. return
  933. }
  934. go func() {
  935. time.Sleep(lhh.lh.punchy.GetDelay())
  936. lhh.lh.metricHolepunchTx.Inc(1)
  937. lhh.lh.punchConn.WriteTo(empty, vpnPeer)
  938. }()
  939. if lhh.l.Level >= logrus.DebugLevel {
  940. //TODO: lacking the ip we are actually punching on, old: l.Debugf("Punching %s on %d for %s", IntIp(a.Ip), a.Port, IntIp(n.Details.VpnIp))
  941. lhh.l.Debugf("Punching on %d for %s", vpnPeer.Port, iputil.VpnIp(n.Details.VpnIp))
  942. }
  943. }
  944. for _, a := range n.Details.Ip4AndPorts {
  945. punch(NewUDPAddrFromLH4(a))
  946. }
  947. for _, a := range n.Details.Ip6AndPorts {
  948. punch(NewUDPAddrFromLH6(a))
  949. }
  950. // This sends a nebula test packet to the host trying to contact us. In the case
  951. // of a double nat or other difficult scenario, this may help establish
  952. // a tunnel.
  953. if lhh.lh.punchy.GetRespond() {
  954. queryVpnIp := iputil.VpnIp(n.Details.VpnIp)
  955. go func() {
  956. time.Sleep(lhh.lh.punchy.GetRespondDelay())
  957. if lhh.l.Level >= logrus.DebugLevel {
  958. lhh.l.Debugf("Sending a nebula test packet to vpn ip %s", queryVpnIp)
  959. }
  960. //NOTE: we have to allocate a new output buffer here since we are spawning a new goroutine
  961. // for each punchBack packet. We should move this into a timerwheel or a single goroutine
  962. // managed by a channel.
  963. w.SendMessageToVpnIp(header.Test, header.TestRequest, queryVpnIp, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  964. }()
  965. }
  966. }
  967. // ipMaskContains checks if testIp is contained by ip after applying a cidr
  968. // zeros is 32 - bits from net.IPMask.Size()
  969. func ipMaskContains(ip iputil.VpnIp, zeros iputil.VpnIp, testIp iputil.VpnIp) bool {
  970. return (testIp^ip)>>zeros == 0
  971. }