From 648324c9b690bcda0ac6f6499370effc0336ee27 Mon Sep 17 00:00:00 2001 From: Miquel Raynal Date: Tue, 14 Feb 2023 14:50:30 +0100 Subject: ieee802154: Use netlink policies when relevant on scan parameters 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 Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/r/20230214135035.1202471-2-miquel.raynal@bootlin.com Signed-off-by: Stefan Schmidt --- net/ieee802154/nl802154.c | 84 ++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 56 deletions(-) diff --git a/net/ieee802154/nl802154.c b/net/ieee802154/nl802154.c index 8661907599e1..3808a8e62a7b 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; -- cgit v1.2.3