peers.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. slog.Debug("peer update for host", "hostId", host.ID.String())
  37. peerIndexMap := make(map[string]int)
  38. for _, nodeID := range host.Nodes {
  39. nodeID := nodeID
  40. node, err := GetNodeByID(nodeID)
  41. if err != nil {
  42. continue
  43. }
  44. if !node.Connected || node.PendingDelete || node.Action == models.NODE_DELETE {
  45. continue
  46. }
  47. if host.OS == models.OS_Types.IoT {
  48. hostPeerUpdate.NodeAddrs = append(hostPeerUpdate.NodeAddrs, node.PrimaryAddressIPNet())
  49. if node.IsRelayed {
  50. relayNode, err := GetNodeByID(node.RelayedBy)
  51. if err != nil {
  52. continue
  53. }
  54. relayHost, err := GetHost(relayNode.HostID.String())
  55. if err != nil {
  56. continue
  57. }
  58. relayPeer := wgtypes.PeerConfig{
  59. PublicKey: relayHost.PublicKey,
  60. PersistentKeepaliveInterval: &relayHost.PersistentKeepalive,
  61. ReplaceAllowedIPs: true,
  62. AllowedIPs: GetAllowedIPs(&node, &relayNode, nil),
  63. }
  64. uselocal := false
  65. if host.EndpointIP.String() == relayHost.EndpointIP.String() {
  66. // peer is on same network
  67. // set to localaddress
  68. uselocal = true
  69. if node.LocalAddress.IP == nil {
  70. // use public endpint
  71. uselocal = false
  72. }
  73. if node.LocalAddress.String() == relayNode.LocalAddress.String() {
  74. uselocal = false
  75. }
  76. }
  77. relayPeer.Endpoint = &net.UDPAddr{
  78. IP: relayHost.EndpointIP,
  79. Port: GetPeerListenPort(relayHost),
  80. }
  81. if uselocal {
  82. relayPeer.Endpoint.IP = relayNode.LocalAddress.IP
  83. relayPeer.Endpoint.Port = relayHost.ListenPort
  84. }
  85. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  86. } else if deletedNode != nil && deletedNode.IsRelay {
  87. relayHost, err := GetHost(deletedNode.HostID.String())
  88. if err != nil {
  89. continue
  90. }
  91. relayPeer := wgtypes.PeerConfig{
  92. PublicKey: relayHost.PublicKey,
  93. Remove: true,
  94. }
  95. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  96. }
  97. continue
  98. }
  99. currentPeers := GetNetworkNodesMemory(allNodes, node.Network)
  100. for _, peer := range currentPeers {
  101. peer := peer
  102. if peer.ID.String() == node.ID.String() {
  103. logger.Log(2, "peer update, skipping self")
  104. // skip yourself
  105. continue
  106. }
  107. peerHost, err := GetHost(peer.HostID.String())
  108. if err != nil {
  109. logger.Log(1, "no peer host", peer.HostID.String(), err.Error())
  110. continue
  111. }
  112. peerConfig := wgtypes.PeerConfig{
  113. PublicKey: peerHost.PublicKey,
  114. PersistentKeepaliveInterval: &peerHost.PersistentKeepalive,
  115. ReplaceAllowedIPs: true,
  116. }
  117. if peer.IsEgressGateway {
  118. hostPeerUpdate.EgressRoutes = append(hostPeerUpdate.EgressRoutes, models.EgressNetworkRoutes{
  119. NodeAddr: node.PrimaryAddressIPNet(),
  120. EgressRanges: peer.EgressGatewayRanges,
  121. })
  122. }
  123. if peer.IsIngressGateway {
  124. hostPeerUpdate.EgressRoutes = append(hostPeerUpdate.EgressRoutes, getExtpeersExtraRoutes(peer.Network)...)
  125. }
  126. if (node.IsRelayed && node.RelayedBy != peer.ID.String()) || (peer.IsRelayed && peer.RelayedBy != node.ID.String()) {
  127. // if node is relayed and peer is not the relay, set remove to true
  128. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; ok {
  129. continue
  130. }
  131. peerConfig.Remove = true
  132. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
  133. peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
  134. continue
  135. }
  136. uselocal := false
  137. if host.EndpointIP.String() == peerHost.EndpointIP.String() {
  138. // peer is on same network
  139. // set to localaddress
  140. uselocal = true
  141. if node.LocalAddress.IP == nil {
  142. // use public endpint
  143. uselocal = false
  144. }
  145. if node.LocalAddress.String() == peer.LocalAddress.String() {
  146. uselocal = false
  147. }
  148. }
  149. peerConfig.Endpoint = &net.UDPAddr{
  150. IP: peerHost.EndpointIP,
  151. Port: GetPeerListenPort(peerHost),
  152. }
  153. if uselocal {
  154. peerConfig.Endpoint.IP = peer.LocalAddress.IP
  155. peerConfig.Endpoint.Port = peerHost.ListenPort
  156. }
  157. allowedips := GetAllowedIPs(&node, &peer, nil)
  158. if peer.Action != models.NODE_DELETE &&
  159. !peer.PendingDelete &&
  160. peer.Connected &&
  161. nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), nodeacls.NodeID(peer.ID.String())) &&
  162. (deletedNode == nil || (deletedNode != nil && peer.ID.String() != deletedNode.ID.String())) {
  163. peerConfig.AllowedIPs = allowedips // only append allowed IPs if valid connection
  164. }
  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: peerHost.ListenPort,
  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: peerHost.ListenPort,
  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. var egressRoutes []models.EgressNetworkRoutes
  201. if node.IsIngressGateway {
  202. extPeers, extPeerIDAndAddrs, egressRoutes, err = getExtPeers(&node, &node)
  203. if err == nil {
  204. hostPeerUpdate.EgressRoutes = append(hostPeerUpdate.EgressRoutes, egressRoutes...)
  205. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, extPeers...)
  206. for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
  207. extPeerIdAndAddr := extPeerIdAndAddr
  208. if node.Network == network {
  209. hostPeerUpdate.PeerIDs[extPeerIdAndAddr.ID] = extPeerIdAndAddr
  210. hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, extPeers...)
  211. }
  212. }
  213. } else if !database.IsEmptyRecord(err) {
  214. logger.Log(1, "error retrieving external clients:", err.Error())
  215. }
  216. }
  217. if node.IsEgressGateway && node.EgressGatewayRequest.NatEnabled == "yes" && len(node.EgressGatewayRequest.Ranges) > 0 {
  218. hostPeerUpdate.FwUpdate.IsEgressGw = true
  219. hostPeerUpdate.FwUpdate.EgressInfo[node.ID.String()] = models.EgressInfo{
  220. EgressID: node.ID.String(),
  221. Network: node.PrimaryNetworkRange(),
  222. EgressGwAddr: net.IPNet{
  223. IP: net.ParseIP(node.PrimaryAddress()),
  224. Mask: getCIDRMaskFromAddr(node.PrimaryAddress()),
  225. },
  226. EgressGWCfg: node.EgressGatewayRequest,
  227. }
  228. }
  229. }
  230. // == post peer calculations ==
  231. // indicate removal if no allowed IPs were calculated
  232. for i := range hostPeerUpdate.Peers {
  233. peer := hostPeerUpdate.Peers[i]
  234. if len(peer.AllowedIPs) == 0 {
  235. peer.Remove = true
  236. }
  237. hostPeerUpdate.Peers[i] = peer
  238. }
  239. if deletedNode != nil && host.OS != models.OS_Types.IoT {
  240. peerHost, err := GetHost(deletedNode.HostID.String())
  241. if err == nil && host.ID != peerHost.ID {
  242. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
  243. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  244. PublicKey: peerHost.PublicKey,
  245. Remove: true,
  246. })
  247. }
  248. }
  249. }
  250. for i := range hostPeerUpdate.NodePeers {
  251. peer := hostPeerUpdate.NodePeers[i]
  252. if len(peer.AllowedIPs) == 0 {
  253. peer.Remove = true
  254. }
  255. hostPeerUpdate.NodePeers[i] = peer
  256. }
  257. if len(deletedClients) > 0 {
  258. for i := range deletedClients {
  259. deletedClient := deletedClients[i]
  260. key, err := wgtypes.ParseKey(deletedClient.PublicKey)
  261. if err == nil {
  262. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  263. PublicKey: key,
  264. Remove: true,
  265. })
  266. }
  267. }
  268. }
  269. return hostPeerUpdate, nil
  270. }
  271. // GetPeerListenPort - given a host, retrieve it's appropriate listening port
  272. func GetPeerListenPort(host *models.Host) int {
  273. peerPort := host.ListenPort
  274. if host.WgPublicListenPort != 0 {
  275. peerPort = host.WgPublicListenPort
  276. }
  277. return peerPort
  278. }
  279. // GetAllowedIPs - calculates the wireguard allowedip field for a peer of a node based on the peer and node settings
  280. func GetAllowedIPs(node, peer *models.Node, metrics *models.Metrics) []net.IPNet {
  281. var allowedips []net.IPNet
  282. allowedips = getNodeAllowedIPs(peer, node)
  283. // handle ingress gateway peers
  284. if peer.IsIngressGateway {
  285. extPeers, _, _, err := getExtPeers(peer, node)
  286. if err != nil {
  287. logger.Log(2, "could not retrieve ext peers for ", peer.ID.String(), err.Error())
  288. }
  289. for _, extPeer := range extPeers {
  290. allowedips = append(allowedips, extPeer.AllowedIPs...)
  291. }
  292. }
  293. if node.IsRelayed && node.RelayedBy == peer.ID.String() {
  294. allowedips = append(allowedips, GetAllowedIpsForRelayed(node, peer)...)
  295. }
  296. return allowedips
  297. }
  298. func GetEgressIPs(peer *models.Node) []net.IPNet {
  299. peerHost, err := GetHost(peer.HostID.String())
  300. if err != nil {
  301. logger.Log(0, "error retrieving host for peer", peer.ID.String(), err.Error())
  302. }
  303. // check for internet gateway
  304. internetGateway := false
  305. if slices.Contains(peer.EgressGatewayRanges, "0.0.0.0/0") || slices.Contains(peer.EgressGatewayRanges, "::/0") {
  306. internetGateway = true
  307. }
  308. allowedips := []net.IPNet{}
  309. for _, iprange := range peer.EgressGatewayRanges { // go through each cidr for egress gateway
  310. _, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
  311. if err != nil {
  312. logger.Log(1, "could not parse gateway IP range. Not adding ", iprange)
  313. continue // if can't parse CIDR
  314. }
  315. // getting the public ip of node
  316. if ipnet.Contains(peerHost.EndpointIP) && !internetGateway { // ensuring egress gateway range does not contain endpoint of node
  317. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peerHost.EndpointIP.String(), ", omitting")
  318. continue // skip adding egress range if overlaps with node's ip
  319. }
  320. // TODO: Could put in a lot of great logic to avoid conflicts / bad routes
  321. if ipnet.Contains(peer.LocalAddress.IP) && !internetGateway { // ensuring egress gateway range does not contain public ip of node
  322. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peer.LocalAddress.String(), ", omitting")
  323. continue // skip adding egress range if overlaps with node's local ip
  324. }
  325. if err != nil {
  326. logger.Log(1, "error encountered when setting egress range", err.Error())
  327. } else {
  328. allowedips = append(allowedips, *ipnet)
  329. }
  330. }
  331. return allowedips
  332. }
  333. func getNodeAllowedIPs(peer, node *models.Node) []net.IPNet {
  334. var allowedips = []net.IPNet{}
  335. if peer.Address.IP != nil {
  336. allowed := net.IPNet{
  337. IP: peer.Address.IP,
  338. Mask: net.CIDRMask(32, 32),
  339. }
  340. allowedips = append(allowedips, allowed)
  341. }
  342. if peer.Address6.IP != nil {
  343. allowed := net.IPNet{
  344. IP: peer.Address6.IP,
  345. Mask: net.CIDRMask(128, 128),
  346. }
  347. allowedips = append(allowedips, allowed)
  348. }
  349. // handle egress gateway peers
  350. if peer.IsEgressGateway {
  351. // hasGateway = true
  352. egressIPs := GetEgressIPs(peer)
  353. allowedips = append(allowedips, egressIPs...)
  354. }
  355. if peer.IsRelay {
  356. allowedips = append(allowedips, RelayedAllowedIPs(peer, node)...)
  357. }
  358. return allowedips
  359. }
  360. func getCIDRMaskFromAddr(addr string) net.IPMask {
  361. cidr := net.CIDRMask(32, 32)
  362. ipAddr, err := netip.ParseAddr(addr)
  363. if err != nil {
  364. return cidr
  365. }
  366. if ipAddr.Is6() {
  367. cidr = net.CIDRMask(128, 128)
  368. }
  369. return cidr
  370. }