lighthouse.go 11 KB

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