Skip to content

Commit

Permalink
Fix rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Jun 14, 2024
1 parent 5528184 commit ed1533f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 78 deletions.
55 changes: 0 additions & 55 deletions client/internal/routemanager/systemops/systemops.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,3 @@ func NewSysOps(wgInterface *iface.WGIface) *SysOps {
wgInterface: wgInterface,
}
}

// IsAddrRouted checks if the candidate address would route to the vpn, in which case it returns true and the matched prefix.
func IsAddrRouted(addr netip.Addr, vpnRoutes []netip.Prefix) (bool, netip.Prefix) {
localRoutes, err := hasSeparateRouting()
if err != nil {
if !errors.Is(err, ErrRoutingIsSeparate) {
log.Errorf("Failed to get routes: %v", err)
}
return false, netip.Prefix{}
}

return isVpnRoute(addr, vpnRoutes, localRoutes)
}

func isVpnRoute(addr netip.Addr, vpnRoutes []netip.Prefix, localRoutes []netip.Prefix) (bool, netip.Prefix) {
vpnPrefixMap := map[netip.Prefix]struct{}{}
for _, prefix := range vpnRoutes {
vpnPrefixMap[prefix] = struct{}{}
}

// remove vpnRoute duplicates
for _, prefix := range localRoutes {
delete(vpnPrefixMap, prefix)
}

var longestPrefix netip.Prefix
var isVpn bool

combinedRoutes := make([]netip.Prefix, len(vpnRoutes)+len(localRoutes))
copy(combinedRoutes, vpnRoutes)
copy(combinedRoutes[len(vpnRoutes):], localRoutes)

for _, prefix := range combinedRoutes {
// Ignore the default route, it has special handling
if prefix.Bits() == 0 {
continue
}

if prefix.Contains(addr) {
// Longest prefix match
if !longestPrefix.IsValid() || prefix.Bits() > longestPrefix.Bits() {
longestPrefix = prefix
_, isVpn = vpnPrefixMap[prefix]
}
}
}

if !longestPrefix.IsValid() {
// No route matched
return false, netip.Prefix{}
}

// Return true if the longest matching prefix is from vpnRoutes
return isVpn, longestPrefix
}
Empty file.
62 changes: 59 additions & 3 deletions client/internal/routemanager/systemops/systemops_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
log "github.com/sirupsen/logrus"

nberrors "github.com/netbirdio/netbird/client/errors"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/internal/routemanager/refcounter"
"github.com/netbirdio/netbird/client/internal/routemanager/util"
"github.com/netbirdio/netbird/client/internal/routemanager/vars"
Expand All @@ -29,7 +28,9 @@ 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)

func (r *SysOps) setupRefCounter(initAddresses []net.IP) (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
var ErrRoutingIsSeparate = errors.New("routing is separate")

func (r *SysOps) setupRefCounter(initAddresses []net.IP) (nbnet.AddHookFunc, nbnet.RemoveHookFunc, 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)
Expand Down Expand Up @@ -273,7 +274,7 @@ func (r *SysOps) genericRemoveVPNRoute(prefix netip.Prefix, intf *net.Interface)
return r.removeFromRouteTable(prefix, nextHop)
}

func (r *SysOps) setupHooks(initAddresses []net.IP) (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
func (r *SysOps) setupHooks(initAddresses []net.IP) (nbnet.AddHookFunc, nbnet.RemoveHookFunc, error) {
beforeHook := func(connID nbnet.ConnectionID, ip net.IP) error {
prefix, err := util.GetPrefixFromIP(ip)
if err != nil {
Expand Down Expand Up @@ -414,3 +415,58 @@ func isSubRange(prefix netip.Prefix) (bool, error) {
}
return false, nil
}

// IsAddrRouted checks if the candidate address would route to the vpn, in which case it returns true and the matched prefix.
func IsAddrRouted(addr netip.Addr, vpnRoutes []netip.Prefix) (bool, netip.Prefix) {
localRoutes, err := hasSeparateRouting()
if err != nil {
if !errors.Is(err, ErrRoutingIsSeparate) {
log.Errorf("Failed to get routes: %v", err)
}
return false, netip.Prefix{}
}

return isVpnRoute(addr, vpnRoutes, localRoutes)
}

func isVpnRoute(addr netip.Addr, vpnRoutes []netip.Prefix, localRoutes []netip.Prefix) (bool, netip.Prefix) {
vpnPrefixMap := map[netip.Prefix]struct{}{}
for _, prefix := range vpnRoutes {
vpnPrefixMap[prefix] = struct{}{}
}

// remove vpnRoute duplicates
for _, prefix := range localRoutes {
delete(vpnPrefixMap, prefix)
}

var longestPrefix netip.Prefix
var isVpn bool

combinedRoutes := make([]netip.Prefix, len(vpnRoutes)+len(localRoutes))
copy(combinedRoutes, vpnRoutes)
copy(combinedRoutes[len(vpnRoutes):], localRoutes)

for _, prefix := range combinedRoutes {
// Ignore the default route, it has special handling
if prefix.Bits() == 0 {
continue
}

if prefix.Contains(addr) {
// Longest prefix match
if !longestPrefix.IsValid() || prefix.Bits() > longestPrefix.Bits() {
longestPrefix = prefix
_, isVpn = vpnPrefixMap[prefix]
}
}
}

if !longestPrefix.IsValid() {
// No route matched
return false, netip.Prefix{}
}

// Return true if the longest matching prefix is from vpnRoutes
return isVpn, longestPrefix
}
Empty file.
7 changes: 7 additions & 0 deletions client/internal/routemanager/systemops/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,10 @@ func getAddressFamily(prefix netip.Prefix) int {
}
return netlink.FAMILY_V6
}

func hasSeparateRouting() ([]netip.Prefix, error) {
if isLegacy() {
return getRoutesFromTable()
}
return nil, ErrRoutingIsSeparate
}
12 changes: 0 additions & 12 deletions client/internal/routemanager/systemops/systemops_mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ func EnableIPForwarding() error {
return nil
}

func hasSeparateRouting() ([]netip.Prefix, error) {
return nil, ErrRoutingIsSeparate
}

func AddVPNRoute(netip.Prefix, *net.Interface) error {
return nil
}

func RemoveVPNRoute(netip.Prefix, *net.Interface) error {
return nil
}

func IsAddrRouted(netip.Addr, []netip.Prefix) (bool, netip.Prefix) {
return false, netip.Prefix{}
}
8 changes: 0 additions & 8 deletions client/internal/routemanager/systemops/systemops_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ func EnableIPForwarding() error {
return nil
}

func AddVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
return genericAddVPNRoute(prefix, intf)
}

func RemoveVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
return genericRemoveVPNRoute(prefix, intf)
}

func hasSeparateRouting() ([]netip.Prefix, error) {
return getRoutesFromTable()
}
1 change: 1 addition & 0 deletions util/net/net.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net

import (
"net"
"os"

"github.com/netbirdio/netbird/iface/netstack"
Expand Down

0 comments on commit ed1533f

Please sign in to comment.