diff mbox series

[wpan,v2,1/6] ieee802154: Use netlink policies when relevant on scan parameters

Message ID 20230214135035.1202471-2-miquel.raynal@bootlin.com (mailing list archive)
State Accepted
Headers show
Series ieee802154: Scan/Beacon fixes | expand

Commit Message

Miquel Raynal Feb. 14, 2023, 1:50 p.m. UTC
Instead of open-coding scan parameters (page, channels, duration, etc),
let's use the existing NLA_POLICY* macros. This help greatly reducing
the error handling and clarifying the overall logic.

Fixes: ed3557c947e1 ("ieee802154: Add support for user scanning requests")
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 net/ieee802154/nl802154.c | 84 +++++++++++++--------------------------
 1 file changed, 28 insertions(+), 56 deletions(-)
diff mbox series

Patch

diff --git a/net/ieee802154/nl802154.c b/net/ieee802154/nl802154.c
index 0d9becd678e3..64fa811e1f0b 100644
--- a/net/ieee802154/nl802154.c
+++ b/net/ieee802154/nl802154.c
@@ -187,8 +187,8 @@  static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
 
 	[NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
 
-	[NL802154_ATTR_PAGE] = { .type = NLA_U8, },
-	[NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
+	[NL802154_ATTR_PAGE] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_PAGE),
+	[NL802154_ATTR_CHANNEL] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_CHANNEL),
 
 	[NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
 
@@ -221,13 +221,19 @@  static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
 
 	[NL802154_ATTR_COORDINATOR] = { .type = NLA_NESTED },
 
-	[NL802154_ATTR_SCAN_TYPE] = { .type = NLA_U8 },
-	[NL802154_ATTR_SCAN_CHANNELS] = { .type = NLA_U32 },
-	[NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_U64 },
-	[NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_U8 },
-	[NL802154_ATTR_SCAN_DURATION] = { .type = NLA_U8 },
-	[NL802154_ATTR_SCAN_DONE_REASON] = { .type = NLA_U8 },
-	[NL802154_ATTR_BEACON_INTERVAL] = { .type = NLA_U8 },
+	[NL802154_ATTR_SCAN_TYPE] =
+		NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_ED, NL802154_SCAN_RIT_PASSIVE),
+	[NL802154_ATTR_SCAN_CHANNELS] =
+		NLA_POLICY_MASK(NLA_U32, GENMASK(IEEE802154_MAX_CHANNEL, 0)),
+	[NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_REJECT },
+	[NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_REJECT },
+	[NL802154_ATTR_SCAN_DURATION] =
+		NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
+	[NL802154_ATTR_SCAN_DONE_REASON] =
+		NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_DONE_REASON_FINISHED,
+				 NL802154_SCAN_DONE_REASON_ABORTED),
+	[NL802154_ATTR_BEACON_INTERVAL] =
+		NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
 
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 	[NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
@@ -1423,51 +1429,23 @@  static int nl802154_trigger_scan(struct sk_buff *skb, struct genl_info *info)
 		goto free_request;
 	}
 
-	if (info->attrs[NL802154_ATTR_PAGE]) {
+	/* Use current page by default */
+	if (info->attrs[NL802154_ATTR_PAGE])
 		request->page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
-		if (request->page > IEEE802154_MAX_PAGE) {
-			pr_err("Invalid page %d > %d\n",
-			       request->page, IEEE802154_MAX_PAGE);
-			err = -EINVAL;
-			goto free_request;
-		}
-	} else {
-		/* Use current page by default */
+	else
 		request->page = wpan_phy->current_page;
-	}
 
-	if (info->attrs[NL802154_ATTR_SCAN_CHANNELS]) {
+	/* Scan all supported channels by default */
+	if (info->attrs[NL802154_ATTR_SCAN_CHANNELS])
 		request->channels = nla_get_u32(info->attrs[NL802154_ATTR_SCAN_CHANNELS]);
-		if (request->channels >= BIT(IEEE802154_MAX_CHANNEL + 1)) {
-			pr_err("Invalid channels bitfield %x ≥ %lx\n",
-			       request->channels,
-			       BIT(IEEE802154_MAX_CHANNEL + 1));
-			err = -EINVAL;
-			goto free_request;
-		}
-	} else {
-		/* Scan all supported channels by default */
+	else
 		request->channels = wpan_phy->supported.channels[request->page];
-	}
 
-	if (info->attrs[NL802154_ATTR_SCAN_PREAMBLE_CODES] ||
-	    info->attrs[NL802154_ATTR_SCAN_MEAN_PRF]) {
-		pr_err("Preamble codes and mean PRF not supported yet\n");
-		err = -EINVAL;
-		goto free_request;
-	}
-
-	if (info->attrs[NL802154_ATTR_SCAN_DURATION]) {
+	/* Use maximum duration order by default */
+	if (info->attrs[NL802154_ATTR_SCAN_DURATION])
 		request->duration = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_DURATION]);
-		if (request->duration > IEEE802154_MAX_SCAN_DURATION) {
-			pr_err("Duration is out of range\n");
-			err = -EINVAL;
-			goto free_request;
-		}
-	} else {
-		/* Use maximum duration order by default */
+	else
 		request->duration = IEEE802154_MAX_SCAN_DURATION;
-	}
 
 	if (wpan_dev->netdev)
 		dev_hold(wpan_dev->netdev);
@@ -1614,17 +1592,11 @@  nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
 	request->wpan_dev = wpan_dev;
 	request->wpan_phy = wpan_phy;
 
-	if (info->attrs[NL802154_ATTR_BEACON_INTERVAL]) {
+	/* Use maximum duration order by default */
+	if (info->attrs[NL802154_ATTR_BEACON_INTERVAL])
 		request->interval = nla_get_u8(info->attrs[NL802154_ATTR_BEACON_INTERVAL]);
-		if (request->interval > IEEE802154_MAX_SCAN_DURATION) {
-			pr_err("Interval is out of range\n");
-			err = -EINVAL;
-			goto free_request;
-		}
-	} else {
-		/* Use maximum duration order by default */
+	else
 		request->interval = IEEE802154_MAX_SCAN_DURATION;
-	}
 
 	if (wpan_dev->netdev)
 		dev_hold(wpan_dev->netdev);
@@ -1640,7 +1612,7 @@  nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
 free_device:
 	if (wpan_dev->netdev)
 		dev_put(wpan_dev->netdev);
-free_request:
+
 	kfree(request);
 
 	return err;