Blame SOURCES/1001-core-fix-generation-of-dependent-rh1857133.patch

063fa4
From 7fadadb8f33675d0a12c94f5c0f5c6ecef4a98cf Mon Sep 17 00:00:00 2001
063fa4
From: Antonio Cardace <acardace@redhat.com>
063fa4
Date: Tue, 14 Jul 2020 11:35:35 +0200
063fa4
Subject: [PATCH] core: fix generation of dependent local routes for VRFs
063fa4
063fa4
When using VRF devices we must pre-generate dependent local
063fa4
routes in the VRF's table otherwise they will be incorrectly added
063fa4
to the local table instead.
063fa4
063fa4
https://bugzilla.redhat.com/show_bug.cgi?id=1857133
063fa4
063fa4
Fixes: a199cd2a7d92 ('core: add dependent local routes configured by kernel')
063fa4
(cherry picked from commit d342af1925223cf8d117750c91f35f3041f05570)
063fa4
(cherry picked from commit ad64da5e85757eeb729fea377bb92ac41c8b92bd)
063fa4
---
063fa4
 src/devices/nm-device.c     |  9 ++++++++-
063fa4
 src/nm-iface-helper.c       | 11 ++++++++++-
063fa4
 src/nm-ip4-config.c         | 19 +++++++++++--------
063fa4
 src/nm-ip4-config.h         |  7 ++++---
063fa4
 src/nm-ip6-config.c         | 17 +++++++++++------
063fa4
 src/nm-ip6-config.h         |  5 +++--
063fa4
 src/vpn/nm-vpn-connection.c | 19 ++++++++++++++++---
063fa4
 7 files changed, 63 insertions(+), 24 deletions(-)
063fa4
063fa4
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
063fa4
index de09e4807..57c32cef8 100644
063fa4
--- a/src/devices/nm-device.c
063fa4
+++ b/src/devices/nm-device.c
063fa4
@@ -68,6 +68,7 @@
063fa4
 
063fa4
 #include "nm-device-generic.h"
063fa4
 #include "nm-device-vlan.h"
063fa4
+#include "nm-device-vrf.h"
063fa4
 #include "nm-device-wireguard.h"
063fa4
 
063fa4
 #include "nm-device-logging.h"
063fa4
@@ -8097,15 +8098,21 @@ ip_config_merge_and_apply (NMDevice *self,
063fa4
 	}
063fa4
 
063fa4
 	if (commit) {
063fa4
+		gboolean is_vrf;
063fa4
+
063fa4
+		is_vrf = priv->master && nm_device_get_device_type (priv->master) == NM_DEVICE_TYPE_VRF;
063fa4
+
063fa4
 		if (IS_IPv4) {
063fa4
 			nm_ip4_config_add_dependent_routes (NM_IP4_CONFIG (composite),
063fa4
 			                                    nm_device_get_route_table (self, addr_family),
063fa4
 			                                    nm_device_get_route_metric (self, addr_family),
063fa4
+			                                    is_vrf,
063fa4
 			                                    &ip4_dev_route_blacklist);
063fa4
 		} else {
063fa4
 			nm_ip6_config_add_dependent_routes (NM_IP6_CONFIG (composite),
063fa4
 			                                    nm_device_get_route_table (self, addr_family),
063fa4
-			                                    nm_device_get_route_metric (self, addr_family));
063fa4
+			                                    nm_device_get_route_metric (self, addr_family),
063fa4
+			                                    is_vrf);
063fa4
 		}
063fa4
 	}
063fa4
 
063fa4
diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c
063fa4
index 6320e722c..c7d65d2a6 100644
063fa4
--- a/src/nm-iface-helper.c
063fa4
+++ b/src/nm-iface-helper.c
063fa4
@@ -41,11 +41,13 @@
063fa4
 static struct {
063fa4
 	GMainLoop *main_loop;
063fa4
 	int ifindex;
063fa4
+	gboolean is_vrf_device;
063fa4
 
063fa4
 	guint dad_failed_id;
063fa4
 	CList dad_failed_lst_head;
063fa4
 } gl/*obal*/ = {
063fa4
 	.ifindex = -1,
063fa4
+	.is_vrf_device = FALSE,
063fa4
 };
063fa4
 
