diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 694a6a4139..d51acce791 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -1230,24 +1230,17 @@ ip_route_add (NMPlatform *platform, ? NMP_OBJECT_TYPE_IP4_ROUTE : NMP_OBJECT_TYPE_IP6_ROUTE, (const NMPlatformObject *) route); - r = &obj->ip_route; + r = NMP_OBJECT_CAST_IP_ROUTE (obj); + nm_platform_ip_route_normalize (addr_family, r); switch (addr_family) { case AF_INET: r4 = NMP_OBJECT_CAST_IP4_ROUTE (obj); - r4->network = nm_utils_ip4_address_clear_host_address (r4->network, r4->plen); - r4->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r4->rt_source), - r4->scope_inv = nm_platform_route_scope_inv (!r4->gateway - ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); if (r4->gateway) has_gateway = TRUE; break; case AF_INET6: r6 = NMP_OBJECT_CAST_IP6_ROUTE (obj); - nm_utils_ip6_address_clear_host_address (&r6->network, &r6->network, r6->plen); - r6->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r6->rt_source), - r6->metric = nm_utils_ip6_route_metric_normalize (r6->metric); - nm_utils_ip6_address_clear_host_address (&r6->src, &r6->src, r6->src_plen); if (!IN6_IS_ADDR_UNSPECIFIED (&r6->gateway)) has_gateway = TRUE; break; diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 71d564bd23..38b5691136 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -5836,30 +5836,20 @@ ip_route_add (NMPlatform *platform, { nm_auto_nlmsg struct nl_msg *nlmsg = NULL; NMPObject obj; - NMPlatformIP4Route *r4; - NMPlatformIP6Route *r6; switch (addr_family) { case AF_INET: nmp_object_stackinit (&obj, NMP_OBJECT_TYPE_IP4_ROUTE, (const NMPlatformObject *) route); - r4 = NMP_OBJECT_CAST_IP4_ROUTE (&obj); - r4->network = nm_utils_ip4_address_clear_host_address (r4->network, r4->plen); - r4->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r4->rt_source), - r4->scope_inv = nm_platform_route_scope_inv (!r4->gateway - ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); break; case AF_INET6: nmp_object_stackinit (&obj, NMP_OBJECT_TYPE_IP6_ROUTE, (const NMPlatformObject *) route); - r6 = NMP_OBJECT_CAST_IP6_ROUTE (&obj); - nm_utils_ip6_address_clear_host_address (&r6->network, &r6->network, r6->plen); - r6->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r6->rt_source), - r6->metric = nm_utils_ip6_route_metric_normalize (r6->metric); - nm_utils_ip6_address_clear_host_address (&r6->src, &r6->src, r6->src_plen); break; default: nm_assert_not_reached (); } + nm_platform_ip_route_normalize (addr_family, NMP_OBJECT_CAST_IP_ROUTE (&obj)); + nlmsg = _nl_msg_new_route (RTM_NEWROUTE, flags, &obj); if (!nlmsg) g_return_val_if_reached (FALSE); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 705e54fa5e..f392537e2a 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -3482,6 +3482,43 @@ nm_platform_ip_address_flush (NMPlatform *self, /*****************************************************************************/ +/** + * nm_platform_ip_route_normalize: + * @addr_family: AF_INET or AF_INET6 + * @route: an NMPlatformIP4Route or NMPlatformIP6Route instance, depending on @addr_family. + * + * Adding a route to kernel via nm_platform_ip_route_add() will normalize/coerce some + * properties of the route. This function modifies (normalizes) the route like it + * would be done by adding the route in kernel. + */ +void +nm_platform_ip_route_normalize (int addr_family, + NMPlatformIPRoute *route) +{ + NMPlatformIP4Route *r4; + NMPlatformIP6Route *r6; + + switch (addr_family) { + case AF_INET: + r4 = (NMPlatformIP4Route *) route; + r4->network = nm_utils_ip4_address_clear_host_address (r4->network, r4->plen); + r4->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r4->rt_source); + r4->scope_inv = nm_platform_route_scope_inv (!r4->gateway + ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); + break; + case AF_INET6: + r6 = (NMPlatformIP6Route *) route; + nm_utils_ip6_address_clear_host_address (&r6->network, &r6->network, r6->plen); + r6->rt_source = nmp_utils_ip_config_source_round_trip_rtprot (r6->rt_source), + r6->metric = nm_utils_ip6_route_metric_normalize (r6->metric); + nm_utils_ip6_address_clear_host_address (&r6->src, &r6->src, r6->src_plen); + break; + default: + nm_assert_not_reached (); + break; + } +} + static gboolean _ip_route_add (NMPlatform *self, NMPNlmFlags flags, diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index ce5b56a6a9..315fcd5c5e 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -1117,6 +1117,9 @@ gboolean nm_platform_ip_address_flush (NMPlatform *self, int addr_family, int ifindex); +void nm_platform_ip_route_normalize (int addr_family, + NMPlatformIPRoute *route); + gboolean nm_platform_ip_route_add (NMPlatform *self, NMPNlmFlags flags, const NMPObject *route);