ethtool: fail with error if request has unknown flags
Subsystems (3)
cover ethtool: fail with error if request has unknown flags
Jakub Kicinski pointed out that if unrecognized flags are set in netlink header request, kernel shoud fail with an error rather than silently ignore them so that we have more freedom in future flags semantics.
To help userspace with handling such errors, inform the client which flags are supported by kernel. For that purpose, we need to allow passing cookies as part of extack also in case of error (they can be only passed on success now).
Michal Kubecek (3):
netlink: allow extack cookie also for error messages
netlink: add nl_set_extack_cookie_u32()
ethtool: reject unrecognized request flags
include/linux/netlink.h | 9 +++++++++ net/ethtool/netlink.c | 16 +++++++++++---- net/netlink/af_netlink.c | 43 ++++++++++++++++------------------------ 3 files changed, 38 insertions(+), 30 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 netlink: allow extack cookie also for error messages
Commit ba0dc5f6e0ba ("netlink: allow sending extended ACK with cookie on success") introduced a cookie which can be sent to userspace as part of extended ack message in the form of NLMSGERR_ATTR_COOKIE attribute. Currently the cookie is ignored if error code is non-zero but there is no technical reason for such limitation and it can be useful to provide machine parseable information as part of an error message.
Include NLMSGERR_ATTR_COOKIE whenever the cookie has been set, regardless of error code.
--- net/netlink/af_netlink.c | 43 ++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 26 deletions(-)
net/netlink/af_netlink.c +17 -26
@@ -2392,19 +2392,14 @@ void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,if (nlk_has_extack && extack && extack->_msg) tlvlen +=nla_total_size (strlen (extack->_msg) +1 ); -if (err) { -if (!(nlk->flags &NETLINK_F_CAP_ACK )) - payload +=nlmsg_len (nlh); -else - flags |=NLM_F_CAPPED ; -if (nlk_has_extack && extack && extack->bad_attr) - tlvlen +=nla_total_size (sizeof (u32)); - }else { +if (err && !(nlk->flags &NETLINK_F_CAP_ACK )) + payload +=nlmsg_len (nlh); +else flags |=NLM_F_CAPPED ; - -if (nlk_has_extack && extack && extack->cookie_len) - tlvlen +=nla_total_size (extack->cookie_len); - } +if (err && nlk_has_extack && extack && extack->bad_attr) + tlvlen +=nla_total_size (sizeof (u32)); +if (nlk_has_extack && extack && extack->cookie_len) + tlvlen +=nla_total_size (extack->cookie_len);if (tlvlen) flags |=NLM_F_ACK_TLVS ;
@@ -2427,20 +2422,16 @@ void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,WARN_ON (nla_put_string (skb,NLMSGERR_ATTR_MSG , extack->_msg)); } -if (err) { -if (extack->bad_attr && - !WARN_ON ((u8 *)extack->bad_attr < in_skb->data || - (u8 *)extack->bad_attr >= in_skb->data + - in_skb->len)) -WARN_ON (nla_put_u32 (skb,NLMSGERR_ATTR_OFFS , - (u8 *)extack->bad_attr - - (u8 *)nlh)); - }else { -if (extack->cookie_len) -WARN_ON (nla_put (skb,NLMSGERR_ATTR_COOKIE , - extack->cookie_len, - extack->cookie)); - } +if (err && extack->bad_attr && + !WARN_ON ((u8 *)extack->bad_attr < in_skb->data || + (u8 *)extack->bad_attr >= in_skb->data + + in_skb->len)) +WARN_ON (nla_put_u32 (skb,NLMSGERR_ATTR_OFFS , + (u8 *)extack->bad_attr - + (u8 *)nlh)); +if (extack->cookie_len) +WARN_ON (nla_put (skb,NLMSGERR_ATTR_COOKIE , + extack->cookie_len, extack->cookie)); }nlmsg_end (skb, rep);
Signature · 1 line
2.25.1
PATCH 2/3 netlink: add nl_set_extack_cookie_u32()
In reply to Michal KubecekSimilar to existing nl_set_extack_cookie_u64(), add new helper nl_set_extack_cookie_u32() which sets extack cookie to a u32 value.
--- include/linux/netlink.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/netlink.h +9 -0
@@ -119,6 +119,15 @@ static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack, extack->cookie_len =sizeof (__cookie); } +static inline void nl_set_extack_cookie_u32 (struct netlink_ext_ack *extack, +u32 cookie) +{ +u32 __cookie = cookie; + +memcpy (extack->cookie, &__cookie,sizeof (__cookie)); + extack->cookie_len =sizeof (__cookie); +} +void netlink_kernel_release (struct sock *sk);int __netlink_change_ngroups (struct sock *sk,unsigned int groups);int netlink_change_ngroups (struct sock *sk,unsigned int groups);
Signature · 1 line
2.25.1
PATCH 3/3 ethtool: reject unrecognized request flags
In reply to Michal KubecekAs pointed out by Jakub Kicinski, we ethtool netlink code should respond with an error if request head has flags set which are not recognized by kernel, either as a mistake or because it expects functionality introduced in later kernel versions.
To avoid unnecessary roundtrips, use extack cookie to provide the information about supported request flags.
--- net/ethtool/netlink.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/ethtool/netlink.c +12 -4
@@ -40,6 +40,7 @@ int ethnl_parse_header(struct ethnl_req_info *req_info,struct nlattr *tb[ETHTOOL_A_HEADER_MAX +1 ];const struct nlattr *devname_attr;struct net_device *dev =NULL ; +u32 flags =0 ;int ret;if (!header) {
@@ -50,8 +51,17 @@ int ethnl_parse_header(struct ethnl_req_info *req_info, ethnl_header_policy, extack);if (ret <0 )return ret; - devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME ]; +if (tb[ETHTOOL_A_HEADER_FLAGS ]) { + flags =nla_get_u32 (tb[ETHTOOL_A_HEADER_FLAGS ]); +if (flags & ~ETHTOOL_FLAG_ALL ) { +NL_SET_ERR_MSG_ATTR (extack, tb[ETHTOOL_A_HEADER_FLAGS ], +"unrecognized request flags" ); +nl_set_extack_cookie_u32 (extack,ETHTOOL_FLAG_ALL ); +return -EOPNOTSUPP ; + } + } + devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME ];if (tb[ETHTOOL_A_HEADER_DEV_INDEX ]) {u32 ifindex =nla_get_u32 (tb[ETHTOOL_A_HEADER_DEV_INDEX ]);
@@ -90,9 +100,7 @@ int ethnl_parse_header(struct ethnl_req_info *req_info, } req_info->dev = dev; -if (tb[ETHTOOL_A_HEADER_FLAGS ]) - req_info->flags =nla_get_u32 (tb[ETHTOOL_A_HEADER_FLAGS ]); - + req_info->flags = flags;return 0 ; }
Signature · 1 line
2.25.1
Re: [PATCH net 3/3] ethtool: reject unrecognized request flags
Hello!
Quoted 1 line · Michal Kubecek
As pointed out by Jakub Kicinski, we ethtool netlink code should respond
s/we/the/?
Quoted 8 lines
with an error if request head has flags set which are not recognized by kernel, either as a mistake or because it expects functionality introduced in later kernel versions.
To avoid unnecessary roundtrips, use extack cookie to provide the information about supported request flags.
Signed-off-by Michal Kubecek <[email protected]>
[...]
MBR, Sergei
David Miller
maintainerFrom: Michal Kubecek <[email protected]>
Date: Sun, 15 Mar 2020 18:17:38 +0100 (CET)
Quoted 8 lines
Jakub Kicinski pointed out that if unrecognized flags are set in netlink header request, kernel shoud fail with an error rather than silently ignore them so that we have more freedom in future flags semantics.
To help userspace with handling such errors, inform the client which flags are supported by kernel. For that purpose, we need to allow passing cookies as part of extack also in case of error (they can be only passed on success now).
Series applied, thanks Michal.