peers.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package logic
  2. import (
  3. "errors"
  4. "net"
  5. "net/netip"
  6. "github.com/gravitl/netmaker/database"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/logic/acls/nodeacls"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/servercfg"
  11. "golang.org/x/exp/slices"
  12. "golang.org/x/exp/slog"
  13. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  14. )
  15. // GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
  16. func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.Node,
  17. deletedNode *models.Node, deletedClients []models.ExtClient) (models.HostPeerUpdate, error) {
  18. if host == nil {
  19. return models.HostPeerUpdate{}, errors.New("host is nil")
  20. }
  21. // track which nodes are deleted
  22. // after peer calculation, if peer not in list, add delete config of peer
  23. hostPeerUpdate := models.HostPeerUpdate{
  24. Host: *host,
  25. Server: servercfg.GetServer(),
  26. ServerVersion: servercfg.GetVersion(),
  27. ServerAddrs: []models.ServerAddr{},
  28. FwUpdate: models.FwUpdate{
  29. EgressInfo: make(map[string]models.EgressInfo),
  30. },
  31. PeerIDs: make(models.PeerMap, 0),
  32. Peers: []wgtypes.PeerConfig{},
  33. NodePeers: []wgtypes.PeerConfig{},
  34. HostNetworkInfo: models.HostInfoMap{},
  35. }
  36. // endpoint detection always comes from the server
  37. hostPeerUpdate.EndpointDetection = servercfg.EndpointDetectionEnabled()
  38. slog.Debug("peer update for host", "hostId", host.ID.String())
  39. peerIndexMap := make(map[string]int)
  40. for _, nodeID := range host.Nodes {
  41. nodeID := nodeID
  42. node, err := GetNodeByID(nodeID)
  43. if err != nil {
  44. continue
  45. }
  46. if !node.Connected || node.PendingDelete || node.Action == models.NODE_DELETE {
  47. continue
  48. }
  49. if host.OS == models.OS_Types.IoT {
  50. hostPeerUpdate.NodeAddrs = append(hostPeerUpdate.NodeAddrs, node.PrimaryAddressIPNet())
  51. if node.IsRelayed {
  52. relayNode, err := GetNodeByID(node.RelayedBy)
  53. if err != nil {
  54. continue
  55. }
  56. relayHost, err := GetHost(relayNode.HostID.String())
  57. if err != nil {
  58. continue
  59. }
  60. relayPeer := wgtypes.PeerConfig{
  61. PublicKey: relayHost.PublicKey,
  62. PersistentKeepaliveInterval: &relayNode.PersistentKeepalive,
  63. ReplaceAllowedIPs: true,
  64. AllowedIPs: GetAllowedIPs(&node, &relayNode, nil),
  65. }
  66. uselocal := false
  67. if host.EndpointIP.String() == relayHost.EndpointIP.String() {
  68. // peer is on same network
  69. // set to localaddress
  70. uselocal = true
  71. if node.LocalAddress.IP == nil {
  72. // use public endpint
  73. uselocal = false
  74. }
  75. if node.LocalAddress.String() == relayNode.LocalAddress.String() {
  76. uselocal = false
  77. }
  78. }
  79. relayPeer.Endpoint = &net.UDPAddr{
  80. IP: relayHost.EndpointIP,
  81. Port: GetPeerListenPort(relayHost),
  82. }
  83. if uselocal {
  84. relayPeer.Endpoint.IP = relayNode.LocalAddress.IP
  85. relayPeer.Endpoint.Port = relayHost.ListenPort
  86. }
  87. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  88. } else if deletedNode != nil && deletedNode.IsRelay {
  89. relayHost, err := GetHost(deletedNode.HostID.String())
  90. if err != nil {
  91. continue
  92. }
  93. relayPeer := wgtypes.PeerConfig{
  94. PublicKey: relayHost.PublicKey,
  95. Remove: true,
  96. }
  97. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  98. }
  99. continue
  100. }
  101. currentPeers := GetNetworkNodesMemory(allNodes, node.Network)
  102. for _, peer := range currentPeers {
  103. peer := peer
  104. if peer.ID.String() == node.ID.String() {
  105. logger.Log(2, "peer update, skipping self")
  106. //skip yourself
  107. continue
  108. }
  109. peerHost, err := GetHost(peer.HostID.String())
  110. if err != nil {
  111. logger.Log(1, "no peer host", peer.HostID.String(), err.Error())
  112. return models.HostPeerUpdate{}, err
  113. }
  114. peerConfig := wgtypes.PeerConfig{
  115. PublicKey: peerHost.PublicKey,
  116. PersistentKeepaliveInterval: &peer.PersistentKeepalive,
  117. ReplaceAllowedIPs: true,
  118. }
  119. if peer.IsEgressGateway {
  120. hostPeerUpdate.EgressRoutes = append(hostPeerUpdate.EgressRoutes, models.EgressNetworkRoutes{
  121. NodeAddr: node.PrimaryAddressIPNet(),
  122. EgressRanges: peer.EgressGatewayRanges,
  123. })
  124. }
  125. if (node.IsRelayed && node.RelayedBy != peer.ID.String()) || (peer.IsRelayed && peer.RelayedBy != node.ID.String()) {
  126. // if node is relayed and peer is not the relay, set remove to true
  127. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; ok {
  128. continue
  129. }
  130. peerConfig.Remove = true
  131. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
  132. peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
  133. continue
  134. }
  135. uselocal := false
  136. if host.EndpointIP.String() == peerHost.EndpointIP.String() {
  137. // peer is on same network
  138. // set to localaddress
  139. uselocal = true
  140. if node.LocalAddress.IP == nil {
  141. // use public endpint
  142. uselocal = false
  143. }
  144. if node.LocalAddress.String() == peer.LocalAddress.String() {
  145. uselocal = false
  146. }
  147. }
  148. peerConfig.Endpoint = &net.UDPAddr{
  149. IP: peerHost.EndpointIP,
  150. Port: GetPeerListenPort(peerHost),
  151. }
  152. if uselocal {
  153. peerConfig.Endpoint.IP = peer.LocalAddress.IP
  154. peerConfig.Endpoint.Port = peerHost.ListenPort
  155. }
  156. allowedips := GetAllowedIPs(&node, &peer, nil)
  157. if peer.Action != models.NODE_DELETE &&
  158. !peer.PendingDelete &&
  159. peer.Connected &&
  160. nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), nodeacls.NodeID(peer.ID.String())) &&
  161. (deletedNode == nil || (deletedNode != nil && peer.ID.String() != deletedNode.ID.String())) {
  162. peerConfig.AllowedIPs = allowedips // only append allowed IPs if valid connection
  163. }
  164. peerPort := GetPeerListenPort(peerHost)
  165. var nodePeer wgtypes.PeerConfig
  166. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
  167. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
  168. peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
  169. hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
  170. Interfaces: peerHost.Interfaces,
  171. ListenPort: peerPort,
  172. IsStatic: peerHost.IsStatic,
  173. }
  174. nodePeer = peerConfig
  175. } else {
  176. peerAllowedIPs := hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs
  177. peerAllowedIPs = append(peerAllowedIPs, peerConfig.AllowedIPs...)
  178. hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
  179. hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].Remove = false
  180. hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
  181. Interfaces: peerHost.Interfaces,
  182. ListenPort: peerPort,
  183. IsStatic: peerHost.IsStatic,
  184. }
  185. nodePeer = hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]]
  186. }
  187. if node.Network == network { // add to peers map for metrics
  188. hostPeerUpdate.PeerIDs[peerHost.PublicKey.String()] = models.IDandAddr{
  189. ID: peer.ID.String(),
  190. Address: peer.PrimaryAddress(),
  191. Name: peerHost.Name,
  192. Network: peer.Network,
  193. ListenPort: peerHost.ListenPort,
  194. }
  195. hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, nodePeer)
  196. }
  197. }
  198. var extPeers []wgtypes.PeerConfig
  199. var extPeerIDAndAddrs []models.IDandAddr
  200. if node.IsIngressGateway {
  201. extPeers, extPeerIDAndAddrs, err = getExtPeers(&node, &node)
  202. if err == nil {
  203. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, extPeers...)
  204. for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
  205. extPeerIdAndAddr := extPeerIdAndAddr
  206. if node.Network == network {
  207. hostPeerUpdate.PeerIDs[extPeerIdAndAddr.ID] = extPeerIdAndAddr
  208. hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, extPeers...)
  209. }
  210. }
  211. } else if !database.IsEmptyRecord(err) {
  212. logger.Log(1, "error retrieving external clients:", err.Error())
  213. }
  214. }
  215. if node.IsEgressGateway && node.EgressGatewayRequest.NatEnabled == "yes" && len(node.EgressGatewayRequest.Ranges) > 0 {
  216. hostPeerUpdate.FwUpdate.IsEgressGw = true
  217. hostPeerUpdate.FwUpdate.EgressInfo[node.ID.String()] = models.EgressInfo{
  218. EgressID: node.ID.String(),
  219. Network: node.PrimaryNetworkRange(),
  220. EgressGwAddr: net.IPNet{
  221. IP: net.ParseIP(node.PrimaryAddress()),
  222. Mask: getCIDRMaskFromAddr(node.PrimaryAddress()),
  223. },
  224. EgressGWCfg: node.EgressGatewayRequest,
  225. }
  226. }
  227. }
  228. // == post peer calculations ==
  229. // indicate removal if no allowed IPs were calculated
  230. for i := range hostPeerUpdate.Peers {
  231. peer := hostPeerUpdate.Peers[i]
  232. if len(peer.AllowedIPs) == 0 {
  233. peer.Remove = true
  234. }
  235. hostPeerUpdate.Peers[i] = peer
  236. }
  237. if deletedNode != nil && host.OS != models.OS_Types.IoT {
  238. peerHost, err := GetHost(deletedNode.HostID.String())
  239. if err == nil && host.ID != peerHost.ID {
  240. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
  241. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  242. PublicKey: peerHost.PublicKey,
  243. Remove: true,
  244. })
  245. }
  246. }
  247. }
  248. for i := range hostPeerUpdate.NodePeers {
  249. peer := hostPeerUpdate.NodePeers[i]
  250. if len(peer.AllowedIPs) == 0 {
  251. peer.Remove = true
  252. }
  253. hostPeerUpdate.NodePeers[i] = peer
  254. }
  255. if len(deletedClients) > 0 {
  256. for i := range deletedClients {
  257. deletedClient := deletedClients[i]
  258. key, err := wgtypes.ParseKey(deletedClient.PublicKey)
  259. if err == nil {
  260. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  261. PublicKey: key,
  262. Remove: true,
  263. })
  264. }
  265. }
  266. }
  267. return hostPeerUpdate, nil
  268. }
  269. // GetPeerListenPort - given a host, retrieve it's appropriate listening port
  270. func GetPeerListenPort(host *models.Host) int {
  271. peerPort := host.ListenPort
  272. if host.WgPublicListenPort != 0 {
  273. peerPort = host.WgPublicListenPort
  274. }
  275. return peerPort
  276. }
  277. func getExtPeers(node, peer *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, error) {
  278. var peers []wgtypes.PeerConfig
  279. var idsAndAddr []models.IDandAddr
  280. extPeers, err := GetNetworkExtClients(node.Network)
  281. if err != nil {
  282. return peers, idsAndAddr, err
  283. }
  284. host, err := GetHost(node.HostID.String())
  285. if err != nil {
  286. return peers, idsAndAddr, err
  287. }
  288. for _, extPeer := range extPeers {
  289. extPeer := extPeer
  290. if !IsClientNodeAllowed(&extPeer, peer.ID.String()) {
  291. continue
  292. }
  293. pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
  294. if err != nil {
  295. logger.Log(1, "error parsing ext pub key:", err.Error())
  296. continue
  297. }
  298. if host.PublicKey.String() == extPeer.PublicKey ||
  299. extPeer.IngressGatewayID != node.ID.String() || !extPeer.Enabled {
  300. continue
  301. }
  302. var allowedips []net.IPNet
  303. var peer wgtypes.PeerConfig
  304. if extPeer.Address != "" {
  305. var peeraddr = net.IPNet{
  306. IP: net.ParseIP(extPeer.Address),
  307. Mask: net.CIDRMask(32, 32),
  308. }
  309. if peeraddr.IP != nil && peeraddr.Mask != nil {
  310. allowedips = append(allowedips, peeraddr)
  311. }
  312. }
  313. if extPeer.Address6 != "" {
  314. var addr6 = net.IPNet{
  315. IP: net.ParseIP(extPeer.Address6),
  316. Mask: net.CIDRMask(128, 128),
  317. }
  318. if addr6.IP != nil && addr6.Mask != nil {
  319. allowedips = append(allowedips, addr6)
  320. }
  321. }
  322. primaryAddr := extPeer.Address
  323. if primaryAddr == "" {
  324. primaryAddr = extPeer.Address6
  325. }
  326. peer = wgtypes.PeerConfig{
  327. PublicKey: pubkey,
  328. ReplaceAllowedIPs: true,
  329. AllowedIPs: allowedips,
  330. }
  331. peers = append(peers, peer)
  332. idsAndAddr = append(idsAndAddr, models.IDandAddr{
  333. ID: peer.PublicKey.String(),
  334. Name: extPeer.ClientID,
  335. Address: primaryAddr,
  336. IsExtClient: true,
  337. })
  338. }
  339. return peers, idsAndAddr, nil
  340. }
  341. // GetAllowedIPs - calculates the wireguard allowedip field for a peer of a node based on the peer and node settings
  342. func GetAllowedIPs(node, peer *models.Node, metrics *models.Metrics) []net.IPNet {
  343. var allowedips []net.IPNet
  344. allowedips = getNodeAllowedIPs(peer, node)
  345. // handle ingress gateway peers
  346. if peer.IsIngressGateway {
  347. extPeers, _, err := getExtPeers(peer, node)
  348. if err != nil {
  349. logger.Log(2, "could not retrieve ext peers for ", peer.ID.String(), err.Error())
  350. }
  351. for _, extPeer := range extPeers {
  352. allowedips = append(allowedips, extPeer.AllowedIPs...)
  353. }
  354. // if node is a failover node, add allowed ips from nodes it is handling
  355. if metrics != nil && peer.Failover && metrics.FailoverPeers != nil {
  356. // traverse through nodes that need handling
  357. logger.Log(3, "peer", peer.ID.String(), "was found to be failover for", node.ID.String(), "checking failover peers...")
  358. for k := range metrics.FailoverPeers {
  359. // if FailoverNode is me for this node, add allowedips
  360. if metrics.FailoverPeers[k] == peer.ID.String() {
  361. // get original node so we can traverse the allowed ips
  362. nodeToFailover, err := GetNodeByID(k)
  363. if err == nil {
  364. failoverNodeMetrics, err := GetMetrics(nodeToFailover.ID.String())
  365. if err == nil && failoverNodeMetrics != nil {
  366. if len(failoverNodeMetrics.NodeName) > 0 {
  367. allowedips = append(allowedips, getNodeAllowedIPs(&nodeToFailover, peer)...)
  368. logger.Log(0, "failing over node", nodeToFailover.ID.String(), nodeToFailover.PrimaryAddress(), "to failover node", peer.ID.String())
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. if node.IsRelayed && node.RelayedBy == peer.ID.String() {
  377. allowedips = append(allowedips, getAllowedIpsForRelayed(node, peer)...)
  378. }
  379. return allowedips
  380. }
  381. func getEgressIPs(peer *models.Node) []net.IPNet {
  382. peerHost, err := GetHost(peer.HostID.String())
  383. if err != nil {
  384. logger.Log(0, "error retrieving host for peer", peer.ID.String(), err.Error())
  385. }
  386. //check for internet gateway
  387. internetGateway := false
  388. if slices.Contains(peer.EgressGatewayRanges, "0.0.0.0/0") || slices.Contains(peer.EgressGatewayRanges, "::/0") {
  389. internetGateway = true
  390. }
  391. allowedips := []net.IPNet{}
  392. for _, iprange := range peer.EgressGatewayRanges { // go through each cidr for egress gateway
  393. _, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
  394. if err != nil {
  395. logger.Log(1, "could not parse gateway IP range. Not adding ", iprange)
  396. continue // if can't parse CIDR
  397. }
  398. // getting the public ip of node
  399. if ipnet.Contains(peerHost.EndpointIP) && !internetGateway { // ensuring egress gateway range does not contain endpoint of node
  400. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peerHost.EndpointIP.String(), ", omitting")
  401. continue // skip adding egress range if overlaps with node's ip
  402. }
  403. // TODO: Could put in a lot of great logic to avoid conflicts / bad routes
  404. if ipnet.Contains(peer.LocalAddress.IP) && !internetGateway { // ensuring egress gateway range does not contain public ip of node
  405. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peer.LocalAddress.String(), ", omitting")
  406. continue // skip adding egress range if overlaps with node's local ip
  407. }
  408. if err != nil {
  409. logger.Log(1, "error encountered when setting egress range", err.Error())
  410. } else {
  411. allowedips = append(allowedips, *ipnet)
  412. }
  413. }
  414. return allowedips
  415. }
  416. func getNodeAllowedIPs(peer, node *models.Node) []net.IPNet {
  417. var allowedips = []net.IPNet{}
  418. if peer.Address.IP != nil {
  419. allowed := net.IPNet{
  420. IP: peer.Address.IP,
  421. Mask: net.CIDRMask(32, 32),
  422. }
  423. allowedips = append(allowedips, allowed)
  424. }
  425. if peer.Address6.IP != nil {
  426. allowed := net.IPNet{
  427. IP: peer.Address6.IP,
  428. Mask: net.CIDRMask(128, 128),
  429. }
  430. allowedips = append(allowedips, allowed)
  431. }
  432. // handle egress gateway peers
  433. if peer.IsEgressGateway {
  434. //hasGateway = true
  435. egressIPs := getEgressIPs(peer)
  436. allowedips = append(allowedips, egressIPs...)
  437. }
  438. if peer.IsRelay {
  439. for _, relayedNodeID := range peer.RelayedNodes {
  440. if node.ID.String() == relayedNodeID {
  441. continue
  442. }
  443. relayedNode, err := GetNodeByID(relayedNodeID)
  444. if err != nil {
  445. continue
  446. }
  447. allowed := getRelayedAddresses(relayedNodeID)
  448. if relayedNode.IsEgressGateway {
  449. allowed = append(allowed, getEgressIPs(&relayedNode)...)
  450. }
  451. allowedips = append(allowedips, allowed...)
  452. }
  453. }
  454. return allowedips
  455. }
  456. // getAllowedIpsForRelayed - returns the peerConfig for a node relayed by relay
  457. func getAllowedIpsForRelayed(relayed, relay *models.Node) (allowedIPs []net.IPNet) {
  458. if relayed.RelayedBy != relay.ID.String() {
  459. logger.Log(0, "RelayedByRelay called with invalid parameters")
  460. return
  461. }
  462. peers, err := GetNetworkNodes(relay.Network)
  463. if err != nil {
  464. logger.Log(0, "error getting network clients", err.Error())
  465. return
  466. }
  467. for _, peer := range peers {
  468. if peer.ID == relayed.ID || peer.ID == relay.ID {
  469. continue
  470. }
  471. if nodeacls.AreNodesAllowed(nodeacls.NetworkID(relayed.Network), nodeacls.NodeID(relayed.ID.String()), nodeacls.NodeID(peer.ID.String())) {
  472. allowedIPs = append(allowedIPs, GetAllowedIPs(relayed, &peer, nil)...)
  473. }
  474. }
  475. return
  476. }
  477. func getCIDRMaskFromAddr(addr string) net.IPMask {
  478. cidr := net.CIDRMask(32, 32)
  479. ipAddr, err := netip.ParseAddr(addr)
  480. if err != nil {
  481. return cidr
  482. }
  483. if ipAddr.Is6() {
  484. cidr = net.CIDRMask(128, 128)
  485. }
  486. return cidr
  487. }