2024-04-08 18:56:52 +02:00
|
|
|
//go:build darwin && !ios
|
|
|
|
|
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2024-04-09 15:27:19 +02:00
|
|
|
"time"
|
2024-04-08 18:56:52 +02:00
|
|
|
|
2024-04-09 15:27:19 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2024-04-08 18:56:52 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
)
|
|
|
|
|
|
|
|
var routeManager *RouteManager
|
|
|
|
|
|
|
|
func setupRouting(initAddresses []net.IP, wgIface *iface.WGIface) (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
|
|
|
|
return setupRoutingWithRouteManager(&routeManager, initAddresses, wgIface)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupRouting() error {
|
|
|
|
return cleanupRoutingWithRouteManager(routeManager)
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:37:27 +02:00
|
|
|
func addToRouteTable(prefix netip.Prefix, nexthop netip.Addr, intf *net.Interface) error {
|
2024-04-08 18:56:52 +02:00
|
|
|
return routeCmd("add", prefix, nexthop, intf)
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:37:27 +02:00
|
|
|
func removeFromRouteTable(prefix netip.Prefix, nexthop netip.Addr, intf *net.Interface) error {
|
2024-04-08 18:56:52 +02:00
|
|
|
return routeCmd("delete", prefix, nexthop, intf)
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:37:27 +02:00
|
|
|
func routeCmd(action string, prefix netip.Prefix, nexthop netip.Addr, intf *net.Interface) error {
|
2024-04-08 18:56:52 +02:00
|
|
|
inet := "-inet"
|
2024-04-09 13:25:14 +02:00
|
|
|
network := prefix.String()
|
|
|
|
if prefix.IsSingleIP() {
|
|
|
|
network = prefix.Addr().String()
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
if prefix.Addr().Is6() {
|
|
|
|
inet = "-inet6"
|
|
|
|
// Special case for IPv6 split default route, pointing to the wg interface fails
|
|
|
|
// TODO: Remove once we have IPv6 support on the interface
|
|
|
|
if prefix.Bits() == 1 {
|
2024-04-26 16:37:27 +02:00
|
|
|
intf = &net.Interface{Name: "lo0"}
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:25:14 +02:00
|
|
|
args := []string{"-n", action, inet, network}
|
2024-04-08 18:56:52 +02:00
|
|
|
if nexthop.IsValid() {
|
|
|
|
args = append(args, nexthop.Unmap().String())
|
2024-04-26 16:37:27 +02:00
|
|
|
} else if intf != nil {
|
|
|
|
args = append(args, "-interface", intf.Name)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 15:27:19 +02:00
|
|
|
if err := retryRouteCmd(args); err != nil {
|
|
|
|
return fmt.Errorf("failed to %s route for %s: %w", action, prefix, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
|
2024-04-09 15:27:19 +02:00
|
|
|
func retryRouteCmd(args []string) error {
|
|
|
|
operation := func() error {
|
|
|
|
out, err := exec.Command("route", args...).CombinedOutput()
|
|
|
|
log.Tracef("route %s: %s", strings.Join(args, " "), out)
|
|
|
|
// https://github.com/golang/go/issues/45736
|
|
|
|
if err != nil && strings.Contains(string(out), "sysctl: cannot allocate memory") {
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
return backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
expBackOff := backoff.NewExponentialBackOff()
|
|
|
|
expBackOff.InitialInterval = 50 * time.Millisecond
|
|
|
|
expBackOff.MaxInterval = 500 * time.Millisecond
|
|
|
|
expBackOff.MaxElapsedTime = 1 * time.Second
|
|
|
|
|
|
|
|
err := backoff.Retry(operation, expBackOff)
|
2024-04-08 18:56:52 +02:00
|
|
|
if err != nil {
|
2024-04-09 15:27:19 +02:00
|
|
|
return fmt.Errorf("route cmd retry failed: %w", err)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|