From a68e80513abb73cc76b94b5d70d0813344fa7cd7 Mon Sep 17 00:00:00 2001
From: Hans Dedecker <dedeckeh@gmail.com>
Date: Sat, 9 Oct 2021 21:14:59 +0200
Subject: [PATCH] system-linux: fix deletion of ip tunnels (FS#4058)

The deletion of IP tunnels via the ioctl interface is broken; instead of
fixing the ioctl interface switch to the netlink based interface to delete
IP tunnel devices as this simplifies and unifies the code

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
(cherry picked from commit 8f82742ca4f47f459284f3a07323d04da72ea5f6)
---
 system-dummy.c |  4 ++--
 system-linux.c | 59 +++++++++++++++-----------------------------------
 system.h       |  4 ++--
 tunnel.c       |  4 ++--
 4 files changed, 23 insertions(+), 48 deletions(-)

diff --git a/system-dummy.c b/system-dummy.c
index b6b0050..40b0750 100644
--- a/system-dummy.c
+++ b/system-dummy.c
@@ -321,12 +321,12 @@ time_t system_get_rtime(void)
 	return 0;
 }
 
-int system_del_ip_tunnel(const char *name, struct blob_attr *attr)
+int system_del_ip_tunnel(const struct device *dev)
 {
 	return 0;
 }
 
-int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
+int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
 {
 	return 0;
 }
diff --git a/system-linux.c b/system-linux.c
index 5ea9558..5a3d271 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -89,7 +89,6 @@ static int cb_rtnl_event(struct nl_msg *msg, void *arg);
 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
 static int system_add_proto_tunnel(const char *name, const uint8_t proto,
 					const unsigned int link, struct blob_attr **tb);
-static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb);
 
 static char dev_buf[256];
 
@@ -3617,7 +3616,7 @@ static int system_add_sit_tunnel(const char *name, const unsigned int link, stru
 	return ret;
 
 failure:
-	__system_del_ip_tunnel(name, tb);
+	system_link_del(name);
 	return ret;
 }
 
@@ -3679,33 +3678,9 @@ static int system_add_proto_tunnel(const char *name, const uint8_t proto, const
 	return -1;
 }
 
-static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb)
+int system_del_ip_tunnel(const struct device *dev)
 {
-	struct blob_attr *cur;
-	const char *str;
-
-	if (!(cur = tb[TUNNEL_ATTR_TYPE]))
-		return -EINVAL;
-	str = blobmsg_data(cur);
-
-	if (!strcmp(str, "greip") || !strcmp(str, "gretapip") ||
-	    !strcmp(str, "greip6") || !strcmp(str, "gretapip6") ||
-	    !strcmp(str, "vtiip") || !strcmp(str, "vtiip6") ||
-	    !strcmp(str, "vxlan") || !strcmp(str, "vxlan6") ||
-	    !strcmp(str, "xfrm"))
-		return system_link_del(name);
-	else
-		return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);
-}
-
-int system_del_ip_tunnel(const char *name, struct blob_attr *attr)
-{
-	struct blob_attr *tb[__TUNNEL_ATTR_MAX];
-
-	blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
-		blob_data(attr), blob_len(attr));
-
-	return __system_del_ip_tunnel(name, tb);
+	return system_link_del(dev->ifname);
 }
 
 int system_update_ipv6_mtu(struct device *dev, int mtu)
@@ -3738,7 +3713,7 @@ out:
 	return ret;
 }
 
-int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
+int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
 {
 	struct blob_attr *tb[__TUNNEL_ATTR_MAX];
 	struct blob_attr *cur;
@@ -3747,7 +3722,7 @@ int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
 	blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
 		blob_data(attr), blob_len(attr));
 
-	__system_del_ip_tunnel(name, tb);
+	system_link_del(dev->ifname);
 
 	if (!(cur = tb[TUNNEL_ATTR_TYPE]))
 		return -EINVAL;
@@ -3771,37 +3746,37 @@ int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
 	}
 
 	if (!strcmp(str, "sit"))
