2023-12-18 11:46:58 +01:00
|
|
|
//go:build !android && !ios
|
2023-04-17 11:15:37 +02:00
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
"github.com/libp2p/go-netroute"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-09-05 09:06:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var errRouteNotFound = fmt.Errorf("route not found")
|
|
|
|
|
|
|
|
func addToRouteTableIfNoExists(prefix netip.Prefix, addr string) error {
|
2023-11-24 11:31:22 +01:00
|
|
|
ok, err := existsInRouteTable(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
log.Warnf("skipping adding a new route for network %s because it already exists", prefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err = isSubRange(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
err := addRouteForCurrentDefaultGateway(prefix)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to add route for current default gateway route. Will proceed without it. error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addToRouteTable(prefix, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRouteForCurrentDefaultGateway(prefix netip.Prefix) error {
|
2023-06-09 12:35:57 +02:00
|
|
|
defaultGateway, err := getExistingRIBRouteGateway(netip.MustParsePrefix("0.0.0.0/0"))
|
2022-09-05 09:06:35 +02:00
|
|
|
if err != nil && err != errRouteNotFound {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-09 12:35:57 +02:00
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
addr := netip.MustParseAddr(defaultGateway.String())
|
|
|
|
|
|
|
|
if !prefix.Contains(addr) {
|
|
|
|
log.Debugf("skipping adding a new route for gateway %s because it is not in the network %s", addr, prefix)
|
2023-06-09 12:35:57 +02:00
|
|
|
return nil
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
gatewayPrefix := netip.PrefixFrom(addr, 32)
|
|
|
|
|
|
|
|
ok, err := existsInRouteTable(gatewayPrefix)
|
2023-06-09 12:35:57 +02:00
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return fmt.Errorf("unable to check if there is an existing route for gateway %s. error: %s", gatewayPrefix, err)
|
2023-06-09 12:35:57 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
|
2023-06-09 12:35:57 +02:00
|
|
|
if ok {
|
2023-11-24 11:31:22 +01:00
|
|
|
log.Debugf("skipping adding a new route for gateway %s because it already exists", gatewayPrefix)
|
2022-09-05 09:06:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-09 12:35:57 +02:00
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
gatewayHop, err := getExistingRIBRouteGateway(gatewayPrefix)
|
|
|
|
if err != nil && err != errRouteNotFound {
|
|
|
|
return fmt.Errorf("unable to get the next hop for the default gateway address. error: %s", err)
|
|
|
|
}
|
|
|
|
log.Debugf("adding a new route for gateway %s with next hop %s", gatewayPrefix, gatewayHop)
|
|
|
|
return addToRouteTable(gatewayPrefix, gatewayHop.String())
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func existsInRouteTable(prefix netip.Prefix) (bool, error) {
|
|
|
|
routes, err := getRoutesFromTable()
|
2022-09-05 09:06:35 +02:00
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return false, err
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
for _, tableRoute := range routes {
|
|
|
|
if tableRoute == prefix {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSubRange(prefix netip.Prefix) (bool, error) {
|
|
|
|
routes, err := getRoutesFromTable()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
for _, tableRoute := range routes {
|
|
|
|
if tableRoute.Bits() > minRangeBits && tableRoute.Contains(prefix.Addr()) && tableRoute.Bits() < prefix.Bits() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromRouteTableIfNonSystem(prefix netip.Prefix, addr string) error {
|
|
|
|
return removeFromRouteTable(prefix, addr)
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getExistingRIBRouteGateway(prefix netip.Prefix) (net.IP, error) {
|
|
|
|
r, err := netroute.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-09 18:48:21 +02:00
|
|
|
_, gateway, preferredSrc, err := r.Route(prefix.Addr().AsSlice())
|
2022-09-05 09:06:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting routes returned an error: %v", err)
|
|
|
|
return nil, errRouteNotFound
|
|
|
|
}
|
|
|
|
|
2023-06-09 18:48:21 +02:00
|
|
|
if gateway == nil {
|
|
|
|
return preferredSrc, nil
|
|
|
|
}
|
|
|
|
|
2022-10-31 11:54:34 +01:00
|
|
|
return gateway, nil
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|