ieee802154: Add support for limiting the number of associated devices
authorMiquel Raynal <miquel.raynal@bootlin.com>
Wed, 27 Sep 2023 18:12:11 +0000 (20:12 +0200)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 20 Nov 2023 10:43:11 +0000 (11:43 +0100)
Coordinators may refuse associations. We need a user input for
that. Let's add a new netlink command which can provide a maximum number
of devices we accept to associate with as a first step. Later, we could
also forward the request to userspace and check whether the association
should be accepted or not.

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

index c844ae63bc0407eeef3602806f5a15bbccac4c76..0d3e9af00198b7ac9dd3a8617bb769b4e11c2f26 100644 (file)
@@ -506,6 +506,7 @@ struct wpan_dev {
        struct mutex association_lock;
        struct ieee802154_pan_device *parent;
        struct list_head children;
+       unsigned int max_associations;
 };
 
 #define to_phy(_dev)   container_of(_dev, struct wpan_phy, dev)
@@ -583,6 +584,13 @@ struct ieee802154_pan_device *
 cfg802154_device_is_child(struct wpan_dev *wpan_dev,
                          struct ieee802154_addr *target);
 
+/**
+ * cfg802154_set_max_associations - Limit the number of future associations
+ * @wpan_dev: the wpan device
+ * @max: the maximum number of devices we accept to associate
+ */
+void cfg802154_set_max_associations(struct wpan_dev *wpan_dev, unsigned int max);
+
 /**
  * cfg802154_get_free_short_addr - Get a free address among the known devices
  * @wpan_dev: the wpan device
index 8a47c14c72f0ca3b10dacfe038c9719f2db12af8..8b26faae49e808078a9906dc0840c6027f474d7c 100644 (file)
@@ -80,6 +80,7 @@ enum nl802154_commands {
        NL802154_CMD_STOP_BEACONS,
        NL802154_CMD_ASSOCIATE,
        NL802154_CMD_DISASSOCIATE,
+       NL802154_CMD_SET_MAX_ASSOCIATIONS,
 
        /* add new commands above here */
 
@@ -149,6 +150,7 @@ enum nl802154_attrs {
        NL802154_ATTR_SCAN_DURATION,
        NL802154_ATTR_SCAN_DONE_REASON,
        NL802154_ATTR_BEACON_INTERVAL,
+       NL802154_ATTR_MAX_ASSOCIATIONS,
 
        /* add attributes here, update the policy in nl802154.c */
 
index 1670a71327a7c5baaa45c89d7ecb77773a50743b..2d6fe45efa052f1483cf254ade991ccdbc998b2e 100644 (file)
@@ -295,6 +295,7 @@ static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
                rdev->devlist_generation++;
                mutex_init(&wpan_dev->association_lock);
                INIT_LIST_HEAD(&wpan_dev->children);
+               wpan_dev->max_associations = SZ_16K;
 
                wpan_dev->netdev = dev;
                break;
index dd86ecd7b193b2bd4b069d4d6aec7d25c64ff7bc..06e0bd6ffcca5ce794efedaabacee1a81b1e5bcb 100644 (file)
@@ -234,6 +234,7 @@ static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
                                 NL802154_SCAN_DONE_REASON_ABORTED),
        [NL802154_ATTR_BEACON_INTERVAL] =
                NLA_POLICY_MAX(NLA_U8, IEEE802154_ACTIVE_SCAN_DURATION),
+       [NL802154_ATTR_MAX_ASSOCIATIONS] = { .type = NLA_U32 },
 
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
        [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
@@ -1696,6 +1697,26 @@ static int nl802154_disassociate(struct sk_buff *skb, struct genl_info *info)
        return 0;
 }
 
+static int nl802154_set_max_associations(struct sk_buff *skb, struct genl_info *info)
+{
+       struct net_device *dev = info->user_ptr[1];
+       struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
+       unsigned int max_assoc;
+
+       if (!info->attrs[NL802154_ATTR_MAX_ASSOCIATIONS]) {
+               NL_SET_ERR_MSG(info->extack, "No maximum number of association given");
+               return -EINVAL;
+       }
+
+       max_assoc = nla_get_u32(info->attrs[NL802154_ATTR_MAX_ASSOCIATIONS]);
+
+       mutex_lock(&wpan_dev->association_lock);
+       cfg802154_set_max_associations(wpan_dev, max_assoc);
+       mutex_unlock(&wpan_dev->association_lock);
+
+       return 0;
+}
+
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
        [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
@@ -2833,6 +2854,13 @@ static const struct genl_ops nl802154_ops[] = {
                                  NL802154_FLAG_CHECK_NETDEV_UP |
                                  NL802154_FLAG_NEED_RTNL,
        },
+       {
+               .cmd = NL802154_CMD_SET_MAX_ASSOCIATIONS,
+               .doit = nl802154_set_max_associations,
+               .flags = GENL_ADMIN_PERM,
+               .internal_flags = NL802154_FLAG_NEED_NETDEV |
+                                 NL802154_FLAG_NEED_RTNL,
+       },
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
        {
                .cmd = NL802154_CMD_SET_SEC_PARAMS,
index 545461069197d0bdcf461fb83663b6368432c43b..fb5b0af2ef684d90c9b989336d2eff1b53f1a22e 100644 (file)
@@ -93,3 +93,11 @@ __le16 cfg802154_get_free_short_addr(struct wpan_dev *wpan_dev)
        return addr;
 }
 EXPORT_SYMBOL_GPL(cfg802154_get_free_short_addr);
+
+void cfg802154_set_max_associations(struct wpan_dev *wpan_dev, unsigned int max)
+{
+       lockdep_assert_held(&wpan_dev->association_lock);
+
+       wpan_dev->max_associations = max;
+}
+EXPORT_SYMBOL_GPL(cfg802154_set_max_associations);