@@ -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
0 commit comments