lighthouse.go 27 KB

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