diff mbox series

[v4,net-next,3/9] net/sched: mqprio: add extack to mqprio_parse_nlattr()

Message ID 20230403103440.2895683-4-vladimir.oltean@nxp.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series Add tc-mqprio and tc-taprio support for preemptible traffic classes | 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: 19 this patch: 19
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: 19 this patch: 19
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 71 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Vladimir Oltean April 3, 2023, 10:34 a.m. UTC
Netlink attribute parsing in mqprio is a minesweeper game, with many
options having the possibility of being passed incorrectly and the user
being none the wiser.

Try to make errors less sour by giving user space some information
regarding what went wrong.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Ferenc Fejes <fejes@inf.elte.hu>
Reviewed-by: Simon Horman <simon.horman@corigine.com>
---
v1->v4: none

 net/sched/sch_mqprio.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

Comments

Jamal Hadi Salim April 7, 2023, 4:06 p.m. UTC | #1
On Mon, Apr 3, 2023 at 6:35 AM Vladimir Oltean <vladimir.oltean@nxp.com> wrote:
>
> Netlink attribute parsing in mqprio is a minesweeper game, with many
> options having the possibility of being passed incorrectly and the user
> being none the wiser.
>
> Try to make errors less sour by giving user space some information
> regarding what went wrong.
>
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
> Reviewed-by: Ferenc Fejes <fejes@inf.elte.hu>
> Reviewed-by: Simon Horman <simon.horman@corigine.com>

Acked-by: Jamal Hadi Salim <jhs@mojatatu.com>

cheers,
jamal

> ---
> v1->v4: none
>
>  net/sched/sch_mqprio.c | 30 +++++++++++++++++++++++-------
>  1 file changed, 23 insertions(+), 7 deletions(-)
>
> diff --git a/net/sched/sch_mqprio.c b/net/sched/sch_mqprio.c
> index 94093971da5e..5a9261c38b95 100644
> --- a/net/sched/sch_mqprio.c
> +++ b/net/sched/sch_mqprio.c
> @@ -150,7 +150,8 @@ static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
>   * TCA_OPTIONS, which are appended right after struct tc_mqprio_qopt.
>   */
>  static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
> -                              struct nlattr *opt)
> +                              struct nlattr *opt,
> +                              struct netlink_ext_ack *extack)
>  {
>         struct nlattr *nlattr_opt = nla_data(opt) + NLA_ALIGN(sizeof(*qopt));
>         int nlattr_opt_len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
> @@ -167,8 +168,11 @@ static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
>                         return err;
>         }
>
> -       if (!qopt->hw)
> +       if (!qopt->hw) {
> +               NL_SET_ERR_MSG(extack,
> +                              "mqprio TCA_OPTIONS can only contain netlink attributes in hardware mode");
>                 return -EINVAL;
> +       }
>
>         if (tb[TCA_MQPRIO_MODE]) {
>                 priv->flags |= TC_MQPRIO_F_MODE;
> @@ -181,13 +185,19 @@ static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
>         }
>
>         if (tb[TCA_MQPRIO_MIN_RATE64]) {
> -               if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
> +               if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
> +                       NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MIN_RATE64],
> +                                           "min_rate accepted only when shaper is in bw_rlimit mode");
>                         return -EINVAL;
> +               }
>                 i = 0;
>                 nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
>                                     rem) {
> -                       if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
> +                       if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) {
> +                               NL_SET_ERR_MSG_ATTR(extack, attr,
> +                                                   "Attribute type expected to be TCA_MQPRIO_MIN_RATE64");
>                                 return -EINVAL;
> +                       }
>                         if (i >= qopt->num_tc)
>                                 break;
>                         priv->min_rate[i] = *(u64 *)nla_data(attr);
> @@ -197,13 +207,19 @@ static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
>         }
>
>         if (tb[TCA_MQPRIO_MAX_RATE64]) {
> -               if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
> +               if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
> +                       NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MAX_RATE64],
> +                                           "max_rate accepted only when shaper is in bw_rlimit mode");
>                         return -EINVAL;
> +               }
>                 i = 0;
>                 nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
>                                     rem) {
> -                       if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
> +                       if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) {
> +                               NL_SET_ERR_MSG_ATTR(extack, attr,
> +                                                   "Attribute type expected to be TCA_MQPRIO_MAX_RATE64");
>                                 return -EINVAL;
> +                       }
>                         if (i >= qopt->num_tc)
>                                 break;
>                         priv->max_rate[i] = *(u64 *)nla_data(attr);
> @@ -252,7 +268,7 @@ static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
>
>         len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
>         if (len > 0) {
> -               err = mqprio_parse_nlattr(sch, qopt, opt);
> +               err = mqprio_parse_nlattr(sch, qopt, opt, extack);
>                 if (err)
>                         return err;
>         }
> --
> 2.34.1
>
diff mbox series

Patch

diff --git a/net/sched/sch_mqprio.c b/net/sched/sch_mqprio.c
index 94093971da5e..5a9261c38b95 100644
--- a/net/sched/sch_mqprio.c
+++ b/net/sched/sch_mqprio.c
@@ -150,7 +150,8 @@  static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
  * TCA_OPTIONS, which are appended right after struct tc_mqprio_qopt.
  */
 static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
-			       struct nlattr *opt)
+			       struct nlattr *opt,
+			       struct netlink_ext_ack *extack)
 {
 	struct nlattr *nlattr_opt = nla_data(opt) + NLA_ALIGN(sizeof(*qopt));
 	int nlattr_opt_len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
@@ -167,8 +168,11 @@  static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
 			return err;
 	}
 
-	if (!qopt->hw)
+	if (!qopt->hw) {
+		NL_SET_ERR_MSG(extack,
+			       "mqprio TCA_OPTIONS can only contain netlink attributes in hardware mode");
 		return -EINVAL;
+	}
 
 	if (tb[TCA_MQPRIO_MODE]) {
 		priv->flags |= TC_MQPRIO_F_MODE;
@@ -181,13 +185,19 @@  static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
 	}
 
 	if (tb[TCA_MQPRIO_MIN_RATE64]) {
-		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
+		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
+			NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MIN_RATE64],
+					    "min_rate accepted only when shaper is in bw_rlimit mode");
 			return -EINVAL;
+		}
 		i = 0;
 		nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
 				    rem) {
-			if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
+			if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) {
+				NL_SET_ERR_MSG_ATTR(extack, attr,
+						    "Attribute type expected to be TCA_MQPRIO_MIN_RATE64");
 				return -EINVAL;
+			}
 			if (i >= qopt->num_tc)
 				break;
 			priv->min_rate[i] = *(u64 *)nla_data(attr);
@@ -197,13 +207,19 @@  static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
 	}
 
 	if (tb[TCA_MQPRIO_MAX_RATE64]) {
-		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
+		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
+			NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MAX_RATE64],
+					    "max_rate accepted only when shaper is in bw_rlimit mode");
 			return -EINVAL;
+		}
 		i = 0;
 		nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
 				    rem) {
-			if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
+			if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) {
+				NL_SET_ERR_MSG_ATTR(extack, attr,
+						    "Attribute type expected to be TCA_MQPRIO_MAX_RATE64");
 				return -EINVAL;
+			}
 			if (i >= qopt->num_tc)
 				break;
 			priv->max_rate[i] = *(u64 *)nla_data(attr);
@@ -252,7 +268,7 @@  static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
 
 	len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
 	if (len > 0) {
-		err = mqprio_parse_nlattr(sch, qopt, opt);
+		err = mqprio_parse_nlattr(sch, qopt, opt, extack);
 		if (err)
 			return err;
 	}