routes.go 2.5 KB

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