lighthouse.go 42 KB

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