routes.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package local
  2. import (
  3. "net"
  4. "github.com/gravitl/netmaker/logger"
  5. "github.com/gravitl/netmaker/netclient/ncutils"
  6. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  7. )
  8. // TODO handle ipv6 in future
  9. // SetPeerRoutes - sets/removes ip routes for each peer on a network
  10. func SetPeerRoutes(iface string, oldPeers map[string][]net.IPNet, newPeers []wgtypes.PeerConfig) {
  11. // traverse through all recieved peers
  12. for _, peer := range newPeers {
  13. // if pubkey found in existing peers, check against existing peer
  14. currPeerAllowedIPs := oldPeers[peer.PublicKey.String()]
  15. if currPeerAllowedIPs != nil {
  16. // traverse IPs, check to see if old peer contains each IP
  17. for _, allowedIP := range peer.AllowedIPs { // compare new ones (if any) to old ones
  18. if !ncutils.IPNetSliceContains(currPeerAllowedIPs, allowedIP) {
  19. if err := setRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil {
  20. logger.Log(1, err.Error())
  21. }
  22. }
  23. }
  24. for _, allowedIP := range currPeerAllowedIPs { // compare old ones (if any) to new ones
  25. if !ncutils.IPNetSliceContains(peer.AllowedIPs, allowedIP) {
  26. if err := deleteRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil {
  27. logger.Log(1, err.Error())
  28. }
  29. }
  30. }
  31. delete(oldPeers, peer.PublicKey.String()) // remove peer as it was found and processed
  32. } else {
  33. for _, allowedIP := range peer.AllowedIPs { // add all routes as peer doesn't exist
  34. if err := setRoute(iface, &allowedIP, allowedIP.String()); err != nil {
  35. logger.Log(1, err.Error())
  36. }
  37. }
  38. }
  39. }
  40. // traverse through all remaining existing peers
  41. for _, allowedIPs := range oldPeers {
  42. for _, allowedIP := range allowedIPs {
  43. deleteRoute(iface, &allowedIP, allowedIP.IP.String())
  44. }
  45. }
  46. }
  47. // SetCurrentPeerRoutes - sets all the current peers
  48. func SetCurrentPeerRoutes(iface, currentAddr string, peers []wgtypes.PeerConfig) {
  49. for _, peer := range peers {
  50. for _, allowedIP := range peer.AllowedIPs {
  51. setRoute(iface, &allowedIP, currentAddr)
  52. }
  53. }
  54. }
  55. // FlushPeerRoutes - removes all current peer routes
  56. func FlushPeerRoutes(iface, currentAddr string, peers []wgtypes.Peer) {
  57. for _, peer := range peers {
  58. for _, allowedIP := range peer.AllowedIPs {
  59. deleteRoute(iface, &allowedIP, currentAddr)
  60. }
  61. }
  62. }
  63. // SetCIDRRoute - sets the CIDR route, used on join and restarts
  64. func SetCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
  65. setCidr(iface, currentAddr, cidr)
  66. }
  67. // RemoveCIDRRoute - removes a static cidr route
  68. func RemoveCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
  69. removeCidr(iface, cidr, currentAddr)
  70. }