nmcs-main: support adding additional routes

This allows a provider to only add additional routes to the applied profile

https://bugzilla.redhat.com/show_bug.cgi?id=1821787
This commit is contained in:
Antonio Cardace 2020-06-18 18:11:41 +02:00
parent 053bce438b
commit 75a84677ca
No known key found for this signature in database
GPG key ID: 6BF80ABD43E377D3
3 changed files with 79 additions and 55 deletions

View file

@ -280,16 +280,17 @@ _nmc_mangle_connection (NMDevice *device,
gboolean *out_changed)
{
NMSettingIPConfig *s_ip;
gboolean addrs_changed;
gboolean routes_changed;
gboolean rules_changed;
gsize i;
in_addr_t gateway;
gint64 rt_metric;
guint32 rt_table;
NMIPRoute *route_entry;
gboolean addrs_changed = FALSE;
gboolean rules_changed = FALSE;
gboolean routes_changed = FALSE;
gs_unref_ptrarray GPtrArray *addrs_new = NULL;
gs_unref_ptrarray GPtrArray *rules_new = NULL;
nm_auto_unref_ip_route NMIPRoute *route_new = NULL;
gs_unref_ptrarray GPtrArray *routes_new = NULL;
if (!nm_streq0 (nm_connection_get_connection_type (connection), NM_SETTING_WIRED_SETTING_NAME))
return FALSE;
@ -298,62 +299,80 @@ _nmc_mangle_connection (NMDevice *device,
if (!s_ip)
return FALSE;
addrs_new = g_ptr_array_new_full (config_data->ipv4s_len, (GDestroyNotify) nm_ip_address_unref);
for (i = 0; i < config_data->ipv4s_len; i++) {
NMIPAddress *entry;
addrs_new = g_ptr_array_new_full (config_data->ipv4s_len,
(GDestroyNotify) nm_ip_address_unref);
rules_new = g_ptr_array_new_full (config_data->ipv4s_len,
(GDestroyNotify) nm_ip_routing_rule_unref);
routes_new = g_ptr_array_new_full (config_data->iproutes_len + !!config_data->ipv4s_len,
(GDestroyNotify) nm_ip_route_unref);
entry = nm_ip_address_new_binary (AF_INET,
&config_data->ipv4s_arr[i],
config_data->cidr_prefix,
NULL);
if (entry)
g_ptr_array_add (addrs_new, entry);
if ( config_data->has_ipv4s
&& config_data->has_cidr) {
for (i = 0; i < config_data->ipv4s_len; i++) {
NMIPAddress *entry;
entry = nm_ip_address_new_binary (AF_INET,
&config_data->ipv4s_arr[i],
config_data->cidr_prefix,
NULL);
if (entry)
g_ptr_array_add (addrs_new, entry);
}
gateway = nm_utils_ip4_address_clear_host_address (config_data->cidr_addr, config_data->cidr_prefix);
((guint8 *) &gateway)[3] += 1;
rt_metric = 10;
rt_table = 30400 + config_data->iface_idx;
route_entry = nm_ip_route_new_binary (AF_INET,
&nm_ip_addr_zero,
0,
&gateway,
rt_metric,
NULL);
nm_ip_route_set_attribute (route_entry,
NM_IP_ROUTE_ATTRIBUTE_TABLE,
g_variant_new_uint32 (rt_table));
g_ptr_array_add (routes_new, route_entry);
for (i = 0; i < config_data->ipv4s_len; i++) {
NMIPRoutingRule *entry;
char sbuf[NM_UTILS_INET_ADDRSTRLEN];
entry = nm_ip_routing_rule_new (AF_INET);
nm_ip_routing_rule_set_priority (entry, rt_table);
nm_ip_routing_rule_set_from (entry,
_nm_utils_inet4_ntop (config_data->ipv4s_arr[i], sbuf),
32);
nm_ip_routing_rule_set_table (entry, rt_table);
nm_assert (nm_ip_routing_rule_validate (entry, NULL));
g_ptr_array_add (rules_new, entry);
}
}
gateway = nm_utils_ip4_address_clear_host_address (config_data->cidr_addr, config_data->cidr_prefix);
((guint8 *) &gateway)[3] += 1;
for (i = 0; i < config_data->iproutes_len; ++i)
g_ptr_array_add (routes_new, config_data->iproutes_arr[i]);
rt_metric = 10;
rt_table = 30400 + config_data->iface_idx;
route_new = nm_ip_route_new_binary (AF_INET,
&nm_ip_addr_zero,
0,
&gateway,
rt_metric,
NULL);
nm_ip_route_set_attribute (route_new,
NM_IP_ROUTE_ATTRIBUTE_TABLE,
g_variant_new_uint32 (rt_table));
rules_new = g_ptr_array_new_full (config_data->ipv4s_len, (GDestroyNotify) nm_ip_routing_rule_unref);
for (i = 0; i < config_data->ipv4s_len; i++) {
NMIPRoutingRule *entry;
char sbuf[NM_UTILS_INET_ADDRSTRLEN];
entry = nm_ip_routing_rule_new (AF_INET);
nm_ip_routing_rule_set_priority (entry, rt_table);
nm_ip_routing_rule_set_from (entry,
_nm_utils_inet4_ntop (config_data->ipv4s_arr[i], sbuf),
32);
nm_ip_routing_rule_set_table (entry, rt_table);
nm_assert (nm_ip_routing_rule_validate (entry, NULL));
g_ptr_array_add (rules_new, entry);
if (addrs_new->len) {
addrs_changed = nmcs_setting_ip_replace_ipv4_addresses (s_ip,
(NMIPAddress **) addrs_new->pdata,
addrs_new->len);
}
addrs_changed = nmcs_setting_ip_replace_ipv4_addresses (s_ip,
(NMIPAddress **) addrs_new->pdata,
addrs_new->len);
if (routes_new->len) {
routes_changed = nmcs_setting_ip_replace_ipv4_routes (s_ip,
(NMIPRoute **) routes_new->pdata,
routes_new->len);
}
routes_changed = nmcs_setting_ip_replace_ipv4_routes (s_ip,
&route_new,
1);
rules_changed = nmcs_setting_ip_replace_ipv4_rules (s_ip,
(NMIPRoutingRule **) rules_new->pdata,
rules_new->len);
if (rules_new->len) {
rules_changed = nmcs_setting_ip_replace_ipv4_rules (s_ip,
(NMIPRoutingRule **) rules_new->pdata,
rules_new->len);
}
NM_SET_OUT (out_changed, addrs_changed
|| routes_changed

View file

@ -114,6 +114,7 @@ _iface_data_free (gpointer data)
NMCSProviderGetConfigIfaceData *iface_data = data;
g_free (iface_data->ipv4s_arr);
g_free (iface_data->iproutes_arr);
nm_g_slice_free (iface_data);
}

View file

@ -18,6 +18,9 @@ typedef struct {
bool has_ipv4s:1;
bool has_cidr:1;
NMIPRoute **iproutes_arr;
gsize iproutes_len;
/* TRUE, if the configuration was requested via hwaddrs argument to
* nmcs_provider_get_config(). */
bool was_requested:1;
@ -29,8 +32,9 @@ nmcs_provider_get_config_iface_data_is_valid (const NMCSProviderGetConfigIfaceDa
{
return config_data
&& config_data->iface_idx >= 0
&& config_data->has_cidr
&& config_data->has_ipv4s;
&& ( ( config_data->has_ipv4s
&& config_data->has_cidr)
|| config_data->iproutes_len);
}
NMCSProviderGetConfigIfaceData *nmcs_provider_get_config_iface_data_new (gboolean was_requested);