Skip to content

Commit 79b6efc

Browse files
committed
Remove add gateway logic
1 parent 988ecfb commit 79b6efc

File tree

5 files changed

+51
-207
lines changed

5 files changed

+51
-207
lines changed

client/internal/routemanager/systemops/systemops_generic.go

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -106,47 +106,6 @@ func (r *SysOps) cleanupRefCounter(stateManager *statemanager.Manager) error {
106106
return nil
107107
}
108108

109-
// TODO: fix: for default our wg address now appears as the default gw
110-
func (r *SysOps) addRouteForCurrentDefaultGateway(prefix netip.Prefix) error {
111-
addr := netip.IPv4Unspecified()
112-
if prefix.Addr().Is6() {
113-
addr = netip.IPv6Unspecified()
114-
}
115-
116-
nexthop, err := GetNextHop(addr)
117-
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
118-
return fmt.Errorf("get existing route gateway: %s", err)
119-
}
120-
121-
if !prefix.Contains(nexthop.IP) {
122-
log.Debugf("Skipping adding a new route for gateway %s because it is not in the network %s", nexthop.IP, prefix)
123-
return nil
124-
}
125-
126-
gatewayPrefix := netip.PrefixFrom(nexthop.IP, 32)
127-
if nexthop.IP.Is6() {
128-
gatewayPrefix = netip.PrefixFrom(nexthop.IP, 128)
129-
}
130-
131-
ok, err := existsInRouteTable(gatewayPrefix)
132-
if err != nil {
133-
return fmt.Errorf("unable to check if there is an existing route for gateway %s. error: %s", gatewayPrefix, err)
134-
}
135-
136-
if ok {
137-
log.Debugf("Skipping adding a new route for gateway %s because it already exists", gatewayPrefix)
138-
return nil
139-
}
140-
141-
nexthop, err = GetNextHop(nexthop.IP)
142-
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
143-
return fmt.Errorf("unable to get the next hop for the default gateway address. error: %s", err)
144-
}
145-
146-
log.Debugf("Adding a new route for gateway %s with next hop %s", gatewayPrefix, nexthop.IP)
147-
return r.addToRouteTable(gatewayPrefix, nexthop)
148-
}
149-
150109
// addRouteToNonVPNIntf adds a new route to the routing table for the given prefix and returns the next hop and interface.
151110
// If the next hop or interface is pointing to the VPN interface, it will return the initial values.
152111
func (r *SysOps) addRouteToNonVPNIntf(prefix netip.Prefix, vpnIntf iface.WGIface, initialNextHop Nexthop) (Nexthop, error) {
@@ -271,32 +230,7 @@ func (r *SysOps) genericAddVPNRoute(prefix netip.Prefix, intf *net.Interface) er
271230
return nil
272231
}
273232

274-
return r.addNonExistingRoute(prefix, intf)
275-
}
276-
277-
// addNonExistingRoute adds a new route to the vpn interface if it doesn't exist in the current routing table
278-
func (r *SysOps) addNonExistingRoute(prefix netip.Prefix, intf *net.Interface) error {
279-
ok, err := existsInRouteTable(prefix)
280-
if err != nil {
281-
return fmt.Errorf("exists in route table: %w", err)
282-
}
283-
if ok {
284-
log.Warnf("Skipping adding a new route for network %s because it already exists", prefix)
285-
return nil
286-
}
287-
288-
ok, err = isSubRange(prefix)
289-
if err != nil {
290-
return fmt.Errorf("sub range: %w", err)
291-
}
292-
293-
if ok {
294-
if err := r.addRouteForCurrentDefaultGateway(prefix); err != nil {
295-
log.Warnf("Unable to add route for current default gateway route. Will proceed without it. error: %s", err)
296-
}
297-
}
298-
299-
return r.addToRouteTable(prefix, Nexthop{netip.Addr{}, intf})
233+
return r.addToRouteTable(prefix, nextHop)
300234
}
301235

302236
// genericRemoveVPNRoute removes the route from the vpn interface. If a default prefix is given,
@@ -457,32 +391,6 @@ func ipToAddr(ip net.IP, intf *net.Interface) (netip.Addr, error) {
457391
return addr.Unmap(), nil
458392
}
459393

