core: merge IPv4 and IPv6 version of _nm_ip_config_merge_route_attributes()

This commit is contained in:
Thomas Haller
2017-11-07 19:49:39 +01:00
parent a9d1f5e543
commit 433d2f8659
3 changed files with 76 additions and 89 deletions

View File

@@ -806,44 +806,79 @@ nm_ip4_config_commit (const NMIP4Config *self,
return success; return success;
} }
static void void
merge_route_attributes (NMIPRoute *s_route, _nm_ip_config_merge_route_attributes (int addr_family,
NMPlatformIP4Route *r, NMIPRoute *s_route,
NMPlatformIPRoute *r,
guint32 route_table) guint32 route_table)
{ {
GVariant *variant; GVariant *variant;
guint32 u32; guint32 table;
in_addr_t addr; NMIPAddr addr;
NMPlatformIP4Route *r4 = (NMPlatformIP4Route *) r;
NMPlatformIP6Route *r6 = (NMPlatformIP6Route *) r;
#define GET_ATTR(name, field, variant_type, type) \ nm_assert (s_route);
variant = nm_ip_route_get_attribute (s_route, name); \ nm_assert_addr_family (addr_family);
if (variant && g_variant_is_of_type (variant, G_VARIANT_TYPE_ ## variant_type)) \ nm_assert (r);
r->field = g_variant_get_ ## type (variant);
variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_TABLE); #define GET_ATTR(name, dst, variant_type, type, dflt) \
u32 = variant && g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32) G_STMT_START { \
? g_variant_get_uint32 (variant) GVariant *_variant = nm_ip_route_get_attribute (s_route, ""name""); \
: 0; \
r->table_coerced = nm_platform_route_table_coerce (u32 ?: (route_table ?: RT_TABLE_MAIN)); if ( _variant \
&& g_variant_is_of_type (_variant, G_VARIANT_TYPE_ ## variant_type)) \
(dst) = g_variant_get_ ## type (_variant); \
else \
(dst) = (dflt); \
} G_STMT_END
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_TOS, tos, BYTE, byte); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_TABLE, table, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_WINDOW, window, UINT32, uint32); r->table_coerced = nm_platform_route_table_coerce (table ?: (route_table ?: RT_TABLE_MAIN));
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_CWND, cwnd, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITCWND, initcwnd, UINT32, uint32); if (addr_family == AF_INET)
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITRWND, initrwnd, UINT32, uint32); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_TOS, r4->tos, BYTE, byte, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_MTU, mtu, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_WINDOW, lock_window, BOOLEAN, boolean); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_WINDOW, r->window, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_CWND, lock_cwnd, BOOLEAN, boolean); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_CWND, r->cwnd, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITCWND, lock_initcwnd, BOOLEAN, boolean); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITCWND, r->initcwnd, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITRWND, lock_initrwnd, BOOLEAN, boolean); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITRWND, r->initrwnd, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_MTU, lock_mtu, BOOLEAN, boolean); GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_MTU, r->mtu, UINT32, uint32, 0);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_WINDOW, r->lock_window, BOOLEAN, boolean, FALSE);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_CWND, r->lock_cwnd, BOOLEAN, boolean, FALSE);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITCWND, r->lock_initcwnd, BOOLEAN, boolean, FALSE);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITRWND, r->lock_initrwnd, BOOLEAN, boolean, FALSE);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_MTU, r->lock_mtu, BOOLEAN, boolean, FALSE);
if ( (variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_SRC)) if ( (variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_SRC))
&& g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) { && g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) {
if (inet_pton (AF_INET, g_variant_get_string (variant, NULL), &addr) == 1) if (inet_pton (addr_family, g_variant_get_string (variant, NULL), &addr) == 1) {
r->pref_src = addr; if (addr_family == AF_INET)
r4->pref_src = addr.addr4;
else
r6->pref_src = addr.addr6;
}
} }
if ( addr_family == AF_INET6
&& (variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_FROM))
&& g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) {
gs_free char *string = NULL;
guint8 plen = 128;
char *sep;
string = g_variant_dup_string (variant, NULL);
sep = strchr (string, '/');
if (sep) {
*sep = 0;
plen = _nm_utils_ascii_str_to_int64 (sep + 1, 10, 1, 128, 255);
}
if ( plen <= 128
&& inet_pton (AF_INET6, string, &addr) == 1) {
r6->src = addr.addr6;
r6->src_plen = plen;
}
}
#undef GET_ATTR #undef GET_ATTR
} }
@@ -939,7 +974,10 @@ nm_ip4_config_merge_setting (NMIP4Config *self,
route.network = nm_utils_ip4_address_clear_host_address (route.network, route.plen); route.network = nm_utils_ip4_address_clear_host_address (route.network, route.plen);
merge_route_attributes (s_route, &route, route_table); _nm_ip_config_merge_route_attributes (AF_INET,
s_route,
NM_PLATFORM_IP_ROUTE_CAST (&route),
route_table);
_add_route (self, NULL, &route, NULL); _add_route (self, NULL, &route, NULL);
} }

