Merge tag 'ieee802154-for-net-next-2022-10-25' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / net / ieee802154 / mac802154_hwsim.c
index 2f0544dd7c2ad918480b4f00c3c24962412687b0..8445c2189d11664ab4910cc8f03e622790ba7b06 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/netdevice.h>
 #include <linux/device.h>
 #include <linux/spinlock.h>
+#include <net/ieee802154_netdev.h>
 #include <net/mac802154.h>
 #include <net/cfg802154.h>
 #include <net/genetlink.h>
@@ -47,6 +48,8 @@ static const struct genl_multicast_group hwsim_mcgrps[] = {
 struct hwsim_pib {
        u8 page;
        u8 channel;
+       struct ieee802154_hw_addr_filt filt;
+       enum ieee802154_filtering_level filt_level;
 
        struct rcu_head rcu;
 };
@@ -88,24 +91,168 @@ static int hwsim_hw_ed(struct ieee802154_hw *hw, u8 *level)
        return 0;
 }
 
-static int hwsim_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
+static int hwsim_update_pib(struct ieee802154_hw *hw, u8 page, u8 channel,
+                           struct ieee802154_hw_addr_filt *filt,
+                           enum ieee802154_filtering_level filt_level)
 {
        struct hwsim_phy *phy = hw->priv;
        struct hwsim_pib *pib, *pib_old;
 
-       pib = kzalloc(sizeof(*pib), GFP_KERNEL);
+       pib = kzalloc(sizeof(*pib), GFP_ATOMIC);
        if (!pib)
                return -ENOMEM;
 
+       pib_old = rtnl_dereference(phy->pib);
+
        pib->page = page;
        pib->channel = channel;
+       pib->filt.short_addr = filt->short_addr;
+       pib->filt.pan_id = filt->pan_id;
+       pib->filt.ieee_addr = filt->ieee_addr;
+       pib->filt.pan_coord = filt->pan_coord;
+       pib->filt_level = filt_level;
 
-       pib_old = rtnl_dereference(phy->pib);
        rcu_assign_pointer(phy->pib, pib);
        kfree_rcu(pib_old, rcu);
        return 0;
 }
 
