1
0

lighthouse.go 43 KB

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