View File

@@ -113,6 +113,11 @@ const NMDedupMultiEntry *_nm_ip_config_lookup_ip_route (const NMDedupMultiIndex
const NMPObject *needle, const NMPObject *needle,
NMPlatformIPRouteCmpType cmp_type); NMPlatformIPRouteCmpType cmp_type);
void _nm_ip_config_merge_route_attributes (int addr_family,
NMIPRoute *s_route,
NMPlatformIPRoute *r,
guint32 route_table);
/*****************************************************************************/ /*****************************************************************************/
#define NM_TYPE_IP4_CONFIG (nm_ip4_config_get_type ()) #define NM_TYPE_IP4_CONFIG (nm_ip4_config_get_type ())

View File

@@ -571,65 +571,6 @@ nm_ip6_config_commit (const NMIP6Config *self,
return success; return success;
} }
static void
merge_route_attributes (NMIPRoute *s_route,
NMPlatformIP6Route *r,
guint32 route_table)
{
GVariant *variant;
guint32 u32;
struct in6_addr addr;
#define GET_ATTR(name, field, variant_type, type) \
variant = nm_ip_route_get_attribute (s_route, name); \
if (variant && g_variant_is_of_type (variant, G_VARIANT_TYPE_ ## variant_type)) \
r->field = g_variant_get_ ## type (variant);
variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_TABLE);
u32 = variant && g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32)
? g_variant_get_uint32 (variant)
: 0;
r->table_coerced = nm_platform_route_table_coerce (u32 ?: (route_table ?: RT_TABLE_MAIN));
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_WINDOW, window, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_CWND, cwnd, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITCWND, initcwnd, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_INITRWND, initrwnd, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_MTU, mtu, UINT32, uint32);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_WINDOW, lock_window, BOOLEAN, boolean);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_CWND, lock_cwnd, BOOLEAN, boolean);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITCWND, lock_initcwnd, BOOLEAN, boolean);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_INITRWND, lock_initrwnd, BOOLEAN, boolean);
GET_ATTR (NM_IP_ROUTE_ATTRIBUTE_LOCK_MTU, lock_mtu, BOOLEAN, boolean);
if ( (variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_SRC))
&& g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) {
if (inet_pton (AF_INET6, g_variant_get_string (variant, NULL), &addr) == 1)
r->pref_src = addr;
}
if ( (variant = nm_ip_route_get_attribute (s_route, NM_IP_ROUTE_ATTRIBUTE_FROM))
&& g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) {
gs_free char *string = NULL;
guint8 plen = 128;
char *sep;
string = g_variant_dup_string (variant, NULL);
sep = strchr (string, '/');
if (sep) {
*sep = 0;
plen = _nm_utils_ascii_str_to_int64 (sep + 1, 10, 1, 128, 255);
}
if ( plen <= 128
&& inet_pton (AF_INET6, string, &addr) == 1) {
r->src = addr;
r->src_plen = plen;
}
}
#undef GET_ATTR
}
void void
nm_ip6_config_merge_setting (NMIP6Config *self, nm_ip6_config_merge_setting (NMIP6Config *self,
NMSettingIPConfig *setting, NMSettingIPConfig *setting,
@@ -716,7 +657,10 @@ nm_ip6_config_merge_setting (NMIP6Config *self,
nm_utils_ip6_address_clear_host_address (&route.network, &route.network, route.plen); nm_utils_ip6_address_clear_host_address (&route.network, &route.network, route.plen);
merge_route_attributes (s_route, &route, route_table); _nm_ip_config_merge_route_attributes (AF_INET,
s_route,
NM_PLATFORM_IP_ROUTE_CAST (&route),
route_table);
_add_route (self, NULL, &route, NULL); _add_route (self, NULL, &route, NULL);
} }