lighthouse.go 26 KB

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