platform: add and use nm_platform_ip_route_normalize()

Adding a route to kernel may coerce/mangle some properties. Add a function
nm_platform_ip_route_normalize() to simulate these changes.
This commit is contained in:
Thomas Haller
2017-08-17 13:37:21 +02:00
parent 990a050aff
commit 69a50a5053
4 changed files with 44 additions and 21 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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,

View File

@@ -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);