ieee802154: Add support for user association requests
authorMiquel Raynal <miquel.raynal@bootlin.com>
Wed, 27 Sep 2023 18:12:06 +0000 (20:12 +0200)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 20 Nov 2023 10:42:24 +0000 (11:42 +0100)
Users may decide to associate with a peer, which becomes our parent
coordinator. Let's add the necessary netlink support for this.

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-4-miquel.raynal@bootlin.com
include/net/cfg802154.h
include/net/ieee802154_netdev.h
include/net/nl802154.h
net/ieee802154/nl802154.c
net/ieee802154/rdev-ops.h
net/ieee802154/trace.h

index a89f1c9cea3f5a01f288581bbd9eb5917cdf832e..d0c03317622087258bd99446cdab5711b69ee466 100644 (file)
@@ -20,6 +20,7 @@ struct wpan_phy;
 struct wpan_phy_cca;
 struct cfg802154_scan_request;
 struct cfg802154_beacon_request;
+struct ieee802154_addr;
 
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 struct ieee802154_llsec_device_key;
@@ -77,6 +78,9 @@ struct cfg802154_ops {
                                struct cfg802154_beacon_request *request);
        int     (*stop_beacons)(struct wpan_phy *wpan_phy,
                                struct wpan_dev *wpan_dev);
