diff mbox series

[net-next,v2,4/4] net/sched: cls_route: make netlink errors meaningful

Message ID 20230825155148.659895-5-pctammela@mojatatu.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series selftests/tc-testing: add tests covering classid | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1330 this patch: 1330
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 1353 this patch: 1353
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1353 this patch: 1353
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 62 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Pedro Tammela Aug. 25, 2023, 3:51 p.m. UTC
Use netlink extended ack and parsing policies to return more meaningful
errors instead of the relying solely on errnos.

Signed-off-by: Pedro Tammela <pctammela@mojatatu.com>
---
 net/sched/cls_route.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

Comments

Jakub Kicinski Aug. 28, 2023, 7:52 p.m. UTC | #1
On Fri, 25 Aug 2023 12:51:48 -0300 Pedro Tammela wrote:
> Use netlink extended ack and parsing policies to return more meaningful
> errors instead of the relying solely on errnos.

Hm, I don't see the changes to the policy, or anything meaningful 
in the existing one.

> diff --git a/net/sched/cls_route.c b/net/sched/cls_route.c
> index 1e20bbd687f1..b34cf02c6c51 100644
> --- a/net/sched/cls_route.c
> +++ b/net/sched/cls_route.c
> @@ -400,30 +400,32 @@ static int route4_set_parms(struct net *net, struct tcf_proto *tp,
>  		if (new && handle & 0x8000)
>  			return -EINVAL;
>  		to = nla_get_u32(tb[TCA_ROUTE4_TO]);
> -		if (to > 0xFF)
> -			return -EINVAL;
>  		nhandle = to;
>  	}
>  
> +	if (tb[TCA_ROUTE4_FROM] && tb[TCA_ROUTE4_IIF]) {
> +		NL_SET_ERR_MSG(extack,
> +			       "'from' and 'fromif' are mutually exclusive");

Let's point at one of them? NL_SET_ERR_MSG_ATTR() ?

> +		return -EINVAL;
> +	}
> +
>  	if (tb[TCA_ROUTE4_FROM]) {
> -		if (tb[TCA_ROUTE4_IIF])
> -			return -EINVAL;
>  		id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
> -		if (id > 0xFF)
> -			return -EINVAL;
>  		nhandle |= id << 16;
>  	} else if (tb[TCA_ROUTE4_IIF]) {
>  		id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
> -		if (id > 0x7FFF)
> -			return -EINVAL;
>  		nhandle |= (id | 0x8000) << 16;
>  	} else
>  		nhandle |= 0xFFFF << 16;
>  
>  	if (handle && new) {
>  		nhandle |= handle & 0x7F00;
> -		if (nhandle != handle)
> +		if (nhandle != handle) {
> +			NL_SET_ERR_MSG_FMT(extack,
> +					   "Unexpected handle %x (expected %x)",
> +					   handle, nhandle);

How about: Handle mismatch constructed: %x (expected: %x)?

>  			return -EINVAL;
> +		}
>  	}
>  
>  	if (!nhandle) {
> @@ -478,7 +480,6 @@ static int route4_change(struct net *net, struct sk_buff *in_skb,
>  	struct route4_filter __rcu **fp;
>  	struct route4_filter *fold, *f1, *pfp, *f = NULL;
>  	struct route4_bucket *b;
> -	struct nlattr *opt = tca[TCA_OPTIONS];
>  	struct nlattr *tb[TCA_ROUTE4_MAX + 1];
>  	unsigned int h, th;
>  	int err;
> @@ -489,10 +490,12 @@ static int route4_change(struct net *net, struct sk_buff *in_skb,
>  		return -EINVAL;
>  	}
>  
> -	if (opt == NULL)
> +	if (NL_REQ_ATTR_CHECK(extack, NULL, tca, TCA_OPTIONS)) {
> +		NL_SET_ERR_MSG_MOD(extack, "missing options");
>  		return -EINVAL;
> +	}
>  
> -	err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, opt,
> +	err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, tca[TCA_OPTIONS],
>  					  route4_policy, NULL);
>  	if (err < 0)
>  		return err;
diff mbox series

Patch

diff --git a/net/sched/cls_route.c b/net/sched/cls_route.c
index 1e20bbd687f1..b34cf02c6c51 100644
--- a/net/sched/cls_route.c
+++ b/net/sched/cls_route.c
@@ -400,30 +400,32 @@  static int route4_set_parms(struct net *net, struct tcf_proto *tp,
 		if (new && handle & 0x8000)
 			return -EINVAL;
 		to = nla_get_u32(tb[TCA_ROUTE4_TO]);
-		if (to > 0xFF)
-			return -EINVAL;
 		nhandle = to;
 	}
 
+	if (tb[TCA_ROUTE4_FROM] && tb[TCA_ROUTE4_IIF]) {
+		NL_SET_ERR_MSG(extack,
+			       "'from' and 'fromif' are mutually exclusive");
+		return -EINVAL;
+	}
+
 	if (tb[TCA_ROUTE4_FROM]) {
-		if (tb[TCA_ROUTE4_IIF])
-			return -EINVAL;
 		id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
-		if (id > 0xFF)
-			return -EINVAL;
 		nhandle |= id << 16;
 	} else if (tb[TCA_ROUTE4_IIF]) {
 		id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
-		if (id > 0x7FFF)
-			return -EINVAL;
 		nhandle |= (id | 0x8000) << 16;
 	} else
 		nhandle |= 0xFFFF << 16;
 
 	if (handle && new) {
 		nhandle |= handle & 0x7F00;
-		if (nhandle != handle)
+		if (nhandle != handle) {
+			NL_SET_ERR_MSG_FMT(extack,
+					   "Unexpected handle %x (expected %x)",
+					   handle, nhandle);
 			return -EINVAL;
+		}
 	}
 
 	if (!nhandle) {
@@ -478,7 +480,6 @@  static int route4_change(struct net *net, struct sk_buff *in_skb,
 	struct route4_filter __rcu **fp;
 	struct route4_filter *fold, *f1, *pfp, *f = NULL;
 	struct route4_bucket *b;
-	struct nlattr *opt = tca[TCA_OPTIONS];
 	struct nlattr *tb[TCA_ROUTE4_MAX + 1];
 	unsigned int h, th;
 	int err;
@@ -489,10 +490,12 @@  static int route4_change(struct net *net, struct sk_buff *in_skb,
 		return -EINVAL;
 	}
 
-	if (opt == NULL)
+	if (NL_REQ_ATTR_CHECK(extack, NULL, tca, TCA_OPTIONS)) {
+		NL_SET_ERR_MSG_MOD(extack, "missing options");
 		return -EINVAL;
+	}
 
-	err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, opt,
+	err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, tca[TCA_OPTIONS],
 					  route4_policy, NULL);
 	if (err < 0)
 		return err;