lighthouse.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package nebula
  2. import (
  3. "fmt"
  4. "net"
  5. "sync"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/rcrowley/go-metrics"
  9. "github.com/slackhq/nebula/cert"
  10. )
  11. type LightHouse struct {
  12. sync.RWMutex //Because we concurrently read and write to our maps
  13. amLighthouse bool
  14. myIp uint32
  15. punchConn *udpConn
  16. // Local cache of answers from light houses
  17. addrMap map[uint32][]udpAddr
  18. // filters remote addresses allowed for each host
  19. // - When we are a lighthouse, this filters what addresses we store and
  20. // respond with.
  21. // - When we are not a lighthouse, this filters which addresses we accept
  22. // from lighthouses.
  23. remoteAllowList *AllowList
  24. // filters local addresses that we advertise to lighthouses
  25. localAllowList *AllowList
  26. // used to trigger the HandshakeManager when we receive HostQueryReply
  27. handshakeTrigger chan<- uint32
  28. // staticList exists to avoid having a bool in each addrMap entry
  29. // since static should be rare
  30. staticList map[uint32]struct{}
  31. lighthouses map[uint32]struct{}
  32. interval int
  33. nebulaPort int
  34. punchBack bool
  35. punchDelay time.Duration
  36. metrics *MessageMetrics
  37. metricHolepunchTx metrics.Counter
  38. }
  39. type EncWriter interface {
  40. SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte)
  41. SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte)
  42. }
  43. func NewLightHouse(amLighthouse bool, myIp uint32, ips []uint32, interval int, nebulaPort int, pc *udpConn, punchBack bool, punchDelay time.Duration, metricsEnabled bool) *LightHouse {
  44. h := LightHouse{
  45. amLighthouse: amLighthouse,
  46. myIp: myIp,
  47. addrMap: make(map[uint32][]udpAddr),
  48. nebulaPort: nebulaPort,
  49. lighthouses: make(map[uint32]struct{}),
  50. staticList: make(map[uint32]struct{}),
  51. interval: interval,
  52. punchConn: pc,
  53. punchBack: punchBack,
  54. punchDelay: punchDelay,
  55. }
  56. if metricsEnabled {
  57. h.metrics = newLighthouseMetrics()
  58. h.metricHolepunchTx = metrics.GetOrRegisterCounter("messages.tx.holepunch", nil)
  59. } else {
  60. h.metricHolepunchTx = metrics.NilCounter{}
  61. }
  62. for _, ip := range ips {
  63. h.lighthouses[ip] = struct{}{}
  64. }
  65. return &h
  66. }
  67. func (lh *LightHouse) SetRemoteAllowList(allowList *AllowList) {
  68. lh.Lock()
  69. defer lh.Unlock()
  70. lh.remoteAllowList = allowList
  71. }
  72. func (lh *LightHouse) SetLocalAllowList(allowList *AllowList) {
  73. lh.Lock()
  74. defer lh.Unlock()
  75. lh.localAllowList = allowList
  76. }
  77. func (lh *LightHouse) ValidateLHStaticEntries() error {
  78. for lhIP, _ := range lh.lighthouses {
  79. if _, ok := lh.staticList[lhIP]; !ok {
  80. return fmt.Errorf("Lighthouse %s does not have a static_host_map entry", IntIp(lhIP))
  81. }
  82. }
  83. return nil
  84. }
  85. func (lh *LightHouse) Query(ip uint32, f EncWriter) ([]udpAddr, error) {
  86. if !lh.IsLighthouseIP(ip) {
  87. lh.QueryServer(ip, f)
  88. }
  89. lh.RLock()
  90. if v, ok := lh.addrMap[ip]; ok {
  91. lh.RUnlock()
  92. return v, nil
  93. }
  94. lh.RUnlock()
  95. return nil, fmt.Errorf("host %s not known, queries sent to lighthouses", IntIp(ip))
  96. }
  97. // This is asynchronous so no reply should be expected
  98. func (lh *LightHouse) QueryServer(ip uint32, f EncWriter) {
  99. if !lh.amLighthouse {
  100. // Send a query to the lighthouses and hope for the best next time
  101. query, err := proto.Marshal(NewLhQueryByInt(ip))
  102. if err != nil {
  103. l.WithError(err).WithField("vpnIp", IntIp(ip)).Error("Failed to marshal lighthouse query payload")
  104. return
  105. }
  106. lh.metricTx(NebulaMeta_HostQuery, int64(len(lh.lighthouses)))
  107. nb := make([]byte, 12, 12)
  108. out := make([]byte, mtu)
  109. for n := range lh.lighthouses {
  110. f.SendMessageToVpnIp(lightHouse, 0, n, query, nb, out)
  111. }
  112. }
  113. }
  114. // Query our local lighthouse cached results
  115. func (lh *LightHouse) QueryCache(ip uint32) []udpAddr {
  116. lh.RLock()
  117. if v, ok := lh.addrMap[ip]; ok {
  118. lh.RUnlock()
  119. return v
  120. }
  121. lh.RUnlock()
  122. return nil
  123. }
  124. func (lh *LightHouse) DeleteVpnIP(vpnIP uint32) {
  125. // First we check the static mapping
  126. // and do nothing if it is there
  127. if _, ok := lh.staticList[vpnIP]; ok {
  128. return
  129. }
  130. lh.Lock()
  131. //l.Debugln(lh.addrMap)
  132. delete(lh.addrMap, vpnIP)
  133. l.Debugf("deleting %s from lighthouse.", IntIp(vpnIP))
  134. lh.Unlock()
  135. }
  136. func (lh *LightHouse) AddRemote(vpnIP uint32, toIp *udpAddr, static bool) {
  137. // First we check if the sender thinks this is a static entry
  138. // and do nothing if it is not, but should be considered static
  139. if static == false {
  140. if _, ok := lh.staticList[vpnIP]; ok {
  141. return
  142. }
  143. }
  144. lh.Lock()
  145. for _, v := range lh.addrMap[vpnIP] {
  146. if v.Equals(toIp) {
  147. lh.Unlock()
  148. return
  149. }
  150. }
  151. allow := lh.remoteAllowList.Allow(udp2ipInt(toIp))
  152. l.WithField("remoteIp", toIp).WithField("allow", allow).Debug("remoteAllowList.Allow")
  153. if !allow {
  154. return
  155. }
  156. //l.Debugf("Adding reply of %s as %s\n", IntIp(vpnIP), toIp)
  157. if static {
  158. lh.staticList[vpnIP] = struct{}{}
  159. }
  160. lh.addrMap[vpnIP] = append(lh.addrMap[vpnIP], *toIp)
  161. lh.Unlock()
  162. }
  163. func (lh *LightHouse) AddRemoteAndReset(vpnIP uint32, toIp *udpAddr) {
  164. if lh.amLighthouse {
  165. lh.DeleteVpnIP(vpnIP)
  166. lh.AddRemote(vpnIP, toIp, false)
  167. }
  168. }
  169. func (lh *LightHouse) IsLighthouseIP(vpnIP uint32) bool {
  170. if _, ok := lh.lighthouses[vpnIP]; ok {
  171. return true
  172. }
  173. return false
  174. }
  175. // Quick generators for protobuf
  176. func NewLhQueryByIpString(VpnIp string) *NebulaMeta {
  177. return NewLhQueryByInt(ip2int(net.ParseIP(VpnIp)))
  178. }
  179. func NewLhQueryByInt(VpnIp uint32) *NebulaMeta {
  180. return &NebulaMeta{
  181. Type: NebulaMeta_HostQuery,
  182. Details: &NebulaMetaDetails{
  183. VpnIp: VpnIp,
  184. },
  185. }
  186. }
  187. func NewLhWhoami() *NebulaMeta {
  188. return &NebulaMeta{
  189. Type: NebulaMeta_HostWhoami,
  190. Details: &NebulaMetaDetails{},
  191. }
  192. }
  193. // End Quick generators for protobuf
  194. func NewIpAndPortFromUDPAddr(addr udpAddr) *IpAndPort {
  195. return &IpAndPort{Ip: udp2ipInt(&addr), Port: uint32(addr.Port)}
  196. }
  197. func NewIpAndPortsFromNetIps(ips []udpAddr) *[]*IpAndPort {
  198. var iap []*IpAndPort
  199. for _, e := range ips {
  200. // Only add IPs that aren't my VPN/tun IP
  201. iap = append(iap, NewIpAndPortFromUDPAddr(e))
  202. }
  203. return &iap
  204. }
  205. func (lh *LightHouse) LhUpdateWorker(f EncWriter) {
  206. if lh.amLighthouse || lh.interval == 0 {
  207. return
  208. }
  209. for {
  210. ipp := []*IpAndPort{}
  211. for _, e := range *localIps(lh.localAllowList) {
  212. // Only add IPs that aren't my VPN/tun IP
  213. if ip2int(e) != lh.myIp {
  214. ipp = append(ipp, &IpAndPort{Ip: ip2int(e), Port: uint32(lh.nebulaPort)})
  215. //fmt.Println(e)
  216. }
  217. }
  218. m := &NebulaMeta{
  219. Type: NebulaMeta_HostUpdateNotification,
  220. Details: &NebulaMetaDetails{
  221. VpnIp: lh.myIp,
  222. IpAndPorts: ipp,
  223. },
  224. }
  225. lh.metricTx(NebulaMeta_HostUpdateNotification, int64(len(lh.lighthouses)))
  226. nb := make([]byte, 12, 12)
  227. out := make([]byte, mtu)
  228. for vpnIp := range lh.lighthouses {
  229. mm, err := proto.Marshal(m)
  230. if err != nil {
  231. l.Debugf("Invalid marshal to update")
  232. }
  233. //l.Error("LIGHTHOUSE PACKET SEND", mm)
  234. f.SendMessageToVpnIp(lightHouse, 0, vpnIp, mm, nb, out)
  235. }
  236. time.Sleep(time.Second * time.Duration(lh.interval))
  237. }
  238. }
  239. func (lh *LightHouse) HandleRequest(rAddr *udpAddr, vpnIp uint32, p []byte, c *cert.NebulaCertificate, f EncWriter) {
  240. n := &NebulaMeta{}
  241. err := proto.Unmarshal(p, n)
  242. if err != nil {
  243. l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).WithField("udpAddr", rAddr).
  244. Error("Failed to unmarshal lighthouse packet")
  245. //TODO: send recv_error?
  246. return
  247. }
  248. if n.Details == nil {
  249. l.WithField("vpnIp", IntIp(vpnIp)).WithField("udpAddr", rAddr).
  250. Error("Invalid lighthouse update")
  251. //TODO: send recv_error?
  252. return
  253. }
  254. lh.metricRx(n.Type, 1)
  255. switch n.Type {
  256. case NebulaMeta_HostQuery:
  257. // Exit if we don't answer queries
  258. if !lh.amLighthouse {
  259. l.Debugln("I don't answer queries, but received from: ", rAddr)
  260. return
  261. }
  262. //l.Debugln("Got Query")
  263. ips, err := lh.Query(n.Details.VpnIp, f)
  264. if err != nil {
  265. //l.Debugf("Can't answer query %s from %s because error: %s", IntIp(n.Details.VpnIp), rAddr, err)
  266. return
  267. } else {
  268. iap := NewIpAndPortsFromNetIps(ips)
  269. answer := &NebulaMeta{
  270. Type: NebulaMeta_HostQueryReply,
  271. Details: &NebulaMetaDetails{
  272. VpnIp: n.Details.VpnIp,
  273. IpAndPorts: *iap,
  274. },
  275. }
  276. reply, err := proto.Marshal(answer)
  277. if err != nil {
  278. l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).Error("Failed to marshal lighthouse host query reply")
  279. return
  280. }
  281. lh.metricTx(NebulaMeta_HostQueryReply, 1)
  282. f.SendMessageToVpnIp(lightHouse, 0, vpnIp, reply, make([]byte, 12, 12), make([]byte, mtu))
  283. // This signals the other side to punch some zero byte udp packets
  284. ips, err = lh.Query(vpnIp, f)
  285. if err != nil {
  286. l.WithField("vpnIp", IntIp(vpnIp)).Debugln("Can't notify host to punch")
  287. return
  288. } else {
  289. //l.Debugln("Notify host to punch", iap)
  290. iap = NewIpAndPortsFromNetIps(ips)
  291. answer = &NebulaMeta{
  292. Type: NebulaMeta_HostPunchNotification,
  293. Details: &NebulaMetaDetails{
  294. VpnIp: vpnIp,
  295. IpAndPorts: *iap,
  296. },
  297. }
  298. reply, _ := proto.Marshal(answer)
  299. lh.metricTx(NebulaMeta_HostPunchNotification, 1)
  300. f.SendMessageToVpnIp(lightHouse, 0, n.Details.VpnIp, reply, make([]byte, 12, 12), make([]byte, mtu))
  301. }
  302. //fmt.Println(reply, remoteaddr)
  303. }
  304. case NebulaMeta_HostQueryReply:
  305. if !lh.IsLighthouseIP(vpnIp) {
  306. return
  307. }
  308. for _, a := range n.Details.IpAndPorts {
  309. //first := n.Details.IpAndPorts[0]
  310. ans := NewUDPAddr(a.Ip, uint16(a.Port))
  311. lh.AddRemote(n.Details.VpnIp, ans, false)
  312. }
  313. // Non-blocking attempt to trigger, skip if it would block
  314. select {
  315. case lh.handshakeTrigger <- n.Details.VpnIp:
  316. default:
  317. }
  318. case NebulaMeta_HostUpdateNotification:
  319. //Simple check that the host sent this not someone else
  320. if n.Details.VpnIp != vpnIp {
  321. l.WithField("vpnIp", IntIp(vpnIp)).WithField("answer", IntIp(n.Details.VpnIp)).Debugln("Host sent invalid update")
  322. return
  323. }
  324. for _, a := range n.Details.IpAndPorts {
  325. ans := NewUDPAddr(a.Ip, uint16(a.Port))
  326. lh.AddRemote(n.Details.VpnIp, ans, false)
  327. }
  328. case NebulaMeta_HostMovedNotification:
  329. case NebulaMeta_HostPunchNotification:
  330. if !lh.IsLighthouseIP(vpnIp) {
  331. return
  332. }
  333. empty := []byte{0}
  334. for _, a := range n.Details.IpAndPorts {
  335. vpnPeer := NewUDPAddr(a.Ip, uint16(a.Port))
  336. go func() {
  337. time.Sleep(lh.punchDelay)
  338. lh.metricHolepunchTx.Inc(1)
  339. lh.punchConn.WriteTo(empty, vpnPeer)
  340. }()
  341. l.Debugf("Punching %s on %d for %s", IntIp(a.Ip), a.Port, IntIp(n.Details.VpnIp))
  342. }
  343. // This sends a nebula test packet to the host trying to contact us. In the case
  344. // of a double nat or other difficult scenario, this may help establish
  345. // a tunnel.
  346. if lh.punchBack {
  347. go func() {
  348. time.Sleep(time.Second * 5)
  349. l.Debugf("Sending a nebula test packet to vpn ip %s", IntIp(n.Details.VpnIp))
  350. f.SendMessageToVpnIp(test, testRequest, n.Details.VpnIp, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  351. }()
  352. }
  353. }
  354. }
  355. func (lh *LightHouse) metricRx(t NebulaMeta_MessageType, i int64) {
  356. lh.metrics.Rx(NebulaMessageType(t), 0, i)
  357. }
  358. func (lh *LightHouse) metricTx(t NebulaMeta_MessageType, i int64) {
  359. lh.metrics.Tx(NebulaMessageType(t), 0, i)
  360. }
  361. /*
  362. func (f *Interface) sendPathCheck(ci *ConnectionState, endpoint *net.UDPAddr, counter int) {
  363. c := ci.messageCounter
  364. b := HeaderEncode(nil, Version, uint8(path_check), 0, ci.remoteIndex, c)
  365. ci.messageCounter++
  366. if ci.eKey != nil {
  367. msg := ci.eKey.EncryptDanger(b, nil, []byte(strconv.Itoa(counter)), c)
  368. //msg := ci.eKey.EncryptDanger(b, nil, []byte(fmt.Sprintf("%d", counter)), c)
  369. f.outside.WriteTo(msg, endpoint)
  370. l.Debugf("path_check sent, remote index: %d, pathCounter %d", ci.remoteIndex, counter)
  371. }
  372. }
  373. func (f *Interface) sendPathCheckReply(ci *ConnectionState, endpoint *net.UDPAddr, counter []byte) {
  374. c := ci.messageCounter
  375. b := HeaderEncode(nil, Version, uint8(path_check_reply), 0, ci.remoteIndex, c)
  376. ci.messageCounter++
  377. if ci.eKey != nil {
  378. msg := ci.eKey.EncryptDanger(b, nil, counter, c)
  379. f.outside.WriteTo(msg, endpoint)
  380. l.Debugln("path_check sent, remote index: ", ci.remoteIndex)
  381. }
  382. }
  383. */