diff mbox series

[net-next,12/12] nexthop: Extract a helper for validation of get/del RTNL requests

Message ID 69b7beb0f8ae239762f08b8385fe74640f3b3f64.1611836479.git.petrm@nvidia.com
State New
Headers show
Series nexthop: Preparations for resilient next-hop groups | expand

Commit Message

Petr Machata Jan. 28, 2021, 12:49 p.m. UTC
Validation of messages for get / del of a next hop is the same as will be
validation of messages for get of a resilient next hop group bucket. The
difference is that policy for resilient next hop group buckets is a
superset of that used for next-hop get.

It is therefore possible to reuse the code that validates the nhmsg fields,
extracts the next-hop ID, and validates that. To that end, extract from
nh_valid_get_del_req() a helper __nh_valid_get_del_req() that does just
that.

Make the nlh argument const so that the function can be called from the
dump context, which only has a const nlh. Propagate the constness to
nh_valid_get_del_req().

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
---
 net/ipv4/nexthop.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)

Comments

David Ahern Jan. 29, 2021, 3:21 a.m. UTC | #1
On 1/28/21 5:49 AM, Petr Machata wrote:
> Validation of messages for get / del of a next hop is the same as will be

> validation of messages for get of a resilient next hop group bucket. The

> difference is that policy for resilient next hop group buckets is a

> superset of that used for next-hop get.

> 

> It is therefore possible to reuse the code that validates the nhmsg fields,

> extracts the next-hop ID, and validates that. To that end, extract from

> nh_valid_get_del_req() a helper __nh_valid_get_del_req() that does just

> that.

> 

> Make the nlh argument const so that the function can be called from the

> dump context, which only has a const nlh. Propagate the constness to

> nh_valid_get_del_req().

> 

> Signed-off-by: Petr Machata <petrm@nvidia.com>

> Reviewed-by: Ido Schimmel <idosch@nvidia.com>

> ---

>  net/ipv4/nexthop.c | 43 +++++++++++++++++++++++++------------------

>  1 file changed, 25 insertions(+), 18 deletions(-)

> 


Reviewed-by: David Ahern <dsahern@kernel.org>
diff mbox series

Patch

diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index 9536cf2f6aca..f1c6cbdb9e43 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -1872,37 +1872,44 @@  static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
 	return err;
 }
 
-static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
-				struct netlink_ext_ack *extack)
+static int __nh_valid_get_del_req(const struct nlmsghdr *nlh,
+				  struct nlattr **tb, u32 *id,
+				  struct netlink_ext_ack *extack)
 {
 	struct nhmsg *nhm = nlmsg_data(nlh);
-	struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
-	int err;
-
-	err = nlmsg_parse(nlh, sizeof(*nhm), tb,
-			  ARRAY_SIZE(rtm_nh_policy_get) - 1,
-			  rtm_nh_policy_get, extack);
-	if (err < 0)
-		return err;
 
-	err = -EINVAL;
 	if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
 		NL_SET_ERR_MSG(extack, "Invalid values in header");
-		goto out;
+		return -EINVAL;
 	}
 
 	if (!tb[NHA_ID]) {
 		NL_SET_ERR_MSG(extack, "Nexthop id is missing");
-		goto out;
+		return -EINVAL;
 	}
 
 	*id = nla_get_u32(tb[NHA_ID]);
-	if (!(*id))
+	if (!(*id)) {
 		NL_SET_ERR_MSG(extack, "Invalid nexthop id");
-	else
-		err = 0;
-out:
-	return err;
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int nh_valid_get_del_req(const struct nlmsghdr *nlh, u32 *id,
+				struct netlink_ext_ack *extack)
+{
+	struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
+	int err;
+
+	err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
+			  ARRAY_SIZE(rtm_nh_policy_get) - 1,
+			  rtm_nh_policy_get, extack);
+	if (err < 0)
+		return err;
+
+	return __nh_valid_get_del_req(nlh, tb, id, extack);
 }
 
 /* rtnl */