lighthouse.go 25 KB

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