lighthouse.go 43 KB

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