Thread (4 messages) flat view 4 messages, 2 authors, 2022-09-26

Re: [PATCHv2 net-next] rtnetlink: Honour NLM_F_ECHO flag in rtnl_{new, set, del}link

From: Nicolas Dichtel <hidden>
Date: 2022-09-26 08:38:01

Le 26/09/2022 à 09:12, Hangbin Liu a écrit :
quoted hunk ↗ jump to hunk
Netlink messages are used for communicating between user and kernel space.
When user space configures the kernel with netlink messages, it can set the
NLM_F_ECHO flag to request the kernel to send the applied configuration back
to the caller. This allows user space to retrieve configuration information
that are filled by the kernel (either because these parameters can only be
set by the kernel or because user space let the kernel choose a default
value).

The kernel has support this feature in some places like RTM_{NEW, DEL}ADDR,
RTM_{NEW, DEL}ROUTE. This patch handles NLM_F_ECHO flag and send link info
back after rtnl_{new, set, del}link.

Suggested-by: Guillaume Nault <redacted>
Signed-off-by: Hangbin Liu <redacted>
---
v2:
1) rename rtnl_echo_link_info() to rtnl_link_notify().
2) remove IFLA_LINK_NETNSID and IFLA_EXT_MASK, which do not fit here.
3) Add NLM_F_ECHO in rtnl_dellink. But we can't re-use the rtnl_link_notify()
   helper as we need to get the link info before rtnl_delete_link().
---
 net/core/rtnetlink.c | 66 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 58 insertions(+), 8 deletions(-)
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 74864dc46a7e..0897cb6cc931 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2645,13 +2645,41 @@ static int do_set_proto_down(struct net_device *dev,
 	return 0;
 }
 
+static void rtnl_link_notify(struct net_device *dev, u32 pid,
+			     struct nlmsghdr *nlh)
+{
+	struct sk_buff *skb;
+	int err = -ENOBUFS;
+
+	skb = nlmsg_new(if_nlmsg_size(dev, 0), GFP_KERNEL);
+	if (!skb)
+		goto errout;
+
+	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), RTM_NEWLINK, pid,
+			       nlh->nlmsg_seq, 0, 0, 0, 0, NULL, 0, 0,
+			       GFP_KERNEL);
+	if (err < 0) {
+		/* -EMSGSIZE implies BUG in if_nlmsg_size */
+		WARN_ON(err == -EMSGSIZE);
+		kfree_skb(skb);
+		goto errout;
+	}
+
+	rtnl_notify(skb, dev_net(dev), pid, RTM_NEWLINK, nlh, GFP_KERNEL);
The fourth argument is the group, not the command. It should be RTNLGRP_LINK
here. But it would be better to pass 0 to avoid multicasting a new / duplicate
notification. Calling functions already multicast one.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help