diff mbox series

[net-next,v3,1/4] net/sched: act_pedit: use extack in 'ex' parsing errors

Message ID 20230317195135.1142050-2-pctammela@mojatatu.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net/sched: act_pedit: minor improvements | 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: 18 this patch: 18
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
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: 18 this patch: 18
netdev/checkpatch warning WARNING: line length of 100 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Pedro Tammela March 17, 2023, 7:51 p.m. UTC
We have extack available when parsing 'ex' keys, so pass it to
tcf_pedit_keys_ex_parse and add more detailed error messages.
While at it, remove redundant code from the 'err_out' label code path.

Reviewed-by: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Pedro Tammela <pctammela@mojatatu.com>
---
 net/sched/act_pedit.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

Comments

Simon Horman March 17, 2023, 8:24 p.m. UTC | #1
Hi Pedro,

On Fri, Mar 17, 2023 at 04:51:32PM -0300, Pedro Tammela wrote:
> We have extack available when parsing 'ex' keys, so pass it to
> tcf_pedit_keys_ex_parse and add more detailed error messages.

This part looks good.

> While at it, remove redundant code from the 'err_out' label code path.

But I think it would be better to do one thing at a time.

> 
> Reviewed-by: Jamal Hadi Salim <jhs@mojatatu.com>
> Signed-off-by: Pedro Tammela <pctammela@mojatatu.com>
> ---
>  net/sched/act_pedit.c | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/net/sched/act_pedit.c b/net/sched/act_pedit.c
> index 4559a1507ea5..cd3cbe397e87 100644
> --- a/net/sched/act_pedit.c
> +++ b/net/sched/act_pedit.c
> @@ -35,7 +35,7 @@ static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
>  };
>  
>  static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
> -							u8 n)
> +							u8 n, struct netlink_ext_ack *extack)
>  {
>  	struct tcf_pedit_key_ex *keys_ex;
>  	struct tcf_pedit_key_ex *k;
> @@ -56,25 +56,25 @@ static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
>  		struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
>  
>  		if (!n) {
> -			err = -EINVAL;
> +			NL_SET_ERR_MSG_MOD(extack, "Can't parse more extended keys than requested");
>  			goto err_out;
>  		}
> +
>  		n--;
>  
>  		if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
> -			err = -EINVAL;
> +			NL_SET_ERR_MSG_MOD(extack, "Unknown attribute, expected extended key");
>  			goto err_out;
>  		}

