lighthouse.go 25 KB

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