2024-04-08 18:56:52 +02:00
|
|
|
//go:build !android && !ios
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
package systemops
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-04-09 13:25:14 +02:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/libp2p/go-netroute"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
nberrors "github.com/netbirdio/netbird/client/errors"
|
2024-04-08 18:56:52 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2024-06-13 13:24:24 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager/refcounter"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager/util"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager/vars"
|
2024-04-08 18:56:52 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
nbnet "github.com/netbirdio/netbird/util/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
var splitDefaultv4_1 = netip.PrefixFrom(netip.IPv4Unspecified(), 1)
|
|
|
|
var splitDefaultv4_2 = netip.PrefixFrom(netip.AddrFrom4([4]byte{128}), 1)
|
|
|
|
var splitDefaultv6_1 = netip.PrefixFrom(netip.IPv6Unspecified(), 1)
|
|
|
|
var splitDefaultv6_2 = netip.PrefixFrom(netip.AddrFrom16([16]byte{0x80}), 1)
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) setupRefCounter(initAddresses []net.IP) (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
|
|
|
|
initialNextHopV4, err := GetNextHop(netip.IPv4Unspecified())
|
|
|
|
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
|
|
|
|
log.Errorf("Unable to get initial v4 default next hop: %v", err)
|
|
|
|
}
|
|
|
|
initialNextHopV6, err := GetNextHop(netip.IPv6Unspecified())
|
|
|
|
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
|
|
|
|
log.Errorf("Unable to get initial v6 default next hop: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
refCounter := refcounter.New(
|
|
|
|
func(prefix netip.Prefix, _ any) (Nexthop, error) {
|
|
|
|
initialNexthop := initialNextHopV4
|
|
|
|
if prefix.Addr().Is6() {
|
|
|
|
initialNexthop = initialNextHopV6
|
|
|
|
}
|
|
|
|
|
|
|
|
nexthop, err := r.addRouteToNonVPNIntf(prefix, r.wgInterface, initialNexthop)
|
|
|
|
if errors.Is(err, vars.ErrRouteNotAllowed) || errors.Is(err, vars.ErrRouteNotFound) {
|
|
|
|
log.Tracef("Adding for prefix %s: %v", prefix, err)
|
|
|
|
// These errors are not critical but also we should not track and try to remove the routes either.
|
|
|
|
return nexthop, refcounter.ErrIgnore
|
|
|
|
}
|
|
|
|
return nexthop, err
|
|
|
|
},
|
|
|
|
r.removeFromRouteTable,
|
|
|
|
)
|
|
|
|
|
|
|
|
r.refCounter = refCounter
|
|
|
|
|
|
|
|
return r.setupHooks(initAddresses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SysOps) cleanupRefCounter() error {
|
|
|
|
if r.refCounter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove hooks selectively
|
|
|
|
nbnet.RemoveDialerHooks()
|
|
|
|
nbnet.RemoveListenerHooks()
|
|
|
|
|
|
|
|
if err := r.refCounter.Flush(); err != nil {
|
|
|
|
return fmt.Errorf("flush route manager: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
// TODO: fix: for default our wg address now appears as the default gw
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) addRouteForCurrentDefaultGateway(prefix netip.Prefix) error {
|
2024-04-08 18:56:52 +02:00
|
|
|
addr := netip.IPv4Unspecified()
|
|
|
|
if prefix.Addr().Is6() {
|
|
|
|
addr = netip.IPv6Unspecified()
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
nexthop, err := GetNextHop(addr)
|
|
|
|
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("get existing route gateway: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
if !prefix.Contains(nexthop.IP) {
|
|
|
|
log.Debugf("Skipping adding a new route for gateway %s because it is not in the network %s", nexthop.IP, prefix)
|
2024-04-08 18:56:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
gatewayPrefix := netip.PrefixFrom(nexthop.IP, 32)
|
|
|
|
if nexthop.IP.Is6() {
|
|
|
|
gatewayPrefix = netip.PrefixFrom(nexthop.IP, 128)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := existsInRouteTable(gatewayPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to check if there is an existing route for gateway %s. error: %s", gatewayPrefix, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
log.Debugf("Skipping adding a new route for gateway %s because it already exists", gatewayPrefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
nexthop, err = GetNextHop(nexthop.IP)
|
|
|
|
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("unable to get the next hop for the default gateway address. error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
log.Debugf("Adding a new route for gateway %s with next hop %s", gatewayPrefix, nexthop.IP)
|
|
|
|
return r.addToRouteTable(gatewayPrefix, nexthop)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// addRouteToNonVPNIntf adds a new route to the routing table for the given prefix and returns the next hop and interface.
|
|
|
|
// If the next hop or interface is pointing to the VPN interface, it will return the initial values.
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) addRouteToNonVPNIntf(prefix netip.Prefix, vpnIntf *iface.WGIface, initialNextHop Nexthop) (Nexthop, error) {
|
2024-04-08 18:56:52 +02:00
|
|
|
addr := prefix.Addr()
|
|
|
|
switch {
|
|
|
|
case addr.IsLoopback(),
|
|
|
|
addr.IsLinkLocalUnicast(),
|
|
|
|
addr.IsLinkLocalMulticast(),
|
|
|
|
addr.IsInterfaceLocalMulticast(),
|
|
|
|
addr.IsUnspecified(),
|
|
|
|
addr.IsMulticast():
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return Nexthop{}, vars.ErrRouteNotAllowed
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the exit interface and next hop for the prefix, so we can add a specific route
|
2024-06-13 13:24:24 +02:00
|
|
|
nexthop, err := GetNextHop(addr)
|
2024-04-08 18:56:52 +02:00
|
|
|
if err != nil {
|
2024-06-13 13:24:24 +02:00
|
|
|
return Nexthop{}, fmt.Errorf("get next hop: %w", err)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
log.Debugf("Found next hop %s for prefix %s with interface %v", nexthop.IP, prefix, nexthop.IP)
|
|
|
|
exitNextHop := Nexthop{
|
|
|
|
IP: nexthop.IP,
|
|
|
|
Intf: nexthop.Intf,
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
vpnAddr, ok := netip.AddrFromSlice(vpnIntf.Address().IP)
|
|
|
|
if !ok {
|
2024-06-13 13:24:24 +02:00
|
|
|
return Nexthop{}, fmt.Errorf("failed to convert vpn address to netip.Addr")
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if next hop is the VPN address or the interface is the VPN interface, we should use the initial values
|
2024-06-13 13:24:24 +02:00
|
|
|
if exitNextHop.IP == vpnAddr || exitNextHop.Intf != nil && exitNextHop.Intf.Name == vpnIntf.Name() {
|
2024-04-08 18:56:52 +02:00
|
|
|
log.Debugf("Route for prefix %s is pointing to the VPN interface", prefix)
|
|
|
|
exitNextHop = initialNextHop
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
log.Debugf("Adding a new route for prefix %s with next hop %s", prefix, exitNextHop.IP)
|
|
|
|
if err := r.addToRouteTable(prefix, exitNextHop); err != nil {
|
|
|
|
return Nexthop{}, fmt.Errorf("add route to table: %w", err)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return exitNextHop, nil
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// genericAddVPNRoute adds a new route to the vpn interface, it splits the default prefix
|
|
|
|
// in two /1 prefixes to avoid replacing the existing default route
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) genericAddVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
|
|
|
|
nextHop := Nexthop{netip.Addr{}, intf}
|
|
|
|
|
|
|
|
if prefix == vars.Defaultv4 {
|
|
|
|
if err := r.addToRouteTable(splitDefaultv4_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
return err
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.addToRouteTable(splitDefaultv4_2, nextHop); err != nil {
|
|
|
|
if err2 := r.removeFromRouteTable(splitDefaultv4_1, nextHop); err2 != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
log.Warnf("Failed to rollback route addition: %s", err2)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove once IPv6 is supported on the interface
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.addToRouteTable(splitDefaultv6_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("add unreachable route split 1: %w", err)
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.addToRouteTable(splitDefaultv6_2, nextHop); err != nil {
|
|
|
|
if err2 := r.removeFromRouteTable(splitDefaultv6_1, nextHop); err2 != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
log.Warnf("Failed to rollback route addition: %s", err2)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("add unreachable route split 2: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-06-13 13:24:24 +02:00
|
|
|
} else if prefix == vars.Defaultv6 {
|
|
|
|
if err := r.addToRouteTable(splitDefaultv6_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("add unreachable route split 1: %w", err)
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.addToRouteTable(splitDefaultv6_2, nextHop); err != nil {
|
|
|
|
if err2 := r.removeFromRouteTable(splitDefaultv6_1, nextHop); err2 != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
log.Warnf("Failed to rollback route addition: %s", err2)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("add unreachable route split 2: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return r.addNonExistingRoute(prefix, intf)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// addNonExistingRoute adds a new route to the vpn interface if it doesn't exist in the current routing table
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) addNonExistingRoute(prefix netip.Prefix, intf *net.Interface) error {
|
2024-04-08 18:56:52 +02:00
|
|
|
ok, err := existsInRouteTable(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exists in route table: %w", 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 fmt.Errorf("sub range: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.addRouteForCurrentDefaultGateway(prefix); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
log.Warnf("Unable to add route for current default gateway route. Will proceed without it. error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return r.addToRouteTable(prefix, Nexthop{netip.Addr{}, intf})
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// genericRemoveVPNRoute removes the route from the vpn interface. If a default prefix is given,
|
|
|
|
// it will remove the split /1 prefixes
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) genericRemoveVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
|
|
|
|
nextHop := Nexthop{netip.Addr{}, intf}
|
|
|
|
|
|
|
|
if prefix == vars.Defaultv4 {
|
2024-04-08 18:56:52 +02:00
|
|
|
var result *multierror.Error
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv4_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv4_2, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove once IPv6 is supported on the interface
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv6_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv6_2, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return nberrors.FormatErrorOrNil(result)
|
|
|
|
} else if prefix == vars.Defaultv6 {
|
2024-04-08 18:56:52 +02:00
|
|
|
var result *multierror.Error
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv6_1, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.removeFromRouteTable(splitDefaultv6_2, nextHop); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return nberrors.FormatErrorOrNil(result)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return r.removeFromRouteTable(prefix, nextHop)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
func (r *SysOps) setupHooks(initAddresses []net.IP) (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
|
2024-04-08 18:56:52 +02:00
|
|
|
beforeHook := func(connID nbnet.ConnectionID, ip net.IP) error {
|
2024-06-13 13:24:24 +02:00
|
|
|
prefix, err := util.GetPrefixFromIP(ip)
|
2024-04-08 18:56:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("convert ip to prefix: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
if _, err := r.refCounter.IncrementWithID(string(connID), prefix, nil); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("adding route reference: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
afterHook := func(connID nbnet.ConnectionID) error {
|
2024-06-13 13:24:24 +02:00
|
|
|
if err := r.refCounter.DecrementWithID(string(connID)); err != nil {
|
2024-04-08 18:56:52 +02:00
|
|
|
return fmt.Errorf("remove route reference: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range initAddresses {
|
|
|
|
if err := beforeHook("init", ip); err != nil {
|
|
|
|
log.Errorf("Failed to add route reference: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nbnet.AddDialerHook(func(ctx context.Context, connID nbnet.ConnectionID, resolvedIPs []net.IPAddr) error {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *multierror.Error
|
|
|
|
for _, ip := range resolvedIPs {
|
|
|
|
result = multierror.Append(result, beforeHook(connID, ip.IP))
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
return nberrors.FormatErrorOrNil(result)
|
2024-04-08 18:56:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
nbnet.AddDialerCloseHook(func(connID nbnet.ConnectionID, conn *net.Conn) error {
|
|
|
|
return afterHook(connID)
|
|
|
|
})
|
|
|
|
|
|
|
|
nbnet.AddListenerWriteHook(func(connID nbnet.ConnectionID, ip *net.IPAddr, data []byte) error {
|
|
|
|
return beforeHook(connID, ip.IP)
|
|
|
|
})
|
|
|
|
|
|
|
|
nbnet.AddListenerCloseHook(func(connID nbnet.ConnectionID, conn net.PacketConn) error {
|
|
|
|
return afterHook(connID)
|
|
|
|
})
|
|
|
|
|
|
|
|
return beforeHook, afterHook, nil
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
func GetNextHop(ip netip.Addr) (Nexthop, error) {
|
|
|
|
r, err := netroute.New()
|
|
|
|
if err != nil {
|
|
|
|
return Nexthop{}, fmt.Errorf("new netroute: %w", err)
|
|
|
|
}
|
|
|
|
intf, gateway, preferredSrc, err := r.Route(ip.AsSlice())
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Failed to get route for %s: %v", ip, err)
|
|
|
|
return Nexthop{}, vars.ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Route for %s: interface %v nexthop %v, preferred source %v", ip, intf, gateway, preferredSrc)
|
|
|
|
if gateway == nil {
|
|
|
|
if runtime.GOOS == "freebsd" {
|
|
|
|
return Nexthop{Intf: intf}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if preferredSrc == nil {
|
|
|
|
return Nexthop{}, vars.ErrRouteNotFound
|
|
|
|
}
|
|
|
|
log.Debugf("No next hop found for IP %s, using preferred source %s", ip, preferredSrc)
|
|
|
|
|
|
|
|
addr, err := ipToAddr(preferredSrc, intf)
|
|
|
|
if err != nil {
|
|
|
|
return Nexthop{}, fmt.Errorf("convert preferred source to address: %w", err)
|
|
|
|
}
|
|
|
|
return Nexthop{
|
|
|
|
IP: addr.Unmap(),
|
|
|
|
Intf: intf,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := ipToAddr(gateway, intf)
|
|
|
|
if err != nil {
|
|
|
|
return Nexthop{}, fmt.Errorf("convert gateway to address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nexthop{
|
|
|
|
IP: addr,
|
|
|
|
Intf: intf,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts a net.IP to a netip.Addr including the zone based on the passed interface
|
|
|
|
func ipToAddr(ip net.IP, intf *net.Interface) (netip.Addr, error) {
|
|
|
|
addr, ok := netip.AddrFromSlice(ip)
|
|
|
|
if !ok {
|
|
|
|
return netip.Addr{}, fmt.Errorf("failed to convert IP address to netip.Addr: %s", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
if intf != nil && (addr.IsLinkLocalMulticast() || addr.IsLinkLocalUnicast()) {
|
|
|
|
log.Tracef("Adding zone %s to address %s", intf.Name, addr)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
addr = addr.WithZone(strconv.Itoa(intf.Index))
|
|
|
|
} else {
|
|
|
|
addr = addr.WithZone(intf.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.Unmap(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func existsInRouteTable(prefix netip.Prefix) (bool, error) {
|
|
|
|
routes, err := getRoutesFromTable()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("get routes from table: %w", err)
|
|
|
|
}
|
|
|
|
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, fmt.Errorf("get routes from table: %w", err)
|
|
|
|
}
|
|
|
|
for _, tableRoute := range routes {
|
|
|
|
if tableRoute.Bits() > vars.MinRangeBits && tableRoute.Contains(prefix.Addr()) && tableRoute.Bits() < prefix.Bits() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|