perhaps I'm missing something terribly obvious but what I see is that
this code sits in a loop and the initial value of err is -EINVAL.
`
>  
> -		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
> -						  ka, pedit_key_ex_policy,
> -						  NULL);
> +		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX, ka,
> +						  pedit_key_ex_policy, extack);
>  		if (err)
>  			goto err_out;

If nla_parse_nested_deprecated() succeeds then, here, err == 0

>  
>  		if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
>  		    !tb[TCA_PEDIT_KEY_EX_CMD]) {
> -			err = -EINVAL;
> +			NL_SET_ERR_MSG_MOD(extack, "Extended Pedit missing required attributes");
>  			goto err_out;

If, f.e.,  this fails the code will now branch to err_out with err == 0.
Which will return ERR_PTR(err);

>  		}
>  
> @@ -83,7 +83,7 @@ static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
>  
>  		if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
>  		    k->cmd > TCA_PEDIT_CMD_MAX) {
> -			err = -EINVAL;
> +			NL_SET_ERR_MSG_MOD(extack, "Extended Pedit key is malformed");
>  			goto err_out;
>  		}
>  
> @@ -91,7 +91,7 @@ static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
>  	}
>  
>  	if (n) {
> -		err = -EINVAL;
> +		NL_SET_ERR_MSG_MOD(extack, "Not enough extended keys to parse");
>  		goto err_out;
>  	}
>  
> @@ -222,7 +222,7 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
>  	}
>  
>  	nparms->tcfp_keys_ex =
> -		tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
> +		tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys, extack);
>  	if (IS_ERR(nparms->tcfp_keys_ex)) {
>  		ret = PTR_ERR(nparms->tcfp_keys_ex);
>  		goto out_free;
> -- 
> 2.34.1
>
Pedro Tammela March 17, 2023, 8:29 p.m. UTC | #2
On 17/03/2023 17:24, Simon Horman wrote:
> Hi Pedro,
> 
> On Fri, Mar 17, 2023 at 04:51:32PM -0300, Pedro Tammela wrote:
>> We have extack available when parsing 'ex' keys, so pass it to
>> tcf_pedit_keys_ex_parse and add more detailed error messages.
> 
> This part looks good.
> 
>> While at it, remove redundant code from the 'err_out' label code path.
> 
> But I think it would be better to do one thing at a time.
> 
>>
>> Reviewed-by: Jamal Hadi Salim <jhs@mojatatu.com>
>> Signed-off-by: Pedro Tammela <pctammela@mojatatu.com>
>> ---
>>   net/sched/act_pedit.c | 20 ++++++++++----------
>>   1 file changed, 10 insertions(+), 10 deletions(-)
>>
>> diff --git a/net/sched/act_pedit.c b/net/sched/act_pedit.c
>> index 4559a1507ea5..cd3cbe397e87 100644
>> --- a/net/sched/act_pedit.c
>> +++ b/net/sched/act_pedit.c
>> @@ -35,7 +35,7 @@ static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
>>   };
>>   
>>   static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
>> -							u8 n)
>> +							u8 n, struct netlink_ext_ack *extack)
>>   {
>>   	struct tcf_pedit_key_ex *keys_ex;
>>   	struct tcf_pedit_key_ex *k;
>> @@ -56,25 +56,25 @@ static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
>>   		struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
>>   
>>   		if (!n) {
>> -			err = -EINVAL;
>> +			NL_SET_ERR_MSG_MOD(extack, "Can't parse more extended keys than requested");
>>   			goto err_out;
>>   		}
>> +
>>   		n--;
>>   
>>   		if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
>> -			err = -EINVAL;
>> +			NL_SET_ERR_MSG_MOD(extack, "Unknown attribute, expected extended key");
>>   			goto err_out;
>>   		}
> 
> perhaps I'm missing something terribly obvious but what I see is that
> this code sits in a loop and the initial value of err is -EINVAL.
> `
>>   
>> -		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
>> -						  ka, pedit_key_ex_policy,
>> -						  NULL);
>> +		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX, ka,
>> +						  pedit_key_ex_policy, extack);
>>   		if (err)
>>   			goto err_out;
> 
> If nla_parse_nested_deprecated() succeeds then, here, err == 0
> 
>>   
>>   		if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
>>   		    !tb[TCA_PEDIT_KEY_EX_CMD]) {
>> -			err = -EINVAL;
>> +			NL_SET_ERR_MSG_MOD(extack, "Extended Pedit missing required attributes");
>>   			goto err_out;
> 
> If, f.e.,  this fails the code will now branch to err_out with err == 0.
> Which will return ERR_PTR(err);
> 

Correct, I sent this change too fast and should have been more thorough.
I will try to be more careful in the future.
Thanks again!
diff mbox series

Patch

diff --git a/net/sched/act_pedit.c b/net/sched/act_pedit.c
index 4559a1507ea5..cd3cbe397e87 100644
--- a/net/sched/act_pedit.c
+++ b/net/sched/act_pedit.c
@@ -35,7 +35,7 @@  static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
 };
 
 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
-							u8 n)
+							u8 n, struct netlink_ext_ack *extack)
 {
 	struct tcf_pedit_key_ex *keys_ex;
 	struct tcf_pedit_key_ex *k;
@@ -56,25 +56,25 @@  static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
 		struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
 
 		if (!n) {
-			err = -EINVAL;
+			NL_SET_ERR_MSG_MOD(extack, "Can't parse more extended keys than requested");
 			goto err_out;
 		}
+
 		n--;
 
 		if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
-			err = -EINVAL;
+			NL_SET_ERR_MSG_MOD(extack, "Unknown attribute, expected extended key");
 			goto err_out;
 		}
 
-		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
-						  ka, pedit_key_ex_policy,
-						  NULL);
+		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX, ka,
+						  pedit_key_ex_policy, extack);
 		if (err)
 			goto err_out;
 
 		if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
 		    !tb[TCA_PEDIT_KEY_EX_CMD]) {
-			err = -EINVAL;
+			NL_SET_ERR_MSG_MOD(extack, "Extended Pedit missing required attributes");
 			goto err_out;
 		}
 
@@ -83,7 +83,7 @@  static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
 
 		if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
 		    k->cmd > TCA_PEDIT_CMD_MAX) {
-			err = -EINVAL;
+			NL_SET_ERR_MSG_MOD(extack, "Extended Pedit key is malformed");
 			goto err_out;
 		}
 
@@ -91,7 +91,7 @@  static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
 	}
 
 	if (n) {
-		err = -EINVAL;
+		NL_SET_ERR_MSG_MOD(extack, "Not enough extended keys to parse");
 		goto err_out;
 	}
 
@@ -222,7 +222,7 @@  static int tcf_pedit_init(struct net *net, struct nlattr *nla,
 	}
 
 	nparms->tcfp_keys_ex =
-		tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
+		tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys, extack);
 	if (IS_ERR(nparms->tcfp_keys_ex)) {
 		ret = PTR_ERR(nparms->tcfp_keys_ex);
 		goto out_free;