routes.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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]bool, newPeers []wgtypes.PeerConfig) {
  11. // traverse through all recieved peers
  12. for _, peer := range newPeers {
  13. for _, allowedIP := range peer.AllowedIPs {
  14. if !oldPeers[allowedIP.String()] {
  15. if err := setRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil {
  16. logger.Log(1, err.Error())
  17. }
  18. } else {
  19. delete(oldPeers, allowedIP.String())
  20. }
  21. }
  22. }
  23. // traverse through all remaining existing peers
  24. for i := range oldPeers {
  25. ip, err := ncutils.GetIPNetFromString(i)
  26. if err != nil {
  27. logger.Log(1, err.Error())
  28. } else {
  29. deleteRoute(iface, &ip, ip.IP.String())
  30. }
  31. }
  32. }
  33. // SetCurrentPeerRoutes - sets all the current peers
  34. func SetCurrentPeerRoutes(iface, currentAddr string, peers []wgtypes.PeerConfig) {
  35. for _, peer := range peers {
  36. for _, allowedIP := range peer.AllowedIPs {
  37. setRoute(iface, &allowedIP, currentAddr)
  38. }
  39. }
  40. }
  41. // FlushPeerRoutes - removes all current peer routes
  42. func FlushPeerRoutes(iface, currentAddr string, peers []wgtypes.Peer) {
  43. for _, peer := range peers {
  44. for _, allowedIP := range peer.AllowedIPs {
  45. deleteRoute(iface, &allowedIP, currentAddr)
  46. }
  47. }
  48. }
  49. // SetCIDRRoute - sets the CIDR route, used on join and restarts
  50. func SetCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
  51. setCidr(iface, currentAddr, cidr)
  52. }
  53. // RemoveCIDRRoute - removes a static cidr route
  54. func RemoveCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
  55. removeCidr(iface, cidr, currentAddr)
  56. }