+static int hwsim_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
+{
+       struct hwsim_phy *phy = hw->priv;
+       struct hwsim_pib *pib;
+       int ret;
+
+       rcu_read_lock();
+       pib = rcu_dereference(phy->pib);
+       ret = hwsim_update_pib(hw, page, channel, &pib->filt, pib->filt_level);
+       rcu_read_unlock();
+
+       return ret;
+}
+
+static int hwsim_hw_addr_filt(struct ieee802154_hw *hw,
+                             struct ieee802154_hw_addr_filt *filt,
+                             unsigned long changed)
+{
+       struct hwsim_phy *phy = hw->priv;
+       struct hwsim_pib *pib;
+       int ret;
+
+       rcu_read_lock();
+       pib = rcu_dereference(phy->pib);
+       ret = hwsim_update_pib(hw, pib->page, pib->channel, filt, pib->filt_level);
+       rcu_read_unlock();
+
+       return ret;
+}
+
+static void hwsim_hw_receive(struct ieee802154_hw *hw, struct sk_buff *skb,
+                            u8 lqi)
+{
+       struct ieee802154_hdr hdr;
+       struct hwsim_phy *phy = hw->priv;
+       struct hwsim_pib *pib;
+
+       rcu_read_lock();
+       pib = rcu_dereference(phy->pib);
+
+       if (!pskb_may_pull(skb, 3)) {
+               dev_dbg(hw->parent, "invalid frame\n");
+               goto drop;
+       }
+
+       memcpy(&hdr, skb->data, 3);
+
+       /* Level 4 filtering: Frame fields validity */
+       if (pib->filt_level == IEEE802154_FILTERING_4_FRAME_FIELDS) {
+               /* a) Drop reserved frame types */
+               switch (mac_cb(skb)->type) {
+               case IEEE802154_FC_TYPE_BEACON:
+               case IEEE802154_FC_TYPE_DATA:
+               case IEEE802154_FC_TYPE_ACK:
+               case IEEE802154_FC_TYPE_MAC_CMD:
+                       break;
+               default:
+                       dev_dbg(hw->parent, "unrecognized frame type 0x%x\n",
+                               mac_cb(skb)->type);
+                       goto drop;
+               }
+
+               /* b) Drop reserved frame versions */
+               switch (hdr.fc.version) {
+               case IEEE802154_2003_STD:
+               case IEEE802154_2006_STD:
+               case IEEE802154_STD:
+                       break;
+               default:
+                       dev_dbg(hw->parent,
+                               "unrecognized frame version 0x%x\n",
+                               hdr.fc.version);
+                       goto drop;
+               }
+
+               /* c) PAN ID constraints */
+               if ((mac_cb(skb)->dest.mode == IEEE802154_ADDR_LONG ||
+                    mac_cb(skb)->dest.mode == IEEE802154_ADDR_SHORT) &&
+                   mac_cb(skb)->dest.pan_id != pib->filt.pan_id &&
+                   mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
+                       dev_dbg(hw->parent,
+                               "unrecognized PAN ID %04x\n",
+                               le16_to_cpu(mac_cb(skb)->dest.pan_id));
+                       goto drop;
+               }
+
+               /* d1) Short address constraints */
+               if (mac_cb(skb)->dest.mode == IEEE802154_ADDR_SHORT &&
+                   mac_cb(skb)->dest.short_addr != pib->filt.short_addr &&
+                   mac_cb(skb)->dest.short_addr != cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
+                       dev_dbg(hw->parent,
+                               "unrecognized short address %04x\n",
+                               le16_to_cpu(mac_cb(skb)->dest.short_addr));
+                       goto drop;
+               }
+
+               /* d2) Extended address constraints */
+               if (mac_cb(skb)->dest.mode == IEEE802154_ADDR_LONG &&
+                   mac_cb(skb)->dest.extended_addr != pib->filt.ieee_addr) {
+                       dev_dbg(hw->parent,
+                               "unrecognized long address 0x%016llx\n",
+                               mac_cb(skb)->dest.extended_addr);
+                       goto drop;
+               }
+
+               /* d4) Specific PAN coordinator case (no parent) */
+               if ((mac_cb(skb)->type == IEEE802154_FC_TYPE_DATA ||
+                    mac_cb(skb)->type == IEEE802154_FC_TYPE_MAC_CMD) &&
+                   mac_cb(skb)->dest.mode == IEEE802154_ADDR_NONE) {
+                       dev_dbg(hw->parent,
+                               "relaying is not supported\n");
+                       goto drop;
+               }
+
+               /* e) Beacon frames follow specific PAN ID rules */
+               if (mac_cb(skb)->type == IEEE802154_FC_TYPE_BEACON &&
+                   pib->filt.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST) &&
+                   mac_cb(skb)->dest.pan_id != pib->filt.pan_id) {
+                       dev_dbg(hw->parent,
+                               "invalid beacon PAN ID %04x\n",
+                               le16_to_cpu(mac_cb(skb)->dest.pan_id));
+                       goto drop;
+               }
+       }
+
+       rcu_read_unlock();
+
+       ieee802154_rx_irqsafe(hw, skb, lqi);
+
+       return;
+
+drop:
+       rcu_read_unlock();
+       kfree_skb(skb);
+}
+
 static int hwsim_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
 {
        struct hwsim_phy *current_phy = hw->priv;
@@ -133,8 +280,7 @@ static int hwsim_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
 
                        einfo = rcu_dereference(e->info);
                        if (newskb)
-                               ieee802154_rx_irqsafe(e->endpoint->hw, newskb,
-                                                     einfo->lqi);
+                               hwsim_hw_receive(e->endpoint->hw, newskb, einfo->lqi);
                }
        }
        rcu_read_unlock();
@@ -148,6 +294,7 @@ static int hwsim_hw_start(struct ieee802154_hw *hw)
        struct hwsim_phy *phy = hw->priv;
 
        phy->suspended = false;
+
        return 0;
 }
 
@@ -161,7 +308,22 @@ static void hwsim_hw_stop(struct ieee802154_hw *hw)
 static int
 hwsim_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
 {
-       return 0;
+       enum ieee802154_filtering_level filt_level;
+       struct hwsim_phy *phy = hw->priv;
+       struct hwsim_pib *pib;
+       int ret;
+
+       if (on)
+               filt_level = IEEE802154_FILTERING_NONE;
+       else
+               filt_level = IEEE802154_FILTERING_4_FRAME_FIELDS;
+
+       rcu_read_lock();
+       pib = rcu_dereference(phy->pib);
+       ret = hwsim_update_pib(hw, pib->page, pib->channel, &pib->filt, filt_level);
+       rcu_read_unlock();
+
+       return ret;
 }
 
 static const struct ieee802154_ops hwsim_ops = {
@@ -172,6 +334,7 @@ static const struct ieee802154_ops hwsim_ops = {
        .start = hwsim_hw_start,
        .stop = hwsim_hw_stop,
        .set_promiscuous_mode = hwsim_set_promiscuous_mode,
+       .set_hw_addr_filt = hwsim_hw_addr_filt,
 };
 
 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
@@ -788,11 +951,13 @@ static int hwsim_add_one(struct genl_info *info, struct device *dev,
        }
 
        pib->channel = 13;
+       pib->filt.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
+       pib->filt.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
        rcu_assign_pointer(phy->pib, pib);
        phy->idx = idx;
        INIT_LIST_HEAD(&phy->edges);
 
-       hw->flags = IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_RX_DROP_BAD_CKSUM;
+       hw->flags = IEEE802154_HW_PROMISCUOUS;
        hw->parent = dev;
 
        err = ieee802154_register_hw(hw);