460-
func existsInRouteTable(prefix netip.Prefix) (bool, error) {
461-
routes, err := GetRoutesFromTable()
462-
if err != nil {
463-
return false, fmt.Errorf("get routes from table: %w", err)
464-
}
465-
for _, tableRoute := range routes {
466-
if tableRoute == prefix {
467-
return true, nil
468-
}
469-
}
470-
return false, nil
471-
}
472-
473-
func isSubRange(prefix netip.Prefix) (bool, error) {
474-
routes, err := GetRoutesFromTable()
475-
if err != nil {
476-
return false, fmt.Errorf("get routes from table: %w", err)
477-
}
478-
for _, tableRoute := range routes {
479-
if tableRoute.Bits() > vars.MinRangeBits && tableRoute.Contains(prefix.Addr()) && tableRoute.Bits() < prefix.Bits() {
480-
return true, nil
481-
}
482-
}
483-
return false, nil
484-
}
485-
486394
// IsAddrRouted checks if the candidate address would route to the vpn, in which case it returns true and the matched prefix.
487395
func IsAddrRouted(addr netip.Addr, vpnRoutes []netip.Prefix) (bool, netip.Prefix) {
488396
localRoutes, err := hasSeparateRouting()

client/internal/routemanager/systemops/systemops_generic_test.go

Lines changed: 30 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/netip"
1111
"os"
1212
"runtime"
13-
"strings"
1413
"testing"
1514

1615
"github.com/pion/transport/v3/stdnet"
@@ -165,25 +164,20 @@ func TestGetNextHop(t *testing.T) {
165164
func TestAddExistAndRemoveRoute(t *testing.T) {
166165
defaultNexthop, err := GetNextHop(netip.MustParseAddr("0.0.0.0"))
167166
t.Log("defaultNexthop: ", defaultNexthop)
168-
if err != nil {
169-
t.Fatal("shouldn't return error when fetching the gateway: ", err)
170-
}
167+
require.NoError(t, err, "shouldn't return error when fetching the gateway")
168+
171169
testCases := []struct {
172170
name string
173171
prefix netip.Prefix
174172
preExistingPrefix netip.Prefix
175173
shouldAddRoute bool
174+
expectError bool
176175
}{
177176
{
178177
name: "Should Add And Remove random Route",
179178
prefix: netip.MustParsePrefix("99.99.99.99/32"),
180179
shouldAddRoute: true,
181180
},
182-
{
183-
name: "Should Not Add Route if overlaps with default gateway",
184-
prefix: netip.MustParsePrefix(defaultNexthop.IP.String() + "/31"),
185-
shouldAddRoute: false,
186-
},
187181
{
188182
name: "Should Add Route if bigger network exists",
189183
prefix: netip.MustParsePrefix("100.100.100.0/24"),
@@ -197,10 +191,10 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
197191
shouldAddRoute: true,
198192
},
199193
{
200-
name: "Should Not Add Route if same network exists",
194+
name: "Should Error on duplicate route",
201195
prefix: netip.MustParsePrefix("100.100.0.0/16"),
202196
preExistingPrefix: netip.MustParsePrefix("100.100.0.0/16"),
203-
shouldAddRoute: false,
197+
expectError: true,
204198
},
205199
}
206200

@@ -217,9 +211,8 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
217211

218212
peerPrivateKey, _ := wgtypes.GeneratePrivateKey()
219213
newNet, err := stdnet.NewNet()
220-
if err != nil {
221-
t.Fatal(err)
222-
}
214+
require.NoError(t, err, "should create new net")
215+
223216
opts := iface.WGIFaceOpts{
224217
IFaceName: fmt.Sprintf("utun53%d", n),
225218
Address: "100.65.75.2/24",
@@ -249,6 +242,12 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
249242

250243
// Add the route
251244
err = r.AddVPNRoute(testCase.prefix, intf)
245+
246+
if testCase.expectError {
247+
require.Error(t, err, "should return error")
248+
return
249+
}
250+
252251
require.NoError(t, err, "should not return err when adding route")
253252

254253
if testCase.shouldAddRoute {
@@ -260,97 +259,16 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
260259
// remove route again if added
261260
err = r.RemoveVPNRoute(testCase.prefix, intf)
262261
require.NoError(t, err, "should not return err")
263-
}
264-
265-
// route should either not have been added or should have been removed
266-
// In case of already existing route, it should not have been added (but still exist)
267-
ok, err := existsInRouteTable(testCase.prefix)
268-
t.Log("Buffer string: ", buf.String())
269-
require.NoError(t, err, "should not return err")
270262

271-
if !strings.Contains(buf.String(), "because it already exists") {
272-
require.False(t, ok, "route should not exist")
263+
// route should be removed
264+
ok, err = existsInRouteTable(testCase.prefix)
265+
require.NoError(t, err, "should not return err")
266+
require.False(t, ok, "route should not exist after removal")
273267
}
274268
})
275269
}
276270
}
277271