+       int     (*associate)(struct wpan_phy *wpan_phy,
+                            struct wpan_dev *wpan_dev,
+                            struct ieee802154_addr *coord);
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
        void    (*get_llsec_table)(struct wpan_phy *wpan_phy,
                                   struct wpan_dev *wpan_dev,
index 063313df447da0716d9c001ad0046304ccf22a3d..ca8c827d0d7ff258342ffa326a1f7f60b02c0f6a 100644 (file)
@@ -125,6 +125,30 @@ struct ieee802154_hdr_fc {
 #endif
 };
 
+struct ieee802154_assoc_req_pl {
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+       u8 reserved1:1,
+          device_type:1,
+          power_source:1,
+          rx_on_when_idle:1,
+          assoc_type:1,
+          reserved2:1,
+          security_cap:1,
+          alloc_addr:1;
+#elif defined(__BIG_ENDIAN_BITFIELD)
+       u8 alloc_addr:1,
+          security_cap:1,
+          reserved2:1,
+          assoc_type:1,
+          rx_on_when_idle:1,
+          power_source:1,
+          device_type:1,
+          reserved1:1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+} __packed;
+
 enum ieee802154_frame_version {
        IEEE802154_2003_STD,
        IEEE802154_2006_STD,
@@ -140,6 +164,14 @@ enum ieee802154_addressing_mode {
        IEEE802154_EXTENDED_ADDRESSING,
 };
 
+enum ieee802154_association_status {
+       IEEE802154_ASSOCIATION_SUCCESSFUL = 0x00,
+       IEEE802154_PAN_AT_CAPACITY = 0x01,
+       IEEE802154_PAN_ACCESS_DENIED = 0x02,
+       IEEE802154_HOPPING_SEQUENCE_OFFSET_DUP = 0x03,
+       IEEE802154_FAST_ASSOCIATION_SUCCESSFUL = 0x80,
+};
+
 struct ieee802154_hdr {
        struct ieee802154_hdr_fc fc;
        u8 seq;
@@ -163,6 +195,12 @@ struct ieee802154_beacon_req_frame {
        struct ieee802154_mac_cmd_pl mac_pl;
 };
 
+struct ieee802154_association_req_frame {
+       struct ieee802154_hdr mhr;
+       struct ieee802154_mac_cmd_pl mac_pl;
+       struct ieee802154_assoc_req_pl assoc_req_pl;
+};
+
 /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
  * the contents of hdr will be, and the actual value of those bits in
  * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
index 8cd9d141f5afe09254f32fa3ee6112fa02595a8e..830e1c51d3df2e0175207e3cbfd2e67faf6e2c1a 100644 (file)
@@ -78,6 +78,7 @@ enum nl802154_commands {
        NL802154_CMD_SCAN_DONE,
        NL802154_CMD_SEND_BEACONS,
        NL802154_CMD_STOP_BEACONS,
+       NL802154_CMD_ASSOCIATE,
 
        /* add new commands above here */
 
index 1b6a9f27a6e4bc4b3ae838c867a1f7ab95d11409..e430dbf66d00beac08002eb222a975a18c9fa302 100644 (file)
@@ -1628,6 +1628,41 @@ nl802154_stop_beacons(struct sk_buff *skb, struct genl_info *info)
        return rdev_stop_beacons(rdev, wpan_dev);
 }
 
+static int nl802154_associate(struct sk_buff *skb, struct genl_info *info)
+{
+       struct cfg802154_registered_device *rdev = info->user_ptr[0];
+       struct net_device *dev = info->user_ptr[1];
+       struct wpan_dev *wpan_dev;
+       struct wpan_phy *wpan_phy;
+       struct ieee802154_addr coord;
+       int err;
+
+       wpan_dev = dev->ieee802154_ptr;
+       wpan_phy = &rdev->wpan_phy;
+
+       if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
+               NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
+               return -EOPNOTSUPP;
+       }
+
+       if (!info->attrs[NL802154_ATTR_PAN_ID] ||
+           !info->attrs[NL802154_ATTR_EXTENDED_ADDR])
+               return -EINVAL;
+
+       coord.pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
+       coord.mode = IEEE802154_ADDR_LONG;
+       coord.extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
+
+       mutex_lock(&wpan_dev->association_lock);
+       err = rdev_associate(rdev, wpan_dev, &coord);
+       mutex_unlock(&wpan_dev->association_lock);
+       if (err)
+               pr_err("Association with PAN ID 0x%x failed (%d)\n",
+                      le16_to_cpu(coord.pan_id), err);
+
+       return err;
+}
+
 #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 },
@@ -2749,6 +2784,14 @@ static const struct genl_ops nl802154_ops[] = {
                                  NL802154_FLAG_CHECK_NETDEV_UP |
                                  NL802154_FLAG_NEED_RTNL,
        },
+       {
+               .cmd = NL802154_CMD_ASSOCIATE,
+               .doit = nl802154_associate,
+               .flags = GENL_ADMIN_PERM,
+               .internal_flags = NL802154_FLAG_NEED_NETDEV |
+                                 NL802154_FLAG_CHECK_NETDEV_UP |
+                                 NL802154_FLAG_NEED_RTNL,
+       },
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
        {
                .cmd = NL802154_CMD_SET_SEC_PARAMS,
index 5eaae15c610e658e6b7d9c0262965d689c5b818f..4843d52f1ee09457901c11ed6a640ed85574d8ff 100644 (file)
@@ -265,6 +265,21 @@ static inline int rdev_stop_beacons(struct cfg802154_registered_device *rdev,
        return ret;
 }
 
+static inline int rdev_associate(struct cfg802154_registered_device *rdev,
+                                struct wpan_dev *wpan_dev,
+                                struct ieee802154_addr *coord)
+{
+       int ret;
+
+       if (!rdev->ops->associate)
+               return -EOPNOTSUPP;
+
+       trace_802154_rdev_associate(&rdev->wpan_phy, wpan_dev, coord);
+       ret = rdev->ops->associate(&rdev->wpan_phy, wpan_dev, coord);
+       trace_802154_rdev_return_int(&rdev->wpan_phy, ret);
+       return ret;
+}
+
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 /* TODO this is already a nl802154, so move into ieee802154 */
 static inline void
index c16db0b326fa4f9616e34c25b27f45d0d509f67a..2e1d4f456316952ea76280c6a081bc74716b6a8d 100644 (file)
@@ -356,6 +356,25 @@ DEFINE_EVENT(802154_wdev_template, 802154_rdev_stop_beacons,
        TP_ARGS(wpan_phy, wpan_dev)
 );
 
+TRACE_EVENT(802154_rdev_associate,
+       TP_PROTO(struct wpan_phy *wpan_phy,
+                struct wpan_dev *wpan_dev,
+                struct ieee802154_addr *coord),
+       TP_ARGS(wpan_phy, wpan_dev, coord),
+       TP_STRUCT__entry(
+               WPAN_PHY_ENTRY
+               WPAN_DEV_ENTRY
+               __field(__le64, addr)
+       ),
+       TP_fast_assign(
+               WPAN_PHY_ASSIGN;
+               WPAN_DEV_ASSIGN;
+               __entry->addr = coord->extended_addr;
+       ),
+       TP_printk(WPAN_PHY_PR_FMT ", " WPAN_DEV_PR_FMT ", associating with: 0x%llx",
+                 WPAN_PHY_PR_ARG, WPAN_DEV_PR_ARG, __entry->addr)
+);
+
 TRACE_EVENT(802154_rdev_return_int,
        TP_PROTO(struct wpan_phy *wpan_phy, int ret),
        TP_ARGS(wpan_phy, ret),