@@ -111,6 +111,10 @@ void RoutingTable::ModifyRouteImpl(const Route& route, Operation action)
111111 {
112112 ModifyLoopbackRouteImpl<TAddr>(route, operation, flags);
113113 }
114+ else if (route.defaultRoute && route.IsOnlink ())
115+ {
116+ ModifyDefaultLinkLocalRouteImpl<TAddr>(route, operation, flags);
117+ }
114118 else if (route.defaultRoute )
115119 {
116120 ModifyDefaultRouteImpl<TAddr>(route, operation, flags);
@@ -181,7 +185,7 @@ void RoutingTable::ModifyLoopbackRouteImpl(const Route& route, int operation, in
181185{
182186 if (!route.to .has_value () || !route.via .has_value ())
183187 {
184- throw RuntimeErrorWithSourceLocation (std::format (" Loopback route {} missing destination or gateway address " , utils::Stringify (route)));
188+ throw RuntimeErrorWithSourceLocation (std::format (" Loopback route {} missing destination or next hop " , utils::Stringify (route)));
185189 }
186190
187191 struct Message : RouteMessage
@@ -193,8 +197,8 @@ void RoutingTable::ModifyLoopbackRouteImpl(const Route& route, int operation, in
193197
194198 GNS_LOG_INFO (
195199 " SendMessage Route (to {}, via {}), operation ({}), netLinkflags ({})" ,
196- route.to .has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
197- route.via .has_value () ? route. via . value ().Addr ().c_str () : " [empty] " ,
200+ route.to .value ().Addr ().c_str (),
201+ route.via .value ().Addr ().c_str (),
198202 RouteOperationToString (operation),
199203 NetLinkFormatFlagsToString (flags).c_str ());
200204
@@ -222,20 +226,52 @@ void RoutingTable::ModifyLoopbackRouteImpl(const Route& route, int operation, in
222226
223227 message.route .rtm_flags |= RTNH_F_ONLINK;
224228 GNS_LOG_INFO (
225- " InitializeAddressAttribute RTA_DST ({}) RTA_GATEWAY ({}) RTA_PRIORITY ([not set])" ,
229+ " Netlink message configuration: RTA_DST ({}) RTA_GATEWAY ({}) RTA_PRIORITY ([not set])" ,
226230 route.to .value ().Addr ().c_str (),
227231 route.via .value ().Addr ().c_str ());
228232 utils::InitializeAddressAttribute<TAddr>(message.to , route.to .value (), RTA_DST);
229233 utils::InitializeAddressAttribute<TAddr>(message.via , route.via .value (), RTA_GATEWAY);
230234 });
231235}
232236
237+ template <typename TAddr>
238+ void RoutingTable::ModifyDefaultLinkLocalRouteImpl (const Route& route, int operation, int flags)
239+ {
240+ if (route.via .has_value ())
241+ {
242+ throw RuntimeErrorWithSourceLocation (" Default route has unexpected next hop" );
243+ }
244+ if (route.to .has_value ())
245+ {
246+ throw RuntimeErrorWithSourceLocation (" Default route has unexpected destination address" );
247+ }
248+
249+ struct Message : RouteMessage
250+ {
251+ utils::IntegerAttribute metric;
252+ } __attribute__ ((packed));
253+
254+ GNS_LOG_INFO (
255+ " SendMessage Route (default onlink), operation ({}), netLinkflags ({})" ,
256+ RouteOperationToString (operation),
257+ NetLinkFormatFlagsToString (flags).c_str ());
258+
259+ SendMessage<Message>(route, operation, flags, [&](Message& message) {
260+ GNS_LOG_INFO (" Netlink message configuration: RTA_DST ([not set]) RTA_GATEWAY ([not set]), RTA_PRIORITY ({})" , route.metric );
261+ utils::InitializeIntegerAttribute (message.metric , route.metric , RTA_PRIORITY);
262+ });
263+ }
264+
233265template <typename TAddr>
234266void RoutingTable::ModifyDefaultRouteImpl (const Route& route, int operation, int flags)
235267{
236268 if (!route.via .has_value ())
237269 {
238- throw RuntimeErrorWithSourceLocation (" Default route is missing its gateway address" );
270+ throw RuntimeErrorWithSourceLocation (" Default route is missing its next hop" );
271+ }
272+ if (route.to .has_value ())
273+ {
274+ throw RuntimeErrorWithSourceLocation (" Default route has unexpected destination address" );
239275 }
240276
241277 struct Message : RouteMessage
@@ -246,15 +282,15 @@ void RoutingTable::ModifyDefaultRouteImpl(const Route& route, int operation, int
246282
247283 GNS_LOG_INFO (
248284 " SendMessage Route (to {}, via {}), operation ({}), netLinkflags ({})" ,
249- route. to . has_value () ? route. to . value (). Addr (). c_str () : " [empty]" ,
250- route.via .has_value () ? route. via . value ().Addr ().c_str () : " [empty] " ,
285+ " [empty]" ,
286+ route.via .value ().Addr ().c_str (),
251287 RouteOperationToString (operation),
252288 NetLinkFormatFlagsToString (flags).c_str ());
253289
254290 SendMessage<Message>(route, operation, flags, [&](Message& message) {
255291 GNS_LOG_INFO (
256- " InitializeAddressAttribute RTA_DST ([not set]) RTA_GATEWAY ({}), RTA_PRIORITY ({})" ,
257- route.to . has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
292+ " Netlink message configuration: RTA_DST ([not set]) RTA_GATEWAY ({}), RTA_PRIORITY ({})" ,
293+ route.via . value ().Addr ().c_str (),
258294 route.metric );
259295 utils::InitializeAddressAttribute<TAddr>(message.via , route.via .value (), RTA_GATEWAY);
260296 utils::InitializeIntegerAttribute (message.metric , route.metric , RTA_PRIORITY);
@@ -264,6 +300,15 @@ void RoutingTable::ModifyDefaultRouteImpl(const Route& route, int operation, int
264300template <typename TAddr>
265301void RoutingTable::ModifyLinkLocalRouteImpl (const Route& route, int operation, int flags)
266302{
303+ if (!route.to .has_value ())
304+ {
305+ throw RuntimeErrorWithSourceLocation (" Link-local route is missing its destination address" );
306+ }
307+ if (route.via .has_value ())
308+ {
309+ throw RuntimeErrorWithSourceLocation (" Link-local route has unexpected next hop" );
310+ }
311+
267312 struct Message : RouteMessage
268313 {
269314 utils::AddressAttribute<TAddr> to;
@@ -272,15 +317,15 @@ void RoutingTable::ModifyLinkLocalRouteImpl(const Route& route, int operation, i
272317
273318 GNS_LOG_INFO (
274319 " SendMessage Route (to {}, via {}), operation ({}), netLinkflags ({})" ,
275- route.to .has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
276- route. via . has_value () ? route. via . value (). Addr (). c_str () : " [empty]" ,
320+ route.to .value ().Addr ().c_str (),
321+ " [empty]" ,
277322 RouteOperationToString (operation),
278323 NetLinkFormatFlagsToString (flags).c_str ());
279324
280325 SendMessage<Message>(route, operation, flags, [&](Message& message) {
281326 GNS_LOG_INFO (
282- " InitializeAddressAttribute RTA_DST ({}) RTA_GATEWAY ([not set]), RTA_PRIORITY ({})" ,
283- route.to .has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
327+ " Netlink message configuration: RTA_DST ({}) RTA_GATEWAY ([not set]), RTA_PRIORITY ({})" ,
328+ route.to .value ().Addr ().c_str (),
284329 route.metric );
285330 utils::InitializeAddressAttribute<TAddr>(message.to , route.to .value (), RTA_DST);
286331 utils::InitializeIntegerAttribute (message.metric , route.metric , RTA_PRIORITY);
@@ -294,6 +339,10 @@ void RoutingTable::ModifyOfflinkRouteImpl(const Route& route, int operation, int
294339 {
295340 throw RuntimeErrorWithSourceLocation (" Offlink route is missing its next hop" );
296341 }
342+ if (!route.to .has_value ())
343+ {
344+ throw RuntimeErrorWithSourceLocation (" Offlink route is missing its destination address" );
345+ }
297346
298347 struct Message : RouteMessage
299348 {
@@ -304,16 +353,16 @@ void RoutingTable::ModifyOfflinkRouteImpl(const Route& route, int operation, int
304353
305354 GNS_LOG_INFO (
306355 " SendMessage Route (to {}, via {}), operation ({}), netLinkflags ({})" ,
307- route.to .has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
308- route.via .has_value () ? route. via . value ().Addr ().c_str () : " [empty] " ,
356+ route.to .value ().Addr ().c_str (),
357+ route.via .value ().Addr ().c_str (),
309358 RouteOperationToString (operation),
310359 NetLinkFormatFlagsToString (flags).c_str ());
311360
312361 SendMessage<Message>(route, operation, flags, [&](Message& message) {
313362 GNS_LOG_INFO (
314- " InitializeAddressAttribute RTA_DST ({}) RTA_GATEWAY ({}), RTA_PRIORITY ({})" ,
315- route.to .has_value () ? route. to . value ().Addr ().c_str () : " [empty] " ,
316- route.via .has_value () ? route. via . value ().Addr ().c_str () : " [empty] " ,
363+ " Netlink message configuration: RTA_DST ({}) RTA_GATEWAY ({}), RTA_PRIORITY ({})" ,
364+ route.to .value ().Addr ().c_str (),
365+ route.via .value ().Addr ().c_str (),
317366 route.metric );
318367 utils::InitializeAddressAttribute<TAddr>(message.to , route.to .value (), RTA_DST);
319368 utils::InitializeAddressAttribute<TAddr>(message.via , route.via .value (), RTA_GATEWAY);
0 commit comments