Skip to content

Commit 83abb66

Browse files
committed
fix: make route normalization keep family
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 #9624 Signed-off-by: Andrey Smirnov <[email protected]> (cherry picked from commit 74b0e8c)
1 parent 228a943 commit 83abb66

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

internal/app/machined/pkg/controllers/network/route_config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (ctrl *RouteConfigController) processDevicesConfiguration(logger *zap.Logge
257257
}
258258
}
259259

260-
route.Normalize()
260+
normalizedFamily := route.Normalize()
261261

262262
route.Priority = in.Metric()
263263
if route.Priority == 0 {
@@ -271,6 +271,8 @@ func (ctrl *RouteConfigController) processDevicesConfiguration(logger *zap.Logge
271271
route.Family = nethelpers.FamilyInet6
272272
case !value.IsZero(route.Destination) && route.Destination.Addr().Is6():
273273
route.Family = nethelpers.FamilyInet6
274+
case normalizedFamily != 0:
275+
route.Family = normalizedFamily
274276
default:
275277
route.Family = nethelpers.FamilyInet4
276278
}

pkg/machinery/resources/network/route_spec.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,54 @@ var (
4848
)
4949

5050
// Normalize converts 0.0.0.0 to zero value.
51-
func (route *RouteSpecSpec) Normalize() {
52-
if route.Destination.Bits() == 0 && (route.Destination.Addr().Compare(zero4) == 0 || route.Destination.Addr().Compare(zero16) == 0) {
51+
//
52+
//nolint:gocyclo
53+
func (route *RouteSpecSpec) Normalize() nethelpers.Family {
54+
var family nethelpers.Family
55+
56+
if route.Destination.Bits() == 0 {
5357
// clear destination to be zero value to support "0.0.0.0/0" routes
54-
route.Destination = netip.Prefix{}
58+
if route.Destination.Addr().Compare(zero4) == 0 {
59+
family = nethelpers.FamilyInet4
60+
route.Destination = netip.Prefix{}
61+
}
62+
63+
if route.Destination.Addr().Compare(zero16) == 0 {
64+
family = nethelpers.FamilyInet6
65+
route.Destination = netip.Prefix{}
66+
}
67+
}
68+
69+
if route.Gateway.Compare(zero4) == 0 {
70+
family = nethelpers.FamilyInet4
71+
route.Gateway = netip.Addr{}
5572
}
5673

57-
if route.Gateway.Compare(zero4) == 0 || route.Gateway.Compare(zero16) == 0 {
74+
if route.Gateway.Compare(zero16) == 0 {
75+
family = nethelpers.FamilyInet6
5876
route.Gateway = netip.Addr{}
5977
}
6078

61-
if route.Source.Compare(zero4) == 0 || route.Source.Compare(zero16) == 0 {
79+
if route.Source.Compare(zero4) == 0 {
80+
family = nethelpers.FamilyInet4
81+
route.Source = netip.Addr{}
82+
}
83+
84+
if route.Source.Compare(zero16) == 0 {
85+
family = nethelpers.FamilyInet6
6286
route.Source = netip.Addr{}
6387
}
6488

6589
switch {
66-
case value.IsZero(route.Gateway):
90+
case value.IsZero(route.Gateway) && !value.IsZero(route.Destination):
6791
route.Scope = nethelpers.ScopeLink
6892
case route.Destination.Addr().IsLoopback():
6993
route.Scope = nethelpers.ScopeHost
7094
default:
7195
route.Scope = nethelpers.ScopeGlobal
7296
}
97+
98+
return family
7399
}
74100

75101
// NewRouteSpec initializes a RouteSpec resource.

pkg/machinery/resources/network/route_spec_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,31 @@ func TestRoutSpecNormalize(t *testing.T) {
7373
MTU: 1400,
7474
}
7575

76-
spec.Normalize()
76+
normalizedFamily := spec.Normalize()
7777

7878
assert.Equal(t, netip.Prefix{}, spec.Destination)
7979
assert.Equal(t, netip.Addr{}, spec.Source)
8080
assert.Equal(t, netip.Addr{}, spec.Gateway)
81+
assert.Equal(t, nethelpers.FamilyInet4, normalizedFamily)
82+
assert.Equal(t, nethelpers.ScopeGlobal, spec.Scope)
83+
}
84+
85+
func TestRoutSpecNormalizeV6(t *testing.T) {
86+
spec := network.RouteSpecSpec{
87+
Family: nethelpers.FamilyInet4,
88+
Destination: netip.MustParsePrefix("::/0"),
89+
OutLinkName: "eth0",
90+
Table: nethelpers.TableLocal,
91+
Priority: 1024,
92+
ConfigLayer: network.ConfigPlatform,
93+
MTU: 1400,
94+
}
95+
96+
normalizedFamily := spec.Normalize()
97+
98+
assert.Equal(t, netip.Prefix{}, spec.Destination)
99+
assert.Equal(t, netip.Addr{}, spec.Source)
100+
assert.Equal(t, netip.Addr{}, spec.Gateway)
101+
assert.Equal(t, nethelpers.FamilyInet6, normalizedFamily)
102+
assert.Equal(t, nethelpers.ScopeGlobal, spec.Scope)
81103
}

0 commit comments

Comments
 (0)