063fa4
 static struct {
063fa4
@@ -120,6 +122,7 @@ dhcp4_state_changed (NMDhcpClient *client,
063fa4
 		nm_ip4_config_add_dependent_routes (existing,
063fa4
 		                                    RT_TABLE_MAIN,
063fa4
 		                                    global_opt.priority_v4,
063fa4
+		                                    gl.is_vrf_device,
063fa4
 		                                    &ip4_dev_route_blacklist);
063fa4
 		if (!nm_ip4_config_commit (existing,
063fa4
 		                           NM_PLATFORM_GET,
063fa4
@@ -236,7 +239,8 @@ ndisc_config_changed (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_in
063fa4
 	nm_ip6_config_merge (existing, ndisc_config, NM_IP_CONFIG_MERGE_DEFAULT, 0);
063fa4
 	nm_ip6_config_add_dependent_routes (existing,
063fa4
 	                                    RT_TABLE_MAIN,
063fa4
-	                                    global_opt.priority_v6);
063fa4
+	                                    global_opt.priority_v6,
063fa4
+	                                    gl.is_vrf_device);
063fa4
 	if (!nm_ip6_config_commit (existing,
063fa4
 	                           NM_PLATFORM_GET,
063fa4
 	                           NM_IP_ROUTE_TABLE_SYNC_MODE_MAIN,
063fa4
@@ -480,6 +484,11 @@ main (int argc, char *argv[])
063fa4
 	if (pllink) {
063fa4
 		hwaddr = nmp_link_address_get_as_bytes (&pllink->l_address);
063fa4
 		bcast_hwaddr = nmp_link_address_get_as_bytes (&pllink->l_broadcast);
063fa4
+
063fa4
+		if (pllink->master > 0) {
063fa4
+			gl.is_vrf_device
063fa4
+			    = nm_platform_link_get_type (NM_PLATFORM_GET, pllink->master) == NM_LINK_TYPE_VRF;
063fa4
+		}
063fa4
 	}
063fa4
 
063fa4
 	if (global_opt.iid_str) {
063fa4
diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c
063fa4
index 490296c8d..f017a3e80 100644
063fa4
--- a/src/nm-ip4-config.c
063fa4
+++ b/src/nm-ip4-config.c
063fa4
@@ -672,9 +672,11 @@ nm_ip4_config_update_routes_metric (NMIP4Config *self, gint64 metric)
063fa4
 }
063fa4
 
063fa4
 static void
063fa4
-_add_local_route_from_addr4 (NMIP4Config *self,
063fa4
-                            const NMPlatformIP4Address *addr,
063fa4
-                            int ifindex)
063fa4
+_add_local_route_from_addr4 (NMIP4Config *               self,
063fa4
+                             const NMPlatformIP4Address *addr,
063fa4
+                             int                         ifindex,
063fa4
+                             guint32                     route_table,
063fa4
+                             gboolean                    is_vrf)
063fa4
 {
063fa4
 	nm_auto_nmpobj NMPObject *r = NULL;
063fa4
 	NMPlatformIP4Route *route;
063fa4
@@ -686,18 +688,19 @@ _add_local_route_from_addr4 (NMIP4Config *self,
063fa4
 	route->network = addr->address;
063fa4
 	route->plen = 32;
063fa4
 	route->pref_src = addr->address;
063fa4
-	route->table_coerced = nm_platform_route_table_coerce (RT_TABLE_LOCAL);
063fa4
 	route->type_coerced = nm_platform_route_type_coerce (RTN_LOCAL);
063fa4
 	route->scope_inv = nm_platform_route_scope_inv (RT_SCOPE_HOST);
063fa4
+	route->table_coerced = nm_platform_route_table_coerce (is_vrf ? route_table : RT_TABLE_LOCAL);
063fa4
 
063fa4
 	_add_route (self, r, NULL, NULL);
063fa4
 }
063fa4
 
063fa4
 void
063fa4
 nm_ip4_config_add_dependent_routes (NMIP4Config *self,
063fa4
-                                    guint32 route_table,
063fa4
-                                    guint32 route_metric,
063fa4
-                                    GPtrArray **out_ip4_dev_route_blacklist)
063fa4
+                                    guint32      route_table,
063fa4
+                                    guint32      route_metric,
063fa4
+                                    gboolean     is_vrf,
063fa4
+                                    GPtrArray ** out_ip4_dev_route_blacklist)
063fa4
 {
063fa4
 	GPtrArray *ip4_dev_route_blacklist = NULL;
063fa4
 	const NMPlatformIP4Address *my_addr;
063fa4
@@ -729,7 +732,7 @@ nm_ip4_config_add_dependent_routes (NMIP4Config *self,
063fa4
 		if (my_addr->external)
063fa4
 			continue;
063fa4
 
063fa4
-		_add_local_route_from_addr4 (self, my_addr, ifindex);
063fa4
+		_add_local_route_from_addr4 (self, my_addr, ifindex, route_table, is_vrf);
063fa4
 
063fa4
 		if (_ipv4_is_zeronet (network)) {
063fa4
 			/* Kernel doesn't add device-routes for destinations that
063fa4
diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h
063fa4
index d4694d936..dc8222fd5 100644
063fa4
--- a/src/nm-ip4-config.h
063fa4
+++ b/src/nm-ip4-config.h
063fa4
@@ -157,9 +157,10 @@ NMDedupMultiIndex *nm_ip4_config_get_multi_idx (const NMIP4Config *self);
063fa4
 NMIP4Config *nm_ip4_config_capture (NMDedupMultiIndex *multi_idx, NMPlatform *platform, int ifindex);
063fa4
 
063fa4
 void nm_ip4_config_add_dependent_routes (NMIP4Config *self,
063fa4
-                                         guint32 route_table,
063fa4
-                                         guint32 route_metric,
063fa4
-                                         GPtrArray **out_ip4_dev_route_blacklist);
063fa4
+                                         guint32      route_table,
063fa4
+                                         guint32      route_metric,
063fa4
+                                         gboolean     is_vrf,
063fa4
+                                         GPtrArray ** out_ip4_dev_route_blacklist);
063fa4
 
063fa4
 gboolean nm_ip4_config_commit (const NMIP4Config *self,
063fa4
                                NMPlatform *platform,
063fa4
diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c
063fa4
index 4911ec1d1..1589cad00 100644
063fa4
--- a/src/nm-ip6-config.c
063fa4
+++ b/src/nm-ip6-config.c
063fa4
@@ -475,27 +475,32 @@ _add_multicast_route6 (NMIP6Config *self, int ifindex)
063fa4
 }
063fa4
 
063fa4
 static void
063fa4
-_add_local_route_from_addr6 (NMIP6Config *self, const NMPlatformIP6Address *addr, int ifindex)
063fa4
+_add_local_route_from_addr6 (NMIP6Config *               self,
063fa4
+                             const NMPlatformIP6Address *addr,
063fa4
+                             int                         ifindex,
063fa4
+                             guint32                     route_table,
063fa4
+                             gboolean                    is_vrf)
063fa4
 {
063fa4
 	nm_auto_nmpobj NMPObject *r = NULL;
063fa4
-	NMPlatformIP6Route *route;
063fa4
+	NMPlatformIP6Route *      route;
063fa4
 
063fa4
 	r = nmp_object_new (NMP_OBJECT_TYPE_IP6_ROUTE, NULL);
063fa4
 	route = NMP_OBJECT_CAST_IP6_ROUTE (r);
063fa4
 	route->ifindex = ifindex;
063fa4
 	route->network = addr->address;
063fa4
 	route->plen = 128;
063fa4
-	route->table_coerced = nm_platform_route_table_coerce (RT_TABLE_LOCAL);
063fa4
 	route->type_coerced = nm_platform_route_type_coerce (RTN_LOCAL);
063fa4
 	route->metric = 0;
063fa4
+	route->table_coerced = nm_platform_route_table_coerce (is_vrf ? route_table : RT_TABLE_LOCAL);
063fa4
 
063fa4
 	_add_route (self, r, NULL, NULL);
063fa4
 }
063fa4
 
063fa4
 void
063fa4
 nm_ip6_config_add_dependent_routes (NMIP6Config *self,
063fa4
-                                    guint32 route_table,
063fa4
-                                    guint32 route_metric)
063fa4
+                                    guint32      route_table,
063fa4
+                                    guint32      route_metric,
063fa4
+                                    gboolean     is_vrf)
063fa4
 {
063fa4
 	const NMPlatformIP6Address *my_addr;
063fa4
 	const NMPlatformIP6Route *my_route;
063fa4
@@ -524,7 +529,7 @@ nm_ip6_config_add_dependent_routes (NMIP6Config *self,
063fa4
 			continue;
063fa4
 
063fa4
 		/* Pre-generate local route added by kernel */
063fa4
-		_add_local_route_from_addr6 (self, my_addr, ifindex);
063fa4
+		_add_local_route_from_addr6 (self, my_addr, ifindex, route_table, is_vrf);
063fa4
 
063fa4
 		if (NM_FLAGS_HAS (my_addr->n_ifa_flags, IFA_F_NOPREFIXROUTE))
063fa4
 			continue;
063fa4
diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h
063fa4
index 36e8518a8..b6f461b29 100644
063fa4
--- a/src/nm-ip6-config.h
063fa4
+++ b/src/nm-ip6-config.h
063fa4
@@ -93,8 +93,9 @@ NMIP6Config *nm_ip6_config_capture (struct _NMDedupMultiIndex *multi_idx, NMPlat
063fa4
                                     NMSettingIP6ConfigPrivacy use_temporary);
063fa4
 
063fa4
 void nm_ip6_config_add_dependent_routes (NMIP6Config *self,
063fa4
-                                         guint32 route_table,
063fa4
-                                         guint32 route_metric);
063fa4
+                                         guint32      route_table,
063fa4
+                                         guint32      route_metric,
063fa4
+                                         gboolean     is_vrf);
063fa4
 
063fa4
 gboolean nm_ip6_config_commit (const NMIP6Config *self,
063fa4
                                NMPlatform *platform,
063fa4
diff --git a/src/vpn/nm-vpn-connection.c b/src/vpn/nm-vpn-connection.c
063fa4
index 6d995dc48..ff6b8e00c 100644
063fa4
--- a/src/vpn/nm-vpn-connection.c
063fa4
+++ b/src/vpn/nm-vpn-connection.c
063fa4
@@ -1447,6 +1447,20 @@ get_route_table (NMVpnConnection *self,
063fa4
 	return route_table ?: (fallback_main ? RT_TABLE_MAIN : 0);
063fa4
 }
063fa4
 
063fa4
+static gboolean
063fa4
+_is_device_vrf (NMVpnConnection *self)
063fa4
+{
063fa4
+	NMDevice *parent;
063fa4
+	NMDevice *master;
063fa4
+
063fa4
+	parent = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (self));
063fa4
+	if (!parent)
063fa4
+		return FALSE;
063fa4
+
063fa4
+	master = nm_device_get_master (parent);
063fa4
+	return master && nm_device_get_link_type (master) == NM_LINK_TYPE_VRF;
063fa4
+}
063fa4
+
063fa4
 static void
063fa4
 nm_vpn_connection_ip4_config_get (NMVpnConnection *self, GVariant *dict)
063fa4
 {
063fa4
@@ -1646,6 +1660,7 @@ nm_vpn_connection_ip4_config_get (NMVpnConnection *self, GVariant *dict)
063fa4
 	nm_ip4_config_add_dependent_routes (config,
063fa4
 	                                    route_table,
063fa4
 	                                    nm_vpn_connection_get_ip4_route_metric (self),
063fa4
+	                                    _is_device_vrf (self),
063fa4
 	                                    &priv->ip4_dev_route_blacklist);
063fa4
 
063fa4
 	if (priv->ip4_config) {
063fa4
@@ -1840,9 +1855,7 @@ next:
063fa4
 		nm_ip6_config_add_route (config, &r, NULL);
063fa4
 	}
063fa4
 
063fa4
-	nm_ip6_config_add_dependent_routes (config,
063fa4
-	                                    route_table,
063fa4
-	                                    route_metric);
063fa4
+	nm_ip6_config_add_dependent_routes (config, route_table, route_metric, _is_device_vrf (self));
063fa4
 
063fa4
 	if (priv->ip6_config) {
063fa4
 		nm_ip6_config_replace (priv->ip6_config, config, NULL);
063fa4
-- 
063fa4
2.26.2
063fa4