278-
func TestIsSubRange(t *testing.T) {
279-
addresses, err := net.InterfaceAddrs()
280-
if err != nil {
281-
t.Fatal("shouldn't return error when fetching interface addresses: ", err)
282-
}
283-
284-
var subRangeAddressPrefixes []netip.Prefix
285-
var nonSubRangeAddressPrefixes []netip.Prefix
286-
for _, address := range addresses {
287-
p := netip.MustParsePrefix(address.String())
288-
if !p.Addr().IsLoopback() && p.Addr().Is4() && p.Bits() < 32 {
289-
p2 := netip.PrefixFrom(p.Masked().Addr(), p.Bits()+1)
290-
subRangeAddressPrefixes = append(subRangeAddressPrefixes, p2)
291-
nonSubRangeAddressPrefixes = append(nonSubRangeAddressPrefixes, p.Masked())
292-
}
293-
}
294-
295-
for _, prefix := range subRangeAddressPrefixes {
296-
isSubRangePrefix, err := isSubRange(prefix)
297-
if err != nil {
298-
t.Fatal("shouldn't return error when checking if address is sub-range: ", err)
299-
}
300-
if !isSubRangePrefix {
301-
t.Fatalf("address %s should be sub-range of an existing route in the table", prefix)
302-
}
303-
}
304-
305-
for _, prefix := range nonSubRangeAddressPrefixes {
306-
isSubRangePrefix, err := isSubRange(prefix)
307-
if err != nil {
308-
t.Fatal("shouldn't return error when checking if address is sub-range: ", err)
309-
}
310-
if isSubRangePrefix {
311-
t.Fatalf("address %s should not be sub-range of an existing route in the table", prefix)
312-
}
313-
}
314-
}
315-
316-
func TestExistsInRouteTable(t *testing.T) {
317-
addresses, err := net.InterfaceAddrs()
318-
if err != nil {
319-
t.Fatal("shouldn't return error when fetching interface addresses: ", err)
320-
}
321-
322-
var addressPrefixes []netip.Prefix
323-
for _, address := range addresses {
324-
p := netip.MustParsePrefix(address.String())
325-
326-
switch {
327-
case p.Addr().Is6():
328-
continue
329-
// Windows sometimes has hidden interface link local addrs that don't turn up on any interface
330-
case runtime.GOOS == "windows" && p.Addr().IsLinkLocalUnicast():
331-
continue
332-
// Linux loopback 127/8 is in the local table, not in the main table and always takes precedence
333-
case runtime.GOOS == "linux" && p.Addr().IsLoopback():
334-
continue
335-
// FreeBSD loopback 127/8 is not added to the routing table
336-
case runtime.GOOS == "freebsd" && p.Addr().IsLoopback():
337-
continue
338-
default:
339-
addressPrefixes = append(addressPrefixes, p.Masked())
340-
}
341-
}
342-
343-
for _, prefix := range addressPrefixes {
344-
exists, err := existsInRouteTable(prefix)
345-
if err != nil {
346-
t.Fatal("shouldn't return error when checking if address exists in route table: ", err)
347-
}
348-
if !exists {
349-
t.Fatalf("address %s should exist in route table", prefix)
350-
}
351-
}
352-
}
353-
354272
func createWGInterface(t *testing.T, interfaceName, ipAddressCIDR string, listenPort int) *iface.WGIface {
355273
t.Helper()
356274

@@ -565,3 +483,16 @@ func TestIsVpnRoute(t *testing.T) {
565483
})
566484
}
567485
}
486+
487+
func existsInRouteTable(prefix netip.Prefix) (bool, error) {
488+
routes, err := GetRoutesFromTable()
489+
if err != nil {
490+
return false, fmt.Errorf("get routes from table: %w", err)
491+
}
492+
for _, tableRoute := range routes {
493+
if tableRoute == prefix {
494+
return true, nil
495+
}
496+
}
497+
return false, nil
498+
}

client/internal/routemanager/systemops/systemops_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func addRoute(prefix netip.Prefix, nexthop Nexthop, tableID int) error {
247247
return fmt.Errorf("add gateway and device: %w", err)
248248
}
249249

250-
if err := netlink.RouteAdd(route); err != nil && !errors.Is(err, syscall.EEXIST) && !isOpErr(err) {
250+
if err := netlink.RouteAdd(route); err != nil && !isOpErr(err) {
251251
return fmt.Errorf("netlink add route: %w", err)
252252
}
253253

@@ -270,7 +270,7 @@ func addUnreachableRoute(prefix netip.Prefix, tableID int) error {
270270
Dst: ipNet,
271271
}
272272

273-
if err := netlink.RouteAdd(route); err != nil && !errors.Is(err, syscall.EEXIST) && !isOpErr(err) {
273+
if err := netlink.RouteAdd(route); err != nil && !isOpErr(err) {
274274
return fmt.Errorf("netlink add unreachable route: %w", err)
275275
}
276276

client/internal/routemanager/systemops/systemops_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func cancelMibChangeNotify2(handle windows.Handle) error {
492492
}
493493

494494
// GetRoutesFromTable returns the current routing table from with prefixes only.
495-
// It ccaches the result for 2 seconds to avoid blocking the caller.
495+
// It caches the result for 2 seconds to avoid blocking the caller.
496496
func GetRoutesFromTable() ([]netip.Prefix, error) {
497497
mux.Lock()
498498
defer mux.Unlock()

0 commit comments

Comments
 (0)