ieee802154: Introduce a helper to validate a channel
authorMiquel Raynal <miquel.raynal@bootlin.com>
Tue, 3 Jan 2023 16:56:41 +0000 (17:56 +0100)
committerStefan Schmidt <stefan@datenfreihafen.org>
Tue, 3 Jan 2023 18:36:53 +0000 (19:36 +0100)
This helper for now only checks if the page member and channel member
are valid (in the specification range) and supported (by checking the
device capabilities). Soon two new parameters will be introduced and
having this helper will let us only modify its content rather than
modifying the logic everywhere else in the subsystem.

There is not functional change.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Acked-by: Alexander Aring <aahringo@redhat.com>
Link: https://lore.kernel.org/r/20230103165644.432209-4-miquel.raynal@bootlin.com
Signed-off-by: Stefan Schmidt <stefan@datenfreihafen.org>
include/net/cfg802154.h
net/ieee802154/nl802154.c

index 76d4f95e9974c7fca871fc1897ef4acfc7c35fe7..1184b543fba7543a7fb600739381574bb6d78f2a 100644 (file)
@@ -246,6 +246,17 @@ static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
        write_pnet(&wpan_phy->_net, net);
 }
 
+static inline bool ieee802154_chan_is_valid(struct wpan_phy *phy,
+                                           u8 page, u8 channel)
+{
+       if (page > IEEE802154_MAX_PAGE ||
+           channel > IEEE802154_MAX_CHANNEL ||
+           !(phy->supported.channels[page] & BIT(channel)))
+               return false;
+
+       return true;
+}
+
 /**
  * struct ieee802154_addr - IEEE802.15.4 device address
  * @mode: Address mode from frame header. Can be one of:
index 11aa693af4492942c49521206f96a87d8d3eaa29..0b7a9f16b3b6f741ba031a308c81bfd43930ab29 100644 (file)
@@ -976,8 +976,7 @@ static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
        channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
 
        /* check 802.15.4 constraints */
-       if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
-           !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
+       if (!ieee802154_chan_is_valid(&rdev->wpan_phy, page, channel))
                return -EINVAL;
 
        return rdev_set_channel(rdev, page, channel);