Skip to content

Commit 648324c

Browse files
miquelraynalStefan-Schmidt
authored andcommitted
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: ed3557c ("ieee802154: Add support for user scanning requests") Suggested-by: Jakub Kicinski <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Stefan Schmidt <[email protected]>
1 parent 6130543 commit 648324c

File tree

1 file changed

+28
-56
lines changed

1 file changed

+28
-56
lines changed

net/ieee802154/nl802154.c

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
187187

188188
[NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
189189

190-
[NL802154_ATTR_PAGE] = { .type = NLA_U8, },
191-
[NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
190+
[NL802154_ATTR_PAGE] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_PAGE),
191+
[NL802154_ATTR_CHANNEL] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_CHANNEL),
192192

193193
[NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
194194

@@ -221,13 +221,19 @@ static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
221221

222222
[NL802154_ATTR_COORDINATOR] = { .type = NLA_NESTED },
223223

224-
[NL802154_ATTR_SCAN_TYPE] = { .type = NLA_U8 },
225-
[NL802154_ATTR_SCAN_CHANNELS] = { .type = NLA_U32 },
226-
[NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_U64 },
227-
[NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_U8 },
228-
[NL802154_ATTR_SCAN_DURATION] = { .type = NLA_U8 },
229-
[NL802154_ATTR_SCAN_DONE_REASON] = { .type = NLA_U8 },
230-
[NL802154_ATTR_BEACON_INTERVAL] = { .type = NLA_U8 },
224+
[NL802154_ATTR_SCAN_TYPE] =
225+
NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_ED, NL802154_SCAN_RIT_PASSIVE),
226+
[NL802154_ATTR_SCAN_CHANNELS] =
227+
NLA_POLICY_MASK(NLA_U32, GENMASK(IEEE802154_MAX_CHANNEL, 0)),
228+
[NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_REJECT },
229+
[NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_REJECT },
230+
[NL802154_ATTR_SCAN_DURATION] =
231+
NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
232+
[NL802154_ATTR_SCAN_DONE_REASON] =
233+
NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_DONE_REASON_FINISHED,
234+
NL802154_SCAN_DONE_REASON_ABORTED),
235+
[NL802154_ATTR_BEACON_INTERVAL] =
236+
NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
231237

232238
#ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
233239
[NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
@@ -1423,51 +1429,23 @@ static int nl802154_trigger_scan(struct sk_buff *skb, struct genl_info *info)
14231429
goto free_request;
14241430
}
14251431

1426-
if (info->attrs[NL802154_ATTR_PAGE]) {
1432+
/* Use current page by default */
1433+
if (info->attrs[NL802154_ATTR_PAGE])
14271434
request->page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
1428-
if (request->page > IEEE802154_MAX_PAGE) {
1429-
pr_err("Invalid page %d > %d\n",
1430-
request->page, IEEE802154_MAX_PAGE);
1431-
err = -EINVAL;
1432-
goto free_request;
1433-
}
1434-
} else {
1435-
/* Use current page by default */
1435+
else
14361436
request->page = wpan_phy->current_page;
1437-
}
14381437

1439-
if (info->attrs[NL802154_ATTR_SCAN_CHANNELS]) {
1438+
/* Scan all supported channels by default */
1439+
if (info->attrs[NL802154_ATTR_SCAN_CHANNELS])
14401440
request->channels = nla_get_u32(info->attrs[NL802154_ATTR_SCAN_CHANNELS]);
1441-
if (request->channels >= BIT(IEEE802154_MAX_CHANNEL + 1)) {
1442-
pr_err("Invalid channels bitfield %x ≥ %lx\n",
1443-
request->channels,
1444-
BIT(IEEE802154_MAX_CHANNEL + 1));
1445-
err = -EINVAL;
1446-
goto free_request;
1447-
}
1448-
} else {
1449-
/* Scan all supported channels by default */
1441+
else
14501442
request->channels = wpan_phy->supported.channels[request->page];
1451-
}
1452-
1453-
if (info->attrs[NL802154_ATTR_SCAN_PREAMBLE_CODES] ||
1454-
info->attrs[NL802154_ATTR_SCAN_MEAN_PRF]) {
1455-
pr_err("Preamble codes and mean PRF not supported yet\n");
1456-
err = -EINVAL;
1457-
goto free_request;
1458-
}
14591443

1460-
if (info->attrs[NL802154_ATTR_SCAN_DURATION]) {
1444+
/* Use maximum duration order by default */
1445+
if (info->attrs[NL802154_ATTR_SCAN_DURATION])
14611446
request->duration = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_DURATION]);
1462-
if (request->duration > IEEE802154_MAX_SCAN_DURATION) {
1463-
pr_err("Duration is out of range\n");
1464-
err = -EINVAL;
1465-
goto free_request;
1466-
}
1467-
} else {
1468-
/* Use maximum duration order by default */
1447+
else
14691448
request->duration = IEEE802154_MAX_SCAN_DURATION;
1470-
}
14711449

14721450
if (wpan_dev->netdev)
14731451
dev_hold(wpan_dev->netdev);
@@ -1614,17 +1592,11 @@ nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
16141592
request->wpan_dev = wpan_dev;
16151593
request->wpan_phy = wpan_phy;
16161594

1617-
if (info->attrs[NL802154_ATTR_BEACON_INTERVAL]) {
1595+
/* Use maximum duration order by default */
1596+
if (info->attrs[NL802154_ATTR_BEACON_INTERVAL])
16181597
request->interval = nla_get_u8(info->attrs[NL802154_ATTR_BEACON_INTERVAL]);
1619-
if (request->interval > IEEE802154_MAX_SCAN_DURATION) {
1620-
pr_err("Interval is out of range\n");
1621-
err = -EINVAL;
1622-
goto free_request;
1623-
}
1624-
} else {
1625-
/* Use maximum duration order by default */
1598+
else
16261599
request->interval = IEEE802154_MAX_SCAN_DURATION;
1627-
}
16281600

16291601
if (wpan_dev->netdev)
16301602
dev_hold(wpan_dev->netdev);
@@ -1640,7 +1612,7 @@ nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
16401612
free_device:
16411613
if (wpan_dev->netdev)
16421614
dev_put(wpan_dev->netdev);
1643-
free_request:
1615+
16441616
kfree(request);
16451617

16461618
return err;

0 commit comments

Comments
 (0)