lighthouse.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "log/slog"
  8. "net"
  9. "net/netip"
  10. "slices"
  11. "strconv"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "github.com/gaissmai/bart"
  16. "github.com/slackhq/nebula/cert"
  17. "github.com/slackhq/nebula/config"
  18. "github.com/slackhq/nebula/header"
  19. "github.com/slackhq/nebula/logging"
  20. "github.com/slackhq/nebula/udp"
  21. "github.com/slackhq/nebula/util"
  22. )
  23. var ErrHostNotKnown = errors.New("host not known")
  24. var ErrBadDetailsVpnAddr = errors.New("invalid packet, malformed detailsVpnAddr")
  25. type LightHouse struct {
  26. //TODO: We need a timer wheel to kick out vpnAddrs that haven't reported in a long time
  27. sync.RWMutex //Because we concurrently read and write to our maps
  28. ctx context.Context
  29. amLighthouse bool
  30. myVpnNetworks []netip.Prefix
  31. myVpnNetworksTable *bart.Lite
  32. punchy *Punchy
  33. // localAddrsFn enumerates the underlay addresses we advertise. It is a field so tests can supply simulated
  34. // addresses rather than whatever this machine's NICs happen to be. Set it before Start.
  35. localAddrsFn func(*LocalAllowList) []netip.Addr
  36. // Local cache of answers from light houses
  37. // map of vpn addr to answers
  38. addrMap map[netip.Addr]*RemoteList
  39. // filters remote addresses allowed for each host
  40. // - When we are a lighthouse, this filters what addresses we store and
  41. // respond with.
  42. // - When we are not a lighthouse, this filters which addresses we accept
  43. // from lighthouses.
  44. remoteAllowList atomic.Pointer[RemoteAllowList]
  45. // filters local addresses that we advertise to lighthouses
  46. localAllowList atomic.Pointer[LocalAllowList]
  47. // used to trigger the HandshakeManager when we receive HostQueryReply
  48. handshakeTrigger chan<- netip.Addr
  49. // staticList exists to avoid having a bool in each addrMap entry
  50. // since static should be rare
  51. staticList atomic.Pointer[map[netip.Addr]struct{}]
  52. lighthouses atomic.Pointer[[]netip.Addr]
  53. interval atomic.Int64
  54. updateCancel context.CancelFunc
  55. ifce EncWriter
  56. nebulaPort uint32 // 32 bits because protobuf does not have a uint16
  57. advertiseAddrs atomic.Pointer[[]netip.AddrPort]
  58. // Addr's of relays that can be used by peers to access me
  59. relaysForMe atomic.Pointer[[]netip.Addr]
  60. updateTrigger chan struct{}
  61. queryChan chan netip.Addr
  62. calculatedRemotes atomic.Pointer[bart.Table[[]*calculatedRemote]] // Maps VpnAddr to []*calculatedRemote
  63. metrics *MessageMetrics
  64. l *slog.Logger
  65. }
  66. // NewLightHouseFromConfig will build a Lighthouse struct from the values provided in the config object
  67. // addrMap should be nil unless this is during a config reload
  68. func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, cs *CertState, pc udp.Conn, p *Punchy) (*LightHouse, error) {
  69. amLighthouse := c.GetBool("lighthouse.am_lighthouse", false)
  70. nebulaPort := uint32(c.GetInt("listen.port", 0))
  71. if amLighthouse && nebulaPort == 0 {
  72. return nil, util.NewContextualError("lighthouse.am_lighthouse enabled on node but no port number is set in config", nil, nil)
  73. }
  74. // If port is dynamic, discover it
  75. if nebulaPort == 0 && pc != nil {
  76. uPort, err := pc.LocalAddr()
  77. if err != nil {
  78. return nil, util.NewContextualError("Failed to get listening port", nil, err)
  79. }
  80. nebulaPort = uint32(uPort.Port())
  81. }
  82. h := LightHouse{
  83. ctx: ctx,
  84. amLighthouse: amLighthouse,
  85. myVpnNetworks: cs.myVpnNetworks,
  86. myVpnNetworksTable: cs.myVpnNetworksTable,
  87. addrMap: make(map[netip.Addr]*RemoteList),
  88. nebulaPort: nebulaPort,
  89. punchy: p,
  90. updateTrigger: make(chan struct{}, 1),
  91. queryChan: make(chan netip.Addr, c.GetUint32("handshakes.query_buffer", 64)),
  92. l: l,
  93. }
  94. h.localAddrsFn = func(al *LocalAllowList) []netip.Addr {
  95. return localAddrs(h.l, al)
  96. }
  97. lighthouses := make([]netip.Addr, 0)
  98. h.lighthouses.Store(&lighthouses)
  99. staticList := make(map[netip.Addr]struct{})
  100. h.staticList.Store(&staticList)
  101. if c.GetBool("stats.lighthouse_metrics", false) {
  102. h.metrics = newLighthouseMetrics()
  103. }
  104. err := h.reload(c, true)
  105. if err != nil {
  106. return nil, err
  107. }
  108. c.RegisterReloadCallback(func(c *config.C) {
  109. err := h.reload(c, false)
  110. switch v := err.(type) {
  111. case *util.ContextualError:
  112. v.Log(l)
  113. case error:
  114. l.Error("failed to reload lighthouse", "error", err)
  115. }
  116. })
  117. h.startQueryWorker()
  118. return &h, nil
  119. }
  120. func (lh *LightHouse) GetStaticHostList() map[netip.Addr]struct{} {
  121. return *lh.staticList.Load()
  122. }
  123. func (lh *LightHouse) GetLighthouses() []netip.Addr {
  124. return *lh.lighthouses.Load()
  125. }
  126. func (lh *LightHouse) GetRemoteAllowList() *RemoteAllowList {
  127. return lh.remoteAllowList.Load()
  128. }
  129. func (lh *LightHouse) GetLocalAllowList() *LocalAllowList {
  130. return lh.localAllowList.Load()
  131. }
  132. func (lh *LightHouse) GetAdvertiseAddrs() []netip.AddrPort {
  133. return *lh.advertiseAddrs.Load()
  134. }
  135. func (lh *LightHouse) GetRelaysForMe() []netip.Addr {
  136. return *lh.relaysForMe.Load()
  137. }
  138. func (lh *LightHouse) getCalculatedRemotes() *bart.Table[[]*calculatedRemote] {
  139. return lh.calculatedRemotes.Load()
  140. }
  141. func (lh *LightHouse) GetUpdateInterval() int64 {
  142. return lh.interval.Load()
  143. }
  144. func (lh *LightHouse) reload(c *config.C, initial bool) error {
  145. if initial || c.HasChanged("lighthouse.advertise_addrs") {
  146. rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
  147. advAddrs := make([]netip.AddrPort, 0)
  148. for i, rawAddr := range rawAdvAddrs {
  149. host, sport, err := net.SplitHostPort(rawAddr)
  150. if err != nil {
  151. return util.NewContextualError("Unable to parse lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err)
  152. }
  153. addrs, err := net.DefaultResolver.LookupNetIP(context.Background(), "ip", host)
  154. if err != nil {
  155. return util.NewContextualError("Unable to lookup lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err)
  156. }
  157. if len(addrs) == 0 {
  158. return util.NewContextualError("Unable to lookup lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, nil)
  159. }
  160. port, err := strconv.Atoi(sport)
  161. if err != nil {
  162. return util.NewContextualError("Unable to parse port in lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err)
  163. }
  164. if port == 0 {
  165. port = int(lh.nebulaPort)
  166. }
  167. //TODO: we could technically insert all returned addrs instead of just the first one if a dns lookup was used
  168. addr := addrs[0].Unmap()
  169. if lh.myVpnNetworksTable.Contains(addr) {
  170. lh.l.Warn("Ignoring lighthouse.advertise_addrs report because it is within the nebula network range",
  171. "addr", rawAddr,
  172. "entry", i+1,
  173. )
  174. continue
  175. }
  176. advAddrs = append(advAddrs, netip.AddrPortFrom(addr, uint16(port)))
  177. }
  178. lh.advertiseAddrs.Store(&advAddrs)
  179. if !initial {
  180. lh.l.Info("lighthouse.advertise_addrs has changed")
  181. }
  182. }
  183. if initial || c.HasChanged("lighthouse.interval") {
  184. lh.interval.Store(int64(c.GetInt("lighthouse.interval", 10)))
  185. if !initial {
  186. lh.l.Info("lighthouse.interval changed",
  187. "interval", lh.interval.Load(),
  188. )
  189. if lh.updateCancel != nil {
  190. // May not always have a running routine
  191. lh.updateCancel()
  192. }
  193. lh.StartUpdateWorker()
  194. }
  195. }
  196. if initial || c.HasChanged("lighthouse.remote_allow_list") || c.HasChanged("lighthouse.remote_allow_ranges") {
  197. ral, err := NewRemoteAllowListFromConfig(c, "lighthouse.remote_allow_list", "lighthouse.remote_allow_ranges")
  198. if err != nil {
  199. return util.NewContextualError("Invalid lighthouse.remote_allow_list", nil, err)
  200. }
  201. lh.remoteAllowList.Store(ral)
  202. if !initial {
  203. lh.l.Info("lighthouse.remote_allow_list and/or lighthouse.remote_allow_ranges has changed")
  204. }
  205. }
  206. if initial || c.HasChanged("lighthouse.local_allow_list") {
  207. lal, err := NewLocalAllowListFromConfig(c, "lighthouse.local_allow_list")
  208. if err != nil {
  209. return util.NewContextualError("Invalid lighthouse.local_allow_list", nil, err)
  210. }
  211. lh.localAllowList.Store(lal)
  212. if !initial {
  213. lh.l.Info("lighthouse.local_allow_list has changed")
  214. }
  215. }
  216. if initial || c.HasChanged("lighthouse.calculated_remotes") {
  217. cr, err := NewCalculatedRemotesFromConfig(c, "lighthouse.calculated_remotes")
  218. if err != nil {
  219. return util.NewContextualError("Invalid lighthouse.calculated_remotes", nil, err)
  220. }
  221. lh.calculatedRemotes.Store(cr)
  222. if !initial {
  223. lh.l.Info("lighthouse.calculated_remotes has changed")
  224. }
  225. }
  226. //NOTE: many things will get much simpler when we combine static_host_map and lighthouse.hosts in config
  227. if initial || c.HasChanged("static_host_map") || c.HasChanged("static_map.cadence") || c.HasChanged("static_map.network") || c.HasChanged("static_map.lookup_timeout") {
  228. // Clean up. Entries still in the static_host_map will be re-built.
  229. ourselves := lh.myVpnNetworks[0].Addr()
  230. oldStaticList := lh.staticList.Load()
  231. if oldStaticList != nil {
  232. lh.RLock()
  233. for staticVpnAddr := range *oldStaticList {
  234. if am, ok := lh.addrMap[staticVpnAddr]; ok && am != nil {
  235. am.ResetForOwner(ourselves)
  236. }
  237. }
  238. lh.RUnlock()
  239. }
  240. // Build a new list based on current config.
  241. staticList := make(map[netip.Addr]struct{})
  242. err := lh.loadStaticMap(c, staticList)
  243. if err != nil {
  244. return err
  245. }
  246. // For entries removed from static_host_map, stop the DNS goroutine and drop the cached addrs.
  247. // All addrs must come from the lighthouses now that it's no longer a static host.
  248. if oldStaticList != nil {
  249. lh.RLock()
  250. for staticVpnAddr := range *oldStaticList {
  251. if _, stillStatic := staticList[staticVpnAddr]; stillStatic {
  252. continue
  253. }
  254. if am, ok := lh.addrMap[staticVpnAddr]; ok && am != nil {
  255. am.ClearHostnameResults()
  256. }
  257. }
  258. lh.RUnlock()
  259. }
  260. lh.staticList.Store(&staticList)
  261. if !initial {
  262. if c.HasChanged("static_host_map") {
  263. lh.l.Info("static_host_map has changed")
  264. }
  265. if c.HasChanged("static_map.cadence") {
  266. lh.l.Info("static_map.cadence has changed")
  267. }
  268. if c.HasChanged("static_map.network") {
  269. lh.l.Info("static_map.network has changed")
  270. }
  271. if c.HasChanged("static_map.lookup_timeout") {
  272. lh.l.Info("static_map.lookup_timeout has changed")
  273. }
  274. }
  275. }
  276. if initial || c.HasChanged("lighthouse.hosts") {
  277. lhList, err := lh.parseLighthouses(c)
  278. if err != nil {
  279. return err
  280. }
  281. lh.lighthouses.Store(&lhList)
  282. if !initial {
  283. //NOTE: we are not tearing down existing lighthouse connections because they might be used for non lighthouse traffic
  284. lh.l.Info("lighthouse.hosts has changed")
  285. lh.TriggerUpdate()
  286. }
  287. }
  288. if initial || c.HasChanged("relay.relays") {
  289. switch c.GetBool("relay.am_relay", false) {
  290. case true:
  291. // Relays aren't allowed to specify other relays
  292. if len(c.GetStringSlice("relay.relays", nil)) > 0 {
  293. lh.l.Info("Ignoring relays from config because am_relay is true")
  294. }
  295. relaysForMe := []netip.Addr{}
  296. lh.relaysForMe.Store(&relaysForMe)
  297. case false:
  298. relaysForMe := []netip.Addr{}
  299. for _, v := range c.GetStringSlice("relay.relays", nil) {
  300. configRIP, err := netip.ParseAddr(v)
  301. if err != nil {
  302. lh.l.Warn("Parse relay from config failed",
  303. "relay", v,
  304. "error", err,
  305. )
  306. } else {
  307. lh.l.Info("Read relay from config", "relay", v)
  308. relaysForMe = append(relaysForMe, configRIP)
  309. }
  310. }
  311. lh.relaysForMe.Store(&relaysForMe)
  312. }
  313. }
  314. return nil
  315. }
  316. func (lh *LightHouse) parseLighthouses(c *config.C) ([]netip.Addr, error) {
  317. lhs := c.GetStringSlice("lighthouse.hosts", []string{})
  318. if lh.amLighthouse && len(lhs) != 0 {
  319. lh.l.Warn("lighthouse.am_lighthouse enabled on node but upstream lighthouses exist in config")
  320. }
  321. out := make([]netip.Addr, len(lhs))
  322. for i, host := range lhs {
  323. addr, err := netip.ParseAddr(host)
  324. if err != nil {
  325. return nil, util.NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, err)
  326. }
  327. if !lh.myVpnNetworksTable.Contains(addr) {
  328. lh.l.Warn("lighthouse host is not within our networks, lighthouse functionality will work but layer 3 network traffic to the lighthouse will not",
  329. "vpnAddr", addr,
  330. "networks", lh.myVpnNetworks,
  331. )
  332. }
  333. out[i] = addr
  334. }
  335. if !lh.amLighthouse && len(out) == 0 {
  336. lh.l.Warn("No lighthouse.hosts configured, this host will only be able to initiate tunnels with static_host_map entries")
  337. }
  338. staticList := lh.GetStaticHostList()
  339. for i := range out {
  340. if _, ok := staticList[out[i]]; !ok {
  341. return nil, fmt.Errorf("lighthouse %s does not have a static_host_map entry", out[i])
  342. }
  343. }
  344. return out, nil
  345. }
  346. func getStaticMapCadence(c *config.C) (time.Duration, error) {
  347. cadence := c.GetString("static_map.cadence", "30s")
  348. d, err := time.ParseDuration(cadence)
  349. if err != nil {
  350. return 0, err
  351. }
  352. return d, nil
  353. }
  354. func getStaticMapLookupTimeout(c *config.C) (time.Duration, error) {
  355. lookupTimeout := c.GetString("static_map.lookup_timeout", "250ms")
  356. d, err := time.ParseDuration(lookupTimeout)
  357. if err != nil {
  358. return 0, err
  359. }
  360. return d, nil
  361. }
  362. func getStaticMapNetwork(c *config.C) (string, error) {
  363. network := c.GetString("static_map.network", "ip4")
  364. if network != "ip" && network != "ip4" && network != "ip6" {
  365. return "", fmt.Errorf("static_map.network must be one of ip, ip4, or ip6")
  366. }
  367. return network, nil
  368. }
  369. func (lh *LightHouse) loadStaticMap(c *config.C, staticList map[netip.Addr]struct{}) error {
  370. d, err := getStaticMapCadence(c)
  371. if err != nil {
  372. return err
  373. }
  374. network, err := getStaticMapNetwork(c)
  375. if err != nil {
  376. return err
  377. }
  378. lookupTimeout, err := getStaticMapLookupTimeout(c)
  379. if err != nil {
  380. return err
  381. }
  382. shm := c.GetMap("static_host_map", map[string]any{})
  383. i := 0
  384. for k, v := range shm {
  385. vpnAddr, err := netip.ParseAddr(fmt.Sprintf("%v", k))
  386. if err != nil {
  387. return util.NewContextualError("Unable to parse static_host_map entry", m{"host": k, "entry": i + 1}, err)
  388. }
  389. if !lh.myVpnNetworksTable.Contains(vpnAddr) {
  390. lh.l.Warn("static_host_map key is not within our networks, layer 3 network traffic to this host will not work",
  391. "vpnAddr", vpnAddr,
  392. "networks", lh.myVpnNetworks,
  393. "entry", i+1,
  394. )
  395. }
  396. vals, ok := v.([]any)
  397. if !ok {
  398. vals = []any{v}
  399. }
  400. remoteAddrs := []string{}
  401. for _, v := range vals {
  402. remoteAddrs = append(remoteAddrs, fmt.Sprintf("%v", v))
  403. }
  404. err = lh.addStaticRemotes(i, d, network, lookupTimeout, vpnAddr, remoteAddrs, staticList)
  405. if err != nil {
  406. return err
  407. }
  408. i++
  409. }
  410. return nil
  411. }
  412. func (lh *LightHouse) Query(vpnAddr netip.Addr) *RemoteList {
  413. if !lh.IsLighthouseAddr(vpnAddr) {
  414. lh.QueryServer(vpnAddr)
  415. }
  416. lh.RLock()
  417. if v, ok := lh.addrMap[vpnAddr]; ok {
  418. lh.RUnlock()
  419. return v
  420. }
  421. lh.RUnlock()
  422. return nil
  423. }
  424. // QueryServer is asynchronous so no reply should be expected
  425. func (lh *LightHouse) QueryServer(vpnAddr netip.Addr) {
  426. // Don't put lighthouse addrs in the query channel because we can't query lighthouses about lighthouses
  427. if lh.amLighthouse || lh.IsLighthouseAddr(vpnAddr) {
  428. return
  429. }
  430. lh.queryChan <- vpnAddr
  431. }
  432. func (lh *LightHouse) QueryCache(vpnAddrs []netip.Addr) *RemoteList {
  433. lh.RLock()
  434. if v, ok := lh.addrMap[vpnAddrs[0]]; ok {
  435. lh.RUnlock()
  436. return v
  437. }
  438. lh.RUnlock()
  439. lh.Lock()
  440. defer lh.Unlock()
  441. // Add an entry if we don't already have one
  442. return lh.unlockedGetRemoteList(vpnAddrs) //todo CERT-V2 this contains addrmap lookups we could potentially skip
  443. }
  444. // queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing
  445. // details from the remote list. It looks for a hit in the addrMap and a hit in the RemoteList under the owner vpnAddr
  446. // If one is found then f() is called with proper locking, f() must return result of n.MarshalTo()
  447. func (lh *LightHouse) queryAndPrepMessage(vpnAddr netip.Addr, f func(*cache) (int, error)) (bool, int, error) {
  448. lh.RLock()
  449. // Do we have an entry in the main cache?
  450. if v, ok := lh.addrMap[vpnAddr]; ok {
  451. // Swap lh lock for remote list lock
  452. v.RLock()
  453. defer v.RUnlock()
  454. lh.RUnlock()
  455. // We may be asking about a non primary address so lets get the primary address
  456. if slices.Contains(v.vpnAddrs, vpnAddr) {
  457. vpnAddr = v.vpnAddrs[0]
  458. }
  459. c := v.cache[vpnAddr]
  460. // Make sure we have
  461. if c != nil {
  462. n, err := f(c)
  463. return true, n, err
  464. }
  465. return false, 0, nil
  466. }
  467. lh.RUnlock()
  468. return false, 0, nil
  469. }
  470. func (lh *LightHouse) DeleteVpnAddrs(allVpnAddrs []netip.Addr) {
  471. // First we check the static host map. If any of the VpnAddrs to be deleted are present, do nothing.
  472. staticList := lh.GetStaticHostList()
  473. for _, addr := range allVpnAddrs {
  474. if _, ok := staticList[addr]; ok {
  475. return
  476. }
  477. }
  478. // None of the VpnAddrs were present. Now we can do the deletes.
  479. lh.Lock()
  480. rm, ok := lh.addrMap[allVpnAddrs[0]]
  481. if ok {
  482. debugEnabled := lh.l.Enabled(context.Background(), slog.LevelDebug)
  483. for _, addr := range allVpnAddrs {
  484. srm := lh.addrMap[addr]
  485. if srm == rm {
  486. delete(lh.addrMap, addr)
  487. if debugEnabled {
  488. lh.l.Debug("deleting from lighthouse", "vpnAddr", addr)
  489. }
  490. }
  491. }
  492. }
  493. lh.Unlock()
  494. }
  495. // AddStaticRemote adds a static host entry for vpnAddr as ourselves as the owner
  496. // We are the owner because we don't want a lighthouse server to advertise for static hosts it was configured with
  497. // And we don't want a lighthouse query reply to interfere with our learned cache if we are a client
  498. // NOTE: this function should not interact with any hot path objects, like lh.staticList, the caller should handle it
  499. func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, timeout time.Duration, vpnAddr netip.Addr, toAddrs []string, staticList map[netip.Addr]struct{}) error {
  500. lh.Lock()
  501. am := lh.unlockedGetRemoteList([]netip.Addr{vpnAddr})
  502. am.Lock()
  503. defer am.Unlock()
  504. ctx := lh.ctx
  505. lh.Unlock()
  506. hr, err := NewHostnameResults(ctx, lh.l, d, network, timeout, toAddrs, func() {
  507. // This callback runs whenever the DNS hostname resolver finds a different set of addr's
  508. // in its resolution for hostnames.
  509. am.Lock()
  510. defer am.Unlock()
  511. am.shouldRebuild = true
  512. })
  513. if err != nil {
  514. return util.NewContextualError("Static host address could not be parsed", m{"vpnAddr": vpnAddr, "entry": i + 1}, err)
  515. }
  516. am.unlockedSetHostnamesResults(hr)
  517. for _, addrPort := range hr.GetAddrs() {
  518. if !lh.shouldAdd([]netip.Addr{vpnAddr}, addrPort.Addr()) {
  519. continue
  520. }
  521. switch {
  522. case addrPort.Addr().Is4():
  523. am.unlockedPrependV4(lh.myVpnNetworks[0].Addr(), netAddrToProtoV4AddrPort(addrPort.Addr(), addrPort.Port()))
  524. case addrPort.Addr().Is6():
  525. am.unlockedPrependV6(lh.myVpnNetworks[0].Addr(), netAddrToProtoV6AddrPort(addrPort.Addr(), addrPort.Port()))
  526. }
  527. }
  528. // Mark it as static in the caller provided map
  529. staticList[vpnAddr] = struct{}{}
  530. return nil
  531. }
  532. // addCalculatedRemotes adds any calculated remotes based on the
  533. // lighthouse.calculated_remotes configuration. It returns true if any
  534. // calculated remotes were added
  535. func (lh *LightHouse) addCalculatedRemotes(vpnAddr netip.Addr) bool {
  536. tree := lh.getCalculatedRemotes()
  537. if tree == nil {
  538. return false
  539. }
  540. calculatedRemotes, ok := tree.Lookup(vpnAddr)
  541. if !ok {
  542. return false
  543. }
  544. var calculatedV4 []*V4AddrPort
  545. var calculatedV6 []*V6AddrPort
  546. for _, cr := range calculatedRemotes {
  547. if vpnAddr.Is4() {
  548. c := cr.ApplyV4(vpnAddr)
  549. if c != nil {
  550. calculatedV4 = append(calculatedV4, c)
  551. }
  552. } else if vpnAddr.Is6() {
  553. c := cr.ApplyV6(vpnAddr)
  554. if c != nil {
  555. calculatedV6 = append(calculatedV6, c)
  556. }
  557. }
  558. }
  559. lh.Lock()
  560. am := lh.unlockedGetRemoteList([]netip.Addr{vpnAddr})
  561. am.Lock()
  562. defer am.Unlock()
  563. lh.Unlock()
  564. if len(calculatedV4) > 0 {
  565. am.unlockedSetV4(lh.myVpnNetworks[0].Addr(), vpnAddr, calculatedV4, lh.unlockedShouldAddV4)
  566. }
  567. if len(calculatedV6) > 0 {
  568. am.unlockedSetV6(lh.myVpnNetworks[0].Addr(), vpnAddr, calculatedV6, lh.unlockedShouldAddV6)
  569. }
  570. return len(calculatedV4) > 0 || len(calculatedV6) > 0
  571. }
  572. // unlockedGetRemoteList assumes you have the lh lock
  573. func (lh *LightHouse) unlockedGetRemoteList(allAddrs []netip.Addr) *RemoteList {
  574. // before we go and make a new remotelist, we need to make sure we don't have one for any of this set of vpnaddrs yet
  575. for i, addr := range allAddrs {
  576. am, ok := lh.addrMap[addr]
  577. if ok {
  578. if i != 0 {
  579. lh.addrMap[allAddrs[0]] = am
  580. }
  581. return am
  582. }
  583. }
  584. am := NewRemoteList(allAddrs, lh.shouldAdd)
  585. for _, addr := range allAddrs {
  586. lh.addrMap[addr] = am
  587. }
  588. return am
  589. }
  590. func (lh *LightHouse) shouldAdd(vpnAddrs []netip.Addr, to netip.Addr) bool {
  591. allow := lh.GetRemoteAllowList().AllowAll(vpnAddrs, to)
  592. if lh.l.Enabled(context.Background(), logging.LevelTrace) {
  593. lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow",
  594. "vpnAddrs", vpnAddrs,
  595. "udpAddr", to,
  596. "allow", allow,
  597. )
  598. }
  599. if !allow {
  600. return false
  601. }
  602. if lh.myVpnNetworksTable.Contains(to) {
  603. return false
  604. }
  605. return true
  606. }
  607. // unlockedShouldAddV4 checks if to is allowed by our allow list
  608. func (lh *LightHouse) unlockedShouldAddV4(vpnAddr netip.Addr, to *V4AddrPort) bool {
  609. udpAddr := protoV4AddrPortToNetAddrPort(to)
  610. allow := lh.GetRemoteAllowList().Allow(vpnAddr, udpAddr.Addr())
  611. if lh.l.Enabled(context.Background(), logging.LevelTrace) {
  612. lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow",
  613. "vpnAddr", vpnAddr,
  614. "udpAddr", udpAddr,
  615. "allow", allow,
  616. )
  617. }
  618. if !allow {
  619. return false
  620. }
  621. if lh.myVpnNetworksTable.Contains(udpAddr.Addr()) {
  622. return false
  623. }
  624. return true
  625. }
  626. // unlockedShouldAddV6 checks if to is allowed by our allow list
  627. func (lh *LightHouse) unlockedShouldAddV6(vpnAddr netip.Addr, to *V6AddrPort) bool {
  628. udpAddr := protoV6AddrPortToNetAddrPort(to)
  629. allow := lh.GetRemoteAllowList().Allow(vpnAddr, udpAddr.Addr())
  630. if lh.l.Enabled(context.Background(), logging.LevelTrace) {
  631. lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow",
  632. "vpnAddr", vpnAddr,
  633. "udpAddr", udpAddr,
  634. "allow", allow,
  635. )
  636. }
  637. if !allow {
  638. return false
  639. }
  640. if lh.myVpnNetworksTable.Contains(udpAddr.Addr()) {
  641. return false
  642. }
  643. return true
  644. }
  645. func (lh *LightHouse) IsLighthouseAddr(vpnAddr netip.Addr) bool {
  646. l := lh.GetLighthouses()
  647. return slices.Contains(l, vpnAddr)
  648. }
  649. func (lh *LightHouse) IsAnyLighthouseAddr(vpnAddrs []netip.Addr) bool {
  650. l := lh.GetLighthouses()
  651. for i := range vpnAddrs {
  652. if slices.Contains(l, vpnAddrs[i]) {
  653. return true
  654. }
  655. }
  656. return false
  657. }
  658. func (lh *LightHouse) startQueryWorker() {
  659. if lh.amLighthouse {
  660. return
  661. }
  662. go func() {
  663. nb := make([]byte, 12, 12)
  664. out := make([]byte, mtu)
  665. for {
  666. select {
  667. case <-lh.ctx.Done():
  668. return
  669. case addr := <-lh.queryChan:
  670. lh.innerQueryServer(addr, nb, out)
  671. }
  672. }
  673. }()
  674. }
  675. func (lh *LightHouse) innerQueryServer(addr netip.Addr, nb, out []byte) {
  676. if lh.IsLighthouseAddr(addr) {
  677. return
  678. }
  679. msg := &NebulaMeta{
  680. Type: NebulaMeta_HostQuery,
  681. Details: &NebulaMetaDetails{},
  682. }
  683. var v1Query, v2Query []byte
  684. var err error
  685. var v cert.Version
  686. queried := 0
  687. lighthouses := lh.GetLighthouses()
  688. for _, lhVpnAddr := range lighthouses {
  689. hi := lh.ifce.GetHostInfo(lhVpnAddr)
  690. if hi != nil {
  691. v = hi.ConnectionState.myCert.Version()
  692. } else {
  693. v = lh.ifce.GetCertState().initiatingVersion
  694. }
  695. if v == cert.Version1 {
  696. if !addr.Is4() {
  697. lh.l.Error("Can't query lighthouse for v6 address using a v1 protocol",
  698. "queryVpnAddr", addr,
  699. "lighthouseAddr", lhVpnAddr,
  700. )
  701. continue
  702. }
  703. if v1Query == nil {
  704. b := addr.As4()
  705. msg.Details.VpnAddr = nil
  706. msg.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
  707. v1Query, err = msg.Marshal()
  708. if err != nil {
  709. lh.l.Error("Failed to marshal lighthouse v1 query payload",
  710. "error", err,
  711. "queryVpnAddr", addr,
  712. "lighthouseAddr", lhVpnAddr,
  713. )
  714. continue
  715. }
  716. }
  717. lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v1Query, nb, out)
  718. queried++
  719. } else if v == cert.Version2 {
  720. if v2Query == nil {
  721. msg.Details.OldVpnAddr = 0
  722. msg.Details.VpnAddr = netAddrToProtoAddr(addr)
  723. v2Query, err = msg.Marshal()
  724. if err != nil {
  725. lh.l.Error("Failed to marshal lighthouse v2 query payload",
  726. "error", err,
  727. "queryVpnAddr", addr,
  728. "lighthouseAddr", lhVpnAddr,
  729. )
  730. continue
  731. }
  732. }
  733. lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v2Query, nb, out)
  734. queried++
  735. } else {
  736. lh.l.Debug("unsupported protocol version",
  737. "op", "query",
  738. "queryVpnAddr", addr,
  739. "version", v,
  740. )
  741. continue
  742. }
  743. }
  744. lh.metricTx(NebulaMeta_HostQuery, int64(queried))
  745. }
  746. func (lh *LightHouse) StartUpdateWorker() {
  747. interval := lh.GetUpdateInterval()
  748. if lh.amLighthouse || interval == 0 {
  749. return
  750. }
  751. clockSource := time.NewTicker(time.Second * time.Duration(interval))
  752. updateCtx, cancel := context.WithCancel(lh.ctx)
  753. lh.updateCancel = cancel
  754. go func() {
  755. defer clockSource.Stop()
  756. for {
  757. lh.SendUpdate()
  758. select {
  759. case <-updateCtx.Done():
  760. return
  761. case <-clockSource.C:
  762. continue
  763. case <-lh.updateTrigger:
  764. continue
  765. }
  766. }
  767. }()
  768. }
  769. // TriggerUpdate requests an immediate lighthouse update. This is a non-blocking
  770. // operation intended to be called after a handshake completes with a lighthouse,
  771. // so the lighthouse has our current addresses without waiting for the next
  772. // periodic update.
  773. func (lh *LightHouse) TriggerUpdate() {
  774. select {
  775. case lh.updateTrigger <- struct{}{}:
  776. default:
  777. }
  778. }
  779. func (lh *LightHouse) SendUpdate() {
  780. var v4 []*V4AddrPort
  781. var v6 []*V6AddrPort
  782. for _, e := range lh.GetAdvertiseAddrs() {
  783. if e.Addr().Is4() {
  784. v4 = append(v4, netAddrToProtoV4AddrPort(e.Addr(), e.Port()))
  785. } else {
  786. v6 = append(v6, netAddrToProtoV6AddrPort(e.Addr(), e.Port()))
  787. }
  788. }
  789. lal := lh.GetLocalAllowList()
  790. for _, e := range lh.localAddrsFn(lal) {
  791. if lh.myVpnNetworksTable.Contains(e) {
  792. continue
  793. }
  794. // Only add addrs that aren't my VPN/tun networks
  795. if e.Is4() {
  796. v4 = append(v4, netAddrToProtoV4AddrPort(e, uint16(lh.nebulaPort)))
  797. } else {
  798. v6 = append(v6, netAddrToProtoV6AddrPort(e, uint16(lh.nebulaPort)))
  799. }
  800. }
  801. nb := make([]byte, 12, 12)
  802. out := make([]byte, mtu)
  803. var v1Update, v2Update []byte
  804. var err error
  805. updated := 0
  806. lighthouses := lh.GetLighthouses()
  807. for _, lhVpnAddr := range lighthouses {
  808. var v cert.Version
  809. hi := lh.ifce.GetHostInfo(lhVpnAddr)
  810. if hi != nil {
  811. v = hi.ConnectionState.myCert.Version()
  812. } else {
  813. v = lh.ifce.GetCertState().initiatingVersion
  814. }
  815. if v == cert.Version1 {
  816. if v1Update == nil {
  817. if !lh.myVpnNetworks[0].Addr().Is4() {
  818. lh.l.Warn("cannot update lighthouse using v1 protocol without an IPv4 address",
  819. "lighthouseAddr", lhVpnAddr,
  820. )
  821. continue
  822. }
  823. var relays []uint32
  824. for _, r := range lh.GetRelaysForMe() {
  825. if !r.Is4() {
  826. continue
  827. }
  828. b := r.As4()
  829. relays = append(relays, binary.BigEndian.Uint32(b[:]))
  830. }
  831. b := lh.myVpnNetworks[0].Addr().As4()
  832. msg := NebulaMeta{
  833. Type: NebulaMeta_HostUpdateNotification,
  834. Details: &NebulaMetaDetails{
  835. V4AddrPorts: v4,
  836. V6AddrPorts: v6,
  837. OldRelayVpnAddrs: relays,
  838. OldVpnAddr: binary.BigEndian.Uint32(b[:]),
  839. },
  840. }
  841. v1Update, err = msg.Marshal()
  842. if err != nil {
  843. lh.l.Error("Error while marshaling for lighthouse v1 update",
  844. "error", err,
  845. "lighthouseAddr", lhVpnAddr,
  846. )
  847. continue
  848. }
  849. }
  850. lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v1Update, nb, out)
  851. updated++
  852. } else if v == cert.Version2 {
  853. if v2Update == nil {
  854. var relays []*Addr
  855. for _, r := range lh.GetRelaysForMe() {
  856. relays = append(relays, netAddrToProtoAddr(r))
  857. }
  858. msg := NebulaMeta{
  859. Type: NebulaMeta_HostUpdateNotification,
  860. Details: &NebulaMetaDetails{
  861. V4AddrPorts: v4,
  862. V6AddrPorts: v6,
  863. RelayVpnAddrs: relays,
  864. },
  865. }
  866. v2Update, err = msg.Marshal()
  867. if err != nil {
  868. lh.l.Error("Error while marshaling for lighthouse v2 update",
  869. "error", err,
  870. "lighthouseAddr", lhVpnAddr,
  871. )
  872. continue
  873. }
  874. }
  875. lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v2Update, nb, out)
  876. updated++
  877. } else {
  878. lh.l.Debug("unsupported protocol version",
  879. "op", "update",
  880. "version", v,
  881. )
  882. continue
  883. }
  884. }
  885. lh.metricTx(NebulaMeta_HostUpdateNotification, int64(updated))
  886. }
  887. type LightHouseHandler struct {
  888. lh *LightHouse
  889. nb []byte
  890. out []byte
  891. pb []byte
  892. meta *NebulaMeta
  893. l *slog.Logger
  894. }
  895. func (lh *LightHouse) NewRequestHandler() *LightHouseHandler {
  896. lhh := &LightHouseHandler{
  897. lh: lh,
  898. nb: make([]byte, 12, 12),
  899. out: make([]byte, mtu),
  900. l: lh.l,
  901. pb: make([]byte, mtu),
  902. meta: &NebulaMeta{
  903. Details: &NebulaMetaDetails{},
  904. },
  905. }
  906. return lhh
  907. }
  908. func (lh *LightHouse) metricRx(t NebulaMeta_MessageType, i int64) {
  909. lh.metrics.Rx(header.MessageType(t), 0, i)
  910. }
  911. func (lh *LightHouse) metricTx(t NebulaMeta_MessageType, i int64) {
  912. lh.metrics.Tx(header.MessageType(t), 0, i)
  913. }
  914. // This method is similar to Reset(), but it re-uses the pointer structs
  915. // so that we don't have to re-allocate them
  916. func (lhh *LightHouseHandler) resetMeta() *NebulaMeta {
  917. details := lhh.meta.Details
  918. lhh.meta.Reset()
  919. // Keep the array memory around
  920. details.V4AddrPorts = details.V4AddrPorts[:0]
  921. details.V6AddrPorts = details.V6AddrPorts[:0]
  922. details.RelayVpnAddrs = details.RelayVpnAddrs[:0]
  923. details.OldRelayVpnAddrs = details.OldRelayVpnAddrs[:0]
  924. details.OldVpnAddr = 0
  925. details.VpnAddr = nil
  926. lhh.meta.Details = details
  927. return lhh.meta
  928. }
  929. func (lhh *LightHouseHandler) HandleRequest(rAddr netip.AddrPort, fromVpnAddrs []netip.Addr, p []byte, w EncWriter) {
  930. n := lhh.resetMeta()
  931. err := n.Unmarshal(p)
  932. if err != nil {
  933. lhh.l.Error("Failed to unmarshal lighthouse packet",
  934. "error", err,
  935. "vpnAddrs", fromVpnAddrs,
  936. "udpAddr", rAddr,
  937. )
  938. return
  939. }
  940. if n.Details == nil {
  941. lhh.l.Error("Invalid lighthouse update",
  942. "vpnAddrs", fromVpnAddrs,
  943. "udpAddr", rAddr,
  944. )
  945. return
  946. }
  947. lhh.lh.metricRx(n.Type, 1)
  948. switch n.Type {
  949. case NebulaMeta_HostQuery:
  950. lhh.handleHostQuery(n, fromVpnAddrs, rAddr, w)
  951. case NebulaMeta_HostQueryReply:
  952. lhh.handleHostQueryReply(n, fromVpnAddrs)
  953. case NebulaMeta_HostUpdateNotification:
  954. lhh.handleHostUpdateNotification(n, fromVpnAddrs, w)
  955. case NebulaMeta_HostMovedNotification:
  956. case NebulaMeta_HostPunchNotification:
  957. lhh.handleHostPunchNotification(n, fromVpnAddrs, w)
  958. case NebulaMeta_HostUpdateNotificationAck:
  959. // noop
  960. }
  961. }
  962. func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, fromVpnAddrs []netip.Addr, addr netip.AddrPort, w EncWriter) {
  963. // Exit if we don't answer queries
  964. if !lhh.lh.amLighthouse {
  965. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  966. lhh.l.Debug("I don't answer queries, but received one", "from", addr)
  967. }
  968. return
  969. }
  970. queryVpnAddr, useVersion, err := n.Details.GetVpnAddrAndVersion()
  971. if err != nil {
  972. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  973. lhh.l.Debug("Dropping malformed HostQuery",
  974. "from", fromVpnAddrs,
  975. "details", n.Details,
  976. )
  977. }
  978. return
  979. }
  980. if useVersion == cert.Version1 && queryVpnAddr.Is6() {
  981. // this case really shouldn't be possible to represent, but reject it anyway.
  982. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  983. lhh.l.Debug("invalid vpn addr for v1 handleHostQuery",
  984. "vpnAddrs", fromVpnAddrs,
  985. "queryVpnAddr", queryVpnAddr,
  986. )
  987. }
  988. return
  989. }
  990. found, ln, err := lhh.lh.queryAndPrepMessage(queryVpnAddr, func(c *cache) (int, error) {
  991. n = lhh.resetMeta()
  992. n.Type = NebulaMeta_HostQueryReply
  993. if useVersion == cert.Version1 {
  994. b := queryVpnAddr.As4()
  995. n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
  996. } else {
  997. n.Details.VpnAddr = netAddrToProtoAddr(queryVpnAddr)
  998. }
  999. lhh.coalesceAnswers(useVersion, c, n)
  1000. return n.MarshalTo(lhh.pb)
  1001. })
  1002. if !found {
  1003. return
  1004. }
  1005. if err != nil {
  1006. lhh.l.Error("Failed to marshal lighthouse host query reply",
  1007. "error", err,
  1008. "vpnAddrs", fromVpnAddrs,
  1009. )
  1010. return
  1011. }
  1012. lhh.lh.metricTx(NebulaMeta_HostQueryReply, 1)
  1013. w.SendMessageToVpnAddr(header.LightHouse, 0, fromVpnAddrs[0], lhh.pb[:ln], lhh.nb, lhh.out[:0])
  1014. lhh.sendHostPunchNotification(n, fromVpnAddrs, queryVpnAddr, w)
  1015. }
  1016. // sendHostPunchNotification signals the other side to punch some zero byte udp packets
  1017. func (lhh *LightHouseHandler) sendHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, punchNotifDest netip.Addr, w EncWriter) {
  1018. whereToPunch := fromVpnAddrs[0]
  1019. found, ln, err := lhh.lh.queryAndPrepMessage(whereToPunch, func(c *cache) (int, error) {
  1020. n = lhh.resetMeta()
  1021. n.Type = NebulaMeta_HostPunchNotification
  1022. targetHI := lhh.lh.ifce.GetHostInfo(punchNotifDest)
  1023. var useVersion cert.Version
  1024. if targetHI == nil {
  1025. useVersion = lhh.lh.ifce.GetCertState().initiatingVersion
  1026. } else {
  1027. crt := targetHI.GetCert().Certificate
  1028. useVersion = crt.Version()
  1029. // we can only retarget if we have a hostinfo
  1030. newDest, ok := findNetworkUnion(crt.Networks(), fromVpnAddrs)
  1031. if ok {
  1032. whereToPunch = newDest
  1033. } else {
  1034. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1035. lhh.l.Debug("unable to punch to host, no addresses in common",
  1036. "to", crt.Networks(),
  1037. )
  1038. }
  1039. }
  1040. }
  1041. if useVersion == cert.Version1 {
  1042. if !whereToPunch.Is4() {
  1043. return 0, fmt.Errorf("invalid vpn addr for v1 handleHostQuery")
  1044. }
  1045. b := whereToPunch.As4()
  1046. n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
  1047. } else if useVersion == cert.Version2 {
  1048. n.Details.VpnAddr = netAddrToProtoAddr(whereToPunch)
  1049. } else {
  1050. return 0, errors.New("unsupported version")
  1051. }
  1052. lhh.coalesceAnswers(useVersion, c, n)
  1053. return n.MarshalTo(lhh.pb)
  1054. })
  1055. if !found {
  1056. return
  1057. }
  1058. if err != nil {
  1059. lhh.l.Error("Failed to marshal lighthouse host was queried for",
  1060. "error", err,
  1061. "vpnAddrs", fromVpnAddrs,
  1062. )
  1063. return
  1064. }
  1065. lhh.lh.metricTx(NebulaMeta_HostPunchNotification, 1)
  1066. w.SendMessageToVpnAddr(header.LightHouse, 0, punchNotifDest, lhh.pb[:ln], lhh.nb, lhh.out[:0])
  1067. }
  1068. func (lhh *LightHouseHandler) coalesceAnswers(v cert.Version, c *cache, n *NebulaMeta) {
  1069. if c.v4 != nil {
  1070. if c.v4.learned != nil {
  1071. n.Details.V4AddrPorts = append(n.Details.V4AddrPorts, c.v4.learned)
  1072. }
  1073. if c.v4.reported != nil && len(c.v4.reported) > 0 {
  1074. n.Details.V4AddrPorts = append(n.Details.V4AddrPorts, c.v4.reported...)
  1075. }
  1076. }
  1077. if c.v6 != nil {
  1078. if c.v6.learned != nil {
  1079. n.Details.V6AddrPorts = append(n.Details.V6AddrPorts, c.v6.learned)
  1080. }
  1081. if c.v6.reported != nil && len(c.v6.reported) > 0 {
  1082. n.Details.V6AddrPorts = append(n.Details.V6AddrPorts, c.v6.reported...)
  1083. }
  1084. }
  1085. if c.relay != nil {
  1086. if v == cert.Version1 {
  1087. b := [4]byte{}
  1088. for _, r := range c.relay.relay {
  1089. if !r.Is4() {
  1090. continue
  1091. }
  1092. b = r.As4()
  1093. n.Details.OldRelayVpnAddrs = append(n.Details.OldRelayVpnAddrs, binary.BigEndian.Uint32(b[:]))
  1094. }
  1095. } else if v == cert.Version2 {
  1096. for _, r := range c.relay.relay {
  1097. n.Details.RelayVpnAddrs = append(n.Details.RelayVpnAddrs, netAddrToProtoAddr(r))
  1098. }
  1099. } else {
  1100. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1101. lhh.l.Debug("unsupported protocol version",
  1102. "op", "coalesceAnswers",
  1103. "version", v,
  1104. )
  1105. }
  1106. }
  1107. }
  1108. }
  1109. func (lhh *LightHouseHandler) handleHostQueryReply(n *NebulaMeta, fromVpnAddrs []netip.Addr) {
  1110. if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) {
  1111. return
  1112. }
  1113. certVpnAddr, _, err := n.Details.GetVpnAddrAndVersion()
  1114. if err != nil {
  1115. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1116. lhh.l.Error("dropping malformed HostQueryReply",
  1117. "error", err,
  1118. "vpnAddrs", fromVpnAddrs,
  1119. )
  1120. }
  1121. return
  1122. }
  1123. relays := n.Details.GetRelays()
  1124. lhh.lh.Lock()
  1125. am := lhh.lh.unlockedGetRemoteList([]netip.Addr{certVpnAddr})
  1126. am.Lock()
  1127. lhh.lh.Unlock()
  1128. am.unlockedSetV4(fromVpnAddrs[0], certVpnAddr, n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4)
  1129. am.unlockedSetV6(fromVpnAddrs[0], certVpnAddr, n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6)
  1130. am.unlockedSetRelay(fromVpnAddrs[0], relays)
  1131. am.Unlock()
  1132. // Non-blocking attempt to trigger, skip if it would block
  1133. select {
  1134. case lhh.lh.handshakeTrigger <- certVpnAddr:
  1135. default:
  1136. }
  1137. }
  1138. func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) {
  1139. if !lhh.lh.amLighthouse {
  1140. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1141. lhh.l.Debug("I am not a lighthouse, do not take host updates", "from", fromVpnAddrs)
  1142. }
  1143. return
  1144. }
  1145. // not using GetVpnAddrAndVersion because we don't want to error on a blank detailsVpnAddr
  1146. var detailsVpnAddr netip.Addr
  1147. var useVersion cert.Version
  1148. if n.Details.OldVpnAddr != 0 { //v1 always sets this field
  1149. b := [4]byte{}
  1150. binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
  1151. detailsVpnAddr = netip.AddrFrom4(b)
  1152. useVersion = cert.Version1
  1153. } else if n.Details.VpnAddr != nil { //this field is "optional" in v2, but if it's set, we should enforce it
  1154. detailsVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
  1155. useVersion = cert.Version2
  1156. } else {
  1157. detailsVpnAddr = netip.Addr{}
  1158. useVersion = cert.Version2
  1159. }
  1160. //Simple check that the host sent this not someone else, if detailsVpnAddr is filled
  1161. if detailsVpnAddr.IsValid() && !slices.Contains(fromVpnAddrs, detailsVpnAddr) {
  1162. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1163. lhh.l.Debug("Host sent invalid update",
  1164. "vpnAddrs", fromVpnAddrs,
  1165. "answer", detailsVpnAddr,
  1166. )
  1167. }
  1168. return
  1169. }
  1170. relays := n.Details.GetRelays()
  1171. lhh.lh.Lock()
  1172. am := lhh.lh.unlockedGetRemoteList(fromVpnAddrs)
  1173. am.Lock()
  1174. lhh.lh.Unlock()
  1175. am.unlockedSetV4(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4)
  1176. am.unlockedSetV6(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6)
  1177. am.unlockedSetRelay(fromVpnAddrs[0], relays)
  1178. am.Unlock()
  1179. n = lhh.resetMeta()
  1180. n.Type = NebulaMeta_HostUpdateNotificationAck
  1181. switch useVersion {
  1182. case cert.Version1:
  1183. if !fromVpnAddrs[0].Is4() {
  1184. lhh.l.Error("Can not send HostUpdateNotificationAck for a ipv6 vpn ip in a v1 message",
  1185. "vpnAddrs", fromVpnAddrs,
  1186. )
  1187. return
  1188. }
  1189. vpnAddrB := fromVpnAddrs[0].As4()
  1190. n.Details.OldVpnAddr = binary.BigEndian.Uint32(vpnAddrB[:])
  1191. case cert.Version2:
  1192. // do nothing, we want to send a blank message
  1193. default:
  1194. lhh.l.Error("invalid protocol version", "useVersion", useVersion)
  1195. return
  1196. }
  1197. ln, err := n.MarshalTo(lhh.pb)
  1198. if err != nil {
  1199. lhh.l.Error("Failed to marshal lighthouse host update ack",
  1200. "error", err,
  1201. "vpnAddrs", fromVpnAddrs,
  1202. )
  1203. return
  1204. }
  1205. lhh.lh.metricTx(NebulaMeta_HostUpdateNotificationAck, 1)
  1206. w.SendMessageToVpnAddr(header.LightHouse, 0, fromVpnAddrs[0], lhh.pb[:ln], lhh.nb, lhh.out[:0])
  1207. }
  1208. func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) {
  1209. //It's possible the lighthouse is communicating with us using a non primary vpn addr,
  1210. //which means we need to compare all fromVpnAddrs against all configured lighthouse vpn addrs.
  1211. if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) {
  1212. return
  1213. }
  1214. detailsVpnAddr, _, err := n.Details.GetVpnAddrAndVersion()
  1215. if err != nil {
  1216. if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
  1217. lhh.l.Debug("dropping invalid HostPunchNotification",
  1218. "details", n.Details,
  1219. "error", err,
  1220. )
  1221. }
  1222. return
  1223. }
  1224. remoteAllowList := lhh.lh.GetRemoteAllowList()
  1225. for _, a := range n.Details.V4AddrPorts {
  1226. if a == nil {
  1227. continue
  1228. }
  1229. b := protoV4AddrPortToNetAddrPort(a)
  1230. if remoteAllowList.Allow(detailsVpnAddr, b.Addr()) {
  1231. lhh.lh.punchy.Schedule(b, detailsVpnAddr)
  1232. }
  1233. }
  1234. for _, a := range n.Details.V6AddrPorts {
  1235. if a == nil {
  1236. continue
  1237. }
  1238. b := protoV6AddrPortToNetAddrPort(a)
  1239. if remoteAllowList.Allow(detailsVpnAddr, b.Addr()) {
  1240. lhh.lh.punchy.Schedule(b, detailsVpnAddr)
  1241. }
  1242. }
  1243. // This sends a nebula test packet to the host trying to contact us. In the case
  1244. // of a double nat or other difficult scenario, this may help establish
  1245. // a tunnel. ScheduleRespond is a no-op when punchy.respond is disabled.
  1246. lhh.lh.punchy.ScheduleRespond(detailsVpnAddr)
  1247. }
  1248. func protoAddrToNetAddr(addr *Addr) netip.Addr {
  1249. b := [16]byte{}
  1250. binary.BigEndian.PutUint64(b[:8], addr.Hi)
  1251. binary.BigEndian.PutUint64(b[8:], addr.Lo)
  1252. return netip.AddrFrom16(b).Unmap()
  1253. }
  1254. func protoV4AddrPortToNetAddrPort(ap *V4AddrPort) netip.AddrPort {
  1255. b := [4]byte{}
  1256. binary.BigEndian.PutUint32(b[:], ap.Addr)
  1257. return netip.AddrPortFrom(netip.AddrFrom4(b), uint16(ap.Port))
  1258. }
  1259. func protoV6AddrPortToNetAddrPort(ap *V6AddrPort) netip.AddrPort {
  1260. b := [16]byte{}
  1261. binary.BigEndian.PutUint64(b[:8], ap.Hi)
  1262. binary.BigEndian.PutUint64(b[8:], ap.Lo)
  1263. return netip.AddrPortFrom(netip.AddrFrom16(b).Unmap(), uint16(ap.Port))
  1264. }
  1265. func netAddrToProtoAddr(addr netip.Addr) *Addr {
  1266. b := addr.As16()
  1267. return &Addr{
  1268. Hi: binary.BigEndian.Uint64(b[:8]),
  1269. Lo: binary.BigEndian.Uint64(b[8:]),
  1270. }
  1271. }
  1272. func netAddrToProtoV4AddrPort(addr netip.Addr, port uint16) *V4AddrPort {
  1273. v4Addr := addr.As4()
  1274. return &V4AddrPort{
  1275. Addr: binary.BigEndian.Uint32(v4Addr[:]),
  1276. Port: uint32(port),
  1277. }
  1278. }
  1279. func netAddrToProtoV6AddrPort(addr netip.Addr, port uint16) *V6AddrPort {
  1280. v6Addr := addr.As16()
  1281. return &V6AddrPort{
  1282. Hi: binary.BigEndian.Uint64(v6Addr[:8]),
  1283. Lo: binary.BigEndian.Uint64(v6Addr[8:]),
  1284. Port: uint32(port),
  1285. }
  1286. }
  1287. func (d *NebulaMetaDetails) GetRelays() []netip.Addr {
  1288. var relays []netip.Addr
  1289. if len(d.OldRelayVpnAddrs) > 0 {
  1290. b := [4]byte{}
  1291. for _, r := range d.OldRelayVpnAddrs {
  1292. binary.BigEndian.PutUint32(b[:], r)
  1293. relays = append(relays, netip.AddrFrom4(b))
  1294. }
  1295. }
  1296. if len(d.RelayVpnAddrs) > 0 {
  1297. for _, r := range d.RelayVpnAddrs {
  1298. if r != nil {
  1299. relays = append(relays, protoAddrToNetAddr(r))
  1300. }
  1301. }
  1302. }
  1303. return relays
  1304. }
  1305. // FindNetworkUnion returns the first netip.Addr contained in the list of provided netip.Prefix, if able
  1306. func findNetworkUnion(prefixes []netip.Prefix, addrs []netip.Addr) (netip.Addr, bool) {
  1307. for i := range prefixes {
  1308. for j := range addrs {
  1309. if prefixes[i].Contains(addrs[j]) {
  1310. return addrs[j], true
  1311. }
  1312. }
  1313. }
  1314. return netip.Addr{}, false
  1315. }
  1316. func (d *NebulaMetaDetails) GetVpnAddrAndVersion() (netip.Addr, cert.Version, error) {
  1317. if d.OldVpnAddr != 0 {
  1318. b := [4]byte{}
  1319. binary.BigEndian.PutUint32(b[:], d.OldVpnAddr)
  1320. detailsVpnAddr := netip.AddrFrom4(b)
  1321. return detailsVpnAddr, cert.Version1, nil
  1322. } else if d.VpnAddr != nil {
  1323. detailsVpnAddr := protoAddrToNetAddr(d.VpnAddr)
  1324. return detailsVpnAddr, cert.Version2, nil
  1325. } else {
  1326. return netip.Addr{}, cert.Version1, ErrBadDetailsVpnAddr
  1327. }
  1328. }