From: Hangbin Liu <liuhangbin@gmail.com>
To: netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Ido Schimmel <idosch@nvidia.com>, Petr Machata <petrm@nvidia.com>,
Florent Fourcot <florent.fourcot@wifirst.fr>,
Nikolay Aleksandrov <razor@blackwall.org>,
Nicolas Dichtel <nicolas.dichtel@6wind.com>,
Guillaume Nault <gnault@redhat.com>,
Hangbin Liu <liuhangbin@gmail.com>
Subject: [PATCHv2 net-next] rtnetlink: Honour NLM_F_ECHO flag in rtnl_{new, set, del}link
Date: Mon, 26 Sep 2022 15:12:46 +0800 [thread overview]
Message-ID: <20220926071246.38805-1-liuhangbin@gmail.com> (raw)
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 <gnault@redhat.com>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
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);
+
+errout:
+ if (err < 0)
+ rtnl_set_sk_err(dev_net(dev), RTM_NEWLINK, err);
+}
+
#define DO_SETLINK_MODIFIED 0x01
/* notify flag means notify + modified. */
#define DO_SETLINK_NOTIFY 0x03
static int do_setlink(const struct sk_buff *skb,
struct net_device *dev, struct ifinfomsg *ifm,
struct netlink_ext_ack *extack,
- struct nlattr **tb, int status)
+ struct nlattr **tb, int status,
+ struct nlmsghdr *nlh)
{
const struct net_device_ops *ops = dev->netdev_ops;
char ifname[IFNAMSIZ];
@@ -3009,6 +3037,8 @@ static int do_setlink(const struct sk_buff *skb,
}
}
+ rtnl_link_notify(dev, NETLINK_CB(skb).portid, nlh);
+
errout:
if (status & DO_SETLINK_MODIFIED) {
if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
@@ -3069,7 +3099,8 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
goto errout;
}
- err = do_setlink(skb, dev, ifm, extack, tb, 0);
+ err = do_setlink(skb, dev, ifm, extack, tb, 0, nlh);
+
errout:
return err;
}
@@ -3130,10 +3161,12 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
struct net *net = sock_net(skb->sk);
+ u32 pid = NETLINK_CB(skb).portid;
struct net *tgt_net = net;
struct net_device *dev = NULL;
struct ifinfomsg *ifm;
struct nlattr *tb[IFLA_MAX+1];
+ struct sk_buff *nskb;
int err;
int netnsid = -1;
@@ -3171,7 +3204,20 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
goto out;
}
+ nskb = nlmsg_new(if_nlmsg_size(dev, 0), GFP_KERNEL);
+ if (nskb) {
+ err = rtnl_fill_ifinfo(nskb, dev, dev_net(dev), RTM_DELLINK, pid,
+ nlh->nlmsg_seq, 0, 0, 0, 0, NULL, 0, 0,
+ GFP_KERNEL);
+ if (err < 0) {
+ WARN_ON(err == -EMSGSIZE);
+ kfree_skb(nskb);
+ }
+ }
+
err = rtnl_delete_link(dev);
+ if (!err && nskb)
+ rtnl_notify(nskb, net, pid, RTM_DELLINK, nlh, GFP_KERNEL);
out:
if (netnsid >= 0)
@@ -3293,14 +3339,14 @@ static int rtnl_group_changelink(const struct sk_buff *skb,
struct net *net, int group,
struct ifinfomsg *ifm,
struct netlink_ext_ack *extack,
- struct nlattr **tb)
+ struct nlattr **tb, struct nlmsghdr *nlh)
{
struct net_device *dev, *aux;
int err;
for_each_netdev_safe(net, dev, aux) {
if (dev->group == group) {
- err = do_setlink(skb, dev, ifm, extack, tb, 0);
+ err = do_setlink(skb, dev, ifm, extack, tb, 0, nlh);
if (err < 0)
return err;
}
@@ -3312,7 +3358,8 @@ static int rtnl_group_changelink(const struct sk_buff *skb,
static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
const struct rtnl_link_ops *ops,
struct nlattr **tb, struct nlattr **data,
- struct netlink_ext_ack *extack)
+ struct netlink_ext_ack *extack,
+ struct nlmsghdr *nlh)
{
unsigned char name_assign_type = NET_NAME_USER;
struct net *net = sock_net(skb->sk);
@@ -3382,6 +3429,9 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
if (err)
goto out_unregister;
}
+
+ rtnl_link_notify(dev, NETLINK_CB(skb).portid, nlh);
+
out:
if (link_net)
put_net(link_net);
@@ -3544,7 +3594,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
status |= DO_SETLINK_NOTIFY;
}
- return do_setlink(skb, dev, ifm, extack, tb, status);
+ return do_setlink(skb, dev, ifm, extack, tb, status, nlh);
}
if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
@@ -3556,7 +3606,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
if (tb[IFLA_GROUP])
return rtnl_group_changelink(skb, net,
nla_get_u32(tb[IFLA_GROUP]),
- ifm, extack, tb);
+ ifm, extack, tb, nlh);
return -ENODEV;
}
@@ -3578,7 +3628,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EOPNOTSUPP;
}
- return rtnl_newlink_create(skb, ifm, ops, tb, data, extack);
+ return rtnl_newlink_create(skb, ifm, ops, tb, data, extack, nlh);
}
static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
--
2.37.2
next reply other threads:[~2022-09-26 7:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-26 7:12 Hangbin Liu [this message]
2022-09-26 8:37 ` [PATCHv2 net-next] rtnetlink: Honour NLM_F_ECHO flag in rtnl_{new, set, del}link Nicolas Dichtel
2022-09-26 9:09 ` Hangbin Liu
2022-09-26 9:17 ` Hangbin Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220926071246.38805-1-liuhangbin@gmail.com \
--to=liuhangbin@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=florent.fourcot@wifirst.fr \
--cc=gnault@redhat.com \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nicolas.dichtel@6wind.com \
--cc=pabeni@redhat.com \
--cc=petrm@nvidia.com \
--cc=razor@blackwall.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).