Skip to content

Commit 188560a

Browse files
committed
fix: add a link-scope route if the cmdline gateway is not reachable
Fixes #7020 Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
1 parent 45c5b47 commit 188560a

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,43 @@ func (ctrl *RouteConfigController) parseCmdline(logger *zap.Logger) (routes []ne
192192
continue
193193
}
194194

195-
var route network.RouteSpecSpec
196-
197-
route.Gateway = linkConfig.Gateway
195+
// add a default gateway route
196+
defaultGatewayRoute := network.RouteSpecSpec{
197+
Gateway: linkConfig.Gateway,
198+
Scope: nethelpers.ScopeGlobal,
199+
Table: nethelpers.TableMain,
200+
Priority: DefaultRouteMetric + uint32(idx), // set different priorities to avoid a conflict
201+
Protocol: nethelpers.ProtocolBoot,
202+
Type: nethelpers.TypeUnicast,
203+
OutLinkName: linkConfig.LinkName,
204+
ConfigLayer: network.ConfigCmdline,
205+
}
198206

199-
if route.Gateway.Is6() {
200-
route.Family = nethelpers.FamilyInet6
207+
if defaultGatewayRoute.Gateway.Is6() {
208+
defaultGatewayRoute.Family = nethelpers.FamilyInet6
201209
} else {
202-
route.Family = nethelpers.FamilyInet4
210+
defaultGatewayRoute.Family = nethelpers.FamilyInet4
203211
}
204212

205-
route.Scope = nethelpers.ScopeGlobal
206-
route.Table = nethelpers.TableMain
207-
route.Priority = DefaultRouteMetric + uint32(idx) // set different priorities to avoid a conflict
208-
route.Protocol = nethelpers.ProtocolBoot
209-
route.Type = nethelpers.TypeUnicast
210-
route.OutLinkName = linkConfig.LinkName
211-
route.ConfigLayer = network.ConfigCmdline
212-
213-
route.Normalize()
214-
215-
routes = append(routes, route)
213+
defaultGatewayRoute.Normalize()
214+
215+
routes = append(routes, defaultGatewayRoute)
216+
217+
// for IPv4, if the gateway is not directly reachable on the link, add a link-scope route for the gateway
218+
if linkConfig.Gateway.Is4() && !linkConfig.Address.Contains(linkConfig.Gateway) {
219+
routes = append(routes, network.RouteSpecSpec{
220+
Family: nethelpers.FamilyInet4,
221+
Destination: netip.PrefixFrom(linkConfig.Gateway, linkConfig.Gateway.BitLen()),
222+
Source: linkConfig.Address.Addr(),
223+
OutLinkName: linkConfig.LinkName,
224+
Table: nethelpers.TableMain,
225+
Priority: defaultGatewayRoute.Priority,
226+
Scope: nethelpers.ScopeLink,
227+
Type: nethelpers.TypeUnicast,
228+
Protocol: nethelpers.ProtocolBoot,
229+
ConfigLayer: network.ConfigCmdline,
230+
})
231+
}
216232
}
217233

218234
return routes

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ func (suite *RouteConfigSuite) TestCmdline() {
102102
)
103103
}
104104

105+
func (suite *RouteConfigSuite) TestCmdlineNotReachable() {
106+
suite.Require().NoError(
107+
suite.runtime.RegisterController(
108+
&netctrl.RouteConfigController{
109+
Cmdline: procfs.NewCmdline("ip=172.20.0.2::172.20.0.1:255.255.255.255::eth1:::::"),
110+
},
111+
),
112+
)
113+
114+
suite.startRuntime()
115+
116+
suite.assertRoutes(
117+
[]string{
118+
"cmdline/inet4/172.20.0.1//1024",
119+
"cmdline/inet4//172.20.0.1/32/1024",
120+
}, func(r *network.RouteSpec, asrt *assert.Assertions) {
121+
asrt.Equal(network.ConfigCmdline, r.TypedSpec().ConfigLayer)
122+
asrt.Equal(nethelpers.FamilyInet4, r.TypedSpec().Family)
123+
124+
switch r.Metadata().ID() {
125+
case "cmdline/inet4/172.20.0.1//1024":
126+
asrt.Equal("eth1", r.TypedSpec().OutLinkName)
127+
asrt.EqualValues(netctrl.DefaultRouteMetric, r.TypedSpec().Priority)
128+
case "cmdline/inet4//172.20.0.1/32/1024":
129+
asrt.Equal("eth1", r.TypedSpec().OutLinkName)
130+
asrt.Equal(netip.Addr{}, r.TypedSpec().Gateway)
131+
asrt.Equal(netip.MustParsePrefix("172.20.0.1/32"), r.TypedSpec().Destination)
132+
asrt.EqualValues(netctrl.DefaultRouteMetric, r.TypedSpec().Priority)
133+
}
134+
},
135+
)
136+
}
137+
105138
func (suite *RouteConfigSuite) TestMachineConfiguration() {
106139
suite.Require().NoError(suite.runtime.RegisterController(&netctrl.RouteConfigController{}))
107140

0 commit comments

Comments
 (0)