-		return system_add_sit_tunnel(name, link, tb);
+		return system_add_sit_tunnel(dev->ifname, link, tb);
 #ifdef IFLA_IPTUN_MAX
 	else if (!strcmp(str, "ipip6")) {
-		return system_add_ip6_tunnel(name, link, tb);
+		return system_add_ip6_tunnel(dev->ifname, link, tb);
 	} else if (!strcmp(str, "greip")) {
-		return system_add_gre_tunnel(name, "gre", link, tb, false);
+		return system_add_gre_tunnel(dev->ifname, "gre", link, tb, false);
 	} else if (!strcmp(str, "gretapip"))  {
-		return system_add_gre_tunnel(name, "gretap", link, tb, false);
+		return system_add_gre_tunnel(dev->ifname, "gretap", link, tb, false);
 	} else if (!strcmp(str, "greip6")) {
-		return system_add_gre_tunnel(name, "ip6gre", link, tb, true);
+		return system_add_gre_tunnel(dev->ifname, "ip6gre", link, tb, true);
 	} else if (!strcmp(str, "gretapip6")) {
-		return system_add_gre_tunnel(name, "ip6gretap", link, tb, true);
+		return system_add_gre_tunnel(dev->ifname, "ip6gretap", link, tb, true);
 #ifdef IFLA_VTI_MAX
 	} else if (!strcmp(str, "vtiip")) {
-		return system_add_vti_tunnel(name, "vti", link, tb, false);
+		return system_add_vti_tunnel(dev->ifname, "vti", link, tb, false);
 	} else if (!strcmp(str, "vtiip6")) {
-		return system_add_vti_tunnel(name, "vti6", link, tb, true);
+		return system_add_vti_tunnel(dev->ifname, "vti6", link, tb, true);
 #endif
 #ifdef IFLA_XFRM_MAX
 	} else if (!strcmp(str, "xfrm")) {
-		return system_add_xfrm_tunnel(name, "xfrm", link, tb);
+		return system_add_xfrm_tunnel(dev->ifname, "xfrm", link, tb);
 #endif
 #ifdef IFLA_VXLAN_MAX
 	} else if(!strcmp(str, "vxlan")) {
-		return system_add_vxlan(name, link, tb, false);
+		return system_add_vxlan(dev->ifname, link, tb, false);
 	} else if(!strcmp(str, "vxlan6")) {
-		return system_add_vxlan(name, link, tb, true);
+		return system_add_vxlan(dev->ifname, link, tb, true);
 #endif
 #endif
 	} else if (!strcmp(str, "ipip")) {
-		return system_add_proto_tunnel(name, IPPROTO_IPIP, link, tb);
+		return system_add_proto_tunnel(dev->ifname, IPPROTO_IPIP, link, tb);
 	}
 	else
 		return -EINVAL;
diff --git a/system.h b/system.h
index d373b66..a17020c 100644
--- a/system.h
+++ b/system.h
@@ -251,8 +251,8 @@ bool system_resolve_rt_table(const char *name, unsigned int *id);
 bool system_is_default_rt_table(unsigned int id);
 bool system_resolve_rpfilter(const char *filter, unsigned int *id);
 
-int system_del_ip_tunnel(const char *name, struct blob_attr *attr);
-int system_add_ip_tunnel(const char *name, struct blob_attr *attr);
+int system_del_ip_tunnel(const struct device *dev);
+int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr);
 
 int system_add_iprule(struct iprule *rule);
 int system_del_iprule(struct iprule *rule);
diff --git a/tunnel.c b/tunnel.c
index 1383384..6d192ac 100644
--- a/tunnel.c
+++ b/tunnel.c
@@ -28,14 +28,14 @@ tunnel_set_state(struct device *dev, bool up)
 	int ret;
 
 	if (up) {
-		ret = system_add_ip_tunnel(dev->ifname, dev->config);
+		ret = system_add_ip_tunnel(dev, dev->config);
 		if (ret != 0)
 			return ret;
 	}
 
 	ret = tun->set_state(dev, up);
 	if (ret || !up)
-		system_del_ip_tunnel(dev->ifname, dev->config);
+		system_del_ip_tunnel(dev);
 
 	return ret;
 }
-- 
GitLab