Skip to content

Commit

Permalink
fix: make route normalization keep family
Browse files Browse the repository at this point in the history
When we normalize the route with e.g. IPv6 all addresses (`::/0`), we
were wiping the family information. Keep the information, and also fix
the scope for such routes.

Fixes siderolabs#9624

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Nov 4, 2024
1 parent 0a3761c commit 74b0e8c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (ctrl *RouteConfigController) processDevicesConfiguration(logger *zap.Logge
}
}

route.Normalize()
normalizedFamily := route.Normalize()

route.Priority = in.Metric()
if route.Priority == 0 {
Expand All @@ -271,6 +271,8 @@ func (ctrl *RouteConfigController) processDevicesConfiguration(logger *zap.Logge
route.Family = nethelpers.FamilyInet6
case !value.IsZero(route.Destination) && route.Destination.Addr().Is6():
route.Family = nethelpers.FamilyInet6
case normalizedFamily != 0:
route.Family = normalizedFamily
default:
route.Family = nethelpers.FamilyInet4
}
Expand Down
38 changes: 32 additions & 6 deletions pkg/machinery/resources/network/route_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,54 @@ var (
)

// Normalize converts 0.0.0.0 to zero value.
func (route *RouteSpecSpec) Normalize() {
if route.Destination.Bits() == 0 && (route.Destination.Addr().Compare(zero4) == 0 || route.Destination.Addr().Compare(zero16) == 0) {
//
//nolint:gocyclo
func (route *RouteSpecSpec) Normalize() nethelpers.Family {
var family nethelpers.Family

if route.Destination.Bits() == 0 {
// clear destination to be zero value to support "0.0.0.0/0" routes
route.Destination = netip.Prefix{}
if route.Destination.Addr().Compare(zero4) == 0 {
family = nethelpers.FamilyInet4
route.Destination = netip.Prefix{}
}

if route.Destination.Addr().Compare(zero16) == 0 {
family = nethelpers.FamilyInet6
route.Destination = netip.Prefix{}
}
}

if route.Gateway.Compare(zero4) == 0 {
family = nethelpers.FamilyInet4
route.Gateway = netip.Addr{}
}

if route.Gateway.Compare(zero4) == 0 || route.Gateway.Compare(zero16) == 0 {
if route.Gateway.Compare(zero16) == 0 {
family = nethelpers.FamilyInet6
route.Gateway = netip.Addr{}
}

if route.Source.Compare(zero4) == 0 || route.Source.Compare(zero16) == 0 {
if route.Source.Compare(zero4) == 0 {
family = nethelpers.FamilyInet4
route.Source = netip.Addr{}
}

if route.Source.Compare(zero16) == 0 {
family = nethelpers.FamilyInet6
route.Source = netip.Addr{}
}

switch {
case value.IsZero(route.Gateway):
case value.IsZero(route.Gateway) && !value.IsZero(route.Destination):
route.Scope = nethelpers.ScopeLink
case route.Destination.Addr().IsLoopback():
route.Scope = nethelpers.ScopeHost
default:
route.Scope = nethelpers.ScopeGlobal
}

return family
}

// NewRouteSpec initializes a RouteSpec resource.
Expand Down
24 changes: 23 additions & 1 deletion pkg/machinery/resources/network/route_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,31 @@ func TestRoutSpecNormalize(t *testing.T) {
MTU: 1400,
}

spec.Normalize()
normalizedFamily := spec.Normalize()

assert.Equal(t, netip.Prefix{}, spec.Destination)
assert.Equal(t, netip.Addr{}, spec.Source)
assert.Equal(t, netip.Addr{}, spec.Gateway)
assert.Equal(t, nethelpers.FamilyInet4, normalizedFamily)
assert.Equal(t, nethelpers.ScopeGlobal, spec.Scope)
}

func TestRoutSpecNormalizeV6(t *testing.T) {
spec := network.RouteSpecSpec{
Family: nethelpers.FamilyInet4,
Destination: netip.MustParsePrefix("::/0"),
OutLinkName: "eth0",
Table: nethelpers.TableLocal,
Priority: 1024,
ConfigLayer: network.ConfigPlatform,
MTU: 1400,
}

normalizedFamily := spec.Normalize()

assert.Equal(t, netip.Prefix{}, spec.Destination)
assert.Equal(t, netip.Addr{}, spec.Source)
assert.Equal(t, netip.Addr{}, spec.Gateway)
assert.Equal(t, nethelpers.FamilyInet6, normalizedFamily)
assert.Equal(t, nethelpers.ScopeGlobal, spec.Scope)
}

0 comments on commit 74b0e8c

Please sign in to comment.