peers.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package logic
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/c-robinson/iplib"
  10. "github.com/gravitl/netmaker/database"
  11. "github.com/gravitl/netmaker/logger"
  12. "github.com/gravitl/netmaker/logic/acls/nodeacls"
  13. "github.com/gravitl/netmaker/models"
  14. "github.com/gravitl/netmaker/netclient/ncutils"
  15. "github.com/gravitl/netmaker/servercfg"
  16. "golang.org/x/exp/slices"
  17. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  18. )
  19. // GetPeerUpdate - gets a wireguard peer config for each peer of a node
  20. func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
  21. var peerUpdate models.PeerUpdate
  22. var peers []wgtypes.PeerConfig
  23. var serverNodeAddresses = []models.ServerAddr{}
  24. var isP2S bool
  25. network, err := GetNetwork(node.Network)
  26. if err != nil {
  27. return peerUpdate, err
  28. } else if network.IsPointToSite == "yes" && node.IsHub != "yes" {
  29. isP2S = true
  30. }
  31. // udppeers = the peers parsed from the local interface
  32. // gives us correct port to reach
  33. udppeers, errN := database.GetPeers(node.Network)
  34. if errN != nil {
  35. logger.Log(2, errN.Error())
  36. }
  37. currentPeers, err := GetNetworkNodes(node.Network)
  38. if err != nil {
  39. return models.PeerUpdate{}, err
  40. }
  41. if node.IsRelayed == "yes" {
  42. return GetPeerUpdateForRelayedNode(node, udppeers)
  43. }
  44. // #1 Set Keepalive values: set_keepalive
  45. // #2 Set local address: set_local - could be a LOT BETTER and fix some bugs with additional logic
  46. // #3 Set allowedips: set_allowedips
  47. for _, peer := range currentPeers {
  48. if peer.ID == node.ID {
  49. //skip yourself
  50. continue
  51. }
  52. // on point to site networks -- get peers regularily if you are the hub --- otherwise the only peer is the hub
  53. if node.NetworkSettings.IsPointToSite == "yes" && node.IsHub == "no" && peer.IsHub == "no" {
  54. continue
  55. }
  56. // if the node is not a server, set the endpoint
  57. var setEndpoint = !(node.IsServer == "yes")
  58. if peer.IsRelayed == "yes" {
  59. if !(node.IsRelay == "yes" && ncutils.StringSliceContains(node.RelayAddrs, peer.PrimaryAddress())) {
  60. //skip -- will be added to relay
  61. continue
  62. } else if node.IsRelay == "yes" && ncutils.StringSliceContains(node.RelayAddrs, peer.PrimaryAddress()) {
  63. // dont set peer endpoint if it's relayed by node
  64. setEndpoint = false
  65. }
  66. }
  67. if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(peer.ID)) {
  68. //skip if not permitted by acl
  69. continue
  70. }
  71. if isP2S && peer.IsHub != "yes" {
  72. continue
  73. }
  74. pubkey, err := wgtypes.ParseKey(peer.PublicKey)
  75. if err != nil {
  76. return models.PeerUpdate{}, err
  77. }
  78. if node.Endpoint == peer.Endpoint {
  79. //peer is on same network
  80. // set_local
  81. if node.LocalAddress != peer.LocalAddress && peer.LocalAddress != "" {
  82. peer.Endpoint = peer.LocalAddress
  83. if peer.LocalListenPort != 0 {
  84. peer.ListenPort = peer.LocalListenPort
  85. }
  86. } else {
  87. continue
  88. }
  89. }
  90. // set address if setEndpoint is true
  91. // otherwise, will get inserted as empty value
  92. var address *net.UDPAddr
  93. // Sets ListenPort to UDP Hole Punching Port assuming:
  94. // - UDP Hole Punching is enabled
  95. // - udppeers retrieval did not return an error
  96. // - the endpoint is valid
  97. if setEndpoint {
  98. var setUDPPort = false
  99. if peer.UDPHolePunch == "yes" && errN == nil && CheckEndpoint(udppeers[peer.PublicKey]) {
  100. endpointstring := udppeers[peer.PublicKey]
  101. endpointarr := strings.Split(endpointstring, ":")
  102. if len(endpointarr) == 2 {
  103. port, err := strconv.Atoi(endpointarr[1])
  104. if err == nil {
  105. setUDPPort = true
  106. peer.ListenPort = int32(port)
  107. }
  108. }
  109. }
  110. // if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
  111. // or, if port is for some reason zero use the LocalListenPort
  112. // but only do this if LocalListenPort is not zero
  113. if ((peer.UDPHolePunch == "yes" && !setUDPPort) || peer.ListenPort == 0) && peer.LocalListenPort != 0 {
  114. peer.ListenPort = peer.LocalListenPort
  115. }
  116. endpoint := peer.Endpoint + ":" + strconv.FormatInt(int64(peer.ListenPort), 10)
  117. address, err = net.ResolveUDPAddr("udp", endpoint)
  118. if err != nil {
  119. return models.PeerUpdate{}, err
  120. }
  121. }
  122. // set_allowedips
  123. allowedips := GetAllowedIPs(node, &peer)
  124. var keepalive time.Duration
  125. if node.PersistentKeepalive != 0 {
  126. // set_keepalive
  127. keepalive, _ = time.ParseDuration(strconv.FormatInt(int64(node.PersistentKeepalive), 10) + "s")
  128. }
  129. var peerData = wgtypes.PeerConfig{
  130. PublicKey: pubkey,
  131. Endpoint: address,
  132. ReplaceAllowedIPs: true,
  133. AllowedIPs: allowedips,
  134. PersistentKeepaliveInterval: &keepalive,
  135. }
  136. peers = append(peers, peerData)
  137. if peer.IsServer == "yes" {
  138. serverNodeAddresses = append(serverNodeAddresses, models.ServerAddr{IsLeader: IsLeader(&peer), Address: peer.Address})
  139. }
  140. }
  141. if node.IsIngressGateway == "yes" {
  142. extPeers, err := getExtPeers(node)
  143. if err == nil {
  144. peers = append(peers, extPeers...)
  145. } else if !database.IsEmptyRecord(err) {
  146. logger.Log(1, "error retrieving external clients:", err.Error())
  147. }
  148. }
  149. peerUpdate.Network = node.Network
  150. peerUpdate.ServerVersion = servercfg.Version
  151. peerUpdate.Peers = peers
  152. peerUpdate.ServerAddrs = serverNodeAddresses
  153. peerUpdate.DNS = getPeerDNS(node.Network)
  154. return peerUpdate, nil
  155. }
  156. func getExtPeers(node *models.Node) ([]wgtypes.PeerConfig, error) {
  157. var peers []wgtypes.PeerConfig
  158. extPeers, err := GetExtPeersList(node)
  159. if err != nil {
  160. return peers, err
  161. }
  162. for _, extPeer := range extPeers {
  163. pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
  164. if err != nil {
  165. logger.Log(1, "error parsing ext pub key:", err.Error())
  166. continue
  167. }
  168. if node.PublicKey == extPeer.PublicKey {
  169. continue
  170. }
  171. var allowedips []net.IPNet
  172. var peer wgtypes.PeerConfig
  173. if extPeer.Address != "" {
  174. var peeraddr = net.IPNet{
  175. IP: net.ParseIP(extPeer.Address),
  176. Mask: net.CIDRMask(32, 32),
  177. }
  178. if peeraddr.IP != nil && peeraddr.Mask != nil {
  179. allowedips = append(allowedips, peeraddr)
  180. }
  181. }
  182. if extPeer.Address6 != "" {
  183. var addr6 = net.IPNet{
  184. IP: net.ParseIP(extPeer.Address6),
  185. Mask: net.CIDRMask(128, 128),
  186. }
  187. if addr6.IP != nil && addr6.Mask != nil {
  188. allowedips = append(allowedips, addr6)
  189. }
  190. }
  191. peer = wgtypes.PeerConfig{
  192. PublicKey: pubkey,
  193. ReplaceAllowedIPs: true,
  194. AllowedIPs: allowedips,
  195. }
  196. peers = append(peers, peer)
  197. }
  198. return peers, nil
  199. }
  200. // GetAllowedIPs - calculates the wireguard allowedip field for a peer of a node based on the peer and node settings
  201. func GetAllowedIPs(node, peer *models.Node) []net.IPNet {
  202. var allowedips []net.IPNet
  203. if peer.Address != "" {
  204. var peeraddr = net.IPNet{
  205. IP: net.ParseIP(peer.Address),
  206. Mask: net.CIDRMask(32, 32),
  207. }
  208. allowedips = append(allowedips, peeraddr)
  209. }
  210. if peer.Address6 != "" {
  211. var addr6 = net.IPNet{
  212. IP: net.ParseIP(peer.Address6),
  213. Mask: net.CIDRMask(128, 128),
  214. }
  215. allowedips = append(allowedips, addr6)
  216. }
  217. // handle manually set peers
  218. for _, allowedIp := range peer.AllowedIPs {
  219. // parsing as a CIDR first. If valid CIDR, append
  220. if _, ipnet, err := net.ParseCIDR(allowedIp); err == nil {
  221. nodeEndpointArr := strings.Split(node.Endpoint, ":")
  222. if !ipnet.Contains(net.IP(nodeEndpointArr[0])) && ipnet.IP.String() != peer.Address { // don't need to add an allowed ip that already exists..
  223. allowedips = append(allowedips, *ipnet)
  224. }
  225. } else { // parsing as an IP second. If valid IP, check if ipv4 or ipv6, then append
  226. if iplib.Version(net.ParseIP(allowedIp)) == 4 && allowedIp != peer.Address {
  227. ipnet := net.IPNet{
  228. IP: net.ParseIP(allowedIp),
  229. Mask: net.CIDRMask(32, 32),
  230. }
  231. allowedips = append(allowedips, ipnet)
  232. } else if iplib.Version(net.ParseIP(allowedIp)) == 6 && allowedIp != peer.Address6 {
  233. ipnet := net.IPNet{
  234. IP: net.ParseIP(allowedIp),
  235. Mask: net.CIDRMask(128, 128),
  236. }
  237. allowedips = append(allowedips, ipnet)
  238. }
  239. }
  240. }
  241. // handle egress gateway peers
  242. if peer.IsEgressGateway == "yes" {
  243. //hasGateway = true
  244. egressIPs := getEgressIPs(node, peer)
  245. // remove internet gateway if server
  246. if node.IsServer == "yes" {
  247. for i := len(egressIPs) - 1; i >= 0; i-- {
  248. if egressIPs[i].String() == "0.0.0.0/0" || egressIPs[i].String() == "::/0" {
  249. egressIPs = append(egressIPs[:i], egressIPs[i+1:]...)
  250. }
  251. }
  252. }
  253. allowedips = append(allowedips, egressIPs...)
  254. }
  255. // handle ingress gateway peers
  256. if peer.IsIngressGateway == "yes" {
  257. extPeers, err := getExtPeers(peer)
  258. if err != nil {
  259. logger.Log(2, "could not retrieve ext peers for ", peer.Name, err.Error())
  260. }
  261. for _, extPeer := range extPeers {
  262. allowedips = append(allowedips, extPeer.AllowedIPs...)
  263. }
  264. }
  265. // handle relay gateway peers
  266. if peer.IsRelay == "yes" {
  267. for _, ip := range peer.RelayAddrs {
  268. //find node ID of relayed peer
  269. relayedPeer, err := findNode(ip)
  270. if err != nil {
  271. logger.Log(0, "failed to find node for ip ", ip, err.Error())
  272. continue
  273. }
  274. if relayedPeer == nil {
  275. continue
  276. }
  277. if relayedPeer.ID == node.ID {
  278. //skip self
  279. continue
  280. }
  281. //check if acl permits comms
  282. if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(relayedPeer.ID)) {
  283. continue
  284. }
  285. if iplib.Version(net.ParseIP(ip)) == 4 {
  286. relayAddr := net.IPNet{
  287. IP: net.ParseIP(ip),
  288. Mask: net.CIDRMask(32, 32),
  289. }
  290. allowedips = append(allowedips, relayAddr)
  291. }
  292. if iplib.Version(net.ParseIP(ip)) == 6 {
  293. relayAddr := net.IPNet{
  294. IP: net.ParseIP(ip),
  295. Mask: net.CIDRMask(128, 128),
  296. }
  297. allowedips = append(allowedips, relayAddr)
  298. }
  299. relayedNode, err := findNode(ip)
  300. if err != nil {
  301. logger.Log(1, "unable to find node for relayed address", ip, err.Error())
  302. continue
  303. }
  304. if relayedNode.IsEgressGateway == "yes" {
  305. extAllowedIPs := getEgressIPs(node, relayedNode)
  306. allowedips = append(allowedips, extAllowedIPs...)
  307. }
  308. if relayedNode.IsIngressGateway == "yes" {
  309. extPeers, err := getExtPeers(relayedNode)
  310. if err == nil {
  311. for _, extPeer := range extPeers {
  312. allowedips = append(allowedips, extPeer.AllowedIPs...)
  313. }
  314. } else {
  315. logger.Log(0, "failed to retrieve extclients from relayed ingress", err.Error())
  316. }
  317. }
  318. }
  319. }
  320. return allowedips
  321. }
  322. func getPeerDNS(network string) string {
  323. var dns string
  324. if nodes, err := GetNetworkNodes(network); err == nil {
  325. for i := range nodes {
  326. dns = dns + fmt.Sprintf("%s %s.%s\n", nodes[i].Address, nodes[i].Name, nodes[i].Network)
  327. }
  328. }
  329. if customDNSEntries, err := GetCustomDNS(network); err == nil {
  330. for _, entry := range customDNSEntries {
  331. // TODO - filter entries based on ACLs / given peers vs nodes in network
  332. dns = dns + fmt.Sprintf("%s %s.%s\n", entry.Address, entry.Name, entry.Network)
  333. }
  334. }
  335. return dns
  336. }
  337. // GetPeerUpdateForRelayedNode - calculates peer update for a relayed node by getting the relay
  338. // copying the relay node's allowed ips and making appropriate substitutions
  339. func GetPeerUpdateForRelayedNode(node *models.Node, udppeers map[string]string) (models.PeerUpdate, error) {
  340. var peerUpdate models.PeerUpdate
  341. var peers []wgtypes.PeerConfig
  342. var serverNodeAddresses = []models.ServerAddr{}
  343. var allowedips []net.IPNet
  344. //find node that is relaying us
  345. relay := FindRelay(node)
  346. if relay == nil {
  347. return models.PeerUpdate{}, errors.New("not found")
  348. }
  349. //add relay to lists of allowed ip
  350. if relay.Address != "" {
  351. relayIP := net.IPNet{
  352. IP: net.ParseIP(relay.Address),
  353. Mask: net.CIDRMask(32, 32),
  354. }
  355. allowedips = append(allowedips, relayIP)
  356. }
  357. if relay.Address6 != "" {
  358. relayIP6 := net.IPNet{
  359. IP: net.ParseIP(relay.Address6),
  360. Mask: net.CIDRMask(128, 128),
  361. }
  362. allowedips = append(allowedips, relayIP6)
  363. }
  364. //get PeerUpdate for relayed node
  365. relayPeerUpdate, err := GetPeerUpdate(relay)
  366. if err != nil {
  367. return models.PeerUpdate{}, err
  368. }
  369. //add the relays allowed ips from all of the relay's peers
  370. for _, peer := range relayPeerUpdate.Peers {
  371. allowedips = append(allowedips, peer.AllowedIPs...)
  372. }
  373. //delete any ips not permitted by acl
  374. for i := len(allowedips) - 1; i >= 0; i-- {
  375. target, err := findNode(allowedips[i].IP.String())
  376. if err != nil {
  377. logger.Log(0, "failed to find node for ip", allowedips[i].IP.String(), err.Error())
  378. continue
  379. }
  380. if target == nil {
  381. logger.Log(0, "failed to find node for ip", allowedips[i].IP.String())
  382. continue
  383. }
  384. if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(target.ID)) {
  385. logger.Log(0, "deleting node from relayednode per acl", node.Name, target.Name)
  386. allowedips = append(allowedips[:i], allowedips[i+1:]...)
  387. }
  388. }
  389. //delete self from allowed ips
  390. for i := len(allowedips) - 1; i >= 0; i-- {
  391. if allowedips[i].IP.String() == node.Address || allowedips[i].IP.String() == node.Address6 {
  392. allowedips = append(allowedips[:i], allowedips[i+1:]...)
  393. }
  394. }
  395. //delete egressrange from allowedip if we are egress gateway
  396. if node.IsEgressGateway == "yes" {
  397. for i := len(allowedips) - 1; i >= 0; i-- {
  398. if StringSliceContains(node.EgressGatewayRanges, allowedips[i].String()) {
  399. allowedips = append(allowedips[:i], allowedips[i+1:]...)
  400. }
  401. }
  402. }
  403. //delete extclients from allowedip if we are ingress gateway
  404. if node.IsIngressGateway == "yes" {
  405. for i := len(allowedips) - 1; i >= 0; i-- {
  406. if strings.Contains(node.IngressGatewayRange, allowedips[i].IP.String()) {
  407. allowedips = append(allowedips[:i], allowedips[i+1:]...)
  408. }
  409. }
  410. }
  411. pubkey, err := wgtypes.ParseKey(relay.PublicKey)
  412. if err != nil {
  413. return models.PeerUpdate{}, err
  414. }
  415. var setUDPPort = false
  416. if relay.UDPHolePunch == "yes" && CheckEndpoint(udppeers[relay.PublicKey]) {
  417. endpointstring := udppeers[relay.PublicKey]
  418. endpointarr := strings.Split(endpointstring, ":")
  419. if len(endpointarr) == 2 {
  420. port, err := strconv.Atoi(endpointarr[1])
  421. if err == nil {
  422. setUDPPort = true
  423. relay.ListenPort = int32(port)
  424. }
  425. }
  426. }
  427. // if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
  428. // or, if port is for some reason zero use the LocalListenPort
  429. // but only do this if LocalListenPort is not zero
  430. if ((relay.UDPHolePunch == "yes" && !setUDPPort) || relay.ListenPort == 0) && relay.LocalListenPort != 0 {
  431. relay.ListenPort = relay.LocalListenPort
  432. }
  433. endpoint := relay.Endpoint + ":" + strconv.FormatInt(int64(relay.ListenPort), 10)
  434. address, err := net.ResolveUDPAddr("udp", endpoint)
  435. if err != nil {
  436. return models.PeerUpdate{}, err
  437. }
  438. var keepalive time.Duration
  439. if node.PersistentKeepalive != 0 {
  440. // set_keepalive
  441. keepalive, _ = time.ParseDuration(strconv.FormatInt(int64(node.PersistentKeepalive), 10) + "s")
  442. }
  443. var peerData = wgtypes.PeerConfig{
  444. PublicKey: pubkey,
  445. Endpoint: address,
  446. ReplaceAllowedIPs: true,
  447. AllowedIPs: allowedips,
  448. PersistentKeepaliveInterval: &keepalive,
  449. }
  450. peers = append(peers, peerData)
  451. if relay.IsServer == "yes" {
  452. serverNodeAddresses = append(serverNodeAddresses, models.ServerAddr{IsLeader: IsLeader(relay), Address: relay.Address})
  453. }
  454. //if ingress add extclients
  455. if node.IsIngressGateway == "yes" {
  456. extPeers, err := getExtPeers(node)
  457. if err == nil {
  458. peers = append(peers, extPeers...)
  459. } else {
  460. logger.Log(2, "could not retrieve ext peers for ", node.Name, err.Error())
  461. }
  462. }
  463. peerUpdate.Network = node.Network
  464. peerUpdate.ServerVersion = servercfg.Version
  465. peerUpdate.Peers = peers
  466. peerUpdate.ServerAddrs = serverNodeAddresses
  467. peerUpdate.DNS = getPeerDNS(node.Network)
  468. return peerUpdate, nil
  469. }
  470. func getEgressIPs(node, peer *models.Node) []net.IPNet {
  471. //check for internet gateway
  472. internetGateway := false
  473. if slices.Contains(peer.EgressGatewayRanges, "0.0.0.0/0") || slices.Contains(peer.EgressGatewayRanges, "::/0") {
  474. internetGateway = true
  475. }
  476. allowedips := []net.IPNet{}
  477. for _, iprange := range peer.EgressGatewayRanges { // go through each cidr for egress gateway
  478. _, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
  479. if err != nil {
  480. logger.Log(1, "could not parse gateway IP range. Not adding ", iprange)
  481. continue // if can't parse CIDR
  482. }
  483. nodeEndpointArr := strings.Split(peer.Endpoint, ":") // getting the public ip of node
  484. if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) && !internetGateway { // ensuring egress gateway range does not contain endpoint of node
  485. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.Endpoint, ", omitting")
  486. continue // skip adding egress range if overlaps with node's ip
  487. }
  488. // TODO: Could put in a lot of great logic to avoid conflicts / bad routes
  489. if ipnet.Contains(net.ParseIP(node.LocalAddress)) && !internetGateway { // ensuring egress gateway range does not contain public ip of node
  490. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.LocalAddress, ", omitting")
  491. continue // skip adding egress range if overlaps with node's local ip
  492. }
  493. if err != nil {
  494. logger.Log(1, "error encountered when setting egress range", err.Error())
  495. } else {
  496. allowedips = append(allowedips, *ipnet)
  497. }
  498. }
  499. return allowedips
  500. }