From 482b7ecf7f7d23c9eaf38ff2c87bd815dbbd1d12 Mon Sep 17 00:00:00 2001
From: Jiri Slaby <jslaby@suse.cz>
Date: Apr 20 2023 11:00:35 +0000
Subject: rtnetlink: Restore RTM_NEW/DELLINK notification behavior

(bsc#1012628).

---

diff --git a/patches.kernel.org/6.2.12-066-rtnetlink-Restore-RTM_NEW-DELLINK-notification.patch b/patches.kernel.org/6.2.12-066-rtnetlink-Restore-RTM_NEW-DELLINK-notification.patch
new file mode 100644
index 0000000..b629f88
--- /dev/null
+++ b/patches.kernel.org/6.2.12-066-rtnetlink-Restore-RTM_NEW-DELLINK-notification.patch
@@ -0,0 +1,112 @@
+From: Martin Willi <martin@strongswan.org>
+Date: Tue, 11 Apr 2023 09:43:19 +0200
+Subject: [PATCH] rtnetlink: Restore RTM_NEW/DELLINK notification behavior
+References: bsc#1012628
+Patch-mainline: 6.2.12
+Git-commit: 59d3efd27c11c59b32291e5ebc307bed2edb65ee
+
+[ Upstream commit 59d3efd27c11c59b32291e5ebc307bed2edb65ee ]
+
+The commits referenced below allows userspace to use the NLM_F_ECHO flag
+for RTM_NEW/DELLINK operations to receive unicast notifications for the
+affected link. Prior to these changes, applications may have relied on
+multicast notifications to learn the same information without specifying
+the NLM_F_ECHO flag.
+
+For such applications, the mentioned commits changed the behavior for
+requests not using NLM_F_ECHO. Multicast notifications are still received,
+but now use the portid of the requester and the sequence number of the
+request instead of zero values used previously. For the application, this
+message may be unexpected and likely handled as a response to the
+NLM_F_ACKed request, especially if it uses the same socket to handle
+requests and notifications.
+
+To fix existing applications relying on the old notification behavior,
+set the portid and sequence number in the notification only if the
+request included the NLM_F_ECHO flag. This restores the old behavior
+for applications not using it, but allows unicasted notifications for
+others.
+
+Fixes: f3a63cce1b4f ("rtnetlink: Honour NLM_F_ECHO flag in rtnl_delete_link")
+Fixes: d88e136cab37 ("rtnetlink: Honour NLM_F_ECHO flag in rtnl_newlink_create")
+Signed-off-by: Martin Willi <martin@strongswan.org>
+Acked-by: Guillaume Nault <gnault@redhat.com>
+Acked-by: Hangbin Liu <liuhangbin@gmail.com>
+Link: https://lore.kernel.org/r/20230411074319.24133-1-martin@strongswan.org
+Signed-off-by: Jakub Kicinski <kuba@kernel.org>
+Signed-off-by: Sasha Levin <sashal@kernel.org>
+Signed-off-by: Jiri Slaby <jslaby@suse.cz>
+---
+ include/linux/rtnetlink.h |  3 ++-
+ net/core/dev.c            |  2 +-
+ net/core/rtnetlink.c      | 11 +++++++++--
+ 3 files changed, 12 insertions(+), 4 deletions(-)
+
+diff --git a/include/linux/rtnetlink.h b/include/linux/rtnetlink.h
+index 92ad7554..b6e6378d 100644
+--- a/include/linux/rtnetlink.h
++++ b/include/linux/rtnetlink.h
+@@ -25,7 +25,8 @@ void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
+ struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
+ 				       unsigned change, u32 event,
+ 				       gfp_t flags, int *new_nsid,
+-				       int new_ifindex, u32 portid, u32 seq);
++				       int new_ifindex, u32 portid,
++				       const struct nlmsghdr *nlh);
+ void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev,
+ 		       gfp_t flags, u32 portid, const struct nlmsghdr *nlh);
+ 
+diff --git a/net/core/dev.c b/net/core/dev.c
+index 12781527..404125e7 100644
+--- a/net/core/dev.c
++++ b/net/core/dev.c
+@@ -10837,7 +10837,7 @@ void unregister_netdevice_many_notify(struct list_head *head,
+ 		    dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
+ 			skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
+ 						     GFP_KERNEL, NULL, 0,
+-						     portid, nlmsg_seq(nlh));
++						     portid, nlh);
+ 
+ 		/*
+ 		 *	Flush the unicast and multicast chains
+diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
+index 64289bc9..f5114b23 100644
+--- a/net/core/rtnetlink.c
++++ b/net/core/rtnetlink.c
+@@ -3939,16 +3939,23 @@ static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
+ struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
+ 				       unsigned int change,
+ 				       u32 event, gfp_t flags, int *new_nsid,
+-				       int new_ifindex, u32 portid, u32 seq)
++				       int new_ifindex, u32 portid,
++				       const struct nlmsghdr *nlh)
+ {
+ 	struct net *net = dev_net(dev);
+ 	struct sk_buff *skb;
+ 	int err = -ENOBUFS;
++	u32 seq = 0;
+ 
+ 	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
+ 	if (skb == NULL)
+ 		goto errout;
+ 
++	if (nlmsg_report(nlh))
++		seq = nlmsg_seq(nlh);
++	else
++		portid = 0;
++
+ 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
+ 			       type, portid, seq, change, 0, 0, event,
+ 			       new_nsid, new_ifindex, -1, flags);
+@@ -3984,7 +3991,7 @@ static void rtmsg_ifinfo_event(int type, struct net_device *dev,
+ 		return;
+ 
+ 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
+-				     new_ifindex, portid, nlmsg_seq(nlh));
++				     new_ifindex, portid, nlh);
+ 	if (skb)
+ 		rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
+ }
+-- 
+2.35.3
+
diff --git a/series.conf b/series.conf
index da97683..596bd1d 100644
--- a/series.conf
+++ b/series.conf
@@ -2293,6 +2293,7 @@
 	patches.kernel.org/6.2.12-063-smc-Fix-use-after-free-in-tcp_write_timer_hand.patch
 	patches.kernel.org/6.2.12-064-net-wwan-iosm-Fix-error-handling-path-in-ipc_p.patch
 	patches.kernel.org/6.2.12-065-cgroup-freezer-hold-cpu_hotplug_lock-before-fr.patch
+	patches.kernel.org/6.2.12-066-rtnetlink-Restore-RTM_NEW-DELLINK-notification.patch
 
 	########################################################
 	# Build fixes that apply to the vanilla kernel too.